blob: 2f1c906a176e3858d6d33cdf40d9548b9936dba2 [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 Grosser94e53712016-12-31 07:46:11 +00001486/* Check that there are "n" dimensions of type "type" starting at "first"
1487 * in "bmap".
1488 */
1489static isl_stat isl_basic_map_check_range(__isl_keep isl_basic_map *bmap,
1490 enum isl_dim_type type, unsigned first, unsigned n)
1491{
1492 unsigned dim;
1493
1494 if (!bmap)
1495 return isl_stat_error;
1496 dim = isl_basic_map_dim(bmap, type);
1497 if (first + n > dim || first + n < first)
1498 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1499 "position or range out of bounds",
1500 return isl_stat_error);
1501 return isl_stat_ok;
1502}
1503
Tobias Grosser9ec4f952016-07-20 16:53:07 +00001504/* Insert an extra integer division, prescribed by "div", to "bmap"
1505 * at (integer division) position "pos".
1506 *
1507 * The integer division is first added at the end and then moved
1508 * into the right position.
1509 */
1510__isl_give isl_basic_map *isl_basic_map_insert_div(
1511 __isl_take isl_basic_map *bmap, int pos, __isl_keep isl_vec *div)
1512{
Tobias Grosser94e53712016-12-31 07:46:11 +00001513 int i, k;
Tobias Grosser9ec4f952016-07-20 16:53:07 +00001514
1515 bmap = isl_basic_map_cow(bmap);
1516 if (!bmap || !div)
1517 return isl_basic_map_free(bmap);
1518
1519 if (div->size != 1 + 1 + isl_basic_map_dim(bmap, isl_dim_all))
1520 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1521 "unexpected size", return isl_basic_map_free(bmap));
Tobias Grosser94e53712016-12-31 07:46:11 +00001522 if (isl_basic_map_check_range(bmap, isl_dim_div, pos, 0) < 0)
1523 return isl_basic_map_free(bmap);
Tobias Grosser9ec4f952016-07-20 16:53:07 +00001524
1525 bmap = isl_basic_map_extend_space(bmap,
1526 isl_basic_map_get_space(bmap), 1, 0, 2);
1527 k = isl_basic_map_alloc_div(bmap);
1528 if (k < 0)
1529 return isl_basic_map_free(bmap);
1530 isl_seq_cpy(bmap->div[k], div->el, div->size);
1531 isl_int_set_si(bmap->div[k][div->size], 0);
1532
1533 for (i = k; i > pos; --i)
1534 isl_basic_map_swap_div(bmap, i, i - 1);
1535
1536 return bmap;
1537}
1538
Tobias Grosser52a25232015-02-04 20:55:43 +00001539int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
1540{
1541 if (!bmap)
1542 return -1;
1543 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
1544 bmap->n_div -= n;
1545 return 0;
1546}
1547
1548int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n)
1549{
Tobias Grosser06e15922016-11-16 11:06:47 +00001550 return isl_basic_map_free_div(bset_to_bmap(bset), n);
Tobias Grosser52a25232015-02-04 20:55:43 +00001551}
1552
1553/* Copy constraint from src to dst, putting the vars of src at offset
1554 * dim_off in dst and the divs of src at offset div_off in dst.
1555 * If both sets are actually map, then dim_off applies to the input
1556 * variables.
1557 */
1558static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
1559 struct isl_basic_map *src_map, isl_int *src,
1560 unsigned in_off, unsigned out_off, unsigned div_off)
1561{
1562 unsigned src_nparam = isl_basic_map_n_param(src_map);
1563 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
1564 unsigned src_in = isl_basic_map_n_in(src_map);
1565 unsigned dst_in = isl_basic_map_n_in(dst_map);
1566 unsigned src_out = isl_basic_map_n_out(src_map);
1567 unsigned dst_out = isl_basic_map_n_out(dst_map);
1568 isl_int_set(dst[0], src[0]);
1569 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
1570 if (dst_nparam > src_nparam)
1571 isl_seq_clr(dst+1+src_nparam,
1572 dst_nparam - src_nparam);
1573 isl_seq_clr(dst+1+dst_nparam, in_off);
1574 isl_seq_cpy(dst+1+dst_nparam+in_off,
1575 src+1+src_nparam,
1576 isl_min(dst_in-in_off, src_in));
1577 if (dst_in-in_off > src_in)
1578 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
1579 dst_in - in_off - src_in);
1580 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
1581 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
1582 src+1+src_nparam+src_in,
1583 isl_min(dst_out-out_off, src_out));
1584 if (dst_out-out_off > src_out)
1585 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
1586 dst_out - out_off - src_out);
1587 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
1588 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
1589 src+1+src_nparam+src_in+src_out,
1590 isl_min(dst_map->extra-div_off, src_map->n_div));
1591 if (dst_map->n_div-div_off > src_map->n_div)
1592 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
1593 div_off+src_map->n_div,
1594 dst_map->n_div - div_off - src_map->n_div);
1595}
1596
1597static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
1598 struct isl_basic_map *src_map, isl_int *src,
1599 unsigned in_off, unsigned out_off, unsigned div_off)
1600{
1601 isl_int_set(dst[0], src[0]);
1602 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
1603}
1604
1605static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
1606 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
1607{
1608 int i;
1609 unsigned div_off;
1610
1611 if (!bmap1 || !bmap2)
1612 goto error;
1613
1614 div_off = bmap1->n_div;
1615
1616 for (i = 0; i < bmap2->n_eq; ++i) {
1617 int i1 = isl_basic_map_alloc_equality(bmap1);
1618 if (i1 < 0)
1619 goto error;
1620 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
1621 i_pos, o_pos, div_off);
1622 }
1623
1624 for (i = 0; i < bmap2->n_ineq; ++i) {
1625 int i1 = isl_basic_map_alloc_inequality(bmap1);
1626 if (i1 < 0)
1627 goto error;
1628 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
1629 i_pos, o_pos, div_off);
1630 }
1631
1632 for (i = 0; i < bmap2->n_div; ++i) {
1633 int i1 = isl_basic_map_alloc_div(bmap1);
1634 if (i1 < 0)
1635 goto error;
1636 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
1637 i_pos, o_pos, div_off);
1638 }
1639
1640 isl_basic_map_free(bmap2);
1641
1642 return bmap1;
1643
1644error:
1645 isl_basic_map_free(bmap1);
1646 isl_basic_map_free(bmap2);
1647 return NULL;
1648}
1649
1650struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1651 struct isl_basic_set *bset2, unsigned pos)
1652{
Tobias Grosser06e15922016-11-16 11:06:47 +00001653 return bset_from_bmap(add_constraints(bset_to_bmap(bset1),
1654 bset_to_bmap(bset2), 0, pos));
Tobias Grosser52a25232015-02-04 20:55:43 +00001655}
1656
1657struct isl_basic_map *isl_basic_map_extend_space(struct isl_basic_map *base,
1658 __isl_take isl_space *dim, unsigned extra,
1659 unsigned n_eq, unsigned n_ineq)
1660{
1661 struct isl_basic_map *ext;
1662 unsigned flags;
1663 int dims_ok;
1664
1665 if (!dim)
1666 goto error;
1667
1668 if (!base)
1669 goto error;
1670
1671 dims_ok = isl_space_is_equal(base->dim, dim) &&
1672 base->extra >= base->n_div + extra;
1673
1674 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1675 room_for_ineq(base, n_ineq)) {
1676 isl_space_free(dim);
1677 return base;
1678 }
1679
1680 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
1681 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
1682 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
1683 extra += base->extra;
1684 n_eq += base->n_eq;
1685 n_ineq += base->n_ineq;
1686
1687 ext = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1688 dim = NULL;
1689 if (!ext)
1690 goto error;
1691
1692 if (dims_ok)
1693 ext->sample = isl_vec_copy(base->sample);
1694 flags = base->flags;
1695 ext = add_constraints(ext, base, 0, 0);
1696 if (ext) {
1697 ext->flags = flags;
1698 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1699 }
1700
1701 return ext;
1702
1703error:
1704 isl_space_free(dim);
1705 isl_basic_map_free(base);
1706 return NULL;
1707}
1708
1709struct isl_basic_set *isl_basic_set_extend_space(struct isl_basic_set *base,
1710 __isl_take isl_space *dim, unsigned extra,
1711 unsigned n_eq, unsigned n_ineq)
1712{
Tobias Grosser06e15922016-11-16 11:06:47 +00001713 return bset_from_bmap(isl_basic_map_extend_space(bset_to_bmap(base),
1714 dim, extra, n_eq, n_ineq));
Tobias Grosser52a25232015-02-04 20:55:43 +00001715}
1716
1717struct isl_basic_map *isl_basic_map_extend_constraints(
1718 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1719{
1720 if (!base)
1721 return NULL;
1722 return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1723 0, n_eq, n_ineq);
1724}
1725
1726struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1727 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1728 unsigned n_eq, unsigned n_ineq)
1729{
1730 struct isl_basic_map *bmap;
1731 isl_space *dim;
1732
1733 if (!base)
1734 return NULL;
1735 dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1736 if (!dim)
1737 goto error;
1738
1739 bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1740 return bmap;
1741error:
1742 isl_basic_map_free(base);
1743 return NULL;
1744}
1745
1746struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1747 unsigned nparam, unsigned dim, unsigned extra,
1748 unsigned n_eq, unsigned n_ineq)
1749{
Tobias Grosser06e15922016-11-16 11:06:47 +00001750 return bset_from_bmap(isl_basic_map_extend(bset_to_bmap(base),
1751 nparam, 0, dim, extra, n_eq, n_ineq));
Tobias Grosser52a25232015-02-04 20:55:43 +00001752}
1753
1754struct isl_basic_set *isl_basic_set_extend_constraints(
1755 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1756{
Tobias Grosser06e15922016-11-16 11:06:47 +00001757 isl_basic_map *bmap = bset_to_bmap(base);
1758 bmap = isl_basic_map_extend_constraints(bmap, n_eq, n_ineq);
1759 return bset_from_bmap(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +00001760}
1761
1762struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1763{
Tobias Grosser06e15922016-11-16 11:06:47 +00001764 return bset_from_bmap(isl_basic_map_cow(bset_to_bmap(bset)));
Tobias Grosser52a25232015-02-04 20:55:43 +00001765}
1766
1767struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1768{
1769 if (!bmap)
1770 return NULL;
1771
1772 if (bmap->ref > 1) {
1773 bmap->ref--;
1774 bmap = isl_basic_map_dup(bmap);
1775 }
Tobias Grosser1fa7b972015-02-16 19:33:40 +00001776 if (bmap) {
Tobias Grosser52a25232015-02-04 20:55:43 +00001777 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
Tobias Grosser1fa7b972015-02-16 19:33:40 +00001778 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
1779 }
Tobias Grosser52a25232015-02-04 20:55:43 +00001780 return bmap;
1781}
1782
Tobias Grosser07b20952016-06-12 04:30:40 +00001783/* Clear all cached information in "map", either because it is about
1784 * to be modified or because it is being freed.
1785 * Always return the same pointer that is passed in.
1786 * This is needed for the use in isl_map_free.
1787 */
1788static __isl_give isl_map *clear_caches(__isl_take isl_map *map)
Tobias Grosser52a25232015-02-04 20:55:43 +00001789{
Tobias Grosser07b20952016-06-12 04:30:40 +00001790 isl_basic_map_free(map->cached_simple_hull[0]);
1791 isl_basic_map_free(map->cached_simple_hull[1]);
1792 map->cached_simple_hull[0] = NULL;
1793 map->cached_simple_hull[1] = NULL;
1794 return map;
Tobias Grosser52a25232015-02-04 20:55:43 +00001795}
1796
Tobias Grosser07b20952016-06-12 04:30:40 +00001797struct isl_set *isl_set_cow(struct isl_set *set)
1798{
1799 return isl_map_cow(set);
1800}
1801
1802/* Return an isl_map that is equal to "map" and that has only
1803 * a single reference.
1804 *
1805 * If the original input already has only one reference, then
1806 * simply return it, but clear all cached information, since
1807 * it may be rendered invalid by the operations that will be
1808 * performed on the result.
1809 *
1810 * Otherwise, create a duplicate (without any cached information).
1811 */
Tobias Grosser52a25232015-02-04 20:55:43 +00001812struct isl_map *isl_map_cow(struct isl_map *map)
1813{
1814 if (!map)
1815 return NULL;
1816
1817 if (map->ref == 1)
Tobias Grosser07b20952016-06-12 04:30:40 +00001818 return clear_caches(map);
Tobias Grosser52a25232015-02-04 20:55:43 +00001819 map->ref--;
1820 return isl_map_dup(map);
1821}
1822
1823static void swap_vars(struct isl_blk blk, isl_int *a,
1824 unsigned a_len, unsigned b_len)
1825{
1826 isl_seq_cpy(blk.data, a+a_len, b_len);
1827 isl_seq_cpy(blk.data+b_len, a, a_len);
1828 isl_seq_cpy(a, blk.data, b_len+a_len);
1829}
1830
1831static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1832 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1833{
1834 int i;
1835 struct isl_blk blk;
1836
1837 if (!bmap)
1838 goto error;
1839
1840 isl_assert(bmap->ctx,
1841 pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error);
1842
1843 if (n1 == 0 || n2 == 0)
1844 return bmap;
1845
1846 bmap = isl_basic_map_cow(bmap);
1847 if (!bmap)
1848 return NULL;
1849
1850 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1851 if (isl_blk_is_error(blk))
1852 goto error;
1853
1854 for (i = 0; i < bmap->n_eq; ++i)
1855 swap_vars(blk,
1856 bmap->eq[i] + pos, n1, n2);
1857
1858 for (i = 0; i < bmap->n_ineq; ++i)
1859 swap_vars(blk,
1860 bmap->ineq[i] + pos, n1, n2);
1861
1862 for (i = 0; i < bmap->n_div; ++i)
1863 swap_vars(blk,
1864 bmap->div[i]+1 + pos, n1, n2);
1865
1866 isl_blk_free(bmap->ctx, blk);
1867
1868 ISL_F_CLR(bmap, ISL_BASIC_SET_NORMALIZED);
1869 bmap = isl_basic_map_gauss(bmap, NULL);
1870 return isl_basic_map_finalize(bmap);
1871error:
1872 isl_basic_map_free(bmap);
1873 return NULL;
1874}
1875
Tobias Grosser52a25232015-02-04 20:55:43 +00001876struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1877{
1878 int i = 0;
1879 unsigned total;
1880 if (!bmap)
1881 goto error;
1882 total = isl_basic_map_total_dim(bmap);
1883 isl_basic_map_free_div(bmap, bmap->n_div);
1884 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1885 if (bmap->n_eq > 0)
1886 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1887 else {
1888 i = isl_basic_map_alloc_equality(bmap);
1889 if (i < 0)
1890 goto error;
1891 }
1892 isl_int_set_si(bmap->eq[i][0], 1);
1893 isl_seq_clr(bmap->eq[i]+1, total);
1894 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1895 isl_vec_free(bmap->sample);
1896 bmap->sample = NULL;
1897 return isl_basic_map_finalize(bmap);
1898error:
1899 isl_basic_map_free(bmap);
1900 return NULL;
1901}
1902
1903struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1904{
Tobias Grosser06e15922016-11-16 11:06:47 +00001905 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset)));
Tobias Grosser52a25232015-02-04 20:55:43 +00001906}
1907
1908/* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
1909 * of "bmap").
1910 */
1911static void swap_div(__isl_keep isl_basic_map *bmap, int a, int b)
1912{
1913 isl_int *t = bmap->div[a];
1914 bmap->div[a] = bmap->div[b];
1915 bmap->div[b] = t;
1916}
1917
1918/* Swap divs "a" and "b" in "bmap" and adjust the constraints and
1919 * div definitions accordingly.
1920 */
1921void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1922{
1923 int i;
1924 unsigned off = isl_space_dim(bmap->dim, isl_dim_all);
1925
1926 swap_div(bmap, a, b);
1927
1928 for (i = 0; i < bmap->n_eq; ++i)
1929 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1930
1931 for (i = 0; i < bmap->n_ineq; ++i)
1932 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1933
1934 for (i = 0; i < bmap->n_div; ++i)
1935 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1936 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1937}
1938
Tobias Grosser932ec012016-07-06 09:11:00 +00001939/* Swap divs "a" and "b" in "bset" and adjust the constraints and
1940 * div definitions accordingly.
1941 */
1942void isl_basic_set_swap_div(__isl_keep isl_basic_set *bset, int a, int b)
1943{
1944 isl_basic_map_swap_div(bset, a, b);
1945}
1946
Tobias Grosser52a25232015-02-04 20:55:43 +00001947/* Eliminate the specified n dimensions starting at first from the
1948 * constraints, without removing the dimensions from the space.
1949 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1950 */
1951__isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
1952 enum isl_dim_type type, unsigned first, unsigned n)
1953{
1954 int i;
1955
1956 if (!map)
1957 return NULL;
1958 if (n == 0)
1959 return map;
1960
1961 if (first + n > isl_map_dim(map, type) || first + n < first)
1962 isl_die(map->ctx, isl_error_invalid,
1963 "index out of bounds", goto error);
1964
1965 map = isl_map_cow(map);
1966 if (!map)
1967 return NULL;
1968
1969 for (i = 0; i < map->n; ++i) {
1970 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
1971 if (!map->p[i])
1972 goto error;
1973 }
1974 return map;
1975error:
1976 isl_map_free(map);
1977 return NULL;
1978}
1979
1980/* Eliminate the specified n dimensions starting at first from the
1981 * constraints, without removing the dimensions from the space.
1982 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1983 */
1984__isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
1985 enum isl_dim_type type, unsigned first, unsigned n)
1986{
Tobias Grosser06e15922016-11-16 11:06:47 +00001987 return set_from_map(isl_map_eliminate(set_to_map(set), type, first, n));
Tobias Grosser52a25232015-02-04 20:55:43 +00001988}
1989
1990/* Eliminate the specified n dimensions starting at first from the
1991 * constraints, without removing the dimensions from the space.
1992 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1993 */
1994__isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
1995 unsigned first, unsigned n)
1996{
1997 return isl_set_eliminate(set, isl_dim_set, first, n);
1998}
1999
2000__isl_give isl_basic_map *isl_basic_map_remove_divs(
2001 __isl_take isl_basic_map *bmap)
2002{
2003 if (!bmap)
2004 return NULL;
2005 bmap = isl_basic_map_eliminate_vars(bmap,
2006 isl_space_dim(bmap->dim, isl_dim_all), bmap->n_div);
2007 if (!bmap)
2008 return NULL;
2009 bmap->n_div = 0;
2010 return isl_basic_map_finalize(bmap);
2011}
2012
2013__isl_give isl_basic_set *isl_basic_set_remove_divs(
2014 __isl_take isl_basic_set *bset)
2015{
Tobias Grosser06e15922016-11-16 11:06:47 +00002016 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset)));
Tobias Grosser52a25232015-02-04 20:55:43 +00002017}
2018
2019__isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
2020{
2021 int i;
2022
2023 if (!map)
2024 return NULL;
2025 if (map->n == 0)
2026 return map;
2027
2028 map = isl_map_cow(map);
2029 if (!map)
2030 return NULL;
2031
2032 for (i = 0; i < map->n; ++i) {
2033 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
2034 if (!map->p[i])
2035 goto error;
2036 }
2037 return map;
2038error:
2039 isl_map_free(map);
2040 return NULL;
2041}
2042
2043__isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
2044{
2045 return isl_map_remove_divs(set);
2046}
2047
2048struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap,
2049 enum isl_dim_type type, unsigned first, unsigned n)
2050{
Tobias Grosser94e53712016-12-31 07:46:11 +00002051 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2052 return isl_basic_map_free(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +00002053 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2054 return bmap;
2055 bmap = isl_basic_map_eliminate_vars(bmap,
2056 isl_basic_map_offset(bmap, type) - 1 + first, n);
2057 if (!bmap)
2058 return bmap;
2059 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
2060 return bmap;
2061 bmap = isl_basic_map_drop(bmap, type, first, n);
2062 return bmap;
Tobias Grosser52a25232015-02-04 20:55:43 +00002063}
2064
2065/* Return true if the definition of the given div (recursively) involves
2066 * any of the given variables.
2067 */
2068static int div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
2069 unsigned first, unsigned n)
2070{
2071 int i;
2072 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2073
2074 if (isl_int_is_zero(bmap->div[div][0]))
2075 return 0;
2076 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
2077 return 1;
2078
2079 for (i = bmap->n_div - 1; i >= 0; --i) {
2080 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2081 continue;
2082 if (div_involves_vars(bmap, i, first, n))
2083 return 1;
2084 }
2085
2086 return 0;
2087}
2088
2089/* Try and add a lower and/or upper bound on "div" to "bmap"
2090 * based on inequality "i".
2091 * "total" is the total number of variables (excluding the divs).
2092 * "v" is a temporary object that can be used during the calculations.
2093 * If "lb" is set, then a lower bound should be constructed.
2094 * If "ub" is set, then an upper bound should be constructed.
2095 *
2096 * The calling function has already checked that the inequality does not
2097 * reference "div", but we still need to check that the inequality is
2098 * of the right form. We'll consider the case where we want to construct
2099 * a lower bound. The construction of upper bounds is similar.
2100 *
2101 * Let "div" be of the form
2102 *
2103 * q = floor((a + f(x))/d)
2104 *
2105 * We essentially check if constraint "i" is of the form
2106 *
2107 * b + f(x) >= 0
2108 *
2109 * so that we can use it to derive a lower bound on "div".
2110 * However, we allow a slightly more general form
2111 *
2112 * b + g(x) >= 0
2113 *
2114 * with the condition that the coefficients of g(x) - f(x) are all
2115 * divisible by d.
2116 * Rewriting this constraint as
2117 *
2118 * 0 >= -b - g(x)
2119 *
2120 * adding a + f(x) to both sides and dividing by d, we obtain
2121 *
2122 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2123 *
2124 * Taking the floor on both sides, we obtain
2125 *
2126 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2127 *
2128 * or
2129 *
2130 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2131 *
2132 * In the case of an upper bound, we construct the constraint
2133 *
2134 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2135 *
2136 */
2137static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
2138 __isl_take isl_basic_map *bmap, int div, int i,
2139 unsigned total, isl_int v, int lb, int ub)
2140{
2141 int j;
2142
2143 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
2144 if (lb) {
2145 isl_int_sub(v, bmap->ineq[i][1 + j],
2146 bmap->div[div][1 + 1 + j]);
2147 lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
2148 }
2149 if (ub) {
2150 isl_int_add(v, bmap->ineq[i][1 + j],
2151 bmap->div[div][1 + 1 + j]);
2152 ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
2153 }
2154 }
2155 if (!lb && !ub)
2156 return bmap;
2157
2158 bmap = isl_basic_map_cow(bmap);
2159 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
2160 if (lb) {
2161 int k = isl_basic_map_alloc_inequality(bmap);
2162 if (k < 0)
2163 goto error;
2164 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2165 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
2166 bmap->div[div][1 + j]);
2167 isl_int_cdiv_q(bmap->ineq[k][j],
2168 bmap->ineq[k][j], bmap->div[div][0]);
2169 }
2170 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
2171 }
2172 if (ub) {
2173 int k = isl_basic_map_alloc_inequality(bmap);
2174 if (k < 0)
2175 goto error;
2176 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2177 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
2178 bmap->div[div][1 + j]);
2179 isl_int_fdiv_q(bmap->ineq[k][j],
2180 bmap->ineq[k][j], bmap->div[div][0]);
2181 }
2182 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
2183 }
2184
2185 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2186 return bmap;
2187error:
2188 isl_basic_map_free(bmap);
2189 return NULL;
2190}
2191
2192/* This function is called right before "div" is eliminated from "bmap"
2193 * using Fourier-Motzkin.
2194 * Look through the constraints of "bmap" for constraints on the argument
2195 * of the integer division and use them to construct constraints on the
2196 * integer division itself. These constraints can then be combined
2197 * during the Fourier-Motzkin elimination.
2198 * Note that it is only useful to introduce lower bounds on "div"
2199 * if "bmap" already contains upper bounds on "div" as the newly
2200 * introduce lower bounds can then be combined with the pre-existing
2201 * upper bounds. Similarly for upper bounds.
2202 * We therefore first check if "bmap" contains any lower and/or upper bounds
2203 * on "div".
2204 *
2205 * It is interesting to note that the introduction of these constraints
2206 * can indeed lead to more accurate results, even when compared to
2207 * deriving constraints on the argument of "div" from constraints on "div".
2208 * Consider, for example, the set
2209 *
2210 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2211 *
2212 * The second constraint can be rewritten as
2213 *
2214 * 2 * [(-i-2j+3)/4] + k >= 0
2215 *
2216 * from which we can derive
2217 *
2218 * -i - 2j + 3 >= -2k
2219 *
2220 * or
2221 *
2222 * i + 2j <= 3 + 2k
2223 *
2224 * Combined with the first constraint, we obtain
2225 *
2226 * -3 <= 3 + 2k or k >= -3
2227 *
2228 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2229 * the first constraint, we obtain
2230 *
2231 * [(i + 2j)/4] >= [-3/4] = -1
2232 *
2233 * Combining this constraint with the second constraint, we obtain
2234 *
2235 * k >= -2
2236 */
2237static __isl_give isl_basic_map *insert_bounds_on_div(
2238 __isl_take isl_basic_map *bmap, int div)
2239{
2240 int i;
2241 int check_lb, check_ub;
2242 isl_int v;
2243 unsigned total;
2244
2245 if (!bmap)
2246 return NULL;
2247
2248 if (isl_int_is_zero(bmap->div[div][0]))
2249 return bmap;
2250
2251 total = isl_space_dim(bmap->dim, isl_dim_all);
2252
2253 check_lb = 0;
2254 check_ub = 0;
2255 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
2256 int s = isl_int_sgn(bmap->ineq[i][1 + total + div]);
2257 if (s > 0)
2258 check_ub = 1;
2259 if (s < 0)
2260 check_lb = 1;
2261 }
2262
2263 if (!check_lb && !check_ub)
2264 return bmap;
2265
2266 isl_int_init(v);
2267
2268 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
2269 if (!isl_int_is_zero(bmap->ineq[i][1 + total + div]))
2270 continue;
2271
2272 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, total, v,
2273 check_lb, check_ub);
2274 }
2275
2276 isl_int_clear(v);
2277
2278 return bmap;
2279}
2280
2281/* Remove all divs (recursively) involving any of the given dimensions
2282 * in their definitions.
2283 */
2284__isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
2285 __isl_take isl_basic_map *bmap,
2286 enum isl_dim_type type, unsigned first, unsigned n)
2287{
2288 int i;
2289
Tobias Grosser94e53712016-12-31 07:46:11 +00002290 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2291 return isl_basic_map_free(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +00002292 first += isl_basic_map_offset(bmap, type);
2293
2294 for (i = bmap->n_div - 1; i >= 0; --i) {
2295 if (!div_involves_vars(bmap, i, first, n))
2296 continue;
2297 bmap = insert_bounds_on_div(bmap, i);
2298 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2299 if (!bmap)
2300 return NULL;
2301 i = bmap->n_div;
2302 }
2303
2304 return bmap;
Tobias Grosser52a25232015-02-04 20:55:43 +00002305}
2306
2307__isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims(
2308 __isl_take isl_basic_set *bset,
2309 enum isl_dim_type type, unsigned first, unsigned n)
2310{
2311 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
2312}
2313
2314__isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
2315 enum isl_dim_type type, unsigned first, unsigned n)
2316{
2317 int i;
2318
2319 if (!map)
2320 return NULL;
2321 if (map->n == 0)
2322 return map;
2323
2324 map = isl_map_cow(map);
2325 if (!map)
2326 return NULL;
2327
2328 for (i = 0; i < map->n; ++i) {
2329 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
2330 type, first, n);
2331 if (!map->p[i])
2332 goto error;
2333 }
2334 return map;
2335error:
2336 isl_map_free(map);
2337 return NULL;
2338}
2339
2340__isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
2341 enum isl_dim_type type, unsigned first, unsigned n)
2342{
Tobias Grosser06e15922016-11-16 11:06:47 +00002343 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set),
2344 type, first, n));
Tobias Grosser52a25232015-02-04 20:55:43 +00002345}
2346
Tobias Grosser06e15922016-11-16 11:06:47 +00002347/* Does the description of "bmap" depend on the specified dimensions?
Tobias Grosser52a25232015-02-04 20:55:43 +00002348 * We also check whether the dimensions appear in any of the div definitions.
2349 * In principle there is no need for this check. If the dimensions appear
2350 * in a div definition, they also appear in the defining constraints of that
2351 * div.
2352 */
Tobias Grosserb2f39922015-05-28 13:32:11 +00002353isl_bool isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
Tobias Grosser52a25232015-02-04 20:55:43 +00002354 enum isl_dim_type type, unsigned first, unsigned n)
2355{
2356 int i;
2357
Tobias Grosser94e53712016-12-31 07:46:11 +00002358 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
Tobias Grosserb2f39922015-05-28 13:32:11 +00002359 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00002360
Tobias Grosser52a25232015-02-04 20:55:43 +00002361 first += isl_basic_map_offset(bmap, type);
2362 for (i = 0; i < bmap->n_eq; ++i)
2363 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
Tobias Grosserb2f39922015-05-28 13:32:11 +00002364 return isl_bool_true;
Tobias Grosser52a25232015-02-04 20:55:43 +00002365 for (i = 0; i < bmap->n_ineq; ++i)
2366 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
Tobias Grosserb2f39922015-05-28 13:32:11 +00002367 return isl_bool_true;
Tobias Grosser52a25232015-02-04 20:55:43 +00002368 for (i = 0; i < bmap->n_div; ++i) {
2369 if (isl_int_is_zero(bmap->div[i][0]))
2370 continue;
2371 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
Tobias Grosserb2f39922015-05-28 13:32:11 +00002372 return isl_bool_true;
Tobias Grosser52a25232015-02-04 20:55:43 +00002373 }
2374
Tobias Grosserb2f39922015-05-28 13:32:11 +00002375 return isl_bool_false;
Tobias Grosser52a25232015-02-04 20:55:43 +00002376}
2377
Tobias Grosserb2f39922015-05-28 13:32:11 +00002378isl_bool isl_map_involves_dims(__isl_keep isl_map *map,
Tobias Grosser52a25232015-02-04 20:55:43 +00002379 enum isl_dim_type type, unsigned first, unsigned n)
2380{
2381 int i;
2382
2383 if (!map)
Tobias Grosserb2f39922015-05-28 13:32:11 +00002384 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00002385
2386 if (first + n > isl_map_dim(map, type))
2387 isl_die(map->ctx, isl_error_invalid,
Tobias Grosserb2f39922015-05-28 13:32:11 +00002388 "index out of bounds", return isl_bool_error);
Tobias Grosser52a25232015-02-04 20:55:43 +00002389
2390 for (i = 0; i < map->n; ++i) {
Tobias Grosserb2f39922015-05-28 13:32:11 +00002391 isl_bool involves = isl_basic_map_involves_dims(map->p[i],
Tobias Grosser52a25232015-02-04 20:55:43 +00002392 type, first, n);
2393 if (involves < 0 || involves)
2394 return involves;
2395 }
2396
Tobias Grosserb2f39922015-05-28 13:32:11 +00002397 return isl_bool_false;
Tobias Grosser52a25232015-02-04 20:55:43 +00002398}
2399
Tobias Grosserb2f39922015-05-28 13:32:11 +00002400isl_bool isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
Tobias Grosser52a25232015-02-04 20:55:43 +00002401 enum isl_dim_type type, unsigned first, unsigned n)
2402{
2403 return isl_basic_map_involves_dims(bset, type, first, n);
2404}
2405
Tobias Grosserb2f39922015-05-28 13:32:11 +00002406isl_bool isl_set_involves_dims(__isl_keep isl_set *set,
Tobias Grosser52a25232015-02-04 20:55:43 +00002407 enum isl_dim_type type, unsigned first, unsigned n)
2408{
2409 return isl_map_involves_dims(set, type, first, n);
2410}
2411
Tobias Grosser94e53712016-12-31 07:46:11 +00002412/* Drop all constraints in bmap that involve any of the dimensions
2413 * first to first+n-1.
2414 */
2415static __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving(
2416 __isl_take isl_basic_map *bmap, unsigned first, unsigned n)
2417{
2418 int i;
2419
2420 if (n == 0)
2421 return bmap;
2422
2423 bmap = isl_basic_map_cow(bmap);
2424
2425 if (!bmap)
2426 return NULL;
2427
2428 for (i = bmap->n_eq - 1; i >= 0; --i) {
2429 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) == -1)
2430 continue;
2431 isl_basic_map_drop_equality(bmap, i);
2432 }
2433
2434 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2435 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) == -1)
2436 continue;
2437 isl_basic_map_drop_inequality(bmap, i);
2438 }
2439
2440 bmap = isl_basic_map_add_known_div_constraints(bmap);
2441 return bmap;
2442}
2443
2444/* Drop all constraints in bset that involve any of the dimensions
2445 * first to first+n-1.
2446 */
2447__isl_give isl_basic_set *isl_basic_set_drop_constraints_involving(
2448 __isl_take isl_basic_set *bset, unsigned first, unsigned n)
2449{
2450 return isl_basic_map_drop_constraints_involving(bset, first, n);
2451}
2452
2453/* Drop all constraints in bmap that do not involve any of the dimensions
2454 * first to first + n - 1 of the given type.
2455 */
2456__isl_give isl_basic_map *isl_basic_map_drop_constraints_not_involving_dims(
2457 __isl_take isl_basic_map *bmap,
2458 enum isl_dim_type type, unsigned first, unsigned n)
2459{
2460 int i;
2461
2462 if (n == 0) {
2463 isl_space *space = isl_basic_map_get_space(bmap);
2464 isl_basic_map_free(bmap);
2465 return isl_basic_map_universe(space);
2466 }
2467 bmap = isl_basic_map_cow(bmap);
2468 if (!bmap)
2469 return NULL;
2470
2471 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2472 return isl_basic_map_free(bmap);
2473
2474 first += isl_basic_map_offset(bmap, type) - 1;
2475
2476 for (i = bmap->n_eq - 1; i >= 0; --i) {
2477 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) != -1)
2478 continue;
2479 isl_basic_map_drop_equality(bmap, i);
2480 }
2481
2482 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2483 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) != -1)
2484 continue;
2485 isl_basic_map_drop_inequality(bmap, i);
2486 }
2487
2488 bmap = isl_basic_map_add_known_div_constraints(bmap);
2489 return bmap;
2490}
2491
2492/* Drop all constraints in bset that do not involve any of the dimensions
2493 * first to first + n - 1 of the given type.
2494 */
2495__isl_give isl_basic_set *isl_basic_set_drop_constraints_not_involving_dims(
2496 __isl_take isl_basic_set *bset,
2497 enum isl_dim_type type, unsigned first, unsigned n)
2498{
2499 return isl_basic_map_drop_constraints_not_involving_dims(bset,
2500 type, first, n);
2501}
2502
2503/* Drop all constraints in bmap that involve any of the dimensions
2504 * first to first + n - 1 of the given type.
2505 */
2506__isl_give isl_basic_map *isl_basic_map_drop_constraints_involving_dims(
2507 __isl_take isl_basic_map *bmap,
2508 enum isl_dim_type type, unsigned first, unsigned n)
2509{
2510 if (!bmap)
2511 return NULL;
2512 if (n == 0)
2513 return bmap;
2514
2515 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2516 return isl_basic_map_free(bmap);
2517
2518 bmap = isl_basic_map_remove_divs_involving_dims(bmap, type, first, n);
2519 first += isl_basic_map_offset(bmap, type) - 1;
2520 return isl_basic_map_drop_constraints_involving(bmap, first, n);
2521}
2522
2523/* Drop all constraints in bset that involve any of the dimensions
2524 * first to first + n - 1 of the given type.
2525 */
2526__isl_give isl_basic_set *isl_basic_set_drop_constraints_involving_dims(
2527 __isl_take isl_basic_set *bset,
2528 enum isl_dim_type type, unsigned first, unsigned n)
2529{
2530 return isl_basic_map_drop_constraints_involving_dims(bset,
2531 type, first, n);
2532}
2533
2534/* Drop constraints from "map" by applying "drop" to each basic map.
2535 */
2536static __isl_give isl_map *drop_constraints(__isl_take isl_map *map,
2537 enum isl_dim_type type, unsigned first, unsigned n,
2538 __isl_give isl_basic_map *(*drop)(__isl_take isl_basic_map *bmap,
2539 enum isl_dim_type type, unsigned first, unsigned n))
2540{
2541 int i;
2542 unsigned dim;
2543
2544 if (!map)
2545 return NULL;
2546
2547 dim = isl_map_dim(map, type);
2548 if (first + n > dim || first + n < first)
2549 isl_die(isl_map_get_ctx(map), isl_error_invalid,
2550 "index out of bounds", return isl_map_free(map));
2551
2552 map = isl_map_cow(map);
2553 if (!map)
2554 return NULL;
2555
2556 for (i = 0; i < map->n; ++i) {
2557 map->p[i] = drop(map->p[i], type, first, n);
2558 if (!map->p[i])
2559 return isl_map_free(map);
2560 }
2561
2562 if (map->n > 1)
2563 ISL_F_CLR(map, ISL_MAP_DISJOINT);
2564
2565 return map;
2566}
2567
2568/* Drop all constraints in map that involve any of the dimensions
2569 * first to first + n - 1 of the given type.
2570 */
2571__isl_give isl_map *isl_map_drop_constraints_involving_dims(
2572 __isl_take isl_map *map,
2573 enum isl_dim_type type, unsigned first, unsigned n)
2574{
2575 if (n == 0)
2576 return map;
2577 return drop_constraints(map, type, first, n,
2578 &isl_basic_map_drop_constraints_involving_dims);
2579}
2580
2581/* Drop all constraints in "map" that do not involve any of the dimensions
2582 * first to first + n - 1 of the given type.
2583 */
2584__isl_give isl_map *isl_map_drop_constraints_not_involving_dims(
2585 __isl_take isl_map *map,
2586 enum isl_dim_type type, unsigned first, unsigned n)
2587{
2588 if (n == 0) {
2589 isl_space *space = isl_map_get_space(map);
2590 isl_map_free(map);
2591 return isl_map_universe(space);
2592 }
2593 return drop_constraints(map, type, first, n,
2594 &isl_basic_map_drop_constraints_not_involving_dims);
2595}
2596
2597/* Drop all constraints in set that involve any of the dimensions
2598 * first to first + n - 1 of the given type.
2599 */
2600__isl_give isl_set *isl_set_drop_constraints_involving_dims(
2601 __isl_take isl_set *set,
2602 enum isl_dim_type type, unsigned first, unsigned n)
2603{
2604 return isl_map_drop_constraints_involving_dims(set, type, first, n);
2605}
2606
2607/* Drop all constraints in "set" that do not involve any of the dimensions
2608 * first to first + n - 1 of the given type.
2609 */
2610__isl_give isl_set *isl_set_drop_constraints_not_involving_dims(
2611 __isl_take isl_set *set,
2612 enum isl_dim_type type, unsigned first, unsigned n)
2613{
2614 return isl_map_drop_constraints_not_involving_dims(set, type, first, n);
2615}
2616
Tobias Grosser932ec012016-07-06 09:11:00 +00002617/* Does local variable "div" of "bmap" have a complete explicit representation?
2618 * Having a complete explicit representation requires not only
2619 * an explicit representation, but also that all local variables
2620 * that appear in this explicit representation in turn have
2621 * a complete explicit representation.
Tobias Grosser52a25232015-02-04 20:55:43 +00002622 */
Tobias Grosser932ec012016-07-06 09:11:00 +00002623isl_bool isl_basic_map_div_is_known(__isl_keep isl_basic_map *bmap, int div)
Tobias Grosser52a25232015-02-04 20:55:43 +00002624{
2625 int i;
2626 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
Tobias Grosser932ec012016-07-06 09:11:00 +00002627 isl_bool marked;
Tobias Grosser52a25232015-02-04 20:55:43 +00002628
Tobias Grosser932ec012016-07-06 09:11:00 +00002629 marked = isl_basic_map_div_is_marked_unknown(bmap, div);
2630 if (marked < 0 || marked)
2631 return isl_bool_not(marked);
Tobias Grosser52a25232015-02-04 20:55:43 +00002632
2633 for (i = bmap->n_div - 1; i >= 0; --i) {
Tobias Grosser932ec012016-07-06 09:11:00 +00002634 isl_bool known;
2635
Tobias Grosser52a25232015-02-04 20:55:43 +00002636 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2637 continue;
Tobias Grosser932ec012016-07-06 09:11:00 +00002638 known = isl_basic_map_div_is_known(bmap, i);
2639 if (known < 0 || !known)
2640 return known;
Tobias Grosser52a25232015-02-04 20:55:43 +00002641 }
2642
Tobias Grosser932ec012016-07-06 09:11:00 +00002643 return isl_bool_true;
2644}
2645
2646/* Does local variable "div" of "bset" have a complete explicit representation?
2647 */
2648isl_bool isl_basic_set_div_is_known(__isl_keep isl_basic_set *bset, int div)
2649{
2650 return isl_basic_map_div_is_known(bset, div);
Tobias Grosser52a25232015-02-04 20:55:43 +00002651}
2652
2653/* Remove all divs that are unknown or defined in terms of unknown divs.
2654 */
2655__isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
2656 __isl_take isl_basic_map *bmap)
2657{
2658 int i;
2659
2660 if (!bmap)
2661 return NULL;
2662
2663 for (i = bmap->n_div - 1; i >= 0; --i) {
Tobias Grosser932ec012016-07-06 09:11:00 +00002664 if (isl_basic_map_div_is_known(bmap, i))
Tobias Grosser52a25232015-02-04 20:55:43 +00002665 continue;
2666 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2667 if (!bmap)
2668 return NULL;
2669 i = bmap->n_div;
2670 }
2671
2672 return bmap;
2673}
2674
2675/* Remove all divs that are unknown or defined in terms of unknown divs.
2676 */
2677__isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
2678 __isl_take isl_basic_set *bset)
2679{
2680 return isl_basic_map_remove_unknown_divs(bset);
2681}
2682
2683__isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
2684{
2685 int i;
2686
2687 if (!map)
2688 return NULL;
2689 if (map->n == 0)
2690 return map;
2691
2692 map = isl_map_cow(map);
2693 if (!map)
2694 return NULL;
2695
2696 for (i = 0; i < map->n; ++i) {
2697 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
2698 if (!map->p[i])
2699 goto error;
2700 }
2701 return map;
2702error:
2703 isl_map_free(map);
2704 return NULL;
2705}
2706
2707__isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
2708{
Tobias Grosser06e15922016-11-16 11:06:47 +00002709 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set)));
Tobias Grosser52a25232015-02-04 20:55:43 +00002710}
2711
2712__isl_give isl_basic_set *isl_basic_set_remove_dims(
2713 __isl_take isl_basic_set *bset,
2714 enum isl_dim_type type, unsigned first, unsigned n)
2715{
Tobias Grosser06e15922016-11-16 11:06:47 +00002716 isl_basic_map *bmap = bset_to_bmap(bset);
2717 bmap = isl_basic_map_remove_dims(bmap, type, first, n);
2718 return bset_from_bmap(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +00002719}
2720
2721struct isl_map *isl_map_remove_dims(struct isl_map *map,
2722 enum isl_dim_type type, unsigned first, unsigned n)
2723{
2724 int i;
2725
2726 if (n == 0)
2727 return map;
2728
2729 map = isl_map_cow(map);
2730 if (!map)
2731 return NULL;
2732 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2733
2734 for (i = 0; i < map->n; ++i) {
2735 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
2736 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
2737 if (!map->p[i])
2738 goto error;
2739 }
2740 map = isl_map_drop(map, type, first, n);
2741 return map;
2742error:
2743 isl_map_free(map);
2744 return NULL;
2745}
2746
2747__isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
2748 enum isl_dim_type type, unsigned first, unsigned n)
2749{
Tobias Grosser06e15922016-11-16 11:06:47 +00002750 return set_from_map(isl_map_remove_dims(set_to_map(bset),
2751 type, first, n));
Tobias Grosser52a25232015-02-04 20:55:43 +00002752}
2753
2754/* Project out n inputs starting at first using Fourier-Motzkin */
2755struct isl_map *isl_map_remove_inputs(struct isl_map *map,
2756 unsigned first, unsigned n)
2757{
2758 return isl_map_remove_dims(map, isl_dim_in, first, n);
2759}
2760
2761static void dump_term(struct isl_basic_map *bmap,
2762 isl_int c, int pos, FILE *out)
2763{
2764 const char *name;
2765 unsigned in = isl_basic_map_n_in(bmap);
2766 unsigned dim = in + isl_basic_map_n_out(bmap);
2767 unsigned nparam = isl_basic_map_n_param(bmap);
2768 if (!pos)
2769 isl_int_print(out, c, 0);
2770 else {
2771 if (!isl_int_is_one(c))
2772 isl_int_print(out, c, 0);
2773 if (pos < 1 + nparam) {
2774 name = isl_space_get_dim_name(bmap->dim,
2775 isl_dim_param, pos - 1);
2776 if (name)
2777 fprintf(out, "%s", name);
2778 else
2779 fprintf(out, "p%d", pos - 1);
2780 } else if (pos < 1 + nparam + in)
2781 fprintf(out, "i%d", pos - 1 - nparam);
2782 else if (pos < 1 + nparam + dim)
2783 fprintf(out, "o%d", pos - 1 - nparam - in);
2784 else
2785 fprintf(out, "e%d", pos - 1 - nparam - dim);
2786 }
2787}
2788
2789static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
2790 int sign, FILE *out)
2791{
2792 int i;
2793 int first;
2794 unsigned len = 1 + isl_basic_map_total_dim(bmap);
2795 isl_int v;
2796
2797 isl_int_init(v);
2798 for (i = 0, first = 1; i < len; ++i) {
2799 if (isl_int_sgn(c[i]) * sign <= 0)
2800 continue;
2801 if (!first)
2802 fprintf(out, " + ");
2803 first = 0;
2804 isl_int_abs(v, c[i]);
2805 dump_term(bmap, v, i, out);
2806 }
2807 isl_int_clear(v);
2808 if (first)
2809 fprintf(out, "0");
2810}
2811
2812static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
2813 const char *op, FILE *out, int indent)
2814{
2815 int i;
2816
2817 fprintf(out, "%*s", indent, "");
2818
2819 dump_constraint_sign(bmap, c, 1, out);
2820 fprintf(out, " %s ", op);
2821 dump_constraint_sign(bmap, c, -1, out);
2822
2823 fprintf(out, "\n");
2824
2825 for (i = bmap->n_div; i < bmap->extra; ++i) {
2826 if (isl_int_is_zero(c[1+isl_space_dim(bmap->dim, isl_dim_all)+i]))
2827 continue;
2828 fprintf(out, "%*s", indent, "");
2829 fprintf(out, "ERROR: unused div coefficient not zero\n");
2830 abort();
2831 }
2832}
2833
2834static void dump_constraints(struct isl_basic_map *bmap,
2835 isl_int **c, unsigned n,
2836 const char *op, FILE *out, int indent)
2837{
2838 int i;
2839
2840 for (i = 0; i < n; ++i)
2841 dump_constraint(bmap, c[i], op, out, indent);
2842}
2843
2844static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
2845{
2846 int j;
2847 int first = 1;
2848 unsigned total = isl_basic_map_total_dim(bmap);
2849
2850 for (j = 0; j < 1 + total; ++j) {
2851 if (isl_int_is_zero(exp[j]))
2852 continue;
2853 if (!first && isl_int_is_pos(exp[j]))
2854 fprintf(out, "+");
2855 dump_term(bmap, exp[j], j, out);
2856 first = 0;
2857 }
2858}
2859
2860static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
2861{
2862 int i;
2863
2864 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
2865 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
2866
2867 for (i = 0; i < bmap->n_div; ++i) {
2868 fprintf(out, "%*s", indent, "");
2869 fprintf(out, "e%d = [(", i);
2870 dump_affine(bmap, bmap->div[i]+1, out);
2871 fprintf(out, ")/");
2872 isl_int_print(out, bmap->div[i][0], 0);
2873 fprintf(out, "]\n");
2874 }
2875}
2876
2877void isl_basic_set_print_internal(struct isl_basic_set *bset,
2878 FILE *out, int indent)
2879{
2880 if (!bset) {
2881 fprintf(out, "null basic set\n");
2882 return;
2883 }
2884
2885 fprintf(out, "%*s", indent, "");
2886 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2887 bset->ref, bset->dim->nparam, bset->dim->n_out,
2888 bset->extra, bset->flags);
Tobias Grosser06e15922016-11-16 11:06:47 +00002889 dump(bset_to_bmap(bset), out, indent);
Tobias Grosser52a25232015-02-04 20:55:43 +00002890}
2891
2892void isl_basic_map_print_internal(struct isl_basic_map *bmap,
2893 FILE *out, int indent)
2894{
2895 if (!bmap) {
2896 fprintf(out, "null basic map\n");
2897 return;
2898 }
2899
2900 fprintf(out, "%*s", indent, "");
2901 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2902 "flags: %x, n_name: %d\n",
2903 bmap->ref,
2904 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
2905 bmap->extra, bmap->flags, bmap->dim->n_id);
2906 dump(bmap, out, indent);
2907}
2908
2909int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
2910{
2911 unsigned total;
2912 if (!bmap)
2913 return -1;
2914 total = isl_basic_map_total_dim(bmap);
2915 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
2916 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
2917 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
2918 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2919 return 0;
2920}
2921
Tobias Grosser07b20952016-06-12 04:30:40 +00002922__isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *space, int n,
Tobias Grosser52a25232015-02-04 20:55:43 +00002923 unsigned flags)
2924{
Tobias Grosser07b20952016-06-12 04:30:40 +00002925 if (!space)
Tobias Grosser52a25232015-02-04 20:55:43 +00002926 return NULL;
Tobias Grosser07b20952016-06-12 04:30:40 +00002927 if (isl_space_dim(space, isl_dim_in) != 0)
2928 isl_die(isl_space_get_ctx(space), isl_error_invalid,
2929 "set cannot have input dimensions", goto error);
2930 return isl_map_alloc_space(space, n, flags);
Tobias Grosser52a25232015-02-04 20:55:43 +00002931error:
Tobias Grosser07b20952016-06-12 04:30:40 +00002932 isl_space_free(space);
Tobias Grosser52a25232015-02-04 20:55:43 +00002933 return NULL;
2934}
2935
2936struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
2937 unsigned nparam, unsigned dim, int n, unsigned flags)
2938{
2939 struct isl_set *set;
2940 isl_space *dims;
2941
2942 dims = isl_space_alloc(ctx, nparam, 0, dim);
2943 if (!dims)
2944 return NULL;
2945
2946 set = isl_set_alloc_space(dims, n, flags);
2947 return set;
2948}
2949
2950/* Make sure "map" has room for at least "n" more basic maps.
2951 */
2952struct isl_map *isl_map_grow(struct isl_map *map, int n)
2953{
2954 int i;
2955 struct isl_map *grown = NULL;
2956
2957 if (!map)
2958 return NULL;
2959 isl_assert(map->ctx, n >= 0, goto error);
2960 if (map->n + n <= map->size)
2961 return map;
2962 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
2963 if (!grown)
2964 goto error;
2965 for (i = 0; i < map->n; ++i) {
2966 grown->p[i] = isl_basic_map_copy(map->p[i]);
2967 if (!grown->p[i])
2968 goto error;
2969 grown->n++;
2970 }
2971 isl_map_free(map);
2972 return grown;
2973error:
2974 isl_map_free(grown);
2975 isl_map_free(map);
2976 return NULL;
2977}
2978
2979/* Make sure "set" has room for at least "n" more basic sets.
2980 */
2981struct isl_set *isl_set_grow(struct isl_set *set, int n)
2982{
Tobias Grosser06e15922016-11-16 11:06:47 +00002983 return set_from_map(isl_map_grow(set_to_map(set), n));
Tobias Grosser52a25232015-02-04 20:55:43 +00002984}
2985
2986struct isl_set *isl_set_dup(struct isl_set *set)
2987{
2988 int i;
2989 struct isl_set *dup;
2990
2991 if (!set)
2992 return NULL;
2993
2994 dup = isl_set_alloc_space(isl_space_copy(set->dim), set->n, set->flags);
2995 if (!dup)
2996 return NULL;
2997 for (i = 0; i < set->n; ++i)
2998 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
2999 return dup;
3000}
3001
3002struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
3003{
3004 return isl_map_from_basic_map(bset);
3005}
3006
3007struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
3008{
3009 struct isl_map *map;
3010
3011 if (!bmap)
3012 return NULL;
3013
3014 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
3015 return isl_map_add_basic_map(map, bmap);
3016}
3017
3018__isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
3019 __isl_take isl_basic_set *bset)
3020{
Tobias Grosser06e15922016-11-16 11:06:47 +00003021 return set_from_map(isl_map_add_basic_map(set_to_map(set),
3022 bset_to_bmap(bset)));
Tobias Grosser52a25232015-02-04 20:55:43 +00003023}
3024
3025__isl_null isl_set *isl_set_free(__isl_take isl_set *set)
3026{
Tobias Grosser07b20952016-06-12 04:30:40 +00003027 return isl_map_free(set);
Tobias Grosser52a25232015-02-04 20:55:43 +00003028}
3029
3030void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
3031{
3032 int i;
3033
3034 if (!set) {
3035 fprintf(out, "null set\n");
3036 return;
3037 }
3038
3039 fprintf(out, "%*s", indent, "");
3040 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
3041 set->ref, set->n, set->dim->nparam, set->dim->n_out,
3042 set->flags);
3043 for (i = 0; i < set->n; ++i) {
3044 fprintf(out, "%*s", indent, "");
3045 fprintf(out, "basic set %d:\n", i);
3046 isl_basic_set_print_internal(set->p[i], out, indent+4);
3047 }
3048}
3049
3050void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
3051{
3052 int i;
3053
3054 if (!map) {
3055 fprintf(out, "null map\n");
3056 return;
3057 }
3058
3059 fprintf(out, "%*s", indent, "");
3060 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
3061 "flags: %x, n_name: %d\n",
3062 map->ref, map->n, map->dim->nparam, map->dim->n_in,
3063 map->dim->n_out, map->flags, map->dim->n_id);
3064 for (i = 0; i < map->n; ++i) {
3065 fprintf(out, "%*s", indent, "");
3066 fprintf(out, "basic map %d:\n", i);
3067 isl_basic_map_print_internal(map->p[i], out, indent+4);
3068 }
3069}
3070
3071struct isl_basic_map *isl_basic_map_intersect_domain(
3072 struct isl_basic_map *bmap, struct isl_basic_set *bset)
3073{
3074 struct isl_basic_map *bmap_domain;
3075
3076 if (!bmap || !bset)
3077 goto error;
3078
3079 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
3080 bset->dim, isl_dim_param), goto error);
3081
3082 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
3083 isl_assert(bset->ctx,
3084 isl_basic_map_compatible_domain(bmap, bset), goto error);
3085
3086 bmap = isl_basic_map_cow(bmap);
3087 if (!bmap)
3088 goto error;
3089 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
3090 bset->n_div, bset->n_eq, bset->n_ineq);
3091 bmap_domain = isl_basic_map_from_domain(bset);
3092 bmap = add_constraints(bmap, bmap_domain, 0, 0);
3093
3094 bmap = isl_basic_map_simplify(bmap);
3095 return isl_basic_map_finalize(bmap);
3096error:
3097 isl_basic_map_free(bmap);
3098 isl_basic_set_free(bset);
3099 return NULL;
3100}
3101
3102struct isl_basic_map *isl_basic_map_intersect_range(
3103 struct isl_basic_map *bmap, struct isl_basic_set *bset)
3104{
3105 struct isl_basic_map *bmap_range;
3106
3107 if (!bmap || !bset)
3108 goto error;
3109
3110 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
3111 bset->dim, isl_dim_param), goto error);
3112
3113 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
3114 isl_assert(bset->ctx,
3115 isl_basic_map_compatible_range(bmap, bset), goto error);
3116
Tobias Grossera67ac972016-02-26 11:35:12 +00003117 if (isl_basic_set_plain_is_universe(bset)) {
Tobias Grosser52a25232015-02-04 20:55:43 +00003118 isl_basic_set_free(bset);
3119 return bmap;
3120 }
3121
3122 bmap = isl_basic_map_cow(bmap);
3123 if (!bmap)
3124 goto error;
3125 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
3126 bset->n_div, bset->n_eq, bset->n_ineq);
Tobias Grossereab0943e2016-11-22 21:31:59 +00003127 bmap_range = bset_to_bmap(bset);
Tobias Grosser52a25232015-02-04 20:55:43 +00003128 bmap = add_constraints(bmap, bmap_range, 0, 0);
3129
3130 bmap = isl_basic_map_simplify(bmap);
3131 return isl_basic_map_finalize(bmap);
3132error:
3133 isl_basic_map_free(bmap);
3134 isl_basic_set_free(bset);
3135 return NULL;
3136}
3137
Tobias Grosserb2f39922015-05-28 13:32:11 +00003138isl_bool isl_basic_map_contains(__isl_keep isl_basic_map *bmap,
3139 __isl_keep isl_vec *vec)
Tobias Grosser52a25232015-02-04 20:55:43 +00003140{
3141 int i;
3142 unsigned total;
3143 isl_int s;
3144
3145 if (!bmap || !vec)
Tobias Grosserb2f39922015-05-28 13:32:11 +00003146 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00003147
3148 total = 1 + isl_basic_map_total_dim(bmap);
3149 if (total != vec->size)
Tobias Grosserb2f39922015-05-28 13:32:11 +00003150 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00003151
3152 isl_int_init(s);
3153
3154 for (i = 0; i < bmap->n_eq; ++i) {
3155 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
3156 if (!isl_int_is_zero(s)) {
3157 isl_int_clear(s);
Tobias Grosserb2f39922015-05-28 13:32:11 +00003158 return isl_bool_false;
Tobias Grosser52a25232015-02-04 20:55:43 +00003159 }
3160 }
3161
3162 for (i = 0; i < bmap->n_ineq; ++i) {
3163 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
3164 if (isl_int_is_neg(s)) {
3165 isl_int_clear(s);
Tobias Grosserb2f39922015-05-28 13:32:11 +00003166 return isl_bool_false;
Tobias Grosser52a25232015-02-04 20:55:43 +00003167 }
3168 }
3169
3170 isl_int_clear(s);
3171
Tobias Grosserb2f39922015-05-28 13:32:11 +00003172 return isl_bool_true;
Tobias Grosser52a25232015-02-04 20:55:43 +00003173}
3174
Tobias Grosserb2f39922015-05-28 13:32:11 +00003175isl_bool isl_basic_set_contains(__isl_keep isl_basic_set *bset,
3176 __isl_keep isl_vec *vec)
Tobias Grosser52a25232015-02-04 20:55:43 +00003177{
Tobias Grosser06e15922016-11-16 11:06:47 +00003178 return isl_basic_map_contains(bset_to_bmap(bset), vec);
Tobias Grosser52a25232015-02-04 20:55:43 +00003179}
3180
3181struct isl_basic_map *isl_basic_map_intersect(
3182 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3183{
3184 struct isl_vec *sample = NULL;
3185
3186 if (!bmap1 || !bmap2)
3187 goto error;
3188
3189 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
3190 bmap2->dim, isl_dim_param), goto error);
3191 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
3192 isl_space_dim(bmap1->dim, isl_dim_param) &&
3193 isl_space_dim(bmap2->dim, isl_dim_all) !=
3194 isl_space_dim(bmap2->dim, isl_dim_param))
3195 return isl_basic_map_intersect(bmap2, bmap1);
3196
3197 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
3198 isl_space_dim(bmap2->dim, isl_dim_param))
3199 isl_assert(bmap1->ctx,
3200 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
3201
Tobias Grosserfb3fb0a2015-11-21 20:48:39 +00003202 if (isl_basic_map_plain_is_empty(bmap1)) {
3203 isl_basic_map_free(bmap2);
3204 return bmap1;
3205 }
3206 if (isl_basic_map_plain_is_empty(bmap2)) {
3207 isl_basic_map_free(bmap1);
3208 return bmap2;
3209 }
3210
Tobias Grosser52a25232015-02-04 20:55:43 +00003211 if (bmap1->sample &&
3212 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
3213 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
3214 sample = isl_vec_copy(bmap1->sample);
3215 else if (bmap2->sample &&
3216 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
3217 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
3218 sample = isl_vec_copy(bmap2->sample);
3219
3220 bmap1 = isl_basic_map_cow(bmap1);
3221 if (!bmap1)
3222 goto error;
3223 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
3224 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
3225 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
3226
3227 if (!bmap1)
3228 isl_vec_free(sample);
3229 else if (sample) {
3230 isl_vec_free(bmap1->sample);
3231 bmap1->sample = sample;
3232 }
3233
3234 bmap1 = isl_basic_map_simplify(bmap1);
3235 return isl_basic_map_finalize(bmap1);
3236error:
3237 if (sample)
3238 isl_vec_free(sample);
3239 isl_basic_map_free(bmap1);
3240 isl_basic_map_free(bmap2);
3241 return NULL;
3242}
3243
3244struct isl_basic_set *isl_basic_set_intersect(
3245 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3246{
Tobias Grosser06e15922016-11-16 11:06:47 +00003247 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1),
3248 bset_to_bmap(bset2)));
Tobias Grosser52a25232015-02-04 20:55:43 +00003249}
3250
3251__isl_give isl_basic_set *isl_basic_set_intersect_params(
3252 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
3253{
3254 return isl_basic_set_intersect(bset1, bset2);
3255}
3256
3257/* Special case of isl_map_intersect, where both map1 and map2
3258 * are convex, without any divs and such that either map1 or map2
3259 * contains a single constraint. This constraint is then simply
3260 * added to the other map.
3261 */
3262static __isl_give isl_map *map_intersect_add_constraint(
3263 __isl_take isl_map *map1, __isl_take isl_map *map2)
3264{
3265 isl_assert(map1->ctx, map1->n == 1, goto error);
3266 isl_assert(map2->ctx, map1->n == 1, goto error);
3267 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
3268 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
3269
3270 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
3271 return isl_map_intersect(map2, map1);
3272
3273 isl_assert(map2->ctx,
3274 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
3275
3276 map1 = isl_map_cow(map1);
3277 if (!map1)
3278 goto error;
3279 if (isl_map_plain_is_empty(map1)) {
3280 isl_map_free(map2);
3281 return map1;
3282 }
3283 map1->p[0] = isl_basic_map_cow(map1->p[0]);
3284 if (map2->p[0]->n_eq == 1)
3285 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
3286 else
3287 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
3288 map2->p[0]->ineq[0]);
3289
3290 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
3291 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
3292 if (!map1->p[0])
3293 goto error;
3294
3295 if (isl_basic_map_plain_is_empty(map1->p[0])) {
3296 isl_basic_map_free(map1->p[0]);
3297 map1->n = 0;
3298 }
3299
3300 isl_map_free(map2);
3301
3302 return map1;
3303error:
3304 isl_map_free(map1);
3305 isl_map_free(map2);
3306 return NULL;
3307}
3308
3309/* map2 may be either a parameter domain or a map living in the same
3310 * space as map1.
3311 */
3312static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
3313 __isl_take isl_map *map2)
3314{
3315 unsigned flags = 0;
3316 isl_map *result;
3317 int i, j;
3318
3319 if (!map1 || !map2)
3320 goto error;
3321
3322 if ((isl_map_plain_is_empty(map1) ||
3323 isl_map_plain_is_universe(map2)) &&
3324 isl_space_is_equal(map1->dim, map2->dim)) {
3325 isl_map_free(map2);
3326 return map1;
3327 }
3328 if ((isl_map_plain_is_empty(map2) ||
3329 isl_map_plain_is_universe(map1)) &&
3330 isl_space_is_equal(map1->dim, map2->dim)) {
3331 isl_map_free(map1);
3332 return map2;
3333 }
3334
3335 if (map1->n == 1 && map2->n == 1 &&
3336 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
3337 isl_space_is_equal(map1->dim, map2->dim) &&
3338 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
3339 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
3340 return map_intersect_add_constraint(map1, map2);
3341
3342 if (isl_space_dim(map2->dim, isl_dim_all) !=
3343 isl_space_dim(map2->dim, isl_dim_param))
3344 isl_assert(map1->ctx,
3345 isl_space_is_equal(map1->dim, map2->dim), goto error);
3346
3347 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
3348 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3349 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3350
3351 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3352 map1->n * map2->n, flags);
3353 if (!result)
3354 goto error;
3355 for (i = 0; i < map1->n; ++i)
3356 for (j = 0; j < map2->n; ++j) {
3357 struct isl_basic_map *part;
3358 part = isl_basic_map_intersect(
3359 isl_basic_map_copy(map1->p[i]),
3360 isl_basic_map_copy(map2->p[j]));
3361 if (isl_basic_map_is_empty(part) < 0)
3362 part = isl_basic_map_free(part);
3363 result = isl_map_add_basic_map(result, part);
3364 if (!result)
3365 goto error;
3366 }
3367 isl_map_free(map1);
3368 isl_map_free(map2);
3369 return result;
3370error:
3371 isl_map_free(map1);
3372 isl_map_free(map2);
3373 return NULL;
3374}
3375
3376static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
3377 __isl_take isl_map *map2)
3378{
3379 if (!map1 || !map2)
3380 goto error;
3381 if (!isl_space_is_equal(map1->dim, map2->dim))
3382 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
3383 "spaces don't match", goto error);
3384 return map_intersect_internal(map1, map2);
3385error:
3386 isl_map_free(map1);
3387 isl_map_free(map2);
3388 return NULL;
3389}
3390
3391__isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
3392 __isl_take isl_map *map2)
3393{
3394 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
3395}
3396
3397struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
3398{
Tobias Grosser06e15922016-11-16 11:06:47 +00003399 return set_from_map(isl_map_intersect(set_to_map(set1),
3400 set_to_map(set2)));
Tobias Grosser52a25232015-02-04 20:55:43 +00003401}
3402
3403/* map_intersect_internal accepts intersections
3404 * with parameter domains, so we can just call that function.
3405 */
3406static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
3407 __isl_take isl_set *params)
3408{
3409 return map_intersect_internal(map, params);
3410}
3411
3412__isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
3413 __isl_take isl_map *map2)
3414{
3415 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
3416}
3417
3418__isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
3419 __isl_take isl_set *params)
3420{
3421 return isl_map_intersect_params(set, params);
3422}
3423
3424struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
3425{
Tobias Grosserb2f39922015-05-28 13:32:11 +00003426 isl_space *space;
3427 unsigned pos, n1, n2;
Tobias Grosser52a25232015-02-04 20:55:43 +00003428
3429 if (!bmap)
3430 return NULL;
3431 bmap = isl_basic_map_cow(bmap);
3432 if (!bmap)
3433 return NULL;
Tobias Grosserb2f39922015-05-28 13:32:11 +00003434 space = isl_space_reverse(isl_space_copy(bmap->dim));
3435 pos = isl_basic_map_offset(bmap, isl_dim_in);
3436 n1 = isl_basic_map_dim(bmap, isl_dim_in);
3437 n2 = isl_basic_map_dim(bmap, isl_dim_out);
3438 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
3439 return isl_basic_map_reset_space(bmap, space);
Tobias Grosser52a25232015-02-04 20:55:43 +00003440}
3441
3442static __isl_give isl_basic_map *basic_map_space_reset(
3443 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
3444{
3445 isl_space *space;
3446
3447 if (!bmap)
3448 return NULL;
3449 if (!isl_space_is_named_or_nested(bmap->dim, type))
3450 return bmap;
3451
3452 space = isl_basic_map_get_space(bmap);
3453 space = isl_space_reset(space, type);
3454 bmap = isl_basic_map_reset_space(bmap, space);
3455 return bmap;
3456}
3457
3458__isl_give isl_basic_map *isl_basic_map_insert_dims(
3459 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3460 unsigned pos, unsigned n)
3461{
3462 isl_space *res_dim;
3463 struct isl_basic_map *res;
3464 struct isl_dim_map *dim_map;
3465 unsigned total, off;
3466 enum isl_dim_type t;
3467
3468 if (n == 0)
3469 return basic_map_space_reset(bmap, type);
3470
3471 if (!bmap)
3472 return NULL;
3473
3474 res_dim = isl_space_insert_dims(isl_basic_map_get_space(bmap), type, pos, n);
3475
3476 total = isl_basic_map_total_dim(bmap) + n;
3477 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3478 off = 0;
3479 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3480 if (t != type) {
3481 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3482 } else {
3483 unsigned size = isl_basic_map_dim(bmap, t);
3484 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3485 0, pos, off);
3486 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3487 pos, size - pos, off + pos + n);
3488 }
3489 off += isl_space_dim(res_dim, t);
3490 }
3491 isl_dim_map_div(dim_map, bmap, off);
3492
3493 res = isl_basic_map_alloc_space(res_dim,
3494 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3495 if (isl_basic_map_is_rational(bmap))
3496 res = isl_basic_map_set_rational(res);
3497 if (isl_basic_map_plain_is_empty(bmap)) {
3498 isl_basic_map_free(bmap);
3499 free(dim_map);
3500 return isl_basic_map_set_to_empty(res);
3501 }
3502 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3503 return isl_basic_map_finalize(res);
3504}
3505
3506__isl_give isl_basic_set *isl_basic_set_insert_dims(
3507 __isl_take isl_basic_set *bset,
3508 enum isl_dim_type type, unsigned pos, unsigned n)
3509{
3510 return isl_basic_map_insert_dims(bset, type, pos, n);
3511}
3512
Tobias Grossere0f8d592015-05-13 13:10:13 +00003513__isl_give isl_basic_map *isl_basic_map_add_dims(__isl_take isl_basic_map *bmap,
Tobias Grosser52a25232015-02-04 20:55:43 +00003514 enum isl_dim_type type, unsigned n)
3515{
3516 if (!bmap)
3517 return NULL;
3518 return isl_basic_map_insert_dims(bmap, type,
3519 isl_basic_map_dim(bmap, type), n);
3520}
3521
3522__isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
3523 enum isl_dim_type type, unsigned n)
3524{
3525 if (!bset)
3526 return NULL;
3527 isl_assert(bset->ctx, type != isl_dim_in, goto error);
Tobias Grossere0f8d592015-05-13 13:10:13 +00003528 return isl_basic_map_add_dims(bset, type, n);
Tobias Grosser52a25232015-02-04 20:55:43 +00003529error:
3530 isl_basic_set_free(bset);
3531 return NULL;
3532}
3533
3534static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
3535 enum isl_dim_type type)
3536{
3537 isl_space *space;
3538
3539 if (!map || !isl_space_is_named_or_nested(map->dim, type))
3540 return map;
3541
3542 space = isl_map_get_space(map);
3543 space = isl_space_reset(space, type);
3544 map = isl_map_reset_space(map, space);
3545 return map;
3546}
3547
3548__isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
3549 enum isl_dim_type type, unsigned pos, unsigned n)
3550{
3551 int i;
3552
3553 if (n == 0)
3554 return map_space_reset(map, type);
3555
3556 map = isl_map_cow(map);
3557 if (!map)
3558 return NULL;
3559
3560 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
3561 if (!map->dim)
3562 goto error;
3563
3564 for (i = 0; i < map->n; ++i) {
3565 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
3566 if (!map->p[i])
3567 goto error;
3568 }
3569
3570 return map;
3571error:
3572 isl_map_free(map);
3573 return NULL;
3574}
3575
3576__isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
3577 enum isl_dim_type type, unsigned pos, unsigned n)
3578{
3579 return isl_map_insert_dims(set, type, pos, n);
3580}
3581
3582__isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
3583 enum isl_dim_type type, unsigned n)
3584{
3585 if (!map)
3586 return NULL;
3587 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
3588}
3589
3590__isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
3591 enum isl_dim_type type, unsigned n)
3592{
3593 if (!set)
3594 return NULL;
3595 isl_assert(set->ctx, type != isl_dim_in, goto error);
Tobias Grosser06e15922016-11-16 11:06:47 +00003596 return set_from_map(isl_map_add_dims(set_to_map(set), type, n));
Tobias Grosser52a25232015-02-04 20:55:43 +00003597error:
3598 isl_set_free(set);
3599 return NULL;
3600}
3601
3602__isl_give isl_basic_map *isl_basic_map_move_dims(
3603 __isl_take isl_basic_map *bmap,
3604 enum isl_dim_type dst_type, unsigned dst_pos,
3605 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3606{
3607 struct isl_dim_map *dim_map;
3608 struct isl_basic_map *res;
3609 enum isl_dim_type t;
3610 unsigned total, off;
3611
3612 if (!bmap)
3613 return NULL;
3614 if (n == 0)
3615 return bmap;
3616
Tobias Grosser94e53712016-12-31 07:46:11 +00003617 if (isl_basic_map_check_range(bmap, src_type, src_pos, n) < 0)
3618 return isl_basic_map_free(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +00003619
3620 if (dst_type == src_type && dst_pos == src_pos)
3621 return bmap;
3622
3623 isl_assert(bmap->ctx, dst_type != src_type, goto error);
3624
3625 if (pos(bmap->dim, dst_type) + dst_pos ==
3626 pos(bmap->dim, src_type) + src_pos +
3627 ((src_type < dst_type) ? n : 0)) {
3628 bmap = isl_basic_map_cow(bmap);
3629 if (!bmap)
3630 return NULL;
3631
3632 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3633 src_type, src_pos, n);
3634 if (!bmap->dim)
3635 goto error;
3636
3637 bmap = isl_basic_map_finalize(bmap);
3638
3639 return bmap;
3640 }
3641
3642 total = isl_basic_map_total_dim(bmap);
3643 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3644
3645 off = 0;
3646 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3647 unsigned size = isl_space_dim(bmap->dim, t);
3648 if (t == dst_type) {
3649 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3650 0, dst_pos, off);
3651 off += dst_pos;
3652 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
3653 src_pos, n, off);
3654 off += n;
3655 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3656 dst_pos, size - dst_pos, off);
3657 off += size - dst_pos;
3658 } else if (t == src_type) {
3659 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3660 0, src_pos, off);
3661 off += src_pos;
3662 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3663 src_pos + n, size - src_pos - n, off);
3664 off += size - src_pos - n;
3665 } else {
3666 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3667 off += size;
3668 }
3669 }
3670 isl_dim_map_div(dim_map, bmap, off);
3671
3672 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3673 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3674 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3675 if (!bmap)
3676 goto error;
3677
3678 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3679 src_type, src_pos, n);
3680 if (!bmap->dim)
3681 goto error;
3682
3683 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
3684 bmap = isl_basic_map_gauss(bmap, NULL);
3685 bmap = isl_basic_map_finalize(bmap);
3686
3687 return bmap;
3688error:
3689 isl_basic_map_free(bmap);
3690 return NULL;
3691}
3692
3693__isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
3694 enum isl_dim_type dst_type, unsigned dst_pos,
3695 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3696{
Tobias Grosser06e15922016-11-16 11:06:47 +00003697 isl_basic_map *bmap = bset_to_bmap(bset);
3698 bmap = isl_basic_map_move_dims(bmap, dst_type, dst_pos,
3699 src_type, src_pos, n);
3700 return bset_from_bmap(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +00003701}
3702
3703__isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
3704 enum isl_dim_type dst_type, unsigned dst_pos,
3705 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3706{
3707 if (!set)
3708 return NULL;
3709 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
Tobias Grosser06e15922016-11-16 11:06:47 +00003710 return set_from_map(isl_map_move_dims(set_to_map(set),
3711 dst_type, dst_pos, src_type, src_pos, n));
Tobias Grosser52a25232015-02-04 20:55:43 +00003712error:
3713 isl_set_free(set);
3714 return NULL;
3715}
3716
3717__isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
3718 enum isl_dim_type dst_type, unsigned dst_pos,
3719 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3720{
3721 int i;
3722
3723 if (!map)
3724 return NULL;
3725 if (n == 0)
3726 return map;
3727
3728 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
3729 goto error);
3730
3731 if (dst_type == src_type && dst_pos == src_pos)
3732 return map;
3733
3734 isl_assert(map->ctx, dst_type != src_type, goto error);
3735
3736 map = isl_map_cow(map);
3737 if (!map)
3738 return NULL;
3739
3740 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
3741 if (!map->dim)
3742 goto error;
3743
3744 for (i = 0; i < map->n; ++i) {
3745 map->p[i] = isl_basic_map_move_dims(map->p[i],
3746 dst_type, dst_pos,
3747 src_type, src_pos, n);
3748 if (!map->p[i])
3749 goto error;
3750 }
3751
3752 return map;
3753error:
3754 isl_map_free(map);
3755 return NULL;
3756}
3757
3758/* Move the specified dimensions to the last columns right before
3759 * the divs. Don't change the dimension specification of bmap.
3760 * That's the responsibility of the caller.
3761 */
3762static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
3763 enum isl_dim_type type, unsigned first, unsigned n)
3764{
3765 struct isl_dim_map *dim_map;
3766 struct isl_basic_map *res;
3767 enum isl_dim_type t;
3768 unsigned total, off;
3769
3770 if (!bmap)
3771 return NULL;
3772 if (pos(bmap->dim, type) + first + n ==
3773 1 + isl_space_dim(bmap->dim, isl_dim_all))
3774 return bmap;
3775
3776 total = isl_basic_map_total_dim(bmap);
3777 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3778
3779 off = 0;
3780 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3781 unsigned size = isl_space_dim(bmap->dim, t);
3782 if (t == type) {
3783 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3784 0, first, off);
3785 off += first;
3786 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3787 first, n, total - bmap->n_div - n);
3788 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3789 first + n, size - (first + n), off);
3790 off += size - (first + n);
3791 } else {
3792 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3793 off += size;
3794 }
3795 }
3796 isl_dim_map_div(dim_map, bmap, off + n);
3797
3798 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3799 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3800 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3801 return res;
3802}
3803
3804/* Insert "n" rows in the divs of "bmap".
3805 *
3806 * The number of columns is not changed, which means that the last
3807 * dimensions of "bmap" are being reintepreted as the new divs.
3808 * The space of "bmap" is not adjusted, however, which means
3809 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
3810 * from the space of "bmap" is the responsibility of the caller.
3811 */
3812static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
3813 int n)
3814{
3815 int i;
3816 size_t row_size;
3817 isl_int **new_div;
3818 isl_int *old;
3819
3820 bmap = isl_basic_map_cow(bmap);
3821 if (!bmap)
3822 return NULL;
3823
3824 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
3825 old = bmap->block2.data;
3826 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
3827 (bmap->extra + n) * (1 + row_size));
3828 if (!bmap->block2.data)
3829 return isl_basic_map_free(bmap);
3830 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
3831 if (!new_div)
3832 return isl_basic_map_free(bmap);
3833 for (i = 0; i < n; ++i) {
3834 new_div[i] = bmap->block2.data +
3835 (bmap->extra + i) * (1 + row_size);
3836 isl_seq_clr(new_div[i], 1 + row_size);
3837 }
3838 for (i = 0; i < bmap->extra; ++i)
3839 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
3840 free(bmap->div);
3841 bmap->div = new_div;
3842 bmap->n_div += n;
3843 bmap->extra += n;
3844
3845 return bmap;
3846}
3847
Tobias Grossera67ac972016-02-26 11:35:12 +00003848/* Drop constraints from "bmap" that only involve the variables
3849 * of "type" in the range [first, first + n] that are not related
3850 * to any of the variables outside that interval.
3851 * These constraints cannot influence the values for the variables
3852 * outside the interval, except in case they cause "bmap" to be empty.
3853 * Only drop the constraints if "bmap" is known to be non-empty.
3854 */
3855static __isl_give isl_basic_map *drop_irrelevant_constraints(
3856 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3857 unsigned first, unsigned n)
3858{
3859 int i;
3860 int *groups;
3861 unsigned dim, n_div;
3862 isl_bool non_empty;
3863
3864 non_empty = isl_basic_map_plain_is_non_empty(bmap);
3865 if (non_empty < 0)
3866 return isl_basic_map_free(bmap);
3867 if (!non_empty)
3868 return bmap;
3869
3870 dim = isl_basic_map_dim(bmap, isl_dim_all);
3871 n_div = isl_basic_map_dim(bmap, isl_dim_div);
3872 groups = isl_calloc_array(isl_basic_map_get_ctx(bmap), int, dim);
3873 if (!groups)
3874 return isl_basic_map_free(bmap);
3875 first += isl_basic_map_offset(bmap, type) - 1;
3876 for (i = 0; i < first; ++i)
3877 groups[i] = -1;
3878 for (i = first + n; i < dim - n_div; ++i)
3879 groups[i] = -1;
3880
3881 bmap = isl_basic_map_drop_unrelated_constraints(bmap, groups);
3882
3883 return bmap;
3884}
3885
Tobias Grosser52a25232015-02-04 20:55:43 +00003886/* Turn the n dimensions of type type, starting at first
3887 * into existentially quantified variables.
Tobias Grossera67ac972016-02-26 11:35:12 +00003888 *
3889 * If a subset of the projected out variables are unrelated
3890 * to any of the variables that remain, then the constraints
3891 * involving this subset are simply dropped first.
Tobias Grosser52a25232015-02-04 20:55:43 +00003892 */
3893__isl_give isl_basic_map *isl_basic_map_project_out(
3894 __isl_take isl_basic_map *bmap,
3895 enum isl_dim_type type, unsigned first, unsigned n)
3896{
3897 if (n == 0)
3898 return basic_map_space_reset(bmap, type);
Tobias Grossera67ac972016-02-26 11:35:12 +00003899 if (type == isl_dim_div)
3900 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3901 "cannot project out existentially quantified variables",
3902 return isl_basic_map_free(bmap));
Tobias Grosser52a25232015-02-04 20:55:43 +00003903
Tobias Grossera67ac972016-02-26 11:35:12 +00003904 bmap = drop_irrelevant_constraints(bmap, type, first, n);
Tobias Grosser52a25232015-02-04 20:55:43 +00003905 if (!bmap)
3906 return NULL;
3907
3908 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
3909 return isl_basic_map_remove_dims(bmap, type, first, n);
3910
Tobias Grosser94e53712016-12-31 07:46:11 +00003911 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
3912 return isl_basic_map_free(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +00003913
3914 bmap = move_last(bmap, type, first, n);
3915 bmap = isl_basic_map_cow(bmap);
3916 bmap = insert_div_rows(bmap, n);
3917 if (!bmap)
3918 return NULL;
3919
3920 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
3921 if (!bmap->dim)
3922 goto error;
3923 bmap = isl_basic_map_simplify(bmap);
3924 bmap = isl_basic_map_drop_redundant_divs(bmap);
3925 return isl_basic_map_finalize(bmap);
3926error:
3927 isl_basic_map_free(bmap);
3928 return NULL;
3929}
3930
3931/* Turn the n dimensions of type type, starting at first
3932 * into existentially quantified variables.
3933 */
3934struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
3935 enum isl_dim_type type, unsigned first, unsigned n)
3936{
Tobias Grosser06e15922016-11-16 11:06:47 +00003937 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset),
3938 type, first, n));
Tobias Grosser52a25232015-02-04 20:55:43 +00003939}
3940
3941/* Turn the n dimensions of type type, starting at first
3942 * into existentially quantified variables.
3943 */
3944__isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
3945 enum isl_dim_type type, unsigned first, unsigned n)
3946{
3947 int i;
3948
3949 if (!map)
3950 return NULL;
3951
3952 if (n == 0)
3953 return map_space_reset(map, type);
3954
3955 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
3956
3957 map = isl_map_cow(map);
3958 if (!map)
3959 return NULL;
3960
3961 map->dim = isl_space_drop_dims(map->dim, type, first, n);
3962 if (!map->dim)
3963 goto error;
3964
3965 for (i = 0; i < map->n; ++i) {
3966 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
3967 if (!map->p[i])
3968 goto error;
3969 }
3970
3971 return map;
3972error:
3973 isl_map_free(map);
3974 return NULL;
3975}
3976
3977/* Turn the n dimensions of type type, starting at first
3978 * into existentially quantified variables.
3979 */
3980__isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
3981 enum isl_dim_type type, unsigned first, unsigned n)
3982{
Tobias Grosser06e15922016-11-16 11:06:47 +00003983 return set_from_map(isl_map_project_out(set_to_map(set),
3984 type, first, n));
Tobias Grosser52a25232015-02-04 20:55:43 +00003985}
3986
Michael Krusee0b34f32016-05-04 14:41:36 +00003987/* Return a map that projects the elements in "set" onto their
3988 * "n" set dimensions starting at "first".
3989 * "type" should be equal to isl_dim_set.
3990 */
3991__isl_give isl_map *isl_set_project_onto_map(__isl_take isl_set *set,
3992 enum isl_dim_type type, unsigned first, unsigned n)
3993{
3994 int i;
3995 int dim;
3996 isl_map *map;
3997
3998 if (!set)
3999 return NULL;
4000 if (type != isl_dim_set)
4001 isl_die(isl_set_get_ctx(set), isl_error_invalid,
4002 "only set dimensions can be projected out", goto error);
4003 dim = isl_set_dim(set, isl_dim_set);
4004 if (first + n > dim || first + n < first)
4005 isl_die(isl_set_get_ctx(set), isl_error_invalid,
4006 "index out of bounds", goto error);
4007
4008 map = isl_map_from_domain(set);
4009 map = isl_map_add_dims(map, isl_dim_out, n);
4010 for (i = 0; i < n; ++i)
4011 map = isl_map_equate(map, isl_dim_in, first + i,
4012 isl_dim_out, i);
4013 return map;
4014error:
4015 isl_set_free(set);
4016 return NULL;
4017}
4018
Tobias Grosser52a25232015-02-04 20:55:43 +00004019static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
4020{
4021 int i, j;
4022
4023 for (i = 0; i < n; ++i) {
4024 j = isl_basic_map_alloc_div(bmap);
4025 if (j < 0)
4026 goto error;
4027 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
4028 }
4029 return bmap;
4030error:
4031 isl_basic_map_free(bmap);
4032 return NULL;
4033}
4034
4035struct isl_basic_map *isl_basic_map_apply_range(
4036 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4037{
4038 isl_space *dim_result = NULL;
4039 struct isl_basic_map *bmap;
4040 unsigned n_in, n_out, n, nparam, total, pos;
4041 struct isl_dim_map *dim_map1, *dim_map2;
4042
4043 if (!bmap1 || !bmap2)
4044 goto error;
4045 if (!isl_space_match(bmap1->dim, isl_dim_param,
4046 bmap2->dim, isl_dim_param))
4047 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4048 "parameters don't match", goto error);
4049 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_out,
4050 bmap2->dim, isl_dim_in))
4051 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4052 "spaces don't match", goto error);
4053
4054 dim_result = isl_space_join(isl_space_copy(bmap1->dim),
4055 isl_space_copy(bmap2->dim));
4056
4057 n_in = isl_basic_map_n_in(bmap1);
4058 n_out = isl_basic_map_n_out(bmap2);
4059 n = isl_basic_map_n_out(bmap1);
4060 nparam = isl_basic_map_n_param(bmap1);
4061
4062 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
4063 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4064 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
4065 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4066 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
4067 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4068 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
4069 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
4070 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4071 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4072 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4073
4074 bmap = isl_basic_map_alloc_space(dim_result,
4075 bmap1->n_div + bmap2->n_div + n,
4076 bmap1->n_eq + bmap2->n_eq,
4077 bmap1->n_ineq + bmap2->n_ineq);
4078 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4079 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4080 bmap = add_divs(bmap, n);
4081 bmap = isl_basic_map_simplify(bmap);
4082 bmap = isl_basic_map_drop_redundant_divs(bmap);
4083 return isl_basic_map_finalize(bmap);
4084error:
4085 isl_basic_map_free(bmap1);
4086 isl_basic_map_free(bmap2);
4087 return NULL;
4088}
4089
4090struct isl_basic_set *isl_basic_set_apply(
4091 struct isl_basic_set *bset, struct isl_basic_map *bmap)
4092{
4093 if (!bset || !bmap)
4094 goto error;
4095
4096 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
4097 goto error);
4098
Tobias Grosser06e15922016-11-16 11:06:47 +00004099 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset),
4100 bmap));
Tobias Grosser52a25232015-02-04 20:55:43 +00004101error:
4102 isl_basic_set_free(bset);
4103 isl_basic_map_free(bmap);
4104 return NULL;
4105}
4106
4107struct isl_basic_map *isl_basic_map_apply_domain(
4108 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4109{
4110 if (!bmap1 || !bmap2)
4111 goto error;
4112
4113 isl_assert(bmap1->ctx,
4114 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
4115 isl_assert(bmap1->ctx,
4116 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
4117 goto error);
4118
4119 bmap1 = isl_basic_map_reverse(bmap1);
4120 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
4121 return isl_basic_map_reverse(bmap1);
4122error:
4123 isl_basic_map_free(bmap1);
4124 isl_basic_map_free(bmap2);
4125 return NULL;
4126}
4127
4128/* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
4129 * A \cap B -> f(A) + f(B)
4130 */
4131struct isl_basic_map *isl_basic_map_sum(
4132 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4133{
4134 unsigned n_in, n_out, nparam, total, pos;
4135 struct isl_basic_map *bmap = NULL;
4136 struct isl_dim_map *dim_map1, *dim_map2;
4137 int i;
4138
4139 if (!bmap1 || !bmap2)
4140 goto error;
4141
4142 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
4143 goto error);
4144
4145 nparam = isl_basic_map_n_param(bmap1);
4146 n_in = isl_basic_map_n_in(bmap1);
4147 n_out = isl_basic_map_n_out(bmap1);
4148
4149 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
4150 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4151 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
4152 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4153 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
4154 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4155 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4156 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
4157 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4158 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4159 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
4160
4161 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
4162 bmap1->n_div + bmap2->n_div + 2 * n_out,
4163 bmap1->n_eq + bmap2->n_eq + n_out,
4164 bmap1->n_ineq + bmap2->n_ineq);
4165 for (i = 0; i < n_out; ++i) {
4166 int j = isl_basic_map_alloc_equality(bmap);
4167 if (j < 0)
4168 goto error;
4169 isl_seq_clr(bmap->eq[j], 1+total);
4170 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
4171 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
4172 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
4173 }
4174 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4175 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4176 bmap = add_divs(bmap, 2 * n_out);
4177
4178 bmap = isl_basic_map_simplify(bmap);
4179 return isl_basic_map_finalize(bmap);
4180error:
4181 isl_basic_map_free(bmap);
4182 isl_basic_map_free(bmap1);
4183 isl_basic_map_free(bmap2);
4184 return NULL;
4185}
4186
4187/* Given two maps A -> f(A) and B -> g(B), construct a map
4188 * A \cap B -> f(A) + f(B)
4189 */
4190struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
4191{
4192 struct isl_map *result;
4193 int i, j;
4194
4195 if (!map1 || !map2)
4196 goto error;
4197
4198 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
4199
4200 result = isl_map_alloc_space(isl_space_copy(map1->dim),
4201 map1->n * map2->n, 0);
4202 if (!result)
4203 goto error;
4204 for (i = 0; i < map1->n; ++i)
4205 for (j = 0; j < map2->n; ++j) {
4206 struct isl_basic_map *part;
4207 part = isl_basic_map_sum(
4208 isl_basic_map_copy(map1->p[i]),
4209 isl_basic_map_copy(map2->p[j]));
4210 if (isl_basic_map_is_empty(part))
4211 isl_basic_map_free(part);
4212 else
4213 result = isl_map_add_basic_map(result, part);
4214 if (!result)
4215 goto error;
4216 }
4217 isl_map_free(map1);
4218 isl_map_free(map2);
4219 return result;
4220error:
4221 isl_map_free(map1);
4222 isl_map_free(map2);
4223 return NULL;
4224}
4225
4226__isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
4227 __isl_take isl_set *set2)
4228{
Tobias Grosser06e15922016-11-16 11:06:47 +00004229 return set_from_map(isl_map_sum(set_to_map(set1), set_to_map(set2)));
Tobias Grosser52a25232015-02-04 20:55:43 +00004230}
4231
4232/* Given a basic map A -> f(A), construct A -> -f(A).
4233 */
4234struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
4235{
4236 int i, j;
4237 unsigned off, n;
4238
4239 bmap = isl_basic_map_cow(bmap);
4240 if (!bmap)
4241 return NULL;
4242
4243 n = isl_basic_map_dim(bmap, isl_dim_out);
4244 off = isl_basic_map_offset(bmap, isl_dim_out);
4245 for (i = 0; i < bmap->n_eq; ++i)
4246 for (j = 0; j < n; ++j)
4247 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
4248 for (i = 0; i < bmap->n_ineq; ++i)
4249 for (j = 0; j < n; ++j)
4250 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
4251 for (i = 0; i < bmap->n_div; ++i)
4252 for (j = 0; j < n; ++j)
4253 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
4254 bmap = isl_basic_map_gauss(bmap, NULL);
4255 return isl_basic_map_finalize(bmap);
4256}
4257
4258__isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
4259{
4260 return isl_basic_map_neg(bset);
4261}
4262
4263/* Given a map A -> f(A), construct A -> -f(A).
4264 */
4265struct isl_map *isl_map_neg(struct isl_map *map)
4266{
4267 int i;
4268
4269 map = isl_map_cow(map);
4270 if (!map)
4271 return NULL;
4272
4273 for (i = 0; i < map->n; ++i) {
4274 map->p[i] = isl_basic_map_neg(map->p[i]);
4275 if (!map->p[i])
4276 goto error;
4277 }
4278
4279 return map;
4280error:
4281 isl_map_free(map);
4282 return NULL;
4283}
4284
4285__isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
4286{
Tobias Grosser06e15922016-11-16 11:06:47 +00004287 return set_from_map(isl_map_neg(set_to_map(set)));
Tobias Grosser52a25232015-02-04 20:55:43 +00004288}
4289
4290/* Given a basic map A -> f(A) and an integer d, construct a basic map
4291 * A -> floor(f(A)/d).
4292 */
4293struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
4294 isl_int d)
4295{
4296 unsigned n_in, n_out, nparam, total, pos;
4297 struct isl_basic_map *result = NULL;
4298 struct isl_dim_map *dim_map;
4299 int i;
4300
4301 if (!bmap)
4302 return NULL;
4303
4304 nparam = isl_basic_map_n_param(bmap);
4305 n_in = isl_basic_map_n_in(bmap);
4306 n_out = isl_basic_map_n_out(bmap);
4307
4308 total = nparam + n_in + n_out + bmap->n_div + n_out;
4309 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4310 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
4311 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
4312 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
4313 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
4314
4315 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
4316 bmap->n_div + n_out,
4317 bmap->n_eq, bmap->n_ineq + 2 * n_out);
4318 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
4319 result = add_divs(result, n_out);
4320 for (i = 0; i < n_out; ++i) {
4321 int j;
4322 j = isl_basic_map_alloc_inequality(result);
4323 if (j < 0)
4324 goto error;
4325 isl_seq_clr(result->ineq[j], 1+total);
4326 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
4327 isl_int_set_si(result->ineq[j][1+pos+i], 1);
4328 j = isl_basic_map_alloc_inequality(result);
4329 if (j < 0)
4330 goto error;
4331 isl_seq_clr(result->ineq[j], 1+total);
4332 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
4333 isl_int_set_si(result->ineq[j][1+pos+i], -1);
4334 isl_int_sub_ui(result->ineq[j][0], d, 1);
4335 }
4336
4337 result = isl_basic_map_simplify(result);
4338 return isl_basic_map_finalize(result);
4339error:
4340 isl_basic_map_free(result);
4341 return NULL;
4342}
4343
4344/* Given a map A -> f(A) and an integer d, construct a map
4345 * A -> floor(f(A)/d).
4346 */
4347struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
4348{
4349 int i;
4350
4351 map = isl_map_cow(map);
4352 if (!map)
4353 return NULL;
4354
4355 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4356 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4357 for (i = 0; i < map->n; ++i) {
4358 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
4359 if (!map->p[i])
4360 goto error;
4361 }
4362
4363 return map;
4364error:
4365 isl_map_free(map);
4366 return NULL;
4367}
4368
4369/* Given a map A -> f(A) and an integer d, construct a map
4370 * A -> floor(f(A)/d).
4371 */
4372__isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
4373 __isl_take isl_val *d)
4374{
4375 if (!map || !d)
4376 goto error;
4377 if (!isl_val_is_int(d))
4378 isl_die(isl_val_get_ctx(d), isl_error_invalid,
4379 "expecting integer denominator", goto error);
4380 map = isl_map_floordiv(map, d->n);
4381 isl_val_free(d);
4382 return map;
4383error:
4384 isl_map_free(map);
4385 isl_val_free(d);
4386 return NULL;
4387}
4388
4389static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
4390{
4391 int i;
4392 unsigned nparam;
4393 unsigned n_in;
4394
4395 i = isl_basic_map_alloc_equality(bmap);
4396 if (i < 0)
4397 goto error;
4398 nparam = isl_basic_map_n_param(bmap);
4399 n_in = isl_basic_map_n_in(bmap);
4400 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
4401 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
4402 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
4403 return isl_basic_map_finalize(bmap);
4404error:
4405 isl_basic_map_free(bmap);
4406 return NULL;
4407}
4408
Tobias Grosser06e15922016-11-16 11:06:47 +00004409/* Add a constraint to "bmap" expressing i_pos < o_pos
Tobias Grosser52a25232015-02-04 20:55:43 +00004410 */
4411static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
4412{
4413 int i;
4414 unsigned nparam;
4415 unsigned n_in;
4416
4417 i = isl_basic_map_alloc_inequality(bmap);
4418 if (i < 0)
4419 goto error;
4420 nparam = isl_basic_map_n_param(bmap);
4421 n_in = isl_basic_map_n_in(bmap);
4422 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4423 isl_int_set_si(bmap->ineq[i][0], -1);
4424 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4425 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4426 return isl_basic_map_finalize(bmap);
4427error:
4428 isl_basic_map_free(bmap);
4429 return NULL;
4430}
4431
4432/* Add a constraint to "bmap" expressing i_pos <= o_pos
4433 */
4434static __isl_give isl_basic_map *var_less_or_equal(
4435 __isl_take isl_basic_map *bmap, unsigned pos)
4436{
4437 int i;
4438 unsigned nparam;
4439 unsigned n_in;
4440
4441 i = isl_basic_map_alloc_inequality(bmap);
4442 if (i < 0)
4443 goto error;
4444 nparam = isl_basic_map_n_param(bmap);
4445 n_in = isl_basic_map_n_in(bmap);
4446 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4447 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4448 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4449 return isl_basic_map_finalize(bmap);
4450error:
4451 isl_basic_map_free(bmap);
4452 return NULL;
4453}
4454
Tobias Grosser06e15922016-11-16 11:06:47 +00004455/* Add a constraint to "bmap" expressing i_pos > o_pos
Tobias Grosser52a25232015-02-04 20:55:43 +00004456 */
4457static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
4458{
4459 int i;
4460 unsigned nparam;
4461 unsigned n_in;
4462
4463 i = isl_basic_map_alloc_inequality(bmap);
4464 if (i < 0)
4465 goto error;
4466 nparam = isl_basic_map_n_param(bmap);
4467 n_in = isl_basic_map_n_in(bmap);
4468 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4469 isl_int_set_si(bmap->ineq[i][0], -1);
4470 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4471 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4472 return isl_basic_map_finalize(bmap);
4473error:
4474 isl_basic_map_free(bmap);
4475 return NULL;
4476}
4477
4478/* Add a constraint to "bmap" expressing i_pos >= o_pos
4479 */
4480static __isl_give isl_basic_map *var_more_or_equal(
4481 __isl_take isl_basic_map *bmap, unsigned pos)
4482{
4483 int i;
4484 unsigned nparam;
4485 unsigned n_in;
4486
4487 i = isl_basic_map_alloc_inequality(bmap);
4488 if (i < 0)
4489 goto error;
4490 nparam = isl_basic_map_n_param(bmap);
4491 n_in = isl_basic_map_n_in(bmap);
4492 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4493 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4494 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4495 return isl_basic_map_finalize(bmap);
4496error:
4497 isl_basic_map_free(bmap);
4498 return NULL;
4499}
4500
4501__isl_give isl_basic_map *isl_basic_map_equal(
4502 __isl_take isl_space *dim, unsigned n_equal)
4503{
4504 int i;
4505 struct isl_basic_map *bmap;
4506 bmap = isl_basic_map_alloc_space(dim, 0, n_equal, 0);
4507 if (!bmap)
4508 return NULL;
4509 for (i = 0; i < n_equal && bmap; ++i)
4510 bmap = var_equal(bmap, i);
4511 return isl_basic_map_finalize(bmap);
4512}
4513
4514/* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4515 */
4516__isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *dim,
4517 unsigned pos)
4518{
4519 int i;
4520 struct isl_basic_map *bmap;
4521 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4522 if (!bmap)
4523 return NULL;
4524 for (i = 0; i < pos && bmap; ++i)
4525 bmap = var_equal(bmap, i);
4526 if (bmap)
4527 bmap = var_less(bmap, pos);
4528 return isl_basic_map_finalize(bmap);
4529}
4530
Tobias Grosser06e15922016-11-16 11:06:47 +00004531/* Return a relation on "dim" expressing i_[0..pos] <<= o_[0..pos]
Tobias Grosser52a25232015-02-04 20:55:43 +00004532 */
4533__isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
4534 __isl_take isl_space *dim, unsigned pos)
4535{
4536 int i;
4537 isl_basic_map *bmap;
4538
4539 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4540 for (i = 0; i < pos; ++i)
4541 bmap = var_equal(bmap, i);
4542 bmap = var_less_or_equal(bmap, pos);
4543 return isl_basic_map_finalize(bmap);
4544}
4545
Tobias Grosser06e15922016-11-16 11:06:47 +00004546/* Return a relation on "dim" expressing i_pos > o_pos
Tobias Grosser52a25232015-02-04 20:55:43 +00004547 */
4548__isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *dim,
4549 unsigned pos)
4550{
4551 int i;
4552 struct isl_basic_map *bmap;
4553 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4554 if (!bmap)
4555 return NULL;
4556 for (i = 0; i < pos && bmap; ++i)
4557 bmap = var_equal(bmap, i);
4558 if (bmap)
4559 bmap = var_more(bmap, pos);
4560 return isl_basic_map_finalize(bmap);
4561}
4562
Tobias Grosser06e15922016-11-16 11:06:47 +00004563/* Return a relation on "dim" expressing i_[0..pos] >>= o_[0..pos]
Tobias Grosser52a25232015-02-04 20:55:43 +00004564 */
4565__isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
4566 __isl_take isl_space *dim, unsigned pos)
4567{
4568 int i;
4569 isl_basic_map *bmap;
4570
4571 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4572 for (i = 0; i < pos; ++i)
4573 bmap = var_equal(bmap, i);
4574 bmap = var_more_or_equal(bmap, pos);
4575 return isl_basic_map_finalize(bmap);
4576}
4577
4578static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *dims,
4579 unsigned n, int equal)
4580{
4581 struct isl_map *map;
4582 int i;
4583
4584 if (n == 0 && equal)
4585 return isl_map_universe(dims);
4586
4587 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4588
4589 for (i = 0; i + 1 < n; ++i)
4590 map = isl_map_add_basic_map(map,
4591 isl_basic_map_less_at(isl_space_copy(dims), i));
4592 if (n > 0) {
4593 if (equal)
4594 map = isl_map_add_basic_map(map,
4595 isl_basic_map_less_or_equal_at(dims, n - 1));
4596 else
4597 map = isl_map_add_basic_map(map,
4598 isl_basic_map_less_at(dims, n - 1));
4599 } else
4600 isl_space_free(dims);
4601
4602 return map;
4603}
4604
4605static __isl_give isl_map *map_lex_lte(__isl_take isl_space *dims, int equal)
4606{
4607 if (!dims)
4608 return NULL;
4609 return map_lex_lte_first(dims, dims->n_out, equal);
4610}
4611
4612__isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
4613{
4614 return map_lex_lte_first(dim, n, 0);
4615}
4616
4617__isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
4618{
4619 return map_lex_lte_first(dim, n, 1);
4620}
4621
4622__isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
4623{
4624 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
4625}
4626
4627__isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
4628{
4629 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
4630}
4631
4632static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *dims,
4633 unsigned n, int equal)
4634{
4635 struct isl_map *map;
4636 int i;
4637
4638 if (n == 0 && equal)
4639 return isl_map_universe(dims);
4640
4641 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4642
4643 for (i = 0; i + 1 < n; ++i)
4644 map = isl_map_add_basic_map(map,
4645 isl_basic_map_more_at(isl_space_copy(dims), i));
4646 if (n > 0) {
4647 if (equal)
4648 map = isl_map_add_basic_map(map,
4649 isl_basic_map_more_or_equal_at(dims, n - 1));
4650 else
4651 map = isl_map_add_basic_map(map,
4652 isl_basic_map_more_at(dims, n - 1));
4653 } else
4654 isl_space_free(dims);
4655
4656 return map;
4657}
4658
4659static __isl_give isl_map *map_lex_gte(__isl_take isl_space *dims, int equal)
4660{
4661 if (!dims)
4662 return NULL;
4663 return map_lex_gte_first(dims, dims->n_out, equal);
4664}
4665
4666__isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
4667{
4668 return map_lex_gte_first(dim, n, 0);
4669}
4670
4671__isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
4672{
4673 return map_lex_gte_first(dim, n, 1);
4674}
4675
4676__isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
4677{
4678 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
4679}
4680
4681__isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
4682{
4683 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
4684}
4685
4686__isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
4687 __isl_take isl_set *set2)
4688{
4689 isl_map *map;
4690 map = isl_map_lex_le(isl_set_get_space(set1));
4691 map = isl_map_intersect_domain(map, set1);
4692 map = isl_map_intersect_range(map, set2);
4693 return map;
4694}
4695
4696__isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
4697 __isl_take isl_set *set2)
4698{
4699 isl_map *map;
4700 map = isl_map_lex_lt(isl_set_get_space(set1));
4701 map = isl_map_intersect_domain(map, set1);
4702 map = isl_map_intersect_range(map, set2);
4703 return map;
4704}
4705
4706__isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
4707 __isl_take isl_set *set2)
4708{
4709 isl_map *map;
4710 map = isl_map_lex_ge(isl_set_get_space(set1));
4711 map = isl_map_intersect_domain(map, set1);
4712 map = isl_map_intersect_range(map, set2);
4713 return map;
4714}
4715
4716__isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
4717 __isl_take isl_set *set2)
4718{
4719 isl_map *map;
4720 map = isl_map_lex_gt(isl_set_get_space(set1));
4721 map = isl_map_intersect_domain(map, set1);
4722 map = isl_map_intersect_range(map, set2);
4723 return map;
4724}
4725
4726__isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
4727 __isl_take isl_map *map2)
4728{
4729 isl_map *map;
4730 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
4731 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4732 map = isl_map_apply_range(map, isl_map_reverse(map2));
4733 return map;
4734}
4735
4736__isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
4737 __isl_take isl_map *map2)
4738{
4739 isl_map *map;
4740 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
4741 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4742 map = isl_map_apply_range(map, isl_map_reverse(map2));
4743 return map;
4744}
4745
4746__isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
4747 __isl_take isl_map *map2)
4748{
4749 isl_map *map;
4750 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
4751 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4752 map = isl_map_apply_range(map, isl_map_reverse(map2));
4753 return map;
4754}
4755
4756__isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
4757 __isl_take isl_map *map2)
4758{
4759 isl_map *map;
4760 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
4761 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4762 map = isl_map_apply_range(map, isl_map_reverse(map2));
4763 return map;
4764}
4765
Tobias Grossereab0943e2016-11-22 21:31:59 +00004766static __isl_give isl_basic_map *basic_map_from_basic_set(
Tobias Grosser52a25232015-02-04 20:55:43 +00004767 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4768{
4769 struct isl_basic_map *bmap;
4770
4771 bset = isl_basic_set_cow(bset);
4772 if (!bset || !dim)
4773 goto error;
4774
Tobias Grossereab0943e2016-11-22 21:31:59 +00004775 isl_assert(bset->ctx, isl_space_compatible_internal(bset->dim, dim),
4776 goto error);
Tobias Grosser52a25232015-02-04 20:55:43 +00004777 isl_space_free(bset->dim);
Tobias Grosser06e15922016-11-16 11:06:47 +00004778 bmap = bset_to_bmap(bset);
Tobias Grosser52a25232015-02-04 20:55:43 +00004779 bmap->dim = dim;
4780 return isl_basic_map_finalize(bmap);
4781error:
4782 isl_basic_set_free(bset);
4783 isl_space_free(dim);
4784 return NULL;
4785}
4786
Tobias Grossereab0943e2016-11-22 21:31:59 +00004787__isl_give isl_basic_map *isl_basic_map_from_basic_set(
4788 __isl_take isl_basic_set *bset, __isl_take isl_space *space)
4789{
4790 return basic_map_from_basic_set(bset, space);
4791}
4792
Tobias Grosser52a25232015-02-04 20:55:43 +00004793/* For a div d = floor(f/m), add the constraint
4794 *
4795 * f - m d >= 0
4796 */
4797static int add_upper_div_constraint(__isl_keep isl_basic_map *bmap,
4798 unsigned pos, isl_int *div)
4799{
4800 int i;
4801 unsigned total = isl_basic_map_total_dim(bmap);
4802
4803 i = isl_basic_map_alloc_inequality(bmap);
4804 if (i < 0)
4805 return -1;
4806 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
4807 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
4808
4809 return 0;
4810}
4811
4812/* For a div d = floor(f/m), add the constraint
4813 *
Tobias Grosser07b20952016-06-12 04:30:40 +00004814 * -(f-(m-1)) + m d >= 0
Tobias Grosser52a25232015-02-04 20:55:43 +00004815 */
4816static int add_lower_div_constraint(__isl_keep isl_basic_map *bmap,
4817 unsigned pos, isl_int *div)
4818{
4819 int i;
4820 unsigned total = isl_basic_map_total_dim(bmap);
4821
4822 i = isl_basic_map_alloc_inequality(bmap);
4823 if (i < 0)
4824 return -1;
4825 isl_seq_neg(bmap->ineq[i], div + 1, 1 + total);
4826 isl_int_set(bmap->ineq[i][1 + pos], div[0]);
4827 isl_int_add(bmap->ineq[i][0], bmap->ineq[i][0], bmap->ineq[i][1 + pos]);
4828 isl_int_sub_ui(bmap->ineq[i][0], bmap->ineq[i][0], 1);
4829
4830 return 0;
4831}
4832
4833/* For a div d = floor(f/m), add the constraints
4834 *
4835 * f - m d >= 0
Tobias Grosser07b20952016-06-12 04:30:40 +00004836 * -(f-(m-1)) + m d >= 0
Tobias Grosser52a25232015-02-04 20:55:43 +00004837 *
4838 * Note that the second constraint is the negation of
4839 *
Tobias Grosser07b20952016-06-12 04:30:40 +00004840 * f - m d >= m
Tobias Grosser52a25232015-02-04 20:55:43 +00004841 */
4842int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
4843 unsigned pos, isl_int *div)
4844{
4845 if (add_upper_div_constraint(bmap, pos, div) < 0)
4846 return -1;
4847 if (add_lower_div_constraint(bmap, pos, div) < 0)
4848 return -1;
4849 return 0;
4850}
4851
4852int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
4853 unsigned pos, isl_int *div)
4854{
Tobias Grosser06e15922016-11-16 11:06:47 +00004855 return isl_basic_map_add_div_constraints_var(bset_to_bmap(bset),
Tobias Grosser52a25232015-02-04 20:55:43 +00004856 pos, div);
4857}
4858
4859int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
4860{
4861 unsigned total = isl_basic_map_total_dim(bmap);
4862 unsigned div_pos = total - bmap->n_div + div;
4863
4864 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
4865 bmap->div[div]);
4866}
4867
4868/* For each known div d = floor(f/m), add the constraints
4869 *
4870 * f - m d >= 0
Tobias Grosser07b20952016-06-12 04:30:40 +00004871 * -(f-(m-1)) + m d >= 0
Michael Kruse959a8dc2016-01-15 15:54:45 +00004872 *
4873 * Remove duplicate constraints in case of some these div constraints
4874 * already appear in "bmap".
Tobias Grosser52a25232015-02-04 20:55:43 +00004875 */
4876__isl_give isl_basic_map *isl_basic_map_add_known_div_constraints(
4877 __isl_take isl_basic_map *bmap)
4878{
Tobias Grosser52a25232015-02-04 20:55:43 +00004879 unsigned n_div;
4880
4881 if (!bmap)
4882 return NULL;
4883 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4884 if (n_div == 0)
4885 return bmap;
Tobias Grosser52a25232015-02-04 20:55:43 +00004886
Michael Kruse959a8dc2016-01-15 15:54:45 +00004887 bmap = add_known_div_constraints(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +00004888 bmap = isl_basic_map_remove_duplicate_constraints(bmap, NULL, 0);
4889 bmap = isl_basic_map_finalize(bmap);
4890 return bmap;
4891}
4892
4893/* Add the div constraint of sign "sign" for div "div" of "bmap".
4894 *
4895 * In particular, if this div is of the form d = floor(f/m),
4896 * then add the constraint
4897 *
4898 * f - m d >= 0
4899 *
4900 * if sign < 0 or the constraint
4901 *
Tobias Grosser07b20952016-06-12 04:30:40 +00004902 * -(f-(m-1)) + m d >= 0
Tobias Grosser52a25232015-02-04 20:55:43 +00004903 *
4904 * if sign > 0.
4905 */
4906int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map *bmap,
4907 unsigned div, int sign)
4908{
4909 unsigned total;
4910 unsigned div_pos;
4911
4912 if (!bmap)
4913 return -1;
4914
4915 total = isl_basic_map_total_dim(bmap);
4916 div_pos = total - bmap->n_div + div;
4917
4918 if (sign < 0)
4919 return add_upper_div_constraint(bmap, div_pos, bmap->div[div]);
4920 else
4921 return add_lower_div_constraint(bmap, div_pos, bmap->div[div]);
4922}
4923
4924int isl_basic_set_add_div_constraints(struct isl_basic_set *bset, unsigned div)
4925{
4926 return isl_basic_map_add_div_constraints(bset, div);
4927}
4928
4929struct isl_basic_set *isl_basic_map_underlying_set(
4930 struct isl_basic_map *bmap)
4931{
4932 if (!bmap)
4933 goto error;
4934 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
4935 bmap->n_div == 0 &&
4936 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
4937 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
Tobias Grosser06e15922016-11-16 11:06:47 +00004938 return bset_from_bmap(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +00004939 bmap = isl_basic_map_cow(bmap);
4940 if (!bmap)
4941 goto error;
4942 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
4943 if (!bmap->dim)
4944 goto error;
4945 bmap->extra -= bmap->n_div;
4946 bmap->n_div = 0;
4947 bmap = isl_basic_map_finalize(bmap);
Tobias Grosser06e15922016-11-16 11:06:47 +00004948 return bset_from_bmap(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +00004949error:
4950 isl_basic_map_free(bmap);
4951 return NULL;
4952}
4953
4954__isl_give isl_basic_set *isl_basic_set_underlying_set(
4955 __isl_take isl_basic_set *bset)
4956{
Tobias Grosser06e15922016-11-16 11:06:47 +00004957 return isl_basic_map_underlying_set(bset_to_bmap(bset));
Tobias Grosser52a25232015-02-04 20:55:43 +00004958}
4959
4960/* Replace each element in "list" by the result of applying
Tobias Grosser1fa7b972015-02-16 19:33:40 +00004961 * isl_basic_map_underlying_set to the element.
Tobias Grosser52a25232015-02-04 20:55:43 +00004962 */
Tobias Grosser1fa7b972015-02-16 19:33:40 +00004963__isl_give isl_basic_set_list *isl_basic_map_list_underlying_set(
4964 __isl_take isl_basic_map_list *list)
Tobias Grosser52a25232015-02-04 20:55:43 +00004965{
4966 int i, n;
4967
4968 if (!list)
4969 return NULL;
4970
Tobias Grosser1fa7b972015-02-16 19:33:40 +00004971 n = isl_basic_map_list_n_basic_map(list);
Tobias Grosser52a25232015-02-04 20:55:43 +00004972 for (i = 0; i < n; ++i) {
Tobias Grosser1fa7b972015-02-16 19:33:40 +00004973 isl_basic_map *bmap;
Tobias Grosser52a25232015-02-04 20:55:43 +00004974 isl_basic_set *bset;
4975
Tobias Grosser1fa7b972015-02-16 19:33:40 +00004976 bmap = isl_basic_map_list_get_basic_map(list, i);
4977 bset = isl_basic_set_underlying_set(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +00004978 list = isl_basic_set_list_set_basic_set(list, i, bset);
4979 }
4980
4981 return list;
4982}
4983
4984struct isl_basic_map *isl_basic_map_overlying_set(
4985 struct isl_basic_set *bset, struct isl_basic_map *like)
4986{
4987 struct isl_basic_map *bmap;
4988 struct isl_ctx *ctx;
4989 unsigned total;
4990 int i;
4991
4992 if (!bset || !like)
4993 goto error;
4994 ctx = bset->ctx;
4995 isl_assert(ctx, bset->n_div == 0, goto error);
4996 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
4997 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
4998 goto error);
Tobias Grosser6e3ba332015-08-11 11:31:18 +00004999 if (like->n_div == 0) {
5000 isl_space *space = isl_basic_map_get_space(like);
Tobias Grosser52a25232015-02-04 20:55:43 +00005001 isl_basic_map_free(like);
Tobias Grosser6e3ba332015-08-11 11:31:18 +00005002 return isl_basic_map_reset_space(bset, space);
Tobias Grosser52a25232015-02-04 20:55:43 +00005003 }
5004 bset = isl_basic_set_cow(bset);
5005 if (!bset)
5006 goto error;
5007 total = bset->dim->n_out + bset->extra;
Tobias Grosser06e15922016-11-16 11:06:47 +00005008 bmap = bset_to_bmap(bset);
Tobias Grosser52a25232015-02-04 20:55:43 +00005009 isl_space_free(bmap->dim);
5010 bmap->dim = isl_space_copy(like->dim);
5011 if (!bmap->dim)
5012 goto error;
5013 bmap->n_div = like->n_div;
5014 bmap->extra += like->n_div;
5015 if (bmap->extra) {
5016 unsigned ltotal;
5017 isl_int **div;
5018 ltotal = total - bmap->extra + like->extra;
5019 if (ltotal > total)
5020 ltotal = total;
5021 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
5022 bmap->extra * (1 + 1 + total));
5023 if (isl_blk_is_error(bmap->block2))
5024 goto error;
5025 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
5026 if (!div)
5027 goto error;
5028 bmap->div = div;
5029 for (i = 0; i < bmap->extra; ++i)
5030 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
5031 for (i = 0; i < like->n_div; ++i) {
5032 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
5033 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
5034 }
5035 bmap = isl_basic_map_add_known_div_constraints(bmap);
5036 }
5037 isl_basic_map_free(like);
5038 bmap = isl_basic_map_simplify(bmap);
5039 bmap = isl_basic_map_finalize(bmap);
5040 return bmap;
5041error:
5042 isl_basic_map_free(like);
5043 isl_basic_set_free(bset);
5044 return NULL;
5045}
5046
5047struct isl_basic_set *isl_basic_set_from_underlying_set(
5048 struct isl_basic_set *bset, struct isl_basic_set *like)
5049{
Tobias Grosser06e15922016-11-16 11:06:47 +00005050 return bset_from_bmap(isl_basic_map_overlying_set(bset,
5051 bset_to_bmap(like)));
Tobias Grosser52a25232015-02-04 20:55:43 +00005052}
5053
5054struct isl_set *isl_set_from_underlying_set(
5055 struct isl_set *set, struct isl_basic_set *like)
5056{
5057 int i;
5058
5059 if (!set || !like)
5060 goto error;
5061 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
5062 goto error);
5063 if (isl_space_is_equal(set->dim, like->dim) && like->n_div == 0) {
5064 isl_basic_set_free(like);
5065 return set;
5066 }
5067 set = isl_set_cow(set);
5068 if (!set)
5069 goto error;
5070 for (i = 0; i < set->n; ++i) {
5071 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
5072 isl_basic_set_copy(like));
5073 if (!set->p[i])
5074 goto error;
5075 }
5076 isl_space_free(set->dim);
5077 set->dim = isl_space_copy(like->dim);
5078 if (!set->dim)
5079 goto error;
5080 isl_basic_set_free(like);
5081 return set;
5082error:
5083 isl_basic_set_free(like);
5084 isl_set_free(set);
5085 return NULL;
5086}
5087
5088struct isl_set *isl_map_underlying_set(struct isl_map *map)
5089{
5090 int i;
5091
5092 map = isl_map_cow(map);
5093 if (!map)
5094 return NULL;
5095 map->dim = isl_space_cow(map->dim);
5096 if (!map->dim)
5097 goto error;
5098
5099 for (i = 1; i < map->n; ++i)
5100 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
5101 goto error);
5102 for (i = 0; i < map->n; ++i) {
Tobias Grosser06e15922016-11-16 11:06:47 +00005103 map->p[i] = bset_to_bmap(
5104 isl_basic_map_underlying_set(map->p[i]));
Tobias Grosser52a25232015-02-04 20:55:43 +00005105 if (!map->p[i])
5106 goto error;
5107 }
5108 if (map->n == 0)
5109 map->dim = isl_space_underlying(map->dim, 0);
5110 else {
5111 isl_space_free(map->dim);
5112 map->dim = isl_space_copy(map->p[0]->dim);
5113 }
5114 if (!map->dim)
5115 goto error;
Tobias Grosser06e15922016-11-16 11:06:47 +00005116 return set_from_map(map);
Tobias Grosser52a25232015-02-04 20:55:43 +00005117error:
5118 isl_map_free(map);
5119 return NULL;
5120}
5121
5122struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
5123{
Tobias Grosser06e15922016-11-16 11:06:47 +00005124 return set_from_map(isl_map_underlying_set(set_to_map(set)));
Tobias Grosser52a25232015-02-04 20:55:43 +00005125}
5126
Michael Kruse959a8dc2016-01-15 15:54:45 +00005127/* Replace the space of "bmap" by "space".
5128 *
5129 * If the space of "bmap" is identical to "space" (including the identifiers
5130 * of the input and output dimensions), then simply return the original input.
5131 */
Tobias Grosser52a25232015-02-04 20:55:43 +00005132__isl_give isl_basic_map *isl_basic_map_reset_space(
Tobias Grosser6e3ba332015-08-11 11:31:18 +00005133 __isl_take isl_basic_map *bmap, __isl_take isl_space *space)
Tobias Grosser52a25232015-02-04 20:55:43 +00005134{
Tobias Grosser6e3ba332015-08-11 11:31:18 +00005135 isl_bool equal;
5136
5137 if (!bmap)
5138 goto error;
5139 equal = isl_space_is_equal(bmap->dim, space);
Michael Kruse959a8dc2016-01-15 15:54:45 +00005140 if (equal >= 0 && equal)
5141 equal = isl_space_match(bmap->dim, isl_dim_in,
5142 space, isl_dim_in);
5143 if (equal >= 0 && equal)
5144 equal = isl_space_match(bmap->dim, isl_dim_out,
5145 space, isl_dim_out);
Tobias Grosser6e3ba332015-08-11 11:31:18 +00005146 if (equal < 0)
5147 goto error;
5148 if (equal) {
5149 isl_space_free(space);
5150 return bmap;
5151 }
Tobias Grosser52a25232015-02-04 20:55:43 +00005152 bmap = isl_basic_map_cow(bmap);
Tobias Grosser6e3ba332015-08-11 11:31:18 +00005153 if (!bmap || !space)
Tobias Grosser52a25232015-02-04 20:55:43 +00005154 goto error;
5155
5156 isl_space_free(bmap->dim);
Tobias Grosser6e3ba332015-08-11 11:31:18 +00005157 bmap->dim = space;
Tobias Grosser52a25232015-02-04 20:55:43 +00005158
5159 bmap = isl_basic_map_finalize(bmap);
5160
5161 return bmap;
5162error:
5163 isl_basic_map_free(bmap);
Tobias Grosser6e3ba332015-08-11 11:31:18 +00005164 isl_space_free(space);
Tobias Grosser52a25232015-02-04 20:55:43 +00005165 return NULL;
5166}
5167
5168__isl_give isl_basic_set *isl_basic_set_reset_space(
5169 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
5170{
Tobias Grosser06e15922016-11-16 11:06:47 +00005171 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset),
5172 dim));
Tobias Grosser52a25232015-02-04 20:55:43 +00005173}
5174
5175__isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
5176 __isl_take isl_space *dim)
5177{
5178 int i;
5179
5180 map = isl_map_cow(map);
5181 if (!map || !dim)
5182 goto error;
5183
5184 for (i = 0; i < map->n; ++i) {
5185 map->p[i] = isl_basic_map_reset_space(map->p[i],
5186 isl_space_copy(dim));
5187 if (!map->p[i])
5188 goto error;
5189 }
5190 isl_space_free(map->dim);
5191 map->dim = dim;
5192
5193 return map;
5194error:
5195 isl_map_free(map);
5196 isl_space_free(dim);
5197 return NULL;
5198}
5199
5200__isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
5201 __isl_take isl_space *dim)
5202{
Tobias Grosser06e15922016-11-16 11:06:47 +00005203 return set_from_map(isl_map_reset_space(set_to_map(set), dim));
Tobias Grosser52a25232015-02-04 20:55:43 +00005204}
5205
5206/* Compute the parameter domain of the given basic set.
5207 */
5208__isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
5209{
5210 isl_space *space;
5211 unsigned n;
5212
5213 if (isl_basic_set_is_params(bset))
5214 return bset;
5215
5216 n = isl_basic_set_dim(bset, isl_dim_set);
5217 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
5218 space = isl_basic_set_get_space(bset);
5219 space = isl_space_params(space);
5220 bset = isl_basic_set_reset_space(bset, space);
5221 return bset;
5222}
5223
5224/* Construct a zero-dimensional basic set with the given parameter domain.
5225 */
5226__isl_give isl_basic_set *isl_basic_set_from_params(
5227 __isl_take isl_basic_set *bset)
5228{
5229 isl_space *space;
5230 space = isl_basic_set_get_space(bset);
5231 space = isl_space_set_from_params(space);
5232 bset = isl_basic_set_reset_space(bset, space);
5233 return bset;
5234}
5235
5236/* Compute the parameter domain of the given set.
5237 */
5238__isl_give isl_set *isl_set_params(__isl_take isl_set *set)
5239{
5240 isl_space *space;
5241 unsigned n;
5242
5243 if (isl_set_is_params(set))
5244 return set;
5245
5246 n = isl_set_dim(set, isl_dim_set);
5247 set = isl_set_project_out(set, isl_dim_set, 0, n);
5248 space = isl_set_get_space(set);
5249 space = isl_space_params(space);
5250 set = isl_set_reset_space(set, space);
5251 return set;
5252}
5253
5254/* Construct a zero-dimensional set with the given parameter domain.
5255 */
5256__isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
5257{
5258 isl_space *space;
5259 space = isl_set_get_space(set);
5260 space = isl_space_set_from_params(space);
5261 set = isl_set_reset_space(set, space);
5262 return set;
5263}
5264
5265/* Compute the parameter domain of the given map.
5266 */
5267__isl_give isl_set *isl_map_params(__isl_take isl_map *map)
5268{
5269 isl_space *space;
5270 unsigned n;
5271
5272 n = isl_map_dim(map, isl_dim_in);
5273 map = isl_map_project_out(map, isl_dim_in, 0, n);
5274 n = isl_map_dim(map, isl_dim_out);
5275 map = isl_map_project_out(map, isl_dim_out, 0, n);
5276 space = isl_map_get_space(map);
5277 space = isl_space_params(space);
5278 map = isl_map_reset_space(map, space);
5279 return map;
5280}
5281
5282struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
5283{
Tobias Grosserb2f39922015-05-28 13:32:11 +00005284 isl_space *space;
Tobias Grosser52a25232015-02-04 20:55:43 +00005285 unsigned n_out;
5286
5287 if (!bmap)
5288 return NULL;
Tobias Grosserb2f39922015-05-28 13:32:11 +00005289 space = isl_space_domain(isl_basic_map_get_space(bmap));
Tobias Grosser52a25232015-02-04 20:55:43 +00005290
Tobias Grosser52a25232015-02-04 20:55:43 +00005291 n_out = isl_basic_map_n_out(bmap);
Tobias Grosserb2f39922015-05-28 13:32:11 +00005292 bmap = isl_basic_map_project_out(bmap, isl_dim_out, 0, n_out);
Tobias Grosser52a25232015-02-04 20:55:43 +00005293
Tobias Grosserb2f39922015-05-28 13:32:11 +00005294 return isl_basic_map_reset_space(bmap, space);
Tobias Grosser52a25232015-02-04 20:55:43 +00005295}
5296
5297int isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
5298{
5299 if (!bmap)
5300 return -1;
5301 return isl_space_may_be_set(bmap->dim);
5302}
5303
5304/* Is this basic map actually a set?
5305 * Users should never call this function. Outside of isl,
5306 * the type should indicate whether something is a set or a map.
5307 */
5308int isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
5309{
5310 if (!bmap)
5311 return -1;
5312 return isl_space_is_set(bmap->dim);
5313}
5314
5315struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
5316{
5317 if (!bmap)
5318 return NULL;
5319 if (isl_basic_map_is_set(bmap))
5320 return bmap;
5321 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
5322}
5323
5324__isl_give isl_basic_map *isl_basic_map_domain_map(
5325 __isl_take isl_basic_map *bmap)
5326{
5327 int i, k;
5328 isl_space *dim;
5329 isl_basic_map *domain;
5330 int nparam, n_in, n_out;
5331 unsigned total;
5332
5333 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5334 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5335 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5336
5337 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
5338 domain = isl_basic_map_universe(dim);
5339
5340 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5341 bmap = isl_basic_map_apply_range(bmap, domain);
5342 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
5343
5344 total = isl_basic_map_total_dim(bmap);
5345
5346 for (i = 0; i < n_in; ++i) {
5347 k = isl_basic_map_alloc_equality(bmap);
5348 if (k < 0)
5349 goto error;
5350 isl_seq_clr(bmap->eq[k], 1 + total);
5351 isl_int_set_si(bmap->eq[k][1 + nparam + i], -1);
5352 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
5353 }
5354
5355 bmap = isl_basic_map_gauss(bmap, NULL);
5356 return isl_basic_map_finalize(bmap);
5357error:
5358 isl_basic_map_free(bmap);
5359 return NULL;
5360}
5361
5362__isl_give isl_basic_map *isl_basic_map_range_map(
5363 __isl_take isl_basic_map *bmap)
5364{
5365 int i, k;
5366 isl_space *dim;
5367 isl_basic_map *range;
5368 int nparam, n_in, n_out;
5369 unsigned total;
5370
5371 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5372 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5373 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5374
5375 dim = isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap)));
5376 range = isl_basic_map_universe(dim);
5377
5378 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5379 bmap = isl_basic_map_apply_range(bmap, range);
5380 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
5381
5382 total = isl_basic_map_total_dim(bmap);
5383
5384 for (i = 0; i < n_out; ++i) {
5385 k = isl_basic_map_alloc_equality(bmap);
5386 if (k < 0)
5387 goto error;
5388 isl_seq_clr(bmap->eq[k], 1 + total);
5389 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + i], -1);
5390 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
5391 }
5392
5393 bmap = isl_basic_map_gauss(bmap, NULL);
5394 return isl_basic_map_finalize(bmap);
5395error:
5396 isl_basic_map_free(bmap);
5397 return NULL;
5398}
5399
5400int isl_map_may_be_set(__isl_keep isl_map *map)
5401{
5402 if (!map)
5403 return -1;
5404 return isl_space_may_be_set(map->dim);
5405}
5406
5407/* Is this map actually a set?
5408 * Users should never call this function. Outside of isl,
5409 * the type should indicate whether something is a set or a map.
5410 */
5411int isl_map_is_set(__isl_keep isl_map *map)
5412{
5413 if (!map)
5414 return -1;
5415 return isl_space_is_set(map->dim);
5416}
5417
5418struct isl_set *isl_map_range(struct isl_map *map)
5419{
5420 int i;
5421 struct isl_set *set;
5422
5423 if (!map)
5424 goto error;
5425 if (isl_map_is_set(map))
Tobias Grosser06e15922016-11-16 11:06:47 +00005426 return set_from_map(map);
Tobias Grosser52a25232015-02-04 20:55:43 +00005427
5428 map = isl_map_cow(map);
5429 if (!map)
5430 goto error;
5431
Tobias Grosser06e15922016-11-16 11:06:47 +00005432 set = set_from_map(map);
Tobias Grosser52a25232015-02-04 20:55:43 +00005433 set->dim = isl_space_range(set->dim);
5434 if (!set->dim)
5435 goto error;
5436 for (i = 0; i < map->n; ++i) {
5437 set->p[i] = isl_basic_map_range(map->p[i]);
5438 if (!set->p[i])
5439 goto error;
5440 }
5441 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5442 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5443 return set;
5444error:
5445 isl_map_free(map);
5446 return NULL;
5447}
5448
5449__isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
5450{
5451 int i;
5452
5453 map = isl_map_cow(map);
5454 if (!map)
5455 return NULL;
5456
5457 map->dim = isl_space_domain_map(map->dim);
5458 if (!map->dim)
5459 goto error;
5460 for (i = 0; i < map->n; ++i) {
5461 map->p[i] = isl_basic_map_domain_map(map->p[i]);
5462 if (!map->p[i])
5463 goto error;
5464 }
5465 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5466 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5467 return map;
5468error:
5469 isl_map_free(map);
5470 return NULL;
5471}
5472
5473__isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
5474{
5475 int i;
5476 isl_space *range_dim;
5477
5478 map = isl_map_cow(map);
5479 if (!map)
5480 return NULL;
5481
5482 range_dim = isl_space_range(isl_map_get_space(map));
5483 range_dim = isl_space_from_range(range_dim);
5484 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
5485 map->dim = isl_space_join(map->dim, range_dim);
5486 if (!map->dim)
5487 goto error;
5488 for (i = 0; i < map->n; ++i) {
5489 map->p[i] = isl_basic_map_range_map(map->p[i]);
5490 if (!map->p[i])
5491 goto error;
5492 }
5493 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5494 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5495 return map;
5496error:
5497 isl_map_free(map);
5498 return NULL;
5499}
5500
5501/* Given a wrapped map of the form A[B -> C],
5502 * return the map A[B -> C] -> B.
5503 */
5504__isl_give isl_map *isl_set_wrapped_domain_map(__isl_take isl_set *set)
5505{
5506 isl_id *id;
5507 isl_map *map;
5508
5509 if (!set)
5510 return NULL;
5511 if (!isl_set_has_tuple_id(set))
5512 return isl_map_domain_map(isl_set_unwrap(set));
5513
5514 id = isl_set_get_tuple_id(set);
5515 map = isl_map_domain_map(isl_set_unwrap(set));
5516 map = isl_map_set_tuple_id(map, isl_dim_in, id);
5517
5518 return map;
5519}
5520
5521__isl_give isl_map *isl_map_from_set(__isl_take isl_set *set,
5522 __isl_take isl_space *dim)
5523{
5524 int i;
5525 struct isl_map *map = NULL;
5526
5527 set = isl_set_cow(set);
5528 if (!set || !dim)
5529 goto error;
Tobias Grossereab0943e2016-11-22 21:31:59 +00005530 isl_assert(set->ctx, isl_space_compatible_internal(set->dim, dim),
5531 goto error);
Tobias Grosser06e15922016-11-16 11:06:47 +00005532 map = set_to_map(set);
Tobias Grosser52a25232015-02-04 20:55:43 +00005533 for (i = 0; i < set->n; ++i) {
Tobias Grossereab0943e2016-11-22 21:31:59 +00005534 map->p[i] = basic_map_from_basic_set(
Tobias Grosser52a25232015-02-04 20:55:43 +00005535 set->p[i], isl_space_copy(dim));
5536 if (!map->p[i])
5537 goto error;
5538 }
5539 isl_space_free(map->dim);
5540 map->dim = dim;
5541 return map;
5542error:
5543 isl_space_free(dim);
5544 isl_set_free(set);
5545 return NULL;
5546}
5547
5548__isl_give isl_basic_map *isl_basic_map_from_domain(
5549 __isl_take isl_basic_set *bset)
5550{
5551 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
5552}
5553
5554__isl_give isl_basic_map *isl_basic_map_from_range(
5555 __isl_take isl_basic_set *bset)
5556{
5557 isl_space *space;
5558 space = isl_basic_set_get_space(bset);
5559 space = isl_space_from_range(space);
5560 bset = isl_basic_set_reset_space(bset, space);
Tobias Grosser06e15922016-11-16 11:06:47 +00005561 return bset_to_bmap(bset);
Tobias Grosser52a25232015-02-04 20:55:43 +00005562}
5563
5564/* Create a relation with the given set as range.
5565 * The domain of the created relation is a zero-dimensional
5566 * flat anonymous space.
5567 */
5568__isl_give isl_map *isl_map_from_range(__isl_take isl_set *set)
5569{
5570 isl_space *space;
5571 space = isl_set_get_space(set);
5572 space = isl_space_from_range(space);
5573 set = isl_set_reset_space(set, space);
Tobias Grosser06e15922016-11-16 11:06:47 +00005574 return set_to_map(set);
Tobias Grosser52a25232015-02-04 20:55:43 +00005575}
5576
5577/* Create a relation with the given set as domain.
5578 * The range of the created relation is a zero-dimensional
5579 * flat anonymous space.
5580 */
5581__isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
5582{
5583 return isl_map_reverse(isl_map_from_range(set));
5584}
5585
5586__isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
5587 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
5588{
5589 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
5590}
5591
5592__isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
5593 __isl_take isl_set *range)
5594{
5595 return isl_map_apply_range(isl_map_reverse(domain), range);
5596}
5597
Tobias Grosser07b20952016-06-12 04:30:40 +00005598/* Return a newly allocated isl_map with given space and flags and
5599 * room for "n" basic maps.
5600 * Make sure that all cached information is cleared.
5601 */
5602__isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *space, int n,
Tobias Grosser52a25232015-02-04 20:55:43 +00005603 unsigned flags)
5604{
5605 struct isl_map *map;
5606
Tobias Grosser07b20952016-06-12 04:30:40 +00005607 if (!space)
Tobias Grosser52a25232015-02-04 20:55:43 +00005608 return NULL;
5609 if (n < 0)
Tobias Grosser07b20952016-06-12 04:30:40 +00005610 isl_die(space->ctx, isl_error_internal,
Tobias Grosser52a25232015-02-04 20:55:43 +00005611 "negative number of basic maps", goto error);
Tobias Grosser07b20952016-06-12 04:30:40 +00005612 map = isl_calloc(space->ctx, struct isl_map,
Tobias Grosser52a25232015-02-04 20:55:43 +00005613 sizeof(struct isl_map) +
5614 (n - 1) * sizeof(struct isl_basic_map *));
5615 if (!map)
5616 goto error;
5617
Tobias Grosser07b20952016-06-12 04:30:40 +00005618 map->ctx = space->ctx;
Tobias Grosser52a25232015-02-04 20:55:43 +00005619 isl_ctx_ref(map->ctx);
5620 map->ref = 1;
5621 map->size = n;
5622 map->n = 0;
Tobias Grosser07b20952016-06-12 04:30:40 +00005623 map->dim = space;
Tobias Grosser52a25232015-02-04 20:55:43 +00005624 map->flags = flags;
5625 return map;
5626error:
Tobias Grosser07b20952016-06-12 04:30:40 +00005627 isl_space_free(space);
Tobias Grosser52a25232015-02-04 20:55:43 +00005628 return NULL;
5629}
5630
5631struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
5632 unsigned nparam, unsigned in, unsigned out, int n,
5633 unsigned flags)
5634{
5635 struct isl_map *map;
5636 isl_space *dims;
5637
5638 dims = isl_space_alloc(ctx, nparam, in, out);
5639 if (!dims)
5640 return NULL;
5641
5642 map = isl_map_alloc_space(dims, n, flags);
5643 return map;
5644}
5645
5646__isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *dim)
5647{
5648 struct isl_basic_map *bmap;
5649 bmap = isl_basic_map_alloc_space(dim, 0, 1, 0);
5650 bmap = isl_basic_map_set_to_empty(bmap);
5651 return bmap;
5652}
5653
5654__isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *dim)
5655{
5656 struct isl_basic_set *bset;
5657 bset = isl_basic_set_alloc_space(dim, 0, 1, 0);
5658 bset = isl_basic_set_set_to_empty(bset);
5659 return bset;
5660}
5661
Tobias Grosser52a25232015-02-04 20:55:43 +00005662__isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *dim)
5663{
5664 struct isl_basic_map *bmap;
5665 bmap = isl_basic_map_alloc_space(dim, 0, 0, 0);
5666 bmap = isl_basic_map_finalize(bmap);
5667 return bmap;
5668}
5669
5670__isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *dim)
5671{
5672 struct isl_basic_set *bset;
5673 bset = isl_basic_set_alloc_space(dim, 0, 0, 0);
5674 bset = isl_basic_set_finalize(bset);
5675 return bset;
5676}
5677
5678__isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_space *dim)
5679{
5680 int i;
5681 unsigned total = isl_space_dim(dim, isl_dim_all);
5682 isl_basic_map *bmap;
5683
5684 bmap= isl_basic_map_alloc_space(dim, 0, 0, total);
5685 for (i = 0; i < total; ++i) {
5686 int k = isl_basic_map_alloc_inequality(bmap);
5687 if (k < 0)
5688 goto error;
5689 isl_seq_clr(bmap->ineq[k], 1 + total);
5690 isl_int_set_si(bmap->ineq[k][1 + i], 1);
5691 }
5692 return bmap;
5693error:
5694 isl_basic_map_free(bmap);
5695 return NULL;
5696}
5697
5698__isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_space *dim)
5699{
5700 return isl_basic_map_nat_universe(dim);
5701}
5702
5703__isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
5704{
5705 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
5706}
5707
5708__isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
5709{
5710 return isl_map_nat_universe(dim);
5711}
5712
Tobias Grosser52a25232015-02-04 20:55:43 +00005713__isl_give isl_map *isl_map_empty(__isl_take isl_space *dim)
5714{
5715 return isl_map_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5716}
5717
Tobias Grosser52a25232015-02-04 20:55:43 +00005718__isl_give isl_set *isl_set_empty(__isl_take isl_space *dim)
5719{
5720 return isl_set_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5721}
5722
Tobias Grosser52a25232015-02-04 20:55:43 +00005723__isl_give isl_map *isl_map_universe(__isl_take isl_space *dim)
5724{
5725 struct isl_map *map;
5726 if (!dim)
5727 return NULL;
5728 map = isl_map_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5729 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
5730 return map;
5731}
5732
5733__isl_give isl_set *isl_set_universe(__isl_take isl_space *dim)
5734{
5735 struct isl_set *set;
5736 if (!dim)
5737 return NULL;
5738 set = isl_set_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5739 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
5740 return set;
5741}
5742
Tobias Grosser52a25232015-02-04 20:55:43 +00005743struct isl_map *isl_map_dup(struct isl_map *map)
5744{
5745 int i;
5746 struct isl_map *dup;
5747
5748 if (!map)
5749 return NULL;
5750 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
5751 for (i = 0; i < map->n; ++i)
5752 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
5753 return dup;
5754}
5755
5756__isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
5757 __isl_take isl_basic_map *bmap)
5758{
5759 if (!bmap || !map)
5760 goto error;
5761 if (isl_basic_map_plain_is_empty(bmap)) {
5762 isl_basic_map_free(bmap);
5763 return map;
5764 }
5765 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
5766 isl_assert(map->ctx, map->n < map->size, goto error);
5767 map->p[map->n] = bmap;
5768 map->n++;
5769 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5770 return map;
5771error:
5772 if (map)
5773 isl_map_free(map);
5774 if (bmap)
5775 isl_basic_map_free(bmap);
5776 return NULL;
5777}
5778
5779__isl_null isl_map *isl_map_free(__isl_take isl_map *map)
5780{
5781 int i;
5782
5783 if (!map)
5784 return NULL;
5785
5786 if (--map->ref > 0)
5787 return NULL;
5788
Tobias Grosser07b20952016-06-12 04:30:40 +00005789 clear_caches(map);
Tobias Grosser52a25232015-02-04 20:55:43 +00005790 isl_ctx_deref(map->ctx);
5791 for (i = 0; i < map->n; ++i)
5792 isl_basic_map_free(map->p[i]);
5793 isl_space_free(map->dim);
5794 free(map);
5795
5796 return NULL;
5797}
5798
Tobias Grosser52a25232015-02-04 20:55:43 +00005799static struct isl_basic_map *isl_basic_map_fix_pos_si(
5800 struct isl_basic_map *bmap, unsigned pos, int value)
5801{
5802 int j;
5803
5804 bmap = isl_basic_map_cow(bmap);
5805 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5806 j = isl_basic_map_alloc_equality(bmap);
5807 if (j < 0)
5808 goto error;
5809 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5810 isl_int_set_si(bmap->eq[j][pos], -1);
5811 isl_int_set_si(bmap->eq[j][0], value);
5812 bmap = isl_basic_map_simplify(bmap);
5813 return isl_basic_map_finalize(bmap);
5814error:
5815 isl_basic_map_free(bmap);
5816 return NULL;
5817}
5818
5819static __isl_give isl_basic_map *isl_basic_map_fix_pos(
5820 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
5821{
5822 int j;
5823
5824 bmap = isl_basic_map_cow(bmap);
5825 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5826 j = isl_basic_map_alloc_equality(bmap);
5827 if (j < 0)
5828 goto error;
5829 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5830 isl_int_set_si(bmap->eq[j][pos], -1);
5831 isl_int_set(bmap->eq[j][0], value);
5832 bmap = isl_basic_map_simplify(bmap);
5833 return isl_basic_map_finalize(bmap);
5834error:
5835 isl_basic_map_free(bmap);
5836 return NULL;
5837}
5838
5839struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
5840 enum isl_dim_type type, unsigned pos, int value)
5841{
Tobias Grosser94e53712016-12-31 07:46:11 +00005842 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
5843 return isl_basic_map_free(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +00005844 return isl_basic_map_fix_pos_si(bmap,
5845 isl_basic_map_offset(bmap, type) + pos, value);
Tobias Grosser52a25232015-02-04 20:55:43 +00005846}
5847
5848__isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
5849 enum isl_dim_type type, unsigned pos, isl_int value)
5850{
Tobias Grosser94e53712016-12-31 07:46:11 +00005851 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
5852 return isl_basic_map_free(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +00005853 return isl_basic_map_fix_pos(bmap,
5854 isl_basic_map_offset(bmap, type) + pos, value);
Tobias Grosser52a25232015-02-04 20:55:43 +00005855}
5856
5857/* Fix the value of the variable at position "pos" of type "type" of "bmap"
5858 * to be equal to "v".
5859 */
5860__isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
5861 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5862{
5863 if (!bmap || !v)
5864 goto error;
5865 if (!isl_val_is_int(v))
5866 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5867 "expecting integer value", goto error);
Tobias Grosser94e53712016-12-31 07:46:11 +00005868 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
5869 goto error;
Tobias Grosser52a25232015-02-04 20:55:43 +00005870 pos += isl_basic_map_offset(bmap, type);
5871 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
5872 isl_val_free(v);
5873 return bmap;
5874error:
5875 isl_basic_map_free(bmap);
5876 isl_val_free(v);
5877 return NULL;
5878}
5879
5880/* Fix the value of the variable at position "pos" of type "type" of "bset"
5881 * to be equal to "v".
5882 */
5883__isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
5884 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5885{
5886 return isl_basic_map_fix_val(bset, type, pos, v);
5887}
5888
5889struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
5890 enum isl_dim_type type, unsigned pos, int value)
5891{
Tobias Grosser06e15922016-11-16 11:06:47 +00005892 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
5893 type, pos, value));
Tobias Grosser52a25232015-02-04 20:55:43 +00005894}
5895
5896__isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
5897 enum isl_dim_type type, unsigned pos, isl_int value)
5898{
Tobias Grosser06e15922016-11-16 11:06:47 +00005899 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset),
5900 type, pos, value));
Tobias Grosser52a25232015-02-04 20:55:43 +00005901}
5902
5903struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
5904 unsigned input, int value)
5905{
5906 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
5907}
5908
5909struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
5910 unsigned dim, int value)
5911{
Tobias Grosser06e15922016-11-16 11:06:47 +00005912 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
5913 isl_dim_set, dim, value));
Tobias Grosser52a25232015-02-04 20:55:43 +00005914}
5915
5916static int remove_if_empty(__isl_keep isl_map *map, int i)
5917{
5918 int empty = isl_basic_map_plain_is_empty(map->p[i]);
5919
5920 if (empty < 0)
5921 return -1;
5922 if (!empty)
5923 return 0;
5924
5925 isl_basic_map_free(map->p[i]);
5926 if (i != map->n - 1) {
5927 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5928 map->p[i] = map->p[map->n - 1];
5929 }
5930 map->n--;
5931
5932 return 0;
5933}
5934
5935/* Perform "fn" on each basic map of "map", where we may not be holding
5936 * the only reference to "map".
5937 * In particular, "fn" should be a semantics preserving operation
5938 * that we want to apply to all copies of "map". We therefore need
5939 * to be careful not to modify "map" in a way that breaks "map"
5940 * in case anything goes wrong.
5941 */
5942__isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
5943 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
5944{
5945 struct isl_basic_map *bmap;
5946 int i;
5947
5948 if (!map)
5949 return NULL;
5950
5951 for (i = map->n - 1; i >= 0; --i) {
5952 bmap = isl_basic_map_copy(map->p[i]);
5953 bmap = fn(bmap);
5954 if (!bmap)
5955 goto error;
5956 isl_basic_map_free(map->p[i]);
5957 map->p[i] = bmap;
5958 if (remove_if_empty(map, i) < 0)
5959 goto error;
5960 }
5961
5962 return map;
5963error:
5964 isl_map_free(map);
5965 return NULL;
5966}
5967
5968struct isl_map *isl_map_fix_si(struct isl_map *map,
5969 enum isl_dim_type type, unsigned pos, int value)
5970{
5971 int i;
5972
5973 map = isl_map_cow(map);
5974 if (!map)
5975 return NULL;
5976
5977 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5978 for (i = map->n - 1; i >= 0; --i) {
5979 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
5980 if (remove_if_empty(map, i) < 0)
5981 goto error;
5982 }
5983 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5984 return map;
5985error:
5986 isl_map_free(map);
5987 return NULL;
5988}
5989
5990__isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
5991 enum isl_dim_type type, unsigned pos, int value)
5992{
Tobias Grosser06e15922016-11-16 11:06:47 +00005993 return set_from_map(isl_map_fix_si(set_to_map(set), type, pos, value));
Tobias Grosser52a25232015-02-04 20:55:43 +00005994}
5995
5996__isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
5997 enum isl_dim_type type, unsigned pos, isl_int value)
5998{
5999 int i;
6000
6001 map = isl_map_cow(map);
6002 if (!map)
6003 return NULL;
6004
6005 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
6006 for (i = 0; i < map->n; ++i) {
6007 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
6008 if (!map->p[i])
6009 goto error;
6010 }
6011 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6012 return map;
6013error:
6014 isl_map_free(map);
6015 return NULL;
6016}
6017
6018__isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
6019 enum isl_dim_type type, unsigned pos, isl_int value)
6020{
Tobias Grosser06e15922016-11-16 11:06:47 +00006021 return set_from_map(isl_map_fix(set_to_map(set), type, pos, value));
Tobias Grosser52a25232015-02-04 20:55:43 +00006022}
6023
6024/* Fix the value of the variable at position "pos" of type "type" of "map"
6025 * to be equal to "v".
6026 */
6027__isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
6028 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6029{
6030 int i;
6031
6032 map = isl_map_cow(map);
6033 if (!map || !v)
6034 goto error;
6035
6036 if (!isl_val_is_int(v))
6037 isl_die(isl_map_get_ctx(map), isl_error_invalid,
6038 "expecting integer value", goto error);
6039 if (pos >= isl_map_dim(map, type))
6040 isl_die(isl_map_get_ctx(map), isl_error_invalid,
6041 "index out of bounds", goto error);
6042 for (i = map->n - 1; i >= 0; --i) {
6043 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
6044 isl_val_copy(v));
6045 if (remove_if_empty(map, i) < 0)
6046 goto error;
6047 }
6048 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6049 isl_val_free(v);
6050 return map;
6051error:
6052 isl_map_free(map);
6053 isl_val_free(v);
6054 return NULL;
6055}
6056
6057/* Fix the value of the variable at position "pos" of type "type" of "set"
6058 * to be equal to "v".
6059 */
6060__isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
6061 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6062{
6063 return isl_map_fix_val(set, type, pos, v);
6064}
6065
6066struct isl_map *isl_map_fix_input_si(struct isl_map *map,
6067 unsigned input, int value)
6068{
6069 return isl_map_fix_si(map, isl_dim_in, input, value);
6070}
6071
6072struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
6073{
Tobias Grosser06e15922016-11-16 11:06:47 +00006074 return set_from_map(isl_map_fix_si(set_to_map(set),
6075 isl_dim_set, dim, value));
Tobias Grosser52a25232015-02-04 20:55:43 +00006076}
6077
6078static __isl_give isl_basic_map *basic_map_bound_si(
6079 __isl_take isl_basic_map *bmap,
6080 enum isl_dim_type type, unsigned pos, int value, int upper)
6081{
6082 int j;
6083
Tobias Grosser94e53712016-12-31 07:46:11 +00006084 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6085 return isl_basic_map_free(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +00006086 pos += isl_basic_map_offset(bmap, type);
6087 bmap = isl_basic_map_cow(bmap);
6088 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6089 j = isl_basic_map_alloc_inequality(bmap);
6090 if (j < 0)
6091 goto error;
6092 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
6093 if (upper) {
6094 isl_int_set_si(bmap->ineq[j][pos], -1);
6095 isl_int_set_si(bmap->ineq[j][0], value);
6096 } else {
6097 isl_int_set_si(bmap->ineq[j][pos], 1);
6098 isl_int_set_si(bmap->ineq[j][0], -value);
6099 }
6100 bmap = isl_basic_map_simplify(bmap);
6101 return isl_basic_map_finalize(bmap);
6102error:
6103 isl_basic_map_free(bmap);
6104 return NULL;
6105}
6106
6107__isl_give isl_basic_map *isl_basic_map_lower_bound_si(
6108 __isl_take isl_basic_map *bmap,
6109 enum isl_dim_type type, unsigned pos, int value)
6110{
6111 return basic_map_bound_si(bmap, type, pos, value, 0);
6112}
6113
6114/* Constrain the values of the given dimension to be no greater than "value".
6115 */
6116__isl_give isl_basic_map *isl_basic_map_upper_bound_si(
6117 __isl_take isl_basic_map *bmap,
6118 enum isl_dim_type type, unsigned pos, int value)
6119{
6120 return basic_map_bound_si(bmap, type, pos, value, 1);
6121}
6122
6123struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
6124 unsigned dim, isl_int value)
6125{
6126 int j;
6127
6128 bset = isl_basic_set_cow(bset);
6129 bset = isl_basic_set_extend_constraints(bset, 0, 1);
6130 j = isl_basic_set_alloc_inequality(bset);
6131 if (j < 0)
6132 goto error;
6133 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
6134 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
6135 isl_int_neg(bset->ineq[j][0], value);
6136 bset = isl_basic_set_simplify(bset);
6137 return isl_basic_set_finalize(bset);
6138error:
6139 isl_basic_set_free(bset);
6140 return NULL;
6141}
6142
6143static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
6144 enum isl_dim_type type, unsigned pos, int value, int upper)
6145{
6146 int i;
6147
6148 map = isl_map_cow(map);
6149 if (!map)
6150 return NULL;
6151
6152 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
6153 for (i = 0; i < map->n; ++i) {
6154 map->p[i] = basic_map_bound_si(map->p[i],
6155 type, pos, value, upper);
6156 if (!map->p[i])
6157 goto error;
6158 }
6159 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6160 return map;
6161error:
6162 isl_map_free(map);
6163 return NULL;
6164}
6165
6166__isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
6167 enum isl_dim_type type, unsigned pos, int value)
6168{
6169 return map_bound_si(map, type, pos, value, 0);
6170}
6171
6172__isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
6173 enum isl_dim_type type, unsigned pos, int value)
6174{
6175 return map_bound_si(map, type, pos, value, 1);
6176}
6177
6178__isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
6179 enum isl_dim_type type, unsigned pos, int value)
6180{
Tobias Grosser06e15922016-11-16 11:06:47 +00006181 return set_from_map(isl_map_lower_bound_si(set_to_map(set),
6182 type, pos, value));
Tobias Grosser52a25232015-02-04 20:55:43 +00006183}
6184
6185__isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
6186 enum isl_dim_type type, unsigned pos, int value)
6187{
6188 return isl_map_upper_bound_si(set, type, pos, value);
6189}
6190
6191/* Bound the given variable of "bmap" from below (or above is "upper"
6192 * is set) to "value".
6193 */
6194static __isl_give isl_basic_map *basic_map_bound(
6195 __isl_take isl_basic_map *bmap,
6196 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6197{
6198 int j;
6199
Tobias Grosser94e53712016-12-31 07:46:11 +00006200 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6201 return isl_basic_map_free(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +00006202 pos += isl_basic_map_offset(bmap, type);
6203 bmap = isl_basic_map_cow(bmap);
6204 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6205 j = isl_basic_map_alloc_inequality(bmap);
6206 if (j < 0)
6207 goto error;
6208 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
6209 if (upper) {
6210 isl_int_set_si(bmap->ineq[j][pos], -1);
6211 isl_int_set(bmap->ineq[j][0], value);
6212 } else {
6213 isl_int_set_si(bmap->ineq[j][pos], 1);
6214 isl_int_neg(bmap->ineq[j][0], value);
6215 }
6216 bmap = isl_basic_map_simplify(bmap);
6217 return isl_basic_map_finalize(bmap);
6218error:
6219 isl_basic_map_free(bmap);
6220 return NULL;
6221}
6222
6223/* Bound the given variable of "map" from below (or above is "upper"
6224 * is set) to "value".
6225 */
6226static __isl_give isl_map *map_bound(__isl_take isl_map *map,
6227 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6228{
6229 int i;
6230
6231 map = isl_map_cow(map);
6232 if (!map)
6233 return NULL;
6234
6235 if (pos >= isl_map_dim(map, type))
6236 isl_die(map->ctx, isl_error_invalid,
6237 "index out of bounds", goto error);
6238 for (i = map->n - 1; i >= 0; --i) {
6239 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
6240 if (remove_if_empty(map, i) < 0)
6241 goto error;
6242 }
6243 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6244 return map;
6245error:
6246 isl_map_free(map);
6247 return NULL;
6248}
6249
6250__isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
6251 enum isl_dim_type type, unsigned pos, isl_int value)
6252{
6253 return map_bound(map, type, pos, value, 0);
6254}
6255
6256__isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
6257 enum isl_dim_type type, unsigned pos, isl_int value)
6258{
6259 return map_bound(map, type, pos, value, 1);
6260}
6261
6262__isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
6263 enum isl_dim_type type, unsigned pos, isl_int value)
6264{
6265 return isl_map_lower_bound(set, type, pos, value);
6266}
6267
6268__isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
6269 enum isl_dim_type type, unsigned pos, isl_int value)
6270{
6271 return isl_map_upper_bound(set, type, pos, value);
6272}
6273
6274/* Force the values of the variable at position "pos" of type "type" of "set"
6275 * to be no smaller than "value".
6276 */
6277__isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
6278 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6279{
6280 if (!value)
6281 goto error;
6282 if (!isl_val_is_int(value))
6283 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6284 "expecting integer value", goto error);
6285 set = isl_set_lower_bound(set, type, pos, value->n);
6286 isl_val_free(value);
6287 return set;
6288error:
6289 isl_val_free(value);
6290 isl_set_free(set);
6291 return NULL;
6292}
6293
6294/* Force the values of the variable at position "pos" of type "type" of "set"
6295 * to be no greater than "value".
6296 */
6297__isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
6298 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6299{
6300 if (!value)
6301 goto error;
6302 if (!isl_val_is_int(value))
6303 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6304 "expecting integer value", goto error);
6305 set = isl_set_upper_bound(set, type, pos, value->n);
6306 isl_val_free(value);
6307 return set;
6308error:
6309 isl_val_free(value);
6310 isl_set_free(set);
6311 return NULL;
6312}
6313
6314struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
6315 isl_int value)
6316{
6317 int i;
6318
6319 set = isl_set_cow(set);
6320 if (!set)
6321 return NULL;
6322
6323 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
6324 for (i = 0; i < set->n; ++i) {
6325 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
6326 if (!set->p[i])
6327 goto error;
6328 }
6329 return set;
6330error:
6331 isl_set_free(set);
6332 return NULL;
6333}
6334
6335struct isl_map *isl_map_reverse(struct isl_map *map)
6336{
6337 int i;
6338
6339 map = isl_map_cow(map);
6340 if (!map)
6341 return NULL;
6342
6343 map->dim = isl_space_reverse(map->dim);
6344 if (!map->dim)
6345 goto error;
6346 for (i = 0; i < map->n; ++i) {
6347 map->p[i] = isl_basic_map_reverse(map->p[i]);
6348 if (!map->p[i])
6349 goto error;
6350 }
6351 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6352 return map;
6353error:
6354 isl_map_free(map);
6355 return NULL;
6356}
6357
Tobias Grosser52a25232015-02-04 20:55:43 +00006358#undef TYPE
6359#define TYPE isl_pw_multi_aff
6360#undef SUFFIX
6361#define SUFFIX _pw_multi_aff
6362#undef EMPTY
6363#define EMPTY isl_pw_multi_aff_empty
6364#undef ADD
6365#define ADD isl_pw_multi_aff_union_add
6366#include "isl_map_lexopt_templ.c"
6367
6368/* Given a map "map", compute the lexicographically minimal
6369 * (or maximal) image element for each domain element in dom,
6370 * in the form of an isl_pw_multi_aff.
Tobias Grosser37034db2016-03-25 19:38:18 +00006371 * If "empty" is not NULL, then set *empty to those elements in dom that
6372 * do not have an image element.
Tobias Grosser932ec012016-07-06 09:11:00 +00006373 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6374 * should be computed over the domain of "map". "empty" is also NULL
6375 * in this case.
Tobias Grosser52a25232015-02-04 20:55:43 +00006376 *
6377 * We first compute the lexicographically minimal or maximal element
6378 * in the first basic map. This results in a partial solution "res"
6379 * and a subset "todo" of dom that still need to be handled.
6380 * We then consider each of the remaining maps in "map" and successively
6381 * update both "res" and "todo".
Tobias Grosser37034db2016-03-25 19:38:18 +00006382 * If "empty" is NULL, then the todo sets are not needed and therefore
6383 * also not computed.
Tobias Grosser52a25232015-02-04 20:55:43 +00006384 */
6385static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
6386 __isl_take isl_map *map, __isl_take isl_set *dom,
Tobias Grosser932ec012016-07-06 09:11:00 +00006387 __isl_give isl_set **empty, unsigned flags)
Tobias Grosser52a25232015-02-04 20:55:43 +00006388{
6389 int i;
Tobias Grosser932ec012016-07-06 09:11:00 +00006390 int full;
Tobias Grosser52a25232015-02-04 20:55:43 +00006391 isl_pw_multi_aff *res;
6392 isl_set *todo;
6393
Tobias Grosser932ec012016-07-06 09:11:00 +00006394 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6395 if (!map || (!full && !dom))
Tobias Grosser52a25232015-02-04 20:55:43 +00006396 goto error;
6397
6398 if (isl_map_plain_is_empty(map)) {
6399 if (empty)
6400 *empty = dom;
6401 else
6402 isl_set_free(dom);
6403 return isl_pw_multi_aff_from_map(map);
6404 }
6405
6406 res = basic_map_partial_lexopt_pw_multi_aff(
6407 isl_basic_map_copy(map->p[0]),
Tobias Grosser932ec012016-07-06 09:11:00 +00006408 isl_set_copy(dom), empty, flags);
Tobias Grosser52a25232015-02-04 20:55:43 +00006409
Tobias Grosser37034db2016-03-25 19:38:18 +00006410 if (empty)
6411 todo = *empty;
Tobias Grosser52a25232015-02-04 20:55:43 +00006412 for (i = 1; i < map->n; ++i) {
6413 isl_pw_multi_aff *res_i;
Tobias Grosser52a25232015-02-04 20:55:43 +00006414
6415 res_i = basic_map_partial_lexopt_pw_multi_aff(
6416 isl_basic_map_copy(map->p[i]),
Tobias Grosser932ec012016-07-06 09:11:00 +00006417 isl_set_copy(dom), empty, flags);
Tobias Grosser52a25232015-02-04 20:55:43 +00006418
Tobias Grosser932ec012016-07-06 09:11:00 +00006419 if (ISL_FL_ISSET(flags, ISL_OPT_MAX))
Tobias Grosser52a25232015-02-04 20:55:43 +00006420 res = isl_pw_multi_aff_union_lexmax(res, res_i);
6421 else
6422 res = isl_pw_multi_aff_union_lexmin(res, res_i);
6423
Tobias Grosser37034db2016-03-25 19:38:18 +00006424 if (empty)
6425 todo = isl_set_intersect(todo, *empty);
Tobias Grosser52a25232015-02-04 20:55:43 +00006426 }
6427
6428 isl_set_free(dom);
6429 isl_map_free(map);
6430
6431 if (empty)
6432 *empty = todo;
Tobias Grosser52a25232015-02-04 20:55:43 +00006433
6434 return res;
6435error:
6436 if (empty)
6437 *empty = NULL;
6438 isl_set_free(dom);
6439 isl_map_free(map);
6440 return NULL;
6441}
6442
6443#undef TYPE
6444#define TYPE isl_map
6445#undef SUFFIX
6446#define SUFFIX
6447#undef EMPTY
6448#define EMPTY isl_map_empty
6449#undef ADD
6450#define ADD isl_map_union_disjoint
6451#include "isl_map_lexopt_templ.c"
6452
6453/* Given a map "map", compute the lexicographically minimal
Michael Krusee0b34f32016-05-04 14:41:36 +00006454 * (or maximal) image element for each domain element in "dom",
6455 * in the form of an isl_map.
6456 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6457 * do not have an image element.
Tobias Grosser932ec012016-07-06 09:11:00 +00006458 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6459 * should be computed over the domain of "map". "empty" is also NULL
6460 * in this case.
Tobias Grosser52a25232015-02-04 20:55:43 +00006461 *
Michael Krusee0b34f32016-05-04 14:41:36 +00006462 * If the input consists of more than one disjunct, then first
6463 * compute the desired result in the form of an isl_pw_multi_aff and
6464 * then convert that into an isl_map.
Tobias Grosser52a25232015-02-04 20:55:43 +00006465 *
Michael Krusee0b34f32016-05-04 14:41:36 +00006466 * This function used to have an explicit implementation in terms
6467 * of isl_maps, but it would continually intersect the domains of
6468 * partial results with the complement of the domain of the next
6469 * partial solution, potentially leading to an explosion in the number
6470 * of disjuncts if there are several disjuncts in the input.
6471 * An even earlier implementation of this function would look for
6472 * better results in the domain of the partial result and for extra
6473 * results in the complement of this domain, which would lead to
6474 * even more splintering.
Tobias Grosser52a25232015-02-04 20:55:43 +00006475 */
6476static __isl_give isl_map *isl_map_partial_lexopt_aligned(
Tobias Grosser932ec012016-07-06 09:11:00 +00006477 __isl_take isl_map *map, __isl_take isl_set *dom,
6478 __isl_give isl_set **empty, unsigned flags)
Tobias Grosser52a25232015-02-04 20:55:43 +00006479{
Tobias Grosser932ec012016-07-06 09:11:00 +00006480 int full;
Tobias Grosser52a25232015-02-04 20:55:43 +00006481 struct isl_map *res;
Michael Krusee0b34f32016-05-04 14:41:36 +00006482 isl_pw_multi_aff *pma;
Tobias Grosser52a25232015-02-04 20:55:43 +00006483
Tobias Grosser932ec012016-07-06 09:11:00 +00006484 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6485 if (!map || (!full && !dom))
Tobias Grosser52a25232015-02-04 20:55:43 +00006486 goto error;
6487
6488 if (isl_map_plain_is_empty(map)) {
6489 if (empty)
6490 *empty = dom;
6491 else
6492 isl_set_free(dom);
6493 return map;
6494 }
6495
Michael Krusee0b34f32016-05-04 14:41:36 +00006496 if (map->n == 1) {
6497 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
Tobias Grosser932ec012016-07-06 09:11:00 +00006498 dom, empty, flags);
Michael Krusee0b34f32016-05-04 14:41:36 +00006499 isl_map_free(map);
6500 return res;
Tobias Grosser52a25232015-02-04 20:55:43 +00006501 }
6502
Tobias Grosser932ec012016-07-06 09:11:00 +00006503 pma = isl_map_partial_lexopt_aligned_pw_multi_aff(map, dom, empty,
6504 flags);
Michael Krusee0b34f32016-05-04 14:41:36 +00006505 return isl_map_from_pw_multi_aff(pma);
Tobias Grosser52a25232015-02-04 20:55:43 +00006506error:
6507 if (empty)
6508 *empty = NULL;
6509 isl_set_free(dom);
6510 isl_map_free(map);
6511 return NULL;
6512}
6513
6514__isl_give isl_map *isl_map_partial_lexmax(
6515 __isl_take isl_map *map, __isl_take isl_set *dom,
6516 __isl_give isl_set **empty)
6517{
Tobias Grosser932ec012016-07-06 09:11:00 +00006518 return isl_map_partial_lexopt(map, dom, empty, ISL_OPT_MAX);
Tobias Grosser52a25232015-02-04 20:55:43 +00006519}
6520
6521__isl_give isl_map *isl_map_partial_lexmin(
6522 __isl_take isl_map *map, __isl_take isl_set *dom,
6523 __isl_give isl_set **empty)
6524{
6525 return isl_map_partial_lexopt(map, dom, empty, 0);
6526}
6527
6528__isl_give isl_set *isl_set_partial_lexmin(
6529 __isl_take isl_set *set, __isl_take isl_set *dom,
6530 __isl_give isl_set **empty)
6531{
Tobias Grosser06e15922016-11-16 11:06:47 +00006532 return set_from_map(isl_map_partial_lexmin(set_to_map(set),
6533 dom, empty));
Tobias Grosser52a25232015-02-04 20:55:43 +00006534}
6535
6536__isl_give isl_set *isl_set_partial_lexmax(
6537 __isl_take isl_set *set, __isl_take isl_set *dom,
6538 __isl_give isl_set **empty)
6539{
Tobias Grosser06e15922016-11-16 11:06:47 +00006540 return set_from_map(isl_map_partial_lexmax(set_to_map(set),
6541 dom, empty));
Tobias Grosser52a25232015-02-04 20:55:43 +00006542}
6543
Tobias Grosser932ec012016-07-06 09:11:00 +00006544/* Compute the lexicographic minimum (or maximum if "flags" includes
6545 * ISL_OPT_MAX) of "bset" over its parametric domain.
Tobias Grosser52a25232015-02-04 20:55:43 +00006546 */
Tobias Grosser932ec012016-07-06 09:11:00 +00006547__isl_give isl_set *isl_basic_set_lexopt(__isl_take isl_basic_set *bset,
6548 unsigned flags)
Tobias Grosser52a25232015-02-04 20:55:43 +00006549{
Tobias Grosser932ec012016-07-06 09:11:00 +00006550 return isl_basic_map_lexopt(bset, flags);
Tobias Grosser52a25232015-02-04 20:55:43 +00006551}
6552
6553__isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
6554{
Tobias Grosser932ec012016-07-06 09:11:00 +00006555 return isl_basic_map_lexopt(bmap, ISL_OPT_MAX);
Tobias Grosser52a25232015-02-04 20:55:43 +00006556}
6557
6558__isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
6559{
Tobias Grosser06e15922016-11-16 11:06:47 +00006560 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset)));
Tobias Grosser52a25232015-02-04 20:55:43 +00006561}
6562
6563__isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
6564{
Tobias Grosser06e15922016-11-16 11:06:47 +00006565 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset)));
Tobias Grosser52a25232015-02-04 20:55:43 +00006566}
6567
Tobias Grosser932ec012016-07-06 09:11:00 +00006568/* Compute the lexicographic minimum of "bset" over its parametric domain
6569 * for the purpose of quantifier elimination.
6570 * That is, find an explicit representation for all the existentially
6571 * quantified variables in "bset" by computing their lexicographic
6572 * minimum.
6573 */
6574static __isl_give isl_set *isl_basic_set_lexmin_compute_divs(
6575 __isl_take isl_basic_set *bset)
6576{
6577 return isl_basic_set_lexopt(bset, ISL_OPT_QE);
6578}
6579
Tobias Grosser52a25232015-02-04 20:55:43 +00006580/* Extract the first and only affine expression from list
6581 * and then add it to *pwaff with the given dom.
6582 * This domain is known to be disjoint from other domains
6583 * because of the way isl_basic_map_foreach_lexmax works.
6584 */
6585static int update_dim_opt(__isl_take isl_basic_set *dom,
6586 __isl_take isl_aff_list *list, void *user)
6587{
6588 isl_ctx *ctx = isl_basic_set_get_ctx(dom);
6589 isl_aff *aff;
6590 isl_pw_aff **pwaff = user;
6591 isl_pw_aff *pwaff_i;
6592
6593 if (!list)
6594 goto error;
6595 if (isl_aff_list_n_aff(list) != 1)
6596 isl_die(ctx, isl_error_internal,
6597 "expecting single element list", goto error);
6598
6599 aff = isl_aff_list_get_aff(list, 0);
6600 pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
6601
6602 *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
6603
6604 isl_aff_list_free(list);
6605
6606 return 0;
6607error:
6608 isl_basic_set_free(dom);
6609 isl_aff_list_free(list);
6610 return -1;
6611}
6612
6613/* Given a basic map with one output dimension, compute the minimum or
6614 * maximum of that dimension as an isl_pw_aff.
6615 *
6616 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6617 * call update_dim_opt on each leaf of the result.
6618 */
6619static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
6620 int max)
6621{
6622 isl_space *dim = isl_basic_map_get_space(bmap);
6623 isl_pw_aff *pwaff;
6624 int r;
6625
6626 dim = isl_space_from_domain(isl_space_domain(dim));
6627 dim = isl_space_add_dims(dim, isl_dim_out, 1);
6628 pwaff = isl_pw_aff_empty(dim);
6629
6630 r = isl_basic_map_foreach_lexopt(bmap, max, &update_dim_opt, &pwaff);
6631 if (r < 0)
6632 return isl_pw_aff_free(pwaff);
6633
6634 return pwaff;
6635}
6636
6637/* Compute the minimum or maximum of the given output dimension
6638 * as a function of the parameters and the input dimensions,
6639 * but independently of the other output dimensions.
6640 *
6641 * We first project out the other output dimension and then compute
6642 * the "lexicographic" maximum in each basic map, combining the results
6643 * using isl_pw_aff_union_max.
6644 */
6645static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
6646 int max)
6647{
6648 int i;
6649 isl_pw_aff *pwaff;
6650 unsigned n_out;
6651
6652 n_out = isl_map_dim(map, isl_dim_out);
6653 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
6654 map = isl_map_project_out(map, isl_dim_out, 0, pos);
6655 if (!map)
6656 return NULL;
6657
6658 if (map->n == 0) {
6659 isl_space *dim = isl_map_get_space(map);
6660 isl_map_free(map);
6661 return isl_pw_aff_empty(dim);
6662 }
6663
6664 pwaff = basic_map_dim_opt(map->p[0], max);
6665 for (i = 1; i < map->n; ++i) {
6666 isl_pw_aff *pwaff_i;
6667
6668 pwaff_i = basic_map_dim_opt(map->p[i], max);
6669 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
6670 }
6671
6672 isl_map_free(map);
6673
6674 return pwaff;
6675}
6676
Tobias Grosser932ec012016-07-06 09:11:00 +00006677/* Compute the minimum of the given output dimension as a function of the
6678 * parameters and input dimensions, but independently of
6679 * the other output dimensions.
6680 */
6681__isl_give isl_pw_aff *isl_map_dim_min(__isl_take isl_map *map, int pos)
6682{
6683 return map_dim_opt(map, pos, 0);
6684}
6685
Tobias Grosser52a25232015-02-04 20:55:43 +00006686/* Compute the maximum of the given output dimension as a function of the
6687 * parameters and input dimensions, but independently of
6688 * the other output dimensions.
6689 */
6690__isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
6691{
6692 return map_dim_opt(map, pos, 1);
6693}
6694
6695/* Compute the minimum or maximum of the given set dimension
6696 * as a function of the parameters,
6697 * but independently of the other set dimensions.
6698 */
6699static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
6700 int max)
6701{
6702 return map_dim_opt(set, pos, max);
6703}
6704
6705/* Compute the maximum of the given set dimension as a function of the
6706 * parameters, but independently of the other set dimensions.
6707 */
6708__isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
6709{
6710 return set_dim_opt(set, pos, 1);
6711}
6712
6713/* Compute the minimum of the given set dimension as a function of the
6714 * parameters, but independently of the other set dimensions.
6715 */
6716__isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
6717{
6718 return set_dim_opt(set, pos, 0);
6719}
6720
6721/* Apply a preimage specified by "mat" on the parameters of "bset".
6722 * bset is assumed to have only parameters and divs.
6723 */
6724static struct isl_basic_set *basic_set_parameter_preimage(
6725 struct isl_basic_set *bset, struct isl_mat *mat)
6726{
6727 unsigned nparam;
6728
6729 if (!bset || !mat)
6730 goto error;
6731
6732 bset->dim = isl_space_cow(bset->dim);
6733 if (!bset->dim)
6734 goto error;
6735
6736 nparam = isl_basic_set_dim(bset, isl_dim_param);
6737
6738 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
6739
6740 bset->dim->nparam = 0;
6741 bset->dim->n_out = nparam;
6742 bset = isl_basic_set_preimage(bset, mat);
6743 if (bset) {
6744 bset->dim->nparam = bset->dim->n_out;
6745 bset->dim->n_out = 0;
6746 }
6747 return bset;
6748error:
6749 isl_mat_free(mat);
6750 isl_basic_set_free(bset);
6751 return NULL;
6752}
6753
6754/* Apply a preimage specified by "mat" on the parameters of "set".
6755 * set is assumed to have only parameters and divs.
6756 */
Tobias Grosser37034db2016-03-25 19:38:18 +00006757static __isl_give isl_set *set_parameter_preimage(__isl_take isl_set *set,
6758 __isl_take isl_mat *mat)
Tobias Grosser52a25232015-02-04 20:55:43 +00006759{
Tobias Grosser37034db2016-03-25 19:38:18 +00006760 isl_space *space;
Tobias Grosser52a25232015-02-04 20:55:43 +00006761 unsigned nparam;
6762
6763 if (!set || !mat)
6764 goto error;
6765
Tobias Grosser52a25232015-02-04 20:55:43 +00006766 nparam = isl_set_dim(set, isl_dim_param);
6767
Tobias Grosser37034db2016-03-25 19:38:18 +00006768 if (mat->n_row != 1 + nparam)
6769 isl_die(isl_set_get_ctx(set), isl_error_internal,
6770 "unexpected number of rows", goto error);
Tobias Grosser52a25232015-02-04 20:55:43 +00006771
Tobias Grosser37034db2016-03-25 19:38:18 +00006772 space = isl_set_get_space(set);
6773 space = isl_space_move_dims(space, isl_dim_set, 0,
6774 isl_dim_param, 0, nparam);
6775 set = isl_set_reset_space(set, space);
Tobias Grosser52a25232015-02-04 20:55:43 +00006776 set = isl_set_preimage(set, mat);
Tobias Grosser37034db2016-03-25 19:38:18 +00006777 nparam = isl_set_dim(set, isl_dim_out);
6778 space = isl_set_get_space(set);
6779 space = isl_space_move_dims(space, isl_dim_param, 0,
6780 isl_dim_out, 0, nparam);
6781 set = isl_set_reset_space(set, space);
Tobias Grosser52a25232015-02-04 20:55:43 +00006782 return set;
6783error:
Tobias Grosser52a25232015-02-04 20:55:43 +00006784 isl_mat_free(mat);
Tobias Grosser52a25232015-02-04 20:55:43 +00006785 isl_set_free(set);
6786 return NULL;
6787}
6788
6789/* Intersect the basic set "bset" with the affine space specified by the
6790 * equalities in "eq".
6791 */
6792static struct isl_basic_set *basic_set_append_equalities(
6793 struct isl_basic_set *bset, struct isl_mat *eq)
6794{
6795 int i, k;
6796 unsigned len;
6797
6798 if (!bset || !eq)
6799 goto error;
6800
6801 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
6802 eq->n_row, 0);
6803 if (!bset)
6804 goto error;
6805
6806 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
6807 for (i = 0; i < eq->n_row; ++i) {
6808 k = isl_basic_set_alloc_equality(bset);
6809 if (k < 0)
6810 goto error;
6811 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
6812 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
6813 }
6814 isl_mat_free(eq);
6815
6816 bset = isl_basic_set_gauss(bset, NULL);
6817 bset = isl_basic_set_finalize(bset);
6818
6819 return bset;
6820error:
6821 isl_mat_free(eq);
6822 isl_basic_set_free(bset);
6823 return NULL;
6824}
6825
6826/* Intersect the set "set" with the affine space specified by the
6827 * equalities in "eq".
6828 */
6829static struct isl_set *set_append_equalities(struct isl_set *set,
6830 struct isl_mat *eq)
6831{
6832 int i;
6833
6834 if (!set || !eq)
6835 goto error;
6836
6837 for (i = 0; i < set->n; ++i) {
6838 set->p[i] = basic_set_append_equalities(set->p[i],
6839 isl_mat_copy(eq));
6840 if (!set->p[i])
6841 goto error;
6842 }
6843 isl_mat_free(eq);
6844 return set;
6845error:
6846 isl_mat_free(eq);
6847 isl_set_free(set);
6848 return NULL;
6849}
6850
6851/* Given a basic set "bset" that only involves parameters and existentially
6852 * quantified variables, return the index of the first equality
6853 * that only involves parameters. If there is no such equality then
6854 * return bset->n_eq.
6855 *
6856 * This function assumes that isl_basic_set_gauss has been called on "bset".
6857 */
6858static int first_parameter_equality(__isl_keep isl_basic_set *bset)
6859{
6860 int i, j;
6861 unsigned nparam, n_div;
6862
6863 if (!bset)
6864 return -1;
6865
6866 nparam = isl_basic_set_dim(bset, isl_dim_param);
6867 n_div = isl_basic_set_dim(bset, isl_dim_div);
6868
6869 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
6870 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
6871 ++i;
6872 }
6873
6874 return i;
6875}
6876
6877/* Compute an explicit representation for the existentially quantified
6878 * variables in "bset" by computing the "minimal value" of the set
6879 * variables. Since there are no set variables, the computation of
6880 * the minimal value essentially computes an explicit representation
6881 * of the non-empty part(s) of "bset".
6882 *
6883 * The input only involves parameters and existentially quantified variables.
6884 * All equalities among parameters have been removed.
6885 *
6886 * Since the existentially quantified variables in the result are in general
6887 * going to be different from those in the input, we first replace
6888 * them by the minimal number of variables based on their equalities.
6889 * This should simplify the parametric integer programming.
6890 */
6891static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
6892{
6893 isl_morph *morph1, *morph2;
6894 isl_set *set;
6895 unsigned n;
6896
6897 if (!bset)
6898 return NULL;
6899 if (bset->n_eq == 0)
Tobias Grosser932ec012016-07-06 09:11:00 +00006900 return isl_basic_set_lexmin_compute_divs(bset);
Tobias Grosser52a25232015-02-04 20:55:43 +00006901
6902 morph1 = isl_basic_set_parameter_compression(bset);
6903 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
6904 bset = isl_basic_set_lift(bset);
6905 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
6906 bset = isl_morph_basic_set(morph2, bset);
6907 n = isl_basic_set_dim(bset, isl_dim_set);
6908 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
6909
Tobias Grosser932ec012016-07-06 09:11:00 +00006910 set = isl_basic_set_lexmin_compute_divs(bset);
Tobias Grosser52a25232015-02-04 20:55:43 +00006911
6912 set = isl_morph_set(isl_morph_inverse(morph1), set);
6913
6914 return set;
6915}
6916
6917/* Project the given basic set onto its parameter domain, possibly introducing
6918 * new, explicit, existential variables in the constraints.
6919 * The input has parameters and (possibly implicit) existential variables.
6920 * The output has the same parameters, but only
6921 * explicit existentially quantified variables.
6922 *
6923 * The actual projection is performed by pip, but pip doesn't seem
6924 * to like equalities very much, so we first remove the equalities
6925 * among the parameters by performing a variable compression on
6926 * the parameters. Afterward, an inverse transformation is performed
6927 * and the equalities among the parameters are inserted back in.
6928 *
6929 * The variable compression on the parameters may uncover additional
6930 * equalities that were only implicit before. We therefore check
6931 * if there are any new parameter equalities in the result and
6932 * if so recurse. The removal of parameter equalities is required
6933 * for the parameter compression performed by base_compute_divs.
6934 */
6935static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
6936{
6937 int i;
6938 struct isl_mat *eq;
6939 struct isl_mat *T, *T2;
6940 struct isl_set *set;
6941 unsigned nparam;
6942
6943 bset = isl_basic_set_cow(bset);
6944 if (!bset)
6945 return NULL;
6946
6947 if (bset->n_eq == 0)
6948 return base_compute_divs(bset);
6949
6950 bset = isl_basic_set_gauss(bset, NULL);
6951 if (!bset)
6952 return NULL;
6953 if (isl_basic_set_plain_is_empty(bset))
6954 return isl_set_from_basic_set(bset);
6955
6956 i = first_parameter_equality(bset);
6957 if (i == bset->n_eq)
6958 return base_compute_divs(bset);
6959
6960 nparam = isl_basic_set_dim(bset, isl_dim_param);
6961 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
6962 0, 1 + nparam);
6963 eq = isl_mat_cow(eq);
6964 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
6965 if (T && T->n_col == 0) {
6966 isl_mat_free(T);
6967 isl_mat_free(T2);
6968 isl_mat_free(eq);
6969 bset = isl_basic_set_set_to_empty(bset);
6970 return isl_set_from_basic_set(bset);
6971 }
6972 bset = basic_set_parameter_preimage(bset, T);
6973
6974 i = first_parameter_equality(bset);
6975 if (!bset)
6976 set = NULL;
6977 else if (i == bset->n_eq)
6978 set = base_compute_divs(bset);
6979 else
6980 set = parameter_compute_divs(bset);
6981 set = set_parameter_preimage(set, T2);
6982 set = set_append_equalities(set, eq);
6983 return set;
6984}
6985
6986/* Insert the divs from "ls" before those of "bmap".
6987 *
6988 * The number of columns is not changed, which means that the last
6989 * dimensions of "bmap" are being reintepreted as the divs from "ls".
6990 * The caller is responsible for removing the same number of dimensions
6991 * from the space of "bmap".
6992 */
6993static __isl_give isl_basic_map *insert_divs_from_local_space(
6994 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
6995{
6996 int i;
6997 int n_div;
6998 int old_n_div;
6999
7000 n_div = isl_local_space_dim(ls, isl_dim_div);
7001 if (n_div == 0)
7002 return bmap;
7003
7004 old_n_div = bmap->n_div;
7005 bmap = insert_div_rows(bmap, n_div);
7006 if (!bmap)
7007 return NULL;
7008
7009 for (i = 0; i < n_div; ++i) {
7010 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
7011 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
7012 }
7013
7014 return bmap;
7015}
7016
7017/* Replace the space of "bmap" by the space and divs of "ls".
7018 *
7019 * If "ls" has any divs, then we simplify the result since we may
7020 * have discovered some additional equalities that could simplify
7021 * the div expressions.
7022 */
7023static __isl_give isl_basic_map *basic_replace_space_by_local_space(
7024 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
7025{
7026 int n_div;
7027
7028 bmap = isl_basic_map_cow(bmap);
7029 if (!bmap || !ls)
7030 goto error;
7031
7032 n_div = isl_local_space_dim(ls, isl_dim_div);
7033 bmap = insert_divs_from_local_space(bmap, ls);
7034 if (!bmap)
7035 goto error;
7036
7037 isl_space_free(bmap->dim);
7038 bmap->dim = isl_local_space_get_space(ls);
7039 if (!bmap->dim)
7040 goto error;
7041
7042 isl_local_space_free(ls);
7043 if (n_div > 0)
7044 bmap = isl_basic_map_simplify(bmap);
7045 bmap = isl_basic_map_finalize(bmap);
7046 return bmap;
7047error:
7048 isl_basic_map_free(bmap);
7049 isl_local_space_free(ls);
7050 return NULL;
7051}
7052
7053/* Replace the space of "map" by the space and divs of "ls".
7054 */
7055static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
7056 __isl_take isl_local_space *ls)
7057{
7058 int i;
7059
7060 map = isl_map_cow(map);
7061 if (!map || !ls)
7062 goto error;
7063
7064 for (i = 0; i < map->n; ++i) {
7065 map->p[i] = basic_replace_space_by_local_space(map->p[i],
7066 isl_local_space_copy(ls));
7067 if (!map->p[i])
7068 goto error;
7069 }
7070 isl_space_free(map->dim);
7071 map->dim = isl_local_space_get_space(ls);
7072 if (!map->dim)
7073 goto error;
7074
7075 isl_local_space_free(ls);
7076 return map;
7077error:
7078 isl_local_space_free(ls);
7079 isl_map_free(map);
7080 return NULL;
7081}
7082
7083/* Compute an explicit representation for the existentially
7084 * quantified variables for which do not know any explicit representation yet.
7085 *
7086 * We first sort the existentially quantified variables so that the
7087 * existentially quantified variables for which we already have an explicit
7088 * representation are placed before those for which we do not.
7089 * The input dimensions, the output dimensions and the existentially
7090 * quantified variables for which we already have an explicit
7091 * representation are then turned into parameters.
7092 * compute_divs returns a map with the same parameters and
7093 * no input or output dimensions and the dimension specification
7094 * is reset to that of the input, including the existentially quantified
7095 * variables for which we already had an explicit representation.
7096 */
7097static struct isl_map *compute_divs(struct isl_basic_map *bmap)
7098{
7099 struct isl_basic_set *bset;
7100 struct isl_set *set;
7101 struct isl_map *map;
7102 isl_space *dim;
7103 isl_local_space *ls;
7104 unsigned nparam;
7105 unsigned n_in;
7106 unsigned n_out;
Tobias Grosser37034db2016-03-25 19:38:18 +00007107 int n_known;
Tobias Grosser52a25232015-02-04 20:55:43 +00007108 int i;
7109
7110 bmap = isl_basic_map_sort_divs(bmap);
7111 bmap = isl_basic_map_cow(bmap);
7112 if (!bmap)
7113 return NULL;
7114
Tobias Grosser37034db2016-03-25 19:38:18 +00007115 n_known = isl_basic_map_first_unknown_div(bmap);
7116 if (n_known < 0)
7117 return isl_map_from_basic_map(isl_basic_map_free(bmap));
Tobias Grosser52a25232015-02-04 20:55:43 +00007118
7119 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7120 n_in = isl_basic_map_dim(bmap, isl_dim_in);
7121 n_out = isl_basic_map_dim(bmap, isl_dim_out);
7122 dim = isl_space_set_alloc(bmap->ctx,
7123 nparam + n_in + n_out + n_known, 0);
7124 if (!dim)
7125 goto error;
7126
7127 ls = isl_basic_map_get_local_space(bmap);
7128 ls = isl_local_space_drop_dims(ls, isl_dim_div,
7129 n_known, bmap->n_div - n_known);
7130 if (n_known > 0) {
7131 for (i = n_known; i < bmap->n_div; ++i)
7132 swap_div(bmap, i - n_known, i);
7133 bmap->n_div -= n_known;
7134 bmap->extra -= n_known;
7135 }
7136 bmap = isl_basic_map_reset_space(bmap, dim);
Tobias Grosser06e15922016-11-16 11:06:47 +00007137 bset = bset_from_bmap(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +00007138
7139 set = parameter_compute_divs(bset);
Tobias Grosser06e15922016-11-16 11:06:47 +00007140 map = set_to_map(set);
Tobias Grosser52a25232015-02-04 20:55:43 +00007141 map = replace_space_by_local_space(map, ls);
7142
7143 return map;
7144error:
7145 isl_basic_map_free(bmap);
7146 return NULL;
7147}
7148
Tobias Grosser37034db2016-03-25 19:38:18 +00007149/* Remove the explicit representation of local variable "div",
7150 * if there is any.
7151 */
7152__isl_give isl_basic_map *isl_basic_map_mark_div_unknown(
7153 __isl_take isl_basic_map *bmap, int div)
7154{
Tobias Grosser932ec012016-07-06 09:11:00 +00007155 isl_bool unknown;
Tobias Grosser37034db2016-03-25 19:38:18 +00007156
Tobias Grosser932ec012016-07-06 09:11:00 +00007157 unknown = isl_basic_map_div_is_marked_unknown(bmap, div);
7158 if (unknown < 0)
Tobias Grosser37034db2016-03-25 19:38:18 +00007159 return isl_basic_map_free(bmap);
Tobias Grosser932ec012016-07-06 09:11:00 +00007160 if (unknown)
Tobias Grosser37034db2016-03-25 19:38:18 +00007161 return bmap;
7162
7163 bmap = isl_basic_map_cow(bmap);
7164 if (!bmap)
7165 return NULL;
7166 isl_int_set_si(bmap->div[div][0], 0);
7167 return bmap;
7168}
7169
Tobias Grosser932ec012016-07-06 09:11:00 +00007170/* Is local variable "div" of "bmap" marked as not having an explicit
7171 * representation?
7172 * Note that even if "div" is not marked in this way and therefore
7173 * has an explicit representation, this representation may still
7174 * depend (indirectly) on other local variables that do not
7175 * have an explicit representation.
Michael Kruse959a8dc2016-01-15 15:54:45 +00007176 */
Tobias Grosser932ec012016-07-06 09:11:00 +00007177isl_bool isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map *bmap,
7178 int div)
Michael Kruse959a8dc2016-01-15 15:54:45 +00007179{
Tobias Grosser94e53712016-12-31 07:46:11 +00007180 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
Michael Kruse959a8dc2016-01-15 15:54:45 +00007181 return isl_bool_error;
Tobias Grosser932ec012016-07-06 09:11:00 +00007182 return isl_int_is_zero(bmap->div[div][0]);
Michael Kruse959a8dc2016-01-15 15:54:45 +00007183}
7184
Tobias Grosser37034db2016-03-25 19:38:18 +00007185/* Return the position of the first local variable that does not
7186 * have an explicit representation.
7187 * Return the total number of local variables if they all have
7188 * an explicit representation.
7189 * Return -1 on error.
7190 */
7191int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map *bmap)
7192{
7193 int i;
7194
7195 if (!bmap)
7196 return -1;
7197
7198 for (i = 0; i < bmap->n_div; ++i) {
7199 if (!isl_basic_map_div_is_known(bmap, i))
7200 return i;
7201 }
7202 return bmap->n_div;
7203}
7204
Tobias Grosser9ec4f952016-07-20 16:53:07 +00007205/* Return the position of the first local variable that does not
7206 * have an explicit representation.
7207 * Return the total number of local variables if they all have
7208 * an explicit representation.
7209 * Return -1 on error.
7210 */
7211int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set *bset)
7212{
7213 return isl_basic_map_first_unknown_div(bset);
7214}
7215
Michael Kruse959a8dc2016-01-15 15:54:45 +00007216/* Does "bmap" have an explicit representation for all local variables?
7217 */
7218isl_bool isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
Tobias Grosser52a25232015-02-04 20:55:43 +00007219{
Tobias Grosser37034db2016-03-25 19:38:18 +00007220 int first, n;
Tobias Grosser52a25232015-02-04 20:55:43 +00007221
Tobias Grosser37034db2016-03-25 19:38:18 +00007222 n = isl_basic_map_dim(bmap, isl_dim_div);
7223 first = isl_basic_map_first_unknown_div(bmap);
7224 if (first < 0)
Michael Kruse959a8dc2016-01-15 15:54:45 +00007225 return isl_bool_error;
Tobias Grosser37034db2016-03-25 19:38:18 +00007226 return first == n;
Tobias Grosser52a25232015-02-04 20:55:43 +00007227}
7228
Michael Kruse959a8dc2016-01-15 15:54:45 +00007229/* Do all basic maps in "map" have an explicit representation
7230 * for all local variables?
7231 */
7232isl_bool isl_map_divs_known(__isl_keep isl_map *map)
Tobias Grosser52a25232015-02-04 20:55:43 +00007233{
7234 int i;
7235
7236 if (!map)
Michael Kruse959a8dc2016-01-15 15:54:45 +00007237 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00007238
7239 for (i = 0; i < map->n; ++i) {
7240 int known = isl_basic_map_divs_known(map->p[i]);
7241 if (known <= 0)
7242 return known;
7243 }
7244
Michael Kruse959a8dc2016-01-15 15:54:45 +00007245 return isl_bool_true;
Tobias Grosser52a25232015-02-04 20:55:43 +00007246}
7247
7248/* If bmap contains any unknown divs, then compute explicit
7249 * expressions for them. However, this computation may be
7250 * quite expensive, so first try to remove divs that aren't
7251 * strictly needed.
7252 */
7253struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
7254{
7255 int known;
7256 struct isl_map *map;
7257
7258 known = isl_basic_map_divs_known(bmap);
7259 if (known < 0)
7260 goto error;
7261 if (known)
7262 return isl_map_from_basic_map(bmap);
7263
7264 bmap = isl_basic_map_drop_redundant_divs(bmap);
7265
7266 known = isl_basic_map_divs_known(bmap);
7267 if (known < 0)
7268 goto error;
7269 if (known)
7270 return isl_map_from_basic_map(bmap);
7271
7272 map = compute_divs(bmap);
7273 return map;
7274error:
7275 isl_basic_map_free(bmap);
7276 return NULL;
7277}
7278
7279struct isl_map *isl_map_compute_divs(struct isl_map *map)
7280{
7281 int i;
7282 int known;
7283 struct isl_map *res;
7284
7285 if (!map)
7286 return NULL;
7287 if (map->n == 0)
7288 return map;
7289
Michael Kruse959a8dc2016-01-15 15:54:45 +00007290 known = isl_map_divs_known(map);
Tobias Grosser52a25232015-02-04 20:55:43 +00007291 if (known < 0) {
7292 isl_map_free(map);
7293 return NULL;
7294 }
7295 if (known)
7296 return map;
7297
7298 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
7299 for (i = 1 ; i < map->n; ++i) {
7300 struct isl_map *r2;
7301 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
7302 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
7303 res = isl_map_union_disjoint(res, r2);
7304 else
7305 res = isl_map_union(res, r2);
7306 }
7307 isl_map_free(map);
7308
7309 return res;
7310}
7311
7312struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
7313{
Tobias Grosser06e15922016-11-16 11:06:47 +00007314 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset)));
Tobias Grosser52a25232015-02-04 20:55:43 +00007315}
7316
7317struct isl_set *isl_set_compute_divs(struct isl_set *set)
7318{
Tobias Grosser06e15922016-11-16 11:06:47 +00007319 return set_from_map(isl_map_compute_divs(set_to_map(set)));
Tobias Grosser52a25232015-02-04 20:55:43 +00007320}
7321
7322struct isl_set *isl_map_domain(struct isl_map *map)
7323{
7324 int i;
7325 struct isl_set *set;
7326
7327 if (!map)
7328 goto error;
7329
7330 map = isl_map_cow(map);
7331 if (!map)
7332 return NULL;
7333
Tobias Grosser06e15922016-11-16 11:06:47 +00007334 set = set_from_map(map);
Tobias Grosser52a25232015-02-04 20:55:43 +00007335 set->dim = isl_space_domain(set->dim);
7336 if (!set->dim)
7337 goto error;
7338 for (i = 0; i < map->n; ++i) {
7339 set->p[i] = isl_basic_map_domain(map->p[i]);
7340 if (!set->p[i])
7341 goto error;
7342 }
7343 ISL_F_CLR(set, ISL_MAP_DISJOINT);
7344 ISL_F_CLR(set, ISL_SET_NORMALIZED);
7345 return set;
7346error:
7347 isl_map_free(map);
7348 return NULL;
7349}
7350
7351/* Return the union of "map1" and "map2", where we assume for now that
7352 * "map1" and "map2" are disjoint. Note that the basic maps inside
7353 * "map1" or "map2" may not be disjoint from each other.
7354 * Also note that this function is also called from isl_map_union,
7355 * which takes care of handling the situation where "map1" and "map2"
7356 * may not be disjoint.
7357 *
7358 * If one of the inputs is empty, we can simply return the other input.
7359 * Similarly, if one of the inputs is universal, then it is equal to the union.
7360 */
7361static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
7362 __isl_take isl_map *map2)
7363{
7364 int i;
7365 unsigned flags = 0;
7366 struct isl_map *map = NULL;
7367 int is_universe;
7368
7369 if (!map1 || !map2)
7370 goto error;
7371
7372 if (!isl_space_is_equal(map1->dim, map2->dim))
7373 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
7374 "spaces don't match", goto error);
7375
7376 if (map1->n == 0) {
7377 isl_map_free(map1);
7378 return map2;
7379 }
7380 if (map2->n == 0) {
7381 isl_map_free(map2);
7382 return map1;
7383 }
7384
7385 is_universe = isl_map_plain_is_universe(map1);
7386 if (is_universe < 0)
7387 goto error;
7388 if (is_universe) {
7389 isl_map_free(map2);
7390 return map1;
7391 }
7392
7393 is_universe = isl_map_plain_is_universe(map2);
7394 if (is_universe < 0)
7395 goto error;
7396 if (is_universe) {
7397 isl_map_free(map1);
7398 return map2;
7399 }
7400
7401 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7402 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7403 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7404
7405 map = isl_map_alloc_space(isl_space_copy(map1->dim),
7406 map1->n + map2->n, flags);
7407 if (!map)
7408 goto error;
7409 for (i = 0; i < map1->n; ++i) {
7410 map = isl_map_add_basic_map(map,
7411 isl_basic_map_copy(map1->p[i]));
7412 if (!map)
7413 goto error;
7414 }
7415 for (i = 0; i < map2->n; ++i) {
7416 map = isl_map_add_basic_map(map,
7417 isl_basic_map_copy(map2->p[i]));
7418 if (!map)
7419 goto error;
7420 }
7421 isl_map_free(map1);
7422 isl_map_free(map2);
7423 return map;
7424error:
7425 isl_map_free(map);
7426 isl_map_free(map1);
7427 isl_map_free(map2);
7428 return NULL;
7429}
7430
7431/* Return the union of "map1" and "map2", where "map1" and "map2" are
7432 * guaranteed to be disjoint by the caller.
7433 *
7434 * Note that this functions is called from within isl_map_make_disjoint,
7435 * so we have to be careful not to touch the constraints of the inputs
7436 * in any way.
7437 */
7438__isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
7439 __isl_take isl_map *map2)
7440{
7441 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
7442}
7443
7444/* Return the union of "map1" and "map2", where "map1" and "map2" may
7445 * not be disjoint. The parameters are assumed to have been aligned.
7446 *
7447 * We currently simply call map_union_disjoint, the internal operation
7448 * of which does not really depend on the inputs being disjoint.
7449 * If the result contains more than one basic map, then we clear
7450 * the disjoint flag since the result may contain basic maps from
7451 * both inputs and these are not guaranteed to be disjoint.
7452 *
7453 * As a special case, if "map1" and "map2" are obviously equal,
7454 * then we simply return "map1".
7455 */
7456static __isl_give isl_map *map_union_aligned(__isl_take isl_map *map1,
7457 __isl_take isl_map *map2)
7458{
7459 int equal;
7460
7461 if (!map1 || !map2)
7462 goto error;
7463
7464 equal = isl_map_plain_is_equal(map1, map2);
7465 if (equal < 0)
7466 goto error;
7467 if (equal) {
7468 isl_map_free(map2);
7469 return map1;
7470 }
7471
7472 map1 = map_union_disjoint(map1, map2);
7473 if (!map1)
7474 return NULL;
7475 if (map1->n > 1)
7476 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
7477 return map1;
7478error:
7479 isl_map_free(map1);
7480 isl_map_free(map2);
7481 return NULL;
7482}
7483
7484/* Return the union of "map1" and "map2", where "map1" and "map2" may
7485 * not be disjoint.
7486 */
7487__isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
7488 __isl_take isl_map *map2)
7489{
7490 return isl_map_align_params_map_map_and(map1, map2, &map_union_aligned);
7491}
7492
7493struct isl_set *isl_set_union_disjoint(
7494 struct isl_set *set1, struct isl_set *set2)
7495{
Tobias Grosser06e15922016-11-16 11:06:47 +00007496 return set_from_map(isl_map_union_disjoint(set_to_map(set1),
7497 set_to_map(set2)));
Tobias Grosser52a25232015-02-04 20:55:43 +00007498}
7499
7500struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
7501{
Tobias Grosser06e15922016-11-16 11:06:47 +00007502 return set_from_map(isl_map_union(set_to_map(set1), set_to_map(set2)));
Tobias Grosser52a25232015-02-04 20:55:43 +00007503}
7504
7505/* Apply "fn" to pairs of elements from "map" and "set" and collect
7506 * the results.
7507 *
7508 * "map" and "set" are assumed to be compatible and non-NULL.
7509 */
7510static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
7511 __isl_take isl_set *set,
7512 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
7513 __isl_take isl_basic_set *bset))
7514{
7515 unsigned flags = 0;
7516 struct isl_map *result;
7517 int i, j;
7518
7519 if (isl_set_plain_is_universe(set)) {
7520 isl_set_free(set);
7521 return map;
7522 }
7523
7524 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
7525 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
7526 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7527
7528 result = isl_map_alloc_space(isl_space_copy(map->dim),
7529 map->n * set->n, flags);
7530 for (i = 0; result && i < map->n; ++i)
7531 for (j = 0; j < set->n; ++j) {
7532 result = isl_map_add_basic_map(result,
7533 fn(isl_basic_map_copy(map->p[i]),
7534 isl_basic_set_copy(set->p[j])));
7535 if (!result)
7536 break;
7537 }
7538
7539 isl_map_free(map);
7540 isl_set_free(set);
7541 return result;
7542}
7543
7544static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
7545 __isl_take isl_set *set)
7546{
7547 if (!map || !set)
7548 goto error;
7549
7550 if (!isl_map_compatible_range(map, set))
7551 isl_die(set->ctx, isl_error_invalid,
7552 "incompatible spaces", goto error);
7553
7554 return map_intersect_set(map, set, &isl_basic_map_intersect_range);
7555error:
7556 isl_map_free(map);
7557 isl_set_free(set);
7558 return NULL;
7559}
7560
7561__isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
7562 __isl_take isl_set *set)
7563{
7564 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
7565}
7566
7567static __isl_give isl_map *map_intersect_domain(__isl_take isl_map *map,
7568 __isl_take isl_set *set)
7569{
7570 if (!map || !set)
7571 goto error;
7572
7573 if (!isl_map_compatible_domain(map, set))
7574 isl_die(set->ctx, isl_error_invalid,
7575 "incompatible spaces", goto error);
7576
7577 return map_intersect_set(map, set, &isl_basic_map_intersect_domain);
7578error:
7579 isl_map_free(map);
7580 isl_set_free(set);
7581 return NULL;
7582}
7583
7584__isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
7585 __isl_take isl_set *set)
7586{
7587 return isl_map_align_params_map_map_and(map, set,
7588 &map_intersect_domain);
7589}
7590
7591static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
7592 __isl_take isl_map *map2)
7593{
7594 if (!map1 || !map2)
7595 goto error;
7596 map1 = isl_map_reverse(map1);
7597 map1 = isl_map_apply_range(map1, map2);
7598 return isl_map_reverse(map1);
7599error:
7600 isl_map_free(map1);
7601 isl_map_free(map2);
7602 return NULL;
7603}
7604
7605__isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
7606 __isl_take isl_map *map2)
7607{
7608 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
7609}
7610
7611static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
7612 __isl_take isl_map *map2)
7613{
7614 isl_space *dim_result;
7615 struct isl_map *result;
7616 int i, j;
7617
7618 if (!map1 || !map2)
7619 goto error;
7620
7621 dim_result = isl_space_join(isl_space_copy(map1->dim),
7622 isl_space_copy(map2->dim));
7623
7624 result = isl_map_alloc_space(dim_result, map1->n * map2->n, 0);
7625 if (!result)
7626 goto error;
7627 for (i = 0; i < map1->n; ++i)
7628 for (j = 0; j < map2->n; ++j) {
7629 result = isl_map_add_basic_map(result,
7630 isl_basic_map_apply_range(
7631 isl_basic_map_copy(map1->p[i]),
7632 isl_basic_map_copy(map2->p[j])));
7633 if (!result)
7634 goto error;
7635 }
7636 isl_map_free(map1);
7637 isl_map_free(map2);
7638 if (result && result->n <= 1)
7639 ISL_F_SET(result, ISL_MAP_DISJOINT);
7640 return result;
7641error:
7642 isl_map_free(map1);
7643 isl_map_free(map2);
7644 return NULL;
7645}
7646
7647__isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
7648 __isl_take isl_map *map2)
7649{
7650 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
7651}
7652
7653/*
7654 * returns range - domain
7655 */
7656struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
7657{
Tobias Grosserb2f39922015-05-28 13:32:11 +00007658 isl_space *target_space;
Tobias Grosser52a25232015-02-04 20:55:43 +00007659 struct isl_basic_set *bset;
7660 unsigned dim;
7661 unsigned nparam;
7662 int i;
7663
7664 if (!bmap)
7665 goto error;
7666 isl_assert(bmap->ctx, isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7667 bmap->dim, isl_dim_out),
7668 goto error);
Tobias Grosserb2f39922015-05-28 13:32:11 +00007669 target_space = isl_space_domain(isl_basic_map_get_space(bmap));
Tobias Grosser52a25232015-02-04 20:55:43 +00007670 dim = isl_basic_map_n_in(bmap);
7671 nparam = isl_basic_map_n_param(bmap);
Tobias Grosserb2f39922015-05-28 13:32:11 +00007672 bmap = isl_basic_map_from_range(isl_basic_map_wrap(bmap));
7673 bmap = isl_basic_map_add_dims(bmap, isl_dim_in, dim);
7674 bmap = isl_basic_map_extend_constraints(bmap, dim, 0);
Tobias Grosser52a25232015-02-04 20:55:43 +00007675 for (i = 0; i < dim; ++i) {
Tobias Grosserb2f39922015-05-28 13:32:11 +00007676 int j = isl_basic_map_alloc_equality(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +00007677 if (j < 0) {
Tobias Grosserb2f39922015-05-28 13:32:11 +00007678 bmap = isl_basic_map_free(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +00007679 break;
7680 }
Tobias Grosserb2f39922015-05-28 13:32:11 +00007681 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7682 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7683 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], 1);
7684 isl_int_set_si(bmap->eq[j][1+nparam+2*dim+i], -1);
Tobias Grosser52a25232015-02-04 20:55:43 +00007685 }
Tobias Grosserb2f39922015-05-28 13:32:11 +00007686 bset = isl_basic_map_domain(bmap);
7687 bset = isl_basic_set_reset_space(bset, target_space);
Tobias Grosser52a25232015-02-04 20:55:43 +00007688 return bset;
7689error:
7690 isl_basic_map_free(bmap);
7691 return NULL;
7692}
7693
7694/*
7695 * returns range - domain
7696 */
7697__isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
7698{
7699 int i;
7700 isl_space *dim;
7701 struct isl_set *result;
7702
7703 if (!map)
7704 return NULL;
7705
7706 isl_assert(map->ctx, isl_space_tuple_is_equal(map->dim, isl_dim_in,
7707 map->dim, isl_dim_out),
7708 goto error);
7709 dim = isl_map_get_space(map);
7710 dim = isl_space_domain(dim);
7711 result = isl_set_alloc_space(dim, map->n, 0);
7712 if (!result)
7713 goto error;
7714 for (i = 0; i < map->n; ++i)
7715 result = isl_set_add_basic_set(result,
7716 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
7717 isl_map_free(map);
7718 return result;
7719error:
7720 isl_map_free(map);
7721 return NULL;
7722}
7723
7724/*
7725 * returns [domain -> range] -> range - domain
7726 */
7727__isl_give isl_basic_map *isl_basic_map_deltas_map(
7728 __isl_take isl_basic_map *bmap)
7729{
7730 int i, k;
7731 isl_space *dim;
7732 isl_basic_map *domain;
7733 int nparam, n;
7734 unsigned total;
7735
7736 if (!isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7737 bmap->dim, isl_dim_out))
7738 isl_die(bmap->ctx, isl_error_invalid,
7739 "domain and range don't match", goto error);
7740
7741 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7742 n = isl_basic_map_dim(bmap, isl_dim_in);
7743
7744 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
7745 domain = isl_basic_map_universe(dim);
7746
7747 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
7748 bmap = isl_basic_map_apply_range(bmap, domain);
7749 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
7750
7751 total = isl_basic_map_total_dim(bmap);
7752
7753 for (i = 0; i < n; ++i) {
7754 k = isl_basic_map_alloc_equality(bmap);
7755 if (k < 0)
7756 goto error;
7757 isl_seq_clr(bmap->eq[k], 1 + total);
7758 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
7759 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
7760 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
7761 }
7762
7763 bmap = isl_basic_map_gauss(bmap, NULL);
7764 return isl_basic_map_finalize(bmap);
7765error:
7766 isl_basic_map_free(bmap);
7767 return NULL;
7768}
7769
7770/*
7771 * returns [domain -> range] -> range - domain
7772 */
7773__isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
7774{
7775 int i;
7776 isl_space *domain_dim;
7777
7778 if (!map)
7779 return NULL;
7780
7781 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
7782 map->dim, isl_dim_out))
7783 isl_die(map->ctx, isl_error_invalid,
7784 "domain and range don't match", goto error);
7785
7786 map = isl_map_cow(map);
7787 if (!map)
7788 return NULL;
7789
7790 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
7791 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
7792 map->dim = isl_space_join(map->dim, domain_dim);
7793 if (!map->dim)
7794 goto error;
7795 for (i = 0; i < map->n; ++i) {
7796 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
7797 if (!map->p[i])
7798 goto error;
7799 }
7800 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
7801 return map;
7802error:
7803 isl_map_free(map);
7804 return NULL;
7805}
7806
7807static __isl_give isl_basic_map *basic_map_identity(__isl_take isl_space *dims)
7808{
7809 struct isl_basic_map *bmap;
7810 unsigned nparam;
7811 unsigned dim;
7812 int i;
7813
7814 if (!dims)
7815 return NULL;
7816
7817 nparam = dims->nparam;
7818 dim = dims->n_out;
7819 bmap = isl_basic_map_alloc_space(dims, 0, dim, 0);
7820 if (!bmap)
7821 goto error;
7822
7823 for (i = 0; i < dim; ++i) {
7824 int j = isl_basic_map_alloc_equality(bmap);
7825 if (j < 0)
7826 goto error;
7827 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7828 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7829 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
7830 }
7831 return isl_basic_map_finalize(bmap);
7832error:
7833 isl_basic_map_free(bmap);
7834 return NULL;
7835}
7836
7837__isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *dim)
7838{
7839 if (!dim)
7840 return NULL;
7841 if (dim->n_in != dim->n_out)
7842 isl_die(dim->ctx, isl_error_invalid,
7843 "number of input and output dimensions needs to be "
7844 "the same", goto error);
7845 return basic_map_identity(dim);
7846error:
7847 isl_space_free(dim);
7848 return NULL;
7849}
7850
Tobias Grosser52a25232015-02-04 20:55:43 +00007851__isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
7852{
7853 return isl_map_from_basic_map(isl_basic_map_identity(dim));
7854}
7855
Tobias Grosser52a25232015-02-04 20:55:43 +00007856__isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
7857{
7858 isl_space *dim = isl_set_get_space(set);
7859 isl_map *id;
7860 id = isl_map_identity(isl_space_map_from_set(dim));
7861 return isl_map_intersect_range(id, set);
7862}
7863
7864/* Construct a basic set with all set dimensions having only non-negative
7865 * values.
7866 */
7867__isl_give isl_basic_set *isl_basic_set_positive_orthant(
7868 __isl_take isl_space *space)
7869{
7870 int i;
7871 unsigned nparam;
7872 unsigned dim;
7873 struct isl_basic_set *bset;
7874
7875 if (!space)
7876 return NULL;
7877 nparam = space->nparam;
7878 dim = space->n_out;
7879 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
7880 if (!bset)
7881 return NULL;
7882 for (i = 0; i < dim; ++i) {
7883 int k = isl_basic_set_alloc_inequality(bset);
7884 if (k < 0)
7885 goto error;
7886 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
7887 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
7888 }
7889 return bset;
7890error:
7891 isl_basic_set_free(bset);
7892 return NULL;
7893}
7894
7895/* Construct the half-space x_pos >= 0.
7896 */
7897static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *dim,
7898 int pos)
7899{
7900 int k;
7901 isl_basic_set *nonneg;
7902
7903 nonneg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7904 k = isl_basic_set_alloc_inequality(nonneg);
7905 if (k < 0)
7906 goto error;
7907 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
7908 isl_int_set_si(nonneg->ineq[k][pos], 1);
7909
7910 return isl_basic_set_finalize(nonneg);
7911error:
7912 isl_basic_set_free(nonneg);
7913 return NULL;
7914}
7915
7916/* Construct the half-space x_pos <= -1.
7917 */
7918static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *dim, int pos)
7919{
7920 int k;
7921 isl_basic_set *neg;
7922
7923 neg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7924 k = isl_basic_set_alloc_inequality(neg);
7925 if (k < 0)
7926 goto error;
7927 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
7928 isl_int_set_si(neg->ineq[k][0], -1);
7929 isl_int_set_si(neg->ineq[k][pos], -1);
7930
7931 return isl_basic_set_finalize(neg);
7932error:
7933 isl_basic_set_free(neg);
7934 return NULL;
7935}
7936
7937__isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
7938 enum isl_dim_type type, unsigned first, unsigned n)
7939{
7940 int i;
Tobias Grossere0f8d592015-05-13 13:10:13 +00007941 unsigned offset;
Tobias Grosser52a25232015-02-04 20:55:43 +00007942 isl_basic_set *nonneg;
7943 isl_basic_set *neg;
7944
7945 if (!set)
7946 return NULL;
7947 if (n == 0)
7948 return set;
7949
7950 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
7951
Tobias Grossere0f8d592015-05-13 13:10:13 +00007952 offset = pos(set->dim, type);
Tobias Grosser52a25232015-02-04 20:55:43 +00007953 for (i = 0; i < n; ++i) {
7954 nonneg = nonneg_halfspace(isl_set_get_space(set),
Tobias Grossere0f8d592015-05-13 13:10:13 +00007955 offset + first + i);
7956 neg = neg_halfspace(isl_set_get_space(set), offset + first + i);
Tobias Grosser52a25232015-02-04 20:55:43 +00007957
7958 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
7959 }
7960
7961 return set;
7962error:
7963 isl_set_free(set);
7964 return NULL;
7965}
7966
7967static int foreach_orthant(__isl_take isl_set *set, int *signs, int first,
7968 int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7969 void *user)
7970{
7971 isl_set *half;
7972
7973 if (!set)
7974 return -1;
7975 if (isl_set_plain_is_empty(set)) {
7976 isl_set_free(set);
7977 return 0;
7978 }
7979 if (first == len)
7980 return fn(set, signs, user);
7981
7982 signs[first] = 1;
7983 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
7984 1 + first));
7985 half = isl_set_intersect(half, isl_set_copy(set));
7986 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
7987 goto error;
7988
7989 signs[first] = -1;
7990 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
7991 1 + first));
7992 half = isl_set_intersect(half, set);
7993 return foreach_orthant(half, signs, first + 1, len, fn, user);
7994error:
7995 isl_set_free(set);
7996 return -1;
7997}
7998
7999/* Call "fn" on the intersections of "set" with each of the orthants
8000 * (except for obviously empty intersections). The orthant is identified
8001 * by the signs array, with each entry having value 1 or -1 according
8002 * to the sign of the corresponding variable.
8003 */
8004int isl_set_foreach_orthant(__isl_keep isl_set *set,
8005 int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
8006 void *user)
8007{
8008 unsigned nparam;
8009 unsigned nvar;
8010 int *signs;
8011 int r;
8012
8013 if (!set)
8014 return -1;
8015 if (isl_set_plain_is_empty(set))
8016 return 0;
8017
8018 nparam = isl_set_dim(set, isl_dim_param);
8019 nvar = isl_set_dim(set, isl_dim_set);
8020
8021 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
8022
8023 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
8024 fn, user);
8025
8026 free(signs);
8027
8028 return r;
8029}
8030
Tobias Grosserb2f39922015-05-28 13:32:11 +00008031isl_bool isl_set_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
Tobias Grosser52a25232015-02-04 20:55:43 +00008032{
Tobias Grosser06e15922016-11-16 11:06:47 +00008033 return isl_map_is_equal(set_to_map(set1), set_to_map(set2));
Tobias Grosser52a25232015-02-04 20:55:43 +00008034}
8035
Tobias Grosserb2f39922015-05-28 13:32:11 +00008036isl_bool isl_basic_map_is_subset(__isl_keep isl_basic_map *bmap1,
8037 __isl_keep isl_basic_map *bmap2)
Tobias Grosser52a25232015-02-04 20:55:43 +00008038{
8039 int is_subset;
8040 struct isl_map *map1;
8041 struct isl_map *map2;
8042
8043 if (!bmap1 || !bmap2)
Tobias Grosserb2f39922015-05-28 13:32:11 +00008044 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00008045
8046 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
8047 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
8048
8049 is_subset = isl_map_is_subset(map1, map2);
8050
8051 isl_map_free(map1);
8052 isl_map_free(map2);
8053
8054 return is_subset;
8055}
8056
Tobias Grosserb2f39922015-05-28 13:32:11 +00008057isl_bool isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
Tobias Grosser52a25232015-02-04 20:55:43 +00008058 __isl_keep isl_basic_set *bset2)
8059{
8060 return isl_basic_map_is_subset(bset1, bset2);
8061}
8062
Tobias Grosserb2f39922015-05-28 13:32:11 +00008063isl_bool isl_basic_map_is_equal(__isl_keep isl_basic_map *bmap1,
8064 __isl_keep isl_basic_map *bmap2)
Tobias Grosser52a25232015-02-04 20:55:43 +00008065{
Tobias Grosserb2f39922015-05-28 13:32:11 +00008066 isl_bool is_subset;
Tobias Grosser52a25232015-02-04 20:55:43 +00008067
8068 if (!bmap1 || !bmap2)
Tobias Grosserb2f39922015-05-28 13:32:11 +00008069 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00008070 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
Tobias Grosserb2f39922015-05-28 13:32:11 +00008071 if (is_subset != isl_bool_true)
Tobias Grosser52a25232015-02-04 20:55:43 +00008072 return is_subset;
8073 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
8074 return is_subset;
8075}
8076
Tobias Grosserb2f39922015-05-28 13:32:11 +00008077isl_bool isl_basic_set_is_equal(__isl_keep isl_basic_set *bset1,
8078 __isl_keep isl_basic_set *bset2)
Tobias Grosser52a25232015-02-04 20:55:43 +00008079{
8080 return isl_basic_map_is_equal(
Tobias Grosser06e15922016-11-16 11:06:47 +00008081 bset_to_bmap(bset1), bset_to_bmap(bset2));
Tobias Grosser52a25232015-02-04 20:55:43 +00008082}
8083
Tobias Grosserb2f39922015-05-28 13:32:11 +00008084isl_bool isl_map_is_empty(__isl_keep isl_map *map)
Tobias Grosser52a25232015-02-04 20:55:43 +00008085{
8086 int i;
8087 int is_empty;
8088
8089 if (!map)
Tobias Grosserb2f39922015-05-28 13:32:11 +00008090 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00008091 for (i = 0; i < map->n; ++i) {
8092 is_empty = isl_basic_map_is_empty(map->p[i]);
8093 if (is_empty < 0)
Tobias Grosserb2f39922015-05-28 13:32:11 +00008094 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00008095 if (!is_empty)
Tobias Grosserb2f39922015-05-28 13:32:11 +00008096 return isl_bool_false;
Tobias Grosser52a25232015-02-04 20:55:43 +00008097 }
Tobias Grosserb2f39922015-05-28 13:32:11 +00008098 return isl_bool_true;
Tobias Grosser52a25232015-02-04 20:55:43 +00008099}
8100
Tobias Grosserb2f39922015-05-28 13:32:11 +00008101isl_bool isl_map_plain_is_empty(__isl_keep isl_map *map)
Tobias Grosser52a25232015-02-04 20:55:43 +00008102{
Tobias Grosserb2f39922015-05-28 13:32:11 +00008103 return map ? map->n == 0 : isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00008104}
8105
Tobias Grosserb2f39922015-05-28 13:32:11 +00008106isl_bool isl_set_plain_is_empty(__isl_keep isl_set *set)
Tobias Grosser52a25232015-02-04 20:55:43 +00008107{
Tobias Grosserb2f39922015-05-28 13:32:11 +00008108 return set ? set->n == 0 : isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00008109}
8110
Tobias Grosserb2f39922015-05-28 13:32:11 +00008111isl_bool isl_set_is_empty(__isl_keep isl_set *set)
Tobias Grosser52a25232015-02-04 20:55:43 +00008112{
Tobias Grosser06e15922016-11-16 11:06:47 +00008113 return isl_map_is_empty(set_to_map(set));
Tobias Grosser52a25232015-02-04 20:55:43 +00008114}
8115
8116int isl_map_has_equal_space(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8117{
8118 if (!map1 || !map2)
8119 return -1;
8120
8121 return isl_space_is_equal(map1->dim, map2->dim);
8122}
8123
8124int isl_set_has_equal_space(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8125{
8126 if (!set1 || !set2)
8127 return -1;
8128
8129 return isl_space_is_equal(set1->dim, set2->dim);
8130}
8131
Tobias Grosserb2f39922015-05-28 13:32:11 +00008132static isl_bool map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
Tobias Grosser52a25232015-02-04 20:55:43 +00008133{
Tobias Grosserb2f39922015-05-28 13:32:11 +00008134 isl_bool is_subset;
Tobias Grosser52a25232015-02-04 20:55:43 +00008135
8136 if (!map1 || !map2)
Tobias Grosserb2f39922015-05-28 13:32:11 +00008137 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00008138 is_subset = isl_map_is_subset(map1, map2);
Tobias Grosserb2f39922015-05-28 13:32:11 +00008139 if (is_subset != isl_bool_true)
Tobias Grosser52a25232015-02-04 20:55:43 +00008140 return is_subset;
8141 is_subset = isl_map_is_subset(map2, map1);
8142 return is_subset;
8143}
8144
Tobias Grosser932ec012016-07-06 09:11:00 +00008145/* Is "map1" equal to "map2"?
8146 *
8147 * First check if they are obviously equal.
8148 * If not, then perform a more detailed analysis.
8149 */
Tobias Grosserb2f39922015-05-28 13:32:11 +00008150isl_bool isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
Tobias Grosser52a25232015-02-04 20:55:43 +00008151{
Tobias Grosser932ec012016-07-06 09:11:00 +00008152 isl_bool equal;
8153
8154 equal = isl_map_plain_is_equal(map1, map2);
8155 if (equal < 0 || equal)
8156 return equal;
Tobias Grosser52a25232015-02-04 20:55:43 +00008157 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
8158}
8159
Tobias Grosserb2f39922015-05-28 13:32:11 +00008160isl_bool isl_basic_map_is_strict_subset(
Tobias Grosser52a25232015-02-04 20:55:43 +00008161 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8162{
Tobias Grosserb2f39922015-05-28 13:32:11 +00008163 isl_bool is_subset;
Tobias Grosser52a25232015-02-04 20:55:43 +00008164
8165 if (!bmap1 || !bmap2)
Tobias Grosserb2f39922015-05-28 13:32:11 +00008166 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00008167 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
Tobias Grosserb2f39922015-05-28 13:32:11 +00008168 if (is_subset != isl_bool_true)
Tobias Grosser52a25232015-02-04 20:55:43 +00008169 return is_subset;
8170 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
Tobias Grosserb2f39922015-05-28 13:32:11 +00008171 if (is_subset == isl_bool_error)
Tobias Grosser52a25232015-02-04 20:55:43 +00008172 return is_subset;
8173 return !is_subset;
8174}
8175
Tobias Grosserb2f39922015-05-28 13:32:11 +00008176isl_bool isl_map_is_strict_subset(__isl_keep isl_map *map1,
8177 __isl_keep isl_map *map2)
Tobias Grosser52a25232015-02-04 20:55:43 +00008178{
Tobias Grosserb2f39922015-05-28 13:32:11 +00008179 isl_bool is_subset;
Tobias Grosser52a25232015-02-04 20:55:43 +00008180
8181 if (!map1 || !map2)
Tobias Grosserb2f39922015-05-28 13:32:11 +00008182 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00008183 is_subset = isl_map_is_subset(map1, map2);
Tobias Grosserb2f39922015-05-28 13:32:11 +00008184 if (is_subset != isl_bool_true)
Tobias Grosser52a25232015-02-04 20:55:43 +00008185 return is_subset;
8186 is_subset = isl_map_is_subset(map2, map1);
Tobias Grosserb2f39922015-05-28 13:32:11 +00008187 if (is_subset == isl_bool_error)
Tobias Grosser52a25232015-02-04 20:55:43 +00008188 return is_subset;
8189 return !is_subset;
8190}
8191
Tobias Grosserb2f39922015-05-28 13:32:11 +00008192isl_bool isl_set_is_strict_subset(__isl_keep isl_set *set1,
8193 __isl_keep isl_set *set2)
Tobias Grosser52a25232015-02-04 20:55:43 +00008194{
Tobias Grosser06e15922016-11-16 11:06:47 +00008195 return isl_map_is_strict_subset(set_to_map(set1), set_to_map(set2));
Tobias Grosser52a25232015-02-04 20:55:43 +00008196}
8197
Tobias Grossera67ac972016-02-26 11:35:12 +00008198/* Is "bmap" obviously equal to the universe with the same space?
8199 *
8200 * That is, does it not have any constraints?
8201 */
8202isl_bool isl_basic_map_plain_is_universe(__isl_keep isl_basic_map *bmap)
Tobias Grosser52a25232015-02-04 20:55:43 +00008203{
8204 if (!bmap)
Tobias Grosserb2f39922015-05-28 13:32:11 +00008205 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00008206 return bmap->n_eq == 0 && bmap->n_ineq == 0;
8207}
8208
Tobias Grossera67ac972016-02-26 11:35:12 +00008209/* Is "bset" obviously equal to the universe with the same space?
8210 */
8211isl_bool isl_basic_set_plain_is_universe(__isl_keep isl_basic_set *bset)
8212{
8213 return isl_basic_map_plain_is_universe(bset);
8214}
8215
8216/* If "c" does not involve any existentially quantified variables,
8217 * then set *univ to false and abort
8218 */
8219static isl_stat involves_divs(__isl_take isl_constraint *c, void *user)
8220{
8221 isl_bool *univ = user;
8222 unsigned n;
8223
8224 n = isl_constraint_dim(c, isl_dim_div);
8225 *univ = isl_constraint_involves_dims(c, isl_dim_div, 0, n);
8226 isl_constraint_free(c);
8227 if (*univ < 0 || !*univ)
8228 return isl_stat_error;
8229 return isl_stat_ok;
8230}
8231
8232/* Is "bmap" equal to the universe with the same space?
8233 *
8234 * First check if it is obviously equal to the universe.
8235 * If not and if there are any constraints not involving
8236 * existentially quantified variables, then it is certainly
8237 * not equal to the universe.
8238 * Otherwise, check if the universe is a subset of "bmap".
8239 */
8240isl_bool isl_basic_map_is_universe(__isl_keep isl_basic_map *bmap)
8241{
8242 isl_bool univ;
8243 isl_basic_map *test;
8244
8245 univ = isl_basic_map_plain_is_universe(bmap);
8246 if (univ < 0 || univ)
8247 return univ;
8248 if (isl_basic_map_dim(bmap, isl_dim_div) == 0)
8249 return isl_bool_false;
8250 univ = isl_bool_true;
8251 if (isl_basic_map_foreach_constraint(bmap, &involves_divs, &univ) < 0 &&
8252 univ)
8253 return isl_bool_error;
8254 if (univ < 0 || !univ)
8255 return univ;
8256 test = isl_basic_map_universe(isl_basic_map_get_space(bmap));
8257 univ = isl_basic_map_is_subset(test, bmap);
8258 isl_basic_map_free(test);
8259 return univ;
8260}
8261
8262/* Is "bset" equal to the universe with the same space?
8263 */
Tobias Grosserb2f39922015-05-28 13:32:11 +00008264isl_bool isl_basic_set_is_universe(__isl_keep isl_basic_set *bset)
Tobias Grosser52a25232015-02-04 20:55:43 +00008265{
Tobias Grossera67ac972016-02-26 11:35:12 +00008266 return isl_basic_map_is_universe(bset);
Tobias Grosser52a25232015-02-04 20:55:43 +00008267}
8268
Tobias Grosserb2f39922015-05-28 13:32:11 +00008269isl_bool isl_map_plain_is_universe(__isl_keep isl_map *map)
Tobias Grosser52a25232015-02-04 20:55:43 +00008270{
8271 int i;
8272
8273 if (!map)
Tobias Grosserb2f39922015-05-28 13:32:11 +00008274 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00008275
8276 for (i = 0; i < map->n; ++i) {
Tobias Grossera67ac972016-02-26 11:35:12 +00008277 isl_bool r = isl_basic_map_plain_is_universe(map->p[i]);
Tobias Grosser52a25232015-02-04 20:55:43 +00008278 if (r < 0 || r)
8279 return r;
8280 }
8281
Tobias Grosserb2f39922015-05-28 13:32:11 +00008282 return isl_bool_false;
Tobias Grosser52a25232015-02-04 20:55:43 +00008283}
8284
Tobias Grosserb2f39922015-05-28 13:32:11 +00008285isl_bool isl_set_plain_is_universe(__isl_keep isl_set *set)
Tobias Grosser52a25232015-02-04 20:55:43 +00008286{
Tobias Grosser06e15922016-11-16 11:06:47 +00008287 return isl_map_plain_is_universe(set_to_map(set));
Tobias Grosser52a25232015-02-04 20:55:43 +00008288}
8289
Tobias Grosserb2f39922015-05-28 13:32:11 +00008290isl_bool isl_basic_map_is_empty(__isl_keep isl_basic_map *bmap)
Tobias Grosser52a25232015-02-04 20:55:43 +00008291{
8292 struct isl_basic_set *bset = NULL;
8293 struct isl_vec *sample = NULL;
Tobias Grossera67ac972016-02-26 11:35:12 +00008294 isl_bool empty, non_empty;
Tobias Grosser52a25232015-02-04 20:55:43 +00008295
8296 if (!bmap)
Tobias Grosserb2f39922015-05-28 13:32:11 +00008297 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00008298
8299 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
Tobias Grosserb2f39922015-05-28 13:32:11 +00008300 return isl_bool_true;
Tobias Grosser52a25232015-02-04 20:55:43 +00008301
Tobias Grossera67ac972016-02-26 11:35:12 +00008302 if (isl_basic_map_plain_is_universe(bmap))
Tobias Grosserb2f39922015-05-28 13:32:11 +00008303 return isl_bool_false;
Tobias Grosser52a25232015-02-04 20:55:43 +00008304
8305 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
8306 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
8307 copy = isl_basic_map_remove_redundancies(copy);
8308 empty = isl_basic_map_plain_is_empty(copy);
8309 isl_basic_map_free(copy);
8310 return empty;
8311 }
8312
Tobias Grossera67ac972016-02-26 11:35:12 +00008313 non_empty = isl_basic_map_plain_is_non_empty(bmap);
8314 if (non_empty < 0)
8315 return isl_bool_error;
8316 if (non_empty)
8317 return isl_bool_false;
Tobias Grosser52a25232015-02-04 20:55:43 +00008318 isl_vec_free(bmap->sample);
8319 bmap->sample = NULL;
8320 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
8321 if (!bset)
Tobias Grosserb2f39922015-05-28 13:32:11 +00008322 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00008323 sample = isl_basic_set_sample_vec(bset);
8324 if (!sample)
Tobias Grosserb2f39922015-05-28 13:32:11 +00008325 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00008326 empty = sample->size == 0;
8327 isl_vec_free(bmap->sample);
8328 bmap->sample = sample;
8329 if (empty)
8330 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
8331
8332 return empty;
8333}
8334
Tobias Grosserb2f39922015-05-28 13:32:11 +00008335isl_bool isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
Tobias Grosser52a25232015-02-04 20:55:43 +00008336{
8337 if (!bmap)
Tobias Grosserb2f39922015-05-28 13:32:11 +00008338 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00008339 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
8340}
8341
Tobias Grosserb2f39922015-05-28 13:32:11 +00008342isl_bool isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
Tobias Grosser52a25232015-02-04 20:55:43 +00008343{
8344 if (!bset)
Tobias Grosserb2f39922015-05-28 13:32:11 +00008345 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00008346 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
8347}
8348
Tobias Grossera67ac972016-02-26 11:35:12 +00008349/* Is "bmap" known to be non-empty?
8350 *
8351 * That is, is the cached sample still valid?
8352 */
8353isl_bool isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map *bmap)
8354{
8355 unsigned total;
8356
8357 if (!bmap)
8358 return isl_bool_error;
8359 if (!bmap->sample)
8360 return isl_bool_false;
8361 total = 1 + isl_basic_map_total_dim(bmap);
8362 if (bmap->sample->size != total)
8363 return isl_bool_false;
8364 return isl_basic_map_contains(bmap, bmap->sample);
8365}
8366
Tobias Grosserb2f39922015-05-28 13:32:11 +00008367isl_bool isl_basic_set_is_empty(__isl_keep isl_basic_set *bset)
Tobias Grosser52a25232015-02-04 20:55:43 +00008368{
Tobias Grosser06e15922016-11-16 11:06:47 +00008369 return isl_basic_map_is_empty(bset_to_bmap(bset));
Tobias Grosser52a25232015-02-04 20:55:43 +00008370}
8371
8372struct isl_map *isl_basic_map_union(
8373 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8374{
8375 struct isl_map *map;
8376 if (!bmap1 || !bmap2)
8377 goto error;
8378
8379 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
8380
8381 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
8382 if (!map)
8383 goto error;
8384 map = isl_map_add_basic_map(map, bmap1);
8385 map = isl_map_add_basic_map(map, bmap2);
8386 return map;
8387error:
8388 isl_basic_map_free(bmap1);
8389 isl_basic_map_free(bmap2);
8390 return NULL;
8391}
8392
8393struct isl_set *isl_basic_set_union(
8394 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
8395{
Tobias Grosser06e15922016-11-16 11:06:47 +00008396 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1),
8397 bset_to_bmap(bset2)));
Tobias Grosser52a25232015-02-04 20:55:43 +00008398}
8399
8400/* Order divs such that any div only depends on previous divs */
8401struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
8402{
8403 int i;
8404 unsigned off;
8405
8406 if (!bmap)
8407 return NULL;
8408
8409 off = isl_space_dim(bmap->dim, isl_dim_all);
8410
8411 for (i = 0; i < bmap->n_div; ++i) {
8412 int pos;
8413 if (isl_int_is_zero(bmap->div[i][0]))
8414 continue;
8415 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
8416 bmap->n_div-i);
8417 if (pos == -1)
8418 continue;
Tobias Grosser37034db2016-03-25 19:38:18 +00008419 if (pos == 0)
8420 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
8421 "integer division depends on itself",
8422 return isl_basic_map_free(bmap));
Tobias Grosser52a25232015-02-04 20:55:43 +00008423 isl_basic_map_swap_div(bmap, i, i + pos);
8424 --i;
8425 }
8426 return bmap;
8427}
8428
8429struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
8430{
Tobias Grosser06e15922016-11-16 11:06:47 +00008431 return bset_from_bmap(isl_basic_map_order_divs(bset_to_bmap(bset)));
Tobias Grosser52a25232015-02-04 20:55:43 +00008432}
8433
8434__isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
8435{
8436 int i;
8437
8438 if (!map)
8439 return 0;
8440
8441 for (i = 0; i < map->n; ++i) {
8442 map->p[i] = isl_basic_map_order_divs(map->p[i]);
8443 if (!map->p[i])
8444 goto error;
8445 }
8446
8447 return map;
8448error:
8449 isl_map_free(map);
8450 return NULL;
8451}
8452
Tobias Grosser06e15922016-11-16 11:06:47 +00008453/* Sort the local variables of "bset".
8454 */
8455__isl_give isl_basic_set *isl_basic_set_sort_divs(
8456 __isl_take isl_basic_set *bset)
8457{
8458 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset)));
8459}
8460
Tobias Grosser52a25232015-02-04 20:55:43 +00008461/* Apply the expansion computed by isl_merge_divs.
8462 * The expansion itself is given by "exp" while the resulting
8463 * list of divs is given by "div".
Tobias Grossera67ac972016-02-26 11:35:12 +00008464 *
Tobias Grosser07b20952016-06-12 04:30:40 +00008465 * Move the integer divisions of "bmap" into the right position
Tobias Grossera67ac972016-02-26 11:35:12 +00008466 * according to "exp" and then introduce the additional integer
8467 * divisions, adding div constraints.
8468 * The moving should be done first to avoid moving coefficients
8469 * in the definitions of the extra integer divisions.
Tobias Grosser52a25232015-02-04 20:55:43 +00008470 */
Tobias Grosser07b20952016-06-12 04:30:40 +00008471__isl_give isl_basic_map *isl_basic_map_expand_divs(
Tobias Grosser06e15922016-11-16 11:06:47 +00008472 __isl_take isl_basic_map *bmap, __isl_take isl_mat *div, int *exp)
Tobias Grosser52a25232015-02-04 20:55:43 +00008473{
8474 int i, j;
8475 int n_div;
8476
Tobias Grosser07b20952016-06-12 04:30:40 +00008477 bmap = isl_basic_map_cow(bmap);
8478 if (!bmap || !div)
Tobias Grosser52a25232015-02-04 20:55:43 +00008479 goto error;
8480
Tobias Grosser07b20952016-06-12 04:30:40 +00008481 if (div->n_row < bmap->n_div)
Tobias Grosser52a25232015-02-04 20:55:43 +00008482 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
8483 "not an expansion", goto error);
8484
Tobias Grosser07b20952016-06-12 04:30:40 +00008485 n_div = bmap->n_div;
8486 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
Tobias Grosser52a25232015-02-04 20:55:43 +00008487 div->n_row - n_div, 0,
8488 2 * (div->n_row - n_div));
8489
8490 for (i = n_div; i < div->n_row; ++i)
Tobias Grosser07b20952016-06-12 04:30:40 +00008491 if (isl_basic_map_alloc_div(bmap) < 0)
Tobias Grosser52a25232015-02-04 20:55:43 +00008492 goto error;
8493
Tobias Grossera67ac972016-02-26 11:35:12 +00008494 for (j = n_div - 1; j >= 0; --j) {
8495 if (exp[j] == j)
8496 break;
Tobias Grosser07b20952016-06-12 04:30:40 +00008497 isl_basic_map_swap_div(bmap, j, exp[j]);
Tobias Grossera67ac972016-02-26 11:35:12 +00008498 }
8499 j = 0;
8500 for (i = 0; i < div->n_row; ++i) {
8501 if (j < n_div && exp[j] == i) {
8502 j++;
Tobias Grosser52a25232015-02-04 20:55:43 +00008503 } else {
Tobias Grosser07b20952016-06-12 04:30:40 +00008504 isl_seq_cpy(bmap->div[i], div->row[i], div->n_col);
Tobias Grosser932ec012016-07-06 09:11:00 +00008505 if (isl_basic_map_div_is_marked_unknown(bmap, i))
Tobias Grosser07b20952016-06-12 04:30:40 +00008506 continue;
8507 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
Tobias Grosser52a25232015-02-04 20:55:43 +00008508 goto error;
8509 }
8510 }
8511
8512 isl_mat_free(div);
Tobias Grosser07b20952016-06-12 04:30:40 +00008513 return bmap;
Tobias Grosser52a25232015-02-04 20:55:43 +00008514error:
Tobias Grosser07b20952016-06-12 04:30:40 +00008515 isl_basic_map_free(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +00008516 isl_mat_free(div);
8517 return NULL;
8518}
8519
Tobias Grosser07b20952016-06-12 04:30:40 +00008520/* Apply the expansion computed by isl_merge_divs.
8521 * The expansion itself is given by "exp" while the resulting
8522 * list of divs is given by "div".
8523 */
8524__isl_give isl_basic_set *isl_basic_set_expand_divs(
8525 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
8526{
8527 return isl_basic_map_expand_divs(bset, div, exp);
8528}
8529
Tobias Grosser52a25232015-02-04 20:55:43 +00008530/* Look for a div in dst that corresponds to the div "div" in src.
8531 * The divs before "div" in src and dst are assumed to be the same.
8532 *
8533 * Returns -1 if no corresponding div was found and the position
8534 * of the corresponding div in dst otherwise.
8535 */
8536static int find_div(struct isl_basic_map *dst,
8537 struct isl_basic_map *src, unsigned div)
8538{
8539 int i;
8540
8541 unsigned total = isl_space_dim(src->dim, isl_dim_all);
8542
8543 isl_assert(dst->ctx, div <= dst->n_div, return -1);
8544 for (i = div; i < dst->n_div; ++i)
8545 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
8546 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
8547 dst->n_div - div) == -1)
8548 return i;
8549 return -1;
8550}
8551
8552/* Align the divs of "dst" to those of "src", adding divs from "src"
8553 * if needed. That is, make sure that the first src->n_div divs
8554 * of the result are equal to those of src.
8555 *
8556 * The result is not finalized as by design it will have redundant
8557 * divs if any divs from "src" were copied.
8558 */
8559__isl_give isl_basic_map *isl_basic_map_align_divs(
8560 __isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src)
8561{
8562 int i;
8563 int known, extended;
8564 unsigned total;
8565
8566 if (!dst || !src)
8567 return isl_basic_map_free(dst);
8568
8569 if (src->n_div == 0)
8570 return dst;
8571
8572 known = isl_basic_map_divs_known(src);
8573 if (known < 0)
8574 return isl_basic_map_free(dst);
8575 if (!known)
8576 isl_die(isl_basic_map_get_ctx(src), isl_error_invalid,
8577 "some src divs are unknown",
8578 return isl_basic_map_free(dst));
8579
8580 src = isl_basic_map_order_divs(src);
8581
8582 extended = 0;
8583 total = isl_space_dim(src->dim, isl_dim_all);
8584 for (i = 0; i < src->n_div; ++i) {
8585 int j = find_div(dst, src, i);
8586 if (j < 0) {
8587 if (!extended) {
8588 int extra = src->n_div - i;
8589 dst = isl_basic_map_cow(dst);
8590 if (!dst)
8591 return NULL;
8592 dst = isl_basic_map_extend_space(dst,
8593 isl_space_copy(dst->dim),
8594 extra, 0, 2 * extra);
8595 extended = 1;
8596 }
8597 j = isl_basic_map_alloc_div(dst);
8598 if (j < 0)
8599 return isl_basic_map_free(dst);
8600 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
8601 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
8602 if (isl_basic_map_add_div_constraints(dst, j) < 0)
8603 return isl_basic_map_free(dst);
8604 }
8605 if (j != i)
8606 isl_basic_map_swap_div(dst, i, j);
8607 }
8608 return dst;
8609}
8610
8611struct isl_basic_set *isl_basic_set_align_divs(
8612 struct isl_basic_set *dst, struct isl_basic_set *src)
8613{
Tobias Grosser06e15922016-11-16 11:06:47 +00008614 return bset_from_bmap(isl_basic_map_align_divs(bset_to_bmap(dst),
8615 bset_to_bmap(src)));
Tobias Grosser52a25232015-02-04 20:55:43 +00008616}
8617
8618struct isl_map *isl_map_align_divs(struct isl_map *map)
8619{
8620 int i;
8621
8622 if (!map)
8623 return NULL;
8624 if (map->n == 0)
8625 return map;
8626 map = isl_map_compute_divs(map);
8627 map = isl_map_cow(map);
8628 if (!map)
8629 return NULL;
8630
8631 for (i = 1; i < map->n; ++i)
8632 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
8633 for (i = 1; i < map->n; ++i) {
8634 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
8635 if (!map->p[i])
8636 return isl_map_free(map);
8637 }
8638
8639 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
8640 return map;
8641}
8642
8643struct isl_set *isl_set_align_divs(struct isl_set *set)
8644{
Tobias Grosser06e15922016-11-16 11:06:47 +00008645 return set_from_map(isl_map_align_divs(set_to_map(set)));
Tobias Grosser52a25232015-02-04 20:55:43 +00008646}
8647
Tobias Grosser1fa7b972015-02-16 19:33:40 +00008648/* Align the divs of the basic maps in "map" to those
8649 * of the basic maps in "list", as well as to the other basic maps in "map".
Tobias Grosser52a25232015-02-04 20:55:43 +00008650 * The elements in "list" are assumed to have known divs.
8651 */
Tobias Grosser1fa7b972015-02-16 19:33:40 +00008652__isl_give isl_map *isl_map_align_divs_to_basic_map_list(
8653 __isl_take isl_map *map, __isl_keep isl_basic_map_list *list)
Tobias Grosser52a25232015-02-04 20:55:43 +00008654{
8655 int i, n;
8656
Tobias Grosser1fa7b972015-02-16 19:33:40 +00008657 map = isl_map_compute_divs(map);
8658 map = isl_map_cow(map);
8659 if (!map || !list)
8660 return isl_map_free(map);
8661 if (map->n == 0)
8662 return map;
Tobias Grosser52a25232015-02-04 20:55:43 +00008663
Tobias Grosser1fa7b972015-02-16 19:33:40 +00008664 n = isl_basic_map_list_n_basic_map(list);
Tobias Grosser52a25232015-02-04 20:55:43 +00008665 for (i = 0; i < n; ++i) {
Tobias Grosser1fa7b972015-02-16 19:33:40 +00008666 isl_basic_map *bmap;
Tobias Grosser52a25232015-02-04 20:55:43 +00008667
Tobias Grosser1fa7b972015-02-16 19:33:40 +00008668 bmap = isl_basic_map_list_get_basic_map(list, i);
8669 map->p[0] = isl_basic_map_align_divs(map->p[0], bmap);
8670 isl_basic_map_free(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +00008671 }
Tobias Grosser1fa7b972015-02-16 19:33:40 +00008672 if (!map->p[0])
8673 return isl_map_free(map);
Tobias Grosser52a25232015-02-04 20:55:43 +00008674
Tobias Grosser1fa7b972015-02-16 19:33:40 +00008675 return isl_map_align_divs(map);
Tobias Grosser52a25232015-02-04 20:55:43 +00008676}
8677
Tobias Grosser1fa7b972015-02-16 19:33:40 +00008678/* Align the divs of each element of "list" to those of "bmap".
8679 * Both "bmap" and the elements of "list" are assumed to have known divs.
Tobias Grosser52a25232015-02-04 20:55:43 +00008680 */
Tobias Grosser1fa7b972015-02-16 19:33:40 +00008681__isl_give isl_basic_map_list *isl_basic_map_list_align_divs_to_basic_map(
8682 __isl_take isl_basic_map_list *list, __isl_keep isl_basic_map *bmap)
Tobias Grosser52a25232015-02-04 20:55:43 +00008683{
8684 int i, n;
8685
Tobias Grosser1fa7b972015-02-16 19:33:40 +00008686 if (!list || !bmap)
8687 return isl_basic_map_list_free(list);
Tobias Grosser52a25232015-02-04 20:55:43 +00008688
Tobias Grosser1fa7b972015-02-16 19:33:40 +00008689 n = isl_basic_map_list_n_basic_map(list);
Tobias Grosser52a25232015-02-04 20:55:43 +00008690 for (i = 0; i < n; ++i) {
Tobias Grosser1fa7b972015-02-16 19:33:40 +00008691 isl_basic_map *bmap_i;
Tobias Grosser52a25232015-02-04 20:55:43 +00008692
Tobias Grosser1fa7b972015-02-16 19:33:40 +00008693 bmap_i = isl_basic_map_list_get_basic_map(list, i);
8694 bmap_i = isl_basic_map_align_divs(bmap_i, bmap);
8695 list = isl_basic_map_list_set_basic_map(list, i, bmap_i);
Tobias Grosser52a25232015-02-04 20:55:43 +00008696 }
8697
8698 return list;
8699}
8700
8701static __isl_give isl_set *set_apply( __isl_take isl_set *set,
8702 __isl_take isl_map *map)
8703{
8704 if (!set || !map)
8705 goto error;
8706 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
8707 map = isl_map_intersect_domain(map, set);
8708 set = isl_map_range(map);
8709 return set;
8710error:
8711 isl_set_free(set);
8712 isl_map_free(map);
8713 return NULL;
8714}
8715
8716__isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
8717 __isl_take isl_map *map)
8718{
8719 return isl_map_align_params_map_map_and(set, map, &set_apply);
8720}
8721
8722/* There is no need to cow as removing empty parts doesn't change
8723 * the meaning of the set.
8724 */
8725struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
8726{
8727 int i;
8728
8729 if (!map)
8730 return NULL;
8731
8732 for (i = map->n - 1; i >= 0; --i)
8733 remove_if_empty(map, i);
8734
8735 return map;
8736}
8737
8738struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
8739{
Tobias Grosser06e15922016-11-16 11:06:47 +00008740 return set_from_map(isl_map_remove_empty_parts(set_to_map(set)));
Tobias Grosser52a25232015-02-04 20:55:43 +00008741}
8742
Tobias Grossereab0943e2016-11-22 21:31:59 +00008743static __isl_give isl_basic_map *map_copy_basic_map(__isl_keep isl_map *map)
Tobias Grosser52a25232015-02-04 20:55:43 +00008744{
8745 struct isl_basic_map *bmap;
8746 if (!map || map->n == 0)
8747 return NULL;
8748 bmap = map->p[map->n-1];
8749 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
8750 return isl_basic_map_copy(bmap);
8751}
8752
Tobias Grossereab0943e2016-11-22 21:31:59 +00008753__isl_give isl_basic_map *isl_map_copy_basic_map(__isl_keep isl_map *map)
Tobias Grosser52a25232015-02-04 20:55:43 +00008754{
Tobias Grossereab0943e2016-11-22 21:31:59 +00008755 return map_copy_basic_map(map);
Tobias Grosser52a25232015-02-04 20:55:43 +00008756}
8757
Tobias Grossereab0943e2016-11-22 21:31:59 +00008758struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
8759{
8760 return bset_from_bmap(map_copy_basic_map(set_to_map(set)));
8761}
8762
8763static __isl_give isl_map *map_drop_basic_map(__isl_take isl_map *map,
Tobias Grosser52a25232015-02-04 20:55:43 +00008764 __isl_keep isl_basic_map *bmap)
8765{
8766 int i;
8767
8768 if (!map || !bmap)
8769 goto error;
8770 for (i = map->n-1; i >= 0; --i) {
8771 if (map->p[i] != bmap)
8772 continue;
8773 map = isl_map_cow(map);
8774 if (!map)
8775 goto error;
8776 isl_basic_map_free(map->p[i]);
8777 if (i != map->n-1) {
8778 ISL_F_CLR(map, ISL_SET_NORMALIZED);
8779 map->p[i] = map->p[map->n-1];
8780 }
8781 map->n--;
8782 return map;
8783 }
8784 return map;
8785error:
8786 isl_map_free(map);
8787 return NULL;
8788}
8789
Tobias Grossereab0943e2016-11-22 21:31:59 +00008790__isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
8791 __isl_keep isl_basic_map *bmap)
8792{
8793 return map_drop_basic_map(map, bmap);
8794}
8795
Tobias Grosser52a25232015-02-04 20:55:43 +00008796struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
8797 struct isl_basic_set *bset)
8798{
Tobias Grossereab0943e2016-11-22 21:31:59 +00008799 return set_from_map(map_drop_basic_map(set_to_map(set),
Tobias Grosser06e15922016-11-16 11:06:47 +00008800 bset_to_bmap(bset)));
Tobias Grosser52a25232015-02-04 20:55:43 +00008801}
8802
8803/* Given two basic sets bset1 and bset2, compute the maximal difference
8804 * between the values of dimension pos in bset1 and those in bset2
8805 * for any common value of the parameters and dimensions preceding pos.
8806 */
8807static enum isl_lp_result basic_set_maximal_difference_at(
8808 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
8809 int pos, isl_int *opt)
8810{
Tobias Grossereab0943e2016-11-22 21:31:59 +00008811 isl_basic_map *bmap1;
8812 isl_basic_map *bmap2;
Tobias Grosser52a25232015-02-04 20:55:43 +00008813 struct isl_ctx *ctx;
8814 struct isl_vec *obj;
8815 unsigned total;
8816 unsigned nparam;
Tobias Grossereab0943e2016-11-22 21:31:59 +00008817 unsigned dim1;
Tobias Grosser52a25232015-02-04 20:55:43 +00008818 enum isl_lp_result res;
8819
8820 if (!bset1 || !bset2)
8821 return isl_lp_error;
8822
8823 nparam = isl_basic_set_n_param(bset1);
8824 dim1 = isl_basic_set_n_dim(bset1);
Tobias Grossereab0943e2016-11-22 21:31:59 +00008825
8826 bmap1 = isl_basic_map_from_range(isl_basic_set_copy(bset1));
8827 bmap2 = isl_basic_map_from_range(isl_basic_set_copy(bset2));
8828 bmap1 = isl_basic_map_move_dims(bmap1, isl_dim_in, 0,
8829 isl_dim_out, 0, pos);
8830 bmap2 = isl_basic_map_move_dims(bmap2, isl_dim_in, 0,
8831 isl_dim_out, 0, pos);
8832 bmap1 = isl_basic_map_range_product(bmap1, bmap2);
Tobias Grosser52a25232015-02-04 20:55:43 +00008833 if (!bmap1)
Tobias Grossereab0943e2016-11-22 21:31:59 +00008834 return isl_lp_error;
8835
Tobias Grosser52a25232015-02-04 20:55:43 +00008836 total = isl_basic_map_total_dim(bmap1);
8837 ctx = bmap1->ctx;
8838 obj = isl_vec_alloc(ctx, 1 + total);
8839 if (!obj)
Tobias Grossereab0943e2016-11-22 21:31:59 +00008840 goto error;
Tobias Grosser52a25232015-02-04 20:55:43 +00008841 isl_seq_clr(obj->block.data, 1 + total);
8842 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
8843 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
8844 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
8845 opt, NULL, NULL);
8846 isl_basic_map_free(bmap1);
8847 isl_vec_free(obj);
8848 return res;
8849error:
Tobias Grosser52a25232015-02-04 20:55:43 +00008850 isl_basic_map_free(bmap1);
8851 return isl_lp_error;
8852}
8853
8854/* Given two _disjoint_ basic sets bset1 and bset2, check whether
8855 * for any common value of the parameters and dimensions preceding pos
8856 * in both basic sets, the values of dimension pos in bset1 are
8857 * smaller or larger than those in bset2.
8858 *
8859 * Returns
8860 * 1 if bset1 follows bset2
8861 * -1 if bset1 precedes bset2
8862 * 0 if bset1 and bset2 are incomparable
8863 * -2 if some error occurred.
8864 */
8865int isl_basic_set_compare_at(struct isl_basic_set *bset1,
8866 struct isl_basic_set *bset2, int pos)
8867{
8868 isl_int opt;
8869 enum isl_lp_result res;
8870 int cmp;
8871
8872 isl_int_init(opt);
8873
8874 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8875
8876 if (res == isl_lp_empty)
8877 cmp = 0;
8878 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8879 res == isl_lp_unbounded)
8880 cmp = 1;
8881 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8882 cmp = -1;
8883 else
8884 cmp = -2;
8885
8886 isl_int_clear(opt);
8887 return cmp;
8888}
8889
8890/* Given two basic sets bset1 and bset2, check whether
8891 * for any common value of the parameters and dimensions preceding pos
8892 * there is a value of dimension pos in bset1 that is larger
8893 * than a value of the same dimension in bset2.
8894 *
8895 * Return
8896 * 1 if there exists such a pair
8897 * 0 if there is no such pair, but there is a pair of equal values
8898 * -1 otherwise
8899 * -2 if some error occurred.
8900 */
8901int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
8902 __isl_keep isl_basic_set *bset2, int pos)
8903{
8904 isl_int opt;
8905 enum isl_lp_result res;
8906 int cmp;
8907
8908 isl_int_init(opt);
8909
8910 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8911
8912 if (res == isl_lp_empty)
8913 cmp = -1;
8914 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8915 res == isl_lp_unbounded)
8916 cmp = 1;
8917 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8918 cmp = -1;
8919 else if (res == isl_lp_ok)
8920 cmp = 0;
8921 else
8922 cmp = -2;
8923
8924 isl_int_clear(opt);
8925 return cmp;
8926}
8927
8928/* Given two sets set1 and set2, check whether
8929 * for any common value of the parameters and dimensions preceding pos
8930 * there is a value of dimension pos in set1 that is larger
8931 * than a value of the same dimension in set2.
8932 *
8933 * Return
8934 * 1 if there exists such a pair
8935 * 0 if there is no such pair, but there is a pair of equal values
8936 * -1 otherwise
8937 * -2 if some error occurred.
8938 */
8939int isl_set_follows_at(__isl_keep isl_set *set1,
8940 __isl_keep isl_set *set2, int pos)
8941{
8942 int i, j;
8943 int follows = -1;
8944
8945 if (!set1 || !set2)
8946 return -2;
8947
8948 for (i = 0; i < set1->n; ++i)
8949 for (j = 0; j < set2->n; ++j) {
8950 int f;
8951 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
8952 if (f == 1 || f == -2)
8953 return f;
8954 if (f > follows)
8955 follows = f;
8956 }
8957
8958 return follows;
8959}
8960
8961static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map *bmap,
8962 unsigned pos, isl_int *val)
8963{
8964 int i;
8965 int d;
8966 unsigned total;
8967
8968 if (!bmap)
8969 return -1;
8970 total = isl_basic_map_total_dim(bmap);
8971 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
8972 for (; d+1 > pos; --d)
8973 if (!isl_int_is_zero(bmap->eq[i][1+d]))
8974 break;
8975 if (d != pos)
8976 continue;
8977 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
8978 return 0;
8979 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
8980 return 0;
8981 if (!isl_int_is_one(bmap->eq[i][1+d]))
8982 return 0;
8983 if (val)
8984 isl_int_neg(*val, bmap->eq[i][0]);
8985 return 1;
8986 }
8987 return 0;
8988}
8989
8990static int isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
8991 unsigned pos, isl_int *val)
8992{
8993 int i;
8994 isl_int v;
8995 isl_int tmp;
8996 int fixed;
8997
8998 if (!map)
8999 return -1;
9000 if (map->n == 0)
9001 return 0;
9002 if (map->n == 1)
9003 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
9004 isl_int_init(v);
9005 isl_int_init(tmp);
9006 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
9007 for (i = 1; fixed == 1 && i < map->n; ++i) {
9008 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
9009 if (fixed == 1 && isl_int_ne(tmp, v))
9010 fixed = 0;
9011 }
9012 if (val)
9013 isl_int_set(*val, v);
9014 isl_int_clear(tmp);
9015 isl_int_clear(v);
9016 return fixed;
9017}
9018
9019static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set *bset,
9020 unsigned pos, isl_int *val)
9021{
Tobias Grosser06e15922016-11-16 11:06:47 +00009022 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset),
Tobias Grosser52a25232015-02-04 20:55:43 +00009023 pos, val);
9024}
9025
9026static int isl_set_plain_has_fixed_var(__isl_keep isl_set *set, unsigned pos,
9027 isl_int *val)
9028{
Tobias Grosser06e15922016-11-16 11:06:47 +00009029 return isl_map_plain_has_fixed_var(set_to_map(set), pos, val);
Tobias Grosser52a25232015-02-04 20:55:43 +00009030}
9031
9032int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
9033 enum isl_dim_type type, unsigned pos, isl_int *val)
9034{
9035 if (pos >= isl_basic_map_dim(bmap, type))
9036 return -1;
9037 return isl_basic_map_plain_has_fixed_var(bmap,
9038 isl_basic_map_offset(bmap, type) - 1 + pos, val);
9039}
9040
9041/* If "bmap" obviously lies on a hyperplane where the given dimension
9042 * has a fixed value, then return that value.
9043 * Otherwise return NaN.
9044 */
9045__isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
9046 __isl_keep isl_basic_map *bmap,
9047 enum isl_dim_type type, unsigned pos)
9048{
9049 isl_ctx *ctx;
9050 isl_val *v;
9051 int fixed;
9052
9053 if (!bmap)
9054 return NULL;
9055 ctx = isl_basic_map_get_ctx(bmap);
9056 v = isl_val_alloc(ctx);
9057 if (!v)
9058 return NULL;
9059 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
9060 if (fixed < 0)
9061 return isl_val_free(v);
9062 if (fixed) {
9063 isl_int_set_si(v->d, 1);
9064 return v;
9065 }
9066 isl_val_free(v);
9067 return isl_val_nan(ctx);
9068}
9069
9070int isl_map_plain_is_fixed(__isl_keep isl_map *map,
9071 enum isl_dim_type type, unsigned pos, isl_int *val)
9072{
9073 if (pos >= isl_map_dim(map, type))
9074 return -1;
9075 return isl_map_plain_has_fixed_var(map,
9076 map_offset(map, type) - 1 + pos, val);
9077}
9078
9079/* If "map" obviously lies on a hyperplane where the given dimension
9080 * has a fixed value, then return that value.
9081 * Otherwise return NaN.
9082 */
9083__isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
9084 enum isl_dim_type type, unsigned pos)
9085{
9086 isl_ctx *ctx;
9087 isl_val *v;
9088 int fixed;
9089
9090 if (!map)
9091 return NULL;
9092 ctx = isl_map_get_ctx(map);
9093 v = isl_val_alloc(ctx);
9094 if (!v)
9095 return NULL;
9096 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
9097 if (fixed < 0)
9098 return isl_val_free(v);
9099 if (fixed) {
9100 isl_int_set_si(v->d, 1);
9101 return v;
9102 }
9103 isl_val_free(v);
9104 return isl_val_nan(ctx);
9105}
9106
9107/* If "set" obviously lies on a hyperplane where the given dimension
9108 * has a fixed value, then return that value.
9109 * Otherwise return NaN.
9110 */
9111__isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
9112 enum isl_dim_type type, unsigned pos)
9113{
9114 return isl_map_plain_get_val_if_fixed(set, type, pos);
9115}
9116
9117int isl_set_plain_is_fixed(__isl_keep isl_set *set,
9118 enum isl_dim_type type, unsigned pos, isl_int *val)
9119{
9120 return isl_map_plain_is_fixed(set, type, pos, val);
9121}
9122
Tobias Grosser52a25232015-02-04 20:55:43 +00009123/* Check if dimension dim has fixed value and if so and if val is not NULL,
9124 * then return this fixed value in *val.
9125 */
9126int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
9127 unsigned dim, isl_int *val)
9128{
9129 return isl_basic_set_plain_has_fixed_var(bset,
9130 isl_basic_set_n_param(bset) + dim, val);
9131}
9132
9133/* Check if dimension dim has fixed value and if so and if val is not NULL,
9134 * then return this fixed value in *val.
9135 */
9136int isl_set_plain_dim_is_fixed(__isl_keep isl_set *set,
9137 unsigned dim, isl_int *val)
9138{
9139 return isl_set_plain_has_fixed_var(set, isl_set_n_param(set) + dim, val);
9140}
9141
Tobias Grosser52a25232015-02-04 20:55:43 +00009142/* Check if input variable in has fixed value and if so and if val is not NULL,
9143 * then return this fixed value in *val.
9144 */
9145int isl_map_plain_input_is_fixed(__isl_keep isl_map *map,
9146 unsigned in, isl_int *val)
9147{
9148 return isl_map_plain_has_fixed_var(map, isl_map_n_param(map) + in, val);
9149}
9150
9151/* Check if dimension dim has an (obvious) fixed lower bound and if so
9152 * and if val is not NULL, then return this lower bound in *val.
9153 */
9154int isl_basic_set_plain_dim_has_fixed_lower_bound(
9155 __isl_keep isl_basic_set *bset, unsigned dim, isl_int *val)
9156{
9157 int i, i_eq = -1, i_ineq = -1;
9158 isl_int *c;
9159 unsigned total;
9160 unsigned nparam;
9161
9162 if (!bset)
9163 return -1;
9164 total = isl_basic_set_total_dim(bset);
9165 nparam = isl_basic_set_n_param(bset);
9166 for (i = 0; i < bset->n_eq; ++i) {
9167 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
9168 continue;
9169 if (i_eq != -1)
9170 return 0;
9171 i_eq = i;
9172 }
9173 for (i = 0; i < bset->n_ineq; ++i) {
9174 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
9175 continue;
9176 if (i_eq != -1 || i_ineq != -1)
9177 return 0;
9178 i_ineq = i;
9179 }
9180 if (i_eq == -1 && i_ineq == -1)
9181 return 0;
9182 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
9183 /* The coefficient should always be one due to normalization. */
9184 if (!isl_int_is_one(c[1+nparam+dim]))
9185 return 0;
9186 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
9187 return 0;
9188 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
9189 total - nparam - dim - 1) != -1)
9190 return 0;
9191 if (val)
9192 isl_int_neg(*val, c[0]);
9193 return 1;
9194}
9195
9196int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set *set,
9197 unsigned dim, isl_int *val)
9198{
9199 int i;
9200 isl_int v;
9201 isl_int tmp;
9202 int fixed;
9203
9204 if (!set)
9205 return -1;
9206 if (set->n == 0)
9207 return 0;
9208 if (set->n == 1)
9209 return isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
9210 dim, val);
9211 isl_int_init(v);
9212 isl_int_init(tmp);
9213 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
9214 dim, &v);
9215 for (i = 1; fixed == 1 && i < set->n; ++i) {
9216 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[i],
9217 dim, &tmp);
9218 if (fixed == 1 && isl_int_ne(tmp, v))
9219 fixed = 0;
9220 }
9221 if (val)
9222 isl_int_set(*val, v);
9223 isl_int_clear(tmp);
9224 isl_int_clear(v);
9225 return fixed;
9226}
9227
Michael Kruse959a8dc2016-01-15 15:54:45 +00009228/* Return -1 if the constraint "c1" should be sorted before "c2"
9229 * and 1 if it should be sorted after "c2".
9230 * Return 0 if the two constraints are the same (up to the constant term).
9231 *
9232 * In particular, if a constraint involves later variables than another
9233 * then it is sorted after this other constraint.
9234 * uset_gist depends on constraints without existentially quantified
Tobias Grosser52a25232015-02-04 20:55:43 +00009235 * variables sorting first.
Michael Kruse959a8dc2016-01-15 15:54:45 +00009236 *
9237 * For constraints that have the same latest variable, those
9238 * with the same coefficient for this latest variable (first in absolute value
9239 * and then in actual value) are grouped together.
9240 * This is useful for detecting pairs of constraints that can
9241 * be chained in their printed representation.
9242 *
9243 * Finally, within a group, constraints are sorted according to
9244 * their coefficients (excluding the constant term).
Tobias Grosser52a25232015-02-04 20:55:43 +00009245 */
9246static int sort_constraint_cmp(const void *p1, const void *p2, void *arg)
9247{
9248 isl_int **c1 = (isl_int **) p1;
9249 isl_int **c2 = (isl_int **) p2;
9250 int l1, l2;
9251 unsigned size = *(unsigned *) arg;
Michael Kruse959a8dc2016-01-15 15:54:45 +00009252 int cmp;
Tobias Grosser52a25232015-02-04 20:55:43 +00009253
9254 l1 = isl_seq_last_non_zero(*c1 + 1, size);
9255 l2 = isl_seq_last_non_zero(*c2 + 1, size);
9256
9257 if (l1 != l2)
9258 return l1 - l2;
9259
Michael Kruse959a8dc2016-01-15 15:54:45 +00009260 cmp = isl_int_abs_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9261 if (cmp != 0)
9262 return cmp;
9263 cmp = isl_int_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9264 if (cmp != 0)
9265 return -cmp;
9266
Tobias Grosser52a25232015-02-04 20:55:43 +00009267 return isl_seq_cmp(*c1 + 1, *c2 + 1, size);
9268}
9269
Michael Kruse959a8dc2016-01-15 15:54:45 +00009270/* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
9271 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
9272 * and 0 if the two constraints are the same (up to the constant term).
9273 */
9274int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map *bmap,
9275 isl_int *c1, isl_int *c2)
9276{
9277 unsigned total;
9278
9279 if (!bmap)
9280 return -2;
9281 total = isl_basic_map_total_dim(bmap);
9282 return sort_constraint_cmp(&c1, &c2, &total);
9283}
9284
9285__isl_give isl_basic_map *isl_basic_map_sort_constraints(
9286 __isl_take isl_basic_map *bmap)
Tobias Grosser52a25232015-02-04 20:55:43 +00009287{
9288 unsigned total;
9289
9290 if (!bmap)
9291 return NULL;
9292 if (bmap->n_ineq == 0)
9293 return bmap;
9294 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
9295 return bmap;
9296 total = isl_basic_map_total_dim(bmap);
9297 if (isl_sort(bmap->ineq, bmap->n_ineq, sizeof(isl_int *),
9298 &sort_constraint_cmp, &total) < 0)
9299 return isl_basic_map_free(bmap);
9300 return bmap;
9301}
9302
9303__isl_give isl_basic_set *isl_basic_set_sort_constraints(
9304 __isl_take isl_basic_set *bset)
9305{
Tobias Grosser06e15922016-11-16 11:06:47 +00009306 isl_basic_map *bmap = bset_to_bmap(bset);
9307 return bset_from_bmap(isl_basic_map_sort_constraints(bmap));
Tobias Grosser52a25232015-02-04 20:55:43 +00009308}
9309
9310struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
9311{
9312 if (!bmap)
9313 return NULL;
9314 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
9315 return bmap;
9316 bmap = isl_basic_map_remove_redundancies(bmap);
9317 bmap = isl_basic_map_sort_constraints(bmap);
9318 if (bmap)
9319 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
9320 return bmap;
9321}
9322
9323struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
9324{
Tobias Grosser06e15922016-11-16 11:06:47 +00009325 return bset_from_bmap(isl_basic_map_normalize(bset_to_bmap(bset)));
Tobias Grosser52a25232015-02-04 20:55:43 +00009326}
9327
9328int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map *bmap1,
9329 const __isl_keep isl_basic_map *bmap2)
9330{
9331 int i, cmp;
9332 unsigned total;
9333
Tobias Grosser3e6070e2015-05-09 09:37:30 +00009334 if (!bmap1 || !bmap2)
9335 return -1;
9336
Tobias Grosser52a25232015-02-04 20:55:43 +00009337 if (bmap1 == bmap2)
9338 return 0;
9339 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
9340 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
9341 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
9342 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
9343 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
9344 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
9345 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
9346 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
9347 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
9348 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
9349 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9350 return 0;
9351 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
9352 return 1;
9353 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9354 return -1;
9355 if (bmap1->n_eq != bmap2->n_eq)
9356 return bmap1->n_eq - bmap2->n_eq;
9357 if (bmap1->n_ineq != bmap2->n_ineq)
9358 return bmap1->n_ineq - bmap2->n_ineq;
9359 if (bmap1->n_div != bmap2->n_div)
9360 return bmap1->n_div - bmap2->n_div;
9361 total = isl_basic_map_total_dim(bmap1);
9362 for (i = 0; i < bmap1->n_eq; ++i) {
9363 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
9364 if (cmp)
9365 return cmp;
9366 }
9367 for (i = 0; i < bmap1->n_ineq; ++i) {
9368 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
9369 if (cmp)
9370 return cmp;
9371 }
9372 for (i = 0; i < bmap1->n_div; ++i) {
9373 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
9374 if (cmp)
9375 return cmp;
9376 }
9377 return 0;
9378}
9379
9380int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set *bset1,
9381 const __isl_keep isl_basic_set *bset2)
9382{
9383 return isl_basic_map_plain_cmp(bset1, bset2);
9384}
9385
9386int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9387{
9388 int i, cmp;
9389
9390 if (set1 == set2)
9391 return 0;
9392 if (set1->n != set2->n)
9393 return set1->n - set2->n;
9394
9395 for (i = 0; i < set1->n; ++i) {
9396 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
9397 if (cmp)
9398 return cmp;
9399 }
9400
9401 return 0;
9402}
9403
Tobias Grosserb2f39922015-05-28 13:32:11 +00009404isl_bool isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
Tobias Grosser52a25232015-02-04 20:55:43 +00009405 __isl_keep isl_basic_map *bmap2)
9406{
Tobias Grossere0f8d592015-05-13 13:10:13 +00009407 if (!bmap1 || !bmap2)
Tobias Grosserb2f39922015-05-28 13:32:11 +00009408 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00009409 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
9410}
9411
Tobias Grosserb2f39922015-05-28 13:32:11 +00009412isl_bool isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
Tobias Grosser52a25232015-02-04 20:55:43 +00009413 __isl_keep isl_basic_set *bset2)
9414{
Tobias Grosser06e15922016-11-16 11:06:47 +00009415 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1),
9416 bset_to_bmap(bset2));
Tobias Grosser52a25232015-02-04 20:55:43 +00009417}
9418
9419static int qsort_bmap_cmp(const void *p1, const void *p2)
9420{
9421 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
9422 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
9423
9424 return isl_basic_map_plain_cmp(bmap1, bmap2);
9425}
9426
9427/* Sort the basic maps of "map" and remove duplicate basic maps.
9428 *
9429 * While removing basic maps, we make sure that the basic maps remain
9430 * sorted because isl_map_normalize expects the basic maps of the result
9431 * to be sorted.
9432 */
9433static __isl_give isl_map *sort_and_remove_duplicates(__isl_take isl_map *map)
9434{
9435 int i, j;
9436
9437 map = isl_map_remove_empty_parts(map);
9438 if (!map)
9439 return NULL;
9440 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
9441 for (i = map->n - 1; i >= 1; --i) {
9442 if (!isl_basic_map_plain_is_equal(map->p[i - 1], map->p[i]))
9443 continue;
9444 isl_basic_map_free(map->p[i-1]);
9445 for (j = i; j < map->n; ++j)
9446 map->p[j - 1] = map->p[j];
9447 map->n--;
9448 }
9449
9450 return map;
9451}
9452
9453/* Remove obvious duplicates among the basic maps of "map".
9454 *
9455 * Unlike isl_map_normalize, this function does not remove redundant
9456 * constraints and only removes duplicates that have exactly the same
9457 * constraints in the input. It does sort the constraints and
9458 * the basic maps to ease the detection of duplicates.
9459 *
9460 * If "map" has already been normalized or if the basic maps are
9461 * disjoint, then there can be no duplicates.
9462 */
9463__isl_give isl_map *isl_map_remove_obvious_duplicates(__isl_take isl_map *map)
9464{
9465 int i;
9466 isl_basic_map *bmap;
9467
9468 if (!map)
9469 return NULL;
9470 if (map->n <= 1)
9471 return map;
9472 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED | ISL_MAP_DISJOINT))
9473 return map;
9474 for (i = 0; i < map->n; ++i) {
9475 bmap = isl_basic_map_copy(map->p[i]);
9476 bmap = isl_basic_map_sort_constraints(bmap);
9477 if (!bmap)
9478 return isl_map_free(map);
9479 isl_basic_map_free(map->p[i]);
9480 map->p[i] = bmap;
9481 }
9482
9483 map = sort_and_remove_duplicates(map);
9484 return map;
9485}
9486
9487/* We normalize in place, but if anything goes wrong we need
9488 * to return NULL, so we need to make sure we don't change the
9489 * meaning of any possible other copies of map.
9490 */
9491__isl_give isl_map *isl_map_normalize(__isl_take isl_map *map)
9492{
9493 int i;
9494 struct isl_basic_map *bmap;
9495
9496 if (!map)
9497 return NULL;
9498 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
9499 return map;
9500 for (i = 0; i < map->n; ++i) {
9501 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
9502 if (!bmap)
9503 goto error;
9504 isl_basic_map_free(map->p[i]);
9505 map->p[i] = bmap;
9506 }
9507
9508 map = sort_and_remove_duplicates(map);
9509 if (map)
9510 ISL_F_SET(map, ISL_MAP_NORMALIZED);
9511 return map;
9512error:
9513 isl_map_free(map);
9514 return NULL;
9515}
9516
9517struct isl_set *isl_set_normalize(struct isl_set *set)
9518{
Tobias Grosser06e15922016-11-16 11:06:47 +00009519 return set_from_map(isl_map_normalize(set_to_map(set)));
Tobias Grosser52a25232015-02-04 20:55:43 +00009520}
9521
Tobias Grosserb2f39922015-05-28 13:32:11 +00009522isl_bool isl_map_plain_is_equal(__isl_keep isl_map *map1,
9523 __isl_keep isl_map *map2)
Tobias Grosser52a25232015-02-04 20:55:43 +00009524{
9525 int i;
Tobias Grosserb2f39922015-05-28 13:32:11 +00009526 isl_bool equal;
Tobias Grosser52a25232015-02-04 20:55:43 +00009527
9528 if (!map1 || !map2)
Tobias Grosserb2f39922015-05-28 13:32:11 +00009529 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00009530
9531 if (map1 == map2)
Tobias Grosserb2f39922015-05-28 13:32:11 +00009532 return isl_bool_true;
Tobias Grosser52a25232015-02-04 20:55:43 +00009533 if (!isl_space_is_equal(map1->dim, map2->dim))
Tobias Grosserb2f39922015-05-28 13:32:11 +00009534 return isl_bool_false;
Tobias Grosser52a25232015-02-04 20:55:43 +00009535
9536 map1 = isl_map_copy(map1);
9537 map2 = isl_map_copy(map2);
9538 map1 = isl_map_normalize(map1);
9539 map2 = isl_map_normalize(map2);
9540 if (!map1 || !map2)
9541 goto error;
9542 equal = map1->n == map2->n;
9543 for (i = 0; equal && i < map1->n; ++i) {
9544 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
9545 if (equal < 0)
9546 goto error;
9547 }
9548 isl_map_free(map1);
9549 isl_map_free(map2);
9550 return equal;
9551error:
9552 isl_map_free(map1);
9553 isl_map_free(map2);
Tobias Grosserb2f39922015-05-28 13:32:11 +00009554 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00009555}
9556
Tobias Grosserb2f39922015-05-28 13:32:11 +00009557isl_bool isl_set_plain_is_equal(__isl_keep isl_set *set1,
9558 __isl_keep isl_set *set2)
Tobias Grosser52a25232015-02-04 20:55:43 +00009559{
Tobias Grosser06e15922016-11-16 11:06:47 +00009560 return isl_map_plain_is_equal(set_to_map(set1), set_to_map(set2));
Tobias Grosser52a25232015-02-04 20:55:43 +00009561}
9562
Tobias Grosser52a25232015-02-04 20:55:43 +00009563/* Return an interval that ranges from min to max (inclusive)
9564 */
9565struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
9566 isl_int min, isl_int max)
9567{
9568 int k;
9569 struct isl_basic_set *bset = NULL;
9570
9571 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
9572 if (!bset)
9573 goto error;
9574
9575 k = isl_basic_set_alloc_inequality(bset);
9576 if (k < 0)
9577 goto error;
9578 isl_int_set_si(bset->ineq[k][1], 1);
9579 isl_int_neg(bset->ineq[k][0], min);
9580
9581 k = isl_basic_set_alloc_inequality(bset);
9582 if (k < 0)
9583 goto error;
9584 isl_int_set_si(bset->ineq[k][1], -1);
9585 isl_int_set(bset->ineq[k][0], max);
9586
9587 return bset;
9588error:
9589 isl_basic_set_free(bset);
9590 return NULL;
9591}
9592
Tobias Grosser1fa7b972015-02-16 19:33:40 +00009593/* Return the basic maps in "map" as a list.
Tobias Grosser52a25232015-02-04 20:55:43 +00009594 */
Tobias Grosser1fa7b972015-02-16 19:33:40 +00009595__isl_give isl_basic_map_list *isl_map_get_basic_map_list(
9596 __isl_keep isl_map *map)
Tobias Grosser52a25232015-02-04 20:55:43 +00009597{
9598 int i;
9599 isl_ctx *ctx;
Tobias Grosser1fa7b972015-02-16 19:33:40 +00009600 isl_basic_map_list *list;
Tobias Grosser52a25232015-02-04 20:55:43 +00009601
Tobias Grosser1fa7b972015-02-16 19:33:40 +00009602 if (!map)
Tobias Grosser52a25232015-02-04 20:55:43 +00009603 return NULL;
Tobias Grosser1fa7b972015-02-16 19:33:40 +00009604 ctx = isl_map_get_ctx(map);
9605 list = isl_basic_map_list_alloc(ctx, map->n);
Tobias Grosser52a25232015-02-04 20:55:43 +00009606
Tobias Grosser1fa7b972015-02-16 19:33:40 +00009607 for (i = 0; i < map->n; ++i) {
9608 isl_basic_map *bmap;
Tobias Grosser52a25232015-02-04 20:55:43 +00009609
Tobias Grosser1fa7b972015-02-16 19:33:40 +00009610 bmap = isl_basic_map_copy(map->p[i]);
9611 list = isl_basic_map_list_add(list, bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +00009612 }
9613
9614 return list;
9615}
9616
9617/* Return the intersection of the elements in the non-empty list "list".
9618 * All elements are assumed to live in the same space.
9619 */
Tobias Grosser1fa7b972015-02-16 19:33:40 +00009620__isl_give isl_basic_map *isl_basic_map_list_intersect(
9621 __isl_take isl_basic_map_list *list)
Tobias Grosser52a25232015-02-04 20:55:43 +00009622{
9623 int i, n;
Tobias Grosser1fa7b972015-02-16 19:33:40 +00009624 isl_basic_map *bmap;
Tobias Grosser52a25232015-02-04 20:55:43 +00009625
9626 if (!list)
9627 return NULL;
Tobias Grosser1fa7b972015-02-16 19:33:40 +00009628 n = isl_basic_map_list_n_basic_map(list);
Tobias Grosser52a25232015-02-04 20:55:43 +00009629 if (n < 1)
Tobias Grosser1fa7b972015-02-16 19:33:40 +00009630 isl_die(isl_basic_map_list_get_ctx(list), isl_error_invalid,
Tobias Grosser52a25232015-02-04 20:55:43 +00009631 "expecting non-empty list", goto error);
Tobias Grosser52a25232015-02-04 20:55:43 +00009632
Tobias Grosser1fa7b972015-02-16 19:33:40 +00009633 bmap = isl_basic_map_list_get_basic_map(list, 0);
9634 for (i = 1; i < n; ++i) {
9635 isl_basic_map *bmap_i;
9636
9637 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9638 bmap = isl_basic_map_intersect(bmap, bmap_i);
Tobias Grosser52a25232015-02-04 20:55:43 +00009639 }
9640
Tobias Grosser1fa7b972015-02-16 19:33:40 +00009641 isl_basic_map_list_free(list);
9642 return bmap;
Tobias Grosser52a25232015-02-04 20:55:43 +00009643error:
Tobias Grosser1fa7b972015-02-16 19:33:40 +00009644 isl_basic_map_list_free(list);
Tobias Grosser52a25232015-02-04 20:55:43 +00009645 return NULL;
9646}
9647
Tobias Grosser1fa7b972015-02-16 19:33:40 +00009648/* Return the intersection of the elements in the non-empty list "list".
9649 * All elements are assumed to live in the same space.
9650 */
9651__isl_give isl_basic_set *isl_basic_set_list_intersect(
9652 __isl_take isl_basic_set_list *list)
9653{
9654 return isl_basic_map_list_intersect(list);
9655}
9656
Tobias Grossereab0943e2016-11-22 21:31:59 +00009657/* Return the union of the elements of "list".
9658 * The list is required to have at least one element.
9659 */
9660__isl_give isl_set *isl_basic_set_list_union(
9661 __isl_take isl_basic_set_list *list)
9662{
9663 int i, n;
9664 isl_space *space;
9665 isl_basic_set *bset;
9666 isl_set *set;
9667
9668 if (!list)
9669 return NULL;
9670 n = isl_basic_set_list_n_basic_set(list);
9671 if (n < 1)
9672 isl_die(isl_basic_set_list_get_ctx(list), isl_error_invalid,
9673 "expecting non-empty list", goto error);
9674
9675 bset = isl_basic_set_list_get_basic_set(list, 0);
9676 space = isl_basic_set_get_space(bset);
9677 isl_basic_set_free(bset);
9678
9679 set = isl_set_alloc_space(space, n, 0);
9680 for (i = 0; i < n; ++i) {
9681 bset = isl_basic_set_list_get_basic_set(list, i);
9682 set = isl_set_add_basic_set(set, bset);
9683 }
9684
9685 isl_basic_set_list_free(list);
9686 return set;
9687error:
9688 isl_basic_set_list_free(list);
9689 return NULL;
9690}
9691
Tobias Grossera67ac972016-02-26 11:35:12 +00009692/* Return the union of the elements in the non-empty list "list".
9693 * All elements are assumed to live in the same space.
9694 */
9695__isl_give isl_set *isl_set_list_union(__isl_take isl_set_list *list)
9696{
9697 int i, n;
9698 isl_set *set;
9699
9700 if (!list)
9701 return NULL;
9702 n = isl_set_list_n_set(list);
9703 if (n < 1)
9704 isl_die(isl_set_list_get_ctx(list), isl_error_invalid,
9705 "expecting non-empty list", goto error);
9706
9707 set = isl_set_list_get_set(list, 0);
9708 for (i = 1; i < n; ++i) {
9709 isl_set *set_i;
9710
9711 set_i = isl_set_list_get_set(list, i);
9712 set = isl_set_union(set, set_i);
9713 }
9714
9715 isl_set_list_free(list);
9716 return set;
9717error:
9718 isl_set_list_free(list);
9719 return NULL;
9720}
9721
Tobias Grosser52a25232015-02-04 20:55:43 +00009722/* Return the Cartesian product of the basic sets in list (in the given order).
9723 */
9724__isl_give isl_basic_set *isl_basic_set_list_product(
9725 __isl_take struct isl_basic_set_list *list)
9726{
9727 int i;
9728 unsigned dim;
9729 unsigned nparam;
9730 unsigned extra;
9731 unsigned n_eq;
9732 unsigned n_ineq;
9733 struct isl_basic_set *product = NULL;
9734
9735 if (!list)
9736 goto error;
9737 isl_assert(list->ctx, list->n > 0, goto error);
9738 isl_assert(list->ctx, list->p[0], goto error);
9739 nparam = isl_basic_set_n_param(list->p[0]);
9740 dim = isl_basic_set_n_dim(list->p[0]);
9741 extra = list->p[0]->n_div;
9742 n_eq = list->p[0]->n_eq;
9743 n_ineq = list->p[0]->n_ineq;
9744 for (i = 1; i < list->n; ++i) {
9745 isl_assert(list->ctx, list->p[i], goto error);
9746 isl_assert(list->ctx,
9747 nparam == isl_basic_set_n_param(list->p[i]), goto error);
9748 dim += isl_basic_set_n_dim(list->p[i]);
9749 extra += list->p[i]->n_div;
9750 n_eq += list->p[i]->n_eq;
9751 n_ineq += list->p[i]->n_ineq;
9752 }
9753 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
9754 n_eq, n_ineq);
9755 if (!product)
9756 goto error;
9757 dim = 0;
9758 for (i = 0; i < list->n; ++i) {
9759 isl_basic_set_add_constraints(product,
9760 isl_basic_set_copy(list->p[i]), dim);
9761 dim += isl_basic_set_n_dim(list->p[i]);
9762 }
9763 isl_basic_set_list_free(list);
9764 return product;
9765error:
9766 isl_basic_set_free(product);
9767 isl_basic_set_list_free(list);
9768 return NULL;
9769}
9770
9771struct isl_basic_map *isl_basic_map_product(
9772 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
9773{
9774 isl_space *dim_result = NULL;
9775 struct isl_basic_map *bmap;
9776 unsigned in1, in2, out1, out2, nparam, total, pos;
9777 struct isl_dim_map *dim_map1, *dim_map2;
9778
9779 if (!bmap1 || !bmap2)
9780 goto error;
9781
9782 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
9783 bmap2->dim, isl_dim_param), goto error);
9784 dim_result = isl_space_product(isl_space_copy(bmap1->dim),
9785 isl_space_copy(bmap2->dim));
9786
9787 in1 = isl_basic_map_n_in(bmap1);
9788 in2 = isl_basic_map_n_in(bmap2);
9789 out1 = isl_basic_map_n_out(bmap1);
9790 out2 = isl_basic_map_n_out(bmap2);
9791 nparam = isl_basic_map_n_param(bmap1);
9792
9793 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
9794 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9795 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9796 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9797 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9798 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9799 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9800 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9801 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9802 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9803 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9804
9805 bmap = isl_basic_map_alloc_space(dim_result,
9806 bmap1->n_div + bmap2->n_div,
9807 bmap1->n_eq + bmap2->n_eq,
9808 bmap1->n_ineq + bmap2->n_ineq);
9809 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9810 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9811 bmap = isl_basic_map_simplify(bmap);
9812 return isl_basic_map_finalize(bmap);
9813error:
9814 isl_basic_map_free(bmap1);
9815 isl_basic_map_free(bmap2);
9816 return NULL;
9817}
9818
9819__isl_give isl_basic_map *isl_basic_map_flat_product(
9820 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9821{
9822 isl_basic_map *prod;
9823
9824 prod = isl_basic_map_product(bmap1, bmap2);
9825 prod = isl_basic_map_flatten(prod);
9826 return prod;
9827}
9828
9829__isl_give isl_basic_set *isl_basic_set_flat_product(
9830 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
9831{
9832 return isl_basic_map_flat_range_product(bset1, bset2);
9833}
9834
9835__isl_give isl_basic_map *isl_basic_map_domain_product(
9836 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9837{
9838 isl_space *space_result = NULL;
9839 isl_basic_map *bmap;
9840 unsigned in1, in2, out, nparam, total, pos;
9841 struct isl_dim_map *dim_map1, *dim_map2;
9842
9843 if (!bmap1 || !bmap2)
9844 goto error;
9845
9846 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
9847 isl_space_copy(bmap2->dim));
9848
9849 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9850 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9851 out = isl_basic_map_dim(bmap1, isl_dim_out);
9852 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9853
9854 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
9855 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9856 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9857 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9858 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9859 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9860 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9861 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9862 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
9863 isl_dim_map_div(dim_map1, bmap1, pos += out);
9864 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9865
9866 bmap = isl_basic_map_alloc_space(space_result,
9867 bmap1->n_div + bmap2->n_div,
9868 bmap1->n_eq + bmap2->n_eq,
9869 bmap1->n_ineq + bmap2->n_ineq);
9870 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9871 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9872 bmap = isl_basic_map_simplify(bmap);
9873 return isl_basic_map_finalize(bmap);
9874error:
9875 isl_basic_map_free(bmap1);
9876 isl_basic_map_free(bmap2);
9877 return NULL;
9878}
9879
9880__isl_give isl_basic_map *isl_basic_map_range_product(
9881 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9882{
Tobias Grosser5652c9c2016-10-01 19:46:51 +00009883 int rational;
Tobias Grosser52a25232015-02-04 20:55:43 +00009884 isl_space *dim_result = NULL;
9885 isl_basic_map *bmap;
9886 unsigned in, out1, out2, nparam, total, pos;
9887 struct isl_dim_map *dim_map1, *dim_map2;
9888
Tobias Grosser5652c9c2016-10-01 19:46:51 +00009889 rational = isl_basic_map_is_rational(bmap1);
9890 if (rational >= 0 && rational)
9891 rational = isl_basic_map_is_rational(bmap2);
9892 if (!bmap1 || !bmap2 || rational < 0)
Tobias Grosser52a25232015-02-04 20:55:43 +00009893 goto error;
9894
9895 if (!isl_space_match(bmap1->dim, isl_dim_param,
9896 bmap2->dim, isl_dim_param))
9897 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
9898 "parameters don't match", goto error);
9899
9900 dim_result = isl_space_range_product(isl_space_copy(bmap1->dim),
9901 isl_space_copy(bmap2->dim));
9902
9903 in = isl_basic_map_dim(bmap1, isl_dim_in);
9904 out1 = isl_basic_map_n_out(bmap1);
9905 out2 = isl_basic_map_n_out(bmap2);
9906 nparam = isl_basic_map_n_param(bmap1);
9907
9908 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
9909 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9910 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9911 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9912 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9913 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9914 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
9915 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
9916 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9917 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9918 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9919
9920 bmap = isl_basic_map_alloc_space(dim_result,
9921 bmap1->n_div + bmap2->n_div,
9922 bmap1->n_eq + bmap2->n_eq,
9923 bmap1->n_ineq + bmap2->n_ineq);
9924 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9925 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
Tobias Grosser5652c9c2016-10-01 19:46:51 +00009926 if (rational)
9927 bmap = isl_basic_map_set_rational(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +00009928 bmap = isl_basic_map_simplify(bmap);
9929 return isl_basic_map_finalize(bmap);
9930error:
9931 isl_basic_map_free(bmap1);
9932 isl_basic_map_free(bmap2);
9933 return NULL;
9934}
9935
9936__isl_give isl_basic_map *isl_basic_map_flat_range_product(
9937 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9938{
9939 isl_basic_map *prod;
9940
9941 prod = isl_basic_map_range_product(bmap1, bmap2);
9942 prod = isl_basic_map_flatten_range(prod);
9943 return prod;
9944}
9945
9946/* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
9947 * and collect the results.
9948 * The result live in the space obtained by calling "space_product"
9949 * on the spaces of "map1" and "map2".
9950 * If "remove_duplicates" is set then the result may contain duplicates
9951 * (even if the inputs do not) and so we try and remove the obvious
9952 * duplicates.
9953 */
9954static __isl_give isl_map *map_product(__isl_take isl_map *map1,
9955 __isl_take isl_map *map2,
9956 __isl_give isl_space *(*space_product)(__isl_take isl_space *left,
9957 __isl_take isl_space *right),
9958 __isl_give isl_basic_map *(*basic_map_product)(
9959 __isl_take isl_basic_map *left,
9960 __isl_take isl_basic_map *right),
9961 int remove_duplicates)
9962{
9963 unsigned flags = 0;
9964 struct isl_map *result;
9965 int i, j;
9966
9967 if (!map1 || !map2)
9968 goto error;
9969
9970 isl_assert(map1->ctx, isl_space_match(map1->dim, isl_dim_param,
9971 map2->dim, isl_dim_param), goto error);
9972
9973 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
9974 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
9975 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
9976
9977 result = isl_map_alloc_space(space_product(isl_space_copy(map1->dim),
9978 isl_space_copy(map2->dim)),
9979 map1->n * map2->n, flags);
9980 if (!result)
9981 goto error;
9982 for (i = 0; i < map1->n; ++i)
9983 for (j = 0; j < map2->n; ++j) {
9984 struct isl_basic_map *part;
9985 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
9986 isl_basic_map_copy(map2->p[j]));
9987 if (isl_basic_map_is_empty(part))
9988 isl_basic_map_free(part);
9989 else
9990 result = isl_map_add_basic_map(result, part);
9991 if (!result)
9992 goto error;
9993 }
9994 if (remove_duplicates)
9995 result = isl_map_remove_obvious_duplicates(result);
9996 isl_map_free(map1);
9997 isl_map_free(map2);
9998 return result;
9999error:
10000 isl_map_free(map1);
10001 isl_map_free(map2);
10002 return NULL;
10003}
10004
10005/* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
10006 */
10007static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
10008 __isl_take isl_map *map2)
10009{
10010 return map_product(map1, map2, &isl_space_product,
10011 &isl_basic_map_product, 0);
10012}
10013
10014__isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
10015 __isl_take isl_map *map2)
10016{
10017 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
10018}
10019
10020/* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
10021 */
10022__isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
10023 __isl_take isl_map *map2)
10024{
10025 isl_map *prod;
10026
10027 prod = isl_map_product(map1, map2);
10028 prod = isl_map_flatten(prod);
10029 return prod;
10030}
10031
10032/* Given two set A and B, construct its Cartesian product A x B.
10033 */
10034struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
10035{
10036 return isl_map_range_product(set1, set2);
10037}
10038
10039__isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
10040 __isl_take isl_set *set2)
10041{
10042 return isl_map_flat_range_product(set1, set2);
10043}
10044
10045/* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
10046 */
10047static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
10048 __isl_take isl_map *map2)
10049{
10050 return map_product(map1, map2, &isl_space_domain_product,
10051 &isl_basic_map_domain_product, 1);
10052}
10053
10054/* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
10055 */
10056static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
10057 __isl_take isl_map *map2)
10058{
10059 return map_product(map1, map2, &isl_space_range_product,
10060 &isl_basic_map_range_product, 1);
10061}
10062
10063__isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
10064 __isl_take isl_map *map2)
10065{
10066 return isl_map_align_params_map_map_and(map1, map2,
10067 &map_domain_product_aligned);
10068}
10069
10070__isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
10071 __isl_take isl_map *map2)
10072{
10073 return isl_map_align_params_map_map_and(map1, map2,
10074 &map_range_product_aligned);
10075}
10076
10077/* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
10078 */
10079__isl_give isl_map *isl_map_factor_domain(__isl_take isl_map *map)
10080{
10081 isl_space *space;
10082 int total1, keep1, total2, keep2;
10083
10084 if (!map)
10085 return NULL;
10086 if (!isl_space_domain_is_wrapping(map->dim) ||
10087 !isl_space_range_is_wrapping(map->dim))
10088 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10089 "not a product", return isl_map_free(map));
10090
10091 space = isl_map_get_space(map);
10092 total1 = isl_space_dim(space, isl_dim_in);
10093 total2 = isl_space_dim(space, isl_dim_out);
10094 space = isl_space_factor_domain(space);
10095 keep1 = isl_space_dim(space, isl_dim_in);
10096 keep2 = isl_space_dim(space, isl_dim_out);
10097 map = isl_map_project_out(map, isl_dim_in, keep1, total1 - keep1);
10098 map = isl_map_project_out(map, isl_dim_out, keep2, total2 - keep2);
10099 map = isl_map_reset_space(map, space);
10100
10101 return map;
10102}
10103
10104/* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
10105 */
10106__isl_give isl_map *isl_map_factor_range(__isl_take isl_map *map)
10107{
10108 isl_space *space;
10109 int total1, keep1, total2, keep2;
10110
10111 if (!map)
10112 return NULL;
10113 if (!isl_space_domain_is_wrapping(map->dim) ||
10114 !isl_space_range_is_wrapping(map->dim))
10115 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10116 "not a product", return isl_map_free(map));
10117
10118 space = isl_map_get_space(map);
10119 total1 = isl_space_dim(space, isl_dim_in);
10120 total2 = isl_space_dim(space, isl_dim_out);
10121 space = isl_space_factor_range(space);
10122 keep1 = isl_space_dim(space, isl_dim_in);
10123 keep2 = isl_space_dim(space, isl_dim_out);
10124 map = isl_map_project_out(map, isl_dim_in, 0, total1 - keep1);
10125 map = isl_map_project_out(map, isl_dim_out, 0, total2 - keep2);
10126 map = isl_map_reset_space(map, space);
10127
10128 return map;
10129}
10130
10131/* Given a map of the form [A -> B] -> C, return the map A -> C.
10132 */
10133__isl_give isl_map *isl_map_domain_factor_domain(__isl_take isl_map *map)
10134{
10135 isl_space *space;
10136 int total, keep;
10137
10138 if (!map)
10139 return NULL;
10140 if (!isl_space_domain_is_wrapping(map->dim))
10141 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10142 "domain is not a product", return isl_map_free(map));
10143
10144 space = isl_map_get_space(map);
10145 total = isl_space_dim(space, isl_dim_in);
10146 space = isl_space_domain_factor_domain(space);
10147 keep = isl_space_dim(space, isl_dim_in);
10148 map = isl_map_project_out(map, isl_dim_in, keep, total - keep);
10149 map = isl_map_reset_space(map, space);
10150
10151 return map;
10152}
10153
10154/* Given a map of the form [A -> B] -> C, return the map B -> C.
10155 */
10156__isl_give isl_map *isl_map_domain_factor_range(__isl_take isl_map *map)
10157{
10158 isl_space *space;
10159 int total, keep;
10160
10161 if (!map)
10162 return NULL;
10163 if (!isl_space_domain_is_wrapping(map->dim))
10164 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10165 "domain is not a product", return isl_map_free(map));
10166
10167 space = isl_map_get_space(map);
10168 total = isl_space_dim(space, isl_dim_in);
10169 space = isl_space_domain_factor_range(space);
10170 keep = isl_space_dim(space, isl_dim_in);
10171 map = isl_map_project_out(map, isl_dim_in, 0, total - keep);
10172 map = isl_map_reset_space(map, space);
10173
10174 return map;
10175}
10176
10177/* Given a map A -> [B -> C], extract the map A -> B.
10178 */
10179__isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
10180{
10181 isl_space *space;
10182 int total, keep;
10183
10184 if (!map)
10185 return NULL;
10186 if (!isl_space_range_is_wrapping(map->dim))
10187 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10188 "range is not a product", return isl_map_free(map));
10189
10190 space = isl_map_get_space(map);
10191 total = isl_space_dim(space, isl_dim_out);
10192 space = isl_space_range_factor_domain(space);
10193 keep = isl_space_dim(space, isl_dim_out);
10194 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
10195 map = isl_map_reset_space(map, space);
10196
10197 return map;
10198}
10199
10200/* Given a map A -> [B -> C], extract the map A -> C.
10201 */
10202__isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
10203{
10204 isl_space *space;
10205 int total, keep;
10206
10207 if (!map)
10208 return NULL;
10209 if (!isl_space_range_is_wrapping(map->dim))
10210 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10211 "range is not a product", return isl_map_free(map));
10212
10213 space = isl_map_get_space(map);
10214 total = isl_space_dim(space, isl_dim_out);
10215 space = isl_space_range_factor_range(space);
10216 keep = isl_space_dim(space, isl_dim_out);
10217 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
10218 map = isl_map_reset_space(map, space);
10219
10220 return map;
10221}
10222
10223/* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
10224 */
10225__isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
10226 __isl_take isl_map *map2)
10227{
10228 isl_map *prod;
10229
10230 prod = isl_map_domain_product(map1, map2);
10231 prod = isl_map_flatten_domain(prod);
10232 return prod;
10233}
10234
10235/* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
10236 */
10237__isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
10238 __isl_take isl_map *map2)
10239{
10240 isl_map *prod;
10241
10242 prod = isl_map_range_product(map1, map2);
10243 prod = isl_map_flatten_range(prod);
10244 return prod;
10245}
10246
10247uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
10248{
10249 int i;
10250 uint32_t hash = isl_hash_init();
10251 unsigned total;
10252
10253 if (!bmap)
10254 return 0;
10255 bmap = isl_basic_map_copy(bmap);
10256 bmap = isl_basic_map_normalize(bmap);
10257 if (!bmap)
10258 return 0;
10259 total = isl_basic_map_total_dim(bmap);
10260 isl_hash_byte(hash, bmap->n_eq & 0xFF);
10261 for (i = 0; i < bmap->n_eq; ++i) {
10262 uint32_t c_hash;
10263 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
10264 isl_hash_hash(hash, c_hash);
10265 }
10266 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
10267 for (i = 0; i < bmap->n_ineq; ++i) {
10268 uint32_t c_hash;
10269 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
10270 isl_hash_hash(hash, c_hash);
10271 }
10272 isl_hash_byte(hash, bmap->n_div & 0xFF);
10273 for (i = 0; i < bmap->n_div; ++i) {
10274 uint32_t c_hash;
10275 if (isl_int_is_zero(bmap->div[i][0]))
10276 continue;
10277 isl_hash_byte(hash, i & 0xFF);
10278 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
10279 isl_hash_hash(hash, c_hash);
10280 }
10281 isl_basic_map_free(bmap);
10282 return hash;
10283}
10284
10285uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
10286{
Tobias Grosser06e15922016-11-16 11:06:47 +000010287 return isl_basic_map_get_hash(bset_to_bmap(bset));
Tobias Grosser52a25232015-02-04 20:55:43 +000010288}
10289
10290uint32_t isl_map_get_hash(__isl_keep isl_map *map)
10291{
10292 int i;
10293 uint32_t hash;
10294
10295 if (!map)
10296 return 0;
10297 map = isl_map_copy(map);
10298 map = isl_map_normalize(map);
10299 if (!map)
10300 return 0;
10301
10302 hash = isl_hash_init();
10303 for (i = 0; i < map->n; ++i) {
10304 uint32_t bmap_hash;
10305 bmap_hash = isl_basic_map_get_hash(map->p[i]);
10306 isl_hash_hash(hash, bmap_hash);
10307 }
10308
10309 isl_map_free(map);
10310
10311 return hash;
10312}
10313
10314uint32_t isl_set_get_hash(__isl_keep isl_set *set)
10315{
Tobias Grosser06e15922016-11-16 11:06:47 +000010316 return isl_map_get_hash(set_to_map(set));
Tobias Grosser52a25232015-02-04 20:55:43 +000010317}
10318
10319/* Check if the value for dimension dim is completely determined
10320 * by the values of the other parameters and variables.
10321 * That is, check if dimension dim is involved in an equality.
10322 */
10323int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
10324{
10325 int i;
10326 unsigned nparam;
10327
10328 if (!bset)
10329 return -1;
10330 nparam = isl_basic_set_n_param(bset);
10331 for (i = 0; i < bset->n_eq; ++i)
10332 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
10333 return 1;
10334 return 0;
10335}
10336
10337/* Check if the value for dimension dim is completely determined
10338 * by the values of the other parameters and variables.
10339 * That is, check if dimension dim is involved in an equality
10340 * for each of the subsets.
10341 */
10342int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
10343{
10344 int i;
10345
10346 if (!set)
10347 return -1;
10348 for (i = 0; i < set->n; ++i) {
10349 int unique;
10350 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
10351 if (unique != 1)
10352 return unique;
10353 }
10354 return 1;
10355}
10356
10357/* Return the number of basic maps in the (current) representation of "map".
10358 */
10359int isl_map_n_basic_map(__isl_keep isl_map *map)
10360{
10361 return map ? map->n : 0;
10362}
10363
10364int isl_set_n_basic_set(__isl_keep isl_set *set)
10365{
10366 return set ? set->n : 0;
10367}
10368
Tobias Grosserb2f39922015-05-28 13:32:11 +000010369isl_stat isl_map_foreach_basic_map(__isl_keep isl_map *map,
10370 isl_stat (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
Tobias Grosser52a25232015-02-04 20:55:43 +000010371{
10372 int i;
10373
10374 if (!map)
Tobias Grosserb2f39922015-05-28 13:32:11 +000010375 return isl_stat_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000010376
10377 for (i = 0; i < map->n; ++i)
10378 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
Tobias Grosserb2f39922015-05-28 13:32:11 +000010379 return isl_stat_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000010380
Tobias Grosserb2f39922015-05-28 13:32:11 +000010381 return isl_stat_ok;
Tobias Grosser52a25232015-02-04 20:55:43 +000010382}
10383
Tobias Grosserb2f39922015-05-28 13:32:11 +000010384isl_stat isl_set_foreach_basic_set(__isl_keep isl_set *set,
10385 isl_stat (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
Tobias Grosser52a25232015-02-04 20:55:43 +000010386{
10387 int i;
10388
10389 if (!set)
Tobias Grosserb2f39922015-05-28 13:32:11 +000010390 return isl_stat_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000010391
10392 for (i = 0; i < set->n; ++i)
10393 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
Tobias Grosserb2f39922015-05-28 13:32:11 +000010394 return isl_stat_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000010395
Tobias Grosserb2f39922015-05-28 13:32:11 +000010396 return isl_stat_ok;
Tobias Grosser52a25232015-02-04 20:55:43 +000010397}
10398
Tobias Grosser4db55312015-06-30 08:22:14 +000010399/* Return a list of basic sets, the union of which is equal to "set".
10400 */
10401__isl_give isl_basic_set_list *isl_set_get_basic_set_list(
10402 __isl_keep isl_set *set)
10403{
10404 int i;
10405 isl_basic_set_list *list;
10406
10407 if (!set)
10408 return NULL;
10409
10410 list = isl_basic_set_list_alloc(isl_set_get_ctx(set), set->n);
10411 for (i = 0; i < set->n; ++i) {
10412 isl_basic_set *bset;
10413
10414 bset = isl_basic_set_copy(set->p[i]);
10415 list = isl_basic_set_list_add(list, bset);
10416 }
10417
10418 return list;
10419}
10420
Tobias Grosser52a25232015-02-04 20:55:43 +000010421__isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
10422{
10423 isl_space *dim;
10424
10425 if (!bset)
10426 return NULL;
10427
10428 bset = isl_basic_set_cow(bset);
10429 if (!bset)
10430 return NULL;
10431
10432 dim = isl_basic_set_get_space(bset);
10433 dim = isl_space_lift(dim, bset->n_div);
10434 if (!dim)
10435 goto error;
10436 isl_space_free(bset->dim);
10437 bset->dim = dim;
10438 bset->extra -= bset->n_div;
10439 bset->n_div = 0;
10440
10441 bset = isl_basic_set_finalize(bset);
10442
10443 return bset;
10444error:
10445 isl_basic_set_free(bset);
10446 return NULL;
10447}
10448
10449__isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
10450{
10451 int i;
10452 isl_space *dim;
10453 unsigned n_div;
10454
10455 set = isl_set_align_divs(set);
10456
10457 if (!set)
10458 return NULL;
10459
10460 set = isl_set_cow(set);
10461 if (!set)
10462 return NULL;
10463
10464 n_div = set->p[0]->n_div;
10465 dim = isl_set_get_space(set);
10466 dim = isl_space_lift(dim, n_div);
10467 if (!dim)
10468 goto error;
10469 isl_space_free(set->dim);
10470 set->dim = dim;
10471
10472 for (i = 0; i < set->n; ++i) {
10473 set->p[i] = isl_basic_set_lift(set->p[i]);
10474 if (!set->p[i])
10475 goto error;
10476 }
10477
10478 return set;
10479error:
10480 isl_set_free(set);
10481 return NULL;
10482}
10483
10484__isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
10485{
10486 isl_space *dim;
10487 struct isl_basic_map *bmap;
10488 unsigned n_set;
10489 unsigned n_div;
10490 unsigned n_param;
10491 unsigned total;
10492 int i, k, l;
10493
10494 set = isl_set_align_divs(set);
10495
10496 if (!set)
10497 return NULL;
10498
10499 dim = isl_set_get_space(set);
10500 if (set->n == 0 || set->p[0]->n_div == 0) {
10501 isl_set_free(set);
10502 return isl_map_identity(isl_space_map_from_set(dim));
10503 }
10504
10505 n_div = set->p[0]->n_div;
10506 dim = isl_space_map_from_set(dim);
10507 n_param = isl_space_dim(dim, isl_dim_param);
10508 n_set = isl_space_dim(dim, isl_dim_in);
10509 dim = isl_space_extend(dim, n_param, n_set, n_set + n_div);
10510 bmap = isl_basic_map_alloc_space(dim, 0, n_set, 2 * n_div);
10511 for (i = 0; i < n_set; ++i)
10512 bmap = var_equal(bmap, i);
10513
10514 total = n_param + n_set + n_set + n_div;
10515 for (i = 0; i < n_div; ++i) {
10516 k = isl_basic_map_alloc_inequality(bmap);
10517 if (k < 0)
10518 goto error;
10519 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
10520 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
10521 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
10522 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
10523 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
10524 set->p[0]->div[i][0]);
10525
10526 l = isl_basic_map_alloc_inequality(bmap);
10527 if (l < 0)
10528 goto error;
10529 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
10530 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
10531 set->p[0]->div[i][0]);
10532 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
10533 }
10534
10535 isl_set_free(set);
10536 bmap = isl_basic_map_simplify(bmap);
10537 bmap = isl_basic_map_finalize(bmap);
10538 return isl_map_from_basic_map(bmap);
10539error:
10540 isl_set_free(set);
10541 isl_basic_map_free(bmap);
10542 return NULL;
10543}
10544
10545int isl_basic_set_size(__isl_keep isl_basic_set *bset)
10546{
10547 unsigned dim;
10548 int size = 0;
10549
10550 if (!bset)
10551 return -1;
10552
10553 dim = isl_basic_set_total_dim(bset);
10554 size += bset->n_eq * (1 + dim);
10555 size += bset->n_ineq * (1 + dim);
10556 size += bset->n_div * (2 + dim);
10557
10558 return size;
10559}
10560
10561int isl_set_size(__isl_keep isl_set *set)
10562{
10563 int i;
10564 int size = 0;
10565
10566 if (!set)
10567 return -1;
10568
10569 for (i = 0; i < set->n; ++i)
10570 size += isl_basic_set_size(set->p[i]);
10571
10572 return size;
10573}
10574
10575/* Check if there is any lower bound (if lower == 0) and/or upper
10576 * bound (if upper == 0) on the specified dim.
10577 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000010578static isl_bool basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
Tobias Grosser52a25232015-02-04 20:55:43 +000010579 enum isl_dim_type type, unsigned pos, int lower, int upper)
10580{
10581 int i;
10582
Tobias Grosser94e53712016-12-31 07:46:11 +000010583 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
Tobias Grosserb2f39922015-05-28 13:32:11 +000010584 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000010585
Tobias Grosser52a25232015-02-04 20:55:43 +000010586 pos += isl_basic_map_offset(bmap, type);
10587
10588 for (i = 0; i < bmap->n_div; ++i) {
10589 if (isl_int_is_zero(bmap->div[i][0]))
10590 continue;
10591 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
Tobias Grosserb2f39922015-05-28 13:32:11 +000010592 return isl_bool_true;
Tobias Grosser52a25232015-02-04 20:55:43 +000010593 }
10594
10595 for (i = 0; i < bmap->n_eq; ++i)
10596 if (!isl_int_is_zero(bmap->eq[i][pos]))
Tobias Grosserb2f39922015-05-28 13:32:11 +000010597 return isl_bool_true;
Tobias Grosser52a25232015-02-04 20:55:43 +000010598
10599 for (i = 0; i < bmap->n_ineq; ++i) {
10600 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
10601 if (sgn > 0)
10602 lower = 1;
10603 if (sgn < 0)
10604 upper = 1;
10605 }
10606
10607 return lower && upper;
10608}
10609
10610int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10611 enum isl_dim_type type, unsigned pos)
10612{
10613 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
10614}
10615
Tobias Grosserb2f39922015-05-28 13:32:11 +000010616isl_bool isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
Tobias Grosser52a25232015-02-04 20:55:43 +000010617 enum isl_dim_type type, unsigned pos)
10618{
10619 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
10620}
10621
Tobias Grosserb2f39922015-05-28 13:32:11 +000010622isl_bool isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
Tobias Grosser52a25232015-02-04 20:55:43 +000010623 enum isl_dim_type type, unsigned pos)
10624{
10625 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
10626}
10627
10628int isl_map_dim_is_bounded(__isl_keep isl_map *map,
10629 enum isl_dim_type type, unsigned pos)
10630{
10631 int i;
10632
10633 if (!map)
10634 return -1;
10635
10636 for (i = 0; i < map->n; ++i) {
10637 int bounded;
10638 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
10639 if (bounded < 0 || !bounded)
10640 return bounded;
10641 }
10642
10643 return 1;
10644}
10645
10646/* Return 1 if the specified dim is involved in both an upper bound
10647 * and a lower bound.
10648 */
10649int isl_set_dim_is_bounded(__isl_keep isl_set *set,
10650 enum isl_dim_type type, unsigned pos)
10651{
Tobias Grosser06e15922016-11-16 11:06:47 +000010652 return isl_map_dim_is_bounded(set_to_map(set), type, pos);
Tobias Grosser52a25232015-02-04 20:55:43 +000010653}
10654
10655/* Does "map" have a bound (according to "fn") for any of its basic maps?
10656 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000010657static isl_bool has_any_bound(__isl_keep isl_map *map,
Tobias Grosser52a25232015-02-04 20:55:43 +000010658 enum isl_dim_type type, unsigned pos,
Tobias Grosserb2f39922015-05-28 13:32:11 +000010659 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
Tobias Grosser52a25232015-02-04 20:55:43 +000010660 enum isl_dim_type type, unsigned pos))
10661{
10662 int i;
10663
10664 if (!map)
Tobias Grosserb2f39922015-05-28 13:32:11 +000010665 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000010666
10667 for (i = 0; i < map->n; ++i) {
Tobias Grosserb2f39922015-05-28 13:32:11 +000010668 isl_bool bounded;
Tobias Grosser52a25232015-02-04 20:55:43 +000010669 bounded = fn(map->p[i], type, pos);
10670 if (bounded < 0 || bounded)
10671 return bounded;
10672 }
10673
Tobias Grosserb2f39922015-05-28 13:32:11 +000010674 return isl_bool_false;
Tobias Grosser52a25232015-02-04 20:55:43 +000010675}
10676
10677/* Return 1 if the specified dim is involved in any lower bound.
10678 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000010679isl_bool isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
Tobias Grosser52a25232015-02-04 20:55:43 +000010680 enum isl_dim_type type, unsigned pos)
10681{
10682 return has_any_bound(set, type, pos,
10683 &isl_basic_map_dim_has_lower_bound);
10684}
10685
10686/* Return 1 if the specified dim is involved in any upper bound.
10687 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000010688isl_bool isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
Tobias Grosser52a25232015-02-04 20:55:43 +000010689 enum isl_dim_type type, unsigned pos)
10690{
10691 return has_any_bound(set, type, pos,
10692 &isl_basic_map_dim_has_upper_bound);
10693}
10694
10695/* Does "map" have a bound (according to "fn") for all of its basic maps?
10696 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000010697static isl_bool has_bound(__isl_keep isl_map *map,
Tobias Grosser52a25232015-02-04 20:55:43 +000010698 enum isl_dim_type type, unsigned pos,
Tobias Grosserb2f39922015-05-28 13:32:11 +000010699 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
Tobias Grosser52a25232015-02-04 20:55:43 +000010700 enum isl_dim_type type, unsigned pos))
10701{
10702 int i;
10703
10704 if (!map)
Tobias Grosserb2f39922015-05-28 13:32:11 +000010705 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000010706
10707 for (i = 0; i < map->n; ++i) {
Tobias Grosserb2f39922015-05-28 13:32:11 +000010708 isl_bool bounded;
Tobias Grosser52a25232015-02-04 20:55:43 +000010709 bounded = fn(map->p[i], type, pos);
10710 if (bounded < 0 || !bounded)
10711 return bounded;
10712 }
10713
Tobias Grosserb2f39922015-05-28 13:32:11 +000010714 return isl_bool_true;
Tobias Grosser52a25232015-02-04 20:55:43 +000010715}
10716
10717/* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10718 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000010719isl_bool isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
Tobias Grosser52a25232015-02-04 20:55:43 +000010720 enum isl_dim_type type, unsigned pos)
10721{
10722 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
10723}
10724
10725/* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10726 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000010727isl_bool isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
Tobias Grosser52a25232015-02-04 20:55:43 +000010728 enum isl_dim_type type, unsigned pos)
10729{
10730 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
10731}
10732
10733/* For each of the "n" variables starting at "first", determine
10734 * the sign of the variable and put the results in the first "n"
10735 * elements of the array "signs".
10736 * Sign
10737 * 1 means that the variable is non-negative
10738 * -1 means that the variable is non-positive
10739 * 0 means the variable attains both positive and negative values.
10740 */
10741int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
10742 unsigned first, unsigned n, int *signs)
10743{
10744 isl_vec *bound = NULL;
10745 struct isl_tab *tab = NULL;
10746 struct isl_tab_undo *snap;
10747 int i;
10748
10749 if (!bset || !signs)
10750 return -1;
10751
10752 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
10753 tab = isl_tab_from_basic_set(bset, 0);
10754 if (!bound || !tab)
10755 goto error;
10756
10757 isl_seq_clr(bound->el, bound->size);
10758 isl_int_set_si(bound->el[0], -1);
10759
10760 snap = isl_tab_snap(tab);
10761 for (i = 0; i < n; ++i) {
10762 int empty;
10763
10764 isl_int_set_si(bound->el[1 + first + i], -1);
10765 if (isl_tab_add_ineq(tab, bound->el) < 0)
10766 goto error;
10767 empty = tab->empty;
10768 isl_int_set_si(bound->el[1 + first + i], 0);
10769 if (isl_tab_rollback(tab, snap) < 0)
10770 goto error;
10771
10772 if (empty) {
10773 signs[i] = 1;
10774 continue;
10775 }
10776
10777 isl_int_set_si(bound->el[1 + first + i], 1);
10778 if (isl_tab_add_ineq(tab, bound->el) < 0)
10779 goto error;
10780 empty = tab->empty;
10781 isl_int_set_si(bound->el[1 + first + i], 0);
10782 if (isl_tab_rollback(tab, snap) < 0)
10783 goto error;
10784
10785 signs[i] = empty ? -1 : 0;
10786 }
10787
10788 isl_tab_free(tab);
10789 isl_vec_free(bound);
10790 return 0;
10791error:
10792 isl_tab_free(tab);
10793 isl_vec_free(bound);
10794 return -1;
10795}
10796
10797int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
10798 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
10799{
10800 if (!bset || !signs)
10801 return -1;
10802 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
10803 return -1);
10804
10805 first += pos(bset->dim, type) - 1;
10806 return isl_basic_set_vars_get_sign(bset, first, n, signs);
10807}
10808
10809/* Is it possible for the integer division "div" to depend (possibly
10810 * indirectly) on any output dimensions?
10811 *
10812 * If the div is undefined, then we conservatively assume that it
10813 * may depend on them.
10814 * Otherwise, we check if it actually depends on them or on any integer
10815 * divisions that may depend on them.
10816 */
10817static int div_may_involve_output(__isl_keep isl_basic_map *bmap, int div)
10818{
10819 int i;
10820 unsigned n_out, o_out;
10821 unsigned n_div, o_div;
10822
10823 if (isl_int_is_zero(bmap->div[div][0]))
10824 return 1;
10825
10826 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10827 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10828
10829 if (isl_seq_first_non_zero(bmap->div[div] + 1 + o_out, n_out) != -1)
10830 return 1;
10831
10832 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10833 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10834
10835 for (i = 0; i < n_div; ++i) {
10836 if (isl_int_is_zero(bmap->div[div][1 + o_div + i]))
10837 continue;
10838 if (div_may_involve_output(bmap, i))
10839 return 1;
10840 }
10841
10842 return 0;
10843}
10844
Michael Kruse959a8dc2016-01-15 15:54:45 +000010845/* Return the first integer division of "bmap" in the range
10846 * [first, first + n[ that may depend on any output dimensions and
10847 * that has a non-zero coefficient in "c" (where the first coefficient
10848 * in "c" corresponds to integer division "first").
10849 */
10850static int first_div_may_involve_output(__isl_keep isl_basic_map *bmap,
10851 isl_int *c, int first, int n)
10852{
10853 int k;
10854
10855 if (!bmap)
10856 return -1;
10857
10858 for (k = first; k < first + n; ++k) {
10859 if (isl_int_is_zero(c[k]))
10860 continue;
10861 if (div_may_involve_output(bmap, k))
10862 return k;
10863 }
10864
10865 return first + n;
10866}
10867
10868/* Look for a pair of inequality constraints in "bmap" of the form
10869 *
10870 * -l + i >= 0 or i >= l
10871 * and
10872 * n + l - i >= 0 or i <= l + n
10873 *
10874 * with n < "m" and i the output dimension at position "pos".
10875 * (Note that n >= 0 as otherwise the two constraints would conflict.)
10876 * Furthermore, "l" is only allowed to involve parameters, input dimensions
10877 * and earlier output dimensions, as well as integer divisions that do
10878 * not involve any of the output dimensions.
10879 *
10880 * Return the index of the first inequality constraint or bmap->n_ineq
10881 * if no such pair can be found.
10882 */
10883static int find_modulo_constraint_pair(__isl_keep isl_basic_map *bmap,
10884 int pos, isl_int m)
10885{
10886 int i, j;
10887 isl_ctx *ctx;
10888 unsigned total;
10889 unsigned n_div, o_div;
10890 unsigned n_out, o_out;
10891 int less;
10892
10893 if (!bmap)
10894 return -1;
10895
10896 ctx = isl_basic_map_get_ctx(bmap);
10897 total = isl_basic_map_total_dim(bmap);
10898 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10899 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10900 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10901 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10902 for (i = 0; i < bmap->n_ineq; ++i) {
10903 if (!isl_int_abs_eq(bmap->ineq[i][o_out + pos], ctx->one))
10904 continue;
10905 if (isl_seq_first_non_zero(bmap->ineq[i] + o_out + pos + 1,
10906 n_out - (pos + 1)) != -1)
10907 continue;
10908 if (first_div_may_involve_output(bmap, bmap->ineq[i] + o_div,
10909 0, n_div) < n_div)
10910 continue;
10911 for (j = i + 1; j < bmap->n_ineq; ++j) {
10912 if (!isl_int_abs_eq(bmap->ineq[j][o_out + pos],
10913 ctx->one))
10914 continue;
10915 if (!isl_seq_is_neg(bmap->ineq[i] + 1,
10916 bmap->ineq[j] + 1, total))
10917 continue;
10918 break;
10919 }
10920 if (j >= bmap->n_ineq)
10921 continue;
10922 isl_int_add(bmap->ineq[i][0],
10923 bmap->ineq[i][0], bmap->ineq[j][0]);
10924 less = isl_int_abs_lt(bmap->ineq[i][0], m);
10925 isl_int_sub(bmap->ineq[i][0],
10926 bmap->ineq[i][0], bmap->ineq[j][0]);
10927 if (!less)
10928 continue;
10929 if (isl_int_is_one(bmap->ineq[i][o_out + pos]))
10930 return i;
10931 else
10932 return j;
10933 }
10934
10935 return bmap->n_ineq;
10936}
10937
Tobias Grosser52a25232015-02-04 20:55:43 +000010938/* Return the index of the equality of "bmap" that defines
10939 * the output dimension "pos" in terms of earlier dimensions.
10940 * The equality may also involve integer divisions, as long
10941 * as those integer divisions are defined in terms of
10942 * parameters or input dimensions.
Michael Kruse959a8dc2016-01-15 15:54:45 +000010943 * In this case, *div is set to the number of integer divisions and
10944 * *ineq is set to the number of inequality constraints (provided
10945 * div and ineq are not NULL).
10946 *
10947 * The equality may also involve a single integer division involving
10948 * the output dimensions (typically only output dimension "pos") as
10949 * long as the coefficient of output dimension "pos" is 1 or -1 and
10950 * there is a pair of constraints i >= l and i <= l + n, with i referring
10951 * to output dimension "pos", l an expression involving only earlier
10952 * dimensions and n smaller than the coefficient of the integer division
10953 * in the equality. In this case, the output dimension can be defined
10954 * in terms of a modulo expression that does not involve the integer division.
10955 * *div is then set to this single integer division and
10956 * *ineq is set to the index of constraint i >= l.
10957 *
Tobias Grosser52a25232015-02-04 20:55:43 +000010958 * Return bmap->n_eq if there is no such equality.
10959 * Return -1 on error.
10960 */
10961int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map *bmap,
Michael Kruse959a8dc2016-01-15 15:54:45 +000010962 int pos, int *div, int *ineq)
Tobias Grosser52a25232015-02-04 20:55:43 +000010963{
Michael Kruse959a8dc2016-01-15 15:54:45 +000010964 int j, k, l;
Tobias Grosser52a25232015-02-04 20:55:43 +000010965 unsigned n_out, o_out;
10966 unsigned n_div, o_div;
10967
10968 if (!bmap)
10969 return -1;
10970
10971 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10972 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10973 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10974 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10975
Michael Kruse959a8dc2016-01-15 15:54:45 +000010976 if (ineq)
10977 *ineq = bmap->n_ineq;
10978 if (div)
10979 *div = n_div;
Tobias Grosser52a25232015-02-04 20:55:43 +000010980 for (j = 0; j < bmap->n_eq; ++j) {
10981 if (isl_int_is_zero(bmap->eq[j][o_out + pos]))
10982 continue;
10983 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + pos + 1,
10984 n_out - (pos + 1)) != -1)
10985 continue;
Michael Kruse959a8dc2016-01-15 15:54:45 +000010986 k = first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
10987 0, n_div);
Tobias Grosser52a25232015-02-04 20:55:43 +000010988 if (k >= n_div)
10989 return j;
Michael Kruse959a8dc2016-01-15 15:54:45 +000010990 if (!isl_int_is_one(bmap->eq[j][o_out + pos]) &&
10991 !isl_int_is_negone(bmap->eq[j][o_out + pos]))
10992 continue;
10993 if (first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
10994 k + 1, n_div - (k+1)) < n_div)
10995 continue;
10996 l = find_modulo_constraint_pair(bmap, pos,
10997 bmap->eq[j][o_div + k]);
10998 if (l < 0)
10999 return -1;
11000 if (l >= bmap->n_ineq)
11001 continue;
11002 if (div)
11003 *div = k;
11004 if (ineq)
11005 *ineq = l;
11006 return j;
Tobias Grosser52a25232015-02-04 20:55:43 +000011007 }
11008
11009 return bmap->n_eq;
11010}
11011
11012/* Check if the given basic map is obviously single-valued.
11013 * In particular, for each output dimension, check that there is
11014 * an equality that defines the output dimension in terms of
11015 * earlier dimensions.
11016 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000011017isl_bool isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
Tobias Grosser52a25232015-02-04 20:55:43 +000011018{
11019 int i;
11020 unsigned n_out;
11021
11022 if (!bmap)
Tobias Grosserb2f39922015-05-28 13:32:11 +000011023 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000011024
11025 n_out = isl_basic_map_dim(bmap, isl_dim_out);
11026
11027 for (i = 0; i < n_out; ++i) {
11028 int eq;
11029
Michael Kruse959a8dc2016-01-15 15:54:45 +000011030 eq = isl_basic_map_output_defining_equality(bmap, i,
11031 NULL, NULL);
Tobias Grosser52a25232015-02-04 20:55:43 +000011032 if (eq < 0)
Tobias Grosserb2f39922015-05-28 13:32:11 +000011033 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000011034 if (eq >= bmap->n_eq)
Tobias Grosserb2f39922015-05-28 13:32:11 +000011035 return isl_bool_false;
Tobias Grosser52a25232015-02-04 20:55:43 +000011036 }
11037
Tobias Grosserb2f39922015-05-28 13:32:11 +000011038 return isl_bool_true;
Tobias Grosser52a25232015-02-04 20:55:43 +000011039}
11040
11041/* Check if the given basic map is single-valued.
11042 * We simply compute
11043 *
11044 * M \circ M^-1
11045 *
11046 * and check if the result is a subset of the identity mapping.
11047 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000011048isl_bool isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
Tobias Grosser52a25232015-02-04 20:55:43 +000011049{
11050 isl_space *space;
11051 isl_basic_map *test;
11052 isl_basic_map *id;
Tobias Grosserb2f39922015-05-28 13:32:11 +000011053 isl_bool sv;
Tobias Grosser52a25232015-02-04 20:55:43 +000011054
11055 sv = isl_basic_map_plain_is_single_valued(bmap);
11056 if (sv < 0 || sv)
11057 return sv;
11058
11059 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
11060 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
11061
11062 space = isl_basic_map_get_space(bmap);
11063 space = isl_space_map_from_set(isl_space_range(space));
11064 id = isl_basic_map_identity(space);
11065
11066 sv = isl_basic_map_is_subset(test, id);
11067
11068 isl_basic_map_free(test);
11069 isl_basic_map_free(id);
11070
11071 return sv;
11072}
11073
11074/* Check if the given map is obviously single-valued.
11075 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000011076isl_bool isl_map_plain_is_single_valued(__isl_keep isl_map *map)
Tobias Grosser52a25232015-02-04 20:55:43 +000011077{
11078 if (!map)
Tobias Grosserb2f39922015-05-28 13:32:11 +000011079 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000011080 if (map->n == 0)
Tobias Grosserb2f39922015-05-28 13:32:11 +000011081 return isl_bool_true;
Tobias Grosser52a25232015-02-04 20:55:43 +000011082 if (map->n >= 2)
Tobias Grosserb2f39922015-05-28 13:32:11 +000011083 return isl_bool_false;
Tobias Grosser52a25232015-02-04 20:55:43 +000011084
11085 return isl_basic_map_plain_is_single_valued(map->p[0]);
11086}
11087
11088/* Check if the given map is single-valued.
11089 * We simply compute
11090 *
11091 * M \circ M^-1
11092 *
11093 * and check if the result is a subset of the identity mapping.
11094 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000011095isl_bool isl_map_is_single_valued(__isl_keep isl_map *map)
Tobias Grosser52a25232015-02-04 20:55:43 +000011096{
11097 isl_space *dim;
11098 isl_map *test;
11099 isl_map *id;
Tobias Grosserb2f39922015-05-28 13:32:11 +000011100 isl_bool sv;
Tobias Grosser52a25232015-02-04 20:55:43 +000011101
11102 sv = isl_map_plain_is_single_valued(map);
11103 if (sv < 0 || sv)
11104 return sv;
11105
11106 test = isl_map_reverse(isl_map_copy(map));
11107 test = isl_map_apply_range(test, isl_map_copy(map));
11108
11109 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
11110 id = isl_map_identity(dim);
11111
11112 sv = isl_map_is_subset(test, id);
11113
11114 isl_map_free(test);
11115 isl_map_free(id);
11116
11117 return sv;
11118}
11119
Tobias Grosserb2f39922015-05-28 13:32:11 +000011120isl_bool isl_map_is_injective(__isl_keep isl_map *map)
Tobias Grosser52a25232015-02-04 20:55:43 +000011121{
Tobias Grosserb2f39922015-05-28 13:32:11 +000011122 isl_bool in;
Tobias Grosser52a25232015-02-04 20:55:43 +000011123
11124 map = isl_map_copy(map);
11125 map = isl_map_reverse(map);
11126 in = isl_map_is_single_valued(map);
11127 isl_map_free(map);
11128
11129 return in;
11130}
11131
11132/* Check if the given map is obviously injective.
11133 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000011134isl_bool isl_map_plain_is_injective(__isl_keep isl_map *map)
Tobias Grosser52a25232015-02-04 20:55:43 +000011135{
Tobias Grosserb2f39922015-05-28 13:32:11 +000011136 isl_bool in;
Tobias Grosser52a25232015-02-04 20:55:43 +000011137
11138 map = isl_map_copy(map);
11139 map = isl_map_reverse(map);
11140 in = isl_map_plain_is_single_valued(map);
11141 isl_map_free(map);
11142
11143 return in;
11144}
11145
Tobias Grosserb2f39922015-05-28 13:32:11 +000011146isl_bool isl_map_is_bijective(__isl_keep isl_map *map)
Tobias Grosser52a25232015-02-04 20:55:43 +000011147{
Tobias Grosserb2f39922015-05-28 13:32:11 +000011148 isl_bool sv;
Tobias Grosser52a25232015-02-04 20:55:43 +000011149
11150 sv = isl_map_is_single_valued(map);
11151 if (sv < 0 || !sv)
11152 return sv;
11153
11154 return isl_map_is_injective(map);
11155}
11156
Tobias Grosserb2f39922015-05-28 13:32:11 +000011157isl_bool isl_set_is_singleton(__isl_keep isl_set *set)
Tobias Grosser52a25232015-02-04 20:55:43 +000011158{
Tobias Grosser06e15922016-11-16 11:06:47 +000011159 return isl_map_is_single_valued(set_to_map(set));
Tobias Grosser52a25232015-02-04 20:55:43 +000011160}
11161
Michael Krusee0b34f32016-05-04 14:41:36 +000011162/* Does "map" only map elements to themselves?
11163 *
11164 * If the domain and range spaces are different, then "map"
11165 * is considered not to be an identity relation, even if it is empty.
11166 * Otherwise, construct the maximal identity relation and
11167 * check whether "map" is a subset of this relation.
11168 */
11169isl_bool isl_map_is_identity(__isl_keep isl_map *map)
11170{
11171 isl_space *space;
11172 isl_map *id;
11173 isl_bool equal, is_identity;
11174
11175 space = isl_map_get_space(map);
11176 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
11177 isl_space_free(space);
11178 if (equal < 0 || !equal)
11179 return equal;
11180
11181 id = isl_map_identity(isl_map_get_space(map));
11182 is_identity = isl_map_is_subset(map, id);
11183 isl_map_free(id);
11184
11185 return is_identity;
11186}
11187
Tobias Grosser52a25232015-02-04 20:55:43 +000011188int isl_map_is_translation(__isl_keep isl_map *map)
11189{
11190 int ok;
11191 isl_set *delta;
11192
11193 delta = isl_map_deltas(isl_map_copy(map));
11194 ok = isl_set_is_singleton(delta);
11195 isl_set_free(delta);
11196
11197 return ok;
11198}
11199
11200static int unique(isl_int *p, unsigned pos, unsigned len)
11201{
11202 if (isl_seq_first_non_zero(p, pos) != -1)
11203 return 0;
11204 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
11205 return 0;
11206 return 1;
11207}
11208
11209int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
11210{
11211 int i, j;
11212 unsigned nvar;
11213 unsigned ovar;
11214
11215 if (!bset)
11216 return -1;
11217
11218 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
11219 return 0;
11220
11221 nvar = isl_basic_set_dim(bset, isl_dim_set);
11222 ovar = isl_space_offset(bset->dim, isl_dim_set);
11223 for (j = 0; j < nvar; ++j) {
11224 int lower = 0, upper = 0;
11225 for (i = 0; i < bset->n_eq; ++i) {
11226 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
11227 continue;
11228 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
11229 return 0;
11230 break;
11231 }
11232 if (i < bset->n_eq)
11233 continue;
11234 for (i = 0; i < bset->n_ineq; ++i) {
11235 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
11236 continue;
11237 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
11238 return 0;
11239 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
11240 lower = 1;
11241 else
11242 upper = 1;
11243 }
11244 if (!lower || !upper)
11245 return 0;
11246 }
11247
11248 return 1;
11249}
11250
11251int isl_set_is_box(__isl_keep isl_set *set)
11252{
11253 if (!set)
11254 return -1;
11255 if (set->n != 1)
11256 return 0;
11257
11258 return isl_basic_set_is_box(set->p[0]);
11259}
11260
Tobias Grosserb2f39922015-05-28 13:32:11 +000011261isl_bool isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
Tobias Grosser52a25232015-02-04 20:55:43 +000011262{
11263 if (!bset)
Tobias Grosserb2f39922015-05-28 13:32:11 +000011264 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000011265
11266 return isl_space_is_wrapping(bset->dim);
11267}
11268
Tobias Grosserb2f39922015-05-28 13:32:11 +000011269isl_bool isl_set_is_wrapping(__isl_keep isl_set *set)
Tobias Grosser52a25232015-02-04 20:55:43 +000011270{
11271 if (!set)
Tobias Grosserb2f39922015-05-28 13:32:11 +000011272 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000011273
11274 return isl_space_is_wrapping(set->dim);
11275}
11276
Michael Kruse959a8dc2016-01-15 15:54:45 +000011277/* Modify the space of "map" through a call to "change".
11278 * If "can_change" is set (not NULL), then first call it to check
11279 * if the modification is allowed, printing the error message "cannot_change"
11280 * if it is not.
11281 */
11282static __isl_give isl_map *isl_map_change_space(__isl_take isl_map *map,
11283 isl_bool (*can_change)(__isl_keep isl_map *map),
11284 const char *cannot_change,
11285 __isl_give isl_space *(*change)(__isl_take isl_space *space))
11286{
11287 isl_bool ok;
11288 isl_space *space;
11289
11290 if (!map)
11291 return NULL;
11292
11293 ok = can_change ? can_change(map) : isl_bool_true;
11294 if (ok < 0)
11295 return isl_map_free(map);
11296 if (!ok)
11297 isl_die(isl_map_get_ctx(map), isl_error_invalid, cannot_change,
11298 return isl_map_free(map));
11299
11300 space = change(isl_map_get_space(map));
11301 map = isl_map_reset_space(map, space);
11302
11303 return map;
11304}
11305
Tobias Grosser52a25232015-02-04 20:55:43 +000011306/* Is the domain of "map" a wrapped relation?
11307 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000011308isl_bool isl_map_domain_is_wrapping(__isl_keep isl_map *map)
Tobias Grosser52a25232015-02-04 20:55:43 +000011309{
11310 if (!map)
Tobias Grosserb2f39922015-05-28 13:32:11 +000011311 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000011312
11313 return isl_space_domain_is_wrapping(map->dim);
11314}
11315
11316/* Is the range of "map" a wrapped relation?
11317 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000011318isl_bool isl_map_range_is_wrapping(__isl_keep isl_map *map)
Tobias Grosser52a25232015-02-04 20:55:43 +000011319{
11320 if (!map)
Tobias Grosserb2f39922015-05-28 13:32:11 +000011321 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000011322
11323 return isl_space_range_is_wrapping(map->dim);
11324}
11325
11326__isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
11327{
11328 bmap = isl_basic_map_cow(bmap);
11329 if (!bmap)
11330 return NULL;
11331
11332 bmap->dim = isl_space_wrap(bmap->dim);
11333 if (!bmap->dim)
11334 goto error;
11335
11336 bmap = isl_basic_map_finalize(bmap);
11337
Tobias Grosser06e15922016-11-16 11:06:47 +000011338 return bset_from_bmap(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +000011339error:
11340 isl_basic_map_free(bmap);
11341 return NULL;
11342}
11343
Michael Kruse959a8dc2016-01-15 15:54:45 +000011344/* Given a map A -> B, return the set (A -> B).
11345 */
Tobias Grosser52a25232015-02-04 20:55:43 +000011346__isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
11347{
Michael Kruse959a8dc2016-01-15 15:54:45 +000011348 return isl_map_change_space(map, NULL, NULL, &isl_space_wrap);
Tobias Grosser52a25232015-02-04 20:55:43 +000011349}
11350
11351__isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
11352{
11353 bset = isl_basic_set_cow(bset);
11354 if (!bset)
11355 return NULL;
11356
11357 bset->dim = isl_space_unwrap(bset->dim);
11358 if (!bset->dim)
11359 goto error;
11360
11361 bset = isl_basic_set_finalize(bset);
11362
Tobias Grosser06e15922016-11-16 11:06:47 +000011363 return bset_to_bmap(bset);
Tobias Grosser52a25232015-02-04 20:55:43 +000011364error:
11365 isl_basic_set_free(bset);
11366 return NULL;
11367}
11368
Michael Kruse959a8dc2016-01-15 15:54:45 +000011369/* Given a set (A -> B), return the map A -> B.
11370 * Error out if "set" is not of the form (A -> B).
11371 */
Tobias Grosser52a25232015-02-04 20:55:43 +000011372__isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
11373{
Michael Kruse959a8dc2016-01-15 15:54:45 +000011374 return isl_map_change_space(set, &isl_set_is_wrapping,
11375 "not a wrapping set", &isl_space_unwrap);
Tobias Grosser52a25232015-02-04 20:55:43 +000011376}
11377
11378__isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
11379 enum isl_dim_type type)
11380{
11381 if (!bmap)
11382 return NULL;
11383
11384 if (!isl_space_is_named_or_nested(bmap->dim, type))
11385 return bmap;
11386
11387 bmap = isl_basic_map_cow(bmap);
11388 if (!bmap)
11389 return NULL;
11390
11391 bmap->dim = isl_space_reset(bmap->dim, type);
11392 if (!bmap->dim)
11393 goto error;
11394
11395 bmap = isl_basic_map_finalize(bmap);
11396
11397 return bmap;
11398error:
11399 isl_basic_map_free(bmap);
11400 return NULL;
11401}
11402
11403__isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
11404 enum isl_dim_type type)
11405{
11406 int i;
11407
11408 if (!map)
11409 return NULL;
11410
11411 if (!isl_space_is_named_or_nested(map->dim, type))
11412 return map;
11413
11414 map = isl_map_cow(map);
11415 if (!map)
11416 return NULL;
11417
11418 for (i = 0; i < map->n; ++i) {
11419 map->p[i] = isl_basic_map_reset(map->p[i], type);
11420 if (!map->p[i])
11421 goto error;
11422 }
11423 map->dim = isl_space_reset(map->dim, type);
11424 if (!map->dim)
11425 goto error;
11426
11427 return map;
11428error:
11429 isl_map_free(map);
11430 return NULL;
11431}
11432
11433__isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
11434{
11435 if (!bmap)
11436 return NULL;
11437
11438 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
11439 return bmap;
11440
11441 bmap = isl_basic_map_cow(bmap);
11442 if (!bmap)
11443 return NULL;
11444
11445 bmap->dim = isl_space_flatten(bmap->dim);
11446 if (!bmap->dim)
11447 goto error;
11448
11449 bmap = isl_basic_map_finalize(bmap);
11450
11451 return bmap;
11452error:
11453 isl_basic_map_free(bmap);
11454 return NULL;
11455}
11456
11457__isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
11458{
Tobias Grosser06e15922016-11-16 11:06:47 +000011459 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset)));
Tobias Grosser52a25232015-02-04 20:55:43 +000011460}
11461
11462__isl_give isl_basic_map *isl_basic_map_flatten_domain(
11463 __isl_take isl_basic_map *bmap)
11464{
11465 if (!bmap)
11466 return NULL;
11467
11468 if (!bmap->dim->nested[0])
11469 return bmap;
11470
11471 bmap = isl_basic_map_cow(bmap);
11472 if (!bmap)
11473 return NULL;
11474
11475 bmap->dim = isl_space_flatten_domain(bmap->dim);
11476 if (!bmap->dim)
11477 goto error;
11478
11479 bmap = isl_basic_map_finalize(bmap);
11480
11481 return bmap;
11482error:
11483 isl_basic_map_free(bmap);
11484 return NULL;
11485}
11486
11487__isl_give isl_basic_map *isl_basic_map_flatten_range(
11488 __isl_take isl_basic_map *bmap)
11489{
11490 if (!bmap)
11491 return NULL;
11492
11493 if (!bmap->dim->nested[1])
11494 return bmap;
11495
11496 bmap = isl_basic_map_cow(bmap);
11497 if (!bmap)
11498 return NULL;
11499
11500 bmap->dim = isl_space_flatten_range(bmap->dim);
11501 if (!bmap->dim)
11502 goto error;
11503
11504 bmap = isl_basic_map_finalize(bmap);
11505
11506 return bmap;
11507error:
11508 isl_basic_map_free(bmap);
11509 return NULL;
11510}
11511
Michael Kruse959a8dc2016-01-15 15:54:45 +000011512/* Remove any internal structure from the spaces of domain and range of "map".
11513 */
Tobias Grosser52a25232015-02-04 20:55:43 +000011514__isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
11515{
Tobias Grosser52a25232015-02-04 20:55:43 +000011516 if (!map)
11517 return NULL;
11518
11519 if (!map->dim->nested[0] && !map->dim->nested[1])
11520 return map;
11521
Michael Kruse959a8dc2016-01-15 15:54:45 +000011522 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten);
Tobias Grosser52a25232015-02-04 20:55:43 +000011523}
11524
11525__isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
11526{
Tobias Grosser06e15922016-11-16 11:06:47 +000011527 return set_from_map(isl_map_flatten(set_to_map(set)));
Tobias Grosser52a25232015-02-04 20:55:43 +000011528}
11529
11530__isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
11531{
11532 isl_space *dim, *flat_dim;
11533 isl_map *map;
11534
11535 dim = isl_set_get_space(set);
11536 flat_dim = isl_space_flatten(isl_space_copy(dim));
11537 map = isl_map_identity(isl_space_join(isl_space_reverse(dim), flat_dim));
11538 map = isl_map_intersect_domain(map, set);
11539
11540 return map;
11541}
11542
Michael Kruse959a8dc2016-01-15 15:54:45 +000011543/* Remove any internal structure from the space of the domain of "map".
11544 */
Tobias Grosser52a25232015-02-04 20:55:43 +000011545__isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
11546{
Tobias Grosser52a25232015-02-04 20:55:43 +000011547 if (!map)
11548 return NULL;
11549
11550 if (!map->dim->nested[0])
11551 return map;
11552
Michael Kruse959a8dc2016-01-15 15:54:45 +000011553 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_domain);
Tobias Grosser52a25232015-02-04 20:55:43 +000011554}
11555
Michael Kruse959a8dc2016-01-15 15:54:45 +000011556/* Remove any internal structure from the space of the range of "map".
11557 */
Tobias Grosser52a25232015-02-04 20:55:43 +000011558__isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
11559{
Tobias Grosser52a25232015-02-04 20:55:43 +000011560 if (!map)
11561 return NULL;
11562
11563 if (!map->dim->nested[1])
11564 return map;
11565
Michael Kruse959a8dc2016-01-15 15:54:45 +000011566 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_range);
Tobias Grosser52a25232015-02-04 20:55:43 +000011567}
11568
11569/* Reorder the dimensions of "bmap" according to the given dim_map
Tobias Grossera7f1e042016-02-04 06:19:12 +000011570 * and set the dimension specification to "dim" and
11571 * perform Gaussian elimination on the result.
Tobias Grosser52a25232015-02-04 20:55:43 +000011572 */
11573__isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
11574 __isl_take isl_space *dim, __isl_take struct isl_dim_map *dim_map)
11575{
11576 isl_basic_map *res;
11577 unsigned flags;
11578
11579 bmap = isl_basic_map_cow(bmap);
11580 if (!bmap || !dim || !dim_map)
11581 goto error;
11582
11583 flags = bmap->flags;
11584 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
11585 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED);
11586 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
11587 res = isl_basic_map_alloc_space(dim,
11588 bmap->n_div, bmap->n_eq, bmap->n_ineq);
11589 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
11590 if (res)
11591 res->flags = flags;
Tobias Grossera7f1e042016-02-04 06:19:12 +000011592 res = isl_basic_map_gauss(res, NULL);
Tobias Grosser52a25232015-02-04 20:55:43 +000011593 res = isl_basic_map_finalize(res);
11594 return res;
11595error:
11596 free(dim_map);
11597 isl_basic_map_free(bmap);
11598 isl_space_free(dim);
11599 return NULL;
11600}
11601
11602/* Reorder the dimensions of "map" according to given reordering.
11603 */
11604__isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
11605 __isl_take isl_reordering *r)
11606{
11607 int i;
11608 struct isl_dim_map *dim_map;
11609
11610 map = isl_map_cow(map);
11611 dim_map = isl_dim_map_from_reordering(r);
11612 if (!map || !r || !dim_map)
11613 goto error;
11614
11615 for (i = 0; i < map->n; ++i) {
11616 struct isl_dim_map *dim_map_i;
11617
11618 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
11619
11620 map->p[i] = isl_basic_map_realign(map->p[i],
11621 isl_space_copy(r->dim), dim_map_i);
11622
11623 if (!map->p[i])
11624 goto error;
11625 }
11626
11627 map = isl_map_reset_space(map, isl_space_copy(r->dim));
11628
11629 isl_reordering_free(r);
11630 free(dim_map);
11631 return map;
11632error:
11633 free(dim_map);
11634 isl_map_free(map);
11635 isl_reordering_free(r);
11636 return NULL;
11637}
11638
11639__isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
11640 __isl_take isl_reordering *r)
11641{
Tobias Grosser06e15922016-11-16 11:06:47 +000011642 return set_from_map(isl_map_realign(set_to_map(set), r));
Tobias Grosser52a25232015-02-04 20:55:43 +000011643}
11644
11645__isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
11646 __isl_take isl_space *model)
11647{
11648 isl_ctx *ctx;
11649
11650 if (!map || !model)
11651 goto error;
11652
11653 ctx = isl_space_get_ctx(model);
11654 if (!isl_space_has_named_params(model))
11655 isl_die(ctx, isl_error_invalid,
11656 "model has unnamed parameters", goto error);
11657 if (!isl_space_has_named_params(map->dim))
11658 isl_die(ctx, isl_error_invalid,
11659 "relation has unnamed parameters", goto error);
11660 if (!isl_space_match(map->dim, isl_dim_param, model, isl_dim_param)) {
11661 isl_reordering *exp;
11662
11663 model = isl_space_drop_dims(model, isl_dim_in,
11664 0, isl_space_dim(model, isl_dim_in));
11665 model = isl_space_drop_dims(model, isl_dim_out,
11666 0, isl_space_dim(model, isl_dim_out));
11667 exp = isl_parameter_alignment_reordering(map->dim, model);
11668 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
11669 map = isl_map_realign(map, exp);
11670 }
11671
11672 isl_space_free(model);
11673 return map;
11674error:
11675 isl_space_free(model);
11676 isl_map_free(map);
11677 return NULL;
11678}
11679
11680__isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
11681 __isl_take isl_space *model)
11682{
11683 return isl_map_align_params(set, model);
11684}
11685
11686/* Align the parameters of "bmap" to those of "model", introducing
11687 * additional parameters if needed.
11688 */
11689__isl_give isl_basic_map *isl_basic_map_align_params(
11690 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
11691{
11692 isl_ctx *ctx;
11693
11694 if (!bmap || !model)
11695 goto error;
11696
11697 ctx = isl_space_get_ctx(model);
11698 if (!isl_space_has_named_params(model))
11699 isl_die(ctx, isl_error_invalid,
11700 "model has unnamed parameters", goto error);
11701 if (!isl_space_has_named_params(bmap->dim))
11702 isl_die(ctx, isl_error_invalid,
11703 "relation has unnamed parameters", goto error);
11704 if (!isl_space_match(bmap->dim, isl_dim_param, model, isl_dim_param)) {
11705 isl_reordering *exp;
11706 struct isl_dim_map *dim_map;
11707
11708 model = isl_space_drop_dims(model, isl_dim_in,
11709 0, isl_space_dim(model, isl_dim_in));
11710 model = isl_space_drop_dims(model, isl_dim_out,
11711 0, isl_space_dim(model, isl_dim_out));
11712 exp = isl_parameter_alignment_reordering(bmap->dim, model);
11713 exp = isl_reordering_extend_space(exp,
11714 isl_basic_map_get_space(bmap));
11715 dim_map = isl_dim_map_from_reordering(exp);
11716 bmap = isl_basic_map_realign(bmap,
11717 exp ? isl_space_copy(exp->dim) : NULL,
11718 isl_dim_map_extend(dim_map, bmap));
11719 isl_reordering_free(exp);
11720 free(dim_map);
11721 }
11722
11723 isl_space_free(model);
11724 return bmap;
11725error:
11726 isl_space_free(model);
11727 isl_basic_map_free(bmap);
11728 return NULL;
11729}
11730
11731/* Align the parameters of "bset" to those of "model", introducing
11732 * additional parameters if needed.
11733 */
11734__isl_give isl_basic_set *isl_basic_set_align_params(
11735 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
11736{
11737 return isl_basic_map_align_params(bset, model);
11738}
11739
11740__isl_give isl_mat *isl_basic_map_equalities_matrix(
11741 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11742 enum isl_dim_type c2, enum isl_dim_type c3,
11743 enum isl_dim_type c4, enum isl_dim_type c5)
11744{
11745 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11746 struct isl_mat *mat;
11747 int i, j, k;
11748 int pos;
11749
11750 if (!bmap)
11751 return NULL;
11752 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
11753 isl_basic_map_total_dim(bmap) + 1);
11754 if (!mat)
11755 return NULL;
11756 for (i = 0; i < bmap->n_eq; ++i)
11757 for (j = 0, pos = 0; j < 5; ++j) {
11758 int off = isl_basic_map_offset(bmap, c[j]);
11759 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11760 isl_int_set(mat->row[i][pos],
11761 bmap->eq[i][off + k]);
11762 ++pos;
11763 }
11764 }
11765
11766 return mat;
11767}
11768
11769__isl_give isl_mat *isl_basic_map_inequalities_matrix(
11770 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11771 enum isl_dim_type c2, enum isl_dim_type c3,
11772 enum isl_dim_type c4, enum isl_dim_type c5)
11773{
11774 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11775 struct isl_mat *mat;
11776 int i, j, k;
11777 int pos;
11778
11779 if (!bmap)
11780 return NULL;
11781 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
11782 isl_basic_map_total_dim(bmap) + 1);
11783 if (!mat)
11784 return NULL;
11785 for (i = 0; i < bmap->n_ineq; ++i)
11786 for (j = 0, pos = 0; j < 5; ++j) {
11787 int off = isl_basic_map_offset(bmap, c[j]);
11788 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11789 isl_int_set(mat->row[i][pos],
11790 bmap->ineq[i][off + k]);
11791 ++pos;
11792 }
11793 }
11794
11795 return mat;
11796}
11797
11798__isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
11799 __isl_take isl_space *dim,
11800 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11801 enum isl_dim_type c2, enum isl_dim_type c3,
11802 enum isl_dim_type c4, enum isl_dim_type c5)
11803{
11804 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11805 isl_basic_map *bmap;
11806 unsigned total;
11807 unsigned extra;
11808 int i, j, k, l;
11809 int pos;
11810
11811 if (!dim || !eq || !ineq)
11812 goto error;
11813
11814 if (eq->n_col != ineq->n_col)
11815 isl_die(dim->ctx, isl_error_invalid,
11816 "equalities and inequalities matrices should have "
11817 "same number of columns", goto error);
11818
11819 total = 1 + isl_space_dim(dim, isl_dim_all);
11820
11821 if (eq->n_col < total)
11822 isl_die(dim->ctx, isl_error_invalid,
11823 "number of columns too small", goto error);
11824
11825 extra = eq->n_col - total;
11826
11827 bmap = isl_basic_map_alloc_space(isl_space_copy(dim), extra,
11828 eq->n_row, ineq->n_row);
11829 if (!bmap)
11830 goto error;
11831 for (i = 0; i < extra; ++i) {
11832 k = isl_basic_map_alloc_div(bmap);
11833 if (k < 0)
11834 goto error;
11835 isl_int_set_si(bmap->div[k][0], 0);
11836 }
11837 for (i = 0; i < eq->n_row; ++i) {
11838 l = isl_basic_map_alloc_equality(bmap);
11839 if (l < 0)
11840 goto error;
11841 for (j = 0, pos = 0; j < 5; ++j) {
11842 int off = isl_basic_map_offset(bmap, c[j]);
11843 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11844 isl_int_set(bmap->eq[l][off + k],
11845 eq->row[i][pos]);
11846 ++pos;
11847 }
11848 }
11849 }
11850 for (i = 0; i < ineq->n_row; ++i) {
11851 l = isl_basic_map_alloc_inequality(bmap);
11852 if (l < 0)
11853 goto error;
11854 for (j = 0, pos = 0; j < 5; ++j) {
11855 int off = isl_basic_map_offset(bmap, c[j]);
11856 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11857 isl_int_set(bmap->ineq[l][off + k],
11858 ineq->row[i][pos]);
11859 ++pos;
11860 }
11861 }
11862 }
11863
11864 isl_space_free(dim);
11865 isl_mat_free(eq);
11866 isl_mat_free(ineq);
11867
11868 bmap = isl_basic_map_simplify(bmap);
11869 return isl_basic_map_finalize(bmap);
11870error:
11871 isl_space_free(dim);
11872 isl_mat_free(eq);
11873 isl_mat_free(ineq);
11874 return NULL;
11875}
11876
11877__isl_give isl_mat *isl_basic_set_equalities_matrix(
11878 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11879 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11880{
Tobias Grosser06e15922016-11-16 11:06:47 +000011881 return isl_basic_map_equalities_matrix(bset_to_bmap(bset),
Tobias Grosser52a25232015-02-04 20:55:43 +000011882 c1, c2, c3, c4, isl_dim_in);
11883}
11884
11885__isl_give isl_mat *isl_basic_set_inequalities_matrix(
11886 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11887 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11888{
Tobias Grosser06e15922016-11-16 11:06:47 +000011889 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset),
Tobias Grosser52a25232015-02-04 20:55:43 +000011890 c1, c2, c3, c4, isl_dim_in);
11891}
11892
11893__isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
11894 __isl_take isl_space *dim,
11895 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11896 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11897{
Tobias Grosser06e15922016-11-16 11:06:47 +000011898 isl_basic_map *bmap;
11899 bmap = isl_basic_map_from_constraint_matrices(dim, eq, ineq,
Tobias Grosser52a25232015-02-04 20:55:43 +000011900 c1, c2, c3, c4, isl_dim_in);
Tobias Grosser06e15922016-11-16 11:06:47 +000011901 return bset_from_bmap(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +000011902}
11903
Tobias Grosserb2f39922015-05-28 13:32:11 +000011904isl_bool isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
Tobias Grosser52a25232015-02-04 20:55:43 +000011905{
11906 if (!bmap)
Tobias Grosserb2f39922015-05-28 13:32:11 +000011907 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000011908
11909 return isl_space_can_zip(bmap->dim);
11910}
11911
Tobias Grosserb2f39922015-05-28 13:32:11 +000011912isl_bool isl_map_can_zip(__isl_keep isl_map *map)
Tobias Grosser52a25232015-02-04 20:55:43 +000011913{
11914 if (!map)
Tobias Grosserb2f39922015-05-28 13:32:11 +000011915 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000011916
11917 return isl_space_can_zip(map->dim);
11918}
11919
11920/* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11921 * (A -> C) -> (B -> D).
11922 */
11923__isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
11924{
11925 unsigned pos;
11926 unsigned n1;
11927 unsigned n2;
11928
11929 if (!bmap)
11930 return NULL;
11931
11932 if (!isl_basic_map_can_zip(bmap))
11933 isl_die(bmap->ctx, isl_error_invalid,
11934 "basic map cannot be zipped", goto error);
11935 pos = isl_basic_map_offset(bmap, isl_dim_in) +
11936 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
11937 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
11938 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
11939 bmap = isl_basic_map_cow(bmap);
11940 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
11941 if (!bmap)
11942 return NULL;
11943 bmap->dim = isl_space_zip(bmap->dim);
11944 if (!bmap->dim)
11945 goto error;
Michael Kruse959a8dc2016-01-15 15:54:45 +000011946 bmap = isl_basic_map_mark_final(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +000011947 return bmap;
11948error:
11949 isl_basic_map_free(bmap);
11950 return NULL;
11951}
11952
11953/* Given a map (A -> B) -> (C -> D), return the corresponding map
11954 * (A -> C) -> (B -> D).
11955 */
11956__isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
11957{
11958 int i;
11959
11960 if (!map)
11961 return NULL;
11962
11963 if (!isl_map_can_zip(map))
11964 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
11965 goto error);
11966
11967 map = isl_map_cow(map);
11968 if (!map)
11969 return NULL;
11970
11971 for (i = 0; i < map->n; ++i) {
11972 map->p[i] = isl_basic_map_zip(map->p[i]);
11973 if (!map->p[i])
11974 goto error;
11975 }
11976
11977 map->dim = isl_space_zip(map->dim);
11978 if (!map->dim)
11979 goto error;
11980
11981 return map;
11982error:
11983 isl_map_free(map);
11984 return NULL;
11985}
11986
11987/* Can we apply isl_basic_map_curry to "bmap"?
11988 * That is, does it have a nested relation in its domain?
11989 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000011990isl_bool isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
Tobias Grosser52a25232015-02-04 20:55:43 +000011991{
11992 if (!bmap)
Tobias Grosserb2f39922015-05-28 13:32:11 +000011993 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000011994
11995 return isl_space_can_curry(bmap->dim);
11996}
11997
11998/* Can we apply isl_map_curry to "map"?
11999 * That is, does it have a nested relation in its domain?
12000 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000012001isl_bool isl_map_can_curry(__isl_keep isl_map *map)
Tobias Grosser52a25232015-02-04 20:55:43 +000012002{
12003 if (!map)
Tobias Grosserb2f39922015-05-28 13:32:11 +000012004 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000012005
12006 return isl_space_can_curry(map->dim);
12007}
12008
12009/* Given a basic map (A -> B) -> C, return the corresponding basic map
12010 * A -> (B -> C).
12011 */
12012__isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
12013{
12014
12015 if (!bmap)
12016 return NULL;
12017
12018 if (!isl_basic_map_can_curry(bmap))
12019 isl_die(bmap->ctx, isl_error_invalid,
12020 "basic map cannot be curried", goto error);
12021 bmap = isl_basic_map_cow(bmap);
12022 if (!bmap)
12023 return NULL;
12024 bmap->dim = isl_space_curry(bmap->dim);
12025 if (!bmap->dim)
12026 goto error;
Michael Kruse959a8dc2016-01-15 15:54:45 +000012027 bmap = isl_basic_map_mark_final(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +000012028 return bmap;
12029error:
12030 isl_basic_map_free(bmap);
12031 return NULL;
12032}
12033
12034/* Given a map (A -> B) -> C, return the corresponding map
12035 * A -> (B -> C).
12036 */
12037__isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
12038{
Michael Kruse959a8dc2016-01-15 15:54:45 +000012039 return isl_map_change_space(map, &isl_map_can_curry,
12040 "map cannot be curried", &isl_space_curry);
12041}
Tobias Grosser52a25232015-02-04 20:55:43 +000012042
Michael Kruse959a8dc2016-01-15 15:54:45 +000012043/* Can isl_map_range_curry be applied to "map"?
12044 * That is, does it have a nested relation in its range,
12045 * the domain of which is itself a nested relation?
12046 */
12047isl_bool isl_map_can_range_curry(__isl_keep isl_map *map)
12048{
Tobias Grosser52a25232015-02-04 20:55:43 +000012049 if (!map)
Michael Kruse959a8dc2016-01-15 15:54:45 +000012050 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000012051
Michael Kruse959a8dc2016-01-15 15:54:45 +000012052 return isl_space_can_range_curry(map->dim);
12053}
Tobias Grosser52a25232015-02-04 20:55:43 +000012054
Michael Kruse959a8dc2016-01-15 15:54:45 +000012055/* Given a map A -> ((B -> C) -> D), return the corresponding map
12056 * A -> (B -> (C -> D)).
12057 */
12058__isl_give isl_map *isl_map_range_curry(__isl_take isl_map *map)
12059{
12060 return isl_map_change_space(map, &isl_map_can_range_curry,
12061 "map range cannot be curried",
12062 &isl_space_range_curry);
Tobias Grosser52a25232015-02-04 20:55:43 +000012063}
12064
12065/* Can we apply isl_basic_map_uncurry to "bmap"?
12066 * That is, does it have a nested relation in its domain?
12067 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000012068isl_bool isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
Tobias Grosser52a25232015-02-04 20:55:43 +000012069{
12070 if (!bmap)
Tobias Grosserb2f39922015-05-28 13:32:11 +000012071 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000012072
12073 return isl_space_can_uncurry(bmap->dim);
12074}
12075
12076/* Can we apply isl_map_uncurry to "map"?
12077 * That is, does it have a nested relation in its domain?
12078 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000012079isl_bool isl_map_can_uncurry(__isl_keep isl_map *map)
Tobias Grosser52a25232015-02-04 20:55:43 +000012080{
12081 if (!map)
Tobias Grosserb2f39922015-05-28 13:32:11 +000012082 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000012083
12084 return isl_space_can_uncurry(map->dim);
12085}
12086
12087/* Given a basic map A -> (B -> C), return the corresponding basic map
12088 * (A -> B) -> C.
12089 */
12090__isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
12091{
12092
12093 if (!bmap)
12094 return NULL;
12095
12096 if (!isl_basic_map_can_uncurry(bmap))
12097 isl_die(bmap->ctx, isl_error_invalid,
12098 "basic map cannot be uncurried",
12099 return isl_basic_map_free(bmap));
12100 bmap = isl_basic_map_cow(bmap);
12101 if (!bmap)
12102 return NULL;
12103 bmap->dim = isl_space_uncurry(bmap->dim);
12104 if (!bmap->dim)
12105 return isl_basic_map_free(bmap);
Michael Kruse959a8dc2016-01-15 15:54:45 +000012106 bmap = isl_basic_map_mark_final(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +000012107 return bmap;
12108}
12109
12110/* Given a map A -> (B -> C), return the corresponding map
12111 * (A -> B) -> C.
12112 */
12113__isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
12114{
Michael Kruse959a8dc2016-01-15 15:54:45 +000012115 return isl_map_change_space(map, &isl_map_can_uncurry,
12116 "map cannot be uncurried", &isl_space_uncurry);
Tobias Grosser52a25232015-02-04 20:55:43 +000012117}
12118
12119/* Construct a basic map mapping the domain of the affine expression
12120 * to a one-dimensional range prescribed by the affine expression.
Tobias Grosser5652c9c2016-10-01 19:46:51 +000012121 * If "rational" is set, then construct a rational basic map.
Tobias Grosser37034db2016-03-25 19:38:18 +000012122 *
12123 * A NaN affine expression cannot be converted to a basic map.
Tobias Grosser52a25232015-02-04 20:55:43 +000012124 */
Tobias Grosser5652c9c2016-10-01 19:46:51 +000012125static __isl_give isl_basic_map *isl_basic_map_from_aff2(
12126 __isl_take isl_aff *aff, int rational)
Tobias Grosser52a25232015-02-04 20:55:43 +000012127{
12128 int k;
12129 int pos;
Tobias Grosser37034db2016-03-25 19:38:18 +000012130 isl_bool is_nan;
Tobias Grosser52a25232015-02-04 20:55:43 +000012131 isl_local_space *ls;
Tobias Grosser37034db2016-03-25 19:38:18 +000012132 isl_basic_map *bmap = NULL;
Tobias Grosser52a25232015-02-04 20:55:43 +000012133
12134 if (!aff)
12135 return NULL;
Tobias Grosser37034db2016-03-25 19:38:18 +000012136 is_nan = isl_aff_is_nan(aff);
12137 if (is_nan < 0)
12138 goto error;
12139 if (is_nan)
12140 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
12141 "cannot convert NaN", goto error);
Tobias Grosser52a25232015-02-04 20:55:43 +000012142
12143 ls = isl_aff_get_local_space(aff);
12144 bmap = isl_basic_map_from_local_space(ls);
12145 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
12146 k = isl_basic_map_alloc_equality(bmap);
12147 if (k < 0)
12148 goto error;
12149
12150 pos = isl_basic_map_offset(bmap, isl_dim_out);
12151 isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
12152 isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
12153 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
12154 aff->v->size - (pos + 1));
12155
12156 isl_aff_free(aff);
Tobias Grosser5652c9c2016-10-01 19:46:51 +000012157 if (rational)
12158 bmap = isl_basic_map_set_rational(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +000012159 bmap = isl_basic_map_finalize(bmap);
12160 return bmap;
12161error:
12162 isl_aff_free(aff);
12163 isl_basic_map_free(bmap);
12164 return NULL;
12165}
12166
Tobias Grosser5652c9c2016-10-01 19:46:51 +000012167/* Construct a basic map mapping the domain of the affine expression
12168 * to a one-dimensional range prescribed by the affine expression.
12169 */
12170__isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
12171{
12172 return isl_basic_map_from_aff2(aff, 0);
12173}
12174
Tobias Grosser52a25232015-02-04 20:55:43 +000012175/* Construct a map mapping the domain of the affine expression
12176 * to a one-dimensional range prescribed by the affine expression.
12177 */
12178__isl_give isl_map *isl_map_from_aff(__isl_take isl_aff *aff)
12179{
12180 isl_basic_map *bmap;
12181
12182 bmap = isl_basic_map_from_aff(aff);
12183 return isl_map_from_basic_map(bmap);
12184}
12185
12186/* Construct a basic map mapping the domain the multi-affine expression
12187 * to its range, with each dimension in the range equated to the
12188 * corresponding affine expression.
Tobias Grosser5652c9c2016-10-01 19:46:51 +000012189 * If "rational" is set, then construct a rational basic map.
Tobias Grosser52a25232015-02-04 20:55:43 +000012190 */
Tobias Grosser5652c9c2016-10-01 19:46:51 +000012191__isl_give isl_basic_map *isl_basic_map_from_multi_aff2(
12192 __isl_take isl_multi_aff *maff, int rational)
Tobias Grosser52a25232015-02-04 20:55:43 +000012193{
12194 int i;
12195 isl_space *space;
12196 isl_basic_map *bmap;
12197
12198 if (!maff)
12199 return NULL;
12200
12201 if (isl_space_dim(maff->space, isl_dim_out) != maff->n)
12202 isl_die(isl_multi_aff_get_ctx(maff), isl_error_internal,
12203 "invalid space", goto error);
12204
12205 space = isl_space_domain(isl_multi_aff_get_space(maff));
12206 bmap = isl_basic_map_universe(isl_space_from_domain(space));
Tobias Grosser5652c9c2016-10-01 19:46:51 +000012207 if (rational)
12208 bmap = isl_basic_map_set_rational(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +000012209
12210 for (i = 0; i < maff->n; ++i) {
12211 isl_aff *aff;
12212 isl_basic_map *bmap_i;
12213
12214 aff = isl_aff_copy(maff->p[i]);
Tobias Grosser5652c9c2016-10-01 19:46:51 +000012215 bmap_i = isl_basic_map_from_aff2(aff, rational);
Tobias Grosser52a25232015-02-04 20:55:43 +000012216
12217 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
12218 }
12219
12220 bmap = isl_basic_map_reset_space(bmap, isl_multi_aff_get_space(maff));
12221
12222 isl_multi_aff_free(maff);
12223 return bmap;
12224error:
12225 isl_multi_aff_free(maff);
12226 return NULL;
12227}
12228
Tobias Grosser5652c9c2016-10-01 19:46:51 +000012229/* Construct a basic map mapping the domain the multi-affine expression
12230 * to its range, with each dimension in the range equated to the
12231 * corresponding affine expression.
12232 */
12233__isl_give isl_basic_map *isl_basic_map_from_multi_aff(
12234 __isl_take isl_multi_aff *ma)
12235{
12236 return isl_basic_map_from_multi_aff2(ma, 0);
12237}
12238
Tobias Grosser52a25232015-02-04 20:55:43 +000012239/* Construct a map mapping the domain the multi-affine expression
12240 * to its range, with each dimension in the range equated to the
12241 * corresponding affine expression.
12242 */
12243__isl_give isl_map *isl_map_from_multi_aff(__isl_take isl_multi_aff *maff)
12244{
12245 isl_basic_map *bmap;
12246
12247 bmap = isl_basic_map_from_multi_aff(maff);
12248 return isl_map_from_basic_map(bmap);
12249}
12250
12251/* Construct a basic map mapping a domain in the given space to
12252 * to an n-dimensional range, with n the number of elements in the list,
12253 * where each coordinate in the range is prescribed by the
12254 * corresponding affine expression.
12255 * The domains of all affine expressions in the list are assumed to match
12256 * domain_dim.
12257 */
12258__isl_give isl_basic_map *isl_basic_map_from_aff_list(
12259 __isl_take isl_space *domain_dim, __isl_take isl_aff_list *list)
12260{
12261 int i;
12262 isl_space *dim;
12263 isl_basic_map *bmap;
12264
12265 if (!list)
12266 return NULL;
12267
12268 dim = isl_space_from_domain(domain_dim);
12269 bmap = isl_basic_map_universe(dim);
12270
12271 for (i = 0; i < list->n; ++i) {
12272 isl_aff *aff;
12273 isl_basic_map *bmap_i;
12274
12275 aff = isl_aff_copy(list->p[i]);
12276 bmap_i = isl_basic_map_from_aff(aff);
12277
12278 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
12279 }
12280
12281 isl_aff_list_free(list);
12282 return bmap;
12283}
12284
12285__isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
12286 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12287{
12288 return isl_map_equate(set, type1, pos1, type2, pos2);
12289}
12290
12291/* Construct a basic map where the given dimensions are equal to each other.
12292 */
12293static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
12294 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12295{
12296 isl_basic_map *bmap = NULL;
12297 int i;
12298
12299 if (!space)
12300 return NULL;
12301
12302 if (pos1 >= isl_space_dim(space, type1))
12303 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12304 "index out of bounds", goto error);
12305 if (pos2 >= isl_space_dim(space, type2))
12306 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12307 "index out of bounds", goto error);
12308
12309 if (type1 == type2 && pos1 == pos2)
12310 return isl_basic_map_universe(space);
12311
12312 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
12313 i = isl_basic_map_alloc_equality(bmap);
12314 if (i < 0)
12315 goto error;
12316 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12317 pos1 += isl_basic_map_offset(bmap, type1);
12318 pos2 += isl_basic_map_offset(bmap, type2);
12319 isl_int_set_si(bmap->eq[i][pos1], -1);
12320 isl_int_set_si(bmap->eq[i][pos2], 1);
12321 bmap = isl_basic_map_finalize(bmap);
12322 isl_space_free(space);
12323 return bmap;
12324error:
12325 isl_space_free(space);
12326 isl_basic_map_free(bmap);
12327 return NULL;
12328}
12329
12330/* Add a constraint imposing that the given two dimensions are equal.
12331 */
12332__isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
12333 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12334{
12335 isl_basic_map *eq;
12336
12337 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12338
12339 bmap = isl_basic_map_intersect(bmap, eq);
12340
12341 return bmap;
12342}
12343
12344/* Add a constraint imposing that the given two dimensions are equal.
12345 */
12346__isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
12347 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12348{
12349 isl_basic_map *bmap;
12350
12351 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
12352
12353 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12354
12355 return map;
12356}
12357
12358/* Add a constraint imposing that the given two dimensions have opposite values.
12359 */
12360__isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
12361 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12362{
12363 isl_basic_map *bmap = NULL;
12364 int i;
12365
12366 if (!map)
12367 return NULL;
12368
12369 if (pos1 >= isl_map_dim(map, type1))
12370 isl_die(map->ctx, isl_error_invalid,
12371 "index out of bounds", goto error);
12372 if (pos2 >= isl_map_dim(map, type2))
12373 isl_die(map->ctx, isl_error_invalid,
12374 "index out of bounds", goto error);
12375
12376 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
12377 i = isl_basic_map_alloc_equality(bmap);
12378 if (i < 0)
12379 goto error;
12380 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12381 pos1 += isl_basic_map_offset(bmap, type1);
12382 pos2 += isl_basic_map_offset(bmap, type2);
12383 isl_int_set_si(bmap->eq[i][pos1], 1);
12384 isl_int_set_si(bmap->eq[i][pos2], 1);
12385 bmap = isl_basic_map_finalize(bmap);
12386
12387 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12388
12389 return map;
12390error:
12391 isl_basic_map_free(bmap);
12392 isl_map_free(map);
12393 return NULL;
12394}
12395
12396/* Construct a constraint imposing that the value of the first dimension is
12397 * greater than or equal to that of the second.
12398 */
12399static __isl_give isl_constraint *constraint_order_ge(
12400 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
12401 enum isl_dim_type type2, int pos2)
12402{
12403 isl_constraint *c;
12404
12405 if (!space)
12406 return NULL;
12407
Tobias Grosserb2f39922015-05-28 13:32:11 +000012408 c = isl_constraint_alloc_inequality(isl_local_space_from_space(space));
Tobias Grosser52a25232015-02-04 20:55:43 +000012409
12410 if (pos1 >= isl_constraint_dim(c, type1))
12411 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
12412 "index out of bounds", return isl_constraint_free(c));
12413 if (pos2 >= isl_constraint_dim(c, type2))
12414 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
12415 "index out of bounds", return isl_constraint_free(c));
12416
12417 if (type1 == type2 && pos1 == pos2)
12418 return c;
12419
12420 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
12421 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
12422
12423 return c;
12424}
12425
12426/* Add a constraint imposing that the value of the first dimension is
12427 * greater than or equal to that of the second.
12428 */
12429__isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
12430 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12431{
12432 isl_constraint *c;
12433 isl_space *space;
12434
12435 if (type1 == type2 && pos1 == pos2)
12436 return bmap;
12437 space = isl_basic_map_get_space(bmap);
12438 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12439 bmap = isl_basic_map_add_constraint(bmap, c);
12440
12441 return bmap;
12442}
12443
12444/* Add a constraint imposing that the value of the first dimension is
12445 * greater than or equal to that of the second.
12446 */
12447__isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
12448 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12449{
12450 isl_constraint *c;
12451 isl_space *space;
12452
12453 if (type1 == type2 && pos1 == pos2)
12454 return map;
12455 space = isl_map_get_space(map);
12456 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12457 map = isl_map_add_constraint(map, c);
12458
12459 return map;
12460}
12461
12462/* Add a constraint imposing that the value of the first dimension is
12463 * less than or equal to that of the second.
12464 */
12465__isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
12466 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12467{
12468 return isl_map_order_ge(map, type2, pos2, type1, pos1);
12469}
12470
12471/* Construct a basic map where the value of the first dimension is
12472 * greater than that of the second.
12473 */
12474static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
12475 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12476{
12477 isl_basic_map *bmap = NULL;
12478 int i;
12479
12480 if (!space)
12481 return NULL;
12482
12483 if (pos1 >= isl_space_dim(space, type1))
12484 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12485 "index out of bounds", goto error);
12486 if (pos2 >= isl_space_dim(space, type2))
12487 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12488 "index out of bounds", goto error);
12489
12490 if (type1 == type2 && pos1 == pos2)
12491 return isl_basic_map_empty(space);
12492
12493 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
12494 i = isl_basic_map_alloc_inequality(bmap);
12495 if (i < 0)
12496 return isl_basic_map_free(bmap);
12497 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
12498 pos1 += isl_basic_map_offset(bmap, type1);
12499 pos2 += isl_basic_map_offset(bmap, type2);
12500 isl_int_set_si(bmap->ineq[i][pos1], 1);
12501 isl_int_set_si(bmap->ineq[i][pos2], -1);
12502 isl_int_set_si(bmap->ineq[i][0], -1);
12503 bmap = isl_basic_map_finalize(bmap);
12504
12505 return bmap;
12506error:
12507 isl_space_free(space);
12508 isl_basic_map_free(bmap);
12509 return NULL;
12510}
12511
12512/* Add a constraint imposing that the value of the first dimension is
12513 * greater than that of the second.
12514 */
12515__isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
12516 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12517{
12518 isl_basic_map *gt;
12519
12520 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12521
12522 bmap = isl_basic_map_intersect(bmap, gt);
12523
12524 return bmap;
12525}
12526
12527/* Add a constraint imposing that the value of the first dimension is
12528 * greater than that of the second.
12529 */
12530__isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
12531 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12532{
12533 isl_basic_map *bmap;
12534
12535 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
12536
12537 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12538
12539 return map;
12540}
12541
12542/* Add a constraint imposing that the value of the first dimension is
12543 * smaller than that of the second.
12544 */
12545__isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
12546 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12547{
12548 return isl_map_order_gt(map, type2, pos2, type1, pos1);
12549}
12550
12551__isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
12552 int pos)
12553{
12554 isl_aff *div;
12555 isl_local_space *ls;
12556
12557 if (!bmap)
12558 return NULL;
12559
12560 if (!isl_basic_map_divs_known(bmap))
12561 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12562 "some divs are unknown", return NULL);
12563
12564 ls = isl_basic_map_get_local_space(bmap);
12565 div = isl_local_space_get_div(ls, pos);
12566 isl_local_space_free(ls);
12567
12568 return div;
12569}
12570
12571__isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
12572 int pos)
12573{
12574 return isl_basic_map_get_div(bset, pos);
12575}
12576
12577/* Plug in "subs" for dimension "type", "pos" of "bset".
12578 *
12579 * Let i be the dimension to replace and let "subs" be of the form
12580 *
12581 * f/d
12582 *
12583 * Any integer division with a non-zero coefficient for i,
12584 *
12585 * floor((a i + g)/m)
12586 *
12587 * is replaced by
12588 *
12589 * floor((a f + d g)/(m d))
12590 *
12591 * Constraints of the form
12592 *
12593 * a i + g
12594 *
12595 * are replaced by
12596 *
12597 * a f + d g
12598 *
12599 * We currently require that "subs" is an integral expression.
12600 * Handling rational expressions may require us to add stride constraints
12601 * as we do in isl_basic_set_preimage_multi_aff.
12602 */
12603__isl_give isl_basic_set *isl_basic_set_substitute(
12604 __isl_take isl_basic_set *bset,
12605 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12606{
12607 int i;
12608 isl_int v;
12609 isl_ctx *ctx;
12610
12611 if (bset && isl_basic_set_plain_is_empty(bset))
12612 return bset;
12613
12614 bset = isl_basic_set_cow(bset);
12615 if (!bset || !subs)
12616 goto error;
12617
12618 ctx = isl_basic_set_get_ctx(bset);
12619 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
12620 isl_die(ctx, isl_error_invalid,
12621 "spaces don't match", goto error);
12622 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
12623 isl_die(ctx, isl_error_unsupported,
12624 "cannot handle divs yet", goto error);
12625 if (!isl_int_is_one(subs->v->el[0]))
12626 isl_die(ctx, isl_error_invalid,
12627 "can only substitute integer expressions", goto error);
12628
12629 pos += isl_basic_set_offset(bset, type);
12630
12631 isl_int_init(v);
12632
12633 for (i = 0; i < bset->n_eq; ++i) {
12634 if (isl_int_is_zero(bset->eq[i][pos]))
12635 continue;
12636 isl_int_set(v, bset->eq[i][pos]);
12637 isl_int_set_si(bset->eq[i][pos], 0);
12638 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
12639 v, subs->v->el + 1, subs->v->size - 1);
12640 }
12641
12642 for (i = 0; i < bset->n_ineq; ++i) {
12643 if (isl_int_is_zero(bset->ineq[i][pos]))
12644 continue;
12645 isl_int_set(v, bset->ineq[i][pos]);
12646 isl_int_set_si(bset->ineq[i][pos], 0);
12647 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
12648 v, subs->v->el + 1, subs->v->size - 1);
12649 }
12650
12651 for (i = 0; i < bset->n_div; ++i) {
12652 if (isl_int_is_zero(bset->div[i][1 + pos]))
12653 continue;
12654 isl_int_set(v, bset->div[i][1 + pos]);
12655 isl_int_set_si(bset->div[i][1 + pos], 0);
12656 isl_seq_combine(bset->div[i] + 1,
12657 subs->v->el[0], bset->div[i] + 1,
12658 v, subs->v->el + 1, subs->v->size - 1);
12659 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
12660 }
12661
12662 isl_int_clear(v);
12663
12664 bset = isl_basic_set_simplify(bset);
12665 return isl_basic_set_finalize(bset);
12666error:
12667 isl_basic_set_free(bset);
12668 return NULL;
12669}
12670
12671/* Plug in "subs" for dimension "type", "pos" of "set".
12672 */
12673__isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
12674 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12675{
12676 int i;
12677
12678 if (set && isl_set_plain_is_empty(set))
12679 return set;
12680
12681 set = isl_set_cow(set);
12682 if (!set || !subs)
12683 goto error;
12684
12685 for (i = set->n - 1; i >= 0; --i) {
12686 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
12687 if (remove_if_empty(set, i) < 0)
12688 goto error;
12689 }
12690
12691 return set;
12692error:
12693 isl_set_free(set);
12694 return NULL;
12695}
12696
12697/* Check if the range of "ma" is compatible with the domain or range
12698 * (depending on "type") of "bmap".
12699 * Return -1 if anything is wrong.
12700 */
12701static int check_basic_map_compatible_range_multi_aff(
12702 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
12703 __isl_keep isl_multi_aff *ma)
12704{
12705 int m;
12706 isl_space *ma_space;
12707
12708 ma_space = isl_multi_aff_get_space(ma);
12709
12710 m = isl_space_match(bmap->dim, isl_dim_param, ma_space, isl_dim_param);
12711 if (m < 0)
12712 goto error;
12713 if (!m)
12714 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12715 "parameters don't match", goto error);
12716 m = isl_space_tuple_is_equal(bmap->dim, type, ma_space, isl_dim_out);
12717 if (m < 0)
12718 goto error;
12719 if (!m)
12720 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12721 "spaces don't match", goto error);
12722
12723 isl_space_free(ma_space);
12724 return m;
12725error:
12726 isl_space_free(ma_space);
12727 return -1;
12728}
12729
12730/* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12731 * coefficients before the transformed range of dimensions,
12732 * the "n_after" coefficients after the transformed range of dimensions
12733 * and the coefficients of the other divs in "bmap".
12734 */
12735static int set_ma_divs(__isl_keep isl_basic_map *bmap,
12736 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
12737{
12738 int i;
12739 int n_param;
12740 int n_set;
12741 isl_local_space *ls;
12742
12743 if (n_div == 0)
12744 return 0;
12745
12746 ls = isl_aff_get_domain_local_space(ma->p[0]);
12747 if (!ls)
12748 return -1;
12749
12750 n_param = isl_local_space_dim(ls, isl_dim_param);
12751 n_set = isl_local_space_dim(ls, isl_dim_set);
12752 for (i = 0; i < n_div; ++i) {
12753 int o_bmap = 0, o_ls = 0;
12754
12755 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
12756 o_bmap += 1 + 1 + n_param;
12757 o_ls += 1 + 1 + n_param;
12758 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
12759 o_bmap += n_before;
12760 isl_seq_cpy(bmap->div[i] + o_bmap,
12761 ls->div->row[i] + o_ls, n_set);
12762 o_bmap += n_set;
12763 o_ls += n_set;
12764 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
12765 o_bmap += n_after;
12766 isl_seq_cpy(bmap->div[i] + o_bmap,
12767 ls->div->row[i] + o_ls, n_div);
12768 o_bmap += n_div;
12769 o_ls += n_div;
12770 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
12771 if (isl_basic_set_add_div_constraints(bmap, i) < 0)
12772 goto error;
12773 }
12774
12775 isl_local_space_free(ls);
12776 return 0;
12777error:
12778 isl_local_space_free(ls);
12779 return -1;
12780}
12781
12782/* How many stride constraints does "ma" enforce?
12783 * That is, how many of the affine expressions have a denominator
12784 * different from one?
12785 */
12786static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
12787{
12788 int i;
12789 int strides = 0;
12790
12791 for (i = 0; i < ma->n; ++i)
12792 if (!isl_int_is_one(ma->p[i]->v->el[0]))
12793 strides++;
12794
12795 return strides;
12796}
12797
12798/* For each affine expression in ma of the form
12799 *
12800 * x_i = (f_i y + h_i)/m_i
12801 *
12802 * with m_i different from one, add a constraint to "bmap"
12803 * of the form
12804 *
12805 * f_i y + h_i = m_i alpha_i
12806 *
12807 * with alpha_i an additional existentially quantified variable.
Tobias Grossera67ac972016-02-26 11:35:12 +000012808 *
12809 * The input variables of "ma" correspond to a subset of the variables
12810 * of "bmap". There are "n_before" variables in "bmap" before this
12811 * subset and "n_after" variables after this subset.
12812 * The integer divisions of the affine expressions in "ma" are assumed
12813 * to have been aligned. There are "n_div_ma" of them and
12814 * they appear first in "bmap", straight after the "n_after" variables.
Tobias Grosser52a25232015-02-04 20:55:43 +000012815 */
12816static __isl_give isl_basic_map *add_ma_strides(
12817 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
Tobias Grossera67ac972016-02-26 11:35:12 +000012818 int n_before, int n_after, int n_div_ma)
Tobias Grosser52a25232015-02-04 20:55:43 +000012819{
12820 int i, k;
12821 int div;
12822 int total;
12823 int n_param;
12824 int n_in;
Tobias Grosser52a25232015-02-04 20:55:43 +000012825
12826 total = isl_basic_map_total_dim(bmap);
12827 n_param = isl_multi_aff_dim(ma, isl_dim_param);
12828 n_in = isl_multi_aff_dim(ma, isl_dim_in);
Tobias Grosser52a25232015-02-04 20:55:43 +000012829 for (i = 0; i < ma->n; ++i) {
12830 int o_bmap = 0, o_ma = 1;
12831
12832 if (isl_int_is_one(ma->p[i]->v->el[0]))
12833 continue;
12834 div = isl_basic_map_alloc_div(bmap);
12835 k = isl_basic_map_alloc_equality(bmap);
12836 if (div < 0 || k < 0)
12837 goto error;
12838 isl_int_set_si(bmap->div[div][0], 0);
12839 isl_seq_cpy(bmap->eq[k] + o_bmap,
12840 ma->p[i]->v->el + o_ma, 1 + n_param);
12841 o_bmap += 1 + n_param;
12842 o_ma += 1 + n_param;
12843 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
12844 o_bmap += n_before;
12845 isl_seq_cpy(bmap->eq[k] + o_bmap,
12846 ma->p[i]->v->el + o_ma, n_in);
12847 o_bmap += n_in;
12848 o_ma += n_in;
12849 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
12850 o_bmap += n_after;
12851 isl_seq_cpy(bmap->eq[k] + o_bmap,
Tobias Grossera67ac972016-02-26 11:35:12 +000012852 ma->p[i]->v->el + o_ma, n_div_ma);
12853 o_bmap += n_div_ma;
12854 o_ma += n_div_ma;
Tobias Grosser52a25232015-02-04 20:55:43 +000012855 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
12856 isl_int_neg(bmap->eq[k][1 + total], ma->p[i]->v->el[0]);
12857 total++;
12858 }
12859
12860 return bmap;
12861error:
12862 isl_basic_map_free(bmap);
12863 return NULL;
12864}
12865
12866/* Replace the domain or range space (depending on "type) of "space" by "set".
12867 */
12868static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
12869 enum isl_dim_type type, __isl_take isl_space *set)
12870{
12871 if (type == isl_dim_in) {
12872 space = isl_space_range(space);
12873 space = isl_space_map_from_domain_and_range(set, space);
12874 } else {
12875 space = isl_space_domain(space);
12876 space = isl_space_map_from_domain_and_range(space, set);
12877 }
12878
12879 return space;
12880}
12881
12882/* Compute the preimage of the domain or range (depending on "type")
12883 * of "bmap" under the function represented by "ma".
12884 * In other words, plug in "ma" in the domain or range of "bmap".
12885 * The result is a basic map that lives in the same space as "bmap"
12886 * except that the domain or range has been replaced by
12887 * the domain space of "ma".
12888 *
12889 * If bmap is represented by
12890 *
12891 * A(p) + S u + B x + T v + C(divs) >= 0,
12892 *
12893 * where u and x are input and output dimensions if type == isl_dim_out
12894 * while x and v are input and output dimensions if type == isl_dim_in,
12895 * and ma is represented by
12896 *
12897 * x = D(p) + F(y) + G(divs')
12898 *
12899 * then the result is
12900 *
12901 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12902 *
12903 * The divs in the input set are similarly adjusted.
12904 * In particular
12905 *
12906 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12907 *
12908 * becomes
12909 *
12910 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12911 * B_i G(divs') + c_i(divs))/n_i)
12912 *
12913 * If bmap is not a rational map and if F(y) involves any denominators
12914 *
12915 * x_i = (f_i y + h_i)/m_i
12916 *
12917 * then additional constraints are added to ensure that we only
12918 * map back integer points. That is we enforce
12919 *
12920 * f_i y + h_i = m_i alpha_i
12921 *
12922 * with alpha_i an additional existentially quantified variable.
12923 *
12924 * We first copy over the divs from "ma".
12925 * Then we add the modified constraints and divs from "bmap".
12926 * Finally, we add the stride constraints, if needed.
12927 */
12928__isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
12929 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
12930 __isl_take isl_multi_aff *ma)
12931{
12932 int i, k;
12933 isl_space *space;
12934 isl_basic_map *res = NULL;
12935 int n_before, n_after, n_div_bmap, n_div_ma;
12936 isl_int f, c1, c2, g;
12937 int rational, strides;
12938
12939 isl_int_init(f);
12940 isl_int_init(c1);
12941 isl_int_init(c2);
12942 isl_int_init(g);
12943
12944 ma = isl_multi_aff_align_divs(ma);
12945 if (!bmap || !ma)
12946 goto error;
12947 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
12948 goto error;
12949
12950 if (type == isl_dim_in) {
12951 n_before = 0;
12952 n_after = isl_basic_map_dim(bmap, isl_dim_out);
12953 } else {
12954 n_before = isl_basic_map_dim(bmap, isl_dim_in);
12955 n_after = 0;
12956 }
12957 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
12958 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
12959
12960 space = isl_multi_aff_get_domain_space(ma);
12961 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
12962 rational = isl_basic_map_is_rational(bmap);
12963 strides = rational ? 0 : multi_aff_strides(ma);
12964 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
12965 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
12966 if (rational)
12967 res = isl_basic_map_set_rational(res);
12968
12969 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
12970 if (isl_basic_map_alloc_div(res) < 0)
12971 goto error;
12972
12973 if (set_ma_divs(res, ma, n_before, n_after, n_div_ma) < 0)
12974 goto error;
12975
12976 for (i = 0; i < bmap->n_eq; ++i) {
12977 k = isl_basic_map_alloc_equality(res);
12978 if (k < 0)
12979 goto error;
12980 isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
12981 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12982 }
12983
12984 for (i = 0; i < bmap->n_ineq; ++i) {
12985 k = isl_basic_map_alloc_inequality(res);
12986 if (k < 0)
12987 goto error;
12988 isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
12989 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12990 }
12991
12992 for (i = 0; i < bmap->n_div; ++i) {
12993 if (isl_int_is_zero(bmap->div[i][0])) {
12994 isl_int_set_si(res->div[n_div_ma + i][0], 0);
12995 continue;
12996 }
12997 isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
12998 n_before, n_after, n_div_ma, n_div_bmap,
12999 f, c1, c2, g, 1);
13000 }
13001
13002 if (strides)
Tobias Grossera67ac972016-02-26 11:35:12 +000013003 res = add_ma_strides(res, ma, n_before, n_after, n_div_ma);
Tobias Grosser52a25232015-02-04 20:55:43 +000013004
13005 isl_int_clear(f);
13006 isl_int_clear(c1);
13007 isl_int_clear(c2);
13008 isl_int_clear(g);
13009 isl_basic_map_free(bmap);
13010 isl_multi_aff_free(ma);
13011 res = isl_basic_set_simplify(res);
13012 return isl_basic_map_finalize(res);
13013error:
13014 isl_int_clear(f);
13015 isl_int_clear(c1);
13016 isl_int_clear(c2);
13017 isl_int_clear(g);
13018 isl_basic_map_free(bmap);
13019 isl_multi_aff_free(ma);
13020 isl_basic_map_free(res);
13021 return NULL;
13022}
13023
13024/* Compute the preimage of "bset" under the function represented by "ma".
13025 * In other words, plug in "ma" in "bset". The result is a basic set
13026 * that lives in the domain space of "ma".
13027 */
13028__isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
13029 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
13030{
13031 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
13032}
13033
13034/* Compute the preimage of the domain of "bmap" under the function
13035 * represented by "ma".
13036 * In other words, plug in "ma" in the domain of "bmap".
13037 * The result is a basic map that lives in the same space as "bmap"
13038 * except that the domain has been replaced by the domain space of "ma".
13039 */
13040__isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
13041 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
13042{
13043 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
13044}
13045
13046/* Compute the preimage of the range of "bmap" under the function
13047 * represented by "ma".
13048 * In other words, plug in "ma" in the range of "bmap".
13049 * The result is a basic map that lives in the same space as "bmap"
13050 * except that the range has been replaced by the domain space of "ma".
13051 */
13052__isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
13053 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
13054{
13055 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
13056}
13057
13058/* Check if the range of "ma" is compatible with the domain or range
13059 * (depending on "type") of "map".
13060 * Return -1 if anything is wrong.
13061 */
13062static int check_map_compatible_range_multi_aff(
13063 __isl_keep isl_map *map, enum isl_dim_type type,
13064 __isl_keep isl_multi_aff *ma)
13065{
13066 int m;
13067 isl_space *ma_space;
13068
13069 ma_space = isl_multi_aff_get_space(ma);
13070 m = isl_space_tuple_is_equal(map->dim, type, ma_space, isl_dim_out);
13071 isl_space_free(ma_space);
13072 if (m >= 0 && !m)
13073 isl_die(isl_map_get_ctx(map), isl_error_invalid,
13074 "spaces don't match", return -1);
13075 return m;
13076}
13077
13078/* Compute the preimage of the domain or range (depending on "type")
13079 * of "map" under the function represented by "ma".
13080 * In other words, plug in "ma" in the domain or range of "map".
13081 * The result is a map that lives in the same space as "map"
13082 * except that the domain or range has been replaced by
13083 * the domain space of "ma".
13084 *
13085 * The parameters are assumed to have been aligned.
13086 */
13087static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
13088 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
13089{
13090 int i;
13091 isl_space *space;
13092
13093 map = isl_map_cow(map);
13094 ma = isl_multi_aff_align_divs(ma);
13095 if (!map || !ma)
13096 goto error;
13097 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
13098 goto error;
13099
13100 for (i = 0; i < map->n; ++i) {
13101 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
13102 isl_multi_aff_copy(ma));
13103 if (!map->p[i])
13104 goto error;
13105 }
13106
13107 space = isl_multi_aff_get_domain_space(ma);
13108 space = isl_space_set(isl_map_get_space(map), type, space);
13109
13110 isl_space_free(map->dim);
13111 map->dim = space;
13112 if (!map->dim)
13113 goto error;
13114
13115 isl_multi_aff_free(ma);
13116 if (map->n > 1)
13117 ISL_F_CLR(map, ISL_MAP_DISJOINT);
13118 ISL_F_CLR(map, ISL_SET_NORMALIZED);
13119 return map;
13120error:
13121 isl_multi_aff_free(ma);
13122 isl_map_free(map);
13123 return NULL;
13124}
13125
13126/* Compute the preimage of the domain or range (depending on "type")
13127 * of "map" under the function represented by "ma".
13128 * In other words, plug in "ma" in the domain or range of "map".
13129 * The result is a map that lives in the same space as "map"
13130 * except that the domain or range has been replaced by
13131 * the domain space of "ma".
13132 */
13133__isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
13134 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
13135{
13136 if (!map || !ma)
13137 goto error;
13138
13139 if (isl_space_match(map->dim, isl_dim_param, ma->space, isl_dim_param))
13140 return map_preimage_multi_aff(map, type, ma);
13141
13142 if (!isl_space_has_named_params(map->dim) ||
13143 !isl_space_has_named_params(ma->space))
13144 isl_die(map->ctx, isl_error_invalid,
13145 "unaligned unnamed parameters", goto error);
13146 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
13147 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
13148
13149 return map_preimage_multi_aff(map, type, ma);
13150error:
13151 isl_multi_aff_free(ma);
13152 return isl_map_free(map);
13153}
13154
13155/* Compute the preimage of "set" under the function represented by "ma".
13156 * In other words, plug in "ma" in "set". The result is a set
13157 * that lives in the domain space of "ma".
13158 */
13159__isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
13160 __isl_take isl_multi_aff *ma)
13161{
13162 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
13163}
13164
13165/* Compute the preimage of the domain of "map" under the function
13166 * represented by "ma".
13167 * In other words, plug in "ma" in the domain of "map".
13168 * The result is a map that lives in the same space as "map"
13169 * except that the domain has been replaced by the domain space of "ma".
13170 */
13171__isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
13172 __isl_take isl_multi_aff *ma)
13173{
13174 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
13175}
13176
13177/* Compute the preimage of the range of "map" under the function
13178 * represented by "ma".
13179 * In other words, plug in "ma" in the range of "map".
13180 * The result is a map that lives in the same space as "map"
13181 * except that the range has been replaced by the domain space of "ma".
13182 */
13183__isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
13184 __isl_take isl_multi_aff *ma)
13185{
13186 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
13187}
13188
13189/* Compute the preimage of "map" under the function represented by "pma".
13190 * In other words, plug in "pma" in the domain or range of "map".
13191 * The result is a map that lives in the same space as "map",
13192 * except that the space of type "type" has been replaced by
13193 * the domain space of "pma".
13194 *
13195 * The parameters of "map" and "pma" are assumed to have been aligned.
13196 */
13197static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
13198 __isl_take isl_map *map, enum isl_dim_type type,
13199 __isl_take isl_pw_multi_aff *pma)
13200{
13201 int i;
13202 isl_map *res;
13203
13204 if (!pma)
13205 goto error;
13206
13207 if (pma->n == 0) {
13208 isl_pw_multi_aff_free(pma);
13209 res = isl_map_empty(isl_map_get_space(map));
13210 isl_map_free(map);
13211 return res;
13212 }
13213
13214 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
13215 isl_multi_aff_copy(pma->p[0].maff));
13216 if (type == isl_dim_in)
13217 res = isl_map_intersect_domain(res,
13218 isl_map_copy(pma->p[0].set));
13219 else
13220 res = isl_map_intersect_range(res,
13221 isl_map_copy(pma->p[0].set));
13222
13223 for (i = 1; i < pma->n; ++i) {
13224 isl_map *res_i;
13225
13226 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
13227 isl_multi_aff_copy(pma->p[i].maff));
13228 if (type == isl_dim_in)
13229 res_i = isl_map_intersect_domain(res_i,
13230 isl_map_copy(pma->p[i].set));
13231 else
13232 res_i = isl_map_intersect_range(res_i,
13233 isl_map_copy(pma->p[i].set));
13234 res = isl_map_union(res, res_i);
13235 }
13236
13237 isl_pw_multi_aff_free(pma);
13238 isl_map_free(map);
13239 return res;
13240error:
13241 isl_pw_multi_aff_free(pma);
13242 isl_map_free(map);
13243 return NULL;
13244}
13245
13246/* Compute the preimage of "map" under the function represented by "pma".
13247 * In other words, plug in "pma" in the domain or range of "map".
13248 * The result is a map that lives in the same space as "map",
13249 * except that the space of type "type" has been replaced by
13250 * the domain space of "pma".
13251 */
13252__isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
13253 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
13254{
13255 if (!map || !pma)
13256 goto error;
13257
13258 if (isl_space_match(map->dim, isl_dim_param, pma->dim, isl_dim_param))
13259 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13260
13261 if (!isl_space_has_named_params(map->dim) ||
13262 !isl_space_has_named_params(pma->dim))
13263 isl_die(map->ctx, isl_error_invalid,
13264 "unaligned unnamed parameters", goto error);
13265 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
13266 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
13267
13268 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13269error:
13270 isl_pw_multi_aff_free(pma);
13271 return isl_map_free(map);
13272}
13273
13274/* Compute the preimage of "set" under the function represented by "pma".
13275 * In other words, plug in "pma" in "set". The result is a set
13276 * that lives in the domain space of "pma".
13277 */
13278__isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
13279 __isl_take isl_pw_multi_aff *pma)
13280{
13281 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
13282}
13283
13284/* Compute the preimage of the domain of "map" under the function
13285 * represented by "pma".
13286 * In other words, plug in "pma" in the domain of "map".
13287 * The result is a map that lives in the same space as "map",
13288 * except that domain space has been replaced by the domain space of "pma".
13289 */
13290__isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
13291 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13292{
13293 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
13294}
13295
13296/* Compute the preimage of the range of "map" under the function
13297 * represented by "pma".
13298 * In other words, plug in "pma" in the range of "map".
13299 * The result is a map that lives in the same space as "map",
13300 * except that range space has been replaced by the domain space of "pma".
13301 */
13302__isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
13303 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13304{
13305 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
13306}
13307
13308/* Compute the preimage of "map" under the function represented by "mpa".
13309 * In other words, plug in "mpa" in the domain or range of "map".
13310 * The result is a map that lives in the same space as "map",
13311 * except that the space of type "type" has been replaced by
13312 * the domain space of "mpa".
13313 *
13314 * If the map does not involve any constraints that refer to the
13315 * dimensions of the substituted space, then the only possible
13316 * effect of "mpa" on the map is to map the space to a different space.
13317 * We create a separate isl_multi_aff to effectuate this change
13318 * in order to avoid spurious splitting of the map along the pieces
13319 * of "mpa".
13320 */
13321__isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
13322 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
13323{
13324 int n;
13325 isl_pw_multi_aff *pma;
13326
13327 if (!map || !mpa)
13328 goto error;
13329
13330 n = isl_map_dim(map, type);
13331 if (!isl_map_involves_dims(map, type, 0, n)) {
13332 isl_space *space;
13333 isl_multi_aff *ma;
13334
13335 space = isl_multi_pw_aff_get_space(mpa);
13336 isl_multi_pw_aff_free(mpa);
13337 ma = isl_multi_aff_zero(space);
13338 return isl_map_preimage_multi_aff(map, type, ma);
13339 }
13340
13341 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
13342 return isl_map_preimage_pw_multi_aff(map, type, pma);
13343error:
13344 isl_map_free(map);
13345 isl_multi_pw_aff_free(mpa);
13346 return NULL;
13347}
13348
13349/* Compute the preimage of "map" under the function represented by "mpa".
13350 * In other words, plug in "mpa" in the domain "map".
13351 * The result is a map that lives in the same space as "map",
13352 * except that domain space has been replaced by the domain space of "mpa".
13353 */
13354__isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
13355 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
13356{
13357 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
13358}
13359
13360/* Compute the preimage of "set" by the function represented by "mpa".
13361 * In other words, plug in "mpa" in "set".
13362 */
13363__isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
13364 __isl_take isl_multi_pw_aff *mpa)
13365{
13366 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);
13367}
Michael Kruse1b8eb412016-12-06 14:37:39 +000013368
Tobias Grosser94e53712016-12-31 07:46:11 +000013369/* Are the "n" "coefficients" starting at "first" of the integer division
13370 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
13371 * to each other?
13372 * The "coefficient" at position 0 is the denominator.
13373 * The "coefficient" at position 1 is the constant term.
13374 */
13375isl_bool isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map *bmap1,
13376 int pos1, __isl_keep isl_basic_map *bmap2, int pos2,
13377 unsigned first, unsigned n)
13378{
13379 if (isl_basic_map_check_range(bmap1, isl_dim_div, pos1, 1) < 0)
13380 return isl_bool_error;
13381 if (isl_basic_map_check_range(bmap2, isl_dim_div, pos2, 1) < 0)
13382 return isl_bool_error;
13383 return isl_seq_eq(bmap1->div[pos1] + first,
13384 bmap2->div[pos2] + first, n);
13385}
13386
13387/* Are the integer division expressions at position "pos1" in "bmap1" and
13388 * "pos2" in "bmap2" equal to each other, except that the constant terms
13389 * are different?
13390 */
13391isl_bool isl_basic_map_equal_div_expr_except_constant(
13392 __isl_keep isl_basic_map *bmap1, int pos1,
13393 __isl_keep isl_basic_map *bmap2, int pos2)
13394{
13395 isl_bool equal;
13396 unsigned total;
13397
13398 if (!bmap1 || !bmap2)
13399 return isl_bool_error;
13400 total = isl_basic_map_total_dim(bmap1);
13401 if (total != isl_basic_map_total_dim(bmap2))
13402 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
13403 "incomparable div expressions", return isl_bool_error);
13404 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13405 0, 1);
13406 if (equal < 0 || !equal)
13407 return equal;
13408 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13409 1, 1);
13410 if (equal < 0 || equal)
13411 return isl_bool_not(equal);
13412 return isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13413 2, total);
13414}
13415
13416/* Replace the numerator of the constant term of the integer division
13417 * expression at position "div" in "bmap" by "value".
13418 * The caller guarantees that this does not change the meaning
13419 * of the input.
13420 */
13421__isl_give isl_basic_map *isl_basic_map_set_div_expr_constant_num_si_inplace(
13422 __isl_take isl_basic_map *bmap, int div, int value)
13423{
13424 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
13425 return isl_basic_map_free(bmap);
13426
13427 isl_int_set_si(bmap->div[div][1], value);
13428
13429 return bmap;
13430}
13431
Michael Kruse1b8eb412016-12-06 14:37:39 +000013432/* Is the point "inner" internal to inequality constraint "ineq"
13433 * of "bset"?
13434 * The point is considered to be internal to the inequality constraint,
13435 * if it strictly lies on the positive side of the inequality constraint,
13436 * or if it lies on the constraint and the constraint is lexico-positive.
13437 */
13438static isl_bool is_internal(__isl_keep isl_vec *inner,
13439 __isl_keep isl_basic_set *bset, int ineq)
13440{
13441 isl_ctx *ctx;
13442 int pos;
13443 unsigned total;
13444
13445 if (!inner || !bset)
13446 return isl_bool_error;
13447
13448 ctx = isl_basic_set_get_ctx(bset);
13449 isl_seq_inner_product(inner->el, bset->ineq[ineq], inner->size,
13450 &ctx->normalize_gcd);
13451 if (!isl_int_is_zero(ctx->normalize_gcd))
13452 return isl_int_is_nonneg(ctx->normalize_gcd);
13453
13454 total = isl_basic_set_dim(bset, isl_dim_all);
13455 pos = isl_seq_first_non_zero(bset->ineq[ineq] + 1, total);
13456 return isl_int_is_pos(bset->ineq[ineq][1 + pos]);
13457}
13458
13459/* Tighten the inequality constraints of "bset" that are outward with respect
13460 * to the point "vec".
13461 * That is, tighten the constraints that are not satisfied by "vec".
13462 *
13463 * "vec" is a point internal to some superset S of "bset" that is used
13464 * to make the subsets of S disjoint, by tightening one half of the constraints
13465 * that separate two subsets. In particular, the constraints of S
13466 * are all satisfied by "vec" and should not be tightened.
13467 * Of the internal constraints, those that have "vec" on the outside
13468 * are tightened. The shared facet is included in the adjacent subset
13469 * with the opposite constraint.
13470 * For constraints that saturate "vec", this criterion cannot be used
13471 * to determine which of the two sides should be tightened.
13472 * Instead, the sign of the first non-zero coefficient is used
13473 * to make this choice. Note that this second criterion is never used
13474 * on the constraints of S since "vec" is interior to "S".
13475 */
13476__isl_give isl_basic_set *isl_basic_set_tighten_outward(
13477 __isl_take isl_basic_set *bset, __isl_keep isl_vec *vec)
13478{
13479 int j;
13480
13481 bset = isl_basic_set_cow(bset);
13482 if (!bset)
13483 return NULL;
13484 for (j = 0; j < bset->n_ineq; ++j) {
13485 isl_bool internal;
13486
13487 internal = is_internal(vec, bset, j);
13488 if (internal < 0)
13489 return isl_basic_set_free(bset);
13490 if (internal)
13491 continue;
13492 isl_int_sub_ui(bset->ineq[j][0], bset->ineq[j][0], 1);
13493 }
13494
13495 return bset;
13496}