isl_coalesce.c: contains: pass isl_coalesce_info instead of basic map
[isl.git] / isl_test.c
blobfe9cbebb7b576226b03bbd3b0afd239e09b3b4e3
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/set.h>
25 #include <isl/flow.h>
26 #include <isl_constraint_private.h>
27 #include <isl/polynomial.h>
28 #include <isl/union_map.h>
29 #include <isl_factorization.h>
30 #include <isl/schedule.h>
31 #include <isl_options_private.h>
32 #include <isl/vertices.h>
33 #include <isl/ast_build.h>
34 #include <isl/val.h>
35 #include <isl/ilp.h>
36 #include <isl_ast_build_expr.h>
37 #include <isl/options.h>
39 #define ARRAY_SIZE(array) (sizeof(array)/sizeof(*array))
41 static char *srcdir;
43 static char *get_filename(isl_ctx *ctx, const char *name, const char *suffix) {
44 char *filename;
45 int length;
46 char *pattern = "%s/test_inputs/%s.%s";
48 length = strlen(pattern) - 6 + strlen(srcdir) + strlen(name)
49 + strlen(suffix) + 1;
50 filename = isl_alloc_array(ctx, char, length);
52 if (!filename)
53 return NULL;
55 sprintf(filename, pattern, srcdir, name, suffix);
57 return filename;
60 void test_parse_map(isl_ctx *ctx, const char *str)
62 isl_map *map;
64 map = isl_map_read_from_str(ctx, str);
65 assert(map);
66 isl_map_free(map);
69 int test_parse_map_equal(isl_ctx *ctx, const char *str, const char *str2)
71 isl_map *map, *map2;
72 int equal;
74 map = isl_map_read_from_str(ctx, str);
75 map2 = isl_map_read_from_str(ctx, str2);
76 equal = isl_map_is_equal(map, map2);
77 isl_map_free(map);
78 isl_map_free(map2);
80 if (equal < 0)
81 return -1;
82 if (!equal)
83 isl_die(ctx, isl_error_unknown, "maps not equal",
84 return -1);
86 return 0;
89 void test_parse_pwqp(isl_ctx *ctx, const char *str)
91 isl_pw_qpolynomial *pwqp;
93 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
94 assert(pwqp);
95 isl_pw_qpolynomial_free(pwqp);
98 static void test_parse_pwaff(isl_ctx *ctx, const char *str)
100 isl_pw_aff *pwaff;
102 pwaff = isl_pw_aff_read_from_str(ctx, str);
103 assert(pwaff);
104 isl_pw_aff_free(pwaff);
107 int test_parse(struct isl_ctx *ctx)
109 isl_map *map, *map2;
110 const char *str, *str2;
112 str = "{ [i] -> [-i] }";
113 map = isl_map_read_from_str(ctx, str);
114 assert(map);
115 isl_map_free(map);
117 str = "{ A[i] -> L[([i/3])] }";
118 map = isl_map_read_from_str(ctx, str);
119 assert(map);
120 isl_map_free(map);
122 test_parse_map(ctx, "{[[s] -> A[i]] -> [[s+1] -> A[i]]}");
123 test_parse_map(ctx, "{ [p1, y1, y2] -> [2, y1, y2] : "
124 "p1 = 1 && (y1 <= y2 || y2 = 0) }");
126 str = "{ [x,y] : [([x/2]+y)/3] >= 1 }";
127 str2 = "{ [x, y] : 2y >= 6 - x }";
128 if (test_parse_map_equal(ctx, str, str2) < 0)
129 return -1;
131 if (test_parse_map_equal(ctx, "{ [x,y] : x <= min(y, 2*y+3) }",
132 "{ [x,y] : x <= y, 2*y + 3 }") < 0)
133 return -1;
134 str = "{ [x, y] : (y <= x and y >= -3) or (2y <= -3 + x and y <= -4) }";
135 if (test_parse_map_equal(ctx, "{ [x,y] : x >= min(y, 2*y+3) }",
136 str) < 0)
137 return -1;
139 str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
140 map = isl_map_read_from_str(ctx, str);
141 str = "{ [new, old] -> [o0, o1] : "
142 "exists (e0 = [(-1 - new + o0)/2], e1 = [(-1 - old + o1)/2]: "
143 "2e0 = -1 - new + o0 and 2e1 = -1 - old + o1 and o0 >= 0 and "
144 "o0 <= 1 and o1 >= 0 and o1 <= 1) }";
145 map2 = isl_map_read_from_str(ctx, str);
146 assert(isl_map_is_equal(map, map2));
147 isl_map_free(map);
148 isl_map_free(map2);
150 str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
151 map = isl_map_read_from_str(ctx, str);
152 str = "{[new,old] -> [(new+1)%2,(old+1)%2]}";
153 map2 = isl_map_read_from_str(ctx, str);
154 assert(isl_map_is_equal(map, map2));
155 isl_map_free(map);
156 isl_map_free(map2);
158 str = "[n] -> { [c1] : c1>=0 and c1<=floord(n-4,3) }";
159 str2 = "[n] -> { [c1] : c1 >= 0 and 3c1 <= -4 + n }";
160 if (test_parse_map_equal(ctx, str, str2) < 0)
161 return -1;
163 str = "{ [i,j] -> [i] : i < j; [i,j] -> [j] : j <= i }";
164 str2 = "{ [i,j] -> [min(i,j)] }";
165 if (test_parse_map_equal(ctx, str, str2) < 0)
166 return -1;
168 str = "{ [i,j] : i != j }";
169 str2 = "{ [i,j] : i < j or i > j }";
170 if (test_parse_map_equal(ctx, str, str2) < 0)
171 return -1;
173 str = "{ [i,j] : (i+1)*2 >= j }";
174 str2 = "{ [i, j] : j <= 2 + 2i }";
175 if (test_parse_map_equal(ctx, str, str2) < 0)
176 return -1;
178 str = "{ [i] -> [i > 0 ? 4 : 5] }";
179 str2 = "{ [i] -> [5] : i <= 0; [i] -> [4] : i >= 1 }";
180 if (test_parse_map_equal(ctx, str, str2) < 0)
181 return -1;
183 str = "[N=2,M] -> { [i=[(M+N)/4]] }";
184 str2 = "[N, M] -> { [i] : N = 2 and 4i <= 2 + M and 4i >= -1 + M }";
185 if (test_parse_map_equal(ctx, str, str2) < 0)
186 return -1;
188 str = "{ [x] : x >= 0 }";
189 str2 = "{ [x] : x-0 >= 0 }";
190 if (test_parse_map_equal(ctx, str, str2) < 0)
191 return -1;
193 str = "{ [i] : ((i > 10)) }";
194 str2 = "{ [i] : i >= 11 }";
195 if (test_parse_map_equal(ctx, str, str2) < 0)
196 return -1;
198 str = "{ [i] -> [0] }";
199 str2 = "{ [i] -> [0 * i] }";
200 if (test_parse_map_equal(ctx, str, str2) < 0)
201 return -1;
203 test_parse_pwqp(ctx, "{ [i] -> i + [ (i + [i/3])/2 ] }");
204 test_parse_map(ctx, "{ S1[i] -> [([i/10]),i%10] : 0 <= i <= 45 }");
205 test_parse_pwaff(ctx, "{ [i] -> [i + 1] : i > 0; [a] -> [a] : a < 0 }");
206 test_parse_pwqp(ctx, "{ [x] -> ([(x)/2] * [(x)/3]) }");
208 if (test_parse_map_equal(ctx, "{ [a] -> [b] : (not false) }",
209 "{ [a] -> [b] : true }") < 0)
210 return -1;
212 if (test_parse_map_equal(ctx, "{ [i] : i/2 <= 5 }",
213 "{ [i] : i <= 10 }") < 0)
214 return -1;
216 if (test_parse_map_equal(ctx, "{Sym=[n] [i] : i <= n }",
217 "[n] -> { [i] : i <= n }") < 0)
218 return -1;
220 if (test_parse_map_equal(ctx, "{ [*] }", "{ [a] }") < 0)
221 return -1;
223 if (test_parse_map_equal(ctx, "{ [i] : 2*floor(i/2) = i }",
224 "{ [i] : exists a : i = 2 a }") < 0)
225 return -1;
227 if (test_parse_map_equal(ctx, "{ [a] -> [b] : a = 5 implies b = 5 }",
228 "{ [a] -> [b] : a != 5 or b = 5 }") < 0)
229 return -1;
231 if (test_parse_map_equal(ctx, "{ [a] -> [a - 1 : a > 0] }",
232 "{ [a] -> [a - 1] : a > 0 }") < 0)
233 return -1;
234 if (test_parse_map_equal(ctx,
235 "{ [a] -> [a - 1 : a > 0; a : a <= 0] }",
236 "{ [a] -> [a - 1] : a > 0; [a] -> [a] : a <= 0 }") < 0)
237 return -1;
238 if (test_parse_map_equal(ctx,
239 "{ [a] -> [(a) * 2 : a >= 0; 0 : a < 0] }",
240 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }") < 0)
241 return -1;
242 if (test_parse_map_equal(ctx,
243 "{ [a] -> [(a * 2) : a >= 0; 0 : a < 0] }",
244 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }") < 0)
245 return -1;
246 if (test_parse_map_equal(ctx,
247 "{ [a] -> [(a * 2 : a >= 0); 0 : a < 0] }",
248 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }") < 0)
249 return -1;
250 if (test_parse_map_equal(ctx,
251 "{ [a] -> [(a * 2 : a >= 0; 0 : a < 0)] }",
252 "{ [a] -> [2a] : a >= 0; [a] -> [0] : a < 0 }") < 0)
253 return -1;
255 return 0;
258 void test_read(struct isl_ctx *ctx)
260 char *filename;
261 FILE *input;
262 struct isl_basic_set *bset1, *bset2;
263 const char *str = "{[y]: Exists ( alpha : 2alpha = y)}";
265 filename = get_filename(ctx, "set", "omega");
266 assert(filename);
267 input = fopen(filename, "r");
268 assert(input);
270 bset1 = isl_basic_set_read_from_file(ctx, input);
271 bset2 = isl_basic_set_read_from_str(ctx, str);
273 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
275 isl_basic_set_free(bset1);
276 isl_basic_set_free(bset2);
277 free(filename);
279 fclose(input);
282 void test_bounded(struct isl_ctx *ctx)
284 isl_set *set;
285 int bounded;
287 set = isl_set_read_from_str(ctx, "[n] -> {[i] : 0 <= i <= n }");
288 bounded = isl_set_is_bounded(set);
289 assert(bounded);
290 isl_set_free(set);
292 set = isl_set_read_from_str(ctx, "{[n, i] : 0 <= i <= n }");
293 bounded = isl_set_is_bounded(set);
294 assert(!bounded);
295 isl_set_free(set);
297 set = isl_set_read_from_str(ctx, "[n] -> {[i] : i <= n }");
298 bounded = isl_set_is_bounded(set);
299 assert(!bounded);
300 isl_set_free(set);
303 /* Construct the basic set { [i] : 5 <= i <= N } */
304 void test_construction(struct isl_ctx *ctx)
306 isl_int v;
307 isl_space *dim;
308 isl_local_space *ls;
309 struct isl_basic_set *bset;
310 struct isl_constraint *c;
312 isl_int_init(v);
314 dim = isl_space_set_alloc(ctx, 1, 1);
315 bset = isl_basic_set_universe(isl_space_copy(dim));
316 ls = isl_local_space_from_space(dim);
318 c = isl_inequality_alloc(isl_local_space_copy(ls));
319 isl_int_set_si(v, -1);
320 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
321 isl_int_set_si(v, 1);
322 isl_constraint_set_coefficient(c, isl_dim_param, 0, v);
323 bset = isl_basic_set_add_constraint(bset, c);
325 c = isl_inequality_alloc(isl_local_space_copy(ls));
326 isl_int_set_si(v, 1);
327 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
328 isl_int_set_si(v, -5);
329 isl_constraint_set_constant(c, v);
330 bset = isl_basic_set_add_constraint(bset, c);
332 isl_local_space_free(ls);
333 isl_basic_set_free(bset);
335 isl_int_clear(v);
338 void test_dim(struct isl_ctx *ctx)
340 const char *str;
341 isl_map *map1, *map2;
343 map1 = isl_map_read_from_str(ctx,
344 "[n] -> { [i] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
345 map1 = isl_map_add_dims(map1, isl_dim_in, 1);
346 map2 = isl_map_read_from_str(ctx,
347 "[n] -> { [i,k] -> [j] : exists (a = [i/10] : i - 10a <= n ) }");
348 assert(isl_map_is_equal(map1, map2));
349 isl_map_free(map2);
351 map1 = isl_map_project_out(map1, isl_dim_in, 0, 1);
352 map2 = isl_map_read_from_str(ctx, "[n] -> { [i] -> [j] : n >= 0 }");
353 assert(isl_map_is_equal(map1, map2));
355 isl_map_free(map1);
356 isl_map_free(map2);
358 str = "[n] -> { [i] -> [] : exists a : 0 <= i <= n and i = 2 a }";
359 map1 = isl_map_read_from_str(ctx, str);
360 str = "{ [i] -> [j] : exists a : 0 <= i <= j and i = 2 a }";
361 map2 = isl_map_read_from_str(ctx, str);
362 map1 = isl_map_move_dims(map1, isl_dim_out, 0, isl_dim_param, 0, 1);
363 assert(isl_map_is_equal(map1, map2));
365 isl_map_free(map1);
366 isl_map_free(map2);
369 struct {
370 __isl_give isl_val *(*op)(__isl_take isl_val *v);
371 const char *arg;
372 const char *res;
373 } val_un_tests[] = {
374 { &isl_val_neg, "0", "0" },
375 { &isl_val_abs, "0", "0" },
376 { &isl_val_2exp, "0", "1" },
377 { &isl_val_floor, "0", "0" },
378 { &isl_val_ceil, "0", "0" },
379 { &isl_val_neg, "1", "-1" },
380 { &isl_val_neg, "-1", "1" },
381 { &isl_val_neg, "1/2", "-1/2" },
382 { &isl_val_neg, "-1/2", "1/2" },
383 { &isl_val_neg, "infty", "-infty" },
384 { &isl_val_neg, "-infty", "infty" },
385 { &isl_val_neg, "NaN", "NaN" },
386 { &isl_val_abs, "1", "1" },
387 { &isl_val_abs, "-1", "1" },
388 { &isl_val_abs, "1/2", "1/2" },
389 { &isl_val_abs, "-1/2", "1/2" },
390 { &isl_val_abs, "infty", "infty" },
391 { &isl_val_abs, "-infty", "infty" },
392 { &isl_val_abs, "NaN", "NaN" },
393 { &isl_val_floor, "1", "1" },
394 { &isl_val_floor, "-1", "-1" },
395 { &isl_val_floor, "1/2", "0" },
396 { &isl_val_floor, "-1/2", "-1" },
397 { &isl_val_floor, "infty", "infty" },
398 { &isl_val_floor, "-infty", "-infty" },
399 { &isl_val_floor, "NaN", "NaN" },
400 { &isl_val_ceil, "1", "1" },
401 { &isl_val_ceil, "-1", "-1" },
402 { &isl_val_ceil, "1/2", "1" },
403 { &isl_val_ceil, "-1/2", "0" },
404 { &isl_val_ceil, "infty", "infty" },
405 { &isl_val_ceil, "-infty", "-infty" },
406 { &isl_val_ceil, "NaN", "NaN" },
407 { &isl_val_2exp, "-3", "1/8" },
408 { &isl_val_2exp, "-1", "1/2" },
409 { &isl_val_2exp, "1", "2" },
410 { &isl_val_2exp, "2", "4" },
411 { &isl_val_2exp, "3", "8" },
412 { &isl_val_inv, "1", "1" },
413 { &isl_val_inv, "2", "1/2" },
414 { &isl_val_inv, "1/2", "2" },
415 { &isl_val_inv, "-2", "-1/2" },
416 { &isl_val_inv, "-1/2", "-2" },
417 { &isl_val_inv, "0", "NaN" },
418 { &isl_val_inv, "NaN", "NaN" },
419 { &isl_val_inv, "infty", "0" },
420 { &isl_val_inv, "-infty", "0" },
423 /* Perform some basic tests of unary operations on isl_val objects.
425 static int test_un_val(isl_ctx *ctx)
427 int i;
428 isl_val *v, *res;
429 __isl_give isl_val *(*fn)(__isl_take isl_val *v);
430 int ok;
432 for (i = 0; i < ARRAY_SIZE(val_un_tests); ++i) {
433 v = isl_val_read_from_str(ctx, val_un_tests[i].arg);
434 res = isl_val_read_from_str(ctx, val_un_tests[i].res);
435 fn = val_un_tests[i].op;
436 v = fn(v);
437 if (isl_val_is_nan(res))
438 ok = isl_val_is_nan(v);
439 else
440 ok = isl_val_eq(v, res);
441 isl_val_free(v);
442 isl_val_free(res);
443 if (ok < 0)
444 return -1;
445 if (!ok)
446 isl_die(ctx, isl_error_unknown,
447 "unexpected result", return -1);
450 return 0;
453 struct {
454 __isl_give isl_val *(*fn)(__isl_take isl_val *v1,
455 __isl_take isl_val *v2);
456 } val_bin_op[] = {
457 ['+'] = { &isl_val_add },
458 ['-'] = { &isl_val_sub },
459 ['*'] = { &isl_val_mul },
460 ['/'] = { &isl_val_div },
461 ['g'] = { &isl_val_gcd },
462 ['m'] = { &isl_val_min },
463 ['M'] = { &isl_val_max },
466 struct {
467 const char *arg1;
468 unsigned char op;
469 const char *arg2;
470 const char *res;
471 } val_bin_tests[] = {
472 { "0", '+', "0", "0" },
473 { "1", '+', "0", "1" },
474 { "1", '+', "1", "2" },
475 { "1", '-', "1", "0" },
476 { "1", '*', "1", "1" },
477 { "1", '/', "1", "1" },
478 { "2", '*', "3", "6" },
479 { "2", '*', "1/2", "1" },
480 { "2", '*', "1/3", "2/3" },
481 { "2/3", '*', "3/5", "2/5" },
482 { "2/3", '*', "7/5", "14/15" },
483 { "2", '/', "1/2", "4" },
484 { "-2", '/', "-1/2", "4" },
485 { "-2", '/', "1/2", "-4" },
486 { "2", '/', "-1/2", "-4" },
487 { "2", '/', "2", "1" },
488 { "2", '/', "3", "2/3" },
489 { "2/3", '/', "5/3", "2/5" },
490 { "2/3", '/', "5/7", "14/15" },
491 { "0", '/', "0", "NaN" },
492 { "42", '/', "0", "NaN" },
493 { "-42", '/', "0", "NaN" },
494 { "infty", '/', "0", "NaN" },
495 { "-infty", '/', "0", "NaN" },
496 { "NaN", '/', "0", "NaN" },
497 { "0", '/', "NaN", "NaN" },
498 { "42", '/', "NaN", "NaN" },
499 { "-42", '/', "NaN", "NaN" },
500 { "infty", '/', "NaN", "NaN" },
501 { "-infty", '/', "NaN", "NaN" },
502 { "NaN", '/', "NaN", "NaN" },
503 { "0", '/', "infty", "0" },
504 { "42", '/', "infty", "0" },
505 { "-42", '/', "infty", "0" },
506 { "infty", '/', "infty", "NaN" },
507 { "-infty", '/', "infty", "NaN" },
508 { "NaN", '/', "infty", "NaN" },
509 { "0", '/', "-infty", "0" },
510 { "42", '/', "-infty", "0" },
511 { "-42", '/', "-infty", "0" },
512 { "infty", '/', "-infty", "NaN" },
513 { "-infty", '/', "-infty", "NaN" },
514 { "NaN", '/', "-infty", "NaN" },
515 { "1", '-', "1/3", "2/3" },
516 { "1/3", '+', "1/2", "5/6" },
517 { "1/2", '+', "1/2", "1" },
518 { "3/4", '-', "1/4", "1/2" },
519 { "1/2", '-', "1/3", "1/6" },
520 { "infty", '+', "42", "infty" },
521 { "infty", '+', "infty", "infty" },
522 { "42", '+', "infty", "infty" },
523 { "infty", '-', "infty", "NaN" },
524 { "infty", '*', "infty", "infty" },
525 { "infty", '*', "-infty", "-infty" },
526 { "-infty", '*', "infty", "-infty" },
527 { "-infty", '*', "-infty", "infty" },
528 { "0", '*', "infty", "NaN" },
529 { "1", '*', "infty", "infty" },
530 { "infty", '*', "0", "NaN" },
531 { "infty", '*', "42", "infty" },
532 { "42", '-', "infty", "-infty" },
533 { "infty", '+', "-infty", "NaN" },
534 { "4", 'g', "6", "2" },
535 { "5", 'g', "6", "1" },
536 { "42", 'm', "3", "3" },
537 { "42", 'M', "3", "42" },
538 { "3", 'm', "42", "3" },
539 { "3", 'M', "42", "42" },
540 { "42", 'm', "infty", "42" },
541 { "42", 'M', "infty", "infty" },
542 { "42", 'm', "-infty", "-infty" },
543 { "42", 'M', "-infty", "42" },
544 { "42", 'm', "NaN", "NaN" },
545 { "42", 'M', "NaN", "NaN" },
546 { "infty", 'm', "-infty", "-infty" },
547 { "infty", 'M', "-infty", "infty" },
550 /* Perform some basic tests of binary operations on isl_val objects.
552 static int test_bin_val(isl_ctx *ctx)
554 int i;
555 isl_val *v1, *v2, *res;
556 __isl_give isl_val *(*fn)(__isl_take isl_val *v1,
557 __isl_take isl_val *v2);
558 int ok;
560 for (i = 0; i < ARRAY_SIZE(val_bin_tests); ++i) {
561 v1 = isl_val_read_from_str(ctx, val_bin_tests[i].arg1);
562 v2 = isl_val_read_from_str(ctx, val_bin_tests[i].arg2);
563 res = isl_val_read_from_str(ctx, val_bin_tests[i].res);
564 fn = val_bin_op[val_bin_tests[i].op].fn;
565 v1 = fn(v1, v2);
566 if (isl_val_is_nan(res))
567 ok = isl_val_is_nan(v1);
568 else
569 ok = isl_val_eq(v1, res);
570 isl_val_free(v1);
571 isl_val_free(res);
572 if (ok < 0)
573 return -1;
574 if (!ok)
575 isl_die(ctx, isl_error_unknown,
576 "unexpected result", return -1);
579 return 0;
582 /* Perform some basic tests on isl_val objects.
584 static int test_val(isl_ctx *ctx)
586 if (test_un_val(ctx) < 0)
587 return -1;
588 if (test_bin_val(ctx) < 0)
589 return -1;
590 return 0;
593 static int test_div(isl_ctx *ctx)
595 unsigned n;
596 const char *str;
597 int empty;
598 isl_int v;
599 isl_space *dim;
600 isl_set *set;
601 isl_local_space *ls;
602 struct isl_basic_set *bset;
603 struct isl_constraint *c;
605 isl_int_init(v);
607 /* test 1 */
608 dim = isl_space_set_alloc(ctx, 0, 3);
609 bset = isl_basic_set_universe(isl_space_copy(dim));
610 ls = isl_local_space_from_space(dim);
612 c = isl_equality_alloc(isl_local_space_copy(ls));
613 isl_int_set_si(v, -1);
614 isl_constraint_set_constant(c, v);
615 isl_int_set_si(v, 1);
616 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
617 isl_int_set_si(v, 3);
618 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
619 bset = isl_basic_set_add_constraint(bset, c);
621 c = isl_equality_alloc(isl_local_space_copy(ls));
622 isl_int_set_si(v, 1);
623 isl_constraint_set_constant(c, v);
624 isl_int_set_si(v, -1);
625 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
626 isl_int_set_si(v, 3);
627 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
628 bset = isl_basic_set_add_constraint(bset, c);
630 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
632 assert(bset && bset->n_div == 1);
633 isl_local_space_free(ls);
634 isl_basic_set_free(bset);
636 /* test 2 */
637 dim = isl_space_set_alloc(ctx, 0, 3);
638 bset = isl_basic_set_universe(isl_space_copy(dim));
639 ls = isl_local_space_from_space(dim);
641 c = isl_equality_alloc(isl_local_space_copy(ls));
642 isl_int_set_si(v, 1);
643 isl_constraint_set_constant(c, v);
644 isl_int_set_si(v, -1);
645 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
646 isl_int_set_si(v, 3);
647 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
648 bset = isl_basic_set_add_constraint(bset, c);
650 c = isl_equality_alloc(isl_local_space_copy(ls));
651 isl_int_set_si(v, -1);
652 isl_constraint_set_constant(c, v);
653 isl_int_set_si(v, 1);
654 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
655 isl_int_set_si(v, 3);
656 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
657 bset = isl_basic_set_add_constraint(bset, c);
659 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
661 assert(bset && bset->n_div == 1);
662 isl_local_space_free(ls);
663 isl_basic_set_free(bset);
665 /* test 3 */
666 dim = isl_space_set_alloc(ctx, 0, 3);
667 bset = isl_basic_set_universe(isl_space_copy(dim));
668 ls = isl_local_space_from_space(dim);
670 c = isl_equality_alloc(isl_local_space_copy(ls));
671 isl_int_set_si(v, 1);
672 isl_constraint_set_constant(c, v);
673 isl_int_set_si(v, -1);
674 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
675 isl_int_set_si(v, 3);
676 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
677 bset = isl_basic_set_add_constraint(bset, c);
679 c = isl_equality_alloc(isl_local_space_copy(ls));
680 isl_int_set_si(v, -3);
681 isl_constraint_set_constant(c, v);
682 isl_int_set_si(v, 1);
683 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
684 isl_int_set_si(v, 4);
685 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
686 bset = isl_basic_set_add_constraint(bset, c);
688 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
690 assert(bset && bset->n_div == 1);
691 isl_local_space_free(ls);
692 isl_basic_set_free(bset);
694 /* test 4 */
695 dim = isl_space_set_alloc(ctx, 0, 3);
696 bset = isl_basic_set_universe(isl_space_copy(dim));
697 ls = isl_local_space_from_space(dim);
699 c = isl_equality_alloc(isl_local_space_copy(ls));
700 isl_int_set_si(v, 2);
701 isl_constraint_set_constant(c, v);
702 isl_int_set_si(v, -1);
703 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
704 isl_int_set_si(v, 3);
705 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
706 bset = isl_basic_set_add_constraint(bset, c);
708 c = isl_equality_alloc(isl_local_space_copy(ls));
709 isl_int_set_si(v, -1);
710 isl_constraint_set_constant(c, v);
711 isl_int_set_si(v, 1);
712 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
713 isl_int_set_si(v, 6);
714 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
715 bset = isl_basic_set_add_constraint(bset, c);
717 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
719 assert(isl_basic_set_is_empty(bset));
720 isl_local_space_free(ls);
721 isl_basic_set_free(bset);
723 /* test 5 */
724 dim = isl_space_set_alloc(ctx, 0, 3);
725 bset = isl_basic_set_universe(isl_space_copy(dim));
726 ls = isl_local_space_from_space(dim);
728 c = isl_equality_alloc(isl_local_space_copy(ls));
729 isl_int_set_si(v, -1);
730 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
731 isl_int_set_si(v, 3);
732 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
733 bset = isl_basic_set_add_constraint(bset, c);
735 c = isl_equality_alloc(isl_local_space_copy(ls));
736 isl_int_set_si(v, 1);
737 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
738 isl_int_set_si(v, -3);
739 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
740 bset = isl_basic_set_add_constraint(bset, c);
742 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
744 assert(bset && bset->n_div == 0);
745 isl_basic_set_free(bset);
746 isl_local_space_free(ls);
748 /* test 6 */
749 dim = isl_space_set_alloc(ctx, 0, 3);
750 bset = isl_basic_set_universe(isl_space_copy(dim));
751 ls = isl_local_space_from_space(dim);
753 c = isl_equality_alloc(isl_local_space_copy(ls));
754 isl_int_set_si(v, -1);
755 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
756 isl_int_set_si(v, 6);
757 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
758 bset = isl_basic_set_add_constraint(bset, c);
760 c = isl_equality_alloc(isl_local_space_copy(ls));
761 isl_int_set_si(v, 1);
762 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
763 isl_int_set_si(v, -3);
764 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
765 bset = isl_basic_set_add_constraint(bset, c);
767 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
769 assert(bset && bset->n_div == 1);
770 isl_basic_set_free(bset);
771 isl_local_space_free(ls);
773 /* test 7 */
774 /* This test is a bit tricky. We set up an equality
775 * a + 3b + 3c = 6 e0
776 * Normalization of divs creates _two_ divs
777 * a = 3 e0
778 * c - b - e0 = 2 e1
779 * Afterwards e0 is removed again because it has coefficient -1
780 * and we end up with the original equality and div again.
781 * Perhaps we can avoid the introduction of this temporary div.
783 dim = isl_space_set_alloc(ctx, 0, 4);
784 bset = isl_basic_set_universe(isl_space_copy(dim));
785 ls = isl_local_space_from_space(dim);
787 c = isl_equality_alloc(isl_local_space_copy(ls));
788 isl_int_set_si(v, -1);
789 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
790 isl_int_set_si(v, -3);
791 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
792 isl_int_set_si(v, -3);
793 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
794 isl_int_set_si(v, 6);
795 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
796 bset = isl_basic_set_add_constraint(bset, c);
798 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
800 /* Test disabled for now */
802 assert(bset && bset->n_div == 1);
804 isl_local_space_free(ls);
805 isl_basic_set_free(bset);
807 /* test 8 */
808 dim = isl_space_set_alloc(ctx, 0, 5);
809 bset = isl_basic_set_universe(isl_space_copy(dim));
810 ls = isl_local_space_from_space(dim);
812 c = isl_equality_alloc(isl_local_space_copy(ls));
813 isl_int_set_si(v, -1);
814 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
815 isl_int_set_si(v, -3);
816 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
817 isl_int_set_si(v, -3);
818 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
819 isl_int_set_si(v, 6);
820 isl_constraint_set_coefficient(c, isl_dim_set, 4, v);
821 bset = isl_basic_set_add_constraint(bset, c);
823 c = isl_equality_alloc(isl_local_space_copy(ls));
824 isl_int_set_si(v, -1);
825 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
826 isl_int_set_si(v, 1);
827 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
828 isl_int_set_si(v, 1);
829 isl_constraint_set_constant(c, v);
830 bset = isl_basic_set_add_constraint(bset, c);
832 bset = isl_basic_set_project_out(bset, isl_dim_set, 4, 1);
834 /* Test disabled for now */
836 assert(bset && bset->n_div == 1);
838 isl_local_space_free(ls);
839 isl_basic_set_free(bset);
841 /* test 9 */
842 dim = isl_space_set_alloc(ctx, 0, 4);
843 bset = isl_basic_set_universe(isl_space_copy(dim));
844 ls = isl_local_space_from_space(dim);
846 c = isl_equality_alloc(isl_local_space_copy(ls));
847 isl_int_set_si(v, 1);
848 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
849 isl_int_set_si(v, -1);
850 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
851 isl_int_set_si(v, -2);
852 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
853 bset = isl_basic_set_add_constraint(bset, c);
855 c = isl_equality_alloc(isl_local_space_copy(ls));
856 isl_int_set_si(v, -1);
857 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
858 isl_int_set_si(v, 3);
859 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
860 isl_int_set_si(v, 2);
861 isl_constraint_set_constant(c, v);
862 bset = isl_basic_set_add_constraint(bset, c);
864 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 2);
866 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
868 assert(!isl_basic_set_is_empty(bset));
870 isl_local_space_free(ls);
871 isl_basic_set_free(bset);
873 /* test 10 */
874 dim = isl_space_set_alloc(ctx, 0, 3);
875 bset = isl_basic_set_universe(isl_space_copy(dim));
876 ls = isl_local_space_from_space(dim);
878 c = isl_equality_alloc(isl_local_space_copy(ls));
879 isl_int_set_si(v, 1);
880 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
881 isl_int_set_si(v, -2);
882 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
883 bset = isl_basic_set_add_constraint(bset, c);
885 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
887 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
889 isl_local_space_free(ls);
890 isl_basic_set_free(bset);
892 isl_int_clear(v);
894 str = "{ [i] : exists (e0, e1: 3e1 >= 1 + 2e0 and "
895 "8e1 <= -1 + 5i - 5e0 and 2e1 >= 1 + 2i - 5e0) }";
896 set = isl_set_read_from_str(ctx, str);
897 set = isl_set_compute_divs(set);
898 isl_set_free(set);
899 if (!set)
900 return -1;
902 str = "{ [i,j] : 2*[i/2] + 3 * [j/4] <= 10 and 2 i = j }";
903 bset = isl_basic_set_read_from_str(ctx, str);
904 n = isl_basic_set_dim(bset, isl_dim_div);
905 isl_basic_set_free(bset);
906 if (!bset)
907 return -1;
908 if (n != 0)
909 isl_die(ctx, isl_error_unknown,
910 "expecting no existentials", return -1);
912 str = "{ [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }";
913 set = isl_set_read_from_str(ctx, str);
914 set = isl_set_remove_divs_involving_dims(set, isl_dim_set, 0, 2);
915 set = isl_set_fix_si(set, isl_dim_set, 2, -3);
916 empty = isl_set_is_empty(set);
917 isl_set_free(set);
918 if (empty < 0)
919 return -1;
920 if (!empty)
921 isl_die(ctx, isl_error_unknown,
922 "result not as accurate as expected", return -1);
924 return 0;
927 void test_application_case(struct isl_ctx *ctx, const char *name)
929 char *filename;
930 FILE *input;
931 struct isl_basic_set *bset1, *bset2;
932 struct isl_basic_map *bmap;
934 filename = get_filename(ctx, name, "omega");
935 assert(filename);
936 input = fopen(filename, "r");
937 assert(input);
939 bset1 = isl_basic_set_read_from_file(ctx, input);
940 bmap = isl_basic_map_read_from_file(ctx, input);
942 bset1 = isl_basic_set_apply(bset1, bmap);
944 bset2 = isl_basic_set_read_from_file(ctx, input);
946 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
948 isl_basic_set_free(bset1);
949 isl_basic_set_free(bset2);
950 free(filename);
952 fclose(input);
955 void test_application(struct isl_ctx *ctx)
957 test_application_case(ctx, "application");
958 test_application_case(ctx, "application2");
961 void test_affine_hull_case(struct isl_ctx *ctx, const char *name)
963 char *filename;
964 FILE *input;
965 struct isl_basic_set *bset1, *bset2;
967 filename = get_filename(ctx, name, "polylib");
968 assert(filename);
969 input = fopen(filename, "r");
970 assert(input);
972 bset1 = isl_basic_set_read_from_file(ctx, input);
973 bset2 = isl_basic_set_read_from_file(ctx, input);
975 bset1 = isl_basic_set_affine_hull(bset1);
977 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
979 isl_basic_set_free(bset1);
980 isl_basic_set_free(bset2);
981 free(filename);
983 fclose(input);
986 int test_affine_hull(struct isl_ctx *ctx)
988 const char *str;
989 isl_set *set;
990 isl_basic_set *bset, *bset2;
991 int n;
992 int subset;
994 test_affine_hull_case(ctx, "affine2");
995 test_affine_hull_case(ctx, "affine");
996 test_affine_hull_case(ctx, "affine3");
998 str = "[m] -> { [i0] : exists (e0, e1: e1 <= 1 + i0 and "
999 "m >= 3 and 4i0 <= 2 + m and e1 >= i0 and "
1000 "e1 >= 0 and e1 <= 2 and e1 >= 1 + 2e0 and "
1001 "2e1 <= 1 + m + 4e0 and 2e1 >= 2 - m + 4i0 - 4e0) }";
1002 set = isl_set_read_from_str(ctx, str);
1003 bset = isl_set_affine_hull(set);
1004 n = isl_basic_set_dim(bset, isl_dim_div);
1005 isl_basic_set_free(bset);
1006 if (n != 0)
1007 isl_die(ctx, isl_error_unknown, "not expecting any divs",
1008 return -1);
1010 /* Check that isl_map_affine_hull is not confused by
1011 * the reordering of divs in isl_map_align_divs.
1013 str = "{ [a, b, c, 0] : exists (e0 = [(b)/32], e1 = [(c)/32]: "
1014 "32e0 = b and 32e1 = c); "
1015 "[a, 0, c, 0] : exists (e0 = [(c)/32]: 32e0 = c) }";
1016 set = isl_set_read_from_str(ctx, str);
1017 bset = isl_set_affine_hull(set);
1018 isl_basic_set_free(bset);
1019 if (!bset)
1020 return -1;
1022 str = "{ [a] : exists e0, e1, e2: 32e1 = 31 + 31a + 31e0 and "
1023 "32e2 = 31 + 31e0 }";
1024 set = isl_set_read_from_str(ctx, str);
1025 bset = isl_set_affine_hull(set);
1026 str = "{ [a] : exists e : a = 32 e }";
1027 bset2 = isl_basic_set_read_from_str(ctx, str);
1028 subset = isl_basic_set_is_subset(bset, bset2);
1029 isl_basic_set_free(bset);
1030 isl_basic_set_free(bset2);
1031 if (subset < 0)
1032 return -1;
1033 if (!subset)
1034 isl_die(ctx, isl_error_unknown, "not as accurate as expected",
1035 return -1);
1037 return 0;
1040 void test_convex_hull_case(struct isl_ctx *ctx, const char *name)
1042 char *filename;
1043 FILE *input;
1044 struct isl_basic_set *bset1, *bset2;
1045 struct isl_set *set;
1047 filename = get_filename(ctx, name, "polylib");
1048 assert(filename);
1049 input = fopen(filename, "r");
1050 assert(input);
1052 bset1 = isl_basic_set_read_from_file(ctx, input);
1053 bset2 = isl_basic_set_read_from_file(ctx, input);
1055 set = isl_basic_set_union(bset1, bset2);
1056 bset1 = isl_set_convex_hull(set);
1058 bset2 = isl_basic_set_read_from_file(ctx, input);
1060 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1062 isl_basic_set_free(bset1);
1063 isl_basic_set_free(bset2);
1064 free(filename);
1066 fclose(input);
1069 struct {
1070 const char *set;
1071 const char *hull;
1072 } convex_hull_tests[] = {
1073 { "{ [i0, i1, i2] : (i2 = 1 and i0 = 0 and i1 >= 0) or "
1074 "(i0 = 1 and i1 = 0 and i2 = 1) or "
1075 "(i0 = 0 and i1 = 0 and i2 = 0) }",
1076 "{ [i0, i1, i2] : i0 >= 0 and i2 >= i0 and i2 <= 1 and i1 >= 0 }" },
1077 { "[n] -> { [i0, i1, i0] : i0 <= -4 + n; "
1078 "[i0, i0, i2] : n = 6 and i0 >= 0 and i2 <= 7 - i0 and "
1079 "i2 <= 5 and i2 >= 4; "
1080 "[3, i1, 3] : n = 5 and i1 <= 2 and i1 >= 0 }",
1081 "[n] -> { [i0, i1, i2] : i2 <= -1 + n and 2i2 <= -6 + 3n - i0 and "
1082 "i2 <= 5 + i0 and i2 >= i0 }" },
1083 { "{ [x, y] : 3y <= 2x and y >= -2 + 2x and 2y >= 2 - x }",
1084 "{ [x, y] : 1 = 0 }" },
1087 void test_convex_hull_algo(struct isl_ctx *ctx, int convex)
1089 int i;
1090 int orig_convex = ctx->opt->convex;
1091 ctx->opt->convex = convex;
1093 test_convex_hull_case(ctx, "convex0");
1094 test_convex_hull_case(ctx, "convex1");
1095 test_convex_hull_case(ctx, "convex2");
1096 test_convex_hull_case(ctx, "convex3");
1097 test_convex_hull_case(ctx, "convex4");
1098 test_convex_hull_case(ctx, "convex5");
1099 test_convex_hull_case(ctx, "convex6");
1100 test_convex_hull_case(ctx, "convex7");
1101 test_convex_hull_case(ctx, "convex8");
1102 test_convex_hull_case(ctx, "convex9");
1103 test_convex_hull_case(ctx, "convex10");
1104 test_convex_hull_case(ctx, "convex11");
1105 test_convex_hull_case(ctx, "convex12");
1106 test_convex_hull_case(ctx, "convex13");
1107 test_convex_hull_case(ctx, "convex14");
1108 test_convex_hull_case(ctx, "convex15");
1110 for (i = 0; i < ARRAY_SIZE(convex_hull_tests); ++i) {
1111 isl_set *set1, *set2;
1113 set1 = isl_set_read_from_str(ctx, convex_hull_tests[i].set);
1114 set2 = isl_set_read_from_str(ctx, convex_hull_tests[i].hull);
1115 set1 = isl_set_from_basic_set(isl_set_convex_hull(set1));
1116 assert(isl_set_is_equal(set1, set2));
1117 isl_set_free(set1);
1118 isl_set_free(set2);
1121 ctx->opt->convex = orig_convex;
1124 void test_convex_hull(struct isl_ctx *ctx)
1126 test_convex_hull_algo(ctx, ISL_CONVEX_HULL_FM);
1127 test_convex_hull_algo(ctx, ISL_CONVEX_HULL_WRAP);
1130 void test_gist_case(struct isl_ctx *ctx, const char *name)
1132 char *filename;
1133 FILE *input;
1134 struct isl_basic_set *bset1, *bset2;
1136 filename = get_filename(ctx, name, "polylib");
1137 assert(filename);
1138 input = fopen(filename, "r");
1139 assert(input);
1141 bset1 = isl_basic_set_read_from_file(ctx, input);
1142 bset2 = isl_basic_set_read_from_file(ctx, input);
1144 bset1 = isl_basic_set_gist(bset1, bset2);
1146 bset2 = isl_basic_set_read_from_file(ctx, input);
1148 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1150 isl_basic_set_free(bset1);
1151 isl_basic_set_free(bset2);
1152 free(filename);
1154 fclose(input);
1157 struct {
1158 const char *set;
1159 const char *context;
1160 const char *gist;
1161 } gist_tests[] = {
1162 { "{ [a, b, c] : a <= 15 and a >= 1 }",
1163 "{ [a, b, c] : exists (e0 = floor((-1 + a)/16): a >= 1 and "
1164 "c <= 30 and 32e0 >= -62 + 2a + 2b - c and b >= 0) }",
1165 "{ [a, b, c] : a <= 15 }" },
1166 { "{ : }", "{ : 1 = 0 }", "{ : }" },
1167 { "{ : 1 = 0 }", "{ : 1 = 0 }", "{ : }" },
1168 { "[M] -> { [x] : exists (e0 = floor((-2 + x)/3): 3e0 = -2 + x) }",
1169 "[M] -> { [3M] }" , "[M] -> { [x] : 1 = 0 }" },
1170 { "{ [m, n, a, b] : a <= 2147 + n }",
1171 "{ [m, n, a, b] : (m >= 1 and n >= 1 and a <= 2148 - m and "
1172 "b <= 2148 - n and b >= 0 and b >= 2149 - n - a) or "
1173 "(n >= 1 and a >= 0 and b <= 2148 - n - a and "
1174 "b >= 0) }",
1175 "{ [m, n, ku, kl] }" },
1178 static int test_gist(struct isl_ctx *ctx)
1180 int i;
1181 const char *str;
1182 isl_basic_set *bset1, *bset2;
1183 isl_map *map1, *map2;
1184 int equal;
1186 for (i = 0; i < ARRAY_SIZE(gist_tests); ++i) {
1187 int equal_input;
1188 isl_set *set1, *set2, *copy;
1190 set1 = isl_set_read_from_str(ctx, gist_tests[i].set);
1191 set2 = isl_set_read_from_str(ctx, gist_tests[i].context);
1192 copy = isl_set_copy(set1);
1193 set1 = isl_set_gist(set1, set2);
1194 set2 = isl_set_read_from_str(ctx, gist_tests[i].gist);
1195 equal = isl_set_is_equal(set1, set2);
1196 isl_set_free(set1);
1197 isl_set_free(set2);
1198 set1 = isl_set_read_from_str(ctx, gist_tests[i].set);
1199 equal_input = isl_set_is_equal(set1, copy);
1200 isl_set_free(set1);
1201 isl_set_free(copy);
1202 if (equal < 0 || equal_input < 0)
1203 return -1;
1204 if (!equal)
1205 isl_die(ctx, isl_error_unknown,
1206 "incorrect gist result", return -1);
1207 if (!equal_input)
1208 isl_die(ctx, isl_error_unknown,
1209 "gist modified input", return -1);
1212 test_gist_case(ctx, "gist1");
1214 str = "[p0, p2, p3, p5, p6, p10] -> { [] : "
1215 "exists (e0 = [(15 + p0 + 15p6 + 15p10)/16], e1 = [(p5)/8], "
1216 "e2 = [(p6)/128], e3 = [(8p2 - p5)/128], "
1217 "e4 = [(128p3 - p6)/4096]: 8e1 = p5 and 128e2 = p6 and "
1218 "128e3 = 8p2 - p5 and 4096e4 = 128p3 - p6 and p2 >= 0 and "
1219 "16e0 >= 16 + 16p6 + 15p10 and p2 <= 15 and p3 >= 0 and "
1220 "p3 <= 31 and p6 >= 128p3 and p5 >= 8p2 and p10 >= 0 and "
1221 "16e0 <= 15 + p0 + 15p6 + 15p10 and 16e0 >= p0 + 15p6 + 15p10 and "
1222 "p10 <= 15 and p10 <= -1 + p0 - p6) }";
1223 bset1 = isl_basic_set_read_from_str(ctx, str);
1224 str = "[p0, p2, p3, p5, p6, p10] -> { [] : exists (e0 = [(p5)/8], "
1225 "e1 = [(p6)/128], e2 = [(8p2 - p5)/128], "
1226 "e3 = [(128p3 - p6)/4096]: 8e0 = p5 and 128e1 = p6 and "
1227 "128e2 = 8p2 - p5 and 4096e3 = 128p3 - p6 and p5 >= -7 and "
1228 "p2 >= 0 and 8p2 <= -1 + p0 and p2 <= 15 and p3 >= 0 and "
1229 "p3 <= 31 and 128p3 <= -1 + p0 and p6 >= -127 and "
1230 "p5 <= -1 + p0 and p6 <= -1 + p0 and p6 >= 128p3 and "
1231 "p0 >= 1 and p5 >= 8p2 and p10 >= 0 and p10 <= 15 ) }";
1232 bset2 = isl_basic_set_read_from_str(ctx, str);
1233 bset1 = isl_basic_set_gist(bset1, bset2);
1234 assert(bset1 && bset1->n_div == 0);
1235 isl_basic_set_free(bset1);
1237 /* Check that the integer divisions of the second disjunct
1238 * do not spread to the first disjunct.
1240 str = "[t1] -> { S_0[] -> A[o0] : (exists (e0 = [(-t1 + o0)/16]: "
1241 "16e0 = -t1 + o0 and o0 >= 0 and o0 <= 15 and t1 >= 0)) or "
1242 "(exists (e0 = [(-1 + t1)/16], "
1243 "e1 = [(-16 + t1 - 16e0)/4294967296]: "
1244 "4294967296e1 = -16 + t1 - o0 - 16e0 and "
1245 "16e0 <= -1 + t1 and 16e0 >= -16 + t1 and o0 >= 0 and "
1246 "o0 <= 4294967295 and t1 <= -1)) }";
1247 map1 = isl_map_read_from_str(ctx, str);
1248 str = "[t1] -> { S_0[] -> A[o0] : t1 >= 0 and t1 <= 4294967295 }";
1249 map2 = isl_map_read_from_str(ctx, str);
1250 map1 = isl_map_gist(map1, map2);
1251 if (!map1)
1252 return -1;
1253 if (map1->n != 1)
1254 isl_die(ctx, isl_error_unknown, "expecting single disjunct",
1255 isl_map_free(map1); return -1);
1256 if (isl_basic_map_dim(map1->p[0], isl_dim_div) != 1)
1257 isl_die(ctx, isl_error_unknown, "expecting single div",
1258 isl_map_free(map1); return -1);
1259 isl_map_free(map1);
1261 return 0;
1264 int test_coalesce_set(isl_ctx *ctx, const char *str, int check_one)
1266 isl_set *set, *set2;
1267 int equal;
1268 int one;
1270 set = isl_set_read_from_str(ctx, str);
1271 set = isl_set_coalesce(set);
1272 set2 = isl_set_read_from_str(ctx, str);
1273 equal = isl_set_is_equal(set, set2);
1274 one = set && set->n == 1;
1275 isl_set_free(set);
1276 isl_set_free(set2);
1278 if (equal < 0)
1279 return -1;
1280 if (!equal)
1281 isl_die(ctx, isl_error_unknown,
1282 "coalesced set not equal to input", return -1);
1283 if (check_one && !one)
1284 isl_die(ctx, isl_error_unknown,
1285 "coalesced set should not be a union", return -1);
1287 return 0;
1290 int test_coalesce_unbounded_wrapping(isl_ctx *ctx)
1292 int r = 0;
1293 int bounded;
1295 bounded = isl_options_get_coalesce_bounded_wrapping(ctx);
1296 isl_options_set_coalesce_bounded_wrapping(ctx, 0);
1298 if (test_coalesce_set(ctx,
1299 "{[x,y,z] : y + 2 >= 0 and x - y + 1 >= 0 and "
1300 "-x - y + 1 >= 0 and -3 <= z <= 3;"
1301 "[x,y,z] : -x+z + 20 >= 0 and -x-z + 20 >= 0 and "
1302 "x-z + 20 >= 0 and x+z + 20 >= 0 and "
1303 "-10 <= y <= 0}", 1) < 0)
1304 goto error;
1305 if (test_coalesce_set(ctx,
1306 "{[x,y] : 0 <= x,y <= 10; [5,y]: 4 <=y <= 11}", 1) < 0)
1307 goto error;
1308 if (test_coalesce_set(ctx,
1309 "{[x,0,0] : -5 <= x <= 5; [0,y,1] : -5 <= y <= 5 }", 1) < 0)
1310 goto error;
1312 if (0) {
1313 error:
1314 r = -1;
1317 isl_options_set_coalesce_bounded_wrapping(ctx, bounded);
1319 return r;
1322 /* Inputs for coalescing tests.
1323 * "str" is a string representation of the input set.
1324 * "single_disjunct" is set if we expect the result to consist of
1325 * a single disjunct.
1327 struct {
1328 int single_disjunct;
1329 const char *str;
1330 } coalesce_tests[] = {
1331 { 1, "{[x,y]: x >= 0 & x <= 10 & y >= 0 & y <= 10 or "
1332 "y >= x & x >= 2 & 5 >= y }" },
1333 { 1, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
1334 "x + y >= 10 & y <= x & x + y <= 20 & y >= 0}" },
1335 { 0, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
1336 "x + y >= 10 & y <= x & x + y <= 19 & y >= 0}" },
1337 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1338 "y >= 0 & x >= 6 & x <= 10 & y <= x}" },
1339 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1340 "y >= 0 & x >= 7 & x <= 10 & y <= x}" },
1341 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1342 "y >= 0 & x >= 6 & x <= 10 & y + 1 <= x}" },
1343 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 6}" },
1344 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 7 & y <= 6}" },
1345 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 5}" },
1346 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 7}" },
1347 { 1, "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }" },
1348 { 0, "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}" },
1349 { 1, "[n] -> { [i] : 1 <= i and i <= n - 1 or 2 <= i and i <= n }" },
1350 { 0, "[n] -> { [[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1351 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1352 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1353 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
1354 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
1355 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
1356 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
1357 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
1358 "[[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1359 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1360 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1361 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
1362 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
1363 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
1364 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
1365 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
1366 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
1367 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }" },
1368 { 0, "[n, m] -> { [o0, o2, o3] : (o3 = 1 and o0 >= 1 + m and "
1369 "o0 <= n + m and o2 <= m and o0 >= 2 + n and o2 >= 3) or "
1370 "(o0 >= 2 + n and o0 >= 1 + m and o0 <= n + m and n >= 1 and "
1371 "o3 <= -1 + o2 and o3 >= 1 - m + o2 and o3 >= 2 and o3 <= n) }" },
1372 { 0, "[M, N] -> { [[i0, i1, i2, i3, i4, i5, i6] -> "
1373 "[o0, o1, o2, o3, o4, o5, o6]] : "
1374 "(o6 <= -4 + 2M - 2N + i0 + i1 - i2 + i6 - o0 - o1 + o2 and "
1375 "o3 <= -2 + i3 and o6 >= 2 + i0 + i3 + i6 - o0 - o3 and "
1376 "o6 >= 2 - M + N + i3 + i4 + i6 - o3 - o4 and o0 <= -1 + i0 and "
1377 "o4 >= 4 - 3M + 3N - i0 - i1 + i2 + 2i3 + i4 + o0 + o1 - o2 - 2o3 "
1378 "and o6 <= -3 + 2M - 2N + i3 + i4 - i5 + i6 - o3 - o4 + o5 and "
1379 "2o6 <= -5 + 5M - 5N + 2i0 + i1 - i2 - i5 + 2i6 - 2o0 - o1 + o2 + o5 "
1380 "and o6 >= 2i0 + i1 + i6 - 2o0 - o1 and "
1381 "3o6 <= -5 + 4M - 4N + 2i0 + i1 - i2 + 2i3 + i4 - i5 + 3i6 "
1382 "- 2o0 - o1 + o2 - 2o3 - o4 + o5) or "
1383 "(N >= 2 and o3 <= -1 + i3 and o0 <= -1 + i0 and "
1384 "o6 >= i3 + i6 - o3 and M >= 0 and "
1385 "2o6 >= 1 + i0 + i3 + 2i6 - o0 - o3 and "
1386 "o6 >= 1 - M + i0 + i6 - o0 and N >= 2M and o6 >= i0 + i6 - o0) }" },
1387 { 0, "[M, N] -> { [o0] : (o0 = 0 and M >= 1 and N >= 2) or "
1388 "(o0 = 0 and M >= 1 and N >= 2M and N >= 2 + M) or "
1389 "(o0 = 0 and M >= 2 and N >= 3) or "
1390 "(M = 0 and o0 = 0 and N >= 3) }" },
1391 { 0, "{ [i0, i1, i2, i3] : (i1 = 10i0 and i0 >= 1 and 10i0 <= 100 and "
1392 "i3 <= 9 + 10 i2 and i3 >= 1 + 10i2 and i3 >= 0) or "
1393 "(i1 <= 9 + 10i0 and i1 >= 1 + 10i0 and i2 >= 0 and "
1394 "i0 >= 0 and i1 <= 100 and i3 <= 9 + 10i2 and i3 >= 1 + 10i2) }" },
1395 { 0, "[M] -> { [i1] : (i1 >= 2 and i1 <= M) or (i1 = M and M >= 1) }" },
1396 { 0, "{[x,y] : x,y >= 0; [x,y] : 10 <= x <= 20 and y >= -1 }" },
1397 { 1, "{ [x, y] : (x >= 1 and y >= 1 and x <= 2 and y <= 2) or "
1398 "(y = 3 and x = 1) }" },
1399 { 1, "[M] -> { [i0, i1, i2, i3, i4] : (i1 >= 3 and i4 >= 2 + i2 and "
1400 "i2 >= 2 and i0 >= 2 and i3 >= 1 + i2 and i0 <= M and "
1401 "i1 <= M and i3 <= M and i4 <= M) or "
1402 "(i1 >= 2 and i4 >= 1 + i2 and i2 >= 2 and i0 >= 2 and "
1403 "i3 >= 1 + i2 and i0 <= M and i1 <= -1 + M and i3 <= M and "
1404 "i4 <= -1 + M) }" },
1405 { 1, "{ [x, y] : (x >= 0 and y >= 0 and x <= 10 and y <= 10) or "
1406 "(x >= 1 and y >= 1 and x <= 11 and y <= 11) }" },
1407 { 0, "{[x,0] : x >= 0; [x,1] : x <= 20}" },
1408 { 1, "{ [x, 1 - x] : 0 <= x <= 1; [0,0] }" },
1409 { 1, "{ [0,0]; [i,i] : 1 <= i <= 10 }" },
1410 { 0, "{ [0,0]; [i,j] : 1 <= i,j <= 10 }" },
1411 { 1, "{ [0,0]; [i,2i] : 1 <= i <= 10 }" },
1412 { 0, "{ [0,0]; [i,2i] : 2 <= i <= 10 }" },
1413 { 0, "{ [1,0]; [i,2i] : 1 <= i <= 10 }" },
1414 { 0, "{ [0,1]; [i,2i] : 1 <= i <= 10 }" },
1415 { 0, "{ [a, b] : exists e : 2e = a and "
1416 "a >= 0 and (a <= 3 or (b <= 0 and b >= -4 + a)) }" },
1417 { 0, "{ [i, j, i', j'] : i <= 2 and j <= 2 and "
1418 "j' >= -1 + 2i + j - 2i' and i' <= -1 + i and "
1419 "j >= 1 and j' <= i + j - i' and i >= 1; "
1420 "[1, 1, 1, 1] }" },
1421 { 1, "{ [i,j] : exists a,b : i = 2a and j = 3b; "
1422 "[i,j] : exists a : j = 3a }" },
1423 { 1, "{ [a, b, c] : (c <= 7 - b and b <= 1 and b >= 0 and "
1424 "c >= 3 + b and b <= 3 + 8a and b >= -26 + 8a and "
1425 "a >= 3) or "
1426 "(b <= 1 and c <= 7 and b >= 0 and c >= 4 + b and "
1427 "b <= 3 + 8a and b >= -26 + 8a and a >= 3) }" },
1428 { 1, "{ [a, 0, c] : c >= 1 and c <= 29 and c >= -1 + 8a and "
1429 "c <= 6 + 8a and a >= 3; "
1430 "[a, -1, c] : c >= 1 and c <= 30 and c >= 8a and "
1431 "c <= 7 + 8a and a >= 3 and a <= 4 }" },
1432 { 1, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
1433 "[x,0] : 3 <= x <= 4 }" },
1434 { 1, "{ [x,y] : 0 <= x <= 3 and y >= 0 and x + 3y <= 6; "
1435 "[x,0] : 4 <= x <= 5 }" },
1436 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
1437 "[x,0] : 3 <= x <= 5 }" },
1438 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + y <= 4; "
1439 "[x,0] : 3 <= x <= 4 }" },
1440 { 1, "{ [i0, i1] : i0 <= 122 and i0 >= 1 and 128i1 >= -249 + i0 and "
1441 "i1 <= 0; "
1442 "[i0, 0] : i0 >= 123 and i0 <= 124 }" },
1443 { 1, "{ [0,0]; [1,1] }" },
1446 /* Test the functionality of isl_set_coalesce.
1447 * That is, check that the output is always equal to the input
1448 * and in some cases that the result consists of a single disjunct.
1450 static int test_coalesce(struct isl_ctx *ctx)
1452 int i;
1454 for (i = 0; i < ARRAY_SIZE(coalesce_tests); ++i) {
1455 const char *str = coalesce_tests[i].str;
1456 int check_one = coalesce_tests[i].single_disjunct;
1457 if (test_coalesce_set(ctx, str, check_one) < 0)
1458 return -1;
1461 if (test_coalesce_unbounded_wrapping(ctx) < 0)
1462 return -1;
1464 return 0;
1467 void test_closure(struct isl_ctx *ctx)
1469 const char *str;
1470 isl_set *dom;
1471 isl_map *up, *right;
1472 isl_map *map, *map2;
1473 int exact;
1475 /* COCOA example 1 */
1476 map = isl_map_read_from_str(ctx,
1477 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
1478 "1 <= i and i < n and 1 <= j and j < n or "
1479 "i2 = i + 1 and j2 = j - 1 and "
1480 "1 <= i and i < n and 2 <= j and j <= n }");
1481 map = isl_map_power(map, &exact);
1482 assert(exact);
1483 isl_map_free(map);
1485 /* COCOA example 1 */
1486 map = isl_map_read_from_str(ctx,
1487 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
1488 "1 <= i and i < n and 1 <= j and j < n or "
1489 "i2 = i + 1 and j2 = j - 1 and "
1490 "1 <= i and i < n and 2 <= j and j <= n }");
1491 map = isl_map_transitive_closure(map, &exact);
1492 assert(exact);
1493 map2 = isl_map_read_from_str(ctx,
1494 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
1495 "1 <= i and i < n and 1 <= j and j <= n and "
1496 "2 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
1497 "i2 = i + k1 + k2 and j2 = j + k1 - k2 and "
1498 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1 )}");
1499 assert(isl_map_is_equal(map, map2));
1500 isl_map_free(map2);
1501 isl_map_free(map);
1503 map = isl_map_read_from_str(ctx,
1504 "[n] -> { [x] -> [y] : y = x + 1 and 0 <= x and x <= n and "
1505 " 0 <= y and y <= n }");
1506 map = isl_map_transitive_closure(map, &exact);
1507 map2 = isl_map_read_from_str(ctx,
1508 "[n] -> { [x] -> [y] : y > x and 0 <= x and x <= n and "
1509 " 0 <= y and y <= n }");
1510 assert(isl_map_is_equal(map, map2));
1511 isl_map_free(map2);
1512 isl_map_free(map);
1514 /* COCOA example 2 */
1515 map = isl_map_read_from_str(ctx,
1516 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j + 2 and "
1517 "1 <= i and i < n - 1 and 1 <= j and j < n - 1 or "
1518 "i2 = i + 2 and j2 = j - 2 and "
1519 "1 <= i and i < n - 1 and 3 <= j and j <= n }");
1520 map = isl_map_transitive_closure(map, &exact);
1521 assert(exact);
1522 map2 = isl_map_read_from_str(ctx,
1523 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
1524 "1 <= i and i < n - 1 and 1 <= j and j <= n and "
1525 "3 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
1526 "i2 = i + 2 k1 + 2 k2 and j2 = j + 2 k1 - 2 k2 and "
1527 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1) }");
1528 assert(isl_map_is_equal(map, map2));
1529 isl_map_free(map);
1530 isl_map_free(map2);
1532 /* COCOA Fig.2 left */
1533 map = isl_map_read_from_str(ctx,
1534 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j and "
1535 "i <= 2 j - 3 and i <= n - 2 and j <= 2 i - 1 and "
1536 "j <= n or "
1537 "i2 = i and j2 = j + 2 and i <= 2 j - 1 and i <= n and "
1538 "j <= 2 i - 3 and j <= n - 2 or "
1539 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1540 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1541 map = isl_map_transitive_closure(map, &exact);
1542 assert(exact);
1543 isl_map_free(map);
1545 /* COCOA Fig.2 right */
1546 map = isl_map_read_from_str(ctx,
1547 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
1548 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
1549 "j <= n or "
1550 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
1551 "j <= 2 i - 4 and j <= n - 3 or "
1552 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1553 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1554 map = isl_map_power(map, &exact);
1555 assert(exact);
1556 isl_map_free(map);
1558 /* COCOA Fig.2 right */
1559 map = isl_map_read_from_str(ctx,
1560 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
1561 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
1562 "j <= n or "
1563 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
1564 "j <= 2 i - 4 and j <= n - 3 or "
1565 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1566 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1567 map = isl_map_transitive_closure(map, &exact);
1568 assert(exact);
1569 map2 = isl_map_read_from_str(ctx,
1570 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k3,k : "
1571 "i <= 2 j - 1 and i <= n and j <= 2 i - 1 and "
1572 "j <= n and 3 + i + 2 j <= 3 n and "
1573 "3 + 2 i + j <= 3n and i2 <= 2 j2 -1 and i2 <= n and "
1574 "i2 <= 3 j2 - 4 and j2 <= 2 i2 -1 and j2 <= n and "
1575 "13 + 4 j2 <= 11 i2 and i2 = i + 3 k1 + k3 and "
1576 "j2 = j + 3 k2 + k3 and k1 >= 0 and k2 >= 0 and "
1577 "k3 >= 0 and k1 + k2 + k3 = k and k > 0) }");
1578 assert(isl_map_is_equal(map, map2));
1579 isl_map_free(map2);
1580 isl_map_free(map);
1582 /* COCOA Fig.1 right */
1583 dom = isl_set_read_from_str(ctx,
1584 "{ [x,y] : x >= 0 and -2 x + 3 y >= 0 and x <= 3 and "
1585 "2 x - 3 y + 3 >= 0 }");
1586 right = isl_map_read_from_str(ctx,
1587 "{ [x,y] -> [x2,y2] : x2 = x + 1 and y2 = y }");
1588 up = isl_map_read_from_str(ctx,
1589 "{ [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 }");
1590 right = isl_map_intersect_domain(right, isl_set_copy(dom));
1591 right = isl_map_intersect_range(right, isl_set_copy(dom));
1592 up = isl_map_intersect_domain(up, isl_set_copy(dom));
1593 up = isl_map_intersect_range(up, dom);
1594 map = isl_map_union(up, right);
1595 map = isl_map_transitive_closure(map, &exact);
1596 assert(exact);
1597 map2 = isl_map_read_from_str(ctx,
1598 "{ [0,0] -> [0,1]; [0,0] -> [1,1]; [0,1] -> [1,1]; "
1599 " [2,2] -> [3,2]; [2,2] -> [3,3]; [3,2] -> [3,3] }");
1600 assert(isl_map_is_equal(map, map2));
1601 isl_map_free(map2);
1602 isl_map_free(map);
1604 /* COCOA Theorem 1 counter example */
1605 map = isl_map_read_from_str(ctx,
1606 "{ [i,j] -> [i2,j2] : i = 0 and 0 <= j and j <= 1 and "
1607 "i2 = 1 and j2 = j or "
1608 "i = 0 and j = 0 and i2 = 0 and j2 = 1 }");
1609 map = isl_map_transitive_closure(map, &exact);
1610 assert(exact);
1611 isl_map_free(map);
1613 map = isl_map_read_from_str(ctx,
1614 "[m,n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 2 and "
1615 "1 <= i,i2 <= n and 1 <= j,j2 <= m or "
1616 "i2 = i + 1 and 3 <= j2 - j <= 4 and "
1617 "1 <= i,i2 <= n and 1 <= j,j2 <= m }");
1618 map = isl_map_transitive_closure(map, &exact);
1619 assert(exact);
1620 isl_map_free(map);
1622 /* Kelly et al 1996, fig 12 */
1623 map = isl_map_read_from_str(ctx,
1624 "[n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 1 and "
1625 "1 <= i,j,j+1 <= n or "
1626 "j = n and j2 = 1 and i2 = i + 1 and "
1627 "1 <= i,i+1 <= n }");
1628 map = isl_map_transitive_closure(map, &exact);
1629 assert(exact);
1630 map2 = isl_map_read_from_str(ctx,
1631 "[n] -> { [i,j] -> [i2,j2] : 1 <= j < j2 <= n and "
1632 "1 <= i <= n and i = i2 or "
1633 "1 <= i < i2 <= n and 1 <= j <= n and "
1634 "1 <= j2 <= n }");
1635 assert(isl_map_is_equal(map, map2));
1636 isl_map_free(map2);
1637 isl_map_free(map);
1639 /* Omega's closure4 */
1640 map = isl_map_read_from_str(ctx,
1641 "[m,n] -> { [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 and "
1642 "1 <= x,y <= 10 or "
1643 "x2 = x + 1 and y2 = y and "
1644 "1 <= x <= 20 && 5 <= y <= 15 }");
1645 map = isl_map_transitive_closure(map, &exact);
1646 assert(exact);
1647 isl_map_free(map);
1649 map = isl_map_read_from_str(ctx,
1650 "[n] -> { [x] -> [y]: 1 <= n <= y - x <= 10 }");
1651 map = isl_map_transitive_closure(map, &exact);
1652 assert(!exact);
1653 map2 = isl_map_read_from_str(ctx,
1654 "[n] -> { [x] -> [y] : 1 <= n <= 10 and y >= n + x }");
1655 assert(isl_map_is_equal(map, map2));
1656 isl_map_free(map);
1657 isl_map_free(map2);
1659 str = "[n, m] -> { [i0, i1, i2, i3] -> [o0, o1, o2, o3] : "
1660 "i3 = 1 and o0 = i0 and o1 = -1 + i1 and o2 = -1 + i2 and "
1661 "o3 = -2 + i2 and i1 <= -1 + i0 and i1 >= 1 - m + i0 and "
1662 "i1 >= 2 and i1 <= n and i2 >= 3 and i2 <= 1 + n and i2 <= m }";
1663 map = isl_map_read_from_str(ctx, str);
1664 map = isl_map_transitive_closure(map, &exact);
1665 assert(exact);
1666 map2 = isl_map_read_from_str(ctx, str);
1667 assert(isl_map_is_equal(map, map2));
1668 isl_map_free(map);
1669 isl_map_free(map2);
1671 str = "{[0] -> [1]; [2] -> [3]}";
1672 map = isl_map_read_from_str(ctx, str);
1673 map = isl_map_transitive_closure(map, &exact);
1674 assert(exact);
1675 map2 = isl_map_read_from_str(ctx, str);
1676 assert(isl_map_is_equal(map, map2));
1677 isl_map_free(map);
1678 isl_map_free(map2);
1680 str = "[n] -> { [[i0, i1, 1, 0, i0] -> [i5, 1]] -> "
1681 "[[i0, -1 + i1, 2, 0, i0] -> [-1 + i5, 2]] : "
1682 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 2 and "
1683 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1684 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1685 "[[i0, i1, 2, 0, i0] -> [i5, 1]] -> "
1686 "[[i0, i1, 1, 0, i0] -> [-1 + i5, 2]] : "
1687 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 1 and "
1688 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1689 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1690 "[[i0, i1, 1, 0, i0] -> [i5, 2]] -> "
1691 "[[i0, -1 + i1, 2, 0, i0] -> [i5, 1]] : "
1692 "exists (e0 = [(3 - n)/3]: i1 >= 2 and i5 >= 1 and "
1693 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1694 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1695 "[[i0, i1, 2, 0, i0] -> [i5, 2]] -> "
1696 "[[i0, i1, 1, 0, i0] -> [i5, 1]] : "
1697 "exists (e0 = [(3 - n)/3]: i5 >= 1 and i1 >= 1 and "
1698 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1699 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n) }";
1700 map = isl_map_read_from_str(ctx, str);
1701 map = isl_map_transitive_closure(map, NULL);
1702 assert(map);
1703 isl_map_free(map);
1706 void test_lex(struct isl_ctx *ctx)
1708 isl_space *dim;
1709 isl_map *map;
1711 dim = isl_space_set_alloc(ctx, 0, 0);
1712 map = isl_map_lex_le(dim);
1713 assert(!isl_map_is_empty(map));
1714 isl_map_free(map);
1717 static int test_lexmin(struct isl_ctx *ctx)
1719 int equal;
1720 const char *str;
1721 isl_basic_map *bmap;
1722 isl_map *map, *map2;
1723 isl_set *set;
1724 isl_set *set2;
1725 isl_pw_multi_aff *pma;
1727 str = "[p0, p1] -> { [] -> [] : "
1728 "exists (e0 = [(2p1)/3], e1, e2, e3 = [(3 - p1 + 3e0)/3], "
1729 "e4 = [(p1)/3], e5 = [(p1 + 3e4)/3]: "
1730 "3e0 >= -2 + 2p1 and 3e0 >= p1 and 3e3 >= 1 - p1 + 3e0 and "
1731 "3e0 <= 2p1 and 3e3 >= -2 + p1 and 3e3 <= -1 + p1 and p1 >= 3 and "
1732 "3e5 >= -2 + 2p1 and 3e5 >= p1 and 3e5 <= -1 + p1 + 3e4 and "
1733 "3e4 <= p1 and 3e4 >= -2 + p1 and e3 <= -1 + e0 and "
1734 "3e4 >= 6 - p1 + 3e1 and 3e1 >= p1 and 3e5 >= -2 + p1 + 3e4 and "
1735 "2e4 >= 3 - p1 + 2e1 and e4 <= e1 and 3e3 <= 2 - p1 + 3e0 and "
1736 "e5 >= 1 + e1 and 3e4 >= 6 - 2p1 + 3e1 and "
1737 "p0 >= 2 and p1 >= p0 and 3e2 >= p1 and 3e4 >= 6 - p1 + 3e2 and "
1738 "e2 <= e1 and e3 >= 1 and e4 <= e2) }";
1739 map = isl_map_read_from_str(ctx, str);
1740 map = isl_map_lexmin(map);
1741 isl_map_free(map);
1743 str = "[C] -> { [obj,a,b,c] : obj <= 38 a + 7 b + 10 c and "
1744 "a + b <= 1 and c <= 10 b and c <= C and a,b,c,C >= 0 }";
1745 set = isl_set_read_from_str(ctx, str);
1746 set = isl_set_lexmax(set);
1747 str = "[C] -> { [obj,a,b,c] : C = 8 }";
1748 set2 = isl_set_read_from_str(ctx, str);
1749 set = isl_set_intersect(set, set2);
1750 assert(!isl_set_is_empty(set));
1751 isl_set_free(set);
1753 str = "{ [x] -> [y] : x <= y <= 10; [x] -> [5] : -8 <= x <= 8 }";
1754 map = isl_map_read_from_str(ctx, str);
1755 map = isl_map_lexmin(map);
1756 str = "{ [x] -> [5] : 6 <= x <= 8; "
1757 "[x] -> [x] : x <= 5 or (9 <= x <= 10) }";
1758 map2 = isl_map_read_from_str(ctx, str);
1759 assert(isl_map_is_equal(map, map2));
1760 isl_map_free(map);
1761 isl_map_free(map2);
1763 str = "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }";
1764 map = isl_map_read_from_str(ctx, str);
1765 map2 = isl_map_copy(map);
1766 map = isl_map_lexmin(map);
1767 assert(isl_map_is_equal(map, map2));
1768 isl_map_free(map);
1769 isl_map_free(map2);
1771 str = "{ [x] -> [y] : x = 4y; [x] -> [y] : x = 2y }";
1772 map = isl_map_read_from_str(ctx, str);
1773 map = isl_map_lexmin(map);
1774 str = "{ [x] -> [y] : (4y = x and x >= 0) or "
1775 "(exists (e0 = [(x)/4], e1 = [(-2 + x)/4]: 2y = x and "
1776 "4e1 = -2 + x and 4e0 <= -1 + x and 4e0 >= -3 + x)) or "
1777 "(exists (e0 = [(x)/4]: 2y = x and 4e0 = x and x <= -4)) }";
1778 map2 = isl_map_read_from_str(ctx, str);
1779 assert(isl_map_is_equal(map, map2));
1780 isl_map_free(map);
1781 isl_map_free(map2);
1783 str = "{ [i] -> [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
1784 " 8i' <= i and 8i' >= -7 + i }";
1785 bmap = isl_basic_map_read_from_str(ctx, str);
1786 pma = isl_basic_map_lexmin_pw_multi_aff(isl_basic_map_copy(bmap));
1787 map2 = isl_map_from_pw_multi_aff(pma);
1788 map = isl_map_from_basic_map(bmap);
1789 assert(isl_map_is_equal(map, map2));
1790 isl_map_free(map);
1791 isl_map_free(map2);
1793 str = "{ T[a] -> S[b, c] : a = 4b-2c and c >= b }";
1794 map = isl_map_read_from_str(ctx, str);
1795 map = isl_map_lexmin(map);
1796 str = "{ T[a] -> S[b, c] : 2b = a and 2c = a }";
1797 map2 = isl_map_read_from_str(ctx, str);
1798 assert(isl_map_is_equal(map, map2));
1799 isl_map_free(map);
1800 isl_map_free(map2);
1802 /* Check that empty pieces are properly combined. */
1803 str = "[K, N] -> { [x, y] -> [a, b] : K+2<=N<=K+4 and x>=4 and "
1804 "2N-6<=x<K+N and N-1<=a<=K+N-1 and N+b-6<=a<=2N-4 and "
1805 "b<=2N-3K+a and 3b<=4N-K+1 and b>=N and a>=x+1 }";
1806 map = isl_map_read_from_str(ctx, str);
1807 map = isl_map_lexmin(map);
1808 str = "[K, N] -> { [x, y] -> [1 + x, N] : x >= -6 + 2N and "
1809 "x <= -5 + 2N and x >= -1 + 3K - N and x <= -2 + K + N and "
1810 "x >= 4 }";
1811 map2 = isl_map_read_from_str(ctx, str);
1812 assert(isl_map_is_equal(map, map2));
1813 isl_map_free(map);
1814 isl_map_free(map2);
1816 str = "[i] -> { [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
1817 " 8i' <= i and 8i' >= -7 + i }";
1818 set = isl_set_read_from_str(ctx, str);
1819 pma = isl_set_lexmin_pw_multi_aff(isl_set_copy(set));
1820 set2 = isl_set_from_pw_multi_aff(pma);
1821 equal = isl_set_is_equal(set, set2);
1822 isl_set_free(set);
1823 isl_set_free(set2);
1824 if (equal < 0)
1825 return -1;
1826 if (!equal)
1827 isl_die(ctx, isl_error_unknown,
1828 "unexpected difference between set and "
1829 "piecewise affine expression", return -1);
1831 return 0;
1834 /* Check that isl_set_min_val and isl_set_max_val compute the correct
1835 * result on non-convex inputs.
1837 static int test_min(struct isl_ctx *ctx)
1839 isl_set *set;
1840 isl_aff *aff;
1841 isl_val *val;
1842 int min_ok, max_ok;
1844 set = isl_set_read_from_str(ctx, "{ [-1]; [1] }");
1845 aff = isl_aff_read_from_str(ctx, "{ [x] -> [x] }");
1846 val = isl_set_min_val(set, aff);
1847 min_ok = isl_val_is_negone(val);
1848 isl_val_free(val);
1849 val = isl_set_max_val(set, aff);
1850 max_ok = isl_val_is_one(val);
1851 isl_val_free(val);
1852 isl_aff_free(aff);
1853 isl_set_free(set);
1855 if (min_ok < 0 || max_ok < 0)
1856 return -1;
1857 if (!min_ok)
1858 isl_die(ctx, isl_error_unknown,
1859 "unexpected minimum", return -1);
1860 if (!max_ok)
1861 isl_die(ctx, isl_error_unknown,
1862 "unexpected maximum", return -1);
1864 return 0;
1867 struct must_may {
1868 isl_map *must;
1869 isl_map *may;
1872 static int collect_must_may(__isl_take isl_map *dep, int must,
1873 void *dep_user, void *user)
1875 struct must_may *mm = (struct must_may *)user;
1877 if (must)
1878 mm->must = isl_map_union(mm->must, dep);
1879 else
1880 mm->may = isl_map_union(mm->may, dep);
1882 return 0;
1885 static int common_space(void *first, void *second)
1887 int depth = *(int *)first;
1888 return 2 * depth;
1891 static int map_is_equal(__isl_keep isl_map *map, const char *str)
1893 isl_map *map2;
1894 int equal;
1896 if (!map)
1897 return -1;
1899 map2 = isl_map_read_from_str(map->ctx, str);
1900 equal = isl_map_is_equal(map, map2);
1901 isl_map_free(map2);
1903 return equal;
1906 static int map_check_equal(__isl_keep isl_map *map, const char *str)
1908 int equal;
1910 equal = map_is_equal(map, str);
1911 if (equal < 0)
1912 return -1;
1913 if (!equal)
1914 isl_die(isl_map_get_ctx(map), isl_error_unknown,
1915 "result not as expected", return -1);
1916 return 0;
1919 void test_dep(struct isl_ctx *ctx)
1921 const char *str;
1922 isl_space *dim;
1923 isl_map *map;
1924 isl_access_info *ai;
1925 isl_flow *flow;
1926 int depth;
1927 struct must_may mm;
1929 depth = 3;
1931 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1932 map = isl_map_read_from_str(ctx, str);
1933 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1935 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1936 map = isl_map_read_from_str(ctx, str);
1937 ai = isl_access_info_add_source(ai, map, 1, &depth);
1939 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1940 map = isl_map_read_from_str(ctx, str);
1941 ai = isl_access_info_add_source(ai, map, 1, &depth);
1943 flow = isl_access_info_compute_flow(ai);
1944 dim = isl_space_alloc(ctx, 0, 3, 3);
1945 mm.must = isl_map_empty(isl_space_copy(dim));
1946 mm.may = isl_map_empty(dim);
1948 isl_flow_foreach(flow, collect_must_may, &mm);
1950 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10); "
1951 " [1,10,0] -> [2,5,0] }";
1952 assert(map_is_equal(mm.must, str));
1953 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1954 assert(map_is_equal(mm.may, str));
1956 isl_map_free(mm.must);
1957 isl_map_free(mm.may);
1958 isl_flow_free(flow);
1961 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1962 map = isl_map_read_from_str(ctx, str);
1963 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1965 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1966 map = isl_map_read_from_str(ctx, str);
1967 ai = isl_access_info_add_source(ai, map, 1, &depth);
1969 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1970 map = isl_map_read_from_str(ctx, str);
1971 ai = isl_access_info_add_source(ai, map, 0, &depth);
1973 flow = isl_access_info_compute_flow(ai);
1974 dim = isl_space_alloc(ctx, 0, 3, 3);
1975 mm.must = isl_map_empty(isl_space_copy(dim));
1976 mm.may = isl_map_empty(dim);
1978 isl_flow_foreach(flow, collect_must_may, &mm);
1980 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10) }";
1981 assert(map_is_equal(mm.must, str));
1982 str = "{ [0,5,0] -> [2,5,0]; [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
1983 assert(map_is_equal(mm.may, str));
1985 isl_map_free(mm.must);
1986 isl_map_free(mm.may);
1987 isl_flow_free(flow);
1990 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1991 map = isl_map_read_from_str(ctx, str);
1992 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1994 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1995 map = isl_map_read_from_str(ctx, str);
1996 ai = isl_access_info_add_source(ai, map, 0, &depth);
1998 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1999 map = isl_map_read_from_str(ctx, str);
2000 ai = isl_access_info_add_source(ai, map, 0, &depth);
2002 flow = isl_access_info_compute_flow(ai);
2003 dim = isl_space_alloc(ctx, 0, 3, 3);
2004 mm.must = isl_map_empty(isl_space_copy(dim));
2005 mm.may = isl_map_empty(dim);
2007 isl_flow_foreach(flow, collect_must_may, &mm);
2009 str = "{ [0,i,0] -> [2,i,0] : 0 <= i <= 10; "
2010 " [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
2011 assert(map_is_equal(mm.may, str));
2012 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2013 assert(map_is_equal(mm.must, str));
2015 isl_map_free(mm.must);
2016 isl_map_free(mm.may);
2017 isl_flow_free(flow);
2020 str = "{ [0,i,2] -> [i] : 0 <= i <= 10 }";
2021 map = isl_map_read_from_str(ctx, str);
2022 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2024 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2025 map = isl_map_read_from_str(ctx, str);
2026 ai = isl_access_info_add_source(ai, map, 0, &depth);
2028 str = "{ [0,i,1] -> [5] : 0 <= i <= 10 }";
2029 map = isl_map_read_from_str(ctx, str);
2030 ai = isl_access_info_add_source(ai, map, 0, &depth);
2032 flow = isl_access_info_compute_flow(ai);
2033 dim = isl_space_alloc(ctx, 0, 3, 3);
2034 mm.must = isl_map_empty(isl_space_copy(dim));
2035 mm.may = isl_map_empty(dim);
2037 isl_flow_foreach(flow, collect_must_may, &mm);
2039 str = "{ [0,i,0] -> [0,i,2] : 0 <= i <= 10; "
2040 " [0,i,1] -> [0,5,2] : 0 <= i <= 5 }";
2041 assert(map_is_equal(mm.may, str));
2042 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2043 assert(map_is_equal(mm.must, str));
2045 isl_map_free(mm.must);
2046 isl_map_free(mm.may);
2047 isl_flow_free(flow);
2050 str = "{ [0,i,1] -> [i] : 0 <= i <= 10 }";
2051 map = isl_map_read_from_str(ctx, str);
2052 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2054 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2055 map = isl_map_read_from_str(ctx, str);
2056 ai = isl_access_info_add_source(ai, map, 0, &depth);
2058 str = "{ [0,i,2] -> [5] : 0 <= i <= 10 }";
2059 map = isl_map_read_from_str(ctx, str);
2060 ai = isl_access_info_add_source(ai, map, 0, &depth);
2062 flow = isl_access_info_compute_flow(ai);
2063 dim = isl_space_alloc(ctx, 0, 3, 3);
2064 mm.must = isl_map_empty(isl_space_copy(dim));
2065 mm.may = isl_map_empty(dim);
2067 isl_flow_foreach(flow, collect_must_may, &mm);
2069 str = "{ [0,i,0] -> [0,i,1] : 0 <= i <= 10; "
2070 " [0,i,2] -> [0,5,1] : 0 <= i <= 4 }";
2071 assert(map_is_equal(mm.may, str));
2072 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2073 assert(map_is_equal(mm.must, str));
2075 isl_map_free(mm.must);
2076 isl_map_free(mm.may);
2077 isl_flow_free(flow);
2080 depth = 5;
2082 str = "{ [1,i,0,0,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
2083 map = isl_map_read_from_str(ctx, str);
2084 ai = isl_access_info_alloc(map, &depth, &common_space, 1);
2086 str = "{ [0,i,0,j,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
2087 map = isl_map_read_from_str(ctx, str);
2088 ai = isl_access_info_add_source(ai, map, 1, &depth);
2090 flow = isl_access_info_compute_flow(ai);
2091 dim = isl_space_alloc(ctx, 0, 5, 5);
2092 mm.must = isl_map_empty(isl_space_copy(dim));
2093 mm.may = isl_map_empty(dim);
2095 isl_flow_foreach(flow, collect_must_may, &mm);
2097 str = "{ [0,i,0,j,0] -> [1,i,0,0,0] : 0 <= i,j <= 10 }";
2098 assert(map_is_equal(mm.must, str));
2099 str = "{ [0,0,0,0,0] -> [0,0,0,0,0] : 1 = 0 }";
2100 assert(map_is_equal(mm.may, str));
2102 isl_map_free(mm.must);
2103 isl_map_free(mm.may);
2104 isl_flow_free(flow);
2107 /* Check that the dependence analysis proceeds without errors.
2108 * Earlier versions of isl would break down during the analysis
2109 * due to the use of the wrong spaces.
2111 static int test_flow(isl_ctx *ctx)
2113 const char *str;
2114 isl_union_map *access, *schedule;
2115 isl_union_map *must_dep, *may_dep;
2116 int r;
2118 str = "{ S0[j] -> i[]; S1[j,i] -> i[]; S2[] -> i[]; S3[] -> i[] }";
2119 access = isl_union_map_read_from_str(ctx, str);
2120 str = "{ S0[j] -> [0,j,0,0] : 0 <= j < 10; "
2121 "S1[j,i] -> [0,j,1,i] : 0 <= j < i < 10; "
2122 "S2[] -> [1,0,0,0]; "
2123 "S3[] -> [-1,0,0,0] }";
2124 schedule = isl_union_map_read_from_str(ctx, str);
2125 r = isl_union_map_compute_flow(access, isl_union_map_copy(access),
2126 isl_union_map_copy(access), schedule,
2127 &must_dep, &may_dep, NULL, NULL);
2128 isl_union_map_free(may_dep);
2129 isl_union_map_free(must_dep);
2131 return r;
2134 struct {
2135 const char *map;
2136 int sv;
2137 } sv_tests[] = {
2138 { "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 9 }", 1 },
2139 { "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 10 }", 0 },
2140 { "{ [i] -> [3*floor(i/2) + 5*floor(i/3)] }", 1 },
2141 { "{ S1[i] -> [i] : 0 <= i <= 9; S2[i] -> [i] : 0 <= i <= 9 }", 1 },
2142 { "{ [i] -> S1[i] : 0 <= i <= 9; [i] -> S2[i] : 0 <= i <= 9 }", 0 },
2143 { "{ A[i] -> [i]; B[i] -> [i]; B[i] -> [i + 1] }", 0 },
2144 { "{ A[i] -> [i]; B[i] -> [i] : i < 0; B[i] -> [i + 1] : i > 0 }", 1 },
2145 { "{ A[i] -> [i]; B[i] -> A[i] : i < 0; B[i] -> [i + 1] : i > 0 }", 1 },
2146 { "{ A[i] -> [i]; B[i] -> [j] : i - 1 <= j <= i }", 0 },
2149 int test_sv(isl_ctx *ctx)
2151 isl_union_map *umap;
2152 int i;
2153 int sv;
2155 for (i = 0; i < ARRAY_SIZE(sv_tests); ++i) {
2156 umap = isl_union_map_read_from_str(ctx, sv_tests[i].map);
2157 sv = isl_union_map_is_single_valued(umap);
2158 isl_union_map_free(umap);
2159 if (sv < 0)
2160 return -1;
2161 if (sv_tests[i].sv && !sv)
2162 isl_die(ctx, isl_error_internal,
2163 "map not detected as single valued", return -1);
2164 if (!sv_tests[i].sv && sv)
2165 isl_die(ctx, isl_error_internal,
2166 "map detected as single valued", return -1);
2169 return 0;
2172 void test_bijective_case(struct isl_ctx *ctx, const char *str, int bijective)
2174 isl_map *map;
2176 map = isl_map_read_from_str(ctx, str);
2177 if (bijective)
2178 assert(isl_map_is_bijective(map));
2179 else
2180 assert(!isl_map_is_bijective(map));
2181 isl_map_free(map);
2184 void test_bijective(struct isl_ctx *ctx)
2186 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i]}", 0);
2187 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=i}", 1);
2188 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=0}", 1);
2189 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=N}", 1);
2190 test_bijective_case(ctx, "[N,M]->{[i,j] -> [j,i]}", 1);
2191 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i+j]}", 0);
2192 test_bijective_case(ctx, "[N,M]->{[i,j] -> []}", 0);
2193 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i,j,N]}", 1);
2194 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i]}", 0);
2195 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i,i]}", 0);
2196 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i,i]}", 0);
2197 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i,j]}", 1);
2198 test_bijective_case(ctx, "[N,M]->{[i,j] -> [x,y] : 2x=i & y =j}", 1);
2201 /* Inputs for isl_pw_qpolynomial_gist tests.
2202 * "pwqp" is the input, "set" is the context and "gist" is the expected result.
2204 struct {
2205 const char *pwqp;
2206 const char *set;
2207 const char *gist;
2208 } pwqp_gist_tests[] = {
2209 { "{ [i] -> i }", "{ [k] : exists a : k = 2a }", "{ [i] -> i }" },
2210 { "{ [i] -> i + [ (i + [i/3])/2 ] }", "{ [10] }", "{ [i] -> 16 }" },
2211 { "{ [i] -> ([(i)/2]) }", "{ [k] : exists a : k = 2a+1 }",
2212 "{ [i] -> -1/2 + 1/2 * i }" },
2213 { "{ [i] -> i^2 : i != 0 }", "{ [i] : i != 0 }", "{ [i] -> i^2 }" },
2216 static int test_pwqp(struct isl_ctx *ctx)
2218 int i;
2219 const char *str;
2220 isl_set *set;
2221 isl_pw_qpolynomial *pwqp1, *pwqp2;
2222 int equal;
2224 str = "{ [i,j,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
2225 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2227 pwqp1 = isl_pw_qpolynomial_move_dims(pwqp1, isl_dim_param, 0,
2228 isl_dim_in, 1, 1);
2230 str = "[j] -> { [i,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
2231 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2233 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2235 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2237 isl_pw_qpolynomial_free(pwqp1);
2239 for (i = 0; i < ARRAY_SIZE(pwqp_gist_tests); ++i) {
2240 str = pwqp_gist_tests[i].pwqp;
2241 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2242 str = pwqp_gist_tests[i].set;
2243 set = isl_set_read_from_str(ctx, str);
2244 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
2245 str = pwqp_gist_tests[i].gist;
2246 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2247 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2248 equal = isl_pw_qpolynomial_is_zero(pwqp1);
2249 isl_pw_qpolynomial_free(pwqp1);
2251 if (equal < 0)
2252 return -1;
2253 if (!equal)
2254 isl_die(ctx, isl_error_unknown,
2255 "unexpected result", return -1);
2258 str = "{ [i] -> ([([i/2] + [i/2])/5]) }";
2259 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2260 str = "{ [i] -> ([(2 * [i/2])/5]) }";
2261 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2263 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2265 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2267 isl_pw_qpolynomial_free(pwqp1);
2269 str = "{ [x] -> ([x/2] + [(x+1)/2]) }";
2270 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2271 str = "{ [x] -> x }";
2272 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2274 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2276 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2278 isl_pw_qpolynomial_free(pwqp1);
2280 str = "{ [i] -> ([i/2]) : i >= 0; [i] -> ([i/3]) : i < 0 }";
2281 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2282 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2283 pwqp1 = isl_pw_qpolynomial_coalesce(pwqp1);
2284 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2285 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2286 isl_pw_qpolynomial_free(pwqp1);
2288 str = "{ [a,b,a] -> (([(2*[a/3]+b)/5]) * ([(2*[a/3]+b)/5])) }";
2289 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2290 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
2291 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2292 set = isl_set_read_from_str(ctx, "{ [a,b,a] }");
2293 pwqp1 = isl_pw_qpolynomial_intersect_domain(pwqp1, set);
2294 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
2295 isl_pw_qpolynomial_free(pwqp1);
2296 isl_pw_qpolynomial_free(pwqp2);
2297 if (equal < 0)
2298 return -1;
2299 if (!equal)
2300 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2302 str = "{ [a,b,c] -> (([(2*[a/3]+1)/5]) * ([(2*[c/3]+1)/5])) : b = 1 }";
2303 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2304 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
2305 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2306 pwqp1 = isl_pw_qpolynomial_fix_val(pwqp1, isl_dim_set, 1,
2307 isl_val_one(ctx));
2308 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
2309 isl_pw_qpolynomial_free(pwqp1);
2310 isl_pw_qpolynomial_free(pwqp2);
2311 if (equal < 0)
2312 return -1;
2313 if (!equal)
2314 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2316 return 0;
2319 void test_split_periods(isl_ctx *ctx)
2321 const char *str;
2322 isl_pw_qpolynomial *pwqp;
2324 str = "{ [U,V] -> 1/3 * U + 2/3 * V - [(U + 2V)/3] + [U/2] : "
2325 "U + 2V + 3 >= 0 and - U -2V >= 0 and - U + 10 >= 0 and "
2326 "U >= 0; [U,V] -> U^2 : U >= 100 }";
2327 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2329 pwqp = isl_pw_qpolynomial_split_periods(pwqp, 2);
2330 assert(pwqp);
2332 isl_pw_qpolynomial_free(pwqp);
2335 void test_union(isl_ctx *ctx)
2337 const char *str;
2338 isl_union_set *uset1, *uset2;
2339 isl_union_map *umap1, *umap2;
2341 str = "{ [i] : 0 <= i <= 1 }";
2342 uset1 = isl_union_set_read_from_str(ctx, str);
2343 str = "{ [1] -> [0] }";
2344 umap1 = isl_union_map_read_from_str(ctx, str);
2346 umap2 = isl_union_set_lex_gt_union_set(isl_union_set_copy(uset1), uset1);
2347 assert(isl_union_map_is_equal(umap1, umap2));
2349 isl_union_map_free(umap1);
2350 isl_union_map_free(umap2);
2352 str = "{ A[i] -> B[i]; B[i] -> C[i]; A[0] -> C[1] }";
2353 umap1 = isl_union_map_read_from_str(ctx, str);
2354 str = "{ A[i]; B[i] }";
2355 uset1 = isl_union_set_read_from_str(ctx, str);
2357 uset2 = isl_union_map_domain(umap1);
2359 assert(isl_union_set_is_equal(uset1, uset2));
2361 isl_union_set_free(uset1);
2362 isl_union_set_free(uset2);
2365 void test_bound(isl_ctx *ctx)
2367 const char *str;
2368 isl_pw_qpolynomial *pwqp;
2369 isl_pw_qpolynomial_fold *pwf;
2371 str = "{ [[a, b, c, d] -> [e]] -> 0 }";
2372 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2373 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
2374 assert(isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in) == 4);
2375 isl_pw_qpolynomial_fold_free(pwf);
2377 str = "{ [[x]->[x]] -> 1 : exists a : x = 2 a }";
2378 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2379 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
2380 assert(isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in) == 1);
2381 isl_pw_qpolynomial_fold_free(pwf);
2384 void test_lift(isl_ctx *ctx)
2386 const char *str;
2387 isl_basic_map *bmap;
2388 isl_basic_set *bset;
2390 str = "{ [i0] : exists e0 : i0 = 4e0 }";
2391 bset = isl_basic_set_read_from_str(ctx, str);
2392 bset = isl_basic_set_lift(bset);
2393 bmap = isl_basic_map_from_range(bset);
2394 bset = isl_basic_map_domain(bmap);
2395 isl_basic_set_free(bset);
2398 struct {
2399 const char *set1;
2400 const char *set2;
2401 int subset;
2402 } subset_tests[] = {
2403 { "{ [112, 0] }",
2404 "{ [i0, i1] : exists (e0 = [(i0 - i1)/16], e1: "
2405 "16e0 <= i0 - i1 and 16e0 >= -15 + i0 - i1 and "
2406 "16e1 <= i1 and 16e0 >= -i1 and 16e1 >= -i0 + i1) }", 1 },
2407 { "{ [65] }",
2408 "{ [i] : exists (e0 = [(255i)/256], e1 = [(127i + 65e0)/191], "
2409 "e2 = [(3i + 61e1)/65], e3 = [(52i + 12e2)/61], "
2410 "e4 = [(2i + e3)/3], e5 = [(4i + e3)/4], e6 = [(8i + e3)/12]: "
2411 "3e4 = 2i + e3 and 4e5 = 4i + e3 and 12e6 = 8i + e3 and "
2412 "i <= 255 and 64e3 >= -45 + 67i and i >= 0 and "
2413 "256e0 <= 255i and 256e0 >= -255 + 255i and "
2414 "191e1 <= 127i + 65e0 and 191e1 >= -190 + 127i + 65e0 and "
2415 "65e2 <= 3i + 61e1 and 65e2 >= -64 + 3i + 61e1 and "
2416 "61e3 <= 52i + 12e2 and 61e3 >= -60 + 52i + 12e2) }", 1 },
2417 { "{ [i] : 0 <= i <= 10 }", "{ rat: [i] : 0 <= i <= 10 }", 1 },
2418 { "{ rat: [i] : 0 <= i <= 10 }", "{ [i] : 0 <= i <= 10 }", 0 },
2419 { "{ rat: [0] }", "{ [i] : 0 <= i <= 10 }", 1 },
2420 { "{ rat: [(1)/2] }", "{ [i] : 0 <= i <= 10 }", 0 },
2421 { "{ [t, i] : (exists (e0 = [(2 + t)/4]: 4e0 <= 2 + t and "
2422 "4e0 >= -1 + t and i >= 57 and i <= 62 and "
2423 "4e0 <= 62 + t - i and 4e0 >= -61 + t + i and "
2424 "t >= 0 and t <= 511 and 4e0 <= -57 + t + i and "
2425 "4e0 >= 58 + t - i and i >= 58 + t and i >= 62 - t)) }",
2426 "{ [i0, i1] : (exists (e0 = [(4 + i0)/4]: 4e0 <= 62 + i0 - i1 and "
2427 "4e0 >= 1 + i0 and i0 >= 0 and i0 <= 511 and "
2428 "4e0 <= -57 + i0 + i1)) or "
2429 "(exists (e0 = [(2 + i0)/4]: 4e0 <= i0 and "
2430 "4e0 >= 58 + i0 - i1 and i0 >= 2 and i0 <= 511 and "
2431 "4e0 >= -61 + i0 + i1)) or "
2432 "(i1 <= 66 - i0 and i0 >= 2 and i1 >= 59 + i0) }", 1 },
2435 static int test_subset(isl_ctx *ctx)
2437 int i;
2438 isl_set *set1, *set2;
2439 int subset;
2441 for (i = 0; i < ARRAY_SIZE(subset_tests); ++i) {
2442 set1 = isl_set_read_from_str(ctx, subset_tests[i].set1);
2443 set2 = isl_set_read_from_str(ctx, subset_tests[i].set2);
2444 subset = isl_set_is_subset(set1, set2);
2445 isl_set_free(set1);
2446 isl_set_free(set2);
2447 if (subset < 0)
2448 return -1;
2449 if (subset != subset_tests[i].subset)
2450 isl_die(ctx, isl_error_unknown,
2451 "incorrect subset result", return -1);
2454 return 0;
2457 struct {
2458 const char *minuend;
2459 const char *subtrahend;
2460 const char *difference;
2461 } subtract_domain_tests[] = {
2462 { "{ A[i] -> B[i] }", "{ A[i] }", "{ }" },
2463 { "{ A[i] -> B[i] }", "{ B[i] }", "{ A[i] -> B[i] }" },
2464 { "{ A[i] -> B[i] }", "{ A[i] : i > 0 }", "{ A[i] -> B[i] : i <= 0 }" },
2467 static int test_subtract(isl_ctx *ctx)
2469 int i;
2470 isl_union_map *umap1, *umap2;
2471 isl_union_pw_multi_aff *upma1, *upma2;
2472 isl_union_set *uset;
2473 int equal;
2475 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
2476 umap1 = isl_union_map_read_from_str(ctx,
2477 subtract_domain_tests[i].minuend);
2478 uset = isl_union_set_read_from_str(ctx,
2479 subtract_domain_tests[i].subtrahend);
2480 umap2 = isl_union_map_read_from_str(ctx,
2481 subtract_domain_tests[i].difference);
2482 umap1 = isl_union_map_subtract_domain(umap1, uset);
2483 equal = isl_union_map_is_equal(umap1, umap2);
2484 isl_union_map_free(umap1);
2485 isl_union_map_free(umap2);
2486 if (equal < 0)
2487 return -1;
2488 if (!equal)
2489 isl_die(ctx, isl_error_unknown,
2490 "incorrect subtract domain result", return -1);
2493 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
2494 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
2495 subtract_domain_tests[i].minuend);
2496 uset = isl_union_set_read_from_str(ctx,
2497 subtract_domain_tests[i].subtrahend);
2498 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
2499 subtract_domain_tests[i].difference);
2500 upma1 = isl_union_pw_multi_aff_subtract_domain(upma1, uset);
2501 equal = isl_union_pw_multi_aff_plain_is_equal(upma1, upma2);
2502 isl_union_pw_multi_aff_free(upma1);
2503 isl_union_pw_multi_aff_free(upma2);
2504 if (equal < 0)
2505 return -1;
2506 if (!equal)
2507 isl_die(ctx, isl_error_unknown,
2508 "incorrect subtract domain result", return -1);
2511 return 0;
2514 int test_factorize(isl_ctx *ctx)
2516 const char *str;
2517 isl_basic_set *bset;
2518 isl_factorizer *f;
2520 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7] : 3i5 <= 2 - 2i0 and "
2521 "i0 >= -2 and i6 >= 1 + i3 and i7 >= 0 and 3i5 >= -2i0 and "
2522 "2i4 <= i2 and i6 >= 1 + 2i0 + 3i1 and i4 <= -1 and "
2523 "i6 >= 1 + 2i0 + 3i5 and i6 <= 2 + 2i0 + 3i5 and "
2524 "3i5 <= 2 - 2i0 - i2 + 3i4 and i6 <= 2 + 2i0 + 3i1 and "
2525 "i0 <= -1 and i7 <= i2 + i3 - 3i4 - i6 and "
2526 "3i5 >= -2i0 - i2 + 3i4 }";
2527 bset = isl_basic_set_read_from_str(ctx, str);
2528 f = isl_basic_set_factorizer(bset);
2529 isl_basic_set_free(bset);
2530 isl_factorizer_free(f);
2531 if (!f)
2532 isl_die(ctx, isl_error_unknown,
2533 "failed to construct factorizer", return -1);
2535 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
2536 "i12 <= 2 + i0 - i11 and 2i8 >= -i4 and i11 >= i1 and "
2537 "3i5 <= -i2 and 2i11 >= -i4 - 2i7 and i11 <= 3 + i0 + 3i9 and "
2538 "i11 <= -i4 - 2i7 and i12 >= -i10 and i2 >= -2 and "
2539 "i11 >= i1 + 3i10 and i11 >= 1 + i0 + 3i9 and "
2540 "i11 <= 1 - i4 - 2i8 and 6i6 <= 6 - i2 and 3i6 >= 1 - i2 and "
2541 "i11 <= 2 + i1 and i12 <= i4 + i11 and i12 >= i0 - i11 and "
2542 "3i5 >= -2 - i2 and i12 >= -1 + i4 + i11 and 3i3 <= 3 - i2 and "
2543 "9i6 <= 11 - i2 + 6i5 and 3i3 >= 1 - i2 and "
2544 "9i6 <= 5 - i2 + 6i3 and i12 <= -1 and i2 <= 0 }";
2545 bset = isl_basic_set_read_from_str(ctx, str);
2546 f = isl_basic_set_factorizer(bset);
2547 isl_basic_set_free(bset);
2548 isl_factorizer_free(f);
2549 if (!f)
2550 isl_die(ctx, isl_error_unknown,
2551 "failed to construct factorizer", return -1);
2553 return 0;
2556 static int check_injective(__isl_take isl_map *map, void *user)
2558 int *injective = user;
2560 *injective = isl_map_is_injective(map);
2561 isl_map_free(map);
2563 if (*injective < 0 || !*injective)
2564 return -1;
2566 return 0;
2569 int test_one_schedule(isl_ctx *ctx, const char *d, const char *w,
2570 const char *r, const char *s, int tilable, int parallel)
2572 int i;
2573 isl_union_set *D;
2574 isl_union_map *W, *R, *S;
2575 isl_union_map *empty;
2576 isl_union_map *dep_raw, *dep_war, *dep_waw, *dep;
2577 isl_union_map *validity, *proximity, *coincidence;
2578 isl_union_map *schedule;
2579 isl_union_map *test;
2580 isl_union_set *delta;
2581 isl_union_set *domain;
2582 isl_set *delta_set;
2583 isl_set *slice;
2584 isl_set *origin;
2585 isl_schedule_constraints *sc;
2586 isl_schedule *sched;
2587 int is_nonneg, is_parallel, is_tilable, is_injection, is_complete;
2589 D = isl_union_set_read_from_str(ctx, d);
2590 W = isl_union_map_read_from_str(ctx, w);
2591 R = isl_union_map_read_from_str(ctx, r);
2592 S = isl_union_map_read_from_str(ctx, s);
2594 W = isl_union_map_intersect_domain(W, isl_union_set_copy(D));
2595 R = isl_union_map_intersect_domain(R, isl_union_set_copy(D));
2597 empty = isl_union_map_empty(isl_union_map_get_space(S));
2598 isl_union_map_compute_flow(isl_union_map_copy(R),
2599 isl_union_map_copy(W), empty,
2600 isl_union_map_copy(S),
2601 &dep_raw, NULL, NULL, NULL);
2602 isl_union_map_compute_flow(isl_union_map_copy(W),
2603 isl_union_map_copy(W),
2604 isl_union_map_copy(R),
2605 isl_union_map_copy(S),
2606 &dep_waw, &dep_war, NULL, NULL);
2608 dep = isl_union_map_union(dep_waw, dep_war);
2609 dep = isl_union_map_union(dep, dep_raw);
2610 validity = isl_union_map_copy(dep);
2611 coincidence = isl_union_map_copy(dep);
2612 proximity = isl_union_map_copy(dep);
2614 sc = isl_schedule_constraints_on_domain(isl_union_set_copy(D));
2615 sc = isl_schedule_constraints_set_validity(sc, validity);
2616 sc = isl_schedule_constraints_set_coincidence(sc, coincidence);
2617 sc = isl_schedule_constraints_set_proximity(sc, proximity);
2618 sched = isl_schedule_constraints_compute_schedule(sc);
2619 schedule = isl_schedule_get_map(sched);
2620 isl_schedule_free(sched);
2621 isl_union_map_free(W);
2622 isl_union_map_free(R);
2623 isl_union_map_free(S);
2625 is_injection = 1;
2626 isl_union_map_foreach_map(schedule, &check_injective, &is_injection);
2628 domain = isl_union_map_domain(isl_union_map_copy(schedule));
2629 is_complete = isl_union_set_is_subset(D, domain);
2630 isl_union_set_free(D);
2631 isl_union_set_free(domain);
2633 test = isl_union_map_reverse(isl_union_map_copy(schedule));
2634 test = isl_union_map_apply_range(test, dep);
2635 test = isl_union_map_apply_range(test, schedule);
2637 delta = isl_union_map_deltas(test);
2638 if (isl_union_set_n_set(delta) == 0) {
2639 is_tilable = 1;
2640 is_parallel = 1;
2641 is_nonneg = 1;
2642 isl_union_set_free(delta);
2643 } else {
2644 delta_set = isl_set_from_union_set(delta);
2646 slice = isl_set_universe(isl_set_get_space(delta_set));
2647 for (i = 0; i < tilable; ++i)
2648 slice = isl_set_lower_bound_si(slice, isl_dim_set, i, 0);
2649 is_tilable = isl_set_is_subset(delta_set, slice);
2650 isl_set_free(slice);
2652 slice = isl_set_universe(isl_set_get_space(delta_set));
2653 for (i = 0; i < parallel; ++i)
2654 slice = isl_set_fix_si(slice, isl_dim_set, i, 0);
2655 is_parallel = isl_set_is_subset(delta_set, slice);
2656 isl_set_free(slice);
2658 origin = isl_set_universe(isl_set_get_space(delta_set));
2659 for (i = 0; i < isl_set_dim(origin, isl_dim_set); ++i)
2660 origin = isl_set_fix_si(origin, isl_dim_set, i, 0);
2662 delta_set = isl_set_union(delta_set, isl_set_copy(origin));
2663 delta_set = isl_set_lexmin(delta_set);
2665 is_nonneg = isl_set_is_equal(delta_set, origin);
2667 isl_set_free(origin);
2668 isl_set_free(delta_set);
2671 if (is_nonneg < 0 || is_parallel < 0 || is_tilable < 0 ||
2672 is_injection < 0 || is_complete < 0)
2673 return -1;
2674 if (!is_complete)
2675 isl_die(ctx, isl_error_unknown,
2676 "generated schedule incomplete", return -1);
2677 if (!is_injection)
2678 isl_die(ctx, isl_error_unknown,
2679 "generated schedule not injective on each statement",
2680 return -1);
2681 if (!is_nonneg)
2682 isl_die(ctx, isl_error_unknown,
2683 "negative dependences in generated schedule",
2684 return -1);
2685 if (!is_tilable)
2686 isl_die(ctx, isl_error_unknown,
2687 "generated schedule not as tilable as expected",
2688 return -1);
2689 if (!is_parallel)
2690 isl_die(ctx, isl_error_unknown,
2691 "generated schedule not as parallel as expected",
2692 return -1);
2694 return 0;
2697 static __isl_give isl_union_map *compute_schedule(isl_ctx *ctx,
2698 const char *domain, const char *validity, const char *proximity)
2700 isl_union_set *dom;
2701 isl_union_map *dep;
2702 isl_union_map *prox;
2703 isl_schedule_constraints *sc;
2704 isl_schedule *schedule;
2705 isl_union_map *sched;
2707 dom = isl_union_set_read_from_str(ctx, domain);
2708 dep = isl_union_map_read_from_str(ctx, validity);
2709 prox = isl_union_map_read_from_str(ctx, proximity);
2710 sc = isl_schedule_constraints_on_domain(dom);
2711 sc = isl_schedule_constraints_set_validity(sc, dep);
2712 sc = isl_schedule_constraints_set_proximity(sc, prox);
2713 schedule = isl_schedule_constraints_compute_schedule(sc);
2714 sched = isl_schedule_get_map(schedule);
2715 isl_schedule_free(schedule);
2717 return sched;
2720 /* Check that a schedule can be constructed on the given domain
2721 * with the given validity and proximity constraints.
2723 static int test_has_schedule(isl_ctx *ctx, const char *domain,
2724 const char *validity, const char *proximity)
2726 isl_union_map *sched;
2728 sched = compute_schedule(ctx, domain, validity, proximity);
2729 if (!sched)
2730 return -1;
2732 isl_union_map_free(sched);
2733 return 0;
2736 int test_special_schedule(isl_ctx *ctx, const char *domain,
2737 const char *validity, const char *proximity, const char *expected_sched)
2739 isl_union_map *sched1, *sched2;
2740 int equal;
2742 sched1 = compute_schedule(ctx, domain, validity, proximity);
2743 sched2 = isl_union_map_read_from_str(ctx, expected_sched);
2745 equal = isl_union_map_is_equal(sched1, sched2);
2746 isl_union_map_free(sched1);
2747 isl_union_map_free(sched2);
2749 if (equal < 0)
2750 return -1;
2751 if (!equal)
2752 isl_die(ctx, isl_error_unknown, "unexpected schedule",
2753 return -1);
2755 return 0;
2758 /* Check that the schedule map is properly padded, even after being
2759 * reconstructed from the band forest.
2761 static int test_padded_schedule(isl_ctx *ctx)
2763 const char *str;
2764 isl_union_set *D;
2765 isl_union_map *validity, *proximity;
2766 isl_schedule_constraints *sc;
2767 isl_schedule *sched;
2768 isl_union_map *map1, *map2;
2769 isl_band_list *list;
2770 int equal;
2772 str = "[N] -> { S0[i] : 0 <= i <= N; S1[i, j] : 0 <= i, j <= N }";
2773 D = isl_union_set_read_from_str(ctx, str);
2774 validity = isl_union_map_empty(isl_union_set_get_space(D));
2775 proximity = isl_union_map_copy(validity);
2776 sc = isl_schedule_constraints_on_domain(D);
2777 sc = isl_schedule_constraints_set_validity(sc, validity);
2778 sc = isl_schedule_constraints_set_proximity(sc, proximity);
2779 sched = isl_schedule_constraints_compute_schedule(sc);
2780 map1 = isl_schedule_get_map(sched);
2781 list = isl_schedule_get_band_forest(sched);
2782 isl_band_list_free(list);
2783 map2 = isl_schedule_get_map(sched);
2784 isl_schedule_free(sched);
2785 equal = isl_union_map_is_equal(map1, map2);
2786 isl_union_map_free(map1);
2787 isl_union_map_free(map2);
2789 if (equal < 0)
2790 return -1;
2791 if (!equal)
2792 isl_die(ctx, isl_error_unknown,
2793 "reconstructed schedule map not the same as original",
2794 return -1);
2796 return 0;
2799 /* Input for testing of schedule construction based on
2800 * conditional constraints.
2802 * domain is the iteration domain
2803 * flow are the flow dependences, which determine the validity and
2804 * proximity constraints
2805 * condition are the conditions on the conditional validity constraints
2806 * conditional_validity are the conditional validity constraints
2807 * outer_band_n is the expected number of members in the outer band
2809 struct {
2810 const char *domain;
2811 const char *flow;
2812 const char *condition;
2813 const char *conditional_validity;
2814 int outer_band_n;
2815 } live_range_tests[] = {
2816 /* Contrived example that illustrates that we need to keep
2817 * track of tagged condition dependences and
2818 * tagged conditional validity dependences
2819 * in isl_sched_edge separately.
2820 * In particular, the conditional validity constraints on A
2821 * cannot be satisfied,
2822 * but they can be ignored because there are no corresponding
2823 * condition constraints. However, we do have an additional
2824 * conditional validity constraint that maps to the same
2825 * dependence relation
2826 * as the condition constraint on B. If we did not make a distinction
2827 * between tagged condition and tagged conditional validity
2828 * dependences, then we
2829 * could end up treating this shared dependence as an condition
2830 * constraint on A, forcing a localization of the conditions,
2831 * which is impossible.
2833 { "{ S[i] : 0 <= 1 < 100; T[i] : 0 <= 1 < 100 }",
2834 "{ S[i] -> S[i+1] : 0 <= i < 99 }",
2835 "{ [S[i] -> B[]] -> [S[i+1] -> B[]] : 0 <= i < 99 }",
2836 "{ [S[i] -> A[]] -> [T[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
2837 "[T[i] -> A[]] -> [S[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
2838 "[S[i] -> A[]] -> [S[i+1] -> A[]] : 0 <= i < 99 }",
2841 /* TACO 2013 Fig. 7 */
2842 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
2843 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
2844 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
2845 "[n] -> { [S1[i,j] -> t[]] -> [S2[i,j] -> t[]] : 0 <= i,j < n;"
2846 "[S2[i,j] -> x1[]] -> [S2[i,j+1] -> x1[]] : "
2847 "0 <= i < n and 0 <= j < n - 1 }",
2848 "[n] -> { [S2[i,j] -> t[]] -> [S1[i,j'] -> t[]] : "
2849 "0 <= i < n and 0 <= j < j' < n;"
2850 "[S2[i,j] -> t[]] -> [S1[i',j'] -> t[]] : "
2851 "0 <= i < i' < n and 0 <= j,j' < n;"
2852 "[S2[i,j] -> x1[]] -> [S2[i,j'] -> x1[]] : "
2853 "0 <= i,j,j' < n and j < j' }",
2856 /* TACO 2013 Fig. 7, without tags */
2857 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
2858 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
2859 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
2860 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
2861 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
2862 "[n] -> { S2[i,j] -> S1[i,j'] : 0 <= i < n and 0 <= j < j' < n;"
2863 "S2[i,j] -> S1[i',j'] : 0 <= i < i' < n and 0 <= j,j' < n;"
2864 "S2[i,j] -> S2[i,j'] : 0 <= i,j,j' < n and j < j' }",
2867 /* TACO 2013 Fig. 12 */
2868 { "{ S1[i,0] : 0 <= i <= 1; S2[i,j] : 0 <= i <= 1 and 1 <= j <= 2;"
2869 "S3[i,3] : 0 <= i <= 1 }",
2870 "{ S1[i,0] -> S2[i,1] : 0 <= i <= 1;"
2871 "S2[i,1] -> S2[i,2] : 0 <= i <= 1;"
2872 "S2[i,2] -> S3[i,3] : 0 <= i <= 1 }",
2873 "{ [S1[i,0]->t[]] -> [S2[i,1]->t[]] : 0 <= i <= 1;"
2874 "[S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
2875 "[S2[i,2]->t[]] -> [S3[i,3]->t[]] : 0 <= i <= 1 }",
2876 "{ [S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
2877 "[S2[0,j]->t[]] -> [S2[1,j']->t[]] : 1 <= j,j' <= 2;"
2878 "[S2[0,j]->t[]] -> [S1[1,0]->t[]] : 1 <= j <= 2;"
2879 "[S3[0,3]->t[]] -> [S2[1,j]->t[]] : 1 <= j <= 2;"
2880 "[S3[0,3]->t[]] -> [S1[1,0]->t[]] }",
2885 /* Test schedule construction based on conditional constraints.
2886 * In particular, check the number of members in the outer band
2887 * as an indication of whether tiling is possible or not.
2889 static int test_conditional_schedule_constraints(isl_ctx *ctx)
2891 int i;
2892 isl_union_set *domain;
2893 isl_union_map *condition;
2894 isl_union_map *flow;
2895 isl_union_map *validity;
2896 isl_schedule_constraints *sc;
2897 isl_schedule *schedule;
2898 isl_band_list *list;
2899 isl_band *band;
2900 int n_member;
2902 for (i = 0; i < ARRAY_SIZE(live_range_tests); ++i) {
2903 domain = isl_union_set_read_from_str(ctx,
2904 live_range_tests[i].domain);
2905 flow = isl_union_map_read_from_str(ctx,
2906 live_range_tests[i].flow);
2907 condition = isl_union_map_read_from_str(ctx,
2908 live_range_tests[i].condition);
2909 validity = isl_union_map_read_from_str(ctx,
2910 live_range_tests[i].conditional_validity);
2911 sc = isl_schedule_constraints_on_domain(domain);
2912 sc = isl_schedule_constraints_set_validity(sc,
2913 isl_union_map_copy(flow));
2914 sc = isl_schedule_constraints_set_proximity(sc, flow);
2915 sc = isl_schedule_constraints_set_conditional_validity(sc,
2916 condition, validity);
2917 schedule = isl_schedule_constraints_compute_schedule(sc);
2918 list = isl_schedule_get_band_forest(schedule);
2919 band = isl_band_list_get_band(list, 0);
2920 n_member = isl_band_n_member(band);
2921 isl_band_free(band);
2922 isl_band_list_free(list);
2923 isl_schedule_free(schedule);
2925 if (!schedule)
2926 return -1;
2927 if (n_member != live_range_tests[i].outer_band_n)
2928 isl_die(ctx, isl_error_unknown,
2929 "unexpected number of members in outer band",
2930 return -1);
2932 return 0;
2935 int test_schedule(isl_ctx *ctx)
2937 const char *D, *W, *R, *V, *P, *S;
2939 /* Handle resulting schedule with zero bands. */
2940 if (test_one_schedule(ctx, "{[]}", "{}", "{}", "{[] -> []}", 0, 0) < 0)
2941 return -1;
2943 /* Jacobi */
2944 D = "[T,N] -> { S1[t,i] : 1 <= t <= T and 2 <= i <= N - 1 }";
2945 W = "{ S1[t,i] -> a[t,i] }";
2946 R = "{ S1[t,i] -> a[t-1,i]; S1[t,i] -> a[t-1,i-1]; "
2947 "S1[t,i] -> a[t-1,i+1] }";
2948 S = "{ S1[t,i] -> [t,i] }";
2949 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
2950 return -1;
2952 /* Fig. 5 of CC2008 */
2953 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and j >= 2 and "
2954 "j <= -1 + N }";
2955 W = "[N] -> { S_0[i, j] -> a[i, j] : i >= 0 and i <= -1 + N and "
2956 "j >= 2 and j <= -1 + N }";
2957 R = "[N] -> { S_0[i, j] -> a[j, i] : i >= 0 and i <= -1 + N and "
2958 "j >= 2 and j <= -1 + N; "
2959 "S_0[i, j] -> a[i, -1 + j] : i >= 0 and i <= -1 + N and "
2960 "j >= 2 and j <= -1 + N }";
2961 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
2962 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
2963 return -1;
2965 D = "{ S1[i] : 0 <= i <= 10; S2[i] : 0 <= i <= 9 }";
2966 W = "{ S1[i] -> a[i] }";
2967 R = "{ S2[i] -> a[i+1] }";
2968 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2969 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
2970 return -1;
2972 D = "{ S1[i] : 0 <= i < 10; S2[i] : 0 <= i < 10 }";
2973 W = "{ S1[i] -> a[i] }";
2974 R = "{ S2[i] -> a[9-i] }";
2975 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2976 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
2977 return -1;
2979 D = "[N] -> { S1[i] : 0 <= i < N; S2[i] : 0 <= i < N }";
2980 W = "{ S1[i] -> a[i] }";
2981 R = "[N] -> { S2[i] -> a[N-1-i] }";
2982 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2983 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
2984 return -1;
2986 D = "{ S1[i] : 0 < i < 10; S2[i] : 0 <= i < 10 }";
2987 W = "{ S1[i] -> a[i]; S2[i] -> b[i] }";
2988 R = "{ S2[i] -> a[i]; S1[i] -> b[i-1] }";
2989 S = "{ S1[i] -> [i,0]; S2[i] -> [i,1] }";
2990 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2991 return -1;
2993 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
2994 W = "{ S1[i] -> a[0,i]; S2[i,j] -> a[i,j] }";
2995 R = "{ S2[i,j] -> a[i-1,j] }";
2996 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
2997 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
2998 return -1;
3000 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
3001 W = "{ S1[i] -> a[i,0]; S2[i,j] -> a[i,j] }";
3002 R = "{ S2[i,j] -> a[i,j-1] }";
3003 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
3004 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
3005 return -1;
3007 D = "[N] -> { S_0[]; S_1[i] : i >= 0 and i <= -1 + N; S_2[] }";
3008 W = "[N] -> { S_0[] -> a[0]; S_2[] -> b[0]; "
3009 "S_1[i] -> a[1 + i] : i >= 0 and i <= -1 + N }";
3010 R = "[N] -> { S_2[] -> a[N]; S_1[i] -> a[i] : i >= 0 and i <= -1 + N }";
3011 S = "[N] -> { S_1[i] -> [1, i, 0]; S_2[] -> [2, 0, 1]; "
3012 "S_0[] -> [0, 0, 0] }";
3013 if (test_one_schedule(ctx, D, W, R, S, 1, 0) < 0)
3014 return -1;
3015 ctx->opt->schedule_parametric = 0;
3016 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3017 return -1;
3018 ctx->opt->schedule_parametric = 1;
3020 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i] : 1 <= i <= N; "
3021 "S3[i,j] : 1 <= i,j <= N; S4[i] : 1 <= i <= N }";
3022 W = "{ S1[i] -> a[i,0]; S2[i] -> a[0,i]; S3[i,j] -> a[i,j] }";
3023 R = "[N] -> { S3[i,j] -> a[i-1,j]; S3[i,j] -> a[i,j-1]; "
3024 "S4[i] -> a[i,N] }";
3025 S = "{ S1[i] -> [0,i,0]; S2[i] -> [1,i,0]; S3[i,j] -> [2,i,j]; "
3026 "S4[i] -> [4,i,0] }";
3027 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
3028 return -1;
3030 D = "[N] -> { S_0[i, j] : i >= 1 and i <= N and j >= 1 and j <= N }";
3031 W = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
3032 "j <= N }";
3033 R = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
3034 "j <= N; "
3035 "S_0[i, j] -> a[i, j] : i >= 1 and i <= N and j >= 1 and "
3036 "j <= N }";
3037 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
3038 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3039 return -1;
3041 D = "[N] -> { S_0[t] : t >= 0 and t <= -1 + N; "
3042 " S_2[t] : t >= 0 and t <= -1 + N; "
3043 " S_1[t, i] : t >= 0 and t <= -1 + N and i >= 0 and "
3044 "i <= -1 + N }";
3045 W = "[N] -> { S_0[t] -> a[t, 0] : t >= 0 and t <= -1 + N; "
3046 " S_2[t] -> b[t] : t >= 0 and t <= -1 + N; "
3047 " S_1[t, i] -> a[t, 1 + i] : t >= 0 and t <= -1 + N and "
3048 "i >= 0 and i <= -1 + N }";
3049 R = "[N] -> { S_1[t, i] -> a[t, i] : t >= 0 and t <= -1 + N and "
3050 "i >= 0 and i <= -1 + N; "
3051 " S_2[t] -> a[t, N] : t >= 0 and t <= -1 + N }";
3052 S = "[N] -> { S_2[t] -> [0, t, 2]; S_1[t, i] -> [0, t, 1, i, 0]; "
3053 " S_0[t] -> [0, t, 0] }";
3055 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
3056 return -1;
3057 ctx->opt->schedule_parametric = 0;
3058 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3059 return -1;
3060 ctx->opt->schedule_parametric = 1;
3062 D = "[N] -> { S1[i,j] : 0 <= i,j < N; S2[i,j] : 0 <= i,j < N }";
3063 S = "{ S1[i,j] -> [0,i,j]; S2[i,j] -> [1,i,j] }";
3064 if (test_one_schedule(ctx, D, "{}", "{}", S, 2, 2) < 0)
3065 return -1;
3067 D = "[M, N] -> { S_1[i] : i >= 0 and i <= -1 + M; "
3068 "S_0[i, j] : i >= 0 and i <= -1 + M and j >= 0 and j <= -1 + N }";
3069 W = "[M, N] -> { S_0[i, j] -> a[j] : i >= 0 and i <= -1 + M and "
3070 "j >= 0 and j <= -1 + N; "
3071 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
3072 R = "[M, N] -> { S_0[i, j] -> a[0] : i >= 0 and i <= -1 + M and "
3073 "j >= 0 and j <= -1 + N; "
3074 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
3075 S = "[M, N] -> { S_1[i] -> [1, i, 0]; S_0[i, j] -> [0, i, 0, j, 0] }";
3076 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3077 return -1;
3079 D = "{ S_0[i] : i >= 0 }";
3080 W = "{ S_0[i] -> a[i] : i >= 0 }";
3081 R = "{ S_0[i] -> a[0] : i >= 0 }";
3082 S = "{ S_0[i] -> [0, i, 0] }";
3083 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3084 return -1;
3086 D = "{ S_0[i] : i >= 0; S_1[i] : i >= 0 }";
3087 W = "{ S_0[i] -> a[i] : i >= 0; S_1[i] -> b[i] : i >= 0 }";
3088 R = "{ S_0[i] -> b[0] : i >= 0; S_1[i] -> a[i] : i >= 0 }";
3089 S = "{ S_1[i] -> [0, i, 1]; S_0[i] -> [0, i, 0] }";
3090 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3091 return -1;
3093 D = "[n] -> { S_0[j, k] : j <= -1 + n and j >= 0 and "
3094 "k <= -1 + n and k >= 0 }";
3095 W = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and " "k <= -1 + n and k >= 0 }";
3096 R = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and "
3097 "k <= -1 + n and k >= 0; "
3098 "S_0[j, k] -> B[k] : j <= -1 + n and j >= 0 and "
3099 "k <= -1 + n and k >= 0; "
3100 "S_0[j, k] -> A[k] : j <= -1 + n and j >= 0 and "
3101 "k <= -1 + n and k >= 0 }";
3102 S = "[n] -> { S_0[j, k] -> [2, j, k] }";
3103 ctx->opt->schedule_outer_coincidence = 1;
3104 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3105 return -1;
3106 ctx->opt->schedule_outer_coincidence = 0;
3108 D = "{Stmt_for_body24[i0, i1, i2, i3]:"
3109 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 6 and i2 >= 2 and "
3110 "i2 <= 6 - i1 and i3 >= 0 and i3 <= -1 + i2;"
3111 "Stmt_for_body24[i0, i1, 1, 0]:"
3112 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 5;"
3113 "Stmt_for_body7[i0, i1, i2]:"
3114 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 7 and i2 >= 0 and "
3115 "i2 <= 7 }";
3117 V = "{Stmt_for_body24[0, i1, i2, i3] -> "
3118 "Stmt_for_body24[1, i1, i2, i3]:"
3119 "i3 >= 0 and i3 <= -1 + i2 and i1 >= 0 and i2 <= 6 - i1 and "
3120 "i2 >= 1;"
3121 "Stmt_for_body24[0, i1, i2, i3] -> "
3122 "Stmt_for_body7[1, 1 + i1 + i3, 1 + i1 + i2]:"
3123 "i3 <= -1 + i2 and i2 <= 6 - i1 and i2 >= 1 and i1 >= 0 and "
3124 "i3 >= 0;"
3125 "Stmt_for_body24[0, i1, i2, i3] ->"
3126 "Stmt_for_body7[1, i1, 1 + i1 + i3]:"
3127 "i3 >= 0 and i2 <= 6 - i1 and i1 >= 0 and i3 <= -1 + i2;"
3128 "Stmt_for_body7[0, i1, i2] -> Stmt_for_body7[1, i1, i2]:"
3129 "(i2 >= 1 + i1 and i2 <= 6 and i1 >= 0 and i1 <= 4) or "
3130 "(i2 >= 3 and i2 <= 7 and i1 >= 1 and i2 >= 1 + i1) or "
3131 "(i2 >= 0 and i2 <= i1 and i2 >= -7 + i1 and i1 <= 7);"
3132 "Stmt_for_body7[0, i1, 1 + i1] -> Stmt_for_body7[1, i1, 1 + i1]:"
3133 "i1 <= 6 and i1 >= 0;"
3134 "Stmt_for_body7[0, 0, 7] -> Stmt_for_body7[1, 0, 7];"
3135 "Stmt_for_body7[i0, i1, i2] -> "
3136 "Stmt_for_body24[i0, o1, -1 + i2 - o1, -1 + i1 - o1]:"
3137 "i0 >= 0 and i0 <= 1 and o1 >= 0 and i2 >= 1 + i1 and "
3138 "o1 <= -2 + i2 and i2 <= 7 and o1 <= -1 + i1;"
3139 "Stmt_for_body7[i0, i1, i2] -> "
3140 "Stmt_for_body24[i0, i1, o2, -1 - i1 + i2]:"
3141 "i0 >= 0 and i0 <= 1 and i1 >= 0 and o2 >= -i1 + i2 and "
3142 "o2 >= 1 and o2 <= 6 - i1 and i2 >= 1 + i1 }";
3143 P = V;
3144 S = "{ Stmt_for_body24[i0, i1, i2, i3] -> "
3145 "[i0, 5i0 + i1, 6i0 + i1 + i2, 1 + 6i0 + i1 + i2 + i3, 1];"
3146 "Stmt_for_body7[i0, i1, i2] -> [0, 5i0, 6i0 + i1, 6i0 + i2, 0] }";
3148 if (test_special_schedule(ctx, D, V, P, S) < 0)
3149 return -1;
3151 D = "{ S_0[i, j] : i >= 1 and i <= 10 and j >= 1 and j <= 8 }";
3152 V = "{ S_0[i, j] -> S_0[i, 1 + j] : i >= 1 and i <= 10 and "
3153 "j >= 1 and j <= 7;"
3154 "S_0[i, j] -> S_0[1 + i, j] : i >= 1 and i <= 9 and "
3155 "j >= 1 and j <= 8 }";
3156 P = "{ }";
3157 S = "{ S_0[i, j] -> [i + j, j] }";
3158 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3159 if (test_special_schedule(ctx, D, V, P, S) < 0)
3160 return -1;
3161 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3163 /* Fig. 1 from Feautrier's "Some Efficient Solutions..." pt. 2, 1992 */
3164 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and "
3165 "j >= 0 and j <= -1 + i }";
3166 V = "[N] -> { S_0[i, j] -> S_0[i, 1 + j] : j <= -2 + i and "
3167 "i <= -1 + N and j >= 0;"
3168 "S_0[i, -1 + i] -> S_0[1 + i, 0] : i >= 1 and "
3169 "i <= -2 + N }";
3170 P = "{ }";
3171 S = "{ S_0[i, j] -> [i, j] }";
3172 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3173 if (test_special_schedule(ctx, D, V, P, S) < 0)
3174 return -1;
3175 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3177 /* Test both algorithms on a case with only proximity dependences. */
3178 D = "{ S[i,j] : 0 <= i <= 10 }";
3179 V = "{ }";
3180 P = "{ S[i,j] -> S[i+1,j] : 0 <= i,j <= 10 }";
3181 S = "{ S[i, j] -> [j, i] }";
3182 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3183 if (test_special_schedule(ctx, D, V, P, S) < 0)
3184 return -1;
3185 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3186 if (test_special_schedule(ctx, D, V, P, S) < 0)
3187 return -1;
3189 D = "{ A[a]; B[] }";
3190 V = "{}";
3191 P = "{ A[a] -> B[] }";
3192 if (test_has_schedule(ctx, D, V, P) < 0)
3193 return -1;
3195 if (test_padded_schedule(ctx) < 0)
3196 return -1;
3198 /* Check that check for progress is not confused by rational
3199 * solution.
3201 D = "[N] -> { S0[i, j] : i >= 0 and i <= N and j >= 0 and j <= N }";
3202 V = "[N] -> { S0[i0, -1 + N] -> S0[2 + i0, 0] : i0 >= 0 and "
3203 "i0 <= -2 + N; "
3204 "S0[i0, i1] -> S0[i0, 1 + i1] : i0 >= 0 and "
3205 "i0 <= N and i1 >= 0 and i1 <= -1 + N }";
3206 P = "{}";
3207 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3208 if (test_has_schedule(ctx, D, V, P) < 0)
3209 return -1;
3210 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3212 /* Check that we allow schedule rows that are only non-trivial
3213 * on some full-dimensional domains.
3215 D = "{ S1[j] : 0 <= j <= 1; S0[]; S2[k] : 0 <= k <= 1 }";
3216 V = "{ S0[] -> S1[j] : 0 <= j <= 1; S2[0] -> S0[];"
3217 "S1[j] -> S2[1] : 0 <= j <= 1 }";
3218 P = "{}";
3219 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3220 if (test_has_schedule(ctx, D, V, P) < 0)
3221 return -1;
3222 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3224 if (test_conditional_schedule_constraints(ctx) < 0)
3225 return -1;
3227 return 0;
3230 int test_plain_injective(isl_ctx *ctx, const char *str, int injective)
3232 isl_union_map *umap;
3233 int test;
3235 umap = isl_union_map_read_from_str(ctx, str);
3236 test = isl_union_map_plain_is_injective(umap);
3237 isl_union_map_free(umap);
3238 if (test < 0)
3239 return -1;
3240 if (test == injective)
3241 return 0;
3242 if (injective)
3243 isl_die(ctx, isl_error_unknown,
3244 "map not detected as injective", return -1);
3245 else
3246 isl_die(ctx, isl_error_unknown,
3247 "map detected as injective", return -1);
3250 int test_injective(isl_ctx *ctx)
3252 const char *str;
3254 if (test_plain_injective(ctx, "{S[i,j] -> A[0]; T[i,j] -> B[1]}", 0))
3255 return -1;
3256 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> B[0]}", 1))
3257 return -1;
3258 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[1]}", 1))
3259 return -1;
3260 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[0]}", 0))
3261 return -1;
3262 if (test_plain_injective(ctx, "{S[i] -> A[i,0]; T[i] -> A[i,1]}", 1))
3263 return -1;
3264 if (test_plain_injective(ctx, "{S[i] -> A[i]; T[i] -> A[i]}", 0))
3265 return -1;
3266 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[0,1]}", 1))
3267 return -1;
3268 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[1,0]}", 1))
3269 return -1;
3271 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[1,0]}";
3272 if (test_plain_injective(ctx, str, 1))
3273 return -1;
3274 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[0,0]}";
3275 if (test_plain_injective(ctx, str, 0))
3276 return -1;
3278 return 0;
3281 static int aff_plain_is_equal(__isl_keep isl_aff *aff, const char *str)
3283 isl_aff *aff2;
3284 int equal;
3286 if (!aff)
3287 return -1;
3289 aff2 = isl_aff_read_from_str(isl_aff_get_ctx(aff), str);
3290 equal = isl_aff_plain_is_equal(aff, aff2);
3291 isl_aff_free(aff2);
3293 return equal;
3296 static int aff_check_plain_equal(__isl_keep isl_aff *aff, const char *str)
3298 int equal;
3300 equal = aff_plain_is_equal(aff, str);
3301 if (equal < 0)
3302 return -1;
3303 if (!equal)
3304 isl_die(isl_aff_get_ctx(aff), isl_error_unknown,
3305 "result not as expected", return -1);
3306 return 0;
3309 struct {
3310 __isl_give isl_aff *(*fn)(__isl_take isl_aff *aff1,
3311 __isl_take isl_aff *aff2);
3312 } aff_bin_op[] = {
3313 ['+'] = { &isl_aff_add },
3314 ['-'] = { &isl_aff_sub },
3315 ['*'] = { &isl_aff_mul },
3316 ['/'] = { &isl_aff_div },
3319 struct {
3320 const char *arg1;
3321 unsigned char op;
3322 const char *arg2;
3323 const char *res;
3324 } aff_bin_tests[] = {
3325 { "{ [i] -> [i] }", '+', "{ [i] -> [i] }",
3326 "{ [i] -> [2i] }" },
3327 { "{ [i] -> [i] }", '-', "{ [i] -> [i] }",
3328 "{ [i] -> [0] }" },
3329 { "{ [i] -> [i] }", '*', "{ [i] -> [2] }",
3330 "{ [i] -> [2i] }" },
3331 { "{ [i] -> [2] }", '*', "{ [i] -> [i] }",
3332 "{ [i] -> [2i] }" },
3333 { "{ [i] -> [i] }", '/', "{ [i] -> [2] }",
3334 "{ [i] -> [i/2] }" },
3335 { "{ [i] -> [2i] }", '/', "{ [i] -> [2] }",
3336 "{ [i] -> [i] }" },
3337 { "{ [i] -> [i] }", '+', "{ [i] -> [NaN] }",
3338 "{ [i] -> [NaN] }" },
3339 { "{ [i] -> [i] }", '-', "{ [i] -> [NaN] }",
3340 "{ [i] -> [NaN] }" },
3341 { "{ [i] -> [i] }", '*', "{ [i] -> [NaN] }",
3342 "{ [i] -> [NaN] }" },
3343 { "{ [i] -> [2] }", '*', "{ [i] -> [NaN] }",
3344 "{ [i] -> [NaN] }" },
3345 { "{ [i] -> [i] }", '/', "{ [i] -> [NaN] }",
3346 "{ [i] -> [NaN] }" },
3347 { "{ [i] -> [2] }", '/', "{ [i] -> [NaN] }",
3348 "{ [i] -> [NaN] }" },
3349 { "{ [i] -> [NaN] }", '+', "{ [i] -> [i] }",
3350 "{ [i] -> [NaN] }" },
3351 { "{ [i] -> [NaN] }", '-', "{ [i] -> [i] }",
3352 "{ [i] -> [NaN] }" },
3353 { "{ [i] -> [NaN] }", '*', "{ [i] -> [2] }",
3354 "{ [i] -> [NaN] }" },
3355 { "{ [i] -> [NaN] }", '*', "{ [i] -> [i] }",
3356 "{ [i] -> [NaN] }" },
3357 { "{ [i] -> [NaN] }", '/', "{ [i] -> [2] }",
3358 "{ [i] -> [NaN] }" },
3359 { "{ [i] -> [NaN] }", '/', "{ [i] -> [i] }",
3360 "{ [i] -> [NaN] }" },
3363 /* Perform some basic tests of binary operations on isl_aff objects.
3365 static int test_bin_aff(isl_ctx *ctx)
3367 int i;
3368 isl_aff *aff1, *aff2, *res;
3369 __isl_give isl_aff *(*fn)(__isl_take isl_aff *aff1,
3370 __isl_take isl_aff *aff2);
3371 int ok;
3373 for (i = 0; i < ARRAY_SIZE(aff_bin_tests); ++i) {
3374 aff1 = isl_aff_read_from_str(ctx, aff_bin_tests[i].arg1);
3375 aff2 = isl_aff_read_from_str(ctx, aff_bin_tests[i].arg2);
3376 res = isl_aff_read_from_str(ctx, aff_bin_tests[i].res);
3377 fn = aff_bin_op[aff_bin_tests[i].op].fn;
3378 aff1 = fn(aff1, aff2);
3379 if (isl_aff_is_nan(res))
3380 ok = isl_aff_is_nan(aff1);
3381 else
3382 ok = isl_aff_plain_is_equal(aff1, res);
3383 isl_aff_free(aff1);
3384 isl_aff_free(res);
3385 if (ok < 0)
3386 return -1;
3387 if (!ok)
3388 isl_die(ctx, isl_error_unknown,
3389 "unexpected result", return -1);
3392 return 0;
3395 struct {
3396 __isl_give isl_union_pw_multi_aff *(*fn)(
3397 __isl_take isl_union_pw_multi_aff *upma1,
3398 __isl_take isl_union_pw_multi_aff *upma2);
3399 const char *arg1;
3400 const char *arg2;
3401 const char *res;
3402 } upma_bin_tests[] = {
3403 { &isl_union_pw_multi_aff_add, "{ A[] -> [0]; B[0] -> [1] }",
3404 "{ B[x] -> [2] : x >= 0 }", "{ B[0] -> [3] }" },
3405 { &isl_union_pw_multi_aff_union_add, "{ A[] -> [0]; B[0] -> [1] }",
3406 "{ B[x] -> [2] : x >= 0 }",
3407 "{ A[] -> [0]; B[0] -> [3]; B[x] -> [2] : x >= 1 }" },
3410 /* Perform some basic tests of binary operations on
3411 * isl_union_pw_multi_aff objects.
3413 static int test_bin_upma(isl_ctx *ctx)
3415 int i;
3416 isl_union_pw_multi_aff *upma1, *upma2, *res;
3417 int ok;
3419 for (i = 0; i < ARRAY_SIZE(upma_bin_tests); ++i) {
3420 upma1 = isl_union_pw_multi_aff_read_from_str(ctx,
3421 upma_bin_tests[i].arg1);
3422 upma2 = isl_union_pw_multi_aff_read_from_str(ctx,
3423 upma_bin_tests[i].arg2);
3424 res = isl_union_pw_multi_aff_read_from_str(ctx,
3425 upma_bin_tests[i].res);
3426 upma1 = upma_bin_tests[i].fn(upma1, upma2);
3427 ok = isl_union_pw_multi_aff_plain_is_equal(upma1, res);
3428 isl_union_pw_multi_aff_free(upma1);
3429 isl_union_pw_multi_aff_free(res);
3430 if (ok < 0)
3431 return -1;
3432 if (!ok)
3433 isl_die(ctx, isl_error_unknown,
3434 "unexpected result", return -1);
3437 return 0;
3440 int test_aff(isl_ctx *ctx)
3442 const char *str;
3443 isl_set *set;
3444 isl_space *space;
3445 isl_local_space *ls;
3446 isl_aff *aff;
3447 int zero, equal;
3449 if (test_bin_aff(ctx) < 0)
3450 return -1;
3451 if (test_bin_upma(ctx) < 0)
3452 return -1;
3454 space = isl_space_set_alloc(ctx, 0, 1);
3455 ls = isl_local_space_from_space(space);
3456 aff = isl_aff_zero_on_domain(ls);
3458 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
3459 aff = isl_aff_scale_down_ui(aff, 3);
3460 aff = isl_aff_floor(aff);
3461 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
3462 aff = isl_aff_scale_down_ui(aff, 2);
3463 aff = isl_aff_floor(aff);
3464 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
3466 str = "{ [10] }";
3467 set = isl_set_read_from_str(ctx, str);
3468 aff = isl_aff_gist(aff, set);
3470 aff = isl_aff_add_constant_si(aff, -16);
3471 zero = isl_aff_plain_is_zero(aff);
3472 isl_aff_free(aff);
3474 if (zero < 0)
3475 return -1;
3476 if (!zero)
3477 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3479 aff = isl_aff_read_from_str(ctx, "{ [-1] }");
3480 aff = isl_aff_scale_down_ui(aff, 64);
3481 aff = isl_aff_floor(aff);
3482 equal = aff_check_plain_equal(aff, "{ [-1] }");
3483 isl_aff_free(aff);
3484 if (equal < 0)
3485 return -1;
3487 return 0;
3490 int test_dim_max(isl_ctx *ctx)
3492 int equal;
3493 const char *str;
3494 isl_set *set1, *set2;
3495 isl_set *set;
3496 isl_map *map;
3497 isl_pw_aff *pwaff;
3499 str = "[N] -> { [i] : 0 <= i <= min(N,10) }";
3500 set = isl_set_read_from_str(ctx, str);
3501 pwaff = isl_set_dim_max(set, 0);
3502 set1 = isl_set_from_pw_aff(pwaff);
3503 str = "[N] -> { [10] : N >= 10; [N] : N <= 9 and N >= 0 }";
3504 set2 = isl_set_read_from_str(ctx, str);
3505 equal = isl_set_is_equal(set1, set2);
3506 isl_set_free(set1);
3507 isl_set_free(set2);
3508 if (equal < 0)
3509 return -1;
3510 if (!equal)
3511 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3513 str = "[N] -> { [i] : 0 <= i <= max(2N,N+6) }";
3514 set = isl_set_read_from_str(ctx, str);
3515 pwaff = isl_set_dim_max(set, 0);
3516 set1 = isl_set_from_pw_aff(pwaff);
3517 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
3518 set2 = isl_set_read_from_str(ctx, str);
3519 equal = isl_set_is_equal(set1, set2);
3520 isl_set_free(set1);
3521 isl_set_free(set2);
3522 if (equal < 0)
3523 return -1;
3524 if (!equal)
3525 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3527 str = "[N] -> { [i] : 0 <= i <= 2N or 0 <= i <= N+6 }";
3528 set = isl_set_read_from_str(ctx, str);
3529 pwaff = isl_set_dim_max(set, 0);
3530 set1 = isl_set_from_pw_aff(pwaff);
3531 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
3532 set2 = isl_set_read_from_str(ctx, str);
3533 equal = isl_set_is_equal(set1, set2);
3534 isl_set_free(set1);
3535 isl_set_free(set2);
3536 if (equal < 0)
3537 return -1;
3538 if (!equal)
3539 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3541 str = "[N,M] -> { [i,j] -> [([i/16]), i%16, ([j/16]), j%16] : "
3542 "0 <= i < N and 0 <= j < M }";
3543 map = isl_map_read_from_str(ctx, str);
3544 set = isl_map_range(map);
3546 pwaff = isl_set_dim_max(isl_set_copy(set), 0);
3547 set1 = isl_set_from_pw_aff(pwaff);
3548 str = "[N,M] -> { [([(N-1)/16])] : M,N > 0 }";
3549 set2 = isl_set_read_from_str(ctx, str);
3550 equal = isl_set_is_equal(set1, set2);
3551 isl_set_free(set1);
3552 isl_set_free(set2);
3554 pwaff = isl_set_dim_max(isl_set_copy(set), 3);
3555 set1 = isl_set_from_pw_aff(pwaff);
3556 str = "[N,M] -> { [t] : t = min(M-1,15) and M,N > 0 }";
3557 set2 = isl_set_read_from_str(ctx, str);
3558 if (equal >= 0 && equal)
3559 equal = isl_set_is_equal(set1, set2);
3560 isl_set_free(set1);
3561 isl_set_free(set2);
3563 isl_set_free(set);
3565 if (equal < 0)
3566 return -1;
3567 if (!equal)
3568 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3570 /* Check that solutions are properly merged. */
3571 str = "[n] -> { [a, b, c] : c >= -4a - 2b and "
3572 "c <= -1 + n - 4a - 2b and c >= -2b and "
3573 "4a >= -4 + n and c >= 0 }";
3574 set = isl_set_read_from_str(ctx, str);
3575 pwaff = isl_set_dim_min(set, 2);
3576 set1 = isl_set_from_pw_aff(pwaff);
3577 str = "[n] -> { [(0)] : n >= 1 }";
3578 set2 = isl_set_read_from_str(ctx, str);
3579 equal = isl_set_is_equal(set1, set2);
3580 isl_set_free(set1);
3581 isl_set_free(set2);
3583 if (equal < 0)
3584 return -1;
3585 if (!equal)
3586 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3588 /* Check that empty solution lie in the right space. */
3589 str = "[n] -> { [t,a] : 1 = 0 }";
3590 set = isl_set_read_from_str(ctx, str);
3591 pwaff = isl_set_dim_max(set, 0);
3592 set1 = isl_set_from_pw_aff(pwaff);
3593 str = "[n] -> { [t] : 1 = 0 }";
3594 set2 = isl_set_read_from_str(ctx, str);
3595 equal = isl_set_is_equal(set1, set2);
3596 isl_set_free(set1);
3597 isl_set_free(set2);
3599 if (equal < 0)
3600 return -1;
3601 if (!equal)
3602 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3604 return 0;
3607 /* Is "pma" obviously equal to the isl_pw_multi_aff represented by "str"?
3609 static int pw_multi_aff_plain_is_equal(__isl_keep isl_pw_multi_aff *pma,
3610 const char *str)
3612 isl_ctx *ctx;
3613 isl_pw_multi_aff *pma2;
3614 int equal;
3616 if (!pma)
3617 return -1;
3619 ctx = isl_pw_multi_aff_get_ctx(pma);
3620 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
3621 equal = isl_pw_multi_aff_plain_is_equal(pma, pma2);
3622 isl_pw_multi_aff_free(pma2);
3624 return equal;
3627 /* Check that "pma" is obviously equal to the isl_pw_multi_aff
3628 * represented by "str".
3630 static int pw_multi_aff_check_plain_equal(__isl_keep isl_pw_multi_aff *pma,
3631 const char *str)
3633 int equal;
3635 equal = pw_multi_aff_plain_is_equal(pma, str);
3636 if (equal < 0)
3637 return -1;
3638 if (!equal)
3639 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_unknown,
3640 "result not as expected", return -1);
3641 return 0;
3644 /* Basic test for isl_pw_multi_aff_product.
3646 * Check that multiple pieces are properly handled.
3648 static int test_product_pma(isl_ctx *ctx)
3650 int equal;
3651 const char *str;
3652 isl_pw_multi_aff *pma1, *pma2;
3654 str = "{ A[i] -> B[1] : i < 0; A[i] -> B[2] : i >= 0 }";
3655 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
3656 str = "{ C[] -> D[] }";
3657 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
3658 pma1 = isl_pw_multi_aff_product(pma1, pma2);
3659 str = "{ [A[i] -> C[]] -> [B[(1)] -> D[]] : i < 0;"
3660 "[A[i] -> C[]] -> [B[(2)] -> D[]] : i >= 0 }";
3661 equal = pw_multi_aff_check_plain_equal(pma1, str);
3662 isl_pw_multi_aff_free(pma1);
3663 if (equal < 0)
3664 return -1;
3666 return 0;
3669 int test_product(isl_ctx *ctx)
3671 const char *str;
3672 isl_set *set;
3673 isl_union_set *uset1, *uset2;
3674 int ok;
3676 str = "{ A[i] }";
3677 set = isl_set_read_from_str(ctx, str);
3678 set = isl_set_product(set, isl_set_copy(set));
3679 ok = isl_set_is_wrapping(set);
3680 isl_set_free(set);
3681 if (ok < 0)
3682 return -1;
3683 if (!ok)
3684 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3686 str = "{ [] }";
3687 uset1 = isl_union_set_read_from_str(ctx, str);
3688 uset1 = isl_union_set_product(uset1, isl_union_set_copy(uset1));
3689 str = "{ [[] -> []] }";
3690 uset2 = isl_union_set_read_from_str(ctx, str);
3691 ok = isl_union_set_is_equal(uset1, uset2);
3692 isl_union_set_free(uset1);
3693 isl_union_set_free(uset2);
3694 if (ok < 0)
3695 return -1;
3696 if (!ok)
3697 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3699 if (test_product_pma(ctx) < 0)
3700 return -1;
3702 return 0;
3705 /* Check that two sets are not considered disjoint just because
3706 * they have a different set of (named) parameters.
3708 static int test_disjoint(isl_ctx *ctx)
3710 const char *str;
3711 isl_set *set, *set2;
3712 int disjoint;
3714 str = "[n] -> { [[]->[]] }";
3715 set = isl_set_read_from_str(ctx, str);
3716 str = "{ [[]->[]] }";
3717 set2 = isl_set_read_from_str(ctx, str);
3718 disjoint = isl_set_is_disjoint(set, set2);
3719 isl_set_free(set);
3720 isl_set_free(set2);
3721 if (disjoint < 0)
3722 return -1;
3723 if (disjoint)
3724 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3726 return 0;
3729 int test_equal(isl_ctx *ctx)
3731 const char *str;
3732 isl_set *set, *set2;
3733 int equal;
3735 str = "{ S_6[i] }";
3736 set = isl_set_read_from_str(ctx, str);
3737 str = "{ S_7[i] }";
3738 set2 = isl_set_read_from_str(ctx, str);
3739 equal = isl_set_is_equal(set, set2);
3740 isl_set_free(set);
3741 isl_set_free(set2);
3742 if (equal < 0)
3743 return -1;
3744 if (equal)
3745 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3747 return 0;
3750 static int test_plain_fixed(isl_ctx *ctx, __isl_take isl_map *map,
3751 enum isl_dim_type type, unsigned pos, int fixed)
3753 int test;
3755 test = isl_map_plain_is_fixed(map, type, pos, NULL);
3756 isl_map_free(map);
3757 if (test < 0)
3758 return -1;
3759 if (test == fixed)
3760 return 0;
3761 if (fixed)
3762 isl_die(ctx, isl_error_unknown,
3763 "map not detected as fixed", return -1);
3764 else
3765 isl_die(ctx, isl_error_unknown,
3766 "map detected as fixed", return -1);
3769 int test_fixed(isl_ctx *ctx)
3771 const char *str;
3772 isl_map *map;
3774 str = "{ [i] -> [i] }";
3775 map = isl_map_read_from_str(ctx, str);
3776 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 0))
3777 return -1;
3778 str = "{ [i] -> [1] }";
3779 map = isl_map_read_from_str(ctx, str);
3780 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
3781 return -1;
3782 str = "{ S_1[p1] -> [o0] : o0 = -2 and p1 >= 1 and p1 <= 7 }";
3783 map = isl_map_read_from_str(ctx, str);
3784 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
3785 return -1;
3786 map = isl_map_read_from_str(ctx, str);
3787 map = isl_map_neg(map);
3788 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
3789 return -1;
3791 return 0;
3794 struct isl_vertices_test_data {
3795 const char *set;
3796 int n;
3797 const char *vertex[2];
3798 } vertices_tests[] = {
3799 { "{ A[t, i] : t = 12 and i >= 4 and i <= 12 }",
3800 2, { "{ A[12, 4] }", "{ A[12, 12] }" } },
3801 { "{ A[t, i] : t = 14 and i = 1 }",
3802 1, { "{ A[14, 1] }" } },
3805 /* Check that "vertex" corresponds to one of the vertices in data->vertex.
3807 static int find_vertex(__isl_take isl_vertex *vertex, void *user)
3809 struct isl_vertices_test_data *data = user;
3810 isl_ctx *ctx;
3811 isl_multi_aff *ma;
3812 isl_basic_set *bset;
3813 isl_pw_multi_aff *pma;
3814 int i;
3815 int equal;
3817 ctx = isl_vertex_get_ctx(vertex);
3818 bset = isl_vertex_get_domain(vertex);
3819 ma = isl_vertex_get_expr(vertex);
3820 pma = isl_pw_multi_aff_alloc(isl_set_from_basic_set(bset), ma);
3822 for (i = 0; i < data->n; ++i) {
3823 isl_pw_multi_aff *pma_i;
3825 pma_i = isl_pw_multi_aff_read_from_str(ctx, data->vertex[i]);
3826 equal = isl_pw_multi_aff_plain_is_equal(pma, pma_i);
3827 isl_pw_multi_aff_free(pma_i);
3829 if (equal < 0 || equal)
3830 break;
3833 isl_pw_multi_aff_free(pma);
3834 isl_vertex_free(vertex);
3836 if (equal < 0)
3837 return -1;
3839 return equal ? 0 : - 1;
3842 int test_vertices(isl_ctx *ctx)
3844 int i;
3846 for (i = 0; i < ARRAY_SIZE(vertices_tests); ++i) {
3847 isl_basic_set *bset;
3848 isl_vertices *vertices;
3849 int ok = 1;
3850 int n;
3852 bset = isl_basic_set_read_from_str(ctx, vertices_tests[i].set);
3853 vertices = isl_basic_set_compute_vertices(bset);
3854 n = isl_vertices_get_n_vertices(vertices);
3855 if (vertices_tests[i].n != n)
3856 ok = 0;
3857 if (isl_vertices_foreach_vertex(vertices, &find_vertex,
3858 &vertices_tests[i]) < 0)
3859 ok = 0;
3860 isl_vertices_free(vertices);
3861 isl_basic_set_free(bset);
3863 if (!vertices)
3864 return -1;
3865 if (!ok)
3866 isl_die(ctx, isl_error_unknown, "unexpected vertices",
3867 return -1);
3870 return 0;
3873 int test_union_pw(isl_ctx *ctx)
3875 int equal;
3876 const char *str;
3877 isl_union_set *uset;
3878 isl_union_pw_qpolynomial *upwqp1, *upwqp2;
3880 str = "{ [x] -> x^2 }";
3881 upwqp1 = isl_union_pw_qpolynomial_read_from_str(ctx, str);
3882 upwqp2 = isl_union_pw_qpolynomial_copy(upwqp1);
3883 uset = isl_union_pw_qpolynomial_domain(upwqp1);
3884 upwqp1 = isl_union_pw_qpolynomial_copy(upwqp2);
3885 upwqp1 = isl_union_pw_qpolynomial_intersect_domain(upwqp1, uset);
3886 equal = isl_union_pw_qpolynomial_plain_is_equal(upwqp1, upwqp2);
3887 isl_union_pw_qpolynomial_free(upwqp1);
3888 isl_union_pw_qpolynomial_free(upwqp2);
3889 if (equal < 0)
3890 return -1;
3891 if (!equal)
3892 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3894 return 0;
3897 int test_output(isl_ctx *ctx)
3899 char *s;
3900 const char *str;
3901 isl_pw_aff *pa;
3902 isl_printer *p;
3903 int equal;
3905 str = "[x] -> { [1] : x % 4 <= 2; [2] : x = 3 }";
3906 pa = isl_pw_aff_read_from_str(ctx, str);
3908 p = isl_printer_to_str(ctx);
3909 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
3910 p = isl_printer_print_pw_aff(p, pa);
3911 s = isl_printer_get_str(p);
3912 isl_printer_free(p);
3913 isl_pw_aff_free(pa);
3914 if (!s)
3915 equal = -1;
3916 else
3917 equal = !strcmp(s, "4 * floord(x, 4) + 2 >= x ? 1 : 2");
3918 free(s);
3919 if (equal < 0)
3920 return -1;
3921 if (!equal)
3922 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3924 return 0;
3927 int test_sample(isl_ctx *ctx)
3929 const char *str;
3930 isl_basic_set *bset1, *bset2;
3931 int empty, subset;
3933 str = "{ [a, b, c, d, e, f, g, h, i, j, k] : "
3934 "3i >= 1073741823b - c - 1073741823e + f and c >= 0 and "
3935 "3i >= -1 + 3221225466b + c + d - 3221225466e - f and "
3936 "2e >= a - b and 3e <= 2a and 3k <= -a and f <= -1 + a and "
3937 "3i <= 4 - a + 4b + 2c - e - 2f and 3k <= -a + c - f and "
3938 "3h >= -2 + a and 3g >= -3 - a and 3k >= -2 - a and "
3939 "3i >= -2 - a - 2c + 3e + 2f and 3h <= a + c - f and "
3940 "3h >= a + 2147483646b + 2c - 2147483646e - 2f and "
3941 "3g <= -1 - a and 3i <= 1 + c + d - f and a <= 1073741823 and "
3942 "f >= 1 - a + 1073741822b + c + d - 1073741822e and "
3943 "3i >= 1 + 2b - 2c + e + 2f + 3g and "
3944 "1073741822f <= 1073741822 - a + 1073741821b + 1073741822c +"
3945 "d - 1073741821e and "
3946 "3j <= 3 - a + 3b and 3g <= -2 - 2b + c + d - e - f and "
3947 "3j >= 1 - a + b + 2e and "
3948 "3f >= -3 + a + 3221225462b + 3c + d - 3221225465e and "
3949 "3i <= 4 - a + 4b - e and "
3950 "f <= 1073741822 + 1073741822b - 1073741822e and 3h <= a and "
3951 "f >= 0 and 2e <= 4 - a + 5b - d and 2e <= a - b + d and "
3952 "c <= -1 + a and 3i >= -2 - a + 3e and "
3953 "1073741822e <= 1073741823 - a + 1073741822b + c and "
3954 "3g >= -4 + 3221225464b + 3c + d - 3221225467e - 3f and "
3955 "3i >= -1 + 3221225466b + 3c + d - 3221225466e - 3f and "
3956 "1073741823e >= 1 + 1073741823b - d and "
3957 "3i >= 1073741823b + c - 1073741823e - f and "
3958 "3i >= 1 + 2b + e + 3g }";
3959 bset1 = isl_basic_set_read_from_str(ctx, str);
3960 bset2 = isl_basic_set_sample(isl_basic_set_copy(bset1));
3961 empty = isl_basic_set_is_empty(bset2);
3962 subset = isl_basic_set_is_subset(bset2, bset1);
3963 isl_basic_set_free(bset1);
3964 isl_basic_set_free(bset2);
3965 if (empty < 0 || subset < 0)
3966 return -1;
3967 if (empty)
3968 isl_die(ctx, isl_error_unknown, "point not found", return -1);
3969 if (!subset)
3970 isl_die(ctx, isl_error_unknown, "bad point found", return -1);
3972 return 0;
3975 int test_fixed_power(isl_ctx *ctx)
3977 const char *str;
3978 isl_map *map;
3979 isl_int exp;
3980 int equal;
3982 isl_int_init(exp);
3983 str = "{ [i] -> [i + 1] }";
3984 map = isl_map_read_from_str(ctx, str);
3985 isl_int_set_si(exp, 23);
3986 map = isl_map_fixed_power(map, exp);
3987 equal = map_check_equal(map, "{ [i] -> [i + 23] }");
3988 isl_int_clear(exp);
3989 isl_map_free(map);
3990 if (equal < 0)
3991 return -1;
3993 return 0;
3996 int test_slice(isl_ctx *ctx)
3998 const char *str;
3999 isl_map *map;
4000 int equal;
4002 str = "{ [i] -> [j] }";
4003 map = isl_map_read_from_str(ctx, str);
4004 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_out, 0);
4005 equal = map_check_equal(map, "{ [i] -> [i] }");
4006 isl_map_free(map);
4007 if (equal < 0)
4008 return -1;
4010 str = "{ [i] -> [j] }";
4011 map = isl_map_read_from_str(ctx, str);
4012 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_in, 0);
4013 equal = map_check_equal(map, "{ [i] -> [j] }");
4014 isl_map_free(map);
4015 if (equal < 0)
4016 return -1;
4018 str = "{ [i] -> [j] }";
4019 map = isl_map_read_from_str(ctx, str);
4020 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_out, 0);
4021 equal = map_check_equal(map, "{ [i] -> [-i] }");
4022 isl_map_free(map);
4023 if (equal < 0)
4024 return -1;
4026 str = "{ [i] -> [j] }";
4027 map = isl_map_read_from_str(ctx, str);
4028 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_in, 0);
4029 equal = map_check_equal(map, "{ [0] -> [j] }");
4030 isl_map_free(map);
4031 if (equal < 0)
4032 return -1;
4034 str = "{ [i] -> [j] }";
4035 map = isl_map_read_from_str(ctx, str);
4036 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_out, 0);
4037 equal = map_check_equal(map, "{ [i] -> [j] : i > j }");
4038 isl_map_free(map);
4039 if (equal < 0)
4040 return -1;
4042 str = "{ [i] -> [j] }";
4043 map = isl_map_read_from_str(ctx, str);
4044 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_in, 0);
4045 equal = map_check_equal(map, "{ [i] -> [j] : false }");
4046 isl_map_free(map);
4047 if (equal < 0)
4048 return -1;
4050 return 0;
4053 int test_eliminate(isl_ctx *ctx)
4055 const char *str;
4056 isl_map *map;
4057 int equal;
4059 str = "{ [i] -> [j] : i = 2j }";
4060 map = isl_map_read_from_str(ctx, str);
4061 map = isl_map_eliminate(map, isl_dim_out, 0, 1);
4062 equal = map_check_equal(map, "{ [i] -> [j] : exists a : i = 2a }");
4063 isl_map_free(map);
4064 if (equal < 0)
4065 return -1;
4067 return 0;
4070 /* Check that isl_set_dim_residue_class detects that the values of j
4071 * in the set below are all odd and that it does not detect any spurious
4072 * strides.
4074 static int test_residue_class(isl_ctx *ctx)
4076 const char *str;
4077 isl_set *set;
4078 isl_int m, r;
4079 int res;
4081 str = "{ [i,j] : j = 4 i + 1 and 0 <= i <= 100; "
4082 "[i,j] : j = 4 i + 3 and 500 <= i <= 600 }";
4083 set = isl_set_read_from_str(ctx, str);
4084 isl_int_init(m);
4085 isl_int_init(r);
4086 res = isl_set_dim_residue_class(set, 1, &m, &r);
4087 if (res >= 0 &&
4088 (isl_int_cmp_si(m, 2) != 0 || isl_int_cmp_si(r, 1) != 0))
4089 isl_die(ctx, isl_error_unknown, "incorrect residue class",
4090 res = -1);
4091 isl_int_clear(r);
4092 isl_int_clear(m);
4093 isl_set_free(set);
4095 return res;
4098 int test_align_parameters(isl_ctx *ctx)
4100 const char *str;
4101 isl_space *space;
4102 isl_multi_aff *ma1, *ma2;
4103 int equal;
4105 str = "{ A[B[] -> C[]] -> D[E[] -> F[]] }";
4106 ma1 = isl_multi_aff_read_from_str(ctx, str);
4108 space = isl_space_params_alloc(ctx, 1);
4109 space = isl_space_set_dim_name(space, isl_dim_param, 0, "N");
4110 ma1 = isl_multi_aff_align_params(ma1, space);
4112 str = "[N] -> { A[B[] -> C[]] -> D[E[] -> F[]] }";
4113 ma2 = isl_multi_aff_read_from_str(ctx, str);
4115 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
4117 isl_multi_aff_free(ma1);
4118 isl_multi_aff_free(ma2);
4120 if (equal < 0)
4121 return -1;
4122 if (!equal)
4123 isl_die(ctx, isl_error_unknown,
4124 "result not as expected", return -1);
4126 return 0;
4129 static int test_list(isl_ctx *ctx)
4131 isl_id *a, *b, *c, *d, *id;
4132 isl_id_list *list;
4133 int ok;
4135 a = isl_id_alloc(ctx, "a", NULL);
4136 b = isl_id_alloc(ctx, "b", NULL);
4137 c = isl_id_alloc(ctx, "c", NULL);
4138 d = isl_id_alloc(ctx, "d", NULL);
4140 list = isl_id_list_alloc(ctx, 4);
4141 list = isl_id_list_add(list, a);
4142 list = isl_id_list_add(list, b);
4143 list = isl_id_list_add(list, c);
4144 list = isl_id_list_add(list, d);
4145 list = isl_id_list_drop(list, 1, 1);
4147 if (isl_id_list_n_id(list) != 3) {
4148 isl_id_list_free(list);
4149 isl_die(ctx, isl_error_unknown,
4150 "unexpected number of elements in list", return -1);
4153 id = isl_id_list_get_id(list, 0);
4154 ok = id == a;
4155 isl_id_free(id);
4156 id = isl_id_list_get_id(list, 1);
4157 ok = ok && id == c;
4158 isl_id_free(id);
4159 id = isl_id_list_get_id(list, 2);
4160 ok = ok && id == d;
4161 isl_id_free(id);
4163 isl_id_list_free(list);
4165 if (!ok)
4166 isl_die(ctx, isl_error_unknown,
4167 "unexpected elements in list", return -1);
4169 return 0;
4172 const char *set_conversion_tests[] = {
4173 "[N] -> { [i] : N - 1 <= 2 i <= N }",
4174 "[N] -> { [i] : exists a : i = 4 a and N - 1 <= i <= N }",
4175 "[N] -> { [i,j] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
4176 "[N] -> { [[i]->[j]] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
4177 "[N] -> { [3*floor(N/2) + 5*floor(N/3)] }",
4180 /* Check that converting from isl_set to isl_pw_multi_aff and back
4181 * to isl_set produces the original isl_set.
4183 static int test_set_conversion(isl_ctx *ctx)
4185 int i;
4186 const char *str;
4187 isl_set *set1, *set2;
4188 isl_pw_multi_aff *pma;
4189 int equal;
4191 for (i = 0; i < ARRAY_SIZE(set_conversion_tests); ++i) {
4192 str = set_conversion_tests[i];
4193 set1 = isl_set_read_from_str(ctx, str);
4194 pma = isl_pw_multi_aff_from_set(isl_set_copy(set1));
4195 set2 = isl_set_from_pw_multi_aff(pma);
4196 equal = isl_set_is_equal(set1, set2);
4197 isl_set_free(set1);
4198 isl_set_free(set2);
4200 if (equal < 0)
4201 return -1;
4202 if (!equal)
4203 isl_die(ctx, isl_error_unknown, "bad conversion",
4204 return -1);
4207 return 0;
4210 /* Check that converting from isl_map to isl_pw_multi_aff and back
4211 * to isl_map produces the original isl_map.
4213 static int test_map_conversion(isl_ctx *ctx)
4215 const char *str;
4216 isl_map *map1, *map2;
4217 isl_pw_multi_aff *pma;
4218 int equal;
4220 str = "{ [a, b, c, d] -> s0[a, b, e, f] : "
4221 "exists (e0 = [(a - 2c)/3], e1 = [(-4 + b - 5d)/9], "
4222 "e2 = [(-d + f)/9]: 3e0 = a - 2c and 9e1 = -4 + b - 5d and "
4223 "9e2 = -d + f and f >= 0 and f <= 8 and 9e >= -5 - 2a and "
4224 "9e <= -2 - 2a) }";
4225 map1 = isl_map_read_from_str(ctx, str);
4226 pma = isl_pw_multi_aff_from_map(isl_map_copy(map1));
4227 map2 = isl_map_from_pw_multi_aff(pma);
4228 equal = isl_map_is_equal(map1, map2);
4229 isl_map_free(map1);
4230 isl_map_free(map2);
4232 if (equal < 0)
4233 return -1;
4234 if (!equal)
4235 isl_die(ctx, isl_error_unknown, "bad conversion", return -1);
4237 return 0;
4240 static int test_conversion(isl_ctx *ctx)
4242 if (test_set_conversion(ctx) < 0)
4243 return -1;
4244 if (test_map_conversion(ctx) < 0)
4245 return -1;
4246 return 0;
4249 /* Check that isl_basic_map_curry does not modify input.
4251 static int test_curry(isl_ctx *ctx)
4253 const char *str;
4254 isl_basic_map *bmap1, *bmap2;
4255 int equal;
4257 str = "{ [A[] -> B[]] -> C[] }";
4258 bmap1 = isl_basic_map_read_from_str(ctx, str);
4259 bmap2 = isl_basic_map_curry(isl_basic_map_copy(bmap1));
4260 equal = isl_basic_map_is_equal(bmap1, bmap2);
4261 isl_basic_map_free(bmap1);
4262 isl_basic_map_free(bmap2);
4264 if (equal < 0)
4265 return -1;
4266 if (equal)
4267 isl_die(ctx, isl_error_unknown,
4268 "curried map should not be equal to original",
4269 return -1);
4271 return 0;
4274 struct {
4275 const char *set;
4276 const char *ma;
4277 const char *res;
4278 } preimage_tests[] = {
4279 { "{ B[i,j] : 0 <= i < 10 and 0 <= j < 100 }",
4280 "{ A[j,i] -> B[i,j] }",
4281 "{ A[j,i] : 0 <= i < 10 and 0 <= j < 100 }" },
4282 { "{ rat: B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
4283 "{ A[a,b] -> B[a/2,b/6] }",
4284 "{ rat: A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 }" },
4285 { "{ B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
4286 "{ A[a,b] -> B[a/2,b/6] }",
4287 "{ A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 and "
4288 "exists i,j : a = 2 i and b = 6 j }" },
4289 { "[n] -> { S[i] : 0 <= i <= 100 }", "[n] -> { S[n] }",
4290 "[n] -> { : 0 <= n <= 100 }" },
4291 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
4292 "{ A[a] -> B[2a] }",
4293 "{ A[a] : 0 <= a < 50 and exists b : a = 2 b }" },
4294 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
4295 "{ A[a] -> B[([a/2])] }",
4296 "{ A[a] : 0 <= a < 200 and exists b : [a/2] = 4 b }" },
4297 { "{ B[i,j,k] : 0 <= i,j,k <= 100 }",
4298 "{ A[a] -> B[a,a,a/3] }",
4299 "{ A[a] : 0 <= a <= 100 and exists b : a = 3 b }" },
4300 { "{ B[i,j] : j = [(i)/2] } ", "{ A[i,j] -> B[i/3,j] }",
4301 "{ A[i,j] : j = [(i)/6] and exists a : i = 3 a }" },
4304 static int test_preimage_basic_set(isl_ctx *ctx)
4306 int i;
4307 isl_basic_set *bset1, *bset2;
4308 isl_multi_aff *ma;
4309 int equal;
4311 for (i = 0; i < ARRAY_SIZE(preimage_tests); ++i) {
4312 bset1 = isl_basic_set_read_from_str(ctx, preimage_tests[i].set);
4313 ma = isl_multi_aff_read_from_str(ctx, preimage_tests[i].ma);
4314 bset2 = isl_basic_set_read_from_str(ctx, preimage_tests[i].res);
4315 bset1 = isl_basic_set_preimage_multi_aff(bset1, ma);
4316 equal = isl_basic_set_is_equal(bset1, bset2);
4317 isl_basic_set_free(bset1);
4318 isl_basic_set_free(bset2);
4319 if (equal < 0)
4320 return -1;
4321 if (!equal)
4322 isl_die(ctx, isl_error_unknown, "bad preimage",
4323 return -1);
4326 return 0;
4329 struct {
4330 const char *map;
4331 const char *ma;
4332 const char *res;
4333 } preimage_domain_tests[] = {
4334 { "{ B[i,j] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }",
4335 "{ A[j,i] -> B[i,j] }",
4336 "{ A[j,i] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }" },
4337 { "{ B[i] -> C[i]; D[i] -> E[i] }",
4338 "{ A[i] -> B[i + 1] }",
4339 "{ A[i] -> C[i + 1] }" },
4340 { "{ B[i] -> C[i]; B[i] -> E[i] }",
4341 "{ A[i] -> B[i + 1] }",
4342 "{ A[i] -> C[i + 1]; A[i] -> E[i + 1] }" },
4343 { "{ B[i] -> C[([i/2])] }",
4344 "{ A[i] -> B[2i] }",
4345 "{ A[i] -> C[i] }" },
4346 { "{ B[i,j] -> C[([i/2]), ([(i+j)/3])] }",
4347 "{ A[i] -> B[([i/5]), ([i/7])] }",
4348 "{ A[i] -> C[([([i/5])/2]), ([(([i/5])+([i/7]))/3])] }" },
4349 { "[N] -> { B[i] -> C[([N/2]), i, ([N/3])] }",
4350 "[N] -> { A[] -> B[([N/5])] }",
4351 "[N] -> { A[] -> C[([N/2]), ([N/5]), ([N/3])] }" },
4352 { "{ B[i] -> C[i] : exists a : i = 5 a }",
4353 "{ A[i] -> B[2i] }",
4354 "{ A[i] -> C[2i] : exists a : 2i = 5 a }" },
4355 { "{ B[i] -> C[i] : exists a : i = 2 a; "
4356 "B[i] -> D[i] : exists a : i = 2 a + 1 }",
4357 "{ A[i] -> B[2i] }",
4358 "{ A[i] -> C[2i] }" },
4361 static int test_preimage_union_map(isl_ctx *ctx)
4363 int i;
4364 isl_union_map *umap1, *umap2;
4365 isl_multi_aff *ma;
4366 int equal;
4368 for (i = 0; i < ARRAY_SIZE(preimage_domain_tests); ++i) {
4369 umap1 = isl_union_map_read_from_str(ctx,
4370 preimage_domain_tests[i].map);
4371 ma = isl_multi_aff_read_from_str(ctx,
4372 preimage_domain_tests[i].ma);
4373 umap2 = isl_union_map_read_from_str(ctx,
4374 preimage_domain_tests[i].res);
4375 umap1 = isl_union_map_preimage_domain_multi_aff(umap1, ma);
4376 equal = isl_union_map_is_equal(umap1, umap2);
4377 isl_union_map_free(umap1);
4378 isl_union_map_free(umap2);
4379 if (equal < 0)
4380 return -1;
4381 if (!equal)
4382 isl_die(ctx, isl_error_unknown, "bad preimage",
4383 return -1);
4386 return 0;
4389 static int test_preimage(isl_ctx *ctx)
4391 if (test_preimage_basic_set(ctx) < 0)
4392 return -1;
4393 if (test_preimage_union_map(ctx) < 0)
4394 return -1;
4396 return 0;
4399 struct {
4400 const char *ma1;
4401 const char *ma;
4402 const char *res;
4403 } pullback_tests[] = {
4404 { "{ B[i,j] -> C[i + 2j] }" , "{ A[a,b] -> B[b,a] }",
4405 "{ A[a,b] -> C[b + 2a] }" },
4406 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/2] }", "{ A[a] -> C[a] }" },
4407 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[2a] }", "{ A[a] -> C[a] }" },
4408 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[(a)/3] }",
4409 "{ A[a] -> C[(a)/6] }" },
4410 { "{ B[i] -> C[2i] }", "{ A[a] -> B[5a] }", "{ A[a] -> C[10a] }" },
4411 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/3] }",
4412 "{ A[a] -> C[(2a)/3] }" },
4413 { "{ B[i,j] -> C[i + j] }", "{ A[a] -> B[a,a] }", "{ A[a] -> C[2a] }"},
4414 { "{ B[a] -> C[a,a] }", "{ A[i,j] -> B[i + j] }",
4415 "{ A[i,j] -> C[i + j, i + j] }"},
4416 { "{ B[i] -> C[([i/2])] }", "{ B[5] }", "{ C[2] }" },
4417 { "[n] -> { B[i,j] -> C[([i/2]) + 2j] }",
4418 "[n] -> { B[n,[n/3]] }", "[n] -> { C[([n/2]) + 2*[n/3]] }", },
4421 static int test_pullback(isl_ctx *ctx)
4423 int i;
4424 isl_multi_aff *ma1, *ma2;
4425 isl_multi_aff *ma;
4426 int equal;
4428 for (i = 0; i < ARRAY_SIZE(pullback_tests); ++i) {
4429 ma1 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma1);
4430 ma = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma);
4431 ma2 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].res);
4432 ma1 = isl_multi_aff_pullback_multi_aff(ma1, ma);
4433 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
4434 isl_multi_aff_free(ma1);
4435 isl_multi_aff_free(ma2);
4436 if (equal < 0)
4437 return -1;
4438 if (!equal)
4439 isl_die(ctx, isl_error_unknown, "bad pullback",
4440 return -1);
4443 return 0;
4446 /* Check that negation is printed correctly.
4448 static int test_ast(isl_ctx *ctx)
4450 isl_ast_expr *expr, *expr1, *expr2, *expr3;
4451 char *str;
4452 int ok;
4454 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
4455 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
4456 expr = isl_ast_expr_add(expr1, expr2);
4457 expr = isl_ast_expr_neg(expr);
4458 str = isl_ast_expr_to_str(expr);
4459 ok = str ? !strcmp(str, "-(A + B)") : -1;
4460 free(str);
4461 isl_ast_expr_free(expr);
4463 if (ok < 0)
4464 return -1;
4465 if (!ok)
4466 isl_die(ctx, isl_error_unknown,
4467 "isl_ast_expr printed incorrectly", return -1);
4469 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
4470 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
4471 expr = isl_ast_expr_add(expr1, expr2);
4472 expr3 = isl_ast_expr_from_id(isl_id_alloc(ctx, "C", NULL));
4473 expr = isl_ast_expr_sub(expr3, expr);
4474 str = isl_ast_expr_to_str(expr);
4475 ok = str ? !strcmp(str, "C - (A + B)") : -1;
4476 free(str);
4477 isl_ast_expr_free(expr);
4479 if (ok < 0)
4480 return -1;
4481 if (!ok)
4482 isl_die(ctx, isl_error_unknown,
4483 "isl_ast_expr printed incorrectly", return -1);
4485 return 0;
4488 /* Check that isl_ast_build_expr_from_set returns a valid expression
4489 * for an empty set. Note that isl_ast_build_expr_from_set getting
4490 * called on an empty set probably indicates a bug in the caller.
4492 static int test_ast_build(isl_ctx *ctx)
4494 isl_set *set;
4495 isl_ast_build *build;
4496 isl_ast_expr *expr;
4498 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4499 build = isl_ast_build_from_context(set);
4501 set = isl_set_empty(isl_space_params_alloc(ctx, 0));
4502 expr = isl_ast_build_expr_from_set(build, set);
4504 isl_ast_expr_free(expr);
4505 isl_ast_build_free(build);
4507 if (!expr)
4508 return -1;
4510 return 0;
4513 /* Internal data structure for before_for and after_for callbacks.
4515 * depth is the current depth
4516 * before is the number of times before_for has been called
4517 * after is the number of times after_for has been called
4519 struct isl_test_codegen_data {
4520 int depth;
4521 int before;
4522 int after;
4525 /* This function is called before each for loop in the AST generated
4526 * from test_ast_gen1.
4528 * Increment the number of calls and the depth.
4529 * Check that the space returned by isl_ast_build_get_schedule_space
4530 * matches the target space of the schedule returned by
4531 * isl_ast_build_get_schedule.
4532 * Return an isl_id that is checked by the corresponding call
4533 * to after_for.
4535 static __isl_give isl_id *before_for(__isl_keep isl_ast_build *build,
4536 void *user)
4538 struct isl_test_codegen_data *data = user;
4539 isl_ctx *ctx;
4540 isl_space *space;
4541 isl_union_map *schedule;
4542 isl_union_set *uset;
4543 isl_set *set;
4544 int empty;
4545 char name[] = "d0";
4547 ctx = isl_ast_build_get_ctx(build);
4549 if (data->before >= 3)
4550 isl_die(ctx, isl_error_unknown,
4551 "unexpected number of for nodes", return NULL);
4552 if (data->depth >= 2)
4553 isl_die(ctx, isl_error_unknown,
4554 "unexpected depth", return NULL);
4556 snprintf(name, sizeof(name), "d%d", data->depth);
4557 data->before++;
4558 data->depth++;
4560 schedule = isl_ast_build_get_schedule(build);
4561 uset = isl_union_map_range(schedule);
4562 if (!uset)
4563 return NULL;
4564 if (isl_union_set_n_set(uset) != 1) {
4565 isl_union_set_free(uset);
4566 isl_die(ctx, isl_error_unknown,
4567 "expecting single range space", return NULL);
4570 space = isl_ast_build_get_schedule_space(build);
4571 set = isl_union_set_extract_set(uset, space);
4572 isl_union_set_free(uset);
4573 empty = isl_set_is_empty(set);
4574 isl_set_free(set);
4576 if (empty < 0)
4577 return NULL;
4578 if (empty)
4579 isl_die(ctx, isl_error_unknown,
4580 "spaces don't match", return NULL);
4582 return isl_id_alloc(ctx, name, NULL);
4585 /* This function is called after each for loop in the AST generated
4586 * from test_ast_gen1.
4588 * Increment the number of calls and decrement the depth.
4589 * Check that the annotation attached to the node matches
4590 * the isl_id returned by the corresponding call to before_for.
4592 static __isl_give isl_ast_node *after_for(__isl_take isl_ast_node *node,
4593 __isl_keep isl_ast_build *build, void *user)
4595 struct isl_test_codegen_data *data = user;
4596 isl_id *id;
4597 const char *name;
4598 int valid;
4600 data->after++;
4601 data->depth--;
4603 if (data->after > data->before)
4604 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
4605 "mismatch in number of for nodes",
4606 return isl_ast_node_free(node));
4608 id = isl_ast_node_get_annotation(node);
4609 if (!id)
4610 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
4611 "missing annotation", return isl_ast_node_free(node));
4613 name = isl_id_get_name(id);
4614 valid = name && atoi(name + 1) == data->depth;
4615 isl_id_free(id);
4617 if (!valid)
4618 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
4619 "wrong annotation", return isl_ast_node_free(node));
4621 return node;
4624 /* Check that the before_each_for and after_each_for callbacks
4625 * are called for each for loop in the generated code,
4626 * that they are called in the right order and that the isl_id
4627 * returned from the before_each_for callback is attached to
4628 * the isl_ast_node passed to the corresponding after_each_for call.
4630 static int test_ast_gen1(isl_ctx *ctx)
4632 const char *str;
4633 isl_set *set;
4634 isl_union_map *schedule;
4635 isl_ast_build *build;
4636 isl_ast_node *tree;
4637 struct isl_test_codegen_data data;
4639 str = "[N] -> { : N >= 10 }";
4640 set = isl_set_read_from_str(ctx, str);
4641 str = "[N] -> { A[i,j] -> S[8,i,3,j] : 0 <= i,j <= N; "
4642 "B[i,j] -> S[8,j,9,i] : 0 <= i,j <= N }";
4643 schedule = isl_union_map_read_from_str(ctx, str);
4645 data.before = 0;
4646 data.after = 0;
4647 data.depth = 0;
4648 build = isl_ast_build_from_context(set);
4649 build = isl_ast_build_set_before_each_for(build,
4650 &before_for, &data);
4651 build = isl_ast_build_set_after_each_for(build,
4652 &after_for, &data);
4653 tree = isl_ast_build_ast_from_schedule(build, schedule);
4654 isl_ast_build_free(build);
4655 if (!tree)
4656 return -1;
4658 isl_ast_node_free(tree);
4660 if (data.before != 3 || data.after != 3)
4661 isl_die(ctx, isl_error_unknown,
4662 "unexpected number of for nodes", return -1);
4664 return 0;
4667 /* Check that the AST generator handles domains that are integrally disjoint
4668 * but not ratinoally disjoint.
4670 static int test_ast_gen2(isl_ctx *ctx)
4672 const char *str;
4673 isl_set *set;
4674 isl_union_map *schedule;
4675 isl_union_map *options;
4676 isl_ast_build *build;
4677 isl_ast_node *tree;
4679 str = "{ A[i,j] -> [i,j] : 0 <= i,j <= 1 }";
4680 schedule = isl_union_map_read_from_str(ctx, str);
4681 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4682 build = isl_ast_build_from_context(set);
4684 str = "{ [i,j] -> atomic[1] : i + j = 1; [i,j] -> unroll[1] : i = j }";
4685 options = isl_union_map_read_from_str(ctx, str);
4686 build = isl_ast_build_set_options(build, options);
4687 tree = isl_ast_build_ast_from_schedule(build, schedule);
4688 isl_ast_build_free(build);
4689 if (!tree)
4690 return -1;
4691 isl_ast_node_free(tree);
4693 return 0;
4696 /* Increment *user on each call.
4698 static __isl_give isl_ast_node *count_domains(__isl_take isl_ast_node *node,
4699 __isl_keep isl_ast_build *build, void *user)
4701 int *n = user;
4703 (*n)++;
4705 return node;
4708 /* Test that unrolling tries to minimize the number of instances.
4709 * In particular, for the schedule given below, make sure it generates
4710 * 3 nodes (rather than 101).
4712 static int test_ast_gen3(isl_ctx *ctx)
4714 const char *str;
4715 isl_set *set;
4716 isl_union_map *schedule;
4717 isl_union_map *options;
4718 isl_ast_build *build;
4719 isl_ast_node *tree;
4720 int n_domain = 0;
4722 str = "[n] -> { A[i] -> [i] : 0 <= i <= 100 and n <= i <= n + 2 }";
4723 schedule = isl_union_map_read_from_str(ctx, str);
4724 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4726 str = "{ [i] -> unroll[0] }";
4727 options = isl_union_map_read_from_str(ctx, str);
4729 build = isl_ast_build_from_context(set);
4730 build = isl_ast_build_set_options(build, options);
4731 build = isl_ast_build_set_at_each_domain(build,
4732 &count_domains, &n_domain);
4733 tree = isl_ast_build_ast_from_schedule(build, schedule);
4734 isl_ast_build_free(build);
4735 if (!tree)
4736 return -1;
4738 isl_ast_node_free(tree);
4740 if (n_domain != 3)
4741 isl_die(ctx, isl_error_unknown,
4742 "unexpected number of for nodes", return -1);
4744 return 0;
4747 /* Check that if the ast_build_exploit_nested_bounds options is set,
4748 * we do not get an outer if node in the generated AST,
4749 * while we do get such an outer if node if the options is not set.
4751 static int test_ast_gen4(isl_ctx *ctx)
4753 const char *str;
4754 isl_set *set;
4755 isl_union_map *schedule;
4756 isl_ast_build *build;
4757 isl_ast_node *tree;
4758 enum isl_ast_node_type type;
4759 int enb;
4761 enb = isl_options_get_ast_build_exploit_nested_bounds(ctx);
4762 str = "[N,M] -> { A[i,j] -> [i,j] : 0 <= i <= N and 0 <= j <= M }";
4764 isl_options_set_ast_build_exploit_nested_bounds(ctx, 1);
4766 schedule = isl_union_map_read_from_str(ctx, str);
4767 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4768 build = isl_ast_build_from_context(set);
4769 tree = isl_ast_build_ast_from_schedule(build, schedule);
4770 isl_ast_build_free(build);
4771 if (!tree)
4772 return -1;
4774 type = isl_ast_node_get_type(tree);
4775 isl_ast_node_free(tree);
4777 if (type == isl_ast_node_if)
4778 isl_die(ctx, isl_error_unknown,
4779 "not expecting if node", return -1);
4781 isl_options_set_ast_build_exploit_nested_bounds(ctx, 0);
4783 schedule = isl_union_map_read_from_str(ctx, str);
4784 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4785 build = isl_ast_build_from_context(set);
4786 tree = isl_ast_build_ast_from_schedule(build, schedule);
4787 isl_ast_build_free(build);
4788 if (!tree)
4789 return -1;
4791 type = isl_ast_node_get_type(tree);
4792 isl_ast_node_free(tree);
4794 if (type != isl_ast_node_if)
4795 isl_die(ctx, isl_error_unknown,
4796 "expecting if node", return -1);
4798 isl_options_set_ast_build_exploit_nested_bounds(ctx, enb);
4800 return 0;
4803 /* This function is called for each leaf in the AST generated
4804 * from test_ast_gen5.
4806 * We finalize the AST generation by extending the outer schedule
4807 * with a zero-dimensional schedule. If this results in any for loops,
4808 * then this means that we did not pass along enough information
4809 * about the outer schedule to the inner AST generation.
4811 static __isl_give isl_ast_node *create_leaf(__isl_take isl_ast_build *build,
4812 void *user)
4814 isl_union_map *schedule, *extra;
4815 isl_ast_node *tree;
4817 schedule = isl_ast_build_get_schedule(build);
4818 extra = isl_union_map_copy(schedule);
4819 extra = isl_union_map_from_domain(isl_union_map_domain(extra));
4820 schedule = isl_union_map_range_product(schedule, extra);
4821 tree = isl_ast_build_ast_from_schedule(build, schedule);
4822 isl_ast_build_free(build);
4824 if (!tree)
4825 return NULL;
4827 if (isl_ast_node_get_type(tree) == isl_ast_node_for)
4828 isl_die(isl_ast_node_get_ctx(tree), isl_error_unknown,
4829 "code should not contain any for loop",
4830 return isl_ast_node_free(tree));
4832 return tree;
4835 /* Check that we do not lose any information when going back and
4836 * forth between internal and external schedule.
4838 * In particular, we create an AST where we unroll the only
4839 * non-constant dimension in the schedule. We therefore do
4840 * not expect any for loops in the AST. However, older versions
4841 * of isl would not pass along enough information about the outer
4842 * schedule when performing an inner code generation from a create_leaf
4843 * callback, resulting in the inner code generation producing a for loop.
4845 static int test_ast_gen5(isl_ctx *ctx)
4847 const char *str;
4848 isl_set *set;
4849 isl_union_map *schedule, *options;
4850 isl_ast_build *build;
4851 isl_ast_node *tree;
4853 str = "{ A[] -> [1, 1, 2]; B[i] -> [1, i, 0] : i >= 1 and i <= 2 }";
4854 schedule = isl_union_map_read_from_str(ctx, str);
4856 str = "{ [a, b, c] -> unroll[1] : exists (e0 = [(a)/4]: "
4857 "4e0 >= -1 + a - b and 4e0 <= -2 + a + b) }";
4858 options = isl_union_map_read_from_str(ctx, str);
4860 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4861 build = isl_ast_build_from_context(set);
4862 build = isl_ast_build_set_options(build, options);
4863 build = isl_ast_build_set_create_leaf(build, &create_leaf, NULL);
4864 tree = isl_ast_build_ast_from_schedule(build, schedule);
4865 isl_ast_build_free(build);
4866 isl_ast_node_free(tree);
4867 if (!tree)
4868 return -1;
4870 return 0;
4873 static int test_ast_gen(isl_ctx *ctx)
4875 if (test_ast_gen1(ctx) < 0)
4876 return -1;
4877 if (test_ast_gen2(ctx) < 0)
4878 return -1;
4879 if (test_ast_gen3(ctx) < 0)
4880 return -1;
4881 if (test_ast_gen4(ctx) < 0)
4882 return -1;
4883 if (test_ast_gen5(ctx) < 0)
4884 return -1;
4885 return 0;
4888 /* Check if dropping output dimensions from an isl_pw_multi_aff
4889 * works properly.
4891 static int test_pw_multi_aff(isl_ctx *ctx)
4893 const char *str;
4894 isl_pw_multi_aff *pma1, *pma2;
4895 int equal;
4897 str = "{ [i,j] -> [i+j, 4i-j] }";
4898 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
4899 str = "{ [i,j] -> [4i-j] }";
4900 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
4902 pma1 = isl_pw_multi_aff_drop_dims(pma1, isl_dim_out, 0, 1);
4904 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
4906 isl_pw_multi_aff_free(pma1);
4907 isl_pw_multi_aff_free(pma2);
4908 if (equal < 0)
4909 return -1;
4910 if (!equal)
4911 isl_die(ctx, isl_error_unknown,
4912 "expressions not equal", return -1);
4914 return 0;
4917 /* Check that we can properly parse multi piecewise affine expressions
4918 * where the piecewise affine expressions have different domains.
4920 static int test_multi_pw_aff(isl_ctx *ctx)
4922 const char *str;
4923 isl_set *dom, *dom2;
4924 isl_multi_pw_aff *mpa1, *mpa2;
4925 isl_pw_aff *pa;
4926 int equal;
4927 int equal_domain;
4929 mpa1 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [i] }");
4930 dom = isl_set_read_from_str(ctx, "{ [i] : i > 0 }");
4931 mpa1 = isl_multi_pw_aff_intersect_domain(mpa1, dom);
4932 mpa2 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [2i] }");
4933 mpa2 = isl_multi_pw_aff_flat_range_product(mpa1, mpa2);
4934 str = "{ [i] -> [(i : i > 0), 2i] }";
4935 mpa1 = isl_multi_pw_aff_read_from_str(ctx, str);
4937 equal = isl_multi_pw_aff_plain_is_equal(mpa1, mpa2);
4939 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 0);
4940 dom = isl_pw_aff_domain(pa);
4941 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 1);
4942 dom2 = isl_pw_aff_domain(pa);
4943 equal_domain = isl_set_is_equal(dom, dom2);
4945 isl_set_free(dom);
4946 isl_set_free(dom2);
4947 isl_multi_pw_aff_free(mpa1);
4948 isl_multi_pw_aff_free(mpa2);
4950 if (equal < 0)
4951 return -1;
4952 if (!equal)
4953 isl_die(ctx, isl_error_unknown,
4954 "expressions not equal", return -1);
4956 if (equal_domain < 0)
4957 return -1;
4958 if (equal_domain)
4959 isl_die(ctx, isl_error_unknown,
4960 "domains unexpectedly equal", return -1);
4962 return 0;
4965 /* This is a regression test for a bug where isl_basic_map_simplify
4966 * would end up in an infinite loop. In particular, we construct
4967 * an empty basic set that is not obviously empty.
4968 * isl_basic_set_is_empty marks the basic set as empty.
4969 * After projecting out i3, the variable can be dropped completely,
4970 * but isl_basic_map_simplify refrains from doing so if the basic set
4971 * is empty and would end up in an infinite loop if it didn't test
4972 * explicitly for empty basic maps in the outer loop.
4974 static int test_simplify(isl_ctx *ctx)
4976 const char *str;
4977 isl_basic_set *bset;
4978 int empty;
4980 str = "{ [i0, i1, i2, i3] : i0 >= -2 and 6i2 <= 4 + i0 + 5i1 and "
4981 "i2 <= 22 and 75i2 <= 111 + 13i0 + 60i1 and "
4982 "25i2 >= 38 + 6i0 + 20i1 and i0 <= -1 and i2 >= 20 and "
4983 "i3 >= i2 }";
4984 bset = isl_basic_set_read_from_str(ctx, str);
4985 empty = isl_basic_set_is_empty(bset);
4986 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
4987 isl_basic_set_free(bset);
4988 if (!bset)
4989 return -1;
4990 if (!empty)
4991 isl_die(ctx, isl_error_unknown,
4992 "basic set should be empty", return -1);
4994 return 0;
4997 /* This is a regression test for a bug where isl_tab_basic_map_partial_lexopt
4998 * with gbr context would fail to disable the use of the shifted tableau
4999 * when transferring equalities for the input to the context, resulting
5000 * in invalid sample values.
5002 static int test_partial_lexmin(isl_ctx *ctx)
5004 const char *str;
5005 isl_basic_set *bset;
5006 isl_basic_map *bmap;
5007 isl_map *map;
5009 str = "{ [1, b, c, 1 - c] -> [e] : 2e <= -c and 2e >= -3 + c }";
5010 bmap = isl_basic_map_read_from_str(ctx, str);
5011 str = "{ [a, b, c, d] : c <= 1 and 2d >= 6 - 4b - c }";
5012 bset = isl_basic_set_read_from_str(ctx, str);
5013 map = isl_basic_map_partial_lexmin(bmap, bset, NULL);
5014 isl_map_free(map);
5016 if (!map)
5017 return -1;
5019 return 0;
5022 /* Check that the variable compression performed on the existentially
5023 * quantified variables inside isl_basic_set_compute_divs is not confused
5024 * by the implicit equalities among the parameters.
5026 static int test_compute_divs(isl_ctx *ctx)
5028 const char *str;
5029 isl_basic_set *bset;
5030 isl_set *set;
5032 str = "[a, b, c, d, e] -> { [] : exists (e0: 2d = b and a <= 124 and "
5033 "b <= 2046 and b >= 0 and b <= 60 + 64a and 2e >= b + 2c and "
5034 "2e >= b and 2e <= 1 + b and 2e <= 1 + b + 2c and "
5035 "32768e0 >= -124 + a and 2097152e0 <= 60 + 64a - b) }";
5036 bset = isl_basic_set_read_from_str(ctx, str);
5037 set = isl_basic_set_compute_divs(bset);
5038 isl_set_free(set);
5039 if (!set)
5040 return -1;
5042 return 0;
5045 struct {
5046 const char *set;
5047 const char *dual;
5048 } coef_tests[] = {
5049 { "{ rat: [i] : 0 <= i <= 10 }",
5050 "{ rat: coefficients[[cst] -> [a]] : cst >= 0 and 10a + cst >= 0 }" },
5051 { "{ rat: [i] : FALSE }",
5052 "{ rat: coefficients[[cst] -> [a]] }" },
5053 { "{ rat: [i] : }",
5054 "{ rat: coefficients[[cst] -> [0]] : cst >= 0 }" },
5057 struct {
5058 const char *set;
5059 const char *dual;
5060 } sol_tests[] = {
5061 { "{ rat: coefficients[[cst] -> [a]] : cst >= 0 and 10a + cst >= 0 }",
5062 "{ rat: [i] : 0 <= i <= 10 }" },
5063 { "{ rat: coefficients[[cst] -> [a]] : FALSE }",
5064 "{ rat: [i] }" },
5065 { "{ rat: coefficients[[cst] -> [a]] }",
5066 "{ rat: [i] : FALSE }" },
5069 /* Test the basic functionality of isl_basic_set_coefficients and
5070 * isl_basic_set_solutions.
5072 static int test_dual(isl_ctx *ctx)
5074 int i;
5076 for (i = 0; i < ARRAY_SIZE(coef_tests); ++i) {
5077 int equal;
5078 isl_basic_set *bset1, *bset2;
5080 bset1 = isl_basic_set_read_from_str(ctx, coef_tests[i].set);
5081 bset2 = isl_basic_set_read_from_str(ctx, coef_tests[i].dual);
5082 bset1 = isl_basic_set_coefficients(bset1);
5083 equal = isl_basic_set_is_equal(bset1, bset2);
5084 isl_basic_set_free(bset1);
5085 isl_basic_set_free(bset2);
5086 if (equal < 0)
5087 return -1;
5088 if (!equal)
5089 isl_die(ctx, isl_error_unknown,
5090 "incorrect dual", return -1);
5093 for (i = 0; i < ARRAY_SIZE(sol_tests); ++i) {
5094 int equal;
5095 isl_basic_set *bset1, *bset2;
5097 bset1 = isl_basic_set_read_from_str(ctx, sol_tests[i].set);
5098 bset2 = isl_basic_set_read_from_str(ctx, sol_tests[i].dual);
5099 bset1 = isl_basic_set_solutions(bset1);
5100 equal = isl_basic_set_is_equal(bset1, bset2);
5101 isl_basic_set_free(bset1);
5102 isl_basic_set_free(bset2);
5103 if (equal < 0)
5104 return -1;
5105 if (!equal)
5106 isl_die(ctx, isl_error_unknown,
5107 "incorrect dual", return -1);
5110 return 0;
5113 struct {
5114 const char *name;
5115 int (*fn)(isl_ctx *ctx);
5116 } tests [] = {
5117 { "dual", &test_dual },
5118 { "dependence analysis", &test_flow },
5119 { "val", &test_val },
5120 { "compute divs", &test_compute_divs },
5121 { "partial lexmin", &test_partial_lexmin },
5122 { "simplify", &test_simplify },
5123 { "curry", &test_curry },
5124 { "piecewise multi affine expressions", &test_pw_multi_aff },
5125 { "multi piecewise affine expressions", &test_multi_pw_aff },
5126 { "conversion", &test_conversion },
5127 { "list", &test_list },
5128 { "align parameters", &test_align_parameters },
5129 { "preimage", &test_preimage },
5130 { "pullback", &test_pullback },
5131 { "AST", &test_ast },
5132 { "AST build", &test_ast_build },
5133 { "AST generation", &test_ast_gen },
5134 { "eliminate", &test_eliminate },
5135 { "residue class", &test_residue_class },
5136 { "div", &test_div },
5137 { "slice", &test_slice },
5138 { "fixed power", &test_fixed_power },
5139 { "sample", &test_sample },
5140 { "output", &test_output },
5141 { "vertices", &test_vertices },
5142 { "fixed", &test_fixed },
5143 { "equal", &test_equal },
5144 { "disjoint", &test_disjoint },
5145 { "product", &test_product },
5146 { "dim_max", &test_dim_max },
5147 { "affine", &test_aff },
5148 { "injective", &test_injective },
5149 { "schedule", &test_schedule },
5150 { "union_pw", &test_union_pw },
5151 { "parse", &test_parse },
5152 { "single-valued", &test_sv },
5153 { "affine hull", &test_affine_hull },
5154 { "coalesce", &test_coalesce },
5155 { "factorize", &test_factorize },
5156 { "subset", &test_subset },
5157 { "subtract", &test_subtract },
5158 { "lexmin", &test_lexmin },
5159 { "min", &test_min },
5160 { "gist", &test_gist },
5161 { "piecewise quasi-polynomials", &test_pwqp },
5164 int main(int argc, char **argv)
5166 int i;
5167 struct isl_ctx *ctx;
5168 struct isl_options *options;
5170 srcdir = getenv("srcdir");
5171 assert(srcdir);
5173 options = isl_options_new_with_defaults();
5174 assert(options);
5175 argc = isl_options_parse(options, argc, argv, ISL_ARG_ALL);
5177 ctx = isl_ctx_alloc_with_options(&isl_options_args, options);
5178 for (i = 0; i < ARRAY_SIZE(tests); ++i) {
5179 printf("%s\n", tests[i].name);
5180 if (tests[i].fn(ctx) < 0)
5181 goto error;
5183 test_lift(ctx);
5184 test_bound(ctx);
5185 test_union(ctx);
5186 test_split_periods(ctx);
5187 test_lex(ctx);
5188 test_bijective(ctx);
5189 test_dep(ctx);
5190 test_read(ctx);
5191 test_bounded(ctx);
5192 test_construction(ctx);
5193 test_dim(ctx);
5194 test_application(ctx);
5195 test_convex_hull(ctx);
5196 test_closure(ctx);
5197 isl_ctx_free(ctx);
5198 return 0;
5199 error:
5200 isl_ctx_free(ctx);
5201 return -1;