fix callers of functions returning additional boolean through pointer
[isl.git] / isl_test.c
blob729770301986504ed2b6026acd22e0a899d0ef08
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 #undef BASE
124 #define BASE multi_pw_aff
126 #include "check_reparse_templ.c"
128 #undef BASE
129 #define BASE pw_multi_aff
131 #include "check_reparse_templ.c"
133 /* Test parsing of piecewise multi affine expressions by printing
134 * the expressions and checking that parsing the output results
135 * in the same expression.
136 * Do this for an expression converted from a map with an output
137 * dimension name that is equal to an automatically generated name.
139 static isl_stat test_parse_pma(isl_ctx *ctx)
141 isl_map *map;
142 isl_pw_multi_aff *pma;
144 map = isl_map_read_from_str(ctx, "{ [a, a] -> [i1 = a + 1] }");
145 pma = isl_pw_multi_aff_from_map(map);
146 if (check_reparse_pw_multi_aff(ctx, pma) < 0)
147 return isl_stat_error;
149 return isl_stat_ok;
152 /* String descriptions of multi piecewise affine expressions
153 * that are used for testing printing and parsing.
155 const char *parse_multi_mpa_tests[] = {
156 "{ A[x, y] -> [] : x + y >= 0 }",
157 "{ A[x, y] -> B[] : x + y >= 0 }",
158 "{ A[x, y] -> [x] : x + y >= 0 }",
159 "[N] -> { A[x, y] -> [x] : x + y <= N }",
160 "{ A[x, y] -> [x, y] : x + y >= 0 }",
161 "{ A[x, y] -> [(x : x >= 0), (y : y >= 0)] : x + y >= 0 }",
162 "[N] -> { [] : N >= 0 }",
163 "[N] -> { [] : N >= 0 }",
164 "[N] -> { [N] : N >= 0 }",
165 "[N] -> { [N, N + 1] : N >= 0 }",
166 "[N, M] -> { [(N : N >= 0), (M : M >= 0)] : N + M >= 0 }",
169 /* Test parsing of multi piecewise affine expressions by printing
170 * the expressions and checking that parsing the output results
171 * in the same expression.
172 * Do this for a couple of manually constructed expressions,
173 * an expression converted from a map with an output dimension name
174 * that is equal to an automatically generated name, and
175 * a set of expressions parsed from strings.
177 static int test_parse_mpa(isl_ctx *ctx)
179 int i;
180 isl_space *space;
181 isl_set *dom;
182 isl_map *map;
183 isl_pw_multi_aff *pma;
184 isl_multi_pw_aff *mpa;
185 isl_stat r;
187 space = isl_space_set_alloc(ctx, 0, 0);
188 space = isl_space_set_tuple_name(space, isl_dim_set, "A");
189 mpa = isl_multi_pw_aff_zero(space);
190 r = check_reparse_multi_pw_aff(ctx, mpa);
191 if (r < 0)
192 return -1;
194 space = isl_space_set_alloc(ctx, 1, 0);
195 space = isl_space_set_dim_name(space, isl_dim_param, 0, "N");
196 space = isl_space_set_tuple_name(space, isl_dim_set, "A");
197 dom = isl_set_universe(isl_space_params(isl_space_copy(space)));
198 dom = isl_set_lower_bound_si(dom, isl_dim_param, 0, 5);
199 mpa = isl_multi_pw_aff_zero(space);
200 mpa = isl_multi_pw_aff_intersect_domain(mpa, dom);
201 r = check_reparse_multi_pw_aff(ctx, mpa);
202 if (r < 0)
203 return -1;
205 map = isl_map_read_from_str(ctx, "{ [a, a] -> [i1 = a + 1] }");
206 pma = isl_pw_multi_aff_from_map(map);
207 mpa = isl_multi_pw_aff_from_pw_multi_aff(pma);
208 if (check_reparse_multi_pw_aff(ctx, mpa) < 0)
209 return -1;
211 for (i = 0; i < ARRAY_SIZE(parse_multi_mpa_tests); ++i) {
212 const char *str;
214 str = parse_multi_mpa_tests[i];
215 mpa = isl_multi_pw_aff_read_from_str(ctx, str);
216 r = check_reparse_multi_pw_aff(ctx, mpa);
217 if (r < 0)
218 return -1;
221 return 0;
225 #undef BASE
226 #define BASE multi_union_pw_aff
228 #include "check_reparse_templ.c"
230 /* String descriptions of multi union piecewise affine expressions
231 * that are used for testing printing and parsing.
233 const char *parse_multi_mupa_tests[] = {
234 "[]",
235 "A[]",
236 "A[B[] -> C[]]",
237 "(A[] : { S[x] : x > 0; T[y] : y >= 0 })",
238 "(A[] : { })",
239 "[N] -> (A[] : { })",
240 "[N] -> (A[] : { : N >= 0 })",
241 "[N] -> (A[] : { S[x] : x > N; T[y] : y >= 0 })",
242 "(A[] : [N] -> { S[x] : x > N; T[y] : y >= 0 })",
243 "A[{ S[x] -> [x + 1]; T[x] -> [x] }]",
244 "(A[{ S[x] -> [x + 1]; T[x] -> [x] }] : "
245 "{ S[x] : x > 0; T[y] : y >= 0 })",
248 /* Test parsing of multi union piecewise affine expressions by printing
249 * the expressions and checking that parsing the output results
250 * in the same expression.
251 * Do this for a couple of manually constructed expressions and
252 * a set of expressions parsed from strings.
254 static int test_parse_mupa(isl_ctx *ctx)
256 int i;
257 isl_space *space;
258 isl_multi_union_pw_aff *mupa;
259 isl_set *dom;
260 isl_union_set *uset;
261 isl_stat r;
263 space = isl_space_set_alloc(ctx, 0, 0);
264 space = isl_space_set_tuple_name(space, isl_dim_set, "A");
265 mupa = isl_multi_union_pw_aff_zero(space);
266 r = check_reparse_multi_union_pw_aff(ctx, mupa);
267 if (r < 0)
268 return -1;
270 space = isl_space_set_alloc(ctx, 1, 0);
271 space = isl_space_set_dim_name(space, isl_dim_param, 0, "N");
272 space = isl_space_set_tuple_name(space, isl_dim_set, "A");
273 dom = isl_set_universe(space);
274 dom = isl_set_lower_bound_si(dom, isl_dim_param, 0, 5);
275 uset = isl_union_set_from_set(dom);
276 space = isl_space_set_alloc(ctx, 1, 0);
277 space = isl_space_set_dim_name(space, isl_dim_param, 0, "N");
278 space = isl_space_set_tuple_name(space, isl_dim_set, "B");
279 mupa = isl_multi_union_pw_aff_zero(space);
280 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, uset);
281 r = check_reparse_multi_union_pw_aff(ctx, mupa);
282 if (r < 0)
283 return -1;
285 for (i = 0; i < ARRAY_SIZE(parse_multi_mupa_tests); ++i) {
286 const char *str;
288 str = parse_multi_mupa_tests[i];
289 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
290 r = check_reparse_multi_union_pw_aff(ctx, mupa);
291 if (r < 0)
292 return -1;
295 return 0;
298 /* Test parsing of multi expressions.
300 static int test_parse_multi(isl_ctx *ctx)
302 if (test_parse_mpa(ctx) < 0)
303 return -1;
304 if (test_parse_mupa(ctx) < 0)
305 return -1;
307 return 0;
310 /* Pairs of binary relation representations that should represent
311 * the same binary relations.
313 struct {
314 const char *map1;
315 const char *map2;
316 } parse_map_equal_tests[] = {
317 { "{ [x,y] : [([x/2]+y)/3] >= 1 }",
318 "{ [x, y] : 2y >= 6 - x }" },
319 { "{ [x,y] : x <= min(y, 2*y+3) }",
320 "{ [x,y] : x <= y, 2*y + 3 }" },
321 { "{ [x,y] : x >= min(y, 2*y+3) }",
322 "{ [x, y] : (y <= x and y >= -3) or (2y <= -3 + x and y <= -4) }" },
323 { "[n] -> { [c1] : c1>=0 and c1<=floord(n-4,3) }",
324 "[n] -> { [c1] : c1 >= 0 and 3c1 <= -4 + n }" },
325 { "{ [i,j] -> [i] : i < j; [i,j] -> [j] : j <= i }",
326 "{ [i,j] -> [min(i,j)] }" },
327 { "{ [i,j] : i != j }",
328 "{ [i,j] : i < j or i > j }" },
329 { "{ [i,j] : (i+1)*2 >= j }",
330 "{ [i, j] : j <= 2 + 2i }" },
331 { "{ [i] -> [i > 0 ? 4 : 5] }",
332 "{ [i] -> [5] : i <= 0; [i] -> [4] : i >= 1 }" },
333 { "[N=2,M] -> { [i=[(M+N)/4]] }",
334 "[N, M] -> { [i] : N = 2 and 4i <= 2 + M and 4i >= -1 + M }" },
335 { "{ [x] : x >= 0 }",
336 "{ [x] : x-0 >= 0 }" },
337 { "{ [i] : ((i > 10)) }",
338 "{ [i] : i >= 11 }" },
339 { "{ [i] -> [0] }",
340 "{ [i] -> [0 * i] }" },
341 { "{ [a] -> [b] : (not false) }",
342 "{ [a] -> [b] : true }" },
343 { "{ [i] : i/2 <= 5 }",
344 "{ [i] : i <= 10 }" },
345 { "{Sym=[n] [i] : i <= n }",
346 "[n] -> { [i] : i <= n }" },
347 { "{ [*] }",
348 "{ [a] }" },
349 { "{ [i] : 2*floor(i/2) = i }",
350 "{ [i] : exists a : i = 2 a }" },
351 { "{ [a] -> [b] : a = 5 implies b = 5 }",
352 "{ [a] -> [b] : a != 5 or b = 5 }" },
353 { "{ [a] -> [a - 1 : a > 0] }",
354 "{ [a] -> [a - 1] : a > 0 }" },
355 { "{ [a] -> [a - 1 : a > 0; a : a <= 0] }",
356 "{ [a] -> [a - 1] : a > 0; [a] -> [a] : a <= 0 }" },
357 { "{ [a] -> [(a) * 2 : a >= 0; 0 : a < 0] }",
358 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }" },
359 { "{ [a] -> [(a * 2) : a >= 0; 0 : a < 0] }",
360 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }" },
361 { "{ [a] -> [(a * 2 : a >= 0); 0 : a < 0] }",
362 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }" },
363 { "{ [a] -> [(a * 2 : a >= 0; 0 : a < 0)] }",
364 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }" },
365 { "{ [a,b] -> [i,j] : a,b << i,j }",
366 "{ [a,b] -> [i,j] : a < i or (a = i and b < j) }" },
367 { "{ [a,b] -> [i,j] : a,b <<= i,j }",
368 "{ [a,b] -> [i,j] : a < i or (a = i and b <= j) }" },
369 { "{ [a,b] -> [i,j] : a,b >> i,j }",
370 "{ [a,b] -> [i,j] : a > i or (a = i and b > j) }" },
371 { "{ [a,b] -> [i,j] : a,b >>= i,j }",
372 "{ [a,b] -> [i,j] : a > i or (a = i and b >= j) }" },
373 { "{ [n] -> [i] : exists (a, b, c: 8b <= i - 32a and "
374 "8b >= -7 + i - 32 a and b >= 0 and b <= 3 and "
375 "8c < n - 32a and i < n and c >= 0 and "
376 "c <= 3 and c >= -4a) }",
377 "{ [n] -> [i] : 0 <= i < n }" },
378 { "{ [x] -> [] : exists (a, b: 0 <= a <= 1 and 0 <= b <= 3 and "
379 "2b <= x - 8a and 2b >= -1 + x - 8a) }",
380 "{ [x] -> [] : 0 <= x <= 15 }" },
381 { "{ [x] -> [x] : }",
382 "{ [x] -> [x] }" },
385 int test_parse(struct isl_ctx *ctx)
387 int i;
388 isl_map *map, *map2;
389 const char *str, *str2;
391 if (test_parse_multi_val(ctx, "{ A[B[2] -> C[5, 7]] }") < 0)
392 return -1;
393 if (test_parse_multi_val(ctx, "[n] -> { [2] }") < 0)
394 return -1;
395 if (test_parse_multi_val(ctx, "{ A[4, infty, NaN, -1/2, 2/3] }") < 0)
396 return -1;
397 if (test_parse_multi(ctx) < 0)
398 return -1;
399 if (test_parse_pma(ctx) < 0)
400 return -1;
402 str = "{ [i] -> [-i] }";
403 map = isl_map_read_from_str(ctx, str);
404 assert(map);
405 isl_map_free(map);
407 str = "{ A[i] -> L[([i/3])] }";
408 map = isl_map_read_from_str(ctx, str);
409 assert(map);
410 isl_map_free(map);
412 test_parse_map(ctx, "{[[s] -> A[i]] -> [[s+1] -> A[i]]}");
413 test_parse_map(ctx, "{ [p1, y1, y2] -> [2, y1, y2] : "
414 "p1 = 1 && (y1 <= y2 || y2 = 0) }");
416 for (i = 0; i < ARRAY_SIZE(parse_map_equal_tests); ++i) {
417 str = parse_map_equal_tests[i].map1;
418 str2 = parse_map_equal_tests[i].map2;
419 if (test_parse_map_equal(ctx, str, str2) < 0)
420 return -1;
423 str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
424 map = isl_map_read_from_str(ctx, str);
425 str = "{ [new, old] -> [o0, o1] : "
426 "exists (e0 = [(-1 - new + o0)/2], e1 = [(-1 - old + o1)/2]: "
427 "2e0 = -1 - new + o0 and 2e1 = -1 - old + o1 and o0 >= 0 and "
428 "o0 <= 1 and o1 >= 0 and o1 <= 1) }";
429 map2 = isl_map_read_from_str(ctx, str);
430 assert(isl_map_is_equal(map, map2));
431 isl_map_free(map);
432 isl_map_free(map2);
434 str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
435 map = isl_map_read_from_str(ctx, str);
436 str = "{[new,old] -> [(new+1)%2,(old+1)%2]}";
437 map2 = isl_map_read_from_str(ctx, str);
438 assert(isl_map_is_equal(map, map2));
439 isl_map_free(map);
440 isl_map_free(map2);
442 test_parse_pwqp(ctx, "{ [i] -> i + [ (i + [i/3])/2 ] }");
443 test_parse_map(ctx, "{ S1[i] -> [([i/10]),i%10] : 0 <= i <= 45 }");
444 test_parse_pwaff(ctx, "{ [i] -> [i + 1] : i > 0; [a] -> [a] : a < 0 }");
445 test_parse_pwqp(ctx, "{ [x] -> ([(x)/2] * [(x)/3]) }");
446 test_parse_pwaff(ctx, "{ [] -> [(100)] }");
448 return 0;
451 static int test_read(isl_ctx *ctx)
453 char *filename;
454 FILE *input;
455 isl_basic_set *bset1, *bset2;
456 const char *str = "{[y]: Exists ( alpha : 2alpha = y)}";
457 int equal;
459 filename = get_filename(ctx, "set", "omega");
460 assert(filename);
461 input = fopen(filename, "r");
462 assert(input);
464 bset1 = isl_basic_set_read_from_file(ctx, input);
465 bset2 = isl_basic_set_read_from_str(ctx, str);
467 equal = isl_basic_set_is_equal(bset1, bset2);
469 isl_basic_set_free(bset1);
470 isl_basic_set_free(bset2);
471 free(filename);
473 fclose(input);
475 if (equal < 0)
476 return -1;
477 if (!equal)
478 isl_die(ctx, isl_error_unknown,
479 "read sets not equal", return -1);
481 return 0;
484 static int test_bounded(isl_ctx *ctx)
486 isl_set *set;
487 isl_bool bounded;
489 set = isl_set_read_from_str(ctx, "[n] -> {[i] : 0 <= i <= n }");
490 bounded = isl_set_is_bounded(set);
491 isl_set_free(set);
493 if (bounded < 0)
494 return -1;
495 if (!bounded)
496 isl_die(ctx, isl_error_unknown,
497 "set not considered bounded", return -1);
499 set = isl_set_read_from_str(ctx, "{[n, i] : 0 <= i <= n }");
500 bounded = isl_set_is_bounded(set);
501 assert(!bounded);
502 isl_set_free(set);
504 if (bounded < 0)
505 return -1;
506 if (bounded)
507 isl_die(ctx, isl_error_unknown,
508 "set considered bounded", return -1);
510 set = isl_set_read_from_str(ctx, "[n] -> {[i] : i <= n }");
511 bounded = isl_set_is_bounded(set);
512 isl_set_free(set);
514 if (bounded < 0)
515 return -1;
516 if (bounded)
517 isl_die(ctx, isl_error_unknown,
518 "set considered bounded", return -1);
520 return 0;
523 /* Construct the basic set { [i] : 5 <= i <= N } */
524 static int test_construction_1(isl_ctx *ctx)
526 isl_space *dim;
527 isl_local_space *ls;
528 isl_basic_set *bset;
529 isl_constraint *c;
531 dim = isl_space_set_alloc(ctx, 1, 1);
532 bset = isl_basic_set_universe(isl_space_copy(dim));
533 ls = isl_local_space_from_space(dim);
535 c = isl_constraint_alloc_inequality(isl_local_space_copy(ls));
536 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
537 c = isl_constraint_set_coefficient_si(c, isl_dim_param, 0, 1);
538 bset = isl_basic_set_add_constraint(bset, c);
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_constant_si(c, -5);
543 bset = isl_basic_set_add_constraint(bset, c);
545 isl_local_space_free(ls);
546 isl_basic_set_free(bset);
548 return 0;
551 /* Construct the basic set { [x] : -100 <= x <= 100 }
552 * using isl_basic_set_{lower,upper}_bound_val and
553 * check that it is equal the same basic set parsed from a string.
555 static int test_construction_2(isl_ctx *ctx)
557 isl_bool equal;
558 isl_val *v;
559 isl_space *space;
560 isl_basic_set *bset1, *bset2;
562 v = isl_val_int_from_si(ctx, 100);
563 space = isl_space_set_alloc(ctx, 0, 1);
564 bset1 = isl_basic_set_universe(space);
565 bset1 = isl_basic_set_upper_bound_val(bset1, isl_dim_set, 0,
566 isl_val_copy(v));
567 bset1 = isl_basic_set_lower_bound_val(bset1, isl_dim_set, 0,
568 isl_val_neg(v));
569 bset2 = isl_basic_set_read_from_str(ctx, "{ [x] : -100 <= x <= 100 }");
570 equal = isl_basic_set_is_equal(bset1, bset2);
571 isl_basic_set_free(bset1);
572 isl_basic_set_free(bset2);
574 if (equal < 0)
575 return -1;
576 if (!equal)
577 isl_die(ctx, isl_error_unknown,
578 "failed construction", return -1);
580 return 0;
583 /* Basic tests for constructing basic sets.
585 static int test_construction(isl_ctx *ctx)
587 if (test_construction_1(ctx) < 0)
588 return -1;
589 if (test_construction_2(ctx) < 0)
590 return -1;
591 return 0;
594 static int test_dim(isl_ctx *ctx)
596 const char *str;
597 isl_map *map1, *map2;
598 int equal;
600 map1 = isl_map_read_from_str(ctx,
601 "[n] -> { [i] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
602 map1 = isl_map_add_dims(map1, isl_dim_in, 1);
603 map2 = isl_map_read_from_str(ctx,
604 "[n] -> { [i,k] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
605 equal = isl_map_is_equal(map1, map2);
606 isl_map_free(map2);
608 map1 = isl_map_project_out(map1, isl_dim_in, 0, 1);
609 map2 = isl_map_read_from_str(ctx, "[n] -> { [i] -> [j] : n >= 0 }");
610 if (equal >= 0 && equal)
611 equal = isl_map_is_equal(map1, map2);
613 isl_map_free(map1);
614 isl_map_free(map2);
616 if (equal < 0)
617 return -1;
618 if (!equal)
619 isl_die(ctx, isl_error_unknown,
620 "unexpected result", return -1);
622 str = "[n] -> { [i] -> [] : exists a : 0 <= i <= n and i = 2 a }";
623 map1 = isl_map_read_from_str(ctx, str);
624 str = "{ [i] -> [j] : exists a : 0 <= i <= j and i = 2 a }";
625 map2 = isl_map_read_from_str(ctx, str);
626 map1 = isl_map_move_dims(map1, isl_dim_out, 0, isl_dim_param, 0, 1);
627 equal = isl_map_is_equal(map1, map2);
628 isl_map_free(map1);
629 isl_map_free(map2);
631 if (equal < 0)
632 return -1;
633 if (!equal)
634 isl_die(ctx, isl_error_unknown,
635 "unexpected result", return -1);
637 return 0;
640 /* Check that "val" is equal to the value described by "str".
641 * If "str" is "NaN", then check for a NaN value explicitly.
643 static isl_stat val_check_equal(__isl_keep isl_val *val, const char *str)
645 isl_bool ok, is_nan;
646 isl_ctx *ctx;
647 isl_val *res;
649 if (!val)
650 return isl_stat_error;
652 ctx = isl_val_get_ctx(val);
653 res = isl_val_read_from_str(ctx, str);
654 is_nan = isl_val_is_nan(res);
655 if (is_nan < 0)
656 ok = isl_bool_error;
657 else if (is_nan)
658 ok = isl_val_is_nan(val);
659 else
660 ok = isl_val_eq(val, res);
661 isl_val_free(res);
662 if (ok < 0)
663 return isl_stat_error;
664 if (!ok)
665 isl_die(ctx, isl_error_unknown,
666 "unexpected result", return isl_stat_error);
667 return isl_stat_ok;
670 struct {
671 __isl_give isl_val *(*op)(__isl_take isl_val *v);
672 const char *arg;
673 const char *res;
674 } val_un_tests[] = {
675 { &isl_val_neg, "0", "0" },
676 { &isl_val_abs, "0", "0" },
677 { &isl_val_pow2, "0", "1" },
678 { &isl_val_floor, "0", "0" },
679 { &isl_val_ceil, "0", "0" },
680 { &isl_val_neg, "1", "-1" },
681 { &isl_val_neg, "-1", "1" },
682 { &isl_val_neg, "1/2", "-1/2" },
683 { &isl_val_neg, "-1/2", "1/2" },
684 { &isl_val_neg, "infty", "-infty" },
685 { &isl_val_neg, "-infty", "infty" },
686 { &isl_val_neg, "NaN", "NaN" },
687 { &isl_val_abs, "1", "1" },
688 { &isl_val_abs, "-1", "1" },
689 { &isl_val_abs, "1/2", "1/2" },
690 { &isl_val_abs, "-1/2", "1/2" },
691 { &isl_val_abs, "infty", "infty" },
692 { &isl_val_abs, "-infty", "infty" },
693 { &isl_val_abs, "NaN", "NaN" },
694 { &isl_val_floor, "1", "1" },
695 { &isl_val_floor, "-1", "-1" },
696 { &isl_val_floor, "1/2", "0" },
697 { &isl_val_floor, "-1/2", "-1" },
698 { &isl_val_floor, "infty", "infty" },
699 { &isl_val_floor, "-infty", "-infty" },
700 { &isl_val_floor, "NaN", "NaN" },
701 { &isl_val_ceil, "1", "1" },
702 { &isl_val_ceil, "-1", "-1" },
703 { &isl_val_ceil, "1/2", "1" },
704 { &isl_val_ceil, "-1/2", "0" },
705 { &isl_val_ceil, "infty", "infty" },
706 { &isl_val_ceil, "-infty", "-infty" },
707 { &isl_val_ceil, "NaN", "NaN" },
708 { &isl_val_pow2, "-3", "1/8" },
709 { &isl_val_pow2, "-1", "1/2" },
710 { &isl_val_pow2, "1", "2" },
711 { &isl_val_pow2, "2", "4" },
712 { &isl_val_pow2, "3", "8" },
713 { &isl_val_inv, "1", "1" },
714 { &isl_val_inv, "2", "1/2" },
715 { &isl_val_inv, "1/2", "2" },
716 { &isl_val_inv, "-2", "-1/2" },
717 { &isl_val_inv, "-1/2", "-2" },
718 { &isl_val_inv, "0", "NaN" },
719 { &isl_val_inv, "NaN", "NaN" },
720 { &isl_val_inv, "infty", "0" },
721 { &isl_val_inv, "-infty", "0" },
724 /* Perform some basic tests of unary operations on isl_val objects.
726 static int test_un_val(isl_ctx *ctx)
728 int i;
729 isl_val *v;
730 __isl_give isl_val *(*fn)(__isl_take isl_val *v);
732 for (i = 0; i < ARRAY_SIZE(val_un_tests); ++i) {
733 isl_stat r;
735 v = isl_val_read_from_str(ctx, val_un_tests[i].arg);
736 fn = val_un_tests[i].op;
737 v = fn(v);
738 r = val_check_equal(v, val_un_tests[i].res);
739 isl_val_free(v);
740 if (r < 0)
741 return -1;
744 return 0;
747 struct {
748 __isl_give isl_val *(*fn)(__isl_take isl_val *v1,
749 __isl_take isl_val *v2);
750 } val_bin_op[] = {
751 ['+'] = { &isl_val_add },
752 ['-'] = { &isl_val_sub },
753 ['*'] = { &isl_val_mul },
754 ['/'] = { &isl_val_div },
755 ['g'] = { &isl_val_gcd },
756 ['m'] = { &isl_val_min },
757 ['M'] = { &isl_val_max },
760 struct {
761 const char *arg1;
762 unsigned char op;
763 const char *arg2;
764 const char *res;
765 } val_bin_tests[] = {
766 { "0", '+', "0", "0" },
767 { "1", '+', "0", "1" },
768 { "1", '+', "1", "2" },
769 { "1", '-', "1", "0" },
770 { "1", '*', "1", "1" },
771 { "1", '/', "1", "1" },
772 { "2", '*', "3", "6" },
773 { "2", '*', "1/2", "1" },
774 { "2", '*', "1/3", "2/3" },
775 { "2/3", '*', "3/5", "2/5" },
776 { "2/3", '*', "7/5", "14/15" },
777 { "2", '/', "1/2", "4" },
778 { "-2", '/', "-1/2", "4" },
779 { "-2", '/', "1/2", "-4" },
780 { "2", '/', "-1/2", "-4" },
781 { "2", '/', "2", "1" },
782 { "2", '/', "3", "2/3" },
783 { "2/3", '/', "5/3", "2/5" },
784 { "2/3", '/', "5/7", "14/15" },
785 { "0", '/', "0", "NaN" },
786 { "42", '/', "0", "NaN" },
787 { "-42", '/', "0", "NaN" },
788 { "infty", '/', "0", "NaN" },
789 { "-infty", '/', "0", "NaN" },
790 { "NaN", '/', "0", "NaN" },
791 { "0", '/', "NaN", "NaN" },
792 { "42", '/', "NaN", "NaN" },
793 { "-42", '/', "NaN", "NaN" },
794 { "infty", '/', "NaN", "NaN" },
795 { "-infty", '/', "NaN", "NaN" },
796 { "NaN", '/', "NaN", "NaN" },
797 { "0", '/', "infty", "0" },
798 { "42", '/', "infty", "0" },
799 { "-42", '/', "infty", "0" },
800 { "infty", '/', "infty", "NaN" },
801 { "-infty", '/', "infty", "NaN" },
802 { "NaN", '/', "infty", "NaN" },
803 { "0", '/', "-infty", "0" },
804 { "42", '/', "-infty", "0" },
805 { "-42", '/', "-infty", "0" },
806 { "infty", '/', "-infty", "NaN" },
807 { "-infty", '/', "-infty", "NaN" },
808 { "NaN", '/', "-infty", "NaN" },
809 { "1", '-', "1/3", "2/3" },
810 { "1/3", '+', "1/2", "5/6" },
811 { "1/2", '+', "1/2", "1" },
812 { "3/4", '-', "1/4", "1/2" },
813 { "1/2", '-', "1/3", "1/6" },
814 { "infty", '+', "42", "infty" },
815 { "infty", '+', "infty", "infty" },
816 { "42", '+', "infty", "infty" },
817 { "infty", '-', "infty", "NaN" },
818 { "infty", '*', "infty", "infty" },
819 { "infty", '*', "-infty", "-infty" },
820 { "-infty", '*', "infty", "-infty" },
821 { "-infty", '*', "-infty", "infty" },
822 { "0", '*', "infty", "NaN" },
823 { "1", '*', "infty", "infty" },
824 { "infty", '*', "0", "NaN" },
825 { "infty", '*', "42", "infty" },
826 { "42", '-', "infty", "-infty" },
827 { "infty", '+', "-infty", "NaN" },
828 { "4", 'g', "6", "2" },
829 { "5", 'g', "6", "1" },
830 { "42", 'm', "3", "3" },
831 { "42", 'M', "3", "42" },
832 { "3", 'm', "42", "3" },
833 { "3", 'M', "42", "42" },
834 { "42", 'm', "infty", "42" },
835 { "42", 'M', "infty", "infty" },
836 { "42", 'm', "-infty", "-infty" },
837 { "42", 'M', "-infty", "42" },
838 { "42", 'm', "NaN", "NaN" },
839 { "42", 'M', "NaN", "NaN" },
840 { "infty", 'm', "-infty", "-infty" },
841 { "infty", 'M', "-infty", "infty" },
844 /* Perform some basic tests of binary operations on isl_val objects.
846 static int test_bin_val(isl_ctx *ctx)
848 int i;
849 isl_val *v1, *v2, *res;
850 __isl_give isl_val *(*fn)(__isl_take isl_val *v1,
851 __isl_take isl_val *v2);
852 int ok;
854 for (i = 0; i < ARRAY_SIZE(val_bin_tests); ++i) {
855 v1 = isl_val_read_from_str(ctx, val_bin_tests[i].arg1);
856 v2 = isl_val_read_from_str(ctx, val_bin_tests[i].arg2);
857 res = isl_val_read_from_str(ctx, val_bin_tests[i].res);
858 fn = val_bin_op[val_bin_tests[i].op].fn;
859 v1 = fn(v1, v2);
860 if (isl_val_is_nan(res))
861 ok = isl_val_is_nan(v1);
862 else
863 ok = isl_val_eq(v1, res);
864 isl_val_free(v1);
865 isl_val_free(res);
866 if (ok < 0)
867 return -1;
868 if (!ok)
869 isl_die(ctx, isl_error_unknown,
870 "unexpected result", return -1);
873 return 0;
876 /* Perform some basic tests on isl_val objects.
878 static int test_val(isl_ctx *ctx)
880 if (test_un_val(ctx) < 0)
881 return -1;
882 if (test_bin_val(ctx) < 0)
883 return -1;
884 return 0;
887 /* Sets described using existentially quantified variables that
888 * can also be described without.
890 static const char *elimination_tests[] = {
891 "{ [i,j] : 2 * [i/2] + 3 * [j/4] <= 10 and 2 i = j }",
892 "{ [m, w] : exists a : w - 2m - 5 <= 3a <= m - 2w }",
893 "{ [m, w] : exists a : w >= 0 and a < m and -1 + w <= a <= 2m - w }",
896 /* Check that redundant existentially quantified variables are
897 * getting removed.
899 static int test_elimination(isl_ctx *ctx)
901 int i;
902 isl_size n;
903 isl_basic_set *bset;
905 for (i = 0; i < ARRAY_SIZE(elimination_tests); ++i) {
906 bset = isl_basic_set_read_from_str(ctx, elimination_tests[i]);
907 n = isl_basic_set_dim(bset, isl_dim_div);
908 isl_basic_set_free(bset);
909 if (n < 0)
910 return -1;
911 if (n != 0)
912 isl_die(ctx, isl_error_unknown,
913 "expecting no existentials", return -1);
916 return 0;
919 static int test_div(isl_ctx *ctx)
921 const char *str;
922 int empty;
923 isl_space *dim;
924 isl_set *set;
925 isl_local_space *ls;
926 struct isl_basic_set *bset;
927 struct isl_constraint *c;
929 /* test 1 */
930 dim = isl_space_set_alloc(ctx, 0, 3);
931 bset = isl_basic_set_universe(isl_space_copy(dim));
932 ls = isl_local_space_from_space(dim);
934 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
935 c = isl_constraint_set_constant_si(c, -1);
936 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
937 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, 3);
938 bset = isl_basic_set_add_constraint(bset, c);
940 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
941 c = isl_constraint_set_constant_si(c, 1);
942 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
943 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, 3);
944 bset = isl_basic_set_add_constraint(bset, c);
946 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
948 assert(bset && bset->n_div == 1);
949 isl_local_space_free(ls);
950 isl_basic_set_free(bset);
952 /* test 2 */
953 dim = isl_space_set_alloc(ctx, 0, 3);
954 bset = isl_basic_set_universe(isl_space_copy(dim));
955 ls = isl_local_space_from_space(dim);
957 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
958 c = isl_constraint_set_constant_si(c, 1);
959 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
960 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, 3);
961 bset = isl_basic_set_add_constraint(bset, c);
963 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
964 c = isl_constraint_set_constant_si(c, -1);
965 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
966 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, 3);
967 bset = isl_basic_set_add_constraint(bset, c);
969 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
971 assert(bset && bset->n_div == 1);
972 isl_local_space_free(ls);
973 isl_basic_set_free(bset);
975 /* test 3 */
976 dim = isl_space_set_alloc(ctx, 0, 3);
977 bset = isl_basic_set_universe(isl_space_copy(dim));
978 ls = isl_local_space_from_space(dim);
980 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
981 c = isl_constraint_set_constant_si(c, 1);
982 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
983 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, 3);
984 bset = isl_basic_set_add_constraint(bset, c);
986 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
987 c = isl_constraint_set_constant_si(c, -3);
988 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
989 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, 4);
990 bset = isl_basic_set_add_constraint(bset, c);
992 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
994 assert(bset && bset->n_div == 1);
995 isl_local_space_free(ls);
996 isl_basic_set_free(bset);
998 /* test 4 */
999 dim = isl_space_set_alloc(ctx, 0, 3);
1000 bset = isl_basic_set_universe(isl_space_copy(dim));
1001 ls = isl_local_space_from_space(dim);
1003 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1004 c = isl_constraint_set_constant_si(c, 2);
1005 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
1006 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, 3);
1007 bset = isl_basic_set_add_constraint(bset, c);
1009 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1010 c = isl_constraint_set_constant_si(c, -1);
1011 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
1012 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, 6);
1013 bset = isl_basic_set_add_constraint(bset, c);
1015 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
1017 assert(isl_basic_set_is_empty(bset));
1018 isl_local_space_free(ls);
1019 isl_basic_set_free(bset);
1021 /* test 5 */
1022 dim = isl_space_set_alloc(ctx, 0, 3);
1023 bset = isl_basic_set_universe(isl_space_copy(dim));
1024 ls = isl_local_space_from_space(dim);
1026 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1027 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
1028 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, 3);
1029 bset = isl_basic_set_add_constraint(bset, c);
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, 1, -3);
1034 bset = isl_basic_set_add_constraint(bset, c);
1036 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
1038 assert(bset && bset->n_div == 0);
1039 isl_basic_set_free(bset);
1040 isl_local_space_free(ls);
1042 /* test 6 */
1043 dim = isl_space_set_alloc(ctx, 0, 3);
1044 bset = isl_basic_set_universe(isl_space_copy(dim));
1045 ls = isl_local_space_from_space(dim);
1047 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1048 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
1049 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, 6);
1050 bset = isl_basic_set_add_constraint(bset, c);
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, 1, -3);
1055 bset = isl_basic_set_add_constraint(bset, c);
1057 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
1059 assert(bset && bset->n_div == 1);
1060 isl_basic_set_free(bset);
1061 isl_local_space_free(ls);
1063 /* test 7 */
1064 /* This test is a bit tricky. We set up an equality
1065 * a + 3b + 3c = 6 e0
1066 * Normalization of divs creates _two_ divs
1067 * a = 3 e0
1068 * c - b - e0 = 2 e1
1069 * Afterwards e0 is removed again because it has coefficient -1
1070 * and we end up with the original equality and div again.
1071 * Perhaps we can avoid the introduction of this temporary div.
1073 dim = isl_space_set_alloc(ctx, 0, 4);
1074 bset = isl_basic_set_universe(isl_space_copy(dim));
1075 ls = isl_local_space_from_space(dim);
1077 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1078 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
1079 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, -3);
1080 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, -3);
1081 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 3, 6);
1082 bset = isl_basic_set_add_constraint(bset, c);
1084 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
1086 /* Test disabled for now */
1088 assert(bset && bset->n_div == 1);
1090 isl_local_space_free(ls);
1091 isl_basic_set_free(bset);
1093 /* test 8 */
1094 dim = isl_space_set_alloc(ctx, 0, 5);
1095 bset = isl_basic_set_universe(isl_space_copy(dim));
1096 ls = isl_local_space_from_space(dim);
1098 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1099 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
1100 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, -3);
1101 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 3, -3);
1102 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 4, 6);
1103 bset = isl_basic_set_add_constraint(bset, c);
1105 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1106 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
1107 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, 1);
1108 c = isl_constraint_set_constant_si(c, 1);
1109 bset = isl_basic_set_add_constraint(bset, c);
1111 bset = isl_basic_set_project_out(bset, isl_dim_set, 4, 1);
1113 /* Test disabled for now */
1115 assert(bset && bset->n_div == 1);
1117 isl_local_space_free(ls);
1118 isl_basic_set_free(bset);
1120 /* test 9 */
1121 dim = isl_space_set_alloc(ctx, 0, 4);
1122 bset = isl_basic_set_universe(isl_space_copy(dim));
1123 ls = isl_local_space_from_space(dim);
1125 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1126 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
1127 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 1, -1);
1128 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, -2);
1129 bset = isl_basic_set_add_constraint(bset, c);
1131 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1132 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, -1);
1133 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 3, 3);
1134 c = isl_constraint_set_constant_si(c, 2);
1135 bset = isl_basic_set_add_constraint(bset, c);
1137 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 2);
1139 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
1141 assert(!isl_basic_set_is_empty(bset));
1143 isl_local_space_free(ls);
1144 isl_basic_set_free(bset);
1146 /* test 10 */
1147 dim = isl_space_set_alloc(ctx, 0, 3);
1148 bset = isl_basic_set_universe(isl_space_copy(dim));
1149 ls = isl_local_space_from_space(dim);
1151 c = isl_constraint_alloc_equality(isl_local_space_copy(ls));
1152 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 0, 1);
1153 c = isl_constraint_set_coefficient_si(c, isl_dim_set, 2, -2);
1154 bset = isl_basic_set_add_constraint(bset, c);
1156 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
1158 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
1160 isl_local_space_free(ls);
1161 isl_basic_set_free(bset);
1163 str = "{ [i] : exists (e0, e1: 3e1 >= 1 + 2e0 and "
1164 "8e1 <= -1 + 5i - 5e0 and 2e1 >= 1 + 2i - 5e0) }";
1165 set = isl_set_read_from_str(ctx, str);
1166 set = isl_set_compute_divs(set);
1167 isl_set_free(set);
1168 if (!set)
1169 return -1;
1171 if (test_elimination(ctx) < 0)
1172 return -1;
1174 str = "{ [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }";
1175 set = isl_set_read_from_str(ctx, str);
1176 set = isl_set_remove_divs_involving_dims(set, isl_dim_set, 0, 2);
1177 set = isl_set_fix_si(set, isl_dim_set, 2, -3);
1178 empty = isl_set_is_empty(set);
1179 isl_set_free(set);
1180 if (empty < 0)
1181 return -1;
1182 if (!empty)
1183 isl_die(ctx, isl_error_unknown,
1184 "result not as accurate as expected", return -1);
1186 return 0;
1189 void test_application_case(struct isl_ctx *ctx, const char *name)
1191 char *filename;
1192 FILE *input;
1193 struct isl_basic_set *bset1, *bset2;
1194 struct isl_basic_map *bmap;
1196 filename = get_filename(ctx, name, "omega");
1197 assert(filename);
1198 input = fopen(filename, "r");
1199 assert(input);
1201 bset1 = isl_basic_set_read_from_file(ctx, input);
1202 bmap = isl_basic_map_read_from_file(ctx, input);
1204 bset1 = isl_basic_set_apply(bset1, bmap);
1206 bset2 = isl_basic_set_read_from_file(ctx, input);
1208 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1210 isl_basic_set_free(bset1);
1211 isl_basic_set_free(bset2);
1212 free(filename);
1214 fclose(input);
1217 static int test_application(isl_ctx *ctx)
1219 test_application_case(ctx, "application");
1220 test_application_case(ctx, "application2");
1222 return 0;
1225 void test_affine_hull_case(struct isl_ctx *ctx, const char *name)
1227 char *filename;
1228 FILE *input;
1229 struct isl_basic_set *bset1, *bset2;
1231 filename = get_filename(ctx, name, "polylib");
1232 assert(filename);
1233 input = fopen(filename, "r");
1234 assert(input);
1236 bset1 = isl_basic_set_read_from_file(ctx, input);
1237 bset2 = isl_basic_set_read_from_file(ctx, input);
1239 bset1 = isl_basic_set_affine_hull(bset1);
1241 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1243 isl_basic_set_free(bset1);
1244 isl_basic_set_free(bset2);
1245 free(filename);
1247 fclose(input);
1250 int test_affine_hull(struct isl_ctx *ctx)
1252 const char *str;
1253 isl_set *set;
1254 isl_basic_set *bset, *bset2;
1255 isl_size n;
1256 isl_bool subset;
1258 test_affine_hull_case(ctx, "affine2");
1259 test_affine_hull_case(ctx, "affine");
1260 test_affine_hull_case(ctx, "affine3");
1262 str = "[m] -> { [i0] : exists (e0, e1: e1 <= 1 + i0 and "
1263 "m >= 3 and 4i0 <= 2 + m and e1 >= i0 and "
1264 "e1 >= 0 and e1 <= 2 and e1 >= 1 + 2e0 and "
1265 "2e1 <= 1 + m + 4e0 and 2e1 >= 2 - m + 4i0 - 4e0) }";
1266 set = isl_set_read_from_str(ctx, str);
1267 bset = isl_set_affine_hull(set);
1268 n = isl_basic_set_dim(bset, isl_dim_div);
1269 isl_basic_set_free(bset);
1270 if (n < 0)
1271 return -1;
1272 if (n != 0)
1273 isl_die(ctx, isl_error_unknown, "not expecting any divs",
1274 return -1);
1276 /* Check that isl_map_affine_hull is not confused by
1277 * the reordering of divs in isl_map_align_divs.
1279 str = "{ [a, b, c, 0] : exists (e0 = [(b)/32], e1 = [(c)/32]: "
1280 "32e0 = b and 32e1 = c); "
1281 "[a, 0, c, 0] : exists (e0 = [(c)/32]: 32e0 = c) }";
1282 set = isl_set_read_from_str(ctx, str);
1283 bset = isl_set_affine_hull(set);
1284 isl_basic_set_free(bset);
1285 if (!bset)
1286 return -1;
1288 str = "{ [a] : exists e0, e1, e2: 32e1 = 31 + 31a + 31e0 and "
1289 "32e2 = 31 + 31e0 }";
1290 set = isl_set_read_from_str(ctx, str);
1291 bset = isl_set_affine_hull(set);
1292 str = "{ [a] : exists e : a = 32 e }";
1293 bset2 = isl_basic_set_read_from_str(ctx, str);
1294 subset = isl_basic_set_is_subset(bset, bset2);
1295 isl_basic_set_free(bset);
1296 isl_basic_set_free(bset2);
1297 if (subset < 0)
1298 return -1;
1299 if (!subset)
1300 isl_die(ctx, isl_error_unknown, "not as accurate as expected",
1301 return -1);
1303 return 0;
1306 /* Pairs of maps and the corresponding expected results of
1307 * isl_map_plain_unshifted_simple_hull.
1309 struct {
1310 const char *map;
1311 const char *hull;
1312 } plain_unshifted_simple_hull_tests[] = {
1313 { "{ [i] -> [j] : i >= 1 and j >= 1 or i >= 2 and j <= 10 }",
1314 "{ [i] -> [j] : i >= 1 }" },
1315 { "{ [n] -> [i,j,k] : (i mod 3 = 2 and j mod 4 = 2) or "
1316 "(j mod 4 = 2 and k mod 6 = n) }",
1317 "{ [n] -> [i,j,k] : j mod 4 = 2 }" },
1320 /* Basic tests for isl_map_plain_unshifted_simple_hull.
1322 static int test_plain_unshifted_simple_hull(isl_ctx *ctx)
1324 int i;
1325 isl_map *map;
1326 isl_basic_map *hull, *expected;
1327 isl_bool equal;
1329 for (i = 0; i < ARRAY_SIZE(plain_unshifted_simple_hull_tests); ++i) {
1330 const char *str;
1331 str = plain_unshifted_simple_hull_tests[i].map;
1332 map = isl_map_read_from_str(ctx, str);
1333 str = plain_unshifted_simple_hull_tests[i].hull;
1334 expected = isl_basic_map_read_from_str(ctx, str);
1335 hull = isl_map_plain_unshifted_simple_hull(map);
1336 equal = isl_basic_map_is_equal(hull, expected);
1337 isl_basic_map_free(hull);
1338 isl_basic_map_free(expected);
1339 if (equal < 0)
1340 return -1;
1341 if (!equal)
1342 isl_die(ctx, isl_error_unknown, "unexpected hull",
1343 return -1);
1346 return 0;
1349 /* Pairs of sets and the corresponding expected results of
1350 * isl_set_unshifted_simple_hull.
1352 struct {
1353 const char *set;
1354 const char *hull;
1355 } unshifted_simple_hull_tests[] = {
1356 { "{ [0,x,y] : x <= -1; [1,x,y] : x <= y <= -x; [2,x,y] : x <= 1 }",
1357 "{ [t,x,y] : 0 <= t <= 2 and x <= 1 }" },
1360 /* Basic tests for isl_set_unshifted_simple_hull.
1362 static int test_unshifted_simple_hull(isl_ctx *ctx)
1364 int i;
1365 isl_set *set;
1366 isl_basic_set *hull, *expected;
1367 isl_bool equal;
1369 for (i = 0; i < ARRAY_SIZE(unshifted_simple_hull_tests); ++i) {
1370 const char *str;
1371 str = unshifted_simple_hull_tests[i].set;
1372 set = isl_set_read_from_str(ctx, str);
1373 str = unshifted_simple_hull_tests[i].hull;
1374 expected = isl_basic_set_read_from_str(ctx, str);
1375 hull = isl_set_unshifted_simple_hull(set);
1376 equal = isl_basic_set_is_equal(hull, expected);
1377 isl_basic_set_free(hull);
1378 isl_basic_set_free(expected);
1379 if (equal < 0)
1380 return -1;
1381 if (!equal)
1382 isl_die(ctx, isl_error_unknown, "unexpected hull",
1383 return -1);
1386 return 0;
1389 static int test_simple_hull(struct isl_ctx *ctx)
1391 const char *str;
1392 isl_set *set;
1393 isl_basic_set *bset;
1394 isl_bool is_empty;
1396 str = "{ [x, y] : 3y <= 2x and y >= -2 + 2x and 2y >= 2 - x;"
1397 "[y, x] : 3y <= 2x and y >= -2 + 2x and 2y >= 2 - x }";
1398 set = isl_set_read_from_str(ctx, str);
1399 bset = isl_set_simple_hull(set);
1400 is_empty = isl_basic_set_is_empty(bset);
1401 isl_basic_set_free(bset);
1403 if (is_empty == isl_bool_error)
1404 return -1;
1406 if (is_empty == isl_bool_false)
1407 isl_die(ctx, isl_error_unknown, "Empty set should be detected",
1408 return -1);
1410 if (test_plain_unshifted_simple_hull(ctx) < 0)
1411 return -1;
1412 if (test_unshifted_simple_hull(ctx) < 0)
1413 return -1;
1415 return 0;
1418 void test_convex_hull_case(struct isl_ctx *ctx, const char *name)
1420 char *filename;
1421 FILE *input;
1422 struct isl_basic_set *bset1, *bset2;
1423 struct isl_set *set;
1425 filename = get_filename(ctx, name, "polylib");
1426 assert(filename);
1427 input = fopen(filename, "r");
1428 assert(input);
1430 bset1 = isl_basic_set_read_from_file(ctx, input);
1431 bset2 = isl_basic_set_read_from_file(ctx, input);
1433 set = isl_basic_set_union(bset1, bset2);
1434 bset1 = isl_set_convex_hull(set);
1436 bset2 = isl_basic_set_read_from_file(ctx, input);
1438 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1440 isl_basic_set_free(bset1);
1441 isl_basic_set_free(bset2);
1442 free(filename);
1444 fclose(input);
1447 struct {
1448 const char *set;
1449 const char *hull;
1450 } convex_hull_tests[] = {
1451 { "{ [i0, i1, i2] : (i2 = 1 and i0 = 0 and i1 >= 0) or "
1452 "(i0 = 1 and i1 = 0 and i2 = 1) or "
1453 "(i0 = 0 and i1 = 0 and i2 = 0) }",
1454 "{ [i0, i1, i2] : i0 >= 0 and i2 >= i0 and i2 <= 1 and i1 >= 0 }" },
1455 { "[n] -> { [i0, i1, i0] : i0 <= -4 + n; "
1456 "[i0, i0, i2] : n = 6 and i0 >= 0 and i2 <= 7 - i0 and "
1457 "i2 <= 5 and i2 >= 4; "
1458 "[3, i1, 3] : n = 5 and i1 <= 2 and i1 >= 0 }",
1459 "[n] -> { [i0, i1, i2] : i2 <= -1 + n and 2i2 <= -6 + 3n - i0 and "
1460 "i2 <= 5 + i0 and i2 >= i0 }" },
1461 { "{ [x, y] : 3y <= 2x and y >= -2 + 2x and 2y >= 2 - x }",
1462 "{ [x, y] : 1 = 0 }" },
1463 { "{ [x, y, z] : 0 <= x, y, z <= 10; [x, y, 0] : x >= 0 and y > 0; "
1464 "[x, y, 0] : x >= 0 and y < 0 }",
1465 "{ [x, y, z] : x >= 0 and 0 <= z <= 10 }" },
1466 { "{ [a, b, c] : a <= 1 and -a < b <= 1 and 0 <= c <= 2 - a - b and "
1467 "c <= a; "
1468 "[0, 2, 0]; [3, 1, 0] }",
1469 "{ [a, b, c] : b > -a and 2b >= -1 + a and 0 <= c <= a and "
1470 "5c <= 6 - a - 3b }" },
1473 static int test_convex_hull_algo(isl_ctx *ctx, int convex)
1475 int i;
1476 int orig_convex = ctx->opt->convex;
1477 ctx->opt->convex = convex;
1479 test_convex_hull_case(ctx, "convex0");
1480 test_convex_hull_case(ctx, "convex1");
1481 test_convex_hull_case(ctx, "convex2");
1482 test_convex_hull_case(ctx, "convex3");
1483 test_convex_hull_case(ctx, "convex4");
1484 test_convex_hull_case(ctx, "convex5");
1485 test_convex_hull_case(ctx, "convex6");
1486 test_convex_hull_case(ctx, "convex7");
1487 test_convex_hull_case(ctx, "convex8");
1488 test_convex_hull_case(ctx, "convex9");
1489 test_convex_hull_case(ctx, "convex10");
1490 test_convex_hull_case(ctx, "convex11");
1491 test_convex_hull_case(ctx, "convex12");
1492 test_convex_hull_case(ctx, "convex13");
1493 test_convex_hull_case(ctx, "convex14");
1494 test_convex_hull_case(ctx, "convex15");
1496 for (i = 0; i < ARRAY_SIZE(convex_hull_tests); ++i) {
1497 isl_set *set1, *set2;
1498 int equal;
1500 set1 = isl_set_read_from_str(ctx, convex_hull_tests[i].set);
1501 set2 = isl_set_read_from_str(ctx, convex_hull_tests[i].hull);
1502 set1 = isl_set_from_basic_set(isl_set_convex_hull(set1));
1503 equal = isl_set_is_equal(set1, set2);
1504 isl_set_free(set1);
1505 isl_set_free(set2);
1507 if (equal < 0)
1508 return -1;
1509 if (!equal)
1510 isl_die(ctx, isl_error_unknown,
1511 "unexpected convex hull", return -1);
1514 ctx->opt->convex = orig_convex;
1516 return 0;
1519 static int test_convex_hull(isl_ctx *ctx)
1521 if (test_convex_hull_algo(ctx, ISL_CONVEX_HULL_FM) < 0)
1522 return -1;
1523 if (test_convex_hull_algo(ctx, ISL_CONVEX_HULL_WRAP) < 0)
1524 return -1;
1525 return 0;
1528 void test_gist_case(struct isl_ctx *ctx, const char *name)
1530 char *filename;
1531 FILE *input;
1532 struct isl_basic_set *bset1, *bset2;
1534 filename = get_filename(ctx, name, "polylib");
1535 assert(filename);
1536 input = fopen(filename, "r");
1537 assert(input);
1539 bset1 = isl_basic_set_read_from_file(ctx, input);
1540 bset2 = isl_basic_set_read_from_file(ctx, input);
1542 bset1 = isl_basic_set_gist(bset1, bset2);
1544 bset2 = isl_basic_set_read_from_file(ctx, input);
1546 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1548 isl_basic_set_free(bset1);
1549 isl_basic_set_free(bset2);
1550 free(filename);
1552 fclose(input);
1555 /* Inputs to isl_map_plain_gist_basic_map, along with the expected output.
1557 struct {
1558 const char *map;
1559 const char *context;
1560 const char *gist;
1561 } plain_gist_tests[] = {
1562 { "{ [i] -> [j] : i >= 1 and j >= 1 or i >= 2 and j <= 10 }",
1563 "{ [i] -> [j] : i >= 1 }",
1564 "{ [i] -> [j] : j >= 1 or i >= 2 and j <= 10 }" },
1565 { "{ [n] -> [i,j,k] : (i mod 3 = 2 and j mod 4 = 2) or "
1566 "(j mod 4 = 2 and k mod 6 = n) }",
1567 "{ [n] -> [i,j,k] : j mod 4 = 2 }",
1568 "{ [n] -> [i,j,k] : (i mod 3 = 2) or (k mod 6 = n) }" },
1569 { "{ [i] -> [j] : i > j and (exists a,b : i <= 2a + 5b <= 2) }",
1570 "{ [i] -> [j] : i > j }",
1571 "{ [i] -> [j] : exists a,b : i <= 2a + 5b <= 2 }" },
1574 /* Basic tests for isl_map_plain_gist_basic_map.
1576 static int test_plain_gist(isl_ctx *ctx)
1578 int i;
1580 for (i = 0; i < ARRAY_SIZE(plain_gist_tests); ++i) {
1581 const char *str;
1582 int equal;
1583 isl_map *map, *gist;
1584 isl_basic_map *context;
1586 map = isl_map_read_from_str(ctx, plain_gist_tests[i].map);
1587 str = plain_gist_tests[i].context;
1588 context = isl_basic_map_read_from_str(ctx, str);
1589 map = isl_map_plain_gist_basic_map(map, context);
1590 gist = isl_map_read_from_str(ctx, plain_gist_tests[i].gist);
1591 equal = isl_map_is_equal(map, gist);
1592 isl_map_free(map);
1593 isl_map_free(gist);
1594 if (equal < 0)
1595 return -1;
1596 if (!equal)
1597 isl_die(ctx, isl_error_unknown,
1598 "incorrect gist result", return -1);
1601 return 0;
1604 /* Inputs for isl_basic_set_gist tests that are expected to fail.
1606 struct {
1607 const char *set;
1608 const char *context;
1609 } gist_fail_tests[] = {
1610 { "{ [i] : exists (e0, e1: 3e1 >= 1 + 2e0 and "
1611 "8e1 <= -1 + 5i - 5e0 and 2e1 >= 1 + 2i - 5e0) }",
1612 "{ [i] : i >= 0 }" },
1615 /* Check that isl_basic_set_gist fails (gracefully) when expected.
1616 * In particular, the user should be able to recover from the failure.
1618 static isl_stat test_gist_fail(struct isl_ctx *ctx)
1620 int i, n;
1621 int on_error;
1623 on_error = isl_options_get_on_error(ctx);
1624 isl_options_set_on_error(ctx, ISL_ON_ERROR_CONTINUE);
1625 n = ARRAY_SIZE(gist_fail_tests);
1626 for (i = 0; i < n; ++i) {
1627 const char *str;
1628 isl_basic_set *bset, *context;
1630 bset = isl_basic_set_read_from_str(ctx, gist_fail_tests[i].set);
1631 str = gist_fail_tests[i].context;
1632 context = isl_basic_set_read_from_str(ctx, str);
1633 bset = isl_basic_set_gist(bset, context);
1634 isl_basic_set_free(bset);
1635 if (bset)
1636 break;
1638 isl_options_set_on_error(ctx, on_error);
1639 if (i < n)
1640 isl_die(ctx, isl_error_unknown,
1641 "operation not expected to succeed",
1642 return isl_stat_error);
1644 return isl_stat_ok;
1647 struct {
1648 const char *set;
1649 const char *context;
1650 const char *gist;
1651 } gist_tests[] = {
1652 { "{ [a, b, c] : a <= 15 and a >= 1 }",
1653 "{ [a, b, c] : exists (e0 = floor((-1 + a)/16): a >= 1 and "
1654 "c <= 30 and 32e0 >= -62 + 2a + 2b - c and b >= 0) }",
1655 "{ [a, b, c] : a <= 15 }" },
1656 { "{ : }", "{ : 1 = 0 }", "{ : }" },
1657 { "{ : 1 = 0 }", "{ : 1 = 0 }", "{ : }" },
1658 { "[M] -> { [x] : exists (e0 = floor((-2 + x)/3): 3e0 = -2 + x) }",
1659 "[M] -> { [3M] }" , "[M] -> { [x] : 1 = 0 }" },
1660 { "{ [m, n, a, b] : a <= 2147 + n }",
1661 "{ [m, n, a, b] : (m >= 1 and n >= 1 and a <= 2148 - m and "
1662 "b <= 2148 - n and b >= 0 and b >= 2149 - n - a) or "
1663 "(n >= 1 and a >= 0 and b <= 2148 - n - a and "
1664 "b >= 0) }",
1665 "{ [m, n, ku, kl] }" },
1666 { "{ [a, a, b] : a >= 10 }",
1667 "{ [a, b, c] : c >= a and c <= b and c >= 2 }",
1668 "{ [a, a, b] : a >= 10 }" },
1669 { "{ [i, j] : i >= 0 and i + j >= 0 }", "{ [i, j] : i <= 0 }",
1670 "{ [0, j] : j >= 0 }" },
1671 /* Check that no constraints on i6 are introduced in the gist */
1672 { "[t1] -> { [i4, i6] : exists (e0 = floor((1530 - 4t1 - 5i4)/20): "
1673 "20e0 <= 1530 - 4t1 - 5i4 and 20e0 >= 1511 - 4t1 - 5i4 and "
1674 "5e0 <= 381 - t1 and i4 <= 1) }",
1675 "[t1] -> { [i4, i6] : exists (e0 = floor((-t1 + i6)/5): "
1676 "5e0 = -t1 + i6 and i6 <= 6 and i6 >= 3) }",
1677 "[t1] -> { [i4, i6] : exists (e0 = floor((1530 - 4t1 - 5i4)/20): "
1678 "i4 <= 1 and 5e0 <= 381 - t1 and 20e0 <= 1530 - 4t1 - 5i4 and "
1679 "20e0 >= 1511 - 4t1 - 5i4) }" },
1680 /* Check that no constraints on i6 are introduced in the gist */
1681 { "[t1, t2] -> { [i4, i5, i6] : exists (e0 = floor((1 + i4)/2), "
1682 "e1 = floor((1530 - 4t1 - 5i4)/20), "
1683 "e2 = floor((-4t1 - 5i4 + 10*floor((1 + i4)/2))/20), "
1684 "e3 = floor((-1 + i4)/2): t2 = 0 and 2e3 = -1 + i4 and "
1685 "20e2 >= -19 - 4t1 - 5i4 + 10e0 and 5e2 <= 1 - t1 and "
1686 "2e0 <= 1 + i4 and 2e0 >= i4 and "
1687 "20e1 <= 1530 - 4t1 - 5i4 and "
1688 "20e1 >= 1511 - 4t1 - 5i4 and i4 <= 1 and "
1689 "5e1 <= 381 - t1 and 20e2 <= -4t1 - 5i4 + 10e0) }",
1690 "[t1, t2] -> { [i4, i5, i6] : exists (e0 = floor((-17 + i4)/2), "
1691 "e1 = floor((-t1 + i6)/5): 5e1 = -t1 + i6 and "
1692 "2e0 <= -17 + i4 and 2e0 >= -18 + i4 and "
1693 "10e0 <= -91 + 5i4 + 4i6 and "
1694 "10e0 >= -105 + 5i4 + 4i6) }",
1695 "[t1, t2] -> { [i4, i5, i6] : exists (e0 = floor((381 - t1)/5), "
1696 "e1 = floor((-1 + i4)/2): t2 = 0 and 2e1 = -1 + i4 and "
1697 "i4 <= 1 and 5e0 <= 381 - t1 and 20e0 >= 1511 - 4t1 - 5i4) }" },
1698 { "{ [0, 0, q, p] : -5 <= q <= 5 and p >= 0 }",
1699 "{ [a, b, q, p] : b >= 1 + a }",
1700 "{ [a, b, q, p] : false }" },
1701 { "[n] -> { [x] : x = n && x mod 32 = 0 }",
1702 "[n] -> { [x] : x mod 32 = 0 }",
1703 "[n] -> { [x = n] }" },
1704 { "{ [x] : x mod 6 = 0 }", "{ [x] : x mod 3 = 0 }",
1705 "{ [x] : x mod 2 = 0 }" },
1706 { "{ [x] : x mod 3200 = 0 }", "{ [x] : x mod 10000 = 0 }",
1707 "{ [x] : x mod 128 = 0 }" },
1708 { "{ [x] : x mod 3200 = 0 }", "{ [x] : x mod 10 = 0 }",
1709 "{ [x] : x mod 3200 = 0 }" },
1710 { "{ [a, b, c] : a mod 2 = 0 and a = c }",
1711 "{ [a, b, c] : b mod 2 = 0 and b = c }",
1712 "{ [a, b, c = a] }" },
1713 { "{ [a, b, c] : a mod 6 = 0 and a = c }",
1714 "{ [a, b, c] : b mod 2 = 0 and b = c }",
1715 "{ [a, b, c = a] : a mod 3 = 0 }" },
1716 { "{ [x] : 0 <= x <= 4 or 6 <= x <= 9 }",
1717 "{ [x] : 1 <= x <= 3 or 7 <= x <= 8 }",
1718 "{ [x] }" },
1719 { "{ [x,y] : x < 0 and 0 <= y <= 4 or x >= -2 and -x <= y <= 10 + x }",
1720 "{ [x,y] : 1 <= y <= 3 }",
1721 "{ [x,y] }" },
1724 /* Check that isl_set_gist behaves as expected.
1726 * For the test cases in gist_tests, besides checking that the result
1727 * is as expected, also check that applying the gist operation does
1728 * not modify the input set (an earlier version of isl would do that) and
1729 * that the test case is consistent, i.e., that the gist has the same
1730 * intersection with the context as the input set.
1732 static int test_gist(struct isl_ctx *ctx)
1734 int i;
1735 const char *str;
1736 isl_basic_set *bset1, *bset2;
1737 isl_map *map1, *map2;
1738 isl_bool equal;
1739 isl_size n_div;
1741 for (i = 0; i < ARRAY_SIZE(gist_tests); ++i) {
1742 isl_bool equal_input, equal_intersection;
1743 isl_set *set1, *set2, *copy, *context;
1745 set1 = isl_set_read_from_str(ctx, gist_tests[i].set);
1746 context = isl_set_read_from_str(ctx, gist_tests[i].context);
1747 copy = isl_set_copy(set1);
1748 set1 = isl_set_gist(set1, isl_set_copy(context));
1749 set2 = isl_set_read_from_str(ctx, gist_tests[i].gist);
1750 equal = isl_set_is_equal(set1, set2);
1751 isl_set_free(set1);
1752 set1 = isl_set_read_from_str(ctx, gist_tests[i].set);
1753 equal_input = isl_set_is_equal(set1, copy);
1754 isl_set_free(copy);
1755 set1 = isl_set_intersect(set1, isl_set_copy(context));
1756 set2 = isl_set_intersect(set2, context);
1757 equal_intersection = isl_set_is_equal(set1, set2);
1758 isl_set_free(set2);
1759 isl_set_free(set1);
1760 if (equal < 0 || equal_input < 0 || equal_intersection < 0)
1761 return -1;
1762 if (!equal)
1763 isl_die(ctx, isl_error_unknown,
1764 "incorrect gist result", return -1);
1765 if (!equal_input)
1766 isl_die(ctx, isl_error_unknown,
1767 "gist modified input", return -1);
1768 if (!equal_input)
1769 isl_die(ctx, isl_error_unknown,
1770 "inconsistent gist test case", return -1);
1773 if (test_gist_fail(ctx) < 0)
1774 return -1;
1776 test_gist_case(ctx, "gist1");
1778 str = "[p0, p2, p3, p5, p6, p10] -> { [] : "
1779 "exists (e0 = [(15 + p0 + 15p6 + 15p10)/16], e1 = [(p5)/8], "
1780 "e2 = [(p6)/128], e3 = [(8p2 - p5)/128], "
1781 "e4 = [(128p3 - p6)/4096]: 8e1 = p5 and 128e2 = p6 and "
1782 "128e3 = 8p2 - p5 and 4096e4 = 128p3 - p6 and p2 >= 0 and "
1783 "16e0 >= 16 + 16p6 + 15p10 and p2 <= 15 and p3 >= 0 and "
1784 "p3 <= 31 and p6 >= 128p3 and p5 >= 8p2 and p10 >= 0 and "
1785 "16e0 <= 15 + p0 + 15p6 + 15p10 and 16e0 >= p0 + 15p6 + 15p10 and "
1786 "p10 <= 15 and p10 <= -1 + p0 - p6) }";
1787 bset1 = isl_basic_set_read_from_str(ctx, str);
1788 str = "[p0, p2, p3, p5, p6, p10] -> { [] : exists (e0 = [(p5)/8], "
1789 "e1 = [(p6)/128], e2 = [(8p2 - p5)/128], "
1790 "e3 = [(128p3 - p6)/4096]: 8e0 = p5 and 128e1 = p6 and "
1791 "128e2 = 8p2 - p5 and 4096e3 = 128p3 - p6 and p5 >= -7 and "
1792 "p2 >= 0 and 8p2 <= -1 + p0 and p2 <= 15 and p3 >= 0 and "
1793 "p3 <= 31 and 128p3 <= -1 + p0 and p6 >= -127 and "
1794 "p5 <= -1 + p0 and p6 <= -1 + p0 and p6 >= 128p3 and "
1795 "p0 >= 1 and p5 >= 8p2 and p10 >= 0 and p10 <= 15 ) }";
1796 bset2 = isl_basic_set_read_from_str(ctx, str);
1797 bset1 = isl_basic_set_gist(bset1, bset2);
1798 assert(bset1 && bset1->n_div == 0);
1799 isl_basic_set_free(bset1);
1801 /* Check that the integer divisions of the second disjunct
1802 * do not spread to the first disjunct.
1804 str = "[t1] -> { S_0[] -> A[o0] : (exists (e0 = [(-t1 + o0)/16]: "
1805 "16e0 = -t1 + o0 and o0 >= 0 and o0 <= 15 and t1 >= 0)) or "
1806 "(exists (e0 = [(-1 + t1)/16], "
1807 "e1 = [(-16 + t1 - 16e0)/4294967296]: "
1808 "4294967296e1 = -16 + t1 - o0 - 16e0 and "
1809 "16e0 <= -1 + t1 and 16e0 >= -16 + t1 and o0 >= 0 and "
1810 "o0 <= 4294967295 and t1 <= -1)) }";
1811 map1 = isl_map_read_from_str(ctx, str);
1812 str = "[t1] -> { S_0[] -> A[o0] : t1 >= 0 and t1 <= 4294967295 }";
1813 map2 = isl_map_read_from_str(ctx, str);
1814 map1 = isl_map_gist(map1, map2);
1815 if (!map1)
1816 return -1;
1817 if (map1->n != 1)
1818 isl_die(ctx, isl_error_unknown, "expecting single disjunct",
1819 isl_map_free(map1); return -1);
1820 n_div = isl_basic_map_dim(map1->p[0], isl_dim_div);
1821 isl_map_free(map1);
1822 if (n_div < 0)
1823 return -1;
1824 if (n_div != 1)
1825 isl_die(ctx, isl_error_unknown, "expecting single div",
1826 return -1);
1828 if (test_plain_gist(ctx) < 0)
1829 return -1;
1831 return 0;
1834 int test_coalesce_set(isl_ctx *ctx, const char *str, int check_one)
1836 isl_set *set, *set2;
1837 int equal;
1838 int one;
1840 set = isl_set_read_from_str(ctx, str);
1841 set = isl_set_coalesce(set);
1842 set2 = isl_set_read_from_str(ctx, str);
1843 equal = isl_set_is_equal(set, set2);
1844 one = set && set->n == 1;
1845 isl_set_free(set);
1846 isl_set_free(set2);
1848 if (equal < 0)
1849 return -1;
1850 if (!equal)
1851 isl_die(ctx, isl_error_unknown,
1852 "coalesced set not equal to input", return -1);
1853 if (check_one && !one)
1854 isl_die(ctx, isl_error_unknown,
1855 "coalesced set should not be a union", return -1);
1857 return 0;
1860 /* Inputs for coalescing tests with unbounded wrapping.
1861 * "str" is a string representation of the input set.
1862 * "single_disjunct" is set if we expect the result to consist of
1863 * a single disjunct.
1865 struct {
1866 int single_disjunct;
1867 const char *str;
1868 } coalesce_unbounded_tests[] = {
1869 { 1, "{ [x,y,z] : y + 2 >= 0 and x - y + 1 >= 0 and "
1870 "-x - y + 1 >= 0 and -3 <= z <= 3;"
1871 "[x,y,z] : -x+z + 20 >= 0 and -x-z + 20 >= 0 and "
1872 "x-z + 20 >= 0 and x+z + 20 >= 0 and "
1873 "-10 <= y <= 0}" },
1874 { 1, "{ [x,y] : 0 <= x,y <= 10; [5,y]: 4 <= y <= 11 }" },
1875 { 1, "{ [x,0,0] : -5 <= x <= 5; [0,y,1] : -5 <= y <= 5 }" },
1876 { 1, "{ [x,y] : 0 <= x <= 10 and 0 >= y >= -1 and x+y >= 0; [0,1] }" },
1877 { 1, "{ [x,y] : (0 <= x,y <= 4) or (2 <= x,y <= 5 and x + y <= 9) }" },
1878 { 0, "{ [x, y, z] : 0 <= x,y,z <= 100 and 0 < z <= 2 + 2x + 2y; "
1879 "[x, y, 0] : x,y <= 100 and y <= 9 + 11x and x <= 9 + 11y }" },
1882 /* Test the functionality of isl_set_coalesce with the bounded wrapping
1883 * option turned off.
1885 int test_coalesce_unbounded_wrapping(isl_ctx *ctx)
1887 int i;
1888 int r = 0;
1889 int bounded;
1891 bounded = isl_options_get_coalesce_bounded_wrapping(ctx);
1892 isl_options_set_coalesce_bounded_wrapping(ctx, 0);
1894 for (i = 0; i < ARRAY_SIZE(coalesce_unbounded_tests); ++i) {
1895 const char *str = coalesce_unbounded_tests[i].str;
1896 int check_one = coalesce_unbounded_tests[i].single_disjunct;
1897 if (test_coalesce_set(ctx, str, check_one) >= 0)
1898 continue;
1899 r = -1;
1900 break;
1903 isl_options_set_coalesce_bounded_wrapping(ctx, bounded);
1905 return r;
1908 /* Inputs for coalescing tests.
1909 * "str" is a string representation of the input set.
1910 * "single_disjunct" is set if we expect the result to consist of
1911 * a single disjunct.
1913 struct {
1914 int single_disjunct;
1915 const char *str;
1916 } coalesce_tests[] = {
1917 { 1, "{[x,y]: x >= 0 & x <= 10 & y >= 0 & y <= 10 or "
1918 "y >= x & x >= 2 & 5 >= y }" },
1919 { 1, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
1920 "x + y >= 10 & y <= x & x + y <= 20 & y >= 0}" },
1921 { 0, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
1922 "x + y >= 10 & y <= x & x + y <= 19 & y >= 0}" },
1923 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1924 "y >= 0 & x >= 6 & x <= 10 & y <= x}" },
1925 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1926 "y >= 0 & x >= 7 & x <= 10 & y <= x}" },
1927 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1928 "y >= 0 & x >= 6 & x <= 10 & y + 1 <= x}" },
1929 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 6}" },
1930 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 7 & y <= 6}" },
1931 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 5}" },
1932 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 7}" },
1933 { 1, "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }" },
1934 { 0, "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}" },
1935 { 1, "[n] -> { [i] : 1 <= i and i <= n - 1 or 2 <= i and i <= n }" },
1936 { 0, "[n] -> { [[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1937 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1938 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1939 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
1940 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
1941 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
1942 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
1943 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
1944 "[[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1945 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1946 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1947 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
1948 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
1949 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
1950 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
1951 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
1952 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
1953 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }" },
1954 { 0, "[n, m] -> { [o0, o2, o3] : (o3 = 1 and o0 >= 1 + m and "
1955 "o0 <= n + m and o2 <= m and o0 >= 2 + n and o2 >= 3) or "
1956 "(o0 >= 2 + n and o0 >= 1 + m and o0 <= n + m and n >= 1 and "
1957 "o3 <= -1 + o2 and o3 >= 1 - m + o2 and o3 >= 2 and o3 <= n) }" },
1958 { 0, "[M, N] -> { [[i0, i1, i2, i3, i4, i5, i6] -> "
1959 "[o0, o1, o2, o3, o4, o5, o6]] : "
1960 "(o6 <= -4 + 2M - 2N + i0 + i1 - i2 + i6 - o0 - o1 + o2 and "
1961 "o3 <= -2 + i3 and o6 >= 2 + i0 + i3 + i6 - o0 - o3 and "
1962 "o6 >= 2 - M + N + i3 + i4 + i6 - o3 - o4 and o0 <= -1 + i0 and "
1963 "o4 >= 4 - 3M + 3N - i0 - i1 + i2 + 2i3 + i4 + o0 + o1 - o2 - 2o3 "
1964 "and o6 <= -3 + 2M - 2N + i3 + i4 - i5 + i6 - o3 - o4 + o5 and "
1965 "2o6 <= -5 + 5M - 5N + 2i0 + i1 - i2 - i5 + 2i6 - 2o0 - o1 + o2 + o5 "
1966 "and o6 >= 2i0 + i1 + i6 - 2o0 - o1 and "
1967 "3o6 <= -5 + 4M - 4N + 2i0 + i1 - i2 + 2i3 + i4 - i5 + 3i6 "
1968 "- 2o0 - o1 + o2 - 2o3 - o4 + o5) or "
1969 "(N >= 2 and o3 <= -1 + i3 and o0 <= -1 + i0 and "
1970 "o6 >= i3 + i6 - o3 and M >= 0 and "
1971 "2o6 >= 1 + i0 + i3 + 2i6 - o0 - o3 and "
1972 "o6 >= 1 - M + i0 + i6 - o0 and N >= 2M and o6 >= i0 + i6 - o0) }" },
1973 { 0, "[M, N] -> { [o0] : (o0 = 0 and M >= 1 and N >= 2) or "
1974 "(o0 = 0 and M >= 1 and N >= 2M and N >= 2 + M) or "
1975 "(o0 = 0 and M >= 2 and N >= 3) or "
1976 "(M = 0 and o0 = 0 and N >= 3) }" },
1977 { 0, "{ [i0, i1, i2, i3] : (i1 = 10i0 and i0 >= 1 and 10i0 <= 100 and "
1978 "i3 <= 9 + 10 i2 and i3 >= 1 + 10i2 and i3 >= 0) or "
1979 "(i1 <= 9 + 10i0 and i1 >= 1 + 10i0 and i2 >= 0 and "
1980 "i0 >= 0 and i1 <= 100 and i3 <= 9 + 10i2 and i3 >= 1 + 10i2) }" },
1981 { 0, "[M] -> { [i1] : (i1 >= 2 and i1 <= M) or (i1 = M and M >= 1) }" },
1982 { 0, "{[x,y] : x,y >= 0; [x,y] : 10 <= x <= 20 and y >= -1 }" },
1983 { 1, "{ [x, y] : (x >= 1 and y >= 1 and x <= 2 and y <= 2) or "
1984 "(y = 3 and x = 1) }" },
1985 { 1, "[M] -> { [i0, i1, i2, i3, i4] : (i1 >= 3 and i4 >= 2 + i2 and "
1986 "i2 >= 2 and i0 >= 2 and i3 >= 1 + i2 and i0 <= M and "
1987 "i1 <= M and i3 <= M and i4 <= M) or "
1988 "(i1 >= 2 and i4 >= 1 + i2 and i2 >= 2 and i0 >= 2 and "
1989 "i3 >= 1 + i2 and i0 <= M and i1 <= -1 + M and i3 <= M and "
1990 "i4 <= -1 + M) }" },
1991 { 1, "{ [x, y] : (x >= 0 and y >= 0 and x <= 10 and y <= 10) or "
1992 "(x >= 1 and y >= 1 and x <= 11 and y <= 11) }" },
1993 { 0, "{[x,0] : x >= 0; [x,1] : x <= 20}" },
1994 { 1, "{ [x, 1 - x] : 0 <= x <= 1; [0,0] }" },
1995 { 1, "{ [0,0]; [i,i] : 1 <= i <= 10 }" },
1996 { 0, "{ [0,0]; [i,j] : 1 <= i,j <= 10 }" },
1997 { 1, "{ [0,0]; [i,2i] : 1 <= i <= 10 }" },
1998 { 0, "{ [0,0]; [i,2i] : 2 <= i <= 10 }" },
1999 { 0, "{ [1,0]; [i,2i] : 1 <= i <= 10 }" },
2000 { 0, "{ [0,1]; [i,2i] : 1 <= i <= 10 }" },
2001 { 0, "{ [a, b] : exists e : 2e = a and "
2002 "a >= 0 and (a <= 3 or (b <= 0 and b >= -4 + a)) }" },
2003 { 0, "{ [i, j, i', j'] : i <= 2 and j <= 2 and "
2004 "j' >= -1 + 2i + j - 2i' and i' <= -1 + i and "
2005 "j >= 1 and j' <= i + j - i' and i >= 1; "
2006 "[1, 1, 1, 1] }" },
2007 { 1, "{ [i,j] : exists a,b : i = 2a and j = 3b; "
2008 "[i,j] : exists a : j = 3a }" },
2009 { 1, "{ [a, b, c] : (c <= 7 - b and b <= 1 and b >= 0 and "
2010 "c >= 3 + b and b <= 3 + 8a and b >= -26 + 8a and "
2011 "a >= 3) or "
2012 "(b <= 1 and c <= 7 and b >= 0 and c >= 4 + b and "
2013 "b <= 3 + 8a and b >= -26 + 8a and a >= 3) }" },
2014 { 1, "{ [a, 0, c] : c >= 1 and c <= 29 and c >= -1 + 8a and "
2015 "c <= 6 + 8a and a >= 3; "
2016 "[a, -1, c] : c >= 1 and c <= 30 and c >= 8a and "
2017 "c <= 7 + 8a and a >= 3 and a <= 4 }" },
2018 { 1, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
2019 "[x,0] : 3 <= x <= 4 }" },
2020 { 1, "{ [x,y] : 0 <= x <= 3 and y >= 0 and x + 3y <= 6; "
2021 "[x,0] : 4 <= x <= 5 }" },
2022 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
2023 "[x,0] : 3 <= x <= 5 }" },
2024 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + y <= 4; "
2025 "[x,0] : 3 <= x <= 4 }" },
2026 { 1, "{ [i0, i1] : i0 <= 122 and i0 >= 1 and 128i1 >= -249 + i0 and "
2027 "i1 <= 0; "
2028 "[i0, 0] : i0 >= 123 and i0 <= 124 }" },
2029 { 1, "{ [0,0]; [1,1] }" },
2030 { 1, "[n] -> { [k] : 16k <= -1 + n and k >= 1; [0] : n >= 2 }" },
2031 { 1, "{ [k, ii, k - ii] : ii >= -6 + k and ii <= 6 and ii >= 1 and "
2032 "ii <= k;"
2033 "[k, 0, k] : k <= 6 and k >= 1 }" },
2034 { 1, "{ [i,j] : i = 4 j and 0 <= i <= 100;"
2035 "[i,j] : 1 <= i <= 100 and i >= 4j + 1 and i <= 4j + 2 }" },
2036 { 1, "{ [x,y] : x % 2 = 0 and y % 2 = 0; [x,x] : x % 2 = 0 }" },
2037 { 1, "[n] -> { [1] : n >= 0;"
2038 "[x] : exists (e0 = floor((x)/2): x >= 2 and "
2039 "2e0 >= -1 + x and 2e0 <= x and 2e0 <= n) }" },
2040 { 1, "[n] -> { [x, y] : exists (e0 = floor((x)/2), e1 = floor((y)/3): "
2041 "3e1 = y and x >= 2 and 2e0 >= -1 + x and "
2042 "2e0 <= x and 2e0 <= n);"
2043 "[1, y] : exists (e0 = floor((y)/3): 3e0 = y and "
2044 "n >= 0) }" },
2045 { 1, "[t1] -> { [i0] : (exists (e0 = floor((63t1)/64): "
2046 "128e0 >= -134 + 127t1 and t1 >= 2 and "
2047 "64e0 <= 63t1 and 64e0 >= -63 + 63t1)) or "
2048 "t1 = 1 }" },
2049 { 1, "{ [i, i] : exists (e0 = floor((1 + 2i)/3): 3e0 <= 2i and "
2050 "3e0 >= -1 + 2i and i <= 9 and i >= 1);"
2051 "[0, 0] }" },
2052 { 1, "{ [t1] : exists (e0 = floor((-11 + t1)/2): 2e0 = -11 + t1 and "
2053 "t1 >= 13 and t1 <= 16);"
2054 "[t1] : t1 <= 15 and t1 >= 12 }" },
2055 { 1, "{ [x,y] : x = 3y and 0 <= y <= 2; [-3,-1] }" },
2056 { 1, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-3,-2] }" },
2057 { 0, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-2,-2] }" },
2058 { 0, "{ [x,y] : 2x = 3y and 0 <= y <= 4; [-3,-1] }" },
2059 { 1, "{ [i] : exists j : i = 4 j and 0 <= i <= 100;"
2060 "[i] : exists j : 1 <= i <= 100 and i >= 4j + 1 and "
2061 "i <= 4j + 2 }" },
2062 { 1, "{ [c0] : (exists (e0 : c0 - 1 <= 3e0 <= c0)) or "
2063 "(exists (e0 : 3e0 = -2 + c0)) }" },
2064 { 0, "[n, b0, t0] -> "
2065 "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
2066 "(exists (e0 = floor((-32b0 + i4)/1048576), "
2067 "e1 = floor((i8)/32): 1048576e0 = -32b0 + i4 and 32e1 = i8 and "
2068 "n <= 2147483647 and b0 <= 32767 and b0 >= 0 and "
2069 "32b0 <= -2 + n and t0 <= 31 and t0 >= 0 and i0 >= 8 + n and "
2070 "3i4 <= -96 + 3t0 + i0 and 3i4 >= -95 - n + 3t0 + i0 and "
2071 "i8 >= -157 + i0 - 4i4 and i8 >= 0 and "
2072 "i8 <= -33 + i0 - 4i4 and 3i8 <= -91 + 4n - i0)) or "
2073 "(exists (e0 = floor((-32b0 + i4)/1048576), "
2074 "e1 = floor((i8)/32): 1048576e0 = -32b0 + i4 and 32e1 = i8 and "
2075 "n <= 2147483647 and b0 <= 32767 and b0 >= 0 and "
2076 "32b0 <= -2 + n and t0 <= 31 and t0 >= 0 and i0 <= 7 + n and "
2077 "4i4 <= -3 + i0 and 3i4 <= -96 + 3t0 + i0 and "
2078 "3i4 >= -95 - n + 3t0 + i0 and i8 >= -157 + i0 - 4i4 and "
2079 "i8 >= 0 and i8 <= -4 + i0 - 3i4 and i8 <= -41 + i0));"
2080 "[i0, i1, i2, i3, 0, i5, i6, i7, i8, i9, i10, i11, i12] : "
2081 "(exists (e0 = floor((i8)/32): b0 = 0 and 32e0 = i8 and "
2082 "n <= 2147483647 and t0 <= 31 and t0 >= 0 and i0 >= 11 and "
2083 "i0 >= 96 - 3t0 and i0 <= 95 + n - 3t0 and i0 <= 7 + n and "
2084 "i8 >= -40 + i0 and i8 <= -10 + i0)) }" },
2085 { 0, "{ [i0, i1, i2] : "
2086 "(exists (e0, e1 = floor((i0)/32), e2 = floor((i1)/32): "
2087 "32e1 = i0 and 32e2 = i1 and i1 >= -31 + i0 and "
2088 "i1 <= 31 + i0 and i2 >= -30 + i0 and i2 >= -30 + i1 and "
2089 "32e0 >= -30 + i0 and 32e0 >= -30 + i1 and "
2090 "32e0 >= -31 + i2 and 32e0 <= 30 + i2 and 32e0 <= 31 + i1 and "
2091 "32e0 <= 31 + i0)) or "
2092 "i0 >= 0 }" },
2093 { 1, "{ [a, b, c] : 2b = 1 + a and 2c = 2 + a; [0, 0, 0] }" },
2094 { 1, "{ [a, a, b, c] : 32*floor((a)/32) = a and 2*floor((b)/2) = b and "
2095 "2*floor((c)/2) = c and 0 <= a <= 192;"
2096 "[224, 224, b, c] : 2*floor((b)/2) = b and 2*floor((c)/2) = c }"
2098 { 1, "[n] -> { [a,b] : (exists e : 1 <= a <= 7e and 9e <= b <= n) or "
2099 "(0 <= a <= b <= n) }" },
2100 { 1, "{ [a, b] : 0 <= a <= 2 and b >= 0 and "
2101 "((0 < b <= 13) or (2*floor((a + b)/2) >= -5 + a + 2b)) }" },
2102 { 1, "{ [a] : (2 <= a <= 5) or (a mod 2 = 1 and 1 <= a <= 5) }" },
2103 { 1, "{ [a, b, c] : (b = -1 + a and 0 < a <= 3 and "
2104 "9*floor((-4a + 2c)/9) <= -3 - 4a + 2c) or "
2105 "(exists (e0 = floor((-16 + 2c)/9): a = 4 and "
2106 "b = 3 and 9e0 <= -19 + 2c)) }" },
2107 { 1, "{ [a, b, c] : (b = -1 + a and 0 < a <= 3 and "
2108 "9*floor((-4a + 2c)/9) <= -3 - 4a + 2c) or "
2109 "(a = 4 and b = 3 and "
2110 "9*floor((-16 + 2c)/9) <= -19 + 2c) }" },
2111 { 0, "{ [a, b, c] : (b <= 2 and b <= -2 + a) or "
2112 "(b = -1 + a and 0 < a <= 3 and "
2113 "9*floor((-4a + 2c)/9) <= -3 - 4a + 2c) or "
2114 "(exists (e0 = floor((-16 + 2c)/9): a = 4 and "
2115 "b = 3 and 9e0 <= -19 + 2c)) }" },
2116 { 1, "{ [y, x] : (x - y) mod 3 = 2 and 2 <= y <= 200 and 0 <= x <= 2;"
2117 "[1, 0] }" },
2118 { 1, "{ [x, y] : (x - y) mod 3 = 2 and 2 <= y <= 200 and 0 <= x <= 2;"
2119 "[0, 1] }" },
2120 { 1, "{ [1, y] : -1 <= y <= 1; [x, -x] : 0 <= x <= 1 }" },
2121 { 1, "{ [1, y] : 0 <= y <= 1; [x, -x] : 0 <= x <= 1 }" },
2122 { 1, "{ [x, y] : 0 <= x <= 10 and x - 4*floor(x/4) <= 1 and y <= 0; "
2123 "[x, y] : 0 <= x <= 10 and x - 4*floor(x/4) > 1 and y <= 0; "
2124 "[x, y] : 0 <= x <= 10 and x - 5*floor(x/5) <= 1 and 0 < y; "
2125 "[x, y] : 0 <= x <= 10 and x - 5*floor(x/5) > 1 and 0 < y }" },
2126 { 1, "{ [x, 0] : 0 <= x <= 10 and x mod 2 = 0; "
2127 "[x, 0] : 0 <= x <= 10 and x mod 2 = 1; "
2128 "[x, y] : 0 <= x <= 10 and 1 <= y <= 10 }" },
2129 { 1, "{ [a] : a <= 8 and "
2130 "(a mod 10 = 7 or a mod 10 = 8 or a mod 10 = 9) }" },
2131 { 1, "{ [x, y] : 2y = -x and x <= 0 or "
2132 "x <= -1 and 2y <= -x - 1 and 2y >= x - 1 }" },
2133 { 0, "{ [x, y] : 2y = -x and x <= 0 or "
2134 "x <= -2 and 2y <= -x - 1 and 2y >= x - 1 }" },
2135 { 1, "{ [a] : (a <= 0 and 3*floor((a)/3) = a) or "
2136 "(a < 0 and 3*floor((a)/3) < a) }" },
2137 { 1, "{ [a] : (a <= 0 and 3*floor((a)/3) = a) or "
2138 "(a < -1 and 3*floor((a)/3) < a) }" },
2139 { 1, "{ [a, b] : a <= 1024 and b >= 0 and "
2140 "((-31 - a + b <= 32*floor((-1 - a)/32) <= -33 + b and "
2141 "32*floor((-1 - a)/32) <= -16 + b + 16*floor((-1 - a)/16))"
2142 "or (2 <= a <= 15 and b < a)) }" },
2143 { 1, "{ [a] : a > 0 and ((16*floor((a)/16) < a and "
2144 "32*floor((a)/32) < a) or a <= 15) }" },
2145 { 1, "{ [a, b, c, d] : (-a + d) mod 64 = 0 and a <= 8 and b <= 1 and "
2146 "10 - a <= c <= 3 and d >= 5 and 9 - 64b <= d <= 70;"
2147 "[a, b = 1, c, d] : (-a + d) mod 64 = 0 and a <= 8 and c >= 4 and "
2148 "10 - a <= c <= 5 and 5 <= d <= 73 - c }" },
2149 { 1, "[n, m] -> { S_0[i] : (-n + i) mod 3 = 0 and m >= 3 + n and "
2150 "i >= n and 3*floor((2 + n + 2m)/3) <= n + 3m - i; "
2151 "S_0[n] : n <= m <= 2 + n }" },
2152 { 1, "{ [a, b] : exists (e0: 0 <= a <= 1 and b >= 0 and "
2153 "2e0 >= -5 + a + 2b and 2e0 >= -1 + a + b and "
2154 "2e0 <= a + b); "
2155 "[a, b] : exists (e0: 0 <= a <= 1 and 2e0 >= -5 + a + 2b and "
2156 "2e0 >= -1 - a + b and 2e0 <= -a + b and "
2157 "2e0 < -a + 2b) }" },
2158 { 1, "{ [i, j, i - 8j] : 8 <= i <= 63 and -7 + i <= 8j <= i; "
2159 "[i, 0, i] : 0 <= i <= 7 }" },
2160 { 1, "{ [a, b] : a >= 0 and 0 <= b <= 1 - a; [1, 1] }" },
2161 { 0, "{ [a, b] : a >= 0 and 0 <= b <= 1 - a; [0, 2] }" },
2162 { 0, "{ [a, b] : a >= 0 and 0 <= b <= 1 - a; [-1, 3] }" },
2163 { 1, "{ [a, b] : a, b >= 0 and a + 2b <= 2; [1, 1] }" },
2164 { 0, "{ [a, b] : a, b >= 0 and a + 2b <= 2; [2, 1] }" },
2165 { 0, "{ [a, c] : (2 + a) mod 4 = 0 or "
2166 "(c = 4 + a and 4 * floor((a)/4) = a and a >= 0 and a <= 4) or "
2167 "(c = 3 + a and 4 * floor((-1 + a)/4) = -1 + a and "
2168 "a > 0 and a <= 5) }" },
2169 { 1, "{ [1, 0, 0]; [a, b, c] : -1 <= -a < b <= 0 and 2c > b }" },
2170 { 0, "{ [j, a, l] : a mod 2 = 0 and j <= 29 and a >= 2 and "
2171 "2a <= -5 + j and 32j + 2a + 2 <= 4l < 33j; "
2172 "[j, 0, l] : 4 <= j <= 29 and -3 + 33j <= 4l <= 33j }" },
2175 /* A specialized coalescing test case that would result
2176 * in a segmentation fault or a failed assertion in earlier versions of isl.
2178 static int test_coalesce_special(struct isl_ctx *ctx)
2180 const char *str;
2181 isl_map *map1, *map2;
2183 str = "[y] -> { [S_L220_OUT[] -> T7[]] -> "
2184 "[[S_L309_IN[] -> T11[]] -> ce_imag2[1, o1]] : "
2185 "(y = 201 and o1 <= 239 and o1 >= 212) or "
2186 "(exists (e0 = [(y)/3]: 3e0 = y and y <= 198 and y >= 3 and "
2187 "o1 <= 239 and o1 >= 212)) or "
2188 "(exists (e0 = [(y)/3]: 3e0 = y and y <= 201 and y >= 3 and "
2189 "o1 <= 241 and o1 >= 240));"
2190 "[S_L220_OUT[] -> T7[]] -> "
2191 "[[S_L309_IN[] -> T11[]] -> ce_imag2[0, o1]] : "
2192 "(y = 2 and o1 <= 241 and o1 >= 212) or "
2193 "(exists (e0 = [(-2 + y)/3]: 3e0 = -2 + y and y <= 200 and "
2194 "y >= 5 and o1 <= 241 and o1 >= 212)) }";
2195 map1 = isl_map_read_from_str(ctx, str);
2196 map1 = isl_map_align_divs_internal(map1);
2197 map1 = isl_map_coalesce(map1);
2198 str = "[y] -> { [S_L220_OUT[] -> T7[]] -> "
2199 "[[S_L309_IN[] -> T11[]] -> ce_imag2[o0, o1]] : "
2200 "exists (e0 = [(-1 - y + o0)/3]: 3e0 = -1 - y + o0 and "
2201 "y <= 201 and o0 <= 2 and o1 >= 212 and o1 <= 241 and "
2202 "o0 >= 3 - y and o0 <= -2 + y and o0 >= 0) }";
2203 map2 = isl_map_read_from_str(ctx, str);
2204 map2 = isl_map_union(map2, map1);
2205 map2 = isl_map_align_divs_internal(map2);
2206 map2 = isl_map_coalesce(map2);
2207 isl_map_free(map2);
2208 if (!map2)
2209 return -1;
2211 return 0;
2214 /* A specialized coalescing test case that would result in an assertion
2215 * in an earlier version of isl.
2216 * The explicit call to isl_basic_set_union prevents the implicit
2217 * equality constraints in the first basic map from being detected prior
2218 * to the call to isl_set_coalesce, at least at the point
2219 * where this test case was introduced.
2221 static int test_coalesce_special2(struct isl_ctx *ctx)
2223 const char *str;
2224 isl_basic_set *bset1, *bset2;
2225 isl_set *set;
2227 str = "{ [x, y] : x, y >= 0 and x + 2y <= 1 and 2x + y <= 1 }";
2228 bset1 = isl_basic_set_read_from_str(ctx, str);
2229 str = "{ [x,0] : -1 <= x <= 1 and x mod 2 = 1 }" ;
2230 bset2 = isl_basic_set_read_from_str(ctx, str);
2231 set = isl_basic_set_union(bset1, bset2);
2232 set = isl_set_coalesce(set);
2233 isl_set_free(set);
2235 if (!set)
2236 return -1;
2237 return 0;
2240 /* Check that calling isl_set_coalesce does not leave other sets
2241 * that may share some information with the input to isl_set_coalesce
2242 * in an inconsistent state.
2243 * In particular, older versions of isl would modify all copies
2244 * of the basic sets in the isl_set_coalesce input in a way
2245 * that could leave them in an inconsistent state.
2246 * The result of printing any other set containing one of these
2247 * basic sets would then result in an invalid set description.
2249 static int test_coalesce_special3(isl_ctx *ctx)
2251 const char *str;
2252 char *s;
2253 isl_set *set1, *set2;
2254 isl_printer *p;
2256 set1 = isl_set_read_from_str(ctx, "{ [0, 0, 0] }");
2257 str = "{ [a, b, a + b] : a >= 0 and b >= 0 and 0 < a + b }";
2258 set2 = isl_set_read_from_str(ctx, str);
2259 set1 = isl_set_union(set1, isl_set_copy(set2));
2260 set1 = isl_set_coalesce(set1);
2261 isl_set_free(set1);
2263 p = isl_printer_to_str(ctx);
2264 p = isl_printer_print_set(p, set2);
2265 isl_set_free(set2);
2266 s = isl_printer_get_str(p);
2267 isl_printer_free(p);
2268 set1 = isl_set_read_from_str(ctx, s);
2269 free(s);
2270 isl_set_free(set1);
2272 if (!set1)
2273 return -1;
2275 return 0;
2278 /* Check that calling isl_set_coalesce on the intersection of
2279 * the sets described by "s1" and "s2" does not leave other sets
2280 * that may share some information with the input to isl_set_coalesce
2281 * in an inconsistent state.
2282 * In particular, when isl_set_coalesce detects equality constraints,
2283 * it does not immediately perform Gaussian elimination on them,
2284 * but then it needs to ensure that it is performed at some point.
2285 * The input set has implicit equality constraints in the first disjunct.
2286 * It is constructed as an intersection, because otherwise
2287 * those equality constraints would already be detected during parsing.
2289 static isl_stat test_coalesce_intersection(isl_ctx *ctx,
2290 const char *s1, const char *s2)
2292 isl_set *set1, *set2;
2294 set1 = isl_set_read_from_str(ctx, s1);
2295 set2 = isl_set_read_from_str(ctx, s2);
2296 set1 = isl_set_intersect(set1, set2);
2297 isl_set_free(isl_set_coalesce(isl_set_copy(set1)));
2298 set1 = isl_set_coalesce(set1);
2299 isl_set_free(set1);
2301 if (!set1)
2302 return isl_stat_error;
2304 return isl_stat_ok;
2307 /* Check that calling isl_set_coalesce does not leave other sets
2308 * that may share some information with the input to isl_set_coalesce
2309 * in an inconsistent state, for the case where one disjunct
2310 * is a subset of the other.
2312 static isl_stat test_coalesce_special4(isl_ctx *ctx)
2314 const char *s1, *s2;
2316 s1 = "{ [a, b] : b <= 0 or a <= 1 }";
2317 s2 = "{ [a, b] : -1 <= -a < b }";
2318 return test_coalesce_intersection(ctx, s1, s2);
2321 /* Check that calling isl_set_coalesce does not leave other sets
2322 * that may share some information with the input to isl_set_coalesce
2323 * in an inconsistent state, for the case where two disjuncts
2324 * can be fused.
2326 static isl_stat test_coalesce_special5(isl_ctx *ctx)
2328 const char *s1, *s2;
2330 s1 = "{ [a, b, c] : b <= 0 }";
2331 s2 = "{ [a, b, c] : -1 <= -a < b and (c >= 0 or c < 0) }";
2332 return test_coalesce_intersection(ctx, s1, s2);
2335 /* Check that calling isl_set_coalesce does not leave other sets
2336 * that may share some information with the input to isl_set_coalesce
2337 * in an inconsistent state, for the case where two disjuncts
2338 * can be fused and where both disjuncts have implicit equality constraints.
2340 static isl_stat test_coalesce_special6(isl_ctx *ctx)
2342 const char *s1, *s2;
2344 s1 = "{ [a, b, c] : c <= 0 }";
2345 s2 = "{ [a, b, c] : 0 <= a <= b <= c or (0 <= b <= c and a > 0) }";
2346 return test_coalesce_intersection(ctx, s1, s2);
2349 /* Test the functionality of isl_set_coalesce.
2350 * That is, check that the output is always equal to the input
2351 * and in some cases that the result consists of a single disjunct.
2353 static int test_coalesce(struct isl_ctx *ctx)
2355 int i;
2357 for (i = 0; i < ARRAY_SIZE(coalesce_tests); ++i) {
2358 const char *str = coalesce_tests[i].str;
2359 int check_one = coalesce_tests[i].single_disjunct;
2360 if (test_coalesce_set(ctx, str, check_one) < 0)
2361 return -1;
2364 if (test_coalesce_unbounded_wrapping(ctx) < 0)
2365 return -1;
2366 if (test_coalesce_special(ctx) < 0)
2367 return -1;
2368 if (test_coalesce_special2(ctx) < 0)
2369 return -1;
2370 if (test_coalesce_special3(ctx) < 0)
2371 return -1;
2372 if (test_coalesce_special4(ctx) < 0)
2373 return -1;
2374 if (test_coalesce_special5(ctx) < 0)
2375 return -1;
2376 if (test_coalesce_special6(ctx) < 0)
2377 return -1;
2380 return 0;
2383 /* Construct a representation of the graph on the right of Figure 1
2384 * in "Computing the Transitive Closure of a Union of
2385 * Affine Integer Tuple Relations".
2387 static __isl_give isl_map *cocoa_fig_1_right_graph(isl_ctx *ctx)
2389 isl_set *dom;
2390 isl_map *up, *right;
2392 dom = isl_set_read_from_str(ctx,
2393 "{ [x,y] : x >= 0 and -2 x + 3 y >= 0 and x <= 3 and "
2394 "2 x - 3 y + 3 >= 0 }");
2395 right = isl_map_read_from_str(ctx,
2396 "{ [x,y] -> [x2,y2] : x2 = x + 1 and y2 = y }");
2397 up = isl_map_read_from_str(ctx,
2398 "{ [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 }");
2399 right = isl_map_intersect_domain(right, isl_set_copy(dom));
2400 right = isl_map_intersect_range(right, isl_set_copy(dom));
2401 up = isl_map_intersect_domain(up, isl_set_copy(dom));
2402 up = isl_map_intersect_range(up, dom);
2403 return isl_map_union(up, right);
2406 /* Construct a representation of the power of the graph
2407 * on the right of Figure 1 in "Computing the Transitive Closure of
2408 * a Union of Affine Integer Tuple Relations".
2410 static __isl_give isl_map *cocoa_fig_1_right_power(isl_ctx *ctx)
2412 return isl_map_read_from_str(ctx,
2413 "{ [1] -> [[0,0] -> [0,1]]; [2] -> [[0,0] -> [1,1]]; "
2414 " [1] -> [[0,1] -> [1,1]]; [1] -> [[2,2] -> [3,2]]; "
2415 " [2] -> [[2,2] -> [3,3]]; [1] -> [[3,2] -> [3,3]] }");
2418 /* Construct a representation of the transitive closure of the graph
2419 * on the right of Figure 1 in "Computing the Transitive Closure of
2420 * a Union of Affine Integer Tuple Relations".
2422 static __isl_give isl_map *cocoa_fig_1_right_tc(isl_ctx *ctx)
2424 return isl_set_unwrap(isl_map_range(cocoa_fig_1_right_power(ctx)));
2427 static int test_closure(isl_ctx *ctx)
2429 const char *str;
2430 isl_map *map, *map2;
2431 isl_bool exact, equal;
2433 /* COCOA example 1 */
2434 map = isl_map_read_from_str(ctx,
2435 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
2436 "1 <= i and i < n and 1 <= j and j < n or "
2437 "i2 = i + 1 and j2 = j - 1 and "
2438 "1 <= i and i < n and 2 <= j and j <= n }");
2439 map = isl_map_power(map, &exact);
2440 assert(exact);
2441 isl_map_free(map);
2443 /* COCOA example 1 */
2444 map = isl_map_read_from_str(ctx,
2445 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
2446 "1 <= i and i < n and 1 <= j and j < n or "
2447 "i2 = i + 1 and j2 = j - 1 and "
2448 "1 <= i and i < n and 2 <= j and j <= 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] : exists (k1,k2,k : "
2453 "1 <= i and i < n and 1 <= j and j <= n and "
2454 "2 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
2455 "i2 = i + k1 + k2 and j2 = j + k1 - k2 and "
2456 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1 )}");
2457 assert(isl_map_is_equal(map, map2));
2458 isl_map_free(map2);
2459 isl_map_free(map);
2461 map = isl_map_read_from_str(ctx,
2462 "[n] -> { [x] -> [y] : y = x + 1 and 0 <= x and x <= n and "
2463 " 0 <= y and y <= n }");
2464 map = isl_map_transitive_closure(map, &exact);
2465 map2 = isl_map_read_from_str(ctx,
2466 "[n] -> { [x] -> [y] : y > x and 0 <= x and x <= n and "
2467 " 0 <= y and y <= n }");
2468 assert(isl_map_is_equal(map, map2));
2469 isl_map_free(map2);
2470 isl_map_free(map);
2472 /* COCOA example 2 */
2473 map = isl_map_read_from_str(ctx,
2474 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j + 2 and "
2475 "1 <= i and i < n - 1 and 1 <= j and j < n - 1 or "
2476 "i2 = i + 2 and j2 = j - 2 and "
2477 "1 <= i and i < n - 1 and 3 <= j and j <= n }");
2478 map = isl_map_transitive_closure(map, &exact);
2479 assert(exact);
2480 map2 = isl_map_read_from_str(ctx,
2481 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
2482 "1 <= i and i < n - 1 and 1 <= j and j <= n and "
2483 "3 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
2484 "i2 = i + 2 k1 + 2 k2 and j2 = j + 2 k1 - 2 k2 and "
2485 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1) }");
2486 assert(isl_map_is_equal(map, map2));
2487 isl_map_free(map);
2488 isl_map_free(map2);
2490 /* COCOA Fig.2 left */
2491 map = isl_map_read_from_str(ctx,
2492 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j and "
2493 "i <= 2 j - 3 and i <= n - 2 and j <= 2 i - 1 and "
2494 "j <= n or "
2495 "i2 = i and j2 = j + 2 and i <= 2 j - 1 and i <= n and "
2496 "j <= 2 i - 3 and j <= n - 2 or "
2497 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
2498 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
2499 map = isl_map_transitive_closure(map, &exact);
2500 assert(exact);
2501 isl_map_free(map);
2503 /* COCOA Fig.2 right */
2504 map = isl_map_read_from_str(ctx,
2505 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
2506 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
2507 "j <= n or "
2508 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
2509 "j <= 2 i - 4 and j <= n - 3 or "
2510 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
2511 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
2512 map = isl_map_power(map, &exact);
2513 assert(exact);
2514 isl_map_free(map);
2516 /* COCOA Fig.2 right */
2517 map = isl_map_read_from_str(ctx,
2518 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
2519 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
2520 "j <= n or "
2521 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
2522 "j <= 2 i - 4 and j <= n - 3 or "
2523 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
2524 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
2525 map = isl_map_transitive_closure(map, &exact);
2526 assert(exact);
2527 map2 = isl_map_read_from_str(ctx,
2528 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k3,k : "
2529 "i <= 2 j - 1 and i <= n and j <= 2 i - 1 and "
2530 "j <= n and 3 + i + 2 j <= 3 n and "
2531 "3 + 2 i + j <= 3n and i2 <= 2 j2 -1 and i2 <= n and "
2532 "i2 <= 3 j2 - 4 and j2 <= 2 i2 -1 and j2 <= n and "
2533 "13 + 4 j2 <= 11 i2 and i2 = i + 3 k1 + k3 and "
2534 "j2 = j + 3 k2 + k3 and k1 >= 0 and k2 >= 0 and "
2535 "k3 >= 0 and k1 + k2 + k3 = k and k > 0) }");
2536 assert(isl_map_is_equal(map, map2));
2537 isl_map_free(map2);
2538 isl_map_free(map);
2540 map = cocoa_fig_1_right_graph(ctx);
2541 map = isl_map_transitive_closure(map, &exact);
2542 assert(exact);
2543 map2 = cocoa_fig_1_right_tc(ctx);
2544 assert(isl_map_is_equal(map, map2));
2545 isl_map_free(map2);
2546 isl_map_free(map);
2548 map = cocoa_fig_1_right_graph(ctx);
2549 map = isl_map_power(map, &exact);
2550 map2 = cocoa_fig_1_right_power(ctx);
2551 equal = isl_map_is_equal(map, map2);
2552 isl_map_free(map2);
2553 isl_map_free(map);
2554 if (equal < 0)
2555 return -1;
2556 if (!exact)
2557 isl_die(ctx, isl_error_unknown, "power not exact", return -1);
2558 if (!equal)
2559 isl_die(ctx, isl_error_unknown, "unexpected power", return -1);
2561 /* COCOA Theorem 1 counter example */
2562 map = isl_map_read_from_str(ctx,
2563 "{ [i,j] -> [i2,j2] : i = 0 and 0 <= j and j <= 1 and "
2564 "i2 = 1 and j2 = j or "
2565 "i = 0 and j = 0 and i2 = 0 and j2 = 1 }");
2566 map = isl_map_transitive_closure(map, &exact);
2567 assert(exact);
2568 isl_map_free(map);
2570 map = isl_map_read_from_str(ctx,
2571 "[m,n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 2 and "
2572 "1 <= i,i2 <= n and 1 <= j,j2 <= m or "
2573 "i2 = i + 1 and 3 <= j2 - j <= 4 and "
2574 "1 <= i,i2 <= n and 1 <= j,j2 <= m }");
2575 map = isl_map_transitive_closure(map, &exact);
2576 assert(exact);
2577 isl_map_free(map);
2579 /* Kelly et al 1996, fig 12 */
2580 map = isl_map_read_from_str(ctx,
2581 "[n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 1 and "
2582 "1 <= i,j,j+1 <= n or "
2583 "j = n and j2 = 1 and i2 = i + 1 and "
2584 "1 <= i,i+1 <= n }");
2585 map = isl_map_transitive_closure(map, &exact);
2586 assert(exact);
2587 map2 = isl_map_read_from_str(ctx,
2588 "[n] -> { [i,j] -> [i2,j2] : 1 <= j < j2 <= n and "
2589 "1 <= i <= n and i = i2 or "
2590 "1 <= i < i2 <= n and 1 <= j <= n and "
2591 "1 <= j2 <= n }");
2592 assert(isl_map_is_equal(map, map2));
2593 isl_map_free(map2);
2594 isl_map_free(map);
2596 /* Omega's closure4 */
2597 map = isl_map_read_from_str(ctx,
2598 "[m,n] -> { [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 and "
2599 "1 <= x,y <= 10 or "
2600 "x2 = x + 1 and y2 = y and "
2601 "1 <= x <= 20 && 5 <= y <= 15 }");
2602 map = isl_map_transitive_closure(map, &exact);
2603 assert(exact);
2604 isl_map_free(map);
2606 map = isl_map_read_from_str(ctx,
2607 "[n] -> { [x] -> [y]: 1 <= n <= y - x <= 10 }");
2608 map = isl_map_transitive_closure(map, &exact);
2609 assert(!exact);
2610 map2 = isl_map_read_from_str(ctx,
2611 "[n] -> { [x] -> [y] : 1 <= n <= 10 and y >= n + x }");
2612 assert(isl_map_is_equal(map, map2));
2613 isl_map_free(map);
2614 isl_map_free(map2);
2616 str = "[n, m] -> { [i0, i1, i2, i3] -> [o0, o1, o2, o3] : "
2617 "i3 = 1 and o0 = i0 and o1 = -1 + i1 and o2 = -1 + i2 and "
2618 "o3 = -2 + i2 and i1 <= -1 + i0 and i1 >= 1 - m + i0 and "
2619 "i1 >= 2 and i1 <= n and i2 >= 3 and i2 <= 1 + n and i2 <= m }";
2620 map = isl_map_read_from_str(ctx, str);
2621 map = isl_map_transitive_closure(map, &exact);
2622 assert(exact);
2623 map2 = isl_map_read_from_str(ctx, str);
2624 assert(isl_map_is_equal(map, map2));
2625 isl_map_free(map);
2626 isl_map_free(map2);
2628 str = "{[0] -> [1]; [2] -> [3]}";
2629 map = isl_map_read_from_str(ctx, str);
2630 map = isl_map_transitive_closure(map, &exact);
2631 assert(exact);
2632 map2 = isl_map_read_from_str(ctx, str);
2633 assert(isl_map_is_equal(map, map2));
2634 isl_map_free(map);
2635 isl_map_free(map2);
2637 str = "[n] -> { [[i0, i1, 1, 0, i0] -> [i5, 1]] -> "
2638 "[[i0, -1 + i1, 2, 0, i0] -> [-1 + i5, 2]] : "
2639 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 2 and "
2640 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2641 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
2642 "[[i0, i1, 2, 0, i0] -> [i5, 1]] -> "
2643 "[[i0, i1, 1, 0, i0] -> [-1 + i5, 2]] : "
2644 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 1 and "
2645 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2646 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
2647 "[[i0, i1, 1, 0, i0] -> [i5, 2]] -> "
2648 "[[i0, -1 + i1, 2, 0, i0] -> [i5, 1]] : "
2649 "exists (e0 = [(3 - n)/3]: i1 >= 2 and i5 >= 1 and "
2650 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2651 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
2652 "[[i0, i1, 2, 0, i0] -> [i5, 2]] -> "
2653 "[[i0, i1, 1, 0, i0] -> [i5, 1]] : "
2654 "exists (e0 = [(3 - n)/3]: i5 >= 1 and i1 >= 1 and "
2655 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
2656 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n) }";
2657 map = isl_map_read_from_str(ctx, str);
2658 map = isl_map_transitive_closure(map, NULL);
2659 assert(map);
2660 isl_map_free(map);
2662 return 0;
2665 /* Check that the actual result of a boolean operation is equal
2666 * to the expected result.
2668 static isl_stat check_bool(isl_ctx *ctx, isl_bool actual, isl_bool expected)
2670 if (actual != expected)
2671 isl_die(ctx, isl_error_unknown,
2672 "incorrect boolean operation", return isl_stat_error);
2673 return isl_stat_ok;
2676 /* Test operations on isl_bool values.
2678 * This tests:
2680 * isl_bool_not
2681 * isl_bool_ok
2683 static int test_isl_bool(isl_ctx *ctx)
2685 if (check_bool(ctx, isl_bool_not(isl_bool_true), isl_bool_false) < 0)
2686 return -1;
2687 if (check_bool(ctx, isl_bool_not(isl_bool_false), isl_bool_true) < 0)
2688 return -1;
2689 if (check_bool(ctx, isl_bool_not(isl_bool_error), isl_bool_error) < 0)
2690 return -1;
2691 if (check_bool(ctx, isl_bool_ok(0), isl_bool_false) < 0)
2692 return -1;
2693 if (check_bool(ctx, isl_bool_ok(1), isl_bool_true) < 0)
2694 return -1;
2695 if (check_bool(ctx, isl_bool_ok(-1), isl_bool_true) < 0)
2696 return -1;
2697 if (check_bool(ctx, isl_bool_ok(2), isl_bool_true) < 0)
2698 return -1;
2699 if (check_bool(ctx, isl_bool_ok(-2), isl_bool_true) < 0)
2700 return -1;
2702 return 0;
2705 static int test_lex(struct isl_ctx *ctx)
2707 isl_space *dim;
2708 isl_map *map;
2709 int empty;
2711 dim = isl_space_set_alloc(ctx, 0, 0);
2712 map = isl_map_lex_le(dim);
2713 empty = isl_map_is_empty(map);
2714 isl_map_free(map);
2716 if (empty < 0)
2717 return -1;
2718 if (empty)
2719 isl_die(ctx, isl_error_unknown,
2720 "expecting non-empty result", return -1);
2722 return 0;
2725 /* Inputs for isl_map_lexmin tests.
2726 * "map" is the input and "lexmin" is the expected result.
2728 struct {
2729 const char *map;
2730 const char *lexmin;
2731 } lexmin_tests [] = {
2732 { "{ [x] -> [y] : x <= y <= 10; [x] -> [5] : -8 <= x <= 8 }",
2733 "{ [x] -> [5] : 6 <= x <= 8; "
2734 "[x] -> [x] : x <= 5 or (9 <= x <= 10) }" },
2735 { "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }",
2736 "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }" },
2737 { "{ [x] -> [y] : x = 4y; [x] -> [y] : x = 2y }",
2738 "{ [x] -> [y] : (4y = x and x >= 0) or "
2739 "(exists (e0 = [(x)/4], e1 = [(-2 + x)/4]: 2y = x and "
2740 "4e1 = -2 + x and 4e0 <= -1 + x and 4e0 >= -3 + x)) or "
2741 "(exists (e0 = [(x)/4]: 2y = x and 4e0 = x and x <= -4)) }" },
2742 { "{ T[a] -> S[b, c] : a = 4b-2c and c >= b }",
2743 "{ T[a] -> S[b, c] : 2b = a and 2c = a }" },
2744 /* Check that empty pieces are properly combined. */
2745 { "[K, N] -> { [x, y] -> [a, b] : K+2<=N<=K+4 and x>=4 and "
2746 "2N-6<=x<K+N and N-1<=a<=K+N-1 and N+b-6<=a<=2N-4 and "
2747 "b<=2N-3K+a and 3b<=4N-K+1 and b>=N and a>=x+1 }",
2748 "[K, N] -> { [x, y] -> [1 + x, N] : x >= -6 + 2N and "
2749 "x <= -5 + 2N and x >= -1 + 3K - N and x <= -2 + K + N and "
2750 "x >= 4 }" },
2751 { "{ [i, k, j] -> [a, b, c, d] : 8*floor((b)/8) = b and k <= 255 and "
2752 "a <= 255 and c <= 255 and d <= 255 - j and "
2753 "255 - j <= 7d <= 7 - i and 240d <= 239 + a and "
2754 "247d <= 247 + k - j and 247d <= 247 + k - b and "
2755 "247d <= 247 + i and 248 - b <= 248d <= c and "
2756 "254d >= i - a + b and 254d >= -a + b and "
2757 "255d >= -i + a - b and 1792d >= -63736 + 257b }",
2758 "{ [i, k, j] -> "
2759 "[-127762 + i + 502j, -62992 + 248j, 63240 - 248j, 255 - j] : "
2760 "k <= 255 and 7j >= 1778 + i and 246j >= 62738 - k and "
2761 "247j >= 62738 - i and 509j <= 129795 + i and "
2762 "742j >= 188724 - i; "
2763 "[0, k, j] -> [1, 0, 248, 1] : k <= 255 and 248 <= j <= 254, k }" },
2764 { "{ [a] -> [b] : 0 <= b <= 255 and -509 + a <= 512b < a and "
2765 "16*floor((8 + b)/16) <= 7 + b; "
2766 "[a] -> [1] }",
2767 "{ [a] -> [b = 1] : a >= 510 or a <= 0; "
2768 "[a] -> [b = 0] : 0 < a <= 509 }" },
2769 { "{ rat: [i] : 1 <= 2i <= 9 }", "{ rat: [i] : 2i = 1 }" },
2770 { "{ rat: [i] : 1 <= 2i <= 9 or i >= 10 }", "{ rat: [i] : 2i = 1 }" },
2773 static int test_lexmin(struct isl_ctx *ctx)
2775 int i;
2776 int equal;
2777 const char *str;
2778 isl_basic_map *bmap;
2779 isl_map *map, *map2;
2780 isl_set *set;
2781 isl_set *set2;
2782 isl_pw_multi_aff *pma;
2784 str = "[p0, p1] -> { [] -> [] : "
2785 "exists (e0 = [(2p1)/3], e1, e2, e3 = [(3 - p1 + 3e0)/3], "
2786 "e4 = [(p1)/3], e5 = [(p1 + 3e4)/3]: "
2787 "3e0 >= -2 + 2p1 and 3e0 >= p1 and 3e3 >= 1 - p1 + 3e0 and "
2788 "3e0 <= 2p1 and 3e3 >= -2 + p1 and 3e3 <= -1 + p1 and p1 >= 3 and "
2789 "3e5 >= -2 + 2p1 and 3e5 >= p1 and 3e5 <= -1 + p1 + 3e4 and "
2790 "3e4 <= p1 and 3e4 >= -2 + p1 and e3 <= -1 + e0 and "
2791 "3e4 >= 6 - p1 + 3e1 and 3e1 >= p1 and 3e5 >= -2 + p1 + 3e4 and "
2792 "2e4 >= 3 - p1 + 2e1 and e4 <= e1 and 3e3 <= 2 - p1 + 3e0 and "
2793 "e5 >= 1 + e1 and 3e4 >= 6 - 2p1 + 3e1 and "
2794 "p0 >= 2 and p1 >= p0 and 3e2 >= p1 and 3e4 >= 6 - p1 + 3e2 and "
2795 "e2 <= e1 and e3 >= 1 and e4 <= e2) }";
2796 map = isl_map_read_from_str(ctx, str);
2797 map = isl_map_lexmin(map);
2798 isl_map_free(map);
2800 str = "[C] -> { [obj,a,b,c] : obj <= 38 a + 7 b + 10 c and "
2801 "a + b <= 1 and c <= 10 b and c <= C and a,b,c,C >= 0 }";
2802 set = isl_set_read_from_str(ctx, str);
2803 set = isl_set_lexmax(set);
2804 str = "[C] -> { [obj,a,b,c] : C = 8 }";
2805 set2 = isl_set_read_from_str(ctx, str);
2806 set = isl_set_intersect(set, set2);
2807 assert(!isl_set_is_empty(set));
2808 isl_set_free(set);
2810 for (i = 0; i < ARRAY_SIZE(lexmin_tests); ++i) {
2811 map = isl_map_read_from_str(ctx, lexmin_tests[i].map);
2812 map = isl_map_lexmin(map);
2813 map2 = isl_map_read_from_str(ctx, lexmin_tests[i].lexmin);
2814 equal = isl_map_is_equal(map, map2);
2815 isl_map_free(map);
2816 isl_map_free(map2);
2818 if (equal < 0)
2819 return -1;
2820 if (!equal)
2821 isl_die(ctx, isl_error_unknown,
2822 "unexpected result", return -1);
2825 str = "{ [i] -> [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
2826 " 8i' <= i and 8i' >= -7 + i }";
2827 bmap = isl_basic_map_read_from_str(ctx, str);
2828 pma = isl_basic_map_lexmin_pw_multi_aff(isl_basic_map_copy(bmap));
2829 map2 = isl_map_from_pw_multi_aff(pma);
2830 map = isl_map_from_basic_map(bmap);
2831 assert(isl_map_is_equal(map, map2));
2832 isl_map_free(map);
2833 isl_map_free(map2);
2835 str = "[i] -> { [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
2836 " 8i' <= i and 8i' >= -7 + i }";
2837 set = isl_set_read_from_str(ctx, str);
2838 pma = isl_set_lexmin_pw_multi_aff(isl_set_copy(set));
2839 set2 = isl_set_from_pw_multi_aff(pma);
2840 equal = isl_set_is_equal(set, set2);
2841 isl_set_free(set);
2842 isl_set_free(set2);
2843 if (equal < 0)
2844 return -1;
2845 if (!equal)
2846 isl_die(ctx, isl_error_unknown,
2847 "unexpected difference between set and "
2848 "piecewise affine expression", return -1);
2850 return 0;
2853 /* A specialized isl_set_min_val test case that would return the wrong result
2854 * in earlier versions of isl.
2855 * The explicit call to isl_basic_set_union prevents the second basic set
2856 * from being determined to be empty prior to the call to isl_set_min_val,
2857 * at least at the point where this test case was introduced.
2859 static int test_min_special(isl_ctx *ctx)
2861 const char *str;
2862 isl_basic_set *bset1, *bset2;
2863 isl_set *set;
2864 isl_aff *obj;
2865 isl_val *res;
2866 int ok;
2868 str = "{ [a, b] : a >= 2 and b >= 0 and 14 - a <= b <= 9 }";
2869 bset1 = isl_basic_set_read_from_str(ctx, str);
2870 str = "{ [a, b] : 1 <= a, b and a + b <= 1 }";
2871 bset2 = isl_basic_set_read_from_str(ctx, str);
2872 set = isl_basic_set_union(bset1, bset2);
2873 obj = isl_aff_read_from_str(ctx, "{ [a, b] -> [a] }");
2875 res = isl_set_min_val(set, obj);
2876 ok = isl_val_cmp_si(res, 5) == 0;
2878 isl_aff_free(obj);
2879 isl_set_free(set);
2880 isl_val_free(res);
2882 if (!res)
2883 return -1;
2884 if (!ok)
2885 isl_die(ctx, isl_error_unknown, "unexpected minimum",
2886 return -1);
2888 return 0;
2891 /* A specialized isl_set_min_val test case that would return an error
2892 * in earlier versions of isl.
2894 static int test_min_special2(isl_ctx *ctx)
2896 const char *str;
2897 isl_basic_set *bset;
2898 isl_aff *obj;
2899 isl_val *res;
2901 str = "{ [i, j, k] : 2j = i and 2k = i + 1 and i >= 2 }";
2902 bset = isl_basic_set_read_from_str(ctx, str);
2904 obj = isl_aff_read_from_str(ctx, "{ [i, j, k] -> [i] }");
2906 res = isl_basic_set_max_val(bset, obj);
2908 isl_basic_set_free(bset);
2909 isl_aff_free(obj);
2910 isl_val_free(res);
2912 if (!res)
2913 return -1;
2915 return 0;
2918 struct {
2919 const char *set;
2920 const char *obj;
2921 __isl_give isl_val *(*fn)(__isl_keep isl_set *set,
2922 __isl_keep isl_aff *obj);
2923 const char *res;
2924 } opt_tests[] = {
2925 { "{ [-1]; [1] }", "{ [x] -> [x] }", &isl_set_min_val, "-1" },
2926 { "{ [-1]; [1] }", "{ [x] -> [x] }", &isl_set_max_val, "1" },
2927 { "{ [a, b] : 0 <= a, b <= 100 and b mod 2 = 0}",
2928 "{ [a, b] -> [floor((b - 2*floor((-a)/4))/5)] }",
2929 &isl_set_max_val, "30" },
2933 /* Perform basic isl_set_min_val and isl_set_max_val tests.
2934 * In particular, check the results on non-convex inputs.
2936 static int test_min(struct isl_ctx *ctx)
2938 int i;
2939 isl_set *set;
2940 isl_aff *obj;
2941 isl_val *val, *res;
2942 isl_bool ok;
2944 for (i = 0; i < ARRAY_SIZE(opt_tests); ++i) {
2945 set = isl_set_read_from_str(ctx, opt_tests[i].set);
2946 obj = isl_aff_read_from_str(ctx, opt_tests[i].obj);
2947 res = isl_val_read_from_str(ctx, opt_tests[i].res);
2948 val = opt_tests[i].fn(set, obj);
2949 ok = isl_val_eq(res, val);
2950 isl_val_free(res);
2951 isl_val_free(val);
2952 isl_aff_free(obj);
2953 isl_set_free(set);
2955 if (ok < 0)
2956 return -1;
2957 if (!ok)
2958 isl_die(ctx, isl_error_unknown,
2959 "unexpected optimum", return -1);
2962 if (test_min_special(ctx) < 0)
2963 return -1;
2964 if (test_min_special2(ctx) < 0)
2965 return -1;
2967 return 0;
2970 struct must_may {
2971 isl_map *must;
2972 isl_map *may;
2975 static isl_stat collect_must_may(__isl_take isl_map *dep, int must,
2976 void *dep_user, void *user)
2978 struct must_may *mm = (struct must_may *)user;
2980 if (must)
2981 mm->must = isl_map_union(mm->must, dep);
2982 else
2983 mm->may = isl_map_union(mm->may, dep);
2985 return isl_stat_ok;
2988 static int common_space(void *first, void *second)
2990 int depth = *(int *)first;
2991 return 2 * depth;
2994 static int map_is_equal(__isl_keep isl_map *map, const char *str)
2996 isl_map *map2;
2997 int equal;
2999 if (!map)
3000 return -1;
3002 map2 = isl_map_read_from_str(map->ctx, str);
3003 equal = isl_map_is_equal(map, map2);
3004 isl_map_free(map2);
3006 return equal;
3009 static int map_check_equal(__isl_keep isl_map *map, const char *str)
3011 int equal;
3013 equal = map_is_equal(map, str);
3014 if (equal < 0)
3015 return -1;
3016 if (!equal)
3017 isl_die(isl_map_get_ctx(map), isl_error_unknown,
3018 "result not as expected", return -1);
3019 return 0;
3022 static int test_dep(struct isl_ctx *ctx)
3024 const char *str;
3025 isl_space *dim;
3026 isl_map *map;
3027 isl_access_info *ai;
3028 isl_flow *flow;
3029 int depth;
3030 struct must_may mm;
3032 depth = 3;
3034 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
3035 map = isl_map_read_from_str(ctx, str);
3036 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
3038 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
3039 map = isl_map_read_from_str(ctx, str);
3040 ai = isl_access_info_add_source(ai, map, 1, &depth);
3042 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
3043 map = isl_map_read_from_str(ctx, str);
3044 ai = isl_access_info_add_source(ai, map, 1, &depth);
3046 flow = isl_access_info_compute_flow(ai);
3047 dim = isl_space_alloc(ctx, 0, 3, 3);
3048 mm.must = isl_map_empty(isl_space_copy(dim));
3049 mm.may = isl_map_empty(dim);
3051 isl_flow_foreach(flow, collect_must_may, &mm);
3053 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10); "
3054 " [1,10,0] -> [2,5,0] }";
3055 assert(map_is_equal(mm.must, str));
3056 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
3057 assert(map_is_equal(mm.may, str));
3059 isl_map_free(mm.must);
3060 isl_map_free(mm.may);
3061 isl_flow_free(flow);
3064 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
3065 map = isl_map_read_from_str(ctx, str);
3066 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
3068 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
3069 map = isl_map_read_from_str(ctx, str);
3070 ai = isl_access_info_add_source(ai, map, 1, &depth);
3072 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
3073 map = isl_map_read_from_str(ctx, str);
3074 ai = isl_access_info_add_source(ai, map, 0, &depth);
3076 flow = isl_access_info_compute_flow(ai);
3077 dim = isl_space_alloc(ctx, 0, 3, 3);
3078 mm.must = isl_map_empty(isl_space_copy(dim));
3079 mm.may = isl_map_empty(dim);
3081 isl_flow_foreach(flow, collect_must_may, &mm);
3083 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10) }";
3084 assert(map_is_equal(mm.must, str));
3085 str = "{ [0,5,0] -> [2,5,0]; [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
3086 assert(map_is_equal(mm.may, str));
3088 isl_map_free(mm.must);
3089 isl_map_free(mm.may);
3090 isl_flow_free(flow);
3093 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
3094 map = isl_map_read_from_str(ctx, str);
3095 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
3097 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
3098 map = isl_map_read_from_str(ctx, str);
3099 ai = isl_access_info_add_source(ai, map, 0, &depth);
3101 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
3102 map = isl_map_read_from_str(ctx, str);
3103 ai = isl_access_info_add_source(ai, map, 0, &depth);
3105 flow = isl_access_info_compute_flow(ai);
3106 dim = isl_space_alloc(ctx, 0, 3, 3);
3107 mm.must = isl_map_empty(isl_space_copy(dim));
3108 mm.may = isl_map_empty(dim);
3110 isl_flow_foreach(flow, collect_must_may, &mm);
3112 str = "{ [0,i,0] -> [2,i,0] : 0 <= i <= 10; "
3113 " [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
3114 assert(map_is_equal(mm.may, str));
3115 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
3116 assert(map_is_equal(mm.must, str));
3118 isl_map_free(mm.must);
3119 isl_map_free(mm.may);
3120 isl_flow_free(flow);
3123 str = "{ [0,i,2] -> [i] : 0 <= i <= 10 }";
3124 map = isl_map_read_from_str(ctx, str);
3125 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
3127 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
3128 map = isl_map_read_from_str(ctx, str);
3129 ai = isl_access_info_add_source(ai, map, 0, &depth);
3131 str = "{ [0,i,1] -> [5] : 0 <= i <= 10 }";
3132 map = isl_map_read_from_str(ctx, str);
3133 ai = isl_access_info_add_source(ai, map, 0, &depth);
3135 flow = isl_access_info_compute_flow(ai);
3136 dim = isl_space_alloc(ctx, 0, 3, 3);
3137 mm.must = isl_map_empty(isl_space_copy(dim));
3138 mm.may = isl_map_empty(dim);
3140 isl_flow_foreach(flow, collect_must_may, &mm);
3142 str = "{ [0,i,0] -> [0,i,2] : 0 <= i <= 10; "
3143 " [0,i,1] -> [0,5,2] : 0 <= i <= 5 }";
3144 assert(map_is_equal(mm.may, str));
3145 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
3146 assert(map_is_equal(mm.must, str));
3148 isl_map_free(mm.must);
3149 isl_map_free(mm.may);
3150 isl_flow_free(flow);
3153 str = "{ [0,i,1] -> [i] : 0 <= i <= 10 }";
3154 map = isl_map_read_from_str(ctx, str);
3155 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
3157 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
3158 map = isl_map_read_from_str(ctx, str);
3159 ai = isl_access_info_add_source(ai, map, 0, &depth);
3161 str = "{ [0,i,2] -> [5] : 0 <= i <= 10 }";
3162 map = isl_map_read_from_str(ctx, str);
3163 ai = isl_access_info_add_source(ai, map, 0, &depth);
3165 flow = isl_access_info_compute_flow(ai);
3166 dim = isl_space_alloc(ctx, 0, 3, 3);
3167 mm.must = isl_map_empty(isl_space_copy(dim));
3168 mm.may = isl_map_empty(dim);
3170 isl_flow_foreach(flow, collect_must_may, &mm);
3172 str = "{ [0,i,0] -> [0,i,1] : 0 <= i <= 10; "
3173 " [0,i,2] -> [0,5,1] : 0 <= i <= 4 }";
3174 assert(map_is_equal(mm.may, str));
3175 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
3176 assert(map_is_equal(mm.must, str));
3178 isl_map_free(mm.must);
3179 isl_map_free(mm.may);
3180 isl_flow_free(flow);
3183 depth = 5;
3185 str = "{ [1,i,0,0,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
3186 map = isl_map_read_from_str(ctx, str);
3187 ai = isl_access_info_alloc(map, &depth, &common_space, 1);
3189 str = "{ [0,i,0,j,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
3190 map = isl_map_read_from_str(ctx, str);
3191 ai = isl_access_info_add_source(ai, map, 1, &depth);
3193 flow = isl_access_info_compute_flow(ai);
3194 dim = isl_space_alloc(ctx, 0, 5, 5);
3195 mm.must = isl_map_empty(isl_space_copy(dim));
3196 mm.may = isl_map_empty(dim);
3198 isl_flow_foreach(flow, collect_must_may, &mm);
3200 str = "{ [0,i,0,j,0] -> [1,i,0,0,0] : 0 <= i,j <= 10 }";
3201 assert(map_is_equal(mm.must, str));
3202 str = "{ [0,0,0,0,0] -> [0,0,0,0,0] : 1 = 0 }";
3203 assert(map_is_equal(mm.may, str));
3205 isl_map_free(mm.must);
3206 isl_map_free(mm.may);
3207 isl_flow_free(flow);
3209 return 0;
3212 /* Check that the dependence analysis proceeds without errors.
3213 * Earlier versions of isl would break down during the analysis
3214 * due to the use of the wrong spaces.
3216 static int test_flow(isl_ctx *ctx)
3218 const char *str;
3219 isl_union_map *access, *schedule;
3220 isl_union_map *must_dep, *may_dep;
3221 int r;
3223 str = "{ S0[j] -> i[]; S1[j,i] -> i[]; S2[] -> i[]; S3[] -> i[] }";
3224 access = isl_union_map_read_from_str(ctx, str);
3225 str = "{ S0[j] -> [0,j,0,0] : 0 <= j < 10; "
3226 "S1[j,i] -> [0,j,1,i] : 0 <= j < i < 10; "
3227 "S2[] -> [1,0,0,0]; "
3228 "S3[] -> [-1,0,0,0] }";
3229 schedule = isl_union_map_read_from_str(ctx, str);
3230 r = isl_union_map_compute_flow(access, isl_union_map_copy(access),
3231 isl_union_map_copy(access), schedule,
3232 &must_dep, &may_dep, NULL, NULL);
3233 isl_union_map_free(may_dep);
3234 isl_union_map_free(must_dep);
3236 return r;
3239 struct {
3240 const char *map;
3241 int sv;
3242 } sv_tests[] = {
3243 { "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 9 }", 1 },
3244 { "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 10 }", 0 },
3245 { "{ [i] -> [3*floor(i/2) + 5*floor(i/3)] }", 1 },
3246 { "{ S1[i] -> [i] : 0 <= i <= 9; S2[i] -> [i] : 0 <= i <= 9 }", 1 },
3247 { "{ [i] -> S1[i] : 0 <= i <= 9; [i] -> S2[i] : 0 <= i <= 9 }", 0 },
3248 { "{ A[i] -> [i]; B[i] -> [i]; B[i] -> [i + 1] }", 0 },
3249 { "{ A[i] -> [i]; B[i] -> [i] : i < 0; B[i] -> [i + 1] : i > 0 }", 1 },
3250 { "{ A[i] -> [i]; B[i] -> A[i] : i < 0; B[i] -> [i + 1] : i > 0 }", 1 },
3251 { "{ A[i] -> [i]; B[i] -> [j] : i - 1 <= j <= i }", 0 },
3254 int test_sv(isl_ctx *ctx)
3256 isl_union_map *umap;
3257 int i;
3258 int sv;
3260 for (i = 0; i < ARRAY_SIZE(sv_tests); ++i) {
3261 umap = isl_union_map_read_from_str(ctx, sv_tests[i].map);
3262 sv = isl_union_map_is_single_valued(umap);
3263 isl_union_map_free(umap);
3264 if (sv < 0)
3265 return -1;
3266 if (sv_tests[i].sv && !sv)
3267 isl_die(ctx, isl_error_internal,
3268 "map not detected as single valued", return -1);
3269 if (!sv_tests[i].sv && sv)
3270 isl_die(ctx, isl_error_internal,
3271 "map detected as single valued", return -1);
3274 return 0;
3277 struct {
3278 const char *str;
3279 int bijective;
3280 } bijective_tests[] = {
3281 { "[N,M]->{[i,j] -> [i]}", 0 },
3282 { "[N,M]->{[i,j] -> [i] : j=i}", 1 },
3283 { "[N,M]->{[i,j] -> [i] : j=0}", 1 },
3284 { "[N,M]->{[i,j] -> [i] : j=N}", 1 },
3285 { "[N,M]->{[i,j] -> [j,i]}", 1 },
3286 { "[N,M]->{[i,j] -> [i+j]}", 0 },
3287 { "[N,M]->{[i,j] -> []}", 0 },
3288 { "[N,M]->{[i,j] -> [i,j,N]}", 1 },
3289 { "[N,M]->{[i,j] -> [2i]}", 0 },
3290 { "[N,M]->{[i,j] -> [i,i]}", 0 },
3291 { "[N,M]->{[i,j] -> [2i,i]}", 0 },
3292 { "[N,M]->{[i,j] -> [2i,j]}", 1 },
3293 { "[N,M]->{[i,j] -> [x,y] : 2x=i & y =j}", 1 },
3296 static int test_bijective(struct isl_ctx *ctx)
3298 isl_map *map;
3299 int i;
3300 int bijective;
3302 for (i = 0; i < ARRAY_SIZE(bijective_tests); ++i) {
3303 map = isl_map_read_from_str(ctx, bijective_tests[i].str);
3304 bijective = isl_map_is_bijective(map);
3305 isl_map_free(map);
3306 if (bijective < 0)
3307 return -1;
3308 if (bijective_tests[i].bijective && !bijective)
3309 isl_die(ctx, isl_error_internal,
3310 "map not detected as bijective", return -1);
3311 if (!bijective_tests[i].bijective && bijective)
3312 isl_die(ctx, isl_error_internal,
3313 "map detected as bijective", return -1);
3316 return 0;
3319 /* Inputs for isl_pw_qpolynomial_gist tests.
3320 * "pwqp" is the input, "set" is the context and "gist" is the expected result.
3322 struct {
3323 const char *pwqp;
3324 const char *set;
3325 const char *gist;
3326 } pwqp_gist_tests[] = {
3327 { "{ [i] -> i }", "{ [k] : exists a : k = 2a }", "{ [i] -> i }" },
3328 { "{ [i] -> i + [ (i + [i/3])/2 ] }", "{ [10] }", "{ [i] -> 16 }" },
3329 { "{ [i] -> ([(i)/2]) }", "{ [k] : exists a : k = 2a+1 }",
3330 "{ [i] -> -1/2 + 1/2 * i }" },
3331 { "{ [i] -> i^2 : i != 0 }", "{ [i] : i != 0 }", "{ [i] -> i^2 }" },
3334 static int test_pwqp(struct isl_ctx *ctx)
3336 int i;
3337 const char *str;
3338 isl_set *set;
3339 isl_pw_qpolynomial *pwqp1, *pwqp2;
3340 int equal;
3342 str = "{ [i,j,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
3343 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3345 pwqp1 = isl_pw_qpolynomial_move_dims(pwqp1, isl_dim_param, 0,
3346 isl_dim_in, 1, 1);
3348 str = "[j] -> { [i,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
3349 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3351 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3353 assert(isl_pw_qpolynomial_is_zero(pwqp1));
3355 isl_pw_qpolynomial_free(pwqp1);
3357 for (i = 0; i < ARRAY_SIZE(pwqp_gist_tests); ++i) {
3358 str = pwqp_gist_tests[i].pwqp;
3359 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3360 str = pwqp_gist_tests[i].set;
3361 set = isl_set_read_from_str(ctx, str);
3362 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
3363 str = pwqp_gist_tests[i].gist;
3364 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3365 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3366 equal = isl_pw_qpolynomial_is_zero(pwqp1);
3367 isl_pw_qpolynomial_free(pwqp1);
3369 if (equal < 0)
3370 return -1;
3371 if (!equal)
3372 isl_die(ctx, isl_error_unknown,
3373 "unexpected result", return -1);
3376 str = "{ [i] -> ([([i/2] + [i/2])/5]) }";
3377 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3378 str = "{ [i] -> ([(2 * [i/2])/5]) }";
3379 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3381 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3383 assert(isl_pw_qpolynomial_is_zero(pwqp1));
3385 isl_pw_qpolynomial_free(pwqp1);
3387 str = "{ [x] -> ([x/2] + [(x+1)/2]) }";
3388 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3389 str = "{ [x] -> x }";
3390 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3392 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3394 assert(isl_pw_qpolynomial_is_zero(pwqp1));
3396 isl_pw_qpolynomial_free(pwqp1);
3398 str = "{ [i] -> ([i/2]) : i >= 0; [i] -> ([i/3]) : i < 0 }";
3399 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3400 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3401 pwqp1 = isl_pw_qpolynomial_coalesce(pwqp1);
3402 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
3403 assert(isl_pw_qpolynomial_is_zero(pwqp1));
3404 isl_pw_qpolynomial_free(pwqp1);
3406 str = "{ [a,b,a] -> (([(2*[a/3]+b)/5]) * ([(2*[a/3]+b)/5])) }";
3407 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3408 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
3409 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3410 set = isl_set_read_from_str(ctx, "{ [a,b,a] }");
3411 pwqp1 = isl_pw_qpolynomial_intersect_domain(pwqp1, set);
3412 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
3413 isl_pw_qpolynomial_free(pwqp1);
3414 isl_pw_qpolynomial_free(pwqp2);
3415 if (equal < 0)
3416 return -1;
3417 if (!equal)
3418 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3420 str = "{ [a,b,c] -> (([(2*[a/3]+1)/5]) * ([(2*[c/3]+1)/5])) : b = 1 }";
3421 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
3422 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
3423 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
3424 pwqp1 = isl_pw_qpolynomial_fix_val(pwqp1, isl_dim_set, 1,
3425 isl_val_one(ctx));
3426 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
3427 isl_pw_qpolynomial_free(pwqp1);
3428 isl_pw_qpolynomial_free(pwqp2);
3429 if (equal < 0)
3430 return -1;
3431 if (!equal)
3432 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3434 return 0;
3437 static int test_split_periods(isl_ctx *ctx)
3439 const char *str;
3440 isl_pw_qpolynomial *pwqp;
3442 str = "{ [U,V] -> 1/3 * U + 2/3 * V - [(U + 2V)/3] + [U/2] : "
3443 "U + 2V + 3 >= 0 and - U -2V >= 0 and - U + 10 >= 0 and "
3444 "U >= 0; [U,V] -> U^2 : U >= 100 }";
3445 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3447 pwqp = isl_pw_qpolynomial_split_periods(pwqp, 2);
3449 isl_pw_qpolynomial_free(pwqp);
3451 if (!pwqp)
3452 return -1;
3454 return 0;
3457 static int test_union(isl_ctx *ctx)
3459 const char *str;
3460 isl_union_set *uset1, *uset2;
3461 isl_union_map *umap1, *umap2;
3462 int equal;
3464 str = "{ [i] : 0 <= i <= 1 }";
3465 uset1 = isl_union_set_read_from_str(ctx, str);
3466 str = "{ [1] -> [0] }";
3467 umap1 = isl_union_map_read_from_str(ctx, str);
3469 umap2 = isl_union_set_lex_gt_union_set(isl_union_set_copy(uset1), uset1);
3470 equal = isl_union_map_is_equal(umap1, umap2);
3472 isl_union_map_free(umap1);
3473 isl_union_map_free(umap2);
3475 if (equal < 0)
3476 return -1;
3477 if (!equal)
3478 isl_die(ctx, isl_error_unknown, "union maps not equal",
3479 return -1);
3481 str = "{ A[i] -> B[i]; B[i] -> C[i]; A[0] -> C[1] }";
3482 umap1 = isl_union_map_read_from_str(ctx, str);
3483 str = "{ A[i]; B[i] }";
3484 uset1 = isl_union_set_read_from_str(ctx, str);
3486 uset2 = isl_union_map_domain(umap1);
3488 equal = isl_union_set_is_equal(uset1, uset2);
3490 isl_union_set_free(uset1);
3491 isl_union_set_free(uset2);
3493 if (equal < 0)
3494 return -1;
3495 if (!equal)
3496 isl_die(ctx, isl_error_unknown, "union sets not equal",
3497 return -1);
3499 return 0;
3502 /* Check that computing a bound of a non-zero polynomial over an unbounded
3503 * domain does not produce a rational value.
3504 * In particular, check that the upper bound is infinity.
3506 static int test_bound_unbounded_domain(isl_ctx *ctx)
3508 const char *str;
3509 isl_pw_qpolynomial *pwqp;
3510 isl_pw_qpolynomial_fold *pwf, *pwf2;
3511 isl_bool equal;
3513 str = "{ [m,n] -> -m * n }";
3514 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3515 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
3516 str = "{ infty }";
3517 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3518 pwf2 = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
3519 equal = isl_pw_qpolynomial_fold_plain_is_equal(pwf, pwf2);
3520 isl_pw_qpolynomial_fold_free(pwf);
3521 isl_pw_qpolynomial_fold_free(pwf2);
3523 if (equal < 0)
3524 return -1;
3525 if (!equal)
3526 isl_die(ctx, isl_error_unknown,
3527 "expecting infinite polynomial bound", return -1);
3529 return 0;
3532 static int test_bound(isl_ctx *ctx)
3534 const char *str;
3535 isl_size dim;
3536 isl_pw_qpolynomial *pwqp;
3537 isl_pw_qpolynomial_fold *pwf;
3539 if (test_bound_unbounded_domain(ctx) < 0)
3540 return -1;
3542 str = "{ [[a, b, c, d] -> [e]] -> 0 }";
3543 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3544 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
3545 dim = isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in);
3546 isl_pw_qpolynomial_fold_free(pwf);
3547 if (dim < 0)
3548 return -1;
3549 if (dim != 4)
3550 isl_die(ctx, isl_error_unknown, "unexpected input dimension",
3551 return -1);
3553 str = "{ [[x]->[x]] -> 1 : exists a : x = 2 a }";
3554 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
3555 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
3556 dim = isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in);
3557 isl_pw_qpolynomial_fold_free(pwf);
3558 if (dim < 0)
3559 return -1;
3560 if (dim != 1)
3561 isl_die(ctx, isl_error_unknown, "unexpected input dimension",
3562 return -1);
3564 return 0;
3567 /* isl_set is defined to isl_map internally, so the corresponding elements
3568 * are isl_basic_map objects.
3570 #undef EL_BASE
3571 #undef SET_BASE
3572 #define EL_BASE basic_map
3573 #define SET_BASE set
3574 #include "isl_test_list_templ.c"
3576 #undef EL_BASE
3577 #undef SET_BASE
3578 #define EL_BASE basic_set
3579 #define SET_BASE union_set
3580 #include "isl_test_list_templ.c"
3582 #undef EL_BASE
3583 #undef SET_BASE
3584 #define EL_BASE set
3585 #define SET_BASE union_set
3586 #include "isl_test_list_templ.c"
3588 #undef EL_BASE
3589 #undef SET_BASE
3590 #define EL_BASE basic_map
3591 #define SET_BASE map
3592 #include "isl_test_list_templ.c"
3594 #undef EL_BASE
3595 #undef SET_BASE
3596 #define EL_BASE map
3597 #define SET_BASE union_map
3598 #include "isl_test_list_templ.c"
3600 /* Check that the conversion from isl objects to lists works as expected.
3602 static int test_get_list(isl_ctx *ctx)
3604 if (test_get_list_basic_map_from_set(ctx, "{ [0]; [2]; [3] }"))
3605 return -1;
3606 if (test_get_list_basic_set_from_union_set(ctx, "{ A[0]; B[2]; B[3] }"))
3607 return -1;
3608 if (test_get_list_set_from_union_set(ctx, "{ A[0]; A[2]; B[3] }"))
3609 return -1;
3610 if (test_get_list_basic_map_from_map(ctx,
3611 "{ [0] -> [0]; [2] -> [0]; [3] -> [0] }"))
3612 return -1;
3613 if (test_get_list_map_from_union_map(ctx,
3614 "{ A[0] -> [0]; A[2] -> [0]; B[3] -> [0] }"))
3615 return -1;
3617 return 0;
3620 static int test_lift(isl_ctx *ctx)
3622 const char *str;
3623 isl_basic_map *bmap;
3624 isl_basic_set *bset;
3626 str = "{ [i0] : exists e0 : i0 = 4e0 }";
3627 bset = isl_basic_set_read_from_str(ctx, str);
3628 bset = isl_basic_set_lift(bset);
3629 bmap = isl_basic_map_from_range(bset);
3630 bset = isl_basic_map_domain(bmap);
3631 isl_basic_set_free(bset);
3633 return 0;
3636 /* Check that isl_set_is_subset is not confused by identical
3637 * integer divisions.
3638 * The call to isl_set_normalize ensures that the equality constraints
3639 * a = b = 0 are discovered, turning e0 and e1 into identical
3640 * integer divisions. Any further simplification would remove
3641 * the duplicate integer divisions.
3643 static isl_stat test_subset_duplicate_integer_divisions(isl_ctx *ctx)
3645 const char *str;
3646 isl_bool is_subset;
3647 isl_set *set1, *set2;
3649 str = "{ [a, b, c, d] : "
3650 "exists (e0 = floor((a + d)/4), e1 = floor((d)/4), "
3651 "e2 = floor((-a - d + 4 *floor((a + d)/4))/10), "
3652 "e3 = floor((-d + 4*floor((d)/4))/10): "
3653 "10e2 = -a - 2c - d + 4e0 and 10e3 = -2c - d + 4e1 and "
3654 "b >= 0 and a <= 0 and b <= a) }";
3655 set1 = isl_set_read_from_str(ctx, str);
3656 set2 = isl_set_read_from_str(ctx, str);
3657 set2 = isl_set_normalize(set2);
3659 is_subset = isl_set_is_subset(set1, set2);
3661 isl_set_free(set1);
3662 isl_set_free(set2);
3664 if (is_subset < 0)
3665 return isl_stat_error;
3666 if (!is_subset)
3667 isl_die(ctx, isl_error_unknown,
3668 "set is not considered to be a subset of itself",
3669 return isl_stat_error);
3671 return isl_stat_ok;
3674 struct {
3675 const char *set1;
3676 const char *set2;
3677 int subset;
3678 } subset_tests[] = {
3679 { "{ [112, 0] }",
3680 "{ [i0, i1] : exists (e0 = [(i0 - i1)/16], e1: "
3681 "16e0 <= i0 - i1 and 16e0 >= -15 + i0 - i1 and "
3682 "16e1 <= i1 and 16e0 >= -i1 and 16e1 >= -i0 + i1) }", 1 },
3683 { "{ [65] }",
3684 "{ [i] : exists (e0 = [(255i)/256], e1 = [(127i + 65e0)/191], "
3685 "e2 = [(3i + 61e1)/65], e3 = [(52i + 12e2)/61], "
3686 "e4 = [(2i + e3)/3], e5 = [(4i + e3)/4], e6 = [(8i + e3)/12]: "
3687 "3e4 = 2i + e3 and 4e5 = 4i + e3 and 12e6 = 8i + e3 and "
3688 "i <= 255 and 64e3 >= -45 + 67i and i >= 0 and "
3689 "256e0 <= 255i and 256e0 >= -255 + 255i and "
3690 "191e1 <= 127i + 65e0 and 191e1 >= -190 + 127i + 65e0 and "
3691 "65e2 <= 3i + 61e1 and 65e2 >= -64 + 3i + 61e1 and "
3692 "61e3 <= 52i + 12e2 and 61e3 >= -60 + 52i + 12e2) }", 1 },
3693 { "{ [i] : 0 <= i <= 10 }", "{ rat: [i] : 0 <= i <= 10 }", 1 },
3694 { "{ rat: [i] : 0 <= i <= 10 }", "{ [i] : 0 <= i <= 10 }", 0 },
3695 { "{ rat: [0] }", "{ [i] : 0 <= i <= 10 }", 1 },
3696 { "{ rat: [(1)/2] }", "{ [i] : 0 <= i <= 10 }", 0 },
3697 { "{ [t, i] : (exists (e0 = [(2 + t)/4]: 4e0 <= 2 + t and "
3698 "4e0 >= -1 + t and i >= 57 and i <= 62 and "
3699 "4e0 <= 62 + t - i and 4e0 >= -61 + t + i and "
3700 "t >= 0 and t <= 511 and 4e0 <= -57 + t + i and "
3701 "4e0 >= 58 + t - i and i >= 58 + t and i >= 62 - t)) }",
3702 "{ [i0, i1] : (exists (e0 = [(4 + i0)/4]: 4e0 <= 62 + i0 - i1 and "
3703 "4e0 >= 1 + i0 and i0 >= 0 and i0 <= 511 and "
3704 "4e0 <= -57 + i0 + i1)) or "
3705 "(exists (e0 = [(2 + i0)/4]: 4e0 <= i0 and "
3706 "4e0 >= 58 + i0 - i1 and i0 >= 2 and i0 <= 511 and "
3707 "4e0 >= -61 + i0 + i1)) or "
3708 "(i1 <= 66 - i0 and i0 >= 2 and i1 >= 59 + i0) }", 1 },
3709 { "[a, b] -> { : a = 0 and b = -1 }", "[b, a] -> { : b >= -10 }", 1 },
3712 static int test_subset(isl_ctx *ctx)
3714 int i;
3715 isl_set *set1, *set2;
3716 int subset;
3718 if (test_subset_duplicate_integer_divisions(ctx) < 0)
3719 return -1;
3721 for (i = 0; i < ARRAY_SIZE(subset_tests); ++i) {
3722 set1 = isl_set_read_from_str(ctx, subset_tests[i].set1);
3723 set2 = isl_set_read_from_str(ctx, subset_tests[i].set2);
3724 subset = isl_set_is_subset(set1, set2);
3725 isl_set_free(set1);
3726 isl_set_free(set2);
3727 if (subset < 0)
3728 return -1;
3729 if (subset != subset_tests[i].subset)
3730 isl_die(ctx, isl_error_unknown,
3731 "incorrect subset result", return -1);
3734 return 0;
3737 struct {
3738 const char *minuend;
3739 const char *subtrahend;
3740 const char *difference;
3741 } subtract_domain_tests[] = {
3742 { "{ A[i] -> B[i] }", "{ A[i] }", "{ }" },
3743 { "{ A[i] -> B[i] }", "{ B[i] }", "{ A[i] -> B[i] }" },
3744 { "{ A[i] -> B[i] }", "{ A[i] : i > 0 }", "{ A[i] -> B[i] : i <= 0 }" },
3747 static int test_subtract(isl_ctx *ctx)
3749 int i;
3750 isl_union_map *umap1, *umap2;
3751 isl_union_pw_multi_aff *upma1, *upma2;
3752 isl_union_set *uset;
3753 int equal;
3755 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
3756 umap1 = isl_union_map_read_from_str(ctx,
3757 subtract_domain_tests[i].minuend);
3758 uset = isl_union_set_read_from_str(ctx,
3759 subtract_domain_tests[i].subtrahend);
3760 umap2 = isl_union_map_read_from_str(ctx,
3761 subtract_domain_tests[i].difference);
3762 umap1 = isl_union_map_subtract_domain(umap1, uset);
3763 equal = isl_union_map_is_equal(umap1, umap2);
3764 isl_union_map_free(umap1);
3765 isl_union_map_free(umap2);
3766 if (equal < 0)
3767 return -1;
3768 if (!equal)
3769 isl_die(ctx, isl_error_unknown,
3770 "incorrect subtract domain result", return -1);
3773 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
3774 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
3775 subtract_domain_tests[i].minuend);
3776 uset = isl_union_set_read_from_str(ctx,
3777 subtract_domain_tests[i].subtrahend);
3778 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
3779 subtract_domain_tests[i].difference);
3780 upma1 = isl_union_pw_multi_aff_subtract_domain(upma1, uset);
3781 equal = isl_union_pw_multi_aff_plain_is_equal(upma1, upma2);
3782 isl_union_pw_multi_aff_free(upma1);
3783 isl_union_pw_multi_aff_free(upma2);
3784 if (equal < 0)
3785 return -1;
3786 if (!equal)
3787 isl_die(ctx, isl_error_unknown,
3788 "incorrect subtract domain result", return -1);
3791 return 0;
3794 /* Check that intersecting the empty basic set with another basic set
3795 * does not increase the number of constraints. In particular,
3796 * the empty basic set should maintain its canonical representation.
3798 static int test_intersect_1(isl_ctx *ctx)
3800 isl_size n1, n2;
3801 isl_basic_set *bset1, *bset2;
3803 bset1 = isl_basic_set_read_from_str(ctx, "{ [a,b,c] : 1 = 0 }");
3804 bset2 = isl_basic_set_read_from_str(ctx, "{ [1,2,3] }");
3805 n1 = isl_basic_set_n_constraint(bset1);
3806 bset1 = isl_basic_set_intersect(bset1, bset2);
3807 n2 = isl_basic_set_n_constraint(bset1);
3808 isl_basic_set_free(bset1);
3809 if (n1 < 0 || n2 < 0)
3810 return -1;
3811 if (n1 != n2)
3812 isl_die(ctx, isl_error_unknown,
3813 "number of constraints of empty set changed",
3814 return -1);
3816 return 0;
3819 /* Check that intersecting a set with itself does not cause
3820 * an explosion in the number of disjuncts.
3822 static isl_stat test_intersect_2(isl_ctx *ctx)
3824 int i;
3825 isl_set *set;
3827 set = isl_set_read_from_str(ctx, "{ [x,y] : x >= 0 or y >= 0 }");
3828 for (i = 0; i < 100; ++i)
3829 set = isl_set_intersect(set, isl_set_copy(set));
3830 isl_set_free(set);
3831 if (!set)
3832 return isl_stat_error;
3833 return isl_stat_ok;
3836 /* Perform some intersection tests.
3838 static int test_intersect(isl_ctx *ctx)
3840 if (test_intersect_1(ctx) < 0)
3841 return -1;
3842 if (test_intersect_2(ctx) < 0)
3843 return -1;
3845 return 0;
3848 int test_factorize(isl_ctx *ctx)
3850 const char *str;
3851 isl_basic_set *bset;
3852 isl_factorizer *f;
3854 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7] : 3i5 <= 2 - 2i0 and "
3855 "i0 >= -2 and i6 >= 1 + i3 and i7 >= 0 and 3i5 >= -2i0 and "
3856 "2i4 <= i2 and i6 >= 1 + 2i0 + 3i1 and i4 <= -1 and "
3857 "i6 >= 1 + 2i0 + 3i5 and i6 <= 2 + 2i0 + 3i5 and "
3858 "3i5 <= 2 - 2i0 - i2 + 3i4 and i6 <= 2 + 2i0 + 3i1 and "
3859 "i0 <= -1 and i7 <= i2 + i3 - 3i4 - i6 and "
3860 "3i5 >= -2i0 - i2 + 3i4 }";
3861 bset = isl_basic_set_read_from_str(ctx, str);
3862 f = isl_basic_set_factorizer(bset);
3863 isl_basic_set_free(bset);
3864 isl_factorizer_free(f);
3865 if (!f)
3866 isl_die(ctx, isl_error_unknown,
3867 "failed to construct factorizer", return -1);
3869 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
3870 "i12 <= 2 + i0 - i11 and 2i8 >= -i4 and i11 >= i1 and "
3871 "3i5 <= -i2 and 2i11 >= -i4 - 2i7 and i11 <= 3 + i0 + 3i9 and "
3872 "i11 <= -i4 - 2i7 and i12 >= -i10 and i2 >= -2 and "
3873 "i11 >= i1 + 3i10 and i11 >= 1 + i0 + 3i9 and "
3874 "i11 <= 1 - i4 - 2i8 and 6i6 <= 6 - i2 and 3i6 >= 1 - i2 and "
3875 "i11 <= 2 + i1 and i12 <= i4 + i11 and i12 >= i0 - i11 and "
3876 "3i5 >= -2 - i2 and i12 >= -1 + i4 + i11 and 3i3 <= 3 - i2 and "
3877 "9i6 <= 11 - i2 + 6i5 and 3i3 >= 1 - i2 and "
3878 "9i6 <= 5 - i2 + 6i3 and i12 <= -1 and i2 <= 0 }";
3879 bset = isl_basic_set_read_from_str(ctx, str);
3880 f = isl_basic_set_factorizer(bset);
3881 isl_basic_set_free(bset);
3882 isl_factorizer_free(f);
3883 if (!f)
3884 isl_die(ctx, isl_error_unknown,
3885 "failed to construct factorizer", return -1);
3887 return 0;
3890 static isl_stat check_injective(__isl_take isl_map *map, void *user)
3892 int *injective = user;
3894 *injective = isl_map_is_injective(map);
3895 isl_map_free(map);
3897 if (*injective < 0 || !*injective)
3898 return isl_stat_error;
3900 return isl_stat_ok;
3903 int test_one_schedule(isl_ctx *ctx, const char *d, const char *w,
3904 const char *r, const char *s, int tilable, int parallel)
3906 int i;
3907 isl_union_set *D;
3908 isl_union_map *W, *R, *S;
3909 isl_union_map *empty;
3910 isl_union_map *dep_raw, *dep_war, *dep_waw, *dep;
3911 isl_union_map *validity, *proximity, *coincidence;
3912 isl_union_map *schedule;
3913 isl_union_map *test;
3914 isl_union_set *delta;
3915 isl_union_set *domain;
3916 isl_set *delta_set;
3917 isl_set *slice;
3918 isl_set *origin;
3919 isl_schedule_constraints *sc;
3920 isl_schedule *sched;
3921 int is_nonneg, is_parallel, is_tilable, is_injection, is_complete;
3922 isl_size n;
3924 D = isl_union_set_read_from_str(ctx, d);
3925 W = isl_union_map_read_from_str(ctx, w);
3926 R = isl_union_map_read_from_str(ctx, r);
3927 S = isl_union_map_read_from_str(ctx, s);
3929 W = isl_union_map_intersect_domain(W, isl_union_set_copy(D));
3930 R = isl_union_map_intersect_domain(R, isl_union_set_copy(D));
3932 empty = isl_union_map_empty(isl_union_map_get_space(S));
3933 isl_union_map_compute_flow(isl_union_map_copy(R),
3934 isl_union_map_copy(W), empty,
3935 isl_union_map_copy(S),
3936 &dep_raw, NULL, NULL, NULL);
3937 isl_union_map_compute_flow(isl_union_map_copy(W),
3938 isl_union_map_copy(W),
3939 isl_union_map_copy(R),
3940 isl_union_map_copy(S),
3941 &dep_waw, &dep_war, NULL, NULL);
3943 dep = isl_union_map_union(dep_waw, dep_war);
3944 dep = isl_union_map_union(dep, dep_raw);
3945 validity = isl_union_map_copy(dep);
3946 coincidence = isl_union_map_copy(dep);
3947 proximity = isl_union_map_copy(dep);
3949 sc = isl_schedule_constraints_on_domain(isl_union_set_copy(D));
3950 sc = isl_schedule_constraints_set_validity(sc, validity);
3951 sc = isl_schedule_constraints_set_coincidence(sc, coincidence);
3952 sc = isl_schedule_constraints_set_proximity(sc, proximity);
3953 sched = isl_schedule_constraints_compute_schedule(sc);
3954 schedule = isl_schedule_get_map(sched);
3955 isl_schedule_free(sched);
3956 isl_union_map_free(W);
3957 isl_union_map_free(R);
3958 isl_union_map_free(S);
3960 is_injection = 1;
3961 isl_union_map_foreach_map(schedule, &check_injective, &is_injection);
3963 domain = isl_union_map_domain(isl_union_map_copy(schedule));
3964 is_complete = isl_union_set_is_subset(D, domain);
3965 isl_union_set_free(D);
3966 isl_union_set_free(domain);
3968 test = isl_union_map_reverse(isl_union_map_copy(schedule));
3969 test = isl_union_map_apply_range(test, dep);
3970 test = isl_union_map_apply_range(test, schedule);
3972 delta = isl_union_map_deltas(test);
3973 n = isl_union_set_n_set(delta);
3974 if (n < 0) {
3975 isl_union_set_free(delta);
3976 return -1;
3978 if (n == 0) {
3979 is_tilable = 1;
3980 is_parallel = 1;
3981 is_nonneg = 1;
3982 isl_union_set_free(delta);
3983 } else {
3984 isl_size dim;
3986 delta_set = isl_set_from_union_set(delta);
3988 slice = isl_set_universe(isl_set_get_space(delta_set));
3989 for (i = 0; i < tilable; ++i)
3990 slice = isl_set_lower_bound_si(slice, isl_dim_set, i, 0);
3991 is_tilable = isl_set_is_subset(delta_set, slice);
3992 isl_set_free(slice);
3994 slice = isl_set_universe(isl_set_get_space(delta_set));
3995 for (i = 0; i < parallel; ++i)
3996 slice = isl_set_fix_si(slice, isl_dim_set, i, 0);
3997 is_parallel = isl_set_is_subset(delta_set, slice);
3998 isl_set_free(slice);
4000 origin = isl_set_universe(isl_set_get_space(delta_set));
4001 dim = isl_set_dim(origin, isl_dim_set);
4002 if (dim < 0)
4003 origin = isl_set_free(origin);
4004 for (i = 0; i < dim; ++i)
4005 origin = isl_set_fix_si(origin, isl_dim_set, i, 0);
4007 delta_set = isl_set_union(delta_set, isl_set_copy(origin));
4008 delta_set = isl_set_lexmin(delta_set);
4010 is_nonneg = isl_set_is_equal(delta_set, origin);
4012 isl_set_free(origin);
4013 isl_set_free(delta_set);
4016 if (is_nonneg < 0 || is_parallel < 0 || is_tilable < 0 ||
4017 is_injection < 0 || is_complete < 0)
4018 return -1;
4019 if (!is_complete)
4020 isl_die(ctx, isl_error_unknown,
4021 "generated schedule incomplete", return -1);
4022 if (!is_injection)
4023 isl_die(ctx, isl_error_unknown,
4024 "generated schedule not injective on each statement",
4025 return -1);
4026 if (!is_nonneg)
4027 isl_die(ctx, isl_error_unknown,
4028 "negative dependences in generated schedule",
4029 return -1);
4030 if (!is_tilable)
4031 isl_die(ctx, isl_error_unknown,
4032 "generated schedule not as tilable as expected",
4033 return -1);
4034 if (!is_parallel)
4035 isl_die(ctx, isl_error_unknown,
4036 "generated schedule not as parallel as expected",
4037 return -1);
4039 return 0;
4042 /* Compute a schedule for the given instance set, validity constraints,
4043 * proximity constraints and context and return a corresponding union map
4044 * representation.
4046 static __isl_give isl_union_map *compute_schedule_with_context(isl_ctx *ctx,
4047 const char *domain, const char *validity, const char *proximity,
4048 const char *context)
4050 isl_set *con;
4051 isl_union_set *dom;
4052 isl_union_map *dep;
4053 isl_union_map *prox;
4054 isl_schedule_constraints *sc;
4055 isl_schedule *schedule;
4056 isl_union_map *sched;
4058 con = isl_set_read_from_str(ctx, context);
4059 dom = isl_union_set_read_from_str(ctx, domain);
4060 dep = isl_union_map_read_from_str(ctx, validity);
4061 prox = isl_union_map_read_from_str(ctx, proximity);
4062 sc = isl_schedule_constraints_on_domain(dom);
4063 sc = isl_schedule_constraints_set_context(sc, con);
4064 sc = isl_schedule_constraints_set_validity(sc, dep);
4065 sc = isl_schedule_constraints_set_proximity(sc, prox);
4066 schedule = isl_schedule_constraints_compute_schedule(sc);
4067 sched = isl_schedule_get_map(schedule);
4068 isl_schedule_free(schedule);
4070 return sched;
4073 /* Compute a schedule for the given instance set, validity constraints and
4074 * proximity constraints and return a corresponding union map representation.
4076 static __isl_give isl_union_map *compute_schedule(isl_ctx *ctx,
4077 const char *domain, const char *validity, const char *proximity)
4079 return compute_schedule_with_context(ctx, domain, validity, proximity,
4080 "{ : }");
4083 /* Check that a schedule can be constructed on the given domain
4084 * with the given validity and proximity constraints.
4086 static int test_has_schedule(isl_ctx *ctx, const char *domain,
4087 const char *validity, const char *proximity)
4089 isl_union_map *sched;
4091 sched = compute_schedule(ctx, domain, validity, proximity);
4092 if (!sched)
4093 return -1;
4095 isl_union_map_free(sched);
4096 return 0;
4099 int test_special_schedule(isl_ctx *ctx, const char *domain,
4100 const char *validity, const char *proximity, const char *expected_sched)
4102 isl_union_map *sched1, *sched2;
4103 int equal;
4105 sched1 = compute_schedule(ctx, domain, validity, proximity);
4106 sched2 = isl_union_map_read_from_str(ctx, expected_sched);
4108 equal = isl_union_map_is_equal(sched1, sched2);
4109 isl_union_map_free(sched1);
4110 isl_union_map_free(sched2);
4112 if (equal < 0)
4113 return -1;
4114 if (!equal)
4115 isl_die(ctx, isl_error_unknown, "unexpected schedule",
4116 return -1);
4118 return 0;
4121 /* Check that the schedule map is properly padded, i.e., that the range
4122 * lives in a single space.
4124 static int test_padded_schedule(isl_ctx *ctx)
4126 const char *str;
4127 isl_union_set *D;
4128 isl_union_map *validity, *proximity;
4129 isl_schedule_constraints *sc;
4130 isl_schedule *sched;
4131 isl_union_map *umap;
4132 isl_union_set *range;
4133 isl_set *set;
4135 str = "[N] -> { S0[i] : 0 <= i <= N; S1[i, j] : 0 <= i, j <= N }";
4136 D = isl_union_set_read_from_str(ctx, str);
4137 validity = isl_union_map_empty(isl_union_set_get_space(D));
4138 proximity = isl_union_map_copy(validity);
4139 sc = isl_schedule_constraints_on_domain(D);
4140 sc = isl_schedule_constraints_set_validity(sc, validity);
4141 sc = isl_schedule_constraints_set_proximity(sc, proximity);
4142 sched = isl_schedule_constraints_compute_schedule(sc);
4143 umap = isl_schedule_get_map(sched);
4144 isl_schedule_free(sched);
4145 range = isl_union_map_range(umap);
4146 set = isl_set_from_union_set(range);
4147 isl_set_free(set);
4149 if (!set)
4150 return -1;
4152 return 0;
4155 /* Check that conditional validity constraints are also taken into
4156 * account across bands.
4157 * In particular, try to make sure that live ranges D[1,0]->C[2,1] and
4158 * D[2,0]->C[3,0] are not local in the outer band of the generated schedule
4159 * and then check that the adjacent order constraint C[2,1]->D[2,0]
4160 * is enforced by the rest of the schedule.
4162 static int test_special_conditional_schedule_constraints(isl_ctx *ctx)
4164 const char *str;
4165 isl_union_set *domain;
4166 isl_union_map *validity, *proximity, *condition;
4167 isl_union_map *sink, *source, *dep;
4168 isl_schedule_constraints *sc;
4169 isl_schedule *schedule;
4170 isl_union_access_info *access;
4171 isl_union_flow *flow;
4172 int empty;
4174 str = "[n] -> { C[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k; "
4175 "A[k] : k >= 1 and k <= -1 + n; "
4176 "B[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k; "
4177 "D[k, i] : k <= -1 + n and i >= 0 and i <= -1 + k }";
4178 domain = isl_union_set_read_from_str(ctx, str);
4179 sc = isl_schedule_constraints_on_domain(domain);
4180 str = "[n] -> { D[k, i] -> C[1 + k, k - i] : "
4181 "k <= -2 + n and i >= 1 and i <= -1 + k; "
4182 "D[k, i] -> C[1 + k, i] : "
4183 "k <= -2 + n and i >= 1 and i <= -1 + k; "
4184 "D[k, 0] -> C[1 + k, k] : k >= 1 and k <= -2 + n; "
4185 "D[k, 0] -> C[1 + k, 0] : k >= 1 and k <= -2 + n }";
4186 validity = isl_union_map_read_from_str(ctx, str);
4187 sc = isl_schedule_constraints_set_validity(sc, validity);
4188 str = "[n] -> { C[k, i] -> D[k, i] : "
4189 "0 <= i <= -1 + k and k <= -1 + n }";
4190 proximity = isl_union_map_read_from_str(ctx, str);
4191 sc = isl_schedule_constraints_set_proximity(sc, proximity);
4192 str = "[n] -> { [D[k, i] -> a[]] -> [C[1 + k, k - i] -> b[]] : "
4193 "i <= -1 + k and i >= 1 and k <= -2 + n; "
4194 "[B[k, i] -> c[]] -> [B[k, 1 + i] -> c[]] : "
4195 "k <= -1 + n and i >= 0 and i <= -2 + k }";
4196 condition = isl_union_map_read_from_str(ctx, str);
4197 str = "[n] -> { [B[k, i] -> e[]] -> [D[k, i] -> a[]] : "
4198 "i >= 0 and i <= -1 + k and k <= -1 + n; "
4199 "[C[k, i] -> b[]] -> [D[k', -1 + k - i] -> a[]] : "
4200 "i >= 0 and i <= -1 + k and k <= -1 + n and "
4201 "k' <= -1 + n and k' >= k - i and k' >= 1 + k; "
4202 "[C[k, i] -> b[]] -> [D[k, -1 + k - i] -> a[]] : "
4203 "i >= 0 and i <= -1 + k and k <= -1 + n; "
4204 "[B[k, i] -> c[]] -> [A[k'] -> d[]] : "
4205 "k <= -1 + n and i >= 0 and i <= -1 + k and "
4206 "k' >= 1 and k' <= -1 + n and k' >= 1 + k }";
4207 validity = isl_union_map_read_from_str(ctx, str);
4208 sc = isl_schedule_constraints_set_conditional_validity(sc, condition,
4209 validity);
4210 schedule = isl_schedule_constraints_compute_schedule(sc);
4211 str = "{ D[2,0] -> [] }";
4212 sink = isl_union_map_read_from_str(ctx, str);
4213 access = isl_union_access_info_from_sink(sink);
4214 str = "{ C[2,1] -> [] }";
4215 source = isl_union_map_read_from_str(ctx, str);
4216 access = isl_union_access_info_set_must_source(access, source);
4217 access = isl_union_access_info_set_schedule(access, schedule);
4218 flow = isl_union_access_info_compute_flow(access);
4219 dep = isl_union_flow_get_must_dependence(flow);
4220 isl_union_flow_free(flow);
4221 empty = isl_union_map_is_empty(dep);
4222 isl_union_map_free(dep);
4224 if (empty < 0)
4225 return -1;
4226 if (empty)
4227 isl_die(ctx, isl_error_unknown,
4228 "conditional validity not respected", return -1);
4230 return 0;
4233 /* Check that the test for violated conditional validity constraints
4234 * is not confused by domain compression.
4235 * In particular, earlier versions of isl would apply
4236 * a schedule on the compressed domains to the original domains,
4237 * resulting in a failure to detect that the default schedule
4238 * violates the conditional validity constraints.
4240 static int test_special_conditional_schedule_constraints_2(isl_ctx *ctx)
4242 const char *str;
4243 isl_bool empty;
4244 isl_union_set *domain;
4245 isl_union_map *validity, *condition;
4246 isl_schedule_constraints *sc;
4247 isl_schedule *schedule;
4248 isl_union_map *umap;
4249 isl_map *map, *ge;
4251 str = "{ A[0, i] : 0 <= i <= 10; B[1, i] : 0 <= i <= 10 }";
4252 domain = isl_union_set_read_from_str(ctx, str);
4253 sc = isl_schedule_constraints_on_domain(domain);
4254 str = "{ B[1, i] -> A[0, i + 1] }";
4255 condition = isl_union_map_read_from_str(ctx, str);
4256 str = "{ A[0, i] -> B[1, i - 1] }";
4257 validity = isl_union_map_read_from_str(ctx, str);
4258 sc = isl_schedule_constraints_set_conditional_validity(sc, condition,
4259 isl_union_map_copy(validity));
4260 schedule = isl_schedule_constraints_compute_schedule(sc);
4261 umap = isl_schedule_get_map(schedule);
4262 isl_schedule_free(schedule);
4263 validity = isl_union_map_apply_domain(validity,
4264 isl_union_map_copy(umap));
4265 validity = isl_union_map_apply_range(validity, umap);
4266 map = isl_map_from_union_map(validity);
4267 ge = isl_map_lex_ge(isl_space_domain(isl_map_get_space(map)));
4268 map = isl_map_intersect(map, ge);
4269 empty = isl_map_is_empty(map);
4270 isl_map_free(map);
4272 if (empty < 0)
4273 return -1;
4274 if (!empty)
4275 isl_die(ctx, isl_error_unknown,
4276 "conditional validity constraints not satisfied",
4277 return -1);
4279 return 0;
4282 /* Input for testing of schedule construction based on
4283 * conditional constraints.
4285 * domain is the iteration domain
4286 * flow are the flow dependences, which determine the validity and
4287 * proximity constraints
4288 * condition are the conditions on the conditional validity constraints
4289 * conditional_validity are the conditional validity constraints
4290 * outer_band_n is the expected number of members in the outer band
4292 struct {
4293 const char *domain;
4294 const char *flow;
4295 const char *condition;
4296 const char *conditional_validity;
4297 int outer_band_n;
4298 } live_range_tests[] = {
4299 /* Contrived example that illustrates that we need to keep
4300 * track of tagged condition dependences and
4301 * tagged conditional validity dependences
4302 * in isl_sched_edge separately.
4303 * In particular, the conditional validity constraints on A
4304 * cannot be satisfied,
4305 * but they can be ignored because there are no corresponding
4306 * condition constraints. However, we do have an additional
4307 * conditional validity constraint that maps to the same
4308 * dependence relation
4309 * as the condition constraint on B. If we did not make a distinction
4310 * between tagged condition and tagged conditional validity
4311 * dependences, then we
4312 * could end up treating this shared dependence as an condition
4313 * constraint on A, forcing a localization of the conditions,
4314 * which is impossible.
4316 { "{ S[i] : 0 <= 1 < 100; T[i] : 0 <= 1 < 100 }",
4317 "{ S[i] -> S[i+1] : 0 <= i < 99 }",
4318 "{ [S[i] -> B[]] -> [S[i+1] -> B[]] : 0 <= i < 99 }",
4319 "{ [S[i] -> A[]] -> [T[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
4320 "[T[i] -> A[]] -> [S[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
4321 "[S[i] -> A[]] -> [S[i+1] -> A[]] : 0 <= i < 99 }",
4324 /* TACO 2013 Fig. 7 */
4325 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
4326 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
4327 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
4328 "[n] -> { [S1[i,j] -> t[]] -> [S2[i,j] -> t[]] : 0 <= i,j < n;"
4329 "[S2[i,j] -> x1[]] -> [S2[i,j+1] -> x1[]] : "
4330 "0 <= i < n and 0 <= j < n - 1 }",
4331 "[n] -> { [S2[i,j] -> t[]] -> [S1[i,j'] -> t[]] : "
4332 "0 <= i < n and 0 <= j < j' < n;"
4333 "[S2[i,j] -> t[]] -> [S1[i',j'] -> t[]] : "
4334 "0 <= i < i' < n and 0 <= j,j' < n;"
4335 "[S2[i,j] -> x1[]] -> [S2[i,j'] -> x1[]] : "
4336 "0 <= i,j,j' < n and j < j' }",
4339 /* TACO 2013 Fig. 7, without tags */
4340 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
4341 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
4342 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
4343 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
4344 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
4345 "[n] -> { S2[i,j] -> S1[i,j'] : 0 <= i < n and 0 <= j < j' < n;"
4346 "S2[i,j] -> S1[i',j'] : 0 <= i < i' < n and 0 <= j,j' < n;"
4347 "S2[i,j] -> S2[i,j'] : 0 <= i,j,j' < n and j < j' }",
4350 /* TACO 2013 Fig. 12 */
4351 { "{ S1[i,0] : 0 <= i <= 1; S2[i,j] : 0 <= i <= 1 and 1 <= j <= 2;"
4352 "S3[i,3] : 0 <= i <= 1 }",
4353 "{ S1[i,0] -> S2[i,1] : 0 <= i <= 1;"
4354 "S2[i,1] -> S2[i,2] : 0 <= i <= 1;"
4355 "S2[i,2] -> S3[i,3] : 0 <= i <= 1 }",
4356 "{ [S1[i,0]->t[]] -> [S2[i,1]->t[]] : 0 <= i <= 1;"
4357 "[S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
4358 "[S2[i,2]->t[]] -> [S3[i,3]->t[]] : 0 <= i <= 1 }",
4359 "{ [S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
4360 "[S2[0,j]->t[]] -> [S2[1,j']->t[]] : 1 <= j,j' <= 2;"
4361 "[S2[0,j]->t[]] -> [S1[1,0]->t[]] : 1 <= j <= 2;"
4362 "[S3[0,3]->t[]] -> [S2[1,j]->t[]] : 1 <= j <= 2;"
4363 "[S3[0,3]->t[]] -> [S1[1,0]->t[]] }",
4368 /* Test schedule construction based on conditional constraints.
4369 * In particular, check the number of members in the outer band node
4370 * as an indication of whether tiling is possible or not.
4372 static int test_conditional_schedule_constraints(isl_ctx *ctx)
4374 int i;
4375 isl_union_set *domain;
4376 isl_union_map *condition;
4377 isl_union_map *flow;
4378 isl_union_map *validity;
4379 isl_schedule_constraints *sc;
4380 isl_schedule *schedule;
4381 isl_schedule_node *node;
4382 isl_size n_member;
4384 if (test_special_conditional_schedule_constraints(ctx) < 0)
4385 return -1;
4386 if (test_special_conditional_schedule_constraints_2(ctx) < 0)
4387 return -1;
4389 for (i = 0; i < ARRAY_SIZE(live_range_tests); ++i) {
4390 domain = isl_union_set_read_from_str(ctx,
4391 live_range_tests[i].domain);
4392 flow = isl_union_map_read_from_str(ctx,
4393 live_range_tests[i].flow);
4394 condition = isl_union_map_read_from_str(ctx,
4395 live_range_tests[i].condition);
4396 validity = isl_union_map_read_from_str(ctx,
4397 live_range_tests[i].conditional_validity);
4398 sc = isl_schedule_constraints_on_domain(domain);
4399 sc = isl_schedule_constraints_set_validity(sc,
4400 isl_union_map_copy(flow));
4401 sc = isl_schedule_constraints_set_proximity(sc, flow);
4402 sc = isl_schedule_constraints_set_conditional_validity(sc,
4403 condition, validity);
4404 schedule = isl_schedule_constraints_compute_schedule(sc);
4405 node = isl_schedule_get_root(schedule);
4406 while (node &&
4407 isl_schedule_node_get_type(node) != isl_schedule_node_band)
4408 node = isl_schedule_node_first_child(node);
4409 n_member = isl_schedule_node_band_n_member(node);
4410 isl_schedule_node_free(node);
4411 isl_schedule_free(schedule);
4413 if (!schedule || n_member < 0)
4414 return -1;
4415 if (n_member != live_range_tests[i].outer_band_n)
4416 isl_die(ctx, isl_error_unknown,
4417 "unexpected number of members in outer band",
4418 return -1);
4420 return 0;
4423 /* Check that the schedule computed for the given instance set and
4424 * dependence relation strongly satisfies the dependences.
4425 * In particular, check that no instance is scheduled before
4426 * or together with an instance on which it depends.
4427 * Earlier versions of isl would produce a schedule that
4428 * only weakly satisfies the dependences.
4430 static int test_strongly_satisfying_schedule(isl_ctx *ctx)
4432 const char *domain, *dep;
4433 isl_union_map *D, *schedule;
4434 isl_map *map, *ge;
4435 int empty;
4437 domain = "{ B[i0, i1] : 0 <= i0 <= 1 and 0 <= i1 <= 11; "
4438 "A[i0] : 0 <= i0 <= 1 }";
4439 dep = "{ B[i0, i1] -> B[i0, 1 + i1] : 0 <= i0 <= 1 and 0 <= i1 <= 10; "
4440 "B[0, 11] -> A[1]; A[i0] -> B[i0, 0] : 0 <= i0 <= 1 }";
4441 schedule = compute_schedule(ctx, domain, dep, dep);
4442 D = isl_union_map_read_from_str(ctx, dep);
4443 D = isl_union_map_apply_domain(D, isl_union_map_copy(schedule));
4444 D = isl_union_map_apply_range(D, schedule);
4445 map = isl_map_from_union_map(D);
4446 ge = isl_map_lex_ge(isl_space_domain(isl_map_get_space(map)));
4447 map = isl_map_intersect(map, ge);
4448 empty = isl_map_is_empty(map);
4449 isl_map_free(map);
4451 if (empty < 0)
4452 return -1;
4453 if (!empty)
4454 isl_die(ctx, isl_error_unknown,
4455 "dependences not strongly satisfied", return -1);
4457 return 0;
4460 /* Compute a schedule for input where the instance set constraints
4461 * conflict with the context constraints.
4462 * Earlier versions of isl did not properly handle this situation.
4464 static int test_conflicting_context_schedule(isl_ctx *ctx)
4466 isl_union_map *schedule;
4467 const char *domain, *context;
4469 domain = "[n] -> { A[] : n >= 0 }";
4470 context = "[n] -> { : n < 0 }";
4471 schedule = compute_schedule_with_context(ctx,
4472 domain, "{}", "{}", context);
4473 isl_union_map_free(schedule);
4475 if (!schedule)
4476 return -1;
4478 return 0;
4481 /* Check that a set of schedule constraints that only allow for
4482 * a coalescing schedule still produces a schedule even if the user
4483 * request a non-coalescing schedule. Earlier versions of isl
4484 * would not handle this case correctly.
4486 static int test_coalescing_schedule(isl_ctx *ctx)
4488 const char *domain, *dep;
4489 isl_union_set *I;
4490 isl_union_map *D;
4491 isl_schedule_constraints *sc;
4492 isl_schedule *schedule;
4493 int treat_coalescing;
4495 domain = "{ S[a, b] : 0 <= a <= 1 and 0 <= b <= 1 }";
4496 dep = "{ S[a, b] -> S[a + b, 1 - b] }";
4497 I = isl_union_set_read_from_str(ctx, domain);
4498 D = isl_union_map_read_from_str(ctx, dep);
4499 sc = isl_schedule_constraints_on_domain(I);
4500 sc = isl_schedule_constraints_set_validity(sc, D);
4501 treat_coalescing = isl_options_get_schedule_treat_coalescing(ctx);
4502 isl_options_set_schedule_treat_coalescing(ctx, 1);
4503 schedule = isl_schedule_constraints_compute_schedule(sc);
4504 isl_options_set_schedule_treat_coalescing(ctx, treat_coalescing);
4505 isl_schedule_free(schedule);
4506 if (!schedule)
4507 return -1;
4508 return 0;
4511 /* Check that the scheduler does not perform any needless
4512 * compound skewing. Earlier versions of isl would compute
4513 * schedules in terms of transformed schedule coefficients and
4514 * would not accurately keep track of the sum of the original
4515 * schedule coefficients. It could then produce the schedule
4516 * S[t,i,j,k] -> [t, 2t + i, 2t + i + j, 2t + i + j + k]
4517 * for the input below instead of the schedule below.
4519 static int test_skewing_schedule(isl_ctx *ctx)
4521 const char *D, *V, *P, *S;
4523 D = "[n] -> { S[t,i,j,k] : 0 <= t,i,j,k < n }";
4524 V = "[n] -> { S[t,i,j,k] -> S[t+1,a,b,c] : 0 <= t,i,j,k,a,b,c < n and "
4525 "-2 <= a-i <= 2 and -1 <= a-i + b-j <= 1 and "
4526 "-1 <= a-i + b-j + c-k <= 1 }";
4527 P = "{ }";
4528 S = "{ S[t,i,j,k] -> [t, 2t + i, t + i + j, 2t + k] }";
4530 return test_special_schedule(ctx, D, V, P, S);
4533 int test_schedule(isl_ctx *ctx)
4535 const char *D, *W, *R, *V, *P, *S;
4536 int max_coincidence;
4537 int treat_coalescing;
4539 /* Handle resulting schedule with zero bands. */
4540 if (test_one_schedule(ctx, "{[]}", "{}", "{}", "{[] -> []}", 0, 0) < 0)
4541 return -1;
4543 /* Jacobi */
4544 D = "[T,N] -> { S1[t,i] : 1 <= t <= T and 2 <= i <= N - 1 }";
4545 W = "{ S1[t,i] -> a[t,i] }";
4546 R = "{ S1[t,i] -> a[t-1,i]; S1[t,i] -> a[t-1,i-1]; "
4547 "S1[t,i] -> a[t-1,i+1] }";
4548 S = "{ S1[t,i] -> [t,i] }";
4549 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
4550 return -1;
4552 /* Fig. 5 of CC2008 */
4553 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and j >= 2 and "
4554 "j <= -1 + N }";
4555 W = "[N] -> { S_0[i, j] -> a[i, j] : i >= 0 and i <= -1 + N and "
4556 "j >= 2 and j <= -1 + N }";
4557 R = "[N] -> { S_0[i, j] -> a[j, i] : i >= 0 and i <= -1 + N and "
4558 "j >= 2 and j <= -1 + N; "
4559 "S_0[i, j] -> a[i, -1 + j] : i >= 0 and i <= -1 + N and "
4560 "j >= 2 and j <= -1 + N }";
4561 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
4562 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
4563 return -1;
4565 D = "{ S1[i] : 0 <= i <= 10; S2[i] : 0 <= i <= 9 }";
4566 W = "{ S1[i] -> a[i] }";
4567 R = "{ S2[i] -> a[i+1] }";
4568 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
4569 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
4570 return -1;
4572 D = "{ S1[i] : 0 <= i < 10; S2[i] : 0 <= i < 10 }";
4573 W = "{ S1[i] -> a[i] }";
4574 R = "{ S2[i] -> a[9-i] }";
4575 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
4576 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
4577 return -1;
4579 D = "[N] -> { S1[i] : 0 <= i < N; S2[i] : 0 <= i < N }";
4580 W = "{ S1[i] -> a[i] }";
4581 R = "[N] -> { S2[i] -> a[N-1-i] }";
4582 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
4583 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
4584 return -1;
4586 D = "{ S1[i] : 0 < i < 10; S2[i] : 0 <= i < 10 }";
4587 W = "{ S1[i] -> a[i]; S2[i] -> b[i] }";
4588 R = "{ S2[i] -> a[i]; S1[i] -> b[i-1] }";
4589 S = "{ S1[i] -> [i,0]; S2[i] -> [i,1] }";
4590 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4591 return -1;
4593 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
4594 W = "{ S1[i] -> a[0,i]; S2[i,j] -> a[i,j] }";
4595 R = "{ S2[i,j] -> a[i-1,j] }";
4596 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
4597 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
4598 return -1;
4600 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
4601 W = "{ S1[i] -> a[i,0]; S2[i,j] -> a[i,j] }";
4602 R = "{ S2[i,j] -> a[i,j-1] }";
4603 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
4604 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
4605 return -1;
4607 D = "[N] -> { S_0[]; S_1[i] : i >= 0 and i <= -1 + N; S_2[] }";
4608 W = "[N] -> { S_0[] -> a[0]; S_2[] -> b[0]; "
4609 "S_1[i] -> a[1 + i] : i >= 0 and i <= -1 + N }";
4610 R = "[N] -> { S_2[] -> a[N]; S_1[i] -> a[i] : i >= 0 and i <= -1 + N }";
4611 S = "[N] -> { S_1[i] -> [1, i, 0]; S_2[] -> [2, 0, 1]; "
4612 "S_0[] -> [0, 0, 0] }";
4613 if (test_one_schedule(ctx, D, W, R, S, 1, 0) < 0)
4614 return -1;
4615 ctx->opt->schedule_parametric = 0;
4616 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4617 return -1;
4618 ctx->opt->schedule_parametric = 1;
4620 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i] : 1 <= i <= N; "
4621 "S3[i,j] : 1 <= i,j <= N; S4[i] : 1 <= i <= N }";
4622 W = "{ S1[i] -> a[i,0]; S2[i] -> a[0,i]; S3[i,j] -> a[i,j] }";
4623 R = "[N] -> { S3[i,j] -> a[i-1,j]; S3[i,j] -> a[i,j-1]; "
4624 "S4[i] -> a[i,N] }";
4625 S = "{ S1[i] -> [0,i,0]; S2[i] -> [1,i,0]; S3[i,j] -> [2,i,j]; "
4626 "S4[i] -> [4,i,0] }";
4627 max_coincidence = isl_options_get_schedule_maximize_coincidence(ctx);
4628 isl_options_set_schedule_maximize_coincidence(ctx, 0);
4629 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
4630 return -1;
4631 isl_options_set_schedule_maximize_coincidence(ctx, max_coincidence);
4633 D = "[N] -> { S_0[i, j] : i >= 1 and i <= N and j >= 1 and j <= N }";
4634 W = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
4635 "j <= N }";
4636 R = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
4637 "j <= N; "
4638 "S_0[i, j] -> a[i, j] : i >= 1 and i <= N and j >= 1 and "
4639 "j <= N }";
4640 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
4641 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4642 return -1;
4644 D = "[N] -> { S_0[t] : t >= 0 and t <= -1 + N; "
4645 " S_2[t] : t >= 0 and t <= -1 + N; "
4646 " S_1[t, i] : t >= 0 and t <= -1 + N and i >= 0 and "
4647 "i <= -1 + N }";
4648 W = "[N] -> { S_0[t] -> a[t, 0] : t >= 0 and t <= -1 + N; "
4649 " S_2[t] -> b[t] : t >= 0 and t <= -1 + N; "
4650 " S_1[t, i] -> a[t, 1 + i] : t >= 0 and t <= -1 + N and "
4651 "i >= 0 and i <= -1 + N }";
4652 R = "[N] -> { S_1[t, i] -> a[t, i] : t >= 0 and t <= -1 + N and "
4653 "i >= 0 and i <= -1 + N; "
4654 " S_2[t] -> a[t, N] : t >= 0 and t <= -1 + N }";
4655 S = "[N] -> { S_2[t] -> [0, t, 2]; S_1[t, i] -> [0, t, 1, i, 0]; "
4656 " S_0[t] -> [0, t, 0] }";
4658 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
4659 return -1;
4660 ctx->opt->schedule_parametric = 0;
4661 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4662 return -1;
4663 ctx->opt->schedule_parametric = 1;
4665 D = "[N] -> { S1[i,j] : 0 <= i,j < N; S2[i,j] : 0 <= i,j < N }";
4666 S = "{ S1[i,j] -> [0,i,j]; S2[i,j] -> [1,i,j] }";
4667 if (test_one_schedule(ctx, D, "{}", "{}", S, 2, 2) < 0)
4668 return -1;
4670 D = "[M, N] -> { S_1[i] : i >= 0 and i <= -1 + M; "
4671 "S_0[i, j] : i >= 0 and i <= -1 + M and j >= 0 and j <= -1 + N }";
4672 W = "[M, N] -> { S_0[i, j] -> a[j] : i >= 0 and i <= -1 + M and "
4673 "j >= 0 and j <= -1 + N; "
4674 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
4675 R = "[M, N] -> { S_0[i, j] -> a[0] : i >= 0 and i <= -1 + M and "
4676 "j >= 0 and j <= -1 + N; "
4677 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
4678 S = "[M, N] -> { S_1[i] -> [1, i, 0]; S_0[i, j] -> [0, i, 0, j, 0] }";
4679 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4680 return -1;
4682 D = "{ S_0[i] : i >= 0 }";
4683 W = "{ S_0[i] -> a[i] : i >= 0 }";
4684 R = "{ S_0[i] -> a[0] : i >= 0 }";
4685 S = "{ S_0[i] -> [0, i, 0] }";
4686 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4687 return -1;
4689 D = "{ S_0[i] : i >= 0; S_1[i] : i >= 0 }";
4690 W = "{ S_0[i] -> a[i] : i >= 0; S_1[i] -> b[i] : i >= 0 }";
4691 R = "{ S_0[i] -> b[0] : i >= 0; S_1[i] -> a[i] : i >= 0 }";
4692 S = "{ S_1[i] -> [0, i, 1]; S_0[i] -> [0, i, 0] }";
4693 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4694 return -1;
4696 D = "[n] -> { S_0[j, k] : j <= -1 + n and j >= 0 and "
4697 "k <= -1 + n and k >= 0 }";
4698 W = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and " "k <= -1 + n and k >= 0 }";
4699 R = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and "
4700 "k <= -1 + n and k >= 0; "
4701 "S_0[j, k] -> B[k] : j <= -1 + n and j >= 0 and "
4702 "k <= -1 + n and k >= 0; "
4703 "S_0[j, k] -> A[k] : j <= -1 + n and j >= 0 and "
4704 "k <= -1 + n and k >= 0 }";
4705 S = "[n] -> { S_0[j, k] -> [2, j, k] }";
4706 ctx->opt->schedule_outer_coincidence = 1;
4707 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
4708 return -1;
4709 ctx->opt->schedule_outer_coincidence = 0;
4711 D = "{Stmt_for_body24[i0, i1, i2, i3]:"
4712 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 6 and i2 >= 2 and "
4713 "i2 <= 6 - i1 and i3 >= 0 and i3 <= -1 + i2;"
4714 "Stmt_for_body24[i0, i1, 1, 0]:"
4715 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 5;"
4716 "Stmt_for_body7[i0, i1, i2]:"
4717 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 7 and i2 >= 0 and "
4718 "i2 <= 7 }";
4720 V = "{Stmt_for_body24[0, i1, i2, i3] -> "
4721 "Stmt_for_body24[1, i1, i2, i3]:"
4722 "i3 >= 0 and i3 <= -1 + i2 and i1 >= 0 and i2 <= 6 - i1 and "
4723 "i2 >= 1;"
4724 "Stmt_for_body24[0, i1, i2, i3] -> "
4725 "Stmt_for_body7[1, 1 + i1 + i3, 1 + i1 + i2]:"
4726 "i3 <= -1 + i2 and i2 <= 6 - i1 and i2 >= 1 and i1 >= 0 and "
4727 "i3 >= 0;"
4728 "Stmt_for_body24[0, i1, i2, i3] ->"
4729 "Stmt_for_body7[1, i1, 1 + i1 + i3]:"
4730 "i3 >= 0 and i2 <= 6 - i1 and i1 >= 0 and i3 <= -1 + i2;"
4731 "Stmt_for_body7[0, i1, i2] -> Stmt_for_body7[1, i1, i2]:"
4732 "(i2 >= 1 + i1 and i2 <= 6 and i1 >= 0 and i1 <= 4) or "
4733 "(i2 >= 3 and i2 <= 7 and i1 >= 1 and i2 >= 1 + i1) or "
4734 "(i2 >= 0 and i2 <= i1 and i2 >= -7 + i1 and i1 <= 7);"
4735 "Stmt_for_body7[0, i1, 1 + i1] -> Stmt_for_body7[1, i1, 1 + i1]:"
4736 "i1 <= 6 and i1 >= 0;"
4737 "Stmt_for_body7[0, 0, 7] -> Stmt_for_body7[1, 0, 7];"
4738 "Stmt_for_body7[i0, i1, i2] -> "
4739 "Stmt_for_body24[i0, o1, -1 + i2 - o1, -1 + i1 - o1]:"
4740 "i0 >= 0 and i0 <= 1 and o1 >= 0 and i2 >= 1 + i1 and "
4741 "o1 <= -2 + i2 and i2 <= 7 and o1 <= -1 + i1;"
4742 "Stmt_for_body7[i0, i1, i2] -> "
4743 "Stmt_for_body24[i0, i1, o2, -1 - i1 + i2]:"
4744 "i0 >= 0 and i0 <= 1 and i1 >= 0 and o2 >= -i1 + i2 and "
4745 "o2 >= 1 and o2 <= 6 - i1 and i2 >= 1 + i1 }";
4746 P = V;
4748 treat_coalescing = isl_options_get_schedule_treat_coalescing(ctx);
4749 isl_options_set_schedule_treat_coalescing(ctx, 0);
4750 if (test_has_schedule(ctx, D, V, P) < 0)
4751 return -1;
4752 isl_options_set_schedule_treat_coalescing(ctx, treat_coalescing);
4754 D = "{ S_0[i, j] : i >= 1 and i <= 10 and j >= 1 and j <= 8 }";
4755 V = "{ S_0[i, j] -> S_0[i, 1 + j] : i >= 1 and i <= 10 and "
4756 "j >= 1 and j <= 7;"
4757 "S_0[i, j] -> S_0[1 + i, j] : i >= 1 and i <= 9 and "
4758 "j >= 1 and j <= 8 }";
4759 P = "{ }";
4760 S = "{ S_0[i, j] -> [i + j, i] }";
4761 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4762 if (test_special_schedule(ctx, D, V, P, S) < 0)
4763 return -1;
4764 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4766 /* Fig. 1 from Feautrier's "Some Efficient Solutions..." pt. 2, 1992 */
4767 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and "
4768 "j >= 0 and j <= -1 + i }";
4769 V = "[N] -> { S_0[i, j] -> S_0[i, 1 + j] : j <= -2 + i and "
4770 "i <= -1 + N and j >= 0;"
4771 "S_0[i, -1 + i] -> S_0[1 + i, 0] : i >= 1 and "
4772 "i <= -2 + N }";
4773 P = "{ }";
4774 S = "{ S_0[i, j] -> [i, j] }";
4775 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4776 if (test_special_schedule(ctx, D, V, P, S) < 0)
4777 return -1;
4778 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4780 /* Test both algorithms on a case with only proximity dependences. */
4781 D = "{ S[i,j] : 0 <= i <= 10 }";
4782 V = "{ }";
4783 P = "{ S[i,j] -> S[i+1,j] : 0 <= i,j <= 10 }";
4784 S = "{ S[i, j] -> [j, i] }";
4785 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4786 if (test_special_schedule(ctx, D, V, P, S) < 0)
4787 return -1;
4788 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4789 if (test_special_schedule(ctx, D, V, P, S) < 0)
4790 return -1;
4792 D = "{ A[a]; B[] }";
4793 V = "{}";
4794 P = "{ A[a] -> B[] }";
4795 if (test_has_schedule(ctx, D, V, P) < 0)
4796 return -1;
4798 if (test_padded_schedule(ctx) < 0)
4799 return -1;
4801 /* Check that check for progress is not confused by rational
4802 * solution.
4804 D = "[N] -> { S0[i, j] : i >= 0 and i <= N and j >= 0 and j <= N }";
4805 V = "[N] -> { S0[i0, -1 + N] -> S0[2 + i0, 0] : i0 >= 0 and "
4806 "i0 <= -2 + N; "
4807 "S0[i0, i1] -> S0[i0, 1 + i1] : i0 >= 0 and "
4808 "i0 <= N and i1 >= 0 and i1 <= -1 + N }";
4809 P = "{}";
4810 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4811 if (test_has_schedule(ctx, D, V, P) < 0)
4812 return -1;
4813 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4815 /* Check that we allow schedule rows that are only non-trivial
4816 * on some full-dimensional domains.
4818 D = "{ S1[j] : 0 <= j <= 1; S0[]; S2[k] : 0 <= k <= 1 }";
4819 V = "{ S0[] -> S1[j] : 0 <= j <= 1; S2[0] -> S0[];"
4820 "S1[j] -> S2[1] : 0 <= j <= 1 }";
4821 P = "{}";
4822 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
4823 if (test_has_schedule(ctx, D, V, P) < 0)
4824 return -1;
4825 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
4827 if (test_conditional_schedule_constraints(ctx) < 0)
4828 return -1;
4830 if (test_strongly_satisfying_schedule(ctx) < 0)
4831 return -1;
4833 if (test_conflicting_context_schedule(ctx) < 0)
4834 return -1;
4836 if (test_coalescing_schedule(ctx) < 0)
4837 return -1;
4838 if (test_skewing_schedule(ctx) < 0)
4839 return -1;
4841 return 0;
4844 /* Perform scheduling tests using the whole component scheduler.
4846 static int test_schedule_whole(isl_ctx *ctx)
4848 int whole;
4849 int r;
4851 whole = isl_options_get_schedule_whole_component(ctx);
4852 isl_options_set_schedule_whole_component(ctx, 1);
4853 r = test_schedule(ctx);
4854 isl_options_set_schedule_whole_component(ctx, whole);
4856 return r;
4859 /* Perform scheduling tests using the incremental scheduler.
4861 static int test_schedule_incremental(isl_ctx *ctx)
4863 int whole;
4864 int r;
4866 whole = isl_options_get_schedule_whole_component(ctx);
4867 isl_options_set_schedule_whole_component(ctx, 0);
4868 r = test_schedule(ctx);
4869 isl_options_set_schedule_whole_component(ctx, whole);
4871 return r;
4874 int test_plain_injective(isl_ctx *ctx, const char *str, int injective)
4876 isl_union_map *umap;
4877 int test;
4879 umap = isl_union_map_read_from_str(ctx, str);
4880 test = isl_union_map_plain_is_injective(umap);
4881 isl_union_map_free(umap);
4882 if (test < 0)
4883 return -1;
4884 if (test == injective)
4885 return 0;
4886 if (injective)
4887 isl_die(ctx, isl_error_unknown,
4888 "map not detected as injective", return -1);
4889 else
4890 isl_die(ctx, isl_error_unknown,
4891 "map detected as injective", return -1);
4894 int test_injective(isl_ctx *ctx)
4896 const char *str;
4898 if (test_plain_injective(ctx, "{S[i,j] -> A[0]; T[i,j] -> B[1]}", 0))
4899 return -1;
4900 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> B[0]}", 1))
4901 return -1;
4902 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[1]}", 1))
4903 return -1;
4904 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[0]}", 0))
4905 return -1;
4906 if (test_plain_injective(ctx, "{S[i] -> A[i,0]; T[i] -> A[i,1]}", 1))
4907 return -1;
4908 if (test_plain_injective(ctx, "{S[i] -> A[i]; T[i] -> A[i]}", 0))
4909 return -1;
4910 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[0,1]}", 1))
4911 return -1;
4912 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[1,0]}", 1))
4913 return -1;
4915 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[1,0]}";
4916 if (test_plain_injective(ctx, str, 1))
4917 return -1;
4918 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[0,0]}";
4919 if (test_plain_injective(ctx, str, 0))
4920 return -1;
4922 return 0;
4925 static int aff_plain_is_equal(__isl_keep isl_aff *aff, const char *str)
4927 isl_aff *aff2;
4928 int equal;
4930 if (!aff)
4931 return -1;
4933 aff2 = isl_aff_read_from_str(isl_aff_get_ctx(aff), str);
4934 equal = isl_aff_plain_is_equal(aff, aff2);
4935 isl_aff_free(aff2);
4937 return equal;
4940 static int aff_check_plain_equal(__isl_keep isl_aff *aff, const char *str)
4942 int equal;
4944 equal = aff_plain_is_equal(aff, str);
4945 if (equal < 0)
4946 return -1;
4947 if (!equal)
4948 isl_die(isl_aff_get_ctx(aff), isl_error_unknown,
4949 "result not as expected", return -1);
4950 return 0;
4953 /* Is "upa" obviously equal to the isl_union_pw_aff represented by "str"?
4955 static isl_bool union_pw_aff_plain_is_equal(__isl_keep isl_union_pw_aff *upa,
4956 const char *str)
4958 isl_ctx *ctx;
4959 isl_union_pw_aff *upa2;
4960 isl_bool equal;
4962 if (!upa)
4963 return isl_bool_error;
4965 ctx = isl_union_pw_aff_get_ctx(upa);
4966 upa2 = isl_union_pw_aff_read_from_str(ctx, str);
4967 equal = isl_union_pw_aff_plain_is_equal(upa, upa2);
4968 isl_union_pw_aff_free(upa2);
4970 return equal;
4973 /* Check that "upa" is obviously equal to the isl_union_pw_aff
4974 * represented by "str".
4976 static isl_stat union_pw_aff_check_plain_equal(__isl_keep isl_union_pw_aff *upa,
4977 const char *str)
4979 isl_bool equal;
4981 equal = union_pw_aff_plain_is_equal(upa, str);
4982 if (equal < 0)
4983 return isl_stat_error;
4984 if (!equal)
4985 isl_die(isl_union_pw_aff_get_ctx(upa), isl_error_unknown,
4986 "result not as expected", return isl_stat_error);
4987 return isl_stat_ok;
4990 /* Basic tests on isl_union_pw_aff.
4992 * In particular, check that isl_union_pw_aff_aff_on_domain
4993 * aligns the parameters of the input objects and
4994 * that isl_union_pw_aff_param_on_domain_id properly
4995 * introduces the parameter.
4997 static int test_upa(isl_ctx *ctx)
4999 const char *str;
5000 isl_id *id;
5001 isl_aff *aff;
5002 isl_union_set *domain;
5003 isl_union_pw_aff *upa;
5004 isl_stat ok;
5006 aff = isl_aff_read_from_str(ctx, "[N] -> { [N] }");
5007 str = "[M] -> { A[i] : 0 <= i < M; B[] }";
5008 domain = isl_union_set_read_from_str(ctx, str);
5009 upa = isl_union_pw_aff_aff_on_domain(domain, aff);
5010 str = "[N, M] -> { A[i] -> [N] : 0 <= i < M; B[] -> [N] }";
5011 ok = union_pw_aff_check_plain_equal(upa, str);
5012 isl_union_pw_aff_free(upa);
5013 if (ok < 0)
5014 return -1;
5016 id = isl_id_alloc(ctx, "N", NULL);
5017 str = "[M] -> { A[i] : 0 <= i < M; B[] }";
5018 domain = isl_union_set_read_from_str(ctx, str);
5019 upa = isl_union_pw_aff_param_on_domain_id(domain, id);
5020 str = "[N, M] -> { A[i] -> [N] : 0 <= i < M; B[] -> [N] }";
5021 ok = union_pw_aff_check_plain_equal(upa, str);
5022 isl_union_pw_aff_free(upa);
5023 if (ok < 0)
5024 return -1;
5026 return 0;
5029 struct {
5030 __isl_give isl_aff *(*fn)(__isl_take isl_aff *aff1,
5031 __isl_take isl_aff *aff2);
5032 } aff_bin_op[] = {
5033 ['+'] = { &isl_aff_add },
5034 ['-'] = { &isl_aff_sub },
5035 ['*'] = { &isl_aff_mul },
5036 ['/'] = { &isl_aff_div },
5039 struct {
5040 const char *arg1;
5041 unsigned char op;
5042 const char *arg2;
5043 const char *res;
5044 } aff_bin_tests[] = {
5045 { "{ [i] -> [i] }", '+', "{ [i] -> [i] }",
5046 "{ [i] -> [2i] }" },
5047 { "{ [i] -> [i] }", '-', "{ [i] -> [i] }",
5048 "{ [i] -> [0] }" },
5049 { "{ [i] -> [i] }", '*', "{ [i] -> [2] }",
5050 "{ [i] -> [2i] }" },
5051 { "{ [i] -> [2] }", '*', "{ [i] -> [i] }",
5052 "{ [i] -> [2i] }" },
5053 { "{ [i] -> [i] }", '/', "{ [i] -> [2] }",
5054 "{ [i] -> [i/2] }" },
5055 { "{ [i] -> [2i] }", '/', "{ [i] -> [2] }",
5056 "{ [i] -> [i] }" },
5057 { "{ [i] -> [i] }", '+', "{ [i] -> [NaN] }",
5058 "{ [i] -> [NaN] }" },
5059 { "{ [i] -> [i] }", '-', "{ [i] -> [NaN] }",
5060 "{ [i] -> [NaN] }" },
5061 { "{ [i] -> [i] }", '*', "{ [i] -> [NaN] }",
5062 "{ [i] -> [NaN] }" },
5063 { "{ [i] -> [2] }", '*', "{ [i] -> [NaN] }",
5064 "{ [i] -> [NaN] }" },
5065 { "{ [i] -> [i] }", '/', "{ [i] -> [NaN] }",
5066 "{ [i] -> [NaN] }" },
5067 { "{ [i] -> [2] }", '/', "{ [i] -> [NaN] }",
5068 "{ [i] -> [NaN] }" },
5069 { "{ [i] -> [NaN] }", '+', "{ [i] -> [i] }",
5070 "{ [i] -> [NaN] }" },
5071 { "{ [i] -> [NaN] }", '-', "{ [i] -> [i] }",
5072 "{ [i] -> [NaN] }" },
5073 { "{ [i] -> [NaN] }", '*', "{ [i] -> [2] }",
5074 "{ [i] -> [NaN] }" },
5075 { "{ [i] -> [NaN] }", '*', "{ [i] -> [i] }",
5076 "{ [i] -> [NaN] }" },
5077 { "{ [i] -> [NaN] }", '/', "{ [i] -> [2] }",
5078 "{ [i] -> [NaN] }" },
5079 { "{ [i] -> [NaN] }", '/', "{ [i] -> [i] }",
5080 "{ [i] -> [NaN] }" },
5083 /* Perform some basic tests of binary operations on isl_aff objects.
5085 static int test_bin_aff(isl_ctx *ctx)
5087 int i;
5088 isl_aff *aff1, *aff2, *res;
5089 __isl_give isl_aff *(*fn)(__isl_take isl_aff *aff1,
5090 __isl_take isl_aff *aff2);
5091 int ok;
5093 for (i = 0; i < ARRAY_SIZE(aff_bin_tests); ++i) {
5094 aff1 = isl_aff_read_from_str(ctx, aff_bin_tests[i].arg1);
5095 aff2 = isl_aff_read_from_str(ctx, aff_bin_tests[i].arg2);
5096 res = isl_aff_read_from_str(ctx, aff_bin_tests[i].res);
5097 fn = aff_bin_op[aff_bin_tests[i].op].fn;
5098 aff1 = fn(aff1, aff2);
5099 if (isl_aff_is_nan(res))
5100 ok = isl_aff_is_nan(aff1);
5101 else
5102 ok = isl_aff_plain_is_equal(aff1, res);
5103 isl_aff_free(aff1);
5104 isl_aff_free(res);
5105 if (ok < 0)
5106 return -1;
5107 if (!ok)
5108 isl_die(ctx, isl_error_unknown,
5109 "unexpected result", return -1);
5112 return 0;
5115 struct {
5116 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pa1,
5117 __isl_take isl_pw_aff *pa2);
5118 } pw_aff_bin_op[] = {
5119 ['m'] = { &isl_pw_aff_min },
5120 ['M'] = { &isl_pw_aff_max },
5123 /* Inputs for binary isl_pw_aff operation tests.
5124 * "arg1" and "arg2" are the two arguments, "op" identifies the operation
5125 * defined by pw_aff_bin_op, and "res" is the expected result.
5127 struct {
5128 const char *arg1;
5129 unsigned char op;
5130 const char *arg2;
5131 const char *res;
5132 } pw_aff_bin_tests[] = {
5133 { "{ [i] -> [i] }", 'm', "{ [i] -> [i] }",
5134 "{ [i] -> [i] }" },
5135 { "{ [i] -> [i] }", 'M', "{ [i] -> [i] }",
5136 "{ [i] -> [i] }" },
5137 { "{ [i] -> [i] }", 'm', "{ [i] -> [0] }",
5138 "{ [i] -> [i] : i <= 0; [i] -> [0] : i > 0 }" },
5139 { "{ [i] -> [i] }", 'M', "{ [i] -> [0] }",
5140 "{ [i] -> [i] : i >= 0; [i] -> [0] : i < 0 }" },
5141 { "{ [i] -> [i] }", 'm', "{ [i] -> [NaN] }",
5142 "{ [i] -> [NaN] }" },
5143 { "{ [i] -> [NaN] }", 'm', "{ [i] -> [i] }",
5144 "{ [i] -> [NaN] }" },
5147 /* Perform some basic tests of binary operations on isl_pw_aff objects.
5149 static int test_bin_pw_aff(isl_ctx *ctx)
5151 int i;
5152 isl_bool ok;
5153 isl_pw_aff *pa1, *pa2, *res;
5155 for (i = 0; i < ARRAY_SIZE(pw_aff_bin_tests); ++i) {
5156 pa1 = isl_pw_aff_read_from_str(ctx, pw_aff_bin_tests[i].arg1);
5157 pa2 = isl_pw_aff_read_from_str(ctx, pw_aff_bin_tests[i].arg2);
5158 res = isl_pw_aff_read_from_str(ctx, pw_aff_bin_tests[i].res);
5159 pa1 = pw_aff_bin_op[pw_aff_bin_tests[i].op].fn(pa1, pa2);
5160 if (isl_pw_aff_involves_nan(res))
5161 ok = isl_pw_aff_involves_nan(pa1);
5162 else
5163 ok = isl_pw_aff_plain_is_equal(pa1, res);
5164 isl_pw_aff_free(pa1);
5165 isl_pw_aff_free(res);
5166 if (ok < 0)
5167 return -1;
5168 if (!ok)
5169 isl_die(ctx, isl_error_unknown,
5170 "unexpected result", return -1);
5173 return 0;
5176 struct {
5177 __isl_give isl_union_pw_multi_aff *(*fn)(
5178 __isl_take isl_union_pw_multi_aff *upma1,
5179 __isl_take isl_union_pw_multi_aff *upma2);
5180 const char *arg1;
5181 const char *arg2;
5182 const char *res;
5183 } upma_bin_tests[] = {
5184 { &isl_union_pw_multi_aff_add, "{ A[] -> [0]; B[0] -> [1] }",
5185 "{ B[x] -> [2] : x >= 0 }", "{ B[0] -> [3] }" },
5186 { &isl_union_pw_multi_aff_union_add, "{ A[] -> [0]; B[0] -> [1] }",
5187 "{ B[x] -> [2] : x >= 0 }",
5188 "{ A[] -> [0]; B[0] -> [3]; B[x] -> [2] : x >= 1 }" },
5189 { &isl_union_pw_multi_aff_pullback_union_pw_multi_aff,
5190 "{ A[] -> B[0]; C[x] -> B[1] : x < 10; C[y] -> B[2] : y >= 10 }",
5191 "{ D[i] -> A[] : i < 0; D[i] -> C[i + 5] : i >= 0 }",
5192 "{ D[i] -> B[0] : i < 0; D[i] -> B[1] : 0 <= i < 5; "
5193 "D[i] -> B[2] : i >= 5 }" },
5194 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
5195 "{ B[x] -> C[2] : x > 0 }",
5196 "{ B[x] -> A[1] : x <= 0; B[x] -> C[2] : x > 0 }" },
5197 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
5198 "{ B[x] -> A[2] : x >= 0 }",
5199 "{ B[x] -> A[1] : x < 0; B[x] -> A[2] : x > 0; B[0] -> A[3] }" },
5202 /* Perform some basic tests of binary operations on
5203 * isl_union_pw_multi_aff objects.
5205 static int test_bin_upma(isl_ctx *ctx)
5207 int i;
5208 isl_union_pw_multi_aff *upma1, *upma2, *res;
5209 int ok;
5211 for (i = 0; i < ARRAY_SIZE(upma_bin_tests); ++i) {
5212 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
5213 upma_bin_tests[i].arg1);
5214 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
5215 upma_bin_tests[i].arg2);
5216 res = isl_union_pw_multi_aff_read_from_str(ctx,
5217 upma_bin_tests[i].res);
5218 upma1 = upma_bin_tests[i].fn(upma1, upma2);
5219 ok = isl_union_pw_multi_aff_plain_is_equal(upma1, res);
5220 isl_union_pw_multi_aff_free(upma1);
5221 isl_union_pw_multi_aff_free(res);
5222 if (ok < 0)
5223 return -1;
5224 if (!ok)
5225 isl_die(ctx, isl_error_unknown,
5226 "unexpected result", return -1);
5229 return 0;
5232 struct {
5233 __isl_give isl_union_pw_multi_aff *(*fn)(
5234 __isl_take isl_union_pw_multi_aff *upma1,
5235 __isl_take isl_union_pw_multi_aff *upma2);
5236 const char *arg1;
5237 const char *arg2;
5238 } upma_bin_fail_tests[] = {
5239 { &isl_union_pw_multi_aff_union_add, "{ B[x] -> A[1] : x <= 0 }",
5240 "{ B[x] -> C[2] : x >= 0 }" },
5243 /* Perform some basic tests of binary operations on
5244 * isl_union_pw_multi_aff objects that are expected to fail.
5246 static int test_bin_upma_fail(isl_ctx *ctx)
5248 int i, n;
5249 isl_union_pw_multi_aff *upma1, *upma2;
5250 int on_error;
5252 on_error = isl_options_get_on_error(ctx);
5253 isl_options_set_on_error(ctx, ISL_ON_ERROR_CONTINUE);
5254 n = ARRAY_SIZE(upma_bin_fail_tests);
5255 for (i = 0; i < n; ++i) {
5256 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
5257 upma_bin_fail_tests[i].arg1);
5258 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
5259 upma_bin_fail_tests[i].arg2);
5260 upma1 = upma_bin_fail_tests[i].fn(upma1, upma2);
5261 isl_union_pw_multi_aff_free(upma1);
5262 if (upma1)
5263 break;
5265 isl_options_set_on_error(ctx, on_error);
5266 if (i < n)
5267 isl_die(ctx, isl_error_unknown,
5268 "operation not expected to succeed", return -1);
5270 return 0;
5273 /* Inputs for basic tests of unary operations on isl_multi_pw_aff objects.
5274 * "fn" is the function that is tested.
5275 * "arg" is a string description of the input.
5276 * "res" is a string description of the expected result.
5278 struct {
5279 __isl_give isl_multi_pw_aff *(*fn)(__isl_take isl_multi_pw_aff *mpa);
5280 const char *arg;
5281 const char *res;
5282 } mpa_un_tests[] = {
5283 { &isl_multi_pw_aff_range_factor_domain,
5284 "{ A[x] -> [B[(1 : x >= 5)] -> C[(2 : x <= 10)]] }",
5285 "{ A[x] -> B[(1 : x >= 5)] }" },
5286 { &isl_multi_pw_aff_range_factor_range,
5287 "{ A[x] -> [B[(1 : x >= 5)] -> C[(2 : x <= 10)]] }",
5288 "{ A[y] -> C[(2 : y <= 10)] }" },
5289 { &isl_multi_pw_aff_range_factor_domain,
5290 "{ A[x] -> [B[(1 : x >= 5)] -> C[]] }",
5291 "{ A[x] -> B[(1 : x >= 5)] }" },
5292 { &isl_multi_pw_aff_range_factor_range,
5293 "{ A[x] -> [B[(1 : x >= 5)] -> C[]] }",
5294 "{ A[y] -> C[] }" },
5295 { &isl_multi_pw_aff_range_factor_domain,
5296 "{ A[x] -> [B[] -> C[(2 : x <= 10)]] }",
5297 "{ A[x] -> B[] }" },
5298 { &isl_multi_pw_aff_range_factor_range,
5299 "{ A[x] -> [B[] -> C[(2 : x <= 10)]] }",
5300 "{ A[y] -> C[(2 : y <= 10)] }" },
5301 { &isl_multi_pw_aff_range_factor_domain,
5302 "{ A[x] -> [B[] -> C[]] }",
5303 "{ A[x] -> B[] }" },
5304 { &isl_multi_pw_aff_range_factor_range,
5305 "{ A[x] -> [B[] -> C[]] }",
5306 "{ A[y] -> C[] }" },
5307 { &isl_multi_pw_aff_factor_range,
5308 "{ [B[] -> C[]] }",
5309 "{ C[] }" },
5310 { &isl_multi_pw_aff_range_factor_domain,
5311 "{ A[x] -> [B[] -> C[]] : x >= 0 }",
5312 "{ A[x] -> B[] : x >= 0 }" },
5313 { &isl_multi_pw_aff_range_factor_range,
5314 "{ A[x] -> [B[] -> C[]] : x >= 0 }",
5315 "{ A[y] -> C[] : y >= 0 }" },
5316 { &isl_multi_pw_aff_factor_range,
5317 "[N] -> { [B[] -> C[]] : N >= 0 }",
5318 "[N] -> { C[] : N >= 0 }" },
5321 /* Perform some basic tests of unary operations on isl_multi_pw_aff objects.
5323 static int test_un_mpa(isl_ctx *ctx)
5325 int i;
5326 isl_bool ok;
5327 isl_multi_pw_aff *mpa, *res;
5329 for (i = 0; i < ARRAY_SIZE(mpa_un_tests); ++i) {
5330 mpa = isl_multi_pw_aff_read_from_str(ctx, mpa_un_tests[i].arg);
5331 res = isl_multi_pw_aff_read_from_str(ctx, mpa_un_tests[i].res);
5332 mpa = mpa_un_tests[i].fn(mpa);
5333 ok = isl_multi_pw_aff_plain_is_equal(mpa, res);
5334 isl_multi_pw_aff_free(mpa);
5335 isl_multi_pw_aff_free(res);
5336 if (ok < 0)
5337 return -1;
5338 if (!ok)
5339 isl_die(ctx, isl_error_unknown,
5340 "unexpected result", return -1);
5343 return 0;
5346 /* Inputs for basic tests of binary operations on isl_multi_pw_aff objects.
5347 * "fn" is the function that is tested.
5348 * "arg1" and "arg2" are string descriptions of the inputs.
5349 * "res" is a string description of the expected result.
5351 struct {
5352 __isl_give isl_multi_pw_aff *(*fn)(
5353 __isl_take isl_multi_pw_aff *mpa1,
5354 __isl_take isl_multi_pw_aff *mpa2);
5355 const char *arg1;
5356 const char *arg2;
5357 const char *res;
5358 } mpa_bin_tests[] = {
5359 { &isl_multi_pw_aff_add, "{ A[] -> [1] }", "{ A[] -> [2] }",
5360 "{ A[] -> [3] }" },
5361 { &isl_multi_pw_aff_add, "{ A[x] -> [(1 : x >= 5)] }",
5362 "{ A[x] -> [(x : x <= 10)] }",
5363 "{ A[x] -> [(1 + x : 5 <= x <= 10)] }" },
5364 { &isl_multi_pw_aff_add, "{ A[x] -> [] : x >= 5 }",
5365 "{ A[x] -> [] : x <= 10 }",
5366 "{ A[x] -> [] : 5 <= x <= 10 }" },
5367 { &isl_multi_pw_aff_add, "{ A[x] -> [] : x >= 5 }",
5368 "[N] -> { A[x] -> [] : x <= N }",
5369 "[N] -> { A[x] -> [] : 5 <= x <= N }" },
5370 { &isl_multi_pw_aff_add,
5371 "[N] -> { A[x] -> [] : x <= N }",
5372 "{ A[x] -> [] : x >= 5 }",
5373 "[N] -> { A[x] -> [] : 5 <= x <= N }" },
5374 { &isl_multi_pw_aff_range_product, "{ A[x] -> B[(1 : x >= 5)] }",
5375 "{ A[y] -> C[(2 : y <= 10)] }",
5376 "{ A[x] -> [B[(1 : x >= 5)] -> C[(2 : x <= 10)]] }" },
5377 { &isl_multi_pw_aff_range_product, "{ A[x] -> B[1] : x >= 5 }",
5378 "{ A[y] -> C[2] : y <= 10 }",
5379 "{ A[x] -> [B[(1 : x >= 5)] -> C[(2 : x <= 10)]] }" },
5380 { &isl_multi_pw_aff_range_product, "{ A[x] -> B[1] : x >= 5 }",
5381 "[N] -> { A[y] -> C[2] : y <= N }",
5382 "[N] -> { A[x] -> [B[(1 : x >= 5)] -> C[(2 : x <= N)]] }" },
5383 { &isl_multi_pw_aff_range_product, "[N] -> { A[x] -> B[1] : x >= N }",
5384 "{ A[y] -> C[2] : y <= 10 }",
5385 "[N] -> { A[x] -> [B[(1 : x >= N)] -> C[(2 : x <= 10)]] }" },
5386 { &isl_multi_pw_aff_range_product, "{ A[] -> B[1] }", "{ A[] -> C[2] }",
5387 "{ A[] -> [B[1] -> C[2]] }" },
5388 { &isl_multi_pw_aff_range_product, "{ A[] -> B[] }", "{ A[] -> C[] }",
5389 "{ A[] -> [B[] -> C[]] }" },
5390 { &isl_multi_pw_aff_range_product, "{ A[x] -> B[(1 : x >= 5)] }",
5391 "{ A[y] -> C[] : y <= 10 }",
5392 "{ A[x] -> [B[(1 : x >= 5)] -> C[]] : x <= 10 }" },
5393 { &isl_multi_pw_aff_range_product, "{ A[y] -> C[] : y <= 10 }",
5394 "{ A[x] -> B[(1 : x >= 5)] }",
5395 "{ A[x] -> [C[] -> B[(1 : x >= 5)]] : x <= 10 }" },
5396 { &isl_multi_pw_aff_product, "{ A[x] -> B[(1 : x >= 5)] }",
5397 "{ A[y] -> C[(2 : y <= 10)] }",
5398 "{ [A[x] -> A[y]] -> [B[(1 : x >= 5)] -> C[(2 : y <= 10)]] }" },
5399 { &isl_multi_pw_aff_product, "{ A[x] -> B[(1 : x >= 5)] }",
5400 "{ A[y] -> C[] : y <= 10 }",
5401 "{ [A[x] -> A[y]] -> [B[(1 : x >= 5)] -> C[]] : y <= 10 }" },
5402 { &isl_multi_pw_aff_product, "{ A[y] -> C[] : y <= 10 }",
5403 "{ A[x] -> B[(1 : x >= 5)] }",
5404 "{ [A[y] -> A[x]] -> [C[] -> B[(1 : x >= 5)]] : y <= 10 }" },
5405 { &isl_multi_pw_aff_product, "{ A[x] -> B[(1 : x >= 5)] }",
5406 "[N] -> { A[y] -> C[] : y <= N }",
5407 "[N] -> { [A[x] -> A[y]] -> [B[(1 : x >= 5)] -> C[]] : y <= N }" },
5408 { &isl_multi_pw_aff_product, "[N] -> { A[y] -> C[] : y <= N }",
5409 "{ A[x] -> B[(1 : x >= 5)] }",
5410 "[N] -> { [A[y] -> A[x]] -> [C[] -> B[(1 : x >= 5)]] : y <= N }" },
5411 { &isl_multi_pw_aff_product, "{ A[x] -> B[] : x >= 5 }",
5412 "{ A[y] -> C[] : y <= 10 }",
5413 "{ [A[x] -> A[y]] -> [B[] -> C[]] : x >= 5 and y <= 10 }" },
5414 { &isl_multi_pw_aff_product, "{ A[] -> B[1] }", "{ A[] -> C[2] }",
5415 "{ [A[] -> A[]] -> [B[1] -> C[2]] }" },
5416 { &isl_multi_pw_aff_product, "{ A[] -> B[] }", "{ A[] -> C[] }",
5417 "{ [A[] -> A[]] -> [B[] -> C[]] }" },
5418 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5419 "{ B[i,j] -> C[i + 2j] }", "{ A[a,b] -> B[b,a] }",
5420 "{ A[a,b] -> C[b + 2a] }" },
5421 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5422 "{ B[i,j] -> C[i + 2j] }",
5423 "{ A[a,b] -> B[(b : b > a),(a : b > a)] }",
5424 "{ A[a,b] -> C[(b + 2a : b > a)] }" },
5425 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5426 "{ B[i,j] -> C[(i + 2j : j > 4)] }",
5427 "{ A[a,b] -> B[(b : b > a),(a : b > a)] }",
5428 "{ A[a,b] -> C[(b + 2a : b > a > 4)] }" },
5429 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5430 "{ B[i,j] -> C[] }",
5431 "{ A[a,b] -> B[(b : b > a),(a : b > a)] }",
5432 "{ A[a,b] -> C[] }" },
5433 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5434 "{ B[i,j] -> C[] : i > j }",
5435 "{ A[a,b] -> B[b,a] }",
5436 "{ A[a,b] -> C[] : b > a }" },
5437 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5438 "{ B[i,j] -> C[] : j > 5 }",
5439 "{ A[a,b] -> B[(b : b > a),(a : b > a)] }",
5440 "{ A[a,b] -> C[] : b > a > 5 }" },
5441 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5442 "[N] -> { B[i,j] -> C[] : j > N }",
5443 "{ A[a,b] -> B[(b : b > a),(a : b > a)] }",
5444 "[N] -> { A[a,b] -> C[] : b > a > N }" },
5445 { &isl_multi_pw_aff_pullback_multi_pw_aff,
5446 "[M,N] -> { B[] -> C[] : N > 5 }",
5447 "[M,N] -> { A[] -> B[] : M > N }",
5448 "[M,N] -> { A[] -> C[] : M > N > 5 }" },
5451 /* Perform some basic tests of binary operations on isl_multi_pw_aff objects.
5453 static int test_bin_mpa(isl_ctx *ctx)
5455 int i;
5456 isl_bool ok;
5457 isl_multi_pw_aff *mpa1, *mpa2, *res;
5459 for (i = 0; i < ARRAY_SIZE(mpa_bin_tests); ++i) {
5460 mpa1 = isl_multi_pw_aff_read_from_str(ctx,
5461 mpa_bin_tests[i].arg1);
5462 mpa2 = isl_multi_pw_aff_read_from_str(ctx,
5463 mpa_bin_tests[i].arg2);
5464 res = isl_multi_pw_aff_read_from_str(ctx,
5465 mpa_bin_tests[i].res);
5466 mpa1 = mpa_bin_tests[i].fn(mpa1, mpa2);
5467 ok = isl_multi_pw_aff_plain_is_equal(mpa1, res);
5468 isl_multi_pw_aff_free(mpa1);
5469 isl_multi_pw_aff_free(res);
5470 if (ok < 0)
5471 return -1;
5472 if (!ok)
5473 isl_die(ctx, isl_error_unknown,
5474 "unexpected result", return -1);
5477 return 0;
5480 /* Inputs for basic tests of unary operations on
5481 * isl_multi_union_pw_aff objects.
5482 * "fn" is the function that is tested.
5483 * "arg" is a string description of the input.
5484 * "res" is a string description of the expected result.
5486 struct {
5487 __isl_give isl_multi_union_pw_aff *(*fn)(
5488 __isl_take isl_multi_union_pw_aff *mupa);
5489 const char *arg;
5490 const char *res;
5491 } mupa_un_tests[] = {
5492 { &isl_multi_union_pw_aff_factor_range,
5493 "[B[{ A[] -> [1] }] -> C[{ A[] -> [2] }]]",
5494 "C[{ A[] -> [2] }]" },
5495 { &isl_multi_union_pw_aff_factor_range,
5496 "[B[] -> C[{ A[] -> [2] }]]",
5497 "C[{ A[] -> [2] }]" },
5498 { &isl_multi_union_pw_aff_factor_range,
5499 "[B[{ A[] -> [1] }] -> C[]]",
5500 "C[]" },
5501 { &isl_multi_union_pw_aff_factor_range,
5502 "[B[] -> C[]]",
5503 "C[]" },
5504 { &isl_multi_union_pw_aff_factor_range,
5505 "([B[] -> C[]] : { A[x] : x >= 0 })",
5506 "(C[] : { A[x] : x >= 0 })" },
5507 { &isl_multi_union_pw_aff_factor_range,
5508 "[N] -> ([B[] -> C[]] : { A[x] : x <= N })",
5509 "[N] -> (C[] : { A[x] : x <= N })" },
5510 { &isl_multi_union_pw_aff_factor_range,
5511 "[N] -> ([B[] -> C[]] : { : N >= 0 })",
5512 "[N] -> (C[] : { : N >= 0 })" },
5515 /* Perform some basic tests of unary operations on
5516 * isl_multi_union_pw_aff objects.
5518 static int test_un_mupa(isl_ctx *ctx)
5520 int i;
5521 isl_bool ok;
5522 isl_multi_union_pw_aff *mupa, *res;
5524 for (i = 0; i < ARRAY_SIZE(mupa_un_tests); ++i) {
5525 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
5526 mupa_un_tests[i].arg);
5527 res = isl_multi_union_pw_aff_read_from_str(ctx,
5528 mupa_un_tests[i].res);
5529 mupa = mupa_un_tests[i].fn(mupa);
5530 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
5531 isl_multi_union_pw_aff_free(mupa);
5532 isl_multi_union_pw_aff_free(res);
5533 if (ok < 0)
5534 return -1;
5535 if (!ok)
5536 isl_die(ctx, isl_error_unknown,
5537 "unexpected result", return -1);
5540 return 0;
5543 /* Inputs for basic tests of binary operations on
5544 * isl_multi_union_pw_aff objects.
5545 * "fn" is the function that is tested.
5546 * "arg1" and "arg2" are string descriptions of the inputs.
5547 * "res" is a string description of the expected result.
5549 struct {
5550 __isl_give isl_multi_union_pw_aff *(*fn)(
5551 __isl_take isl_multi_union_pw_aff *mupa1,
5552 __isl_take isl_multi_union_pw_aff *mupa2);
5553 const char *arg1;
5554 const char *arg2;
5555 const char *res;
5556 } mupa_bin_tests[] = {
5557 { &isl_multi_union_pw_aff_add, "[{ A[] -> [1] }]", "[{ A[] -> [2] }]",
5558 "[{ A[] -> [3] }]" },
5559 { &isl_multi_union_pw_aff_sub, "[{ A[] -> [1] }]", "[{ A[] -> [2] }]",
5560 "[{ A[] -> [-1] }]" },
5561 { &isl_multi_union_pw_aff_add,
5562 "[{ A[] -> [1]; B[] -> [4] }]",
5563 "[{ A[] -> [2]; C[] -> [5] }]",
5564 "[{ A[] -> [3] }]" },
5565 { &isl_multi_union_pw_aff_union_add,
5566 "[{ A[] -> [1]; B[] -> [4] }]",
5567 "[{ A[] -> [2]; C[] -> [5] }]",
5568 "[{ A[] -> [3]; B[] -> [4]; C[] -> [5] }]" },
5569 { &isl_multi_union_pw_aff_add, "[{ A[x] -> [(1)] : x >= 5 }]",
5570 "[{ A[x] -> [(x)] : x <= 10 }]",
5571 "[{ A[x] -> [(1 + x)] : 5 <= x <= 10 }]" },
5572 { &isl_multi_union_pw_aff_add, "([] : { A[x] : x >= 5 })",
5573 "([] : { A[x] : x <= 10 })",
5574 "([] : { A[x] : 5 <= x <= 10 })" },
5575 { &isl_multi_union_pw_aff_add, "([] : { A[x] : x >= 5 })",
5576 "[N] -> ([] : { A[x] : x <= N })",
5577 "[N] -> ([] : { A[x] : 5 <= x <= N })" },
5578 { &isl_multi_union_pw_aff_add, "[N] -> ([] : { A[x] : x >= N })",
5579 "([] : { A[x] : x <= 10 })",
5580 "[N] -> ([] : { A[x] : N <= x <= 10 })" },
5581 { &isl_multi_union_pw_aff_union_add, "[{ A[x] -> [(1)] : x >= 5 }]",
5582 "[{ A[x] -> [(x)] : x <= 10 }]",
5583 "[{ A[x] -> [(1 + x)] : 5 <= x <= 10; "
5584 "A[x] -> [(1)] : x > 10; A[x] -> [(x)] : x < 5 }]" },
5585 { &isl_multi_union_pw_aff_union_add, "([] : { A[x] : x >= 5 })",
5586 "([] : { A[x] : x <= 10 })",
5587 "([] : { A[x] })" },
5588 { &isl_multi_union_pw_aff_union_add, "([] : { A[x] : x >= 0 })",
5589 "[N] -> ([] : { A[x] : x >= N })",
5590 "[N] -> ([] : { A[x] : x >= 0 or x >= N })" },
5591 { &isl_multi_union_pw_aff_union_add,
5592 "[N] -> ([] : { A[] : N >= 0})",
5593 "[N] -> ([] : { A[] : N <= 0})",
5594 "[N] -> ([] : { A[] })" },
5595 { &isl_multi_union_pw_aff_union_add,
5596 "[N] -> ([] : { A[] })",
5597 "[N] -> ([] : { : })",
5598 "[N] -> ([] : { : })" },
5599 { &isl_multi_union_pw_aff_union_add,
5600 "[N] -> ([] : { : })",
5601 "[N] -> ([] : { A[] })",
5602 "[N] -> ([] : { : })" },
5603 { &isl_multi_union_pw_aff_union_add,
5604 "[N] -> ([] : { : N >= 0})",
5605 "[N] -> ([] : { : N <= 0})",
5606 "[N] -> ([] : { : })" },
5607 { &isl_multi_union_pw_aff_range_product,
5608 "B[{ A[] -> [1] }]",
5609 "C[{ A[] -> [2] }]",
5610 "[B[{ A[] -> [1] }] -> C[{ A[] -> [2] }]]" },
5611 { &isl_multi_union_pw_aff_range_product,
5612 "(B[] : { A[x] : x >= 5 })",
5613 "(C[] : { A[x] : x <= 10 })",
5614 "([B[] -> C[]] : { A[x] : 5 <= x <= 10 })" },
5615 { &isl_multi_union_pw_aff_range_product,
5616 "B[{ A[x] -> [x + 1] : x >= 5 }]",
5617 "(C[] : { A[x] : x <= 10 })",
5618 "[B[{ A[x] -> [x + 1] : 5 <= x <= 10 }] -> C[]]" },
5619 { &isl_multi_union_pw_aff_range_product,
5620 "(C[] : { A[x] : x <= 10 })",
5621 "B[{ A[x] -> [x + 1] : x >= 5 }]",
5622 "[C[] -> B[{ A[x] -> [x + 1] : 5 <= x <= 10 }]]" },
5623 { &isl_multi_union_pw_aff_range_product,
5624 "B[{ A[x] -> [x + 1] : x >= 5 }]",
5625 "[N] -> (C[] : { A[x] : x <= N })",
5626 "[N] -> [B[{ A[x] -> [x + 1] : 5 <= x <= N }] -> C[]]" },
5627 { &isl_multi_union_pw_aff_range_product,
5628 "[N] -> (C[] : { A[x] : x <= N })",
5629 "B[{ A[x] -> [x + 1] : x >= 5 }]",
5630 "[N] -> [C[] -> B[{ A[x] -> [x + 1] : 5 <= x <= N }]]" },
5631 { &isl_multi_union_pw_aff_range_product,
5632 "B[{ A[] -> [1]; D[] -> [3] }]",
5633 "C[{ A[] -> [2] }]",
5634 "[B[{ A[] -> [1]; D[] -> [3] }] -> C[{ A[] -> [2] }]]" },
5635 { &isl_multi_union_pw_aff_range_product,
5636 "B[] }]",
5637 "(C[] : { A[x] })",
5638 "([B[] -> C[]] : { A[x] })" },
5639 { &isl_multi_union_pw_aff_range_product,
5640 "(B[] : { A[x] })",
5641 "C[] }]",
5642 "([B[] -> C[]] : { A[x] })" },
5645 /* Perform some basic tests of binary operations on
5646 * isl_multi_union_pw_aff objects.
5648 static int test_bin_mupa(isl_ctx *ctx)
5650 int i;
5651 isl_bool ok;
5652 isl_multi_union_pw_aff *mupa1, *mupa2, *res;
5654 for (i = 0; i < ARRAY_SIZE(mupa_bin_tests); ++i) {
5655 mupa1 = isl_multi_union_pw_aff_read_from_str(ctx,
5656 mupa_bin_tests[i].arg1);
5657 mupa2 = isl_multi_union_pw_aff_read_from_str(ctx,
5658 mupa_bin_tests[i].arg2);
5659 res = isl_multi_union_pw_aff_read_from_str(ctx,
5660 mupa_bin_tests[i].res);
5661 mupa1 = mupa_bin_tests[i].fn(mupa1, mupa2);
5662 ok = isl_multi_union_pw_aff_plain_is_equal(mupa1, res);
5663 isl_multi_union_pw_aff_free(mupa1);
5664 isl_multi_union_pw_aff_free(res);
5665 if (ok < 0)
5666 return -1;
5667 if (!ok)
5668 isl_die(ctx, isl_error_unknown,
5669 "unexpected result", return -1);
5672 return 0;
5675 /* Inputs for basic tests of binary operations on
5676 * pairs of isl_multi_union_pw_aff and isl_set objects.
5677 * "fn" is the function that is tested.
5678 * "arg1" and "arg2" are string descriptions of the inputs.
5679 * "res" is a string description of the expected result.
5681 struct {
5682 __isl_give isl_multi_union_pw_aff *(*fn)(
5683 __isl_take isl_multi_union_pw_aff *mupa,
5684 __isl_take isl_set *set);
5685 const char *arg1;
5686 const char *arg2;
5687 const char *res;
5688 } mupa_set_tests[] = {
5689 { &isl_multi_union_pw_aff_intersect_range,
5690 "C[{ B[i,j] -> [i + 2j] }]", "{ C[1] }",
5691 "C[{ B[i,j] -> [i + 2j] : i + 2j = 1 }]" },
5692 { &isl_multi_union_pw_aff_intersect_range,
5693 "C[{ B[i,j] -> [i + 2j] }]", "[N] -> { C[N] }",
5694 "[N] -> C[{ B[i,j] -> [i + 2j] : i + 2j = N }]" },
5695 { &isl_multi_union_pw_aff_intersect_range,
5696 "[N] -> C[{ B[i,j] -> [i + 2j + N] }]", "{ C[1] }",
5697 "[N] -> C[{ B[i,j] -> [i + 2j + N] : i + 2j + N = 1 }]" },
5698 { &isl_multi_union_pw_aff_intersect_range,
5699 "C[{ B[i,j] -> [i + 2j] }]", "[N] -> { C[x] : N >= 0 }",
5700 "[N] -> C[{ B[i,j] -> [i + 2j] : N >= 0 }]" },
5701 { &isl_multi_union_pw_aff_intersect_range,
5702 "C[]", "{ C[] }", "C[]" },
5703 { &isl_multi_union_pw_aff_intersect_range,
5704 "[N] -> (C[] : { : N >= 0 })",
5705 "{ C[] }",
5706 "[N] -> (C[] : { : N >= 0 })" },
5707 { &isl_multi_union_pw_aff_intersect_range,
5708 "(C[] : { A[a,b] })",
5709 "{ C[] }",
5710 "(C[] : { A[a,b] })" },
5711 { &isl_multi_union_pw_aff_intersect_range,
5712 "[N] -> (C[] : { A[a,b] : a,b <= N })",
5713 "{ C[] }",
5714 "[N] -> (C[] : { A[a,b] : a,b <= N })" },
5715 { &isl_multi_union_pw_aff_intersect_range,
5716 "C[]",
5717 "[N] -> { C[] : N >= 0 }",
5718 "[N] -> (C[] : { : N >= 0 })" },
5719 { &isl_multi_union_pw_aff_intersect_range,
5720 "(C[] : { A[a,b] })",
5721 "[N] -> { C[] : N >= 0 }",
5722 "[N] -> (C[] : { A[a,b] : N >= 0 })" },
5723 { &isl_multi_union_pw_aff_intersect_range,
5724 "[N] -> (C[] : { : N >= 0 })",
5725 "[N] -> { C[] : N < 1024 }",
5726 "[N] -> (C[] : { : 0 <= N < 1024 })" },
5727 { &isl_multi_union_pw_aff_intersect_params,
5728 "C[{ B[i,j] -> [i + 2j] }]", "[N] -> { : N >= 0 }",
5729 "[N] -> C[{ B[i,j] -> [i + 2j] : N >= 0}]" },
5730 { &isl_multi_union_pw_aff_intersect_params,
5731 "[N] -> C[{ B[i,j] -> [i + 2j] : N <= 256 }]", "[N] -> { : N >= 0 }",
5732 "[N] -> C[{ B[i,j] -> [i + 2j] : 0 <= N <= 256 }]" },
5733 { &isl_multi_union_pw_aff_intersect_params,
5734 "[N] -> C[{ B[i,j] -> [i + 2j] : N <= 256 }]", "{ : }",
5735 "[N] -> C[{ B[i,j] -> [i + 2j] : N <= 256 }]" },
5736 { &isl_multi_union_pw_aff_intersect_params,
5737 "C[]", "[N] -> { : N >= 0 }",
5738 "[N] -> (C[] : { : N >= 0 })" },
5739 { &isl_multi_union_pw_aff_intersect_params,
5740 "(C[] : { A[a,b] })", "[N] -> { : N >= 0 }",
5741 "[N] -> (C[] : { A[a,b] : N >= 0 })" },
5742 { &isl_multi_union_pw_aff_intersect_params,
5743 "[N] -> (C[] : { A[a,N] })", "{ : }",
5744 "[N] -> (C[] : { A[a,N] })" },
5745 { &isl_multi_union_pw_aff_intersect_params,
5746 "[N] -> (C[] : { A[a,b] : N <= 256 })", "[N] -> { : N >= 0 }",
5747 "[N] -> (C[] : { A[a,b] : 0 <= N <= 256 })" },
5750 /* Perform some basic tests of binary operations on
5751 * pairs of isl_multi_union_pw_aff and isl_set objects.
5753 static int test_mupa_set(isl_ctx *ctx)
5755 int i;
5756 isl_bool ok;
5757 isl_multi_union_pw_aff *mupa, *res;
5758 isl_set *set;
5760 for (i = 0; i < ARRAY_SIZE(mupa_set_tests); ++i) {
5761 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
5762 mupa_set_tests[i].arg1);
5763 set = isl_set_read_from_str(ctx, mupa_set_tests[i].arg2);
5764 res = isl_multi_union_pw_aff_read_from_str(ctx,
5765 mupa_set_tests[i].res);
5766 mupa = mupa_set_tests[i].fn(mupa, set);
5767 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
5768 isl_multi_union_pw_aff_free(mupa);
5769 isl_multi_union_pw_aff_free(res);
5770 if (ok < 0)
5771 return -1;
5772 if (!ok)
5773 isl_die(ctx, isl_error_unknown,
5774 "unexpected result", return -1);
5777 return 0;
5780 /* Inputs for basic tests of binary operations on
5781 * pairs of isl_multi_union_pw_aff and isl_union_set objects.
5782 * "fn" is the function that is tested.
5783 * "arg1" and "arg2" are string descriptions of the inputs.
5784 * "res" is a string description of the expected result.
5786 struct {
5787 __isl_give isl_multi_union_pw_aff *(*fn)(
5788 __isl_take isl_multi_union_pw_aff *mupa,
5789 __isl_take isl_union_set *uset);
5790 const char *arg1;
5791 const char *arg2;
5792 const char *res;
5793 } mupa_uset_tests[] = {
5794 { &isl_multi_union_pw_aff_intersect_domain,
5795 "C[{ B[i,j] -> [i + 2j] }]", "{ B[i,i] }",
5796 "C[{ B[i,i] -> [3i] }]" },
5797 { &isl_multi_union_pw_aff_intersect_domain,
5798 "(C[] : { B[i,j] })", "{ B[i,i] }",
5799 "(C[] : { B[i,i] })" },
5800 { &isl_multi_union_pw_aff_intersect_domain,
5801 "(C[] : { B[i,j] })", "[N] -> { B[N,N] }",
5802 "[N] -> (C[] : { B[N,N] })" },
5803 { &isl_multi_union_pw_aff_intersect_domain,
5804 "C[]", "{ B[i,i] }",
5805 "(C[] : { B[i,i] })" },
5806 { &isl_multi_union_pw_aff_intersect_domain,
5807 "[N] -> (C[] : { : N >= 0 })", "{ B[i,i] }",
5808 "[N] -> (C[] : { B[i,i] : N >= 0 })" },
5811 /* Perform some basic tests of binary operations on
5812 * pairs of isl_multi_union_pw_aff and isl_union_set objects.
5814 static int test_mupa_uset(isl_ctx *ctx)
5816 int i;
5817 isl_bool ok;
5818 isl_multi_union_pw_aff *mupa, *res;
5819 isl_union_set *uset;
5821 for (i = 0; i < ARRAY_SIZE(mupa_uset_tests); ++i) {
5822 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
5823 mupa_uset_tests[i].arg1);
5824 uset = isl_union_set_read_from_str(ctx,
5825 mupa_uset_tests[i].arg2);
5826 res = isl_multi_union_pw_aff_read_from_str(ctx,
5827 mupa_uset_tests[i].res);
5828 mupa = mupa_uset_tests[i].fn(mupa, uset);
5829 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
5830 isl_multi_union_pw_aff_free(mupa);
5831 isl_multi_union_pw_aff_free(res);
5832 if (ok < 0)
5833 return -1;
5834 if (!ok)
5835 isl_die(ctx, isl_error_unknown,
5836 "unexpected result", return -1);
5839 return 0;
5842 /* Inputs for basic tests of binary operations on
5843 * pairs of isl_multi_union_pw_aff and isl_multi_aff objects.
5844 * "fn" is the function that is tested.
5845 * "arg1" and "arg2" are string descriptions of the inputs.
5846 * "res" is a string description of the expected result.
5848 struct {
5849 __isl_give isl_multi_union_pw_aff *(*fn)(
5850 __isl_take isl_multi_union_pw_aff *mupa,
5851 __isl_take isl_multi_aff *ma);
5852 const char *arg1;
5853 const char *arg2;
5854 const char *res;
5855 } mupa_ma_tests[] = {
5856 { &isl_multi_union_pw_aff_apply_multi_aff,
5857 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }, "
5858 "{ A[i,j] -> [j]; B[i,j] -> [i] }]",
5859 "{ C[a,b] -> D[b,a] }",
5860 "D[{ A[i,j] -> [j]; B[i,j] -> [i] }, "
5861 "{ A[i,j] -> [i]; B[i,j] -> [j] }]" },
5862 { &isl_multi_union_pw_aff_apply_multi_aff,
5863 "C[{ A[i,j] -> [i] : i >= 0; B[i,j] -> [j] }, "
5864 "{ A[i,j] -> [j]; B[i,j] -> [i] }]",
5865 "{ C[a,b] -> D[b,a] }",
5866 "D[{ A[i,j] -> [j] : i >= 0; B[i,j] -> [i] }, "
5867 "{ A[i,j] -> [i] : i >= 0; B[i,j] -> [j] }]" },
5868 { &isl_multi_union_pw_aff_apply_multi_aff,
5869 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }]",
5870 "[N] -> { C[a] -> D[a + N] }",
5871 "[N] -> D[{ A[i,j] -> [i + N]; B[i,j] -> [j + N] }] " },
5872 { &isl_multi_union_pw_aff_apply_multi_aff,
5873 "C[]",
5874 "{ C[] -> D[] }",
5875 "D[]" },
5876 { &isl_multi_union_pw_aff_apply_multi_aff,
5877 "[N] -> (C[] : { : N >= 0 })",
5878 "{ C[] -> D[] }",
5879 "[N] -> (D[] : { : N >= 0 })" },
5880 { &isl_multi_union_pw_aff_apply_multi_aff,
5881 "C[]",
5882 "[N] -> { C[] -> D[N] }",
5883 "[N] -> D[{ [N] }]" },
5884 { &isl_multi_union_pw_aff_apply_multi_aff,
5885 "(C[] : { A[i,j] : i >= j })",
5886 "{ C[] -> D[] }",
5887 "(D[] : { A[i,j] : i >= j })" },
5888 { &isl_multi_union_pw_aff_apply_multi_aff,
5889 "[N] -> (C[] : { A[i,j] : N >= 0 })",
5890 "{ C[] -> D[] }",
5891 "[N] -> (D[] : { A[i,j] : N >= 0 })" },
5892 { &isl_multi_union_pw_aff_apply_multi_aff,
5893 "(C[] : { A[i,j] : i >= j })",
5894 "[N] -> { C[] -> D[N] }",
5895 "[N] -> (D[{ A[i,j] -> [N] : i >= j }])" },
5898 /* Perform some basic tests of binary operations on
5899 * pairs of isl_multi_union_pw_aff and isl_multi_aff objects.
5901 static int test_mupa_ma(isl_ctx *ctx)
5903 int i;
5904 isl_bool ok;
5905 isl_multi_union_pw_aff *mupa, *res;
5906 isl_multi_aff *ma;
5908 for (i = 0; i < ARRAY_SIZE(mupa_ma_tests); ++i) {
5909 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
5910 mupa_ma_tests[i].arg1);
5911 ma = isl_multi_aff_read_from_str(ctx, mupa_ma_tests[i].arg2);
5912 res = isl_multi_union_pw_aff_read_from_str(ctx,
5913 mupa_ma_tests[i].res);
5914 mupa = mupa_ma_tests[i].fn(mupa, ma);
5915 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
5916 isl_multi_union_pw_aff_free(mupa);
5917 isl_multi_union_pw_aff_free(res);
5918 if (ok < 0)
5919 return -1;
5920 if (!ok)
5921 isl_die(ctx, isl_error_unknown,
5922 "unexpected result", return -1);
5925 return 0;
5928 /* Inputs for basic tests of binary operations on
5929 * pairs of isl_multi_union_pw_aff and isl_pw_aff objects.
5930 * "fn" is the function that is tested.
5931 * "arg1" and "arg2" are string descriptions of the inputs.
5932 * "res" is a string description of the expected result.
5934 struct {
5935 __isl_give isl_union_pw_aff *(*fn)(
5936 __isl_take isl_multi_union_pw_aff *mupa,
5937 __isl_take isl_pw_aff *pa);
5938 const char *arg1;
5939 const char *arg2;
5940 const char *res;
5941 } mupa_pa_tests[] = {
5942 { &isl_multi_union_pw_aff_apply_pw_aff,
5943 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }]",
5944 "[N] -> { C[a] -> [a + N] }",
5945 "[N] -> { A[i,j] -> [i + N]; B[i,j] -> [j + N] }" },
5946 { &isl_multi_union_pw_aff_apply_pw_aff,
5947 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }]",
5948 "{ C[a] -> [a] : a >= 0; C[a] -> [-a] : a < 0 }",
5949 "{ A[i,j] -> [i] : i >= 0; A[i,j] -> [-i] : i < 0; "
5950 "B[i,j] -> [j] : j >= 0; B[i,j] -> [-j] : j < 0 }" },
5951 { &isl_multi_union_pw_aff_apply_pw_aff,
5952 "C[]",
5953 "[N] -> { C[] -> [N] }",
5954 "[N] -> { [N] }" },
5955 { &isl_multi_union_pw_aff_apply_pw_aff,
5956 "C[]",
5957 "[N] -> { C[] -> [N] : N >= 0; C[] -> [-N] : N < 0 }",
5958 "[N] -> { [N] : N >= 0; [-N] : N < 0 }" },
5959 { &isl_multi_union_pw_aff_apply_pw_aff,
5960 "[N] -> (C[] : { : N >= 0 })",
5961 "[N] -> { C[] -> [N] }",
5962 "[N] -> { [N] : N >= 0 }" },
5963 { &isl_multi_union_pw_aff_apply_pw_aff,
5964 "[N] -> (C[] : { : N >= 0 })",
5965 "[N] -> { C[] -> [N] : N >= 0; C[] -> [-N] : N < 0 }",
5966 "[N] -> { [N] : N >= 0 }" },
5967 { &isl_multi_union_pw_aff_apply_pw_aff,
5968 "[N] -> (C[] : { : N >= 0 })",
5969 "{ C[] -> [0] }",
5970 "[N] -> { [0] : N >= 0 }" },
5971 { &isl_multi_union_pw_aff_apply_pw_aff,
5972 "(C[] : { A[i,j] : i >= j })",
5973 "[N] -> { C[] -> [N] }",
5974 "[N] -> { A[i,j] -> [N] : i >= j }" },
5975 { &isl_multi_union_pw_aff_apply_pw_aff,
5976 "(C[] : { A[i,j] : i >= j })",
5977 "[N] -> { C[] -> [N] : N >= 0 }",
5978 "[N] -> { A[i,j] -> [N] : i >= j and N >= 0 }" },
5981 /* Perform some basic tests of binary operations on
5982 * pairs of isl_multi_union_pw_aff and isl_pw_aff objects.
5984 static int test_mupa_pa(isl_ctx *ctx)
5986 int i;
5987 isl_bool ok;
5988 isl_multi_union_pw_aff *mupa;
5989 isl_union_pw_aff *upa, *res;
5990 isl_pw_aff *pa;
5992 for (i = 0; i < ARRAY_SIZE(mupa_pa_tests); ++i) {
5993 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
5994 mupa_pa_tests[i].arg1);
5995 pa = isl_pw_aff_read_from_str(ctx, mupa_pa_tests[i].arg2);
5996 res = isl_union_pw_aff_read_from_str(ctx,
5997 mupa_pa_tests[i].res);
5998 upa = mupa_pa_tests[i].fn(mupa, pa);
5999 ok = isl_union_pw_aff_plain_is_equal(upa, res);
6000 isl_union_pw_aff_free(upa);
6001 isl_union_pw_aff_free(res);
6002 if (ok < 0)
6003 return -1;
6004 if (!ok)
6005 isl_die(ctx, isl_error_unknown,
6006 "unexpected result", return -1);
6009 return 0;
6012 /* Inputs for basic tests of binary operations on
6013 * pairs of isl_multi_union_pw_aff and isl_pw_multi_aff objects.
6014 * "fn" is the function that is tested.
6015 * "arg1" and "arg2" are string descriptions of the inputs.
6016 * "res" is a string description of the expected result.
6018 struct {
6019 __isl_give isl_multi_union_pw_aff *(*fn)(
6020 __isl_take isl_multi_union_pw_aff *mupa,
6021 __isl_take isl_pw_multi_aff *pma);
6022 const char *arg1;
6023 const char *arg2;
6024 const char *res;
6025 } mupa_pma_tests[] = {
6026 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6027 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }, "
6028 "{ A[i,j] -> [j]; B[i,j] -> [i] }]",
6029 "{ C[a,b] -> D[b,a] }",
6030 "D[{ A[i,j] -> [j]; B[i,j] -> [i] }, "
6031 "{ A[i,j] -> [i]; B[i,j] -> [j] }]" },
6032 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6033 "C[{ A[i,j] -> [i] : i >= 0; B[i,j] -> [j] }, "
6034 "{ A[i,j] -> [j]; B[i,j] -> [i] }]",
6035 "{ C[a,b] -> D[b,a] }",
6036 "D[{ A[i,j] -> [j] : i >= 0; B[i,j] -> [i] }, "
6037 "{ A[i,j] -> [i] : i >= 0; B[i,j] -> [j] }]" },
6038 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6039 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }]",
6040 "[N] -> { C[a] -> D[a + N] }",
6041 "[N] -> D[{ A[i,j] -> [i + N]; B[i,j] -> [j + N] }]" },
6042 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6043 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }]",
6044 "{ C[a] -> D[a] : a >= 0; C[a] -> D[-a] : a < 0 }",
6045 "D[{ A[i,j] -> [i] : i >= 0; A[i,j] -> [-i] : i < 0; "
6046 "B[i,j] -> [j] : j >= 0; B[i,j] -> [-j] : j < 0 }]" },
6047 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6048 "C[{ A[i,j] -> [i]; B[i,j] -> [j] }, "
6049 "{ A[i,j] -> [j]; B[i,j] -> [i] }]",
6050 "{ C[a,b] -> D[a,b] : a >= b; C[a,b] -> D[b,a] : a < b }",
6051 "D[{ A[i,j] -> [i] : i >= j; A[i,j] -> [j] : i < j; "
6052 "B[i,j] -> [j] : i <= j; B[i,j] -> [i] : i > j }, "
6053 "{ A[i,j] -> [j] : i >= j; A[i,j] -> [i] : i < j; "
6054 "B[i,j] -> [i] : i <= j; B[i,j] -> [j] : i > j }]" },
6055 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6056 "C[]",
6057 "{ C[] -> D[] }",
6058 "D[]" },
6059 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6060 "[N] -> (C[] : { : N >= 0 })",
6061 "{ C[] -> D[] }",
6062 "[N] -> (D[] : { : N >= 0 })" },
6063 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6064 "C[]",
6065 "[N] -> { C[] -> D[N] }",
6066 "[N] -> D[{ [N] }]" },
6067 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6068 "(C[] : { A[i,j] : i >= j })",
6069 "{ C[] -> D[] }",
6070 "(D[] : { A[i,j] : i >= j })" },
6071 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6072 "[N] -> (C[] : { A[i,j] : N >= 0 })",
6073 "{ C[] -> D[] }",
6074 "[N] -> (D[] : { A[i,j] : N >= 0 })" },
6075 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6076 "(C[] : { A[i,j] : i >= j })",
6077 "[N] -> { C[] -> D[N] }",
6078 "[N] -> (D[{ A[i,j] -> [N] : i >= j }])" },
6079 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6080 "C[]",
6081 "[N] -> { C[] -> D[N] : N >= 0; C[] -> D[-N] : N < 0 }",
6082 "[N] -> D[{ [N] : N >= 0; [-N] : N < 0 }]" },
6083 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6084 "[N] -> (C[] : { : N >= 0 })",
6085 "[N] -> { C[] -> D[N] }",
6086 "[N] -> D[{ [N] : N >= 0 }]" },
6087 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6088 "[N] -> (C[] : { : N >= 0 })",
6089 "[N] -> { C[] -> D[N] : N >= 0; C[] -> D[-N] : N < 0 }",
6090 "[N] -> D[{ [N] : N >= 0 }]" },
6091 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6092 "[N] -> (C[] : { : N >= 0 })",
6093 "{ C[] -> D[0] }",
6094 "[N] -> D[{ [0] : N >= 0 }]" },
6095 { &isl_multi_union_pw_aff_apply_pw_multi_aff,
6096 "(C[] : { A[i,j] : i >= j })",
6097 "[N] -> { C[] -> D[N] : N >= 0 }",
6098 "[N] -> D[{ A[i,j] -> [N] : i >= j and N >= 0 }]" },
6101 /* Perform some basic tests of binary operations on
6102 * pairs of isl_multi_union_pw_aff and isl_pw_multi_aff objects.
6104 static int test_mupa_pma(isl_ctx *ctx)
6106 int i;
6107 isl_bool ok;
6108 isl_multi_union_pw_aff *mupa, *res;
6109 isl_pw_multi_aff *pma;
6111 for (i = 0; i < ARRAY_SIZE(mupa_pma_tests); ++i) {
6112 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
6113 mupa_pma_tests[i].arg1);
6114 pma = isl_pw_multi_aff_read_from_str(ctx,
6115 mupa_pma_tests[i].arg2);
6116 res = isl_multi_union_pw_aff_read_from_str(ctx,
6117 mupa_pma_tests[i].res);
6118 mupa = mupa_pma_tests[i].fn(mupa, pma);
6119 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
6120 isl_multi_union_pw_aff_free(mupa);
6121 isl_multi_union_pw_aff_free(res);
6122 if (ok < 0)
6123 return -1;
6124 if (!ok)
6125 isl_die(ctx, isl_error_unknown,
6126 "unexpected result", return -1);
6129 return 0;
6132 /* Inputs for basic tests of binary operations on
6133 * pairs of isl_multi_union_pw_aff and isl_union_pw_multi_aff objects.
6134 * "fn" is the function that is tested.
6135 * "arg1" and "arg2" are string descriptions of the inputs.
6136 * "res" is a string description of the expected result.
6138 struct {
6139 __isl_give isl_multi_union_pw_aff *(*fn)(
6140 __isl_take isl_multi_union_pw_aff *mupa,
6141 __isl_take isl_union_pw_multi_aff *upma);
6142 const char *arg1;
6143 const char *arg2;
6144 const char *res;
6145 } mupa_upma_tests[] = {
6146 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6147 "C[{ B[i,j] -> [i + 2j] }]", "{ A[a,b] -> B[b,a] }",
6148 "C[{ A[a,b] -> [b + 2a] }]" },
6149 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6150 "C[{ B[i,j] -> [i + 2j] }]",
6151 "{ A[a,b] -> B[b,a] : b > a }",
6152 "C[{ A[a,b] -> [b + 2a] : b > a }]" },
6153 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6154 "C[{ B[i,j] -> [i + 2j] : j > 4 }]",
6155 "{ A[a,b] -> B[b,a] : b > a }",
6156 "C[{ A[a,b] -> [b + 2a] : b > a > 4 }]" },
6157 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6158 "C[{ B[i,j] -> [i + 2j] }]",
6159 "{ A[a,b] -> B[b,a] : a > b; A[a,b] -> B[a,b] : a <= b }",
6160 "C[{ A[a,b] -> [b + 2a] : a > b; A[a,b] -> [a + 2b] : a <= b }]" },
6161 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6162 "(C[] : { B[a,b] })",
6163 "{ A[a,b] -> B[b,a] }",
6164 "(C[] : { A[a,b] })" },
6165 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6166 "(C[] : { B[a,b] })",
6167 "{ B[a,b] -> A[b,a] }",
6168 "(C[] : { })" },
6169 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6170 "(C[] : { B[a,b] })",
6171 "{ A[a,b] -> B[b,a] : a > b }",
6172 "(C[] : { A[a,b] : a > b })" },
6173 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6174 "(C[] : { B[a,b] : a > b })",
6175 "{ A[a,b] -> B[b,a] }",
6176 "(C[] : { A[a,b] : b > a })" },
6177 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6178 "[N] -> (C[] : { B[a,b] : a > N })",
6179 "{ A[a,b] -> B[b,a] : a > b }",
6180 "[N] -> (C[] : { A[a,b] : a > b > N })" },
6181 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6182 "(C[] : { B[a,b] : a > b })",
6183 "[N] -> { A[a,b] -> B[b,a] : a > N }",
6184 "[N] -> (C[] : { A[a,b] : b > a > N })" },
6185 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6186 "C[]",
6187 "{ A[a,b] -> B[b,a] }",
6188 "C[]" },
6189 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6190 "[N] -> (C[] : { : N >= 0 })",
6191 "{ A[a,b] -> B[b,a] }",
6192 "[N] -> (C[] : { : N >= 0 })" },
6193 { &isl_multi_union_pw_aff_pullback_union_pw_multi_aff,
6194 "C[]",
6195 "[N] -> { A[a,b] -> B[b,a] : N >= 0 }",
6196 "[N] -> (C[] : { : N >= 0 })" },
6199 /* Perform some basic tests of binary operations on
6200 * pairs of isl_multi_union_pw_aff and isl_union_pw_multi_aff objects.
6202 static int test_mupa_upma(isl_ctx *ctx)
6204 int i;
6205 isl_bool ok;
6206 isl_multi_union_pw_aff *mupa, *res;
6207 isl_union_pw_multi_aff *upma;
6209 for (i = 0; i < ARRAY_SIZE(mupa_upma_tests); ++i) {
6210 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
6211 mupa_upma_tests[i].arg1);
6212 upma = isl_union_pw_multi_aff_read_from_str(ctx,
6213 mupa_upma_tests[i].arg2);
6214 res = isl_multi_union_pw_aff_read_from_str(ctx,
6215 mupa_upma_tests[i].res);
6216 mupa = mupa_upma_tests[i].fn(mupa, upma);
6217 ok = isl_multi_union_pw_aff_plain_is_equal(mupa, res);
6218 isl_multi_union_pw_aff_free(mupa);
6219 isl_multi_union_pw_aff_free(res);
6220 if (ok < 0)
6221 return -1;
6222 if (!ok)
6223 isl_die(ctx, isl_error_unknown,
6224 "unexpected result", return -1);
6227 return 0;
6230 /* Check that the input tuple of an isl_aff can be set properly.
6232 static isl_stat test_aff_set_tuple_id(isl_ctx *ctx)
6234 isl_id *id;
6235 isl_aff *aff;
6236 int equal;
6238 aff = isl_aff_read_from_str(ctx, "{ [x] -> [x + 1] }");
6239 id = isl_id_alloc(ctx, "A", NULL);
6240 aff = isl_aff_set_tuple_id(aff, isl_dim_in, id);
6241 equal = aff_check_plain_equal(aff, "{ A[x] -> [x + 1] }");
6242 isl_aff_free(aff);
6243 if (equal < 0)
6244 return isl_stat_error;
6246 return isl_stat_ok;
6249 int test_aff(isl_ctx *ctx)
6251 const char *str;
6252 isl_set *set;
6253 isl_space *space;
6254 isl_local_space *ls;
6255 isl_aff *aff;
6256 int zero, equal;
6258 if (test_upa(ctx) < 0)
6259 return -1;
6260 if (test_bin_aff(ctx) < 0)
6261 return -1;
6262 if (test_bin_pw_aff(ctx) < 0)
6263 return -1;
6264 if (test_bin_upma(ctx) < 0)
6265 return -1;
6266 if (test_bin_upma_fail(ctx) < 0)
6267 return -1;
6268 if (test_un_mpa(ctx) < 0)
6269 return -1;
6270 if (test_bin_mpa(ctx) < 0)
6271 return -1;
6272 if (test_un_mupa(ctx) < 0)
6273 return -1;
6274 if (test_bin_mupa(ctx) < 0)
6275 return -1;
6276 if (test_mupa_set(ctx) < 0)
6277 return -1;
6278 if (test_mupa_uset(ctx) < 0)
6279 return -1;
6280 if (test_mupa_ma(ctx) < 0)
6281 return -1;
6282 if (test_mupa_pa(ctx) < 0)
6283 return -1;
6284 if (test_mupa_pma(ctx) < 0)
6285 return -1;
6286 if (test_mupa_upma(ctx) < 0)
6287 return -1;
6289 space = isl_space_set_alloc(ctx, 0, 1);
6290 ls = isl_local_space_from_space(space);
6291 aff = isl_aff_zero_on_domain(ls);
6293 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
6294 aff = isl_aff_scale_down_ui(aff, 3);
6295 aff = isl_aff_floor(aff);
6296 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
6297 aff = isl_aff_scale_down_ui(aff, 2);
6298 aff = isl_aff_floor(aff);
6299 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
6301 str = "{ [10] }";
6302 set = isl_set_read_from_str(ctx, str);
6303 aff = isl_aff_gist(aff, set);
6305 aff = isl_aff_add_constant_si(aff, -16);
6306 zero = isl_aff_plain_is_zero(aff);
6307 isl_aff_free(aff);
6309 if (zero < 0)
6310 return -1;
6311 if (!zero)
6312 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6314 aff = isl_aff_read_from_str(ctx, "{ [-1] }");
6315 aff = isl_aff_scale_down_ui(aff, 64);
6316 aff = isl_aff_floor(aff);
6317 equal = aff_check_plain_equal(aff, "{ [-1] }");
6318 isl_aff_free(aff);
6319 if (equal < 0)
6320 return -1;
6322 if (test_aff_set_tuple_id(ctx) < 0)
6323 return -1;
6325 return 0;
6328 /* Check that "pa" consists of a single expression.
6330 static int check_single_piece(isl_ctx *ctx, __isl_take isl_pw_aff *pa)
6332 isl_size n;
6334 n = isl_pw_aff_n_piece(pa);
6335 isl_pw_aff_free(pa);
6337 if (n < 0)
6338 return -1;
6339 if (n != 1)
6340 isl_die(ctx, isl_error_unknown, "expecting single expression",
6341 return -1);
6343 return 0;
6346 /* Check that the computation below results in a single expression.
6347 * One or two expressions may result depending on which constraint
6348 * ends up being considered as redundant with respect to the other
6349 * constraints after the projection that is performed internally
6350 * by isl_set_dim_min.
6352 static int test_dim_max_1(isl_ctx *ctx)
6354 const char *str;
6355 isl_set *set;
6356 isl_pw_aff *pa;
6358 str = "[n] -> { [a, b] : n >= 0 and 4a >= -4 + n and b >= 0 and "
6359 "-4a <= b <= 3 and b < n - 4a }";
6360 set = isl_set_read_from_str(ctx, str);
6361 pa = isl_set_dim_min(set, 0);
6362 return check_single_piece(ctx, pa);
6365 /* Check that the computation below results in a single expression.
6366 * The PIP problem corresponding to these constraints has a row
6367 * that causes a split of the solution domain. The solver should
6368 * first pick rows that split off empty parts such that the actual
6369 * solution domain does not get split.
6370 * Note that the description contains some redundant constraints.
6371 * If these constraints get removed first, then the row mentioned
6372 * above does not appear in the PIP problem.
6374 static int test_dim_max_2(isl_ctx *ctx)
6376 const char *str;
6377 isl_set *set;
6378 isl_pw_aff *pa;
6380 str = "[P, N] -> { [a] : a < N and a >= 0 and N > P and a <= P and "
6381 "N > 0 and P >= 0 }";
6382 set = isl_set_read_from_str(ctx, str);
6383 pa = isl_set_dim_max(set, 0);
6384 return check_single_piece(ctx, pa);
6387 int test_dim_max(isl_ctx *ctx)
6389 int equal;
6390 const char *str;
6391 isl_set *set1, *set2;
6392 isl_set *set;
6393 isl_map *map;
6394 isl_pw_aff *pwaff;
6396 if (test_dim_max_1(ctx) < 0)
6397 return -1;
6398 if (test_dim_max_2(ctx) < 0)
6399 return -1;
6401 str = "[N] -> { [i] : 0 <= i <= min(N,10) }";
6402 set = isl_set_read_from_str(ctx, str);
6403 pwaff = isl_set_dim_max(set, 0);
6404 set1 = isl_set_from_pw_aff(pwaff);
6405 str = "[N] -> { [10] : N >= 10; [N] : N <= 9 and N >= 0 }";
6406 set2 = isl_set_read_from_str(ctx, str);
6407 equal = isl_set_is_equal(set1, set2);
6408 isl_set_free(set1);
6409 isl_set_free(set2);
6410 if (equal < 0)
6411 return -1;
6412 if (!equal)
6413 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6415 str = "[N] -> { [i] : 0 <= i <= max(2N,N+6) }";
6416 set = isl_set_read_from_str(ctx, str);
6417 pwaff = isl_set_dim_max(set, 0);
6418 set1 = isl_set_from_pw_aff(pwaff);
6419 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
6420 set2 = isl_set_read_from_str(ctx, str);
6421 equal = isl_set_is_equal(set1, set2);
6422 isl_set_free(set1);
6423 isl_set_free(set2);
6424 if (equal < 0)
6425 return -1;
6426 if (!equal)
6427 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6429 str = "[N] -> { [i] : 0 <= i <= 2N or 0 <= i <= N+6 }";
6430 set = isl_set_read_from_str(ctx, str);
6431 pwaff = isl_set_dim_max(set, 0);
6432 set1 = isl_set_from_pw_aff(pwaff);
6433 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
6434 set2 = isl_set_read_from_str(ctx, str);
6435 equal = isl_set_is_equal(set1, set2);
6436 isl_set_free(set1);
6437 isl_set_free(set2);
6438 if (equal < 0)
6439 return -1;
6440 if (!equal)
6441 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6443 str = "[N,M] -> { [i,j] -> [([i/16]), i%16, ([j/16]), j%16] : "
6444 "0 <= i < N and 0 <= j < M }";
6445 map = isl_map_read_from_str(ctx, str);
6446 set = isl_map_range(map);
6448 pwaff = isl_set_dim_max(isl_set_copy(set), 0);
6449 set1 = isl_set_from_pw_aff(pwaff);
6450 str = "[N,M] -> { [([(N-1)/16])] : M,N > 0 }";
6451 set2 = isl_set_read_from_str(ctx, str);
6452 equal = isl_set_is_equal(set1, set2);
6453 isl_set_free(set1);
6454 isl_set_free(set2);
6456 pwaff = isl_set_dim_max(isl_set_copy(set), 3);
6457 set1 = isl_set_from_pw_aff(pwaff);
6458 str = "[N,M] -> { [t] : t = min(M-1,15) and M,N > 0 }";
6459 set2 = isl_set_read_from_str(ctx, str);
6460 if (equal >= 0 && equal)
6461 equal = isl_set_is_equal(set1, set2);
6462 isl_set_free(set1);
6463 isl_set_free(set2);
6465 isl_set_free(set);
6467 if (equal < 0)
6468 return -1;
6469 if (!equal)
6470 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6472 /* Check that solutions are properly merged. */
6473 str = "[n] -> { [a, b, c] : c >= -4a - 2b and "
6474 "c <= -1 + n - 4a - 2b and c >= -2b and "
6475 "4a >= -4 + n and c >= 0 }";
6476 set = isl_set_read_from_str(ctx, str);
6477 pwaff = isl_set_dim_min(set, 2);
6478 set1 = isl_set_from_pw_aff(pwaff);
6479 str = "[n] -> { [(0)] : n >= 1 }";
6480 set2 = isl_set_read_from_str(ctx, str);
6481 equal = isl_set_is_equal(set1, set2);
6482 isl_set_free(set1);
6483 isl_set_free(set2);
6485 if (equal < 0)
6486 return -1;
6487 if (!equal)
6488 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6490 /* Check that empty solution lie in the right space. */
6491 str = "[n] -> { [t,a] : 1 = 0 }";
6492 set = isl_set_read_from_str(ctx, str);
6493 pwaff = isl_set_dim_max(set, 0);
6494 set1 = isl_set_from_pw_aff(pwaff);
6495 str = "[n] -> { [t] : 1 = 0 }";
6496 set2 = isl_set_read_from_str(ctx, str);
6497 equal = isl_set_is_equal(set1, set2);
6498 isl_set_free(set1);
6499 isl_set_free(set2);
6501 if (equal < 0)
6502 return -1;
6503 if (!equal)
6504 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6506 return 0;
6509 /* Is "pma" obviously equal to the isl_pw_multi_aff represented by "str"?
6511 static int pw_multi_aff_plain_is_equal(__isl_keep isl_pw_multi_aff *pma,
6512 const char *str)
6514 isl_ctx *ctx;
6515 isl_pw_multi_aff *pma2;
6516 int equal;
6518 if (!pma)
6519 return -1;
6521 ctx = isl_pw_multi_aff_get_ctx(pma);
6522 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
6523 equal = isl_pw_multi_aff_plain_is_equal(pma, pma2);
6524 isl_pw_multi_aff_free(pma2);
6526 return equal;
6529 /* Check that "pma" is obviously equal to the isl_pw_multi_aff
6530 * represented by "str".
6532 static int pw_multi_aff_check_plain_equal(__isl_keep isl_pw_multi_aff *pma,
6533 const char *str)
6535 int equal;
6537 equal = pw_multi_aff_plain_is_equal(pma, str);
6538 if (equal < 0)
6539 return -1;
6540 if (!equal)
6541 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_unknown,
6542 "result not as expected", return -1);
6543 return 0;
6546 /* Basic test for isl_pw_multi_aff_product.
6548 * Check that multiple pieces are properly handled.
6550 static int test_product_pma(isl_ctx *ctx)
6552 int equal;
6553 const char *str;
6554 isl_pw_multi_aff *pma1, *pma2;
6556 str = "{ A[i] -> B[1] : i < 0; A[i] -> B[2] : i >= 0 }";
6557 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
6558 str = "{ C[] -> D[] }";
6559 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
6560 pma1 = isl_pw_multi_aff_product(pma1, pma2);
6561 str = "{ [A[i] -> C[]] -> [B[(1)] -> D[]] : i < 0;"
6562 "[A[i] -> C[]] -> [B[(2)] -> D[]] : i >= 0 }";
6563 equal = pw_multi_aff_check_plain_equal(pma1, str);
6564 isl_pw_multi_aff_free(pma1);
6565 if (equal < 0)
6566 return -1;
6568 return 0;
6571 int test_product(isl_ctx *ctx)
6573 const char *str;
6574 isl_set *set;
6575 isl_union_set *uset1, *uset2;
6576 int ok;
6578 str = "{ A[i] }";
6579 set = isl_set_read_from_str(ctx, str);
6580 set = isl_set_product(set, isl_set_copy(set));
6581 ok = isl_set_is_wrapping(set);
6582 isl_set_free(set);
6583 if (ok < 0)
6584 return -1;
6585 if (!ok)
6586 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6588 str = "{ [] }";
6589 uset1 = isl_union_set_read_from_str(ctx, str);
6590 uset1 = isl_union_set_product(uset1, isl_union_set_copy(uset1));
6591 str = "{ [[] -> []] }";
6592 uset2 = isl_union_set_read_from_str(ctx, str);
6593 ok = isl_union_set_is_equal(uset1, uset2);
6594 isl_union_set_free(uset1);
6595 isl_union_set_free(uset2);
6596 if (ok < 0)
6597 return -1;
6598 if (!ok)
6599 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6601 if (test_product_pma(ctx) < 0)
6602 return -1;
6604 return 0;
6607 /* Check that two sets are not considered disjoint just because
6608 * they have a different set of (named) parameters.
6610 static int test_disjoint(isl_ctx *ctx)
6612 const char *str;
6613 isl_set *set, *set2;
6614 int disjoint;
6616 str = "[n] -> { [[]->[]] }";
6617 set = isl_set_read_from_str(ctx, str);
6618 str = "{ [[]->[]] }";
6619 set2 = isl_set_read_from_str(ctx, str);
6620 disjoint = isl_set_is_disjoint(set, set2);
6621 isl_set_free(set);
6622 isl_set_free(set2);
6623 if (disjoint < 0)
6624 return -1;
6625 if (disjoint)
6626 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6628 return 0;
6631 /* Inputs for isl_pw_multi_aff_is_equal tests.
6632 * "f1" and "f2" are the two function that need to be compared.
6633 * "equal" is the expected result.
6635 struct {
6636 int equal;
6637 const char *f1;
6638 const char *f2;
6639 } pma_equal_tests[] = {
6640 { 1, "[N] -> { [floor(N/2)] : 0 <= N <= 1 }",
6641 "[N] -> { [0] : 0 <= N <= 1 }" },
6642 { 1, "[N] -> { [floor(N/2)] : 0 <= N <= 2 }",
6643 "[N] -> { [0] : 0 <= N <= 1; [1] : N = 2 }" },
6644 { 0, "[N] -> { [floor(N/2)] : 0 <= N <= 2 }",
6645 "[N] -> { [0] : 0 <= N <= 1 }" },
6646 { 0, "{ [NaN] }", "{ [NaN] }" },
6649 int test_equal(isl_ctx *ctx)
6651 int i;
6652 const char *str;
6653 isl_set *set, *set2;
6654 int equal;
6656 str = "{ S_6[i] }";
6657 set = isl_set_read_from_str(ctx, str);
6658 str = "{ S_7[i] }";
6659 set2 = isl_set_read_from_str(ctx, str);
6660 equal = isl_set_is_equal(set, set2);
6661 isl_set_free(set);
6662 isl_set_free(set2);
6663 if (equal < 0)
6664 return -1;
6665 if (equal)
6666 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6668 for (i = 0; i < ARRAY_SIZE(pma_equal_tests); ++i) {
6669 int expected = pma_equal_tests[i].equal;
6670 isl_pw_multi_aff *f1, *f2;
6672 f1 = isl_pw_multi_aff_read_from_str(ctx, pma_equal_tests[i].f1);
6673 f2 = isl_pw_multi_aff_read_from_str(ctx, pma_equal_tests[i].f2);
6674 equal = isl_pw_multi_aff_is_equal(f1, f2);
6675 isl_pw_multi_aff_free(f1);
6676 isl_pw_multi_aff_free(f2);
6677 if (equal < 0)
6678 return -1;
6679 if (equal != expected)
6680 isl_die(ctx, isl_error_unknown,
6681 "unexpected equality result", return -1);
6684 return 0;
6687 static int test_plain_fixed(isl_ctx *ctx, __isl_take isl_map *map,
6688 enum isl_dim_type type, unsigned pos, int fixed)
6690 isl_bool test;
6692 test = isl_map_plain_is_fixed(map, type, pos, NULL);
6693 isl_map_free(map);
6694 if (test < 0)
6695 return -1;
6696 if (test == fixed)
6697 return 0;
6698 if (fixed)
6699 isl_die(ctx, isl_error_unknown,
6700 "map not detected as fixed", return -1);
6701 else
6702 isl_die(ctx, isl_error_unknown,
6703 "map detected as fixed", return -1);
6706 int test_fixed(isl_ctx *ctx)
6708 const char *str;
6709 isl_map *map;
6711 str = "{ [i] -> [i] }";
6712 map = isl_map_read_from_str(ctx, str);
6713 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 0))
6714 return -1;
6715 str = "{ [i] -> [1] }";
6716 map = isl_map_read_from_str(ctx, str);
6717 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
6718 return -1;
6719 str = "{ S_1[p1] -> [o0] : o0 = -2 and p1 >= 1 and p1 <= 7 }";
6720 map = isl_map_read_from_str(ctx, str);
6721 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
6722 return -1;
6723 map = isl_map_read_from_str(ctx, str);
6724 map = isl_map_neg(map);
6725 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
6726 return -1;
6728 return 0;
6731 struct isl_vertices_test_data {
6732 const char *set;
6733 int n;
6734 const char *vertex[6];
6735 } vertices_tests[] = {
6736 { "{ A[t, i] : t = 12 and i >= 4 and i <= 12 }",
6737 2, { "{ A[12, 4] }", "{ A[12, 12] }" } },
6738 { "{ A[t, i] : t = 14 and i = 1 }",
6739 1, { "{ A[14, 1] }" } },
6740 { "[n, m] -> { [a, b, c] : b <= a and a <= n and b > 0 and c >= b and "
6741 "c <= m and m <= n and m > 0 }",
6742 6, {
6743 "[n, m] -> { [n, m, m] : 0 < m <= n }",
6744 "[n, m] -> { [n, 1, m] : 0 < m <= n }",
6745 "[n, m] -> { [n, 1, 1] : 0 < m <= n }",
6746 "[n, m] -> { [m, m, m] : 0 < m <= n }",
6747 "[n, m] -> { [1, 1, m] : 0 < m <= n }",
6748 "[n, m] -> { [1, 1, 1] : 0 < m <= n }"
6749 } },
6752 /* Check that "vertex" corresponds to one of the vertices in data->vertex.
6754 static isl_stat find_vertex(__isl_take isl_vertex *vertex, void *user)
6756 struct isl_vertices_test_data *data = user;
6757 isl_ctx *ctx;
6758 isl_multi_aff *ma;
6759 isl_basic_set *bset;
6760 isl_pw_multi_aff *pma;
6761 int i;
6762 isl_bool equal;
6764 ctx = isl_vertex_get_ctx(vertex);
6765 bset = isl_vertex_get_domain(vertex);
6766 ma = isl_vertex_get_expr(vertex);
6767 pma = isl_pw_multi_aff_alloc(isl_set_from_basic_set(bset), ma);
6769 for (i = 0; i < data->n; ++i) {
6770 isl_pw_multi_aff *pma_i;
6772 pma_i = isl_pw_multi_aff_read_from_str(ctx, data->vertex[i]);
6773 equal = isl_pw_multi_aff_plain_is_equal(pma, pma_i);
6774 isl_pw_multi_aff_free(pma_i);
6776 if (equal < 0 || equal)
6777 break;
6780 isl_pw_multi_aff_free(pma);
6781 isl_vertex_free(vertex);
6783 if (equal < 0)
6784 return isl_stat_error;
6786 return equal ? isl_stat_ok : isl_stat_error;
6789 int test_vertices(isl_ctx *ctx)
6791 int i;
6793 for (i = 0; i < ARRAY_SIZE(vertices_tests); ++i) {
6794 isl_basic_set *bset;
6795 isl_vertices *vertices;
6796 int ok = 1;
6797 isl_size n;
6799 bset = isl_basic_set_read_from_str(ctx, vertices_tests[i].set);
6800 vertices = isl_basic_set_compute_vertices(bset);
6801 n = isl_vertices_get_n_vertices(vertices);
6802 if (vertices_tests[i].n != n)
6803 ok = 0;
6804 if (isl_vertices_foreach_vertex(vertices, &find_vertex,
6805 &vertices_tests[i]) < 0)
6806 ok = 0;
6807 isl_vertices_free(vertices);
6808 isl_basic_set_free(bset);
6810 if (n < 0)
6811 return -1;
6812 if (!ok)
6813 isl_die(ctx, isl_error_unknown, "unexpected vertices",
6814 return -1);
6817 return 0;
6820 int test_union_pw(isl_ctx *ctx)
6822 int equal;
6823 const char *str;
6824 isl_union_set *uset;
6825 isl_union_pw_qpolynomial *upwqp1, *upwqp2;
6827 str = "{ [x] -> x^2 }";
6828 upwqp1 = isl_union_pw_qpolynomial_read_from_str(ctx, str);
6829 upwqp2 = isl_union_pw_qpolynomial_copy(upwqp1);
6830 uset = isl_union_pw_qpolynomial_domain(upwqp1);
6831 upwqp1 = isl_union_pw_qpolynomial_copy(upwqp2);
6832 upwqp1 = isl_union_pw_qpolynomial_intersect_domain(upwqp1, uset);
6833 equal = isl_union_pw_qpolynomial_plain_is_equal(upwqp1, upwqp2);
6834 isl_union_pw_qpolynomial_free(upwqp1);
6835 isl_union_pw_qpolynomial_free(upwqp2);
6836 if (equal < 0)
6837 return -1;
6838 if (!equal)
6839 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
6841 return 0;
6844 /* Inputs for basic tests of functions that select
6845 * subparts of the domain of an isl_multi_union_pw_aff.
6846 * "fn" is the function that is tested.
6847 * "arg" is a string description of the input.
6848 * "res" is a string description of the expected result.
6850 struct {
6851 __isl_give isl_union_set *(*fn)(
6852 __isl_take isl_multi_union_pw_aff *mupa);
6853 const char *arg;
6854 const char *res;
6855 } un_locus_tests[] = {
6856 { &isl_multi_union_pw_aff_zero_union_set,
6857 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }]",
6858 "{ A[0,j]; B[0,j] }" },
6859 { &isl_multi_union_pw_aff_zero_union_set,
6860 "F[{ A[i,j] -> [i-j]; B[i,j] -> [i-j] : i >= 0 }]",
6861 "{ A[i,i]; B[i,i] : i >= 0 }" },
6862 { &isl_multi_union_pw_aff_zero_union_set,
6863 "(F[] : { A[i,j]; B[i,i] : i >= 0 })",
6864 "{ A[i,j]; B[i,i] : i >= 0 }" },
6867 /* Perform some basic tests of functions that select
6868 * subparts of the domain of an isl_multi_union_pw_aff.
6870 static int test_un_locus(isl_ctx *ctx)
6872 int i;
6873 isl_bool ok;
6874 isl_union_set *uset, *res;
6875 isl_multi_union_pw_aff *mupa;
6877 for (i = 0; i < ARRAY_SIZE(un_locus_tests); ++i) {
6878 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
6879 un_locus_tests[i].arg);
6880 res = isl_union_set_read_from_str(ctx, un_locus_tests[i].res);
6881 uset = un_locus_tests[i].fn(mupa);
6882 ok = isl_union_set_is_equal(uset, res);
6883 isl_union_set_free(uset);
6884 isl_union_set_free(res);
6885 if (ok < 0)
6886 return -1;
6887 if (!ok)
6888 isl_die(ctx, isl_error_unknown,
6889 "unexpected result", return -1);
6892 return 0;
6895 /* Inputs for basic tests of functions that select
6896 * subparts of an isl_union_map based on a relation
6897 * specified by an isl_multi_union_pw_aff.
6898 * "fn" is the function that is tested.
6899 * "arg1" and "arg2" are string descriptions of the inputs.
6900 * "res" is a string description of the expected result.
6902 struct {
6903 __isl_give isl_union_map *(*fn)(
6904 __isl_take isl_union_map *umap,
6905 __isl_take isl_multi_union_pw_aff *mupa);
6906 const char *arg1;
6907 const char *arg2;
6908 const char *res;
6909 } bin_locus_tests[] = {
6910 { &isl_union_map_eq_at_multi_union_pw_aff,
6911 "{ A[i,j] -> B[i',j'] }",
6912 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }]",
6913 "{ A[i,j] -> B[i,j'] }" },
6914 { &isl_union_map_eq_at_multi_union_pw_aff,
6915 "{ A[i,j] -> B[i',j'] }",
6916 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }, "
6917 "{ A[i,j] -> [j]; B[i,j] -> [j] }]",
6918 "{ A[i,j] -> B[i,j] }" },
6919 { &isl_union_map_eq_at_multi_union_pw_aff,
6920 "{ A[i,j] -> B[i',j']; A[i,j] -> C[i',j'] }",
6921 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }]",
6922 "{ A[i,j] -> B[i,j'] }" },
6923 { &isl_union_map_eq_at_multi_union_pw_aff,
6924 "{ A[i,j] -> B[i',j']; A[i,j] -> C[i',j'] }",
6925 "F[{ A[i,j] -> [i]; B[i,j] -> [i]; C[i,j] -> [0] }]",
6926 "{ A[i,j] -> B[i,j']; A[0,j] -> C[i',j'] }" },
6927 { &isl_union_map_eq_at_multi_union_pw_aff,
6928 "{ A[i,j] -> B[i',j'] }",
6929 "F[{ A[i,j] -> [i] : i > j; B[i,j] -> [i] }]",
6930 "{ A[i,j] -> B[i,j'] : i > j }" },
6931 { &isl_union_map_lex_lt_at_multi_union_pw_aff,
6932 "{ A[i,j] -> B[i',j'] }",
6933 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }, "
6934 "{ A[i,j] -> [j]; B[i,j] -> [j] }]",
6935 "{ A[i,j] -> B[i',j'] : i,j << i',j' }" },
6936 { &isl_union_map_lex_gt_at_multi_union_pw_aff,
6937 "{ A[i,j] -> B[i',j'] }",
6938 "F[{ A[i,j] -> [i]; B[i,j] -> [i] }, "
6939 "{ A[i,j] -> [j]; B[i,j] -> [j] }]",
6940 "{ A[i,j] -> B[i',j'] : i,j >> i',j' }" },
6941 { &isl_union_map_eq_at_multi_union_pw_aff,
6942 "{ A[i,j] -> B[i',j']; A[i,j] -> C[i',j'] }",
6943 "(F[] : { A[i,j]; B[i,j] })",
6944 "{ A[i,j] -> B[i',j'] }" },
6945 { &isl_union_map_eq_at_multi_union_pw_aff,
6946 "{ A[i,j] -> B[i',j'] }",
6947 "(F[] : { A[i,j] : i > j; B[i,j] : i < j })",
6948 "{ A[i,j] -> B[i',j'] : i > j and i' < j' }" },
6949 { &isl_union_map_eq_at_multi_union_pw_aff,
6950 "[N] -> { A[i,j] -> B[i',j'] : i,i' <= N }",
6951 "(F[] : { A[i,j] : i > j; B[i,j] : i < j })",
6952 "[N] -> { A[i,j] -> B[i',j'] : i > j and i' < j' and i,i' <= N }" },
6953 { &isl_union_map_eq_at_multi_union_pw_aff,
6954 "{ A[i,j] -> B[i',j'] }",
6955 "[N] -> (F[] : { A[i,j] : i < N; B[i,j] : i < N })",
6956 "[N] -> { A[i,j] -> B[i',j'] : i,i' < N }" },
6957 { &isl_union_map_eq_at_multi_union_pw_aff,
6958 "{ A[i,j] -> B[i',j'] }",
6959 "[N] -> (F[] : { : N >= 0 })",
6960 "[N] -> { A[i,j] -> B[i',j'] : N >= 0 }" },
6963 /* Perform some basic tests of functions that select
6964 * subparts of an isl_union_map based on a relation
6965 * specified by an isl_multi_union_pw_aff.
6967 static int test_bin_locus(isl_ctx *ctx)
6969 int i;
6970 isl_bool ok;
6971 isl_union_map *umap, *res;
6972 isl_multi_union_pw_aff *mupa;
6974 for (i = 0; i < ARRAY_SIZE(bin_locus_tests); ++i) {
6975 umap = isl_union_map_read_from_str(ctx,
6976 bin_locus_tests[i].arg1);
6977 mupa = isl_multi_union_pw_aff_read_from_str(ctx,
6978 bin_locus_tests[i].arg2);
6979 res = isl_union_map_read_from_str(ctx, bin_locus_tests[i].res);
6980 umap = bin_locus_tests[i].fn(umap, mupa);
6981 ok = isl_union_map_is_equal(umap, res);
6982 isl_union_map_free(umap);
6983 isl_union_map_free(res);
6984 if (ok < 0)
6985 return -1;
6986 if (!ok)
6987 isl_die(ctx, isl_error_unknown,
6988 "unexpected result", return -1);
6991 return 0;
6994 /* Perform basic locus tests.
6996 static int test_locus(isl_ctx *ctx)
6998 if (test_un_locus(ctx) < 0)
6999 return -1;
7000 if (test_bin_locus(ctx) < 0)
7001 return -1;
7002 return 0;
7005 /* Test that isl_union_pw_qpolynomial_eval picks up the function
7006 * defined over the correct domain space.
7008 static int test_eval_1(isl_ctx *ctx)
7010 const char *str;
7011 isl_point *pnt;
7012 isl_set *set;
7013 isl_union_pw_qpolynomial *upwqp;
7014 isl_val *v;
7015 int cmp;
7017 str = "{ A[x] -> x^2; B[x] -> -x^2 }";
7018 upwqp = isl_union_pw_qpolynomial_read_from_str(ctx, str);
7019 str = "{ A[6] }";
7020 set = isl_set_read_from_str(ctx, str);
7021 pnt = isl_set_sample_point(set);
7022 v = isl_union_pw_qpolynomial_eval(upwqp, pnt);
7023 cmp = isl_val_cmp_si(v, 36);
7024 isl_val_free(v);
7026 if (!v)
7027 return -1;
7028 if (cmp != 0)
7029 isl_die(ctx, isl_error_unknown, "unexpected value", return -1);
7031 return 0;
7034 /* Check that isl_qpolynomial_eval handles getting called on a void point.
7036 static int test_eval_2(isl_ctx *ctx)
7038 const char *str;
7039 isl_point *pnt;
7040 isl_set *set;
7041 isl_qpolynomial *qp;
7042 isl_val *v;
7043 isl_bool ok;
7045 str = "{ A[x] -> [x] }";
7046 qp = isl_qpolynomial_from_aff(isl_aff_read_from_str(ctx, str));
7047 str = "{ A[x] : false }";
7048 set = isl_set_read_from_str(ctx, str);
7049 pnt = isl_set_sample_point(set);
7050 v = isl_qpolynomial_eval(qp, pnt);
7051 ok = isl_val_is_nan(v);
7052 isl_val_free(v);
7054 if (ok < 0)
7055 return -1;
7056 if (!ok)
7057 isl_die(ctx, isl_error_unknown, "expecting NaN", return -1);
7059 return 0;
7062 /* Check that a polynomial (without local variables) can be evaluated
7063 * in a rational point.
7065 static isl_stat test_eval_3(isl_ctx *ctx)
7067 isl_pw_qpolynomial *pwqp;
7068 isl_point *pnt;
7069 isl_val *v;
7070 isl_stat r;
7072 pwqp = isl_pw_qpolynomial_read_from_str(ctx, "{ [x] -> x^2 }");
7073 pnt = isl_point_zero(isl_pw_qpolynomial_get_domain_space(pwqp));
7074 v = isl_val_read_from_str(ctx, "1/2");
7075 pnt = isl_point_set_coordinate_val(pnt, isl_dim_set, 0, v);
7076 v = isl_pw_qpolynomial_eval(pwqp, pnt);
7077 r = val_check_equal(v, "1/4");
7078 isl_val_free(v);
7080 return r;
7083 /* Inputs for isl_pw_aff_eval test.
7084 * "f" is the affine function.
7085 * "p" is the point where the function should be evaluated.
7086 * "res" is the expected result.
7088 struct {
7089 const char *f;
7090 const char *p;
7091 const char *res;
7092 } aff_eval_tests[] = {
7093 { "{ [i] -> [2 * i] }", "{ [4] }", "8" },
7094 { "{ [i] -> [2 * i] }", "{ [x] : false }", "NaN" },
7095 { "{ [i] -> [i + floor(i/2) + floor(i/3)] }", "{ [0] }", "0" },
7096 { "{ [i] -> [i + floor(i/2) + floor(i/3)] }", "{ [1] }", "1" },
7097 { "{ [i] -> [i + floor(i/2) + floor(i/3)] }", "{ [2] }", "3" },
7098 { "{ [i] -> [i + floor(i/2) + floor(i/3)] }", "{ [3] }", "5" },
7099 { "{ [i] -> [i + floor(i/2) + floor(i/3)] }", "{ [4] }", "7" },
7100 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [0] }", "0" },
7101 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [1] }", "0" },
7102 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [2] }", "0" },
7103 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [3] }", "0" },
7104 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [4] }", "1" },
7105 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [6] }", "1" },
7106 { "{ [i] -> [floor((3 * floor(i/2))/5)] }", "{ [8] }", "2" },
7107 { "{ [i] -> [i] : i > 0; [i] -> [-i] : i < 0 }", "{ [4] }", "4" },
7108 { "{ [i] -> [i] : i > 0; [i] -> [-i] : i < 0 }", "{ [-2] }", "2" },
7109 { "{ [i] -> [i] : i > 0; [i] -> [-i] : i < 0 }", "{ [0] }", "NaN" },
7110 { "[N] -> { [2 * N] }", "[N] -> { : N = 4 }", "8" },
7111 { "{ [i, j] -> [(i + j)/2] }", "{ [1, 1] }", "1" },
7112 { "{ [i, j] -> [(i + j)/2] }", "{ [1, 2] }", "3/2" },
7113 { "{ [i] -> [i] : i mod 2 = 0 }", "{ [4] }", "4" },
7114 { "{ [i] -> [i] : i mod 2 = 0 }", "{ [3] }", "NaN" },
7115 { "{ [i] -> [i] : i mod 2 = 0 }", "{ [x] : false }", "NaN" },
7118 /* Perform basic isl_pw_aff_eval tests.
7120 static int test_eval_aff(isl_ctx *ctx)
7122 int i;
7124 for (i = 0; i < ARRAY_SIZE(aff_eval_tests); ++i) {
7125 isl_stat r;
7126 isl_pw_aff *pa;
7127 isl_set *set;
7128 isl_point *pnt;
7129 isl_val *v;
7131 pa = isl_pw_aff_read_from_str(ctx, aff_eval_tests[i].f);
7132 set = isl_set_read_from_str(ctx, aff_eval_tests[i].p);
7133 pnt = isl_set_sample_point(set);
7134 v = isl_pw_aff_eval(pa, pnt);
7135 r = val_check_equal(v, aff_eval_tests[i].res);
7136 isl_val_free(v);
7137 if (r < 0)
7138 return -1;
7140 return 0;
7143 /* Perform basic evaluation tests.
7145 static int test_eval(isl_ctx *ctx)
7147 if (test_eval_1(ctx) < 0)
7148 return -1;
7149 if (test_eval_2(ctx) < 0)
7150 return -1;
7151 if (test_eval_3(ctx) < 0)
7152 return -1;
7153 if (test_eval_aff(ctx) < 0)
7154 return -1;
7155 return 0;
7158 /* Descriptions of sets that are tested for reparsing after printing.
7160 const char *output_tests[] = {
7161 "{ [1, y] : 0 <= y <= 1; [x, -x] : 0 <= x <= 1 }",
7162 "{ [x] : 1 = 0 }",
7163 "{ [x] : false }",
7164 "{ [x] : x mod 2 = 0 }",
7165 "{ [x] : x mod 2 = 1 }",
7166 "{ [x, y] : x mod 2 = 0 and 3*floor(y/2) < x }",
7167 "{ [y, x] : x mod 2 = 0 and 3*floor(y/2) < x }",
7168 "{ [x, y] : x mod 2 = 0 and 3*floor(y/2) = x + y }",
7169 "{ [y, x] : x mod 2 = 0 and 3*floor(y/2) = x + y }",
7170 "[n] -> { [y, x] : 2*((x + 2y) mod 3) = n }",
7171 "{ [x, y] : (2*floor(x/3) + 3*floor(y/4)) mod 5 = x }",
7174 /* Check that printing a set and reparsing a set from the printed output
7175 * results in the same set.
7177 static int test_output_set(isl_ctx *ctx)
7179 int i;
7180 char *str;
7181 isl_set *set1, *set2;
7182 isl_bool equal;
7184 for (i = 0; i < ARRAY_SIZE(output_tests); ++i) {
7185 set1 = isl_set_read_from_str(ctx, output_tests[i]);
7186 str = isl_set_to_str(set1);
7187 set2 = isl_set_read_from_str(ctx, str);
7188 free(str);
7189 equal = isl_set_is_equal(set1, set2);
7190 isl_set_free(set1);
7191 isl_set_free(set2);
7192 if (equal < 0)
7193 return -1;
7194 if (!equal)
7195 isl_die(ctx, isl_error_unknown,
7196 "parsed output not the same", return -1);
7199 return 0;
7202 /* Check that an isl_multi_aff is printed using a consistent space.
7204 static isl_stat test_output_ma(isl_ctx *ctx)
7206 char *str;
7207 isl_bool equal;
7208 isl_aff *aff;
7209 isl_multi_aff *ma, *ma2;
7211 ma = isl_multi_aff_read_from_str(ctx, "{ [a, b] -> [a + b] }");
7212 aff = isl_aff_read_from_str(ctx, "{ [c, d] -> [c + d] }");
7213 ma = isl_multi_aff_set_aff(ma, 0, aff);
7214 str = isl_multi_aff_to_str(ma);
7215 ma2 = isl_multi_aff_read_from_str(ctx, str);
7216 free(str);
7217 equal = isl_multi_aff_plain_is_equal(ma, ma2);
7218 isl_multi_aff_free(ma2);
7219 isl_multi_aff_free(ma);
7221 if (equal < 0)
7222 return isl_stat_error;
7223 if (!equal)
7224 isl_die(ctx, isl_error_unknown, "bad conversion",
7225 return isl_stat_error);
7227 return isl_stat_ok;
7230 /* Check that an isl_multi_pw_aff is printed using a consistent space.
7232 static isl_stat test_output_mpa(isl_ctx *ctx)
7234 char *str;
7235 isl_bool equal;
7236 isl_pw_aff *pa;
7237 isl_multi_pw_aff *mpa, *mpa2;
7239 mpa = isl_multi_pw_aff_read_from_str(ctx, "{ [a, b] -> [a + b] }");
7240 pa = isl_pw_aff_read_from_str(ctx, "{ [c, d] -> [c + d] }");
7241 mpa = isl_multi_pw_aff_set_pw_aff(mpa, 0, pa);
7242 str = isl_multi_pw_aff_to_str(mpa);
7243 mpa2 = isl_multi_pw_aff_read_from_str(ctx, str);
7244 free(str);
7245 equal = isl_multi_pw_aff_plain_is_equal(mpa, mpa2);
7246 isl_multi_pw_aff_free(mpa2);
7247 isl_multi_pw_aff_free(mpa);
7249 if (equal < 0)
7250 return isl_stat_error;
7251 if (!equal)
7252 isl_die(ctx, isl_error_unknown, "bad conversion",
7253 return isl_stat_error);
7255 return isl_stat_ok;
7258 int test_output(isl_ctx *ctx)
7260 char *s;
7261 const char *str;
7262 isl_pw_aff *pa;
7263 isl_printer *p;
7264 int equal;
7266 if (test_output_set(ctx) < 0)
7267 return -1;
7268 if (test_output_ma(ctx) < 0)
7269 return -1;
7270 if (test_output_mpa(ctx) < 0)
7271 return -1;
7273 str = "[x] -> { [1] : x % 4 <= 2; [2] : x = 3 }";
7274 pa = isl_pw_aff_read_from_str(ctx, str);
7276 p = isl_printer_to_str(ctx);
7277 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
7278 p = isl_printer_print_pw_aff(p, pa);
7279 s = isl_printer_get_str(p);
7280 isl_printer_free(p);
7281 isl_pw_aff_free(pa);
7282 if (!s)
7283 equal = -1;
7284 else
7285 equal = !strcmp(s, "4 * floord(x, 4) + 2 >= x ? 1 : 2");
7286 free(s);
7287 if (equal < 0)
7288 return -1;
7289 if (!equal)
7290 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
7292 return 0;
7295 int test_sample(isl_ctx *ctx)
7297 const char *str;
7298 isl_basic_set *bset1, *bset2;
7299 int empty, subset;
7301 str = "{ [a, b, c, d, e, f, g, h, i, j, k] : "
7302 "3i >= 1073741823b - c - 1073741823e + f and c >= 0 and "
7303 "3i >= -1 + 3221225466b + c + d - 3221225466e - f and "
7304 "2e >= a - b and 3e <= 2a and 3k <= -a and f <= -1 + a and "
7305 "3i <= 4 - a + 4b + 2c - e - 2f and 3k <= -a + c - f and "
7306 "3h >= -2 + a and 3g >= -3 - a and 3k >= -2 - a and "
7307 "3i >= -2 - a - 2c + 3e + 2f and 3h <= a + c - f and "
7308 "3h >= a + 2147483646b + 2c - 2147483646e - 2f and "
7309 "3g <= -1 - a and 3i <= 1 + c + d - f and a <= 1073741823 and "
7310 "f >= 1 - a + 1073741822b + c + d - 1073741822e and "
7311 "3i >= 1 + 2b - 2c + e + 2f + 3g and "
7312 "1073741822f <= 1073741822 - a + 1073741821b + 1073741822c +"
7313 "d - 1073741821e and "
7314 "3j <= 3 - a + 3b and 3g <= -2 - 2b + c + d - e - f and "
7315 "3j >= 1 - a + b + 2e and "
7316 "3f >= -3 + a + 3221225462b + 3c + d - 3221225465e and "
7317 "3i <= 4 - a + 4b - e and "
7318 "f <= 1073741822 + 1073741822b - 1073741822e and 3h <= a and "
7319 "f >= 0 and 2e <= 4 - a + 5b - d and 2e <= a - b + d and "
7320 "c <= -1 + a and 3i >= -2 - a + 3e and "
7321 "1073741822e <= 1073741823 - a + 1073741822b + c and "
7322 "3g >= -4 + 3221225464b + 3c + d - 3221225467e - 3f and "
7323 "3i >= -1 + 3221225466b + 3c + d - 3221225466e - 3f and "
7324 "1073741823e >= 1 + 1073741823b - d and "
7325 "3i >= 1073741823b + c - 1073741823e - f and "
7326 "3i >= 1 + 2b + e + 3g }";
7327 bset1 = isl_basic_set_read_from_str(ctx, str);
7328 bset2 = isl_basic_set_sample(isl_basic_set_copy(bset1));
7329 empty = isl_basic_set_is_empty(bset2);
7330 subset = isl_basic_set_is_subset(bset2, bset1);
7331 isl_basic_set_free(bset1);
7332 isl_basic_set_free(bset2);
7333 if (empty < 0 || subset < 0)
7334 return -1;
7335 if (empty)
7336 isl_die(ctx, isl_error_unknown, "point not found", return -1);
7337 if (!subset)
7338 isl_die(ctx, isl_error_unknown, "bad point found", return -1);
7340 return 0;
7343 int test_fixed_power(isl_ctx *ctx)
7345 const char *str;
7346 isl_map *map;
7347 isl_val *exp;
7348 int equal;
7350 str = "{ [i] -> [i + 1] }";
7351 map = isl_map_read_from_str(ctx, str);
7352 exp = isl_val_int_from_si(ctx, 23);
7353 map = isl_map_fixed_power_val(map, exp);
7354 equal = map_check_equal(map, "{ [i] -> [i + 23] }");
7355 isl_map_free(map);
7356 if (equal < 0)
7357 return -1;
7359 return 0;
7362 int test_slice(isl_ctx *ctx)
7364 const char *str;
7365 isl_map *map;
7366 int equal;
7368 str = "{ [i] -> [j] }";
7369 map = isl_map_read_from_str(ctx, str);
7370 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_out, 0);
7371 equal = map_check_equal(map, "{ [i] -> [i] }");
7372 isl_map_free(map);
7373 if (equal < 0)
7374 return -1;
7376 str = "{ [i] -> [j] }";
7377 map = isl_map_read_from_str(ctx, str);
7378 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_in, 0);
7379 equal = map_check_equal(map, "{ [i] -> [j] }");
7380 isl_map_free(map);
7381 if (equal < 0)
7382 return -1;
7384 str = "{ [i] -> [j] }";
7385 map = isl_map_read_from_str(ctx, str);
7386 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_out, 0);
7387 equal = map_check_equal(map, "{ [i] -> [-i] }");
7388 isl_map_free(map);
7389 if (equal < 0)
7390 return -1;
7392 str = "{ [i] -> [j] }";
7393 map = isl_map_read_from_str(ctx, str);
7394 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_in, 0);
7395 equal = map_check_equal(map, "{ [0] -> [j] }");
7396 isl_map_free(map);
7397 if (equal < 0)
7398 return -1;
7400 str = "{ [i] -> [j] }";
7401 map = isl_map_read_from_str(ctx, str);
7402 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_out, 0);
7403 equal = map_check_equal(map, "{ [i] -> [j] : i > j }");
7404 isl_map_free(map);
7405 if (equal < 0)
7406 return -1;
7408 str = "{ [i] -> [j] }";
7409 map = isl_map_read_from_str(ctx, str);
7410 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_in, 0);
7411 equal = map_check_equal(map, "{ [i] -> [j] : false }");
7412 isl_map_free(map);
7413 if (equal < 0)
7414 return -1;
7416 return 0;
7419 int test_eliminate(isl_ctx *ctx)
7421 const char *str;
7422 isl_map *map;
7423 int equal;
7425 str = "{ [i] -> [j] : i = 2j }";
7426 map = isl_map_read_from_str(ctx, str);
7427 map = isl_map_eliminate(map, isl_dim_out, 0, 1);
7428 equal = map_check_equal(map, "{ [i] -> [j] : exists a : i = 2a }");
7429 isl_map_free(map);
7430 if (equal < 0)
7431 return -1;
7433 return 0;
7436 /* Check basic functionality of isl_map_deltas_map.
7438 static int test_deltas_map(isl_ctx *ctx)
7440 const char *str;
7441 isl_map *map;
7442 int equal;
7444 str = "{ A[i] -> A[i + 1] }";
7445 map = isl_map_read_from_str(ctx, str);
7446 map = isl_map_deltas_map(map);
7447 equal = map_check_equal(map, "{ [A[i] -> A[i + 1]] -> A[1] }");
7448 isl_map_free(map);
7449 if (equal < 0)
7450 return -1;
7452 return 0;
7455 /* Check that isl_set_dim_residue_class detects that the values of j
7456 * in the set below are all odd and that it does not detect any spurious
7457 * strides.
7459 static int test_residue_class(isl_ctx *ctx)
7461 const char *str;
7462 isl_set *set;
7463 isl_int m, r;
7464 isl_stat res;
7466 str = "{ [i,j] : j = 4 i + 1 and 0 <= i <= 100; "
7467 "[i,j] : j = 4 i + 3 and 500 <= i <= 600 }";
7468 set = isl_set_read_from_str(ctx, str);
7469 isl_int_init(m);
7470 isl_int_init(r);
7471 res = isl_set_dim_residue_class(set, 1, &m, &r);
7472 if (res >= 0 &&
7473 (isl_int_cmp_si(m, 2) != 0 || isl_int_cmp_si(r, 1) != 0))
7474 isl_die(ctx, isl_error_unknown, "incorrect residue class",
7475 res = isl_stat_error);
7476 isl_int_clear(r);
7477 isl_int_clear(m);
7478 isl_set_free(set);
7480 return res;
7483 static int test_align_parameters_1(isl_ctx *ctx)
7485 const char *str;
7486 isl_space *space;
7487 isl_multi_aff *ma1, *ma2;
7488 int equal;
7490 str = "{ A[B[] -> C[]] -> D[E[] -> F[]] }";
7491 ma1 = isl_multi_aff_read_from_str(ctx, str);
7493 space = isl_space_params_alloc(ctx, 1);
7494 space = isl_space_set_dim_name(space, isl_dim_param, 0, "N");
7495 ma1 = isl_multi_aff_align_params(ma1, space);
7497 str = "[N] -> { A[B[] -> C[]] -> D[E[] -> F[]] }";
7498 ma2 = isl_multi_aff_read_from_str(ctx, str);
7500 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
7502 isl_multi_aff_free(ma1);
7503 isl_multi_aff_free(ma2);
7505 if (equal < 0)
7506 return -1;
7507 if (!equal)
7508 isl_die(ctx, isl_error_unknown,
7509 "result not as expected", return -1);
7511 return 0;
7514 /* Check the isl_multi_*_from_*_list operation in case inputs
7515 * have unaligned parameters.
7516 * In particular, older versions of isl would simply fail
7517 * (without printing any error message).
7519 static isl_stat test_align_parameters_2(isl_ctx *ctx)
7521 isl_space *space;
7522 isl_map *map;
7523 isl_aff *aff;
7524 isl_multi_aff *ma;
7526 map = isl_map_read_from_str(ctx, "{ A[] -> M[x] }");
7527 space = isl_map_get_space(map);
7528 isl_map_free(map);
7530 aff = isl_aff_read_from_str(ctx, "[N] -> { A[] -> [N] }");
7531 ma = isl_multi_aff_from_aff_list(space, isl_aff_list_from_aff(aff));
7532 isl_multi_aff_free(ma);
7534 if (!ma)
7535 return isl_stat_error;
7536 return isl_stat_ok;
7539 /* Perform basic parameter alignment tests.
7541 static int test_align_parameters(isl_ctx *ctx)
7543 if (test_align_parameters_1(ctx) < 0)
7544 return -1;
7545 if (test_align_parameters_2(ctx) < 0)
7546 return -1;
7548 return 0;
7551 /* Check that isl_*_drop_unused_params actually drops the unused parameters
7552 * by comparing the result using isl_*_plain_is_equal.
7553 * Note that this assumes that isl_*_plain_is_equal does not consider
7554 * objects that only differ by unused parameters to be equal.
7556 int test_drop_unused_parameters(isl_ctx *ctx)
7558 const char *str_with, *str_without;
7559 isl_basic_set *bset1, *bset2;
7560 isl_set *set1, *set2;
7561 isl_pw_aff *pwa1, *pwa2;
7562 int equal;
7564 str_with = "[n, m, o] -> { [m] }";
7565 str_without = "[m] -> { [m] }";
7567 bset1 = isl_basic_set_read_from_str(ctx, str_with);
7568 bset2 = isl_basic_set_read_from_str(ctx, str_without);
7569 bset1 = isl_basic_set_drop_unused_params(bset1);
7570 equal = isl_basic_set_plain_is_equal(bset1, bset2);
7571 isl_basic_set_free(bset1);
7572 isl_basic_set_free(bset2);
7574 if (equal < 0)
7575 return -1;
7576 if (!equal)
7577 isl_die(ctx, isl_error_unknown,
7578 "result not as expected", return -1);
7580 set1 = isl_set_read_from_str(ctx, str_with);
7581 set2 = isl_set_read_from_str(ctx, str_without);
7582 set1 = isl_set_drop_unused_params(set1);
7583 equal = isl_set_plain_is_equal(set1, set2);
7584 isl_set_free(set1);
7585 isl_set_free(set2);
7587 if (equal < 0)
7588 return -1;
7589 if (!equal)
7590 isl_die(ctx, isl_error_unknown,
7591 "result not as expected", return -1);
7593 pwa1 = isl_pw_aff_read_from_str(ctx, str_with);
7594 pwa2 = isl_pw_aff_read_from_str(ctx, str_without);
7595 pwa1 = isl_pw_aff_drop_unused_params(pwa1);
7596 equal = isl_pw_aff_plain_is_equal(pwa1, pwa2);
7597 isl_pw_aff_free(pwa1);
7598 isl_pw_aff_free(pwa2);
7600 if (equal < 0)
7601 return -1;
7602 if (!equal)
7603 isl_die(ctx, isl_error_unknown,
7604 "result not as expected", return -1);
7606 return 0;
7609 static int test_list(isl_ctx *ctx)
7611 isl_id *a, *b, *c, *d, *id;
7612 isl_id_list *list;
7613 isl_size n;
7614 int ok;
7616 a = isl_id_alloc(ctx, "a", NULL);
7617 b = isl_id_alloc(ctx, "b", NULL);
7618 c = isl_id_alloc(ctx, "c", NULL);
7619 d = isl_id_alloc(ctx, "d", NULL);
7621 list = isl_id_list_alloc(ctx, 4);
7622 list = isl_id_list_add(list, b);
7623 list = isl_id_list_insert(list, 0, a);
7624 list = isl_id_list_add(list, c);
7625 list = isl_id_list_add(list, d);
7626 list = isl_id_list_drop(list, 1, 1);
7628 n = isl_id_list_n_id(list);
7629 if (n < 0)
7630 return -1;
7631 if (n != 3) {
7632 isl_id_list_free(list);
7633 isl_die(ctx, isl_error_unknown,
7634 "unexpected number of elements in list", return -1);
7637 id = isl_id_list_get_id(list, 0);
7638 ok = id == a;
7639 isl_id_free(id);
7640 id = isl_id_list_get_id(list, 1);
7641 ok = ok && id == c;
7642 isl_id_free(id);
7643 id = isl_id_list_get_id(list, 2);
7644 ok = ok && id == d;
7645 isl_id_free(id);
7647 isl_id_list_free(list);
7649 if (!ok)
7650 isl_die(ctx, isl_error_unknown,
7651 "unexpected elements in list", return -1);
7653 return 0;
7656 /* Check the conversion from an isl_multi_aff to an isl_basic_set.
7658 static isl_stat test_ma_conversion(isl_ctx *ctx)
7660 const char *str;
7661 isl_bool equal;
7662 isl_multi_aff *ma;
7663 isl_basic_set *bset1, *bset2;
7665 str = "[N] -> { A[0, N + 1] }";
7666 ma = isl_multi_aff_read_from_str(ctx, str);
7667 bset1 = isl_basic_set_read_from_str(ctx, str);
7668 bset2 = isl_basic_set_from_multi_aff(ma);
7669 equal = isl_basic_set_is_equal(bset1, bset2);
7670 isl_basic_set_free(bset1);
7671 isl_basic_set_free(bset2);
7672 if (equal < 0)
7673 return isl_stat_error;
7674 if (!equal)
7675 isl_die(ctx, isl_error_unknown, "bad conversion",
7676 return isl_stat_error);
7677 return isl_stat_ok;
7680 const char *set_conversion_tests[] = {
7681 "[N] -> { [i] : N - 1 <= 2 i <= N }",
7682 "[N] -> { [i] : exists a : i = 4 a and N - 1 <= i <= N }",
7683 "[N] -> { [i,j] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
7684 "[N] -> { [[i]->[j]] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
7685 "[N] -> { [3*floor(N/2) + 5*floor(N/3)] }",
7686 "[a, b] -> { [c, d] : (4*floor((-a + c)/4) = -a + c and "
7687 "32*floor((-b + d)/32) = -b + d and 5 <= c <= 8 and "
7688 "-3 + c <= d <= 28 + c) }",
7691 /* Check that converting from isl_set to isl_pw_multi_aff and back
7692 * to isl_set produces the original isl_set.
7694 static int test_set_conversion(isl_ctx *ctx)
7696 int i;
7697 const char *str;
7698 isl_set *set1, *set2;
7699 isl_pw_multi_aff *pma;
7700 int equal;
7702 for (i = 0; i < ARRAY_SIZE(set_conversion_tests); ++i) {
7703 str = set_conversion_tests[i];
7704 set1 = isl_set_read_from_str(ctx, str);
7705 pma = isl_pw_multi_aff_from_set(isl_set_copy(set1));
7706 set2 = isl_set_from_pw_multi_aff(pma);
7707 equal = isl_set_is_equal(set1, set2);
7708 isl_set_free(set1);
7709 isl_set_free(set2);
7711 if (equal < 0)
7712 return -1;
7713 if (!equal)
7714 isl_die(ctx, isl_error_unknown, "bad conversion",
7715 return -1);
7718 return 0;
7721 const char *conversion_tests[] = {
7722 "{ [a, b, c, d] -> s0[a, b, e, f] : "
7723 "exists (e0 = [(a - 2c)/3], e1 = [(-4 + b - 5d)/9], "
7724 "e2 = [(-d + f)/9]: 3e0 = a - 2c and 9e1 = -4 + b - 5d and "
7725 "9e2 = -d + f and f >= 0 and f <= 8 and 9e >= -5 - 2a and "
7726 "9e <= -2 - 2a) }",
7727 "{ [a, b] -> [c] : exists (e0 = floor((-a - b + c)/5): "
7728 "5e0 = -a - b + c and c >= -a and c <= 4 - a) }",
7729 "{ [a, b] -> [c] : exists d : 18 * d = -3 - a + 2c and 1 <= c <= 3 }",
7732 /* Check that converting from isl_map to isl_pw_multi_aff and back
7733 * to isl_map produces the original isl_map.
7735 static int test_map_conversion(isl_ctx *ctx)
7737 int i;
7738 isl_map *map1, *map2;
7739 isl_pw_multi_aff *pma;
7740 int equal;
7742 for (i = 0; i < ARRAY_SIZE(conversion_tests); ++i) {
7743 map1 = isl_map_read_from_str(ctx, conversion_tests[i]);
7744 pma = isl_pw_multi_aff_from_map(isl_map_copy(map1));
7745 map2 = isl_map_from_pw_multi_aff(pma);
7746 equal = isl_map_is_equal(map1, map2);
7747 isl_map_free(map1);
7748 isl_map_free(map2);
7750 if (equal < 0)
7751 return -1;
7752 if (!equal)
7753 isl_die(ctx, isl_error_unknown, "bad conversion",
7754 return -1);
7757 return 0;
7760 /* Descriptions of isl_pw_multi_aff objects for testing conversion
7761 * to isl_multi_pw_aff and back.
7763 const char *mpa_conversion_tests[] = {
7764 "{ [x] -> A[x] }",
7765 "{ [x] -> A[x] : x >= 0 }",
7766 "{ [x] -> A[x] : x >= 0; [x] -> A[-x] : x < 0 }",
7767 "{ [x] -> A[x, x + 1] }",
7768 "{ [x] -> A[] }",
7769 "{ [x] -> A[] : x >= 0 }",
7772 /* Check that conversion from isl_pw_multi_aff to isl_multi_pw_aff and
7773 * back to isl_pw_multi_aff preserves the original meaning.
7775 static int test_mpa_conversion(isl_ctx *ctx)
7777 int i;
7778 isl_pw_multi_aff *pma1, *pma2;
7779 isl_multi_pw_aff *mpa;
7780 int equal;
7782 for (i = 0; i < ARRAY_SIZE(mpa_conversion_tests); ++i) {
7783 const char *str;
7784 str = mpa_conversion_tests[i];
7785 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
7786 pma2 = isl_pw_multi_aff_copy(pma1);
7787 mpa = isl_multi_pw_aff_from_pw_multi_aff(pma1);
7788 pma1 = isl_pw_multi_aff_from_multi_pw_aff(mpa);
7789 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
7790 isl_pw_multi_aff_free(pma1);
7791 isl_pw_multi_aff_free(pma2);
7793 if (equal < 0)
7794 return -1;
7795 if (!equal)
7796 isl_die(ctx, isl_error_unknown, "bad conversion",
7797 return -1);
7800 return 0;
7803 /* Descriptions of union maps that should be convertible
7804 * to an isl_multi_union_pw_aff.
7806 const char *umap_mupa_conversion_tests[] = {
7807 "{ [a, b, c, d] -> s0[a, b, e, f] : "
7808 "exists (e0 = [(a - 2c)/3], e1 = [(-4 + b - 5d)/9], "
7809 "e2 = [(-d + f)/9]: 3e0 = a - 2c and 9e1 = -4 + b - 5d and "
7810 "9e2 = -d + f and f >= 0 and f <= 8 and 9e >= -5 - 2a and "
7811 "9e <= -2 - 2a) }",
7812 "{ [a, b] -> [c] : exists (e0 = floor((-a - b + c)/5): "
7813 "5e0 = -a - b + c and c >= -a and c <= 4 - a) }",
7814 "{ [a, b] -> [c] : exists d : 18 * d = -3 - a + 2c and 1 <= c <= 3 }",
7815 "{ A[] -> B[0]; C[] -> B[1] }",
7816 "{ A[] -> B[]; C[] -> B[] }",
7819 /* Check that converting from isl_union_map to isl_multi_union_pw_aff and back
7820 * to isl_union_map produces the original isl_union_map.
7822 static int test_union_map_mupa_conversion(isl_ctx *ctx)
7824 int i;
7825 isl_union_map *umap1, *umap2;
7826 isl_multi_union_pw_aff *mupa;
7827 int equal;
7829 for (i = 0; i < ARRAY_SIZE(umap_mupa_conversion_tests); ++i) {
7830 const char *str;
7831 str = umap_mupa_conversion_tests[i];
7832 umap1 = isl_union_map_read_from_str(ctx, str);
7833 umap2 = isl_union_map_copy(umap1);
7834 mupa = isl_multi_union_pw_aff_from_union_map(umap2);
7835 umap2 = isl_union_map_from_multi_union_pw_aff(mupa);
7836 equal = isl_union_map_is_equal(umap1, umap2);
7837 isl_union_map_free(umap1);
7838 isl_union_map_free(umap2);
7840 if (equal < 0)
7841 return -1;
7842 if (!equal)
7843 isl_die(ctx, isl_error_unknown, "bad conversion",
7844 return -1);
7847 return 0;
7850 static int test_conversion(isl_ctx *ctx)
7852 if (test_ma_conversion(ctx) < 0)
7853 return -1;
7854 if (test_set_conversion(ctx) < 0)
7855 return -1;
7856 if (test_map_conversion(ctx) < 0)
7857 return -1;
7858 if (test_mpa_conversion(ctx) < 0)
7859 return -1;
7860 if (test_union_map_mupa_conversion(ctx) < 0)
7861 return -1;
7862 return 0;
7865 /* Check that isl_basic_map_curry does not modify input.
7867 static int test_curry(isl_ctx *ctx)
7869 const char *str;
7870 isl_basic_map *bmap1, *bmap2;
7871 int equal;
7873 str = "{ [A[] -> B[]] -> C[] }";
7874 bmap1 = isl_basic_map_read_from_str(ctx, str);
7875 bmap2 = isl_basic_map_curry(isl_basic_map_copy(bmap1));
7876 equal = isl_basic_map_is_equal(bmap1, bmap2);
7877 isl_basic_map_free(bmap1);
7878 isl_basic_map_free(bmap2);
7880 if (equal < 0)
7881 return -1;
7882 if (equal)
7883 isl_die(ctx, isl_error_unknown,
7884 "curried map should not be equal to original",
7885 return -1);
7887 return 0;
7890 struct {
7891 const char *set;
7892 const char *ma;
7893 const char *res;
7894 } preimage_tests[] = {
7895 { "{ B[i,j] : 0 <= i < 10 and 0 <= j < 100 }",
7896 "{ A[j,i] -> B[i,j] }",
7897 "{ A[j,i] : 0 <= i < 10 and 0 <= j < 100 }" },
7898 { "{ rat: B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
7899 "{ A[a,b] -> B[a/2,b/6] }",
7900 "{ rat: A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 }" },
7901 { "{ B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
7902 "{ A[a,b] -> B[a/2,b/6] }",
7903 "{ A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 and "
7904 "exists i,j : a = 2 i and b = 6 j }" },
7905 { "[n] -> { S[i] : 0 <= i <= 100 }", "[n] -> { S[n] }",
7906 "[n] -> { : 0 <= n <= 100 }" },
7907 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
7908 "{ A[a] -> B[2a] }",
7909 "{ A[a] : 0 <= a < 50 and exists b : a = 2 b }" },
7910 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
7911 "{ A[a] -> B[([a/2])] }",
7912 "{ A[a] : 0 <= a < 200 and exists b : [a/2] = 4 b }" },
7913 { "{ B[i,j,k] : 0 <= i,j,k <= 100 }",
7914 "{ A[a] -> B[a,a,a/3] }",
7915 "{ A[a] : 0 <= a <= 100 and exists b : a = 3 b }" },
7916 { "{ B[i,j] : j = [(i)/2] } ", "{ A[i,j] -> B[i/3,j] }",
7917 "{ A[i,j] : j = [(i)/6] and exists a : i = 3 a }" },
7920 static int test_preimage_basic_set(isl_ctx *ctx)
7922 int i;
7923 isl_basic_set *bset1, *bset2;
7924 isl_multi_aff *ma;
7925 int equal;
7927 for (i = 0; i < ARRAY_SIZE(preimage_tests); ++i) {
7928 bset1 = isl_basic_set_read_from_str(ctx, preimage_tests[i].set);
7929 ma = isl_multi_aff_read_from_str(ctx, preimage_tests[i].ma);
7930 bset2 = isl_basic_set_read_from_str(ctx, preimage_tests[i].res);
7931 bset1 = isl_basic_set_preimage_multi_aff(bset1, ma);
7932 equal = isl_basic_set_is_equal(bset1, bset2);
7933 isl_basic_set_free(bset1);
7934 isl_basic_set_free(bset2);
7935 if (equal < 0)
7936 return -1;
7937 if (!equal)
7938 isl_die(ctx, isl_error_unknown, "bad preimage",
7939 return -1);
7942 return 0;
7945 struct {
7946 const char *map;
7947 const char *ma;
7948 const char *res;
7949 } preimage_domain_tests[] = {
7950 { "{ B[i,j] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }",
7951 "{ A[j,i] -> B[i,j] }",
7952 "{ A[j,i] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }" },
7953 { "{ B[i] -> C[i]; D[i] -> E[i] }",
7954 "{ A[i] -> B[i + 1] }",
7955 "{ A[i] -> C[i + 1] }" },
7956 { "{ B[i] -> C[i]; B[i] -> E[i] }",
7957 "{ A[i] -> B[i + 1] }",
7958 "{ A[i] -> C[i + 1]; A[i] -> E[i + 1] }" },
7959 { "{ B[i] -> C[([i/2])] }",
7960 "{ A[i] -> B[2i] }",
7961 "{ A[i] -> C[i] }" },
7962 { "{ B[i,j] -> C[([i/2]), ([(i+j)/3])] }",
7963 "{ A[i] -> B[([i/5]), ([i/7])] }",
7964 "{ A[i] -> C[([([i/5])/2]), ([(([i/5])+([i/7]))/3])] }" },
7965 { "[N] -> { B[i] -> C[([N/2]), i, ([N/3])] }",
7966 "[N] -> { A[] -> B[([N/5])] }",
7967 "[N] -> { A[] -> C[([N/2]), ([N/5]), ([N/3])] }" },
7968 { "{ B[i] -> C[i] : exists a : i = 5 a }",
7969 "{ A[i] -> B[2i] }",
7970 "{ A[i] -> C[2i] : exists a : 2i = 5 a }" },
7971 { "{ B[i] -> C[i] : exists a : i = 2 a; "
7972 "B[i] -> D[i] : exists a : i = 2 a + 1 }",
7973 "{ A[i] -> B[2i] }",
7974 "{ A[i] -> C[2i] }" },
7975 { "{ A[i] -> B[i] }", "{ C[i] -> A[(i + floor(i/3))/2] }",
7976 "{ C[i] -> B[j] : 2j = i + floor(i/3) }" },
7979 static int test_preimage_union_map(isl_ctx *ctx)
7981 int i;
7982 isl_union_map *umap1, *umap2;
7983 isl_multi_aff *ma;
7984 int equal;
7986 for (i = 0; i < ARRAY_SIZE(preimage_domain_tests); ++i) {
7987 umap1 = isl_union_map_read_from_str(ctx,
7988 preimage_domain_tests[i].map);
7989 ma = isl_multi_aff_read_from_str(ctx,
7990 preimage_domain_tests[i].ma);
7991 umap2 = isl_union_map_read_from_str(ctx,
7992 preimage_domain_tests[i].res);
7993 umap1 = isl_union_map_preimage_domain_multi_aff(umap1, ma);
7994 equal = isl_union_map_is_equal(umap1, umap2);
7995 isl_union_map_free(umap1);
7996 isl_union_map_free(umap2);
7997 if (equal < 0)
7998 return -1;
7999 if (!equal)
8000 isl_die(ctx, isl_error_unknown, "bad preimage",
8001 return -1);
8004 return 0;
8007 static int test_preimage(isl_ctx *ctx)
8009 if (test_preimage_basic_set(ctx) < 0)
8010 return -1;
8011 if (test_preimage_union_map(ctx) < 0)
8012 return -1;
8014 return 0;
8017 struct {
8018 const char *ma1;
8019 const char *ma;
8020 const char *res;
8021 } pullback_tests[] = {
8022 { "{ B[i,j] -> C[i + 2j] }" , "{ A[a,b] -> B[b,a] }",
8023 "{ A[a,b] -> C[b + 2a] }" },
8024 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/2] }", "{ A[a] -> C[a] }" },
8025 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[2a] }", "{ A[a] -> C[a] }" },
8026 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[(a)/3] }",
8027 "{ A[a] -> C[(a)/6] }" },
8028 { "{ B[i] -> C[2i] }", "{ A[a] -> B[5a] }", "{ A[a] -> C[10a] }" },
8029 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/3] }",
8030 "{ A[a] -> C[(2a)/3] }" },
8031 { "{ B[i,j] -> C[i + j] }", "{ A[a] -> B[a,a] }", "{ A[a] -> C[2a] }"},
8032 { "{ B[a] -> C[a,a] }", "{ A[i,j] -> B[i + j] }",
8033 "{ A[i,j] -> C[i + j, i + j] }"},
8034 { "{ B[i] -> C[([i/2])] }", "{ B[5] }", "{ C[2] }" },
8035 { "[n] -> { B[i,j] -> C[([i/2]) + 2j] }",
8036 "[n] -> { B[n,[n/3]] }", "[n] -> { C[([n/2]) + 2*[n/3]] }", },
8037 { "{ [i, j] -> [floor((i)/4) + floor((2*i+j)/5)] }",
8038 "{ [i, j] -> [floor((i)/3), j] }",
8039 "{ [i, j] -> [(floor((i)/12) + floor((j + 2*floor((i)/3))/5))] }" },
8042 static int test_pullback(isl_ctx *ctx)
8044 int i;
8045 isl_multi_aff *ma1, *ma2;
8046 isl_multi_aff *ma;
8047 int equal;
8049 for (i = 0; i < ARRAY_SIZE(pullback_tests); ++i) {
8050 ma1 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma1);
8051 ma = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma);
8052 ma2 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].res);
8053 ma1 = isl_multi_aff_pullback_multi_aff(ma1, ma);
8054 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
8055 isl_multi_aff_free(ma1);
8056 isl_multi_aff_free(ma2);
8057 if (equal < 0)
8058 return -1;
8059 if (!equal)
8060 isl_die(ctx, isl_error_unknown, "bad pullback",
8061 return -1);
8064 return 0;
8067 /* Check that negation is printed correctly and that equal expressions
8068 * are correctly identified.
8070 static int test_ast(isl_ctx *ctx)
8072 isl_ast_expr *expr, *expr1, *expr2, *expr3;
8073 char *str;
8074 int ok, equal;
8076 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
8077 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
8078 expr = isl_ast_expr_add(expr1, expr2);
8079 expr2 = isl_ast_expr_copy(expr);
8080 expr = isl_ast_expr_neg(expr);
8081 expr2 = isl_ast_expr_neg(expr2);
8082 equal = isl_ast_expr_is_equal(expr, expr2);
8083 str = isl_ast_expr_to_C_str(expr);
8084 ok = str ? !strcmp(str, "-(A + B)") : -1;
8085 free(str);
8086 isl_ast_expr_free(expr);
8087 isl_ast_expr_free(expr2);
8089 if (ok < 0 || equal < 0)
8090 return -1;
8091 if (!equal)
8092 isl_die(ctx, isl_error_unknown,
8093 "equal expressions not considered equal", return -1);
8094 if (!ok)
8095 isl_die(ctx, isl_error_unknown,
8096 "isl_ast_expr printed incorrectly", return -1);
8098 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
8099 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
8100 expr = isl_ast_expr_add(expr1, expr2);
8101 expr3 = isl_ast_expr_from_id(isl_id_alloc(ctx, "C", NULL));
8102 expr = isl_ast_expr_sub(expr3, expr);
8103 str = isl_ast_expr_to_C_str(expr);
8104 ok = str ? !strcmp(str, "C - (A + B)") : -1;
8105 free(str);
8106 isl_ast_expr_free(expr);
8108 if (ok < 0)
8109 return -1;
8110 if (!ok)
8111 isl_die(ctx, isl_error_unknown,
8112 "isl_ast_expr printed incorrectly", return -1);
8114 return 0;
8117 /* Check that isl_ast_build_expr_from_set returns a valid expression
8118 * for an empty set. Note that isl_ast_build_expr_from_set getting
8119 * called on an empty set probably indicates a bug in the caller.
8121 static int test_ast_build(isl_ctx *ctx)
8123 isl_set *set;
8124 isl_ast_build *build;
8125 isl_ast_expr *expr;
8127 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
8128 build = isl_ast_build_from_context(set);
8130 set = isl_set_empty(isl_space_params_alloc(ctx, 0));
8131 expr = isl_ast_build_expr_from_set(build, set);
8133 isl_ast_expr_free(expr);
8134 isl_ast_build_free(build);
8136 if (!expr)
8137 return -1;
8139 return 0;
8142 /* Internal data structure for before_for and after_for callbacks.
8144 * depth is the current depth
8145 * before is the number of times before_for has been called
8146 * after is the number of times after_for has been called
8148 struct isl_test_codegen_data {
8149 int depth;
8150 int before;
8151 int after;
8154 /* This function is called before each for loop in the AST generated
8155 * from test_ast_gen1.
8157 * Increment the number of calls and the depth.
8158 * Check that the space returned by isl_ast_build_get_schedule_space
8159 * matches the target space of the schedule returned by
8160 * isl_ast_build_get_schedule.
8161 * Return an isl_id that is checked by the corresponding call
8162 * to after_for.
8164 static __isl_give isl_id *before_for(__isl_keep isl_ast_build *build,
8165 void *user)
8167 struct isl_test_codegen_data *data = user;
8168 isl_ctx *ctx;
8169 isl_space *space;
8170 isl_union_map *schedule;
8171 isl_union_set *uset;
8172 isl_set *set;
8173 isl_bool empty;
8174 isl_size n;
8175 char name[] = "d0";
8177 ctx = isl_ast_build_get_ctx(build);
8179 if (data->before >= 3)
8180 isl_die(ctx, isl_error_unknown,
8181 "unexpected number of for nodes", return NULL);
8182 if (data->depth >= 2)
8183 isl_die(ctx, isl_error_unknown,
8184 "unexpected depth", return NULL);
8186 snprintf(name, sizeof(name), "d%d", data->depth);
8187 data->before++;
8188 data->depth++;
8190 schedule = isl_ast_build_get_schedule(build);
8191 uset = isl_union_map_range(schedule);
8192 n = isl_union_set_n_set(uset);
8193 if (n != 1) {
8194 isl_union_set_free(uset);
8195 if (n < 0)
8196 return NULL;
8197 isl_die(ctx, isl_error_unknown,
8198 "expecting single range space", return NULL);
8201 space = isl_ast_build_get_schedule_space(build);
8202 set = isl_union_set_extract_set(uset, space);
8203 isl_union_set_free(uset);
8204 empty = isl_set_is_empty(set);
8205 isl_set_free(set);
8207 if (empty < 0)
8208 return NULL;
8209 if (empty)
8210 isl_die(ctx, isl_error_unknown,
8211 "spaces don't match", return NULL);
8213 return isl_id_alloc(ctx, name, NULL);
8216 /* This function is called after each for loop in the AST generated
8217 * from test_ast_gen1.
8219 * Increment the number of calls and decrement the depth.
8220 * Check that the annotation attached to the node matches
8221 * the isl_id returned by the corresponding call to before_for.
8223 static __isl_give isl_ast_node *after_for(__isl_take isl_ast_node *node,
8224 __isl_keep isl_ast_build *build, void *user)
8226 struct isl_test_codegen_data *data = user;
8227 isl_id *id;
8228 const char *name;
8229 int valid;
8231 data->after++;
8232 data->depth--;
8234 if (data->after > data->before)
8235 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
8236 "mismatch in number of for nodes",
8237 return isl_ast_node_free(node));
8239 id = isl_ast_node_get_annotation(node);
8240 if (!id)
8241 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
8242 "missing annotation", return isl_ast_node_free(node));
8244 name = isl_id_get_name(id);
8245 valid = name && atoi(name + 1) == data->depth;
8246 isl_id_free(id);
8248 if (!valid)
8249 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
8250 "wrong annotation", return isl_ast_node_free(node));
8252 return node;
8255 /* Check that the before_each_for and after_each_for callbacks
8256 * are called for each for loop in the generated code,
8257 * that they are called in the right order and that the isl_id
8258 * returned from the before_each_for callback is attached to
8259 * the isl_ast_node passed to the corresponding after_each_for call.
8261 static int test_ast_gen1(isl_ctx *ctx)
8263 const char *str;
8264 isl_set *set;
8265 isl_union_map *schedule;
8266 isl_ast_build *build;
8267 isl_ast_node *tree;
8268 struct isl_test_codegen_data data;
8270 str = "[N] -> { : N >= 10 }";
8271 set = isl_set_read_from_str(ctx, str);
8272 str = "[N] -> { A[i,j] -> S[8,i,3,j] : 0 <= i,j <= N; "
8273 "B[i,j] -> S[8,j,9,i] : 0 <= i,j <= N }";
8274 schedule = isl_union_map_read_from_str(ctx, str);
8276 data.before = 0;
8277 data.after = 0;
8278 data.depth = 0;
8279 build = isl_ast_build_from_context(set);
8280 build = isl_ast_build_set_before_each_for(build,
8281 &before_for, &data);
8282 build = isl_ast_build_set_after_each_for(build,
8283 &after_for, &data);
8284 tree = isl_ast_build_node_from_schedule_map(build, schedule);
8285 isl_ast_build_free(build);
8286 if (!tree)
8287 return -1;
8289 isl_ast_node_free(tree);
8291 if (data.before != 3 || data.after != 3)
8292 isl_die(ctx, isl_error_unknown,
8293 "unexpected number of for nodes", return -1);
8295 return 0;
8298 /* Check that the AST generator handles domains that are integrally disjoint
8299 * but not rationally disjoint.
8301 static int test_ast_gen2(isl_ctx *ctx)
8303 const char *str;
8304 isl_set *set;
8305 isl_union_map *schedule;
8306 isl_union_map *options;
8307 isl_ast_build *build;
8308 isl_ast_node *tree;
8310 str = "{ A[i,j] -> [i,j] : 0 <= i,j <= 1 }";
8311 schedule = isl_union_map_read_from_str(ctx, str);
8312 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
8313 build = isl_ast_build_from_context(set);
8315 str = "{ [i,j] -> atomic[1] : i + j = 1; [i,j] -> unroll[1] : i = j }";
8316 options = isl_union_map_read_from_str(ctx, str);
8317 build = isl_ast_build_set_options(build, options);
8318 tree = isl_ast_build_node_from_schedule_map(build, schedule);
8319 isl_ast_build_free(build);
8320 if (!tree)
8321 return -1;
8322 isl_ast_node_free(tree);
8324 return 0;
8327 /* Increment *user on each call.
8329 static __isl_give isl_ast_node *count_domains(__isl_take isl_ast_node *node,
8330 __isl_keep isl_ast_build *build, void *user)
8332 int *n = user;
8334 (*n)++;
8336 return node;
8339 /* Test that unrolling tries to minimize the number of instances.
8340 * In particular, for the schedule given below, make sure it generates
8341 * 3 nodes (rather than 101).
8343 static int test_ast_gen3(isl_ctx *ctx)
8345 const char *str;
8346 isl_set *set;
8347 isl_union_map *schedule;
8348 isl_union_map *options;
8349 isl_ast_build *build;
8350 isl_ast_node *tree;
8351 int n_domain = 0;
8353 str = "[n] -> { A[i] -> [i] : 0 <= i <= 100 and n <= i <= n + 2 }";
8354 schedule = isl_union_map_read_from_str(ctx, str);
8355 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
8357 str = "{ [i] -> unroll[0] }";
8358 options = isl_union_map_read_from_str(ctx, str);
8360 build = isl_ast_build_from_context(set);
8361 build = isl_ast_build_set_options(build, options);
8362 build = isl_ast_build_set_at_each_domain(build,
8363 &count_domains, &n_domain);
8364 tree = isl_ast_build_node_from_schedule_map(build, schedule);
8365 isl_ast_build_free(build);
8366 if (!tree)
8367 return -1;
8369 isl_ast_node_free(tree);
8371 if (n_domain != 3)
8372 isl_die(ctx, isl_error_unknown,
8373 "unexpected number of for nodes", return -1);
8375 return 0;
8378 /* Check that if the ast_build_exploit_nested_bounds options is set,
8379 * we do not get an outer if node in the generated AST,
8380 * while we do get such an outer if node if the options is not set.
8382 static int test_ast_gen4(isl_ctx *ctx)
8384 const char *str;
8385 isl_set *set;
8386 isl_union_map *schedule;
8387 isl_ast_build *build;
8388 isl_ast_node *tree;
8389 enum isl_ast_node_type type;
8390 int enb;
8392 enb = isl_options_get_ast_build_exploit_nested_bounds(ctx);
8393 str = "[N,M] -> { A[i,j] -> [i,j] : 0 <= i <= N and 0 <= j <= M }";
8395 isl_options_set_ast_build_exploit_nested_bounds(ctx, 1);
8397 schedule = isl_union_map_read_from_str(ctx, str);
8398 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
8399 build = isl_ast_build_from_context(set);
8400 tree = isl_ast_build_node_from_schedule_map(build, schedule);
8401 isl_ast_build_free(build);
8402 if (!tree)
8403 return -1;
8405 type = isl_ast_node_get_type(tree);
8406 isl_ast_node_free(tree);
8408 if (type == isl_ast_node_if)
8409 isl_die(ctx, isl_error_unknown,
8410 "not expecting if node", return -1);
8412 isl_options_set_ast_build_exploit_nested_bounds(ctx, 0);
8414 schedule = isl_union_map_read_from_str(ctx, str);
8415 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
8416 build = isl_ast_build_from_context(set);
8417 tree = isl_ast_build_node_from_schedule_map(build, schedule);
8418 isl_ast_build_free(build);
8419 if (!tree)
8420 return -1;
8422 type = isl_ast_node_get_type(tree);
8423 isl_ast_node_free(tree);
8425 if (type != isl_ast_node_if)
8426 isl_die(ctx, isl_error_unknown,
8427 "expecting if node", return -1);
8429 isl_options_set_ast_build_exploit_nested_bounds(ctx, enb);
8431 return 0;
8434 /* This function is called for each leaf in the AST generated
8435 * from test_ast_gen5.
8437 * We finalize the AST generation by extending the outer schedule
8438 * with a zero-dimensional schedule. If this results in any for loops,
8439 * then this means that we did not pass along enough information
8440 * about the outer schedule to the inner AST generation.
8442 static __isl_give isl_ast_node *create_leaf(__isl_take isl_ast_build *build,
8443 void *user)
8445 isl_union_map *schedule, *extra;
8446 isl_ast_node *tree;
8448 schedule = isl_ast_build_get_schedule(build);
8449 extra = isl_union_map_copy(schedule);
8450 extra = isl_union_map_from_domain(isl_union_map_domain(extra));
8451 schedule = isl_union_map_range_product(schedule, extra);
8452 tree = isl_ast_build_node_from_schedule_map(build, schedule);
8453 isl_ast_build_free(build);
8455 if (!tree)
8456 return NULL;
8458 if (isl_ast_node_get_type(tree) == isl_ast_node_for)
8459 isl_die(isl_ast_node_get_ctx(tree), isl_error_unknown,
8460 "code should not contain any for loop",
8461 return isl_ast_node_free(tree));
8463 return tree;
8466 /* Check that we do not lose any information when going back and
8467 * forth between internal and external schedule.
8469 * In particular, we create an AST where we unroll the only
8470 * non-constant dimension in the schedule. We therefore do
8471 * not expect any for loops in the AST. However, older versions
8472 * of isl would not pass along enough information about the outer
8473 * schedule when performing an inner code generation from a create_leaf
8474 * callback, resulting in the inner code generation producing a for loop.
8476 static int test_ast_gen5(isl_ctx *ctx)
8478 const char *str;
8479 isl_set *set;
8480 isl_union_map *schedule, *options;
8481 isl_ast_build *build;
8482 isl_ast_node *tree;
8484 str = "{ A[] -> [1, 1, 2]; B[i] -> [1, i, 0] : i >= 1 and i <= 2 }";
8485 schedule = isl_union_map_read_from_str(ctx, str);
8487 str = "{ [a, b, c] -> unroll[1] : exists (e0 = [(a)/4]: "
8488 "4e0 >= -1 + a - b and 4e0 <= -2 + a + b) }";
8489 options = isl_union_map_read_from_str(ctx, str);
8491 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
8492 build = isl_ast_build_from_context(set);
8493 build = isl_ast_build_set_options(build, options);
8494 build = isl_ast_build_set_create_leaf(build, &create_leaf, NULL);
8495 tree = isl_ast_build_node_from_schedule_map(build, schedule);
8496 isl_ast_build_free(build);
8497 isl_ast_node_free(tree);
8498 if (!tree)
8499 return -1;
8501 return 0;
8504 /* Check that the expression
8506 * [n] -> { [n/2] : n <= 0 and n % 2 = 0; [0] : n > 0 }
8508 * is not combined into
8510 * min(n/2, 0)
8512 * as this would result in n/2 being evaluated in parts of
8513 * the definition domain where n is not a multiple of 2.
8515 static int test_ast_expr(isl_ctx *ctx)
8517 const char *str;
8518 isl_pw_aff *pa;
8519 isl_ast_build *build;
8520 isl_ast_expr *expr;
8521 int min_max;
8522 int is_min;
8524 min_max = isl_options_get_ast_build_detect_min_max(ctx);
8525 isl_options_set_ast_build_detect_min_max(ctx, 1);
8527 str = "[n] -> { [n/2] : n <= 0 and n % 2 = 0; [0] : n > 0 }";
8528 pa = isl_pw_aff_read_from_str(ctx, str);
8529 build = isl_ast_build_alloc(ctx);
8530 expr = isl_ast_build_expr_from_pw_aff(build, pa);
8531 is_min = isl_ast_expr_get_type(expr) == isl_ast_expr_op &&
8532 isl_ast_expr_get_op_type(expr) == isl_ast_expr_op_min;
8533 isl_ast_build_free(build);
8534 isl_ast_expr_free(expr);
8536 isl_options_set_ast_build_detect_min_max(ctx, min_max);
8538 if (!expr)
8539 return -1;
8540 if (is_min)
8541 isl_die(ctx, isl_error_unknown,
8542 "expressions should not be combined", return -1);
8544 return 0;
8547 static int test_ast_gen(isl_ctx *ctx)
8549 if (test_ast_gen1(ctx) < 0)
8550 return -1;
8551 if (test_ast_gen2(ctx) < 0)
8552 return -1;
8553 if (test_ast_gen3(ctx) < 0)
8554 return -1;
8555 if (test_ast_gen4(ctx) < 0)
8556 return -1;
8557 if (test_ast_gen5(ctx) < 0)
8558 return -1;
8559 if (test_ast_expr(ctx) < 0)
8560 return -1;
8561 return 0;
8564 /* Check if dropping output dimensions from an isl_pw_multi_aff
8565 * works properly.
8567 static int test_pw_multi_aff(isl_ctx *ctx)
8569 const char *str;
8570 isl_pw_multi_aff *pma1, *pma2;
8571 int equal;
8573 str = "{ [i,j] -> [i+j, 4i-j] }";
8574 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
8575 str = "{ [i,j] -> [4i-j] }";
8576 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
8578 pma1 = isl_pw_multi_aff_drop_dims(pma1, isl_dim_out, 0, 1);
8580 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
8582 isl_pw_multi_aff_free(pma1);
8583 isl_pw_multi_aff_free(pma2);
8584 if (equal < 0)
8585 return -1;
8586 if (!equal)
8587 isl_die(ctx, isl_error_unknown,
8588 "expressions not equal", return -1);
8590 return 0;
8593 /* Check that we can properly parse multi piecewise affine expressions
8594 * where the piecewise affine expressions have different domains.
8596 static int test_multi_pw_aff_1(isl_ctx *ctx)
8598 const char *str;
8599 isl_set *dom, *dom2;
8600 isl_multi_pw_aff *mpa1, *mpa2;
8601 isl_pw_aff *pa;
8602 int equal;
8603 int equal_domain;
8605 mpa1 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [i] }");
8606 dom = isl_set_read_from_str(ctx, "{ [i] : i > 0 }");
8607 mpa1 = isl_multi_pw_aff_intersect_domain(mpa1, dom);
8608 mpa2 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [2i] }");
8609 mpa2 = isl_multi_pw_aff_flat_range_product(mpa1, mpa2);
8610 str = "{ [i] -> [(i : i > 0), 2i] }";
8611 mpa1 = isl_multi_pw_aff_read_from_str(ctx, str);
8613 equal = isl_multi_pw_aff_plain_is_equal(mpa1, mpa2);
8615 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 0);
8616 dom = isl_pw_aff_domain(pa);
8617 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 1);
8618 dom2 = isl_pw_aff_domain(pa);
8619 equal_domain = isl_set_is_equal(dom, dom2);
8621 isl_set_free(dom);
8622 isl_set_free(dom2);
8623 isl_multi_pw_aff_free(mpa1);
8624 isl_multi_pw_aff_free(mpa2);
8626 if (equal < 0)
8627 return -1;
8628 if (!equal)
8629 isl_die(ctx, isl_error_unknown,
8630 "expressions not equal", return -1);
8632 if (equal_domain < 0)
8633 return -1;
8634 if (equal_domain)
8635 isl_die(ctx, isl_error_unknown,
8636 "domains unexpectedly equal", return -1);
8638 return 0;
8641 /* Check that the dimensions in the explicit domain
8642 * of a multi piecewise affine expression are properly
8643 * taken into account.
8645 static int test_multi_pw_aff_2(isl_ctx *ctx)
8647 const char *str;
8648 isl_bool involves1, involves2, involves3, equal;
8649 isl_multi_pw_aff *mpa, *mpa1, *mpa2;
8651 str = "{ A[x,y] -> B[] : x >= y }";
8652 mpa = isl_multi_pw_aff_read_from_str(ctx, str);
8653 involves1 = isl_multi_pw_aff_involves_dims(mpa, isl_dim_in, 0, 2);
8654 mpa1 = isl_multi_pw_aff_copy(mpa);
8656 mpa = isl_multi_pw_aff_insert_dims(mpa, isl_dim_in, 0, 1);
8657 involves2 = isl_multi_pw_aff_involves_dims(mpa, isl_dim_in, 0, 1);
8658 involves3 = isl_multi_pw_aff_involves_dims(mpa, isl_dim_in, 1, 2);
8659 str = "{ [a,x,y] -> B[] : x >= y }";
8660 mpa2 = isl_multi_pw_aff_read_from_str(ctx, str);
8661 equal = isl_multi_pw_aff_plain_is_equal(mpa, mpa2);
8662 isl_multi_pw_aff_free(mpa2);
8664 mpa = isl_multi_pw_aff_drop_dims(mpa, isl_dim_in, 0, 1);
8665 mpa = isl_multi_pw_aff_set_tuple_name(mpa, isl_dim_in, "A");
8666 if (equal >= 0 && equal)
8667 equal = isl_multi_pw_aff_plain_is_equal(mpa, mpa1);
8668 isl_multi_pw_aff_free(mpa1);
8669 isl_multi_pw_aff_free(mpa);
8671 if (involves1 < 0 || involves2 < 0 || involves3 < 0 || equal < 0)
8672 return -1;
8673 if (!equal)
8674 isl_die(ctx, isl_error_unknown,
8675 "incorrect result of dimension insertion/removal",
8676 return isl_stat_error);
8677 if (!involves1 || involves2 || !involves3)
8678 isl_die(ctx, isl_error_unknown,
8679 "incorrect characterization of involved dimensions",
8680 return isl_stat_error);
8682 return 0;
8685 /* Check that isl_multi_union_pw_aff_multi_val_on_domain
8686 * sets the explicit domain of a zero-dimensional result,
8687 * such that it can be converted to an isl_union_map.
8689 static isl_stat test_multi_pw_aff_3(isl_ctx *ctx)
8691 isl_space *space;
8692 isl_union_set *dom;
8693 isl_multi_val *mv;
8694 isl_multi_union_pw_aff *mupa;
8695 isl_union_map *umap;
8697 dom = isl_union_set_read_from_str(ctx, "{ A[]; B[] }");
8698 space = isl_union_set_get_space(dom);
8699 mv = isl_multi_val_zero(isl_space_set_from_params(space));
8700 mupa = isl_multi_union_pw_aff_multi_val_on_domain(dom, mv);
8701 umap = isl_union_map_from_multi_union_pw_aff(mupa);
8702 isl_union_map_free(umap);
8703 if (!umap)
8704 return isl_stat_error;
8706 return isl_stat_ok;
8709 /* Perform some tests on multi piecewise affine expressions.
8711 static int test_multi_pw_aff(isl_ctx *ctx)
8713 if (test_multi_pw_aff_1(ctx) < 0)
8714 return -1;
8715 if (test_multi_pw_aff_2(ctx) < 0)
8716 return -1;
8717 if (test_multi_pw_aff_3(ctx) < 0)
8718 return -1;
8719 return 0;
8722 /* This is a regression test for a bug where isl_basic_map_simplify
8723 * would end up in an infinite loop. In particular, we construct
8724 * an empty basic set that is not obviously empty.
8725 * isl_basic_set_is_empty marks the basic set as empty.
8726 * After projecting out i3, the variable can be dropped completely,
8727 * but isl_basic_map_simplify refrains from doing so if the basic set
8728 * is empty and would end up in an infinite loop if it didn't test
8729 * explicitly for empty basic maps in the outer loop.
8731 static int test_simplify_1(isl_ctx *ctx)
8733 const char *str;
8734 isl_basic_set *bset;
8735 int empty;
8737 str = "{ [i0, i1, i2, i3] : i0 >= -2 and 6i2 <= 4 + i0 + 5i1 and "
8738 "i2 <= 22 and 75i2 <= 111 + 13i0 + 60i1 and "
8739 "25i2 >= 38 + 6i0 + 20i1 and i0 <= -1 and i2 >= 20 and "
8740 "i3 >= i2 }";
8741 bset = isl_basic_set_read_from_str(ctx, str);
8742 empty = isl_basic_set_is_empty(bset);
8743 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
8744 isl_basic_set_free(bset);
8745 if (!bset)
8746 return -1;
8747 if (!empty)
8748 isl_die(ctx, isl_error_unknown,
8749 "basic set should be empty", return -1);
8751 return 0;
8754 /* Check that the equality in the set description below
8755 * is simplified away.
8757 static int test_simplify_2(isl_ctx *ctx)
8759 const char *str;
8760 isl_basic_set *bset;
8761 isl_bool universe;
8763 str = "{ [a] : exists e0, e1: 32e1 = 31 + 31a + 31e0 }";
8764 bset = isl_basic_set_read_from_str(ctx, str);
8765 universe = isl_basic_set_plain_is_universe(bset);
8766 isl_basic_set_free(bset);
8768 if (universe < 0)
8769 return -1;
8770 if (!universe)
8771 isl_die(ctx, isl_error_unknown,
8772 "equality not simplified away", return -1);
8773 return 0;
8776 /* Some simplification tests.
8778 static int test_simplify(isl_ctx *ctx)
8780 if (test_simplify_1(ctx) < 0)
8781 return -1;
8782 if (test_simplify_2(ctx) < 0)
8783 return -1;
8784 return 0;
8787 /* This is a regression test for a bug where isl_tab_basic_map_partial_lexopt
8788 * with gbr context would fail to disable the use of the shifted tableau
8789 * when transferring equalities for the input to the context, resulting
8790 * in invalid sample values.
8792 static int test_partial_lexmin(isl_ctx *ctx)
8794 const char *str;
8795 isl_basic_set *bset;
8796 isl_basic_map *bmap;
8797 isl_map *map;
8799 str = "{ [1, b, c, 1 - c] -> [e] : 2e <= -c and 2e >= -3 + c }";
8800 bmap = isl_basic_map_read_from_str(ctx, str);
8801 str = "{ [a, b, c, d] : c <= 1 and 2d >= 6 - 4b - c }";
8802 bset = isl_basic_set_read_from_str(ctx, str);
8803 map = isl_basic_map_partial_lexmin(bmap, bset, NULL);
8804 isl_map_free(map);
8806 if (!map)
8807 return -1;
8809 return 0;
8812 /* Check that the variable compression performed on the existentially
8813 * quantified variables inside isl_basic_set_compute_divs is not confused
8814 * by the implicit equalities among the parameters.
8816 static int test_compute_divs(isl_ctx *ctx)
8818 const char *str;
8819 isl_basic_set *bset;
8820 isl_set *set;
8822 str = "[a, b, c, d, e] -> { [] : exists (e0: 2d = b and a <= 124 and "
8823 "b <= 2046 and b >= 0 and b <= 60 + 64a and 2e >= b + 2c and "
8824 "2e >= b and 2e <= 1 + b and 2e <= 1 + b + 2c and "
8825 "32768e0 >= -124 + a and 2097152e0 <= 60 + 64a - b) }";
8826 bset = isl_basic_set_read_from_str(ctx, str);
8827 set = isl_basic_set_compute_divs(bset);
8828 isl_set_free(set);
8829 if (!set)
8830 return -1;
8832 return 0;
8835 /* Check that isl_schedule_get_map is not confused by a schedule tree
8836 * with divergent filter node parameters, as can result from a call
8837 * to isl_schedule_intersect_domain.
8839 static int test_schedule_tree(isl_ctx *ctx)
8841 const char *str;
8842 isl_union_set *uset;
8843 isl_schedule *sched1, *sched2;
8844 isl_union_map *umap;
8846 uset = isl_union_set_read_from_str(ctx, "{ A[i] }");
8847 sched1 = isl_schedule_from_domain(uset);
8848 uset = isl_union_set_read_from_str(ctx, "{ B[] }");
8849 sched2 = isl_schedule_from_domain(uset);
8851 sched1 = isl_schedule_sequence(sched1, sched2);
8852 str = "[n] -> { A[i] : 0 <= i < n; B[] }";
8853 uset = isl_union_set_read_from_str(ctx, str);
8854 sched1 = isl_schedule_intersect_domain(sched1, uset);
8855 umap = isl_schedule_get_map(sched1);
8856 isl_schedule_free(sched1);
8857 isl_union_map_free(umap);
8858 if (!umap)
8859 return -1;
8861 return 0;
8864 /* Check that a zero-dimensional prefix schedule keeps track
8865 * of the domain and outer filters.
8867 static int test_schedule_tree_prefix(isl_ctx *ctx)
8869 const char *str;
8870 isl_bool equal;
8871 isl_union_set *uset;
8872 isl_union_set_list *filters;
8873 isl_multi_union_pw_aff *mupa, *mupa2;
8874 isl_schedule_node *node;
8876 str = "{ S1[i,j] : 0 <= i,j < 10; S2[i,j] : 0 <= i,j < 10 }";
8877 uset = isl_union_set_read_from_str(ctx, str);
8878 node = isl_schedule_node_from_domain(uset);
8879 node = isl_schedule_node_child(node, 0);
8881 str = "{ S1[i,j] : i > j }";
8882 uset = isl_union_set_read_from_str(ctx, str);
8883 filters = isl_union_set_list_from_union_set(uset);
8884 str = "{ S1[i,j] : i <= j; S2[i,j] }";
8885 uset = isl_union_set_read_from_str(ctx, str);
8886 filters = isl_union_set_list_add(filters, uset);
8887 node = isl_schedule_node_insert_sequence(node, filters);
8889 node = isl_schedule_node_child(node, 0);
8890 node = isl_schedule_node_child(node, 0);
8891 mupa = isl_schedule_node_get_prefix_schedule_multi_union_pw_aff(node);
8892 str = "([] : { S1[i,j] : i > j })";
8893 mupa2 = isl_multi_union_pw_aff_read_from_str(ctx, str);
8894 equal = isl_multi_union_pw_aff_plain_is_equal(mupa, mupa2);
8895 isl_multi_union_pw_aff_free(mupa2);
8896 isl_multi_union_pw_aff_free(mupa);
8897 isl_schedule_node_free(node);
8899 if (equal < 0)
8900 return -1;
8901 if (!equal)
8902 isl_die(ctx, isl_error_unknown, "unexpected prefix schedule",
8903 return -1);
8905 return 0;
8908 /* Check that the reaching domain elements and the prefix schedule
8909 * at a leaf node are the same before and after grouping.
8911 static int test_schedule_tree_group_1(isl_ctx *ctx)
8913 int equal;
8914 const char *str;
8915 isl_id *id;
8916 isl_union_set *uset;
8917 isl_multi_union_pw_aff *mupa;
8918 isl_union_pw_multi_aff *upma1, *upma2;
8919 isl_union_set *domain1, *domain2;
8920 isl_union_map *umap1, *umap2;
8921 isl_schedule_node *node;
8923 str = "{ S1[i,j] : 0 <= i,j < 10; S2[i,j] : 0 <= i,j < 10 }";
8924 uset = isl_union_set_read_from_str(ctx, str);
8925 node = isl_schedule_node_from_domain(uset);
8926 node = isl_schedule_node_child(node, 0);
8927 str = "[{ S1[i,j] -> [i]; S2[i,j] -> [9 - i] }]";
8928 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
8929 node = isl_schedule_node_insert_partial_schedule(node, mupa);
8930 node = isl_schedule_node_child(node, 0);
8931 str = "[{ S1[i,j] -> [j]; S2[i,j] -> [j] }]";
8932 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
8933 node = isl_schedule_node_insert_partial_schedule(node, mupa);
8934 node = isl_schedule_node_child(node, 0);
8935 umap1 = isl_schedule_node_get_prefix_schedule_union_map(node);
8936 upma1 = isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(node);
8937 domain1 = isl_schedule_node_get_domain(node);
8938 id = isl_id_alloc(ctx, "group", NULL);
8939 node = isl_schedule_node_parent(node);
8940 node = isl_schedule_node_group(node, id);
8941 node = isl_schedule_node_child(node, 0);
8942 umap2 = isl_schedule_node_get_prefix_schedule_union_map(node);
8943 upma2 = isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(node);
8944 domain2 = isl_schedule_node_get_domain(node);
8945 equal = isl_union_pw_multi_aff_plain_is_equal(upma1, upma2);
8946 if (equal >= 0 && equal)
8947 equal = isl_union_set_is_equal(domain1, domain2);
8948 if (equal >= 0 && equal)
8949 equal = isl_union_map_is_equal(umap1, umap2);
8950 isl_union_map_free(umap1);
8951 isl_union_map_free(umap2);
8952 isl_union_set_free(domain1);
8953 isl_union_set_free(domain2);
8954 isl_union_pw_multi_aff_free(upma1);
8955 isl_union_pw_multi_aff_free(upma2);
8956 isl_schedule_node_free(node);
8958 if (equal < 0)
8959 return -1;
8960 if (!equal)
8961 isl_die(ctx, isl_error_unknown,
8962 "expressions not equal", return -1);
8964 return 0;
8967 /* Check that we can have nested groupings and that the union map
8968 * schedule representation is the same before and after the grouping.
8969 * Note that after the grouping, the union map representation contains
8970 * the domain constraints from the ranges of the expansion nodes,
8971 * while they are missing from the union map representation of
8972 * the tree without expansion nodes.
8974 * Also check that the global expansion is as expected.
8976 static int test_schedule_tree_group_2(isl_ctx *ctx)
8978 int equal, equal_expansion;
8979 const char *str;
8980 isl_id *id;
8981 isl_union_set *uset;
8982 isl_union_map *umap1, *umap2;
8983 isl_union_map *expansion1, *expansion2;
8984 isl_union_set_list *filters;
8985 isl_multi_union_pw_aff *mupa;
8986 isl_schedule *schedule;
8987 isl_schedule_node *node;
8989 str = "{ S1[i,j] : 0 <= i,j < 10; S2[i,j] : 0 <= i,j < 10; "
8990 "S3[i,j] : 0 <= i,j < 10 }";
8991 uset = isl_union_set_read_from_str(ctx, str);
8992 node = isl_schedule_node_from_domain(uset);
8993 node = isl_schedule_node_child(node, 0);
8994 str = "[{ S1[i,j] -> [i]; S2[i,j] -> [i]; S3[i,j] -> [i] }]";
8995 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
8996 node = isl_schedule_node_insert_partial_schedule(node, mupa);
8997 node = isl_schedule_node_child(node, 0);
8998 str = "{ S1[i,j] }";
8999 uset = isl_union_set_read_from_str(ctx, str);
9000 filters = isl_union_set_list_from_union_set(uset);
9001 str = "{ S2[i,j]; S3[i,j] }";
9002 uset = isl_union_set_read_from_str(ctx, str);
9003 filters = isl_union_set_list_add(filters, uset);
9004 node = isl_schedule_node_insert_sequence(node, filters);
9005 node = isl_schedule_node_child(node, 1);
9006 node = isl_schedule_node_child(node, 0);
9007 str = "{ S2[i,j] }";
9008 uset = isl_union_set_read_from_str(ctx, str);
9009 filters = isl_union_set_list_from_union_set(uset);
9010 str = "{ S3[i,j] }";
9011 uset = isl_union_set_read_from_str(ctx, str);
9012 filters = isl_union_set_list_add(filters, uset);
9013 node = isl_schedule_node_insert_sequence(node, filters);
9015 schedule = isl_schedule_node_get_schedule(node);
9016 umap1 = isl_schedule_get_map(schedule);
9017 uset = isl_schedule_get_domain(schedule);
9018 umap1 = isl_union_map_intersect_domain(umap1, uset);
9019 isl_schedule_free(schedule);
9021 node = isl_schedule_node_parent(node);
9022 node = isl_schedule_node_parent(node);
9023 id = isl_id_alloc(ctx, "group1", NULL);
9024 node = isl_schedule_node_group(node, id);
9025 node = isl_schedule_node_child(node, 1);
9026 node = isl_schedule_node_child(node, 0);
9027 id = isl_id_alloc(ctx, "group2", NULL);
9028 node = isl_schedule_node_group(node, id);
9030 schedule = isl_schedule_node_get_schedule(node);
9031 umap2 = isl_schedule_get_map(schedule);
9032 isl_schedule_free(schedule);
9034 node = isl_schedule_node_root(node);
9035 node = isl_schedule_node_child(node, 0);
9036 expansion1 = isl_schedule_node_get_subtree_expansion(node);
9037 isl_schedule_node_free(node);
9039 str = "{ group1[i] -> S1[i,j] : 0 <= i,j < 10; "
9040 "group1[i] -> S2[i,j] : 0 <= i,j < 10; "
9041 "group1[i] -> S3[i,j] : 0 <= i,j < 10 }";
9043 expansion2 = isl_union_map_read_from_str(ctx, str);
9045 equal = isl_union_map_is_equal(umap1, umap2);
9046 equal_expansion = isl_union_map_is_equal(expansion1, expansion2);
9048 isl_union_map_free(umap1);
9049 isl_union_map_free(umap2);
9050 isl_union_map_free(expansion1);
9051 isl_union_map_free(expansion2);
9053 if (equal < 0 || equal_expansion < 0)
9054 return -1;
9055 if (!equal)
9056 isl_die(ctx, isl_error_unknown,
9057 "expressions not equal", return -1);
9058 if (!equal_expansion)
9059 isl_die(ctx, isl_error_unknown,
9060 "unexpected expansion", return -1);
9062 return 0;
9065 /* Some tests for the isl_schedule_node_group function.
9067 static int test_schedule_tree_group(isl_ctx *ctx)
9069 if (test_schedule_tree_group_1(ctx) < 0)
9070 return -1;
9071 if (test_schedule_tree_group_2(ctx) < 0)
9072 return -1;
9073 return 0;
9076 struct {
9077 const char *set;
9078 const char *dual;
9079 } coef_tests[] = {
9080 { "{ rat: [i] : 0 <= i <= 10 }",
9081 "{ rat: coefficients[[cst] -> [a]] : cst >= 0 and 10a + cst >= 0 }" },
9082 { "{ rat: [i] : FALSE }",
9083 "{ rat: coefficients[[cst] -> [a]] }" },
9084 { "{ rat: [i] : }",
9085 "{ rat: coefficients[[cst] -> [0]] : cst >= 0 }" },
9088 struct {
9089 const char *set;
9090 const char *dual;
9091 } sol_tests[] = {
9092 { "{ rat: coefficients[[cst] -> [a]] : cst >= 0 and 10a + cst >= 0 }",
9093 "{ rat: [i] : 0 <= i <= 10 }" },
9094 { "{ rat: coefficients[[cst] -> [a]] : FALSE }",
9095 "{ rat: [i] }" },
9096 { "{ rat: coefficients[[cst] -> [a]] }",
9097 "{ rat: [i] : FALSE }" },
9100 /* Test the basic functionality of isl_basic_set_coefficients and
9101 * isl_basic_set_solutions.
9103 static int test_dual(isl_ctx *ctx)
9105 int i;
9107 for (i = 0; i < ARRAY_SIZE(coef_tests); ++i) {
9108 int equal;
9109 isl_basic_set *bset1, *bset2;
9111 bset1 = isl_basic_set_read_from_str(ctx, coef_tests[i].set);
9112 bset2 = isl_basic_set_read_from_str(ctx, coef_tests[i].dual);
9113 bset1 = isl_basic_set_coefficients(bset1);
9114 equal = isl_basic_set_is_equal(bset1, bset2);
9115 isl_basic_set_free(bset1);
9116 isl_basic_set_free(bset2);
9117 if (equal < 0)
9118 return -1;
9119 if (!equal)
9120 isl_die(ctx, isl_error_unknown,
9121 "incorrect dual", return -1);
9124 for (i = 0; i < ARRAY_SIZE(sol_tests); ++i) {
9125 int equal;
9126 isl_basic_set *bset1, *bset2;
9128 bset1 = isl_basic_set_read_from_str(ctx, sol_tests[i].set);
9129 bset2 = isl_basic_set_read_from_str(ctx, sol_tests[i].dual);
9130 bset1 = isl_basic_set_solutions(bset1);
9131 equal = isl_basic_set_is_equal(bset1, bset2);
9132 isl_basic_set_free(bset1);
9133 isl_basic_set_free(bset2);
9134 if (equal < 0)
9135 return -1;
9136 if (!equal)
9137 isl_die(ctx, isl_error_unknown,
9138 "incorrect dual", return -1);
9141 return 0;
9144 struct {
9145 int scale_tile;
9146 int shift_point;
9147 const char *domain;
9148 const char *schedule;
9149 const char *sizes;
9150 const char *tile;
9151 const char *point;
9152 } tile_tests[] = {
9153 { 0, 0, "[n] -> { S[i,j] : 0 <= i,j < n }",
9154 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
9155 "{ [32,32] }",
9156 "[{ S[i,j] -> [floor(i/32)] }, { S[i,j] -> [floor(j/32)] }]",
9157 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
9159 { 1, 0, "[n] -> { S[i,j] : 0 <= i,j < n }",
9160 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
9161 "{ [32,32] }",
9162 "[{ S[i,j] -> [32*floor(i/32)] }, { S[i,j] -> [32*floor(j/32)] }]",
9163 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
9165 { 0, 1, "[n] -> { S[i,j] : 0 <= i,j < n }",
9166 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
9167 "{ [32,32] }",
9168 "[{ S[i,j] -> [floor(i/32)] }, { S[i,j] -> [floor(j/32)] }]",
9169 "[{ S[i,j] -> [i%32] }, { S[i,j] -> [j%32] }]",
9171 { 1, 1, "[n] -> { S[i,j] : 0 <= i,j < n }",
9172 "[{ S[i,j] -> [i] }, { S[i,j] -> [j] }]",
9173 "{ [32,32] }",
9174 "[{ S[i,j] -> [32*floor(i/32)] }, { S[i,j] -> [32*floor(j/32)] }]",
9175 "[{ S[i,j] -> [i%32] }, { S[i,j] -> [j%32] }]",
9179 /* Basic tiling tests. Create a schedule tree with a domain and a band node,
9180 * tile the band and then check if the tile and point bands have the
9181 * expected partial schedule.
9183 static int test_tile(isl_ctx *ctx)
9185 int i;
9186 int scale;
9187 int shift;
9189 scale = isl_options_get_tile_scale_tile_loops(ctx);
9190 shift = isl_options_get_tile_shift_point_loops(ctx);
9192 for (i = 0; i < ARRAY_SIZE(tile_tests); ++i) {
9193 int opt;
9194 int equal;
9195 const char *str;
9196 isl_union_set *domain;
9197 isl_multi_union_pw_aff *mupa, *mupa2;
9198 isl_schedule_node *node;
9199 isl_multi_val *sizes;
9201 opt = tile_tests[i].scale_tile;
9202 isl_options_set_tile_scale_tile_loops(ctx, opt);
9203 opt = tile_tests[i].shift_point;
9204 isl_options_set_tile_shift_point_loops(ctx, opt);
9206 str = tile_tests[i].domain;
9207 domain = isl_union_set_read_from_str(ctx, str);
9208 node = isl_schedule_node_from_domain(domain);
9209 node = isl_schedule_node_child(node, 0);
9210 str = tile_tests[i].schedule;
9211 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
9212 node = isl_schedule_node_insert_partial_schedule(node, mupa);
9213 str = tile_tests[i].sizes;
9214 sizes = isl_multi_val_read_from_str(ctx, str);
9215 node = isl_schedule_node_band_tile(node, sizes);
9217 str = tile_tests[i].tile;
9218 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
9219 mupa2 = isl_schedule_node_band_get_partial_schedule(node);
9220 equal = isl_multi_union_pw_aff_plain_is_equal(mupa, mupa2);
9221 isl_multi_union_pw_aff_free(mupa);
9222 isl_multi_union_pw_aff_free(mupa2);
9224 node = isl_schedule_node_child(node, 0);
9226 str = tile_tests[i].point;
9227 mupa = isl_multi_union_pw_aff_read_from_str(ctx, str);
9228 mupa2 = isl_schedule_node_band_get_partial_schedule(node);
9229 if (equal >= 0 && equal)
9230 equal = isl_multi_union_pw_aff_plain_is_equal(mupa,
9231 mupa2);
9232 isl_multi_union_pw_aff_free(mupa);
9233 isl_multi_union_pw_aff_free(mupa2);
9235 isl_schedule_node_free(node);
9237 if (equal < 0)
9238 return -1;
9239 if (!equal)
9240 isl_die(ctx, isl_error_unknown,
9241 "unexpected result", return -1);
9244 isl_options_set_tile_scale_tile_loops(ctx, scale);
9245 isl_options_set_tile_shift_point_loops(ctx, shift);
9247 return 0;
9250 /* Check that the domain hash of a space is equal to the hash
9251 * of the domain of the space.
9253 static int test_domain_hash(isl_ctx *ctx)
9255 isl_map *map;
9256 isl_space *space;
9257 uint32_t hash1, hash2;
9259 map = isl_map_read_from_str(ctx, "[n] -> { A[B[x] -> C[]] -> D[] }");
9260 space = isl_map_get_space(map);
9261 isl_map_free(map);
9262 hash1 = isl_space_get_domain_hash(space);
9263 space = isl_space_domain(space);
9264 hash2 = isl_space_get_hash(space);
9265 isl_space_free(space);
9267 if (!space)
9268 return -1;
9269 if (hash1 != hash2)
9270 isl_die(ctx, isl_error_unknown,
9271 "domain hash not equal to hash of domain", return -1);
9273 return 0;
9276 /* Check that a universe basic set that is not obviously equal to the universe
9277 * is still recognized as being equal to the universe.
9279 static int test_universe(isl_ctx *ctx)
9281 const char *s;
9282 isl_basic_set *bset;
9283 isl_bool is_univ;
9285 s = "{ [] : exists x, y : 3y <= 2x and y >= -3 + 2x and 2y >= 2 - x }";
9286 bset = isl_basic_set_read_from_str(ctx, s);
9287 is_univ = isl_basic_set_is_universe(bset);
9288 isl_basic_set_free(bset);
9290 if (is_univ < 0)
9291 return -1;
9292 if (!is_univ)
9293 isl_die(ctx, isl_error_unknown,
9294 "not recognized as universe set", return -1);
9296 return 0;
9299 /* Sets for which chambers are computed and checked.
9301 const char *chambers_tests[] = {
9302 "[A, B, C] -> { [x, y, z] : x >= 0 and y >= 0 and y <= A - x and "
9303 "z >= 0 and z <= C - y and z <= B - x - y }",
9306 /* Add the domain of "cell" to "cells".
9308 static isl_stat add_cell(__isl_take isl_cell *cell, void *user)
9310 isl_basic_set_list **cells = user;
9311 isl_basic_set *dom;
9313 dom = isl_cell_get_domain(cell);
9314 isl_cell_free(cell);
9315 *cells = isl_basic_set_list_add(*cells, dom);
9317 return *cells ? isl_stat_ok : isl_stat_error;
9320 /* Check that the elements of "list" are pairwise disjoint.
9322 static isl_stat check_pairwise_disjoint(__isl_keep isl_basic_set_list *list)
9324 int i, j;
9325 isl_size n;
9327 n = isl_basic_set_list_n_basic_set(list);
9328 if (n < 0)
9329 return isl_stat_error;
9331 for (i = 0; i < n; ++i) {
9332 isl_basic_set *bset_i;
9334 bset_i = isl_basic_set_list_get_basic_set(list, i);
9335 for (j = i + 1; j < n; ++j) {
9336 isl_basic_set *bset_j;
9337 isl_bool disjoint;
9339 bset_j = isl_basic_set_list_get_basic_set(list, j);
9340 disjoint = isl_basic_set_is_disjoint(bset_i, bset_j);
9341 isl_basic_set_free(bset_j);
9342 if (!disjoint)
9343 isl_die(isl_basic_set_list_get_ctx(list),
9344 isl_error_unknown, "not disjoint",
9345 break);
9346 if (disjoint < 0 || !disjoint)
9347 break;
9349 isl_basic_set_free(bset_i);
9350 if (j < n)
9351 return isl_stat_error;
9354 return isl_stat_ok;
9357 /* Check that the chambers computed by isl_vertices_foreach_disjoint_cell
9358 * are pairwise disjoint.
9360 static int test_chambers(isl_ctx *ctx)
9362 int i;
9364 for (i = 0; i < ARRAY_SIZE(chambers_tests); ++i) {
9365 isl_basic_set *bset;
9366 isl_vertices *vertices;
9367 isl_basic_set_list *cells;
9368 isl_stat ok;
9370 bset = isl_basic_set_read_from_str(ctx, chambers_tests[i]);
9371 vertices = isl_basic_set_compute_vertices(bset);
9372 cells = isl_basic_set_list_alloc(ctx, 0);
9373 if (isl_vertices_foreach_disjoint_cell(vertices, &add_cell,
9374 &cells) < 0)
9375 cells = isl_basic_set_list_free(cells);
9376 ok = check_pairwise_disjoint(cells);
9377 isl_basic_set_list_free(cells);
9378 isl_vertices_free(vertices);
9379 isl_basic_set_free(bset);
9381 if (ok < 0)
9382 return -1;
9385 return 0;
9388 struct {
9389 const char *name;
9390 int (*fn)(isl_ctx *ctx);
9391 } tests [] = {
9392 { "universe", &test_universe },
9393 { "domain hash", &test_domain_hash },
9394 { "dual", &test_dual },
9395 { "dependence analysis", &test_flow },
9396 { "val", &test_val },
9397 { "compute divs", &test_compute_divs },
9398 { "partial lexmin", &test_partial_lexmin },
9399 { "simplify", &test_simplify },
9400 { "curry", &test_curry },
9401 { "piecewise multi affine expressions", &test_pw_multi_aff },
9402 { "multi piecewise affine expressions", &test_multi_pw_aff },
9403 { "conversion", &test_conversion },
9404 { "list", &test_list },
9405 { "align parameters", &test_align_parameters },
9406 { "drop unused parameters", &test_drop_unused_parameters },
9407 { "preimage", &test_preimage },
9408 { "pullback", &test_pullback },
9409 { "AST", &test_ast },
9410 { "AST build", &test_ast_build },
9411 { "AST generation", &test_ast_gen },
9412 { "eliminate", &test_eliminate },
9413 { "deltas_map", &test_deltas_map },
9414 { "residue class", &test_residue_class },
9415 { "div", &test_div },
9416 { "slice", &test_slice },
9417 { "fixed power", &test_fixed_power },
9418 { "sample", &test_sample },
9419 { "output", &test_output },
9420 { "vertices", &test_vertices },
9421 { "chambers", &test_chambers },
9422 { "fixed", &test_fixed },
9423 { "equal", &test_equal },
9424 { "disjoint", &test_disjoint },
9425 { "product", &test_product },
9426 { "dim_max", &test_dim_max },
9427 { "affine", &test_aff },
9428 { "injective", &test_injective },
9429 { "schedule (whole component)", &test_schedule_whole },
9430 { "schedule (incremental)", &test_schedule_incremental },
9431 { "schedule tree", &test_schedule_tree },
9432 { "schedule tree prefix", &test_schedule_tree_prefix },
9433 { "schedule tree grouping", &test_schedule_tree_group },
9434 { "tile", &test_tile },
9435 { "union_pw", &test_union_pw },
9436 { "locus", &test_locus },
9437 { "eval", &test_eval },
9438 { "parse", &test_parse },
9439 { "single-valued", &test_sv },
9440 { "affine hull", &test_affine_hull },
9441 { "simple_hull", &test_simple_hull },
9442 { "coalesce", &test_coalesce },
9443 { "factorize", &test_factorize },
9444 { "subset", &test_subset },
9445 { "subtract", &test_subtract },
9446 { "intersect", &test_intersect },
9447 { "lexmin", &test_lexmin },
9448 { "min", &test_min },
9449 { "gist", &test_gist },
9450 { "piecewise quasi-polynomials", &test_pwqp },
9451 { "lift", &test_lift },
9452 { "bound", &test_bound },
9453 { "get lists", &test_get_list },
9454 { "union", &test_union },
9455 { "split periods", &test_split_periods },
9456 { "lexicographic order", &test_lex },
9457 { "bijectivity", &test_bijective },
9458 { "dataflow analysis", &test_dep },
9459 { "reading", &test_read },
9460 { "bounded", &test_bounded },
9461 { "construction", &test_construction },
9462 { "dimension manipulation", &test_dim },
9463 { "map application", &test_application },
9464 { "convex hull", &test_convex_hull },
9465 { "transitive closure", &test_closure },
9466 { "isl_bool", &test_isl_bool},
9469 int main(int argc, char **argv)
9471 int i;
9472 struct isl_ctx *ctx;
9473 struct isl_options *options;
9475 options = isl_options_new_with_defaults();
9476 assert(options);
9477 argc = isl_options_parse(options, argc, argv, ISL_ARG_ALL);
9479 ctx = isl_ctx_alloc_with_options(&isl_options_args, options);
9480 for (i = 0; i < ARRAY_SIZE(tests); ++i) {
9481 printf("%s\n", tests[i].name);
9482 if (tests[i].fn(ctx) < 0)
9483 goto error;
9485 isl_ctx_free(ctx);
9486 return 0;
9487 error:
9488 isl_ctx_free(ctx);
9489 return -1;