isl_schedule_constraints_compute_schedule: allow partial carrying schedule rows
[isl.git] / isl_test.c
blob7c5c7647e6012453c965d0e7549d3ba867b843b1
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" },
414 /* Perform some basic tests of unary operations on isl_val objects.
416 static int test_un_val(isl_ctx *ctx)
418 int i;
419 isl_val *v, *res;
420 __isl_give isl_val *(*fn)(__isl_take isl_val *v);
421 int ok;
423 for (i = 0; i < ARRAY_SIZE(val_un_tests); ++i) {
424 v = isl_val_read_from_str(ctx, val_un_tests[i].arg);
425 res = isl_val_read_from_str(ctx, val_un_tests[i].res);
426 fn = val_un_tests[i].op;
427 v = fn(v);
428 if (isl_val_is_nan(res))
429 ok = isl_val_is_nan(v);
430 else
431 ok = isl_val_eq(v, res);
432 isl_val_free(v);
433 isl_val_free(res);
434 if (ok < 0)
435 return -1;
436 if (!ok)
437 isl_die(ctx, isl_error_unknown,
438 "unexpected result", return -1);
441 return 0;
444 struct {
445 __isl_give isl_val *(*fn)(__isl_take isl_val *v1,
446 __isl_take isl_val *v2);
447 } val_bin_op[] = {
448 ['+'] = { &isl_val_add },
449 ['-'] = { &isl_val_sub },
450 ['*'] = { &isl_val_mul },
451 ['/'] = { &isl_val_div },
452 ['g'] = { &isl_val_gcd },
453 ['m'] = { &isl_val_min },
454 ['M'] = { &isl_val_max },
457 struct {
458 const char *arg1;
459 unsigned char op;
460 const char *arg2;
461 const char *res;
462 } val_bin_tests[] = {
463 { "0", '+', "0", "0" },
464 { "1", '+', "0", "1" },
465 { "1", '+', "1", "2" },
466 { "1", '-', "1", "0" },
467 { "1", '*', "1", "1" },
468 { "1", '/', "1", "1" },
469 { "2", '*', "3", "6" },
470 { "2", '*', "1/2", "1" },
471 { "2", '*', "1/3", "2/3" },
472 { "2/3", '*', "3/5", "2/5" },
473 { "2/3", '*', "7/5", "14/15" },
474 { "2", '/', "1/2", "4" },
475 { "-2", '/', "-1/2", "4" },
476 { "-2", '/', "1/2", "-4" },
477 { "2", '/', "-1/2", "-4" },
478 { "2", '/', "2", "1" },
479 { "2", '/', "3", "2/3" },
480 { "2/3", '/', "5/3", "2/5" },
481 { "2/3", '/', "5/7", "14/15" },
482 { "0", '/', "0", "NaN" },
483 { "42", '/', "0", "NaN" },
484 { "-42", '/', "0", "NaN" },
485 { "infty", '/', "0", "NaN" },
486 { "-infty", '/', "0", "NaN" },
487 { "NaN", '/', "0", "NaN" },
488 { "0", '/', "NaN", "NaN" },
489 { "42", '/', "NaN", "NaN" },
490 { "-42", '/', "NaN", "NaN" },
491 { "infty", '/', "NaN", "NaN" },
492 { "-infty", '/', "NaN", "NaN" },
493 { "NaN", '/', "NaN", "NaN" },
494 { "0", '/', "infty", "0" },
495 { "42", '/', "infty", "0" },
496 { "-42", '/', "infty", "0" },
497 { "infty", '/', "infty", "NaN" },
498 { "-infty", '/', "infty", "NaN" },
499 { "NaN", '/', "infty", "NaN" },
500 { "0", '/', "-infty", "0" },
501 { "42", '/', "-infty", "0" },
502 { "-42", '/', "-infty", "0" },
503 { "infty", '/', "-infty", "NaN" },
504 { "-infty", '/', "-infty", "NaN" },
505 { "NaN", '/', "-infty", "NaN" },
506 { "1", '-', "1/3", "2/3" },
507 { "1/3", '+', "1/2", "5/6" },
508 { "1/2", '+', "1/2", "1" },
509 { "3/4", '-', "1/4", "1/2" },
510 { "1/2", '-', "1/3", "1/6" },
511 { "infty", '+', "42", "infty" },
512 { "infty", '+', "infty", "infty" },
513 { "42", '+', "infty", "infty" },
514 { "infty", '-', "infty", "NaN" },
515 { "infty", '*', "infty", "infty" },
516 { "infty", '*', "-infty", "-infty" },
517 { "-infty", '*', "infty", "-infty" },
518 { "-infty", '*', "-infty", "infty" },
519 { "0", '*', "infty", "NaN" },
520 { "1", '*', "infty", "infty" },
521 { "infty", '*', "0", "NaN" },
522 { "infty", '*', "42", "infty" },
523 { "42", '-', "infty", "-infty" },
524 { "infty", '+', "-infty", "NaN" },
525 { "4", 'g', "6", "2" },
526 { "5", 'g', "6", "1" },
527 { "42", 'm', "3", "3" },
528 { "42", 'M', "3", "42" },
529 { "3", 'm', "42", "3" },
530 { "3", 'M', "42", "42" },
531 { "42", 'm', "infty", "42" },
532 { "42", 'M', "infty", "infty" },
533 { "42", 'm', "-infty", "-infty" },
534 { "42", 'M', "-infty", "42" },
535 { "42", 'm', "NaN", "NaN" },
536 { "42", 'M', "NaN", "NaN" },
537 { "infty", 'm', "-infty", "-infty" },
538 { "infty", 'M', "-infty", "infty" },
541 /* Perform some basic tests of binary operations on isl_val objects.
543 static int test_bin_val(isl_ctx *ctx)
545 int i;
546 isl_val *v1, *v2, *res;
547 __isl_give isl_val *(*fn)(__isl_take isl_val *v1,
548 __isl_take isl_val *v2);
549 int ok;
551 for (i = 0; i < ARRAY_SIZE(val_bin_tests); ++i) {
552 v1 = isl_val_read_from_str(ctx, val_bin_tests[i].arg1);
553 v2 = isl_val_read_from_str(ctx, val_bin_tests[i].arg2);
554 res = isl_val_read_from_str(ctx, val_bin_tests[i].res);
555 fn = val_bin_op[val_bin_tests[i].op].fn;
556 v1 = fn(v1, v2);
557 if (isl_val_is_nan(res))
558 ok = isl_val_is_nan(v1);
559 else
560 ok = isl_val_eq(v1, res);
561 isl_val_free(v1);
562 isl_val_free(res);
563 if (ok < 0)
564 return -1;
565 if (!ok)
566 isl_die(ctx, isl_error_unknown,
567 "unexpected result", return -1);
570 return 0;
573 /* Perform some basic tests on isl_val objects.
575 static int test_val(isl_ctx *ctx)
577 if (test_un_val(ctx) < 0)
578 return -1;
579 if (test_bin_val(ctx) < 0)
580 return -1;
581 return 0;
584 static int test_div(isl_ctx *ctx)
586 unsigned n;
587 const char *str;
588 int empty;
589 isl_int v;
590 isl_space *dim;
591 isl_set *set;
592 isl_local_space *ls;
593 struct isl_basic_set *bset;
594 struct isl_constraint *c;
596 isl_int_init(v);
598 /* test 1 */
599 dim = isl_space_set_alloc(ctx, 0, 3);
600 bset = isl_basic_set_universe(isl_space_copy(dim));
601 ls = isl_local_space_from_space(dim);
603 c = isl_equality_alloc(isl_local_space_copy(ls));
604 isl_int_set_si(v, -1);
605 isl_constraint_set_constant(c, v);
606 isl_int_set_si(v, 1);
607 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
608 isl_int_set_si(v, 3);
609 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
610 bset = isl_basic_set_add_constraint(bset, c);
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, 2, v);
619 bset = isl_basic_set_add_constraint(bset, c);
621 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
623 assert(bset && bset->n_div == 1);
624 isl_local_space_free(ls);
625 isl_basic_set_free(bset);
627 /* test 2 */
628 dim = isl_space_set_alloc(ctx, 0, 3);
629 bset = isl_basic_set_universe(isl_space_copy(dim));
630 ls = isl_local_space_from_space(dim);
632 c = isl_equality_alloc(isl_local_space_copy(ls));
633 isl_int_set_si(v, 1);
634 isl_constraint_set_constant(c, v);
635 isl_int_set_si(v, -1);
636 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
637 isl_int_set_si(v, 3);
638 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
639 bset = isl_basic_set_add_constraint(bset, c);
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, 2, v);
648 bset = isl_basic_set_add_constraint(bset, c);
650 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
652 assert(bset && bset->n_div == 1);
653 isl_local_space_free(ls);
654 isl_basic_set_free(bset);
656 /* test 3 */
657 dim = isl_space_set_alloc(ctx, 0, 3);
658 bset = isl_basic_set_universe(isl_space_copy(dim));
659 ls = isl_local_space_from_space(dim);
661 c = isl_equality_alloc(isl_local_space_copy(ls));
662 isl_int_set_si(v, 1);
663 isl_constraint_set_constant(c, v);
664 isl_int_set_si(v, -1);
665 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
666 isl_int_set_si(v, 3);
667 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
668 bset = isl_basic_set_add_constraint(bset, c);
670 c = isl_equality_alloc(isl_local_space_copy(ls));
671 isl_int_set_si(v, -3);
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, 4);
676 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
677 bset = isl_basic_set_add_constraint(bset, c);
679 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
681 assert(bset && bset->n_div == 1);
682 isl_local_space_free(ls);
683 isl_basic_set_free(bset);
685 /* test 4 */
686 dim = isl_space_set_alloc(ctx, 0, 3);
687 bset = isl_basic_set_universe(isl_space_copy(dim));
688 ls = isl_local_space_from_space(dim);
690 c = isl_equality_alloc(isl_local_space_copy(ls));
691 isl_int_set_si(v, 2);
692 isl_constraint_set_constant(c, v);
693 isl_int_set_si(v, -1);
694 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
695 isl_int_set_si(v, 3);
696 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
697 bset = isl_basic_set_add_constraint(bset, c);
699 c = isl_equality_alloc(isl_local_space_copy(ls));
700 isl_int_set_si(v, -1);
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, 6);
705 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
706 bset = isl_basic_set_add_constraint(bset, c);
708 bset = isl_basic_set_project_out(bset, isl_dim_set, 1, 2);
710 assert(isl_basic_set_is_empty(bset));
711 isl_local_space_free(ls);
712 isl_basic_set_free(bset);
714 /* test 5 */
715 dim = isl_space_set_alloc(ctx, 0, 3);
716 bset = isl_basic_set_universe(isl_space_copy(dim));
717 ls = isl_local_space_from_space(dim);
719 c = isl_equality_alloc(isl_local_space_copy(ls));
720 isl_int_set_si(v, -1);
721 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
722 isl_int_set_si(v, 3);
723 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
724 bset = isl_basic_set_add_constraint(bset, c);
726 c = isl_equality_alloc(isl_local_space_copy(ls));
727 isl_int_set_si(v, 1);
728 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
729 isl_int_set_si(v, -3);
730 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
731 bset = isl_basic_set_add_constraint(bset, c);
733 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
735 assert(bset && bset->n_div == 0);
736 isl_basic_set_free(bset);
737 isl_local_space_free(ls);
739 /* test 6 */
740 dim = isl_space_set_alloc(ctx, 0, 3);
741 bset = isl_basic_set_universe(isl_space_copy(dim));
742 ls = isl_local_space_from_space(dim);
744 c = isl_equality_alloc(isl_local_space_copy(ls));
745 isl_int_set_si(v, -1);
746 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
747 isl_int_set_si(v, 6);
748 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
749 bset = isl_basic_set_add_constraint(bset, c);
751 c = isl_equality_alloc(isl_local_space_copy(ls));
752 isl_int_set_si(v, 1);
753 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
754 isl_int_set_si(v, -3);
755 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
756 bset = isl_basic_set_add_constraint(bset, c);
758 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
760 assert(bset && bset->n_div == 1);
761 isl_basic_set_free(bset);
762 isl_local_space_free(ls);
764 /* test 7 */
765 /* This test is a bit tricky. We set up an equality
766 * a + 3b + 3c = 6 e0
767 * Normalization of divs creates _two_ divs
768 * a = 3 e0
769 * c - b - e0 = 2 e1
770 * Afterwards e0 is removed again because it has coefficient -1
771 * and we end up with the original equality and div again.
772 * Perhaps we can avoid the introduction of this temporary div.
774 dim = isl_space_set_alloc(ctx, 0, 4);
775 bset = isl_basic_set_universe(isl_space_copy(dim));
776 ls = isl_local_space_from_space(dim);
778 c = isl_equality_alloc(isl_local_space_copy(ls));
779 isl_int_set_si(v, -1);
780 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
781 isl_int_set_si(v, -3);
782 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
783 isl_int_set_si(v, -3);
784 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
785 isl_int_set_si(v, 6);
786 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
787 bset = isl_basic_set_add_constraint(bset, c);
789 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
791 /* Test disabled for now */
793 assert(bset && bset->n_div == 1);
795 isl_local_space_free(ls);
796 isl_basic_set_free(bset);
798 /* test 8 */
799 dim = isl_space_set_alloc(ctx, 0, 5);
800 bset = isl_basic_set_universe(isl_space_copy(dim));
801 ls = isl_local_space_from_space(dim);
803 c = isl_equality_alloc(isl_local_space_copy(ls));
804 isl_int_set_si(v, -1);
805 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
806 isl_int_set_si(v, -3);
807 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
808 isl_int_set_si(v, -3);
809 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
810 isl_int_set_si(v, 6);
811 isl_constraint_set_coefficient(c, isl_dim_set, 4, v);
812 bset = isl_basic_set_add_constraint(bset, c);
814 c = isl_equality_alloc(isl_local_space_copy(ls));
815 isl_int_set_si(v, -1);
816 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
817 isl_int_set_si(v, 1);
818 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
819 isl_int_set_si(v, 1);
820 isl_constraint_set_constant(c, v);
821 bset = isl_basic_set_add_constraint(bset, c);
823 bset = isl_basic_set_project_out(bset, isl_dim_set, 4, 1);
825 /* Test disabled for now */
827 assert(bset && bset->n_div == 1);
829 isl_local_space_free(ls);
830 isl_basic_set_free(bset);
832 /* test 9 */
833 dim = isl_space_set_alloc(ctx, 0, 4);
834 bset = isl_basic_set_universe(isl_space_copy(dim));
835 ls = isl_local_space_from_space(dim);
837 c = isl_equality_alloc(isl_local_space_copy(ls));
838 isl_int_set_si(v, 1);
839 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
840 isl_int_set_si(v, -1);
841 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
842 isl_int_set_si(v, -2);
843 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
844 bset = isl_basic_set_add_constraint(bset, c);
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, 3);
850 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
851 isl_int_set_si(v, 2);
852 isl_constraint_set_constant(c, v);
853 bset = isl_basic_set_add_constraint(bset, c);
855 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 2);
857 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
859 assert(!isl_basic_set_is_empty(bset));
861 isl_local_space_free(ls);
862 isl_basic_set_free(bset);
864 /* test 10 */
865 dim = isl_space_set_alloc(ctx, 0, 3);
866 bset = isl_basic_set_universe(isl_space_copy(dim));
867 ls = isl_local_space_from_space(dim);
869 c = isl_equality_alloc(isl_local_space_copy(ls));
870 isl_int_set_si(v, 1);
871 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
872 isl_int_set_si(v, -2);
873 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
874 bset = isl_basic_set_add_constraint(bset, c);
876 bset = isl_basic_set_project_out(bset, isl_dim_set, 2, 1);
878 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
880 isl_local_space_free(ls);
881 isl_basic_set_free(bset);
883 isl_int_clear(v);
885 str = "{ [i] : exists (e0, e1: 3e1 >= 1 + 2e0 and "
886 "8e1 <= -1 + 5i - 5e0 and 2e1 >= 1 + 2i - 5e0) }";
887 set = isl_set_read_from_str(ctx, str);
888 set = isl_set_compute_divs(set);
889 isl_set_free(set);
890 if (!set)
891 return -1;
893 str = "{ [i,j] : 2*[i/2] + 3 * [j/4] <= 10 and 2 i = j }";
894 bset = isl_basic_set_read_from_str(ctx, str);
895 n = isl_basic_set_dim(bset, isl_dim_div);
896 isl_basic_set_free(bset);
897 if (!bset)
898 return -1;
899 if (n != 0)
900 isl_die(ctx, isl_error_unknown,
901 "expecting no existentials", return -1);
903 str = "{ [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }";
904 set = isl_set_read_from_str(ctx, str);
905 set = isl_set_remove_divs_involving_dims(set, isl_dim_set, 0, 2);
906 set = isl_set_fix_si(set, isl_dim_set, 2, -3);
907 empty = isl_set_is_empty(set);
908 isl_set_free(set);
909 if (empty < 0)
910 return -1;
911 if (!empty)
912 isl_die(ctx, isl_error_unknown,
913 "result not as accurate as expected", return -1);
915 return 0;
918 void test_application_case(struct isl_ctx *ctx, const char *name)
920 char *filename;
921 FILE *input;
922 struct isl_basic_set *bset1, *bset2;
923 struct isl_basic_map *bmap;
925 filename = get_filename(ctx, name, "omega");
926 assert(filename);
927 input = fopen(filename, "r");
928 assert(input);
930 bset1 = isl_basic_set_read_from_file(ctx, input);
931 bmap = isl_basic_map_read_from_file(ctx, input);
933 bset1 = isl_basic_set_apply(bset1, bmap);
935 bset2 = isl_basic_set_read_from_file(ctx, input);
937 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
939 isl_basic_set_free(bset1);
940 isl_basic_set_free(bset2);
941 free(filename);
943 fclose(input);
946 void test_application(struct isl_ctx *ctx)
948 test_application_case(ctx, "application");
949 test_application_case(ctx, "application2");
952 void test_affine_hull_case(struct isl_ctx *ctx, const char *name)
954 char *filename;
955 FILE *input;
956 struct isl_basic_set *bset1, *bset2;
958 filename = get_filename(ctx, name, "polylib");
959 assert(filename);
960 input = fopen(filename, "r");
961 assert(input);
963 bset1 = isl_basic_set_read_from_file(ctx, input);
964 bset2 = isl_basic_set_read_from_file(ctx, input);
966 bset1 = isl_basic_set_affine_hull(bset1);
968 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
970 isl_basic_set_free(bset1);
971 isl_basic_set_free(bset2);
972 free(filename);
974 fclose(input);
977 int test_affine_hull(struct isl_ctx *ctx)
979 const char *str;
980 isl_set *set;
981 isl_basic_set *bset, *bset2;
982 int n;
983 int subset;
985 test_affine_hull_case(ctx, "affine2");
986 test_affine_hull_case(ctx, "affine");
987 test_affine_hull_case(ctx, "affine3");
989 str = "[m] -> { [i0] : exists (e0, e1: e1 <= 1 + i0 and "
990 "m >= 3 and 4i0 <= 2 + m and e1 >= i0 and "
991 "e1 >= 0 and e1 <= 2 and e1 >= 1 + 2e0 and "
992 "2e1 <= 1 + m + 4e0 and 2e1 >= 2 - m + 4i0 - 4e0) }";
993 set = isl_set_read_from_str(ctx, str);
994 bset = isl_set_affine_hull(set);
995 n = isl_basic_set_dim(bset, isl_dim_div);
996 isl_basic_set_free(bset);
997 if (n != 0)
998 isl_die(ctx, isl_error_unknown, "not expecting any divs",
999 return -1);
1001 /* Check that isl_map_affine_hull is not confused by
1002 * the reordering of divs in isl_map_align_divs.
1004 str = "{ [a, b, c, 0] : exists (e0 = [(b)/32], e1 = [(c)/32]: "
1005 "32e0 = b and 32e1 = c); "
1006 "[a, 0, c, 0] : exists (e0 = [(c)/32]: 32e0 = c) }";
1007 set = isl_set_read_from_str(ctx, str);
1008 bset = isl_set_affine_hull(set);
1009 isl_basic_set_free(bset);
1010 if (!bset)
1011 return -1;
1013 str = "{ [a] : exists e0, e1, e2: 32e1 = 31 + 31a + 31e0 and "
1014 "32e2 = 31 + 31e0 }";
1015 set = isl_set_read_from_str(ctx, str);
1016 bset = isl_set_affine_hull(set);
1017 str = "{ [a] : exists e : a = 32 e }";
1018 bset2 = isl_basic_set_read_from_str(ctx, str);
1019 subset = isl_basic_set_is_subset(bset, bset2);
1020 isl_basic_set_free(bset);
1021 isl_basic_set_free(bset2);
1022 if (subset < 0)
1023 return -1;
1024 if (!subset)
1025 isl_die(ctx, isl_error_unknown, "not as accurate as expected",
1026 return -1);
1028 return 0;
1031 void test_convex_hull_case(struct isl_ctx *ctx, const char *name)
1033 char *filename;
1034 FILE *input;
1035 struct isl_basic_set *bset1, *bset2;
1036 struct isl_set *set;
1038 filename = get_filename(ctx, name, "polylib");
1039 assert(filename);
1040 input = fopen(filename, "r");
1041 assert(input);
1043 bset1 = isl_basic_set_read_from_file(ctx, input);
1044 bset2 = isl_basic_set_read_from_file(ctx, input);
1046 set = isl_basic_set_union(bset1, bset2);
1047 bset1 = isl_set_convex_hull(set);
1049 bset2 = isl_basic_set_read_from_file(ctx, input);
1051 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1053 isl_basic_set_free(bset1);
1054 isl_basic_set_free(bset2);
1055 free(filename);
1057 fclose(input);
1060 struct {
1061 const char *set;
1062 const char *hull;
1063 } convex_hull_tests[] = {
1064 { "{ [i0, i1, i2] : (i2 = 1 and i0 = 0 and i1 >= 0) or "
1065 "(i0 = 1 and i1 = 0 and i2 = 1) or "
1066 "(i0 = 0 and i1 = 0 and i2 = 0) }",
1067 "{ [i0, i1, i2] : i0 >= 0 and i2 >= i0 and i2 <= 1 and i1 >= 0 }" },
1068 { "[n] -> { [i0, i1, i0] : i0 <= -4 + n; "
1069 "[i0, i0, i2] : n = 6 and i0 >= 0 and i2 <= 7 - i0 and "
1070 "i2 <= 5 and i2 >= 4; "
1071 "[3, i1, 3] : n = 5 and i1 <= 2 and i1 >= 0 }",
1072 "[n] -> { [i0, i1, i2] : i2 <= -1 + n and 2i2 <= -6 + 3n - i0 and "
1073 "i2 <= 5 + i0 and i2 >= i0 }" },
1076 void test_convex_hull_algo(struct isl_ctx *ctx, int convex)
1078 int i;
1079 int orig_convex = ctx->opt->convex;
1080 ctx->opt->convex = convex;
1082 test_convex_hull_case(ctx, "convex0");
1083 test_convex_hull_case(ctx, "convex1");
1084 test_convex_hull_case(ctx, "convex2");
1085 test_convex_hull_case(ctx, "convex3");
1086 test_convex_hull_case(ctx, "convex4");
1087 test_convex_hull_case(ctx, "convex5");
1088 test_convex_hull_case(ctx, "convex6");
1089 test_convex_hull_case(ctx, "convex7");
1090 test_convex_hull_case(ctx, "convex8");
1091 test_convex_hull_case(ctx, "convex9");
1092 test_convex_hull_case(ctx, "convex10");
1093 test_convex_hull_case(ctx, "convex11");
1094 test_convex_hull_case(ctx, "convex12");
1095 test_convex_hull_case(ctx, "convex13");
1096 test_convex_hull_case(ctx, "convex14");
1097 test_convex_hull_case(ctx, "convex15");
1099 for (i = 0; i < ARRAY_SIZE(convex_hull_tests); ++i) {
1100 isl_set *set1, *set2;
1102 set1 = isl_set_read_from_str(ctx, convex_hull_tests[i].set);
1103 set2 = isl_set_read_from_str(ctx, convex_hull_tests[i].hull);
1104 set1 = isl_set_from_basic_set(isl_set_convex_hull(set1));
1105 assert(isl_set_is_equal(set1, set2));
1106 isl_set_free(set1);
1107 isl_set_free(set2);
1110 ctx->opt->convex = orig_convex;
1113 void test_convex_hull(struct isl_ctx *ctx)
1115 test_convex_hull_algo(ctx, ISL_CONVEX_HULL_FM);
1116 test_convex_hull_algo(ctx, ISL_CONVEX_HULL_WRAP);
1119 void test_gist_case(struct isl_ctx *ctx, const char *name)
1121 char *filename;
1122 FILE *input;
1123 struct isl_basic_set *bset1, *bset2;
1125 filename = get_filename(ctx, name, "polylib");
1126 assert(filename);
1127 input = fopen(filename, "r");
1128 assert(input);
1130 bset1 = isl_basic_set_read_from_file(ctx, input);
1131 bset2 = isl_basic_set_read_from_file(ctx, input);
1133 bset1 = isl_basic_set_gist(bset1, bset2);
1135 bset2 = isl_basic_set_read_from_file(ctx, input);
1137 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
1139 isl_basic_set_free(bset1);
1140 isl_basic_set_free(bset2);
1141 free(filename);
1143 fclose(input);
1146 struct {
1147 const char *set;
1148 const char *context;
1149 const char *gist;
1150 } gist_tests[] = {
1151 { "{ [a, b, c] : a <= 15 and a >= 1 }",
1152 "{ [a, b, c] : exists (e0 = floor((-1 + a)/16): a >= 1 and "
1153 "c <= 30 and 32e0 >= -62 + 2a + 2b - c and b >= 0) }",
1154 "{ [a, b, c] : a <= 15 }" },
1155 { "{ : }", "{ : 1 = 0 }", "{ : }" },
1156 { "{ : 1 = 0 }", "{ : 1 = 0 }", "{ : }" },
1159 static int test_gist(struct isl_ctx *ctx)
1161 int i;
1162 const char *str;
1163 isl_basic_set *bset1, *bset2;
1164 isl_map *map1, *map2;
1165 int equal;
1167 for (i = 0; i < ARRAY_SIZE(gist_tests); ++i) {
1168 bset1 = isl_basic_set_read_from_str(ctx, gist_tests[i].set);
1169 bset2 = isl_basic_set_read_from_str(ctx, gist_tests[i].context);
1170 bset1 = isl_basic_set_gist(bset1, bset2);
1171 bset2 = isl_basic_set_read_from_str(ctx, gist_tests[i].gist);
1172 equal = isl_basic_set_is_equal(bset1, bset2);
1173 isl_basic_set_free(bset1);
1174 isl_basic_set_free(bset2);
1175 if (equal < 0)
1176 return -1;
1177 if (!equal)
1178 isl_die(ctx, isl_error_unknown,
1179 "incorrect gist result", return -1);
1182 test_gist_case(ctx, "gist1");
1184 str = "[p0, p2, p3, p5, p6, p10] -> { [] : "
1185 "exists (e0 = [(15 + p0 + 15p6 + 15p10)/16], e1 = [(p5)/8], "
1186 "e2 = [(p6)/128], e3 = [(8p2 - p5)/128], "
1187 "e4 = [(128p3 - p6)/4096]: 8e1 = p5 and 128e2 = p6 and "
1188 "128e3 = 8p2 - p5 and 4096e4 = 128p3 - p6 and p2 >= 0 and "
1189 "16e0 >= 16 + 16p6 + 15p10 and p2 <= 15 and p3 >= 0 and "
1190 "p3 <= 31 and p6 >= 128p3 and p5 >= 8p2 and p10 >= 0 and "
1191 "16e0 <= 15 + p0 + 15p6 + 15p10 and 16e0 >= p0 + 15p6 + 15p10 and "
1192 "p10 <= 15 and p10 <= -1 + p0 - p6) }";
1193 bset1 = isl_basic_set_read_from_str(ctx, str);
1194 str = "[p0, p2, p3, p5, p6, p10] -> { [] : exists (e0 = [(p5)/8], "
1195 "e1 = [(p6)/128], e2 = [(8p2 - p5)/128], "
1196 "e3 = [(128p3 - p6)/4096]: 8e0 = p5 and 128e1 = p6 and "
1197 "128e2 = 8p2 - p5 and 4096e3 = 128p3 - p6 and p5 >= -7 and "
1198 "p2 >= 0 and 8p2 <= -1 + p0 and p2 <= 15 and p3 >= 0 and "
1199 "p3 <= 31 and 128p3 <= -1 + p0 and p6 >= -127 and "
1200 "p5 <= -1 + p0 and p6 <= -1 + p0 and p6 >= 128p3 and "
1201 "p0 >= 1 and p5 >= 8p2 and p10 >= 0 and p10 <= 15 ) }";
1202 bset2 = isl_basic_set_read_from_str(ctx, str);
1203 bset1 = isl_basic_set_gist(bset1, bset2);
1204 assert(bset1 && bset1->n_div == 0);
1205 isl_basic_set_free(bset1);
1207 /* Check that the integer divisions of the second disjunct
1208 * do not spread to the first disjunct.
1210 str = "[t1] -> { S_0[] -> A[o0] : (exists (e0 = [(-t1 + o0)/16]: "
1211 "16e0 = -t1 + o0 and o0 >= 0 and o0 <= 15 and t1 >= 0)) or "
1212 "(exists (e0 = [(-1 + t1)/16], "
1213 "e1 = [(-16 + t1 - 16e0)/4294967296]: "
1214 "4294967296e1 = -16 + t1 - o0 - 16e0 and "
1215 "16e0 <= -1 + t1 and 16e0 >= -16 + t1 and o0 >= 0 and "
1216 "o0 <= 4294967295 and t1 <= -1)) }";
1217 map1 = isl_map_read_from_str(ctx, str);
1218 str = "[t1] -> { S_0[] -> A[o0] : t1 >= 0 and t1 <= 4294967295 }";
1219 map2 = isl_map_read_from_str(ctx, str);
1220 map1 = isl_map_gist(map1, map2);
1221 if (!map1)
1222 return -1;
1223 if (map1->n != 1)
1224 isl_die(ctx, isl_error_unknown, "expecting single disjunct",
1225 isl_map_free(map1); return -1);
1226 if (isl_basic_map_dim(map1->p[0], isl_dim_div) != 1)
1227 isl_die(ctx, isl_error_unknown, "expecting single div",
1228 isl_map_free(map1); return -1);
1229 isl_map_free(map1);
1231 return 0;
1234 int test_coalesce_set(isl_ctx *ctx, const char *str, int check_one)
1236 isl_set *set, *set2;
1237 int equal;
1238 int one;
1240 set = isl_set_read_from_str(ctx, str);
1241 set = isl_set_coalesce(set);
1242 set2 = isl_set_read_from_str(ctx, str);
1243 equal = isl_set_is_equal(set, set2);
1244 one = set && set->n == 1;
1245 isl_set_free(set);
1246 isl_set_free(set2);
1248 if (equal < 0)
1249 return -1;
1250 if (!equal)
1251 isl_die(ctx, isl_error_unknown,
1252 "coalesced set not equal to input", return -1);
1253 if (check_one && !one)
1254 isl_die(ctx, isl_error_unknown,
1255 "coalesced set should not be a union", return -1);
1257 return 0;
1260 int test_coalesce_unbounded_wrapping(isl_ctx *ctx)
1262 int r = 0;
1263 int bounded;
1265 bounded = isl_options_get_coalesce_bounded_wrapping(ctx);
1266 isl_options_set_coalesce_bounded_wrapping(ctx, 0);
1268 if (test_coalesce_set(ctx,
1269 "{[x,y,z] : y + 2 >= 0 and x - y + 1 >= 0 and "
1270 "-x - y + 1 >= 0 and -3 <= z <= 3;"
1271 "[x,y,z] : -x+z + 20 >= 0 and -x-z + 20 >= 0 and "
1272 "x-z + 20 >= 0 and x+z + 20 >= 0 and "
1273 "-10 <= y <= 0}", 1) < 0)
1274 goto error;
1275 if (test_coalesce_set(ctx,
1276 "{[x,y] : 0 <= x,y <= 10; [5,y]: 4 <=y <= 11}", 1) < 0)
1277 goto error;
1278 if (test_coalesce_set(ctx,
1279 "{[x,0,0] : -5 <= x <= 5; [0,y,1] : -5 <= y <= 5 }", 1) < 0)
1280 goto error;
1282 if (0) {
1283 error:
1284 r = -1;
1287 isl_options_set_coalesce_bounded_wrapping(ctx, bounded);
1289 return r;
1292 /* Inputs for coalescing tests.
1293 * "str" is a string representation of the input set.
1294 * "single_disjunct" is set if we expect the result to consist of
1295 * a single disjunct.
1297 struct {
1298 int single_disjunct;
1299 const char *str;
1300 } coalesce_tests[] = {
1301 { 1, "{[x,y]: x >= 0 & x <= 10 & y >= 0 & y <= 10 or "
1302 "y >= x & x >= 2 & 5 >= y }" },
1303 { 1, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
1304 "x + y >= 10 & y <= x & x + y <= 20 & y >= 0}" },
1305 { 0, "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
1306 "x + y >= 10 & y <= x & x + y <= 19 & y >= 0}" },
1307 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1308 "y >= 0 & x >= 6 & x <= 10 & y <= x}" },
1309 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1310 "y >= 0 & x >= 7 & x <= 10 & y <= x}" },
1311 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or "
1312 "y >= 0 & x >= 6 & x <= 10 & y + 1 <= x}" },
1313 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 6}" },
1314 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 7 & y <= 6}" },
1315 { 1, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 5}" },
1316 { 0, "{[x,y]: y >= 0 & x <= 5 & y <= x or y >= 0 & x = 6 & y <= 7}" },
1317 { 1, "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }" },
1318 { 0, "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}" },
1319 { 1, "[n] -> { [i] : 1 <= i and i <= n - 1 or 2 <= i and i <= n }" },
1320 { 0, "[n] -> { [[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1321 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1322 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1323 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
1324 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
1325 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
1326 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
1327 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
1328 "[[i0] -> [o0]] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
1329 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
1330 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
1331 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
1332 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
1333 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
1334 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
1335 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
1336 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
1337 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }" },
1338 { 0, "[n, m] -> { [o0, o2, o3] : (o3 = 1 and o0 >= 1 + m and "
1339 "o0 <= n + m and o2 <= m and o0 >= 2 + n and o2 >= 3) or "
1340 "(o0 >= 2 + n and o0 >= 1 + m and o0 <= n + m and n >= 1 and "
1341 "o3 <= -1 + o2 and o3 >= 1 - m + o2 and o3 >= 2 and o3 <= n) }" },
1342 { 0, "[M, N] -> { [[i0, i1, i2, i3, i4, i5, i6] -> "
1343 "[o0, o1, o2, o3, o4, o5, o6]] : "
1344 "(o6 <= -4 + 2M - 2N + i0 + i1 - i2 + i6 - o0 - o1 + o2 and "
1345 "o3 <= -2 + i3 and o6 >= 2 + i0 + i3 + i6 - o0 - o3 and "
1346 "o6 >= 2 - M + N + i3 + i4 + i6 - o3 - o4 and o0 <= -1 + i0 and "
1347 "o4 >= 4 - 3M + 3N - i0 - i1 + i2 + 2i3 + i4 + o0 + o1 - o2 - 2o3 "
1348 "and o6 <= -3 + 2M - 2N + i3 + i4 - i5 + i6 - o3 - o4 + o5 and "
1349 "2o6 <= -5 + 5M - 5N + 2i0 + i1 - i2 - i5 + 2i6 - 2o0 - o1 + o2 + o5 "
1350 "and o6 >= 2i0 + i1 + i6 - 2o0 - o1 and "
1351 "3o6 <= -5 + 4M - 4N + 2i0 + i1 - i2 + 2i3 + i4 - i5 + 3i6 "
1352 "- 2o0 - o1 + o2 - 2o3 - o4 + o5) or "
1353 "(N >= 2 and o3 <= -1 + i3 and o0 <= -1 + i0 and "
1354 "o6 >= i3 + i6 - o3 and M >= 0 and "
1355 "2o6 >= 1 + i0 + i3 + 2i6 - o0 - o3 and "
1356 "o6 >= 1 - M + i0 + i6 - o0 and N >= 2M and o6 >= i0 + i6 - o0) }" },
1357 { 0, "[M, N] -> { [o0] : (o0 = 0 and M >= 1 and N >= 2) or "
1358 "(o0 = 0 and M >= 1 and N >= 2M and N >= 2 + M) or "
1359 "(o0 = 0 and M >= 2 and N >= 3) or "
1360 "(M = 0 and o0 = 0 and N >= 3) }" },
1361 { 0, "{ [i0, i1, i2, i3] : (i1 = 10i0 and i0 >= 1 and 10i0 <= 100 and "
1362 "i3 <= 9 + 10 i2 and i3 >= 1 + 10i2 and i3 >= 0) or "
1363 "(i1 <= 9 + 10i0 and i1 >= 1 + 10i0 and i2 >= 0 and "
1364 "i0 >= 0 and i1 <= 100 and i3 <= 9 + 10i2 and i3 >= 1 + 10i2) }" },
1365 { 0, "[M] -> { [i1] : (i1 >= 2 and i1 <= M) or (i1 = M and M >= 1) }" },
1366 { 0, "{[x,y] : x,y >= 0; [x,y] : 10 <= x <= 20 and y >= -1 }" },
1367 { 1, "{ [x, y] : (x >= 1 and y >= 1 and x <= 2 and y <= 2) or "
1368 "(y = 3 and x = 1) }" },
1369 { 1, "[M] -> { [i0, i1, i2, i3, i4] : (i1 >= 3 and i4 >= 2 + i2 and "
1370 "i2 >= 2 and i0 >= 2 and i3 >= 1 + i2 and i0 <= M and "
1371 "i1 <= M and i3 <= M and i4 <= M) or "
1372 "(i1 >= 2 and i4 >= 1 + i2 and i2 >= 2 and i0 >= 2 and "
1373 "i3 >= 1 + i2 and i0 <= M and i1 <= -1 + M and i3 <= M and "
1374 "i4 <= -1 + M) }" },
1375 { 1, "{ [x, y] : (x >= 0 and y >= 0 and x <= 10 and y <= 10) or "
1376 "(x >= 1 and y >= 1 and x <= 11 and y <= 11) }" },
1377 { 0, "{[x,0] : x >= 0; [x,1] : x <= 20}" },
1378 { 1, "{ [x, 1 - x] : 0 <= x <= 1; [0,0] }" },
1379 { 1, "{ [0,0]; [i,i] : 1 <= i <= 10 }" },
1380 { 0, "{ [0,0]; [i,j] : 1 <= i,j <= 10 }" },
1381 { 1, "{ [0,0]; [i,2i] : 1 <= i <= 10 }" },
1382 { 0, "{ [0,0]; [i,2i] : 2 <= i <= 10 }" },
1383 { 0, "{ [1,0]; [i,2i] : 1 <= i <= 10 }" },
1384 { 0, "{ [0,1]; [i,2i] : 1 <= i <= 10 }" },
1385 { 0, "{ [a, b] : exists e : 2e = a and "
1386 "a >= 0 and (a <= 3 or (b <= 0 and b >= -4 + a)) }" },
1387 { 0, "{ [i, j, i', j'] : i <= 2 and j <= 2 and "
1388 "j' >= -1 + 2i + j - 2i' and i' <= -1 + i and "
1389 "j >= 1 and j' <= i + j - i' and i >= 1; "
1390 "[1, 1, 1, 1] }" },
1391 { 1, "{ [i,j] : exists a,b : i = 2a and j = 3b; "
1392 "[i,j] : exists a : j = 3a }" },
1393 { 1, "{ [a, b, c] : (c <= 7 - b and b <= 1 and b >= 0 and "
1394 "c >= 3 + b and b <= 3 + 8a and b >= -26 + 8a and "
1395 "a >= 3) or "
1396 "(b <= 1 and c <= 7 and b >= 0 and c >= 4 + b and "
1397 "b <= 3 + 8a and b >= -26 + 8a and a >= 3) }" },
1398 { 1, "{ [a, 0, c] : c >= 1 and c <= 29 and c >= -1 + 8a and "
1399 "c <= 6 + 8a and a >= 3; "
1400 "[a, -1, c] : c >= 1 and c <= 30 and c >= 8a and "
1401 "c <= 7 + 8a and a >= 3 and a <= 4 }" },
1402 { 1, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
1403 "[x,0] : 3 <= x <= 4 }" },
1404 { 1, "{ [x,y] : 0 <= x <= 3 and y >= 0 and x + 3y <= 6; "
1405 "[x,0] : 4 <= x <= 5 }" },
1406 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + 2y <= 4; "
1407 "[x,0] : 3 <= x <= 5 }" },
1408 { 0, "{ [x,y] : 0 <= x <= 2 and y >= 0 and x + y <= 4; "
1409 "[x,0] : 3 <= x <= 4 }" },
1410 { 1 , "{ [i0, i1] : i0 <= 122 and i0 >= 1 and 128i1 >= -249 + i0 and "
1411 "i1 <= 0; "
1412 "[i0, 0] : i0 >= 123 and i0 <= 124 }" },
1415 /* Test the functionality of isl_set_coalesce.
1416 * That is, check that the output is always equal to the input
1417 * and in some cases that the result consists of a single disjunct.
1419 static int test_coalesce(struct isl_ctx *ctx)
1421 int i;
1423 for (i = 0; i < ARRAY_SIZE(coalesce_tests); ++i) {
1424 const char *str = coalesce_tests[i].str;
1425 int check_one = coalesce_tests[i].single_disjunct;
1426 if (test_coalesce_set(ctx, str, check_one) < 0)
1427 return -1;
1430 if (test_coalesce_unbounded_wrapping(ctx) < 0)
1431 return -1;
1433 return 0;
1436 void test_closure(struct isl_ctx *ctx)
1438 const char *str;
1439 isl_set *dom;
1440 isl_map *up, *right;
1441 isl_map *map, *map2;
1442 int exact;
1444 /* COCOA example 1 */
1445 map = isl_map_read_from_str(ctx,
1446 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
1447 "1 <= i and i < n and 1 <= j and j < n or "
1448 "i2 = i + 1 and j2 = j - 1 and "
1449 "1 <= i and i < n and 2 <= j and j <= n }");
1450 map = isl_map_power(map, &exact);
1451 assert(exact);
1452 isl_map_free(map);
1454 /* COCOA example 1 */
1455 map = isl_map_read_from_str(ctx,
1456 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
1457 "1 <= i and i < n and 1 <= j and j < n or "
1458 "i2 = i + 1 and j2 = j - 1 and "
1459 "1 <= i and i < n and 2 <= j and j <= n }");
1460 map = isl_map_transitive_closure(map, &exact);
1461 assert(exact);
1462 map2 = isl_map_read_from_str(ctx,
1463 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
1464 "1 <= i and i < n and 1 <= j and j <= n and "
1465 "2 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
1466 "i2 = i + k1 + k2 and j2 = j + k1 - k2 and "
1467 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1 )}");
1468 assert(isl_map_is_equal(map, map2));
1469 isl_map_free(map2);
1470 isl_map_free(map);
1472 map = isl_map_read_from_str(ctx,
1473 "[n] -> { [x] -> [y] : y = x + 1 and 0 <= x and x <= n and "
1474 " 0 <= y and y <= n }");
1475 map = isl_map_transitive_closure(map, &exact);
1476 map2 = isl_map_read_from_str(ctx,
1477 "[n] -> { [x] -> [y] : y > x and 0 <= x and x <= n and "
1478 " 0 <= y and y <= n }");
1479 assert(isl_map_is_equal(map, map2));
1480 isl_map_free(map2);
1481 isl_map_free(map);
1483 /* COCOA example 2 */
1484 map = isl_map_read_from_str(ctx,
1485 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j + 2 and "
1486 "1 <= i and i < n - 1 and 1 <= j and j < n - 1 or "
1487 "i2 = i + 2 and j2 = j - 2 and "
1488 "1 <= i and i < n - 1 and 3 <= j and j <= n }");
1489 map = isl_map_transitive_closure(map, &exact);
1490 assert(exact);
1491 map2 = isl_map_read_from_str(ctx,
1492 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
1493 "1 <= i and i < n - 1 and 1 <= j and j <= n and "
1494 "3 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
1495 "i2 = i + 2 k1 + 2 k2 and j2 = j + 2 k1 - 2 k2 and "
1496 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1) }");
1497 assert(isl_map_is_equal(map, map2));
1498 isl_map_free(map);
1499 isl_map_free(map2);
1501 /* COCOA Fig.2 left */
1502 map = isl_map_read_from_str(ctx,
1503 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j and "
1504 "i <= 2 j - 3 and i <= n - 2 and j <= 2 i - 1 and "
1505 "j <= n or "
1506 "i2 = i and j2 = j + 2 and i <= 2 j - 1 and i <= n and "
1507 "j <= 2 i - 3 and j <= n - 2 or "
1508 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1509 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1510 map = isl_map_transitive_closure(map, &exact);
1511 assert(exact);
1512 isl_map_free(map);
1514 /* COCOA Fig.2 right */
1515 map = isl_map_read_from_str(ctx,
1516 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
1517 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
1518 "j <= n or "
1519 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
1520 "j <= 2 i - 4 and j <= n - 3 or "
1521 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1522 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1523 map = isl_map_power(map, &exact);
1524 assert(exact);
1525 isl_map_free(map);
1527 /* COCOA Fig.2 right */
1528 map = isl_map_read_from_str(ctx,
1529 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
1530 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
1531 "j <= n or "
1532 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
1533 "j <= 2 i - 4 and j <= n - 3 or "
1534 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1535 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }");
1536 map = isl_map_transitive_closure(map, &exact);
1537 assert(exact);
1538 map2 = isl_map_read_from_str(ctx,
1539 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k3,k : "
1540 "i <= 2 j - 1 and i <= n and j <= 2 i - 1 and "
1541 "j <= n and 3 + i + 2 j <= 3 n and "
1542 "3 + 2 i + j <= 3n and i2 <= 2 j2 -1 and i2 <= n and "
1543 "i2 <= 3 j2 - 4 and j2 <= 2 i2 -1 and j2 <= n and "
1544 "13 + 4 j2 <= 11 i2 and i2 = i + 3 k1 + k3 and "
1545 "j2 = j + 3 k2 + k3 and k1 >= 0 and k2 >= 0 and "
1546 "k3 >= 0 and k1 + k2 + k3 = k and k > 0) }");
1547 assert(isl_map_is_equal(map, map2));
1548 isl_map_free(map2);
1549 isl_map_free(map);
1551 /* COCOA Fig.1 right */
1552 dom = isl_set_read_from_str(ctx,
1553 "{ [x,y] : x >= 0 and -2 x + 3 y >= 0 and x <= 3 and "
1554 "2 x - 3 y + 3 >= 0 }");
1555 right = isl_map_read_from_str(ctx,
1556 "{ [x,y] -> [x2,y2] : x2 = x + 1 and y2 = y }");
1557 up = isl_map_read_from_str(ctx,
1558 "{ [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 }");
1559 right = isl_map_intersect_domain(right, isl_set_copy(dom));
1560 right = isl_map_intersect_range(right, isl_set_copy(dom));
1561 up = isl_map_intersect_domain(up, isl_set_copy(dom));
1562 up = isl_map_intersect_range(up, dom);
1563 map = isl_map_union(up, right);
1564 map = isl_map_transitive_closure(map, &exact);
1565 assert(exact);
1566 map2 = isl_map_read_from_str(ctx,
1567 "{ [0,0] -> [0,1]; [0,0] -> [1,1]; [0,1] -> [1,1]; "
1568 " [2,2] -> [3,2]; [2,2] -> [3,3]; [3,2] -> [3,3] }");
1569 assert(isl_map_is_equal(map, map2));
1570 isl_map_free(map2);
1571 isl_map_free(map);
1573 /* COCOA Theorem 1 counter example */
1574 map = isl_map_read_from_str(ctx,
1575 "{ [i,j] -> [i2,j2] : i = 0 and 0 <= j and j <= 1 and "
1576 "i2 = 1 and j2 = j or "
1577 "i = 0 and j = 0 and i2 = 0 and j2 = 1 }");
1578 map = isl_map_transitive_closure(map, &exact);
1579 assert(exact);
1580 isl_map_free(map);
1582 map = isl_map_read_from_str(ctx,
1583 "[m,n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 2 and "
1584 "1 <= i,i2 <= n and 1 <= j,j2 <= m or "
1585 "i2 = i + 1 and 3 <= j2 - j <= 4 and "
1586 "1 <= i,i2 <= n and 1 <= j,j2 <= m }");
1587 map = isl_map_transitive_closure(map, &exact);
1588 assert(exact);
1589 isl_map_free(map);
1591 /* Kelly et al 1996, fig 12 */
1592 map = isl_map_read_from_str(ctx,
1593 "[n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 1 and "
1594 "1 <= i,j,j+1 <= n or "
1595 "j = n and j2 = 1 and i2 = i + 1 and "
1596 "1 <= i,i+1 <= n }");
1597 map = isl_map_transitive_closure(map, &exact);
1598 assert(exact);
1599 map2 = isl_map_read_from_str(ctx,
1600 "[n] -> { [i,j] -> [i2,j2] : 1 <= j < j2 <= n and "
1601 "1 <= i <= n and i = i2 or "
1602 "1 <= i < i2 <= n and 1 <= j <= n and "
1603 "1 <= j2 <= n }");
1604 assert(isl_map_is_equal(map, map2));
1605 isl_map_free(map2);
1606 isl_map_free(map);
1608 /* Omega's closure4 */
1609 map = isl_map_read_from_str(ctx,
1610 "[m,n] -> { [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 and "
1611 "1 <= x,y <= 10 or "
1612 "x2 = x + 1 and y2 = y and "
1613 "1 <= x <= 20 && 5 <= y <= 15 }");
1614 map = isl_map_transitive_closure(map, &exact);
1615 assert(exact);
1616 isl_map_free(map);
1618 map = isl_map_read_from_str(ctx,
1619 "[n] -> { [x] -> [y]: 1 <= n <= y - x <= 10 }");
1620 map = isl_map_transitive_closure(map, &exact);
1621 assert(!exact);
1622 map2 = isl_map_read_from_str(ctx,
1623 "[n] -> { [x] -> [y] : 1 <= n <= 10 and y >= n + x }");
1624 assert(isl_map_is_equal(map, map2));
1625 isl_map_free(map);
1626 isl_map_free(map2);
1628 str = "[n, m] -> { [i0, i1, i2, i3] -> [o0, o1, o2, o3] : "
1629 "i3 = 1 and o0 = i0 and o1 = -1 + i1 and o2 = -1 + i2 and "
1630 "o3 = -2 + i2 and i1 <= -1 + i0 and i1 >= 1 - m + i0 and "
1631 "i1 >= 2 and i1 <= n and i2 >= 3 and i2 <= 1 + n and i2 <= m }";
1632 map = isl_map_read_from_str(ctx, str);
1633 map = isl_map_transitive_closure(map, &exact);
1634 assert(exact);
1635 map2 = isl_map_read_from_str(ctx, str);
1636 assert(isl_map_is_equal(map, map2));
1637 isl_map_free(map);
1638 isl_map_free(map2);
1640 str = "{[0] -> [1]; [2] -> [3]}";
1641 map = isl_map_read_from_str(ctx, str);
1642 map = isl_map_transitive_closure(map, &exact);
1643 assert(exact);
1644 map2 = isl_map_read_from_str(ctx, str);
1645 assert(isl_map_is_equal(map, map2));
1646 isl_map_free(map);
1647 isl_map_free(map2);
1649 str = "[n] -> { [[i0, i1, 1, 0, i0] -> [i5, 1]] -> "
1650 "[[i0, -1 + i1, 2, 0, i0] -> [-1 + i5, 2]] : "
1651 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 2 and "
1652 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1653 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1654 "[[i0, i1, 2, 0, i0] -> [i5, 1]] -> "
1655 "[[i0, i1, 1, 0, i0] -> [-1 + i5, 2]] : "
1656 "exists (e0 = [(3 - n)/3]: i5 >= 2 and i1 >= 1 and "
1657 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1658 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1659 "[[i0, i1, 1, 0, i0] -> [i5, 2]] -> "
1660 "[[i0, -1 + i1, 2, 0, i0] -> [i5, 1]] : "
1661 "exists (e0 = [(3 - n)/3]: i1 >= 2 and i5 >= 1 and "
1662 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1663 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n); "
1664 "[[i0, i1, 2, 0, i0] -> [i5, 2]] -> "
1665 "[[i0, i1, 1, 0, i0] -> [i5, 1]] : "
1666 "exists (e0 = [(3 - n)/3]: i5 >= 1 and i1 >= 1 and "
1667 "3i0 <= -1 + n and i1 <= -1 + n and i5 <= -1 + n and "
1668 "3e0 >= 1 - n and 3e0 <= 2 - n and 3i0 >= -2 + n) }";
1669 map = isl_map_read_from_str(ctx, str);
1670 map = isl_map_transitive_closure(map, NULL);
1671 assert(map);
1672 isl_map_free(map);
1675 void test_lex(struct isl_ctx *ctx)
1677 isl_space *dim;
1678 isl_map *map;
1680 dim = isl_space_set_alloc(ctx, 0, 0);
1681 map = isl_map_lex_le(dim);
1682 assert(!isl_map_is_empty(map));
1683 isl_map_free(map);
1686 static int test_lexmin(struct isl_ctx *ctx)
1688 int equal;
1689 const char *str;
1690 isl_basic_map *bmap;
1691 isl_map *map, *map2;
1692 isl_set *set;
1693 isl_set *set2;
1694 isl_pw_multi_aff *pma;
1696 str = "[p0, p1] -> { [] -> [] : "
1697 "exists (e0 = [(2p1)/3], e1, e2, e3 = [(3 - p1 + 3e0)/3], "
1698 "e4 = [(p1)/3], e5 = [(p1 + 3e4)/3]: "
1699 "3e0 >= -2 + 2p1 and 3e0 >= p1 and 3e3 >= 1 - p1 + 3e0 and "
1700 "3e0 <= 2p1 and 3e3 >= -2 + p1 and 3e3 <= -1 + p1 and p1 >= 3 and "
1701 "3e5 >= -2 + 2p1 and 3e5 >= p1 and 3e5 <= -1 + p1 + 3e4 and "
1702 "3e4 <= p1 and 3e4 >= -2 + p1 and e3 <= -1 + e0 and "
1703 "3e4 >= 6 - p1 + 3e1 and 3e1 >= p1 and 3e5 >= -2 + p1 + 3e4 and "
1704 "2e4 >= 3 - p1 + 2e1 and e4 <= e1 and 3e3 <= 2 - p1 + 3e0 and "
1705 "e5 >= 1 + e1 and 3e4 >= 6 - 2p1 + 3e1 and "
1706 "p0 >= 2 and p1 >= p0 and 3e2 >= p1 and 3e4 >= 6 - p1 + 3e2 and "
1707 "e2 <= e1 and e3 >= 1 and e4 <= e2) }";
1708 map = isl_map_read_from_str(ctx, str);
1709 map = isl_map_lexmin(map);
1710 isl_map_free(map);
1712 str = "[C] -> { [obj,a,b,c] : obj <= 38 a + 7 b + 10 c and "
1713 "a + b <= 1 and c <= 10 b and c <= C and a,b,c,C >= 0 }";
1714 set = isl_set_read_from_str(ctx, str);
1715 set = isl_set_lexmax(set);
1716 str = "[C] -> { [obj,a,b,c] : C = 8 }";
1717 set2 = isl_set_read_from_str(ctx, str);
1718 set = isl_set_intersect(set, set2);
1719 assert(!isl_set_is_empty(set));
1720 isl_set_free(set);
1722 str = "{ [x] -> [y] : x <= y <= 10; [x] -> [5] : -8 <= x <= 8 }";
1723 map = isl_map_read_from_str(ctx, str);
1724 map = isl_map_lexmin(map);
1725 str = "{ [x] -> [5] : 6 <= x <= 8; "
1726 "[x] -> [x] : x <= 5 or (9 <= x <= 10) }";
1727 map2 = isl_map_read_from_str(ctx, str);
1728 assert(isl_map_is_equal(map, map2));
1729 isl_map_free(map);
1730 isl_map_free(map2);
1732 str = "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }";
1733 map = isl_map_read_from_str(ctx, str);
1734 map2 = isl_map_copy(map);
1735 map = isl_map_lexmin(map);
1736 assert(isl_map_is_equal(map, map2));
1737 isl_map_free(map);
1738 isl_map_free(map2);
1740 str = "{ [x] -> [y] : x = 4y; [x] -> [y] : x = 2y }";
1741 map = isl_map_read_from_str(ctx, str);
1742 map = isl_map_lexmin(map);
1743 str = "{ [x] -> [y] : (4y = x and x >= 0) or "
1744 "(exists (e0 = [(x)/4], e1 = [(-2 + x)/4]: 2y = x and "
1745 "4e1 = -2 + x and 4e0 <= -1 + x and 4e0 >= -3 + x)) or "
1746 "(exists (e0 = [(x)/4]: 2y = x and 4e0 = x and x <= -4)) }";
1747 map2 = isl_map_read_from_str(ctx, str);
1748 assert(isl_map_is_equal(map, map2));
1749 isl_map_free(map);
1750 isl_map_free(map2);
1752 str = "{ [i] -> [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
1753 " 8i' <= i and 8i' >= -7 + i }";
1754 bmap = isl_basic_map_read_from_str(ctx, str);
1755 pma = isl_basic_map_lexmin_pw_multi_aff(isl_basic_map_copy(bmap));
1756 map2 = isl_map_from_pw_multi_aff(pma);
1757 map = isl_map_from_basic_map(bmap);
1758 assert(isl_map_is_equal(map, map2));
1759 isl_map_free(map);
1760 isl_map_free(map2);
1762 str = "{ T[a] -> S[b, c] : a = 4b-2c and c >= b }";
1763 map = isl_map_read_from_str(ctx, str);
1764 map = isl_map_lexmin(map);
1765 str = "{ T[a] -> S[b, c] : 2b = a and 2c = a }";
1766 map2 = isl_map_read_from_str(ctx, str);
1767 assert(isl_map_is_equal(map, map2));
1768 isl_map_free(map);
1769 isl_map_free(map2);
1771 /* Check that empty pieces are properly combined. */
1772 str = "[K, N] -> { [x, y] -> [a, b] : K+2<=N<=K+4 and x>=4 and "
1773 "2N-6<=x<K+N and N-1<=a<=K+N-1 and N+b-6<=a<=2N-4 and "
1774 "b<=2N-3K+a and 3b<=4N-K+1 and b>=N and a>=x+1 }";
1775 map = isl_map_read_from_str(ctx, str);
1776 map = isl_map_lexmin(map);
1777 str = "[K, N] -> { [x, y] -> [1 + x, N] : x >= -6 + 2N and "
1778 "x <= -5 + 2N and x >= -1 + 3K - N and x <= -2 + K + N and "
1779 "x >= 4 }";
1780 map2 = isl_map_read_from_str(ctx, str);
1781 assert(isl_map_is_equal(map, map2));
1782 isl_map_free(map);
1783 isl_map_free(map2);
1785 str = "[i] -> { [i', j] : j = i - 8i' and i' >= 0 and i' <= 7 and "
1786 " 8i' <= i and 8i' >= -7 + i }";
1787 set = isl_set_read_from_str(ctx, str);
1788 pma = isl_set_lexmin_pw_multi_aff(isl_set_copy(set));
1789 set2 = isl_set_from_pw_multi_aff(pma);
1790 equal = isl_set_is_equal(set, set2);
1791 isl_set_free(set);
1792 isl_set_free(set2);
1793 if (equal < 0)
1794 return -1;
1795 if (!equal)
1796 isl_die(ctx, isl_error_unknown,
1797 "unexpected difference between set and "
1798 "piecewise affine expression", return -1);
1800 return 0;
1803 /* Check that isl_set_min_val and isl_set_max_val compute the correct
1804 * result on non-convex inputs.
1806 static int test_min(struct isl_ctx *ctx)
1808 isl_set *set;
1809 isl_aff *aff;
1810 isl_val *val;
1811 int min_ok, max_ok;
1813 set = isl_set_read_from_str(ctx, "{ [-1]; [1] }");
1814 aff = isl_aff_read_from_str(ctx, "{ [x] -> [x] }");
1815 val = isl_set_min_val(set, aff);
1816 min_ok = isl_val_is_negone(val);
1817 isl_val_free(val);
1818 val = isl_set_max_val(set, aff);
1819 max_ok = isl_val_is_one(val);
1820 isl_val_free(val);
1821 isl_aff_free(aff);
1822 isl_set_free(set);
1824 if (min_ok < 0 || max_ok < 0)
1825 return -1;
1826 if (!min_ok)
1827 isl_die(ctx, isl_error_unknown,
1828 "unexpected minimum", return -1);
1829 if (!max_ok)
1830 isl_die(ctx, isl_error_unknown,
1831 "unexpected maximum", return -1);
1833 return 0;
1836 struct must_may {
1837 isl_map *must;
1838 isl_map *may;
1841 static int collect_must_may(__isl_take isl_map *dep, int must,
1842 void *dep_user, void *user)
1844 struct must_may *mm = (struct must_may *)user;
1846 if (must)
1847 mm->must = isl_map_union(mm->must, dep);
1848 else
1849 mm->may = isl_map_union(mm->may, dep);
1851 return 0;
1854 static int common_space(void *first, void *second)
1856 int depth = *(int *)first;
1857 return 2 * depth;
1860 static int map_is_equal(__isl_keep isl_map *map, const char *str)
1862 isl_map *map2;
1863 int equal;
1865 if (!map)
1866 return -1;
1868 map2 = isl_map_read_from_str(map->ctx, str);
1869 equal = isl_map_is_equal(map, map2);
1870 isl_map_free(map2);
1872 return equal;
1875 static int map_check_equal(__isl_keep isl_map *map, const char *str)
1877 int equal;
1879 equal = map_is_equal(map, str);
1880 if (equal < 0)
1881 return -1;
1882 if (!equal)
1883 isl_die(isl_map_get_ctx(map), isl_error_unknown,
1884 "result not as expected", return -1);
1885 return 0;
1888 void test_dep(struct isl_ctx *ctx)
1890 const char *str;
1891 isl_space *dim;
1892 isl_map *map;
1893 isl_access_info *ai;
1894 isl_flow *flow;
1895 int depth;
1896 struct must_may mm;
1898 depth = 3;
1900 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1901 map = isl_map_read_from_str(ctx, str);
1902 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1904 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1905 map = isl_map_read_from_str(ctx, str);
1906 ai = isl_access_info_add_source(ai, map, 1, &depth);
1908 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1909 map = isl_map_read_from_str(ctx, str);
1910 ai = isl_access_info_add_source(ai, map, 1, &depth);
1912 flow = isl_access_info_compute_flow(ai);
1913 dim = isl_space_alloc(ctx, 0, 3, 3);
1914 mm.must = isl_map_empty(isl_space_copy(dim));
1915 mm.may = isl_map_empty(dim);
1917 isl_flow_foreach(flow, collect_must_may, &mm);
1919 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10); "
1920 " [1,10,0] -> [2,5,0] }";
1921 assert(map_is_equal(mm.must, str));
1922 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1923 assert(map_is_equal(mm.may, str));
1925 isl_map_free(mm.must);
1926 isl_map_free(mm.may);
1927 isl_flow_free(flow);
1930 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1931 map = isl_map_read_from_str(ctx, str);
1932 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1934 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1935 map = isl_map_read_from_str(ctx, str);
1936 ai = isl_access_info_add_source(ai, map, 1, &depth);
1938 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1939 map = isl_map_read_from_str(ctx, str);
1940 ai = isl_access_info_add_source(ai, map, 0, &depth);
1942 flow = isl_access_info_compute_flow(ai);
1943 dim = isl_space_alloc(ctx, 0, 3, 3);
1944 mm.must = isl_map_empty(isl_space_copy(dim));
1945 mm.may = isl_map_empty(dim);
1947 isl_flow_foreach(flow, collect_must_may, &mm);
1949 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10) }";
1950 assert(map_is_equal(mm.must, str));
1951 str = "{ [0,5,0] -> [2,5,0]; [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
1952 assert(map_is_equal(mm.may, str));
1954 isl_map_free(mm.must);
1955 isl_map_free(mm.may);
1956 isl_flow_free(flow);
1959 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1960 map = isl_map_read_from_str(ctx, str);
1961 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1963 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1964 map = isl_map_read_from_str(ctx, str);
1965 ai = isl_access_info_add_source(ai, map, 0, &depth);
1967 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1968 map = isl_map_read_from_str(ctx, str);
1969 ai = isl_access_info_add_source(ai, map, 0, &depth);
1971 flow = isl_access_info_compute_flow(ai);
1972 dim = isl_space_alloc(ctx, 0, 3, 3);
1973 mm.must = isl_map_empty(isl_space_copy(dim));
1974 mm.may = isl_map_empty(dim);
1976 isl_flow_foreach(flow, collect_must_may, &mm);
1978 str = "{ [0,i,0] -> [2,i,0] : 0 <= i <= 10; "
1979 " [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
1980 assert(map_is_equal(mm.may, str));
1981 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1982 assert(map_is_equal(mm.must, str));
1984 isl_map_free(mm.must);
1985 isl_map_free(mm.may);
1986 isl_flow_free(flow);
1989 str = "{ [0,i,2] -> [i] : 0 <= i <= 10 }";
1990 map = isl_map_read_from_str(ctx, str);
1991 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1993 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1994 map = isl_map_read_from_str(ctx, str);
1995 ai = isl_access_info_add_source(ai, map, 0, &depth);
1997 str = "{ [0,i,1] -> [5] : 0 <= i <= 10 }";
1998 map = isl_map_read_from_str(ctx, str);
1999 ai = isl_access_info_add_source(ai, map, 0, &depth);
2001 flow = isl_access_info_compute_flow(ai);
2002 dim = isl_space_alloc(ctx, 0, 3, 3);
2003 mm.must = isl_map_empty(isl_space_copy(dim));
2004 mm.may = isl_map_empty(dim);
2006 isl_flow_foreach(flow, collect_must_may, &mm);
2008 str = "{ [0,i,0] -> [0,i,2] : 0 <= i <= 10; "
2009 " [0,i,1] -> [0,5,2] : 0 <= i <= 5 }";
2010 assert(map_is_equal(mm.may, str));
2011 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2012 assert(map_is_equal(mm.must, str));
2014 isl_map_free(mm.must);
2015 isl_map_free(mm.may);
2016 isl_flow_free(flow);
2019 str = "{ [0,i,1] -> [i] : 0 <= i <= 10 }";
2020 map = isl_map_read_from_str(ctx, str);
2021 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
2023 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
2024 map = isl_map_read_from_str(ctx, str);
2025 ai = isl_access_info_add_source(ai, map, 0, &depth);
2027 str = "{ [0,i,2] -> [5] : 0 <= i <= 10 }";
2028 map = isl_map_read_from_str(ctx, str);
2029 ai = isl_access_info_add_source(ai, map, 0, &depth);
2031 flow = isl_access_info_compute_flow(ai);
2032 dim = isl_space_alloc(ctx, 0, 3, 3);
2033 mm.must = isl_map_empty(isl_space_copy(dim));
2034 mm.may = isl_map_empty(dim);
2036 isl_flow_foreach(flow, collect_must_may, &mm);
2038 str = "{ [0,i,0] -> [0,i,1] : 0 <= i <= 10; "
2039 " [0,i,2] -> [0,5,1] : 0 <= i <= 4 }";
2040 assert(map_is_equal(mm.may, str));
2041 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
2042 assert(map_is_equal(mm.must, str));
2044 isl_map_free(mm.must);
2045 isl_map_free(mm.may);
2046 isl_flow_free(flow);
2049 depth = 5;
2051 str = "{ [1,i,0,0,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
2052 map = isl_map_read_from_str(ctx, str);
2053 ai = isl_access_info_alloc(map, &depth, &common_space, 1);
2055 str = "{ [0,i,0,j,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
2056 map = isl_map_read_from_str(ctx, str);
2057 ai = isl_access_info_add_source(ai, map, 1, &depth);
2059 flow = isl_access_info_compute_flow(ai);
2060 dim = isl_space_alloc(ctx, 0, 5, 5);
2061 mm.must = isl_map_empty(isl_space_copy(dim));
2062 mm.may = isl_map_empty(dim);
2064 isl_flow_foreach(flow, collect_must_may, &mm);
2066 str = "{ [0,i,0,j,0] -> [1,i,0,0,0] : 0 <= i,j <= 10 }";
2067 assert(map_is_equal(mm.must, str));
2068 str = "{ [0,0,0,0,0] -> [0,0,0,0,0] : 1 = 0 }";
2069 assert(map_is_equal(mm.may, str));
2071 isl_map_free(mm.must);
2072 isl_map_free(mm.may);
2073 isl_flow_free(flow);
2076 /* Check that the dependence analysis proceeds without errors.
2077 * Earlier versions of isl would break down during the analysis
2078 * due to the use of the wrong spaces.
2080 static int test_flow(isl_ctx *ctx)
2082 const char *str;
2083 isl_union_map *access, *schedule;
2084 isl_union_map *must_dep, *may_dep;
2085 int r;
2087 str = "{ S0[j] -> i[]; S1[j,i] -> i[]; S2[] -> i[]; S3[] -> i[] }";
2088 access = isl_union_map_read_from_str(ctx, str);
2089 str = "{ S0[j] -> [0,j,0,0] : 0 <= j < 10; "
2090 "S1[j,i] -> [0,j,1,i] : 0 <= j < i < 10; "
2091 "S2[] -> [1,0,0,0]; "
2092 "S3[] -> [-1,0,0,0] }";
2093 schedule = isl_union_map_read_from_str(ctx, str);
2094 r = isl_union_map_compute_flow(access, isl_union_map_copy(access),
2095 isl_union_map_copy(access), schedule,
2096 &must_dep, &may_dep, NULL, NULL);
2097 isl_union_map_free(may_dep);
2098 isl_union_map_free(must_dep);
2100 return r;
2103 struct {
2104 const char *map;
2105 int sv;
2106 } sv_tests[] = {
2107 { "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 9 }", 1 },
2108 { "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 10 }", 0 },
2109 { "{ [i] -> [3*floor(i/2) + 5*floor(i/3)] }", 1 },
2110 { "{ S1[i] -> [i] : 0 <= i <= 9; S2[i] -> [i] : 0 <= i <= 9 }", 1 },
2111 { "{ [i] -> S1[i] : 0 <= i <= 9; [i] -> S2[i] : 0 <= i <= 9 }", 0 },
2112 { "{ A[i] -> [i]; B[i] -> [i]; B[i] -> [i + 1] }", 0 },
2113 { "{ A[i] -> [i]; B[i] -> [i] : i < 0; B[i] -> [i + 1] : i > 0 }", 1 },
2114 { "{ A[i] -> [i]; B[i] -> A[i] : i < 0; B[i] -> [i + 1] : i > 0 }", 1 },
2115 { "{ A[i] -> [i]; B[i] -> [j] : i - 1 <= j <= i }", 0 },
2118 int test_sv(isl_ctx *ctx)
2120 isl_union_map *umap;
2121 int i;
2122 int sv;
2124 for (i = 0; i < ARRAY_SIZE(sv_tests); ++i) {
2125 umap = isl_union_map_read_from_str(ctx, sv_tests[i].map);
2126 sv = isl_union_map_is_single_valued(umap);
2127 isl_union_map_free(umap);
2128 if (sv < 0)
2129 return -1;
2130 if (sv_tests[i].sv && !sv)
2131 isl_die(ctx, isl_error_internal,
2132 "map not detected as single valued", return -1);
2133 if (!sv_tests[i].sv && sv)
2134 isl_die(ctx, isl_error_internal,
2135 "map detected as single valued", return -1);
2138 return 0;
2141 void test_bijective_case(struct isl_ctx *ctx, const char *str, int bijective)
2143 isl_map *map;
2145 map = isl_map_read_from_str(ctx, str);
2146 if (bijective)
2147 assert(isl_map_is_bijective(map));
2148 else
2149 assert(!isl_map_is_bijective(map));
2150 isl_map_free(map);
2153 void test_bijective(struct isl_ctx *ctx)
2155 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i]}", 0);
2156 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=i}", 1);
2157 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=0}", 1);
2158 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=N}", 1);
2159 test_bijective_case(ctx, "[N,M]->{[i,j] -> [j,i]}", 1);
2160 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i+j]}", 0);
2161 test_bijective_case(ctx, "[N,M]->{[i,j] -> []}", 0);
2162 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i,j,N]}", 1);
2163 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i]}", 0);
2164 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i,i]}", 0);
2165 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i,i]}", 0);
2166 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i,j]}", 1);
2167 test_bijective_case(ctx, "[N,M]->{[i,j] -> [x,y] : 2x=i & y =j}", 1);
2170 /* Inputs for isl_pw_qpolynomial_gist tests.
2171 * "pwqp" is the input, "set" is the context and "gist" is the expected result.
2173 struct {
2174 const char *pwqp;
2175 const char *set;
2176 const char *gist;
2177 } pwqp_gist_tests[] = {
2178 { "{ [i] -> i }", "{ [k] : exists a : k = 2a }", "{ [i] -> i }" },
2179 { "{ [i] -> i + [ (i + [i/3])/2 ] }", "{ [10] }", "{ [i] -> 16 }" },
2180 { "{ [i] -> ([(i)/2]) }", "{ [k] : exists a : k = 2a+1 }",
2181 "{ [i] -> -1/2 + 1/2 * i }" },
2182 { "{ [i] -> i^2 : i != 0 }", "{ [i] : i != 0 }", "{ [i] -> i^2 }" },
2185 static int test_pwqp(struct isl_ctx *ctx)
2187 int i;
2188 const char *str;
2189 isl_set *set;
2190 isl_pw_qpolynomial *pwqp1, *pwqp2;
2191 int equal;
2193 str = "{ [i,j,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
2194 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2196 pwqp1 = isl_pw_qpolynomial_move_dims(pwqp1, isl_dim_param, 0,
2197 isl_dim_in, 1, 1);
2199 str = "[j] -> { [i,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
2200 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2202 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2204 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2206 isl_pw_qpolynomial_free(pwqp1);
2208 for (i = 0; i < ARRAY_SIZE(pwqp_gist_tests); ++i) {
2209 str = pwqp_gist_tests[i].pwqp;
2210 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2211 str = pwqp_gist_tests[i].set;
2212 set = isl_set_read_from_str(ctx, str);
2213 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
2214 str = pwqp_gist_tests[i].gist;
2215 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2216 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2217 equal = isl_pw_qpolynomial_is_zero(pwqp1);
2218 isl_pw_qpolynomial_free(pwqp1);
2220 if (equal < 0)
2221 return -1;
2222 if (!equal)
2223 isl_die(ctx, isl_error_unknown,
2224 "unexpected result", return -1);
2227 str = "{ [i] -> ([([i/2] + [i/2])/5]) }";
2228 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2229 str = "{ [i] -> ([(2 * [i/2])/5]) }";
2230 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2232 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2234 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2236 isl_pw_qpolynomial_free(pwqp1);
2238 str = "{ [x] -> ([x/2] + [(x+1)/2]) }";
2239 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2240 str = "{ [x] -> x }";
2241 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2243 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2245 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2247 isl_pw_qpolynomial_free(pwqp1);
2249 str = "{ [i] -> ([i/2]) : i >= 0; [i] -> ([i/3]) : i < 0 }";
2250 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2251 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2252 pwqp1 = isl_pw_qpolynomial_coalesce(pwqp1);
2253 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
2254 assert(isl_pw_qpolynomial_is_zero(pwqp1));
2255 isl_pw_qpolynomial_free(pwqp1);
2257 str = "{ [a,b,a] -> (([(2*[a/3]+b)/5]) * ([(2*[a/3]+b)/5])) }";
2258 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2259 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
2260 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2261 set = isl_set_read_from_str(ctx, "{ [a,b,a] }");
2262 pwqp1 = isl_pw_qpolynomial_intersect_domain(pwqp1, set);
2263 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
2264 isl_pw_qpolynomial_free(pwqp1);
2265 isl_pw_qpolynomial_free(pwqp2);
2266 if (equal < 0)
2267 return -1;
2268 if (!equal)
2269 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2271 str = "{ [a,b,c] -> (([(2*[a/3]+1)/5]) * ([(2*[c/3]+1)/5])) : b = 1 }";
2272 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
2273 str = "{ [a,b,c] -> (([(2*[a/3]+b)/5]) * ([(2*[c/3]+b)/5])) }";
2274 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
2275 pwqp1 = isl_pw_qpolynomial_fix_val(pwqp1, isl_dim_set, 1,
2276 isl_val_one(ctx));
2277 equal = isl_pw_qpolynomial_plain_is_equal(pwqp1, pwqp2);
2278 isl_pw_qpolynomial_free(pwqp1);
2279 isl_pw_qpolynomial_free(pwqp2);
2280 if (equal < 0)
2281 return -1;
2282 if (!equal)
2283 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
2285 return 0;
2288 void test_split_periods(isl_ctx *ctx)
2290 const char *str;
2291 isl_pw_qpolynomial *pwqp;
2293 str = "{ [U,V] -> 1/3 * U + 2/3 * V - [(U + 2V)/3] + [U/2] : "
2294 "U + 2V + 3 >= 0 and - U -2V >= 0 and - U + 10 >= 0 and "
2295 "U >= 0; [U,V] -> U^2 : U >= 100 }";
2296 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2298 pwqp = isl_pw_qpolynomial_split_periods(pwqp, 2);
2299 assert(pwqp);
2301 isl_pw_qpolynomial_free(pwqp);
2304 void test_union(isl_ctx *ctx)
2306 const char *str;
2307 isl_union_set *uset1, *uset2;
2308 isl_union_map *umap1, *umap2;
2310 str = "{ [i] : 0 <= i <= 1 }";
2311 uset1 = isl_union_set_read_from_str(ctx, str);
2312 str = "{ [1] -> [0] }";
2313 umap1 = isl_union_map_read_from_str(ctx, str);
2315 umap2 = isl_union_set_lex_gt_union_set(isl_union_set_copy(uset1), uset1);
2316 assert(isl_union_map_is_equal(umap1, umap2));
2318 isl_union_map_free(umap1);
2319 isl_union_map_free(umap2);
2321 str = "{ A[i] -> B[i]; B[i] -> C[i]; A[0] -> C[1] }";
2322 umap1 = isl_union_map_read_from_str(ctx, str);
2323 str = "{ A[i]; B[i] }";
2324 uset1 = isl_union_set_read_from_str(ctx, str);
2326 uset2 = isl_union_map_domain(umap1);
2328 assert(isl_union_set_is_equal(uset1, uset2));
2330 isl_union_set_free(uset1);
2331 isl_union_set_free(uset2);
2334 void test_bound(isl_ctx *ctx)
2336 const char *str;
2337 isl_pw_qpolynomial *pwqp;
2338 isl_pw_qpolynomial_fold *pwf;
2340 str = "{ [[a, b, c, d] -> [e]] -> 0 }";
2341 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2342 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
2343 assert(isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in) == 4);
2344 isl_pw_qpolynomial_fold_free(pwf);
2346 str = "{ [[x]->[x]] -> 1 : exists a : x = 2 a }";
2347 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
2348 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
2349 assert(isl_pw_qpolynomial_fold_dim(pwf, isl_dim_in) == 1);
2350 isl_pw_qpolynomial_fold_free(pwf);
2353 void test_lift(isl_ctx *ctx)
2355 const char *str;
2356 isl_basic_map *bmap;
2357 isl_basic_set *bset;
2359 str = "{ [i0] : exists e0 : i0 = 4e0 }";
2360 bset = isl_basic_set_read_from_str(ctx, str);
2361 bset = isl_basic_set_lift(bset);
2362 bmap = isl_basic_map_from_range(bset);
2363 bset = isl_basic_map_domain(bmap);
2364 isl_basic_set_free(bset);
2367 struct {
2368 const char *set1;
2369 const char *set2;
2370 int subset;
2371 } subset_tests[] = {
2372 { "{ [112, 0] }",
2373 "{ [i0, i1] : exists (e0 = [(i0 - i1)/16], e1: "
2374 "16e0 <= i0 - i1 and 16e0 >= -15 + i0 - i1 and "
2375 "16e1 <= i1 and 16e0 >= -i1 and 16e1 >= -i0 + i1) }", 1 },
2376 { "{ [65] }",
2377 "{ [i] : exists (e0 = [(255i)/256], e1 = [(127i + 65e0)/191], "
2378 "e2 = [(3i + 61e1)/65], e3 = [(52i + 12e2)/61], "
2379 "e4 = [(2i + e3)/3], e5 = [(4i + e3)/4], e6 = [(8i + e3)/12]: "
2380 "3e4 = 2i + e3 and 4e5 = 4i + e3 and 12e6 = 8i + e3 and "
2381 "i <= 255 and 64e3 >= -45 + 67i and i >= 0 and "
2382 "256e0 <= 255i and 256e0 >= -255 + 255i and "
2383 "191e1 <= 127i + 65e0 and 191e1 >= -190 + 127i + 65e0 and "
2384 "65e2 <= 3i + 61e1 and 65e2 >= -64 + 3i + 61e1 and "
2385 "61e3 <= 52i + 12e2 and 61e3 >= -60 + 52i + 12e2) }", 1 },
2386 { "{ [i] : 0 <= i <= 10 }", "{ rat: [i] : 0 <= i <= 10 }", 1 },
2387 { "{ rat: [i] : 0 <= i <= 10 }", "{ [i] : 0 <= i <= 10 }", 0 },
2388 { "{ rat: [0] }", "{ [i] : 0 <= i <= 10 }", 1 },
2389 { "{ rat: [(1)/2] }", "{ [i] : 0 <= i <= 10 }", 0 },
2390 { "{ [t, i] : (exists (e0 = [(2 + t)/4]: 4e0 <= 2 + t and "
2391 "4e0 >= -1 + t and i >= 57 and i <= 62 and "
2392 "4e0 <= 62 + t - i and 4e0 >= -61 + t + i and "
2393 "t >= 0 and t <= 511 and 4e0 <= -57 + t + i and "
2394 "4e0 >= 58 + t - i and i >= 58 + t and i >= 62 - t)) }",
2395 "{ [i0, i1] : (exists (e0 = [(4 + i0)/4]: 4e0 <= 62 + i0 - i1 and "
2396 "4e0 >= 1 + i0 and i0 >= 0 and i0 <= 511 and "
2397 "4e0 <= -57 + i0 + i1)) or "
2398 "(exists (e0 = [(2 + i0)/4]: 4e0 <= i0 and "
2399 "4e0 >= 58 + i0 - i1 and i0 >= 2 and i0 <= 511 and "
2400 "4e0 >= -61 + i0 + i1)) or "
2401 "(i1 <= 66 - i0 and i0 >= 2 and i1 >= 59 + i0) }", 1 },
2404 static int test_subset(isl_ctx *ctx)
2406 int i;
2407 isl_set *set1, *set2;
2408 int subset;
2410 for (i = 0; i < ARRAY_SIZE(subset_tests); ++i) {
2411 set1 = isl_set_read_from_str(ctx, subset_tests[i].set1);
2412 set2 = isl_set_read_from_str(ctx, subset_tests[i].set2);
2413 subset = isl_set_is_subset(set1, set2);
2414 isl_set_free(set1);
2415 isl_set_free(set2);
2416 if (subset < 0)
2417 return -1;
2418 if (subset != subset_tests[i].subset)
2419 isl_die(ctx, isl_error_unknown,
2420 "incorrect subset result", return -1);
2423 return 0;
2426 struct {
2427 const char *minuend;
2428 const char *subtrahend;
2429 const char *difference;
2430 } subtract_domain_tests[] = {
2431 { "{ A[i] -> B[i] }", "{ A[i] }", "{ }" },
2432 { "{ A[i] -> B[i] }", "{ B[i] }", "{ A[i] -> B[i] }" },
2433 { "{ A[i] -> B[i] }", "{ A[i] : i > 0 }", "{ A[i] -> B[i] : i <= 0 }" },
2436 static int test_subtract(isl_ctx *ctx)
2438 int i;
2439 isl_union_map *umap1, *umap2;
2440 isl_union_set *uset;
2441 int equal;
2443 for (i = 0; i < ARRAY_SIZE(subtract_domain_tests); ++i) {
2444 umap1 = isl_union_map_read_from_str(ctx,
2445 subtract_domain_tests[i].minuend);
2446 uset = isl_union_set_read_from_str(ctx,
2447 subtract_domain_tests[i].subtrahend);
2448 umap2 = isl_union_map_read_from_str(ctx,
2449 subtract_domain_tests[i].difference);
2450 umap1 = isl_union_map_subtract_domain(umap1, uset);
2451 equal = isl_union_map_is_equal(umap1, umap2);
2452 isl_union_map_free(umap1);
2453 isl_union_map_free(umap2);
2454 if (equal < 0)
2455 return -1;
2456 if (!equal)
2457 isl_die(ctx, isl_error_unknown,
2458 "incorrect subtract domain result", return -1);
2461 return 0;
2464 int test_factorize(isl_ctx *ctx)
2466 const char *str;
2467 isl_basic_set *bset;
2468 isl_factorizer *f;
2470 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7] : 3i5 <= 2 - 2i0 and "
2471 "i0 >= -2 and i6 >= 1 + i3 and i7 >= 0 and 3i5 >= -2i0 and "
2472 "2i4 <= i2 and i6 >= 1 + 2i0 + 3i1 and i4 <= -1 and "
2473 "i6 >= 1 + 2i0 + 3i5 and i6 <= 2 + 2i0 + 3i5 and "
2474 "3i5 <= 2 - 2i0 - i2 + 3i4 and i6 <= 2 + 2i0 + 3i1 and "
2475 "i0 <= -1 and i7 <= i2 + i3 - 3i4 - i6 and "
2476 "3i5 >= -2i0 - i2 + 3i4 }";
2477 bset = isl_basic_set_read_from_str(ctx, str);
2478 f = isl_basic_set_factorizer(bset);
2479 isl_basic_set_free(bset);
2480 isl_factorizer_free(f);
2481 if (!f)
2482 isl_die(ctx, isl_error_unknown,
2483 "failed to construct factorizer", return -1);
2485 str = "{ [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12] : "
2486 "i12 <= 2 + i0 - i11 and 2i8 >= -i4 and i11 >= i1 and "
2487 "3i5 <= -i2 and 2i11 >= -i4 - 2i7 and i11 <= 3 + i0 + 3i9 and "
2488 "i11 <= -i4 - 2i7 and i12 >= -i10 and i2 >= -2 and "
2489 "i11 >= i1 + 3i10 and i11 >= 1 + i0 + 3i9 and "
2490 "i11 <= 1 - i4 - 2i8 and 6i6 <= 6 - i2 and 3i6 >= 1 - i2 and "
2491 "i11 <= 2 + i1 and i12 <= i4 + i11 and i12 >= i0 - i11 and "
2492 "3i5 >= -2 - i2 and i12 >= -1 + i4 + i11 and 3i3 <= 3 - i2 and "
2493 "9i6 <= 11 - i2 + 6i5 and 3i3 >= 1 - i2 and "
2494 "9i6 <= 5 - i2 + 6i3 and i12 <= -1 and i2 <= 0 }";
2495 bset = isl_basic_set_read_from_str(ctx, str);
2496 f = isl_basic_set_factorizer(bset);
2497 isl_basic_set_free(bset);
2498 isl_factorizer_free(f);
2499 if (!f)
2500 isl_die(ctx, isl_error_unknown,
2501 "failed to construct factorizer", return -1);
2503 return 0;
2506 static int check_injective(__isl_take isl_map *map, void *user)
2508 int *injective = user;
2510 *injective = isl_map_is_injective(map);
2511 isl_map_free(map);
2513 if (*injective < 0 || !*injective)
2514 return -1;
2516 return 0;
2519 int test_one_schedule(isl_ctx *ctx, const char *d, const char *w,
2520 const char *r, const char *s, int tilable, int parallel)
2522 int i;
2523 isl_union_set *D;
2524 isl_union_map *W, *R, *S;
2525 isl_union_map *empty;
2526 isl_union_map *dep_raw, *dep_war, *dep_waw, *dep;
2527 isl_union_map *validity, *proximity, *coincidence;
2528 isl_union_map *schedule;
2529 isl_union_map *test;
2530 isl_union_set *delta;
2531 isl_union_set *domain;
2532 isl_set *delta_set;
2533 isl_set *slice;
2534 isl_set *origin;
2535 isl_schedule_constraints *sc;
2536 isl_schedule *sched;
2537 int is_nonneg, is_parallel, is_tilable, is_injection, is_complete;
2539 D = isl_union_set_read_from_str(ctx, d);
2540 W = isl_union_map_read_from_str(ctx, w);
2541 R = isl_union_map_read_from_str(ctx, r);
2542 S = isl_union_map_read_from_str(ctx, s);
2544 W = isl_union_map_intersect_domain(W, isl_union_set_copy(D));
2545 R = isl_union_map_intersect_domain(R, isl_union_set_copy(D));
2547 empty = isl_union_map_empty(isl_union_map_get_space(S));
2548 isl_union_map_compute_flow(isl_union_map_copy(R),
2549 isl_union_map_copy(W), empty,
2550 isl_union_map_copy(S),
2551 &dep_raw, NULL, NULL, NULL);
2552 isl_union_map_compute_flow(isl_union_map_copy(W),
2553 isl_union_map_copy(W),
2554 isl_union_map_copy(R),
2555 isl_union_map_copy(S),
2556 &dep_waw, &dep_war, NULL, NULL);
2558 dep = isl_union_map_union(dep_waw, dep_war);
2559 dep = isl_union_map_union(dep, dep_raw);
2560 validity = isl_union_map_copy(dep);
2561 coincidence = isl_union_map_copy(dep);
2562 proximity = isl_union_map_copy(dep);
2564 sc = isl_schedule_constraints_on_domain(isl_union_set_copy(D));
2565 sc = isl_schedule_constraints_set_validity(sc, validity);
2566 sc = isl_schedule_constraints_set_coincidence(sc, coincidence);
2567 sc = isl_schedule_constraints_set_proximity(sc, proximity);
2568 sched = isl_schedule_constraints_compute_schedule(sc);
2569 schedule = isl_schedule_get_map(sched);
2570 isl_schedule_free(sched);
2571 isl_union_map_free(W);
2572 isl_union_map_free(R);
2573 isl_union_map_free(S);
2575 is_injection = 1;
2576 isl_union_map_foreach_map(schedule, &check_injective, &is_injection);
2578 domain = isl_union_map_domain(isl_union_map_copy(schedule));
2579 is_complete = isl_union_set_is_subset(D, domain);
2580 isl_union_set_free(D);
2581 isl_union_set_free(domain);
2583 test = isl_union_map_reverse(isl_union_map_copy(schedule));
2584 test = isl_union_map_apply_range(test, dep);
2585 test = isl_union_map_apply_range(test, schedule);
2587 delta = isl_union_map_deltas(test);
2588 if (isl_union_set_n_set(delta) == 0) {
2589 is_tilable = 1;
2590 is_parallel = 1;
2591 is_nonneg = 1;
2592 isl_union_set_free(delta);
2593 } else {
2594 delta_set = isl_set_from_union_set(delta);
2596 slice = isl_set_universe(isl_set_get_space(delta_set));
2597 for (i = 0; i < tilable; ++i)
2598 slice = isl_set_lower_bound_si(slice, isl_dim_set, i, 0);
2599 is_tilable = isl_set_is_subset(delta_set, slice);
2600 isl_set_free(slice);
2602 slice = isl_set_universe(isl_set_get_space(delta_set));
2603 for (i = 0; i < parallel; ++i)
2604 slice = isl_set_fix_si(slice, isl_dim_set, i, 0);
2605 is_parallel = isl_set_is_subset(delta_set, slice);
2606 isl_set_free(slice);
2608 origin = isl_set_universe(isl_set_get_space(delta_set));
2609 for (i = 0; i < isl_set_dim(origin, isl_dim_set); ++i)
2610 origin = isl_set_fix_si(origin, isl_dim_set, i, 0);
2612 delta_set = isl_set_union(delta_set, isl_set_copy(origin));
2613 delta_set = isl_set_lexmin(delta_set);
2615 is_nonneg = isl_set_is_equal(delta_set, origin);
2617 isl_set_free(origin);
2618 isl_set_free(delta_set);
2621 if (is_nonneg < 0 || is_parallel < 0 || is_tilable < 0 ||
2622 is_injection < 0 || is_complete < 0)
2623 return -1;
2624 if (!is_complete)
2625 isl_die(ctx, isl_error_unknown,
2626 "generated schedule incomplete", return -1);
2627 if (!is_injection)
2628 isl_die(ctx, isl_error_unknown,
2629 "generated schedule not injective on each statement",
2630 return -1);
2631 if (!is_nonneg)
2632 isl_die(ctx, isl_error_unknown,
2633 "negative dependences in generated schedule",
2634 return -1);
2635 if (!is_tilable)
2636 isl_die(ctx, isl_error_unknown,
2637 "generated schedule not as tilable as expected",
2638 return -1);
2639 if (!is_parallel)
2640 isl_die(ctx, isl_error_unknown,
2641 "generated schedule not as parallel as expected",
2642 return -1);
2644 return 0;
2647 static __isl_give isl_union_map *compute_schedule(isl_ctx *ctx,
2648 const char *domain, const char *validity, const char *proximity)
2650 isl_union_set *dom;
2651 isl_union_map *dep;
2652 isl_union_map *prox;
2653 isl_schedule_constraints *sc;
2654 isl_schedule *schedule;
2655 isl_union_map *sched;
2657 dom = isl_union_set_read_from_str(ctx, domain);
2658 dep = isl_union_map_read_from_str(ctx, validity);
2659 prox = isl_union_map_read_from_str(ctx, proximity);
2660 sc = isl_schedule_constraints_on_domain(dom);
2661 sc = isl_schedule_constraints_set_validity(sc, dep);
2662 sc = isl_schedule_constraints_set_proximity(sc, prox);
2663 schedule = isl_schedule_constraints_compute_schedule(sc);
2664 sched = isl_schedule_get_map(schedule);
2665 isl_schedule_free(schedule);
2667 return sched;
2670 /* Check that a schedule can be constructed on the given domain
2671 * with the given validity and proximity constraints.
2673 static int test_has_schedule(isl_ctx *ctx, const char *domain,
2674 const char *validity, const char *proximity)
2676 isl_union_map *sched;
2678 sched = compute_schedule(ctx, domain, validity, proximity);
2679 if (!sched)
2680 return -1;
2682 isl_union_map_free(sched);
2683 return 0;
2686 int test_special_schedule(isl_ctx *ctx, const char *domain,
2687 const char *validity, const char *proximity, const char *expected_sched)
2689 isl_union_map *sched1, *sched2;
2690 int equal;
2692 sched1 = compute_schedule(ctx, domain, validity, proximity);
2693 sched2 = isl_union_map_read_from_str(ctx, expected_sched);
2695 equal = isl_union_map_is_equal(sched1, sched2);
2696 isl_union_map_free(sched1);
2697 isl_union_map_free(sched2);
2699 if (equal < 0)
2700 return -1;
2701 if (!equal)
2702 isl_die(ctx, isl_error_unknown, "unexpected schedule",
2703 return -1);
2705 return 0;
2708 /* Check that the schedule map is properly padded, even after being
2709 * reconstructed from the band forest.
2711 static int test_padded_schedule(isl_ctx *ctx)
2713 const char *str;
2714 isl_union_set *D;
2715 isl_union_map *validity, *proximity;
2716 isl_schedule_constraints *sc;
2717 isl_schedule *sched;
2718 isl_union_map *map1, *map2;
2719 isl_band_list *list;
2720 int equal;
2722 str = "[N] -> { S0[i] : 0 <= i <= N; S1[i, j] : 0 <= i, j <= N }";
2723 D = isl_union_set_read_from_str(ctx, str);
2724 validity = isl_union_map_empty(isl_union_set_get_space(D));
2725 proximity = isl_union_map_copy(validity);
2726 sc = isl_schedule_constraints_on_domain(D);
2727 sc = isl_schedule_constraints_set_validity(sc, validity);
2728 sc = isl_schedule_constraints_set_proximity(sc, proximity);
2729 sched = isl_schedule_constraints_compute_schedule(sc);
2730 map1 = isl_schedule_get_map(sched);
2731 list = isl_schedule_get_band_forest(sched);
2732 isl_band_list_free(list);
2733 map2 = isl_schedule_get_map(sched);
2734 isl_schedule_free(sched);
2735 equal = isl_union_map_is_equal(map1, map2);
2736 isl_union_map_free(map1);
2737 isl_union_map_free(map2);
2739 if (equal < 0)
2740 return -1;
2741 if (!equal)
2742 isl_die(ctx, isl_error_unknown,
2743 "reconstructed schedule map not the same as original",
2744 return -1);
2746 return 0;
2749 /* Input for testing of schedule construction based on
2750 * conditional constraints.
2752 * domain is the iteration domain
2753 * flow are the flow dependences, which determine the validity and
2754 * proximity constraints
2755 * condition are the conditions on the conditional validity constraints
2756 * conditional_validity are the conditional validity constraints
2757 * outer_band_n is the expected number of members in the outer band
2759 struct {
2760 const char *domain;
2761 const char *flow;
2762 const char *condition;
2763 const char *conditional_validity;
2764 int outer_band_n;
2765 } live_range_tests[] = {
2766 /* Contrived example that illustrates that we need to keep
2767 * track of tagged condition dependences and
2768 * tagged conditional validity dependences
2769 * in isl_sched_edge separately.
2770 * In particular, the conditional validity constraints on A
2771 * cannot be satisfied,
2772 * but they can be ignored because there are no corresponding
2773 * condition constraints. However, we do have an additional
2774 * conditional validity constraint that maps to the same
2775 * dependence relation
2776 * as the condition constraint on B. If we did not make a distinction
2777 * between tagged condition and tagged conditional validity
2778 * dependences, then we
2779 * could end up treating this shared dependence as an condition
2780 * constraint on A, forcing a localization of the conditions,
2781 * which is impossible.
2783 { "{ S[i] : 0 <= 1 < 100; T[i] : 0 <= 1 < 100 }",
2784 "{ S[i] -> S[i+1] : 0 <= i < 99 }",
2785 "{ [S[i] -> B[]] -> [S[i+1] -> B[]] : 0 <= i < 99 }",
2786 "{ [S[i] -> A[]] -> [T[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
2787 "[T[i] -> A[]] -> [S[i'] -> A[]] : 0 <= i', i < 100 and i != i';"
2788 "[S[i] -> A[]] -> [S[i+1] -> A[]] : 0 <= i < 99 }",
2791 /* TACO 2013 Fig. 7 */
2792 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
2793 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
2794 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
2795 "[n] -> { [S1[i,j] -> t[]] -> [S2[i,j] -> t[]] : 0 <= i,j < n;"
2796 "[S2[i,j] -> x1[]] -> [S2[i,j+1] -> x1[]] : "
2797 "0 <= i < n and 0 <= j < n - 1 }",
2798 "[n] -> { [S2[i,j] -> t[]] -> [S1[i,j'] -> t[]] : "
2799 "0 <= i < n and 0 <= j < j' < n;"
2800 "[S2[i,j] -> t[]] -> [S1[i',j'] -> t[]] : "
2801 "0 <= i < i' < n and 0 <= j,j' < n;"
2802 "[S2[i,j] -> x1[]] -> [S2[i,j'] -> x1[]] : "
2803 "0 <= i,j,j' < n and j < j' }",
2806 /* TACO 2013 Fig. 7, without tags */
2807 { "[n] -> { S1[i,j] : 0 <= i,j < n; S2[i,j] : 0 <= i,j < n }",
2808 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
2809 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
2810 "[n] -> { S1[i,j] -> S2[i,j] : 0 <= i,j < n;"
2811 "S2[i,j] -> S2[i,j+1] : 0 <= i < n and 0 <= j < n - 1 }",
2812 "[n] -> { S2[i,j] -> S1[i,j'] : 0 <= i < n and 0 <= j < j' < n;"
2813 "S2[i,j] -> S1[i',j'] : 0 <= i < i' < n and 0 <= j,j' < n;"
2814 "S2[i,j] -> S2[i,j'] : 0 <= i,j,j' < n and j < j' }",
2817 /* TACO 2013 Fig. 12 */
2818 { "{ S1[i,0] : 0 <= i <= 1; S2[i,j] : 0 <= i <= 1 and 1 <= j <= 2;"
2819 "S3[i,3] : 0 <= i <= 1 }",
2820 "{ S1[i,0] -> S2[i,1] : 0 <= i <= 1;"
2821 "S2[i,1] -> S2[i,2] : 0 <= i <= 1;"
2822 "S2[i,2] -> S3[i,3] : 0 <= i <= 1 }",
2823 "{ [S1[i,0]->t[]] -> [S2[i,1]->t[]] : 0 <= i <= 1;"
2824 "[S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
2825 "[S2[i,2]->t[]] -> [S3[i,3]->t[]] : 0 <= i <= 1 }",
2826 "{ [S2[i,1]->t[]] -> [S2[i,2]->t[]] : 0 <= i <= 1;"
2827 "[S2[0,j]->t[]] -> [S2[1,j']->t[]] : 1 <= j,j' <= 2;"
2828 "[S2[0,j]->t[]] -> [S1[1,0]->t[]] : 1 <= j <= 2;"
2829 "[S3[0,3]->t[]] -> [S2[1,j]->t[]] : 1 <= j <= 2;"
2830 "[S3[0,3]->t[]] -> [S1[1,0]->t[]] }",
2835 /* Test schedule construction based on conditional constraints.
2836 * In particular, check the number of members in the outer band
2837 * as an indication of whether tiling is possible or not.
2839 static int test_conditional_schedule_constraints(isl_ctx *ctx)
2841 int i;
2842 isl_union_set *domain;
2843 isl_union_map *condition;
2844 isl_union_map *flow;
2845 isl_union_map *validity;
2846 isl_schedule_constraints *sc;
2847 isl_schedule *schedule;
2848 isl_band_list *list;
2849 isl_band *band;
2850 int n_member;
2852 for (i = 0; i < ARRAY_SIZE(live_range_tests); ++i) {
2853 domain = isl_union_set_read_from_str(ctx,
2854 live_range_tests[i].domain);
2855 flow = isl_union_map_read_from_str(ctx,
2856 live_range_tests[i].flow);
2857 condition = isl_union_map_read_from_str(ctx,
2858 live_range_tests[i].condition);
2859 validity = isl_union_map_read_from_str(ctx,
2860 live_range_tests[i].conditional_validity);
2861 sc = isl_schedule_constraints_on_domain(domain);
2862 sc = isl_schedule_constraints_set_validity(sc,
2863 isl_union_map_copy(flow));
2864 sc = isl_schedule_constraints_set_proximity(sc, flow);
2865 sc = isl_schedule_constraints_set_conditional_validity(sc,
2866 condition, validity);
2867 schedule = isl_schedule_constraints_compute_schedule(sc);
2868 list = isl_schedule_get_band_forest(schedule);
2869 band = isl_band_list_get_band(list, 0);
2870 n_member = isl_band_n_member(band);
2871 isl_band_free(band);
2872 isl_band_list_free(list);
2873 isl_schedule_free(schedule);
2875 if (!schedule)
2876 return -1;
2877 if (n_member != live_range_tests[i].outer_band_n)
2878 isl_die(ctx, isl_error_unknown,
2879 "unexpected number of members in outer band",
2880 return -1);
2882 return 0;
2885 int test_schedule(isl_ctx *ctx)
2887 const char *D, *W, *R, *V, *P, *S;
2889 /* Handle resulting schedule with zero bands. */
2890 if (test_one_schedule(ctx, "{[]}", "{}", "{}", "{[] -> []}", 0, 0) < 0)
2891 return -1;
2893 /* Jacobi */
2894 D = "[T,N] -> { S1[t,i] : 1 <= t <= T and 2 <= i <= N - 1 }";
2895 W = "{ S1[t,i] -> a[t,i] }";
2896 R = "{ S1[t,i] -> a[t-1,i]; S1[t,i] -> a[t-1,i-1]; "
2897 "S1[t,i] -> a[t-1,i+1] }";
2898 S = "{ S1[t,i] -> [t,i] }";
2899 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
2900 return -1;
2902 /* Fig. 5 of CC2008 */
2903 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and j >= 2 and "
2904 "j <= -1 + N }";
2905 W = "[N] -> { S_0[i, j] -> a[i, j] : i >= 0 and i <= -1 + N and "
2906 "j >= 2 and j <= -1 + N }";
2907 R = "[N] -> { S_0[i, j] -> a[j, i] : i >= 0 and i <= -1 + N and "
2908 "j >= 2 and j <= -1 + N; "
2909 "S_0[i, j] -> a[i, -1 + j] : i >= 0 and i <= -1 + N and "
2910 "j >= 2 and j <= -1 + N }";
2911 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
2912 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
2913 return -1;
2915 D = "{ S1[i] : 0 <= i <= 10; S2[i] : 0 <= i <= 9 }";
2916 W = "{ S1[i] -> a[i] }";
2917 R = "{ S2[i] -> a[i+1] }";
2918 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2919 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
2920 return -1;
2922 D = "{ S1[i] : 0 <= i < 10; S2[i] : 0 <= i < 10 }";
2923 W = "{ S1[i] -> a[i] }";
2924 R = "{ S2[i] -> a[9-i] }";
2925 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2926 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
2927 return -1;
2929 D = "[N] -> { S1[i] : 0 <= i < N; S2[i] : 0 <= i < N }";
2930 W = "{ S1[i] -> a[i] }";
2931 R = "[N] -> { S2[i] -> a[N-1-i] }";
2932 S = "{ S1[i] -> [0,i]; S2[i] -> [1,i] }";
2933 if (test_one_schedule(ctx, D, W, R, S, 1, 1) < 0)
2934 return -1;
2936 D = "{ S1[i] : 0 < i < 10; S2[i] : 0 <= i < 10 }";
2937 W = "{ S1[i] -> a[i]; S2[i] -> b[i] }";
2938 R = "{ S2[i] -> a[i]; S1[i] -> b[i-1] }";
2939 S = "{ S1[i] -> [i,0]; S2[i] -> [i,1] }";
2940 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2941 return -1;
2943 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
2944 W = "{ S1[i] -> a[0,i]; S2[i,j] -> a[i,j] }";
2945 R = "{ S2[i,j] -> a[i-1,j] }";
2946 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
2947 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
2948 return -1;
2950 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i,j] : 1 <= i,j <= N }";
2951 W = "{ S1[i] -> a[i,0]; S2[i,j] -> a[i,j] }";
2952 R = "{ S2[i,j] -> a[i,j-1] }";
2953 S = "{ S1[i] -> [0,i,0]; S2[i,j] -> [1,i,j] }";
2954 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
2955 return -1;
2957 D = "[N] -> { S_0[]; S_1[i] : i >= 0 and i <= -1 + N; S_2[] }";
2958 W = "[N] -> { S_0[] -> a[0]; S_2[] -> b[0]; "
2959 "S_1[i] -> a[1 + i] : i >= 0 and i <= -1 + N }";
2960 R = "[N] -> { S_2[] -> a[N]; S_1[i] -> a[i] : i >= 0 and i <= -1 + N }";
2961 S = "[N] -> { S_1[i] -> [1, i, 0]; S_2[] -> [2, 0, 1]; "
2962 "S_0[] -> [0, 0, 0] }";
2963 if (test_one_schedule(ctx, D, W, R, S, 1, 0) < 0)
2964 return -1;
2965 ctx->opt->schedule_parametric = 0;
2966 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2967 return -1;
2968 ctx->opt->schedule_parametric = 1;
2970 D = "[N] -> { S1[i] : 1 <= i <= N; S2[i] : 1 <= i <= N; "
2971 "S3[i,j] : 1 <= i,j <= N; S4[i] : 1 <= i <= N }";
2972 W = "{ S1[i] -> a[i,0]; S2[i] -> a[0,i]; S3[i,j] -> a[i,j] }";
2973 R = "[N] -> { S3[i,j] -> a[i-1,j]; S3[i,j] -> a[i,j-1]; "
2974 "S4[i] -> a[i,N] }";
2975 S = "{ S1[i] -> [0,i,0]; S2[i] -> [1,i,0]; S3[i,j] -> [2,i,j]; "
2976 "S4[i] -> [4,i,0] }";
2977 if (test_one_schedule(ctx, D, W, R, S, 2, 0) < 0)
2978 return -1;
2980 D = "[N] -> { S_0[i, j] : i >= 1 and i <= N and j >= 1 and j <= N }";
2981 W = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
2982 "j <= N }";
2983 R = "[N] -> { S_0[i, j] -> s[0] : i >= 1 and i <= N and j >= 1 and "
2984 "j <= N; "
2985 "S_0[i, j] -> a[i, j] : i >= 1 and i <= N and j >= 1 and "
2986 "j <= N }";
2987 S = "[N] -> { S_0[i, j] -> [0, i, 0, j, 0] }";
2988 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
2989 return -1;
2991 D = "[N] -> { S_0[t] : t >= 0 and t <= -1 + N; "
2992 " S_2[t] : t >= 0 and t <= -1 + N; "
2993 " S_1[t, i] : t >= 0 and t <= -1 + N and i >= 0 and "
2994 "i <= -1 + N }";
2995 W = "[N] -> { S_0[t] -> a[t, 0] : t >= 0 and t <= -1 + N; "
2996 " S_2[t] -> b[t] : t >= 0 and t <= -1 + N; "
2997 " S_1[t, i] -> a[t, 1 + i] : t >= 0 and t <= -1 + N and "
2998 "i >= 0 and i <= -1 + N }";
2999 R = "[N] -> { S_1[t, i] -> a[t, i] : t >= 0 and t <= -1 + N and "
3000 "i >= 0 and i <= -1 + N; "
3001 " S_2[t] -> a[t, N] : t >= 0 and t <= -1 + N }";
3002 S = "[N] -> { S_2[t] -> [0, t, 2]; S_1[t, i] -> [0, t, 1, i, 0]; "
3003 " S_0[t] -> [0, t, 0] }";
3005 if (test_one_schedule(ctx, D, W, R, S, 2, 1) < 0)
3006 return -1;
3007 ctx->opt->schedule_parametric = 0;
3008 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3009 return -1;
3010 ctx->opt->schedule_parametric = 1;
3012 D = "[N] -> { S1[i,j] : 0 <= i,j < N; S2[i,j] : 0 <= i,j < N }";
3013 S = "{ S1[i,j] -> [0,i,j]; S2[i,j] -> [1,i,j] }";
3014 if (test_one_schedule(ctx, D, "{}", "{}", S, 2, 2) < 0)
3015 return -1;
3017 D = "[M, N] -> { S_1[i] : i >= 0 and i <= -1 + M; "
3018 "S_0[i, j] : i >= 0 and i <= -1 + M and j >= 0 and j <= -1 + N }";
3019 W = "[M, N] -> { S_0[i, j] -> a[j] : i >= 0 and i <= -1 + M and "
3020 "j >= 0 and j <= -1 + N; "
3021 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
3022 R = "[M, N] -> { S_0[i, j] -> a[0] : i >= 0 and i <= -1 + M and "
3023 "j >= 0 and j <= -1 + N; "
3024 "S_1[i] -> b[0] : i >= 0 and i <= -1 + M }";
3025 S = "[M, N] -> { S_1[i] -> [1, i, 0]; S_0[i, j] -> [0, i, 0, j, 0] }";
3026 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3027 return -1;
3029 D = "{ S_0[i] : i >= 0 }";
3030 W = "{ S_0[i] -> a[i] : i >= 0 }";
3031 R = "{ S_0[i] -> a[0] : i >= 0 }";
3032 S = "{ S_0[i] -> [0, i, 0] }";
3033 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3034 return -1;
3036 D = "{ S_0[i] : i >= 0; S_1[i] : i >= 0 }";
3037 W = "{ S_0[i] -> a[i] : i >= 0; S_1[i] -> b[i] : i >= 0 }";
3038 R = "{ S_0[i] -> b[0] : i >= 0; S_1[i] -> a[i] : i >= 0 }";
3039 S = "{ S_1[i] -> [0, i, 1]; S_0[i] -> [0, i, 0] }";
3040 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3041 return -1;
3043 D = "[n] -> { S_0[j, k] : j <= -1 + n and j >= 0 and "
3044 "k <= -1 + n and k >= 0 }";
3045 W = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and " "k <= -1 + n and k >= 0 }";
3046 R = "[n] -> { S_0[j, k] -> B[j] : j <= -1 + n and j >= 0 and "
3047 "k <= -1 + n and k >= 0; "
3048 "S_0[j, k] -> B[k] : j <= -1 + n and j >= 0 and "
3049 "k <= -1 + n and k >= 0; "
3050 "S_0[j, k] -> A[k] : j <= -1 + n and j >= 0 and "
3051 "k <= -1 + n and k >= 0 }";
3052 S = "[n] -> { S_0[j, k] -> [2, j, k] }";
3053 ctx->opt->schedule_outer_coincidence = 1;
3054 if (test_one_schedule(ctx, D, W, R, S, 0, 0) < 0)
3055 return -1;
3056 ctx->opt->schedule_outer_coincidence = 0;
3058 D = "{Stmt_for_body24[i0, i1, i2, i3]:"
3059 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 6 and i2 >= 2 and "
3060 "i2 <= 6 - i1 and i3 >= 0 and i3 <= -1 + i2;"
3061 "Stmt_for_body24[i0, i1, 1, 0]:"
3062 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 5;"
3063 "Stmt_for_body7[i0, i1, i2]:"
3064 "i0 >= 0 and i0 <= 1 and i1 >= 0 and i1 <= 7 and i2 >= 0 and "
3065 "i2 <= 7 }";
3067 V = "{Stmt_for_body24[0, i1, i2, i3] -> "
3068 "Stmt_for_body24[1, i1, i2, i3]:"
3069 "i3 >= 0 and i3 <= -1 + i2 and i1 >= 0 and i2 <= 6 - i1 and "
3070 "i2 >= 1;"
3071 "Stmt_for_body24[0, i1, i2, i3] -> "
3072 "Stmt_for_body7[1, 1 + i1 + i3, 1 + i1 + i2]:"
3073 "i3 <= -1 + i2 and i2 <= 6 - i1 and i2 >= 1 and i1 >= 0 and "
3074 "i3 >= 0;"
3075 "Stmt_for_body24[0, i1, i2, i3] ->"
3076 "Stmt_for_body7[1, i1, 1 + i1 + i3]:"
3077 "i3 >= 0 and i2 <= 6 - i1 and i1 >= 0 and i3 <= -1 + i2;"
3078 "Stmt_for_body7[0, i1, i2] -> Stmt_for_body7[1, i1, i2]:"
3079 "(i2 >= 1 + i1 and i2 <= 6 and i1 >= 0 and i1 <= 4) or "
3080 "(i2 >= 3 and i2 <= 7 and i1 >= 1 and i2 >= 1 + i1) or "
3081 "(i2 >= 0 and i2 <= i1 and i2 >= -7 + i1 and i1 <= 7);"
3082 "Stmt_for_body7[0, i1, 1 + i1] -> Stmt_for_body7[1, i1, 1 + i1]:"
3083 "i1 <= 6 and i1 >= 0;"
3084 "Stmt_for_body7[0, 0, 7] -> Stmt_for_body7[1, 0, 7];"
3085 "Stmt_for_body7[i0, i1, i2] -> "
3086 "Stmt_for_body24[i0, o1, -1 + i2 - o1, -1 + i1 - o1]:"
3087 "i0 >= 0 and i0 <= 1 and o1 >= 0 and i2 >= 1 + i1 and "
3088 "o1 <= -2 + i2 and i2 <= 7 and o1 <= -1 + i1;"
3089 "Stmt_for_body7[i0, i1, i2] -> "
3090 "Stmt_for_body24[i0, i1, o2, -1 - i1 + i2]:"
3091 "i0 >= 0 and i0 <= 1 and i1 >= 0 and o2 >= -i1 + i2 and "
3092 "o2 >= 1 and o2 <= 6 - i1 and i2 >= 1 + i1 }";
3093 P = V;
3094 S = "{ Stmt_for_body24[i0, i1, i2, i3] -> "
3095 "[i0, 5i0 + i1, 6i0 + i1 + i2, 1 + 6i0 + i1 + i2 + i3, 1];"
3096 "Stmt_for_body7[i0, i1, i2] -> [0, 5i0, 6i0 + i1, 6i0 + i2, 0] }";
3098 if (test_special_schedule(ctx, D, V, P, S) < 0)
3099 return -1;
3101 D = "{ S_0[i, j] : i >= 1 and i <= 10 and j >= 1 and j <= 8 }";
3102 V = "{ S_0[i, j] -> S_0[i, 1 + j] : i >= 1 and i <= 10 and "
3103 "j >= 1 and j <= 7;"
3104 "S_0[i, j] -> S_0[1 + i, j] : i >= 1 and i <= 9 and "
3105 "j >= 1 and j <= 8 }";
3106 P = "{ }";
3107 S = "{ S_0[i, j] -> [i + j, j] }";
3108 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3109 if (test_special_schedule(ctx, D, V, P, S) < 0)
3110 return -1;
3111 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3113 /* Fig. 1 from Feautrier's "Some Efficient Solutions..." pt. 2, 1992 */
3114 D = "[N] -> { S_0[i, j] : i >= 0 and i <= -1 + N and "
3115 "j >= 0 and j <= -1 + i }";
3116 V = "[N] -> { S_0[i, j] -> S_0[i, 1 + j] : j <= -2 + i and "
3117 "i <= -1 + N and j >= 0;"
3118 "S_0[i, -1 + i] -> S_0[1 + i, 0] : i >= 1 and "
3119 "i <= -2 + N }";
3120 P = "{ }";
3121 S = "{ S_0[i, j] -> [i, j] }";
3122 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3123 if (test_special_schedule(ctx, D, V, P, S) < 0)
3124 return -1;
3125 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3127 /* Test both algorithms on a case with only proximity dependences. */
3128 D = "{ S[i,j] : 0 <= i <= 10 }";
3129 V = "{ }";
3130 P = "{ S[i,j] -> S[i+1,j] : 0 <= i,j <= 10 }";
3131 S = "{ S[i, j] -> [j, i] }";
3132 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3133 if (test_special_schedule(ctx, D, V, P, S) < 0)
3134 return -1;
3135 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3136 if (test_special_schedule(ctx, D, V, P, S) < 0)
3137 return -1;
3139 D = "{ A[a]; B[] }";
3140 V = "{}";
3141 P = "{ A[a] -> B[] }";
3142 if (test_has_schedule(ctx, D, V, P) < 0)
3143 return -1;
3145 if (test_padded_schedule(ctx) < 0)
3146 return -1;
3148 /* Check that check for progress is not confused by rational
3149 * solution.
3151 D = "[N] -> { S0[i, j] : i >= 0 and i <= N and j >= 0 and j <= N }";
3152 V = "[N] -> { S0[i0, -1 + N] -> S0[2 + i0, 0] : i0 >= 0 and "
3153 "i0 <= -2 + N; "
3154 "S0[i0, i1] -> S0[i0, 1 + i1] : i0 >= 0 and "
3155 "i0 <= N and i1 >= 0 and i1 <= -1 + N }";
3156 P = "{}";
3157 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3158 if (test_has_schedule(ctx, D, V, P) < 0)
3159 return -1;
3160 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3162 /* Check that we allow schedule rows that are only non-trivial
3163 * on some full-dimensional domains.
3165 D = "{ S1[j] : 0 <= j <= 1; S0[]; S2[k] : 0 <= k <= 1 }";
3166 V = "{ S0[] -> S1[j] : 0 <= j <= 1; S2[0] -> S0[];"
3167 "S1[j] -> S2[1] : 0 <= j <= 1 }";
3168 P = "{}";
3169 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_FEAUTRIER;
3170 if (test_has_schedule(ctx, D, V, P) < 0)
3171 return -1;
3172 ctx->opt->schedule_algorithm = ISL_SCHEDULE_ALGORITHM_ISL;
3174 if (test_conditional_schedule_constraints(ctx) < 0)
3175 return -1;
3177 return 0;
3180 int test_plain_injective(isl_ctx *ctx, const char *str, int injective)
3182 isl_union_map *umap;
3183 int test;
3185 umap = isl_union_map_read_from_str(ctx, str);
3186 test = isl_union_map_plain_is_injective(umap);
3187 isl_union_map_free(umap);
3188 if (test < 0)
3189 return -1;
3190 if (test == injective)
3191 return 0;
3192 if (injective)
3193 isl_die(ctx, isl_error_unknown,
3194 "map not detected as injective", return -1);
3195 else
3196 isl_die(ctx, isl_error_unknown,
3197 "map detected as injective", return -1);
3200 int test_injective(isl_ctx *ctx)
3202 const char *str;
3204 if (test_plain_injective(ctx, "{S[i,j] -> A[0]; T[i,j] -> B[1]}", 0))
3205 return -1;
3206 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> B[0]}", 1))
3207 return -1;
3208 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[1]}", 1))
3209 return -1;
3210 if (test_plain_injective(ctx, "{S[] -> A[0]; T[] -> A[0]}", 0))
3211 return -1;
3212 if (test_plain_injective(ctx, "{S[i] -> A[i,0]; T[i] -> A[i,1]}", 1))
3213 return -1;
3214 if (test_plain_injective(ctx, "{S[i] -> A[i]; T[i] -> A[i]}", 0))
3215 return -1;
3216 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[0,1]}", 1))
3217 return -1;
3218 if (test_plain_injective(ctx, "{S[] -> A[0,0]; T[] -> A[1,0]}", 1))
3219 return -1;
3221 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[1,0]}";
3222 if (test_plain_injective(ctx, str, 1))
3223 return -1;
3224 str = "{S[] -> A[0,0]; T[] -> A[0,1]; U[] -> A[0,0]}";
3225 if (test_plain_injective(ctx, str, 0))
3226 return -1;
3228 return 0;
3231 static int aff_plain_is_equal(__isl_keep isl_aff *aff, const char *str)
3233 isl_aff *aff2;
3234 int equal;
3236 if (!aff)
3237 return -1;
3239 aff2 = isl_aff_read_from_str(isl_aff_get_ctx(aff), str);
3240 equal = isl_aff_plain_is_equal(aff, aff2);
3241 isl_aff_free(aff2);
3243 return equal;
3246 static int aff_check_plain_equal(__isl_keep isl_aff *aff, const char *str)
3248 int equal;
3250 equal = aff_plain_is_equal(aff, str);
3251 if (equal < 0)
3252 return -1;
3253 if (!equal)
3254 isl_die(isl_aff_get_ctx(aff), isl_error_unknown,
3255 "result not as expected", return -1);
3256 return 0;
3259 struct {
3260 __isl_give isl_aff *(*fn)(__isl_take isl_aff *aff1,
3261 __isl_take isl_aff *aff2);
3262 } aff_bin_op[] = {
3263 ['+'] = { &isl_aff_add },
3264 ['-'] = { &isl_aff_sub },
3265 ['*'] = { &isl_aff_mul },
3266 ['/'] = { &isl_aff_div },
3269 struct {
3270 const char *arg1;
3271 unsigned char op;
3272 const char *arg2;
3273 const char *res;
3274 } aff_bin_tests[] = {
3275 { "{ [i] -> [i] }", '+', "{ [i] -> [i] }",
3276 "{ [i] -> [2i] }" },
3277 { "{ [i] -> [i] }", '-', "{ [i] -> [i] }",
3278 "{ [i] -> [0] }" },
3279 { "{ [i] -> [i] }", '*', "{ [i] -> [2] }",
3280 "{ [i] -> [2i] }" },
3281 { "{ [i] -> [2] }", '*', "{ [i] -> [i] }",
3282 "{ [i] -> [2i] }" },
3283 { "{ [i] -> [i] }", '/', "{ [i] -> [2] }",
3284 "{ [i] -> [i/2] }" },
3285 { "{ [i] -> [2i] }", '/', "{ [i] -> [2] }",
3286 "{ [i] -> [i] }" },
3287 { "{ [i] -> [i] }", '+', "{ [i] -> [NaN] }",
3288 "{ [i] -> [NaN] }" },
3289 { "{ [i] -> [i] }", '-', "{ [i] -> [NaN] }",
3290 "{ [i] -> [NaN] }" },
3291 { "{ [i] -> [i] }", '*', "{ [i] -> [NaN] }",
3292 "{ [i] -> [NaN] }" },
3293 { "{ [i] -> [2] }", '*', "{ [i] -> [NaN] }",
3294 "{ [i] -> [NaN] }" },
3295 { "{ [i] -> [i] }", '/', "{ [i] -> [NaN] }",
3296 "{ [i] -> [NaN] }" },
3297 { "{ [i] -> [2] }", '/', "{ [i] -> [NaN] }",
3298 "{ [i] -> [NaN] }" },
3299 { "{ [i] -> [NaN] }", '+', "{ [i] -> [i] }",
3300 "{ [i] -> [NaN] }" },
3301 { "{ [i] -> [NaN] }", '-', "{ [i] -> [i] }",
3302 "{ [i] -> [NaN] }" },
3303 { "{ [i] -> [NaN] }", '*', "{ [i] -> [2] }",
3304 "{ [i] -> [NaN] }" },
3305 { "{ [i] -> [NaN] }", '*', "{ [i] -> [i] }",
3306 "{ [i] -> [NaN] }" },
3307 { "{ [i] -> [NaN] }", '/', "{ [i] -> [2] }",
3308 "{ [i] -> [NaN] }" },
3309 { "{ [i] -> [NaN] }", '/', "{ [i] -> [i] }",
3310 "{ [i] -> [NaN] }" },
3313 /* Perform some basic tests of binary operations on isl_aff objects.
3315 static int test_bin_aff(isl_ctx *ctx)
3317 int i;
3318 isl_aff *aff1, *aff2, *res;
3319 __isl_give isl_aff *(*fn)(__isl_take isl_aff *aff1,
3320 __isl_take isl_aff *aff2);
3321 int ok;
3323 for (i = 0; i < ARRAY_SIZE(aff_bin_tests); ++i) {
3324 aff1 = isl_aff_read_from_str(ctx, aff_bin_tests[i].arg1);
3325 aff2 = isl_aff_read_from_str(ctx, aff_bin_tests[i].arg2);
3326 res = isl_aff_read_from_str(ctx, aff_bin_tests[i].res);
3327 fn = aff_bin_op[aff_bin_tests[i].op].fn;
3328 aff1 = fn(aff1, aff2);
3329 if (isl_aff_is_nan(res))
3330 ok = isl_aff_is_nan(aff1);
3331 else
3332 ok = isl_aff_plain_is_equal(aff1, res);
3333 isl_aff_free(aff1);
3334 isl_aff_free(res);
3335 if (ok < 0)
3336 return -1;
3337 if (!ok)
3338 isl_die(ctx, isl_error_unknown,
3339 "unexpected result", return -1);
3342 return 0;
3345 int test_aff(isl_ctx *ctx)
3347 const char *str;
3348 isl_set *set;
3349 isl_space *space;
3350 isl_local_space *ls;
3351 isl_aff *aff;
3352 int zero, equal;
3354 if (test_bin_aff(ctx) < 0)
3355 return -1;
3357 space = isl_space_set_alloc(ctx, 0, 1);
3358 ls = isl_local_space_from_space(space);
3359 aff = isl_aff_zero_on_domain(ls);
3361 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
3362 aff = isl_aff_scale_down_ui(aff, 3);
3363 aff = isl_aff_floor(aff);
3364 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
3365 aff = isl_aff_scale_down_ui(aff, 2);
3366 aff = isl_aff_floor(aff);
3367 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
3369 str = "{ [10] }";
3370 set = isl_set_read_from_str(ctx, str);
3371 aff = isl_aff_gist(aff, set);
3373 aff = isl_aff_add_constant_si(aff, -16);
3374 zero = isl_aff_plain_is_zero(aff);
3375 isl_aff_free(aff);
3377 if (zero < 0)
3378 return -1;
3379 if (!zero)
3380 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3382 aff = isl_aff_read_from_str(ctx, "{ [-1] }");
3383 aff = isl_aff_scale_down_ui(aff, 64);
3384 aff = isl_aff_floor(aff);
3385 equal = aff_check_plain_equal(aff, "{ [-1] }");
3386 isl_aff_free(aff);
3387 if (equal < 0)
3388 return -1;
3390 return 0;
3393 int test_dim_max(isl_ctx *ctx)
3395 int equal;
3396 const char *str;
3397 isl_set *set1, *set2;
3398 isl_set *set;
3399 isl_map *map;
3400 isl_pw_aff *pwaff;
3402 str = "[N] -> { [i] : 0 <= i <= min(N,10) }";
3403 set = isl_set_read_from_str(ctx, str);
3404 pwaff = isl_set_dim_max(set, 0);
3405 set1 = isl_set_from_pw_aff(pwaff);
3406 str = "[N] -> { [10] : N >= 10; [N] : N <= 9 and N >= 0 }";
3407 set2 = isl_set_read_from_str(ctx, str);
3408 equal = isl_set_is_equal(set1, set2);
3409 isl_set_free(set1);
3410 isl_set_free(set2);
3411 if (equal < 0)
3412 return -1;
3413 if (!equal)
3414 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3416 str = "[N] -> { [i] : 0 <= i <= max(2N,N+6) }";
3417 set = isl_set_read_from_str(ctx, str);
3418 pwaff = isl_set_dim_max(set, 0);
3419 set1 = isl_set_from_pw_aff(pwaff);
3420 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
3421 set2 = isl_set_read_from_str(ctx, str);
3422 equal = isl_set_is_equal(set1, set2);
3423 isl_set_free(set1);
3424 isl_set_free(set2);
3425 if (equal < 0)
3426 return -1;
3427 if (!equal)
3428 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3430 str = "[N] -> { [i] : 0 <= i <= 2N or 0 <= i <= N+6 }";
3431 set = isl_set_read_from_str(ctx, str);
3432 pwaff = isl_set_dim_max(set, 0);
3433 set1 = isl_set_from_pw_aff(pwaff);
3434 str = "[N] -> { [6 + N] : -6 <= N <= 5; [2N] : N >= 6 }";
3435 set2 = isl_set_read_from_str(ctx, str);
3436 equal = isl_set_is_equal(set1, set2);
3437 isl_set_free(set1);
3438 isl_set_free(set2);
3439 if (equal < 0)
3440 return -1;
3441 if (!equal)
3442 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3444 str = "[N,M] -> { [i,j] -> [([i/16]), i%16, ([j/16]), j%16] : "
3445 "0 <= i < N and 0 <= j < M }";
3446 map = isl_map_read_from_str(ctx, str);
3447 set = isl_map_range(map);
3449 pwaff = isl_set_dim_max(isl_set_copy(set), 0);
3450 set1 = isl_set_from_pw_aff(pwaff);
3451 str = "[N,M] -> { [([(N-1)/16])] : M,N > 0 }";
3452 set2 = isl_set_read_from_str(ctx, str);
3453 equal = isl_set_is_equal(set1, set2);
3454 isl_set_free(set1);
3455 isl_set_free(set2);
3457 pwaff = isl_set_dim_max(isl_set_copy(set), 3);
3458 set1 = isl_set_from_pw_aff(pwaff);
3459 str = "[N,M] -> { [t] : t = min(M-1,15) and M,N > 0 }";
3460 set2 = isl_set_read_from_str(ctx, str);
3461 if (equal >= 0 && equal)
3462 equal = isl_set_is_equal(set1, set2);
3463 isl_set_free(set1);
3464 isl_set_free(set2);
3466 isl_set_free(set);
3468 if (equal < 0)
3469 return -1;
3470 if (!equal)
3471 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3473 /* Check that solutions are properly merged. */
3474 str = "[n] -> { [a, b, c] : c >= -4a - 2b and "
3475 "c <= -1 + n - 4a - 2b and c >= -2b and "
3476 "4a >= -4 + n and c >= 0 }";
3477 set = isl_set_read_from_str(ctx, str);
3478 pwaff = isl_set_dim_min(set, 2);
3479 set1 = isl_set_from_pw_aff(pwaff);
3480 str = "[n] -> { [(0)] : n >= 1 }";
3481 set2 = isl_set_read_from_str(ctx, str);
3482 equal = isl_set_is_equal(set1, set2);
3483 isl_set_free(set1);
3484 isl_set_free(set2);
3486 if (equal < 0)
3487 return -1;
3488 if (!equal)
3489 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3491 /* Check that empty solution lie in the right space. */
3492 str = "[n] -> { [t,a] : 1 = 0 }";
3493 set = isl_set_read_from_str(ctx, str);
3494 pwaff = isl_set_dim_max(set, 0);
3495 set1 = isl_set_from_pw_aff(pwaff);
3496 str = "[n] -> { [t] : 1 = 0 }";
3497 set2 = isl_set_read_from_str(ctx, str);
3498 equal = isl_set_is_equal(set1, set2);
3499 isl_set_free(set1);
3500 isl_set_free(set2);
3502 if (equal < 0)
3503 return -1;
3504 if (!equal)
3505 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3507 return 0;
3510 /* Is "pma" obviously equal to the isl_pw_multi_aff represented by "str"?
3512 static int pw_multi_aff_plain_is_equal(__isl_keep isl_pw_multi_aff *pma,
3513 const char *str)
3515 isl_ctx *ctx;
3516 isl_pw_multi_aff *pma2;
3517 int equal;
3519 if (!pma)
3520 return -1;
3522 ctx = isl_pw_multi_aff_get_ctx(pma);
3523 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
3524 equal = isl_pw_multi_aff_plain_is_equal(pma, pma2);
3525 isl_pw_multi_aff_free(pma2);
3527 return equal;
3530 /* Check that "pma" is obviously equal to the isl_pw_multi_aff
3531 * represented by "str".
3533 static int pw_multi_aff_check_plain_equal(__isl_keep isl_pw_multi_aff *pma,
3534 const char *str)
3536 int equal;
3538 equal = pw_multi_aff_plain_is_equal(pma, str);
3539 if (equal < 0)
3540 return -1;
3541 if (!equal)
3542 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_unknown,
3543 "result not as expected", return -1);
3544 return 0;
3547 /* Basic test for isl_pw_multi_aff_product.
3549 * Check that multiple pieces are properly handled.
3551 static int test_product_pma(isl_ctx *ctx)
3553 int equal;
3554 const char *str;
3555 isl_pw_multi_aff *pma1, *pma2;
3557 str = "{ A[i] -> B[1] : i < 0; A[i] -> B[2] : i >= 0 }";
3558 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
3559 str = "{ C[] -> D[] }";
3560 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
3561 pma1 = isl_pw_multi_aff_product(pma1, pma2);
3562 str = "{ [A[i] -> C[]] -> [B[(1)] -> D[]] : i < 0;"
3563 "[A[i] -> C[]] -> [B[(2)] -> D[]] : i >= 0 }";
3564 equal = pw_multi_aff_check_plain_equal(pma1, str);
3565 isl_pw_multi_aff_free(pma1);
3566 if (equal < 0)
3567 return -1;
3569 return 0;
3572 int test_product(isl_ctx *ctx)
3574 const char *str;
3575 isl_set *set;
3576 isl_union_set *uset1, *uset2;
3577 int ok;
3579 str = "{ A[i] }";
3580 set = isl_set_read_from_str(ctx, str);
3581 set = isl_set_product(set, isl_set_copy(set));
3582 ok = isl_set_is_wrapping(set);
3583 isl_set_free(set);
3584 if (ok < 0)
3585 return -1;
3586 if (!ok)
3587 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3589 str = "{ [] }";
3590 uset1 = isl_union_set_read_from_str(ctx, str);
3591 uset1 = isl_union_set_product(uset1, isl_union_set_copy(uset1));
3592 str = "{ [[] -> []] }";
3593 uset2 = isl_union_set_read_from_str(ctx, str);
3594 ok = isl_union_set_is_equal(uset1, uset2);
3595 isl_union_set_free(uset1);
3596 isl_union_set_free(uset2);
3597 if (ok < 0)
3598 return -1;
3599 if (!ok)
3600 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3602 if (test_product_pma(ctx) < 0)
3603 return -1;
3605 return 0;
3608 /* Check that two sets are not considered disjoint just because
3609 * they have a different set of (named) parameters.
3611 static int test_disjoint(isl_ctx *ctx)
3613 const char *str;
3614 isl_set *set, *set2;
3615 int disjoint;
3617 str = "[n] -> { [[]->[]] }";
3618 set = isl_set_read_from_str(ctx, str);
3619 str = "{ [[]->[]] }";
3620 set2 = isl_set_read_from_str(ctx, str);
3621 disjoint = isl_set_is_disjoint(set, set2);
3622 isl_set_free(set);
3623 isl_set_free(set2);
3624 if (disjoint < 0)
3625 return -1;
3626 if (disjoint)
3627 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3629 return 0;
3632 int test_equal(isl_ctx *ctx)
3634 const char *str;
3635 isl_set *set, *set2;
3636 int equal;
3638 str = "{ S_6[i] }";
3639 set = isl_set_read_from_str(ctx, str);
3640 str = "{ S_7[i] }";
3641 set2 = isl_set_read_from_str(ctx, str);
3642 equal = isl_set_is_equal(set, set2);
3643 isl_set_free(set);
3644 isl_set_free(set2);
3645 if (equal < 0)
3646 return -1;
3647 if (equal)
3648 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3650 return 0;
3653 static int test_plain_fixed(isl_ctx *ctx, __isl_take isl_map *map,
3654 enum isl_dim_type type, unsigned pos, int fixed)
3656 int test;
3658 test = isl_map_plain_is_fixed(map, type, pos, NULL);
3659 isl_map_free(map);
3660 if (test < 0)
3661 return -1;
3662 if (test == fixed)
3663 return 0;
3664 if (fixed)
3665 isl_die(ctx, isl_error_unknown,
3666 "map not detected as fixed", return -1);
3667 else
3668 isl_die(ctx, isl_error_unknown,
3669 "map detected as fixed", return -1);
3672 int test_fixed(isl_ctx *ctx)
3674 const char *str;
3675 isl_map *map;
3677 str = "{ [i] -> [i] }";
3678 map = isl_map_read_from_str(ctx, str);
3679 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 0))
3680 return -1;
3681 str = "{ [i] -> [1] }";
3682 map = isl_map_read_from_str(ctx, str);
3683 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
3684 return -1;
3685 str = "{ S_1[p1] -> [o0] : o0 = -2 and p1 >= 1 and p1 <= 7 }";
3686 map = isl_map_read_from_str(ctx, str);
3687 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
3688 return -1;
3689 map = isl_map_read_from_str(ctx, str);
3690 map = isl_map_neg(map);
3691 if (test_plain_fixed(ctx, map, isl_dim_out, 0, 1))
3692 return -1;
3694 return 0;
3697 struct isl_vertices_test_data {
3698 const char *set;
3699 int n;
3700 const char *vertex[2];
3701 } vertices_tests[] = {
3702 { "{ A[t, i] : t = 12 and i >= 4 and i <= 12 }",
3703 2, { "{ A[12, 4] }", "{ A[12, 12] }" } },
3704 { "{ A[t, i] : t = 14 and i = 1 }",
3705 1, { "{ A[14, 1] }" } },
3708 /* Check that "vertex" corresponds to one of the vertices in data->vertex.
3710 static int find_vertex(__isl_take isl_vertex *vertex, void *user)
3712 struct isl_vertices_test_data *data = user;
3713 isl_ctx *ctx;
3714 isl_multi_aff *ma;
3715 isl_basic_set *bset;
3716 isl_pw_multi_aff *pma;
3717 int i;
3718 int equal;
3720 ctx = isl_vertex_get_ctx(vertex);
3721 bset = isl_vertex_get_domain(vertex);
3722 ma = isl_vertex_get_expr(vertex);
3723 pma = isl_pw_multi_aff_alloc(isl_set_from_basic_set(bset), ma);
3725 for (i = 0; i < data->n; ++i) {
3726 isl_pw_multi_aff *pma_i;
3728 pma_i = isl_pw_multi_aff_read_from_str(ctx, data->vertex[i]);
3729 equal = isl_pw_multi_aff_plain_is_equal(pma, pma_i);
3730 isl_pw_multi_aff_free(pma_i);
3732 if (equal < 0 || equal)
3733 break;
3736 isl_pw_multi_aff_free(pma);
3737 isl_vertex_free(vertex);
3739 if (equal < 0)
3740 return -1;
3742 return equal ? 0 : - 1;
3745 int test_vertices(isl_ctx *ctx)
3747 int i;
3749 for (i = 0; i < ARRAY_SIZE(vertices_tests); ++i) {
3750 isl_basic_set *bset;
3751 isl_vertices *vertices;
3752 int ok = 1;
3753 int n;
3755 bset = isl_basic_set_read_from_str(ctx, vertices_tests[i].set);
3756 vertices = isl_basic_set_compute_vertices(bset);
3757 n = isl_vertices_get_n_vertices(vertices);
3758 if (vertices_tests[i].n != n)
3759 ok = 0;
3760 if (isl_vertices_foreach_vertex(vertices, &find_vertex,
3761 &vertices_tests[i]) < 0)
3762 ok = 0;
3763 isl_vertices_free(vertices);
3764 isl_basic_set_free(bset);
3766 if (!vertices)
3767 return -1;
3768 if (!ok)
3769 isl_die(ctx, isl_error_unknown, "unexpected vertices",
3770 return -1);
3773 return 0;
3776 int test_union_pw(isl_ctx *ctx)
3778 int equal;
3779 const char *str;
3780 isl_union_set *uset;
3781 isl_union_pw_qpolynomial *upwqp1, *upwqp2;
3783 str = "{ [x] -> x^2 }";
3784 upwqp1 = isl_union_pw_qpolynomial_read_from_str(ctx, str);
3785 upwqp2 = isl_union_pw_qpolynomial_copy(upwqp1);
3786 uset = isl_union_pw_qpolynomial_domain(upwqp1);
3787 upwqp1 = isl_union_pw_qpolynomial_copy(upwqp2);
3788 upwqp1 = isl_union_pw_qpolynomial_intersect_domain(upwqp1, uset);
3789 equal = isl_union_pw_qpolynomial_plain_is_equal(upwqp1, upwqp2);
3790 isl_union_pw_qpolynomial_free(upwqp1);
3791 isl_union_pw_qpolynomial_free(upwqp2);
3792 if (equal < 0)
3793 return -1;
3794 if (!equal)
3795 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3797 return 0;
3800 int test_output(isl_ctx *ctx)
3802 char *s;
3803 const char *str;
3804 isl_pw_aff *pa;
3805 isl_printer *p;
3806 int equal;
3808 str = "[x] -> { [1] : x % 4 <= 2; [2] : x = 3 }";
3809 pa = isl_pw_aff_read_from_str(ctx, str);
3811 p = isl_printer_to_str(ctx);
3812 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
3813 p = isl_printer_print_pw_aff(p, pa);
3814 s = isl_printer_get_str(p);
3815 isl_printer_free(p);
3816 isl_pw_aff_free(pa);
3817 if (!s)
3818 equal = -1;
3819 else
3820 equal = !strcmp(s, "4 * floord(x, 4) + 2 >= x ? 1 : 2");
3821 free(s);
3822 if (equal < 0)
3823 return -1;
3824 if (!equal)
3825 isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
3827 return 0;
3830 int test_sample(isl_ctx *ctx)
3832 const char *str;
3833 isl_basic_set *bset1, *bset2;
3834 int empty, subset;
3836 str = "{ [a, b, c, d, e, f, g, h, i, j, k] : "
3837 "3i >= 1073741823b - c - 1073741823e + f and c >= 0 and "
3838 "3i >= -1 + 3221225466b + c + d - 3221225466e - f and "
3839 "2e >= a - b and 3e <= 2a and 3k <= -a and f <= -1 + a and "
3840 "3i <= 4 - a + 4b + 2c - e - 2f and 3k <= -a + c - f and "
3841 "3h >= -2 + a and 3g >= -3 - a and 3k >= -2 - a and "
3842 "3i >= -2 - a - 2c + 3e + 2f and 3h <= a + c - f and "
3843 "3h >= a + 2147483646b + 2c - 2147483646e - 2f and "
3844 "3g <= -1 - a and 3i <= 1 + c + d - f and a <= 1073741823 and "
3845 "f >= 1 - a + 1073741822b + c + d - 1073741822e and "
3846 "3i >= 1 + 2b - 2c + e + 2f + 3g and "
3847 "1073741822f <= 1073741822 - a + 1073741821b + 1073741822c +"
3848 "d - 1073741821e and "
3849 "3j <= 3 - a + 3b and 3g <= -2 - 2b + c + d - e - f and "
3850 "3j >= 1 - a + b + 2e and "
3851 "3f >= -3 + a + 3221225462b + 3c + d - 3221225465e and "
3852 "3i <= 4 - a + 4b - e and "
3853 "f <= 1073741822 + 1073741822b - 1073741822e and 3h <= a and "
3854 "f >= 0 and 2e <= 4 - a + 5b - d and 2e <= a - b + d and "
3855 "c <= -1 + a and 3i >= -2 - a + 3e and "
3856 "1073741822e <= 1073741823 - a + 1073741822b + c and "
3857 "3g >= -4 + 3221225464b + 3c + d - 3221225467e - 3f and "
3858 "3i >= -1 + 3221225466b + 3c + d - 3221225466e - 3f and "
3859 "1073741823e >= 1 + 1073741823b - d and "
3860 "3i >= 1073741823b + c - 1073741823e - f and "
3861 "3i >= 1 + 2b + e + 3g }";
3862 bset1 = isl_basic_set_read_from_str(ctx, str);
3863 bset2 = isl_basic_set_sample(isl_basic_set_copy(bset1));
3864 empty = isl_basic_set_is_empty(bset2);
3865 subset = isl_basic_set_is_subset(bset2, bset1);
3866 isl_basic_set_free(bset1);
3867 isl_basic_set_free(bset2);
3868 if (empty < 0 || subset < 0)
3869 return -1;
3870 if (empty)
3871 isl_die(ctx, isl_error_unknown, "point not found", return -1);
3872 if (!subset)
3873 isl_die(ctx, isl_error_unknown, "bad point found", return -1);
3875 return 0;
3878 int test_fixed_power(isl_ctx *ctx)
3880 const char *str;
3881 isl_map *map;
3882 isl_int exp;
3883 int equal;
3885 isl_int_init(exp);
3886 str = "{ [i] -> [i + 1] }";
3887 map = isl_map_read_from_str(ctx, str);
3888 isl_int_set_si(exp, 23);
3889 map = isl_map_fixed_power(map, exp);
3890 equal = map_check_equal(map, "{ [i] -> [i + 23] }");
3891 isl_int_clear(exp);
3892 isl_map_free(map);
3893 if (equal < 0)
3894 return -1;
3896 return 0;
3899 int test_slice(isl_ctx *ctx)
3901 const char *str;
3902 isl_map *map;
3903 int equal;
3905 str = "{ [i] -> [j] }";
3906 map = isl_map_read_from_str(ctx, str);
3907 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_out, 0);
3908 equal = map_check_equal(map, "{ [i] -> [i] }");
3909 isl_map_free(map);
3910 if (equal < 0)
3911 return -1;
3913 str = "{ [i] -> [j] }";
3914 map = isl_map_read_from_str(ctx, str);
3915 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_in, 0);
3916 equal = map_check_equal(map, "{ [i] -> [j] }");
3917 isl_map_free(map);
3918 if (equal < 0)
3919 return -1;
3921 str = "{ [i] -> [j] }";
3922 map = isl_map_read_from_str(ctx, str);
3923 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_out, 0);
3924 equal = map_check_equal(map, "{ [i] -> [-i] }");
3925 isl_map_free(map);
3926 if (equal < 0)
3927 return -1;
3929 str = "{ [i] -> [j] }";
3930 map = isl_map_read_from_str(ctx, str);
3931 map = isl_map_oppose(map, isl_dim_in, 0, isl_dim_in, 0);
3932 equal = map_check_equal(map, "{ [0] -> [j] }");
3933 isl_map_free(map);
3934 if (equal < 0)
3935 return -1;
3937 str = "{ [i] -> [j] }";
3938 map = isl_map_read_from_str(ctx, str);
3939 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_out, 0);
3940 equal = map_check_equal(map, "{ [i] -> [j] : i > j }");
3941 isl_map_free(map);
3942 if (equal < 0)
3943 return -1;
3945 str = "{ [i] -> [j] }";
3946 map = isl_map_read_from_str(ctx, str);
3947 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_in, 0);
3948 equal = map_check_equal(map, "{ [i] -> [j] : false }");
3949 isl_map_free(map);
3950 if (equal < 0)
3951 return -1;
3953 return 0;
3956 int test_eliminate(isl_ctx *ctx)
3958 const char *str;
3959 isl_map *map;
3960 int equal;
3962 str = "{ [i] -> [j] : i = 2j }";
3963 map = isl_map_read_from_str(ctx, str);
3964 map = isl_map_eliminate(map, isl_dim_out, 0, 1);
3965 equal = map_check_equal(map, "{ [i] -> [j] : exists a : i = 2a }");
3966 isl_map_free(map);
3967 if (equal < 0)
3968 return -1;
3970 return 0;
3973 /* Check that isl_set_dim_residue_class detects that the values of j
3974 * in the set below are all odd and that it does not detect any spurious
3975 * strides.
3977 static int test_residue_class(isl_ctx *ctx)
3979 const char *str;
3980 isl_set *set;
3981 isl_int m, r;
3982 int res;
3984 str = "{ [i,j] : j = 4 i + 1 and 0 <= i <= 100; "
3985 "[i,j] : j = 4 i + 3 and 500 <= i <= 600 }";
3986 set = isl_set_read_from_str(ctx, str);
3987 isl_int_init(m);
3988 isl_int_init(r);
3989 res = isl_set_dim_residue_class(set, 1, &m, &r);
3990 if (res >= 0 &&
3991 (isl_int_cmp_si(m, 2) != 0 || isl_int_cmp_si(r, 1) != 0))
3992 isl_die(ctx, isl_error_unknown, "incorrect residue class",
3993 res = -1);
3994 isl_int_clear(r);
3995 isl_int_clear(m);
3996 isl_set_free(set);
3998 return res;
4001 int test_align_parameters(isl_ctx *ctx)
4003 const char *str;
4004 isl_space *space;
4005 isl_multi_aff *ma1, *ma2;
4006 int equal;
4008 str = "{ A[B[] -> C[]] -> D[E[] -> F[]] }";
4009 ma1 = isl_multi_aff_read_from_str(ctx, str);
4011 space = isl_space_params_alloc(ctx, 1);
4012 space = isl_space_set_dim_name(space, isl_dim_param, 0, "N");
4013 ma1 = isl_multi_aff_align_params(ma1, space);
4015 str = "[N] -> { A[B[] -> C[]] -> D[E[] -> F[]] }";
4016 ma2 = isl_multi_aff_read_from_str(ctx, str);
4018 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
4020 isl_multi_aff_free(ma1);
4021 isl_multi_aff_free(ma2);
4023 if (equal < 0)
4024 return -1;
4025 if (!equal)
4026 isl_die(ctx, isl_error_unknown,
4027 "result not as expected", return -1);
4029 return 0;
4032 static int test_list(isl_ctx *ctx)
4034 isl_id *a, *b, *c, *d, *id;
4035 isl_id_list *list;
4036 int ok;
4038 a = isl_id_alloc(ctx, "a", NULL);
4039 b = isl_id_alloc(ctx, "b", NULL);
4040 c = isl_id_alloc(ctx, "c", NULL);
4041 d = isl_id_alloc(ctx, "d", NULL);
4043 list = isl_id_list_alloc(ctx, 4);
4044 list = isl_id_list_add(list, a);
4045 list = isl_id_list_add(list, b);
4046 list = isl_id_list_add(list, c);
4047 list = isl_id_list_add(list, d);
4048 list = isl_id_list_drop(list, 1, 1);
4050 if (isl_id_list_n_id(list) != 3) {
4051 isl_id_list_free(list);
4052 isl_die(ctx, isl_error_unknown,
4053 "unexpected number of elements in list", return -1);
4056 id = isl_id_list_get_id(list, 0);
4057 ok = id == a;
4058 isl_id_free(id);
4059 id = isl_id_list_get_id(list, 1);
4060 ok = ok && id == c;
4061 isl_id_free(id);
4062 id = isl_id_list_get_id(list, 2);
4063 ok = ok && id == d;
4064 isl_id_free(id);
4066 isl_id_list_free(list);
4068 if (!ok)
4069 isl_die(ctx, isl_error_unknown,
4070 "unexpected elements in list", return -1);
4072 return 0;
4075 const char *set_conversion_tests[] = {
4076 "[N] -> { [i] : N - 1 <= 2 i <= N }",
4077 "[N] -> { [i] : exists a : i = 4 a and N - 1 <= i <= N }",
4078 "[N] -> { [i,j] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
4079 "[N] -> { [[i]->[j]] : exists a : i = 4 a and N - 1 <= i, 2j <= N }",
4080 "[N] -> { [3*floor(N/2) + 5*floor(N/3)] }",
4083 /* Check that converting from isl_set to isl_pw_multi_aff and back
4084 * to isl_set produces the original isl_set.
4086 static int test_set_conversion(isl_ctx *ctx)
4088 int i;
4089 const char *str;
4090 isl_set *set1, *set2;
4091 isl_pw_multi_aff *pma;
4092 int equal;
4094 for (i = 0; i < ARRAY_SIZE(set_conversion_tests); ++i) {
4095 str = set_conversion_tests[i];
4096 set1 = isl_set_read_from_str(ctx, str);
4097 pma = isl_pw_multi_aff_from_set(isl_set_copy(set1));
4098 set2 = isl_set_from_pw_multi_aff(pma);
4099 equal = isl_set_is_equal(set1, set2);
4100 isl_set_free(set1);
4101 isl_set_free(set2);
4103 if (equal < 0)
4104 return -1;
4105 if (!equal)
4106 isl_die(ctx, isl_error_unknown, "bad conversion",
4107 return -1);
4110 return 0;
4113 /* Check that converting from isl_map to isl_pw_multi_aff and back
4114 * to isl_map produces the original isl_map.
4116 static int test_map_conversion(isl_ctx *ctx)
4118 const char *str;
4119 isl_map *map1, *map2;
4120 isl_pw_multi_aff *pma;
4121 int equal;
4123 str = "{ [a, b, c, d] -> s0[a, b, e, f] : "
4124 "exists (e0 = [(a - 2c)/3], e1 = [(-4 + b - 5d)/9], "
4125 "e2 = [(-d + f)/9]: 3e0 = a - 2c and 9e1 = -4 + b - 5d and "
4126 "9e2 = -d + f and f >= 0 and f <= 8 and 9e >= -5 - 2a and "
4127 "9e <= -2 - 2a) }";
4128 map1 = isl_map_read_from_str(ctx, str);
4129 pma = isl_pw_multi_aff_from_map(isl_map_copy(map1));
4130 map2 = isl_map_from_pw_multi_aff(pma);
4131 equal = isl_map_is_equal(map1, map2);
4132 isl_map_free(map1);
4133 isl_map_free(map2);
4135 if (equal < 0)
4136 return -1;
4137 if (!equal)
4138 isl_die(ctx, isl_error_unknown, "bad conversion", return -1);
4140 return 0;
4143 static int test_conversion(isl_ctx *ctx)
4145 if (test_set_conversion(ctx) < 0)
4146 return -1;
4147 if (test_map_conversion(ctx) < 0)
4148 return -1;
4149 return 0;
4152 /* Check that isl_basic_map_curry does not modify input.
4154 static int test_curry(isl_ctx *ctx)
4156 const char *str;
4157 isl_basic_map *bmap1, *bmap2;
4158 int equal;
4160 str = "{ [A[] -> B[]] -> C[] }";
4161 bmap1 = isl_basic_map_read_from_str(ctx, str);
4162 bmap2 = isl_basic_map_curry(isl_basic_map_copy(bmap1));
4163 equal = isl_basic_map_is_equal(bmap1, bmap2);
4164 isl_basic_map_free(bmap1);
4165 isl_basic_map_free(bmap2);
4167 if (equal < 0)
4168 return -1;
4169 if (equal)
4170 isl_die(ctx, isl_error_unknown,
4171 "curried map should not be equal to original",
4172 return -1);
4174 return 0;
4177 struct {
4178 const char *set;
4179 const char *ma;
4180 const char *res;
4181 } preimage_tests[] = {
4182 { "{ B[i,j] : 0 <= i < 10 and 0 <= j < 100 }",
4183 "{ A[j,i] -> B[i,j] }",
4184 "{ A[j,i] : 0 <= i < 10 and 0 <= j < 100 }" },
4185 { "{ rat: B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
4186 "{ A[a,b] -> B[a/2,b/6] }",
4187 "{ rat: A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 }" },
4188 { "{ B[i,j] : 0 <= i, j and 3 i + 5 j <= 100 }",
4189 "{ A[a,b] -> B[a/2,b/6] }",
4190 "{ A[a,b] : 0 <= a, b and 9 a + 5 b <= 600 and "
4191 "exists i,j : a = 2 i and b = 6 j }" },
4192 { "[n] -> { S[i] : 0 <= i <= 100 }", "[n] -> { S[n] }",
4193 "[n] -> { : 0 <= n <= 100 }" },
4194 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
4195 "{ A[a] -> B[2a] }",
4196 "{ A[a] : 0 <= a < 50 and exists b : a = 2 b }" },
4197 { "{ B[i] : 0 <= i < 100 and exists a : i = 4 a }",
4198 "{ A[a] -> B[([a/2])] }",
4199 "{ A[a] : 0 <= a < 200 and exists b : [a/2] = 4 b }" },
4200 { "{ B[i,j,k] : 0 <= i,j,k <= 100 }",
4201 "{ A[a] -> B[a,a,a/3] }",
4202 "{ A[a] : 0 <= a <= 100 and exists b : a = 3 b }" },
4203 { "{ B[i,j] : j = [(i)/2] } ", "{ A[i,j] -> B[i/3,j] }",
4204 "{ A[i,j] : j = [(i)/6] and exists a : i = 3 a }" },
4207 static int test_preimage_basic_set(isl_ctx *ctx)
4209 int i;
4210 isl_basic_set *bset1, *bset2;
4211 isl_multi_aff *ma;
4212 int equal;
4214 for (i = 0; i < ARRAY_SIZE(preimage_tests); ++i) {
4215 bset1 = isl_basic_set_read_from_str(ctx, preimage_tests[i].set);
4216 ma = isl_multi_aff_read_from_str(ctx, preimage_tests[i].ma);
4217 bset2 = isl_basic_set_read_from_str(ctx, preimage_tests[i].res);
4218 bset1 = isl_basic_set_preimage_multi_aff(bset1, ma);
4219 equal = isl_basic_set_is_equal(bset1, bset2);
4220 isl_basic_set_free(bset1);
4221 isl_basic_set_free(bset2);
4222 if (equal < 0)
4223 return -1;
4224 if (!equal)
4225 isl_die(ctx, isl_error_unknown, "bad preimage",
4226 return -1);
4229 return 0;
4232 struct {
4233 const char *map;
4234 const char *ma;
4235 const char *res;
4236 } preimage_domain_tests[] = {
4237 { "{ B[i,j] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }",
4238 "{ A[j,i] -> B[i,j] }",
4239 "{ A[j,i] -> C[2i + 3j] : 0 <= i < 10 and 0 <= j < 100 }" },
4240 { "{ B[i] -> C[i]; D[i] -> E[i] }",
4241 "{ A[i] -> B[i + 1] }",
4242 "{ A[i] -> C[i + 1] }" },
4243 { "{ B[i] -> C[i]; B[i] -> E[i] }",
4244 "{ A[i] -> B[i + 1] }",
4245 "{ A[i] -> C[i + 1]; A[i] -> E[i + 1] }" },
4246 { "{ B[i] -> C[([i/2])] }",
4247 "{ A[i] -> B[2i] }",
4248 "{ A[i] -> C[i] }" },
4249 { "{ B[i,j] -> C[([i/2]), ([(i+j)/3])] }",
4250 "{ A[i] -> B[([i/5]), ([i/7])] }",
4251 "{ A[i] -> C[([([i/5])/2]), ([(([i/5])+([i/7]))/3])] }" },
4252 { "[N] -> { B[i] -> C[([N/2]), i, ([N/3])] }",
4253 "[N] -> { A[] -> B[([N/5])] }",
4254 "[N] -> { A[] -> C[([N/2]), ([N/5]), ([N/3])] }" },
4255 { "{ B[i] -> C[i] : exists a : i = 5 a }",
4256 "{ A[i] -> B[2i] }",
4257 "{ A[i] -> C[2i] : exists a : 2i = 5 a }" },
4258 { "{ B[i] -> C[i] : exists a : i = 2 a; "
4259 "B[i] -> D[i] : exists a : i = 2 a + 1 }",
4260 "{ A[i] -> B[2i] }",
4261 "{ A[i] -> C[2i] }" },
4264 static int test_preimage_union_map(isl_ctx *ctx)
4266 int i;
4267 isl_union_map *umap1, *umap2;
4268 isl_multi_aff *ma;
4269 int equal;
4271 for (i = 0; i < ARRAY_SIZE(preimage_domain_tests); ++i) {
4272 umap1 = isl_union_map_read_from_str(ctx,
4273 preimage_domain_tests[i].map);
4274 ma = isl_multi_aff_read_from_str(ctx,
4275 preimage_domain_tests[i].ma);
4276 umap2 = isl_union_map_read_from_str(ctx,
4277 preimage_domain_tests[i].res);
4278 umap1 = isl_union_map_preimage_domain_multi_aff(umap1, ma);
4279 equal = isl_union_map_is_equal(umap1, umap2);
4280 isl_union_map_free(umap1);
4281 isl_union_map_free(umap2);
4282 if (equal < 0)
4283 return -1;
4284 if (!equal)
4285 isl_die(ctx, isl_error_unknown, "bad preimage",
4286 return -1);
4289 return 0;
4292 static int test_preimage(isl_ctx *ctx)
4294 if (test_preimage_basic_set(ctx) < 0)
4295 return -1;
4296 if (test_preimage_union_map(ctx) < 0)
4297 return -1;
4299 return 0;
4302 struct {
4303 const char *ma1;
4304 const char *ma;
4305 const char *res;
4306 } pullback_tests[] = {
4307 { "{ B[i,j] -> C[i + 2j] }" , "{ A[a,b] -> B[b,a] }",
4308 "{ A[a,b] -> C[b + 2a] }" },
4309 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/2] }", "{ A[a] -> C[a] }" },
4310 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[2a] }", "{ A[a] -> C[a] }" },
4311 { "{ B[i] -> C[(i)/2] }", "{ A[a] -> B[(a)/3] }",
4312 "{ A[a] -> C[(a)/6] }" },
4313 { "{ B[i] -> C[2i] }", "{ A[a] -> B[5a] }", "{ A[a] -> C[10a] }" },
4314 { "{ B[i] -> C[2i] }", "{ A[a] -> B[(a)/3] }",
4315 "{ A[a] -> C[(2a)/3] }" },
4316 { "{ B[i,j] -> C[i + j] }", "{ A[a] -> B[a,a] }", "{ A[a] -> C[2a] }"},
4317 { "{ B[a] -> C[a,a] }", "{ A[i,j] -> B[i + j] }",
4318 "{ A[i,j] -> C[i + j, i + j] }"},
4319 { "{ B[i] -> C[([i/2])] }", "{ B[5] }", "{ C[2] }" },
4320 { "[n] -> { B[i,j] -> C[([i/2]) + 2j] }",
4321 "[n] -> { B[n,[n/3]] }", "[n] -> { C[([n/2]) + 2*[n/3]] }", },
4324 static int test_pullback(isl_ctx *ctx)
4326 int i;
4327 isl_multi_aff *ma1, *ma2;
4328 isl_multi_aff *ma;
4329 int equal;
4331 for (i = 0; i < ARRAY_SIZE(pullback_tests); ++i) {
4332 ma1 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma1);
4333 ma = isl_multi_aff_read_from_str(ctx, pullback_tests[i].ma);
4334 ma2 = isl_multi_aff_read_from_str(ctx, pullback_tests[i].res);
4335 ma1 = isl_multi_aff_pullback_multi_aff(ma1, ma);
4336 equal = isl_multi_aff_plain_is_equal(ma1, ma2);
4337 isl_multi_aff_free(ma1);
4338 isl_multi_aff_free(ma2);
4339 if (equal < 0)
4340 return -1;
4341 if (!equal)
4342 isl_die(ctx, isl_error_unknown, "bad pullback",
4343 return -1);
4346 return 0;
4349 /* Check that negation is printed correctly.
4351 static int test_ast(isl_ctx *ctx)
4353 isl_ast_expr *expr, *expr1, *expr2, *expr3;
4354 char *str;
4355 int ok;
4357 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
4358 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
4359 expr = isl_ast_expr_add(expr1, expr2);
4360 expr = isl_ast_expr_neg(expr);
4361 str = isl_ast_expr_to_str(expr);
4362 ok = str ? !strcmp(str, "-(A + B)") : -1;
4363 free(str);
4364 isl_ast_expr_free(expr);
4366 if (ok < 0)
4367 return -1;
4368 if (!ok)
4369 isl_die(ctx, isl_error_unknown,
4370 "isl_ast_expr printed incorrectly", return -1);
4372 expr1 = isl_ast_expr_from_id(isl_id_alloc(ctx, "A", NULL));
4373 expr2 = isl_ast_expr_from_id(isl_id_alloc(ctx, "B", NULL));
4374 expr = isl_ast_expr_add(expr1, expr2);
4375 expr3 = isl_ast_expr_from_id(isl_id_alloc(ctx, "C", NULL));
4376 expr = isl_ast_expr_sub(expr3, expr);
4377 str = isl_ast_expr_to_str(expr);
4378 ok = str ? !strcmp(str, "C - (A + B)") : -1;
4379 free(str);
4380 isl_ast_expr_free(expr);
4382 if (ok < 0)
4383 return -1;
4384 if (!ok)
4385 isl_die(ctx, isl_error_unknown,
4386 "isl_ast_expr printed incorrectly", return -1);
4388 return 0;
4391 /* Check that isl_ast_build_expr_from_set returns a valid expression
4392 * for an empty set. Note that isl_ast_build_expr_from_set getting
4393 * called on an empty set probably indicates a bug in the caller.
4395 static int test_ast_build(isl_ctx *ctx)
4397 isl_set *set;
4398 isl_ast_build *build;
4399 isl_ast_expr *expr;
4401 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4402 build = isl_ast_build_from_context(set);
4404 set = isl_set_empty(isl_space_params_alloc(ctx, 0));
4405 expr = isl_ast_build_expr_from_set(build, set);
4407 isl_ast_expr_free(expr);
4408 isl_ast_build_free(build);
4410 if (!expr)
4411 return -1;
4413 return 0;
4416 /* Internal data structure for before_for and after_for callbacks.
4418 * depth is the current depth
4419 * before is the number of times before_for has been called
4420 * after is the number of times after_for has been called
4422 struct isl_test_codegen_data {
4423 int depth;
4424 int before;
4425 int after;
4428 /* This function is called before each for loop in the AST generated
4429 * from test_ast_gen1.
4431 * Increment the number of calls and the depth.
4432 * Check that the space returned by isl_ast_build_get_schedule_space
4433 * matches the target space of the schedule returned by
4434 * isl_ast_build_get_schedule.
4435 * Return an isl_id that is checked by the corresponding call
4436 * to after_for.
4438 static __isl_give isl_id *before_for(__isl_keep isl_ast_build *build,
4439 void *user)
4441 struct isl_test_codegen_data *data = user;
4442 isl_ctx *ctx;
4443 isl_space *space;
4444 isl_union_map *schedule;
4445 isl_union_set *uset;
4446 isl_set *set;
4447 int empty;
4448 char name[] = "d0";
4450 ctx = isl_ast_build_get_ctx(build);
4452 if (data->before >= 3)
4453 isl_die(ctx, isl_error_unknown,
4454 "unexpected number of for nodes", return NULL);
4455 if (data->depth >= 2)
4456 isl_die(ctx, isl_error_unknown,
4457 "unexpected depth", return NULL);
4459 snprintf(name, sizeof(name), "d%d", data->depth);
4460 data->before++;
4461 data->depth++;
4463 schedule = isl_ast_build_get_schedule(build);
4464 uset = isl_union_map_range(schedule);
4465 if (!uset)
4466 return NULL;
4467 if (isl_union_set_n_set(uset) != 1) {
4468 isl_union_set_free(uset);
4469 isl_die(ctx, isl_error_unknown,
4470 "expecting single range space", return NULL);
4473 space = isl_ast_build_get_schedule_space(build);
4474 set = isl_union_set_extract_set(uset, space);
4475 isl_union_set_free(uset);
4476 empty = isl_set_is_empty(set);
4477 isl_set_free(set);
4479 if (empty < 0)
4480 return NULL;
4481 if (empty)
4482 isl_die(ctx, isl_error_unknown,
4483 "spaces don't match", return NULL);
4485 return isl_id_alloc(ctx, name, NULL);
4488 /* This function is called after each for loop in the AST generated
4489 * from test_ast_gen1.
4491 * Increment the number of calls and decrement the depth.
4492 * Check that the annotation attached to the node matches
4493 * the isl_id returned by the corresponding call to before_for.
4495 static __isl_give isl_ast_node *after_for(__isl_take isl_ast_node *node,
4496 __isl_keep isl_ast_build *build, void *user)
4498 struct isl_test_codegen_data *data = user;
4499 isl_id *id;
4500 const char *name;
4501 int valid;
4503 data->after++;
4504 data->depth--;
4506 if (data->after > data->before)
4507 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
4508 "mismatch in number of for nodes",
4509 return isl_ast_node_free(node));
4511 id = isl_ast_node_get_annotation(node);
4512 if (!id)
4513 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
4514 "missing annotation", return isl_ast_node_free(node));
4516 name = isl_id_get_name(id);
4517 valid = name && atoi(name + 1) == data->depth;
4518 isl_id_free(id);
4520 if (!valid)
4521 isl_die(isl_ast_node_get_ctx(node), isl_error_unknown,
4522 "wrong annotation", return isl_ast_node_free(node));
4524 return node;
4527 /* Check that the before_each_for and after_each_for callbacks
4528 * are called for each for loop in the generated code,
4529 * that they are called in the right order and that the isl_id
4530 * returned from the before_each_for callback is attached to
4531 * the isl_ast_node passed to the corresponding after_each_for call.
4533 static int test_ast_gen1(isl_ctx *ctx)
4535 const char *str;
4536 isl_set *set;
4537 isl_union_map *schedule;
4538 isl_ast_build *build;
4539 isl_ast_node *tree;
4540 struct isl_test_codegen_data data;
4542 str = "[N] -> { : N >= 10 }";
4543 set = isl_set_read_from_str(ctx, str);
4544 str = "[N] -> { A[i,j] -> S[8,i,3,j] : 0 <= i,j <= N; "
4545 "B[i,j] -> S[8,j,9,i] : 0 <= i,j <= N }";
4546 schedule = isl_union_map_read_from_str(ctx, str);
4548 data.before = 0;
4549 data.after = 0;
4550 data.depth = 0;
4551 build = isl_ast_build_from_context(set);
4552 build = isl_ast_build_set_before_each_for(build,
4553 &before_for, &data);
4554 build = isl_ast_build_set_after_each_for(build,
4555 &after_for, &data);
4556 tree = isl_ast_build_ast_from_schedule(build, schedule);
4557 isl_ast_build_free(build);
4558 if (!tree)
4559 return -1;
4561 isl_ast_node_free(tree);
4563 if (data.before != 3 || data.after != 3)
4564 isl_die(ctx, isl_error_unknown,
4565 "unexpected number of for nodes", return -1);
4567 return 0;
4570 /* Check that the AST generator handles domains that are integrally disjoint
4571 * but not ratinoally disjoint.
4573 static int test_ast_gen2(isl_ctx *ctx)
4575 const char *str;
4576 isl_set *set;
4577 isl_union_map *schedule;
4578 isl_union_map *options;
4579 isl_ast_build *build;
4580 isl_ast_node *tree;
4582 str = "{ A[i,j] -> [i,j] : 0 <= i,j <= 1 }";
4583 schedule = isl_union_map_read_from_str(ctx, str);
4584 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4585 build = isl_ast_build_from_context(set);
4587 str = "{ [i,j] -> atomic[1] : i + j = 1; [i,j] -> unroll[1] : i = j }";
4588 options = isl_union_map_read_from_str(ctx, str);
4589 build = isl_ast_build_set_options(build, options);
4590 tree = isl_ast_build_ast_from_schedule(build, schedule);
4591 isl_ast_build_free(build);
4592 if (!tree)
4593 return -1;
4594 isl_ast_node_free(tree);
4596 return 0;
4599 /* Increment *user on each call.
4601 static __isl_give isl_ast_node *count_domains(__isl_take isl_ast_node *node,
4602 __isl_keep isl_ast_build *build, void *user)
4604 int *n = user;
4606 (*n)++;
4608 return node;
4611 /* Test that unrolling tries to minimize the number of instances.
4612 * In particular, for the schedule given below, make sure it generates
4613 * 3 nodes (rather than 101).
4615 static int test_ast_gen3(isl_ctx *ctx)
4617 const char *str;
4618 isl_set *set;
4619 isl_union_map *schedule;
4620 isl_union_map *options;
4621 isl_ast_build *build;
4622 isl_ast_node *tree;
4623 int n_domain = 0;
4625 str = "[n] -> { A[i] -> [i] : 0 <= i <= 100 and n <= i <= n + 2 }";
4626 schedule = isl_union_map_read_from_str(ctx, str);
4627 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4629 str = "{ [i] -> unroll[0] }";
4630 options = isl_union_map_read_from_str(ctx, str);
4632 build = isl_ast_build_from_context(set);
4633 build = isl_ast_build_set_options(build, options);
4634 build = isl_ast_build_set_at_each_domain(build,
4635 &count_domains, &n_domain);
4636 tree = isl_ast_build_ast_from_schedule(build, schedule);
4637 isl_ast_build_free(build);
4638 if (!tree)
4639 return -1;
4641 isl_ast_node_free(tree);
4643 if (n_domain != 3)
4644 isl_die(ctx, isl_error_unknown,
4645 "unexpected number of for nodes", return -1);
4647 return 0;
4650 /* Check that if the ast_build_exploit_nested_bounds options is set,
4651 * we do not get an outer if node in the generated AST,
4652 * while we do get such an outer if node if the options is not set.
4654 static int test_ast_gen4(isl_ctx *ctx)
4656 const char *str;
4657 isl_set *set;
4658 isl_union_map *schedule;
4659 isl_ast_build *build;
4660 isl_ast_node *tree;
4661 enum isl_ast_node_type type;
4662 int enb;
4664 enb = isl_options_get_ast_build_exploit_nested_bounds(ctx);
4665 str = "[N,M] -> { A[i,j] -> [i,j] : 0 <= i <= N and 0 <= j <= M }";
4667 isl_options_set_ast_build_exploit_nested_bounds(ctx, 1);
4669 schedule = isl_union_map_read_from_str(ctx, str);
4670 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4671 build = isl_ast_build_from_context(set);
4672 tree = isl_ast_build_ast_from_schedule(build, schedule);
4673 isl_ast_build_free(build);
4674 if (!tree)
4675 return -1;
4677 type = isl_ast_node_get_type(tree);
4678 isl_ast_node_free(tree);
4680 if (type == isl_ast_node_if)
4681 isl_die(ctx, isl_error_unknown,
4682 "not expecting if node", return -1);
4684 isl_options_set_ast_build_exploit_nested_bounds(ctx, 0);
4686 schedule = isl_union_map_read_from_str(ctx, str);
4687 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4688 build = isl_ast_build_from_context(set);
4689 tree = isl_ast_build_ast_from_schedule(build, schedule);
4690 isl_ast_build_free(build);
4691 if (!tree)
4692 return -1;
4694 type = isl_ast_node_get_type(tree);
4695 isl_ast_node_free(tree);
4697 if (type != isl_ast_node_if)
4698 isl_die(ctx, isl_error_unknown,
4699 "expecting if node", return -1);
4701 isl_options_set_ast_build_exploit_nested_bounds(ctx, enb);
4703 return 0;
4706 /* This function is called for each leaf in the AST generated
4707 * from test_ast_gen5.
4709 * We finalize the AST generation by extending the outer schedule
4710 * with a zero-dimensional schedule. If this results in any for loops,
4711 * then this means that we did not pass along enough information
4712 * about the outer schedule to the inner AST generation.
4714 static __isl_give isl_ast_node *create_leaf(__isl_take isl_ast_build *build,
4715 void *user)
4717 isl_union_map *schedule, *extra;
4718 isl_ast_node *tree;
4720 schedule = isl_ast_build_get_schedule(build);
4721 extra = isl_union_map_copy(schedule);
4722 extra = isl_union_map_from_domain(isl_union_map_domain(extra));
4723 schedule = isl_union_map_range_product(schedule, extra);
4724 tree = isl_ast_build_ast_from_schedule(build, schedule);
4725 isl_ast_build_free(build);
4727 if (!tree)
4728 return NULL;
4730 if (isl_ast_node_get_type(tree) == isl_ast_node_for)
4731 isl_die(isl_ast_node_get_ctx(tree), isl_error_unknown,
4732 "code should not contain any for loop",
4733 return isl_ast_node_free(tree));
4735 return tree;
4738 /* Check that we do not lose any information when going back and
4739 * forth between internal and external schedule.
4741 * In particular, we create an AST where we unroll the only
4742 * non-constant dimension in the schedule. We therefore do
4743 * not expect any for loops in the AST. However, older versions
4744 * of isl would not pass along enough information about the outer
4745 * schedule when performing an inner code generation from a create_leaf
4746 * callback, resulting in the inner code generation producing a for loop.
4748 static int test_ast_gen5(isl_ctx *ctx)
4750 const char *str;
4751 isl_set *set;
4752 isl_union_map *schedule, *options;
4753 isl_ast_build *build;
4754 isl_ast_node *tree;
4756 str = "{ A[] -> [1, 1, 2]; B[i] -> [1, i, 0] : i >= 1 and i <= 2 }";
4757 schedule = isl_union_map_read_from_str(ctx, str);
4759 str = "{ [a, b, c] -> unroll[1] : exists (e0 = [(a)/4]: "
4760 "4e0 >= -1 + a - b and 4e0 <= -2 + a + b) }";
4761 options = isl_union_map_read_from_str(ctx, str);
4763 set = isl_set_universe(isl_space_params_alloc(ctx, 0));
4764 build = isl_ast_build_from_context(set);
4765 build = isl_ast_build_set_options(build, options);
4766 build = isl_ast_build_set_create_leaf(build, &create_leaf, NULL);
4767 tree = isl_ast_build_ast_from_schedule(build, schedule);
4768 isl_ast_build_free(build);
4769 isl_ast_node_free(tree);
4770 if (!tree)
4771 return -1;
4773 return 0;
4776 static int test_ast_gen(isl_ctx *ctx)
4778 if (test_ast_gen1(ctx) < 0)
4779 return -1;
4780 if (test_ast_gen2(ctx) < 0)
4781 return -1;
4782 if (test_ast_gen3(ctx) < 0)
4783 return -1;
4784 if (test_ast_gen4(ctx) < 0)
4785 return -1;
4786 if (test_ast_gen5(ctx) < 0)
4787 return -1;
4788 return 0;
4791 /* Check if dropping output dimensions from an isl_pw_multi_aff
4792 * works properly.
4794 static int test_pw_multi_aff(isl_ctx *ctx)
4796 const char *str;
4797 isl_pw_multi_aff *pma1, *pma2;
4798 int equal;
4800 str = "{ [i,j] -> [i+j, 4i-j] }";
4801 pma1 = isl_pw_multi_aff_read_from_str(ctx, str);
4802 str = "{ [i,j] -> [4i-j] }";
4803 pma2 = isl_pw_multi_aff_read_from_str(ctx, str);
4805 pma1 = isl_pw_multi_aff_drop_dims(pma1, isl_dim_out, 0, 1);
4807 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
4809 isl_pw_multi_aff_free(pma1);
4810 isl_pw_multi_aff_free(pma2);
4811 if (equal < 0)
4812 return -1;
4813 if (!equal)
4814 isl_die(ctx, isl_error_unknown,
4815 "expressions not equal", return -1);
4817 return 0;
4820 /* Check that we can properly parse multi piecewise affine expressions
4821 * where the piecewise affine expressions have different domains.
4823 static int test_multi_pw_aff(isl_ctx *ctx)
4825 const char *str;
4826 isl_set *dom, *dom2;
4827 isl_multi_pw_aff *mpa1, *mpa2;
4828 isl_pw_aff *pa;
4829 int equal;
4830 int equal_domain;
4832 mpa1 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [i] }");
4833 dom = isl_set_read_from_str(ctx, "{ [i] : i > 0 }");
4834 mpa1 = isl_multi_pw_aff_intersect_domain(mpa1, dom);
4835 mpa2 = isl_multi_pw_aff_read_from_str(ctx, "{ [i] -> [2i] }");
4836 mpa2 = isl_multi_pw_aff_flat_range_product(mpa1, mpa2);
4837 str = "{ [i] -> [(i : i > 0), 2i] }";
4838 mpa1 = isl_multi_pw_aff_read_from_str(ctx, str);
4840 equal = isl_multi_pw_aff_plain_is_equal(mpa1, mpa2);
4842 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 0);
4843 dom = isl_pw_aff_domain(pa);
4844 pa = isl_multi_pw_aff_get_pw_aff(mpa1, 1);
4845 dom2 = isl_pw_aff_domain(pa);
4846 equal_domain = isl_set_is_equal(dom, dom2);
4848 isl_set_free(dom);
4849 isl_set_free(dom2);
4850 isl_multi_pw_aff_free(mpa1);
4851 isl_multi_pw_aff_free(mpa2);
4853 if (equal < 0)
4854 return -1;
4855 if (!equal)
4856 isl_die(ctx, isl_error_unknown,
4857 "expressions not equal", return -1);
4859 if (equal_domain < 0)
4860 return -1;
4861 if (equal_domain)
4862 isl_die(ctx, isl_error_unknown,
4863 "domains unexpectedly equal", return -1);
4865 return 0;
4868 /* This is a regression test for a bug where isl_basic_map_simplify
4869 * would end up in an infinite loop. In particular, we construct
4870 * an empty basic set that is not obviously empty.
4871 * isl_basic_set_is_empty marks the basic set as empty.
4872 * After projecting out i3, the variable can be dropped completely,
4873 * but isl_basic_map_simplify refrains from doing so if the basic set
4874 * is empty and would end up in an infinite loop if it didn't test
4875 * explicitly for empty basic maps in the outer loop.
4877 static int test_simplify(isl_ctx *ctx)
4879 const char *str;
4880 isl_basic_set *bset;
4881 int empty;
4883 str = "{ [i0, i1, i2, i3] : i0 >= -2 and 6i2 <= 4 + i0 + 5i1 and "
4884 "i2 <= 22 and 75i2 <= 111 + 13i0 + 60i1 and "
4885 "25i2 >= 38 + 6i0 + 20i1 and i0 <= -1 and i2 >= 20 and "
4886 "i3 >= i2 }";
4887 bset = isl_basic_set_read_from_str(ctx, str);
4888 empty = isl_basic_set_is_empty(bset);
4889 bset = isl_basic_set_project_out(bset, isl_dim_set, 3, 1);
4890 isl_basic_set_free(bset);
4891 if (!bset)
4892 return -1;
4893 if (!empty)
4894 isl_die(ctx, isl_error_unknown,
4895 "basic set should be empty", return -1);
4897 return 0;
4900 /* This is a regression test for a bug where isl_tab_basic_map_partial_lexopt
4901 * with gbr context would fail to disable the use of the shifted tableau
4902 * when transferring equalities for the input to the context, resulting
4903 * in invalid sample values.
4905 static int test_partial_lexmin(isl_ctx *ctx)
4907 const char *str;
4908 isl_basic_set *bset;
4909 isl_basic_map *bmap;
4910 isl_map *map;
4912 str = "{ [1, b, c, 1 - c] -> [e] : 2e <= -c and 2e >= -3 + c }";
4913 bmap = isl_basic_map_read_from_str(ctx, str);
4914 str = "{ [a, b, c, d] : c <= 1 and 2d >= 6 - 4b - c }";
4915 bset = isl_basic_set_read_from_str(ctx, str);
4916 map = isl_basic_map_partial_lexmin(bmap, bset, NULL);
4917 isl_map_free(map);
4919 if (!map)
4920 return -1;
4922 return 0;
4925 /* Check that the variable compression performed on the existentially
4926 * quantified variables inside isl_basic_set_compute_divs is not confused
4927 * by the implicit equalities among the parameters.
4929 static int test_compute_divs(isl_ctx *ctx)
4931 const char *str;
4932 isl_basic_set *bset;
4933 isl_set *set;
4935 str = "[a, b, c, d, e] -> { [] : exists (e0: 2d = b and a <= 124 and "
4936 "b <= 2046 and b >= 0 and b <= 60 + 64a and 2e >= b + 2c and "
4937 "2e >= b and 2e <= 1 + b and 2e <= 1 + b + 2c and "
4938 "32768e0 >= -124 + a and 2097152e0 <= 60 + 64a - b) }";
4939 bset = isl_basic_set_read_from_str(ctx, str);
4940 set = isl_basic_set_compute_divs(bset);
4941 isl_set_free(set);
4942 if (!set)
4943 return -1;
4945 return 0;
4948 struct {
4949 const char *set;
4950 const char *dual;
4951 } coef_tests[] = {
4952 { "{ rat: [i] : 0 <= i <= 10 }",
4953 "{ rat: coefficients[[cst] -> [a]] : cst >= 0 and 10a + cst >= 0 }" },
4954 { "{ rat: [i] : FALSE }",
4955 "{ rat: coefficients[[cst] -> [a]] }" },
4956 { "{ rat: [i] : }",
4957 "{ rat: coefficients[[cst] -> [0]] : cst >= 0 }" },
4960 struct {
4961 const char *set;
4962 const char *dual;
4963 } sol_tests[] = {
4964 { "{ rat: coefficients[[cst] -> [a]] : cst >= 0 and 10a + cst >= 0 }",
4965 "{ rat: [i] : 0 <= i <= 10 }" },
4966 { "{ rat: coefficients[[cst] -> [a]] : FALSE }",
4967 "{ rat: [i] }" },
4968 { "{ rat: coefficients[[cst] -> [a]] }",
4969 "{ rat: [i] : FALSE }" },
4972 /* Test the basic functionality of isl_basic_set_coefficients and
4973 * isl_basic_set_solutions.
4975 static int test_dual(isl_ctx *ctx)
4977 int i;
4979 for (i = 0; i < ARRAY_SIZE(coef_tests); ++i) {
4980 int equal;
4981 isl_basic_set *bset1, *bset2;
4983 bset1 = isl_basic_set_read_from_str(ctx, coef_tests[i].set);
4984 bset2 = isl_basic_set_read_from_str(ctx, coef_tests[i].dual);
4985 bset1 = isl_basic_set_coefficients(bset1);
4986 equal = isl_basic_set_is_equal(bset1, bset2);
4987 isl_basic_set_free(bset1);
4988 isl_basic_set_free(bset2);
4989 if (equal < 0)
4990 return -1;
4991 if (!equal)
4992 isl_die(ctx, isl_error_unknown,
4993 "incorrect dual", return -1);
4996 for (i = 0; i < ARRAY_SIZE(sol_tests); ++i) {
4997 int equal;
4998 isl_basic_set *bset1, *bset2;
5000 bset1 = isl_basic_set_read_from_str(ctx, sol_tests[i].set);
5001 bset2 = isl_basic_set_read_from_str(ctx, sol_tests[i].dual);
5002 bset1 = isl_basic_set_solutions(bset1);
5003 equal = isl_basic_set_is_equal(bset1, bset2);
5004 isl_basic_set_free(bset1);
5005 isl_basic_set_free(bset2);
5006 if (equal < 0)
5007 return -1;
5008 if (!equal)
5009 isl_die(ctx, isl_error_unknown,
5010 "incorrect dual", return -1);
5013 return 0;
5016 struct {
5017 const char *name;
5018 int (*fn)(isl_ctx *ctx);
5019 } tests [] = {
5020 { "dual", &test_dual },
5021 { "dependence analysis", &test_flow },
5022 { "val", &test_val },
5023 { "compute divs", &test_compute_divs },
5024 { "partial lexmin", &test_partial_lexmin },
5025 { "simplify", &test_simplify },
5026 { "curry", &test_curry },
5027 { "piecewise multi affine expressions", &test_pw_multi_aff },
5028 { "multi piecewise affine expressions", &test_multi_pw_aff },
5029 { "conversion", &test_conversion },
5030 { "list", &test_list },
5031 { "align parameters", &test_align_parameters },
5032 { "preimage", &test_preimage },
5033 { "pullback", &test_pullback },
5034 { "AST", &test_ast },
5035 { "AST build", &test_ast_build },
5036 { "AST generation", &test_ast_gen },
5037 { "eliminate", &test_eliminate },
5038 { "residue class", &test_residue_class },
5039 { "div", &test_div },
5040 { "slice", &test_slice },
5041 { "fixed power", &test_fixed_power },
5042 { "sample", &test_sample },
5043 { "output", &test_output },
5044 { "vertices", &test_vertices },
5045 { "fixed", &test_fixed },
5046 { "equal", &test_equal },
5047 { "disjoint", &test_disjoint },
5048 { "product", &test_product },
5049 { "dim_max", &test_dim_max },
5050 { "affine", &test_aff },
5051 { "injective", &test_injective },
5052 { "schedule", &test_schedule },
5053 { "union_pw", &test_union_pw },
5054 { "parse", &test_parse },
5055 { "single-valued", &test_sv },
5056 { "affine hull", &test_affine_hull },
5057 { "coalesce", &test_coalesce },
5058 { "factorize", &test_factorize },
5059 { "subset", &test_subset },
5060 { "subtract", &test_subtract },
5061 { "lexmin", &test_lexmin },
5062 { "min", &test_min },
5063 { "gist", &test_gist },
5064 { "piecewise quasi-polynomials", &test_pwqp },
5067 int main(int argc, char **argv)
5069 int i;
5070 struct isl_ctx *ctx;
5071 struct isl_options *options;
5073 srcdir = getenv("srcdir");
5074 assert(srcdir);
5076 options = isl_options_new_with_defaults();
5077 assert(options);
5078 argc = isl_options_parse(options, argc, argv, ISL_ARG_ALL);
5080 ctx = isl_ctx_alloc_with_options(&isl_options_args, options);
5081 for (i = 0; i < ARRAY_SIZE(tests); ++i) {
5082 printf("%s\n", tests[i].name);
5083 if (tests[i].fn(ctx) < 0)
5084 goto error;
5086 test_lift(ctx);
5087 test_bound(ctx);
5088 test_union(ctx);
5089 test_split_periods(ctx);
5090 test_lex(ctx);
5091 test_bijective(ctx);
5092 test_dep(ctx);
5093 test_read(ctx);
5094 test_bounded(ctx);
5095 test_construction(ctx);
5096 test_dim(ctx);
5097 test_application(ctx);
5098 test_convex_hull(ctx);
5099 test_closure(ctx);
5100 isl_ctx_free(ctx);
5101 return 0;
5102 error:
5103 isl_ctx_free(ctx);
5104 return -1;