blob: 5d22aa2d5f9960ab7f8947328c3a1608586c124e [file] [log] [blame]
Tobias Grosser52a25232015-02-04 20:55:43 +00001/*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2014 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
Tobias Grosserccae1ee2016-12-22 23:08:57 +00006 * Copyright 2016 INRIA Paris
Michael Krusee0b34f32016-05-04 14:41:36 +00007 * Copyright 2016 Sven Verdoolaege
Tobias Grosser52a25232015-02-04 20:55:43 +00008 *
9 * Use of this software is governed by the MIT license
10 *
11 * Written by Sven Verdoolaege, K.U.Leuven, Departement
12 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
13 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
14 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
15 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
16 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
17 * B.P. 105 - 78153 Le Chesnay, France
Tobias Grosserccae1ee2016-12-22 23:08:57 +000018 * and Centre de Recherche Inria de Paris, 2 rue Simone Iff - Voie DQ12,
19 * CS 42112, 75589 Paris Cedex 12, France
Tobias Grosser52a25232015-02-04 20:55:43 +000020 */
21
22#include <string.h>
23#include <isl_ctx_private.h>
24#include <isl_map_private.h>
25#include <isl_blk.h>
Tobias Grosser3e6070e2015-05-09 09:37:30 +000026#include <isl/constraint.h>
Tobias Grosser52a25232015-02-04 20:55:43 +000027#include "isl_space_private.h"
28#include "isl_equalities.h"
29#include <isl_lp_private.h>
30#include <isl_seq.h>
31#include <isl/set.h>
32#include <isl/map.h>
33#include <isl_reordering.h>
34#include "isl_sample.h"
35#include <isl_sort.h>
36#include "isl_tab.h"
37#include <isl/vec.h>
38#include <isl_mat_private.h>
39#include <isl_vec_private.h>
40#include <isl_dim_map.h>
41#include <isl_local_space_private.h>
42#include <isl_aff_private.h>
43#include <isl_options_private.h>
44#include <isl_morph.h>
45#include <isl_val_private.h>
46#include <isl/deprecated/map_int.h>
47#include <isl/deprecated/set_int.h>
48
Tobias Grosser06e15922016-11-16 11:06:47 +000049#include <bset_to_bmap.c>
50#include <bset_from_bmap.c>
51#include <set_to_map.c>
52#include <set_from_map.c>
53
Tobias Grosser52a25232015-02-04 20:55:43 +000054static unsigned n(__isl_keep isl_space *dim, enum isl_dim_type type)
55{
56 switch (type) {
57 case isl_dim_param: return dim->nparam;
58 case isl_dim_in: return dim->n_in;
59 case isl_dim_out: return dim->n_out;
60 case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
61 default: return 0;
62 }
63}
64
65static unsigned pos(__isl_keep isl_space *dim, enum isl_dim_type type)
66{
67 switch (type) {
68 case isl_dim_param: return 1;
69 case isl_dim_in: return 1 + dim->nparam;
70 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
71 default: return 0;
72 }
73}
74
75unsigned isl_basic_map_dim(__isl_keep isl_basic_map *bmap,
76 enum isl_dim_type type)
77{
78 if (!bmap)
79 return 0;
80 switch (type) {
81 case isl_dim_cst: return 1;
82 case isl_dim_param:
83 case isl_dim_in:
84 case isl_dim_out: return isl_space_dim(bmap->dim, type);
85 case isl_dim_div: return bmap->n_div;
86 case isl_dim_all: return isl_basic_map_total_dim(bmap);
87 default: return 0;
88 }
89}
90
91unsigned isl_map_dim(__isl_keep isl_map *map, enum isl_dim_type type)
92{
93 return map ? n(map->dim, type) : 0;
94}
95
96unsigned isl_set_dim(__isl_keep isl_set *set, enum isl_dim_type type)
97{
98 return set ? n(set->dim, type) : 0;
99}
100
101unsigned isl_basic_map_offset(struct isl_basic_map *bmap,
102 enum isl_dim_type type)
103{
Michael Kruseffb32782016-08-17 15:24:45 +0000104 isl_space *space;
105
106 if (!bmap)
107 return 0;
108
109 space = bmap->dim;
Tobias Grosser52a25232015-02-04 20:55:43 +0000110 switch (type) {
111 case isl_dim_cst: return 0;
112 case isl_dim_param: return 1;
Michael Kruseffb32782016-08-17 15:24:45 +0000113 case isl_dim_in: return 1 + space->nparam;
114 case isl_dim_out: return 1 + space->nparam + space->n_in;
115 case isl_dim_div: return 1 + space->nparam + space->n_in +
116 space->n_out;
Tobias Grosser52a25232015-02-04 20:55:43 +0000117 default: return 0;
118 }
119}
120
121unsigned isl_basic_set_offset(struct isl_basic_set *bset,
122 enum isl_dim_type type)
123{
124 return isl_basic_map_offset(bset, type);
125}
126
127static unsigned map_offset(struct isl_map *map, enum isl_dim_type type)
128{
129 return pos(map->dim, type);
130}
131
132unsigned isl_basic_set_dim(__isl_keep isl_basic_set *bset,
133 enum isl_dim_type type)
134{
135 return isl_basic_map_dim(bset, type);
136}
137
138unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set *bset)
139{
140 return isl_basic_set_dim(bset, isl_dim_set);
141}
142
143unsigned isl_basic_set_n_param(__isl_keep isl_basic_set *bset)
144{
145 return isl_basic_set_dim(bset, isl_dim_param);
146}
147
148unsigned isl_basic_set_total_dim(const struct isl_basic_set *bset)
149{
150 if (!bset)
151 return 0;
152 return isl_space_dim(bset->dim, isl_dim_all) + bset->n_div;
153}
154
155unsigned isl_set_n_dim(__isl_keep isl_set *set)
156{
157 return isl_set_dim(set, isl_dim_set);
158}
159
160unsigned isl_set_n_param(__isl_keep isl_set *set)
161{
162 return isl_set_dim(set, isl_dim_param);
163}
164
165unsigned isl_basic_map_n_in(const struct isl_basic_map *bmap)
166{
167 return bmap ? bmap->dim->n_in : 0;
168}
169
170unsigned isl_basic_map_n_out(const struct isl_basic_map *bmap)
171{
172 return bmap ? bmap->dim->n_out : 0;
173}
174
175unsigned isl_basic_map_n_param(const struct isl_basic_map *bmap)
176{
177 return bmap ? bmap->dim->nparam : 0;
178}
179
180unsigned isl_basic_map_n_div(const struct isl_basic_map *bmap)
181{
182 return bmap ? bmap->n_div : 0;
183}
184
185unsigned isl_basic_map_total_dim(const struct isl_basic_map *bmap)
186{
187 return bmap ? isl_space_dim(bmap->dim, isl_dim_all) + bmap->n_div : 0;
188}
189
190unsigned isl_map_n_in(const struct isl_map *map)
191{
192 return map ? map->dim->n_in : 0;
193}
194
195unsigned isl_map_n_out(const struct isl_map *map)
196{
197 return map ? map->dim->n_out : 0;
198}
199
200unsigned isl_map_n_param(const struct isl_map *map)
201{
202 return map ? map->dim->nparam : 0;
203}
204
205int isl_map_compatible_domain(struct isl_map *map, struct isl_set *set)
206{
207 int m;
208 if (!map || !set)
209 return -1;
210 m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
211 if (m < 0 || !m)
212 return m;
213 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
214 set->dim, isl_dim_set);
215}
216
Tobias Grosser932ec012016-07-06 09:11:00 +0000217isl_bool isl_basic_map_compatible_domain(__isl_keep isl_basic_map *bmap,
218 __isl_keep isl_basic_set *bset)
Tobias Grosser52a25232015-02-04 20:55:43 +0000219{
Tobias Grosser932ec012016-07-06 09:11:00 +0000220 isl_bool m;
Tobias Grosser52a25232015-02-04 20:55:43 +0000221 if (!bmap || !bset)
Tobias Grosser932ec012016-07-06 09:11:00 +0000222 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +0000223 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
224 if (m < 0 || !m)
225 return m;
226 return isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
227 bset->dim, isl_dim_set);
228}
229
230int isl_map_compatible_range(__isl_keep isl_map *map, __isl_keep isl_set *set)
231{
232 int m;
233 if (!map || !set)
234 return -1;
235 m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
236 if (m < 0 || !m)
237 return m;
238 return isl_space_tuple_is_equal(map->dim, isl_dim_out,
239 set->dim, isl_dim_set);
240}
241
242int isl_basic_map_compatible_range(struct isl_basic_map *bmap,
243 struct isl_basic_set *bset)
244{
245 int m;
246 if (!bmap || !bset)
247 return -1;
248 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
249 if (m < 0 || !m)
250 return m;
251 return isl_space_tuple_is_equal(bmap->dim, isl_dim_out,
252 bset->dim, isl_dim_set);
253}
254
255isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
256{
257 return bmap ? bmap->ctx : NULL;
258}
259
260isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
261{
262 return bset ? bset->ctx : NULL;
263}
264
265isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
266{
267 return map ? map->ctx : NULL;
268}
269
270isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
271{
272 return set ? set->ctx : NULL;
273}
274
275__isl_give isl_space *isl_basic_map_get_space(__isl_keep isl_basic_map *bmap)
276{
277 if (!bmap)
278 return NULL;
279 return isl_space_copy(bmap->dim);
280}
281
282__isl_give isl_space *isl_basic_set_get_space(__isl_keep isl_basic_set *bset)
283{
284 if (!bset)
285 return NULL;
286 return isl_space_copy(bset->dim);
287}
288
289/* Extract the divs in "bmap" as a matrix.
290 */
291__isl_give isl_mat *isl_basic_map_get_divs(__isl_keep isl_basic_map *bmap)
292{
293 int i;
294 isl_ctx *ctx;
295 isl_mat *div;
296 unsigned total;
297 unsigned cols;
298
299 if (!bmap)
300 return NULL;
301
302 ctx = isl_basic_map_get_ctx(bmap);
303 total = isl_space_dim(bmap->dim, isl_dim_all);
304 cols = 1 + 1 + total + bmap->n_div;
305 div = isl_mat_alloc(ctx, bmap->n_div, cols);
306 if (!div)
307 return NULL;
308
309 for (i = 0; i < bmap->n_div; ++i)
310 isl_seq_cpy(div->row[i], bmap->div[i], cols);
311
312 return div;
313}
314
315/* Extract the divs in "bset" as a matrix.
316 */
317__isl_give isl_mat *isl_basic_set_get_divs(__isl_keep isl_basic_set *bset)
318{
319 return isl_basic_map_get_divs(bset);
320}
321
322__isl_give isl_local_space *isl_basic_map_get_local_space(
323 __isl_keep isl_basic_map *bmap)
324{
325 isl_mat *div;
326
327 if (!bmap)
328 return NULL;
329
330 div = isl_basic_map_get_divs(bmap);
331 return isl_local_space_alloc_div(isl_space_copy(bmap->dim), div);
332}
333
334__isl_give isl_local_space *isl_basic_set_get_local_space(
335 __isl_keep isl_basic_set *bset)
336{
337 return isl_basic_map_get_local_space(bset);
338}
339
Michael Kruse959a8dc2016-01-15 15:54:45 +0000340/* For each known div d = floor(f/m), add the constraints
341 *
342 * f - m d >= 0
Tobias Grosser07b20952016-06-12 04:30:40 +0000343 * -(f-(m-1)) + m d >= 0
Michael Kruse959a8dc2016-01-15 15:54:45 +0000344 *
345 * Do not finalize the result.
346 */
347static __isl_give isl_basic_map *add_known_div_constraints(
348 __isl_take isl_basic_map *bmap)
349{
350 int i;
351 unsigned n_div;
352
353 if (!bmap)
354 return NULL;
355 n_div = isl_basic_map_dim(bmap, isl_dim_div);
356 if (n_div == 0)
357 return bmap;
358 bmap = isl_basic_map_cow(bmap);
359 bmap = isl_basic_map_extend_constraints(bmap, 0, 2 * n_div);
360 if (!bmap)
361 return NULL;
362 for (i = 0; i < n_div; ++i) {
363 if (isl_int_is_zero(bmap->div[i][0]))
364 continue;
365 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
366 return isl_basic_map_free(bmap);
367 }
368
369 return bmap;
370}
371
Tobias Grosser52a25232015-02-04 20:55:43 +0000372__isl_give isl_basic_map *isl_basic_map_from_local_space(
373 __isl_take isl_local_space *ls)
374{
375 int i;
376 int n_div;
377 isl_basic_map *bmap;
378
379 if (!ls)
380 return NULL;
381
382 n_div = isl_local_space_dim(ls, isl_dim_div);
383 bmap = isl_basic_map_alloc_space(isl_local_space_get_space(ls),
384 n_div, 0, 2 * n_div);
385
386 for (i = 0; i < n_div; ++i)
387 if (isl_basic_map_alloc_div(bmap) < 0)
388 goto error;
389
Michael Kruse959a8dc2016-01-15 15:54:45 +0000390 for (i = 0; i < n_div; ++i)
Tobias Grosser52a25232015-02-04 20:55:43 +0000391 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
Michael Kruse959a8dc2016-01-15 15:54:45 +0000392 bmap = add_known_div_constraints(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +0000393
394 isl_local_space_free(ls);
395 return bmap;
396error:
397 isl_local_space_free(ls);
398 isl_basic_map_free(bmap);
399 return NULL;
400}
401
402__isl_give isl_basic_set *isl_basic_set_from_local_space(
403 __isl_take isl_local_space *ls)
404{
405 return isl_basic_map_from_local_space(ls);
406}
407
408__isl_give isl_space *isl_map_get_space(__isl_keep isl_map *map)
409{
410 if (!map)
411 return NULL;
412 return isl_space_copy(map->dim);
413}
414
415__isl_give isl_space *isl_set_get_space(__isl_keep isl_set *set)
416{
417 if (!set)
418 return NULL;
419 return isl_space_copy(set->dim);
420}
421
422__isl_give isl_basic_map *isl_basic_map_set_tuple_name(
423 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
424{
425 bmap = isl_basic_map_cow(bmap);
426 if (!bmap)
427 return NULL;
428 bmap->dim = isl_space_set_tuple_name(bmap->dim, type, s);
429 if (!bmap->dim)
430 goto error;
431 bmap = isl_basic_map_finalize(bmap);
432 return bmap;
433error:
434 isl_basic_map_free(bmap);
435 return NULL;
436}
437
438__isl_give isl_basic_set *isl_basic_set_set_tuple_name(
439 __isl_take isl_basic_set *bset, const char *s)
440{
441 return isl_basic_map_set_tuple_name(bset, isl_dim_set, s);
442}
443
444const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
445 enum isl_dim_type type)
446{
447 return bmap ? isl_space_get_tuple_name(bmap->dim, type) : NULL;
448}
449
450__isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
451 enum isl_dim_type type, const char *s)
452{
453 int i;
454
455 map = isl_map_cow(map);
456 if (!map)
457 return NULL;
458
459 map->dim = isl_space_set_tuple_name(map->dim, type, s);
460 if (!map->dim)
461 goto error;
462
463 for (i = 0; i < map->n; ++i) {
464 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
465 if (!map->p[i])
466 goto error;
467 }
468
469 return map;
470error:
471 isl_map_free(map);
472 return NULL;
473}
474
475/* Replace the identifier of the tuple of type "type" by "id".
476 */
477__isl_give isl_basic_map *isl_basic_map_set_tuple_id(
478 __isl_take isl_basic_map *bmap,
479 enum isl_dim_type type, __isl_take isl_id *id)
480{
481 bmap = isl_basic_map_cow(bmap);
482 if (!bmap)
483 goto error;
484 bmap->dim = isl_space_set_tuple_id(bmap->dim, type, id);
485 if (!bmap->dim)
486 return isl_basic_map_free(bmap);
487 bmap = isl_basic_map_finalize(bmap);
488 return bmap;
489error:
490 isl_id_free(id);
491 return NULL;
492}
493
494/* Replace the identifier of the tuple by "id".
495 */
496__isl_give isl_basic_set *isl_basic_set_set_tuple_id(
497 __isl_take isl_basic_set *bset, __isl_take isl_id *id)
498{
499 return isl_basic_map_set_tuple_id(bset, isl_dim_set, id);
500}
501
502/* Does the input or output tuple have a name?
503 */
Tobias Grosserb2f39922015-05-28 13:32:11 +0000504isl_bool isl_map_has_tuple_name(__isl_keep isl_map *map, enum isl_dim_type type)
Tobias Grosser52a25232015-02-04 20:55:43 +0000505{
Tobias Grosserb2f39922015-05-28 13:32:11 +0000506 return map ? isl_space_has_tuple_name(map->dim, type) : isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +0000507}
508
509const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
510 enum isl_dim_type type)
511{
512 return map ? isl_space_get_tuple_name(map->dim, type) : NULL;
513}
514
515__isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
516 const char *s)
517{
Tobias Grosser06e15922016-11-16 11:06:47 +0000518 return set_from_map(isl_map_set_tuple_name(set_to_map(set),
519 isl_dim_set, s));
Tobias Grosser52a25232015-02-04 20:55:43 +0000520}
521
522__isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map,
523 enum isl_dim_type type, __isl_take isl_id *id)
524{
525 map = isl_map_cow(map);
526 if (!map)
527 goto error;
528
529 map->dim = isl_space_set_tuple_id(map->dim, type, id);
530
531 return isl_map_reset_space(map, isl_space_copy(map->dim));
532error:
533 isl_id_free(id);
534 return NULL;
535}
536
537__isl_give isl_set *isl_set_set_tuple_id(__isl_take isl_set *set,
538 __isl_take isl_id *id)
539{
540 return isl_map_set_tuple_id(set, isl_dim_set, id);
541}
542
543__isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map,
544 enum isl_dim_type type)
545{
546 map = isl_map_cow(map);
547 if (!map)
548 return NULL;
549
550 map->dim = isl_space_reset_tuple_id(map->dim, type);
551
552 return isl_map_reset_space(map, isl_space_copy(map->dim));
553}
554
555__isl_give isl_set *isl_set_reset_tuple_id(__isl_take isl_set *set)
556{
557 return isl_map_reset_tuple_id(set, isl_dim_set);
558}
559
Tobias Grosserb2f39922015-05-28 13:32:11 +0000560isl_bool isl_map_has_tuple_id(__isl_keep isl_map *map, enum isl_dim_type type)
Tobias Grosser52a25232015-02-04 20:55:43 +0000561{
Tobias Grosserb2f39922015-05-28 13:32:11 +0000562 return map ? isl_space_has_tuple_id(map->dim, type) : isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +0000563}
564
565__isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map,
566 enum isl_dim_type type)
567{
568 return map ? isl_space_get_tuple_id(map->dim, type) : NULL;
569}
570
Tobias Grosserb2f39922015-05-28 13:32:11 +0000571isl_bool isl_set_has_tuple_id(__isl_keep isl_set *set)
Tobias Grosser52a25232015-02-04 20:55:43 +0000572{
573 return isl_map_has_tuple_id(set, isl_dim_set);
574}
575
576__isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_set *set)
577{
578 return isl_map_get_tuple_id(set, isl_dim_set);
579}
580
581/* Does the set tuple have a name?
582 */
Tobias Grosserb2f39922015-05-28 13:32:11 +0000583isl_bool isl_set_has_tuple_name(__isl_keep isl_set *set)
Tobias Grosser52a25232015-02-04 20:55:43 +0000584{
Tobias Grosserb2f39922015-05-28 13:32:11 +0000585 if (!set)
586 return isl_bool_error;
587 return isl_space_has_tuple_name(set->dim, isl_dim_set);
Tobias Grosser52a25232015-02-04 20:55:43 +0000588}
589
590
591const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
592{
593 return bset ? isl_space_get_tuple_name(bset->dim, isl_dim_set) : NULL;
594}
595
596const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
597{
598 return set ? isl_space_get_tuple_name(set->dim, isl_dim_set) : NULL;
599}
600
601const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
602 enum isl_dim_type type, unsigned pos)
603{
604 return bmap ? isl_space_get_dim_name(bmap->dim, type, pos) : NULL;
605}
606
607const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
608 enum isl_dim_type type, unsigned pos)
609{
610 return bset ? isl_space_get_dim_name(bset->dim, type, pos) : NULL;
611}
612
613/* Does the given dimension have a name?
614 */
Tobias Grosserb2f39922015-05-28 13:32:11 +0000615isl_bool isl_map_has_dim_name(__isl_keep isl_map *map,
Tobias Grosser52a25232015-02-04 20:55:43 +0000616 enum isl_dim_type type, unsigned pos)
617{
Tobias Grosserb2f39922015-05-28 13:32:11 +0000618 if (!map)
619 return isl_bool_error;
620 return isl_space_has_dim_name(map->dim, type, pos);
Tobias Grosser52a25232015-02-04 20:55:43 +0000621}
622
623const char *isl_map_get_dim_name(__isl_keep isl_map *map,
624 enum isl_dim_type type, unsigned pos)
625{
626 return map ? isl_space_get_dim_name(map->dim, type, pos) : NULL;
627}
628
629const char *isl_set_get_dim_name(__isl_keep isl_set *set,
630 enum isl_dim_type type, unsigned pos)
631{
632 return set ? isl_space_get_dim_name(set->dim, type, pos) : NULL;
633}
634
635/* Does the given dimension have a name?
636 */
Tobias Grosserb2f39922015-05-28 13:32:11 +0000637isl_bool isl_set_has_dim_name(__isl_keep isl_set *set,
Tobias Grosser52a25232015-02-04 20:55:43 +0000638 enum isl_dim_type type, unsigned pos)
639{
Tobias Grosserb2f39922015-05-28 13:32:11 +0000640 if (!set)
641 return isl_bool_error;
642 return isl_space_has_dim_name(set->dim, type, pos);
Tobias Grosser52a25232015-02-04 20:55:43 +0000643}
644
645__isl_give isl_basic_map *isl_basic_map_set_dim_name(
646 __isl_take isl_basic_map *bmap,
647 enum isl_dim_type type, unsigned pos, const char *s)
648{
649 bmap = isl_basic_map_cow(bmap);
650 if (!bmap)
651 return NULL;
652 bmap->dim = isl_space_set_dim_name(bmap->dim, type, pos, s);
653 if (!bmap->dim)
654 goto error;
655 return isl_basic_map_finalize(bmap);
656error:
657 isl_basic_map_free(bmap);
658 return NULL;
659}
660
661__isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
662 enum isl_dim_type type, unsigned pos, const char *s)
663{
664 int i;
665
666 map = isl_map_cow(map);
667 if (!map)
668 return NULL;
669
670 map->dim = isl_space_set_dim_name(map->dim, type, pos, s);
671 if (!map->dim)
672 goto error;
673
674 for (i = 0; i < map->n; ++i) {
675 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
676 if (!map->p[i])
677 goto error;
678 }
679
680 return map;
681error:
682 isl_map_free(map);
683 return NULL;
684}
685
686__isl_give isl_basic_set *isl_basic_set_set_dim_name(
687 __isl_take isl_basic_set *bset,
688 enum isl_dim_type type, unsigned pos, const char *s)
689{
Tobias Grosser06e15922016-11-16 11:06:47 +0000690 return bset_from_bmap(isl_basic_map_set_dim_name(bset_to_bmap(bset),
691 type, pos, s));
Tobias Grosser52a25232015-02-04 20:55:43 +0000692}
693
694__isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
695 enum isl_dim_type type, unsigned pos, const char *s)
696{
Tobias Grosser06e15922016-11-16 11:06:47 +0000697 return set_from_map(isl_map_set_dim_name(set_to_map(set),
698 type, pos, s));
Tobias Grosser52a25232015-02-04 20:55:43 +0000699}
700
Tobias Grosserb2f39922015-05-28 13:32:11 +0000701isl_bool isl_basic_map_has_dim_id(__isl_keep isl_basic_map *bmap,
Tobias Grosser52a25232015-02-04 20:55:43 +0000702 enum isl_dim_type type, unsigned pos)
703{
Tobias Grosserb2f39922015-05-28 13:32:11 +0000704 if (!bmap)
705 return isl_bool_error;
706 return isl_space_has_dim_id(bmap->dim, type, pos);
Tobias Grosser52a25232015-02-04 20:55:43 +0000707}
708
709__isl_give isl_id *isl_basic_set_get_dim_id(__isl_keep isl_basic_set *bset,
710 enum isl_dim_type type, unsigned pos)
711{
712 return bset ? isl_space_get_dim_id(bset->dim, type, pos) : NULL;
713}
714
Tobias Grosserb2f39922015-05-28 13:32:11 +0000715isl_bool isl_map_has_dim_id(__isl_keep isl_map *map,
Tobias Grosser52a25232015-02-04 20:55:43 +0000716 enum isl_dim_type type, unsigned pos)
717{
Tobias Grosserb2f39922015-05-28 13:32:11 +0000718 return map ? isl_space_has_dim_id(map->dim, type, pos) : isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +0000719}
720
721__isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
722 enum isl_dim_type type, unsigned pos)
723{
724 return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL;
725}
726
Tobias Grosserb2f39922015-05-28 13:32:11 +0000727isl_bool isl_set_has_dim_id(__isl_keep isl_set *set,
Tobias Grosser52a25232015-02-04 20:55:43 +0000728 enum isl_dim_type type, unsigned pos)
729{
730 return isl_map_has_dim_id(set, type, pos);
731}
732
733__isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set,
734 enum isl_dim_type type, unsigned pos)
735{
736 return isl_map_get_dim_id(set, type, pos);
737}
738
739__isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
740 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
741{
742 map = isl_map_cow(map);
743 if (!map)
744 goto error;
745
746 map->dim = isl_space_set_dim_id(map->dim, type, pos, id);
747
748 return isl_map_reset_space(map, isl_space_copy(map->dim));
749error:
750 isl_id_free(id);
751 return NULL;
752}
753
754__isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set,
755 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
756{
757 return isl_map_set_dim_id(set, type, pos, id);
758}
759
760int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
761 __isl_keep isl_id *id)
762{
763 if (!map)
764 return -1;
765 return isl_space_find_dim_by_id(map->dim, type, id);
766}
767
768int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type,
769 __isl_keep isl_id *id)
770{
771 return isl_map_find_dim_by_id(set, type, id);
772}
773
774/* Return the position of the dimension of the given type and name
775 * in "bmap".
776 * Return -1 if no such dimension can be found.
777 */
778int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map *bmap,
779 enum isl_dim_type type, const char *name)
780{
781 if (!bmap)
782 return -1;
783 return isl_space_find_dim_by_name(bmap->dim, type, name);
784}
785
786int isl_map_find_dim_by_name(__isl_keep isl_map *map, enum isl_dim_type type,
787 const char *name)
788{
789 if (!map)
790 return -1;
791 return isl_space_find_dim_by_name(map->dim, type, name);
792}
793
794int isl_set_find_dim_by_name(__isl_keep isl_set *set, enum isl_dim_type type,
795 const char *name)
796{
797 return isl_map_find_dim_by_name(set, type, name);
798}
799
800/* Reset the user pointer on all identifiers of parameters and tuples
801 * of the space of "map".
802 */
803__isl_give isl_map *isl_map_reset_user(__isl_take isl_map *map)
804{
805 isl_space *space;
806
807 space = isl_map_get_space(map);
808 space = isl_space_reset_user(space);
809 map = isl_map_reset_space(map, space);
810
811 return map;
812}
813
814/* Reset the user pointer on all identifiers of parameters and tuples
815 * of the space of "set".
816 */
817__isl_give isl_set *isl_set_reset_user(__isl_take isl_set *set)
818{
819 return isl_map_reset_user(set);
820}
821
822int isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
823{
824 if (!bmap)
825 return -1;
826 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
827}
828
Tobias Grosser5652c9c2016-10-01 19:46:51 +0000829/* Has "map" been marked as a rational map?
830 * In particular, have all basic maps in "map" been marked this way?
831 * An empty map is not considered to be rational.
832 * Maps where only some of the basic maps are marked rational
833 * are not allowed.
834 */
835isl_bool isl_map_is_rational(__isl_keep isl_map *map)
836{
837 int i;
838 isl_bool rational;
839
840 if (!map)
841 return isl_bool_error;
842 if (map->n == 0)
843 return isl_bool_false;
844 rational = isl_basic_map_is_rational(map->p[0]);
845 if (rational < 0)
846 return rational;
847 for (i = 1; i < map->n; ++i) {
848 isl_bool rational_i;
849
850 rational_i = isl_basic_map_is_rational(map->p[i]);
851 if (rational_i < 0)
852 return rational;
853 if (rational != rational_i)
854 isl_die(isl_map_get_ctx(map), isl_error_unsupported,
855 "mixed rational and integer basic maps "
856 "not supported", return isl_bool_error);
857 }
858
859 return rational;
860}
861
862/* Has "set" been marked as a rational set?
863 * In particular, have all basic set in "set" been marked this way?
864 * An empty set is not considered to be rational.
865 * Sets where only some of the basic sets are marked rational
866 * are not allowed.
867 */
868isl_bool isl_set_is_rational(__isl_keep isl_set *set)
869{
870 return isl_map_is_rational(set);
871}
872
Tobias Grosser52a25232015-02-04 20:55:43 +0000873int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
874{
875 return isl_basic_map_is_rational(bset);
876}
877
878/* Does "bmap" contain any rational points?
879 *
880 * If "bmap" has an equality for each dimension, equating the dimension
881 * to an integer constant, then it has no rational points, even if it
882 * is marked as rational.
883 */
884int isl_basic_map_has_rational(__isl_keep isl_basic_map *bmap)
885{
886 int has_rational = 1;
887 unsigned total;
888
889 if (!bmap)
890 return -1;
891 if (isl_basic_map_plain_is_empty(bmap))
892 return 0;
893 if (!isl_basic_map_is_rational(bmap))
894 return 0;
895 bmap = isl_basic_map_copy(bmap);
896 bmap = isl_basic_map_implicit_equalities(bmap);
897 if (!bmap)
898 return -1;
899 total = isl_basic_map_total_dim(bmap);
900 if (bmap->n_eq == total) {
901 int i, j;
902 for (i = 0; i < bmap->n_eq; ++i) {
903 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
904 if (j < 0)
905 break;
906 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
907 !isl_int_is_negone(bmap->eq[i][1 + j]))
908 break;
909 j = isl_seq_first_non_zero(bmap->eq[i] + 1 + j + 1,
910 total - j - 1);
911 if (j >= 0)
912 break;
913 }
914 if (i == bmap->n_eq)
915 has_rational = 0;
916 }
917 isl_basic_map_free(bmap);
918
919 return has_rational;
920}
921
922/* Does "map" contain any rational points?
923 */
924int isl_map_has_rational(__isl_keep isl_map *map)
925{
926 int i;
927 int has_rational;
928
929 if (!map)
930 return -1;
931 for (i = 0; i < map->n; ++i) {
932 has_rational = isl_basic_map_has_rational(map->p[i]);
933 if (has_rational < 0)
934 return -1;
935 if (has_rational)
936 return 1;
937 }
938 return 0;
939}
940
941/* Does "set" contain any rational points?
942 */
943int isl_set_has_rational(__isl_keep isl_set *set)
944{
945 return isl_map_has_rational(set);
946}
947
948/* Is this basic set a parameter domain?
949 */
950int isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
951{
952 if (!bset)
953 return -1;
954 return isl_space_is_params(bset->dim);
955}
956
957/* Is this set a parameter domain?
958 */
Tobias Grosserb2f39922015-05-28 13:32:11 +0000959isl_bool isl_set_is_params(__isl_keep isl_set *set)
Tobias Grosser52a25232015-02-04 20:55:43 +0000960{
961 if (!set)
Tobias Grosserb2f39922015-05-28 13:32:11 +0000962 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +0000963 return isl_space_is_params(set->dim);
964}
965
966/* Is this map actually a parameter domain?
967 * Users should never call this function. Outside of isl,
968 * a map can never be a parameter domain.
969 */
970int isl_map_is_params(__isl_keep isl_map *map)
971{
972 if (!map)
973 return -1;
974 return isl_space_is_params(map->dim);
975}
976
977static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
978 struct isl_basic_map *bmap, unsigned extra,
979 unsigned n_eq, unsigned n_ineq)
980{
981 int i;
982 size_t row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + extra;
983
984 bmap->ctx = ctx;
985 isl_ctx_ref(ctx);
986
987 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
988 if (isl_blk_is_error(bmap->block))
989 goto error;
990
991 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
992 if ((n_ineq + n_eq) && !bmap->ineq)
993 goto error;
994
995 if (extra == 0) {
996 bmap->block2 = isl_blk_empty();
997 bmap->div = NULL;
998 } else {
999 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
1000 if (isl_blk_is_error(bmap->block2))
1001 goto error;
1002
1003 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
1004 if (!bmap->div)
1005 goto error;
1006 }
1007
1008 for (i = 0; i < n_ineq + n_eq; ++i)
1009 bmap->ineq[i] = bmap->block.data + i * row_size;
1010
1011 for (i = 0; i < extra; ++i)
1012 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
1013
1014 bmap->ref = 1;
1015 bmap->flags = 0;
1016 bmap->c_size = n_eq + n_ineq;
1017 bmap->eq = bmap->ineq + n_ineq;
1018 bmap->extra = extra;
1019 bmap->n_eq = 0;
1020 bmap->n_ineq = 0;
1021 bmap->n_div = 0;
1022 bmap->sample = NULL;
1023
1024 return bmap;
1025error:
1026 isl_basic_map_free(bmap);
1027 return NULL;
1028}
1029
1030struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
1031 unsigned nparam, unsigned dim, unsigned extra,
1032 unsigned n_eq, unsigned n_ineq)
1033{
1034 struct isl_basic_map *bmap;
1035 isl_space *space;
1036
1037 space = isl_space_set_alloc(ctx, nparam, dim);
1038 if (!space)
1039 return NULL;
1040
1041 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
Tobias Grosser06e15922016-11-16 11:06:47 +00001042 return bset_from_bmap(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +00001043}
1044
1045struct isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *dim,
1046 unsigned extra, unsigned n_eq, unsigned n_ineq)
1047{
1048 struct isl_basic_map *bmap;
1049 if (!dim)
1050 return NULL;
1051 isl_assert(dim->ctx, dim->n_in == 0, goto error);
1052 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
Tobias Grosser06e15922016-11-16 11:06:47 +00001053 return bset_from_bmap(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +00001054error:
1055 isl_space_free(dim);
1056 return NULL;
1057}
1058
1059struct isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *dim,
1060 unsigned extra, unsigned n_eq, unsigned n_ineq)
1061{
1062 struct isl_basic_map *bmap;
1063
1064 if (!dim)
1065 return NULL;
1066 bmap = isl_calloc_type(dim->ctx, struct isl_basic_map);
1067 if (!bmap)
1068 goto error;
1069 bmap->dim = dim;
1070
1071 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
1072error:
1073 isl_space_free(dim);
1074 return NULL;
1075}
1076
1077struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
1078 unsigned nparam, unsigned in, unsigned out, unsigned extra,
1079 unsigned n_eq, unsigned n_ineq)
1080{
1081 struct isl_basic_map *bmap;
1082 isl_space *dim;
1083
1084 dim = isl_space_alloc(ctx, nparam, in, out);
1085 if (!dim)
1086 return NULL;
1087
1088 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1089 return bmap;
1090}
1091
1092static void dup_constraints(
1093 struct isl_basic_map *dst, struct isl_basic_map *src)
1094{
1095 int i;
1096 unsigned total = isl_basic_map_total_dim(src);
1097
1098 for (i = 0; i < src->n_eq; ++i) {
1099 int j = isl_basic_map_alloc_equality(dst);
1100 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
1101 }
1102
1103 for (i = 0; i < src->n_ineq; ++i) {
1104 int j = isl_basic_map_alloc_inequality(dst);
1105 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
1106 }
1107
1108 for (i = 0; i < src->n_div; ++i) {
1109 int j = isl_basic_map_alloc_div(dst);
1110 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
1111 }
1112 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
1113}
1114
1115struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
1116{
1117 struct isl_basic_map *dup;
1118
1119 if (!bmap)
1120 return NULL;
1121 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
1122 bmap->n_div, bmap->n_eq, bmap->n_ineq);
1123 if (!dup)
1124 return NULL;
1125 dup_constraints(dup, bmap);
1126 dup->flags = bmap->flags;
1127 dup->sample = isl_vec_copy(bmap->sample);
1128 return dup;
1129}
1130
1131struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
1132{
1133 struct isl_basic_map *dup;
1134
Tobias Grosser06e15922016-11-16 11:06:47 +00001135 dup = isl_basic_map_dup(bset_to_bmap(bset));
1136 return bset_from_bmap(dup);
Tobias Grosser52a25232015-02-04 20:55:43 +00001137}
1138
1139struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
1140{
1141 if (!bset)
1142 return NULL;
1143
1144 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
1145 bset->ref++;
1146 return bset;
1147 }
1148 return isl_basic_set_dup(bset);
1149}
1150
1151struct isl_set *isl_set_copy(struct isl_set *set)
1152{
1153 if (!set)
1154 return NULL;
1155
1156 set->ref++;
1157 return set;
1158}
1159
1160struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
1161{
1162 if (!bmap)
1163 return NULL;
1164
1165 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
1166 bmap->ref++;
1167 return bmap;
1168 }
1169 bmap = isl_basic_map_dup(bmap);
1170 if (bmap)
1171 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1172 return bmap;
1173}
1174
1175struct isl_map *isl_map_copy(struct isl_map *map)
1176{
1177 if (!map)
1178 return NULL;
1179
1180 map->ref++;
1181 return map;
1182}
1183
1184__isl_null isl_basic_map *isl_basic_map_free(__isl_take isl_basic_map *bmap)
1185{
1186 if (!bmap)
1187 return NULL;
1188
1189 if (--bmap->ref > 0)
1190 return NULL;
1191
1192 isl_ctx_deref(bmap->ctx);
1193 free(bmap->div);
1194 isl_blk_free(bmap->ctx, bmap->block2);
1195 free(bmap->ineq);
1196 isl_blk_free(bmap->ctx, bmap->block);
1197 isl_vec_free(bmap->sample);
1198 isl_space_free(bmap->dim);
1199 free(bmap);
1200
1201 return NULL;
1202}
1203
1204__isl_null isl_basic_set *isl_basic_set_free(__isl_take isl_basic_set *bset)
1205{
Tobias Grosser06e15922016-11-16 11:06:47 +00001206 return isl_basic_map_free(bset_to_bmap(bset));
Tobias Grosser52a25232015-02-04 20:55:43 +00001207}
1208
1209static int room_for_con(struct isl_basic_map *bmap, unsigned n)
1210{
1211 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
1212}
1213
1214__isl_give isl_map *isl_map_align_params_map_map_and(
1215 __isl_take isl_map *map1, __isl_take isl_map *map2,
1216 __isl_give isl_map *(*fn)(__isl_take isl_map *map1,
1217 __isl_take isl_map *map2))
1218{
1219 if (!map1 || !map2)
1220 goto error;
1221 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
1222 return fn(map1, map2);
1223 if (!isl_space_has_named_params(map1->dim) ||
1224 !isl_space_has_named_params(map2->dim))
1225 isl_die(map1->ctx, isl_error_invalid,
1226 "unaligned unnamed parameters", goto error);
1227 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1228 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1229 return fn(map1, map2);
1230error:
1231 isl_map_free(map1);
1232 isl_map_free(map2);
1233 return NULL;
1234}
1235
Tobias Grosserb2f39922015-05-28 13:32:11 +00001236isl_bool isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1,
Tobias Grosser52a25232015-02-04 20:55:43 +00001237 __isl_keep isl_map *map2,
Tobias Grosserb2f39922015-05-28 13:32:11 +00001238 isl_bool (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
Tobias Grosser52a25232015-02-04 20:55:43 +00001239{
Tobias Grosserb2f39922015-05-28 13:32:11 +00001240 isl_bool r;
Tobias Grosser52a25232015-02-04 20:55:43 +00001241
1242 if (!map1 || !map2)
Tobias Grosserb2f39922015-05-28 13:32:11 +00001243 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00001244 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
1245 return fn(map1, map2);
1246 if (!isl_space_has_named_params(map1->dim) ||
1247 !isl_space_has_named_params(map2->dim))
1248 isl_die(map1->ctx, isl_error_invalid,
Tobias Grosserb2f39922015-05-28 13:32:11 +00001249 "unaligned unnamed parameters", return isl_bool_error);
Tobias Grosser52a25232015-02-04 20:55:43 +00001250 map1 = isl_map_copy(map1);
1251 map2 = isl_map_copy(map2);
1252 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1253 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1254 r = fn(map1, map2);
1255 isl_map_free(map1);
1256 isl_map_free(map2);
1257 return r;
1258}
1259
1260int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
1261{
1262 struct isl_ctx *ctx;
1263 if (!bmap)
1264 return -1;
1265 ctx = bmap->ctx;
1266 isl_assert(ctx, room_for_con(bmap, 1), return -1);
1267 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
1268 return -1);
1269 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1270 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1271 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1272 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1273 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1274 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
1275 isl_int *t;
1276 int j = isl_basic_map_alloc_inequality(bmap);
1277 if (j < 0)
1278 return -1;
1279 t = bmap->ineq[j];
1280 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
1281 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1282 bmap->eq[-1] = t;
1283 bmap->n_eq++;
1284 bmap->n_ineq--;
1285 bmap->eq--;
1286 return 0;
1287 }
1288 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
1289 bmap->extra - bmap->n_div);
1290 return bmap->n_eq++;
1291}
1292
1293int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
1294{
Tobias Grosser06e15922016-11-16 11:06:47 +00001295 return isl_basic_map_alloc_equality(bset_to_bmap(bset));
Tobias Grosser52a25232015-02-04 20:55:43 +00001296}
1297
1298int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
1299{
1300 if (!bmap)
1301 return -1;
1302 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
1303 bmap->n_eq -= n;
1304 return 0;
1305}
1306
1307int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
1308{
Tobias Grosser06e15922016-11-16 11:06:47 +00001309 return isl_basic_map_free_equality(bset_to_bmap(bset), n);
Tobias Grosser52a25232015-02-04 20:55:43 +00001310}
1311
1312int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
1313{
1314 isl_int *t;
1315 if (!bmap)
1316 return -1;
1317 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
1318
1319 if (pos != bmap->n_eq - 1) {
1320 t = bmap->eq[pos];
1321 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
1322 bmap->eq[bmap->n_eq - 1] = t;
1323 }
1324 bmap->n_eq--;
1325 return 0;
1326}
1327
1328int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
1329{
Tobias Grosser06e15922016-11-16 11:06:47 +00001330 return isl_basic_map_drop_equality(bset_to_bmap(bset), pos);
Tobias Grosser52a25232015-02-04 20:55:43 +00001331}
1332
1333/* Turn inequality "pos" of "bmap" into an equality.
1334 *
1335 * In particular, we move the inequality in front of the equalities
1336 * and move the last inequality in the position of the moved inequality.
1337 * Note that isl_tab_make_equalities_explicit depends on this particular
1338 * change in the ordering of the constraints.
1339 */
1340void isl_basic_map_inequality_to_equality(
1341 struct isl_basic_map *bmap, unsigned pos)
1342{
1343 isl_int *t;
1344
1345 t = bmap->ineq[pos];
1346 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1347 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1348 bmap->eq[-1] = t;
1349 bmap->n_eq++;
1350 bmap->n_ineq--;
1351 bmap->eq--;
1352 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1353 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1354 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1355 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1356}
1357
1358static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
1359{
1360 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1361}
1362
1363int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
1364{
1365 struct isl_ctx *ctx;
1366 if (!bmap)
1367 return -1;
1368 ctx = bmap->ctx;
1369 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1370 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1371 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1372 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1373 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1374 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
1375 1 + isl_basic_map_total_dim(bmap),
1376 bmap->extra - bmap->n_div);
1377 return bmap->n_ineq++;
1378}
1379
1380int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
1381{
Tobias Grosser06e15922016-11-16 11:06:47 +00001382 return isl_basic_map_alloc_inequality(bset_to_bmap(bset));
Tobias Grosser52a25232015-02-04 20:55:43 +00001383}
1384
1385int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
1386{
1387 if (!bmap)
1388 return -1;
1389 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
1390 bmap->n_ineq -= n;
1391 return 0;
1392}
1393
1394int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
1395{
Tobias Grosser06e15922016-11-16 11:06:47 +00001396 return isl_basic_map_free_inequality(bset_to_bmap(bset), n);
Tobias Grosser52a25232015-02-04 20:55:43 +00001397}
1398
1399int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
1400{
1401 isl_int *t;
1402 if (!bmap)
1403 return -1;
1404 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1405
1406 if (pos != bmap->n_ineq - 1) {
1407 t = bmap->ineq[pos];
1408 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1409 bmap->ineq[bmap->n_ineq - 1] = t;
1410 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1411 }
1412 bmap->n_ineq--;
1413 return 0;
1414}
1415
1416int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
1417{
Tobias Grosser06e15922016-11-16 11:06:47 +00001418 return isl_basic_map_drop_inequality(bset_to_bmap(bset), pos);
Tobias Grosser52a25232015-02-04 20:55:43 +00001419}
1420
1421__isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1422 isl_int *eq)
1423{
1424 int k;
1425
1426 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1427 if (!bmap)
1428 return NULL;
1429 k = isl_basic_map_alloc_equality(bmap);
1430 if (k < 0)
1431 goto error;
1432 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
1433 return bmap;
1434error:
1435 isl_basic_map_free(bmap);
1436 return NULL;
1437}
1438
1439__isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1440 isl_int *eq)
1441{
Tobias Grosser06e15922016-11-16 11:06:47 +00001442 return bset_from_bmap(isl_basic_map_add_eq(bset_to_bmap(bset), eq));
Tobias Grosser52a25232015-02-04 20:55:43 +00001443}
1444
1445__isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1446 isl_int *ineq)
1447{
1448 int k;
1449
1450 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1451 if (!bmap)
1452 return NULL;
1453 k = isl_basic_map_alloc_inequality(bmap);
1454 if (k < 0)
1455 goto error;
1456 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
1457 return bmap;
1458error:
1459 isl_basic_map_free(bmap);
1460 return NULL;
1461}
1462
1463__isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1464 isl_int *ineq)
1465{
Tobias Grosser06e15922016-11-16 11:06:47 +00001466 return bset_from_bmap(isl_basic_map_add_ineq(bset_to_bmap(bset), ineq));
Tobias Grosser52a25232015-02-04 20:55:43 +00001467}
1468
1469int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
1470{
1471 if (!bmap)
1472 return -1;
1473 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1474 isl_seq_clr(bmap->div[bmap->n_div] +
1475 1 + 1 + isl_basic_map_total_dim(bmap),
1476 bmap->extra - bmap->n_div);
1477 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1478 return bmap->n_div++;
1479}
1480
1481int isl_basic_set_alloc_div(struct isl_basic_set *bset)
1482{
Tobias Grosser06e15922016-11-16 11:06:47 +00001483 return isl_basic_map_alloc_div(bset_to_bmap(bset));
Tobias Grosser52a25232015-02-04 20:55:43 +00001484}
1485
Tobias Grosser9ec4f952016-07-20 16:53:07 +00001486/* Insert an extra integer division, prescribed by "div", to "bmap"
1487 * at (integer division) position "pos".
1488 *
1489 * The integer division is first added at the end and then moved
1490 * into the right position.
1491 */
1492__isl_give isl_basic_map *isl_basic_map_insert_div(
1493 __isl_take isl_basic_map *bmap, int pos, __isl_keep isl_vec *div)
1494{
1495 int i, k, n_div;
1496
1497 bmap = isl_basic_map_cow(bmap);
1498 if (!bmap || !div)
1499 return isl_basic_map_free(bmap);
1500
1501 if (div->size != 1 + 1 + isl_basic_map_dim(bmap, isl_dim_all))
1502 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1503 "unexpected size", return isl_basic_map_free(bmap));
1504 n_div = isl_basic_map_dim(bmap, isl_dim_div);
1505 if (pos < 0 || pos > n_div)
1506 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1507 "invalid position", return isl_basic_map_free(bmap));
1508
1509 bmap = isl_basic_map_extend_space(bmap,
1510 isl_basic_map_get_space(bmap), 1, 0, 2);
1511 k = isl_basic_map_alloc_div(bmap);
1512 if (k < 0)
1513 return isl_basic_map_free(bmap);
1514 isl_seq_cpy(bmap->div[k], div->el, div->size);
1515 isl_int_set_si(bmap->div[k][div->size], 0);
1516
1517 for (i = k; i > pos; --i)
1518 isl_basic_map_swap_div(bmap, i, i - 1);
1519
1520 return bmap;
1521}
1522
Tobias Grosser52a25232015-02-04 20:55:43 +00001523int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
1524{
1525 if (!bmap)
1526 return -1;
1527 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
1528 bmap->n_div -= n;
1529 return 0;
1530}
1531
1532int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n)
1533{
Tobias Grosser06e15922016-11-16 11:06:47 +00001534 return isl_basic_map_free_div(bset_to_bmap(bset), n);
Tobias Grosser52a25232015-02-04 20:55:43 +00001535}
1536
1537/* Copy constraint from src to dst, putting the vars of src at offset
1538 * dim_off in dst and the divs of src at offset div_off in dst.
1539 * If both sets are actually map, then dim_off applies to the input
1540 * variables.
1541 */
1542static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
1543 struct isl_basic_map *src_map, isl_int *src,
1544 unsigned in_off, unsigned out_off, unsigned div_off)
1545{
1546 unsigned src_nparam = isl_basic_map_n_param(src_map);
1547 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
1548 unsigned src_in = isl_basic_map_n_in(src_map);
1549 unsigned dst_in = isl_basic_map_n_in(dst_map);
1550 unsigned src_out = isl_basic_map_n_out(src_map);
1551 unsigned dst_out = isl_basic_map_n_out(dst_map);
1552 isl_int_set(dst[0], src[0]);
1553 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
1554 if (dst_nparam > src_nparam)
1555 isl_seq_clr(dst+1+src_nparam,
1556 dst_nparam - src_nparam);
1557 isl_seq_clr(dst+1+dst_nparam, in_off);
1558 isl_seq_cpy(dst+1+dst_nparam+in_off,
1559 src+1+src_nparam,
1560 isl_min(dst_in-in_off, src_in));
1561 if (dst_in-in_off > src_in)
1562 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
1563 dst_in - in_off - src_in);
1564 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
1565 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
1566 src+1+src_nparam+src_in,
1567 isl_min(dst_out-out_off, src_out));
1568 if (dst_out-out_off > src_out)
1569 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
1570 dst_out - out_off - src_out);
1571 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
1572 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
1573 src+1+src_nparam+src_in+src_out,
1574 isl_min(dst_map->extra-div_off, src_map->n_div));
1575 if (dst_map->n_div-div_off > src_map->n_div)
1576 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
1577 div_off+src_map->n_div,
1578 dst_map->n_div - div_off - src_map->n_div);
1579}
1580
1581static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
1582 struct isl_basic_map *src_map, isl_int *src,
1583 unsigned in_off, unsigned out_off, unsigned div_off)
1584{
1585 isl_int_set(dst[0], src[0]);
1586 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
1587}
1588
1589static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
1590 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
1591{
1592 int i;
1593 unsigned div_off;
1594
1595 if (!bmap1 || !bmap2)
1596 goto error;
1597
1598 div_off = bmap1->n_div;
1599
1600 for (i = 0; i < bmap2->n_eq; ++i) {
1601 int i1 = isl_basic_map_alloc_equality(bmap1);
1602 if (i1 < 0)
1603 goto error;
1604 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
1605 i_pos, o_pos, div_off);
1606 }
1607
1608 for (i = 0; i < bmap2->n_ineq; ++i) {
1609 int i1 = isl_basic_map_alloc_inequality(bmap1);
1610 if (i1 < 0)
1611 goto error;
1612 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
1613 i_pos, o_pos, div_off);
1614 }
1615
1616 for (i = 0; i < bmap2->n_div; ++i) {
1617 int i1 = isl_basic_map_alloc_div(bmap1);
1618 if (i1 < 0)
1619 goto error;
1620 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
1621 i_pos, o_pos, div_off);
1622 }
1623
1624 isl_basic_map_free(bmap2);
1625
1626 return bmap1;
1627
1628error:
1629 isl_basic_map_free(bmap1);
1630 isl_basic_map_free(bmap2);
1631 return NULL;
1632}
1633
1634struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1635 struct isl_basic_set *bset2, unsigned pos)
1636{
Tobias Grosser06e15922016-11-16 11:06:47 +00001637 return bset_from_bmap(add_constraints(bset_to_bmap(bset1),
1638 bset_to_bmap(bset2), 0, pos));
Tobias Grosser52a25232015-02-04 20:55:43 +00001639}
1640
1641struct isl_basic_map *isl_basic_map_extend_space(struct isl_basic_map *base,
1642 __isl_take isl_space *dim, unsigned extra,
1643 unsigned n_eq, unsigned n_ineq)
1644{
1645 struct isl_basic_map *ext;
1646 unsigned flags;
1647 int dims_ok;
1648
1649 if (!dim)
1650 goto error;
1651
1652 if (!base)
1653 goto error;
1654
1655 dims_ok = isl_space_is_equal(base->dim, dim) &&
1656 base->extra >= base->n_div + extra;
1657
1658 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1659 room_for_ineq(base, n_ineq)) {
1660 isl_space_free(dim);
1661 return base;
1662 }
1663
1664 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
1665 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
1666 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
1667 extra += base->extra;
1668 n_eq += base->n_eq;
1669 n_ineq += base->n_ineq;
1670
1671 ext = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1672 dim = NULL;
1673 if (!ext)
1674 goto error;
1675
1676 if (dims_ok)
1677 ext->sample = isl_vec_copy(base->sample);
1678 flags = base->flags;
1679 ext = add_constraints(ext, base, 0, 0);
1680 if (ext) {
1681 ext->flags = flags;
1682 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1683 }
1684
1685 return ext;
1686
1687error:
1688 isl_space_free(dim);
1689 isl_basic_map_free(base);
1690 return NULL;
1691}
1692
1693struct isl_basic_set *isl_basic_set_extend_space(struct isl_basic_set *base,
1694 __isl_take isl_space *dim, unsigned extra,
1695 unsigned n_eq, unsigned n_ineq)
1696{
Tobias Grosser06e15922016-11-16 11:06:47 +00001697 return bset_from_bmap(isl_basic_map_extend_space(bset_to_bmap(base),
1698 dim, extra, n_eq, n_ineq));
Tobias Grosser52a25232015-02-04 20:55:43 +00001699}
1700
1701struct isl_basic_map *isl_basic_map_extend_constraints(
1702 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1703{
1704 if (!base)
1705 return NULL;
1706 return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1707 0, n_eq, n_ineq);
1708}
1709
1710struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1711 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1712 unsigned n_eq, unsigned n_ineq)
1713{
1714 struct isl_basic_map *bmap;
1715 isl_space *dim;
1716
1717 if (!base)
1718 return NULL;
1719 dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1720 if (!dim)
1721 goto error;
1722
1723 bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1724 return bmap;
1725error:
1726 isl_basic_map_free(base);
1727 return NULL;
1728}
1729
1730struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1731 unsigned nparam, unsigned dim, unsigned extra,
1732 unsigned n_eq, unsigned n_ineq)
1733{
Tobias Grosser06e15922016-11-16 11:06:47 +00001734 return bset_from_bmap(isl_basic_map_extend(bset_to_bmap(base),
1735 nparam, 0, dim, extra, n_eq, n_ineq));
Tobias Grosser52a25232015-02-04 20:55:43 +00001736}
1737
1738struct isl_basic_set *isl_basic_set_extend_constraints(
1739 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1740{
Tobias Grosser06e15922016-11-16 11:06:47 +00001741 isl_basic_map *bmap = bset_to_bmap(base);
1742 bmap = isl_basic_map_extend_constraints(bmap, n_eq, n_ineq);
1743 return bset_from_bmap(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +00001744}
1745
1746struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1747{
Tobias Grosser06e15922016-11-16 11:06:47 +00001748 return bset_from_bmap(isl_basic_map_cow(bset_to_bmap(bset)));
Tobias Grosser52a25232015-02-04 20:55:43 +00001749}
1750
1751struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1752{
1753 if (!bmap)
1754 return NULL;
1755
1756 if (bmap->ref > 1) {
1757 bmap->ref--;
1758 bmap = isl_basic_map_dup(bmap);
1759 }
Tobias Grosser1fa7b972015-02-16 19:33:40 +00001760 if (bmap) {
Tobias Grosser52a25232015-02-04 20:55:43 +00001761 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
Tobias Grosser1fa7b972015-02-16 19:33:40 +00001762 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
1763 }
Tobias Grosser52a25232015-02-04 20:55:43 +00001764 return bmap;
1765}
1766
Tobias Grosser07b20952016-06-12 04:30:40 +00001767/* Clear all cached information in "map", either because it is about
1768 * to be modified or because it is being freed.
1769 * Always return the same pointer that is passed in.
1770 * This is needed for the use in isl_map_free.
1771 */
1772static __isl_give isl_map *clear_caches(__isl_take isl_map *map)
Tobias Grosser52a25232015-02-04 20:55:43 +00001773{
Tobias Grosser07b20952016-06-12 04:30:40 +00001774 isl_basic_map_free(map->cached_simple_hull[0]);
1775 isl_basic_map_free(map->cached_simple_hull[1]);
1776 map->cached_simple_hull[0] = NULL;
1777 map->cached_simple_hull[1] = NULL;
1778 return map;
Tobias Grosser52a25232015-02-04 20:55:43 +00001779}
1780
Tobias Grosser07b20952016-06-12 04:30:40 +00001781struct isl_set *isl_set_cow(struct isl_set *set)
1782{
1783 return isl_map_cow(set);
1784}
1785
1786/* Return an isl_map that is equal to "map" and that has only
1787 * a single reference.
1788 *
1789 * If the original input already has only one reference, then
1790 * simply return it, but clear all cached information, since
1791 * it may be rendered invalid by the operations that will be
1792 * performed on the result.
1793 *
1794 * Otherwise, create a duplicate (without any cached information).
1795 */
Tobias Grosser52a25232015-02-04 20:55:43 +00001796struct isl_map *isl_map_cow(struct isl_map *map)
1797{
1798 if (!map)
1799 return NULL;
1800
1801 if (map->ref == 1)
Tobias Grosser07b20952016-06-12 04:30:40 +00001802 return clear_caches(map);
Tobias Grosser52a25232015-02-04 20:55:43 +00001803 map->ref--;
1804 return isl_map_dup(map);
1805}
1806
1807static void swap_vars(struct isl_blk blk, isl_int *a,
1808 unsigned a_len, unsigned b_len)
1809{
1810 isl_seq_cpy(blk.data, a+a_len, b_len);
1811 isl_seq_cpy(blk.data+b_len, a, a_len);
1812 isl_seq_cpy(a, blk.data, b_len+a_len);
1813}
1814
1815static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1816 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1817{
1818 int i;
1819 struct isl_blk blk;
1820
1821 if (!bmap)
1822 goto error;
1823
1824 isl_assert(bmap->ctx,
1825 pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error);
1826
1827 if (n1 == 0 || n2 == 0)
1828 return bmap;
1829
1830 bmap = isl_basic_map_cow(bmap);
1831 if (!bmap)
1832 return NULL;
1833
1834 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1835 if (isl_blk_is_error(blk))
1836 goto error;
1837
1838 for (i = 0; i < bmap->n_eq; ++i)
1839 swap_vars(blk,
1840 bmap->eq[i] + pos, n1, n2);
1841
1842 for (i = 0; i < bmap->n_ineq; ++i)
1843 swap_vars(blk,
1844 bmap->ineq[i] + pos, n1, n2);
1845
1846 for (i = 0; i < bmap->n_div; ++i)
1847 swap_vars(blk,
1848 bmap->div[i]+1 + pos, n1, n2);
1849
1850 isl_blk_free(bmap->ctx, blk);
1851
1852 ISL_F_CLR(bmap, ISL_BASIC_SET_NORMALIZED);
1853 bmap = isl_basic_map_gauss(bmap, NULL);
1854 return isl_basic_map_finalize(bmap);
1855error:
1856 isl_basic_map_free(bmap);
1857 return NULL;
1858}
1859
Tobias Grosser52a25232015-02-04 20:55:43 +00001860struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1861{
1862 int i = 0;
1863 unsigned total;
1864 if (!bmap)
1865 goto error;
1866 total = isl_basic_map_total_dim(bmap);
1867 isl_basic_map_free_div(bmap, bmap->n_div);
1868 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1869 if (bmap->n_eq > 0)
1870 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1871 else {
1872 i = isl_basic_map_alloc_equality(bmap);
1873 if (i < 0)
1874 goto error;
1875 }
1876 isl_int_set_si(bmap->eq[i][0], 1);
1877 isl_seq_clr(bmap->eq[i]+1, total);
1878 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1879 isl_vec_free(bmap->sample);
1880 bmap->sample = NULL;
1881 return isl_basic_map_finalize(bmap);
1882error:
1883 isl_basic_map_free(bmap);
1884 return NULL;
1885}
1886
1887struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1888{
Tobias Grosser06e15922016-11-16 11:06:47 +00001889 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset)));
Tobias Grosser52a25232015-02-04 20:55:43 +00001890}
1891
1892/* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
1893 * of "bmap").
1894 */
1895static void swap_div(__isl_keep isl_basic_map *bmap, int a, int b)
1896{
1897 isl_int *t = bmap->div[a];
1898 bmap->div[a] = bmap->div[b];
1899 bmap->div[b] = t;
1900}
1901
1902/* Swap divs "a" and "b" in "bmap" and adjust the constraints and
1903 * div definitions accordingly.
1904 */
1905void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1906{
1907 int i;
1908 unsigned off = isl_space_dim(bmap->dim, isl_dim_all);
1909
1910 swap_div(bmap, a, b);
1911
1912 for (i = 0; i < bmap->n_eq; ++i)
1913 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1914
1915 for (i = 0; i < bmap->n_ineq; ++i)
1916 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1917
1918 for (i = 0; i < bmap->n_div; ++i)
1919 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1920 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1921}
1922
Tobias Grosser932ec012016-07-06 09:11:00 +00001923/* Swap divs "a" and "b" in "bset" and adjust the constraints and
1924 * div definitions accordingly.
1925 */
1926void isl_basic_set_swap_div(__isl_keep isl_basic_set *bset, int a, int b)
1927{
1928 isl_basic_map_swap_div(bset, a, b);
1929}
1930
Tobias Grosser52a25232015-02-04 20:55:43 +00001931/* Eliminate the specified n dimensions starting at first from the
1932 * constraints, without removing the dimensions from the space.
1933 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1934 */
1935__isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
1936 enum isl_dim_type type, unsigned first, unsigned n)
1937{
1938 int i;
1939
1940 if (!map)
1941 return NULL;
1942 if (n == 0)
1943 return map;
1944
1945 if (first + n > isl_map_dim(map, type) || first + n < first)
1946 isl_die(map->ctx, isl_error_invalid,
1947 "index out of bounds", goto error);
1948
1949 map = isl_map_cow(map);
1950 if (!map)
1951 return NULL;
1952
1953 for (i = 0; i < map->n; ++i) {
1954 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
1955 if (!map->p[i])
1956 goto error;
1957 }
1958 return map;
1959error:
1960 isl_map_free(map);
1961 return NULL;
1962}
1963
1964/* Eliminate the specified n dimensions starting at first from the
1965 * constraints, without removing the dimensions from the space.
1966 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1967 */
1968__isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
1969 enum isl_dim_type type, unsigned first, unsigned n)
1970{
Tobias Grosser06e15922016-11-16 11:06:47 +00001971 return set_from_map(isl_map_eliminate(set_to_map(set), type, first, n));
Tobias Grosser52a25232015-02-04 20:55:43 +00001972}
1973
1974/* Eliminate the specified n dimensions starting at first from the
1975 * constraints, without removing the dimensions from the space.
1976 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1977 */
1978__isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
1979 unsigned first, unsigned n)
1980{
1981 return isl_set_eliminate(set, isl_dim_set, first, n);
1982}
1983
1984__isl_give isl_basic_map *isl_basic_map_remove_divs(
1985 __isl_take isl_basic_map *bmap)
1986{
1987 if (!bmap)
1988 return NULL;
1989 bmap = isl_basic_map_eliminate_vars(bmap,
1990 isl_space_dim(bmap->dim, isl_dim_all), bmap->n_div);
1991 if (!bmap)
1992 return NULL;
1993 bmap->n_div = 0;
1994 return isl_basic_map_finalize(bmap);
1995}
1996
1997__isl_give isl_basic_set *isl_basic_set_remove_divs(
1998 __isl_take isl_basic_set *bset)
1999{
Tobias Grosser06e15922016-11-16 11:06:47 +00002000 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset)));
Tobias Grosser52a25232015-02-04 20:55:43 +00002001}
2002
2003__isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
2004{
2005 int i;
2006
2007 if (!map)
2008 return NULL;
2009 if (map->n == 0)
2010 return map;
2011
2012 map = isl_map_cow(map);
2013 if (!map)
2014 return NULL;
2015
2016 for (i = 0; i < map->n; ++i) {
2017 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
2018 if (!map->p[i])
2019 goto error;
2020 }
2021 return map;
2022error:
2023 isl_map_free(map);
2024 return NULL;
2025}
2026
2027__isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
2028{
2029 return isl_map_remove_divs(set);
2030}
2031
2032struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap,
2033 enum isl_dim_type type, unsigned first, unsigned n)
2034{
2035 if (!bmap)
2036 return NULL;
2037 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
2038 goto error);
2039 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2040 return bmap;
2041 bmap = isl_basic_map_eliminate_vars(bmap,
2042 isl_basic_map_offset(bmap, type) - 1 + first, n);
2043 if (!bmap)
2044 return bmap;
2045 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
2046 return bmap;
2047 bmap = isl_basic_map_drop(bmap, type, first, n);
2048 return bmap;
2049error:
2050 isl_basic_map_free(bmap);
2051 return NULL;
2052}
2053
2054/* Return true if the definition of the given div (recursively) involves
2055 * any of the given variables.
2056 */
2057static int div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
2058 unsigned first, unsigned n)
2059{
2060 int i;
2061 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2062
2063 if (isl_int_is_zero(bmap->div[div][0]))
2064 return 0;
2065 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
2066 return 1;
2067
2068 for (i = bmap->n_div - 1; i >= 0; --i) {
2069 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2070 continue;
2071 if (div_involves_vars(bmap, i, first, n))
2072 return 1;
2073 }
2074
2075 return 0;
2076}
2077
2078/* Try and add a lower and/or upper bound on "div" to "bmap"
2079 * based on inequality "i".
2080 * "total" is the total number of variables (excluding the divs).
2081 * "v" is a temporary object that can be used during the calculations.
2082 * If "lb" is set, then a lower bound should be constructed.
2083 * If "ub" is set, then an upper bound should be constructed.
2084 *
2085 * The calling function has already checked that the inequality does not
2086 * reference "div", but we still need to check that the inequality is
2087 * of the right form. We'll consider the case where we want to construct
2088 * a lower bound. The construction of upper bounds is similar.
2089 *
2090 * Let "div" be of the form
2091 *
2092 * q = floor((a + f(x))/d)
2093 *
2094 * We essentially check if constraint "i" is of the form
2095 *
2096 * b + f(x) >= 0
2097 *
2098 * so that we can use it to derive a lower bound on "div".
2099 * However, we allow a slightly more general form
2100 *
2101 * b + g(x) >= 0
2102 *
2103 * with the condition that the coefficients of g(x) - f(x) are all
2104 * divisible by d.
2105 * Rewriting this constraint as
2106 *
2107 * 0 >= -b - g(x)
2108 *
2109 * adding a + f(x) to both sides and dividing by d, we obtain
2110 *
2111 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2112 *
2113 * Taking the floor on both sides, we obtain
2114 *
2115 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2116 *
2117 * or
2118 *
2119 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2120 *
2121 * In the case of an upper bound, we construct the constraint
2122 *
2123 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2124 *
2125 */
2126static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
2127 __isl_take isl_basic_map *bmap, int div, int i,
2128 unsigned total, isl_int v, int lb, int ub)
2129{
2130 int j;
2131
2132 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
2133 if (lb) {
2134 isl_int_sub(v, bmap->ineq[i][1 + j],
2135 bmap->div[div][1 + 1 + j]);
2136 lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
2137 }
2138 if (ub) {
2139 isl_int_add(v, bmap->ineq[i][1 + j],
2140 bmap->div[div][1 + 1 + j]);
2141 ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
2142 }
2143 }
2144 if (!lb && !ub)
2145 return bmap;
2146
2147 bmap = isl_basic_map_cow(bmap);
2148 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
2149 if (lb) {
2150 int k = isl_basic_map_alloc_inequality(bmap);
2151 if (k < 0)
2152 goto error;
2153 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2154 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
2155 bmap->div[div][1 + j]);
2156 isl_int_cdiv_q(bmap->ineq[k][j],
2157 bmap->ineq[k][j], bmap->div[div][0]);
2158 }
2159 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
2160 }
2161 if (ub) {
2162 int k = isl_basic_map_alloc_inequality(bmap);
2163 if (k < 0)
2164 goto error;
2165 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2166 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
2167 bmap->div[div][1 + j]);
2168 isl_int_fdiv_q(bmap->ineq[k][j],
2169 bmap->ineq[k][j], bmap->div[div][0]);
2170 }
2171 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
2172 }
2173
2174 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2175 return bmap;
2176error:
2177 isl_basic_map_free(bmap);
2178 return NULL;
2179}
2180
2181/* This function is called right before "div" is eliminated from "bmap"
2182 * using Fourier-Motzkin.
2183 * Look through the constraints of "bmap" for constraints on the argument
2184 * of the integer division and use them to construct constraints on the
2185 * integer division itself. These constraints can then be combined
2186 * during the Fourier-Motzkin elimination.
2187 * Note that it is only useful to introduce lower bounds on "div"
2188 * if "bmap" already contains upper bounds on "div" as the newly
2189 * introduce lower bounds can then be combined with the pre-existing
2190 * upper bounds. Similarly for upper bounds.
2191 * We therefore first check if "bmap" contains any lower and/or upper bounds
2192 * on "div".
2193 *
2194 * It is interesting to note that the introduction of these constraints
2195 * can indeed lead to more accurate results, even when compared to
2196 * deriving constraints on the argument of "div" from constraints on "div".
2197 * Consider, for example, the set
2198 *
2199 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2200 *
2201 * The second constraint can be rewritten as
2202 *
2203 * 2 * [(-i-2j+3)/4] + k >= 0
2204 *
2205 * from which we can derive
2206 *
2207 * -i - 2j + 3 >= -2k
2208 *
2209 * or
2210 *
2211 * i + 2j <= 3 + 2k
2212 *
2213 * Combined with the first constraint, we obtain
2214 *
2215 * -3 <= 3 + 2k or k >= -3
2216 *
2217 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2218 * the first constraint, we obtain
2219 *
2220 * [(i + 2j)/4] >= [-3/4] = -1
2221 *
2222 * Combining this constraint with the second constraint, we obtain
2223 *
2224 * k >= -2
2225 */
2226static __isl_give isl_basic_map *insert_bounds_on_div(
2227 __isl_take isl_basic_map *bmap, int div)
2228{
2229 int i;
2230 int check_lb, check_ub;
2231 isl_int v;
2232 unsigned total;
2233
2234 if (!bmap)
2235 return NULL;
2236
2237 if (isl_int_is_zero(bmap->div[div][0]))
2238 return bmap;
2239
2240 total = isl_space_dim(bmap->dim, isl_dim_all);
2241
2242 check_lb = 0;
2243 check_ub = 0;
2244 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
2245 int s = isl_int_sgn(bmap->ineq[i][1 + total + div]);
2246 if (s > 0)
2247 check_ub = 1;
2248 if (s < 0)
2249 check_lb = 1;
2250 }
2251
2252 if (!check_lb && !check_ub)
2253 return bmap;
2254
2255 isl_int_init(v);
2256
2257 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
2258 if (!isl_int_is_zero(bmap->ineq[i][1 + total + div]))
2259 continue;
2260
2261 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, total, v,
2262 check_lb, check_ub);
2263 }
2264
2265 isl_int_clear(v);
2266
2267 return bmap;
2268}
2269
2270/* Remove all divs (recursively) involving any of the given dimensions
2271 * in their definitions.
2272 */
2273__isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
2274 __isl_take isl_basic_map *bmap,
2275 enum isl_dim_type type, unsigned first, unsigned n)
2276{
2277 int i;
2278
2279 if (!bmap)
2280 return NULL;
2281 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
2282 goto error);
2283 first += isl_basic_map_offset(bmap, type);
2284
2285 for (i = bmap->n_div - 1; i >= 0; --i) {
2286 if (!div_involves_vars(bmap, i, first, n))
2287 continue;
2288 bmap = insert_bounds_on_div(bmap, i);
2289 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2290 if (!bmap)
2291 return NULL;
2292 i = bmap->n_div;
2293 }
2294
2295 return bmap;
2296error:
2297 isl_basic_map_free(bmap);
2298 return NULL;
2299}
2300
2301__isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims(
2302 __isl_take isl_basic_set *bset,
2303 enum isl_dim_type type, unsigned first, unsigned n)
2304{
2305 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
2306}
2307
2308__isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
2309 enum isl_dim_type type, unsigned first, unsigned n)
2310{
2311 int i;
2312
2313 if (!map)
2314 return NULL;
2315 if (map->n == 0)
2316 return map;
2317
2318 map = isl_map_cow(map);
2319 if (!map)
2320 return NULL;
2321
2322 for (i = 0; i < map->n; ++i) {
2323 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
2324 type, first, n);
2325 if (!map->p[i])
2326 goto error;
2327 }
2328 return map;
2329error:
2330 isl_map_free(map);
2331 return NULL;
2332}
2333
2334__isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
2335 enum isl_dim_type type, unsigned first, unsigned n)
2336{
Tobias Grosser06e15922016-11-16 11:06:47 +00002337 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set),
2338 type, first, n));
Tobias Grosser52a25232015-02-04 20:55:43 +00002339}
2340
Tobias Grosser06e15922016-11-16 11:06:47 +00002341/* Does the description of "bmap" depend on the specified dimensions?
Tobias Grosser52a25232015-02-04 20:55:43 +00002342 * We also check whether the dimensions appear in any of the div definitions.
2343 * In principle there is no need for this check. If the dimensions appear
2344 * in a div definition, they also appear in the defining constraints of that
2345 * div.
2346 */
Tobias Grosserb2f39922015-05-28 13:32:11 +00002347isl_bool isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
Tobias Grosser52a25232015-02-04 20:55:43 +00002348 enum isl_dim_type type, unsigned first, unsigned n)
2349{
2350 int i;
2351
2352 if (!bmap)
Tobias Grosserb2f39922015-05-28 13:32:11 +00002353 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00002354
2355 if (first + n > isl_basic_map_dim(bmap, type))
2356 isl_die(bmap->ctx, isl_error_invalid,
Tobias Grosserb2f39922015-05-28 13:32:11 +00002357 "index out of bounds", return isl_bool_error);
Tobias Grosser52a25232015-02-04 20:55:43 +00002358
2359 first += isl_basic_map_offset(bmap, type);
2360 for (i = 0; i < bmap->n_eq; ++i)
2361 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
Tobias Grosserb2f39922015-05-28 13:32:11 +00002362 return isl_bool_true;
Tobias Grosser52a25232015-02-04 20:55:43 +00002363 for (i = 0; i < bmap->n_ineq; ++i)
2364 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
Tobias Grosserb2f39922015-05-28 13:32:11 +00002365 return isl_bool_true;
Tobias Grosser52a25232015-02-04 20:55:43 +00002366 for (i = 0; i < bmap->n_div; ++i) {
2367 if (isl_int_is_zero(bmap->div[i][0]))
2368 continue;
2369 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
Tobias Grosserb2f39922015-05-28 13:32:11 +00002370 return isl_bool_true;
Tobias Grosser52a25232015-02-04 20:55:43 +00002371 }
2372
Tobias Grosserb2f39922015-05-28 13:32:11 +00002373 return isl_bool_false;
Tobias Grosser52a25232015-02-04 20:55:43 +00002374}
2375
Tobias Grosserb2f39922015-05-28 13:32:11 +00002376isl_bool isl_map_involves_dims(__isl_keep isl_map *map,
Tobias Grosser52a25232015-02-04 20:55:43 +00002377 enum isl_dim_type type, unsigned first, unsigned n)
2378{
2379 int i;
2380
2381 if (!map)
Tobias Grosserb2f39922015-05-28 13:32:11 +00002382 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00002383
2384 if (first + n > isl_map_dim(map, type))
2385 isl_die(map->ctx, isl_error_invalid,
Tobias Grosserb2f39922015-05-28 13:32:11 +00002386 "index out of bounds", return isl_bool_error);
Tobias Grosser52a25232015-02-04 20:55:43 +00002387
2388 for (i = 0; i < map->n; ++i) {
Tobias Grosserb2f39922015-05-28 13:32:11 +00002389 isl_bool involves = isl_basic_map_involves_dims(map->p[i],
Tobias Grosser52a25232015-02-04 20:55:43 +00002390 type, first, n);
2391 if (involves < 0 || involves)
2392 return involves;
2393 }
2394
Tobias Grosserb2f39922015-05-28 13:32:11 +00002395 return isl_bool_false;
Tobias Grosser52a25232015-02-04 20:55:43 +00002396}
2397
Tobias Grosserb2f39922015-05-28 13:32:11 +00002398isl_bool isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
Tobias Grosser52a25232015-02-04 20:55:43 +00002399 enum isl_dim_type type, unsigned first, unsigned n)
2400{
2401 return isl_basic_map_involves_dims(bset, type, first, n);
2402}
2403
Tobias Grosserb2f39922015-05-28 13:32:11 +00002404isl_bool isl_set_involves_dims(__isl_keep isl_set *set,
Tobias Grosser52a25232015-02-04 20:55:43 +00002405 enum isl_dim_type type, unsigned first, unsigned n)
2406{
2407 return isl_map_involves_dims(set, type, first, n);
2408}
2409
Tobias Grosser932ec012016-07-06 09:11:00 +00002410/* Does local variable "div" of "bmap" have a complete explicit representation?
2411 * Having a complete explicit representation requires not only
2412 * an explicit representation, but also that all local variables
2413 * that appear in this explicit representation in turn have
2414 * a complete explicit representation.
Tobias Grosser52a25232015-02-04 20:55:43 +00002415 */
Tobias Grosser932ec012016-07-06 09:11:00 +00002416isl_bool isl_basic_map_div_is_known(__isl_keep isl_basic_map *bmap, int div)
Tobias Grosser52a25232015-02-04 20:55:43 +00002417{
2418 int i;
2419 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
Tobias Grosser932ec012016-07-06 09:11:00 +00002420 isl_bool marked;
Tobias Grosser52a25232015-02-04 20:55:43 +00002421
Tobias Grosser932ec012016-07-06 09:11:00 +00002422 marked = isl_basic_map_div_is_marked_unknown(bmap, div);
2423 if (marked < 0 || marked)
2424 return isl_bool_not(marked);
Tobias Grosser52a25232015-02-04 20:55:43 +00002425
2426 for (i = bmap->n_div - 1; i >= 0; --i) {
Tobias Grosser932ec012016-07-06 09:11:00 +00002427 isl_bool known;
2428
Tobias Grosser52a25232015-02-04 20:55:43 +00002429 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2430 continue;
Tobias Grosser932ec012016-07-06 09:11:00 +00002431 known = isl_basic_map_div_is_known(bmap, i);
2432 if (known < 0 || !known)
2433 return known;
Tobias Grosser52a25232015-02-04 20:55:43 +00002434 }
2435
Tobias Grosser932ec012016-07-06 09:11:00 +00002436 return isl_bool_true;
2437}
2438
2439/* Does local variable "div" of "bset" have a complete explicit representation?
2440 */
2441isl_bool isl_basic_set_div_is_known(__isl_keep isl_basic_set *bset, int div)
2442{
2443 return isl_basic_map_div_is_known(bset, div);
Tobias Grosser52a25232015-02-04 20:55:43 +00002444}
2445
2446/* Remove all divs that are unknown or defined in terms of unknown divs.
2447 */
2448__isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
2449 __isl_take isl_basic_map *bmap)
2450{
2451 int i;
2452
2453 if (!bmap)
2454 return NULL;
2455
2456 for (i = bmap->n_div - 1; i >= 0; --i) {
Tobias Grosser932ec012016-07-06 09:11:00 +00002457 if (isl_basic_map_div_is_known(bmap, i))
Tobias Grosser52a25232015-02-04 20:55:43 +00002458 continue;
2459 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2460 if (!bmap)
2461 return NULL;
2462 i = bmap->n_div;
2463 }
2464
2465 return bmap;
2466}
2467
2468/* Remove all divs that are unknown or defined in terms of unknown divs.
2469 */
2470__isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
2471 __isl_take isl_basic_set *bset)
2472{
2473 return isl_basic_map_remove_unknown_divs(bset);
2474}
2475
2476__isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
2477{
2478 int i;
2479
2480 if (!map)
2481 return NULL;
2482 if (map->n == 0)
2483 return map;
2484
2485 map = isl_map_cow(map);
2486 if (!map)
2487 return NULL;
2488
2489 for (i = 0; i < map->n; ++i) {
2490 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
2491 if (!map->p[i])
2492 goto error;
2493 }
2494 return map;
2495error:
2496 isl_map_free(map);
2497 return NULL;
2498}
2499
2500__isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
2501{
Tobias Grosser06e15922016-11-16 11:06:47 +00002502 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set)));
Tobias Grosser52a25232015-02-04 20:55:43 +00002503}
2504
2505__isl_give isl_basic_set *isl_basic_set_remove_dims(
2506 __isl_take isl_basic_set *bset,
2507 enum isl_dim_type type, unsigned first, unsigned n)
2508{
Tobias Grosser06e15922016-11-16 11:06:47 +00002509 isl_basic_map *bmap = bset_to_bmap(bset);
2510 bmap = isl_basic_map_remove_dims(bmap, type, first, n);
2511 return bset_from_bmap(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +00002512}
2513
2514struct isl_map *isl_map_remove_dims(struct isl_map *map,
2515 enum isl_dim_type type, unsigned first, unsigned n)
2516{
2517 int i;
2518
2519 if (n == 0)
2520 return map;
2521
2522 map = isl_map_cow(map);
2523 if (!map)
2524 return NULL;
2525 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2526
2527 for (i = 0; i < map->n; ++i) {
2528 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
2529 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
2530 if (!map->p[i])
2531 goto error;
2532 }
2533 map = isl_map_drop(map, type, first, n);
2534 return map;
2535error:
2536 isl_map_free(map);
2537 return NULL;
2538}
2539
2540__isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
2541 enum isl_dim_type type, unsigned first, unsigned n)
2542{
Tobias Grosser06e15922016-11-16 11:06:47 +00002543 return set_from_map(isl_map_remove_dims(set_to_map(bset),
2544 type, first, n));
Tobias Grosser52a25232015-02-04 20:55:43 +00002545}
2546
2547/* Project out n inputs starting at first using Fourier-Motzkin */
2548struct isl_map *isl_map_remove_inputs(struct isl_map *map,
2549 unsigned first, unsigned n)
2550{
2551 return isl_map_remove_dims(map, isl_dim_in, first, n);
2552}
2553
2554static void dump_term(struct isl_basic_map *bmap,
2555 isl_int c, int pos, FILE *out)
2556{
2557 const char *name;
2558 unsigned in = isl_basic_map_n_in(bmap);
2559 unsigned dim = in + isl_basic_map_n_out(bmap);
2560 unsigned nparam = isl_basic_map_n_param(bmap);
2561 if (!pos)
2562 isl_int_print(out, c, 0);
2563 else {
2564 if (!isl_int_is_one(c))
2565 isl_int_print(out, c, 0);
2566 if (pos < 1 + nparam) {
2567 name = isl_space_get_dim_name(bmap->dim,
2568 isl_dim_param, pos - 1);
2569 if (name)
2570 fprintf(out, "%s", name);
2571 else
2572 fprintf(out, "p%d", pos - 1);
2573 } else if (pos < 1 + nparam + in)
2574 fprintf(out, "i%d", pos - 1 - nparam);
2575 else if (pos < 1 + nparam + dim)
2576 fprintf(out, "o%d", pos - 1 - nparam - in);
2577 else
2578 fprintf(out, "e%d", pos - 1 - nparam - dim);
2579 }
2580}
2581
2582static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
2583 int sign, FILE *out)
2584{
2585 int i;
2586 int first;
2587 unsigned len = 1 + isl_basic_map_total_dim(bmap);
2588 isl_int v;
2589
2590 isl_int_init(v);
2591 for (i = 0, first = 1; i < len; ++i) {
2592 if (isl_int_sgn(c[i]) * sign <= 0)
2593 continue;
2594 if (!first)
2595 fprintf(out, " + ");
2596 first = 0;
2597 isl_int_abs(v, c[i]);
2598 dump_term(bmap, v, i, out);
2599 }
2600 isl_int_clear(v);
2601 if (first)
2602 fprintf(out, "0");
2603}
2604
2605static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
2606 const char *op, FILE *out, int indent)
2607{
2608 int i;
2609
2610 fprintf(out, "%*s", indent, "");
2611
2612 dump_constraint_sign(bmap, c, 1, out);
2613 fprintf(out, " %s ", op);
2614 dump_constraint_sign(bmap, c, -1, out);
2615
2616 fprintf(out, "\n");
2617
2618 for (i = bmap->n_div; i < bmap->extra; ++i) {
2619 if (isl_int_is_zero(c[1+isl_space_dim(bmap->dim, isl_dim_all)+i]))
2620 continue;
2621 fprintf(out, "%*s", indent, "");
2622 fprintf(out, "ERROR: unused div coefficient not zero\n");
2623 abort();
2624 }
2625}
2626
2627static void dump_constraints(struct isl_basic_map *bmap,
2628 isl_int **c, unsigned n,
2629 const char *op, FILE *out, int indent)
2630{
2631 int i;
2632
2633 for (i = 0; i < n; ++i)
2634 dump_constraint(bmap, c[i], op, out, indent);
2635}
2636
2637static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
2638{
2639 int j;
2640 int first = 1;
2641 unsigned total = isl_basic_map_total_dim(bmap);
2642
2643 for (j = 0; j < 1 + total; ++j) {
2644 if (isl_int_is_zero(exp[j]))
2645 continue;
2646 if (!first && isl_int_is_pos(exp[j]))
2647 fprintf(out, "+");
2648 dump_term(bmap, exp[j], j, out);
2649 first = 0;
2650 }
2651}
2652
2653static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
2654{
2655 int i;
2656
2657 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
2658 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
2659
2660 for (i = 0; i < bmap->n_div; ++i) {
2661 fprintf(out, "%*s", indent, "");
2662 fprintf(out, "e%d = [(", i);
2663 dump_affine(bmap, bmap->div[i]+1, out);
2664 fprintf(out, ")/");
2665 isl_int_print(out, bmap->div[i][0], 0);
2666 fprintf(out, "]\n");
2667 }
2668}
2669
2670void isl_basic_set_print_internal(struct isl_basic_set *bset,
2671 FILE *out, int indent)
2672{
2673 if (!bset) {
2674 fprintf(out, "null basic set\n");
2675 return;
2676 }
2677
2678 fprintf(out, "%*s", indent, "");
2679 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2680 bset->ref, bset->dim->nparam, bset->dim->n_out,
2681 bset->extra, bset->flags);
Tobias Grosser06e15922016-11-16 11:06:47 +00002682 dump(bset_to_bmap(bset), out, indent);
Tobias Grosser52a25232015-02-04 20:55:43 +00002683}
2684
2685void isl_basic_map_print_internal(struct isl_basic_map *bmap,
2686 FILE *out, int indent)
2687{
2688 if (!bmap) {
2689 fprintf(out, "null basic map\n");
2690 return;
2691 }
2692
2693 fprintf(out, "%*s", indent, "");
2694 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2695 "flags: %x, n_name: %d\n",
2696 bmap->ref,
2697 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
2698 bmap->extra, bmap->flags, bmap->dim->n_id);
2699 dump(bmap, out, indent);
2700}
2701
2702int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
2703{
2704 unsigned total;
2705 if (!bmap)
2706 return -1;
2707 total = isl_basic_map_total_dim(bmap);
2708 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
2709 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
2710 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
2711 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2712 return 0;
2713}
2714
Tobias Grosser07b20952016-06-12 04:30:40 +00002715__isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *space, int n,
Tobias Grosser52a25232015-02-04 20:55:43 +00002716 unsigned flags)
2717{
Tobias Grosser07b20952016-06-12 04:30:40 +00002718 if (!space)
Tobias Grosser52a25232015-02-04 20:55:43 +00002719 return NULL;
Tobias Grosser07b20952016-06-12 04:30:40 +00002720 if (isl_space_dim(space, isl_dim_in) != 0)
2721 isl_die(isl_space_get_ctx(space), isl_error_invalid,
2722 "set cannot have input dimensions", goto error);
2723 return isl_map_alloc_space(space, n, flags);
Tobias Grosser52a25232015-02-04 20:55:43 +00002724error:
Tobias Grosser07b20952016-06-12 04:30:40 +00002725 isl_space_free(space);
Tobias Grosser52a25232015-02-04 20:55:43 +00002726 return NULL;
2727}
2728
2729struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
2730 unsigned nparam, unsigned dim, int n, unsigned flags)
2731{
2732 struct isl_set *set;
2733 isl_space *dims;
2734
2735 dims = isl_space_alloc(ctx, nparam, 0, dim);
2736 if (!dims)
2737 return NULL;
2738
2739 set = isl_set_alloc_space(dims, n, flags);
2740 return set;
2741}
2742
2743/* Make sure "map" has room for at least "n" more basic maps.
2744 */
2745struct isl_map *isl_map_grow(struct isl_map *map, int n)
2746{
2747 int i;
2748 struct isl_map *grown = NULL;
2749
2750 if (!map)
2751 return NULL;
2752 isl_assert(map->ctx, n >= 0, goto error);
2753 if (map->n + n <= map->size)
2754 return map;
2755 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
2756 if (!grown)
2757 goto error;
2758 for (i = 0; i < map->n; ++i) {
2759 grown->p[i] = isl_basic_map_copy(map->p[i]);
2760 if (!grown->p[i])
2761 goto error;
2762 grown->n++;
2763 }
2764 isl_map_free(map);
2765 return grown;
2766error:
2767 isl_map_free(grown);
2768 isl_map_free(map);
2769 return NULL;
2770}
2771
2772/* Make sure "set" has room for at least "n" more basic sets.
2773 */
2774struct isl_set *isl_set_grow(struct isl_set *set, int n)
2775{
Tobias Grosser06e15922016-11-16 11:06:47 +00002776 return set_from_map(isl_map_grow(set_to_map(set), n));
Tobias Grosser52a25232015-02-04 20:55:43 +00002777}
2778
2779struct isl_set *isl_set_dup(struct isl_set *set)
2780{
2781 int i;
2782 struct isl_set *dup;
2783
2784 if (!set)
2785 return NULL;
2786
2787 dup = isl_set_alloc_space(isl_space_copy(set->dim), set->n, set->flags);
2788 if (!dup)
2789 return NULL;
2790 for (i = 0; i < set->n; ++i)
2791 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
2792 return dup;
2793}
2794
2795struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
2796{
2797 return isl_map_from_basic_map(bset);
2798}
2799
2800struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
2801{
2802 struct isl_map *map;
2803
2804 if (!bmap)
2805 return NULL;
2806
2807 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
2808 return isl_map_add_basic_map(map, bmap);
2809}
2810
2811__isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
2812 __isl_take isl_basic_set *bset)
2813{
Tobias Grosser06e15922016-11-16 11:06:47 +00002814 return set_from_map(isl_map_add_basic_map(set_to_map(set),
2815 bset_to_bmap(bset)));
Tobias Grosser52a25232015-02-04 20:55:43 +00002816}
2817
2818__isl_null isl_set *isl_set_free(__isl_take isl_set *set)
2819{
Tobias Grosser07b20952016-06-12 04:30:40 +00002820 return isl_map_free(set);
Tobias Grosser52a25232015-02-04 20:55:43 +00002821}
2822
2823void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
2824{
2825 int i;
2826
2827 if (!set) {
2828 fprintf(out, "null set\n");
2829 return;
2830 }
2831
2832 fprintf(out, "%*s", indent, "");
2833 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2834 set->ref, set->n, set->dim->nparam, set->dim->n_out,
2835 set->flags);
2836 for (i = 0; i < set->n; ++i) {
2837 fprintf(out, "%*s", indent, "");
2838 fprintf(out, "basic set %d:\n", i);
2839 isl_basic_set_print_internal(set->p[i], out, indent+4);
2840 }
2841}
2842
2843void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
2844{
2845 int i;
2846
2847 if (!map) {
2848 fprintf(out, "null map\n");
2849 return;
2850 }
2851
2852 fprintf(out, "%*s", indent, "");
2853 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2854 "flags: %x, n_name: %d\n",
2855 map->ref, map->n, map->dim->nparam, map->dim->n_in,
2856 map->dim->n_out, map->flags, map->dim->n_id);
2857 for (i = 0; i < map->n; ++i) {
2858 fprintf(out, "%*s", indent, "");
2859 fprintf(out, "basic map %d:\n", i);
2860 isl_basic_map_print_internal(map->p[i], out, indent+4);
2861 }
2862}
2863
2864struct isl_basic_map *isl_basic_map_intersect_domain(
2865 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2866{
2867 struct isl_basic_map *bmap_domain;
2868
2869 if (!bmap || !bset)
2870 goto error;
2871
2872 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2873 bset->dim, isl_dim_param), goto error);
2874
2875 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2876 isl_assert(bset->ctx,
2877 isl_basic_map_compatible_domain(bmap, bset), goto error);
2878
2879 bmap = isl_basic_map_cow(bmap);
2880 if (!bmap)
2881 goto error;
2882 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2883 bset->n_div, bset->n_eq, bset->n_ineq);
2884 bmap_domain = isl_basic_map_from_domain(bset);
2885 bmap = add_constraints(bmap, bmap_domain, 0, 0);
2886
2887 bmap = isl_basic_map_simplify(bmap);
2888 return isl_basic_map_finalize(bmap);
2889error:
2890 isl_basic_map_free(bmap);
2891 isl_basic_set_free(bset);
2892 return NULL;
2893}
2894
2895struct isl_basic_map *isl_basic_map_intersect_range(
2896 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2897{
2898 struct isl_basic_map *bmap_range;
2899
2900 if (!bmap || !bset)
2901 goto error;
2902
2903 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2904 bset->dim, isl_dim_param), goto error);
2905
2906 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2907 isl_assert(bset->ctx,
2908 isl_basic_map_compatible_range(bmap, bset), goto error);
2909
Tobias Grossera67ac972016-02-26 11:35:12 +00002910 if (isl_basic_set_plain_is_universe(bset)) {
Tobias Grosser52a25232015-02-04 20:55:43 +00002911 isl_basic_set_free(bset);
2912 return bmap;
2913 }
2914
2915 bmap = isl_basic_map_cow(bmap);
2916 if (!bmap)
2917 goto error;
2918 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2919 bset->n_div, bset->n_eq, bset->n_ineq);
Tobias Grossereab0943e2016-11-22 21:31:59 +00002920 bmap_range = bset_to_bmap(bset);
Tobias Grosser52a25232015-02-04 20:55:43 +00002921 bmap = add_constraints(bmap, bmap_range, 0, 0);
2922
2923 bmap = isl_basic_map_simplify(bmap);
2924 return isl_basic_map_finalize(bmap);
2925error:
2926 isl_basic_map_free(bmap);
2927 isl_basic_set_free(bset);
2928 return NULL;
2929}
2930
Tobias Grosserb2f39922015-05-28 13:32:11 +00002931isl_bool isl_basic_map_contains(__isl_keep isl_basic_map *bmap,
2932 __isl_keep isl_vec *vec)
Tobias Grosser52a25232015-02-04 20:55:43 +00002933{
2934 int i;
2935 unsigned total;
2936 isl_int s;
2937
2938 if (!bmap || !vec)
Tobias Grosserb2f39922015-05-28 13:32:11 +00002939 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00002940
2941 total = 1 + isl_basic_map_total_dim(bmap);
2942 if (total != vec->size)
Tobias Grosserb2f39922015-05-28 13:32:11 +00002943 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00002944
2945 isl_int_init(s);
2946
2947 for (i = 0; i < bmap->n_eq; ++i) {
2948 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
2949 if (!isl_int_is_zero(s)) {
2950 isl_int_clear(s);
Tobias Grosserb2f39922015-05-28 13:32:11 +00002951 return isl_bool_false;
Tobias Grosser52a25232015-02-04 20:55:43 +00002952 }
2953 }
2954
2955 for (i = 0; i < bmap->n_ineq; ++i) {
2956 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
2957 if (isl_int_is_neg(s)) {
2958 isl_int_clear(s);
Tobias Grosserb2f39922015-05-28 13:32:11 +00002959 return isl_bool_false;
Tobias Grosser52a25232015-02-04 20:55:43 +00002960 }
2961 }
2962
2963 isl_int_clear(s);
2964
Tobias Grosserb2f39922015-05-28 13:32:11 +00002965 return isl_bool_true;
Tobias Grosser52a25232015-02-04 20:55:43 +00002966}
2967
Tobias Grosserb2f39922015-05-28 13:32:11 +00002968isl_bool isl_basic_set_contains(__isl_keep isl_basic_set *bset,
2969 __isl_keep isl_vec *vec)
Tobias Grosser52a25232015-02-04 20:55:43 +00002970{
Tobias Grosser06e15922016-11-16 11:06:47 +00002971 return isl_basic_map_contains(bset_to_bmap(bset), vec);
Tobias Grosser52a25232015-02-04 20:55:43 +00002972}
2973
2974struct isl_basic_map *isl_basic_map_intersect(
2975 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2976{
2977 struct isl_vec *sample = NULL;
2978
2979 if (!bmap1 || !bmap2)
2980 goto error;
2981
2982 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
2983 bmap2->dim, isl_dim_param), goto error);
2984 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
2985 isl_space_dim(bmap1->dim, isl_dim_param) &&
2986 isl_space_dim(bmap2->dim, isl_dim_all) !=
2987 isl_space_dim(bmap2->dim, isl_dim_param))
2988 return isl_basic_map_intersect(bmap2, bmap1);
2989
2990 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
2991 isl_space_dim(bmap2->dim, isl_dim_param))
2992 isl_assert(bmap1->ctx,
2993 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
2994
Tobias Grosserfb3fb0a2015-11-21 20:48:39 +00002995 if (isl_basic_map_plain_is_empty(bmap1)) {
2996 isl_basic_map_free(bmap2);
2997 return bmap1;
2998 }
2999 if (isl_basic_map_plain_is_empty(bmap2)) {
3000 isl_basic_map_free(bmap1);
3001 return bmap2;
3002 }
3003
Tobias Grosser52a25232015-02-04 20:55:43 +00003004 if (bmap1->sample &&
3005 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
3006 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
3007 sample = isl_vec_copy(bmap1->sample);
3008 else if (bmap2->sample &&
3009 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
3010 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
3011 sample = isl_vec_copy(bmap2->sample);
3012
3013 bmap1 = isl_basic_map_cow(bmap1);
3014 if (!bmap1)
3015 goto error;
3016 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
3017 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
3018 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
3019
3020 if (!bmap1)
3021 isl_vec_free(sample);
3022 else if (sample) {
3023 isl_vec_free(bmap1->sample);
3024 bmap1->sample = sample;
3025 }
3026
3027 bmap1 = isl_basic_map_simplify(bmap1);
3028 return isl_basic_map_finalize(bmap1);
3029error:
3030 if (sample)
3031 isl_vec_free(sample);
3032 isl_basic_map_free(bmap1);
3033 isl_basic_map_free(bmap2);
3034 return NULL;
3035}
3036
3037struct isl_basic_set *isl_basic_set_intersect(
3038 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3039{
Tobias Grosser06e15922016-11-16 11:06:47 +00003040 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1),
3041 bset_to_bmap(bset2)));
Tobias Grosser52a25232015-02-04 20:55:43 +00003042}
3043
3044__isl_give isl_basic_set *isl_basic_set_intersect_params(
3045 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
3046{
3047 return isl_basic_set_intersect(bset1, bset2);
3048}
3049
3050/* Special case of isl_map_intersect, where both map1 and map2
3051 * are convex, without any divs and such that either map1 or map2
3052 * contains a single constraint. This constraint is then simply
3053 * added to the other map.
3054 */
3055static __isl_give isl_map *map_intersect_add_constraint(
3056 __isl_take isl_map *map1, __isl_take isl_map *map2)
3057{
3058 isl_assert(map1->ctx, map1->n == 1, goto error);
3059 isl_assert(map2->ctx, map1->n == 1, goto error);
3060 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
3061 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
3062
3063 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
3064 return isl_map_intersect(map2, map1);
3065
3066 isl_assert(map2->ctx,
3067 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
3068
3069 map1 = isl_map_cow(map1);
3070 if (!map1)
3071 goto error;
3072 if (isl_map_plain_is_empty(map1)) {
3073 isl_map_free(map2);
3074 return map1;
3075 }
3076 map1->p[0] = isl_basic_map_cow(map1->p[0]);
3077 if (map2->p[0]->n_eq == 1)
3078 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
3079 else
3080 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
3081 map2->p[0]->ineq[0]);
3082
3083 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
3084 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
3085 if (!map1->p[0])
3086 goto error;
3087
3088 if (isl_basic_map_plain_is_empty(map1->p[0])) {
3089 isl_basic_map_free(map1->p[0]);
3090 map1->n = 0;
3091 }
3092
3093 isl_map_free(map2);
3094
3095 return map1;
3096error:
3097 isl_map_free(map1);
3098 isl_map_free(map2);
3099 return NULL;
3100}
3101
3102/* map2 may be either a parameter domain or a map living in the same
3103 * space as map1.
3104 */
3105static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
3106 __isl_take isl_map *map2)
3107{
3108 unsigned flags = 0;
3109 isl_map *result;
3110 int i, j;
3111
3112 if (!map1 || !map2)
3113 goto error;
3114
3115 if ((isl_map_plain_is_empty(map1) ||
3116 isl_map_plain_is_universe(map2)) &&
3117 isl_space_is_equal(map1->dim, map2->dim)) {
3118 isl_map_free(map2);
3119 return map1;
3120 }
3121 if ((isl_map_plain_is_empty(map2) ||
3122 isl_map_plain_is_universe(map1)) &&
3123 isl_space_is_equal(map1->dim, map2->dim)) {
3124 isl_map_free(map1);
3125 return map2;
3126 }
3127
3128 if (map1->n == 1 && map2->n == 1 &&
3129 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
3130 isl_space_is_equal(map1->dim, map2->dim) &&
3131 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
3132 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
3133 return map_intersect_add_constraint(map1, map2);
3134
3135 if (isl_space_dim(map2->dim, isl_dim_all) !=
3136 isl_space_dim(map2->dim, isl_dim_param))
3137 isl_assert(map1->ctx,
3138 isl_space_is_equal(map1->dim, map2->dim), goto error);
3139
3140 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
3141 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3142 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3143
3144 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3145 map1->n * map2->n, flags);
3146 if (!result)
3147 goto error;
3148 for (i = 0; i < map1->n; ++i)
3149 for (j = 0; j < map2->n; ++j) {
3150 struct isl_basic_map *part;
3151 part = isl_basic_map_intersect(
3152 isl_basic_map_copy(map1->p[i]),
3153 isl_basic_map_copy(map2->p[j]));
3154 if (isl_basic_map_is_empty(part) < 0)
3155 part = isl_basic_map_free(part);
3156 result = isl_map_add_basic_map(result, part);
3157 if (!result)
3158 goto error;
3159 }
3160 isl_map_free(map1);
3161 isl_map_free(map2);
3162 return result;
3163error:
3164 isl_map_free(map1);
3165 isl_map_free(map2);
3166 return NULL;
3167}
3168
3169static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
3170 __isl_take isl_map *map2)
3171{
3172 if (!map1 || !map2)
3173 goto error;
3174 if (!isl_space_is_equal(map1->dim, map2->dim))
3175 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
3176 "spaces don't match", goto error);
3177 return map_intersect_internal(map1, map2);
3178error:
3179 isl_map_free(map1);
3180 isl_map_free(map2);
3181 return NULL;
3182}
3183
3184__isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
3185 __isl_take isl_map *map2)
3186{
3187 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
3188}
3189
3190struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
3191{
Tobias Grosser06e15922016-11-16 11:06:47 +00003192 return set_from_map(isl_map_intersect(set_to_map(set1),
3193 set_to_map(set2)));
Tobias Grosser52a25232015-02-04 20:55:43 +00003194}
3195
3196/* map_intersect_internal accepts intersections
3197 * with parameter domains, so we can just call that function.
3198 */
3199static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
3200 __isl_take isl_set *params)
3201{
3202 return map_intersect_internal(map, params);
3203}
3204
3205__isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
3206 __isl_take isl_map *map2)
3207{
3208 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
3209}
3210
3211__isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
3212 __isl_take isl_set *params)
3213{
3214 return isl_map_intersect_params(set, params);
3215}
3216
3217struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
3218{
Tobias Grosserb2f39922015-05-28 13:32:11 +00003219 isl_space *space;
3220 unsigned pos, n1, n2;
Tobias Grosser52a25232015-02-04 20:55:43 +00003221
3222 if (!bmap)
3223 return NULL;
3224 bmap = isl_basic_map_cow(bmap);
3225 if (!bmap)
3226 return NULL;
Tobias Grosserb2f39922015-05-28 13:32:11 +00003227 space = isl_space_reverse(isl_space_copy(bmap->dim));
3228 pos = isl_basic_map_offset(bmap, isl_dim_in);
3229 n1 = isl_basic_map_dim(bmap, isl_dim_in);
3230 n2 = isl_basic_map_dim(bmap, isl_dim_out);
3231 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
3232 return isl_basic_map_reset_space(bmap, space);
Tobias Grosser52a25232015-02-04 20:55:43 +00003233}
3234
3235static __isl_give isl_basic_map *basic_map_space_reset(
3236 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
3237{
3238 isl_space *space;
3239
3240 if (!bmap)
3241 return NULL;
3242 if (!isl_space_is_named_or_nested(bmap->dim, type))
3243 return bmap;
3244
3245 space = isl_basic_map_get_space(bmap);
3246 space = isl_space_reset(space, type);
3247 bmap = isl_basic_map_reset_space(bmap, space);
3248 return bmap;
3249}
3250
3251__isl_give isl_basic_map *isl_basic_map_insert_dims(
3252 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3253 unsigned pos, unsigned n)
3254{
3255 isl_space *res_dim;
3256 struct isl_basic_map *res;
3257 struct isl_dim_map *dim_map;
3258 unsigned total, off;
3259 enum isl_dim_type t;
3260
3261 if (n == 0)
3262 return basic_map_space_reset(bmap, type);
3263
3264 if (!bmap)
3265 return NULL;
3266
3267 res_dim = isl_space_insert_dims(isl_basic_map_get_space(bmap), type, pos, n);
3268
3269 total = isl_basic_map_total_dim(bmap) + n;
3270 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3271 off = 0;
3272 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3273 if (t != type) {
3274 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3275 } else {
3276 unsigned size = isl_basic_map_dim(bmap, t);
3277 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3278 0, pos, off);
3279 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3280 pos, size - pos, off + pos + n);
3281 }
3282 off += isl_space_dim(res_dim, t);
3283 }
3284 isl_dim_map_div(dim_map, bmap, off);
3285
3286 res = isl_basic_map_alloc_space(res_dim,
3287 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3288 if (isl_basic_map_is_rational(bmap))
3289 res = isl_basic_map_set_rational(res);
3290 if (isl_basic_map_plain_is_empty(bmap)) {
3291 isl_basic_map_free(bmap);
3292 free(dim_map);
3293 return isl_basic_map_set_to_empty(res);
3294 }
3295 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3296 return isl_basic_map_finalize(res);
3297}
3298
3299__isl_give isl_basic_set *isl_basic_set_insert_dims(
3300 __isl_take isl_basic_set *bset,
3301 enum isl_dim_type type, unsigned pos, unsigned n)
3302{
3303 return isl_basic_map_insert_dims(bset, type, pos, n);
3304}
3305
Tobias Grossere0f8d592015-05-13 13:10:13 +00003306__isl_give isl_basic_map *isl_basic_map_add_dims(__isl_take isl_basic_map *bmap,
Tobias Grosser52a25232015-02-04 20:55:43 +00003307 enum isl_dim_type type, unsigned n)
3308{
3309 if (!bmap)
3310 return NULL;
3311 return isl_basic_map_insert_dims(bmap, type,
3312 isl_basic_map_dim(bmap, type), n);
3313}
3314
3315__isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
3316 enum isl_dim_type type, unsigned n)
3317{
3318 if (!bset)
3319 return NULL;
3320 isl_assert(bset->ctx, type != isl_dim_in, goto error);
Tobias Grossere0f8d592015-05-13 13:10:13 +00003321 return isl_basic_map_add_dims(bset, type, n);
Tobias Grosser52a25232015-02-04 20:55:43 +00003322error:
3323 isl_basic_set_free(bset);
3324 return NULL;
3325}
3326
3327static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
3328 enum isl_dim_type type)
3329{
3330 isl_space *space;
3331
3332 if (!map || !isl_space_is_named_or_nested(map->dim, type))
3333 return map;
3334
3335 space = isl_map_get_space(map);
3336 space = isl_space_reset(space, type);
3337 map = isl_map_reset_space(map, space);
3338 return map;
3339}
3340
3341__isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
3342 enum isl_dim_type type, unsigned pos, unsigned n)
3343{
3344 int i;
3345
3346 if (n == 0)
3347 return map_space_reset(map, type);
3348
3349 map = isl_map_cow(map);
3350 if (!map)
3351 return NULL;
3352
3353 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
3354 if (!map->dim)
3355 goto error;
3356
3357 for (i = 0; i < map->n; ++i) {
3358 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
3359 if (!map->p[i])
3360 goto error;
3361 }
3362
3363 return map;
3364error:
3365 isl_map_free(map);
3366 return NULL;
3367}
3368
3369__isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
3370 enum isl_dim_type type, unsigned pos, unsigned n)
3371{
3372 return isl_map_insert_dims(set, type, pos, n);
3373}
3374
3375__isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
3376 enum isl_dim_type type, unsigned n)
3377{
3378 if (!map)
3379 return NULL;
3380 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
3381}
3382
3383__isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
3384 enum isl_dim_type type, unsigned n)
3385{
3386 if (!set)
3387 return NULL;
3388 isl_assert(set->ctx, type != isl_dim_in, goto error);
Tobias Grosser06e15922016-11-16 11:06:47 +00003389 return set_from_map(isl_map_add_dims(set_to_map(set), type, n));
Tobias Grosser52a25232015-02-04 20:55:43 +00003390error:
3391 isl_set_free(set);
3392 return NULL;
3393}
3394
3395__isl_give isl_basic_map *isl_basic_map_move_dims(
3396 __isl_take isl_basic_map *bmap,
3397 enum isl_dim_type dst_type, unsigned dst_pos,
3398 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3399{
3400 struct isl_dim_map *dim_map;
3401 struct isl_basic_map *res;
3402 enum isl_dim_type t;
3403 unsigned total, off;
3404
3405 if (!bmap)
3406 return NULL;
3407 if (n == 0)
3408 return bmap;
3409
3410 isl_assert(bmap->ctx, src_pos + n <= isl_basic_map_dim(bmap, src_type),
3411 goto error);
3412
3413 if (dst_type == src_type && dst_pos == src_pos)
3414 return bmap;
3415
3416 isl_assert(bmap->ctx, dst_type != src_type, goto error);
3417
3418 if (pos(bmap->dim, dst_type) + dst_pos ==
3419 pos(bmap->dim, src_type) + src_pos +
3420 ((src_type < dst_type) ? n : 0)) {
3421 bmap = isl_basic_map_cow(bmap);
3422 if (!bmap)
3423 return NULL;
3424
3425 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3426 src_type, src_pos, n);
3427 if (!bmap->dim)
3428 goto error;
3429
3430 bmap = isl_basic_map_finalize(bmap);
3431
3432 return bmap;
3433 }
3434
3435 total = isl_basic_map_total_dim(bmap);
3436 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3437
3438 off = 0;
3439 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3440 unsigned size = isl_space_dim(bmap->dim, t);
3441 if (t == dst_type) {
3442 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3443 0, dst_pos, off);
3444 off += dst_pos;
3445 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
3446 src_pos, n, off);
3447 off += n;
3448 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3449 dst_pos, size - dst_pos, off);
3450 off += size - dst_pos;
3451 } else if (t == src_type) {
3452 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3453 0, src_pos, off);
3454 off += src_pos;
3455 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3456 src_pos + n, size - src_pos - n, off);
3457 off += size - src_pos - n;
3458 } else {
3459 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3460 off += size;
3461 }
3462 }
3463 isl_dim_map_div(dim_map, bmap, off);
3464
3465 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3466 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3467 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3468 if (!bmap)
3469 goto error;
3470
3471 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3472 src_type, src_pos, n);
3473 if (!bmap->dim)
3474 goto error;
3475
3476 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
3477 bmap = isl_basic_map_gauss(bmap, NULL);
3478 bmap = isl_basic_map_finalize(bmap);
3479
3480 return bmap;
3481error:
3482 isl_basic_map_free(bmap);
3483 return NULL;
3484}
3485
3486__isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
3487 enum isl_dim_type dst_type, unsigned dst_pos,
3488 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3489{
Tobias Grosser06e15922016-11-16 11:06:47 +00003490 isl_basic_map *bmap = bset_to_bmap(bset);
3491 bmap = isl_basic_map_move_dims(bmap, dst_type, dst_pos,
3492 src_type, src_pos, n);
3493 return bset_from_bmap(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +00003494}
3495
3496__isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
3497 enum isl_dim_type dst_type, unsigned dst_pos,
3498 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3499{
3500 if (!set)
3501 return NULL;
3502 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
Tobias Grosser06e15922016-11-16 11:06:47 +00003503 return set_from_map(isl_map_move_dims(set_to_map(set),
3504 dst_type, dst_pos, src_type, src_pos, n));
Tobias Grosser52a25232015-02-04 20:55:43 +00003505error:
3506 isl_set_free(set);
3507 return NULL;
3508}
3509
3510__isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
3511 enum isl_dim_type dst_type, unsigned dst_pos,
3512 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3513{
3514 int i;
3515
3516 if (!map)
3517 return NULL;
3518 if (n == 0)
3519 return map;
3520
3521 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
3522 goto error);
3523
3524 if (dst_type == src_type && dst_pos == src_pos)
3525 return map;
3526
3527 isl_assert(map->ctx, dst_type != src_type, goto error);
3528
3529 map = isl_map_cow(map);
3530 if (!map)
3531 return NULL;
3532
3533 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
3534 if (!map->dim)
3535 goto error;
3536
3537 for (i = 0; i < map->n; ++i) {
3538 map->p[i] = isl_basic_map_move_dims(map->p[i],
3539 dst_type, dst_pos,
3540 src_type, src_pos, n);
3541 if (!map->p[i])
3542 goto error;
3543 }
3544
3545 return map;
3546error:
3547 isl_map_free(map);
3548 return NULL;
3549}
3550
3551/* Move the specified dimensions to the last columns right before
3552 * the divs. Don't change the dimension specification of bmap.
3553 * That's the responsibility of the caller.
3554 */
3555static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
3556 enum isl_dim_type type, unsigned first, unsigned n)
3557{
3558 struct isl_dim_map *dim_map;
3559 struct isl_basic_map *res;
3560 enum isl_dim_type t;
3561 unsigned total, off;
3562
3563 if (!bmap)
3564 return NULL;
3565 if (pos(bmap->dim, type) + first + n ==
3566 1 + isl_space_dim(bmap->dim, isl_dim_all))
3567 return bmap;
3568
3569 total = isl_basic_map_total_dim(bmap);
3570 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3571
3572 off = 0;
3573 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3574 unsigned size = isl_space_dim(bmap->dim, t);
3575 if (t == type) {
3576 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3577 0, first, off);
3578 off += first;
3579 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3580 first, n, total - bmap->n_div - n);
3581 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3582 first + n, size - (first + n), off);
3583 off += size - (first + n);
3584 } else {
3585 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3586 off += size;
3587 }
3588 }
3589 isl_dim_map_div(dim_map, bmap, off + n);
3590
3591 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3592 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3593 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3594 return res;
3595}
3596
3597/* Insert "n" rows in the divs of "bmap".
3598 *
3599 * The number of columns is not changed, which means that the last
3600 * dimensions of "bmap" are being reintepreted as the new divs.
3601 * The space of "bmap" is not adjusted, however, which means
3602 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
3603 * from the space of "bmap" is the responsibility of the caller.
3604 */
3605static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
3606 int n)
3607{
3608 int i;
3609 size_t row_size;
3610 isl_int **new_div;
3611 isl_int *old;
3612
3613 bmap = isl_basic_map_cow(bmap);
3614 if (!bmap)
3615 return NULL;
3616
3617 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
3618 old = bmap->block2.data;
3619 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
3620 (bmap->extra + n) * (1 + row_size));
3621 if (!bmap->block2.data)
3622 return isl_basic_map_free(bmap);
3623 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
3624 if (!new_div)
3625 return isl_basic_map_free(bmap);
3626 for (i = 0; i < n; ++i) {
3627 new_div[i] = bmap->block2.data +
3628 (bmap->extra + i) * (1 + row_size);
3629 isl_seq_clr(new_div[i], 1 + row_size);
3630 }
3631 for (i = 0; i < bmap->extra; ++i)
3632 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
3633 free(bmap->div);
3634 bmap->div = new_div;
3635 bmap->n_div += n;
3636 bmap->extra += n;
3637
3638 return bmap;
3639}
3640
Tobias Grossera67ac972016-02-26 11:35:12 +00003641/* Drop constraints from "bmap" that only involve the variables
3642 * of "type" in the range [first, first + n] that are not related
3643 * to any of the variables outside that interval.
3644 * These constraints cannot influence the values for the variables
3645 * outside the interval, except in case they cause "bmap" to be empty.
3646 * Only drop the constraints if "bmap" is known to be non-empty.
3647 */
3648static __isl_give isl_basic_map *drop_irrelevant_constraints(
3649 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3650 unsigned first, unsigned n)
3651{
3652 int i;
3653 int *groups;
3654 unsigned dim, n_div;
3655 isl_bool non_empty;
3656
3657 non_empty = isl_basic_map_plain_is_non_empty(bmap);
3658 if (non_empty < 0)
3659 return isl_basic_map_free(bmap);
3660 if (!non_empty)
3661 return bmap;
3662
3663 dim = isl_basic_map_dim(bmap, isl_dim_all);
3664 n_div = isl_basic_map_dim(bmap, isl_dim_div);
3665 groups = isl_calloc_array(isl_basic_map_get_ctx(bmap), int, dim);
3666 if (!groups)
3667 return isl_basic_map_free(bmap);
3668 first += isl_basic_map_offset(bmap, type) - 1;
3669 for (i = 0; i < first; ++i)
3670 groups[i] = -1;
3671 for (i = first + n; i < dim - n_div; ++i)
3672 groups[i] = -1;
3673
3674 bmap = isl_basic_map_drop_unrelated_constraints(bmap, groups);
3675
3676 return bmap;
3677}
3678
Tobias Grosser52a25232015-02-04 20:55:43 +00003679/* Turn the n dimensions of type type, starting at first
3680 * into existentially quantified variables.
Tobias Grossera67ac972016-02-26 11:35:12 +00003681 *
3682 * If a subset of the projected out variables are unrelated
3683 * to any of the variables that remain, then the constraints
3684 * involving this subset are simply dropped first.
Tobias Grosser52a25232015-02-04 20:55:43 +00003685 */
3686__isl_give isl_basic_map *isl_basic_map_project_out(
3687 __isl_take isl_basic_map *bmap,
3688 enum isl_dim_type type, unsigned first, unsigned n)
3689{
3690 if (n == 0)
3691 return basic_map_space_reset(bmap, type);
Tobias Grossera67ac972016-02-26 11:35:12 +00003692 if (type == isl_dim_div)
3693 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3694 "cannot project out existentially quantified variables",
3695 return isl_basic_map_free(bmap));
Tobias Grosser52a25232015-02-04 20:55:43 +00003696
Tobias Grossera67ac972016-02-26 11:35:12 +00003697 bmap = drop_irrelevant_constraints(bmap, type, first, n);
Tobias Grosser52a25232015-02-04 20:55:43 +00003698 if (!bmap)
3699 return NULL;
3700
3701 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
3702 return isl_basic_map_remove_dims(bmap, type, first, n);
3703
3704 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
3705 goto error);
3706
3707 bmap = move_last(bmap, type, first, n);
3708 bmap = isl_basic_map_cow(bmap);
3709 bmap = insert_div_rows(bmap, n);
3710 if (!bmap)
3711 return NULL;
3712
3713 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
3714 if (!bmap->dim)
3715 goto error;
3716 bmap = isl_basic_map_simplify(bmap);
3717 bmap = isl_basic_map_drop_redundant_divs(bmap);
3718 return isl_basic_map_finalize(bmap);
3719error:
3720 isl_basic_map_free(bmap);
3721 return NULL;
3722}
3723
3724/* Turn the n dimensions of type type, starting at first
3725 * into existentially quantified variables.
3726 */
3727struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
3728 enum isl_dim_type type, unsigned first, unsigned n)
3729{
Tobias Grosser06e15922016-11-16 11:06:47 +00003730 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset),
3731 type, first, n));
Tobias Grosser52a25232015-02-04 20:55:43 +00003732}
3733
3734/* Turn the n dimensions of type type, starting at first
3735 * into existentially quantified variables.
3736 */
3737__isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
3738 enum isl_dim_type type, unsigned first, unsigned n)
3739{
3740 int i;
3741
3742 if (!map)
3743 return NULL;
3744
3745 if (n == 0)
3746 return map_space_reset(map, type);
3747
3748 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
3749
3750 map = isl_map_cow(map);
3751 if (!map)
3752 return NULL;
3753
3754 map->dim = isl_space_drop_dims(map->dim, type, first, n);
3755 if (!map->dim)
3756 goto error;
3757
3758 for (i = 0; i < map->n; ++i) {
3759 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
3760 if (!map->p[i])
3761 goto error;
3762 }
3763
3764 return map;
3765error:
3766 isl_map_free(map);
3767 return NULL;
3768}
3769
3770/* Turn the n dimensions of type type, starting at first
3771 * into existentially quantified variables.
3772 */
3773__isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
3774 enum isl_dim_type type, unsigned first, unsigned n)
3775{
Tobias Grosser06e15922016-11-16 11:06:47 +00003776 return set_from_map(isl_map_project_out(set_to_map(set),
3777 type, first, n));
Tobias Grosser52a25232015-02-04 20:55:43 +00003778}
3779
Michael Krusee0b34f32016-05-04 14:41:36 +00003780/* Return a map that projects the elements in "set" onto their
3781 * "n" set dimensions starting at "first".
3782 * "type" should be equal to isl_dim_set.
3783 */
3784__isl_give isl_map *isl_set_project_onto_map(__isl_take isl_set *set,
3785 enum isl_dim_type type, unsigned first, unsigned n)
3786{
3787 int i;
3788 int dim;
3789 isl_map *map;
3790
3791 if (!set)
3792 return NULL;
3793 if (type != isl_dim_set)
3794 isl_die(isl_set_get_ctx(set), isl_error_invalid,
3795 "only set dimensions can be projected out", goto error);
3796 dim = isl_set_dim(set, isl_dim_set);
3797 if (first + n > dim || first + n < first)
3798 isl_die(isl_set_get_ctx(set), isl_error_invalid,
3799 "index out of bounds", goto error);
3800
3801 map = isl_map_from_domain(set);
3802 map = isl_map_add_dims(map, isl_dim_out, n);
3803 for (i = 0; i < n; ++i)
3804 map = isl_map_equate(map, isl_dim_in, first + i,
3805 isl_dim_out, i);
3806 return map;
3807error:
3808 isl_set_free(set);
3809 return NULL;
3810}
3811
Tobias Grosser52a25232015-02-04 20:55:43 +00003812static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
3813{
3814 int i, j;
3815
3816 for (i = 0; i < n; ++i) {
3817 j = isl_basic_map_alloc_div(bmap);
3818 if (j < 0)
3819 goto error;
3820 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
3821 }
3822 return bmap;
3823error:
3824 isl_basic_map_free(bmap);
3825 return NULL;
3826}
3827
3828struct isl_basic_map *isl_basic_map_apply_range(
3829 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3830{
3831 isl_space *dim_result = NULL;
3832 struct isl_basic_map *bmap;
3833 unsigned n_in, n_out, n, nparam, total, pos;
3834 struct isl_dim_map *dim_map1, *dim_map2;
3835
3836 if (!bmap1 || !bmap2)
3837 goto error;
3838 if (!isl_space_match(bmap1->dim, isl_dim_param,
3839 bmap2->dim, isl_dim_param))
3840 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
3841 "parameters don't match", goto error);
3842 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_out,
3843 bmap2->dim, isl_dim_in))
3844 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
3845 "spaces don't match", goto error);
3846
3847 dim_result = isl_space_join(isl_space_copy(bmap1->dim),
3848 isl_space_copy(bmap2->dim));
3849
3850 n_in = isl_basic_map_n_in(bmap1);
3851 n_out = isl_basic_map_n_out(bmap2);
3852 n = isl_basic_map_n_out(bmap1);
3853 nparam = isl_basic_map_n_param(bmap1);
3854
3855 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
3856 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3857 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
3858 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3859 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
3860 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3861 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
3862 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
3863 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3864 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3865 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3866
3867 bmap = isl_basic_map_alloc_space(dim_result,
3868 bmap1->n_div + bmap2->n_div + n,
3869 bmap1->n_eq + bmap2->n_eq,
3870 bmap1->n_ineq + bmap2->n_ineq);
3871 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3872 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3873 bmap = add_divs(bmap, n);
3874 bmap = isl_basic_map_simplify(bmap);
3875 bmap = isl_basic_map_drop_redundant_divs(bmap);
3876 return isl_basic_map_finalize(bmap);
3877error:
3878 isl_basic_map_free(bmap1);
3879 isl_basic_map_free(bmap2);
3880 return NULL;
3881}
3882
3883struct isl_basic_set *isl_basic_set_apply(
3884 struct isl_basic_set *bset, struct isl_basic_map *bmap)
3885{
3886 if (!bset || !bmap)
3887 goto error;
3888
3889 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
3890 goto error);
3891
Tobias Grosser06e15922016-11-16 11:06:47 +00003892 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset),
3893 bmap));
Tobias Grosser52a25232015-02-04 20:55:43 +00003894error:
3895 isl_basic_set_free(bset);
3896 isl_basic_map_free(bmap);
3897 return NULL;
3898}
3899
3900struct isl_basic_map *isl_basic_map_apply_domain(
3901 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3902{
3903 if (!bmap1 || !bmap2)
3904 goto error;
3905
3906 isl_assert(bmap1->ctx,
3907 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
3908 isl_assert(bmap1->ctx,
3909 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
3910 goto error);
3911
3912 bmap1 = isl_basic_map_reverse(bmap1);
3913 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
3914 return isl_basic_map_reverse(bmap1);
3915error:
3916 isl_basic_map_free(bmap1);
3917 isl_basic_map_free(bmap2);
3918 return NULL;
3919}
3920
3921/* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3922 * A \cap B -> f(A) + f(B)
3923 */
3924struct isl_basic_map *isl_basic_map_sum(
3925 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3926{
3927 unsigned n_in, n_out, nparam, total, pos;
3928 struct isl_basic_map *bmap = NULL;
3929 struct isl_dim_map *dim_map1, *dim_map2;
3930 int i;
3931
3932 if (!bmap1 || !bmap2)
3933 goto error;
3934
3935 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
3936 goto error);
3937
3938 nparam = isl_basic_map_n_param(bmap1);
3939 n_in = isl_basic_map_n_in(bmap1);
3940 n_out = isl_basic_map_n_out(bmap1);
3941
3942 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
3943 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3944 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
3945 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3946 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
3947 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3948 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3949 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
3950 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3951 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3952 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
3953
3954 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
3955 bmap1->n_div + bmap2->n_div + 2 * n_out,
3956 bmap1->n_eq + bmap2->n_eq + n_out,
3957 bmap1->n_ineq + bmap2->n_ineq);
3958 for (i = 0; i < n_out; ++i) {
3959 int j = isl_basic_map_alloc_equality(bmap);
3960 if (j < 0)
3961 goto error;
3962 isl_seq_clr(bmap->eq[j], 1+total);
3963 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
3964 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
3965 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
3966 }
3967 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3968 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3969 bmap = add_divs(bmap, 2 * n_out);
3970
3971 bmap = isl_basic_map_simplify(bmap);
3972 return isl_basic_map_finalize(bmap);
3973error:
3974 isl_basic_map_free(bmap);
3975 isl_basic_map_free(bmap1);
3976 isl_basic_map_free(bmap2);
3977 return NULL;
3978}
3979
3980/* Given two maps A -> f(A) and B -> g(B), construct a map
3981 * A \cap B -> f(A) + f(B)
3982 */
3983struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
3984{
3985 struct isl_map *result;
3986 int i, j;
3987
3988 if (!map1 || !map2)
3989 goto error;
3990
3991 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
3992
3993 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3994 map1->n * map2->n, 0);
3995 if (!result)
3996 goto error;
3997 for (i = 0; i < map1->n; ++i)
3998 for (j = 0; j < map2->n; ++j) {
3999 struct isl_basic_map *part;
4000 part = isl_basic_map_sum(
4001 isl_basic_map_copy(map1->p[i]),
4002 isl_basic_map_copy(map2->p[j]));
4003 if (isl_basic_map_is_empty(part))
4004 isl_basic_map_free(part);
4005 else
4006 result = isl_map_add_basic_map(result, part);
4007 if (!result)
4008 goto error;
4009 }
4010 isl_map_free(map1);
4011 isl_map_free(map2);
4012 return result;
4013error:
4014 isl_map_free(map1);
4015 isl_map_free(map2);
4016 return NULL;
4017}
4018
4019__isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
4020 __isl_take isl_set *set2)
4021{
Tobias Grosser06e15922016-11-16 11:06:47 +00004022 return set_from_map(isl_map_sum(set_to_map(set1), set_to_map(set2)));
Tobias Grosser52a25232015-02-04 20:55:43 +00004023}
4024
4025/* Given a basic map A -> f(A), construct A -> -f(A).
4026 */
4027struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
4028{
4029 int i, j;
4030 unsigned off, n;
4031
4032 bmap = isl_basic_map_cow(bmap);
4033 if (!bmap)
4034 return NULL;
4035
4036 n = isl_basic_map_dim(bmap, isl_dim_out);
4037 off = isl_basic_map_offset(bmap, isl_dim_out);
4038 for (i = 0; i < bmap->n_eq; ++i)
4039 for (j = 0; j < n; ++j)
4040 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
4041 for (i = 0; i < bmap->n_ineq; ++i)
4042 for (j = 0; j < n; ++j)
4043 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
4044 for (i = 0; i < bmap->n_div; ++i)
4045 for (j = 0; j < n; ++j)
4046 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
4047 bmap = isl_basic_map_gauss(bmap, NULL);
4048 return isl_basic_map_finalize(bmap);
4049}
4050
4051__isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
4052{
4053 return isl_basic_map_neg(bset);
4054}
4055
4056/* Given a map A -> f(A), construct A -> -f(A).
4057 */
4058struct isl_map *isl_map_neg(struct isl_map *map)
4059{
4060 int i;
4061
4062 map = isl_map_cow(map);
4063 if (!map)
4064 return NULL;
4065
4066 for (i = 0; i < map->n; ++i) {
4067 map->p[i] = isl_basic_map_neg(map->p[i]);
4068 if (!map->p[i])
4069 goto error;
4070 }
4071
4072 return map;
4073error:
4074 isl_map_free(map);
4075 return NULL;
4076}
4077
4078__isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
4079{
Tobias Grosser06e15922016-11-16 11:06:47 +00004080 return set_from_map(isl_map_neg(set_to_map(set)));
Tobias Grosser52a25232015-02-04 20:55:43 +00004081}
4082
4083/* Given a basic map A -> f(A) and an integer d, construct a basic map
4084 * A -> floor(f(A)/d).
4085 */
4086struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
4087 isl_int d)
4088{
4089 unsigned n_in, n_out, nparam, total, pos;
4090 struct isl_basic_map *result = NULL;
4091 struct isl_dim_map *dim_map;
4092 int i;
4093
4094 if (!bmap)
4095 return NULL;
4096
4097 nparam = isl_basic_map_n_param(bmap);
4098 n_in = isl_basic_map_n_in(bmap);
4099 n_out = isl_basic_map_n_out(bmap);
4100
4101 total = nparam + n_in + n_out + bmap->n_div + n_out;
4102 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4103 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
4104 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
4105 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
4106 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
4107
4108 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
4109 bmap->n_div + n_out,
4110 bmap->n_eq, bmap->n_ineq + 2 * n_out);
4111 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
4112 result = add_divs(result, n_out);
4113 for (i = 0; i < n_out; ++i) {
4114 int j;
4115 j = isl_basic_map_alloc_inequality(result);
4116 if (j < 0)
4117 goto error;
4118 isl_seq_clr(result->ineq[j], 1+total);
4119 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
4120 isl_int_set_si(result->ineq[j][1+pos+i], 1);
4121 j = isl_basic_map_alloc_inequality(result);
4122 if (j < 0)
4123 goto error;
4124 isl_seq_clr(result->ineq[j], 1+total);
4125 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
4126 isl_int_set_si(result->ineq[j][1+pos+i], -1);
4127 isl_int_sub_ui(result->ineq[j][0], d, 1);
4128 }
4129
4130 result = isl_basic_map_simplify(result);
4131 return isl_basic_map_finalize(result);
4132error:
4133 isl_basic_map_free(result);
4134 return NULL;
4135}
4136
4137/* Given a map A -> f(A) and an integer d, construct a map
4138 * A -> floor(f(A)/d).
4139 */
4140struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
4141{
4142 int i;
4143
4144 map = isl_map_cow(map);
4145 if (!map)
4146 return NULL;
4147
4148 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4149 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4150 for (i = 0; i < map->n; ++i) {
4151 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
4152 if (!map->p[i])
4153 goto error;
4154 }
4155
4156 return map;
4157error:
4158 isl_map_free(map);
4159 return NULL;
4160}
4161
4162/* Given a map A -> f(A) and an integer d, construct a map
4163 * A -> floor(f(A)/d).
4164 */
4165__isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
4166 __isl_take isl_val *d)
4167{
4168 if (!map || !d)
4169 goto error;
4170 if (!isl_val_is_int(d))
4171 isl_die(isl_val_get_ctx(d), isl_error_invalid,
4172 "expecting integer denominator", goto error);
4173 map = isl_map_floordiv(map, d->n);
4174 isl_val_free(d);
4175 return map;
4176error:
4177 isl_map_free(map);
4178 isl_val_free(d);
4179 return NULL;
4180}
4181
4182static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
4183{
4184 int i;
4185 unsigned nparam;
4186 unsigned n_in;
4187
4188 i = isl_basic_map_alloc_equality(bmap);
4189 if (i < 0)
4190 goto error;
4191 nparam = isl_basic_map_n_param(bmap);
4192 n_in = isl_basic_map_n_in(bmap);
4193 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
4194 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
4195 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
4196 return isl_basic_map_finalize(bmap);
4197error:
4198 isl_basic_map_free(bmap);
4199 return NULL;
4200}
4201
Tobias Grosser06e15922016-11-16 11:06:47 +00004202/* Add a constraint to "bmap" expressing i_pos < o_pos
Tobias Grosser52a25232015-02-04 20:55:43 +00004203 */
4204static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
4205{
4206 int i;
4207 unsigned nparam;
4208 unsigned n_in;
4209
4210 i = isl_basic_map_alloc_inequality(bmap);
4211 if (i < 0)
4212 goto error;
4213 nparam = isl_basic_map_n_param(bmap);
4214 n_in = isl_basic_map_n_in(bmap);
4215 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4216 isl_int_set_si(bmap->ineq[i][0], -1);
4217 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4218 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4219 return isl_basic_map_finalize(bmap);
4220error:
4221 isl_basic_map_free(bmap);
4222 return NULL;
4223}
4224
4225/* Add a constraint to "bmap" expressing i_pos <= o_pos
4226 */
4227static __isl_give isl_basic_map *var_less_or_equal(
4228 __isl_take isl_basic_map *bmap, unsigned pos)
4229{
4230 int i;
4231 unsigned nparam;
4232 unsigned n_in;
4233
4234 i = isl_basic_map_alloc_inequality(bmap);
4235 if (i < 0)
4236 goto error;
4237 nparam = isl_basic_map_n_param(bmap);
4238 n_in = isl_basic_map_n_in(bmap);
4239 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4240 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4241 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4242 return isl_basic_map_finalize(bmap);
4243error:
4244 isl_basic_map_free(bmap);
4245 return NULL;
4246}
4247
Tobias Grosser06e15922016-11-16 11:06:47 +00004248/* Add a constraint to "bmap" expressing i_pos > o_pos
Tobias Grosser52a25232015-02-04 20:55:43 +00004249 */
4250static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
4251{
4252 int i;
4253 unsigned nparam;
4254 unsigned n_in;
4255
4256 i = isl_basic_map_alloc_inequality(bmap);
4257 if (i < 0)
4258 goto error;
4259 nparam = isl_basic_map_n_param(bmap);
4260 n_in = isl_basic_map_n_in(bmap);
4261 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4262 isl_int_set_si(bmap->ineq[i][0], -1);
4263 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4264 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4265 return isl_basic_map_finalize(bmap);
4266error:
4267 isl_basic_map_free(bmap);
4268 return NULL;
4269}
4270
4271/* Add a constraint to "bmap" expressing i_pos >= o_pos
4272 */
4273static __isl_give isl_basic_map *var_more_or_equal(
4274 __isl_take isl_basic_map *bmap, unsigned pos)
4275{
4276 int i;
4277 unsigned nparam;
4278 unsigned n_in;
4279
4280 i = isl_basic_map_alloc_inequality(bmap);
4281 if (i < 0)
4282 goto error;
4283 nparam = isl_basic_map_n_param(bmap);
4284 n_in = isl_basic_map_n_in(bmap);
4285 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4286 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4287 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4288 return isl_basic_map_finalize(bmap);
4289error:
4290 isl_basic_map_free(bmap);
4291 return NULL;
4292}
4293
4294__isl_give isl_basic_map *isl_basic_map_equal(
4295 __isl_take isl_space *dim, unsigned n_equal)
4296{
4297 int i;
4298 struct isl_basic_map *bmap;
4299 bmap = isl_basic_map_alloc_space(dim, 0, n_equal, 0);
4300 if (!bmap)
4301 return NULL;
4302 for (i = 0; i < n_equal && bmap; ++i)
4303 bmap = var_equal(bmap, i);
4304 return isl_basic_map_finalize(bmap);
4305}
4306
4307/* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4308 */
4309__isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *dim,
4310 unsigned pos)
4311{
4312 int i;
4313 struct isl_basic_map *bmap;
4314 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4315 if (!bmap)
4316 return NULL;
4317 for (i = 0; i < pos && bmap; ++i)
4318 bmap = var_equal(bmap, i);
4319 if (bmap)
4320 bmap = var_less(bmap, pos);
4321 return isl_basic_map_finalize(bmap);
4322}
4323
Tobias Grosser06e15922016-11-16 11:06:47 +00004324/* Return a relation on "dim" expressing i_[0..pos] <<= o_[0..pos]
Tobias Grosser52a25232015-02-04 20:55:43 +00004325 */
4326__isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
4327 __isl_take isl_space *dim, unsigned pos)
4328{
4329 int i;
4330 isl_basic_map *bmap;
4331
4332 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4333 for (i = 0; i < pos; ++i)
4334 bmap = var_equal(bmap, i);
4335 bmap = var_less_or_equal(bmap, pos);
4336 return isl_basic_map_finalize(bmap);
4337}
4338
Tobias Grosser06e15922016-11-16 11:06:47 +00004339/* Return a relation on "dim" expressing i_pos > o_pos
Tobias Grosser52a25232015-02-04 20:55:43 +00004340 */
4341__isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *dim,
4342 unsigned pos)
4343{
4344 int i;
4345 struct isl_basic_map *bmap;
4346 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4347 if (!bmap)
4348 return NULL;
4349 for (i = 0; i < pos && bmap; ++i)
4350 bmap = var_equal(bmap, i);
4351 if (bmap)
4352 bmap = var_more(bmap, pos);
4353 return isl_basic_map_finalize(bmap);
4354}
4355
Tobias Grosser06e15922016-11-16 11:06:47 +00004356/* Return a relation on "dim" expressing i_[0..pos] >>= o_[0..pos]
Tobias Grosser52a25232015-02-04 20:55:43 +00004357 */
4358__isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
4359 __isl_take isl_space *dim, unsigned pos)
4360{
4361 int i;
4362 isl_basic_map *bmap;
4363
4364 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4365 for (i = 0; i < pos; ++i)
4366 bmap = var_equal(bmap, i);
4367 bmap = var_more_or_equal(bmap, pos);
4368 return isl_basic_map_finalize(bmap);
4369}
4370
4371static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *dims,
4372 unsigned n, int equal)
4373{
4374 struct isl_map *map;
4375 int i;
4376
4377 if (n == 0 && equal)
4378 return isl_map_universe(dims);
4379
4380 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4381
4382 for (i = 0; i + 1 < n; ++i)
4383 map = isl_map_add_basic_map(map,
4384 isl_basic_map_less_at(isl_space_copy(dims), i));
4385 if (n > 0) {
4386 if (equal)
4387 map = isl_map_add_basic_map(map,
4388 isl_basic_map_less_or_equal_at(dims, n - 1));
4389 else
4390 map = isl_map_add_basic_map(map,
4391 isl_basic_map_less_at(dims, n - 1));
4392 } else
4393 isl_space_free(dims);
4394
4395 return map;
4396}
4397
4398static __isl_give isl_map *map_lex_lte(__isl_take isl_space *dims, int equal)
4399{
4400 if (!dims)
4401 return NULL;
4402 return map_lex_lte_first(dims, dims->n_out, equal);
4403}
4404
4405__isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
4406{
4407 return map_lex_lte_first(dim, n, 0);
4408}
4409
4410__isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
4411{
4412 return map_lex_lte_first(dim, n, 1);
4413}
4414
4415__isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
4416{
4417 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
4418}
4419
4420__isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
4421{
4422 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
4423}
4424
4425static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *dims,
4426 unsigned n, int equal)
4427{
4428 struct isl_map *map;
4429 int i;
4430
4431 if (n == 0 && equal)
4432 return isl_map_universe(dims);
4433
4434 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4435
4436 for (i = 0; i + 1 < n; ++i)
4437 map = isl_map_add_basic_map(map,
4438 isl_basic_map_more_at(isl_space_copy(dims), i));
4439 if (n > 0) {
4440 if (equal)
4441 map = isl_map_add_basic_map(map,
4442 isl_basic_map_more_or_equal_at(dims, n - 1));
4443 else
4444 map = isl_map_add_basic_map(map,
4445 isl_basic_map_more_at(dims, n - 1));
4446 } else
4447 isl_space_free(dims);
4448
4449 return map;
4450}
4451
4452static __isl_give isl_map *map_lex_gte(__isl_take isl_space *dims, int equal)
4453{
4454 if (!dims)
4455 return NULL;
4456 return map_lex_gte_first(dims, dims->n_out, equal);
4457}
4458
4459__isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
4460{
4461 return map_lex_gte_first(dim, n, 0);
4462}
4463
4464__isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
4465{
4466 return map_lex_gte_first(dim, n, 1);
4467}
4468
4469__isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
4470{
4471 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
4472}
4473
4474__isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
4475{
4476 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
4477}
4478
4479__isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
4480 __isl_take isl_set *set2)
4481{
4482 isl_map *map;
4483 map = isl_map_lex_le(isl_set_get_space(set1));
4484 map = isl_map_intersect_domain(map, set1);
4485 map = isl_map_intersect_range(map, set2);
4486 return map;
4487}
4488
4489__isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
4490 __isl_take isl_set *set2)
4491{
4492 isl_map *map;
4493 map = isl_map_lex_lt(isl_set_get_space(set1));
4494 map = isl_map_intersect_domain(map, set1);
4495 map = isl_map_intersect_range(map, set2);
4496 return map;
4497}
4498
4499__isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
4500 __isl_take isl_set *set2)
4501{
4502 isl_map *map;
4503 map = isl_map_lex_ge(isl_set_get_space(set1));
4504 map = isl_map_intersect_domain(map, set1);
4505 map = isl_map_intersect_range(map, set2);
4506 return map;
4507}
4508
4509__isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
4510 __isl_take isl_set *set2)
4511{
4512 isl_map *map;
4513 map = isl_map_lex_gt(isl_set_get_space(set1));
4514 map = isl_map_intersect_domain(map, set1);
4515 map = isl_map_intersect_range(map, set2);
4516 return map;
4517}
4518
4519__isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
4520 __isl_take isl_map *map2)
4521{
4522 isl_map *map;
4523 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
4524 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4525 map = isl_map_apply_range(map, isl_map_reverse(map2));
4526 return map;
4527}
4528
4529__isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
4530 __isl_take isl_map *map2)
4531{
4532 isl_map *map;
4533 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
4534 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4535 map = isl_map_apply_range(map, isl_map_reverse(map2));
4536 return map;
4537}
4538
4539__isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
4540 __isl_take isl_map *map2)
4541{
4542 isl_map *map;
4543 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
4544 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4545 map = isl_map_apply_range(map, isl_map_reverse(map2));
4546 return map;
4547}
4548
4549__isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
4550 __isl_take isl_map *map2)
4551{
4552 isl_map *map;
4553 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
4554 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4555 map = isl_map_apply_range(map, isl_map_reverse(map2));
4556 return map;
4557}
4558
Tobias Grossereab0943e2016-11-22 21:31:59 +00004559static __isl_give isl_basic_map *basic_map_from_basic_set(
Tobias Grosser52a25232015-02-04 20:55:43 +00004560 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4561{
4562 struct isl_basic_map *bmap;
4563
4564 bset = isl_basic_set_cow(bset);
4565 if (!bset || !dim)
4566 goto error;
4567
Tobias Grossereab0943e2016-11-22 21:31:59 +00004568 isl_assert(bset->ctx, isl_space_compatible_internal(bset->dim, dim),
4569 goto error);
Tobias Grosser52a25232015-02-04 20:55:43 +00004570 isl_space_free(bset->dim);
Tobias Grosser06e15922016-11-16 11:06:47 +00004571 bmap = bset_to_bmap(bset);
Tobias Grosser52a25232015-02-04 20:55:43 +00004572 bmap->dim = dim;
4573 return isl_basic_map_finalize(bmap);
4574error:
4575 isl_basic_set_free(bset);
4576 isl_space_free(dim);
4577 return NULL;
4578}
4579
Tobias Grossereab0943e2016-11-22 21:31:59 +00004580__isl_give isl_basic_map *isl_basic_map_from_basic_set(
4581 __isl_take isl_basic_set *bset, __isl_take isl_space *space)
4582{
4583 return basic_map_from_basic_set(bset, space);
4584}
4585
Tobias Grosser52a25232015-02-04 20:55:43 +00004586/* For a div d = floor(f/m), add the constraint
4587 *
4588 * f - m d >= 0
4589 */
4590static int add_upper_div_constraint(__isl_keep isl_basic_map *bmap,
4591 unsigned pos, isl_int *div)
4592{
4593 int i;
4594 unsigned total = isl_basic_map_total_dim(bmap);
4595
4596 i = isl_basic_map_alloc_inequality(bmap);
4597 if (i < 0)
4598 return -1;
4599 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
4600 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
4601
4602 return 0;
4603}
4604
4605/* For a div d = floor(f/m), add the constraint
4606 *
Tobias Grosser07b20952016-06-12 04:30:40 +00004607 * -(f-(m-1)) + m d >= 0
Tobias Grosser52a25232015-02-04 20:55:43 +00004608 */
4609static int add_lower_div_constraint(__isl_keep isl_basic_map *bmap,
4610 unsigned pos, isl_int *div)
4611{
4612 int i;
4613 unsigned total = isl_basic_map_total_dim(bmap);
4614
4615 i = isl_basic_map_alloc_inequality(bmap);
4616 if (i < 0)
4617 return -1;
4618 isl_seq_neg(bmap->ineq[i], div + 1, 1 + total);
4619 isl_int_set(bmap->ineq[i][1 + pos], div[0]);
4620 isl_int_add(bmap->ineq[i][0], bmap->ineq[i][0], bmap->ineq[i][1 + pos]);
4621 isl_int_sub_ui(bmap->ineq[i][0], bmap->ineq[i][0], 1);
4622
4623 return 0;
4624}
4625
4626/* For a div d = floor(f/m), add the constraints
4627 *
4628 * f - m d >= 0
Tobias Grosser07b20952016-06-12 04:30:40 +00004629 * -(f-(m-1)) + m d >= 0
Tobias Grosser52a25232015-02-04 20:55:43 +00004630 *
4631 * Note that the second constraint is the negation of
4632 *
Tobias Grosser07b20952016-06-12 04:30:40 +00004633 * f - m d >= m
Tobias Grosser52a25232015-02-04 20:55:43 +00004634 */
4635int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
4636 unsigned pos, isl_int *div)
4637{
4638 if (add_upper_div_constraint(bmap, pos, div) < 0)
4639 return -1;
4640 if (add_lower_div_constraint(bmap, pos, div) < 0)
4641 return -1;
4642 return 0;
4643}
4644
4645int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
4646 unsigned pos, isl_int *div)
4647{
Tobias Grosser06e15922016-11-16 11:06:47 +00004648 return isl_basic_map_add_div_constraints_var(bset_to_bmap(bset),
Tobias Grosser52a25232015-02-04 20:55:43 +00004649 pos, div);
4650}
4651
4652int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
4653{
4654 unsigned total = isl_basic_map_total_dim(bmap);
4655 unsigned div_pos = total - bmap->n_div + div;
4656
4657 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
4658 bmap->div[div]);
4659}
4660
4661/* For each known div d = floor(f/m), add the constraints
4662 *
4663 * f - m d >= 0
Tobias Grosser07b20952016-06-12 04:30:40 +00004664 * -(f-(m-1)) + m d >= 0
Michael Kruse959a8dc2016-01-15 15:54:45 +00004665 *
4666 * Remove duplicate constraints in case of some these div constraints
4667 * already appear in "bmap".
Tobias Grosser52a25232015-02-04 20:55:43 +00004668 */
4669__isl_give isl_basic_map *isl_basic_map_add_known_div_constraints(
4670 __isl_take isl_basic_map *bmap)
4671{
Tobias Grosser52a25232015-02-04 20:55:43 +00004672 unsigned n_div;
4673
4674 if (!bmap)
4675 return NULL;
4676 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4677 if (n_div == 0)
4678 return bmap;
Tobias Grosser52a25232015-02-04 20:55:43 +00004679
Michael Kruse959a8dc2016-01-15 15:54:45 +00004680 bmap = add_known_div_constraints(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +00004681 bmap = isl_basic_map_remove_duplicate_constraints(bmap, NULL, 0);
4682 bmap = isl_basic_map_finalize(bmap);
4683 return bmap;
4684}
4685
4686/* Add the div constraint of sign "sign" for div "div" of "bmap".
4687 *
4688 * In particular, if this div is of the form d = floor(f/m),
4689 * then add the constraint
4690 *
4691 * f - m d >= 0
4692 *
4693 * if sign < 0 or the constraint
4694 *
Tobias Grosser07b20952016-06-12 04:30:40 +00004695 * -(f-(m-1)) + m d >= 0
Tobias Grosser52a25232015-02-04 20:55:43 +00004696 *
4697 * if sign > 0.
4698 */
4699int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map *bmap,
4700 unsigned div, int sign)
4701{
4702 unsigned total;
4703 unsigned div_pos;
4704
4705 if (!bmap)
4706 return -1;
4707
4708 total = isl_basic_map_total_dim(bmap);
4709 div_pos = total - bmap->n_div + div;
4710
4711 if (sign < 0)
4712 return add_upper_div_constraint(bmap, div_pos, bmap->div[div]);
4713 else
4714 return add_lower_div_constraint(bmap, div_pos, bmap->div[div]);
4715}
4716
4717int isl_basic_set_add_div_constraints(struct isl_basic_set *bset, unsigned div)
4718{
4719 return isl_basic_map_add_div_constraints(bset, div);
4720}
4721
4722struct isl_basic_set *isl_basic_map_underlying_set(
4723 struct isl_basic_map *bmap)
4724{
4725 if (!bmap)
4726 goto error;
4727 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
4728 bmap->n_div == 0 &&
4729 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
4730 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
Tobias Grosser06e15922016-11-16 11:06:47 +00004731 return bset_from_bmap(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +00004732 bmap = isl_basic_map_cow(bmap);
4733 if (!bmap)
4734 goto error;
4735 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
4736 if (!bmap->dim)
4737 goto error;
4738 bmap->extra -= bmap->n_div;
4739 bmap->n_div = 0;
4740 bmap = isl_basic_map_finalize(bmap);
Tobias Grosser06e15922016-11-16 11:06:47 +00004741 return bset_from_bmap(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +00004742error:
4743 isl_basic_map_free(bmap);
4744 return NULL;
4745}
4746
4747__isl_give isl_basic_set *isl_basic_set_underlying_set(
4748 __isl_take isl_basic_set *bset)
4749{
Tobias Grosser06e15922016-11-16 11:06:47 +00004750 return isl_basic_map_underlying_set(bset_to_bmap(bset));
Tobias Grosser52a25232015-02-04 20:55:43 +00004751}
4752
4753/* Replace each element in "list" by the result of applying
Tobias Grosser1fa7b972015-02-16 19:33:40 +00004754 * isl_basic_map_underlying_set to the element.
Tobias Grosser52a25232015-02-04 20:55:43 +00004755 */
Tobias Grosser1fa7b972015-02-16 19:33:40 +00004756__isl_give isl_basic_set_list *isl_basic_map_list_underlying_set(
4757 __isl_take isl_basic_map_list *list)
Tobias Grosser52a25232015-02-04 20:55:43 +00004758{
4759 int i, n;
4760
4761 if (!list)
4762 return NULL;
4763
Tobias Grosser1fa7b972015-02-16 19:33:40 +00004764 n = isl_basic_map_list_n_basic_map(list);
Tobias Grosser52a25232015-02-04 20:55:43 +00004765 for (i = 0; i < n; ++i) {
Tobias Grosser1fa7b972015-02-16 19:33:40 +00004766 isl_basic_map *bmap;
Tobias Grosser52a25232015-02-04 20:55:43 +00004767 isl_basic_set *bset;
4768
Tobias Grosser1fa7b972015-02-16 19:33:40 +00004769 bmap = isl_basic_map_list_get_basic_map(list, i);
4770 bset = isl_basic_set_underlying_set(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +00004771 list = isl_basic_set_list_set_basic_set(list, i, bset);
4772 }
4773
4774 return list;
4775}
4776
4777struct isl_basic_map *isl_basic_map_overlying_set(
4778 struct isl_basic_set *bset, struct isl_basic_map *like)
4779{
4780 struct isl_basic_map *bmap;
4781 struct isl_ctx *ctx;
4782 unsigned total;
4783 int i;
4784
4785 if (!bset || !like)
4786 goto error;
4787 ctx = bset->ctx;
4788 isl_assert(ctx, bset->n_div == 0, goto error);
4789 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
4790 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
4791 goto error);
Tobias Grosser6e3ba332015-08-11 11:31:18 +00004792 if (like->n_div == 0) {
4793 isl_space *space = isl_basic_map_get_space(like);
Tobias Grosser52a25232015-02-04 20:55:43 +00004794 isl_basic_map_free(like);
Tobias Grosser6e3ba332015-08-11 11:31:18 +00004795 return isl_basic_map_reset_space(bset, space);
Tobias Grosser52a25232015-02-04 20:55:43 +00004796 }
4797 bset = isl_basic_set_cow(bset);
4798 if (!bset)
4799 goto error;
4800 total = bset->dim->n_out + bset->extra;
Tobias Grosser06e15922016-11-16 11:06:47 +00004801 bmap = bset_to_bmap(bset);
Tobias Grosser52a25232015-02-04 20:55:43 +00004802 isl_space_free(bmap->dim);
4803 bmap->dim = isl_space_copy(like->dim);
4804 if (!bmap->dim)
4805 goto error;
4806 bmap->n_div = like->n_div;
4807 bmap->extra += like->n_div;
4808 if (bmap->extra) {
4809 unsigned ltotal;
4810 isl_int **div;
4811 ltotal = total - bmap->extra + like->extra;
4812 if (ltotal > total)
4813 ltotal = total;
4814 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
4815 bmap->extra * (1 + 1 + total));
4816 if (isl_blk_is_error(bmap->block2))
4817 goto error;
4818 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
4819 if (!div)
4820 goto error;
4821 bmap->div = div;
4822 for (i = 0; i < bmap->extra; ++i)
4823 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
4824 for (i = 0; i < like->n_div; ++i) {
4825 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
4826 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
4827 }
4828 bmap = isl_basic_map_add_known_div_constraints(bmap);
4829 }
4830 isl_basic_map_free(like);
4831 bmap = isl_basic_map_simplify(bmap);
4832 bmap = isl_basic_map_finalize(bmap);
4833 return bmap;
4834error:
4835 isl_basic_map_free(like);
4836 isl_basic_set_free(bset);
4837 return NULL;
4838}
4839
4840struct isl_basic_set *isl_basic_set_from_underlying_set(
4841 struct isl_basic_set *bset, struct isl_basic_set *like)
4842{
Tobias Grosser06e15922016-11-16 11:06:47 +00004843 return bset_from_bmap(isl_basic_map_overlying_set(bset,
4844 bset_to_bmap(like)));
Tobias Grosser52a25232015-02-04 20:55:43 +00004845}
4846
4847struct isl_set *isl_set_from_underlying_set(
4848 struct isl_set *set, struct isl_basic_set *like)
4849{
4850 int i;
4851
4852 if (!set || !like)
4853 goto error;
4854 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
4855 goto error);
4856 if (isl_space_is_equal(set->dim, like->dim) && like->n_div == 0) {
4857 isl_basic_set_free(like);
4858 return set;
4859 }
4860 set = isl_set_cow(set);
4861 if (!set)
4862 goto error;
4863 for (i = 0; i < set->n; ++i) {
4864 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
4865 isl_basic_set_copy(like));
4866 if (!set->p[i])
4867 goto error;
4868 }
4869 isl_space_free(set->dim);
4870 set->dim = isl_space_copy(like->dim);
4871 if (!set->dim)
4872 goto error;
4873 isl_basic_set_free(like);
4874 return set;
4875error:
4876 isl_basic_set_free(like);
4877 isl_set_free(set);
4878 return NULL;
4879}
4880
4881struct isl_set *isl_map_underlying_set(struct isl_map *map)
4882{
4883 int i;
4884
4885 map = isl_map_cow(map);
4886 if (!map)
4887 return NULL;
4888 map->dim = isl_space_cow(map->dim);
4889 if (!map->dim)
4890 goto error;
4891
4892 for (i = 1; i < map->n; ++i)
4893 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
4894 goto error);
4895 for (i = 0; i < map->n; ++i) {
Tobias Grosser06e15922016-11-16 11:06:47 +00004896 map->p[i] = bset_to_bmap(
4897 isl_basic_map_underlying_set(map->p[i]));
Tobias Grosser52a25232015-02-04 20:55:43 +00004898 if (!map->p[i])
4899 goto error;
4900 }
4901 if (map->n == 0)
4902 map->dim = isl_space_underlying(map->dim, 0);
4903 else {
4904 isl_space_free(map->dim);
4905 map->dim = isl_space_copy(map->p[0]->dim);
4906 }
4907 if (!map->dim)
4908 goto error;
Tobias Grosser06e15922016-11-16 11:06:47 +00004909 return set_from_map(map);
Tobias Grosser52a25232015-02-04 20:55:43 +00004910error:
4911 isl_map_free(map);
4912 return NULL;
4913}
4914
4915struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
4916{
Tobias Grosser06e15922016-11-16 11:06:47 +00004917 return set_from_map(isl_map_underlying_set(set_to_map(set)));
Tobias Grosser52a25232015-02-04 20:55:43 +00004918}
4919
Michael Kruse959a8dc2016-01-15 15:54:45 +00004920/* Replace the space of "bmap" by "space".
4921 *
4922 * If the space of "bmap" is identical to "space" (including the identifiers
4923 * of the input and output dimensions), then simply return the original input.
4924 */
Tobias Grosser52a25232015-02-04 20:55:43 +00004925__isl_give isl_basic_map *isl_basic_map_reset_space(
Tobias Grosser6e3ba332015-08-11 11:31:18 +00004926 __isl_take isl_basic_map *bmap, __isl_take isl_space *space)
Tobias Grosser52a25232015-02-04 20:55:43 +00004927{
Tobias Grosser6e3ba332015-08-11 11:31:18 +00004928 isl_bool equal;
4929
4930 if (!bmap)
4931 goto error;
4932 equal = isl_space_is_equal(bmap->dim, space);
Michael Kruse959a8dc2016-01-15 15:54:45 +00004933 if (equal >= 0 && equal)
4934 equal = isl_space_match(bmap->dim, isl_dim_in,
4935 space, isl_dim_in);
4936 if (equal >= 0 && equal)
4937 equal = isl_space_match(bmap->dim, isl_dim_out,
4938 space, isl_dim_out);
Tobias Grosser6e3ba332015-08-11 11:31:18 +00004939 if (equal < 0)
4940 goto error;
4941 if (equal) {
4942 isl_space_free(space);
4943 return bmap;
4944 }
Tobias Grosser52a25232015-02-04 20:55:43 +00004945 bmap = isl_basic_map_cow(bmap);
Tobias Grosser6e3ba332015-08-11 11:31:18 +00004946 if (!bmap || !space)
Tobias Grosser52a25232015-02-04 20:55:43 +00004947 goto error;
4948
4949 isl_space_free(bmap->dim);
Tobias Grosser6e3ba332015-08-11 11:31:18 +00004950 bmap->dim = space;
Tobias Grosser52a25232015-02-04 20:55:43 +00004951
4952 bmap = isl_basic_map_finalize(bmap);
4953
4954 return bmap;
4955error:
4956 isl_basic_map_free(bmap);
Tobias Grosser6e3ba332015-08-11 11:31:18 +00004957 isl_space_free(space);
Tobias Grosser52a25232015-02-04 20:55:43 +00004958 return NULL;
4959}
4960
4961__isl_give isl_basic_set *isl_basic_set_reset_space(
4962 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4963{
Tobias Grosser06e15922016-11-16 11:06:47 +00004964 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset),
4965 dim));
Tobias Grosser52a25232015-02-04 20:55:43 +00004966}
4967
4968__isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
4969 __isl_take isl_space *dim)
4970{
4971 int i;
4972
4973 map = isl_map_cow(map);
4974 if (!map || !dim)
4975 goto error;
4976
4977 for (i = 0; i < map->n; ++i) {
4978 map->p[i] = isl_basic_map_reset_space(map->p[i],
4979 isl_space_copy(dim));
4980 if (!map->p[i])
4981 goto error;
4982 }
4983 isl_space_free(map->dim);
4984 map->dim = dim;
4985
4986 return map;
4987error:
4988 isl_map_free(map);
4989 isl_space_free(dim);
4990 return NULL;
4991}
4992
4993__isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
4994 __isl_take isl_space *dim)
4995{
Tobias Grosser06e15922016-11-16 11:06:47 +00004996 return set_from_map(isl_map_reset_space(set_to_map(set), dim));
Tobias Grosser52a25232015-02-04 20:55:43 +00004997}
4998
4999/* Compute the parameter domain of the given basic set.
5000 */
5001__isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
5002{
5003 isl_space *space;
5004 unsigned n;
5005
5006 if (isl_basic_set_is_params(bset))
5007 return bset;
5008
5009 n = isl_basic_set_dim(bset, isl_dim_set);
5010 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
5011 space = isl_basic_set_get_space(bset);
5012 space = isl_space_params(space);
5013 bset = isl_basic_set_reset_space(bset, space);
5014 return bset;
5015}
5016
5017/* Construct a zero-dimensional basic set with the given parameter domain.
5018 */
5019__isl_give isl_basic_set *isl_basic_set_from_params(
5020 __isl_take isl_basic_set *bset)
5021{
5022 isl_space *space;
5023 space = isl_basic_set_get_space(bset);
5024 space = isl_space_set_from_params(space);
5025 bset = isl_basic_set_reset_space(bset, space);
5026 return bset;
5027}
5028
5029/* Compute the parameter domain of the given set.
5030 */
5031__isl_give isl_set *isl_set_params(__isl_take isl_set *set)
5032{
5033 isl_space *space;
5034 unsigned n;
5035
5036 if (isl_set_is_params(set))
5037 return set;
5038
5039 n = isl_set_dim(set, isl_dim_set);
5040 set = isl_set_project_out(set, isl_dim_set, 0, n);
5041 space = isl_set_get_space(set);
5042 space = isl_space_params(space);
5043 set = isl_set_reset_space(set, space);
5044 return set;
5045}
5046
5047/* Construct a zero-dimensional set with the given parameter domain.
5048 */
5049__isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
5050{
5051 isl_space *space;
5052 space = isl_set_get_space(set);
5053 space = isl_space_set_from_params(space);
5054 set = isl_set_reset_space(set, space);
5055 return set;
5056}
5057
5058/* Compute the parameter domain of the given map.
5059 */
5060__isl_give isl_set *isl_map_params(__isl_take isl_map *map)
5061{
5062 isl_space *space;
5063 unsigned n;
5064
5065 n = isl_map_dim(map, isl_dim_in);
5066 map = isl_map_project_out(map, isl_dim_in, 0, n);
5067 n = isl_map_dim(map, isl_dim_out);
5068 map = isl_map_project_out(map, isl_dim_out, 0, n);
5069 space = isl_map_get_space(map);
5070 space = isl_space_params(space);
5071 map = isl_map_reset_space(map, space);
5072 return map;
5073}
5074
5075struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
5076{
Tobias Grosserb2f39922015-05-28 13:32:11 +00005077 isl_space *space;
Tobias Grosser52a25232015-02-04 20:55:43 +00005078 unsigned n_out;
5079
5080 if (!bmap)
5081 return NULL;
Tobias Grosserb2f39922015-05-28 13:32:11 +00005082 space = isl_space_domain(isl_basic_map_get_space(bmap));
Tobias Grosser52a25232015-02-04 20:55:43 +00005083
Tobias Grosser52a25232015-02-04 20:55:43 +00005084 n_out = isl_basic_map_n_out(bmap);
Tobias Grosserb2f39922015-05-28 13:32:11 +00005085 bmap = isl_basic_map_project_out(bmap, isl_dim_out, 0, n_out);
Tobias Grosser52a25232015-02-04 20:55:43 +00005086
Tobias Grosserb2f39922015-05-28 13:32:11 +00005087 return isl_basic_map_reset_space(bmap, space);
Tobias Grosser52a25232015-02-04 20:55:43 +00005088}
5089
5090int isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
5091{
5092 if (!bmap)
5093 return -1;
5094 return isl_space_may_be_set(bmap->dim);
5095}
5096
5097/* Is this basic map actually a set?
5098 * Users should never call this function. Outside of isl,
5099 * the type should indicate whether something is a set or a map.
5100 */
5101int isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
5102{
5103 if (!bmap)
5104 return -1;
5105 return isl_space_is_set(bmap->dim);
5106}
5107
5108struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
5109{
5110 if (!bmap)
5111 return NULL;
5112 if (isl_basic_map_is_set(bmap))
5113 return bmap;
5114 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
5115}
5116
5117__isl_give isl_basic_map *isl_basic_map_domain_map(
5118 __isl_take isl_basic_map *bmap)
5119{
5120 int i, k;
5121 isl_space *dim;
5122 isl_basic_map *domain;
5123 int nparam, n_in, n_out;
5124 unsigned total;
5125
5126 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5127 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5128 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5129
5130 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
5131 domain = isl_basic_map_universe(dim);
5132
5133 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5134 bmap = isl_basic_map_apply_range(bmap, domain);
5135 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
5136
5137 total = isl_basic_map_total_dim(bmap);
5138
5139 for (i = 0; i < n_in; ++i) {
5140 k = isl_basic_map_alloc_equality(bmap);
5141 if (k < 0)
5142 goto error;
5143 isl_seq_clr(bmap->eq[k], 1 + total);
5144 isl_int_set_si(bmap->eq[k][1 + nparam + i], -1);
5145 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
5146 }
5147
5148 bmap = isl_basic_map_gauss(bmap, NULL);
5149 return isl_basic_map_finalize(bmap);
5150error:
5151 isl_basic_map_free(bmap);
5152 return NULL;
5153}
5154
5155__isl_give isl_basic_map *isl_basic_map_range_map(
5156 __isl_take isl_basic_map *bmap)
5157{
5158 int i, k;
5159 isl_space *dim;
5160 isl_basic_map *range;
5161 int nparam, n_in, n_out;
5162 unsigned total;
5163
5164 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5165 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5166 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5167
5168 dim = isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap)));
5169 range = isl_basic_map_universe(dim);
5170
5171 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5172 bmap = isl_basic_map_apply_range(bmap, range);
5173 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
5174
5175 total = isl_basic_map_total_dim(bmap);
5176
5177 for (i = 0; i < n_out; ++i) {
5178 k = isl_basic_map_alloc_equality(bmap);
5179 if (k < 0)
5180 goto error;
5181 isl_seq_clr(bmap->eq[k], 1 + total);
5182 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + i], -1);
5183 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
5184 }
5185
5186 bmap = isl_basic_map_gauss(bmap, NULL);
5187 return isl_basic_map_finalize(bmap);
5188error:
5189 isl_basic_map_free(bmap);
5190 return NULL;
5191}
5192
5193int isl_map_may_be_set(__isl_keep isl_map *map)
5194{
5195 if (!map)
5196 return -1;
5197 return isl_space_may_be_set(map->dim);
5198}
5199
5200/* Is this map actually a set?
5201 * Users should never call this function. Outside of isl,
5202 * the type should indicate whether something is a set or a map.
5203 */
5204int isl_map_is_set(__isl_keep isl_map *map)
5205{
5206 if (!map)
5207 return -1;
5208 return isl_space_is_set(map->dim);
5209}
5210
5211struct isl_set *isl_map_range(struct isl_map *map)
5212{
5213 int i;
5214 struct isl_set *set;
5215
5216 if (!map)
5217 goto error;
5218 if (isl_map_is_set(map))
Tobias Grosser06e15922016-11-16 11:06:47 +00005219 return set_from_map(map);
Tobias Grosser52a25232015-02-04 20:55:43 +00005220
5221 map = isl_map_cow(map);
5222 if (!map)
5223 goto error;
5224
Tobias Grosser06e15922016-11-16 11:06:47 +00005225 set = set_from_map(map);
Tobias Grosser52a25232015-02-04 20:55:43 +00005226 set->dim = isl_space_range(set->dim);
5227 if (!set->dim)
5228 goto error;
5229 for (i = 0; i < map->n; ++i) {
5230 set->p[i] = isl_basic_map_range(map->p[i]);
5231 if (!set->p[i])
5232 goto error;
5233 }
5234 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5235 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5236 return set;
5237error:
5238 isl_map_free(map);
5239 return NULL;
5240}
5241
5242__isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
5243{
5244 int i;
5245
5246 map = isl_map_cow(map);
5247 if (!map)
5248 return NULL;
5249
5250 map->dim = isl_space_domain_map(map->dim);
5251 if (!map->dim)
5252 goto error;
5253 for (i = 0; i < map->n; ++i) {
5254 map->p[i] = isl_basic_map_domain_map(map->p[i]);
5255 if (!map->p[i])
5256 goto error;
5257 }
5258 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5259 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5260 return map;
5261error:
5262 isl_map_free(map);
5263 return NULL;
5264}
5265
5266__isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
5267{
5268 int i;
5269 isl_space *range_dim;
5270
5271 map = isl_map_cow(map);
5272 if (!map)
5273 return NULL;
5274
5275 range_dim = isl_space_range(isl_map_get_space(map));
5276 range_dim = isl_space_from_range(range_dim);
5277 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
5278 map->dim = isl_space_join(map->dim, range_dim);
5279 if (!map->dim)
5280 goto error;
5281 for (i = 0; i < map->n; ++i) {
5282 map->p[i] = isl_basic_map_range_map(map->p[i]);
5283 if (!map->p[i])
5284 goto error;
5285 }
5286 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5287 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5288 return map;
5289error:
5290 isl_map_free(map);
5291 return NULL;
5292}
5293
5294/* Given a wrapped map of the form A[B -> C],
5295 * return the map A[B -> C] -> B.
5296 */
5297__isl_give isl_map *isl_set_wrapped_domain_map(__isl_take isl_set *set)
5298{
5299 isl_id *id;
5300 isl_map *map;
5301
5302 if (!set)
5303 return NULL;
5304 if (!isl_set_has_tuple_id(set))
5305 return isl_map_domain_map(isl_set_unwrap(set));
5306
5307 id = isl_set_get_tuple_id(set);
5308 map = isl_map_domain_map(isl_set_unwrap(set));
5309 map = isl_map_set_tuple_id(map, isl_dim_in, id);
5310
5311 return map;
5312}
5313
5314__isl_give isl_map *isl_map_from_set(__isl_take isl_set *set,
5315 __isl_take isl_space *dim)
5316{
5317 int i;
5318 struct isl_map *map = NULL;
5319
5320 set = isl_set_cow(set);
5321 if (!set || !dim)
5322 goto error;
Tobias Grossereab0943e2016-11-22 21:31:59 +00005323 isl_assert(set->ctx, isl_space_compatible_internal(set->dim, dim),
5324 goto error);
Tobias Grosser06e15922016-11-16 11:06:47 +00005325 map = set_to_map(set);
Tobias Grosser52a25232015-02-04 20:55:43 +00005326 for (i = 0; i < set->n; ++i) {
Tobias Grossereab0943e2016-11-22 21:31:59 +00005327 map->p[i] = basic_map_from_basic_set(
Tobias Grosser52a25232015-02-04 20:55:43 +00005328 set->p[i], isl_space_copy(dim));
5329 if (!map->p[i])
5330 goto error;
5331 }
5332 isl_space_free(map->dim);
5333 map->dim = dim;
5334 return map;
5335error:
5336 isl_space_free(dim);
5337 isl_set_free(set);
5338 return NULL;
5339}
5340
5341__isl_give isl_basic_map *isl_basic_map_from_domain(
5342 __isl_take isl_basic_set *bset)
5343{
5344 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
5345}
5346
5347__isl_give isl_basic_map *isl_basic_map_from_range(
5348 __isl_take isl_basic_set *bset)
5349{
5350 isl_space *space;
5351 space = isl_basic_set_get_space(bset);
5352 space = isl_space_from_range(space);
5353 bset = isl_basic_set_reset_space(bset, space);
Tobias Grosser06e15922016-11-16 11:06:47 +00005354 return bset_to_bmap(bset);
Tobias Grosser52a25232015-02-04 20:55:43 +00005355}
5356
5357/* Create a relation with the given set as range.
5358 * The domain of the created relation is a zero-dimensional
5359 * flat anonymous space.
5360 */
5361__isl_give isl_map *isl_map_from_range(__isl_take isl_set *set)
5362{
5363 isl_space *space;
5364 space = isl_set_get_space(set);
5365 space = isl_space_from_range(space);
5366 set = isl_set_reset_space(set, space);
Tobias Grosser06e15922016-11-16 11:06:47 +00005367 return set_to_map(set);
Tobias Grosser52a25232015-02-04 20:55:43 +00005368}
5369
5370/* Create a relation with the given set as domain.
5371 * The range of the created relation is a zero-dimensional
5372 * flat anonymous space.
5373 */
5374__isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
5375{
5376 return isl_map_reverse(isl_map_from_range(set));
5377}
5378
5379__isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
5380 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
5381{
5382 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
5383}
5384
5385__isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
5386 __isl_take isl_set *range)
5387{
5388 return isl_map_apply_range(isl_map_reverse(domain), range);
5389}
5390
Tobias Grosser07b20952016-06-12 04:30:40 +00005391/* Return a newly allocated isl_map with given space and flags and
5392 * room for "n" basic maps.
5393 * Make sure that all cached information is cleared.
5394 */
5395__isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *space, int n,
Tobias Grosser52a25232015-02-04 20:55:43 +00005396 unsigned flags)
5397{
5398 struct isl_map *map;
5399
Tobias Grosser07b20952016-06-12 04:30:40 +00005400 if (!space)
Tobias Grosser52a25232015-02-04 20:55:43 +00005401 return NULL;
5402 if (n < 0)
Tobias Grosser07b20952016-06-12 04:30:40 +00005403 isl_die(space->ctx, isl_error_internal,
Tobias Grosser52a25232015-02-04 20:55:43 +00005404 "negative number of basic maps", goto error);
Tobias Grosser07b20952016-06-12 04:30:40 +00005405 map = isl_calloc(space->ctx, struct isl_map,
Tobias Grosser52a25232015-02-04 20:55:43 +00005406 sizeof(struct isl_map) +
5407 (n - 1) * sizeof(struct isl_basic_map *));
5408 if (!map)
5409 goto error;
5410
Tobias Grosser07b20952016-06-12 04:30:40 +00005411 map->ctx = space->ctx;
Tobias Grosser52a25232015-02-04 20:55:43 +00005412 isl_ctx_ref(map->ctx);
5413 map->ref = 1;
5414 map->size = n;
5415 map->n = 0;
Tobias Grosser07b20952016-06-12 04:30:40 +00005416 map->dim = space;
Tobias Grosser52a25232015-02-04 20:55:43 +00005417 map->flags = flags;
5418 return map;
5419error:
Tobias Grosser07b20952016-06-12 04:30:40 +00005420 isl_space_free(space);
Tobias Grosser52a25232015-02-04 20:55:43 +00005421 return NULL;
5422}
5423
5424struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
5425 unsigned nparam, unsigned in, unsigned out, int n,
5426 unsigned flags)
5427{
5428 struct isl_map *map;
5429 isl_space *dims;
5430
5431 dims = isl_space_alloc(ctx, nparam, in, out);
5432 if (!dims)
5433 return NULL;
5434
5435 map = isl_map_alloc_space(dims, n, flags);
5436 return map;
5437}
5438
5439__isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *dim)
5440{
5441 struct isl_basic_map *bmap;
5442 bmap = isl_basic_map_alloc_space(dim, 0, 1, 0);
5443 bmap = isl_basic_map_set_to_empty(bmap);
5444 return bmap;
5445}
5446
5447__isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *dim)
5448{
5449 struct isl_basic_set *bset;
5450 bset = isl_basic_set_alloc_space(dim, 0, 1, 0);
5451 bset = isl_basic_set_set_to_empty(bset);
5452 return bset;
5453}
5454
Tobias Grosser52a25232015-02-04 20:55:43 +00005455__isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *dim)
5456{
5457 struct isl_basic_map *bmap;
5458 bmap = isl_basic_map_alloc_space(dim, 0, 0, 0);
5459 bmap = isl_basic_map_finalize(bmap);
5460 return bmap;
5461}
5462
5463__isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *dim)
5464{
5465 struct isl_basic_set *bset;
5466 bset = isl_basic_set_alloc_space(dim, 0, 0, 0);
5467 bset = isl_basic_set_finalize(bset);
5468 return bset;
5469}
5470
5471__isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_space *dim)
5472{
5473 int i;
5474 unsigned total = isl_space_dim(dim, isl_dim_all);
5475 isl_basic_map *bmap;
5476
5477 bmap= isl_basic_map_alloc_space(dim, 0, 0, total);
5478 for (i = 0; i < total; ++i) {
5479 int k = isl_basic_map_alloc_inequality(bmap);
5480 if (k < 0)
5481 goto error;
5482 isl_seq_clr(bmap->ineq[k], 1 + total);
5483 isl_int_set_si(bmap->ineq[k][1 + i], 1);
5484 }
5485 return bmap;
5486error:
5487 isl_basic_map_free(bmap);
5488 return NULL;
5489}
5490
5491__isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_space *dim)
5492{
5493 return isl_basic_map_nat_universe(dim);
5494}
5495
5496__isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
5497{
5498 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
5499}
5500
5501__isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
5502{
5503 return isl_map_nat_universe(dim);
5504}
5505
Tobias Grosser52a25232015-02-04 20:55:43 +00005506__isl_give isl_map *isl_map_empty(__isl_take isl_space *dim)
5507{
5508 return isl_map_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5509}
5510
Tobias Grosser52a25232015-02-04 20:55:43 +00005511__isl_give isl_set *isl_set_empty(__isl_take isl_space *dim)
5512{
5513 return isl_set_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5514}
5515
Tobias Grosser52a25232015-02-04 20:55:43 +00005516__isl_give isl_map *isl_map_universe(__isl_take isl_space *dim)
5517{
5518 struct isl_map *map;
5519 if (!dim)
5520 return NULL;
5521 map = isl_map_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5522 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
5523 return map;
5524}
5525
5526__isl_give isl_set *isl_set_universe(__isl_take isl_space *dim)
5527{
5528 struct isl_set *set;
5529 if (!dim)
5530 return NULL;
5531 set = isl_set_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5532 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
5533 return set;
5534}
5535
Tobias Grosser52a25232015-02-04 20:55:43 +00005536struct isl_map *isl_map_dup(struct isl_map *map)
5537{
5538 int i;
5539 struct isl_map *dup;
5540
5541 if (!map)
5542 return NULL;
5543 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
5544 for (i = 0; i < map->n; ++i)
5545 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
5546 return dup;
5547}
5548
5549__isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
5550 __isl_take isl_basic_map *bmap)
5551{
5552 if (!bmap || !map)
5553 goto error;
5554 if (isl_basic_map_plain_is_empty(bmap)) {
5555 isl_basic_map_free(bmap);
5556 return map;
5557 }
5558 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
5559 isl_assert(map->ctx, map->n < map->size, goto error);
5560 map->p[map->n] = bmap;
5561 map->n++;
5562 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5563 return map;
5564error:
5565 if (map)
5566 isl_map_free(map);
5567 if (bmap)
5568 isl_basic_map_free(bmap);
5569 return NULL;
5570}
5571
5572__isl_null isl_map *isl_map_free(__isl_take isl_map *map)
5573{
5574 int i;
5575
5576 if (!map)
5577 return NULL;
5578
5579 if (--map->ref > 0)
5580 return NULL;
5581
Tobias Grosser07b20952016-06-12 04:30:40 +00005582 clear_caches(map);
Tobias Grosser52a25232015-02-04 20:55:43 +00005583 isl_ctx_deref(map->ctx);
5584 for (i = 0; i < map->n; ++i)
5585 isl_basic_map_free(map->p[i]);
5586 isl_space_free(map->dim);
5587 free(map);
5588
5589 return NULL;
5590}
5591
Tobias Grosser52a25232015-02-04 20:55:43 +00005592static struct isl_basic_map *isl_basic_map_fix_pos_si(
5593 struct isl_basic_map *bmap, unsigned pos, int value)
5594{
5595 int j;
5596
5597 bmap = isl_basic_map_cow(bmap);
5598 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5599 j = isl_basic_map_alloc_equality(bmap);
5600 if (j < 0)
5601 goto error;
5602 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5603 isl_int_set_si(bmap->eq[j][pos], -1);
5604 isl_int_set_si(bmap->eq[j][0], value);
5605 bmap = isl_basic_map_simplify(bmap);
5606 return isl_basic_map_finalize(bmap);
5607error:
5608 isl_basic_map_free(bmap);
5609 return NULL;
5610}
5611
5612static __isl_give isl_basic_map *isl_basic_map_fix_pos(
5613 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
5614{
5615 int j;
5616
5617 bmap = isl_basic_map_cow(bmap);
5618 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5619 j = isl_basic_map_alloc_equality(bmap);
5620 if (j < 0)
5621 goto error;
5622 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5623 isl_int_set_si(bmap->eq[j][pos], -1);
5624 isl_int_set(bmap->eq[j][0], value);
5625 bmap = isl_basic_map_simplify(bmap);
5626 return isl_basic_map_finalize(bmap);
5627error:
5628 isl_basic_map_free(bmap);
5629 return NULL;
5630}
5631
5632struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
5633 enum isl_dim_type type, unsigned pos, int value)
5634{
5635 if (!bmap)
5636 return NULL;
5637 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5638 return isl_basic_map_fix_pos_si(bmap,
5639 isl_basic_map_offset(bmap, type) + pos, value);
5640error:
5641 isl_basic_map_free(bmap);
5642 return NULL;
5643}
5644
5645__isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
5646 enum isl_dim_type type, unsigned pos, isl_int value)
5647{
5648 if (!bmap)
5649 return NULL;
5650 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5651 return isl_basic_map_fix_pos(bmap,
5652 isl_basic_map_offset(bmap, type) + pos, value);
5653error:
5654 isl_basic_map_free(bmap);
5655 return NULL;
5656}
5657
5658/* Fix the value of the variable at position "pos" of type "type" of "bmap"
5659 * to be equal to "v".
5660 */
5661__isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
5662 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5663{
5664 if (!bmap || !v)
5665 goto error;
5666 if (!isl_val_is_int(v))
5667 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5668 "expecting integer value", goto error);
5669 if (pos >= isl_basic_map_dim(bmap, type))
5670 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5671 "index out of bounds", goto error);
5672 pos += isl_basic_map_offset(bmap, type);
5673 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
5674 isl_val_free(v);
5675 return bmap;
5676error:
5677 isl_basic_map_free(bmap);
5678 isl_val_free(v);
5679 return NULL;
5680}
5681
5682/* Fix the value of the variable at position "pos" of type "type" of "bset"
5683 * to be equal to "v".
5684 */
5685__isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
5686 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5687{
5688 return isl_basic_map_fix_val(bset, type, pos, v);
5689}
5690
5691struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
5692 enum isl_dim_type type, unsigned pos, int value)
5693{
Tobias Grosser06e15922016-11-16 11:06:47 +00005694 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
5695 type, pos, value));
Tobias Grosser52a25232015-02-04 20:55:43 +00005696}
5697
5698__isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
5699 enum isl_dim_type type, unsigned pos, isl_int value)
5700{
Tobias Grosser06e15922016-11-16 11:06:47 +00005701 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset),
5702 type, pos, value));
Tobias Grosser52a25232015-02-04 20:55:43 +00005703}
5704
5705struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
5706 unsigned input, int value)
5707{
5708 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
5709}
5710
5711struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
5712 unsigned dim, int value)
5713{
Tobias Grosser06e15922016-11-16 11:06:47 +00005714 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
5715 isl_dim_set, dim, value));
Tobias Grosser52a25232015-02-04 20:55:43 +00005716}
5717
5718static int remove_if_empty(__isl_keep isl_map *map, int i)
5719{
5720 int empty = isl_basic_map_plain_is_empty(map->p[i]);
5721
5722 if (empty < 0)
5723 return -1;
5724 if (!empty)
5725 return 0;
5726
5727 isl_basic_map_free(map->p[i]);
5728 if (i != map->n - 1) {
5729 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5730 map->p[i] = map->p[map->n - 1];
5731 }
5732 map->n--;
5733
5734 return 0;
5735}
5736
5737/* Perform "fn" on each basic map of "map", where we may not be holding
5738 * the only reference to "map".
5739 * In particular, "fn" should be a semantics preserving operation
5740 * that we want to apply to all copies of "map". We therefore need
5741 * to be careful not to modify "map" in a way that breaks "map"
5742 * in case anything goes wrong.
5743 */
5744__isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
5745 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
5746{
5747 struct isl_basic_map *bmap;
5748 int i;
5749
5750 if (!map)
5751 return NULL;
5752
5753 for (i = map->n - 1; i >= 0; --i) {
5754 bmap = isl_basic_map_copy(map->p[i]);
5755 bmap = fn(bmap);
5756 if (!bmap)
5757 goto error;
5758 isl_basic_map_free(map->p[i]);
5759 map->p[i] = bmap;
5760 if (remove_if_empty(map, i) < 0)
5761 goto error;
5762 }
5763
5764 return map;
5765error:
5766 isl_map_free(map);
5767 return NULL;
5768}
5769
5770struct isl_map *isl_map_fix_si(struct isl_map *map,
5771 enum isl_dim_type type, unsigned pos, int value)
5772{
5773 int i;
5774
5775 map = isl_map_cow(map);
5776 if (!map)
5777 return NULL;
5778
5779 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5780 for (i = map->n - 1; i >= 0; --i) {
5781 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
5782 if (remove_if_empty(map, i) < 0)
5783 goto error;
5784 }
5785 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5786 return map;
5787error:
5788 isl_map_free(map);
5789 return NULL;
5790}
5791
5792__isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
5793 enum isl_dim_type type, unsigned pos, int value)
5794{
Tobias Grosser06e15922016-11-16 11:06:47 +00005795 return set_from_map(isl_map_fix_si(set_to_map(set), type, pos, value));
Tobias Grosser52a25232015-02-04 20:55:43 +00005796}
5797
5798__isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
5799 enum isl_dim_type type, unsigned pos, isl_int value)
5800{
5801 int i;
5802
5803 map = isl_map_cow(map);
5804 if (!map)
5805 return NULL;
5806
5807 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5808 for (i = 0; i < map->n; ++i) {
5809 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
5810 if (!map->p[i])
5811 goto error;
5812 }
5813 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5814 return map;
5815error:
5816 isl_map_free(map);
5817 return NULL;
5818}
5819
5820__isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
5821 enum isl_dim_type type, unsigned pos, isl_int value)
5822{
Tobias Grosser06e15922016-11-16 11:06:47 +00005823 return set_from_map(isl_map_fix(set_to_map(set), type, pos, value));
Tobias Grosser52a25232015-02-04 20:55:43 +00005824}
5825
5826/* Fix the value of the variable at position "pos" of type "type" of "map"
5827 * to be equal to "v".
5828 */
5829__isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
5830 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5831{
5832 int i;
5833
5834 map = isl_map_cow(map);
5835 if (!map || !v)
5836 goto error;
5837
5838 if (!isl_val_is_int(v))
5839 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5840 "expecting integer value", goto error);
5841 if (pos >= isl_map_dim(map, type))
5842 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5843 "index out of bounds", goto error);
5844 for (i = map->n - 1; i >= 0; --i) {
5845 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
5846 isl_val_copy(v));
5847 if (remove_if_empty(map, i) < 0)
5848 goto error;
5849 }
5850 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5851 isl_val_free(v);
5852 return map;
5853error:
5854 isl_map_free(map);
5855 isl_val_free(v);
5856 return NULL;
5857}
5858
5859/* Fix the value of the variable at position "pos" of type "type" of "set"
5860 * to be equal to "v".
5861 */
5862__isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
5863 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5864{
5865 return isl_map_fix_val(set, type, pos, v);
5866}
5867
5868struct isl_map *isl_map_fix_input_si(struct isl_map *map,
5869 unsigned input, int value)
5870{
5871 return isl_map_fix_si(map, isl_dim_in, input, value);
5872}
5873
5874struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
5875{
Tobias Grosser06e15922016-11-16 11:06:47 +00005876 return set_from_map(isl_map_fix_si(set_to_map(set),
5877 isl_dim_set, dim, value));
Tobias Grosser52a25232015-02-04 20:55:43 +00005878}
5879
5880static __isl_give isl_basic_map *basic_map_bound_si(
5881 __isl_take isl_basic_map *bmap,
5882 enum isl_dim_type type, unsigned pos, int value, int upper)
5883{
5884 int j;
5885
5886 if (!bmap)
5887 return NULL;
5888 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5889 pos += isl_basic_map_offset(bmap, type);
5890 bmap = isl_basic_map_cow(bmap);
5891 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5892 j = isl_basic_map_alloc_inequality(bmap);
5893 if (j < 0)
5894 goto error;
5895 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5896 if (upper) {
5897 isl_int_set_si(bmap->ineq[j][pos], -1);
5898 isl_int_set_si(bmap->ineq[j][0], value);
5899 } else {
5900 isl_int_set_si(bmap->ineq[j][pos], 1);
5901 isl_int_set_si(bmap->ineq[j][0], -value);
5902 }
5903 bmap = isl_basic_map_simplify(bmap);
5904 return isl_basic_map_finalize(bmap);
5905error:
5906 isl_basic_map_free(bmap);
5907 return NULL;
5908}
5909
5910__isl_give isl_basic_map *isl_basic_map_lower_bound_si(
5911 __isl_take isl_basic_map *bmap,
5912 enum isl_dim_type type, unsigned pos, int value)
5913{
5914 return basic_map_bound_si(bmap, type, pos, value, 0);
5915}
5916
5917/* Constrain the values of the given dimension to be no greater than "value".
5918 */
5919__isl_give isl_basic_map *isl_basic_map_upper_bound_si(
5920 __isl_take isl_basic_map *bmap,
5921 enum isl_dim_type type, unsigned pos, int value)
5922{
5923 return basic_map_bound_si(bmap, type, pos, value, 1);
5924}
5925
5926struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
5927 unsigned dim, isl_int value)
5928{
5929 int j;
5930
5931 bset = isl_basic_set_cow(bset);
5932 bset = isl_basic_set_extend_constraints(bset, 0, 1);
5933 j = isl_basic_set_alloc_inequality(bset);
5934 if (j < 0)
5935 goto error;
5936 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
5937 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
5938 isl_int_neg(bset->ineq[j][0], value);
5939 bset = isl_basic_set_simplify(bset);
5940 return isl_basic_set_finalize(bset);
5941error:
5942 isl_basic_set_free(bset);
5943 return NULL;
5944}
5945
5946static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
5947 enum isl_dim_type type, unsigned pos, int value, int upper)
5948{
5949 int i;
5950
5951 map = isl_map_cow(map);
5952 if (!map)
5953 return NULL;
5954
5955 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5956 for (i = 0; i < map->n; ++i) {
5957 map->p[i] = basic_map_bound_si(map->p[i],
5958 type, pos, value, upper);
5959 if (!map->p[i])
5960 goto error;
5961 }
5962 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5963 return map;
5964error:
5965 isl_map_free(map);
5966 return NULL;
5967}
5968
5969__isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
5970 enum isl_dim_type type, unsigned pos, int value)
5971{
5972 return map_bound_si(map, type, pos, value, 0);
5973}
5974
5975__isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
5976 enum isl_dim_type type, unsigned pos, int value)
5977{
5978 return map_bound_si(map, type, pos, value, 1);
5979}
5980
5981__isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
5982 enum isl_dim_type type, unsigned pos, int value)
5983{
Tobias Grosser06e15922016-11-16 11:06:47 +00005984 return set_from_map(isl_map_lower_bound_si(set_to_map(set),
5985 type, pos, value));
Tobias Grosser52a25232015-02-04 20:55:43 +00005986}
5987
5988__isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
5989 enum isl_dim_type type, unsigned pos, int value)
5990{
5991 return isl_map_upper_bound_si(set, type, pos, value);
5992}
5993
5994/* Bound the given variable of "bmap" from below (or above is "upper"
5995 * is set) to "value".
5996 */
5997static __isl_give isl_basic_map *basic_map_bound(
5998 __isl_take isl_basic_map *bmap,
5999 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6000{
6001 int j;
6002
6003 if (!bmap)
6004 return NULL;
6005 if (pos >= isl_basic_map_dim(bmap, type))
6006 isl_die(bmap->ctx, isl_error_invalid,
6007 "index out of bounds", goto error);
6008 pos += isl_basic_map_offset(bmap, type);
6009 bmap = isl_basic_map_cow(bmap);
6010 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6011 j = isl_basic_map_alloc_inequality(bmap);
6012 if (j < 0)
6013 goto error;
6014 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
6015 if (upper) {
6016 isl_int_set_si(bmap->ineq[j][pos], -1);
6017 isl_int_set(bmap->ineq[j][0], value);
6018 } else {
6019 isl_int_set_si(bmap->ineq[j][pos], 1);
6020 isl_int_neg(bmap->ineq[j][0], value);
6021 }
6022 bmap = isl_basic_map_simplify(bmap);
6023 return isl_basic_map_finalize(bmap);
6024error:
6025 isl_basic_map_free(bmap);
6026 return NULL;
6027}
6028
6029/* Bound the given variable of "map" from below (or above is "upper"
6030 * is set) to "value".
6031 */
6032static __isl_give isl_map *map_bound(__isl_take isl_map *map,
6033 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6034{
6035 int i;
6036
6037 map = isl_map_cow(map);
6038 if (!map)
6039 return NULL;
6040
6041 if (pos >= isl_map_dim(map, type))
6042 isl_die(map->ctx, isl_error_invalid,
6043 "index out of bounds", goto error);
6044 for (i = map->n - 1; i >= 0; --i) {
6045 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
6046 if (remove_if_empty(map, i) < 0)
6047 goto error;
6048 }
6049 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6050 return map;
6051error:
6052 isl_map_free(map);
6053 return NULL;
6054}
6055
6056__isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
6057 enum isl_dim_type type, unsigned pos, isl_int value)
6058{
6059 return map_bound(map, type, pos, value, 0);
6060}
6061
6062__isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
6063 enum isl_dim_type type, unsigned pos, isl_int value)
6064{
6065 return map_bound(map, type, pos, value, 1);
6066}
6067
6068__isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
6069 enum isl_dim_type type, unsigned pos, isl_int value)
6070{
6071 return isl_map_lower_bound(set, type, pos, value);
6072}
6073
6074__isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
6075 enum isl_dim_type type, unsigned pos, isl_int value)
6076{
6077 return isl_map_upper_bound(set, type, pos, value);
6078}
6079
6080/* Force the values of the variable at position "pos" of type "type" of "set"
6081 * to be no smaller than "value".
6082 */
6083__isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
6084 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6085{
6086 if (!value)
6087 goto error;
6088 if (!isl_val_is_int(value))
6089 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6090 "expecting integer value", goto error);
6091 set = isl_set_lower_bound(set, type, pos, value->n);
6092 isl_val_free(value);
6093 return set;
6094error:
6095 isl_val_free(value);
6096 isl_set_free(set);
6097 return NULL;
6098}
6099
6100/* Force the values of the variable at position "pos" of type "type" of "set"
6101 * to be no greater than "value".
6102 */
6103__isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
6104 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6105{
6106 if (!value)
6107 goto error;
6108 if (!isl_val_is_int(value))
6109 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6110 "expecting integer value", goto error);
6111 set = isl_set_upper_bound(set, type, pos, value->n);
6112 isl_val_free(value);
6113 return set;
6114error:
6115 isl_val_free(value);
6116 isl_set_free(set);
6117 return NULL;
6118}
6119
6120struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
6121 isl_int value)
6122{
6123 int i;
6124
6125 set = isl_set_cow(set);
6126 if (!set)
6127 return NULL;
6128
6129 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
6130 for (i = 0; i < set->n; ++i) {
6131 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
6132 if (!set->p[i])
6133 goto error;
6134 }
6135 return set;
6136error:
6137 isl_set_free(set);
6138 return NULL;
6139}
6140
6141struct isl_map *isl_map_reverse(struct isl_map *map)
6142{
6143 int i;
6144
6145 map = isl_map_cow(map);
6146 if (!map)
6147 return NULL;
6148
6149 map->dim = isl_space_reverse(map->dim);
6150 if (!map->dim)
6151 goto error;
6152 for (i = 0; i < map->n; ++i) {
6153 map->p[i] = isl_basic_map_reverse(map->p[i]);
6154 if (!map->p[i])
6155 goto error;
6156 }
6157 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6158 return map;
6159error:
6160 isl_map_free(map);
6161 return NULL;
6162}
6163
Tobias Grosser52a25232015-02-04 20:55:43 +00006164#undef TYPE
6165#define TYPE isl_pw_multi_aff
6166#undef SUFFIX
6167#define SUFFIX _pw_multi_aff
6168#undef EMPTY
6169#define EMPTY isl_pw_multi_aff_empty
6170#undef ADD
6171#define ADD isl_pw_multi_aff_union_add
6172#include "isl_map_lexopt_templ.c"
6173
6174/* Given a map "map", compute the lexicographically minimal
6175 * (or maximal) image element for each domain element in dom,
6176 * in the form of an isl_pw_multi_aff.
Tobias Grosser37034db2016-03-25 19:38:18 +00006177 * If "empty" is not NULL, then set *empty to those elements in dom that
6178 * do not have an image element.
Tobias Grosser932ec012016-07-06 09:11:00 +00006179 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6180 * should be computed over the domain of "map". "empty" is also NULL
6181 * in this case.
Tobias Grosser52a25232015-02-04 20:55:43 +00006182 *
6183 * We first compute the lexicographically minimal or maximal element
6184 * in the first basic map. This results in a partial solution "res"
6185 * and a subset "todo" of dom that still need to be handled.
6186 * We then consider each of the remaining maps in "map" and successively
6187 * update both "res" and "todo".
Tobias Grosser37034db2016-03-25 19:38:18 +00006188 * If "empty" is NULL, then the todo sets are not needed and therefore
6189 * also not computed.
Tobias Grosser52a25232015-02-04 20:55:43 +00006190 */
6191static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
6192 __isl_take isl_map *map, __isl_take isl_set *dom,
Tobias Grosser932ec012016-07-06 09:11:00 +00006193 __isl_give isl_set **empty, unsigned flags)
Tobias Grosser52a25232015-02-04 20:55:43 +00006194{
6195 int i;
Tobias Grosser932ec012016-07-06 09:11:00 +00006196 int full;
Tobias Grosser52a25232015-02-04 20:55:43 +00006197 isl_pw_multi_aff *res;
6198 isl_set *todo;
6199
Tobias Grosser932ec012016-07-06 09:11:00 +00006200 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6201 if (!map || (!full && !dom))
Tobias Grosser52a25232015-02-04 20:55:43 +00006202 goto error;
6203
6204 if (isl_map_plain_is_empty(map)) {
6205 if (empty)
6206 *empty = dom;
6207 else
6208 isl_set_free(dom);
6209 return isl_pw_multi_aff_from_map(map);
6210 }
6211
6212 res = basic_map_partial_lexopt_pw_multi_aff(
6213 isl_basic_map_copy(map->p[0]),
Tobias Grosser932ec012016-07-06 09:11:00 +00006214 isl_set_copy(dom), empty, flags);
Tobias Grosser52a25232015-02-04 20:55:43 +00006215
Tobias Grosser37034db2016-03-25 19:38:18 +00006216 if (empty)
6217 todo = *empty;
Tobias Grosser52a25232015-02-04 20:55:43 +00006218 for (i = 1; i < map->n; ++i) {
6219 isl_pw_multi_aff *res_i;
Tobias Grosser52a25232015-02-04 20:55:43 +00006220
6221 res_i = basic_map_partial_lexopt_pw_multi_aff(
6222 isl_basic_map_copy(map->p[i]),
Tobias Grosser932ec012016-07-06 09:11:00 +00006223 isl_set_copy(dom), empty, flags);
Tobias Grosser52a25232015-02-04 20:55:43 +00006224
Tobias Grosser932ec012016-07-06 09:11:00 +00006225 if (ISL_FL_ISSET(flags, ISL_OPT_MAX))
Tobias Grosser52a25232015-02-04 20:55:43 +00006226 res = isl_pw_multi_aff_union_lexmax(res, res_i);
6227 else
6228 res = isl_pw_multi_aff_union_lexmin(res, res_i);
6229
Tobias Grosser37034db2016-03-25 19:38:18 +00006230 if (empty)
6231 todo = isl_set_intersect(todo, *empty);
Tobias Grosser52a25232015-02-04 20:55:43 +00006232 }
6233
6234 isl_set_free(dom);
6235 isl_map_free(map);
6236
6237 if (empty)
6238 *empty = todo;
Tobias Grosser52a25232015-02-04 20:55:43 +00006239
6240 return res;
6241error:
6242 if (empty)
6243 *empty = NULL;
6244 isl_set_free(dom);
6245 isl_map_free(map);
6246 return NULL;
6247}
6248
6249#undef TYPE
6250#define TYPE isl_map
6251#undef SUFFIX
6252#define SUFFIX
6253#undef EMPTY
6254#define EMPTY isl_map_empty
6255#undef ADD
6256#define ADD isl_map_union_disjoint
6257#include "isl_map_lexopt_templ.c"
6258
6259/* Given a map "map", compute the lexicographically minimal
Michael Krusee0b34f32016-05-04 14:41:36 +00006260 * (or maximal) image element for each domain element in "dom",
6261 * in the form of an isl_map.
6262 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6263 * do not have an image element.
Tobias Grosser932ec012016-07-06 09:11:00 +00006264 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6265 * should be computed over the domain of "map". "empty" is also NULL
6266 * in this case.
Tobias Grosser52a25232015-02-04 20:55:43 +00006267 *
Michael Krusee0b34f32016-05-04 14:41:36 +00006268 * If the input consists of more than one disjunct, then first
6269 * compute the desired result in the form of an isl_pw_multi_aff and
6270 * then convert that into an isl_map.
Tobias Grosser52a25232015-02-04 20:55:43 +00006271 *
Michael Krusee0b34f32016-05-04 14:41:36 +00006272 * This function used to have an explicit implementation in terms
6273 * of isl_maps, but it would continually intersect the domains of
6274 * partial results with the complement of the domain of the next
6275 * partial solution, potentially leading to an explosion in the number
6276 * of disjuncts if there are several disjuncts in the input.
6277 * An even earlier implementation of this function would look for
6278 * better results in the domain of the partial result and for extra
6279 * results in the complement of this domain, which would lead to
6280 * even more splintering.
Tobias Grosser52a25232015-02-04 20:55:43 +00006281 */
6282static __isl_give isl_map *isl_map_partial_lexopt_aligned(
Tobias Grosser932ec012016-07-06 09:11:00 +00006283 __isl_take isl_map *map, __isl_take isl_set *dom,
6284 __isl_give isl_set **empty, unsigned flags)
Tobias Grosser52a25232015-02-04 20:55:43 +00006285{
Tobias Grosser932ec012016-07-06 09:11:00 +00006286 int full;
Tobias Grosser52a25232015-02-04 20:55:43 +00006287 struct isl_map *res;
Michael Krusee0b34f32016-05-04 14:41:36 +00006288 isl_pw_multi_aff *pma;
Tobias Grosser52a25232015-02-04 20:55:43 +00006289
Tobias Grosser932ec012016-07-06 09:11:00 +00006290 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6291 if (!map || (!full && !dom))
Tobias Grosser52a25232015-02-04 20:55:43 +00006292 goto error;
6293
6294 if (isl_map_plain_is_empty(map)) {
6295 if (empty)
6296 *empty = dom;
6297 else
6298 isl_set_free(dom);
6299 return map;
6300 }
6301
Michael Krusee0b34f32016-05-04 14:41:36 +00006302 if (map->n == 1) {
6303 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
Tobias Grosser932ec012016-07-06 09:11:00 +00006304 dom, empty, flags);
Michael Krusee0b34f32016-05-04 14:41:36 +00006305 isl_map_free(map);
6306 return res;
Tobias Grosser52a25232015-02-04 20:55:43 +00006307 }
6308
Tobias Grosser932ec012016-07-06 09:11:00 +00006309 pma = isl_map_partial_lexopt_aligned_pw_multi_aff(map, dom, empty,
6310 flags);
Michael Krusee0b34f32016-05-04 14:41:36 +00006311 return isl_map_from_pw_multi_aff(pma);
Tobias Grosser52a25232015-02-04 20:55:43 +00006312error:
6313 if (empty)
6314 *empty = NULL;
6315 isl_set_free(dom);
6316 isl_map_free(map);
6317 return NULL;
6318}
6319
6320__isl_give isl_map *isl_map_partial_lexmax(
6321 __isl_take isl_map *map, __isl_take isl_set *dom,
6322 __isl_give isl_set **empty)
6323{
Tobias Grosser932ec012016-07-06 09:11:00 +00006324 return isl_map_partial_lexopt(map, dom, empty, ISL_OPT_MAX);
Tobias Grosser52a25232015-02-04 20:55:43 +00006325}
6326
6327__isl_give isl_map *isl_map_partial_lexmin(
6328 __isl_take isl_map *map, __isl_take isl_set *dom,
6329 __isl_give isl_set **empty)
6330{
6331 return isl_map_partial_lexopt(map, dom, empty, 0);
6332}
6333
6334__isl_give isl_set *isl_set_partial_lexmin(
6335 __isl_take isl_set *set, __isl_take isl_set *dom,
6336 __isl_give isl_set **empty)
6337{
Tobias Grosser06e15922016-11-16 11:06:47 +00006338 return set_from_map(isl_map_partial_lexmin(set_to_map(set),
6339 dom, empty));
Tobias Grosser52a25232015-02-04 20:55:43 +00006340}
6341
6342__isl_give isl_set *isl_set_partial_lexmax(
6343 __isl_take isl_set *set, __isl_take isl_set *dom,
6344 __isl_give isl_set **empty)
6345{
Tobias Grosser06e15922016-11-16 11:06:47 +00006346 return set_from_map(isl_map_partial_lexmax(set_to_map(set),
6347 dom, empty));
Tobias Grosser52a25232015-02-04 20:55:43 +00006348}
6349
Tobias Grosser932ec012016-07-06 09:11:00 +00006350/* Compute the lexicographic minimum (or maximum if "flags" includes
6351 * ISL_OPT_MAX) of "bset" over its parametric domain.
Tobias Grosser52a25232015-02-04 20:55:43 +00006352 */
Tobias Grosser932ec012016-07-06 09:11:00 +00006353__isl_give isl_set *isl_basic_set_lexopt(__isl_take isl_basic_set *bset,
6354 unsigned flags)
Tobias Grosser52a25232015-02-04 20:55:43 +00006355{
Tobias Grosser932ec012016-07-06 09:11:00 +00006356 return isl_basic_map_lexopt(bset, flags);
Tobias Grosser52a25232015-02-04 20:55:43 +00006357}
6358
6359__isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
6360{
Tobias Grosser932ec012016-07-06 09:11:00 +00006361 return isl_basic_map_lexopt(bmap, ISL_OPT_MAX);
Tobias Grosser52a25232015-02-04 20:55:43 +00006362}
6363
6364__isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
6365{
Tobias Grosser06e15922016-11-16 11:06:47 +00006366 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset)));
Tobias Grosser52a25232015-02-04 20:55:43 +00006367}
6368
6369__isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
6370{
Tobias Grosser06e15922016-11-16 11:06:47 +00006371 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset)));
Tobias Grosser52a25232015-02-04 20:55:43 +00006372}
6373
Tobias Grosser932ec012016-07-06 09:11:00 +00006374/* Compute the lexicographic minimum of "bset" over its parametric domain
6375 * for the purpose of quantifier elimination.
6376 * That is, find an explicit representation for all the existentially
6377 * quantified variables in "bset" by computing their lexicographic
6378 * minimum.
6379 */
6380static __isl_give isl_set *isl_basic_set_lexmin_compute_divs(
6381 __isl_take isl_basic_set *bset)
6382{
6383 return isl_basic_set_lexopt(bset, ISL_OPT_QE);
6384}
6385
Tobias Grosser52a25232015-02-04 20:55:43 +00006386/* Extract the first and only affine expression from list
6387 * and then add it to *pwaff with the given dom.
6388 * This domain is known to be disjoint from other domains
6389 * because of the way isl_basic_map_foreach_lexmax works.
6390 */
6391static int update_dim_opt(__isl_take isl_basic_set *dom,
6392 __isl_take isl_aff_list *list, void *user)
6393{
6394 isl_ctx *ctx = isl_basic_set_get_ctx(dom);
6395 isl_aff *aff;
6396 isl_pw_aff **pwaff = user;
6397 isl_pw_aff *pwaff_i;
6398
6399 if (!list)
6400 goto error;
6401 if (isl_aff_list_n_aff(list) != 1)
6402 isl_die(ctx, isl_error_internal,
6403 "expecting single element list", goto error);
6404
6405 aff = isl_aff_list_get_aff(list, 0);
6406 pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
6407
6408 *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
6409
6410 isl_aff_list_free(list);
6411
6412 return 0;
6413error:
6414 isl_basic_set_free(dom);
6415 isl_aff_list_free(list);
6416 return -1;
6417}
6418
6419/* Given a basic map with one output dimension, compute the minimum or
6420 * maximum of that dimension as an isl_pw_aff.
6421 *
6422 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6423 * call update_dim_opt on each leaf of the result.
6424 */
6425static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
6426 int max)
6427{
6428 isl_space *dim = isl_basic_map_get_space(bmap);
6429 isl_pw_aff *pwaff;
6430 int r;
6431
6432 dim = isl_space_from_domain(isl_space_domain(dim));
6433 dim = isl_space_add_dims(dim, isl_dim_out, 1);
6434 pwaff = isl_pw_aff_empty(dim);
6435
6436 r = isl_basic_map_foreach_lexopt(bmap, max, &update_dim_opt, &pwaff);
6437 if (r < 0)
6438 return isl_pw_aff_free(pwaff);
6439
6440 return pwaff;
6441}
6442
6443/* Compute the minimum or maximum of the given output dimension
6444 * as a function of the parameters and the input dimensions,
6445 * but independently of the other output dimensions.
6446 *
6447 * We first project out the other output dimension and then compute
6448 * the "lexicographic" maximum in each basic map, combining the results
6449 * using isl_pw_aff_union_max.
6450 */
6451static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
6452 int max)
6453{
6454 int i;
6455 isl_pw_aff *pwaff;
6456 unsigned n_out;
6457
6458 n_out = isl_map_dim(map, isl_dim_out);
6459 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
6460 map = isl_map_project_out(map, isl_dim_out, 0, pos);
6461 if (!map)
6462 return NULL;
6463
6464 if (map->n == 0) {
6465 isl_space *dim = isl_map_get_space(map);
6466 isl_map_free(map);
6467 return isl_pw_aff_empty(dim);
6468 }
6469
6470 pwaff = basic_map_dim_opt(map->p[0], max);
6471 for (i = 1; i < map->n; ++i) {
6472 isl_pw_aff *pwaff_i;
6473
6474 pwaff_i = basic_map_dim_opt(map->p[i], max);
6475 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
6476 }
6477
6478 isl_map_free(map);
6479
6480 return pwaff;
6481}
6482
Tobias Grosser932ec012016-07-06 09:11:00 +00006483/* Compute the minimum of the given output dimension as a function of the
6484 * parameters and input dimensions, but independently of
6485 * the other output dimensions.
6486 */
6487__isl_give isl_pw_aff *isl_map_dim_min(__isl_take isl_map *map, int pos)
6488{
6489 return map_dim_opt(map, pos, 0);
6490}
6491
Tobias Grosser52a25232015-02-04 20:55:43 +00006492/* Compute the maximum of the given output dimension as a function of the
6493 * parameters and input dimensions, but independently of
6494 * the other output dimensions.
6495 */
6496__isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
6497{
6498 return map_dim_opt(map, pos, 1);
6499}
6500
6501/* Compute the minimum or maximum of the given set dimension
6502 * as a function of the parameters,
6503 * but independently of the other set dimensions.
6504 */
6505static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
6506 int max)
6507{
6508 return map_dim_opt(set, pos, max);
6509}
6510
6511/* Compute the maximum of the given set dimension as a function of the
6512 * parameters, but independently of the other set dimensions.
6513 */
6514__isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
6515{
6516 return set_dim_opt(set, pos, 1);
6517}
6518
6519/* Compute the minimum of the given set dimension as a function of the
6520 * parameters, but independently of the other set dimensions.
6521 */
6522__isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
6523{
6524 return set_dim_opt(set, pos, 0);
6525}
6526
6527/* Apply a preimage specified by "mat" on the parameters of "bset".
6528 * bset is assumed to have only parameters and divs.
6529 */
6530static struct isl_basic_set *basic_set_parameter_preimage(
6531 struct isl_basic_set *bset, struct isl_mat *mat)
6532{
6533 unsigned nparam;
6534
6535 if (!bset || !mat)
6536 goto error;
6537
6538 bset->dim = isl_space_cow(bset->dim);
6539 if (!bset->dim)
6540 goto error;
6541
6542 nparam = isl_basic_set_dim(bset, isl_dim_param);
6543
6544 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
6545
6546 bset->dim->nparam = 0;
6547 bset->dim->n_out = nparam;
6548 bset = isl_basic_set_preimage(bset, mat);
6549 if (bset) {
6550 bset->dim->nparam = bset->dim->n_out;
6551 bset->dim->n_out = 0;
6552 }
6553 return bset;
6554error:
6555 isl_mat_free(mat);
6556 isl_basic_set_free(bset);
6557 return NULL;
6558}
6559
6560/* Apply a preimage specified by "mat" on the parameters of "set".
6561 * set is assumed to have only parameters and divs.
6562 */
Tobias Grosser37034db2016-03-25 19:38:18 +00006563static __isl_give isl_set *set_parameter_preimage(__isl_take isl_set *set,
6564 __isl_take isl_mat *mat)
Tobias Grosser52a25232015-02-04 20:55:43 +00006565{
Tobias Grosser37034db2016-03-25 19:38:18 +00006566 isl_space *space;
Tobias Grosser52a25232015-02-04 20:55:43 +00006567 unsigned nparam;
6568
6569 if (!set || !mat)
6570 goto error;
6571
Tobias Grosser52a25232015-02-04 20:55:43 +00006572 nparam = isl_set_dim(set, isl_dim_param);
6573
Tobias Grosser37034db2016-03-25 19:38:18 +00006574 if (mat->n_row != 1 + nparam)
6575 isl_die(isl_set_get_ctx(set), isl_error_internal,
6576 "unexpected number of rows", goto error);
Tobias Grosser52a25232015-02-04 20:55:43 +00006577
Tobias Grosser37034db2016-03-25 19:38:18 +00006578 space = isl_set_get_space(set);
6579 space = isl_space_move_dims(space, isl_dim_set, 0,
6580 isl_dim_param, 0, nparam);
6581 set = isl_set_reset_space(set, space);
Tobias Grosser52a25232015-02-04 20:55:43 +00006582 set = isl_set_preimage(set, mat);
Tobias Grosser37034db2016-03-25 19:38:18 +00006583 nparam = isl_set_dim(set, isl_dim_out);
6584 space = isl_set_get_space(set);
6585 space = isl_space_move_dims(space, isl_dim_param, 0,
6586 isl_dim_out, 0, nparam);
6587 set = isl_set_reset_space(set, space);
Tobias Grosser52a25232015-02-04 20:55:43 +00006588 return set;
6589error:
Tobias Grosser52a25232015-02-04 20:55:43 +00006590 isl_mat_free(mat);
Tobias Grosser52a25232015-02-04 20:55:43 +00006591 isl_set_free(set);
6592 return NULL;
6593}
6594
6595/* Intersect the basic set "bset" with the affine space specified by the
6596 * equalities in "eq".
6597 */
6598static struct isl_basic_set *basic_set_append_equalities(
6599 struct isl_basic_set *bset, struct isl_mat *eq)
6600{
6601 int i, k;
6602 unsigned len;
6603
6604 if (!bset || !eq)
6605 goto error;
6606
6607 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
6608 eq->n_row, 0);
6609 if (!bset)
6610 goto error;
6611
6612 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
6613 for (i = 0; i < eq->n_row; ++i) {
6614 k = isl_basic_set_alloc_equality(bset);
6615 if (k < 0)
6616 goto error;
6617 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
6618 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
6619 }
6620 isl_mat_free(eq);
6621
6622 bset = isl_basic_set_gauss(bset, NULL);
6623 bset = isl_basic_set_finalize(bset);
6624
6625 return bset;
6626error:
6627 isl_mat_free(eq);
6628 isl_basic_set_free(bset);
6629 return NULL;
6630}
6631
6632/* Intersect the set "set" with the affine space specified by the
6633 * equalities in "eq".
6634 */
6635static struct isl_set *set_append_equalities(struct isl_set *set,
6636 struct isl_mat *eq)
6637{
6638 int i;
6639
6640 if (!set || !eq)
6641 goto error;
6642
6643 for (i = 0; i < set->n; ++i) {
6644 set->p[i] = basic_set_append_equalities(set->p[i],
6645 isl_mat_copy(eq));
6646 if (!set->p[i])
6647 goto error;
6648 }
6649 isl_mat_free(eq);
6650 return set;
6651error:
6652 isl_mat_free(eq);
6653 isl_set_free(set);
6654 return NULL;
6655}
6656
6657/* Given a basic set "bset" that only involves parameters and existentially
6658 * quantified variables, return the index of the first equality
6659 * that only involves parameters. If there is no such equality then
6660 * return bset->n_eq.
6661 *
6662 * This function assumes that isl_basic_set_gauss has been called on "bset".
6663 */
6664static int first_parameter_equality(__isl_keep isl_basic_set *bset)
6665{
6666 int i, j;
6667 unsigned nparam, n_div;
6668
6669 if (!bset)
6670 return -1;
6671
6672 nparam = isl_basic_set_dim(bset, isl_dim_param);
6673 n_div = isl_basic_set_dim(bset, isl_dim_div);
6674
6675 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
6676 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
6677 ++i;
6678 }
6679
6680 return i;
6681}
6682
6683/* Compute an explicit representation for the existentially quantified
6684 * variables in "bset" by computing the "minimal value" of the set
6685 * variables. Since there are no set variables, the computation of
6686 * the minimal value essentially computes an explicit representation
6687 * of the non-empty part(s) of "bset".
6688 *
6689 * The input only involves parameters and existentially quantified variables.
6690 * All equalities among parameters have been removed.
6691 *
6692 * Since the existentially quantified variables in the result are in general
6693 * going to be different from those in the input, we first replace
6694 * them by the minimal number of variables based on their equalities.
6695 * This should simplify the parametric integer programming.
6696 */
6697static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
6698{
6699 isl_morph *morph1, *morph2;
6700 isl_set *set;
6701 unsigned n;
6702
6703 if (!bset)
6704 return NULL;
6705 if (bset->n_eq == 0)
Tobias Grosser932ec012016-07-06 09:11:00 +00006706 return isl_basic_set_lexmin_compute_divs(bset);
Tobias Grosser52a25232015-02-04 20:55:43 +00006707
6708 morph1 = isl_basic_set_parameter_compression(bset);
6709 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
6710 bset = isl_basic_set_lift(bset);
6711 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
6712 bset = isl_morph_basic_set(morph2, bset);
6713 n = isl_basic_set_dim(bset, isl_dim_set);
6714 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
6715
Tobias Grosser932ec012016-07-06 09:11:00 +00006716 set = isl_basic_set_lexmin_compute_divs(bset);
Tobias Grosser52a25232015-02-04 20:55:43 +00006717
6718 set = isl_morph_set(isl_morph_inverse(morph1), set);
6719
6720 return set;
6721}
6722
6723/* Project the given basic set onto its parameter domain, possibly introducing
6724 * new, explicit, existential variables in the constraints.
6725 * The input has parameters and (possibly implicit) existential variables.
6726 * The output has the same parameters, but only
6727 * explicit existentially quantified variables.
6728 *
6729 * The actual projection is performed by pip, but pip doesn't seem
6730 * to like equalities very much, so we first remove the equalities
6731 * among the parameters by performing a variable compression on
6732 * the parameters. Afterward, an inverse transformation is performed
6733 * and the equalities among the parameters are inserted back in.
6734 *
6735 * The variable compression on the parameters may uncover additional
6736 * equalities that were only implicit before. We therefore check
6737 * if there are any new parameter equalities in the result and
6738 * if so recurse. The removal of parameter equalities is required
6739 * for the parameter compression performed by base_compute_divs.
6740 */
6741static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
6742{
6743 int i;
6744 struct isl_mat *eq;
6745 struct isl_mat *T, *T2;
6746 struct isl_set *set;
6747 unsigned nparam;
6748
6749 bset = isl_basic_set_cow(bset);
6750 if (!bset)
6751 return NULL;
6752
6753 if (bset->n_eq == 0)
6754 return base_compute_divs(bset);
6755
6756 bset = isl_basic_set_gauss(bset, NULL);
6757 if (!bset)
6758 return NULL;
6759 if (isl_basic_set_plain_is_empty(bset))
6760 return isl_set_from_basic_set(bset);
6761
6762 i = first_parameter_equality(bset);
6763 if (i == bset->n_eq)
6764 return base_compute_divs(bset);
6765
6766 nparam = isl_basic_set_dim(bset, isl_dim_param);
6767 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
6768 0, 1 + nparam);
6769 eq = isl_mat_cow(eq);
6770 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
6771 if (T && T->n_col == 0) {
6772 isl_mat_free(T);
6773 isl_mat_free(T2);
6774 isl_mat_free(eq);
6775 bset = isl_basic_set_set_to_empty(bset);
6776 return isl_set_from_basic_set(bset);
6777 }
6778 bset = basic_set_parameter_preimage(bset, T);
6779
6780 i = first_parameter_equality(bset);
6781 if (!bset)
6782 set = NULL;
6783 else if (i == bset->n_eq)
6784 set = base_compute_divs(bset);
6785 else
6786 set = parameter_compute_divs(bset);
6787 set = set_parameter_preimage(set, T2);
6788 set = set_append_equalities(set, eq);
6789 return set;
6790}
6791
6792/* Insert the divs from "ls" before those of "bmap".
6793 *
6794 * The number of columns is not changed, which means that the last
6795 * dimensions of "bmap" are being reintepreted as the divs from "ls".
6796 * The caller is responsible for removing the same number of dimensions
6797 * from the space of "bmap".
6798 */
6799static __isl_give isl_basic_map *insert_divs_from_local_space(
6800 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
6801{
6802 int i;
6803 int n_div;
6804 int old_n_div;
6805
6806 n_div = isl_local_space_dim(ls, isl_dim_div);
6807 if (n_div == 0)
6808 return bmap;
6809
6810 old_n_div = bmap->n_div;
6811 bmap = insert_div_rows(bmap, n_div);
6812 if (!bmap)
6813 return NULL;
6814
6815 for (i = 0; i < n_div; ++i) {
6816 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
6817 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
6818 }
6819
6820 return bmap;
6821}
6822
6823/* Replace the space of "bmap" by the space and divs of "ls".
6824 *
6825 * If "ls" has any divs, then we simplify the result since we may
6826 * have discovered some additional equalities that could simplify
6827 * the div expressions.
6828 */
6829static __isl_give isl_basic_map *basic_replace_space_by_local_space(
6830 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
6831{
6832 int n_div;
6833
6834 bmap = isl_basic_map_cow(bmap);
6835 if (!bmap || !ls)
6836 goto error;
6837
6838 n_div = isl_local_space_dim(ls, isl_dim_div);
6839 bmap = insert_divs_from_local_space(bmap, ls);
6840 if (!bmap)
6841 goto error;
6842
6843 isl_space_free(bmap->dim);
6844 bmap->dim = isl_local_space_get_space(ls);
6845 if (!bmap->dim)
6846 goto error;
6847
6848 isl_local_space_free(ls);
6849 if (n_div > 0)
6850 bmap = isl_basic_map_simplify(bmap);
6851 bmap = isl_basic_map_finalize(bmap);
6852 return bmap;
6853error:
6854 isl_basic_map_free(bmap);
6855 isl_local_space_free(ls);
6856 return NULL;
6857}
6858
6859/* Replace the space of "map" by the space and divs of "ls".
6860 */
6861static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
6862 __isl_take isl_local_space *ls)
6863{
6864 int i;
6865
6866 map = isl_map_cow(map);
6867 if (!map || !ls)
6868 goto error;
6869
6870 for (i = 0; i < map->n; ++i) {
6871 map->p[i] = basic_replace_space_by_local_space(map->p[i],
6872 isl_local_space_copy(ls));
6873 if (!map->p[i])
6874 goto error;
6875 }
6876 isl_space_free(map->dim);
6877 map->dim = isl_local_space_get_space(ls);
6878 if (!map->dim)
6879 goto error;
6880
6881 isl_local_space_free(ls);
6882 return map;
6883error:
6884 isl_local_space_free(ls);
6885 isl_map_free(map);
6886 return NULL;
6887}
6888
6889/* Compute an explicit representation for the existentially
6890 * quantified variables for which do not know any explicit representation yet.
6891 *
6892 * We first sort the existentially quantified variables so that the
6893 * existentially quantified variables for which we already have an explicit
6894 * representation are placed before those for which we do not.
6895 * The input dimensions, the output dimensions and the existentially
6896 * quantified variables for which we already have an explicit
6897 * representation are then turned into parameters.
6898 * compute_divs returns a map with the same parameters and
6899 * no input or output dimensions and the dimension specification
6900 * is reset to that of the input, including the existentially quantified
6901 * variables for which we already had an explicit representation.
6902 */
6903static struct isl_map *compute_divs(struct isl_basic_map *bmap)
6904{
6905 struct isl_basic_set *bset;
6906 struct isl_set *set;
6907 struct isl_map *map;
6908 isl_space *dim;
6909 isl_local_space *ls;
6910 unsigned nparam;
6911 unsigned n_in;
6912 unsigned n_out;
Tobias Grosser37034db2016-03-25 19:38:18 +00006913 int n_known;
Tobias Grosser52a25232015-02-04 20:55:43 +00006914 int i;
6915
6916 bmap = isl_basic_map_sort_divs(bmap);
6917 bmap = isl_basic_map_cow(bmap);
6918 if (!bmap)
6919 return NULL;
6920
Tobias Grosser37034db2016-03-25 19:38:18 +00006921 n_known = isl_basic_map_first_unknown_div(bmap);
6922 if (n_known < 0)
6923 return isl_map_from_basic_map(isl_basic_map_free(bmap));
Tobias Grosser52a25232015-02-04 20:55:43 +00006924
6925 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6926 n_in = isl_basic_map_dim(bmap, isl_dim_in);
6927 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6928 dim = isl_space_set_alloc(bmap->ctx,
6929 nparam + n_in + n_out + n_known, 0);
6930 if (!dim)
6931 goto error;
6932
6933 ls = isl_basic_map_get_local_space(bmap);
6934 ls = isl_local_space_drop_dims(ls, isl_dim_div,
6935 n_known, bmap->n_div - n_known);
6936 if (n_known > 0) {
6937 for (i = n_known; i < bmap->n_div; ++i)
6938 swap_div(bmap, i - n_known, i);
6939 bmap->n_div -= n_known;
6940 bmap->extra -= n_known;
6941 }
6942 bmap = isl_basic_map_reset_space(bmap, dim);
Tobias Grosser06e15922016-11-16 11:06:47 +00006943 bset = bset_from_bmap(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +00006944
6945 set = parameter_compute_divs(bset);
Tobias Grosser06e15922016-11-16 11:06:47 +00006946 map = set_to_map(set);
Tobias Grosser52a25232015-02-04 20:55:43 +00006947 map = replace_space_by_local_space(map, ls);
6948
6949 return map;
6950error:
6951 isl_basic_map_free(bmap);
6952 return NULL;
6953}
6954
Tobias Grosser37034db2016-03-25 19:38:18 +00006955/* Remove the explicit representation of local variable "div",
6956 * if there is any.
6957 */
6958__isl_give isl_basic_map *isl_basic_map_mark_div_unknown(
6959 __isl_take isl_basic_map *bmap, int div)
6960{
Tobias Grosser932ec012016-07-06 09:11:00 +00006961 isl_bool unknown;
Tobias Grosser37034db2016-03-25 19:38:18 +00006962
Tobias Grosser932ec012016-07-06 09:11:00 +00006963 unknown = isl_basic_map_div_is_marked_unknown(bmap, div);
6964 if (unknown < 0)
Tobias Grosser37034db2016-03-25 19:38:18 +00006965 return isl_basic_map_free(bmap);
Tobias Grosser932ec012016-07-06 09:11:00 +00006966 if (unknown)
Tobias Grosser37034db2016-03-25 19:38:18 +00006967 return bmap;
6968
6969 bmap = isl_basic_map_cow(bmap);
6970 if (!bmap)
6971 return NULL;
6972 isl_int_set_si(bmap->div[div][0], 0);
6973 return bmap;
6974}
6975
Tobias Grosser932ec012016-07-06 09:11:00 +00006976/* Is local variable "div" of "bmap" marked as not having an explicit
6977 * representation?
6978 * Note that even if "div" is not marked in this way and therefore
6979 * has an explicit representation, this representation may still
6980 * depend (indirectly) on other local variables that do not
6981 * have an explicit representation.
Michael Kruse959a8dc2016-01-15 15:54:45 +00006982 */
Tobias Grosser932ec012016-07-06 09:11:00 +00006983isl_bool isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map *bmap,
6984 int div)
Michael Kruse959a8dc2016-01-15 15:54:45 +00006985{
6986 if (!bmap)
6987 return isl_bool_error;
6988 if (div < 0 || div >= isl_basic_map_dim(bmap, isl_dim_div))
6989 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
6990 "position out of bounds", return isl_bool_error);
Tobias Grosser932ec012016-07-06 09:11:00 +00006991 return isl_int_is_zero(bmap->div[div][0]);
Michael Kruse959a8dc2016-01-15 15:54:45 +00006992}
6993
Tobias Grosser37034db2016-03-25 19:38:18 +00006994/* Return the position of the first local variable that does not
6995 * have an explicit representation.
6996 * Return the total number of local variables if they all have
6997 * an explicit representation.
6998 * Return -1 on error.
6999 */
7000int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map *bmap)
7001{
7002 int i;
7003
7004 if (!bmap)
7005 return -1;
7006
7007 for (i = 0; i < bmap->n_div; ++i) {
7008 if (!isl_basic_map_div_is_known(bmap, i))
7009 return i;
7010 }
7011 return bmap->n_div;
7012}
7013
Tobias Grosser9ec4f952016-07-20 16:53:07 +00007014/* Return the position of the first local variable that does not
7015 * have an explicit representation.
7016 * Return the total number of local variables if they all have
7017 * an explicit representation.
7018 * Return -1 on error.
7019 */
7020int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set *bset)
7021{
7022 return isl_basic_map_first_unknown_div(bset);
7023}
7024
Michael Kruse959a8dc2016-01-15 15:54:45 +00007025/* Does "bmap" have an explicit representation for all local variables?
7026 */
7027isl_bool isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
Tobias Grosser52a25232015-02-04 20:55:43 +00007028{
Tobias Grosser37034db2016-03-25 19:38:18 +00007029 int first, n;
Tobias Grosser52a25232015-02-04 20:55:43 +00007030
Tobias Grosser37034db2016-03-25 19:38:18 +00007031 n = isl_basic_map_dim(bmap, isl_dim_div);
7032 first = isl_basic_map_first_unknown_div(bmap);
7033 if (first < 0)
Michael Kruse959a8dc2016-01-15 15:54:45 +00007034 return isl_bool_error;
Tobias Grosser37034db2016-03-25 19:38:18 +00007035 return first == n;
Tobias Grosser52a25232015-02-04 20:55:43 +00007036}
7037
Michael Kruse959a8dc2016-01-15 15:54:45 +00007038/* Do all basic maps in "map" have an explicit representation
7039 * for all local variables?
7040 */
7041isl_bool isl_map_divs_known(__isl_keep isl_map *map)
Tobias Grosser52a25232015-02-04 20:55:43 +00007042{
7043 int i;
7044
7045 if (!map)
Michael Kruse959a8dc2016-01-15 15:54:45 +00007046 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00007047
7048 for (i = 0; i < map->n; ++i) {
7049 int known = isl_basic_map_divs_known(map->p[i]);
7050 if (known <= 0)
7051 return known;
7052 }
7053
Michael Kruse959a8dc2016-01-15 15:54:45 +00007054 return isl_bool_true;
Tobias Grosser52a25232015-02-04 20:55:43 +00007055}
7056
7057/* If bmap contains any unknown divs, then compute explicit
7058 * expressions for them. However, this computation may be
7059 * quite expensive, so first try to remove divs that aren't
7060 * strictly needed.
7061 */
7062struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
7063{
7064 int known;
7065 struct isl_map *map;
7066
7067 known = isl_basic_map_divs_known(bmap);
7068 if (known < 0)
7069 goto error;
7070 if (known)
7071 return isl_map_from_basic_map(bmap);
7072
7073 bmap = isl_basic_map_drop_redundant_divs(bmap);
7074
7075 known = isl_basic_map_divs_known(bmap);
7076 if (known < 0)
7077 goto error;
7078 if (known)
7079 return isl_map_from_basic_map(bmap);
7080
7081 map = compute_divs(bmap);
7082 return map;
7083error:
7084 isl_basic_map_free(bmap);
7085 return NULL;
7086}
7087
7088struct isl_map *isl_map_compute_divs(struct isl_map *map)
7089{
7090 int i;
7091 int known;
7092 struct isl_map *res;
7093
7094 if (!map)
7095 return NULL;
7096 if (map->n == 0)
7097 return map;
7098
Michael Kruse959a8dc2016-01-15 15:54:45 +00007099 known = isl_map_divs_known(map);
Tobias Grosser52a25232015-02-04 20:55:43 +00007100 if (known < 0) {
7101 isl_map_free(map);
7102 return NULL;
7103 }
7104 if (known)
7105 return map;
7106
7107 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
7108 for (i = 1 ; i < map->n; ++i) {
7109 struct isl_map *r2;
7110 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
7111 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
7112 res = isl_map_union_disjoint(res, r2);
7113 else
7114 res = isl_map_union(res, r2);
7115 }
7116 isl_map_free(map);
7117
7118 return res;
7119}
7120
7121struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
7122{
Tobias Grosser06e15922016-11-16 11:06:47 +00007123 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset)));
Tobias Grosser52a25232015-02-04 20:55:43 +00007124}
7125
7126struct isl_set *isl_set_compute_divs(struct isl_set *set)
7127{
Tobias Grosser06e15922016-11-16 11:06:47 +00007128 return set_from_map(isl_map_compute_divs(set_to_map(set)));
Tobias Grosser52a25232015-02-04 20:55:43 +00007129}
7130
7131struct isl_set *isl_map_domain(struct isl_map *map)
7132{
7133 int i;
7134 struct isl_set *set;
7135
7136 if (!map)
7137 goto error;
7138
7139 map = isl_map_cow(map);
7140 if (!map)
7141 return NULL;
7142
Tobias Grosser06e15922016-11-16 11:06:47 +00007143 set = set_from_map(map);
Tobias Grosser52a25232015-02-04 20:55:43 +00007144 set->dim = isl_space_domain(set->dim);
7145 if (!set->dim)
7146 goto error;
7147 for (i = 0; i < map->n; ++i) {
7148 set->p[i] = isl_basic_map_domain(map->p[i]);
7149 if (!set->p[i])
7150 goto error;
7151 }
7152 ISL_F_CLR(set, ISL_MAP_DISJOINT);
7153 ISL_F_CLR(set, ISL_SET_NORMALIZED);
7154 return set;
7155error:
7156 isl_map_free(map);
7157 return NULL;
7158}
7159
7160/* Return the union of "map1" and "map2", where we assume for now that
7161 * "map1" and "map2" are disjoint. Note that the basic maps inside
7162 * "map1" or "map2" may not be disjoint from each other.
7163 * Also note that this function is also called from isl_map_union,
7164 * which takes care of handling the situation where "map1" and "map2"
7165 * may not be disjoint.
7166 *
7167 * If one of the inputs is empty, we can simply return the other input.
7168 * Similarly, if one of the inputs is universal, then it is equal to the union.
7169 */
7170static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
7171 __isl_take isl_map *map2)
7172{
7173 int i;
7174 unsigned flags = 0;
7175 struct isl_map *map = NULL;
7176 int is_universe;
7177
7178 if (!map1 || !map2)
7179 goto error;
7180
7181 if (!isl_space_is_equal(map1->dim, map2->dim))
7182 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
7183 "spaces don't match", goto error);
7184
7185 if (map1->n == 0) {
7186 isl_map_free(map1);
7187 return map2;
7188 }
7189 if (map2->n == 0) {
7190 isl_map_free(map2);
7191 return map1;
7192 }
7193
7194 is_universe = isl_map_plain_is_universe(map1);
7195 if (is_universe < 0)
7196 goto error;
7197 if (is_universe) {
7198 isl_map_free(map2);
7199 return map1;
7200 }
7201
7202 is_universe = isl_map_plain_is_universe(map2);
7203 if (is_universe < 0)
7204 goto error;
7205 if (is_universe) {
7206 isl_map_free(map1);
7207 return map2;
7208 }
7209
7210 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7211 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7212 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7213
7214 map = isl_map_alloc_space(isl_space_copy(map1->dim),
7215 map1->n + map2->n, flags);
7216 if (!map)
7217 goto error;
7218 for (i = 0; i < map1->n; ++i) {
7219 map = isl_map_add_basic_map(map,
7220 isl_basic_map_copy(map1->p[i]));
7221 if (!map)
7222 goto error;
7223 }
7224 for (i = 0; i < map2->n; ++i) {
7225 map = isl_map_add_basic_map(map,
7226 isl_basic_map_copy(map2->p[i]));
7227 if (!map)
7228 goto error;
7229 }
7230 isl_map_free(map1);
7231 isl_map_free(map2);
7232 return map;
7233error:
7234 isl_map_free(map);
7235 isl_map_free(map1);
7236 isl_map_free(map2);
7237 return NULL;
7238}
7239
7240/* Return the union of "map1" and "map2", where "map1" and "map2" are
7241 * guaranteed to be disjoint by the caller.
7242 *
7243 * Note that this functions is called from within isl_map_make_disjoint,
7244 * so we have to be careful not to touch the constraints of the inputs
7245 * in any way.
7246 */
7247__isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
7248 __isl_take isl_map *map2)
7249{
7250 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
7251}
7252
7253/* Return the union of "map1" and "map2", where "map1" and "map2" may
7254 * not be disjoint. The parameters are assumed to have been aligned.
7255 *
7256 * We currently simply call map_union_disjoint, the internal operation
7257 * of which does not really depend on the inputs being disjoint.
7258 * If the result contains more than one basic map, then we clear
7259 * the disjoint flag since the result may contain basic maps from
7260 * both inputs and these are not guaranteed to be disjoint.
7261 *
7262 * As a special case, if "map1" and "map2" are obviously equal,
7263 * then we simply return "map1".
7264 */
7265static __isl_give isl_map *map_union_aligned(__isl_take isl_map *map1,
7266 __isl_take isl_map *map2)
7267{
7268 int equal;
7269
7270 if (!map1 || !map2)
7271 goto error;
7272
7273 equal = isl_map_plain_is_equal(map1, map2);
7274 if (equal < 0)
7275 goto error;
7276 if (equal) {
7277 isl_map_free(map2);
7278 return map1;
7279 }
7280
7281 map1 = map_union_disjoint(map1, map2);
7282 if (!map1)
7283 return NULL;
7284 if (map1->n > 1)
7285 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
7286 return map1;
7287error:
7288 isl_map_free(map1);
7289 isl_map_free(map2);
7290 return NULL;
7291}
7292
7293/* Return the union of "map1" and "map2", where "map1" and "map2" may
7294 * not be disjoint.
7295 */
7296__isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
7297 __isl_take isl_map *map2)
7298{
7299 return isl_map_align_params_map_map_and(map1, map2, &map_union_aligned);
7300}
7301
7302struct isl_set *isl_set_union_disjoint(
7303 struct isl_set *set1, struct isl_set *set2)
7304{
Tobias Grosser06e15922016-11-16 11:06:47 +00007305 return set_from_map(isl_map_union_disjoint(set_to_map(set1),
7306 set_to_map(set2)));
Tobias Grosser52a25232015-02-04 20:55:43 +00007307}
7308
7309struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
7310{
Tobias Grosser06e15922016-11-16 11:06:47 +00007311 return set_from_map(isl_map_union(set_to_map(set1), set_to_map(set2)));
Tobias Grosser52a25232015-02-04 20:55:43 +00007312}
7313
7314/* Apply "fn" to pairs of elements from "map" and "set" and collect
7315 * the results.
7316 *
7317 * "map" and "set" are assumed to be compatible and non-NULL.
7318 */
7319static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
7320 __isl_take isl_set *set,
7321 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
7322 __isl_take isl_basic_set *bset))
7323{
7324 unsigned flags = 0;
7325 struct isl_map *result;
7326 int i, j;
7327
7328 if (isl_set_plain_is_universe(set)) {
7329 isl_set_free(set);
7330 return map;
7331 }
7332
7333 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
7334 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
7335 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7336
7337 result = isl_map_alloc_space(isl_space_copy(map->dim),
7338 map->n * set->n, flags);
7339 for (i = 0; result && i < map->n; ++i)
7340 for (j = 0; j < set->n; ++j) {
7341 result = isl_map_add_basic_map(result,
7342 fn(isl_basic_map_copy(map->p[i]),
7343 isl_basic_set_copy(set->p[j])));
7344 if (!result)
7345 break;
7346 }
7347
7348 isl_map_free(map);
7349 isl_set_free(set);
7350 return result;
7351}
7352
7353static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
7354 __isl_take isl_set *set)
7355{
7356 if (!map || !set)
7357 goto error;
7358
7359 if (!isl_map_compatible_range(map, set))
7360 isl_die(set->ctx, isl_error_invalid,
7361 "incompatible spaces", goto error);
7362
7363 return map_intersect_set(map, set, &isl_basic_map_intersect_range);
7364error:
7365 isl_map_free(map);
7366 isl_set_free(set);
7367 return NULL;
7368}
7369
7370__isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
7371 __isl_take isl_set *set)
7372{
7373 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
7374}
7375
7376static __isl_give isl_map *map_intersect_domain(__isl_take isl_map *map,
7377 __isl_take isl_set *set)
7378{
7379 if (!map || !set)
7380 goto error;
7381
7382 if (!isl_map_compatible_domain(map, set))
7383 isl_die(set->ctx, isl_error_invalid,
7384 "incompatible spaces", goto error);
7385
7386 return map_intersect_set(map, set, &isl_basic_map_intersect_domain);
7387error:
7388 isl_map_free(map);
7389 isl_set_free(set);
7390 return NULL;
7391}
7392
7393__isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
7394 __isl_take isl_set *set)
7395{
7396 return isl_map_align_params_map_map_and(map, set,
7397 &map_intersect_domain);
7398}
7399
7400static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
7401 __isl_take isl_map *map2)
7402{
7403 if (!map1 || !map2)
7404 goto error;
7405 map1 = isl_map_reverse(map1);
7406 map1 = isl_map_apply_range(map1, map2);
7407 return isl_map_reverse(map1);
7408error:
7409 isl_map_free(map1);
7410 isl_map_free(map2);
7411 return NULL;
7412}
7413
7414__isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
7415 __isl_take isl_map *map2)
7416{
7417 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
7418}
7419
7420static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
7421 __isl_take isl_map *map2)
7422{
7423 isl_space *dim_result;
7424 struct isl_map *result;
7425 int i, j;
7426
7427 if (!map1 || !map2)
7428 goto error;
7429
7430 dim_result = isl_space_join(isl_space_copy(map1->dim),
7431 isl_space_copy(map2->dim));
7432
7433 result = isl_map_alloc_space(dim_result, map1->n * map2->n, 0);
7434 if (!result)
7435 goto error;
7436 for (i = 0; i < map1->n; ++i)
7437 for (j = 0; j < map2->n; ++j) {
7438 result = isl_map_add_basic_map(result,
7439 isl_basic_map_apply_range(
7440 isl_basic_map_copy(map1->p[i]),
7441 isl_basic_map_copy(map2->p[j])));
7442 if (!result)
7443 goto error;
7444 }
7445 isl_map_free(map1);
7446 isl_map_free(map2);
7447 if (result && result->n <= 1)
7448 ISL_F_SET(result, ISL_MAP_DISJOINT);
7449 return result;
7450error:
7451 isl_map_free(map1);
7452 isl_map_free(map2);
7453 return NULL;
7454}
7455
7456__isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
7457 __isl_take isl_map *map2)
7458{
7459 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
7460}
7461
7462/*
7463 * returns range - domain
7464 */
7465struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
7466{
Tobias Grosserb2f39922015-05-28 13:32:11 +00007467 isl_space *target_space;
Tobias Grosser52a25232015-02-04 20:55:43 +00007468 struct isl_basic_set *bset;
7469 unsigned dim;
7470 unsigned nparam;
7471 int i;
7472
7473 if (!bmap)
7474 goto error;
7475 isl_assert(bmap->ctx, isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7476 bmap->dim, isl_dim_out),
7477 goto error);
Tobias Grosserb2f39922015-05-28 13:32:11 +00007478 target_space = isl_space_domain(isl_basic_map_get_space(bmap));
Tobias Grosser52a25232015-02-04 20:55:43 +00007479 dim = isl_basic_map_n_in(bmap);
7480 nparam = isl_basic_map_n_param(bmap);
Tobias Grosserb2f39922015-05-28 13:32:11 +00007481 bmap = isl_basic_map_from_range(isl_basic_map_wrap(bmap));
7482 bmap = isl_basic_map_add_dims(bmap, isl_dim_in, dim);
7483 bmap = isl_basic_map_extend_constraints(bmap, dim, 0);
Tobias Grosser52a25232015-02-04 20:55:43 +00007484 for (i = 0; i < dim; ++i) {
Tobias Grosserb2f39922015-05-28 13:32:11 +00007485 int j = isl_basic_map_alloc_equality(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +00007486 if (j < 0) {
Tobias Grosserb2f39922015-05-28 13:32:11 +00007487 bmap = isl_basic_map_free(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +00007488 break;
7489 }
Tobias Grosserb2f39922015-05-28 13:32:11 +00007490 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7491 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7492 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], 1);
7493 isl_int_set_si(bmap->eq[j][1+nparam+2*dim+i], -1);
Tobias Grosser52a25232015-02-04 20:55:43 +00007494 }
Tobias Grosserb2f39922015-05-28 13:32:11 +00007495 bset = isl_basic_map_domain(bmap);
7496 bset = isl_basic_set_reset_space(bset, target_space);
Tobias Grosser52a25232015-02-04 20:55:43 +00007497 return bset;
7498error:
7499 isl_basic_map_free(bmap);
7500 return NULL;
7501}
7502
7503/*
7504 * returns range - domain
7505 */
7506__isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
7507{
7508 int i;
7509 isl_space *dim;
7510 struct isl_set *result;
7511
7512 if (!map)
7513 return NULL;
7514
7515 isl_assert(map->ctx, isl_space_tuple_is_equal(map->dim, isl_dim_in,
7516 map->dim, isl_dim_out),
7517 goto error);
7518 dim = isl_map_get_space(map);
7519 dim = isl_space_domain(dim);
7520 result = isl_set_alloc_space(dim, map->n, 0);
7521 if (!result)
7522 goto error;
7523 for (i = 0; i < map->n; ++i)
7524 result = isl_set_add_basic_set(result,
7525 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
7526 isl_map_free(map);
7527 return result;
7528error:
7529 isl_map_free(map);
7530 return NULL;
7531}
7532
7533/*
7534 * returns [domain -> range] -> range - domain
7535 */
7536__isl_give isl_basic_map *isl_basic_map_deltas_map(
7537 __isl_take isl_basic_map *bmap)
7538{
7539 int i, k;
7540 isl_space *dim;
7541 isl_basic_map *domain;
7542 int nparam, n;
7543 unsigned total;
7544
7545 if (!isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7546 bmap->dim, isl_dim_out))
7547 isl_die(bmap->ctx, isl_error_invalid,
7548 "domain and range don't match", goto error);
7549
7550 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7551 n = isl_basic_map_dim(bmap, isl_dim_in);
7552
7553 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
7554 domain = isl_basic_map_universe(dim);
7555
7556 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
7557 bmap = isl_basic_map_apply_range(bmap, domain);
7558 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
7559
7560 total = isl_basic_map_total_dim(bmap);
7561
7562 for (i = 0; i < n; ++i) {
7563 k = isl_basic_map_alloc_equality(bmap);
7564 if (k < 0)
7565 goto error;
7566 isl_seq_clr(bmap->eq[k], 1 + total);
7567 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
7568 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
7569 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
7570 }
7571
7572 bmap = isl_basic_map_gauss(bmap, NULL);
7573 return isl_basic_map_finalize(bmap);
7574error:
7575 isl_basic_map_free(bmap);
7576 return NULL;
7577}
7578
7579/*
7580 * returns [domain -> range] -> range - domain
7581 */
7582__isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
7583{
7584 int i;
7585 isl_space *domain_dim;
7586
7587 if (!map)
7588 return NULL;
7589
7590 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
7591 map->dim, isl_dim_out))
7592 isl_die(map->ctx, isl_error_invalid,
7593 "domain and range don't match", goto error);
7594
7595 map = isl_map_cow(map);
7596 if (!map)
7597 return NULL;
7598
7599 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
7600 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
7601 map->dim = isl_space_join(map->dim, domain_dim);
7602 if (!map->dim)
7603 goto error;
7604 for (i = 0; i < map->n; ++i) {
7605 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
7606 if (!map->p[i])
7607 goto error;
7608 }
7609 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
7610 return map;
7611error:
7612 isl_map_free(map);
7613 return NULL;
7614}
7615
7616static __isl_give isl_basic_map *basic_map_identity(__isl_take isl_space *dims)
7617{
7618 struct isl_basic_map *bmap;
7619 unsigned nparam;
7620 unsigned dim;
7621 int i;
7622
7623 if (!dims)
7624 return NULL;
7625
7626 nparam = dims->nparam;
7627 dim = dims->n_out;
7628 bmap = isl_basic_map_alloc_space(dims, 0, dim, 0);
7629 if (!bmap)
7630 goto error;
7631
7632 for (i = 0; i < dim; ++i) {
7633 int j = isl_basic_map_alloc_equality(bmap);
7634 if (j < 0)
7635 goto error;
7636 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7637 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7638 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
7639 }
7640 return isl_basic_map_finalize(bmap);
7641error:
7642 isl_basic_map_free(bmap);
7643 return NULL;
7644}
7645
7646__isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *dim)
7647{
7648 if (!dim)
7649 return NULL;
7650 if (dim->n_in != dim->n_out)
7651 isl_die(dim->ctx, isl_error_invalid,
7652 "number of input and output dimensions needs to be "
7653 "the same", goto error);
7654 return basic_map_identity(dim);
7655error:
7656 isl_space_free(dim);
7657 return NULL;
7658}
7659
Tobias Grosser52a25232015-02-04 20:55:43 +00007660__isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
7661{
7662 return isl_map_from_basic_map(isl_basic_map_identity(dim));
7663}
7664
Tobias Grosser52a25232015-02-04 20:55:43 +00007665__isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
7666{
7667 isl_space *dim = isl_set_get_space(set);
7668 isl_map *id;
7669 id = isl_map_identity(isl_space_map_from_set(dim));
7670 return isl_map_intersect_range(id, set);
7671}
7672
7673/* Construct a basic set with all set dimensions having only non-negative
7674 * values.
7675 */
7676__isl_give isl_basic_set *isl_basic_set_positive_orthant(
7677 __isl_take isl_space *space)
7678{
7679 int i;
7680 unsigned nparam;
7681 unsigned dim;
7682 struct isl_basic_set *bset;
7683
7684 if (!space)
7685 return NULL;
7686 nparam = space->nparam;
7687 dim = space->n_out;
7688 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
7689 if (!bset)
7690 return NULL;
7691 for (i = 0; i < dim; ++i) {
7692 int k = isl_basic_set_alloc_inequality(bset);
7693 if (k < 0)
7694 goto error;
7695 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
7696 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
7697 }
7698 return bset;
7699error:
7700 isl_basic_set_free(bset);
7701 return NULL;
7702}
7703
7704/* Construct the half-space x_pos >= 0.
7705 */
7706static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *dim,
7707 int pos)
7708{
7709 int k;
7710 isl_basic_set *nonneg;
7711
7712 nonneg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7713 k = isl_basic_set_alloc_inequality(nonneg);
7714 if (k < 0)
7715 goto error;
7716 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
7717 isl_int_set_si(nonneg->ineq[k][pos], 1);
7718
7719 return isl_basic_set_finalize(nonneg);
7720error:
7721 isl_basic_set_free(nonneg);
7722 return NULL;
7723}
7724
7725/* Construct the half-space x_pos <= -1.
7726 */
7727static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *dim, int pos)
7728{
7729 int k;
7730 isl_basic_set *neg;
7731
7732 neg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7733 k = isl_basic_set_alloc_inequality(neg);
7734 if (k < 0)
7735 goto error;
7736 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
7737 isl_int_set_si(neg->ineq[k][0], -1);
7738 isl_int_set_si(neg->ineq[k][pos], -1);
7739
7740 return isl_basic_set_finalize(neg);
7741error:
7742 isl_basic_set_free(neg);
7743 return NULL;
7744}
7745
7746__isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
7747 enum isl_dim_type type, unsigned first, unsigned n)
7748{
7749 int i;
Tobias Grossere0f8d592015-05-13 13:10:13 +00007750 unsigned offset;
Tobias Grosser52a25232015-02-04 20:55:43 +00007751 isl_basic_set *nonneg;
7752 isl_basic_set *neg;
7753
7754 if (!set)
7755 return NULL;
7756 if (n == 0)
7757 return set;
7758
7759 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
7760
Tobias Grossere0f8d592015-05-13 13:10:13 +00007761 offset = pos(set->dim, type);
Tobias Grosser52a25232015-02-04 20:55:43 +00007762 for (i = 0; i < n; ++i) {
7763 nonneg = nonneg_halfspace(isl_set_get_space(set),
Tobias Grossere0f8d592015-05-13 13:10:13 +00007764 offset + first + i);
7765 neg = neg_halfspace(isl_set_get_space(set), offset + first + i);
Tobias Grosser52a25232015-02-04 20:55:43 +00007766
7767 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
7768 }
7769
7770 return set;
7771error:
7772 isl_set_free(set);
7773 return NULL;
7774}
7775
7776static int foreach_orthant(__isl_take isl_set *set, int *signs, int first,
7777 int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7778 void *user)
7779{
7780 isl_set *half;
7781
7782 if (!set)
7783 return -1;
7784 if (isl_set_plain_is_empty(set)) {
7785 isl_set_free(set);
7786 return 0;
7787 }
7788 if (first == len)
7789 return fn(set, signs, user);
7790
7791 signs[first] = 1;
7792 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
7793 1 + first));
7794 half = isl_set_intersect(half, isl_set_copy(set));
7795 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
7796 goto error;
7797
7798 signs[first] = -1;
7799 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
7800 1 + first));
7801 half = isl_set_intersect(half, set);
7802 return foreach_orthant(half, signs, first + 1, len, fn, user);
7803error:
7804 isl_set_free(set);
7805 return -1;
7806}
7807
7808/* Call "fn" on the intersections of "set" with each of the orthants
7809 * (except for obviously empty intersections). The orthant is identified
7810 * by the signs array, with each entry having value 1 or -1 according
7811 * to the sign of the corresponding variable.
7812 */
7813int isl_set_foreach_orthant(__isl_keep isl_set *set,
7814 int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7815 void *user)
7816{
7817 unsigned nparam;
7818 unsigned nvar;
7819 int *signs;
7820 int r;
7821
7822 if (!set)
7823 return -1;
7824 if (isl_set_plain_is_empty(set))
7825 return 0;
7826
7827 nparam = isl_set_dim(set, isl_dim_param);
7828 nvar = isl_set_dim(set, isl_dim_set);
7829
7830 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
7831
7832 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
7833 fn, user);
7834
7835 free(signs);
7836
7837 return r;
7838}
7839
Tobias Grosserb2f39922015-05-28 13:32:11 +00007840isl_bool isl_set_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
Tobias Grosser52a25232015-02-04 20:55:43 +00007841{
Tobias Grosser06e15922016-11-16 11:06:47 +00007842 return isl_map_is_equal(set_to_map(set1), set_to_map(set2));
Tobias Grosser52a25232015-02-04 20:55:43 +00007843}
7844
Tobias Grosserb2f39922015-05-28 13:32:11 +00007845isl_bool isl_basic_map_is_subset(__isl_keep isl_basic_map *bmap1,
7846 __isl_keep isl_basic_map *bmap2)
Tobias Grosser52a25232015-02-04 20:55:43 +00007847{
7848 int is_subset;
7849 struct isl_map *map1;
7850 struct isl_map *map2;
7851
7852 if (!bmap1 || !bmap2)
Tobias Grosserb2f39922015-05-28 13:32:11 +00007853 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00007854
7855 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
7856 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
7857
7858 is_subset = isl_map_is_subset(map1, map2);
7859
7860 isl_map_free(map1);
7861 isl_map_free(map2);
7862
7863 return is_subset;
7864}
7865
Tobias Grosserb2f39922015-05-28 13:32:11 +00007866isl_bool isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
Tobias Grosser52a25232015-02-04 20:55:43 +00007867 __isl_keep isl_basic_set *bset2)
7868{
7869 return isl_basic_map_is_subset(bset1, bset2);
7870}
7871
Tobias Grosserb2f39922015-05-28 13:32:11 +00007872isl_bool isl_basic_map_is_equal(__isl_keep isl_basic_map *bmap1,
7873 __isl_keep isl_basic_map *bmap2)
Tobias Grosser52a25232015-02-04 20:55:43 +00007874{
Tobias Grosserb2f39922015-05-28 13:32:11 +00007875 isl_bool is_subset;
Tobias Grosser52a25232015-02-04 20:55:43 +00007876
7877 if (!bmap1 || !bmap2)
Tobias Grosserb2f39922015-05-28 13:32:11 +00007878 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00007879 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
Tobias Grosserb2f39922015-05-28 13:32:11 +00007880 if (is_subset != isl_bool_true)
Tobias Grosser52a25232015-02-04 20:55:43 +00007881 return is_subset;
7882 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7883 return is_subset;
7884}
7885
Tobias Grosserb2f39922015-05-28 13:32:11 +00007886isl_bool isl_basic_set_is_equal(__isl_keep isl_basic_set *bset1,
7887 __isl_keep isl_basic_set *bset2)
Tobias Grosser52a25232015-02-04 20:55:43 +00007888{
7889 return isl_basic_map_is_equal(
Tobias Grosser06e15922016-11-16 11:06:47 +00007890 bset_to_bmap(bset1), bset_to_bmap(bset2));
Tobias Grosser52a25232015-02-04 20:55:43 +00007891}
7892
Tobias Grosserb2f39922015-05-28 13:32:11 +00007893isl_bool isl_map_is_empty(__isl_keep isl_map *map)
Tobias Grosser52a25232015-02-04 20:55:43 +00007894{
7895 int i;
7896 int is_empty;
7897
7898 if (!map)
Tobias Grosserb2f39922015-05-28 13:32:11 +00007899 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00007900 for (i = 0; i < map->n; ++i) {
7901 is_empty = isl_basic_map_is_empty(map->p[i]);
7902 if (is_empty < 0)
Tobias Grosserb2f39922015-05-28 13:32:11 +00007903 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00007904 if (!is_empty)
Tobias Grosserb2f39922015-05-28 13:32:11 +00007905 return isl_bool_false;
Tobias Grosser52a25232015-02-04 20:55:43 +00007906 }
Tobias Grosserb2f39922015-05-28 13:32:11 +00007907 return isl_bool_true;
Tobias Grosser52a25232015-02-04 20:55:43 +00007908}
7909
Tobias Grosserb2f39922015-05-28 13:32:11 +00007910isl_bool isl_map_plain_is_empty(__isl_keep isl_map *map)
Tobias Grosser52a25232015-02-04 20:55:43 +00007911{
Tobias Grosserb2f39922015-05-28 13:32:11 +00007912 return map ? map->n == 0 : isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00007913}
7914
Tobias Grosserb2f39922015-05-28 13:32:11 +00007915isl_bool isl_set_plain_is_empty(__isl_keep isl_set *set)
Tobias Grosser52a25232015-02-04 20:55:43 +00007916{
Tobias Grosserb2f39922015-05-28 13:32:11 +00007917 return set ? set->n == 0 : isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00007918}
7919
Tobias Grosserb2f39922015-05-28 13:32:11 +00007920isl_bool isl_set_is_empty(__isl_keep isl_set *set)
Tobias Grosser52a25232015-02-04 20:55:43 +00007921{
Tobias Grosser06e15922016-11-16 11:06:47 +00007922 return isl_map_is_empty(set_to_map(set));
Tobias Grosser52a25232015-02-04 20:55:43 +00007923}
7924
7925int isl_map_has_equal_space(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7926{
7927 if (!map1 || !map2)
7928 return -1;
7929
7930 return isl_space_is_equal(map1->dim, map2->dim);
7931}
7932
7933int isl_set_has_equal_space(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7934{
7935 if (!set1 || !set2)
7936 return -1;
7937
7938 return isl_space_is_equal(set1->dim, set2->dim);
7939}
7940
Tobias Grosserb2f39922015-05-28 13:32:11 +00007941static isl_bool map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
Tobias Grosser52a25232015-02-04 20:55:43 +00007942{
Tobias Grosserb2f39922015-05-28 13:32:11 +00007943 isl_bool is_subset;
Tobias Grosser52a25232015-02-04 20:55:43 +00007944
7945 if (!map1 || !map2)
Tobias Grosserb2f39922015-05-28 13:32:11 +00007946 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00007947 is_subset = isl_map_is_subset(map1, map2);
Tobias Grosserb2f39922015-05-28 13:32:11 +00007948 if (is_subset != isl_bool_true)
Tobias Grosser52a25232015-02-04 20:55:43 +00007949 return is_subset;
7950 is_subset = isl_map_is_subset(map2, map1);
7951 return is_subset;
7952}
7953
Tobias Grosser932ec012016-07-06 09:11:00 +00007954/* Is "map1" equal to "map2"?
7955 *
7956 * First check if they are obviously equal.
7957 * If not, then perform a more detailed analysis.
7958 */
Tobias Grosserb2f39922015-05-28 13:32:11 +00007959isl_bool isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
Tobias Grosser52a25232015-02-04 20:55:43 +00007960{
Tobias Grosser932ec012016-07-06 09:11:00 +00007961 isl_bool equal;
7962
7963 equal = isl_map_plain_is_equal(map1, map2);
7964 if (equal < 0 || equal)
7965 return equal;
Tobias Grosser52a25232015-02-04 20:55:43 +00007966 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
7967}
7968
Tobias Grosserb2f39922015-05-28 13:32:11 +00007969isl_bool isl_basic_map_is_strict_subset(
Tobias Grosser52a25232015-02-04 20:55:43 +00007970 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7971{
Tobias Grosserb2f39922015-05-28 13:32:11 +00007972 isl_bool is_subset;
Tobias Grosser52a25232015-02-04 20:55:43 +00007973
7974 if (!bmap1 || !bmap2)
Tobias Grosserb2f39922015-05-28 13:32:11 +00007975 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00007976 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
Tobias Grosserb2f39922015-05-28 13:32:11 +00007977 if (is_subset != isl_bool_true)
Tobias Grosser52a25232015-02-04 20:55:43 +00007978 return is_subset;
7979 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
Tobias Grosserb2f39922015-05-28 13:32:11 +00007980 if (is_subset == isl_bool_error)
Tobias Grosser52a25232015-02-04 20:55:43 +00007981 return is_subset;
7982 return !is_subset;
7983}
7984
Tobias Grosserb2f39922015-05-28 13:32:11 +00007985isl_bool isl_map_is_strict_subset(__isl_keep isl_map *map1,
7986 __isl_keep isl_map *map2)
Tobias Grosser52a25232015-02-04 20:55:43 +00007987{
Tobias Grosserb2f39922015-05-28 13:32:11 +00007988 isl_bool is_subset;
Tobias Grosser52a25232015-02-04 20:55:43 +00007989
7990 if (!map1 || !map2)
Tobias Grosserb2f39922015-05-28 13:32:11 +00007991 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00007992 is_subset = isl_map_is_subset(map1, map2);
Tobias Grosserb2f39922015-05-28 13:32:11 +00007993 if (is_subset != isl_bool_true)
Tobias Grosser52a25232015-02-04 20:55:43 +00007994 return is_subset;
7995 is_subset = isl_map_is_subset(map2, map1);
Tobias Grosserb2f39922015-05-28 13:32:11 +00007996 if (is_subset == isl_bool_error)
Tobias Grosser52a25232015-02-04 20:55:43 +00007997 return is_subset;
7998 return !is_subset;
7999}
8000
Tobias Grosserb2f39922015-05-28 13:32:11 +00008001isl_bool isl_set_is_strict_subset(__isl_keep isl_set *set1,
8002 __isl_keep isl_set *set2)
Tobias Grosser52a25232015-02-04 20:55:43 +00008003{
Tobias Grosser06e15922016-11-16 11:06:47 +00008004 return isl_map_is_strict_subset(set_to_map(set1), set_to_map(set2));
Tobias Grosser52a25232015-02-04 20:55:43 +00008005}
8006
Tobias Grossera67ac972016-02-26 11:35:12 +00008007/* Is "bmap" obviously equal to the universe with the same space?
8008 *
8009 * That is, does it not have any constraints?
8010 */
8011isl_bool isl_basic_map_plain_is_universe(__isl_keep isl_basic_map *bmap)
Tobias Grosser52a25232015-02-04 20:55:43 +00008012{
8013 if (!bmap)
Tobias Grosserb2f39922015-05-28 13:32:11 +00008014 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00008015 return bmap->n_eq == 0 && bmap->n_ineq == 0;
8016}
8017
Tobias Grossera67ac972016-02-26 11:35:12 +00008018/* Is "bset" obviously equal to the universe with the same space?
8019 */
8020isl_bool isl_basic_set_plain_is_universe(__isl_keep isl_basic_set *bset)
8021{
8022 return isl_basic_map_plain_is_universe(bset);
8023}
8024
8025/* If "c" does not involve any existentially quantified variables,
8026 * then set *univ to false and abort
8027 */
8028static isl_stat involves_divs(__isl_take isl_constraint *c, void *user)
8029{
8030 isl_bool *univ = user;
8031 unsigned n;
8032
8033 n = isl_constraint_dim(c, isl_dim_div);
8034 *univ = isl_constraint_involves_dims(c, isl_dim_div, 0, n);
8035 isl_constraint_free(c);
8036 if (*univ < 0 || !*univ)
8037 return isl_stat_error;
8038 return isl_stat_ok;
8039}
8040
8041/* Is "bmap" equal to the universe with the same space?
8042 *
8043 * First check if it is obviously equal to the universe.
8044 * If not and if there are any constraints not involving
8045 * existentially quantified variables, then it is certainly
8046 * not equal to the universe.
8047 * Otherwise, check if the universe is a subset of "bmap".
8048 */
8049isl_bool isl_basic_map_is_universe(__isl_keep isl_basic_map *bmap)
8050{
8051 isl_bool univ;
8052 isl_basic_map *test;
8053
8054 univ = isl_basic_map_plain_is_universe(bmap);
8055 if (univ < 0 || univ)
8056 return univ;
8057 if (isl_basic_map_dim(bmap, isl_dim_div) == 0)
8058 return isl_bool_false;
8059 univ = isl_bool_true;
8060 if (isl_basic_map_foreach_constraint(bmap, &involves_divs, &univ) < 0 &&
8061 univ)
8062 return isl_bool_error;
8063 if (univ < 0 || !univ)
8064 return univ;
8065 test = isl_basic_map_universe(isl_basic_map_get_space(bmap));
8066 univ = isl_basic_map_is_subset(test, bmap);
8067 isl_basic_map_free(test);
8068 return univ;
8069}
8070
8071/* Is "bset" equal to the universe with the same space?
8072 */
Tobias Grosserb2f39922015-05-28 13:32:11 +00008073isl_bool isl_basic_set_is_universe(__isl_keep isl_basic_set *bset)
Tobias Grosser52a25232015-02-04 20:55:43 +00008074{
Tobias Grossera67ac972016-02-26 11:35:12 +00008075 return isl_basic_map_is_universe(bset);
Tobias Grosser52a25232015-02-04 20:55:43 +00008076}
8077
Tobias Grosserb2f39922015-05-28 13:32:11 +00008078isl_bool isl_map_plain_is_universe(__isl_keep isl_map *map)
Tobias Grosser52a25232015-02-04 20:55:43 +00008079{
8080 int i;
8081
8082 if (!map)
Tobias Grosserb2f39922015-05-28 13:32:11 +00008083 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00008084
8085 for (i = 0; i < map->n; ++i) {
Tobias Grossera67ac972016-02-26 11:35:12 +00008086 isl_bool r = isl_basic_map_plain_is_universe(map->p[i]);
Tobias Grosser52a25232015-02-04 20:55:43 +00008087 if (r < 0 || r)
8088 return r;
8089 }
8090
Tobias Grosserb2f39922015-05-28 13:32:11 +00008091 return isl_bool_false;
Tobias Grosser52a25232015-02-04 20:55:43 +00008092}
8093
Tobias Grosserb2f39922015-05-28 13:32:11 +00008094isl_bool isl_set_plain_is_universe(__isl_keep isl_set *set)
Tobias Grosser52a25232015-02-04 20:55:43 +00008095{
Tobias Grosser06e15922016-11-16 11:06:47 +00008096 return isl_map_plain_is_universe(set_to_map(set));
Tobias Grosser52a25232015-02-04 20:55:43 +00008097}
8098
Tobias Grosserb2f39922015-05-28 13:32:11 +00008099isl_bool isl_basic_map_is_empty(__isl_keep isl_basic_map *bmap)
Tobias Grosser52a25232015-02-04 20:55:43 +00008100{
8101 struct isl_basic_set *bset = NULL;
8102 struct isl_vec *sample = NULL;
Tobias Grossera67ac972016-02-26 11:35:12 +00008103 isl_bool empty, non_empty;
Tobias Grosser52a25232015-02-04 20:55:43 +00008104
8105 if (!bmap)
Tobias Grosserb2f39922015-05-28 13:32:11 +00008106 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00008107
8108 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
Tobias Grosserb2f39922015-05-28 13:32:11 +00008109 return isl_bool_true;
Tobias Grosser52a25232015-02-04 20:55:43 +00008110
Tobias Grossera67ac972016-02-26 11:35:12 +00008111 if (isl_basic_map_plain_is_universe(bmap))
Tobias Grosserb2f39922015-05-28 13:32:11 +00008112 return isl_bool_false;
Tobias Grosser52a25232015-02-04 20:55:43 +00008113
8114 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
8115 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
8116 copy = isl_basic_map_remove_redundancies(copy);
8117 empty = isl_basic_map_plain_is_empty(copy);
8118 isl_basic_map_free(copy);
8119 return empty;
8120 }
8121
Tobias Grossera67ac972016-02-26 11:35:12 +00008122 non_empty = isl_basic_map_plain_is_non_empty(bmap);
8123 if (non_empty < 0)
8124 return isl_bool_error;
8125 if (non_empty)
8126 return isl_bool_false;
Tobias Grosser52a25232015-02-04 20:55:43 +00008127 isl_vec_free(bmap->sample);
8128 bmap->sample = NULL;
8129 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
8130 if (!bset)
Tobias Grosserb2f39922015-05-28 13:32:11 +00008131 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00008132 sample = isl_basic_set_sample_vec(bset);
8133 if (!sample)
Tobias Grosserb2f39922015-05-28 13:32:11 +00008134 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00008135 empty = sample->size == 0;
8136 isl_vec_free(bmap->sample);
8137 bmap->sample = sample;
8138 if (empty)
8139 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
8140
8141 return empty;
8142}
8143
Tobias Grosserb2f39922015-05-28 13:32:11 +00008144isl_bool isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
Tobias Grosser52a25232015-02-04 20:55:43 +00008145{
8146 if (!bmap)
Tobias Grosserb2f39922015-05-28 13:32:11 +00008147 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00008148 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
8149}
8150
Tobias Grosserb2f39922015-05-28 13:32:11 +00008151isl_bool isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
Tobias Grosser52a25232015-02-04 20:55:43 +00008152{
8153 if (!bset)
Tobias Grosserb2f39922015-05-28 13:32:11 +00008154 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00008155 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
8156}
8157
Tobias Grossera67ac972016-02-26 11:35:12 +00008158/* Is "bmap" known to be non-empty?
8159 *
8160 * That is, is the cached sample still valid?
8161 */
8162isl_bool isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map *bmap)
8163{
8164 unsigned total;
8165
8166 if (!bmap)
8167 return isl_bool_error;
8168 if (!bmap->sample)
8169 return isl_bool_false;
8170 total = 1 + isl_basic_map_total_dim(bmap);
8171 if (bmap->sample->size != total)
8172 return isl_bool_false;
8173 return isl_basic_map_contains(bmap, bmap->sample);
8174}
8175
Tobias Grosserb2f39922015-05-28 13:32:11 +00008176isl_bool isl_basic_set_is_empty(__isl_keep isl_basic_set *bset)
Tobias Grosser52a25232015-02-04 20:55:43 +00008177{
Tobias Grosser06e15922016-11-16 11:06:47 +00008178 return isl_basic_map_is_empty(bset_to_bmap(bset));
Tobias Grosser52a25232015-02-04 20:55:43 +00008179}
8180
8181struct isl_map *isl_basic_map_union(
8182 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8183{
8184 struct isl_map *map;
8185 if (!bmap1 || !bmap2)
8186 goto error;
8187
8188 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
8189
8190 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
8191 if (!map)
8192 goto error;
8193 map = isl_map_add_basic_map(map, bmap1);
8194 map = isl_map_add_basic_map(map, bmap2);
8195 return map;
8196error:
8197 isl_basic_map_free(bmap1);
8198 isl_basic_map_free(bmap2);
8199 return NULL;
8200}
8201
8202struct isl_set *isl_basic_set_union(
8203 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
8204{
Tobias Grosser06e15922016-11-16 11:06:47 +00008205 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1),
8206 bset_to_bmap(bset2)));
Tobias Grosser52a25232015-02-04 20:55:43 +00008207}
8208
8209/* Order divs such that any div only depends on previous divs */
8210struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
8211{
8212 int i;
8213 unsigned off;
8214
8215 if (!bmap)
8216 return NULL;
8217
8218 off = isl_space_dim(bmap->dim, isl_dim_all);
8219
8220 for (i = 0; i < bmap->n_div; ++i) {
8221 int pos;
8222 if (isl_int_is_zero(bmap->div[i][0]))
8223 continue;
8224 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
8225 bmap->n_div-i);
8226 if (pos == -1)
8227 continue;
Tobias Grosser37034db2016-03-25 19:38:18 +00008228 if (pos == 0)
8229 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
8230 "integer division depends on itself",
8231 return isl_basic_map_free(bmap));
Tobias Grosser52a25232015-02-04 20:55:43 +00008232 isl_basic_map_swap_div(bmap, i, i + pos);
8233 --i;
8234 }
8235 return bmap;
8236}
8237
8238struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
8239{
Tobias Grosser06e15922016-11-16 11:06:47 +00008240 return bset_from_bmap(isl_basic_map_order_divs(bset_to_bmap(bset)));
Tobias Grosser52a25232015-02-04 20:55:43 +00008241}
8242
8243__isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
8244{
8245 int i;
8246
8247 if (!map)
8248 return 0;
8249
8250 for (i = 0; i < map->n; ++i) {
8251 map->p[i] = isl_basic_map_order_divs(map->p[i]);
8252 if (!map->p[i])
8253 goto error;
8254 }
8255
8256 return map;
8257error:
8258 isl_map_free(map);
8259 return NULL;
8260}
8261
Tobias Grosser06e15922016-11-16 11:06:47 +00008262/* Sort the local variables of "bset".
8263 */
8264__isl_give isl_basic_set *isl_basic_set_sort_divs(
8265 __isl_take isl_basic_set *bset)
8266{
8267 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset)));
8268}
8269
Tobias Grosser52a25232015-02-04 20:55:43 +00008270/* Apply the expansion computed by isl_merge_divs.
8271 * The expansion itself is given by "exp" while the resulting
8272 * list of divs is given by "div".
Tobias Grossera67ac972016-02-26 11:35:12 +00008273 *
Tobias Grosser07b20952016-06-12 04:30:40 +00008274 * Move the integer divisions of "bmap" into the right position
Tobias Grossera67ac972016-02-26 11:35:12 +00008275 * according to "exp" and then introduce the additional integer
8276 * divisions, adding div constraints.
8277 * The moving should be done first to avoid moving coefficients
8278 * in the definitions of the extra integer divisions.
Tobias Grosser52a25232015-02-04 20:55:43 +00008279 */
Tobias Grosser07b20952016-06-12 04:30:40 +00008280__isl_give isl_basic_map *isl_basic_map_expand_divs(
Tobias Grosser06e15922016-11-16 11:06:47 +00008281 __isl_take isl_basic_map *bmap, __isl_take isl_mat *div, int *exp)
Tobias Grosser52a25232015-02-04 20:55:43 +00008282{
8283 int i, j;
8284 int n_div;
8285
Tobias Grosser07b20952016-06-12 04:30:40 +00008286 bmap = isl_basic_map_cow(bmap);
8287 if (!bmap || !div)
Tobias Grosser52a25232015-02-04 20:55:43 +00008288 goto error;
8289
Tobias Grosser07b20952016-06-12 04:30:40 +00008290 if (div->n_row < bmap->n_div)
Tobias Grosser52a25232015-02-04 20:55:43 +00008291 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
8292 "not an expansion", goto error);
8293
Tobias Grosser07b20952016-06-12 04:30:40 +00008294 n_div = bmap->n_div;
8295 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
Tobias Grosser52a25232015-02-04 20:55:43 +00008296 div->n_row - n_div, 0,
8297 2 * (div->n_row - n_div));
8298
8299 for (i = n_div; i < div->n_row; ++i)
Tobias Grosser07b20952016-06-12 04:30:40 +00008300 if (isl_basic_map_alloc_div(bmap) < 0)
Tobias Grosser52a25232015-02-04 20:55:43 +00008301 goto error;
8302
Tobias Grossera67ac972016-02-26 11:35:12 +00008303 for (j = n_div - 1; j >= 0; --j) {
8304 if (exp[j] == j)
8305 break;
Tobias Grosser07b20952016-06-12 04:30:40 +00008306 isl_basic_map_swap_div(bmap, j, exp[j]);
Tobias Grossera67ac972016-02-26 11:35:12 +00008307 }
8308 j = 0;
8309 for (i = 0; i < div->n_row; ++i) {
8310 if (j < n_div && exp[j] == i) {
8311 j++;
Tobias Grosser52a25232015-02-04 20:55:43 +00008312 } else {
Tobias Grosser07b20952016-06-12 04:30:40 +00008313 isl_seq_cpy(bmap->div[i], div->row[i], div->n_col);
Tobias Grosser932ec012016-07-06 09:11:00 +00008314 if (isl_basic_map_div_is_marked_unknown(bmap, i))
Tobias Grosser07b20952016-06-12 04:30:40 +00008315 continue;
8316 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
Tobias Grosser52a25232015-02-04 20:55:43 +00008317 goto error;
8318 }
8319 }
8320
8321 isl_mat_free(div);
Tobias Grosser07b20952016-06-12 04:30:40 +00008322 return bmap;
Tobias Grosser52a25232015-02-04 20:55:43 +00008323error:
Tobias Grosser07b20952016-06-12 04:30:40 +00008324 isl_basic_map_free(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +00008325 isl_mat_free(div);
8326 return NULL;
8327}
8328
Tobias Grosser07b20952016-06-12 04:30:40 +00008329/* Apply the expansion computed by isl_merge_divs.
8330 * The expansion itself is given by "exp" while the resulting
8331 * list of divs is given by "div".
8332 */
8333__isl_give isl_basic_set *isl_basic_set_expand_divs(
8334 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
8335{
8336 return isl_basic_map_expand_divs(bset, div, exp);
8337}
8338
Tobias Grosser52a25232015-02-04 20:55:43 +00008339/* Look for a div in dst that corresponds to the div "div" in src.
8340 * The divs before "div" in src and dst are assumed to be the same.
8341 *
8342 * Returns -1 if no corresponding div was found and the position
8343 * of the corresponding div in dst otherwise.
8344 */
8345static int find_div(struct isl_basic_map *dst,
8346 struct isl_basic_map *src, unsigned div)
8347{
8348 int i;
8349
8350 unsigned total = isl_space_dim(src->dim, isl_dim_all);
8351
8352 isl_assert(dst->ctx, div <= dst->n_div, return -1);
8353 for (i = div; i < dst->n_div; ++i)
8354 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
8355 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
8356 dst->n_div - div) == -1)
8357 return i;
8358 return -1;
8359}
8360
8361/* Align the divs of "dst" to those of "src", adding divs from "src"
8362 * if needed. That is, make sure that the first src->n_div divs
8363 * of the result are equal to those of src.
8364 *
8365 * The result is not finalized as by design it will have redundant
8366 * divs if any divs from "src" were copied.
8367 */
8368__isl_give isl_basic_map *isl_basic_map_align_divs(
8369 __isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src)
8370{
8371 int i;
8372 int known, extended;
8373 unsigned total;
8374
8375 if (!dst || !src)
8376 return isl_basic_map_free(dst);
8377
8378 if (src->n_div == 0)
8379 return dst;
8380
8381 known = isl_basic_map_divs_known(src);
8382 if (known < 0)
8383 return isl_basic_map_free(dst);
8384 if (!known)
8385 isl_die(isl_basic_map_get_ctx(src), isl_error_invalid,
8386 "some src divs are unknown",
8387 return isl_basic_map_free(dst));
8388
8389 src = isl_basic_map_order_divs(src);
8390
8391 extended = 0;
8392 total = isl_space_dim(src->dim, isl_dim_all);
8393 for (i = 0; i < src->n_div; ++i) {
8394 int j = find_div(dst, src, i);
8395 if (j < 0) {
8396 if (!extended) {
8397 int extra = src->n_div - i;
8398 dst = isl_basic_map_cow(dst);
8399 if (!dst)
8400 return NULL;
8401 dst = isl_basic_map_extend_space(dst,
8402 isl_space_copy(dst->dim),
8403 extra, 0, 2 * extra);
8404 extended = 1;
8405 }
8406 j = isl_basic_map_alloc_div(dst);
8407 if (j < 0)
8408 return isl_basic_map_free(dst);
8409 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
8410 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
8411 if (isl_basic_map_add_div_constraints(dst, j) < 0)
8412 return isl_basic_map_free(dst);
8413 }
8414 if (j != i)
8415 isl_basic_map_swap_div(dst, i, j);
8416 }
8417 return dst;
8418}
8419
8420struct isl_basic_set *isl_basic_set_align_divs(
8421 struct isl_basic_set *dst, struct isl_basic_set *src)
8422{
Tobias Grosser06e15922016-11-16 11:06:47 +00008423 return bset_from_bmap(isl_basic_map_align_divs(bset_to_bmap(dst),
8424 bset_to_bmap(src)));
Tobias Grosser52a25232015-02-04 20:55:43 +00008425}
8426
8427struct isl_map *isl_map_align_divs(struct isl_map *map)
8428{
8429 int i;
8430
8431 if (!map)
8432 return NULL;
8433 if (map->n == 0)
8434 return map;
8435 map = isl_map_compute_divs(map);
8436 map = isl_map_cow(map);
8437 if (!map)
8438 return NULL;
8439
8440 for (i = 1; i < map->n; ++i)
8441 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
8442 for (i = 1; i < map->n; ++i) {
8443 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
8444 if (!map->p[i])
8445 return isl_map_free(map);
8446 }
8447
8448 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
8449 return map;
8450}
8451
8452struct isl_set *isl_set_align_divs(struct isl_set *set)
8453{
Tobias Grosser06e15922016-11-16 11:06:47 +00008454 return set_from_map(isl_map_align_divs(set_to_map(set)));
Tobias Grosser52a25232015-02-04 20:55:43 +00008455}
8456
Tobias Grosser1fa7b972015-02-16 19:33:40 +00008457/* Align the divs of the basic maps in "map" to those
8458 * of the basic maps in "list", as well as to the other basic maps in "map".
Tobias Grosser52a25232015-02-04 20:55:43 +00008459 * The elements in "list" are assumed to have known divs.
8460 */
Tobias Grosser1fa7b972015-02-16 19:33:40 +00008461__isl_give isl_map *isl_map_align_divs_to_basic_map_list(
8462 __isl_take isl_map *map, __isl_keep isl_basic_map_list *list)
Tobias Grosser52a25232015-02-04 20:55:43 +00008463{
8464 int i, n;
8465
Tobias Grosser1fa7b972015-02-16 19:33:40 +00008466 map = isl_map_compute_divs(map);
8467 map = isl_map_cow(map);
8468 if (!map || !list)
8469 return isl_map_free(map);
8470 if (map->n == 0)
8471 return map;
Tobias Grosser52a25232015-02-04 20:55:43 +00008472
Tobias Grosser1fa7b972015-02-16 19:33:40 +00008473 n = isl_basic_map_list_n_basic_map(list);
Tobias Grosser52a25232015-02-04 20:55:43 +00008474 for (i = 0; i < n; ++i) {
Tobias Grosser1fa7b972015-02-16 19:33:40 +00008475 isl_basic_map *bmap;
Tobias Grosser52a25232015-02-04 20:55:43 +00008476
Tobias Grosser1fa7b972015-02-16 19:33:40 +00008477 bmap = isl_basic_map_list_get_basic_map(list, i);
8478 map->p[0] = isl_basic_map_align_divs(map->p[0], bmap);
8479 isl_basic_map_free(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +00008480 }
Tobias Grosser1fa7b972015-02-16 19:33:40 +00008481 if (!map->p[0])
8482 return isl_map_free(map);
Tobias Grosser52a25232015-02-04 20:55:43 +00008483
Tobias Grosser1fa7b972015-02-16 19:33:40 +00008484 return isl_map_align_divs(map);
Tobias Grosser52a25232015-02-04 20:55:43 +00008485}
8486
Tobias Grosser1fa7b972015-02-16 19:33:40 +00008487/* Align the divs of each element of "list" to those of "bmap".
8488 * Both "bmap" and the elements of "list" are assumed to have known divs.
Tobias Grosser52a25232015-02-04 20:55:43 +00008489 */
Tobias Grosser1fa7b972015-02-16 19:33:40 +00008490__isl_give isl_basic_map_list *isl_basic_map_list_align_divs_to_basic_map(
8491 __isl_take isl_basic_map_list *list, __isl_keep isl_basic_map *bmap)
Tobias Grosser52a25232015-02-04 20:55:43 +00008492{
8493 int i, n;
8494
Tobias Grosser1fa7b972015-02-16 19:33:40 +00008495 if (!list || !bmap)
8496 return isl_basic_map_list_free(list);
Tobias Grosser52a25232015-02-04 20:55:43 +00008497
Tobias Grosser1fa7b972015-02-16 19:33:40 +00008498 n = isl_basic_map_list_n_basic_map(list);
Tobias Grosser52a25232015-02-04 20:55:43 +00008499 for (i = 0; i < n; ++i) {
Tobias Grosser1fa7b972015-02-16 19:33:40 +00008500 isl_basic_map *bmap_i;
Tobias Grosser52a25232015-02-04 20:55:43 +00008501
Tobias Grosser1fa7b972015-02-16 19:33:40 +00008502 bmap_i = isl_basic_map_list_get_basic_map(list, i);
8503 bmap_i = isl_basic_map_align_divs(bmap_i, bmap);
8504 list = isl_basic_map_list_set_basic_map(list, i, bmap_i);
Tobias Grosser52a25232015-02-04 20:55:43 +00008505 }
8506
8507 return list;
8508}
8509
8510static __isl_give isl_set *set_apply( __isl_take isl_set *set,
8511 __isl_take isl_map *map)
8512{
8513 if (!set || !map)
8514 goto error;
8515 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
8516 map = isl_map_intersect_domain(map, set);
8517 set = isl_map_range(map);
8518 return set;
8519error:
8520 isl_set_free(set);
8521 isl_map_free(map);
8522 return NULL;
8523}
8524
8525__isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
8526 __isl_take isl_map *map)
8527{
8528 return isl_map_align_params_map_map_and(set, map, &set_apply);
8529}
8530
8531/* There is no need to cow as removing empty parts doesn't change
8532 * the meaning of the set.
8533 */
8534struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
8535{
8536 int i;
8537
8538 if (!map)
8539 return NULL;
8540
8541 for (i = map->n - 1; i >= 0; --i)
8542 remove_if_empty(map, i);
8543
8544 return map;
8545}
8546
8547struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
8548{
Tobias Grosser06e15922016-11-16 11:06:47 +00008549 return set_from_map(isl_map_remove_empty_parts(set_to_map(set)));
Tobias Grosser52a25232015-02-04 20:55:43 +00008550}
8551
Tobias Grossereab0943e2016-11-22 21:31:59 +00008552static __isl_give isl_basic_map *map_copy_basic_map(__isl_keep isl_map *map)
Tobias Grosser52a25232015-02-04 20:55:43 +00008553{
8554 struct isl_basic_map *bmap;
8555 if (!map || map->n == 0)
8556 return NULL;
8557 bmap = map->p[map->n-1];
8558 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
8559 return isl_basic_map_copy(bmap);
8560}
8561
Tobias Grossereab0943e2016-11-22 21:31:59 +00008562__isl_give isl_basic_map *isl_map_copy_basic_map(__isl_keep isl_map *map)
Tobias Grosser52a25232015-02-04 20:55:43 +00008563{
Tobias Grossereab0943e2016-11-22 21:31:59 +00008564 return map_copy_basic_map(map);
Tobias Grosser52a25232015-02-04 20:55:43 +00008565}
8566
Tobias Grossereab0943e2016-11-22 21:31:59 +00008567struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
8568{
8569 return bset_from_bmap(map_copy_basic_map(set_to_map(set)));
8570}
8571
8572static __isl_give isl_map *map_drop_basic_map(__isl_take isl_map *map,
Tobias Grosser52a25232015-02-04 20:55:43 +00008573 __isl_keep isl_basic_map *bmap)
8574{
8575 int i;
8576
8577 if (!map || !bmap)
8578 goto error;
8579 for (i = map->n-1; i >= 0; --i) {
8580 if (map->p[i] != bmap)
8581 continue;
8582 map = isl_map_cow(map);
8583 if (!map)
8584 goto error;
8585 isl_basic_map_free(map->p[i]);
8586 if (i != map->n-1) {
8587 ISL_F_CLR(map, ISL_SET_NORMALIZED);
8588 map->p[i] = map->p[map->n-1];
8589 }
8590 map->n--;
8591 return map;
8592 }
8593 return map;
8594error:
8595 isl_map_free(map);
8596 return NULL;
8597}
8598
Tobias Grossereab0943e2016-11-22 21:31:59 +00008599__isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
8600 __isl_keep isl_basic_map *bmap)
8601{
8602 return map_drop_basic_map(map, bmap);
8603}
8604
Tobias Grosser52a25232015-02-04 20:55:43 +00008605struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
8606 struct isl_basic_set *bset)
8607{
Tobias Grossereab0943e2016-11-22 21:31:59 +00008608 return set_from_map(map_drop_basic_map(set_to_map(set),
Tobias Grosser06e15922016-11-16 11:06:47 +00008609 bset_to_bmap(bset)));
Tobias Grosser52a25232015-02-04 20:55:43 +00008610}
8611
8612/* Given two basic sets bset1 and bset2, compute the maximal difference
8613 * between the values of dimension pos in bset1 and those in bset2
8614 * for any common value of the parameters and dimensions preceding pos.
8615 */
8616static enum isl_lp_result basic_set_maximal_difference_at(
8617 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
8618 int pos, isl_int *opt)
8619{
Tobias Grossereab0943e2016-11-22 21:31:59 +00008620 isl_basic_map *bmap1;
8621 isl_basic_map *bmap2;
Tobias Grosser52a25232015-02-04 20:55:43 +00008622 struct isl_ctx *ctx;
8623 struct isl_vec *obj;
8624 unsigned total;
8625 unsigned nparam;
Tobias Grossereab0943e2016-11-22 21:31:59 +00008626 unsigned dim1;
Tobias Grosser52a25232015-02-04 20:55:43 +00008627 enum isl_lp_result res;
8628
8629 if (!bset1 || !bset2)
8630 return isl_lp_error;
8631
8632 nparam = isl_basic_set_n_param(bset1);
8633 dim1 = isl_basic_set_n_dim(bset1);
Tobias Grossereab0943e2016-11-22 21:31:59 +00008634
8635 bmap1 = isl_basic_map_from_range(isl_basic_set_copy(bset1));
8636 bmap2 = isl_basic_map_from_range(isl_basic_set_copy(bset2));
8637 bmap1 = isl_basic_map_move_dims(bmap1, isl_dim_in, 0,
8638 isl_dim_out, 0, pos);
8639 bmap2 = isl_basic_map_move_dims(bmap2, isl_dim_in, 0,
8640 isl_dim_out, 0, pos);
8641 bmap1 = isl_basic_map_range_product(bmap1, bmap2);
Tobias Grosser52a25232015-02-04 20:55:43 +00008642 if (!bmap1)
Tobias Grossereab0943e2016-11-22 21:31:59 +00008643 return isl_lp_error;
8644
Tobias Grosser52a25232015-02-04 20:55:43 +00008645 total = isl_basic_map_total_dim(bmap1);
8646 ctx = bmap1->ctx;
8647 obj = isl_vec_alloc(ctx, 1 + total);
8648 if (!obj)
Tobias Grossereab0943e2016-11-22 21:31:59 +00008649 goto error;
Tobias Grosser52a25232015-02-04 20:55:43 +00008650 isl_seq_clr(obj->block.data, 1 + total);
8651 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
8652 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
8653 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
8654 opt, NULL, NULL);
8655 isl_basic_map_free(bmap1);
8656 isl_vec_free(obj);
8657 return res;
8658error:
Tobias Grosser52a25232015-02-04 20:55:43 +00008659 isl_basic_map_free(bmap1);
8660 return isl_lp_error;
8661}
8662
8663/* Given two _disjoint_ basic sets bset1 and bset2, check whether
8664 * for any common value of the parameters and dimensions preceding pos
8665 * in both basic sets, the values of dimension pos in bset1 are
8666 * smaller or larger than those in bset2.
8667 *
8668 * Returns
8669 * 1 if bset1 follows bset2
8670 * -1 if bset1 precedes bset2
8671 * 0 if bset1 and bset2 are incomparable
8672 * -2 if some error occurred.
8673 */
8674int isl_basic_set_compare_at(struct isl_basic_set *bset1,
8675 struct isl_basic_set *bset2, int pos)
8676{
8677 isl_int opt;
8678 enum isl_lp_result res;
8679 int cmp;
8680
8681 isl_int_init(opt);
8682
8683 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8684
8685 if (res == isl_lp_empty)
8686 cmp = 0;
8687 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8688 res == isl_lp_unbounded)
8689 cmp = 1;
8690 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8691 cmp = -1;
8692 else
8693 cmp = -2;
8694
8695 isl_int_clear(opt);
8696 return cmp;
8697}
8698
8699/* Given two basic sets bset1 and bset2, check whether
8700 * for any common value of the parameters and dimensions preceding pos
8701 * there is a value of dimension pos in bset1 that is larger
8702 * than a value of the same dimension in bset2.
8703 *
8704 * Return
8705 * 1 if there exists such a pair
8706 * 0 if there is no such pair, but there is a pair of equal values
8707 * -1 otherwise
8708 * -2 if some error occurred.
8709 */
8710int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
8711 __isl_keep isl_basic_set *bset2, int pos)
8712{
8713 isl_int opt;
8714 enum isl_lp_result res;
8715 int cmp;
8716
8717 isl_int_init(opt);
8718
8719 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8720
8721 if (res == isl_lp_empty)
8722 cmp = -1;
8723 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8724 res == isl_lp_unbounded)
8725 cmp = 1;
8726 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8727 cmp = -1;
8728 else if (res == isl_lp_ok)
8729 cmp = 0;
8730 else
8731 cmp = -2;
8732
8733 isl_int_clear(opt);
8734 return cmp;
8735}
8736
8737/* Given two sets set1 and set2, check whether
8738 * for any common value of the parameters and dimensions preceding pos
8739 * there is a value of dimension pos in set1 that is larger
8740 * than a value of the same dimension in set2.
8741 *
8742 * Return
8743 * 1 if there exists such a pair
8744 * 0 if there is no such pair, but there is a pair of equal values
8745 * -1 otherwise
8746 * -2 if some error occurred.
8747 */
8748int isl_set_follows_at(__isl_keep isl_set *set1,
8749 __isl_keep isl_set *set2, int pos)
8750{
8751 int i, j;
8752 int follows = -1;
8753
8754 if (!set1 || !set2)
8755 return -2;
8756
8757 for (i = 0; i < set1->n; ++i)
8758 for (j = 0; j < set2->n; ++j) {
8759 int f;
8760 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
8761 if (f == 1 || f == -2)
8762 return f;
8763 if (f > follows)
8764 follows = f;
8765 }
8766
8767 return follows;
8768}
8769
8770static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map *bmap,
8771 unsigned pos, isl_int *val)
8772{
8773 int i;
8774 int d;
8775 unsigned total;
8776
8777 if (!bmap)
8778 return -1;
8779 total = isl_basic_map_total_dim(bmap);
8780 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
8781 for (; d+1 > pos; --d)
8782 if (!isl_int_is_zero(bmap->eq[i][1+d]))
8783 break;
8784 if (d != pos)
8785 continue;
8786 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
8787 return 0;
8788 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
8789 return 0;
8790 if (!isl_int_is_one(bmap->eq[i][1+d]))
8791 return 0;
8792 if (val)
8793 isl_int_neg(*val, bmap->eq[i][0]);
8794 return 1;
8795 }
8796 return 0;
8797}
8798
8799static int isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
8800 unsigned pos, isl_int *val)
8801{
8802 int i;
8803 isl_int v;
8804 isl_int tmp;
8805 int fixed;
8806
8807 if (!map)
8808 return -1;
8809 if (map->n == 0)
8810 return 0;
8811 if (map->n == 1)
8812 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
8813 isl_int_init(v);
8814 isl_int_init(tmp);
8815 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
8816 for (i = 1; fixed == 1 && i < map->n; ++i) {
8817 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
8818 if (fixed == 1 && isl_int_ne(tmp, v))
8819 fixed = 0;
8820 }
8821 if (val)
8822 isl_int_set(*val, v);
8823 isl_int_clear(tmp);
8824 isl_int_clear(v);
8825 return fixed;
8826}
8827
8828static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set *bset,
8829 unsigned pos, isl_int *val)
8830{
Tobias Grosser06e15922016-11-16 11:06:47 +00008831 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset),
Tobias Grosser52a25232015-02-04 20:55:43 +00008832 pos, val);
8833}
8834
8835static int isl_set_plain_has_fixed_var(__isl_keep isl_set *set, unsigned pos,
8836 isl_int *val)
8837{
Tobias Grosser06e15922016-11-16 11:06:47 +00008838 return isl_map_plain_has_fixed_var(set_to_map(set), pos, val);
Tobias Grosser52a25232015-02-04 20:55:43 +00008839}
8840
8841int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
8842 enum isl_dim_type type, unsigned pos, isl_int *val)
8843{
8844 if (pos >= isl_basic_map_dim(bmap, type))
8845 return -1;
8846 return isl_basic_map_plain_has_fixed_var(bmap,
8847 isl_basic_map_offset(bmap, type) - 1 + pos, val);
8848}
8849
8850/* If "bmap" obviously lies on a hyperplane where the given dimension
8851 * has a fixed value, then return that value.
8852 * Otherwise return NaN.
8853 */
8854__isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
8855 __isl_keep isl_basic_map *bmap,
8856 enum isl_dim_type type, unsigned pos)
8857{
8858 isl_ctx *ctx;
8859 isl_val *v;
8860 int fixed;
8861
8862 if (!bmap)
8863 return NULL;
8864 ctx = isl_basic_map_get_ctx(bmap);
8865 v = isl_val_alloc(ctx);
8866 if (!v)
8867 return NULL;
8868 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
8869 if (fixed < 0)
8870 return isl_val_free(v);
8871 if (fixed) {
8872 isl_int_set_si(v->d, 1);
8873 return v;
8874 }
8875 isl_val_free(v);
8876 return isl_val_nan(ctx);
8877}
8878
8879int isl_map_plain_is_fixed(__isl_keep isl_map *map,
8880 enum isl_dim_type type, unsigned pos, isl_int *val)
8881{
8882 if (pos >= isl_map_dim(map, type))
8883 return -1;
8884 return isl_map_plain_has_fixed_var(map,
8885 map_offset(map, type) - 1 + pos, val);
8886}
8887
8888/* If "map" obviously lies on a hyperplane where the given dimension
8889 * has a fixed value, then return that value.
8890 * Otherwise return NaN.
8891 */
8892__isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
8893 enum isl_dim_type type, unsigned pos)
8894{
8895 isl_ctx *ctx;
8896 isl_val *v;
8897 int fixed;
8898
8899 if (!map)
8900 return NULL;
8901 ctx = isl_map_get_ctx(map);
8902 v = isl_val_alloc(ctx);
8903 if (!v)
8904 return NULL;
8905 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
8906 if (fixed < 0)
8907 return isl_val_free(v);
8908 if (fixed) {
8909 isl_int_set_si(v->d, 1);
8910 return v;
8911 }
8912 isl_val_free(v);
8913 return isl_val_nan(ctx);
8914}
8915
8916/* If "set" obviously lies on a hyperplane where the given dimension
8917 * has a fixed value, then return that value.
8918 * Otherwise return NaN.
8919 */
8920__isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
8921 enum isl_dim_type type, unsigned pos)
8922{
8923 return isl_map_plain_get_val_if_fixed(set, type, pos);
8924}
8925
8926int isl_set_plain_is_fixed(__isl_keep isl_set *set,
8927 enum isl_dim_type type, unsigned pos, isl_int *val)
8928{
8929 return isl_map_plain_is_fixed(set, type, pos, val);
8930}
8931
Tobias Grosser52a25232015-02-04 20:55:43 +00008932/* Check if dimension dim has fixed value and if so and if val is not NULL,
8933 * then return this fixed value in *val.
8934 */
8935int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
8936 unsigned dim, isl_int *val)
8937{
8938 return isl_basic_set_plain_has_fixed_var(bset,
8939 isl_basic_set_n_param(bset) + dim, val);
8940}
8941
8942/* Check if dimension dim has fixed value and if so and if val is not NULL,
8943 * then return this fixed value in *val.
8944 */
8945int isl_set_plain_dim_is_fixed(__isl_keep isl_set *set,
8946 unsigned dim, isl_int *val)
8947{
8948 return isl_set_plain_has_fixed_var(set, isl_set_n_param(set) + dim, val);
8949}
8950
Tobias Grosser52a25232015-02-04 20:55:43 +00008951/* Check if input variable in has fixed value and if so and if val is not NULL,
8952 * then return this fixed value in *val.
8953 */
8954int isl_map_plain_input_is_fixed(__isl_keep isl_map *map,
8955 unsigned in, isl_int *val)
8956{
8957 return isl_map_plain_has_fixed_var(map, isl_map_n_param(map) + in, val);
8958}
8959
8960/* Check if dimension dim has an (obvious) fixed lower bound and if so
8961 * and if val is not NULL, then return this lower bound in *val.
8962 */
8963int isl_basic_set_plain_dim_has_fixed_lower_bound(
8964 __isl_keep isl_basic_set *bset, unsigned dim, isl_int *val)
8965{
8966 int i, i_eq = -1, i_ineq = -1;
8967 isl_int *c;
8968 unsigned total;
8969 unsigned nparam;
8970
8971 if (!bset)
8972 return -1;
8973 total = isl_basic_set_total_dim(bset);
8974 nparam = isl_basic_set_n_param(bset);
8975 for (i = 0; i < bset->n_eq; ++i) {
8976 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
8977 continue;
8978 if (i_eq != -1)
8979 return 0;
8980 i_eq = i;
8981 }
8982 for (i = 0; i < bset->n_ineq; ++i) {
8983 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
8984 continue;
8985 if (i_eq != -1 || i_ineq != -1)
8986 return 0;
8987 i_ineq = i;
8988 }
8989 if (i_eq == -1 && i_ineq == -1)
8990 return 0;
8991 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
8992 /* The coefficient should always be one due to normalization. */
8993 if (!isl_int_is_one(c[1+nparam+dim]))
8994 return 0;
8995 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
8996 return 0;
8997 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
8998 total - nparam - dim - 1) != -1)
8999 return 0;
9000 if (val)
9001 isl_int_neg(*val, c[0]);
9002 return 1;
9003}
9004
9005int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set *set,
9006 unsigned dim, isl_int *val)
9007{
9008 int i;
9009 isl_int v;
9010 isl_int tmp;
9011 int fixed;
9012
9013 if (!set)
9014 return -1;
9015 if (set->n == 0)
9016 return 0;
9017 if (set->n == 1)
9018 return isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
9019 dim, val);
9020 isl_int_init(v);
9021 isl_int_init(tmp);
9022 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
9023 dim, &v);
9024 for (i = 1; fixed == 1 && i < set->n; ++i) {
9025 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[i],
9026 dim, &tmp);
9027 if (fixed == 1 && isl_int_ne(tmp, v))
9028 fixed = 0;
9029 }
9030 if (val)
9031 isl_int_set(*val, v);
9032 isl_int_clear(tmp);
9033 isl_int_clear(v);
9034 return fixed;
9035}
9036
Michael Kruse959a8dc2016-01-15 15:54:45 +00009037/* Return -1 if the constraint "c1" should be sorted before "c2"
9038 * and 1 if it should be sorted after "c2".
9039 * Return 0 if the two constraints are the same (up to the constant term).
9040 *
9041 * In particular, if a constraint involves later variables than another
9042 * then it is sorted after this other constraint.
9043 * uset_gist depends on constraints without existentially quantified
Tobias Grosser52a25232015-02-04 20:55:43 +00009044 * variables sorting first.
Michael Kruse959a8dc2016-01-15 15:54:45 +00009045 *
9046 * For constraints that have the same latest variable, those
9047 * with the same coefficient for this latest variable (first in absolute value
9048 * and then in actual value) are grouped together.
9049 * This is useful for detecting pairs of constraints that can
9050 * be chained in their printed representation.
9051 *
9052 * Finally, within a group, constraints are sorted according to
9053 * their coefficients (excluding the constant term).
Tobias Grosser52a25232015-02-04 20:55:43 +00009054 */
9055static int sort_constraint_cmp(const void *p1, const void *p2, void *arg)
9056{
9057 isl_int **c1 = (isl_int **) p1;
9058 isl_int **c2 = (isl_int **) p2;
9059 int l1, l2;
9060 unsigned size = *(unsigned *) arg;
Michael Kruse959a8dc2016-01-15 15:54:45 +00009061 int cmp;
Tobias Grosser52a25232015-02-04 20:55:43 +00009062
9063 l1 = isl_seq_last_non_zero(*c1 + 1, size);
9064 l2 = isl_seq_last_non_zero(*c2 + 1, size);
9065
9066 if (l1 != l2)
9067 return l1 - l2;
9068
Michael Kruse959a8dc2016-01-15 15:54:45 +00009069 cmp = isl_int_abs_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9070 if (cmp != 0)
9071 return cmp;
9072 cmp = isl_int_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9073 if (cmp != 0)
9074 return -cmp;
9075
Tobias Grosser52a25232015-02-04 20:55:43 +00009076 return isl_seq_cmp(*c1 + 1, *c2 + 1, size);
9077}
9078
Michael Kruse959a8dc2016-01-15 15:54:45 +00009079/* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
9080 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
9081 * and 0 if the two constraints are the same (up to the constant term).
9082 */
9083int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map *bmap,
9084 isl_int *c1, isl_int *c2)
9085{
9086 unsigned total;
9087
9088 if (!bmap)
9089 return -2;
9090 total = isl_basic_map_total_dim(bmap);
9091 return sort_constraint_cmp(&c1, &c2, &total);
9092}
9093
9094__isl_give isl_basic_map *isl_basic_map_sort_constraints(
9095 __isl_take isl_basic_map *bmap)
Tobias Grosser52a25232015-02-04 20:55:43 +00009096{
9097 unsigned total;
9098
9099 if (!bmap)
9100 return NULL;
9101 if (bmap->n_ineq == 0)
9102 return bmap;
9103 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
9104 return bmap;
9105 total = isl_basic_map_total_dim(bmap);
9106 if (isl_sort(bmap->ineq, bmap->n_ineq, sizeof(isl_int *),
9107 &sort_constraint_cmp, &total) < 0)
9108 return isl_basic_map_free(bmap);
9109 return bmap;
9110}
9111
9112__isl_give isl_basic_set *isl_basic_set_sort_constraints(
9113 __isl_take isl_basic_set *bset)
9114{
Tobias Grosser06e15922016-11-16 11:06:47 +00009115 isl_basic_map *bmap = bset_to_bmap(bset);
9116 return bset_from_bmap(isl_basic_map_sort_constraints(bmap));
Tobias Grosser52a25232015-02-04 20:55:43 +00009117}
9118
9119struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
9120{
9121 if (!bmap)
9122 return NULL;
9123 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
9124 return bmap;
9125 bmap = isl_basic_map_remove_redundancies(bmap);
9126 bmap = isl_basic_map_sort_constraints(bmap);
9127 if (bmap)
9128 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
9129 return bmap;
9130}
9131
9132struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
9133{
Tobias Grosser06e15922016-11-16 11:06:47 +00009134 return bset_from_bmap(isl_basic_map_normalize(bset_to_bmap(bset)));
Tobias Grosser52a25232015-02-04 20:55:43 +00009135}
9136
9137int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map *bmap1,
9138 const __isl_keep isl_basic_map *bmap2)
9139{
9140 int i, cmp;
9141 unsigned total;
9142
Tobias Grosser3e6070e2015-05-09 09:37:30 +00009143 if (!bmap1 || !bmap2)
9144 return -1;
9145
Tobias Grosser52a25232015-02-04 20:55:43 +00009146 if (bmap1 == bmap2)
9147 return 0;
9148 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
9149 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
9150 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
9151 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
9152 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
9153 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
9154 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
9155 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
9156 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
9157 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
9158 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9159 return 0;
9160 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
9161 return 1;
9162 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9163 return -1;
9164 if (bmap1->n_eq != bmap2->n_eq)
9165 return bmap1->n_eq - bmap2->n_eq;
9166 if (bmap1->n_ineq != bmap2->n_ineq)
9167 return bmap1->n_ineq - bmap2->n_ineq;
9168 if (bmap1->n_div != bmap2->n_div)
9169 return bmap1->n_div - bmap2->n_div;
9170 total = isl_basic_map_total_dim(bmap1);
9171 for (i = 0; i < bmap1->n_eq; ++i) {
9172 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
9173 if (cmp)
9174 return cmp;
9175 }
9176 for (i = 0; i < bmap1->n_ineq; ++i) {
9177 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
9178 if (cmp)
9179 return cmp;
9180 }
9181 for (i = 0; i < bmap1->n_div; ++i) {
9182 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
9183 if (cmp)
9184 return cmp;
9185 }
9186 return 0;
9187}
9188
9189int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set *bset1,
9190 const __isl_keep isl_basic_set *bset2)
9191{
9192 return isl_basic_map_plain_cmp(bset1, bset2);
9193}
9194
9195int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9196{
9197 int i, cmp;
9198
9199 if (set1 == set2)
9200 return 0;
9201 if (set1->n != set2->n)
9202 return set1->n - set2->n;
9203
9204 for (i = 0; i < set1->n; ++i) {
9205 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
9206 if (cmp)
9207 return cmp;
9208 }
9209
9210 return 0;
9211}
9212
Tobias Grosserb2f39922015-05-28 13:32:11 +00009213isl_bool isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
Tobias Grosser52a25232015-02-04 20:55:43 +00009214 __isl_keep isl_basic_map *bmap2)
9215{
Tobias Grossere0f8d592015-05-13 13:10:13 +00009216 if (!bmap1 || !bmap2)
Tobias Grosserb2f39922015-05-28 13:32:11 +00009217 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00009218 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
9219}
9220
Tobias Grosserb2f39922015-05-28 13:32:11 +00009221isl_bool isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
Tobias Grosser52a25232015-02-04 20:55:43 +00009222 __isl_keep isl_basic_set *bset2)
9223{
Tobias Grosser06e15922016-11-16 11:06:47 +00009224 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1),
9225 bset_to_bmap(bset2));
Tobias Grosser52a25232015-02-04 20:55:43 +00009226}
9227
9228static int qsort_bmap_cmp(const void *p1, const void *p2)
9229{
9230 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
9231 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
9232
9233 return isl_basic_map_plain_cmp(bmap1, bmap2);
9234}
9235
9236/* Sort the basic maps of "map" and remove duplicate basic maps.
9237 *
9238 * While removing basic maps, we make sure that the basic maps remain
9239 * sorted because isl_map_normalize expects the basic maps of the result
9240 * to be sorted.
9241 */
9242static __isl_give isl_map *sort_and_remove_duplicates(__isl_take isl_map *map)
9243{
9244 int i, j;
9245
9246 map = isl_map_remove_empty_parts(map);
9247 if (!map)
9248 return NULL;
9249 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
9250 for (i = map->n - 1; i >= 1; --i) {
9251 if (!isl_basic_map_plain_is_equal(map->p[i - 1], map->p[i]))
9252 continue;
9253 isl_basic_map_free(map->p[i-1]);
9254 for (j = i; j < map->n; ++j)
9255 map->p[j - 1] = map->p[j];
9256 map->n--;
9257 }
9258
9259 return map;
9260}
9261
9262/* Remove obvious duplicates among the basic maps of "map".
9263 *
9264 * Unlike isl_map_normalize, this function does not remove redundant
9265 * constraints and only removes duplicates that have exactly the same
9266 * constraints in the input. It does sort the constraints and
9267 * the basic maps to ease the detection of duplicates.
9268 *
9269 * If "map" has already been normalized or if the basic maps are
9270 * disjoint, then there can be no duplicates.
9271 */
9272__isl_give isl_map *isl_map_remove_obvious_duplicates(__isl_take isl_map *map)
9273{
9274 int i;
9275 isl_basic_map *bmap;
9276
9277 if (!map)
9278 return NULL;
9279 if (map->n <= 1)
9280 return map;
9281 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED | ISL_MAP_DISJOINT))
9282 return map;
9283 for (i = 0; i < map->n; ++i) {
9284 bmap = isl_basic_map_copy(map->p[i]);
9285 bmap = isl_basic_map_sort_constraints(bmap);
9286 if (!bmap)
9287 return isl_map_free(map);
9288 isl_basic_map_free(map->p[i]);
9289 map->p[i] = bmap;
9290 }
9291
9292 map = sort_and_remove_duplicates(map);
9293 return map;
9294}
9295
9296/* We normalize in place, but if anything goes wrong we need
9297 * to return NULL, so we need to make sure we don't change the
9298 * meaning of any possible other copies of map.
9299 */
9300__isl_give isl_map *isl_map_normalize(__isl_take isl_map *map)
9301{
9302 int i;
9303 struct isl_basic_map *bmap;
9304
9305 if (!map)
9306 return NULL;
9307 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
9308 return map;
9309 for (i = 0; i < map->n; ++i) {
9310 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
9311 if (!bmap)
9312 goto error;
9313 isl_basic_map_free(map->p[i]);
9314 map->p[i] = bmap;
9315 }
9316
9317 map = sort_and_remove_duplicates(map);
9318 if (map)
9319 ISL_F_SET(map, ISL_MAP_NORMALIZED);
9320 return map;
9321error:
9322 isl_map_free(map);
9323 return NULL;
9324}
9325
9326struct isl_set *isl_set_normalize(struct isl_set *set)
9327{
Tobias Grosser06e15922016-11-16 11:06:47 +00009328 return set_from_map(isl_map_normalize(set_to_map(set)));
Tobias Grosser52a25232015-02-04 20:55:43 +00009329}
9330
Tobias Grosserb2f39922015-05-28 13:32:11 +00009331isl_bool isl_map_plain_is_equal(__isl_keep isl_map *map1,
9332 __isl_keep isl_map *map2)
Tobias Grosser52a25232015-02-04 20:55:43 +00009333{
9334 int i;
Tobias Grosserb2f39922015-05-28 13:32:11 +00009335 isl_bool equal;
Tobias Grosser52a25232015-02-04 20:55:43 +00009336
9337 if (!map1 || !map2)
Tobias Grosserb2f39922015-05-28 13:32:11 +00009338 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00009339
9340 if (map1 == map2)
Tobias Grosserb2f39922015-05-28 13:32:11 +00009341 return isl_bool_true;
Tobias Grosser52a25232015-02-04 20:55:43 +00009342 if (!isl_space_is_equal(map1->dim, map2->dim))
Tobias Grosserb2f39922015-05-28 13:32:11 +00009343 return isl_bool_false;
Tobias Grosser52a25232015-02-04 20:55:43 +00009344
9345 map1 = isl_map_copy(map1);
9346 map2 = isl_map_copy(map2);
9347 map1 = isl_map_normalize(map1);
9348 map2 = isl_map_normalize(map2);
9349 if (!map1 || !map2)
9350 goto error;
9351 equal = map1->n == map2->n;
9352 for (i = 0; equal && i < map1->n; ++i) {
9353 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
9354 if (equal < 0)
9355 goto error;
9356 }
9357 isl_map_free(map1);
9358 isl_map_free(map2);
9359 return equal;
9360error:
9361 isl_map_free(map1);
9362 isl_map_free(map2);
Tobias Grosserb2f39922015-05-28 13:32:11 +00009363 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00009364}
9365
Tobias Grosserb2f39922015-05-28 13:32:11 +00009366isl_bool isl_set_plain_is_equal(__isl_keep isl_set *set1,
9367 __isl_keep isl_set *set2)
Tobias Grosser52a25232015-02-04 20:55:43 +00009368{
Tobias Grosser06e15922016-11-16 11:06:47 +00009369 return isl_map_plain_is_equal(set_to_map(set1), set_to_map(set2));
Tobias Grosser52a25232015-02-04 20:55:43 +00009370}
9371
Tobias Grosser52a25232015-02-04 20:55:43 +00009372/* Return an interval that ranges from min to max (inclusive)
9373 */
9374struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
9375 isl_int min, isl_int max)
9376{
9377 int k;
9378 struct isl_basic_set *bset = NULL;
9379
9380 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
9381 if (!bset)
9382 goto error;
9383
9384 k = isl_basic_set_alloc_inequality(bset);
9385 if (k < 0)
9386 goto error;
9387 isl_int_set_si(bset->ineq[k][1], 1);
9388 isl_int_neg(bset->ineq[k][0], min);
9389
9390 k = isl_basic_set_alloc_inequality(bset);
9391 if (k < 0)
9392 goto error;
9393 isl_int_set_si(bset->ineq[k][1], -1);
9394 isl_int_set(bset->ineq[k][0], max);
9395
9396 return bset;
9397error:
9398 isl_basic_set_free(bset);
9399 return NULL;
9400}
9401
Tobias Grosser1fa7b972015-02-16 19:33:40 +00009402/* Return the basic maps in "map" as a list.
Tobias Grosser52a25232015-02-04 20:55:43 +00009403 */
Tobias Grosser1fa7b972015-02-16 19:33:40 +00009404__isl_give isl_basic_map_list *isl_map_get_basic_map_list(
9405 __isl_keep isl_map *map)
Tobias Grosser52a25232015-02-04 20:55:43 +00009406{
9407 int i;
9408 isl_ctx *ctx;
Tobias Grosser1fa7b972015-02-16 19:33:40 +00009409 isl_basic_map_list *list;
Tobias Grosser52a25232015-02-04 20:55:43 +00009410
Tobias Grosser1fa7b972015-02-16 19:33:40 +00009411 if (!map)
Tobias Grosser52a25232015-02-04 20:55:43 +00009412 return NULL;
Tobias Grosser1fa7b972015-02-16 19:33:40 +00009413 ctx = isl_map_get_ctx(map);
9414 list = isl_basic_map_list_alloc(ctx, map->n);
Tobias Grosser52a25232015-02-04 20:55:43 +00009415
Tobias Grosser1fa7b972015-02-16 19:33:40 +00009416 for (i = 0; i < map->n; ++i) {
9417 isl_basic_map *bmap;
Tobias Grosser52a25232015-02-04 20:55:43 +00009418
Tobias Grosser1fa7b972015-02-16 19:33:40 +00009419 bmap = isl_basic_map_copy(map->p[i]);
9420 list = isl_basic_map_list_add(list, bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +00009421 }
9422
9423 return list;
9424}
9425
9426/* Return the intersection of the elements in the non-empty list "list".
9427 * All elements are assumed to live in the same space.
9428 */
Tobias Grosser1fa7b972015-02-16 19:33:40 +00009429__isl_give isl_basic_map *isl_basic_map_list_intersect(
9430 __isl_take isl_basic_map_list *list)
Tobias Grosser52a25232015-02-04 20:55:43 +00009431{
9432 int i, n;
Tobias Grosser1fa7b972015-02-16 19:33:40 +00009433 isl_basic_map *bmap;
Tobias Grosser52a25232015-02-04 20:55:43 +00009434
9435 if (!list)
9436 return NULL;
Tobias Grosser1fa7b972015-02-16 19:33:40 +00009437 n = isl_basic_map_list_n_basic_map(list);
Tobias Grosser52a25232015-02-04 20:55:43 +00009438 if (n < 1)
Tobias Grosser1fa7b972015-02-16 19:33:40 +00009439 isl_die(isl_basic_map_list_get_ctx(list), isl_error_invalid,
Tobias Grosser52a25232015-02-04 20:55:43 +00009440 "expecting non-empty list", goto error);
Tobias Grosser52a25232015-02-04 20:55:43 +00009441
Tobias Grosser1fa7b972015-02-16 19:33:40 +00009442 bmap = isl_basic_map_list_get_basic_map(list, 0);
9443 for (i = 1; i < n; ++i) {
9444 isl_basic_map *bmap_i;
9445
9446 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9447 bmap = isl_basic_map_intersect(bmap, bmap_i);
Tobias Grosser52a25232015-02-04 20:55:43 +00009448 }
9449
Tobias Grosser1fa7b972015-02-16 19:33:40 +00009450 isl_basic_map_list_free(list);
9451 return bmap;
Tobias Grosser52a25232015-02-04 20:55:43 +00009452error:
Tobias Grosser1fa7b972015-02-16 19:33:40 +00009453 isl_basic_map_list_free(list);
Tobias Grosser52a25232015-02-04 20:55:43 +00009454 return NULL;
9455}
9456
Tobias Grosser1fa7b972015-02-16 19:33:40 +00009457/* Return the intersection of the elements in the non-empty list "list".
9458 * All elements are assumed to live in the same space.
9459 */
9460__isl_give isl_basic_set *isl_basic_set_list_intersect(
9461 __isl_take isl_basic_set_list *list)
9462{
9463 return isl_basic_map_list_intersect(list);
9464}
9465
Tobias Grossereab0943e2016-11-22 21:31:59 +00009466/* Return the union of the elements of "list".
9467 * The list is required to have at least one element.
9468 */
9469__isl_give isl_set *isl_basic_set_list_union(
9470 __isl_take isl_basic_set_list *list)
9471{
9472 int i, n;
9473 isl_space *space;
9474 isl_basic_set *bset;
9475 isl_set *set;
9476
9477 if (!list)
9478 return NULL;
9479 n = isl_basic_set_list_n_basic_set(list);
9480 if (n < 1)
9481 isl_die(isl_basic_set_list_get_ctx(list), isl_error_invalid,
9482 "expecting non-empty list", goto error);
9483
9484 bset = isl_basic_set_list_get_basic_set(list, 0);
9485 space = isl_basic_set_get_space(bset);
9486 isl_basic_set_free(bset);
9487
9488 set = isl_set_alloc_space(space, n, 0);
9489 for (i = 0; i < n; ++i) {
9490 bset = isl_basic_set_list_get_basic_set(list, i);
9491 set = isl_set_add_basic_set(set, bset);
9492 }
9493
9494 isl_basic_set_list_free(list);
9495 return set;
9496error:
9497 isl_basic_set_list_free(list);
9498 return NULL;
9499}
9500
Tobias Grossera67ac972016-02-26 11:35:12 +00009501/* Return the union of the elements in the non-empty list "list".
9502 * All elements are assumed to live in the same space.
9503 */
9504__isl_give isl_set *isl_set_list_union(__isl_take isl_set_list *list)
9505{
9506 int i, n;
9507 isl_set *set;
9508
9509 if (!list)
9510 return NULL;
9511 n = isl_set_list_n_set(list);
9512 if (n < 1)
9513 isl_die(isl_set_list_get_ctx(list), isl_error_invalid,
9514 "expecting non-empty list", goto error);
9515
9516 set = isl_set_list_get_set(list, 0);
9517 for (i = 1; i < n; ++i) {
9518 isl_set *set_i;
9519
9520 set_i = isl_set_list_get_set(list, i);
9521 set = isl_set_union(set, set_i);
9522 }
9523
9524 isl_set_list_free(list);
9525 return set;
9526error:
9527 isl_set_list_free(list);
9528 return NULL;
9529}
9530
Tobias Grosser52a25232015-02-04 20:55:43 +00009531/* Return the Cartesian product of the basic sets in list (in the given order).
9532 */
9533__isl_give isl_basic_set *isl_basic_set_list_product(
9534 __isl_take struct isl_basic_set_list *list)
9535{
9536 int i;
9537 unsigned dim;
9538 unsigned nparam;
9539 unsigned extra;
9540 unsigned n_eq;
9541 unsigned n_ineq;
9542 struct isl_basic_set *product = NULL;
9543
9544 if (!list)
9545 goto error;
9546 isl_assert(list->ctx, list->n > 0, goto error);
9547 isl_assert(list->ctx, list->p[0], goto error);
9548 nparam = isl_basic_set_n_param(list->p[0]);
9549 dim = isl_basic_set_n_dim(list->p[0]);
9550 extra = list->p[0]->n_div;
9551 n_eq = list->p[0]->n_eq;
9552 n_ineq = list->p[0]->n_ineq;
9553 for (i = 1; i < list->n; ++i) {
9554 isl_assert(list->ctx, list->p[i], goto error);
9555 isl_assert(list->ctx,
9556 nparam == isl_basic_set_n_param(list->p[i]), goto error);
9557 dim += isl_basic_set_n_dim(list->p[i]);
9558 extra += list->p[i]->n_div;
9559 n_eq += list->p[i]->n_eq;
9560 n_ineq += list->p[i]->n_ineq;
9561 }
9562 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
9563 n_eq, n_ineq);
9564 if (!product)
9565 goto error;
9566 dim = 0;
9567 for (i = 0; i < list->n; ++i) {
9568 isl_basic_set_add_constraints(product,
9569 isl_basic_set_copy(list->p[i]), dim);
9570 dim += isl_basic_set_n_dim(list->p[i]);
9571 }
9572 isl_basic_set_list_free(list);
9573 return product;
9574error:
9575 isl_basic_set_free(product);
9576 isl_basic_set_list_free(list);
9577 return NULL;
9578}
9579
9580struct isl_basic_map *isl_basic_map_product(
9581 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
9582{
9583 isl_space *dim_result = NULL;
9584 struct isl_basic_map *bmap;
9585 unsigned in1, in2, out1, out2, nparam, total, pos;
9586 struct isl_dim_map *dim_map1, *dim_map2;
9587
9588 if (!bmap1 || !bmap2)
9589 goto error;
9590
9591 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
9592 bmap2->dim, isl_dim_param), goto error);
9593 dim_result = isl_space_product(isl_space_copy(bmap1->dim),
9594 isl_space_copy(bmap2->dim));
9595
9596 in1 = isl_basic_map_n_in(bmap1);
9597 in2 = isl_basic_map_n_in(bmap2);
9598 out1 = isl_basic_map_n_out(bmap1);
9599 out2 = isl_basic_map_n_out(bmap2);
9600 nparam = isl_basic_map_n_param(bmap1);
9601
9602 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
9603 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9604 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9605 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9606 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9607 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9608 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9609 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9610 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9611 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9612 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9613
9614 bmap = isl_basic_map_alloc_space(dim_result,
9615 bmap1->n_div + bmap2->n_div,
9616 bmap1->n_eq + bmap2->n_eq,
9617 bmap1->n_ineq + bmap2->n_ineq);
9618 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9619 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9620 bmap = isl_basic_map_simplify(bmap);
9621 return isl_basic_map_finalize(bmap);
9622error:
9623 isl_basic_map_free(bmap1);
9624 isl_basic_map_free(bmap2);
9625 return NULL;
9626}
9627
9628__isl_give isl_basic_map *isl_basic_map_flat_product(
9629 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9630{
9631 isl_basic_map *prod;
9632
9633 prod = isl_basic_map_product(bmap1, bmap2);
9634 prod = isl_basic_map_flatten(prod);
9635 return prod;
9636}
9637
9638__isl_give isl_basic_set *isl_basic_set_flat_product(
9639 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
9640{
9641 return isl_basic_map_flat_range_product(bset1, bset2);
9642}
9643
9644__isl_give isl_basic_map *isl_basic_map_domain_product(
9645 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9646{
9647 isl_space *space_result = NULL;
9648 isl_basic_map *bmap;
9649 unsigned in1, in2, out, nparam, total, pos;
9650 struct isl_dim_map *dim_map1, *dim_map2;
9651
9652 if (!bmap1 || !bmap2)
9653 goto error;
9654
9655 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
9656 isl_space_copy(bmap2->dim));
9657
9658 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9659 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9660 out = isl_basic_map_dim(bmap1, isl_dim_out);
9661 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9662
9663 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
9664 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9665 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9666 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9667 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9668 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9669 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9670 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9671 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
9672 isl_dim_map_div(dim_map1, bmap1, pos += out);
9673 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9674
9675 bmap = isl_basic_map_alloc_space(space_result,
9676 bmap1->n_div + bmap2->n_div,
9677 bmap1->n_eq + bmap2->n_eq,
9678 bmap1->n_ineq + bmap2->n_ineq);
9679 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9680 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9681 bmap = isl_basic_map_simplify(bmap);
9682 return isl_basic_map_finalize(bmap);
9683error:
9684 isl_basic_map_free(bmap1);
9685 isl_basic_map_free(bmap2);
9686 return NULL;
9687}
9688
9689__isl_give isl_basic_map *isl_basic_map_range_product(
9690 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9691{
Tobias Grosser5652c9c2016-10-01 19:46:51 +00009692 int rational;
Tobias Grosser52a25232015-02-04 20:55:43 +00009693 isl_space *dim_result = NULL;
9694 isl_basic_map *bmap;
9695 unsigned in, out1, out2, nparam, total, pos;
9696 struct isl_dim_map *dim_map1, *dim_map2;
9697
Tobias Grosser5652c9c2016-10-01 19:46:51 +00009698 rational = isl_basic_map_is_rational(bmap1);
9699 if (rational >= 0 && rational)
9700 rational = isl_basic_map_is_rational(bmap2);
9701 if (!bmap1 || !bmap2 || rational < 0)
Tobias Grosser52a25232015-02-04 20:55:43 +00009702 goto error;
9703
9704 if (!isl_space_match(bmap1->dim, isl_dim_param,
9705 bmap2->dim, isl_dim_param))
9706 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
9707 "parameters don't match", goto error);
9708
9709 dim_result = isl_space_range_product(isl_space_copy(bmap1->dim),
9710 isl_space_copy(bmap2->dim));
9711
9712 in = isl_basic_map_dim(bmap1, isl_dim_in);
9713 out1 = isl_basic_map_n_out(bmap1);
9714 out2 = isl_basic_map_n_out(bmap2);
9715 nparam = isl_basic_map_n_param(bmap1);
9716
9717 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
9718 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9719 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9720 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9721 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9722 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9723 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
9724 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
9725 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9726 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9727 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9728
9729 bmap = isl_basic_map_alloc_space(dim_result,
9730 bmap1->n_div + bmap2->n_div,
9731 bmap1->n_eq + bmap2->n_eq,
9732 bmap1->n_ineq + bmap2->n_ineq);
9733 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9734 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
Tobias Grosser5652c9c2016-10-01 19:46:51 +00009735 if (rational)
9736 bmap = isl_basic_map_set_rational(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +00009737 bmap = isl_basic_map_simplify(bmap);
9738 return isl_basic_map_finalize(bmap);
9739error:
9740 isl_basic_map_free(bmap1);
9741 isl_basic_map_free(bmap2);
9742 return NULL;
9743}
9744
9745__isl_give isl_basic_map *isl_basic_map_flat_range_product(
9746 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9747{
9748 isl_basic_map *prod;
9749
9750 prod = isl_basic_map_range_product(bmap1, bmap2);
9751 prod = isl_basic_map_flatten_range(prod);
9752 return prod;
9753}
9754
9755/* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
9756 * and collect the results.
9757 * The result live in the space obtained by calling "space_product"
9758 * on the spaces of "map1" and "map2".
9759 * If "remove_duplicates" is set then the result may contain duplicates
9760 * (even if the inputs do not) and so we try and remove the obvious
9761 * duplicates.
9762 */
9763static __isl_give isl_map *map_product(__isl_take isl_map *map1,
9764 __isl_take isl_map *map2,
9765 __isl_give isl_space *(*space_product)(__isl_take isl_space *left,
9766 __isl_take isl_space *right),
9767 __isl_give isl_basic_map *(*basic_map_product)(
9768 __isl_take isl_basic_map *left,
9769 __isl_take isl_basic_map *right),
9770 int remove_duplicates)
9771{
9772 unsigned flags = 0;
9773 struct isl_map *result;
9774 int i, j;
9775
9776 if (!map1 || !map2)
9777 goto error;
9778
9779 isl_assert(map1->ctx, isl_space_match(map1->dim, isl_dim_param,
9780 map2->dim, isl_dim_param), goto error);
9781
9782 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
9783 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
9784 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
9785
9786 result = isl_map_alloc_space(space_product(isl_space_copy(map1->dim),
9787 isl_space_copy(map2->dim)),
9788 map1->n * map2->n, flags);
9789 if (!result)
9790 goto error;
9791 for (i = 0; i < map1->n; ++i)
9792 for (j = 0; j < map2->n; ++j) {
9793 struct isl_basic_map *part;
9794 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
9795 isl_basic_map_copy(map2->p[j]));
9796 if (isl_basic_map_is_empty(part))
9797 isl_basic_map_free(part);
9798 else
9799 result = isl_map_add_basic_map(result, part);
9800 if (!result)
9801 goto error;
9802 }
9803 if (remove_duplicates)
9804 result = isl_map_remove_obvious_duplicates(result);
9805 isl_map_free(map1);
9806 isl_map_free(map2);
9807 return result;
9808error:
9809 isl_map_free(map1);
9810 isl_map_free(map2);
9811 return NULL;
9812}
9813
9814/* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
9815 */
9816static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
9817 __isl_take isl_map *map2)
9818{
9819 return map_product(map1, map2, &isl_space_product,
9820 &isl_basic_map_product, 0);
9821}
9822
9823__isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
9824 __isl_take isl_map *map2)
9825{
9826 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
9827}
9828
9829/* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
9830 */
9831__isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
9832 __isl_take isl_map *map2)
9833{
9834 isl_map *prod;
9835
9836 prod = isl_map_product(map1, map2);
9837 prod = isl_map_flatten(prod);
9838 return prod;
9839}
9840
9841/* Given two set A and B, construct its Cartesian product A x B.
9842 */
9843struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
9844{
9845 return isl_map_range_product(set1, set2);
9846}
9847
9848__isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
9849 __isl_take isl_set *set2)
9850{
9851 return isl_map_flat_range_product(set1, set2);
9852}
9853
9854/* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
9855 */
9856static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
9857 __isl_take isl_map *map2)
9858{
9859 return map_product(map1, map2, &isl_space_domain_product,
9860 &isl_basic_map_domain_product, 1);
9861}
9862
9863/* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
9864 */
9865static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
9866 __isl_take isl_map *map2)
9867{
9868 return map_product(map1, map2, &isl_space_range_product,
9869 &isl_basic_map_range_product, 1);
9870}
9871
9872__isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
9873 __isl_take isl_map *map2)
9874{
9875 return isl_map_align_params_map_map_and(map1, map2,
9876 &map_domain_product_aligned);
9877}
9878
9879__isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
9880 __isl_take isl_map *map2)
9881{
9882 return isl_map_align_params_map_map_and(map1, map2,
9883 &map_range_product_aligned);
9884}
9885
9886/* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
9887 */
9888__isl_give isl_map *isl_map_factor_domain(__isl_take isl_map *map)
9889{
9890 isl_space *space;
9891 int total1, keep1, total2, keep2;
9892
9893 if (!map)
9894 return NULL;
9895 if (!isl_space_domain_is_wrapping(map->dim) ||
9896 !isl_space_range_is_wrapping(map->dim))
9897 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9898 "not a product", return isl_map_free(map));
9899
9900 space = isl_map_get_space(map);
9901 total1 = isl_space_dim(space, isl_dim_in);
9902 total2 = isl_space_dim(space, isl_dim_out);
9903 space = isl_space_factor_domain(space);
9904 keep1 = isl_space_dim(space, isl_dim_in);
9905 keep2 = isl_space_dim(space, isl_dim_out);
9906 map = isl_map_project_out(map, isl_dim_in, keep1, total1 - keep1);
9907 map = isl_map_project_out(map, isl_dim_out, keep2, total2 - keep2);
9908 map = isl_map_reset_space(map, space);
9909
9910 return map;
9911}
9912
9913/* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
9914 */
9915__isl_give isl_map *isl_map_factor_range(__isl_take isl_map *map)
9916{
9917 isl_space *space;
9918 int total1, keep1, total2, keep2;
9919
9920 if (!map)
9921 return NULL;
9922 if (!isl_space_domain_is_wrapping(map->dim) ||
9923 !isl_space_range_is_wrapping(map->dim))
9924 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9925 "not a product", return isl_map_free(map));
9926
9927 space = isl_map_get_space(map);
9928 total1 = isl_space_dim(space, isl_dim_in);
9929 total2 = isl_space_dim(space, isl_dim_out);
9930 space = isl_space_factor_range(space);
9931 keep1 = isl_space_dim(space, isl_dim_in);
9932 keep2 = isl_space_dim(space, isl_dim_out);
9933 map = isl_map_project_out(map, isl_dim_in, 0, total1 - keep1);
9934 map = isl_map_project_out(map, isl_dim_out, 0, total2 - keep2);
9935 map = isl_map_reset_space(map, space);
9936
9937 return map;
9938}
9939
9940/* Given a map of the form [A -> B] -> C, return the map A -> C.
9941 */
9942__isl_give isl_map *isl_map_domain_factor_domain(__isl_take isl_map *map)
9943{
9944 isl_space *space;
9945 int total, keep;
9946
9947 if (!map)
9948 return NULL;
9949 if (!isl_space_domain_is_wrapping(map->dim))
9950 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9951 "domain is not a product", return isl_map_free(map));
9952
9953 space = isl_map_get_space(map);
9954 total = isl_space_dim(space, isl_dim_in);
9955 space = isl_space_domain_factor_domain(space);
9956 keep = isl_space_dim(space, isl_dim_in);
9957 map = isl_map_project_out(map, isl_dim_in, keep, total - keep);
9958 map = isl_map_reset_space(map, space);
9959
9960 return map;
9961}
9962
9963/* Given a map of the form [A -> B] -> C, return the map B -> C.
9964 */
9965__isl_give isl_map *isl_map_domain_factor_range(__isl_take isl_map *map)
9966{
9967 isl_space *space;
9968 int total, keep;
9969
9970 if (!map)
9971 return NULL;
9972 if (!isl_space_domain_is_wrapping(map->dim))
9973 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9974 "domain is not a product", return isl_map_free(map));
9975
9976 space = isl_map_get_space(map);
9977 total = isl_space_dim(space, isl_dim_in);
9978 space = isl_space_domain_factor_range(space);
9979 keep = isl_space_dim(space, isl_dim_in);
9980 map = isl_map_project_out(map, isl_dim_in, 0, total - keep);
9981 map = isl_map_reset_space(map, space);
9982
9983 return map;
9984}
9985
9986/* Given a map A -> [B -> C], extract the map A -> B.
9987 */
9988__isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
9989{
9990 isl_space *space;
9991 int total, keep;
9992
9993 if (!map)
9994 return NULL;
9995 if (!isl_space_range_is_wrapping(map->dim))
9996 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9997 "range is not a product", return isl_map_free(map));
9998
9999 space = isl_map_get_space(map);
10000 total = isl_space_dim(space, isl_dim_out);
10001 space = isl_space_range_factor_domain(space);
10002 keep = isl_space_dim(space, isl_dim_out);
10003 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
10004 map = isl_map_reset_space(map, space);
10005
10006 return map;
10007}
10008
10009/* Given a map A -> [B -> C], extract the map A -> C.
10010 */
10011__isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
10012{
10013 isl_space *space;
10014 int total, keep;
10015
10016 if (!map)
10017 return NULL;
10018 if (!isl_space_range_is_wrapping(map->dim))
10019 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10020 "range is not a product", return isl_map_free(map));
10021
10022 space = isl_map_get_space(map);
10023 total = isl_space_dim(space, isl_dim_out);
10024 space = isl_space_range_factor_range(space);
10025 keep = isl_space_dim(space, isl_dim_out);
10026 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
10027 map = isl_map_reset_space(map, space);
10028
10029 return map;
10030}
10031
10032/* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
10033 */
10034__isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
10035 __isl_take isl_map *map2)
10036{
10037 isl_map *prod;
10038
10039 prod = isl_map_domain_product(map1, map2);
10040 prod = isl_map_flatten_domain(prod);
10041 return prod;
10042}
10043
10044/* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
10045 */
10046__isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
10047 __isl_take isl_map *map2)
10048{
10049 isl_map *prod;
10050
10051 prod = isl_map_range_product(map1, map2);
10052 prod = isl_map_flatten_range(prod);
10053 return prod;
10054}
10055
10056uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
10057{
10058 int i;
10059 uint32_t hash = isl_hash_init();
10060 unsigned total;
10061
10062 if (!bmap)
10063 return 0;
10064 bmap = isl_basic_map_copy(bmap);
10065 bmap = isl_basic_map_normalize(bmap);
10066 if (!bmap)
10067 return 0;
10068 total = isl_basic_map_total_dim(bmap);
10069 isl_hash_byte(hash, bmap->n_eq & 0xFF);
10070 for (i = 0; i < bmap->n_eq; ++i) {
10071 uint32_t c_hash;
10072 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
10073 isl_hash_hash(hash, c_hash);
10074 }
10075 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
10076 for (i = 0; i < bmap->n_ineq; ++i) {
10077 uint32_t c_hash;
10078 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
10079 isl_hash_hash(hash, c_hash);
10080 }
10081 isl_hash_byte(hash, bmap->n_div & 0xFF);
10082 for (i = 0; i < bmap->n_div; ++i) {
10083 uint32_t c_hash;
10084 if (isl_int_is_zero(bmap->div[i][0]))
10085 continue;
10086 isl_hash_byte(hash, i & 0xFF);
10087 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
10088 isl_hash_hash(hash, c_hash);
10089 }
10090 isl_basic_map_free(bmap);
10091 return hash;
10092}
10093
10094uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
10095{
Tobias Grosser06e15922016-11-16 11:06:47 +000010096 return isl_basic_map_get_hash(bset_to_bmap(bset));
Tobias Grosser52a25232015-02-04 20:55:43 +000010097}
10098
10099uint32_t isl_map_get_hash(__isl_keep isl_map *map)
10100{
10101 int i;
10102 uint32_t hash;
10103
10104 if (!map)
10105 return 0;
10106 map = isl_map_copy(map);
10107 map = isl_map_normalize(map);
10108 if (!map)
10109 return 0;
10110
10111 hash = isl_hash_init();
10112 for (i = 0; i < map->n; ++i) {
10113 uint32_t bmap_hash;
10114 bmap_hash = isl_basic_map_get_hash(map->p[i]);
10115 isl_hash_hash(hash, bmap_hash);
10116 }
10117
10118 isl_map_free(map);
10119
10120 return hash;
10121}
10122
10123uint32_t isl_set_get_hash(__isl_keep isl_set *set)
10124{
Tobias Grosser06e15922016-11-16 11:06:47 +000010125 return isl_map_get_hash(set_to_map(set));
Tobias Grosser52a25232015-02-04 20:55:43 +000010126}
10127
10128/* Check if the value for dimension dim is completely determined
10129 * by the values of the other parameters and variables.
10130 * That is, check if dimension dim is involved in an equality.
10131 */
10132int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
10133{
10134 int i;
10135 unsigned nparam;
10136
10137 if (!bset)
10138 return -1;
10139 nparam = isl_basic_set_n_param(bset);
10140 for (i = 0; i < bset->n_eq; ++i)
10141 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
10142 return 1;
10143 return 0;
10144}
10145
10146/* Check if the value for dimension dim is completely determined
10147 * by the values of the other parameters and variables.
10148 * That is, check if dimension dim is involved in an equality
10149 * for each of the subsets.
10150 */
10151int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
10152{
10153 int i;
10154
10155 if (!set)
10156 return -1;
10157 for (i = 0; i < set->n; ++i) {
10158 int unique;
10159 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
10160 if (unique != 1)
10161 return unique;
10162 }
10163 return 1;
10164}
10165
10166/* Return the number of basic maps in the (current) representation of "map".
10167 */
10168int isl_map_n_basic_map(__isl_keep isl_map *map)
10169{
10170 return map ? map->n : 0;
10171}
10172
10173int isl_set_n_basic_set(__isl_keep isl_set *set)
10174{
10175 return set ? set->n : 0;
10176}
10177
Tobias Grosserb2f39922015-05-28 13:32:11 +000010178isl_stat isl_map_foreach_basic_map(__isl_keep isl_map *map,
10179 isl_stat (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
Tobias Grosser52a25232015-02-04 20:55:43 +000010180{
10181 int i;
10182
10183 if (!map)
Tobias Grosserb2f39922015-05-28 13:32:11 +000010184 return isl_stat_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000010185
10186 for (i = 0; i < map->n; ++i)
10187 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
Tobias Grosserb2f39922015-05-28 13:32:11 +000010188 return isl_stat_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000010189
Tobias Grosserb2f39922015-05-28 13:32:11 +000010190 return isl_stat_ok;
Tobias Grosser52a25232015-02-04 20:55:43 +000010191}
10192
Tobias Grosserb2f39922015-05-28 13:32:11 +000010193isl_stat isl_set_foreach_basic_set(__isl_keep isl_set *set,
10194 isl_stat (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
Tobias Grosser52a25232015-02-04 20:55:43 +000010195{
10196 int i;
10197
10198 if (!set)
Tobias Grosserb2f39922015-05-28 13:32:11 +000010199 return isl_stat_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000010200
10201 for (i = 0; i < set->n; ++i)
10202 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
Tobias Grosserb2f39922015-05-28 13:32:11 +000010203 return isl_stat_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000010204
Tobias Grosserb2f39922015-05-28 13:32:11 +000010205 return isl_stat_ok;
Tobias Grosser52a25232015-02-04 20:55:43 +000010206}
10207
Tobias Grosser4db55312015-06-30 08:22:14 +000010208/* Return a list of basic sets, the union of which is equal to "set".
10209 */
10210__isl_give isl_basic_set_list *isl_set_get_basic_set_list(
10211 __isl_keep isl_set *set)
10212{
10213 int i;
10214 isl_basic_set_list *list;
10215
10216 if (!set)
10217 return NULL;
10218
10219 list = isl_basic_set_list_alloc(isl_set_get_ctx(set), set->n);
10220 for (i = 0; i < set->n; ++i) {
10221 isl_basic_set *bset;
10222
10223 bset = isl_basic_set_copy(set->p[i]);
10224 list = isl_basic_set_list_add(list, bset);
10225 }
10226
10227 return list;
10228}
10229
Tobias Grosser52a25232015-02-04 20:55:43 +000010230__isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
10231{
10232 isl_space *dim;
10233
10234 if (!bset)
10235 return NULL;
10236
10237 bset = isl_basic_set_cow(bset);
10238 if (!bset)
10239 return NULL;
10240
10241 dim = isl_basic_set_get_space(bset);
10242 dim = isl_space_lift(dim, bset->n_div);
10243 if (!dim)
10244 goto error;
10245 isl_space_free(bset->dim);
10246 bset->dim = dim;
10247 bset->extra -= bset->n_div;
10248 bset->n_div = 0;
10249
10250 bset = isl_basic_set_finalize(bset);
10251
10252 return bset;
10253error:
10254 isl_basic_set_free(bset);
10255 return NULL;
10256}
10257
10258__isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
10259{
10260 int i;
10261 isl_space *dim;
10262 unsigned n_div;
10263
10264 set = isl_set_align_divs(set);
10265
10266 if (!set)
10267 return NULL;
10268
10269 set = isl_set_cow(set);
10270 if (!set)
10271 return NULL;
10272
10273 n_div = set->p[0]->n_div;
10274 dim = isl_set_get_space(set);
10275 dim = isl_space_lift(dim, n_div);
10276 if (!dim)
10277 goto error;
10278 isl_space_free(set->dim);
10279 set->dim = dim;
10280
10281 for (i = 0; i < set->n; ++i) {
10282 set->p[i] = isl_basic_set_lift(set->p[i]);
10283 if (!set->p[i])
10284 goto error;
10285 }
10286
10287 return set;
10288error:
10289 isl_set_free(set);
10290 return NULL;
10291}
10292
10293__isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
10294{
10295 isl_space *dim;
10296 struct isl_basic_map *bmap;
10297 unsigned n_set;
10298 unsigned n_div;
10299 unsigned n_param;
10300 unsigned total;
10301 int i, k, l;
10302
10303 set = isl_set_align_divs(set);
10304
10305 if (!set)
10306 return NULL;
10307
10308 dim = isl_set_get_space(set);
10309 if (set->n == 0 || set->p[0]->n_div == 0) {
10310 isl_set_free(set);
10311 return isl_map_identity(isl_space_map_from_set(dim));
10312 }
10313
10314 n_div = set->p[0]->n_div;
10315 dim = isl_space_map_from_set(dim);
10316 n_param = isl_space_dim(dim, isl_dim_param);
10317 n_set = isl_space_dim(dim, isl_dim_in);
10318 dim = isl_space_extend(dim, n_param, n_set, n_set + n_div);
10319 bmap = isl_basic_map_alloc_space(dim, 0, n_set, 2 * n_div);
10320 for (i = 0; i < n_set; ++i)
10321 bmap = var_equal(bmap, i);
10322
10323 total = n_param + n_set + n_set + n_div;
10324 for (i = 0; i < n_div; ++i) {
10325 k = isl_basic_map_alloc_inequality(bmap);
10326 if (k < 0)
10327 goto error;
10328 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
10329 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
10330 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
10331 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
10332 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
10333 set->p[0]->div[i][0]);
10334
10335 l = isl_basic_map_alloc_inequality(bmap);
10336 if (l < 0)
10337 goto error;
10338 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
10339 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
10340 set->p[0]->div[i][0]);
10341 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
10342 }
10343
10344 isl_set_free(set);
10345 bmap = isl_basic_map_simplify(bmap);
10346 bmap = isl_basic_map_finalize(bmap);
10347 return isl_map_from_basic_map(bmap);
10348error:
10349 isl_set_free(set);
10350 isl_basic_map_free(bmap);
10351 return NULL;
10352}
10353
10354int isl_basic_set_size(__isl_keep isl_basic_set *bset)
10355{
10356 unsigned dim;
10357 int size = 0;
10358
10359 if (!bset)
10360 return -1;
10361
10362 dim = isl_basic_set_total_dim(bset);
10363 size += bset->n_eq * (1 + dim);
10364 size += bset->n_ineq * (1 + dim);
10365 size += bset->n_div * (2 + dim);
10366
10367 return size;
10368}
10369
10370int isl_set_size(__isl_keep isl_set *set)
10371{
10372 int i;
10373 int size = 0;
10374
10375 if (!set)
10376 return -1;
10377
10378 for (i = 0; i < set->n; ++i)
10379 size += isl_basic_set_size(set->p[i]);
10380
10381 return size;
10382}
10383
10384/* Check if there is any lower bound (if lower == 0) and/or upper
10385 * bound (if upper == 0) on the specified dim.
10386 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000010387static isl_bool basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
Tobias Grosser52a25232015-02-04 20:55:43 +000010388 enum isl_dim_type type, unsigned pos, int lower, int upper)
10389{
10390 int i;
10391
10392 if (!bmap)
Tobias Grosserb2f39922015-05-28 13:32:11 +000010393 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000010394
Tobias Grosserb2f39922015-05-28 13:32:11 +000010395 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type),
10396 return isl_bool_error);
Tobias Grosser52a25232015-02-04 20:55:43 +000010397
10398 pos += isl_basic_map_offset(bmap, type);
10399
10400 for (i = 0; i < bmap->n_div; ++i) {
10401 if (isl_int_is_zero(bmap->div[i][0]))
10402 continue;
10403 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
Tobias Grosserb2f39922015-05-28 13:32:11 +000010404 return isl_bool_true;
Tobias Grosser52a25232015-02-04 20:55:43 +000010405 }
10406
10407 for (i = 0; i < bmap->n_eq; ++i)
10408 if (!isl_int_is_zero(bmap->eq[i][pos]))
Tobias Grosserb2f39922015-05-28 13:32:11 +000010409 return isl_bool_true;
Tobias Grosser52a25232015-02-04 20:55:43 +000010410
10411 for (i = 0; i < bmap->n_ineq; ++i) {
10412 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
10413 if (sgn > 0)
10414 lower = 1;
10415 if (sgn < 0)
10416 upper = 1;
10417 }
10418
10419 return lower && upper;
10420}
10421
10422int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10423 enum isl_dim_type type, unsigned pos)
10424{
10425 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
10426}
10427
Tobias Grosserb2f39922015-05-28 13:32:11 +000010428isl_bool isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
Tobias Grosser52a25232015-02-04 20:55:43 +000010429 enum isl_dim_type type, unsigned pos)
10430{
10431 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
10432}
10433
Tobias Grosserb2f39922015-05-28 13:32:11 +000010434isl_bool isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
Tobias Grosser52a25232015-02-04 20:55:43 +000010435 enum isl_dim_type type, unsigned pos)
10436{
10437 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
10438}
10439
10440int isl_map_dim_is_bounded(__isl_keep isl_map *map,
10441 enum isl_dim_type type, unsigned pos)
10442{
10443 int i;
10444
10445 if (!map)
10446 return -1;
10447
10448 for (i = 0; i < map->n; ++i) {
10449 int bounded;
10450 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
10451 if (bounded < 0 || !bounded)
10452 return bounded;
10453 }
10454
10455 return 1;
10456}
10457
10458/* Return 1 if the specified dim is involved in both an upper bound
10459 * and a lower bound.
10460 */
10461int isl_set_dim_is_bounded(__isl_keep isl_set *set,
10462 enum isl_dim_type type, unsigned pos)
10463{
Tobias Grosser06e15922016-11-16 11:06:47 +000010464 return isl_map_dim_is_bounded(set_to_map(set), type, pos);
Tobias Grosser52a25232015-02-04 20:55:43 +000010465}
10466
10467/* Does "map" have a bound (according to "fn") for any of its basic maps?
10468 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000010469static isl_bool has_any_bound(__isl_keep isl_map *map,
Tobias Grosser52a25232015-02-04 20:55:43 +000010470 enum isl_dim_type type, unsigned pos,
Tobias Grosserb2f39922015-05-28 13:32:11 +000010471 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
Tobias Grosser52a25232015-02-04 20:55:43 +000010472 enum isl_dim_type type, unsigned pos))
10473{
10474 int i;
10475
10476 if (!map)
Tobias Grosserb2f39922015-05-28 13:32:11 +000010477 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000010478
10479 for (i = 0; i < map->n; ++i) {
Tobias Grosserb2f39922015-05-28 13:32:11 +000010480 isl_bool bounded;
Tobias Grosser52a25232015-02-04 20:55:43 +000010481 bounded = fn(map->p[i], type, pos);
10482 if (bounded < 0 || bounded)
10483 return bounded;
10484 }
10485
Tobias Grosserb2f39922015-05-28 13:32:11 +000010486 return isl_bool_false;
Tobias Grosser52a25232015-02-04 20:55:43 +000010487}
10488
10489/* Return 1 if the specified dim is involved in any lower bound.
10490 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000010491isl_bool isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
Tobias Grosser52a25232015-02-04 20:55:43 +000010492 enum isl_dim_type type, unsigned pos)
10493{
10494 return has_any_bound(set, type, pos,
10495 &isl_basic_map_dim_has_lower_bound);
10496}
10497
10498/* Return 1 if the specified dim is involved in any upper bound.
10499 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000010500isl_bool isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
Tobias Grosser52a25232015-02-04 20:55:43 +000010501 enum isl_dim_type type, unsigned pos)
10502{
10503 return has_any_bound(set, type, pos,
10504 &isl_basic_map_dim_has_upper_bound);
10505}
10506
10507/* Does "map" have a bound (according to "fn") for all of its basic maps?
10508 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000010509static isl_bool has_bound(__isl_keep isl_map *map,
Tobias Grosser52a25232015-02-04 20:55:43 +000010510 enum isl_dim_type type, unsigned pos,
Tobias Grosserb2f39922015-05-28 13:32:11 +000010511 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
Tobias Grosser52a25232015-02-04 20:55:43 +000010512 enum isl_dim_type type, unsigned pos))
10513{
10514 int i;
10515
10516 if (!map)
Tobias Grosserb2f39922015-05-28 13:32:11 +000010517 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000010518
10519 for (i = 0; i < map->n; ++i) {
Tobias Grosserb2f39922015-05-28 13:32:11 +000010520 isl_bool bounded;
Tobias Grosser52a25232015-02-04 20:55:43 +000010521 bounded = fn(map->p[i], type, pos);
10522 if (bounded < 0 || !bounded)
10523 return bounded;
10524 }
10525
Tobias Grosserb2f39922015-05-28 13:32:11 +000010526 return isl_bool_true;
Tobias Grosser52a25232015-02-04 20:55:43 +000010527}
10528
10529/* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10530 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000010531isl_bool isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
Tobias Grosser52a25232015-02-04 20:55:43 +000010532 enum isl_dim_type type, unsigned pos)
10533{
10534 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
10535}
10536
10537/* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10538 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000010539isl_bool isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
Tobias Grosser52a25232015-02-04 20:55:43 +000010540 enum isl_dim_type type, unsigned pos)
10541{
10542 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
10543}
10544
10545/* For each of the "n" variables starting at "first", determine
10546 * the sign of the variable and put the results in the first "n"
10547 * elements of the array "signs".
10548 * Sign
10549 * 1 means that the variable is non-negative
10550 * -1 means that the variable is non-positive
10551 * 0 means the variable attains both positive and negative values.
10552 */
10553int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
10554 unsigned first, unsigned n, int *signs)
10555{
10556 isl_vec *bound = NULL;
10557 struct isl_tab *tab = NULL;
10558 struct isl_tab_undo *snap;
10559 int i;
10560
10561 if (!bset || !signs)
10562 return -1;
10563
10564 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
10565 tab = isl_tab_from_basic_set(bset, 0);
10566 if (!bound || !tab)
10567 goto error;
10568
10569 isl_seq_clr(bound->el, bound->size);
10570 isl_int_set_si(bound->el[0], -1);
10571
10572 snap = isl_tab_snap(tab);
10573 for (i = 0; i < n; ++i) {
10574 int empty;
10575
10576 isl_int_set_si(bound->el[1 + first + i], -1);
10577 if (isl_tab_add_ineq(tab, bound->el) < 0)
10578 goto error;
10579 empty = tab->empty;
10580 isl_int_set_si(bound->el[1 + first + i], 0);
10581 if (isl_tab_rollback(tab, snap) < 0)
10582 goto error;
10583
10584 if (empty) {
10585 signs[i] = 1;
10586 continue;
10587 }
10588
10589 isl_int_set_si(bound->el[1 + first + i], 1);
10590 if (isl_tab_add_ineq(tab, bound->el) < 0)
10591 goto error;
10592 empty = tab->empty;
10593 isl_int_set_si(bound->el[1 + first + i], 0);
10594 if (isl_tab_rollback(tab, snap) < 0)
10595 goto error;
10596
10597 signs[i] = empty ? -1 : 0;
10598 }
10599
10600 isl_tab_free(tab);
10601 isl_vec_free(bound);
10602 return 0;
10603error:
10604 isl_tab_free(tab);
10605 isl_vec_free(bound);
10606 return -1;
10607}
10608
10609int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
10610 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
10611{
10612 if (!bset || !signs)
10613 return -1;
10614 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
10615 return -1);
10616
10617 first += pos(bset->dim, type) - 1;
10618 return isl_basic_set_vars_get_sign(bset, first, n, signs);
10619}
10620
10621/* Is it possible for the integer division "div" to depend (possibly
10622 * indirectly) on any output dimensions?
10623 *
10624 * If the div is undefined, then we conservatively assume that it
10625 * may depend on them.
10626 * Otherwise, we check if it actually depends on them or on any integer
10627 * divisions that may depend on them.
10628 */
10629static int div_may_involve_output(__isl_keep isl_basic_map *bmap, int div)
10630{
10631 int i;
10632 unsigned n_out, o_out;
10633 unsigned n_div, o_div;
10634
10635 if (isl_int_is_zero(bmap->div[div][0]))
10636 return 1;
10637
10638 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10639 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10640
10641 if (isl_seq_first_non_zero(bmap->div[div] + 1 + o_out, n_out) != -1)
10642 return 1;
10643
10644 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10645 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10646
10647 for (i = 0; i < n_div; ++i) {
10648 if (isl_int_is_zero(bmap->div[div][1 + o_div + i]))
10649 continue;
10650 if (div_may_involve_output(bmap, i))
10651 return 1;
10652 }
10653
10654 return 0;
10655}
10656
Michael Kruse959a8dc2016-01-15 15:54:45 +000010657/* Return the first integer division of "bmap" in the range
10658 * [first, first + n[ that may depend on any output dimensions and
10659 * that has a non-zero coefficient in "c" (where the first coefficient
10660 * in "c" corresponds to integer division "first").
10661 */
10662static int first_div_may_involve_output(__isl_keep isl_basic_map *bmap,
10663 isl_int *c, int first, int n)
10664{
10665 int k;
10666
10667 if (!bmap)
10668 return -1;
10669
10670 for (k = first; k < first + n; ++k) {
10671 if (isl_int_is_zero(c[k]))
10672 continue;
10673 if (div_may_involve_output(bmap, k))
10674 return k;
10675 }
10676
10677 return first + n;
10678}
10679
10680/* Look for a pair of inequality constraints in "bmap" of the form
10681 *
10682 * -l + i >= 0 or i >= l
10683 * and
10684 * n + l - i >= 0 or i <= l + n
10685 *
10686 * with n < "m" and i the output dimension at position "pos".
10687 * (Note that n >= 0 as otherwise the two constraints would conflict.)
10688 * Furthermore, "l" is only allowed to involve parameters, input dimensions
10689 * and earlier output dimensions, as well as integer divisions that do
10690 * not involve any of the output dimensions.
10691 *
10692 * Return the index of the first inequality constraint or bmap->n_ineq
10693 * if no such pair can be found.
10694 */
10695static int find_modulo_constraint_pair(__isl_keep isl_basic_map *bmap,
10696 int pos, isl_int m)
10697{
10698 int i, j;
10699 isl_ctx *ctx;
10700 unsigned total;
10701 unsigned n_div, o_div;
10702 unsigned n_out, o_out;
10703 int less;
10704
10705 if (!bmap)
10706 return -1;
10707
10708 ctx = isl_basic_map_get_ctx(bmap);
10709 total = isl_basic_map_total_dim(bmap);
10710 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10711 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10712 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10713 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10714 for (i = 0; i < bmap->n_ineq; ++i) {
10715 if (!isl_int_abs_eq(bmap->ineq[i][o_out + pos], ctx->one))
10716 continue;
10717 if (isl_seq_first_non_zero(bmap->ineq[i] + o_out + pos + 1,
10718 n_out - (pos + 1)) != -1)
10719 continue;
10720 if (first_div_may_involve_output(bmap, bmap->ineq[i] + o_div,
10721 0, n_div) < n_div)
10722 continue;
10723 for (j = i + 1; j < bmap->n_ineq; ++j) {
10724 if (!isl_int_abs_eq(bmap->ineq[j][o_out + pos],
10725 ctx->one))
10726 continue;
10727 if (!isl_seq_is_neg(bmap->ineq[i] + 1,
10728 bmap->ineq[j] + 1, total))
10729 continue;
10730 break;
10731 }
10732 if (j >= bmap->n_ineq)
10733 continue;
10734 isl_int_add(bmap->ineq[i][0],
10735 bmap->ineq[i][0], bmap->ineq[j][0]);
10736 less = isl_int_abs_lt(bmap->ineq[i][0], m);
10737 isl_int_sub(bmap->ineq[i][0],
10738 bmap->ineq[i][0], bmap->ineq[j][0]);
10739 if (!less)
10740 continue;
10741 if (isl_int_is_one(bmap->ineq[i][o_out + pos]))
10742 return i;
10743 else
10744 return j;
10745 }
10746
10747 return bmap->n_ineq;
10748}
10749
Tobias Grosser52a25232015-02-04 20:55:43 +000010750/* Return the index of the equality of "bmap" that defines
10751 * the output dimension "pos" in terms of earlier dimensions.
10752 * The equality may also involve integer divisions, as long
10753 * as those integer divisions are defined in terms of
10754 * parameters or input dimensions.
Michael Kruse959a8dc2016-01-15 15:54:45 +000010755 * In this case, *div is set to the number of integer divisions and
10756 * *ineq is set to the number of inequality constraints (provided
10757 * div and ineq are not NULL).
10758 *
10759 * The equality may also involve a single integer division involving
10760 * the output dimensions (typically only output dimension "pos") as
10761 * long as the coefficient of output dimension "pos" is 1 or -1 and
10762 * there is a pair of constraints i >= l and i <= l + n, with i referring
10763 * to output dimension "pos", l an expression involving only earlier
10764 * dimensions and n smaller than the coefficient of the integer division
10765 * in the equality. In this case, the output dimension can be defined
10766 * in terms of a modulo expression that does not involve the integer division.
10767 * *div is then set to this single integer division and
10768 * *ineq is set to the index of constraint i >= l.
10769 *
Tobias Grosser52a25232015-02-04 20:55:43 +000010770 * Return bmap->n_eq if there is no such equality.
10771 * Return -1 on error.
10772 */
10773int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map *bmap,
Michael Kruse959a8dc2016-01-15 15:54:45 +000010774 int pos, int *div, int *ineq)
Tobias Grosser52a25232015-02-04 20:55:43 +000010775{
Michael Kruse959a8dc2016-01-15 15:54:45 +000010776 int j, k, l;
Tobias Grosser52a25232015-02-04 20:55:43 +000010777 unsigned n_out, o_out;
10778 unsigned n_div, o_div;
10779
10780 if (!bmap)
10781 return -1;
10782
10783 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10784 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10785 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10786 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10787
Michael Kruse959a8dc2016-01-15 15:54:45 +000010788 if (ineq)
10789 *ineq = bmap->n_ineq;
10790 if (div)
10791 *div = n_div;
Tobias Grosser52a25232015-02-04 20:55:43 +000010792 for (j = 0; j < bmap->n_eq; ++j) {
10793 if (isl_int_is_zero(bmap->eq[j][o_out + pos]))
10794 continue;
10795 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + pos + 1,
10796 n_out - (pos + 1)) != -1)
10797 continue;
Michael Kruse959a8dc2016-01-15 15:54:45 +000010798 k = first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
10799 0, n_div);
Tobias Grosser52a25232015-02-04 20:55:43 +000010800 if (k >= n_div)
10801 return j;
Michael Kruse959a8dc2016-01-15 15:54:45 +000010802 if (!isl_int_is_one(bmap->eq[j][o_out + pos]) &&
10803 !isl_int_is_negone(bmap->eq[j][o_out + pos]))
10804 continue;
10805 if (first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
10806 k + 1, n_div - (k+1)) < n_div)
10807 continue;
10808 l = find_modulo_constraint_pair(bmap, pos,
10809 bmap->eq[j][o_div + k]);
10810 if (l < 0)
10811 return -1;
10812 if (l >= bmap->n_ineq)
10813 continue;
10814 if (div)
10815 *div = k;
10816 if (ineq)
10817 *ineq = l;
10818 return j;
Tobias Grosser52a25232015-02-04 20:55:43 +000010819 }
10820
10821 return bmap->n_eq;
10822}
10823
10824/* Check if the given basic map is obviously single-valued.
10825 * In particular, for each output dimension, check that there is
10826 * an equality that defines the output dimension in terms of
10827 * earlier dimensions.
10828 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000010829isl_bool isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
Tobias Grosser52a25232015-02-04 20:55:43 +000010830{
10831 int i;
10832 unsigned n_out;
10833
10834 if (!bmap)
Tobias Grosserb2f39922015-05-28 13:32:11 +000010835 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000010836
10837 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10838
10839 for (i = 0; i < n_out; ++i) {
10840 int eq;
10841
Michael Kruse959a8dc2016-01-15 15:54:45 +000010842 eq = isl_basic_map_output_defining_equality(bmap, i,
10843 NULL, NULL);
Tobias Grosser52a25232015-02-04 20:55:43 +000010844 if (eq < 0)
Tobias Grosserb2f39922015-05-28 13:32:11 +000010845 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000010846 if (eq >= bmap->n_eq)
Tobias Grosserb2f39922015-05-28 13:32:11 +000010847 return isl_bool_false;
Tobias Grosser52a25232015-02-04 20:55:43 +000010848 }
10849
Tobias Grosserb2f39922015-05-28 13:32:11 +000010850 return isl_bool_true;
Tobias Grosser52a25232015-02-04 20:55:43 +000010851}
10852
10853/* Check if the given basic map is single-valued.
10854 * We simply compute
10855 *
10856 * M \circ M^-1
10857 *
10858 * and check if the result is a subset of the identity mapping.
10859 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000010860isl_bool isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
Tobias Grosser52a25232015-02-04 20:55:43 +000010861{
10862 isl_space *space;
10863 isl_basic_map *test;
10864 isl_basic_map *id;
Tobias Grosserb2f39922015-05-28 13:32:11 +000010865 isl_bool sv;
Tobias Grosser52a25232015-02-04 20:55:43 +000010866
10867 sv = isl_basic_map_plain_is_single_valued(bmap);
10868 if (sv < 0 || sv)
10869 return sv;
10870
10871 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
10872 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
10873
10874 space = isl_basic_map_get_space(bmap);
10875 space = isl_space_map_from_set(isl_space_range(space));
10876 id = isl_basic_map_identity(space);
10877
10878 sv = isl_basic_map_is_subset(test, id);
10879
10880 isl_basic_map_free(test);
10881 isl_basic_map_free(id);
10882
10883 return sv;
10884}
10885
10886/* Check if the given map is obviously single-valued.
10887 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000010888isl_bool isl_map_plain_is_single_valued(__isl_keep isl_map *map)
Tobias Grosser52a25232015-02-04 20:55:43 +000010889{
10890 if (!map)
Tobias Grosserb2f39922015-05-28 13:32:11 +000010891 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000010892 if (map->n == 0)
Tobias Grosserb2f39922015-05-28 13:32:11 +000010893 return isl_bool_true;
Tobias Grosser52a25232015-02-04 20:55:43 +000010894 if (map->n >= 2)
Tobias Grosserb2f39922015-05-28 13:32:11 +000010895 return isl_bool_false;
Tobias Grosser52a25232015-02-04 20:55:43 +000010896
10897 return isl_basic_map_plain_is_single_valued(map->p[0]);
10898}
10899
10900/* Check if the given map is single-valued.
10901 * We simply compute
10902 *
10903 * M \circ M^-1
10904 *
10905 * and check if the result is a subset of the identity mapping.
10906 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000010907isl_bool isl_map_is_single_valued(__isl_keep isl_map *map)
Tobias Grosser52a25232015-02-04 20:55:43 +000010908{
10909 isl_space *dim;
10910 isl_map *test;
10911 isl_map *id;
Tobias Grosserb2f39922015-05-28 13:32:11 +000010912 isl_bool sv;
Tobias Grosser52a25232015-02-04 20:55:43 +000010913
10914 sv = isl_map_plain_is_single_valued(map);
10915 if (sv < 0 || sv)
10916 return sv;
10917
10918 test = isl_map_reverse(isl_map_copy(map));
10919 test = isl_map_apply_range(test, isl_map_copy(map));
10920
10921 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
10922 id = isl_map_identity(dim);
10923
10924 sv = isl_map_is_subset(test, id);
10925
10926 isl_map_free(test);
10927 isl_map_free(id);
10928
10929 return sv;
10930}
10931
Tobias Grosserb2f39922015-05-28 13:32:11 +000010932isl_bool isl_map_is_injective(__isl_keep isl_map *map)
Tobias Grosser52a25232015-02-04 20:55:43 +000010933{
Tobias Grosserb2f39922015-05-28 13:32:11 +000010934 isl_bool in;
Tobias Grosser52a25232015-02-04 20:55:43 +000010935
10936 map = isl_map_copy(map);
10937 map = isl_map_reverse(map);
10938 in = isl_map_is_single_valued(map);
10939 isl_map_free(map);
10940
10941 return in;
10942}
10943
10944/* Check if the given map is obviously injective.
10945 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000010946isl_bool isl_map_plain_is_injective(__isl_keep isl_map *map)
Tobias Grosser52a25232015-02-04 20:55:43 +000010947{
Tobias Grosserb2f39922015-05-28 13:32:11 +000010948 isl_bool in;
Tobias Grosser52a25232015-02-04 20:55:43 +000010949
10950 map = isl_map_copy(map);
10951 map = isl_map_reverse(map);
10952 in = isl_map_plain_is_single_valued(map);
10953 isl_map_free(map);
10954
10955 return in;
10956}
10957
Tobias Grosserb2f39922015-05-28 13:32:11 +000010958isl_bool isl_map_is_bijective(__isl_keep isl_map *map)
Tobias Grosser52a25232015-02-04 20:55:43 +000010959{
Tobias Grosserb2f39922015-05-28 13:32:11 +000010960 isl_bool sv;
Tobias Grosser52a25232015-02-04 20:55:43 +000010961
10962 sv = isl_map_is_single_valued(map);
10963 if (sv < 0 || !sv)
10964 return sv;
10965
10966 return isl_map_is_injective(map);
10967}
10968
Tobias Grosserb2f39922015-05-28 13:32:11 +000010969isl_bool isl_set_is_singleton(__isl_keep isl_set *set)
Tobias Grosser52a25232015-02-04 20:55:43 +000010970{
Tobias Grosser06e15922016-11-16 11:06:47 +000010971 return isl_map_is_single_valued(set_to_map(set));
Tobias Grosser52a25232015-02-04 20:55:43 +000010972}
10973
Michael Krusee0b34f32016-05-04 14:41:36 +000010974/* Does "map" only map elements to themselves?
10975 *
10976 * If the domain and range spaces are different, then "map"
10977 * is considered not to be an identity relation, even if it is empty.
10978 * Otherwise, construct the maximal identity relation and
10979 * check whether "map" is a subset of this relation.
10980 */
10981isl_bool isl_map_is_identity(__isl_keep isl_map *map)
10982{
10983 isl_space *space;
10984 isl_map *id;
10985 isl_bool equal, is_identity;
10986
10987 space = isl_map_get_space(map);
10988 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
10989 isl_space_free(space);
10990 if (equal < 0 || !equal)
10991 return equal;
10992
10993 id = isl_map_identity(isl_map_get_space(map));
10994 is_identity = isl_map_is_subset(map, id);
10995 isl_map_free(id);
10996
10997 return is_identity;
10998}
10999
Tobias Grosser52a25232015-02-04 20:55:43 +000011000int isl_map_is_translation(__isl_keep isl_map *map)
11001{
11002 int ok;
11003 isl_set *delta;
11004
11005 delta = isl_map_deltas(isl_map_copy(map));
11006 ok = isl_set_is_singleton(delta);
11007 isl_set_free(delta);
11008
11009 return ok;
11010}
11011
11012static int unique(isl_int *p, unsigned pos, unsigned len)
11013{
11014 if (isl_seq_first_non_zero(p, pos) != -1)
11015 return 0;
11016 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
11017 return 0;
11018 return 1;
11019}
11020
11021int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
11022{
11023 int i, j;
11024 unsigned nvar;
11025 unsigned ovar;
11026
11027 if (!bset)
11028 return -1;
11029
11030 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
11031 return 0;
11032
11033 nvar = isl_basic_set_dim(bset, isl_dim_set);
11034 ovar = isl_space_offset(bset->dim, isl_dim_set);
11035 for (j = 0; j < nvar; ++j) {
11036 int lower = 0, upper = 0;
11037 for (i = 0; i < bset->n_eq; ++i) {
11038 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
11039 continue;
11040 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
11041 return 0;
11042 break;
11043 }
11044 if (i < bset->n_eq)
11045 continue;
11046 for (i = 0; i < bset->n_ineq; ++i) {
11047 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
11048 continue;
11049 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
11050 return 0;
11051 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
11052 lower = 1;
11053 else
11054 upper = 1;
11055 }
11056 if (!lower || !upper)
11057 return 0;
11058 }
11059
11060 return 1;
11061}
11062
11063int isl_set_is_box(__isl_keep isl_set *set)
11064{
11065 if (!set)
11066 return -1;
11067 if (set->n != 1)
11068 return 0;
11069
11070 return isl_basic_set_is_box(set->p[0]);
11071}
11072
Tobias Grosserb2f39922015-05-28 13:32:11 +000011073isl_bool isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
Tobias Grosser52a25232015-02-04 20:55:43 +000011074{
11075 if (!bset)
Tobias Grosserb2f39922015-05-28 13:32:11 +000011076 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000011077
11078 return isl_space_is_wrapping(bset->dim);
11079}
11080
Tobias Grosserb2f39922015-05-28 13:32:11 +000011081isl_bool isl_set_is_wrapping(__isl_keep isl_set *set)
Tobias Grosser52a25232015-02-04 20:55:43 +000011082{
11083 if (!set)
Tobias Grosserb2f39922015-05-28 13:32:11 +000011084 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000011085
11086 return isl_space_is_wrapping(set->dim);
11087}
11088
Michael Kruse959a8dc2016-01-15 15:54:45 +000011089/* Modify the space of "map" through a call to "change".
11090 * If "can_change" is set (not NULL), then first call it to check
11091 * if the modification is allowed, printing the error message "cannot_change"
11092 * if it is not.
11093 */
11094static __isl_give isl_map *isl_map_change_space(__isl_take isl_map *map,
11095 isl_bool (*can_change)(__isl_keep isl_map *map),
11096 const char *cannot_change,
11097 __isl_give isl_space *(*change)(__isl_take isl_space *space))
11098{
11099 isl_bool ok;
11100 isl_space *space;
11101
11102 if (!map)
11103 return NULL;
11104
11105 ok = can_change ? can_change(map) : isl_bool_true;
11106 if (ok < 0)
11107 return isl_map_free(map);
11108 if (!ok)
11109 isl_die(isl_map_get_ctx(map), isl_error_invalid, cannot_change,
11110 return isl_map_free(map));
11111
11112 space = change(isl_map_get_space(map));
11113 map = isl_map_reset_space(map, space);
11114
11115 return map;
11116}
11117
Tobias Grosser52a25232015-02-04 20:55:43 +000011118/* Is the domain of "map" a wrapped relation?
11119 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000011120isl_bool isl_map_domain_is_wrapping(__isl_keep isl_map *map)
Tobias Grosser52a25232015-02-04 20:55:43 +000011121{
11122 if (!map)
Tobias Grosserb2f39922015-05-28 13:32:11 +000011123 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000011124
11125 return isl_space_domain_is_wrapping(map->dim);
11126}
11127
11128/* Is the range of "map" a wrapped relation?
11129 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000011130isl_bool isl_map_range_is_wrapping(__isl_keep isl_map *map)
Tobias Grosser52a25232015-02-04 20:55:43 +000011131{
11132 if (!map)
Tobias Grosserb2f39922015-05-28 13:32:11 +000011133 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000011134
11135 return isl_space_range_is_wrapping(map->dim);
11136}
11137
11138__isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
11139{
11140 bmap = isl_basic_map_cow(bmap);
11141 if (!bmap)
11142 return NULL;
11143
11144 bmap->dim = isl_space_wrap(bmap->dim);
11145 if (!bmap->dim)
11146 goto error;
11147
11148 bmap = isl_basic_map_finalize(bmap);
11149
Tobias Grosser06e15922016-11-16 11:06:47 +000011150 return bset_from_bmap(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +000011151error:
11152 isl_basic_map_free(bmap);
11153 return NULL;
11154}
11155
Michael Kruse959a8dc2016-01-15 15:54:45 +000011156/* Given a map A -> B, return the set (A -> B).
11157 */
Tobias Grosser52a25232015-02-04 20:55:43 +000011158__isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
11159{
Michael Kruse959a8dc2016-01-15 15:54:45 +000011160 return isl_map_change_space(map, NULL, NULL, &isl_space_wrap);
Tobias Grosser52a25232015-02-04 20:55:43 +000011161}
11162
11163__isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
11164{
11165 bset = isl_basic_set_cow(bset);
11166 if (!bset)
11167 return NULL;
11168
11169 bset->dim = isl_space_unwrap(bset->dim);
11170 if (!bset->dim)
11171 goto error;
11172
11173 bset = isl_basic_set_finalize(bset);
11174
Tobias Grosser06e15922016-11-16 11:06:47 +000011175 return bset_to_bmap(bset);
Tobias Grosser52a25232015-02-04 20:55:43 +000011176error:
11177 isl_basic_set_free(bset);
11178 return NULL;
11179}
11180
Michael Kruse959a8dc2016-01-15 15:54:45 +000011181/* Given a set (A -> B), return the map A -> B.
11182 * Error out if "set" is not of the form (A -> B).
11183 */
Tobias Grosser52a25232015-02-04 20:55:43 +000011184__isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
11185{
Michael Kruse959a8dc2016-01-15 15:54:45 +000011186 return isl_map_change_space(set, &isl_set_is_wrapping,
11187 "not a wrapping set", &isl_space_unwrap);
Tobias Grosser52a25232015-02-04 20:55:43 +000011188}
11189
11190__isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
11191 enum isl_dim_type type)
11192{
11193 if (!bmap)
11194 return NULL;
11195
11196 if (!isl_space_is_named_or_nested(bmap->dim, type))
11197 return bmap;
11198
11199 bmap = isl_basic_map_cow(bmap);
11200 if (!bmap)
11201 return NULL;
11202
11203 bmap->dim = isl_space_reset(bmap->dim, type);
11204 if (!bmap->dim)
11205 goto error;
11206
11207 bmap = isl_basic_map_finalize(bmap);
11208
11209 return bmap;
11210error:
11211 isl_basic_map_free(bmap);
11212 return NULL;
11213}
11214
11215__isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
11216 enum isl_dim_type type)
11217{
11218 int i;
11219
11220 if (!map)
11221 return NULL;
11222
11223 if (!isl_space_is_named_or_nested(map->dim, type))
11224 return map;
11225
11226 map = isl_map_cow(map);
11227 if (!map)
11228 return NULL;
11229
11230 for (i = 0; i < map->n; ++i) {
11231 map->p[i] = isl_basic_map_reset(map->p[i], type);
11232 if (!map->p[i])
11233 goto error;
11234 }
11235 map->dim = isl_space_reset(map->dim, type);
11236 if (!map->dim)
11237 goto error;
11238
11239 return map;
11240error:
11241 isl_map_free(map);
11242 return NULL;
11243}
11244
11245__isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
11246{
11247 if (!bmap)
11248 return NULL;
11249
11250 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
11251 return bmap;
11252
11253 bmap = isl_basic_map_cow(bmap);
11254 if (!bmap)
11255 return NULL;
11256
11257 bmap->dim = isl_space_flatten(bmap->dim);
11258 if (!bmap->dim)
11259 goto error;
11260
11261 bmap = isl_basic_map_finalize(bmap);
11262
11263 return bmap;
11264error:
11265 isl_basic_map_free(bmap);
11266 return NULL;
11267}
11268
11269__isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
11270{
Tobias Grosser06e15922016-11-16 11:06:47 +000011271 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset)));
Tobias Grosser52a25232015-02-04 20:55:43 +000011272}
11273
11274__isl_give isl_basic_map *isl_basic_map_flatten_domain(
11275 __isl_take isl_basic_map *bmap)
11276{
11277 if (!bmap)
11278 return NULL;
11279
11280 if (!bmap->dim->nested[0])
11281 return bmap;
11282
11283 bmap = isl_basic_map_cow(bmap);
11284 if (!bmap)
11285 return NULL;
11286
11287 bmap->dim = isl_space_flatten_domain(bmap->dim);
11288 if (!bmap->dim)
11289 goto error;
11290
11291 bmap = isl_basic_map_finalize(bmap);
11292
11293 return bmap;
11294error:
11295 isl_basic_map_free(bmap);
11296 return NULL;
11297}
11298
11299__isl_give isl_basic_map *isl_basic_map_flatten_range(
11300 __isl_take isl_basic_map *bmap)
11301{
11302 if (!bmap)
11303 return NULL;
11304
11305 if (!bmap->dim->nested[1])
11306 return bmap;
11307
11308 bmap = isl_basic_map_cow(bmap);
11309 if (!bmap)
11310 return NULL;
11311
11312 bmap->dim = isl_space_flatten_range(bmap->dim);
11313 if (!bmap->dim)
11314 goto error;
11315
11316 bmap = isl_basic_map_finalize(bmap);
11317
11318 return bmap;
11319error:
11320 isl_basic_map_free(bmap);
11321 return NULL;
11322}
11323
Michael Kruse959a8dc2016-01-15 15:54:45 +000011324/* Remove any internal structure from the spaces of domain and range of "map".
11325 */
Tobias Grosser52a25232015-02-04 20:55:43 +000011326__isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
11327{
Tobias Grosser52a25232015-02-04 20:55:43 +000011328 if (!map)
11329 return NULL;
11330
11331 if (!map->dim->nested[0] && !map->dim->nested[1])
11332 return map;
11333
Michael Kruse959a8dc2016-01-15 15:54:45 +000011334 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten);
Tobias Grosser52a25232015-02-04 20:55:43 +000011335}
11336
11337__isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
11338{
Tobias Grosser06e15922016-11-16 11:06:47 +000011339 return set_from_map(isl_map_flatten(set_to_map(set)));
Tobias Grosser52a25232015-02-04 20:55:43 +000011340}
11341
11342__isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
11343{
11344 isl_space *dim, *flat_dim;
11345 isl_map *map;
11346
11347 dim = isl_set_get_space(set);
11348 flat_dim = isl_space_flatten(isl_space_copy(dim));
11349 map = isl_map_identity(isl_space_join(isl_space_reverse(dim), flat_dim));
11350 map = isl_map_intersect_domain(map, set);
11351
11352 return map;
11353}
11354
Michael Kruse959a8dc2016-01-15 15:54:45 +000011355/* Remove any internal structure from the space of the domain of "map".
11356 */
Tobias Grosser52a25232015-02-04 20:55:43 +000011357__isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
11358{
Tobias Grosser52a25232015-02-04 20:55:43 +000011359 if (!map)
11360 return NULL;
11361
11362 if (!map->dim->nested[0])
11363 return map;
11364
Michael Kruse959a8dc2016-01-15 15:54:45 +000011365 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_domain);
Tobias Grosser52a25232015-02-04 20:55:43 +000011366}
11367
Michael Kruse959a8dc2016-01-15 15:54:45 +000011368/* Remove any internal structure from the space of the range of "map".
11369 */
Tobias Grosser52a25232015-02-04 20:55:43 +000011370__isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
11371{
Tobias Grosser52a25232015-02-04 20:55:43 +000011372 if (!map)
11373 return NULL;
11374
11375 if (!map->dim->nested[1])
11376 return map;
11377
Michael Kruse959a8dc2016-01-15 15:54:45 +000011378 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_range);
Tobias Grosser52a25232015-02-04 20:55:43 +000011379}
11380
11381/* Reorder the dimensions of "bmap" according to the given dim_map
Tobias Grossera7f1e042016-02-04 06:19:12 +000011382 * and set the dimension specification to "dim" and
11383 * perform Gaussian elimination on the result.
Tobias Grosser52a25232015-02-04 20:55:43 +000011384 */
11385__isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
11386 __isl_take isl_space *dim, __isl_take struct isl_dim_map *dim_map)
11387{
11388 isl_basic_map *res;
11389 unsigned flags;
11390
11391 bmap = isl_basic_map_cow(bmap);
11392 if (!bmap || !dim || !dim_map)
11393 goto error;
11394
11395 flags = bmap->flags;
11396 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
11397 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED);
11398 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
11399 res = isl_basic_map_alloc_space(dim,
11400 bmap->n_div, bmap->n_eq, bmap->n_ineq);
11401 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
11402 if (res)
11403 res->flags = flags;
Tobias Grossera7f1e042016-02-04 06:19:12 +000011404 res = isl_basic_map_gauss(res, NULL);
Tobias Grosser52a25232015-02-04 20:55:43 +000011405 res = isl_basic_map_finalize(res);
11406 return res;
11407error:
11408 free(dim_map);
11409 isl_basic_map_free(bmap);
11410 isl_space_free(dim);
11411 return NULL;
11412}
11413
11414/* Reorder the dimensions of "map" according to given reordering.
11415 */
11416__isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
11417 __isl_take isl_reordering *r)
11418{
11419 int i;
11420 struct isl_dim_map *dim_map;
11421
11422 map = isl_map_cow(map);
11423 dim_map = isl_dim_map_from_reordering(r);
11424 if (!map || !r || !dim_map)
11425 goto error;
11426
11427 for (i = 0; i < map->n; ++i) {
11428 struct isl_dim_map *dim_map_i;
11429
11430 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
11431
11432 map->p[i] = isl_basic_map_realign(map->p[i],
11433 isl_space_copy(r->dim), dim_map_i);
11434
11435 if (!map->p[i])
11436 goto error;
11437 }
11438
11439 map = isl_map_reset_space(map, isl_space_copy(r->dim));
11440
11441 isl_reordering_free(r);
11442 free(dim_map);
11443 return map;
11444error:
11445 free(dim_map);
11446 isl_map_free(map);
11447 isl_reordering_free(r);
11448 return NULL;
11449}
11450
11451__isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
11452 __isl_take isl_reordering *r)
11453{
Tobias Grosser06e15922016-11-16 11:06:47 +000011454 return set_from_map(isl_map_realign(set_to_map(set), r));
Tobias Grosser52a25232015-02-04 20:55:43 +000011455}
11456
11457__isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
11458 __isl_take isl_space *model)
11459{
11460 isl_ctx *ctx;
11461
11462 if (!map || !model)
11463 goto error;
11464
11465 ctx = isl_space_get_ctx(model);
11466 if (!isl_space_has_named_params(model))
11467 isl_die(ctx, isl_error_invalid,
11468 "model has unnamed parameters", goto error);
11469 if (!isl_space_has_named_params(map->dim))
11470 isl_die(ctx, isl_error_invalid,
11471 "relation has unnamed parameters", goto error);
11472 if (!isl_space_match(map->dim, isl_dim_param, model, isl_dim_param)) {
11473 isl_reordering *exp;
11474
11475 model = isl_space_drop_dims(model, isl_dim_in,
11476 0, isl_space_dim(model, isl_dim_in));
11477 model = isl_space_drop_dims(model, isl_dim_out,
11478 0, isl_space_dim(model, isl_dim_out));
11479 exp = isl_parameter_alignment_reordering(map->dim, model);
11480 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
11481 map = isl_map_realign(map, exp);
11482 }
11483
11484 isl_space_free(model);
11485 return map;
11486error:
11487 isl_space_free(model);
11488 isl_map_free(map);
11489 return NULL;
11490}
11491
11492__isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
11493 __isl_take isl_space *model)
11494{
11495 return isl_map_align_params(set, model);
11496}
11497
11498/* Align the parameters of "bmap" to those of "model", introducing
11499 * additional parameters if needed.
11500 */
11501__isl_give isl_basic_map *isl_basic_map_align_params(
11502 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
11503{
11504 isl_ctx *ctx;
11505
11506 if (!bmap || !model)
11507 goto error;
11508
11509 ctx = isl_space_get_ctx(model);
11510 if (!isl_space_has_named_params(model))
11511 isl_die(ctx, isl_error_invalid,
11512 "model has unnamed parameters", goto error);
11513 if (!isl_space_has_named_params(bmap->dim))
11514 isl_die(ctx, isl_error_invalid,
11515 "relation has unnamed parameters", goto error);
11516 if (!isl_space_match(bmap->dim, isl_dim_param, model, isl_dim_param)) {
11517 isl_reordering *exp;
11518 struct isl_dim_map *dim_map;
11519
11520 model = isl_space_drop_dims(model, isl_dim_in,
11521 0, isl_space_dim(model, isl_dim_in));
11522 model = isl_space_drop_dims(model, isl_dim_out,
11523 0, isl_space_dim(model, isl_dim_out));
11524 exp = isl_parameter_alignment_reordering(bmap->dim, model);
11525 exp = isl_reordering_extend_space(exp,
11526 isl_basic_map_get_space(bmap));
11527 dim_map = isl_dim_map_from_reordering(exp);
11528 bmap = isl_basic_map_realign(bmap,
11529 exp ? isl_space_copy(exp->dim) : NULL,
11530 isl_dim_map_extend(dim_map, bmap));
11531 isl_reordering_free(exp);
11532 free(dim_map);
11533 }
11534
11535 isl_space_free(model);
11536 return bmap;
11537error:
11538 isl_space_free(model);
11539 isl_basic_map_free(bmap);
11540 return NULL;
11541}
11542
11543/* Align the parameters of "bset" to those of "model", introducing
11544 * additional parameters if needed.
11545 */
11546__isl_give isl_basic_set *isl_basic_set_align_params(
11547 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
11548{
11549 return isl_basic_map_align_params(bset, model);
11550}
11551
11552__isl_give isl_mat *isl_basic_map_equalities_matrix(
11553 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11554 enum isl_dim_type c2, enum isl_dim_type c3,
11555 enum isl_dim_type c4, enum isl_dim_type c5)
11556{
11557 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11558 struct isl_mat *mat;
11559 int i, j, k;
11560 int pos;
11561
11562 if (!bmap)
11563 return NULL;
11564 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
11565 isl_basic_map_total_dim(bmap) + 1);
11566 if (!mat)
11567 return NULL;
11568 for (i = 0; i < bmap->n_eq; ++i)
11569 for (j = 0, pos = 0; j < 5; ++j) {
11570 int off = isl_basic_map_offset(bmap, c[j]);
11571 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11572 isl_int_set(mat->row[i][pos],
11573 bmap->eq[i][off + k]);
11574 ++pos;
11575 }
11576 }
11577
11578 return mat;
11579}
11580
11581__isl_give isl_mat *isl_basic_map_inequalities_matrix(
11582 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11583 enum isl_dim_type c2, enum isl_dim_type c3,
11584 enum isl_dim_type c4, enum isl_dim_type c5)
11585{
11586 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11587 struct isl_mat *mat;
11588 int i, j, k;
11589 int pos;
11590
11591 if (!bmap)
11592 return NULL;
11593 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
11594 isl_basic_map_total_dim(bmap) + 1);
11595 if (!mat)
11596 return NULL;
11597 for (i = 0; i < bmap->n_ineq; ++i)
11598 for (j = 0, pos = 0; j < 5; ++j) {
11599 int off = isl_basic_map_offset(bmap, c[j]);
11600 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11601 isl_int_set(mat->row[i][pos],
11602 bmap->ineq[i][off + k]);
11603 ++pos;
11604 }
11605 }
11606
11607 return mat;
11608}
11609
11610__isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
11611 __isl_take isl_space *dim,
11612 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11613 enum isl_dim_type c2, enum isl_dim_type c3,
11614 enum isl_dim_type c4, enum isl_dim_type c5)
11615{
11616 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11617 isl_basic_map *bmap;
11618 unsigned total;
11619 unsigned extra;
11620 int i, j, k, l;
11621 int pos;
11622
11623 if (!dim || !eq || !ineq)
11624 goto error;
11625
11626 if (eq->n_col != ineq->n_col)
11627 isl_die(dim->ctx, isl_error_invalid,
11628 "equalities and inequalities matrices should have "
11629 "same number of columns", goto error);
11630
11631 total = 1 + isl_space_dim(dim, isl_dim_all);
11632
11633 if (eq->n_col < total)
11634 isl_die(dim->ctx, isl_error_invalid,
11635 "number of columns too small", goto error);
11636
11637 extra = eq->n_col - total;
11638
11639 bmap = isl_basic_map_alloc_space(isl_space_copy(dim), extra,
11640 eq->n_row, ineq->n_row);
11641 if (!bmap)
11642 goto error;
11643 for (i = 0; i < extra; ++i) {
11644 k = isl_basic_map_alloc_div(bmap);
11645 if (k < 0)
11646 goto error;
11647 isl_int_set_si(bmap->div[k][0], 0);
11648 }
11649 for (i = 0; i < eq->n_row; ++i) {
11650 l = isl_basic_map_alloc_equality(bmap);
11651 if (l < 0)
11652 goto error;
11653 for (j = 0, pos = 0; j < 5; ++j) {
11654 int off = isl_basic_map_offset(bmap, c[j]);
11655 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11656 isl_int_set(bmap->eq[l][off + k],
11657 eq->row[i][pos]);
11658 ++pos;
11659 }
11660 }
11661 }
11662 for (i = 0; i < ineq->n_row; ++i) {
11663 l = isl_basic_map_alloc_inequality(bmap);
11664 if (l < 0)
11665 goto error;
11666 for (j = 0, pos = 0; j < 5; ++j) {
11667 int off = isl_basic_map_offset(bmap, c[j]);
11668 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11669 isl_int_set(bmap->ineq[l][off + k],
11670 ineq->row[i][pos]);
11671 ++pos;
11672 }
11673 }
11674 }
11675
11676 isl_space_free(dim);
11677 isl_mat_free(eq);
11678 isl_mat_free(ineq);
11679
11680 bmap = isl_basic_map_simplify(bmap);
11681 return isl_basic_map_finalize(bmap);
11682error:
11683 isl_space_free(dim);
11684 isl_mat_free(eq);
11685 isl_mat_free(ineq);
11686 return NULL;
11687}
11688
11689__isl_give isl_mat *isl_basic_set_equalities_matrix(
11690 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11691 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11692{
Tobias Grosser06e15922016-11-16 11:06:47 +000011693 return isl_basic_map_equalities_matrix(bset_to_bmap(bset),
Tobias Grosser52a25232015-02-04 20:55:43 +000011694 c1, c2, c3, c4, isl_dim_in);
11695}
11696
11697__isl_give isl_mat *isl_basic_set_inequalities_matrix(
11698 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11699 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11700{
Tobias Grosser06e15922016-11-16 11:06:47 +000011701 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset),
Tobias Grosser52a25232015-02-04 20:55:43 +000011702 c1, c2, c3, c4, isl_dim_in);
11703}
11704
11705__isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
11706 __isl_take isl_space *dim,
11707 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11708 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11709{
Tobias Grosser06e15922016-11-16 11:06:47 +000011710 isl_basic_map *bmap;
11711 bmap = isl_basic_map_from_constraint_matrices(dim, eq, ineq,
Tobias Grosser52a25232015-02-04 20:55:43 +000011712 c1, c2, c3, c4, isl_dim_in);
Tobias Grosser06e15922016-11-16 11:06:47 +000011713 return bset_from_bmap(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +000011714}
11715
Tobias Grosserb2f39922015-05-28 13:32:11 +000011716isl_bool isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
Tobias Grosser52a25232015-02-04 20:55:43 +000011717{
11718 if (!bmap)
Tobias Grosserb2f39922015-05-28 13:32:11 +000011719 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000011720
11721 return isl_space_can_zip(bmap->dim);
11722}
11723
Tobias Grosserb2f39922015-05-28 13:32:11 +000011724isl_bool isl_map_can_zip(__isl_keep isl_map *map)
Tobias Grosser52a25232015-02-04 20:55:43 +000011725{
11726 if (!map)
Tobias Grosserb2f39922015-05-28 13:32:11 +000011727 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000011728
11729 return isl_space_can_zip(map->dim);
11730}
11731
11732/* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11733 * (A -> C) -> (B -> D).
11734 */
11735__isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
11736{
11737 unsigned pos;
11738 unsigned n1;
11739 unsigned n2;
11740
11741 if (!bmap)
11742 return NULL;
11743
11744 if (!isl_basic_map_can_zip(bmap))
11745 isl_die(bmap->ctx, isl_error_invalid,
11746 "basic map cannot be zipped", goto error);
11747 pos = isl_basic_map_offset(bmap, isl_dim_in) +
11748 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
11749 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
11750 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
11751 bmap = isl_basic_map_cow(bmap);
11752 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
11753 if (!bmap)
11754 return NULL;
11755 bmap->dim = isl_space_zip(bmap->dim);
11756 if (!bmap->dim)
11757 goto error;
Michael Kruse959a8dc2016-01-15 15:54:45 +000011758 bmap = isl_basic_map_mark_final(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +000011759 return bmap;
11760error:
11761 isl_basic_map_free(bmap);
11762 return NULL;
11763}
11764
11765/* Given a map (A -> B) -> (C -> D), return the corresponding map
11766 * (A -> C) -> (B -> D).
11767 */
11768__isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
11769{
11770 int i;
11771
11772 if (!map)
11773 return NULL;
11774
11775 if (!isl_map_can_zip(map))
11776 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
11777 goto error);
11778
11779 map = isl_map_cow(map);
11780 if (!map)
11781 return NULL;
11782
11783 for (i = 0; i < map->n; ++i) {
11784 map->p[i] = isl_basic_map_zip(map->p[i]);
11785 if (!map->p[i])
11786 goto error;
11787 }
11788
11789 map->dim = isl_space_zip(map->dim);
11790 if (!map->dim)
11791 goto error;
11792
11793 return map;
11794error:
11795 isl_map_free(map);
11796 return NULL;
11797}
11798
11799/* Can we apply isl_basic_map_curry to "bmap"?
11800 * That is, does it have a nested relation in its domain?
11801 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000011802isl_bool isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
Tobias Grosser52a25232015-02-04 20:55:43 +000011803{
11804 if (!bmap)
Tobias Grosserb2f39922015-05-28 13:32:11 +000011805 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000011806
11807 return isl_space_can_curry(bmap->dim);
11808}
11809
11810/* Can we apply isl_map_curry to "map"?
11811 * That is, does it have a nested relation in its domain?
11812 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000011813isl_bool isl_map_can_curry(__isl_keep isl_map *map)
Tobias Grosser52a25232015-02-04 20:55:43 +000011814{
11815 if (!map)
Tobias Grosserb2f39922015-05-28 13:32:11 +000011816 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000011817
11818 return isl_space_can_curry(map->dim);
11819}
11820
11821/* Given a basic map (A -> B) -> C, return the corresponding basic map
11822 * A -> (B -> C).
11823 */
11824__isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
11825{
11826
11827 if (!bmap)
11828 return NULL;
11829
11830 if (!isl_basic_map_can_curry(bmap))
11831 isl_die(bmap->ctx, isl_error_invalid,
11832 "basic map cannot be curried", goto error);
11833 bmap = isl_basic_map_cow(bmap);
11834 if (!bmap)
11835 return NULL;
11836 bmap->dim = isl_space_curry(bmap->dim);
11837 if (!bmap->dim)
11838 goto error;
Michael Kruse959a8dc2016-01-15 15:54:45 +000011839 bmap = isl_basic_map_mark_final(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +000011840 return bmap;
11841error:
11842 isl_basic_map_free(bmap);
11843 return NULL;
11844}
11845
11846/* Given a map (A -> B) -> C, return the corresponding map
11847 * A -> (B -> C).
11848 */
11849__isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
11850{
Michael Kruse959a8dc2016-01-15 15:54:45 +000011851 return isl_map_change_space(map, &isl_map_can_curry,
11852 "map cannot be curried", &isl_space_curry);
11853}
Tobias Grosser52a25232015-02-04 20:55:43 +000011854
Michael Kruse959a8dc2016-01-15 15:54:45 +000011855/* Can isl_map_range_curry be applied to "map"?
11856 * That is, does it have a nested relation in its range,
11857 * the domain of which is itself a nested relation?
11858 */
11859isl_bool isl_map_can_range_curry(__isl_keep isl_map *map)
11860{
Tobias Grosser52a25232015-02-04 20:55:43 +000011861 if (!map)
Michael Kruse959a8dc2016-01-15 15:54:45 +000011862 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000011863
Michael Kruse959a8dc2016-01-15 15:54:45 +000011864 return isl_space_can_range_curry(map->dim);
11865}
Tobias Grosser52a25232015-02-04 20:55:43 +000011866
Michael Kruse959a8dc2016-01-15 15:54:45 +000011867/* Given a map A -> ((B -> C) -> D), return the corresponding map
11868 * A -> (B -> (C -> D)).
11869 */
11870__isl_give isl_map *isl_map_range_curry(__isl_take isl_map *map)
11871{
11872 return isl_map_change_space(map, &isl_map_can_range_curry,
11873 "map range cannot be curried",
11874 &isl_space_range_curry);
Tobias Grosser52a25232015-02-04 20:55:43 +000011875}
11876
11877/* Can we apply isl_basic_map_uncurry to "bmap"?
11878 * That is, does it have a nested relation in its domain?
11879 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000011880isl_bool isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
Tobias Grosser52a25232015-02-04 20:55:43 +000011881{
11882 if (!bmap)
Tobias Grosserb2f39922015-05-28 13:32:11 +000011883 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000011884
11885 return isl_space_can_uncurry(bmap->dim);
11886}
11887
11888/* Can we apply isl_map_uncurry to "map"?
11889 * That is, does it have a nested relation in its domain?
11890 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000011891isl_bool isl_map_can_uncurry(__isl_keep isl_map *map)
Tobias Grosser52a25232015-02-04 20:55:43 +000011892{
11893 if (!map)
Tobias Grosserb2f39922015-05-28 13:32:11 +000011894 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000011895
11896 return isl_space_can_uncurry(map->dim);
11897}
11898
11899/* Given a basic map A -> (B -> C), return the corresponding basic map
11900 * (A -> B) -> C.
11901 */
11902__isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
11903{
11904
11905 if (!bmap)
11906 return NULL;
11907
11908 if (!isl_basic_map_can_uncurry(bmap))
11909 isl_die(bmap->ctx, isl_error_invalid,
11910 "basic map cannot be uncurried",
11911 return isl_basic_map_free(bmap));
11912 bmap = isl_basic_map_cow(bmap);
11913 if (!bmap)
11914 return NULL;
11915 bmap->dim = isl_space_uncurry(bmap->dim);
11916 if (!bmap->dim)
11917 return isl_basic_map_free(bmap);
Michael Kruse959a8dc2016-01-15 15:54:45 +000011918 bmap = isl_basic_map_mark_final(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +000011919 return bmap;
11920}
11921
11922/* Given a map A -> (B -> C), return the corresponding map
11923 * (A -> B) -> C.
11924 */
11925__isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
11926{
Michael Kruse959a8dc2016-01-15 15:54:45 +000011927 return isl_map_change_space(map, &isl_map_can_uncurry,
11928 "map cannot be uncurried", &isl_space_uncurry);
Tobias Grosser52a25232015-02-04 20:55:43 +000011929}
11930
11931/* Construct a basic map mapping the domain of the affine expression
11932 * to a one-dimensional range prescribed by the affine expression.
Tobias Grosser5652c9c2016-10-01 19:46:51 +000011933 * If "rational" is set, then construct a rational basic map.
Tobias Grosser37034db2016-03-25 19:38:18 +000011934 *
11935 * A NaN affine expression cannot be converted to a basic map.
Tobias Grosser52a25232015-02-04 20:55:43 +000011936 */
Tobias Grosser5652c9c2016-10-01 19:46:51 +000011937static __isl_give isl_basic_map *isl_basic_map_from_aff2(
11938 __isl_take isl_aff *aff, int rational)
Tobias Grosser52a25232015-02-04 20:55:43 +000011939{
11940 int k;
11941 int pos;
Tobias Grosser37034db2016-03-25 19:38:18 +000011942 isl_bool is_nan;
Tobias Grosser52a25232015-02-04 20:55:43 +000011943 isl_local_space *ls;
Tobias Grosser37034db2016-03-25 19:38:18 +000011944 isl_basic_map *bmap = NULL;
Tobias Grosser52a25232015-02-04 20:55:43 +000011945
11946 if (!aff)
11947 return NULL;
Tobias Grosser37034db2016-03-25 19:38:18 +000011948 is_nan = isl_aff_is_nan(aff);
11949 if (is_nan < 0)
11950 goto error;
11951 if (is_nan)
11952 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
11953 "cannot convert NaN", goto error);
Tobias Grosser52a25232015-02-04 20:55:43 +000011954
11955 ls = isl_aff_get_local_space(aff);
11956 bmap = isl_basic_map_from_local_space(ls);
11957 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
11958 k = isl_basic_map_alloc_equality(bmap);
11959 if (k < 0)
11960 goto error;
11961
11962 pos = isl_basic_map_offset(bmap, isl_dim_out);
11963 isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
11964 isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
11965 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
11966 aff->v->size - (pos + 1));
11967
11968 isl_aff_free(aff);
Tobias Grosser5652c9c2016-10-01 19:46:51 +000011969 if (rational)
11970 bmap = isl_basic_map_set_rational(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +000011971 bmap = isl_basic_map_finalize(bmap);
11972 return bmap;
11973error:
11974 isl_aff_free(aff);
11975 isl_basic_map_free(bmap);
11976 return NULL;
11977}
11978
Tobias Grosser5652c9c2016-10-01 19:46:51 +000011979/* Construct a basic map mapping the domain of the affine expression
11980 * to a one-dimensional range prescribed by the affine expression.
11981 */
11982__isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
11983{
11984 return isl_basic_map_from_aff2(aff, 0);
11985}
11986
Tobias Grosser52a25232015-02-04 20:55:43 +000011987/* Construct a map mapping the domain of the affine expression
11988 * to a one-dimensional range prescribed by the affine expression.
11989 */
11990__isl_give isl_map *isl_map_from_aff(__isl_take isl_aff *aff)
11991{
11992 isl_basic_map *bmap;
11993
11994 bmap = isl_basic_map_from_aff(aff);
11995 return isl_map_from_basic_map(bmap);
11996}
11997
11998/* Construct a basic map mapping the domain the multi-affine expression
11999 * to its range, with each dimension in the range equated to the
12000 * corresponding affine expression.
Tobias Grosser5652c9c2016-10-01 19:46:51 +000012001 * If "rational" is set, then construct a rational basic map.
Tobias Grosser52a25232015-02-04 20:55:43 +000012002 */
Tobias Grosser5652c9c2016-10-01 19:46:51 +000012003__isl_give isl_basic_map *isl_basic_map_from_multi_aff2(
12004 __isl_take isl_multi_aff *maff, int rational)
Tobias Grosser52a25232015-02-04 20:55:43 +000012005{
12006 int i;
12007 isl_space *space;
12008 isl_basic_map *bmap;
12009
12010 if (!maff)
12011 return NULL;
12012
12013 if (isl_space_dim(maff->space, isl_dim_out) != maff->n)
12014 isl_die(isl_multi_aff_get_ctx(maff), isl_error_internal,
12015 "invalid space", goto error);
12016
12017 space = isl_space_domain(isl_multi_aff_get_space(maff));
12018 bmap = isl_basic_map_universe(isl_space_from_domain(space));
Tobias Grosser5652c9c2016-10-01 19:46:51 +000012019 if (rational)
12020 bmap = isl_basic_map_set_rational(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +000012021
12022 for (i = 0; i < maff->n; ++i) {
12023 isl_aff *aff;
12024 isl_basic_map *bmap_i;
12025
12026 aff = isl_aff_copy(maff->p[i]);
Tobias Grosser5652c9c2016-10-01 19:46:51 +000012027 bmap_i = isl_basic_map_from_aff2(aff, rational);
Tobias Grosser52a25232015-02-04 20:55:43 +000012028
12029 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
12030 }
12031
12032 bmap = isl_basic_map_reset_space(bmap, isl_multi_aff_get_space(maff));
12033
12034 isl_multi_aff_free(maff);
12035 return bmap;
12036error:
12037 isl_multi_aff_free(maff);
12038 return NULL;
12039}
12040
Tobias Grosser5652c9c2016-10-01 19:46:51 +000012041/* Construct a basic map mapping the domain the multi-affine expression
12042 * to its range, with each dimension in the range equated to the
12043 * corresponding affine expression.
12044 */
12045__isl_give isl_basic_map *isl_basic_map_from_multi_aff(
12046 __isl_take isl_multi_aff *ma)
12047{
12048 return isl_basic_map_from_multi_aff2(ma, 0);
12049}
12050
Tobias Grosser52a25232015-02-04 20:55:43 +000012051/* Construct a map mapping the domain the multi-affine expression
12052 * to its range, with each dimension in the range equated to the
12053 * corresponding affine expression.
12054 */
12055__isl_give isl_map *isl_map_from_multi_aff(__isl_take isl_multi_aff *maff)
12056{
12057 isl_basic_map *bmap;
12058
12059 bmap = isl_basic_map_from_multi_aff(maff);
12060 return isl_map_from_basic_map(bmap);
12061}
12062
12063/* Construct a basic map mapping a domain in the given space to
12064 * to an n-dimensional range, with n the number of elements in the list,
12065 * where each coordinate in the range is prescribed by the
12066 * corresponding affine expression.
12067 * The domains of all affine expressions in the list are assumed to match
12068 * domain_dim.
12069 */
12070__isl_give isl_basic_map *isl_basic_map_from_aff_list(
12071 __isl_take isl_space *domain_dim, __isl_take isl_aff_list *list)
12072{
12073 int i;
12074 isl_space *dim;
12075 isl_basic_map *bmap;
12076
12077 if (!list)
12078 return NULL;
12079
12080 dim = isl_space_from_domain(domain_dim);
12081 bmap = isl_basic_map_universe(dim);
12082
12083 for (i = 0; i < list->n; ++i) {
12084 isl_aff *aff;
12085 isl_basic_map *bmap_i;
12086
12087 aff = isl_aff_copy(list->p[i]);
12088 bmap_i = isl_basic_map_from_aff(aff);
12089
12090 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
12091 }
12092
12093 isl_aff_list_free(list);
12094 return bmap;
12095}
12096
12097__isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
12098 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12099{
12100 return isl_map_equate(set, type1, pos1, type2, pos2);
12101}
12102
12103/* Construct a basic map where the given dimensions are equal to each other.
12104 */
12105static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
12106 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12107{
12108 isl_basic_map *bmap = NULL;
12109 int i;
12110
12111 if (!space)
12112 return NULL;
12113
12114 if (pos1 >= isl_space_dim(space, type1))
12115 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12116 "index out of bounds", goto error);
12117 if (pos2 >= isl_space_dim(space, type2))
12118 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12119 "index out of bounds", goto error);
12120
12121 if (type1 == type2 && pos1 == pos2)
12122 return isl_basic_map_universe(space);
12123
12124 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
12125 i = isl_basic_map_alloc_equality(bmap);
12126 if (i < 0)
12127 goto error;
12128 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12129 pos1 += isl_basic_map_offset(bmap, type1);
12130 pos2 += isl_basic_map_offset(bmap, type2);
12131 isl_int_set_si(bmap->eq[i][pos1], -1);
12132 isl_int_set_si(bmap->eq[i][pos2], 1);
12133 bmap = isl_basic_map_finalize(bmap);
12134 isl_space_free(space);
12135 return bmap;
12136error:
12137 isl_space_free(space);
12138 isl_basic_map_free(bmap);
12139 return NULL;
12140}
12141
12142/* Add a constraint imposing that the given two dimensions are equal.
12143 */
12144__isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
12145 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12146{
12147 isl_basic_map *eq;
12148
12149 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12150
12151 bmap = isl_basic_map_intersect(bmap, eq);
12152
12153 return bmap;
12154}
12155
12156/* Add a constraint imposing that the given two dimensions are equal.
12157 */
12158__isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
12159 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12160{
12161 isl_basic_map *bmap;
12162
12163 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
12164
12165 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12166
12167 return map;
12168}
12169
12170/* Add a constraint imposing that the given two dimensions have opposite values.
12171 */
12172__isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
12173 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12174{
12175 isl_basic_map *bmap = NULL;
12176 int i;
12177
12178 if (!map)
12179 return NULL;
12180
12181 if (pos1 >= isl_map_dim(map, type1))
12182 isl_die(map->ctx, isl_error_invalid,
12183 "index out of bounds", goto error);
12184 if (pos2 >= isl_map_dim(map, type2))
12185 isl_die(map->ctx, isl_error_invalid,
12186 "index out of bounds", goto error);
12187
12188 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
12189 i = isl_basic_map_alloc_equality(bmap);
12190 if (i < 0)
12191 goto error;
12192 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12193 pos1 += isl_basic_map_offset(bmap, type1);
12194 pos2 += isl_basic_map_offset(bmap, type2);
12195 isl_int_set_si(bmap->eq[i][pos1], 1);
12196 isl_int_set_si(bmap->eq[i][pos2], 1);
12197 bmap = isl_basic_map_finalize(bmap);
12198
12199 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12200
12201 return map;
12202error:
12203 isl_basic_map_free(bmap);
12204 isl_map_free(map);
12205 return NULL;
12206}
12207
12208/* Construct a constraint imposing that the value of the first dimension is
12209 * greater than or equal to that of the second.
12210 */
12211static __isl_give isl_constraint *constraint_order_ge(
12212 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
12213 enum isl_dim_type type2, int pos2)
12214{
12215 isl_constraint *c;
12216
12217 if (!space)
12218 return NULL;
12219
Tobias Grosserb2f39922015-05-28 13:32:11 +000012220 c = isl_constraint_alloc_inequality(isl_local_space_from_space(space));
Tobias Grosser52a25232015-02-04 20:55:43 +000012221
12222 if (pos1 >= isl_constraint_dim(c, type1))
12223 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
12224 "index out of bounds", return isl_constraint_free(c));
12225 if (pos2 >= isl_constraint_dim(c, type2))
12226 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
12227 "index out of bounds", return isl_constraint_free(c));
12228
12229 if (type1 == type2 && pos1 == pos2)
12230 return c;
12231
12232 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
12233 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
12234
12235 return c;
12236}
12237
12238/* Add a constraint imposing that the value of the first dimension is
12239 * greater than or equal to that of the second.
12240 */
12241__isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
12242 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12243{
12244 isl_constraint *c;
12245 isl_space *space;
12246
12247 if (type1 == type2 && pos1 == pos2)
12248 return bmap;
12249 space = isl_basic_map_get_space(bmap);
12250 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12251 bmap = isl_basic_map_add_constraint(bmap, c);
12252
12253 return bmap;
12254}
12255
12256/* Add a constraint imposing that the value of the first dimension is
12257 * greater than or equal to that of the second.
12258 */
12259__isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
12260 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12261{
12262 isl_constraint *c;
12263 isl_space *space;
12264
12265 if (type1 == type2 && pos1 == pos2)
12266 return map;
12267 space = isl_map_get_space(map);
12268 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12269 map = isl_map_add_constraint(map, c);
12270
12271 return map;
12272}
12273
12274/* Add a constraint imposing that the value of the first dimension is
12275 * less than or equal to that of the second.
12276 */
12277__isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
12278 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12279{
12280 return isl_map_order_ge(map, type2, pos2, type1, pos1);
12281}
12282
12283/* Construct a basic map where the value of the first dimension is
12284 * greater than that of the second.
12285 */
12286static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
12287 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12288{
12289 isl_basic_map *bmap = NULL;
12290 int i;
12291
12292 if (!space)
12293 return NULL;
12294
12295 if (pos1 >= isl_space_dim(space, type1))
12296 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12297 "index out of bounds", goto error);
12298 if (pos2 >= isl_space_dim(space, type2))
12299 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12300 "index out of bounds", goto error);
12301
12302 if (type1 == type2 && pos1 == pos2)
12303 return isl_basic_map_empty(space);
12304
12305 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
12306 i = isl_basic_map_alloc_inequality(bmap);
12307 if (i < 0)
12308 return isl_basic_map_free(bmap);
12309 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
12310 pos1 += isl_basic_map_offset(bmap, type1);
12311 pos2 += isl_basic_map_offset(bmap, type2);
12312 isl_int_set_si(bmap->ineq[i][pos1], 1);
12313 isl_int_set_si(bmap->ineq[i][pos2], -1);
12314 isl_int_set_si(bmap->ineq[i][0], -1);
12315 bmap = isl_basic_map_finalize(bmap);
12316
12317 return bmap;
12318error:
12319 isl_space_free(space);
12320 isl_basic_map_free(bmap);
12321 return NULL;
12322}
12323
12324/* Add a constraint imposing that the value of the first dimension is
12325 * greater than that of the second.
12326 */
12327__isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
12328 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12329{
12330 isl_basic_map *gt;
12331
12332 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12333
12334 bmap = isl_basic_map_intersect(bmap, gt);
12335
12336 return bmap;
12337}
12338
12339/* Add a constraint imposing that the value of the first dimension is
12340 * greater than that of the second.
12341 */
12342__isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
12343 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12344{
12345 isl_basic_map *bmap;
12346
12347 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
12348
12349 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12350
12351 return map;
12352}
12353
12354/* Add a constraint imposing that the value of the first dimension is
12355 * smaller than that of the second.
12356 */
12357__isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
12358 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12359{
12360 return isl_map_order_gt(map, type2, pos2, type1, pos1);
12361}
12362
12363__isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
12364 int pos)
12365{
12366 isl_aff *div;
12367 isl_local_space *ls;
12368
12369 if (!bmap)
12370 return NULL;
12371
12372 if (!isl_basic_map_divs_known(bmap))
12373 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12374 "some divs are unknown", return NULL);
12375
12376 ls = isl_basic_map_get_local_space(bmap);
12377 div = isl_local_space_get_div(ls, pos);
12378 isl_local_space_free(ls);
12379
12380 return div;
12381}
12382
12383__isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
12384 int pos)
12385{
12386 return isl_basic_map_get_div(bset, pos);
12387}
12388
12389/* Plug in "subs" for dimension "type", "pos" of "bset".
12390 *
12391 * Let i be the dimension to replace and let "subs" be of the form
12392 *
12393 * f/d
12394 *
12395 * Any integer division with a non-zero coefficient for i,
12396 *
12397 * floor((a i + g)/m)
12398 *
12399 * is replaced by
12400 *
12401 * floor((a f + d g)/(m d))
12402 *
12403 * Constraints of the form
12404 *
12405 * a i + g
12406 *
12407 * are replaced by
12408 *
12409 * a f + d g
12410 *
12411 * We currently require that "subs" is an integral expression.
12412 * Handling rational expressions may require us to add stride constraints
12413 * as we do in isl_basic_set_preimage_multi_aff.
12414 */
12415__isl_give isl_basic_set *isl_basic_set_substitute(
12416 __isl_take isl_basic_set *bset,
12417 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12418{
12419 int i;
12420 isl_int v;
12421 isl_ctx *ctx;
12422
12423 if (bset && isl_basic_set_plain_is_empty(bset))
12424 return bset;
12425
12426 bset = isl_basic_set_cow(bset);
12427 if (!bset || !subs)
12428 goto error;
12429
12430 ctx = isl_basic_set_get_ctx(bset);
12431 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
12432 isl_die(ctx, isl_error_invalid,
12433 "spaces don't match", goto error);
12434 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
12435 isl_die(ctx, isl_error_unsupported,
12436 "cannot handle divs yet", goto error);
12437 if (!isl_int_is_one(subs->v->el[0]))
12438 isl_die(ctx, isl_error_invalid,
12439 "can only substitute integer expressions", goto error);
12440
12441 pos += isl_basic_set_offset(bset, type);
12442
12443 isl_int_init(v);
12444
12445 for (i = 0; i < bset->n_eq; ++i) {
12446 if (isl_int_is_zero(bset->eq[i][pos]))
12447 continue;
12448 isl_int_set(v, bset->eq[i][pos]);
12449 isl_int_set_si(bset->eq[i][pos], 0);
12450 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
12451 v, subs->v->el + 1, subs->v->size - 1);
12452 }
12453
12454 for (i = 0; i < bset->n_ineq; ++i) {
12455 if (isl_int_is_zero(bset->ineq[i][pos]))
12456 continue;
12457 isl_int_set(v, bset->ineq[i][pos]);
12458 isl_int_set_si(bset->ineq[i][pos], 0);
12459 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
12460 v, subs->v->el + 1, subs->v->size - 1);
12461 }
12462
12463 for (i = 0; i < bset->n_div; ++i) {
12464 if (isl_int_is_zero(bset->div[i][1 + pos]))
12465 continue;
12466 isl_int_set(v, bset->div[i][1 + pos]);
12467 isl_int_set_si(bset->div[i][1 + pos], 0);
12468 isl_seq_combine(bset->div[i] + 1,
12469 subs->v->el[0], bset->div[i] + 1,
12470 v, subs->v->el + 1, subs->v->size - 1);
12471 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
12472 }
12473
12474 isl_int_clear(v);
12475
12476 bset = isl_basic_set_simplify(bset);
12477 return isl_basic_set_finalize(bset);
12478error:
12479 isl_basic_set_free(bset);
12480 return NULL;
12481}
12482
12483/* Plug in "subs" for dimension "type", "pos" of "set".
12484 */
12485__isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
12486 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12487{
12488 int i;
12489
12490 if (set && isl_set_plain_is_empty(set))
12491 return set;
12492
12493 set = isl_set_cow(set);
12494 if (!set || !subs)
12495 goto error;
12496
12497 for (i = set->n - 1; i >= 0; --i) {
12498 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
12499 if (remove_if_empty(set, i) < 0)
12500 goto error;
12501 }
12502
12503 return set;
12504error:
12505 isl_set_free(set);
12506 return NULL;
12507}
12508
12509/* Check if the range of "ma" is compatible with the domain or range
12510 * (depending on "type") of "bmap".
12511 * Return -1 if anything is wrong.
12512 */
12513static int check_basic_map_compatible_range_multi_aff(
12514 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
12515 __isl_keep isl_multi_aff *ma)
12516{
12517 int m;
12518 isl_space *ma_space;
12519
12520 ma_space = isl_multi_aff_get_space(ma);
12521
12522 m = isl_space_match(bmap->dim, isl_dim_param, ma_space, isl_dim_param);
12523 if (m < 0)
12524 goto error;
12525 if (!m)
12526 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12527 "parameters don't match", goto error);
12528 m = isl_space_tuple_is_equal(bmap->dim, type, ma_space, isl_dim_out);
12529 if (m < 0)
12530 goto error;
12531 if (!m)
12532 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12533 "spaces don't match", goto error);
12534
12535 isl_space_free(ma_space);
12536 return m;
12537error:
12538 isl_space_free(ma_space);
12539 return -1;
12540}
12541
12542/* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12543 * coefficients before the transformed range of dimensions,
12544 * the "n_after" coefficients after the transformed range of dimensions
12545 * and the coefficients of the other divs in "bmap".
12546 */
12547static int set_ma_divs(__isl_keep isl_basic_map *bmap,
12548 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
12549{
12550 int i;
12551 int n_param;
12552 int n_set;
12553 isl_local_space *ls;
12554
12555 if (n_div == 0)
12556 return 0;
12557
12558 ls = isl_aff_get_domain_local_space(ma->p[0]);
12559 if (!ls)
12560 return -1;
12561
12562 n_param = isl_local_space_dim(ls, isl_dim_param);
12563 n_set = isl_local_space_dim(ls, isl_dim_set);
12564 for (i = 0; i < n_div; ++i) {
12565 int o_bmap = 0, o_ls = 0;
12566
12567 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
12568 o_bmap += 1 + 1 + n_param;
12569 o_ls += 1 + 1 + n_param;
12570 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
12571 o_bmap += n_before;
12572 isl_seq_cpy(bmap->div[i] + o_bmap,
12573 ls->div->row[i] + o_ls, n_set);
12574 o_bmap += n_set;
12575 o_ls += n_set;
12576 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
12577 o_bmap += n_after;
12578 isl_seq_cpy(bmap->div[i] + o_bmap,
12579 ls->div->row[i] + o_ls, n_div);
12580 o_bmap += n_div;
12581 o_ls += n_div;
12582 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
12583 if (isl_basic_set_add_div_constraints(bmap, i) < 0)
12584 goto error;
12585 }
12586
12587 isl_local_space_free(ls);
12588 return 0;
12589error:
12590 isl_local_space_free(ls);
12591 return -1;
12592}
12593
12594/* How many stride constraints does "ma" enforce?
12595 * That is, how many of the affine expressions have a denominator
12596 * different from one?
12597 */
12598static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
12599{
12600 int i;
12601 int strides = 0;
12602
12603 for (i = 0; i < ma->n; ++i)
12604 if (!isl_int_is_one(ma->p[i]->v->el[0]))
12605 strides++;
12606
12607 return strides;
12608}
12609
12610/* For each affine expression in ma of the form
12611 *
12612 * x_i = (f_i y + h_i)/m_i
12613 *
12614 * with m_i different from one, add a constraint to "bmap"
12615 * of the form
12616 *
12617 * f_i y + h_i = m_i alpha_i
12618 *
12619 * with alpha_i an additional existentially quantified variable.
Tobias Grossera67ac972016-02-26 11:35:12 +000012620 *
12621 * The input variables of "ma" correspond to a subset of the variables
12622 * of "bmap". There are "n_before" variables in "bmap" before this
12623 * subset and "n_after" variables after this subset.
12624 * The integer divisions of the affine expressions in "ma" are assumed
12625 * to have been aligned. There are "n_div_ma" of them and
12626 * they appear first in "bmap", straight after the "n_after" variables.
Tobias Grosser52a25232015-02-04 20:55:43 +000012627 */
12628static __isl_give isl_basic_map *add_ma_strides(
12629 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
Tobias Grossera67ac972016-02-26 11:35:12 +000012630 int n_before, int n_after, int n_div_ma)
Tobias Grosser52a25232015-02-04 20:55:43 +000012631{
12632 int i, k;
12633 int div;
12634 int total;
12635 int n_param;
12636 int n_in;
Tobias Grosser52a25232015-02-04 20:55:43 +000012637
12638 total = isl_basic_map_total_dim(bmap);
12639 n_param = isl_multi_aff_dim(ma, isl_dim_param);
12640 n_in = isl_multi_aff_dim(ma, isl_dim_in);
Tobias Grosser52a25232015-02-04 20:55:43 +000012641 for (i = 0; i < ma->n; ++i) {
12642 int o_bmap = 0, o_ma = 1;
12643
12644 if (isl_int_is_one(ma->p[i]->v->el[0]))
12645 continue;
12646 div = isl_basic_map_alloc_div(bmap);
12647 k = isl_basic_map_alloc_equality(bmap);
12648 if (div < 0 || k < 0)
12649 goto error;
12650 isl_int_set_si(bmap->div[div][0], 0);
12651 isl_seq_cpy(bmap->eq[k] + o_bmap,
12652 ma->p[i]->v->el + o_ma, 1 + n_param);
12653 o_bmap += 1 + n_param;
12654 o_ma += 1 + n_param;
12655 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
12656 o_bmap += n_before;
12657 isl_seq_cpy(bmap->eq[k] + o_bmap,
12658 ma->p[i]->v->el + o_ma, n_in);
12659 o_bmap += n_in;
12660 o_ma += n_in;
12661 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
12662 o_bmap += n_after;
12663 isl_seq_cpy(bmap->eq[k] + o_bmap,
Tobias Grossera67ac972016-02-26 11:35:12 +000012664 ma->p[i]->v->el + o_ma, n_div_ma);
12665 o_bmap += n_div_ma;
12666 o_ma += n_div_ma;
Tobias Grosser52a25232015-02-04 20:55:43 +000012667 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
12668 isl_int_neg(bmap->eq[k][1 + total], ma->p[i]->v->el[0]);
12669 total++;
12670 }
12671
12672 return bmap;
12673error:
12674 isl_basic_map_free(bmap);
12675 return NULL;
12676}
12677
12678/* Replace the domain or range space (depending on "type) of "space" by "set".
12679 */
12680static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
12681 enum isl_dim_type type, __isl_take isl_space *set)
12682{
12683 if (type == isl_dim_in) {
12684 space = isl_space_range(space);
12685 space = isl_space_map_from_domain_and_range(set, space);
12686 } else {
12687 space = isl_space_domain(space);
12688 space = isl_space_map_from_domain_and_range(space, set);
12689 }
12690
12691 return space;
12692}
12693
12694/* Compute the preimage of the domain or range (depending on "type")
12695 * of "bmap" under the function represented by "ma".
12696 * In other words, plug in "ma" in the domain or range of "bmap".
12697 * The result is a basic map that lives in the same space as "bmap"
12698 * except that the domain or range has been replaced by
12699 * the domain space of "ma".
12700 *
12701 * If bmap is represented by
12702 *
12703 * A(p) + S u + B x + T v + C(divs) >= 0,
12704 *
12705 * where u and x are input and output dimensions if type == isl_dim_out
12706 * while x and v are input and output dimensions if type == isl_dim_in,
12707 * and ma is represented by
12708 *
12709 * x = D(p) + F(y) + G(divs')
12710 *
12711 * then the result is
12712 *
12713 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12714 *
12715 * The divs in the input set are similarly adjusted.
12716 * In particular
12717 *
12718 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12719 *
12720 * becomes
12721 *
12722 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12723 * B_i G(divs') + c_i(divs))/n_i)
12724 *
12725 * If bmap is not a rational map and if F(y) involves any denominators
12726 *
12727 * x_i = (f_i y + h_i)/m_i
12728 *
12729 * then additional constraints are added to ensure that we only
12730 * map back integer points. That is we enforce
12731 *
12732 * f_i y + h_i = m_i alpha_i
12733 *
12734 * with alpha_i an additional existentially quantified variable.
12735 *
12736 * We first copy over the divs from "ma".
12737 * Then we add the modified constraints and divs from "bmap".
12738 * Finally, we add the stride constraints, if needed.
12739 */
12740__isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
12741 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
12742 __isl_take isl_multi_aff *ma)
12743{
12744 int i, k;
12745 isl_space *space;
12746 isl_basic_map *res = NULL;
12747 int n_before, n_after, n_div_bmap, n_div_ma;
12748 isl_int f, c1, c2, g;
12749 int rational, strides;
12750
12751 isl_int_init(f);
12752 isl_int_init(c1);
12753 isl_int_init(c2);
12754 isl_int_init(g);
12755
12756 ma = isl_multi_aff_align_divs(ma);
12757 if (!bmap || !ma)
12758 goto error;
12759 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
12760 goto error;
12761
12762 if (type == isl_dim_in) {
12763 n_before = 0;
12764 n_after = isl_basic_map_dim(bmap, isl_dim_out);
12765 } else {
12766 n_before = isl_basic_map_dim(bmap, isl_dim_in);
12767 n_after = 0;
12768 }
12769 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
12770 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
12771
12772 space = isl_multi_aff_get_domain_space(ma);
12773 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
12774 rational = isl_basic_map_is_rational(bmap);
12775 strides = rational ? 0 : multi_aff_strides(ma);
12776 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
12777 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
12778 if (rational)
12779 res = isl_basic_map_set_rational(res);
12780
12781 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
12782 if (isl_basic_map_alloc_div(res) < 0)
12783 goto error;
12784
12785 if (set_ma_divs(res, ma, n_before, n_after, n_div_ma) < 0)
12786 goto error;
12787
12788 for (i = 0; i < bmap->n_eq; ++i) {
12789 k = isl_basic_map_alloc_equality(res);
12790 if (k < 0)
12791 goto error;
12792 isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
12793 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12794 }
12795
12796 for (i = 0; i < bmap->n_ineq; ++i) {
12797 k = isl_basic_map_alloc_inequality(res);
12798 if (k < 0)
12799 goto error;
12800 isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
12801 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12802 }
12803
12804 for (i = 0; i < bmap->n_div; ++i) {
12805 if (isl_int_is_zero(bmap->div[i][0])) {
12806 isl_int_set_si(res->div[n_div_ma + i][0], 0);
12807 continue;
12808 }
12809 isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
12810 n_before, n_after, n_div_ma, n_div_bmap,
12811 f, c1, c2, g, 1);
12812 }
12813
12814 if (strides)
Tobias Grossera67ac972016-02-26 11:35:12 +000012815 res = add_ma_strides(res, ma, n_before, n_after, n_div_ma);
Tobias Grosser52a25232015-02-04 20:55:43 +000012816
12817 isl_int_clear(f);
12818 isl_int_clear(c1);
12819 isl_int_clear(c2);
12820 isl_int_clear(g);
12821 isl_basic_map_free(bmap);
12822 isl_multi_aff_free(ma);
12823 res = isl_basic_set_simplify(res);
12824 return isl_basic_map_finalize(res);
12825error:
12826 isl_int_clear(f);
12827 isl_int_clear(c1);
12828 isl_int_clear(c2);
12829 isl_int_clear(g);
12830 isl_basic_map_free(bmap);
12831 isl_multi_aff_free(ma);
12832 isl_basic_map_free(res);
12833 return NULL;
12834}
12835
12836/* Compute the preimage of "bset" under the function represented by "ma".
12837 * In other words, plug in "ma" in "bset". The result is a basic set
12838 * that lives in the domain space of "ma".
12839 */
12840__isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
12841 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
12842{
12843 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
12844}
12845
12846/* Compute the preimage of the domain of "bmap" under the function
12847 * represented by "ma".
12848 * In other words, plug in "ma" in the domain of "bmap".
12849 * The result is a basic map that lives in the same space as "bmap"
12850 * except that the domain has been replaced by the domain space of "ma".
12851 */
12852__isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
12853 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12854{
12855 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
12856}
12857
12858/* Compute the preimage of the range of "bmap" under the function
12859 * represented by "ma".
12860 * In other words, plug in "ma" in the range of "bmap".
12861 * The result is a basic map that lives in the same space as "bmap"
12862 * except that the range has been replaced by the domain space of "ma".
12863 */
12864__isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
12865 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12866{
12867 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
12868}
12869
12870/* Check if the range of "ma" is compatible with the domain or range
12871 * (depending on "type") of "map".
12872 * Return -1 if anything is wrong.
12873 */
12874static int check_map_compatible_range_multi_aff(
12875 __isl_keep isl_map *map, enum isl_dim_type type,
12876 __isl_keep isl_multi_aff *ma)
12877{
12878 int m;
12879 isl_space *ma_space;
12880
12881 ma_space = isl_multi_aff_get_space(ma);
12882 m = isl_space_tuple_is_equal(map->dim, type, ma_space, isl_dim_out);
12883 isl_space_free(ma_space);
12884 if (m >= 0 && !m)
12885 isl_die(isl_map_get_ctx(map), isl_error_invalid,
12886 "spaces don't match", return -1);
12887 return m;
12888}
12889
12890/* Compute the preimage of the domain or range (depending on "type")
12891 * of "map" under the function represented by "ma".
12892 * In other words, plug in "ma" in the domain or range of "map".
12893 * The result is a map that lives in the same space as "map"
12894 * except that the domain or range has been replaced by
12895 * the domain space of "ma".
12896 *
12897 * The parameters are assumed to have been aligned.
12898 */
12899static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
12900 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12901{
12902 int i;
12903 isl_space *space;
12904
12905 map = isl_map_cow(map);
12906 ma = isl_multi_aff_align_divs(ma);
12907 if (!map || !ma)
12908 goto error;
12909 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
12910 goto error;
12911
12912 for (i = 0; i < map->n; ++i) {
12913 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
12914 isl_multi_aff_copy(ma));
12915 if (!map->p[i])
12916 goto error;
12917 }
12918
12919 space = isl_multi_aff_get_domain_space(ma);
12920 space = isl_space_set(isl_map_get_space(map), type, space);
12921
12922 isl_space_free(map->dim);
12923 map->dim = space;
12924 if (!map->dim)
12925 goto error;
12926
12927 isl_multi_aff_free(ma);
12928 if (map->n > 1)
12929 ISL_F_CLR(map, ISL_MAP_DISJOINT);
12930 ISL_F_CLR(map, ISL_SET_NORMALIZED);
12931 return map;
12932error:
12933 isl_multi_aff_free(ma);
12934 isl_map_free(map);
12935 return NULL;
12936}
12937
12938/* Compute the preimage of the domain or range (depending on "type")
12939 * of "map" under the function represented by "ma".
12940 * In other words, plug in "ma" in the domain or range of "map".
12941 * The result is a map that lives in the same space as "map"
12942 * except that the domain or range has been replaced by
12943 * the domain space of "ma".
12944 */
12945__isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
12946 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12947{
12948 if (!map || !ma)
12949 goto error;
12950
12951 if (isl_space_match(map->dim, isl_dim_param, ma->space, isl_dim_param))
12952 return map_preimage_multi_aff(map, type, ma);
12953
12954 if (!isl_space_has_named_params(map->dim) ||
12955 !isl_space_has_named_params(ma->space))
12956 isl_die(map->ctx, isl_error_invalid,
12957 "unaligned unnamed parameters", goto error);
12958 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
12959 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
12960
12961 return map_preimage_multi_aff(map, type, ma);
12962error:
12963 isl_multi_aff_free(ma);
12964 return isl_map_free(map);
12965}
12966
12967/* Compute the preimage of "set" under the function represented by "ma".
12968 * In other words, plug in "ma" in "set". The result is a set
12969 * that lives in the domain space of "ma".
12970 */
12971__isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
12972 __isl_take isl_multi_aff *ma)
12973{
12974 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
12975}
12976
12977/* Compute the preimage of the domain of "map" under the function
12978 * represented by "ma".
12979 * In other words, plug in "ma" in the domain of "map".
12980 * The result is a map that lives in the same space as "map"
12981 * except that the domain has been replaced by the domain space of "ma".
12982 */
12983__isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
12984 __isl_take isl_multi_aff *ma)
12985{
12986 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
12987}
12988
12989/* Compute the preimage of the range of "map" under the function
12990 * represented by "ma".
12991 * In other words, plug in "ma" in the range of "map".
12992 * The result is a map that lives in the same space as "map"
12993 * except that the range has been replaced by the domain space of "ma".
12994 */
12995__isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
12996 __isl_take isl_multi_aff *ma)
12997{
12998 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
12999}
13000
13001/* Compute the preimage of "map" under the function represented by "pma".
13002 * In other words, plug in "pma" in the domain or range of "map".
13003 * The result is a map that lives in the same space as "map",
13004 * except that the space of type "type" has been replaced by
13005 * the domain space of "pma".
13006 *
13007 * The parameters of "map" and "pma" are assumed to have been aligned.
13008 */
13009static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
13010 __isl_take isl_map *map, enum isl_dim_type type,
13011 __isl_take isl_pw_multi_aff *pma)
13012{
13013 int i;
13014 isl_map *res;
13015
13016 if (!pma)
13017 goto error;
13018
13019 if (pma->n == 0) {
13020 isl_pw_multi_aff_free(pma);
13021 res = isl_map_empty(isl_map_get_space(map));
13022 isl_map_free(map);
13023 return res;
13024 }
13025
13026 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
13027 isl_multi_aff_copy(pma->p[0].maff));
13028 if (type == isl_dim_in)
13029 res = isl_map_intersect_domain(res,
13030 isl_map_copy(pma->p[0].set));
13031 else
13032 res = isl_map_intersect_range(res,
13033 isl_map_copy(pma->p[0].set));
13034
13035 for (i = 1; i < pma->n; ++i) {
13036 isl_map *res_i;
13037
13038 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
13039 isl_multi_aff_copy(pma->p[i].maff));
13040 if (type == isl_dim_in)
13041 res_i = isl_map_intersect_domain(res_i,
13042 isl_map_copy(pma->p[i].set));
13043 else
13044 res_i = isl_map_intersect_range(res_i,
13045 isl_map_copy(pma->p[i].set));
13046 res = isl_map_union(res, res_i);
13047 }
13048
13049 isl_pw_multi_aff_free(pma);
13050 isl_map_free(map);
13051 return res;
13052error:
13053 isl_pw_multi_aff_free(pma);
13054 isl_map_free(map);
13055 return NULL;
13056}
13057
13058/* Compute the preimage of "map" under the function represented by "pma".
13059 * In other words, plug in "pma" in the domain or range of "map".
13060 * The result is a map that lives in the same space as "map",
13061 * except that the space of type "type" has been replaced by
13062 * the domain space of "pma".
13063 */
13064__isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
13065 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
13066{
13067 if (!map || !pma)
13068 goto error;
13069
13070 if (isl_space_match(map->dim, isl_dim_param, pma->dim, isl_dim_param))
13071 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13072
13073 if (!isl_space_has_named_params(map->dim) ||
13074 !isl_space_has_named_params(pma->dim))
13075 isl_die(map->ctx, isl_error_invalid,
13076 "unaligned unnamed parameters", goto error);
13077 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
13078 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
13079
13080 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13081error:
13082 isl_pw_multi_aff_free(pma);
13083 return isl_map_free(map);
13084}
13085
13086/* Compute the preimage of "set" under the function represented by "pma".
13087 * In other words, plug in "pma" in "set". The result is a set
13088 * that lives in the domain space of "pma".
13089 */
13090__isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
13091 __isl_take isl_pw_multi_aff *pma)
13092{
13093 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
13094}
13095
13096/* Compute the preimage of the domain of "map" under the function
13097 * represented by "pma".
13098 * In other words, plug in "pma" in the domain of "map".
13099 * The result is a map that lives in the same space as "map",
13100 * except that domain space has been replaced by the domain space of "pma".
13101 */
13102__isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
13103 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13104{
13105 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
13106}
13107
13108/* Compute the preimage of the range of "map" under the function
13109 * represented by "pma".
13110 * In other words, plug in "pma" in the range of "map".
13111 * The result is a map that lives in the same space as "map",
13112 * except that range space has been replaced by the domain space of "pma".
13113 */
13114__isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
13115 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13116{
13117 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
13118}
13119
13120/* Compute the preimage of "map" under the function represented by "mpa".
13121 * In other words, plug in "mpa" in the domain or range of "map".
13122 * The result is a map that lives in the same space as "map",
13123 * except that the space of type "type" has been replaced by
13124 * the domain space of "mpa".
13125 *
13126 * If the map does not involve any constraints that refer to the
13127 * dimensions of the substituted space, then the only possible
13128 * effect of "mpa" on the map is to map the space to a different space.
13129 * We create a separate isl_multi_aff to effectuate this change
13130 * in order to avoid spurious splitting of the map along the pieces
13131 * of "mpa".
13132 */
13133__isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
13134 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
13135{
13136 int n;
13137 isl_pw_multi_aff *pma;
13138
13139 if (!map || !mpa)
13140 goto error;
13141
13142 n = isl_map_dim(map, type);
13143 if (!isl_map_involves_dims(map, type, 0, n)) {
13144 isl_space *space;
13145 isl_multi_aff *ma;
13146
13147 space = isl_multi_pw_aff_get_space(mpa);
13148 isl_multi_pw_aff_free(mpa);
13149 ma = isl_multi_aff_zero(space);
13150 return isl_map_preimage_multi_aff(map, type, ma);
13151 }
13152
13153 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
13154 return isl_map_preimage_pw_multi_aff(map, type, pma);
13155error:
13156 isl_map_free(map);
13157 isl_multi_pw_aff_free(mpa);
13158 return NULL;
13159}
13160
13161/* Compute the preimage of "map" under the function represented by "mpa".
13162 * In other words, plug in "mpa" in the domain "map".
13163 * The result is a map that lives in the same space as "map",
13164 * except that domain space has been replaced by the domain space of "mpa".
13165 */
13166__isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
13167 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
13168{
13169 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
13170}
13171
13172/* Compute the preimage of "set" by the function represented by "mpa".
13173 * In other words, plug in "mpa" in "set".
13174 */
13175__isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
13176 __isl_take isl_multi_pw_aff *mpa)
13177{
13178 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);
13179}
Michael Kruse1b8eb412016-12-06 14:37:39 +000013180
13181/* Is the point "inner" internal to inequality constraint "ineq"
13182 * of "bset"?
13183 * The point is considered to be internal to the inequality constraint,
13184 * if it strictly lies on the positive side of the inequality constraint,
13185 * or if it lies on the constraint and the constraint is lexico-positive.
13186 */
13187static isl_bool is_internal(__isl_keep isl_vec *inner,
13188 __isl_keep isl_basic_set *bset, int ineq)
13189{
13190 isl_ctx *ctx;
13191 int pos;
13192 unsigned total;
13193
13194 if (!inner || !bset)
13195 return isl_bool_error;
13196
13197 ctx = isl_basic_set_get_ctx(bset);
13198 isl_seq_inner_product(inner->el, bset->ineq[ineq], inner->size,
13199 &ctx->normalize_gcd);
13200 if (!isl_int_is_zero(ctx->normalize_gcd))
13201 return isl_int_is_nonneg(ctx->normalize_gcd);
13202
13203 total = isl_basic_set_dim(bset, isl_dim_all);
13204 pos = isl_seq_first_non_zero(bset->ineq[ineq] + 1, total);
13205 return isl_int_is_pos(bset->ineq[ineq][1 + pos]);
13206}
13207
13208/* Tighten the inequality constraints of "bset" that are outward with respect
13209 * to the point "vec".
13210 * That is, tighten the constraints that are not satisfied by "vec".
13211 *
13212 * "vec" is a point internal to some superset S of "bset" that is used
13213 * to make the subsets of S disjoint, by tightening one half of the constraints
13214 * that separate two subsets. In particular, the constraints of S
13215 * are all satisfied by "vec" and should not be tightened.
13216 * Of the internal constraints, those that have "vec" on the outside
13217 * are tightened. The shared facet is included in the adjacent subset
13218 * with the opposite constraint.
13219 * For constraints that saturate "vec", this criterion cannot be used
13220 * to determine which of the two sides should be tightened.
13221 * Instead, the sign of the first non-zero coefficient is used
13222 * to make this choice. Note that this second criterion is never used
13223 * on the constraints of S since "vec" is interior to "S".
13224 */
13225__isl_give isl_basic_set *isl_basic_set_tighten_outward(
13226 __isl_take isl_basic_set *bset, __isl_keep isl_vec *vec)
13227{
13228 int j;
13229
13230 bset = isl_basic_set_cow(bset);
13231 if (!bset)
13232 return NULL;
13233 for (j = 0; j < bset->n_ineq; ++j) {
13234 isl_bool internal;
13235
13236 internal = is_internal(vec, bset, j);
13237 if (internal < 0)
13238 return isl_basic_set_free(bset);
13239 if (internal)
13240 continue;
13241 isl_int_sub_ui(bset->ineq[j][0], bset->ineq[j][0], 1);
13242 }
13243
13244 return bset;
13245}