blob: c8543820f227ca43730fc7a4ea99dbcdeba56b5c [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
Tobias Grossere5671e52017-03-10 09:17:55 +000091/* Return the space of "map".
92 */
93__isl_keep isl_space *isl_map_peek_space(__isl_keep const isl_map *map)
94{
95 return map ? map->dim : NULL;
96}
97
Tobias Grosser52a25232015-02-04 20:55:43 +000098unsigned isl_map_dim(__isl_keep isl_map *map, enum isl_dim_type type)
99{
100 return map ? n(map->dim, type) : 0;
101}
102
103unsigned isl_set_dim(__isl_keep isl_set *set, enum isl_dim_type type)
104{
105 return set ? n(set->dim, type) : 0;
106}
107
108unsigned isl_basic_map_offset(struct isl_basic_map *bmap,
109 enum isl_dim_type type)
110{
Michael Kruseffb32782016-08-17 15:24:45 +0000111 isl_space *space;
112
113 if (!bmap)
114 return 0;
115
116 space = bmap->dim;
Tobias Grosser52a25232015-02-04 20:55:43 +0000117 switch (type) {
118 case isl_dim_cst: return 0;
119 case isl_dim_param: return 1;
Michael Kruseffb32782016-08-17 15:24:45 +0000120 case isl_dim_in: return 1 + space->nparam;
121 case isl_dim_out: return 1 + space->nparam + space->n_in;
122 case isl_dim_div: return 1 + space->nparam + space->n_in +
123 space->n_out;
Tobias Grosser52a25232015-02-04 20:55:43 +0000124 default: return 0;
125 }
126}
127
128unsigned isl_basic_set_offset(struct isl_basic_set *bset,
129 enum isl_dim_type type)
130{
131 return isl_basic_map_offset(bset, type);
132}
133
134static unsigned map_offset(struct isl_map *map, enum isl_dim_type type)
135{
136 return pos(map->dim, type);
137}
138
139unsigned isl_basic_set_dim(__isl_keep isl_basic_set *bset,
140 enum isl_dim_type type)
141{
142 return isl_basic_map_dim(bset, type);
143}
144
145unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set *bset)
146{
147 return isl_basic_set_dim(bset, isl_dim_set);
148}
149
150unsigned isl_basic_set_n_param(__isl_keep isl_basic_set *bset)
151{
152 return isl_basic_set_dim(bset, isl_dim_param);
153}
154
155unsigned isl_basic_set_total_dim(const struct isl_basic_set *bset)
156{
157 if (!bset)
158 return 0;
159 return isl_space_dim(bset->dim, isl_dim_all) + bset->n_div;
160}
161
162unsigned isl_set_n_dim(__isl_keep isl_set *set)
163{
164 return isl_set_dim(set, isl_dim_set);
165}
166
167unsigned isl_set_n_param(__isl_keep isl_set *set)
168{
169 return isl_set_dim(set, isl_dim_param);
170}
171
Tobias Grosserf4fe34b2017-03-16 21:33:20 +0000172unsigned isl_basic_map_n_in(__isl_keep const isl_basic_map *bmap)
Tobias Grosser52a25232015-02-04 20:55:43 +0000173{
174 return bmap ? bmap->dim->n_in : 0;
175}
176
Tobias Grosserf4fe34b2017-03-16 21:33:20 +0000177unsigned isl_basic_map_n_out(__isl_keep const isl_basic_map *bmap)
Tobias Grosser52a25232015-02-04 20:55:43 +0000178{
179 return bmap ? bmap->dim->n_out : 0;
180}
181
Tobias Grosserf4fe34b2017-03-16 21:33:20 +0000182unsigned isl_basic_map_n_param(__isl_keep const isl_basic_map *bmap)
Tobias Grosser52a25232015-02-04 20:55:43 +0000183{
184 return bmap ? bmap->dim->nparam : 0;
185}
186
Tobias Grosserf4fe34b2017-03-16 21:33:20 +0000187unsigned isl_basic_map_n_div(__isl_keep const isl_basic_map *bmap)
Tobias Grosser52a25232015-02-04 20:55:43 +0000188{
189 return bmap ? bmap->n_div : 0;
190}
191
192unsigned isl_basic_map_total_dim(const struct isl_basic_map *bmap)
193{
194 return bmap ? isl_space_dim(bmap->dim, isl_dim_all) + bmap->n_div : 0;
195}
196
197unsigned isl_map_n_in(const struct isl_map *map)
198{
199 return map ? map->dim->n_in : 0;
200}
201
202unsigned isl_map_n_out(const struct isl_map *map)
203{
204 return map ? map->dim->n_out : 0;
205}
206
207unsigned isl_map_n_param(const struct isl_map *map)
208{
209 return map ? map->dim->nparam : 0;
210}
211
Tobias Grossere5671e52017-03-10 09:17:55 +0000212/* Do "bmap1" and "bmap2" have the same parameters?
213 */
214static isl_bool isl_basic_map_has_equal_params(__isl_keep isl_basic_map *bmap1,
215 __isl_keep isl_basic_map *bmap2)
216{
217 isl_space *space1, *space2;
218
219 space1 = isl_basic_map_peek_space(bmap1);
220 space2 = isl_basic_map_peek_space(bmap2);
Tobias Grosser593ebdf2017-03-14 07:33:26 +0000221 return isl_space_has_equal_params(space1, space2);
Tobias Grossere5671e52017-03-10 09:17:55 +0000222}
223
224/* Do "map1" and "map2" have the same parameters?
225 */
226isl_bool isl_map_has_equal_params(__isl_keep isl_map *map1,
227 __isl_keep isl_map *map2)
228{
229 isl_space *space1, *space2;
230
231 space1 = isl_map_peek_space(map1);
232 space2 = isl_map_peek_space(map2);
Tobias Grosser593ebdf2017-03-14 07:33:26 +0000233 return isl_space_has_equal_params(space1, space2);
Tobias Grossere5671e52017-03-10 09:17:55 +0000234}
235
236/* Do "map" and "set" have the same parameters?
237 */
238static isl_bool isl_map_set_has_equal_params(__isl_keep isl_map *map,
239 __isl_keep isl_set *set)
240{
241 return isl_map_has_equal_params(map, set_to_map(set));
242}
243
Tobias Grosser72745c22017-02-17 05:11:16 +0000244isl_bool isl_map_compatible_domain(__isl_keep isl_map *map,
245 __isl_keep isl_set *set)
Tobias Grosser52a25232015-02-04 20:55:43 +0000246{
Tobias Grosser72745c22017-02-17 05:11:16 +0000247 isl_bool m;
Tobias Grosser52a25232015-02-04 20:55:43 +0000248 if (!map || !set)
Tobias Grosser72745c22017-02-17 05:11:16 +0000249 return isl_bool_error;
Tobias Grossere5671e52017-03-10 09:17:55 +0000250 m = isl_map_has_equal_params(map, set_to_map(set));
Tobias Grosser52a25232015-02-04 20:55:43 +0000251 if (m < 0 || !m)
252 return m;
253 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
254 set->dim, isl_dim_set);
255}
256
Tobias Grosser932ec012016-07-06 09:11:00 +0000257isl_bool isl_basic_map_compatible_domain(__isl_keep isl_basic_map *bmap,
258 __isl_keep isl_basic_set *bset)
Tobias Grosser52a25232015-02-04 20:55:43 +0000259{
Tobias Grosser932ec012016-07-06 09:11:00 +0000260 isl_bool m;
Tobias Grosser52a25232015-02-04 20:55:43 +0000261 if (!bmap || !bset)
Tobias Grosser932ec012016-07-06 09:11:00 +0000262 return isl_bool_error;
Tobias Grossere5671e52017-03-10 09:17:55 +0000263 m = isl_basic_map_has_equal_params(bmap, bset_to_bmap(bset));
Tobias Grosser52a25232015-02-04 20:55:43 +0000264 if (m < 0 || !m)
265 return m;
266 return isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
267 bset->dim, isl_dim_set);
268}
269
Tobias Grosser72745c22017-02-17 05:11:16 +0000270isl_bool isl_map_compatible_range(__isl_keep isl_map *map,
271 __isl_keep isl_set *set)
Tobias Grosser52a25232015-02-04 20:55:43 +0000272{
Tobias Grosser72745c22017-02-17 05:11:16 +0000273 isl_bool m;
Tobias Grosser52a25232015-02-04 20:55:43 +0000274 if (!map || !set)
Tobias Grosser72745c22017-02-17 05:11:16 +0000275 return isl_bool_error;
Tobias Grossere5671e52017-03-10 09:17:55 +0000276 m = isl_map_has_equal_params(map, set_to_map(set));
Tobias Grosser52a25232015-02-04 20:55:43 +0000277 if (m < 0 || !m)
278 return m;
279 return isl_space_tuple_is_equal(map->dim, isl_dim_out,
280 set->dim, isl_dim_set);
281}
282
Tobias Grosser72745c22017-02-17 05:11:16 +0000283isl_bool isl_basic_map_compatible_range(__isl_keep isl_basic_map *bmap,
284 __isl_keep isl_basic_set *bset)
Tobias Grosser52a25232015-02-04 20:55:43 +0000285{
Tobias Grosser72745c22017-02-17 05:11:16 +0000286 isl_bool m;
Tobias Grosser52a25232015-02-04 20:55:43 +0000287 if (!bmap || !bset)
Tobias Grosser72745c22017-02-17 05:11:16 +0000288 return isl_bool_error;
Tobias Grossere5671e52017-03-10 09:17:55 +0000289 m = isl_basic_map_has_equal_params(bmap, bset_to_bmap(bset));
Tobias Grosser52a25232015-02-04 20:55:43 +0000290 if (m < 0 || !m)
291 return m;
292 return isl_space_tuple_is_equal(bmap->dim, isl_dim_out,
293 bset->dim, isl_dim_set);
294}
295
296isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
297{
298 return bmap ? bmap->ctx : NULL;
299}
300
301isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
302{
303 return bset ? bset->ctx : NULL;
304}
305
306isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
307{
308 return map ? map->ctx : NULL;
309}
310
311isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
312{
313 return set ? set->ctx : NULL;
314}
315
Tobias Grossere5671e52017-03-10 09:17:55 +0000316/* Return the space of "bmap".
317 */
318__isl_keep isl_space *isl_basic_map_peek_space(
319 __isl_keep const isl_basic_map *bmap)
320{
321 return bmap ? bmap->dim : NULL;
322}
323
324/* Return the space of "bset".
325 */
326__isl_keep isl_space *isl_basic_set_peek_space(__isl_keep isl_basic_set *bset)
327{
328 return isl_basic_map_peek_space(bset_to_bmap(bset));
329}
330
Tobias Grosser52a25232015-02-04 20:55:43 +0000331__isl_give isl_space *isl_basic_map_get_space(__isl_keep isl_basic_map *bmap)
332{
Tobias Grossere5671e52017-03-10 09:17:55 +0000333 return isl_space_copy(isl_basic_map_peek_space(bmap));
Tobias Grosser52a25232015-02-04 20:55:43 +0000334}
335
336__isl_give isl_space *isl_basic_set_get_space(__isl_keep isl_basic_set *bset)
337{
Tobias Grossere5671e52017-03-10 09:17:55 +0000338 return isl_basic_map_get_space(bset_to_bmap(bset));
Tobias Grosser52a25232015-02-04 20:55:43 +0000339}
340
341/* Extract the divs in "bmap" as a matrix.
342 */
343__isl_give isl_mat *isl_basic_map_get_divs(__isl_keep isl_basic_map *bmap)
344{
345 int i;
346 isl_ctx *ctx;
347 isl_mat *div;
348 unsigned total;
349 unsigned cols;
350
351 if (!bmap)
352 return NULL;
353
354 ctx = isl_basic_map_get_ctx(bmap);
355 total = isl_space_dim(bmap->dim, isl_dim_all);
356 cols = 1 + 1 + total + bmap->n_div;
357 div = isl_mat_alloc(ctx, bmap->n_div, cols);
358 if (!div)
359 return NULL;
360
361 for (i = 0; i < bmap->n_div; ++i)
362 isl_seq_cpy(div->row[i], bmap->div[i], cols);
363
364 return div;
365}
366
367/* Extract the divs in "bset" as a matrix.
368 */
369__isl_give isl_mat *isl_basic_set_get_divs(__isl_keep isl_basic_set *bset)
370{
371 return isl_basic_map_get_divs(bset);
372}
373
374__isl_give isl_local_space *isl_basic_map_get_local_space(
375 __isl_keep isl_basic_map *bmap)
376{
377 isl_mat *div;
378
379 if (!bmap)
380 return NULL;
381
382 div = isl_basic_map_get_divs(bmap);
383 return isl_local_space_alloc_div(isl_space_copy(bmap->dim), div);
384}
385
386__isl_give isl_local_space *isl_basic_set_get_local_space(
387 __isl_keep isl_basic_set *bset)
388{
389 return isl_basic_map_get_local_space(bset);
390}
391
Michael Kruse959a8dc2016-01-15 15:54:45 +0000392/* For each known div d = floor(f/m), add the constraints
393 *
394 * f - m d >= 0
Tobias Grosser07b20952016-06-12 04:30:40 +0000395 * -(f-(m-1)) + m d >= 0
Michael Kruse959a8dc2016-01-15 15:54:45 +0000396 *
397 * Do not finalize the result.
398 */
399static __isl_give isl_basic_map *add_known_div_constraints(
400 __isl_take isl_basic_map *bmap)
401{
402 int i;
403 unsigned n_div;
404
405 if (!bmap)
406 return NULL;
407 n_div = isl_basic_map_dim(bmap, isl_dim_div);
408 if (n_div == 0)
409 return bmap;
410 bmap = isl_basic_map_cow(bmap);
411 bmap = isl_basic_map_extend_constraints(bmap, 0, 2 * n_div);
412 if (!bmap)
413 return NULL;
414 for (i = 0; i < n_div; ++i) {
415 if (isl_int_is_zero(bmap->div[i][0]))
416 continue;
417 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
418 return isl_basic_map_free(bmap);
419 }
420
421 return bmap;
422}
423
Tobias Grosser52a25232015-02-04 20:55:43 +0000424__isl_give isl_basic_map *isl_basic_map_from_local_space(
425 __isl_take isl_local_space *ls)
426{
427 int i;
428 int n_div;
429 isl_basic_map *bmap;
430
431 if (!ls)
432 return NULL;
433
434 n_div = isl_local_space_dim(ls, isl_dim_div);
435 bmap = isl_basic_map_alloc_space(isl_local_space_get_space(ls),
436 n_div, 0, 2 * n_div);
437
438 for (i = 0; i < n_div; ++i)
439 if (isl_basic_map_alloc_div(bmap) < 0)
440 goto error;
441
Michael Kruse959a8dc2016-01-15 15:54:45 +0000442 for (i = 0; i < n_div; ++i)
Tobias Grosser52a25232015-02-04 20:55:43 +0000443 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
Michael Kruse959a8dc2016-01-15 15:54:45 +0000444 bmap = add_known_div_constraints(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +0000445
446 isl_local_space_free(ls);
447 return bmap;
448error:
449 isl_local_space_free(ls);
450 isl_basic_map_free(bmap);
451 return NULL;
452}
453
454__isl_give isl_basic_set *isl_basic_set_from_local_space(
455 __isl_take isl_local_space *ls)
456{
457 return isl_basic_map_from_local_space(ls);
458}
459
460__isl_give isl_space *isl_map_get_space(__isl_keep isl_map *map)
461{
Tobias Grossere5671e52017-03-10 09:17:55 +0000462 return isl_space_copy(isl_map_peek_space(map));
Tobias Grosser52a25232015-02-04 20:55:43 +0000463}
464
465__isl_give isl_space *isl_set_get_space(__isl_keep isl_set *set)
466{
467 if (!set)
468 return NULL;
469 return isl_space_copy(set->dim);
470}
471
472__isl_give isl_basic_map *isl_basic_map_set_tuple_name(
473 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
474{
475 bmap = isl_basic_map_cow(bmap);
476 if (!bmap)
477 return NULL;
478 bmap->dim = isl_space_set_tuple_name(bmap->dim, type, s);
479 if (!bmap->dim)
480 goto error;
481 bmap = isl_basic_map_finalize(bmap);
482 return bmap;
483error:
484 isl_basic_map_free(bmap);
485 return NULL;
486}
487
488__isl_give isl_basic_set *isl_basic_set_set_tuple_name(
489 __isl_take isl_basic_set *bset, const char *s)
490{
491 return isl_basic_map_set_tuple_name(bset, isl_dim_set, s);
492}
493
494const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
495 enum isl_dim_type type)
496{
497 return bmap ? isl_space_get_tuple_name(bmap->dim, type) : NULL;
498}
499
500__isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
501 enum isl_dim_type type, const char *s)
502{
503 int i;
504
505 map = isl_map_cow(map);
506 if (!map)
507 return NULL;
508
509 map->dim = isl_space_set_tuple_name(map->dim, type, s);
510 if (!map->dim)
511 goto error;
512
513 for (i = 0; i < map->n; ++i) {
514 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
515 if (!map->p[i])
516 goto error;
517 }
518
519 return map;
520error:
521 isl_map_free(map);
522 return NULL;
523}
524
525/* Replace the identifier of the tuple of type "type" by "id".
526 */
527__isl_give isl_basic_map *isl_basic_map_set_tuple_id(
528 __isl_take isl_basic_map *bmap,
529 enum isl_dim_type type, __isl_take isl_id *id)
530{
531 bmap = isl_basic_map_cow(bmap);
532 if (!bmap)
533 goto error;
534 bmap->dim = isl_space_set_tuple_id(bmap->dim, type, id);
535 if (!bmap->dim)
536 return isl_basic_map_free(bmap);
537 bmap = isl_basic_map_finalize(bmap);
538 return bmap;
539error:
540 isl_id_free(id);
541 return NULL;
542}
543
544/* Replace the identifier of the tuple by "id".
545 */
546__isl_give isl_basic_set *isl_basic_set_set_tuple_id(
547 __isl_take isl_basic_set *bset, __isl_take isl_id *id)
548{
549 return isl_basic_map_set_tuple_id(bset, isl_dim_set, id);
550}
551
552/* Does the input or output tuple have a name?
553 */
Tobias Grosserb2f39922015-05-28 13:32:11 +0000554isl_bool isl_map_has_tuple_name(__isl_keep isl_map *map, enum isl_dim_type type)
Tobias Grosser52a25232015-02-04 20:55:43 +0000555{
Tobias Grosserb2f39922015-05-28 13:32:11 +0000556 return map ? isl_space_has_tuple_name(map->dim, type) : isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +0000557}
558
559const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
560 enum isl_dim_type type)
561{
562 return map ? isl_space_get_tuple_name(map->dim, type) : NULL;
563}
564
565__isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
566 const char *s)
567{
Tobias Grosser06e15922016-11-16 11:06:47 +0000568 return set_from_map(isl_map_set_tuple_name(set_to_map(set),
569 isl_dim_set, s));
Tobias Grosser52a25232015-02-04 20:55:43 +0000570}
571
572__isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map,
573 enum isl_dim_type type, __isl_take isl_id *id)
574{
575 map = isl_map_cow(map);
576 if (!map)
577 goto error;
578
579 map->dim = isl_space_set_tuple_id(map->dim, type, id);
580
581 return isl_map_reset_space(map, isl_space_copy(map->dim));
582error:
583 isl_id_free(id);
584 return NULL;
585}
586
587__isl_give isl_set *isl_set_set_tuple_id(__isl_take isl_set *set,
588 __isl_take isl_id *id)
589{
590 return isl_map_set_tuple_id(set, isl_dim_set, id);
591}
592
593__isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map,
594 enum isl_dim_type type)
595{
596 map = isl_map_cow(map);
597 if (!map)
598 return NULL;
599
600 map->dim = isl_space_reset_tuple_id(map->dim, type);
601
602 return isl_map_reset_space(map, isl_space_copy(map->dim));
603}
604
605__isl_give isl_set *isl_set_reset_tuple_id(__isl_take isl_set *set)
606{
607 return isl_map_reset_tuple_id(set, isl_dim_set);
608}
609
Tobias Grosserb2f39922015-05-28 13:32:11 +0000610isl_bool isl_map_has_tuple_id(__isl_keep isl_map *map, enum isl_dim_type type)
Tobias Grosser52a25232015-02-04 20:55:43 +0000611{
Tobias Grosserb2f39922015-05-28 13:32:11 +0000612 return map ? isl_space_has_tuple_id(map->dim, type) : isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +0000613}
614
615__isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map,
616 enum isl_dim_type type)
617{
618 return map ? isl_space_get_tuple_id(map->dim, type) : NULL;
619}
620
Tobias Grosserb2f39922015-05-28 13:32:11 +0000621isl_bool isl_set_has_tuple_id(__isl_keep isl_set *set)
Tobias Grosser52a25232015-02-04 20:55:43 +0000622{
623 return isl_map_has_tuple_id(set, isl_dim_set);
624}
625
626__isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_set *set)
627{
628 return isl_map_get_tuple_id(set, isl_dim_set);
629}
630
631/* Does the set tuple have a name?
632 */
Tobias Grosserb2f39922015-05-28 13:32:11 +0000633isl_bool isl_set_has_tuple_name(__isl_keep isl_set *set)
Tobias Grosser52a25232015-02-04 20:55:43 +0000634{
Tobias Grosserb2f39922015-05-28 13:32:11 +0000635 if (!set)
636 return isl_bool_error;
637 return isl_space_has_tuple_name(set->dim, isl_dim_set);
Tobias Grosser52a25232015-02-04 20:55:43 +0000638}
639
640
641const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
642{
643 return bset ? isl_space_get_tuple_name(bset->dim, isl_dim_set) : NULL;
644}
645
646const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
647{
648 return set ? isl_space_get_tuple_name(set->dim, isl_dim_set) : NULL;
649}
650
651const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
652 enum isl_dim_type type, unsigned pos)
653{
654 return bmap ? isl_space_get_dim_name(bmap->dim, type, pos) : NULL;
655}
656
657const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
658 enum isl_dim_type type, unsigned pos)
659{
660 return bset ? isl_space_get_dim_name(bset->dim, type, pos) : NULL;
661}
662
663/* Does the given dimension have a name?
664 */
Tobias Grosserb2f39922015-05-28 13:32:11 +0000665isl_bool isl_map_has_dim_name(__isl_keep isl_map *map,
Tobias Grosser52a25232015-02-04 20:55:43 +0000666 enum isl_dim_type type, unsigned pos)
667{
Tobias Grosserb2f39922015-05-28 13:32:11 +0000668 if (!map)
669 return isl_bool_error;
670 return isl_space_has_dim_name(map->dim, type, pos);
Tobias Grosser52a25232015-02-04 20:55:43 +0000671}
672
673const char *isl_map_get_dim_name(__isl_keep isl_map *map,
674 enum isl_dim_type type, unsigned pos)
675{
676 return map ? isl_space_get_dim_name(map->dim, type, pos) : NULL;
677}
678
679const char *isl_set_get_dim_name(__isl_keep isl_set *set,
680 enum isl_dim_type type, unsigned pos)
681{
682 return set ? isl_space_get_dim_name(set->dim, type, pos) : NULL;
683}
684
685/* Does the given dimension have a name?
686 */
Tobias Grosserb2f39922015-05-28 13:32:11 +0000687isl_bool isl_set_has_dim_name(__isl_keep isl_set *set,
Tobias Grosser52a25232015-02-04 20:55:43 +0000688 enum isl_dim_type type, unsigned pos)
689{
Tobias Grosserb2f39922015-05-28 13:32:11 +0000690 if (!set)
691 return isl_bool_error;
692 return isl_space_has_dim_name(set->dim, type, pos);
Tobias Grosser52a25232015-02-04 20:55:43 +0000693}
694
695__isl_give isl_basic_map *isl_basic_map_set_dim_name(
696 __isl_take isl_basic_map *bmap,
697 enum isl_dim_type type, unsigned pos, const char *s)
698{
699 bmap = isl_basic_map_cow(bmap);
700 if (!bmap)
701 return NULL;
702 bmap->dim = isl_space_set_dim_name(bmap->dim, type, pos, s);
703 if (!bmap->dim)
704 goto error;
705 return isl_basic_map_finalize(bmap);
706error:
707 isl_basic_map_free(bmap);
708 return NULL;
709}
710
711__isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
712 enum isl_dim_type type, unsigned pos, const char *s)
713{
714 int i;
715
716 map = isl_map_cow(map);
717 if (!map)
718 return NULL;
719
720 map->dim = isl_space_set_dim_name(map->dim, type, pos, s);
721 if (!map->dim)
722 goto error;
723
724 for (i = 0; i < map->n; ++i) {
725 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
726 if (!map->p[i])
727 goto error;
728 }
729
730 return map;
731error:
732 isl_map_free(map);
733 return NULL;
734}
735
736__isl_give isl_basic_set *isl_basic_set_set_dim_name(
737 __isl_take isl_basic_set *bset,
738 enum isl_dim_type type, unsigned pos, const char *s)
739{
Tobias Grosser06e15922016-11-16 11:06:47 +0000740 return bset_from_bmap(isl_basic_map_set_dim_name(bset_to_bmap(bset),
741 type, pos, s));
Tobias Grosser52a25232015-02-04 20:55:43 +0000742}
743
744__isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
745 enum isl_dim_type type, unsigned pos, const char *s)
746{
Tobias Grosser06e15922016-11-16 11:06:47 +0000747 return set_from_map(isl_map_set_dim_name(set_to_map(set),
748 type, pos, s));
Tobias Grosser52a25232015-02-04 20:55:43 +0000749}
750
Tobias Grosserb2f39922015-05-28 13:32:11 +0000751isl_bool isl_basic_map_has_dim_id(__isl_keep isl_basic_map *bmap,
Tobias Grosser52a25232015-02-04 20:55:43 +0000752 enum isl_dim_type type, unsigned pos)
753{
Tobias Grosserb2f39922015-05-28 13:32:11 +0000754 if (!bmap)
755 return isl_bool_error;
756 return isl_space_has_dim_id(bmap->dim, type, pos);
Tobias Grosser52a25232015-02-04 20:55:43 +0000757}
758
759__isl_give isl_id *isl_basic_set_get_dim_id(__isl_keep isl_basic_set *bset,
760 enum isl_dim_type type, unsigned pos)
761{
762 return bset ? isl_space_get_dim_id(bset->dim, type, pos) : NULL;
763}
764
Tobias Grosserb2f39922015-05-28 13:32:11 +0000765isl_bool isl_map_has_dim_id(__isl_keep isl_map *map,
Tobias Grosser52a25232015-02-04 20:55:43 +0000766 enum isl_dim_type type, unsigned pos)
767{
Tobias Grosserb2f39922015-05-28 13:32:11 +0000768 return map ? isl_space_has_dim_id(map->dim, type, pos) : isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +0000769}
770
771__isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
772 enum isl_dim_type type, unsigned pos)
773{
774 return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL;
775}
776
Tobias Grosserb2f39922015-05-28 13:32:11 +0000777isl_bool isl_set_has_dim_id(__isl_keep isl_set *set,
Tobias Grosser52a25232015-02-04 20:55:43 +0000778 enum isl_dim_type type, unsigned pos)
779{
780 return isl_map_has_dim_id(set, type, pos);
781}
782
783__isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set,
784 enum isl_dim_type type, unsigned pos)
785{
786 return isl_map_get_dim_id(set, type, pos);
787}
788
789__isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
790 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
791{
792 map = isl_map_cow(map);
793 if (!map)
794 goto error;
795
796 map->dim = isl_space_set_dim_id(map->dim, type, pos, id);
797
798 return isl_map_reset_space(map, isl_space_copy(map->dim));
799error:
800 isl_id_free(id);
801 return NULL;
802}
803
804__isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set,
805 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
806{
807 return isl_map_set_dim_id(set, type, pos, id);
808}
809
810int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
811 __isl_keep isl_id *id)
812{
813 if (!map)
814 return -1;
815 return isl_space_find_dim_by_id(map->dim, type, id);
816}
817
818int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type,
819 __isl_keep isl_id *id)
820{
821 return isl_map_find_dim_by_id(set, type, id);
822}
823
824/* Return the position of the dimension of the given type and name
825 * in "bmap".
826 * Return -1 if no such dimension can be found.
827 */
828int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map *bmap,
829 enum isl_dim_type type, const char *name)
830{
831 if (!bmap)
832 return -1;
833 return isl_space_find_dim_by_name(bmap->dim, type, name);
834}
835
836int isl_map_find_dim_by_name(__isl_keep isl_map *map, enum isl_dim_type type,
837 const char *name)
838{
839 if (!map)
840 return -1;
841 return isl_space_find_dim_by_name(map->dim, type, name);
842}
843
844int isl_set_find_dim_by_name(__isl_keep isl_set *set, enum isl_dim_type type,
845 const char *name)
846{
847 return isl_map_find_dim_by_name(set, type, name);
848}
849
Tobias Grosser0d622a42017-04-06 03:41:47 +0000850/* Check whether equality i of bset is a pure stride constraint
851 * on a single dimension, i.e., of the form
852 *
853 * v = k e
854 *
855 * with k a constant and e an existentially quantified variable.
856 */
857isl_bool isl_basic_set_eq_is_stride(__isl_keep isl_basic_set *bset, int i)
858{
859 unsigned nparam;
860 unsigned d;
861 unsigned n_div;
862 int pos1;
863 int pos2;
864
865 if (!bset)
866 return isl_bool_error;
867
868 if (!isl_int_is_zero(bset->eq[i][0]))
869 return isl_bool_false;
870
871 nparam = isl_basic_set_dim(bset, isl_dim_param);
872 d = isl_basic_set_dim(bset, isl_dim_set);
873 n_div = isl_basic_set_dim(bset, isl_dim_div);
874
875 if (isl_seq_first_non_zero(bset->eq[i] + 1, nparam) != -1)
876 return isl_bool_false;
877 pos1 = isl_seq_first_non_zero(bset->eq[i] + 1 + nparam, d);
878 if (pos1 == -1)
879 return isl_bool_false;
880 if (isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + pos1 + 1,
881 d - pos1 - 1) != -1)
882 return isl_bool_false;
883
884 pos2 = isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + d, n_div);
885 if (pos2 == -1)
886 return isl_bool_false;
887 if (isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + d + pos2 + 1,
888 n_div - pos2 - 1) != -1)
889 return isl_bool_false;
890 if (!isl_int_is_one(bset->eq[i][1 + nparam + pos1]) &&
891 !isl_int_is_negone(bset->eq[i][1 + nparam + pos1]))
892 return isl_bool_false;
893
894 return isl_bool_true;
895}
896
Tobias Grosser52a25232015-02-04 20:55:43 +0000897/* Reset the user pointer on all identifiers of parameters and tuples
898 * of the space of "map".
899 */
900__isl_give isl_map *isl_map_reset_user(__isl_take isl_map *map)
901{
902 isl_space *space;
903
904 space = isl_map_get_space(map);
905 space = isl_space_reset_user(space);
906 map = isl_map_reset_space(map, space);
907
908 return map;
909}
910
911/* Reset the user pointer on all identifiers of parameters and tuples
912 * of the space of "set".
913 */
914__isl_give isl_set *isl_set_reset_user(__isl_take isl_set *set)
915{
916 return isl_map_reset_user(set);
917}
918
Tobias Grosser72745c22017-02-17 05:11:16 +0000919isl_bool isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
Tobias Grosser52a25232015-02-04 20:55:43 +0000920{
921 if (!bmap)
Tobias Grosser72745c22017-02-17 05:11:16 +0000922 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +0000923 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
924}
925
Tobias Grosser5652c9c2016-10-01 19:46:51 +0000926/* Has "map" been marked as a rational map?
927 * In particular, have all basic maps in "map" been marked this way?
928 * An empty map is not considered to be rational.
929 * Maps where only some of the basic maps are marked rational
930 * are not allowed.
931 */
932isl_bool isl_map_is_rational(__isl_keep isl_map *map)
933{
934 int i;
935 isl_bool rational;
936
937 if (!map)
938 return isl_bool_error;
939 if (map->n == 0)
940 return isl_bool_false;
941 rational = isl_basic_map_is_rational(map->p[0]);
942 if (rational < 0)
943 return rational;
944 for (i = 1; i < map->n; ++i) {
945 isl_bool rational_i;
946
947 rational_i = isl_basic_map_is_rational(map->p[i]);
948 if (rational_i < 0)
Tobias Grosser72745c22017-02-17 05:11:16 +0000949 return rational_i;
Tobias Grosser5652c9c2016-10-01 19:46:51 +0000950 if (rational != rational_i)
951 isl_die(isl_map_get_ctx(map), isl_error_unsupported,
952 "mixed rational and integer basic maps "
953 "not supported", return isl_bool_error);
954 }
955
956 return rational;
957}
958
959/* Has "set" been marked as a rational set?
960 * In particular, have all basic set in "set" been marked this way?
961 * An empty set is not considered to be rational.
962 * Sets where only some of the basic sets are marked rational
963 * are not allowed.
964 */
965isl_bool isl_set_is_rational(__isl_keep isl_set *set)
966{
967 return isl_map_is_rational(set);
968}
969
Tobias Grosser52a25232015-02-04 20:55:43 +0000970int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
971{
972 return isl_basic_map_is_rational(bset);
973}
974
975/* Does "bmap" contain any rational points?
976 *
977 * If "bmap" has an equality for each dimension, equating the dimension
978 * to an integer constant, then it has no rational points, even if it
979 * is marked as rational.
980 */
Tobias Grosser72745c22017-02-17 05:11:16 +0000981isl_bool isl_basic_map_has_rational(__isl_keep isl_basic_map *bmap)
Tobias Grosser52a25232015-02-04 20:55:43 +0000982{
Tobias Grosser72745c22017-02-17 05:11:16 +0000983 isl_bool has_rational = isl_bool_true;
Tobias Grosser52a25232015-02-04 20:55:43 +0000984 unsigned total;
985
986 if (!bmap)
Tobias Grosser72745c22017-02-17 05:11:16 +0000987 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +0000988 if (isl_basic_map_plain_is_empty(bmap))
Tobias Grosser72745c22017-02-17 05:11:16 +0000989 return isl_bool_false;
Tobias Grosser52a25232015-02-04 20:55:43 +0000990 if (!isl_basic_map_is_rational(bmap))
Tobias Grosser72745c22017-02-17 05:11:16 +0000991 return isl_bool_false;
Tobias Grosser52a25232015-02-04 20:55:43 +0000992 bmap = isl_basic_map_copy(bmap);
993 bmap = isl_basic_map_implicit_equalities(bmap);
994 if (!bmap)
Tobias Grosser72745c22017-02-17 05:11:16 +0000995 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +0000996 total = isl_basic_map_total_dim(bmap);
997 if (bmap->n_eq == total) {
998 int i, j;
999 for (i = 0; i < bmap->n_eq; ++i) {
1000 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
1001 if (j < 0)
1002 break;
1003 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
1004 !isl_int_is_negone(bmap->eq[i][1 + j]))
1005 break;
1006 j = isl_seq_first_non_zero(bmap->eq[i] + 1 + j + 1,
1007 total - j - 1);
1008 if (j >= 0)
1009 break;
1010 }
1011 if (i == bmap->n_eq)
Tobias Grosser72745c22017-02-17 05:11:16 +00001012 has_rational = isl_bool_false;
Tobias Grosser52a25232015-02-04 20:55:43 +00001013 }
1014 isl_basic_map_free(bmap);
1015
1016 return has_rational;
1017}
1018
1019/* Does "map" contain any rational points?
1020 */
Tobias Grosser72745c22017-02-17 05:11:16 +00001021isl_bool isl_map_has_rational(__isl_keep isl_map *map)
Tobias Grosser52a25232015-02-04 20:55:43 +00001022{
1023 int i;
Tobias Grosser72745c22017-02-17 05:11:16 +00001024 isl_bool has_rational;
Tobias Grosser52a25232015-02-04 20:55:43 +00001025
1026 if (!map)
Tobias Grosser72745c22017-02-17 05:11:16 +00001027 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00001028 for (i = 0; i < map->n; ++i) {
1029 has_rational = isl_basic_map_has_rational(map->p[i]);
Tobias Grosser72745c22017-02-17 05:11:16 +00001030 if (has_rational < 0 || has_rational)
1031 return has_rational;
Tobias Grosser52a25232015-02-04 20:55:43 +00001032 }
Tobias Grosser72745c22017-02-17 05:11:16 +00001033 return isl_bool_false;
Tobias Grosser52a25232015-02-04 20:55:43 +00001034}
1035
1036/* Does "set" contain any rational points?
1037 */
Tobias Grosser72745c22017-02-17 05:11:16 +00001038isl_bool isl_set_has_rational(__isl_keep isl_set *set)
Tobias Grosser52a25232015-02-04 20:55:43 +00001039{
1040 return isl_map_has_rational(set);
1041}
1042
1043/* Is this basic set a parameter domain?
1044 */
Tobias Grosser72745c22017-02-17 05:11:16 +00001045isl_bool isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
Tobias Grosser52a25232015-02-04 20:55:43 +00001046{
1047 if (!bset)
Tobias Grosser72745c22017-02-17 05:11:16 +00001048 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00001049 return isl_space_is_params(bset->dim);
1050}
1051
1052/* Is this set a parameter domain?
1053 */
Tobias Grosserb2f39922015-05-28 13:32:11 +00001054isl_bool isl_set_is_params(__isl_keep isl_set *set)
Tobias Grosser52a25232015-02-04 20:55:43 +00001055{
1056 if (!set)
Tobias Grosserb2f39922015-05-28 13:32:11 +00001057 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00001058 return isl_space_is_params(set->dim);
1059}
1060
1061/* Is this map actually a parameter domain?
1062 * Users should never call this function. Outside of isl,
1063 * a map can never be a parameter domain.
1064 */
Tobias Grosser72745c22017-02-17 05:11:16 +00001065isl_bool isl_map_is_params(__isl_keep isl_map *map)
Tobias Grosser52a25232015-02-04 20:55:43 +00001066{
1067 if (!map)
Tobias Grosser72745c22017-02-17 05:11:16 +00001068 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00001069 return isl_space_is_params(map->dim);
1070}
1071
1072static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
1073 struct isl_basic_map *bmap, unsigned extra,
1074 unsigned n_eq, unsigned n_ineq)
1075{
1076 int i;
1077 size_t row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + extra;
1078
1079 bmap->ctx = ctx;
1080 isl_ctx_ref(ctx);
1081
1082 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
1083 if (isl_blk_is_error(bmap->block))
1084 goto error;
1085
1086 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
1087 if ((n_ineq + n_eq) && !bmap->ineq)
1088 goto error;
1089
1090 if (extra == 0) {
1091 bmap->block2 = isl_blk_empty();
1092 bmap->div = NULL;
1093 } else {
1094 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
1095 if (isl_blk_is_error(bmap->block2))
1096 goto error;
1097
1098 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
1099 if (!bmap->div)
1100 goto error;
1101 }
1102
1103 for (i = 0; i < n_ineq + n_eq; ++i)
1104 bmap->ineq[i] = bmap->block.data + i * row_size;
1105
1106 for (i = 0; i < extra; ++i)
1107 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
1108
1109 bmap->ref = 1;
1110 bmap->flags = 0;
1111 bmap->c_size = n_eq + n_ineq;
1112 bmap->eq = bmap->ineq + n_ineq;
1113 bmap->extra = extra;
1114 bmap->n_eq = 0;
1115 bmap->n_ineq = 0;
1116 bmap->n_div = 0;
1117 bmap->sample = NULL;
1118
1119 return bmap;
1120error:
1121 isl_basic_map_free(bmap);
1122 return NULL;
1123}
1124
1125struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
1126 unsigned nparam, unsigned dim, unsigned extra,
1127 unsigned n_eq, unsigned n_ineq)
1128{
1129 struct isl_basic_map *bmap;
1130 isl_space *space;
1131
1132 space = isl_space_set_alloc(ctx, nparam, dim);
1133 if (!space)
1134 return NULL;
1135
1136 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
Tobias Grosser06e15922016-11-16 11:06:47 +00001137 return bset_from_bmap(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +00001138}
1139
1140struct isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *dim,
1141 unsigned extra, unsigned n_eq, unsigned n_ineq)
1142{
1143 struct isl_basic_map *bmap;
1144 if (!dim)
1145 return NULL;
1146 isl_assert(dim->ctx, dim->n_in == 0, goto error);
1147 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
Tobias Grosser06e15922016-11-16 11:06:47 +00001148 return bset_from_bmap(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +00001149error:
1150 isl_space_free(dim);
1151 return NULL;
1152}
1153
1154struct isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *dim,
1155 unsigned extra, unsigned n_eq, unsigned n_ineq)
1156{
1157 struct isl_basic_map *bmap;
1158
1159 if (!dim)
1160 return NULL;
1161 bmap = isl_calloc_type(dim->ctx, struct isl_basic_map);
1162 if (!bmap)
1163 goto error;
1164 bmap->dim = dim;
1165
1166 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
1167error:
1168 isl_space_free(dim);
1169 return NULL;
1170}
1171
1172struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
1173 unsigned nparam, unsigned in, unsigned out, unsigned extra,
1174 unsigned n_eq, unsigned n_ineq)
1175{
1176 struct isl_basic_map *bmap;
1177 isl_space *dim;
1178
1179 dim = isl_space_alloc(ctx, nparam, in, out);
1180 if (!dim)
1181 return NULL;
1182
1183 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1184 return bmap;
1185}
1186
1187static void dup_constraints(
1188 struct isl_basic_map *dst, struct isl_basic_map *src)
1189{
1190 int i;
1191 unsigned total = isl_basic_map_total_dim(src);
1192
1193 for (i = 0; i < src->n_eq; ++i) {
1194 int j = isl_basic_map_alloc_equality(dst);
1195 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
1196 }
1197
1198 for (i = 0; i < src->n_ineq; ++i) {
1199 int j = isl_basic_map_alloc_inequality(dst);
1200 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
1201 }
1202
1203 for (i = 0; i < src->n_div; ++i) {
1204 int j = isl_basic_map_alloc_div(dst);
1205 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
1206 }
1207 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
1208}
1209
1210struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
1211{
1212 struct isl_basic_map *dup;
1213
1214 if (!bmap)
1215 return NULL;
1216 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
1217 bmap->n_div, bmap->n_eq, bmap->n_ineq);
1218 if (!dup)
1219 return NULL;
1220 dup_constraints(dup, bmap);
1221 dup->flags = bmap->flags;
1222 dup->sample = isl_vec_copy(bmap->sample);
1223 return dup;
1224}
1225
1226struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
1227{
1228 struct isl_basic_map *dup;
1229
Tobias Grosser06e15922016-11-16 11:06:47 +00001230 dup = isl_basic_map_dup(bset_to_bmap(bset));
1231 return bset_from_bmap(dup);
Tobias Grosser52a25232015-02-04 20:55:43 +00001232}
1233
1234struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
1235{
1236 if (!bset)
1237 return NULL;
1238
1239 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
1240 bset->ref++;
1241 return bset;
1242 }
1243 return isl_basic_set_dup(bset);
1244}
1245
1246struct isl_set *isl_set_copy(struct isl_set *set)
1247{
1248 if (!set)
1249 return NULL;
1250
1251 set->ref++;
1252 return set;
1253}
1254
1255struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
1256{
1257 if (!bmap)
1258 return NULL;
1259
1260 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
1261 bmap->ref++;
1262 return bmap;
1263 }
1264 bmap = isl_basic_map_dup(bmap);
1265 if (bmap)
1266 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1267 return bmap;
1268}
1269
1270struct isl_map *isl_map_copy(struct isl_map *map)
1271{
1272 if (!map)
1273 return NULL;
1274
1275 map->ref++;
1276 return map;
1277}
1278
1279__isl_null isl_basic_map *isl_basic_map_free(__isl_take isl_basic_map *bmap)
1280{
1281 if (!bmap)
1282 return NULL;
1283
1284 if (--bmap->ref > 0)
1285 return NULL;
1286
1287 isl_ctx_deref(bmap->ctx);
1288 free(bmap->div);
1289 isl_blk_free(bmap->ctx, bmap->block2);
1290 free(bmap->ineq);
1291 isl_blk_free(bmap->ctx, bmap->block);
1292 isl_vec_free(bmap->sample);
1293 isl_space_free(bmap->dim);
1294 free(bmap);
1295
1296 return NULL;
1297}
1298
1299__isl_null isl_basic_set *isl_basic_set_free(__isl_take isl_basic_set *bset)
1300{
Tobias Grosser06e15922016-11-16 11:06:47 +00001301 return isl_basic_map_free(bset_to_bmap(bset));
Tobias Grosser52a25232015-02-04 20:55:43 +00001302}
1303
1304static int room_for_con(struct isl_basic_map *bmap, unsigned n)
1305{
1306 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
1307}
1308
Tobias Grossere5671e52017-03-10 09:17:55 +00001309/* Check that "map" has only named parameters, reporting an error
1310 * if it does not.
1311 */
1312isl_stat isl_map_check_named_params(__isl_keep isl_map *map)
1313{
1314 return isl_space_check_named_params(isl_map_peek_space(map));
1315}
1316
1317/* Check that "bmap1" and "bmap2" have the same parameters,
1318 * reporting an error if they do not.
1319 */
1320static isl_stat isl_basic_map_check_equal_params(
1321 __isl_keep isl_basic_map *bmap1, __isl_keep isl_basic_map *bmap2)
1322{
1323 isl_bool match;
1324
1325 match = isl_basic_map_has_equal_params(bmap1, bmap2);
1326 if (match < 0)
1327 return isl_stat_error;
1328 if (!match)
1329 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
1330 "parameters don't match", return isl_stat_error);
1331 return isl_stat_ok;
1332}
1333
Tobias Grosser52a25232015-02-04 20:55:43 +00001334__isl_give isl_map *isl_map_align_params_map_map_and(
1335 __isl_take isl_map *map1, __isl_take isl_map *map2,
1336 __isl_give isl_map *(*fn)(__isl_take isl_map *map1,
1337 __isl_take isl_map *map2))
1338{
1339 if (!map1 || !map2)
1340 goto error;
Tobias Grossere5671e52017-03-10 09:17:55 +00001341 if (isl_map_has_equal_params(map1, map2))
Tobias Grosser52a25232015-02-04 20:55:43 +00001342 return fn(map1, map2);
Tobias Grossere5671e52017-03-10 09:17:55 +00001343 if (isl_map_check_named_params(map1) < 0)
1344 goto error;
1345 if (isl_map_check_named_params(map2) < 0)
1346 goto error;
Tobias Grosser52a25232015-02-04 20:55:43 +00001347 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1348 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1349 return fn(map1, map2);
1350error:
1351 isl_map_free(map1);
1352 isl_map_free(map2);
1353 return NULL;
1354}
1355
Tobias Grosserb2f39922015-05-28 13:32:11 +00001356isl_bool isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1,
Tobias Grosser52a25232015-02-04 20:55:43 +00001357 __isl_keep isl_map *map2,
Tobias Grosserb2f39922015-05-28 13:32:11 +00001358 isl_bool (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
Tobias Grosser52a25232015-02-04 20:55:43 +00001359{
Tobias Grosserb2f39922015-05-28 13:32:11 +00001360 isl_bool r;
Tobias Grosser52a25232015-02-04 20:55:43 +00001361
1362 if (!map1 || !map2)
Tobias Grosserb2f39922015-05-28 13:32:11 +00001363 return isl_bool_error;
Tobias Grossere5671e52017-03-10 09:17:55 +00001364 if (isl_map_has_equal_params(map1, map2))
Tobias Grosser52a25232015-02-04 20:55:43 +00001365 return fn(map1, map2);
Tobias Grossere5671e52017-03-10 09:17:55 +00001366 if (isl_map_check_named_params(map1) < 0)
1367 return isl_bool_error;
1368 if (isl_map_check_named_params(map2) < 0)
1369 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00001370 map1 = isl_map_copy(map1);
1371 map2 = isl_map_copy(map2);
1372 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1373 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1374 r = fn(map1, map2);
1375 isl_map_free(map1);
1376 isl_map_free(map2);
1377 return r;
1378}
1379
1380int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
1381{
1382 struct isl_ctx *ctx;
1383 if (!bmap)
1384 return -1;
1385 ctx = bmap->ctx;
1386 isl_assert(ctx, room_for_con(bmap, 1), return -1);
1387 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
1388 return -1);
1389 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1390 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1391 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1392 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1393 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1394 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
1395 isl_int *t;
1396 int j = isl_basic_map_alloc_inequality(bmap);
1397 if (j < 0)
1398 return -1;
1399 t = bmap->ineq[j];
1400 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
1401 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1402 bmap->eq[-1] = t;
1403 bmap->n_eq++;
1404 bmap->n_ineq--;
1405 bmap->eq--;
1406 return 0;
1407 }
1408 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
1409 bmap->extra - bmap->n_div);
1410 return bmap->n_eq++;
1411}
1412
1413int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
1414{
Tobias Grosser06e15922016-11-16 11:06:47 +00001415 return isl_basic_map_alloc_equality(bset_to_bmap(bset));
Tobias Grosser52a25232015-02-04 20:55:43 +00001416}
1417
1418int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
1419{
1420 if (!bmap)
1421 return -1;
1422 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
1423 bmap->n_eq -= n;
1424 return 0;
1425}
1426
1427int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
1428{
Tobias Grosser06e15922016-11-16 11:06:47 +00001429 return isl_basic_map_free_equality(bset_to_bmap(bset), n);
Tobias Grosser52a25232015-02-04 20:55:43 +00001430}
1431
1432int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
1433{
1434 isl_int *t;
1435 if (!bmap)
1436 return -1;
1437 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
1438
1439 if (pos != bmap->n_eq - 1) {
1440 t = bmap->eq[pos];
1441 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
1442 bmap->eq[bmap->n_eq - 1] = t;
1443 }
1444 bmap->n_eq--;
1445 return 0;
1446}
1447
Tobias Grosser52a25232015-02-04 20:55:43 +00001448/* Turn inequality "pos" of "bmap" into an equality.
1449 *
1450 * In particular, we move the inequality in front of the equalities
1451 * and move the last inequality in the position of the moved inequality.
1452 * Note that isl_tab_make_equalities_explicit depends on this particular
1453 * change in the ordering of the constraints.
1454 */
1455void isl_basic_map_inequality_to_equality(
1456 struct isl_basic_map *bmap, unsigned pos)
1457{
1458 isl_int *t;
1459
1460 t = bmap->ineq[pos];
1461 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1462 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1463 bmap->eq[-1] = t;
1464 bmap->n_eq++;
1465 bmap->n_ineq--;
1466 bmap->eq--;
1467 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1468 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1469 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1470 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1471}
1472
1473static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
1474{
1475 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1476}
1477
1478int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
1479{
1480 struct isl_ctx *ctx;
1481 if (!bmap)
1482 return -1;
1483 ctx = bmap->ctx;
1484 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1485 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1486 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1487 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1488 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1489 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
1490 1 + isl_basic_map_total_dim(bmap),
1491 bmap->extra - bmap->n_div);
1492 return bmap->n_ineq++;
1493}
1494
1495int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
1496{
Tobias Grosser06e15922016-11-16 11:06:47 +00001497 return isl_basic_map_alloc_inequality(bset_to_bmap(bset));
Tobias Grosser52a25232015-02-04 20:55:43 +00001498}
1499
1500int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
1501{
1502 if (!bmap)
1503 return -1;
1504 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
1505 bmap->n_ineq -= n;
1506 return 0;
1507}
1508
1509int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
1510{
Tobias Grosser06e15922016-11-16 11:06:47 +00001511 return isl_basic_map_free_inequality(bset_to_bmap(bset), n);
Tobias Grosser52a25232015-02-04 20:55:43 +00001512}
1513
1514int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
1515{
1516 isl_int *t;
1517 if (!bmap)
1518 return -1;
1519 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1520
1521 if (pos != bmap->n_ineq - 1) {
1522 t = bmap->ineq[pos];
1523 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1524 bmap->ineq[bmap->n_ineq - 1] = t;
1525 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1526 }
1527 bmap->n_ineq--;
1528 return 0;
1529}
1530
1531int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
1532{
Tobias Grosser06e15922016-11-16 11:06:47 +00001533 return isl_basic_map_drop_inequality(bset_to_bmap(bset), pos);
Tobias Grosser52a25232015-02-04 20:55:43 +00001534}
1535
1536__isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1537 isl_int *eq)
1538{
1539 int k;
1540
1541 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1542 if (!bmap)
1543 return NULL;
1544 k = isl_basic_map_alloc_equality(bmap);
1545 if (k < 0)
1546 goto error;
1547 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
1548 return bmap;
1549error:
1550 isl_basic_map_free(bmap);
1551 return NULL;
1552}
1553
1554__isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1555 isl_int *eq)
1556{
Tobias Grosser06e15922016-11-16 11:06:47 +00001557 return bset_from_bmap(isl_basic_map_add_eq(bset_to_bmap(bset), eq));
Tobias Grosser52a25232015-02-04 20:55:43 +00001558}
1559
1560__isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1561 isl_int *ineq)
1562{
1563 int k;
1564
1565 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1566 if (!bmap)
1567 return NULL;
1568 k = isl_basic_map_alloc_inequality(bmap);
1569 if (k < 0)
1570 goto error;
1571 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
1572 return bmap;
1573error:
1574 isl_basic_map_free(bmap);
1575 return NULL;
1576}
1577
1578__isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1579 isl_int *ineq)
1580{
Tobias Grosser06e15922016-11-16 11:06:47 +00001581 return bset_from_bmap(isl_basic_map_add_ineq(bset_to_bmap(bset), ineq));
Tobias Grosser52a25232015-02-04 20:55:43 +00001582}
1583
1584int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
1585{
1586 if (!bmap)
1587 return -1;
1588 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1589 isl_seq_clr(bmap->div[bmap->n_div] +
1590 1 + 1 + isl_basic_map_total_dim(bmap),
1591 bmap->extra - bmap->n_div);
1592 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1593 return bmap->n_div++;
1594}
1595
1596int isl_basic_set_alloc_div(struct isl_basic_set *bset)
1597{
Tobias Grosser06e15922016-11-16 11:06:47 +00001598 return isl_basic_map_alloc_div(bset_to_bmap(bset));
Tobias Grosser52a25232015-02-04 20:55:43 +00001599}
1600
Tobias Grosser94e53712016-12-31 07:46:11 +00001601/* Check that there are "n" dimensions of type "type" starting at "first"
1602 * in "bmap".
1603 */
1604static isl_stat isl_basic_map_check_range(__isl_keep isl_basic_map *bmap,
1605 enum isl_dim_type type, unsigned first, unsigned n)
1606{
1607 unsigned dim;
1608
1609 if (!bmap)
1610 return isl_stat_error;
1611 dim = isl_basic_map_dim(bmap, type);
1612 if (first + n > dim || first + n < first)
1613 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1614 "position or range out of bounds",
1615 return isl_stat_error);
1616 return isl_stat_ok;
1617}
1618
Tobias Grosser9ec4f952016-07-20 16:53:07 +00001619/* Insert an extra integer division, prescribed by "div", to "bmap"
1620 * at (integer division) position "pos".
1621 *
1622 * The integer division is first added at the end and then moved
1623 * into the right position.
1624 */
1625__isl_give isl_basic_map *isl_basic_map_insert_div(
1626 __isl_take isl_basic_map *bmap, int pos, __isl_keep isl_vec *div)
1627{
Tobias Grosser94e53712016-12-31 07:46:11 +00001628 int i, k;
Tobias Grosser9ec4f952016-07-20 16:53:07 +00001629
1630 bmap = isl_basic_map_cow(bmap);
1631 if (!bmap || !div)
1632 return isl_basic_map_free(bmap);
1633
1634 if (div->size != 1 + 1 + isl_basic_map_dim(bmap, isl_dim_all))
1635 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1636 "unexpected size", return isl_basic_map_free(bmap));
Tobias Grosser94e53712016-12-31 07:46:11 +00001637 if (isl_basic_map_check_range(bmap, isl_dim_div, pos, 0) < 0)
1638 return isl_basic_map_free(bmap);
Tobias Grosser9ec4f952016-07-20 16:53:07 +00001639
1640 bmap = isl_basic_map_extend_space(bmap,
1641 isl_basic_map_get_space(bmap), 1, 0, 2);
1642 k = isl_basic_map_alloc_div(bmap);
1643 if (k < 0)
1644 return isl_basic_map_free(bmap);
1645 isl_seq_cpy(bmap->div[k], div->el, div->size);
1646 isl_int_set_si(bmap->div[k][div->size], 0);
1647
1648 for (i = k; i > pos; --i)
1649 isl_basic_map_swap_div(bmap, i, i - 1);
1650
1651 return bmap;
1652}
1653
Tobias Grosser72745c22017-02-17 05:11:16 +00001654isl_stat isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
Tobias Grosser52a25232015-02-04 20:55:43 +00001655{
1656 if (!bmap)
Tobias Grosser72745c22017-02-17 05:11:16 +00001657 return isl_stat_error;
1658 isl_assert(bmap->ctx, n <= bmap->n_div, return isl_stat_error);
Tobias Grosser52a25232015-02-04 20:55:43 +00001659 bmap->n_div -= n;
Tobias Grosser72745c22017-02-17 05:11:16 +00001660 return isl_stat_ok;
Tobias Grosser52a25232015-02-04 20:55:43 +00001661}
1662
Tobias Grosser52a25232015-02-04 20:55:43 +00001663/* Copy constraint from src to dst, putting the vars of src at offset
1664 * dim_off in dst and the divs of src at offset div_off in dst.
1665 * If both sets are actually map, then dim_off applies to the input
1666 * variables.
1667 */
1668static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
1669 struct isl_basic_map *src_map, isl_int *src,
1670 unsigned in_off, unsigned out_off, unsigned div_off)
1671{
Tobias Grosserf4fe34b2017-03-16 21:33:20 +00001672 unsigned src_nparam = isl_basic_map_dim(src_map, isl_dim_param);
1673 unsigned dst_nparam = isl_basic_map_dim(dst_map, isl_dim_param);
1674 unsigned src_in = isl_basic_map_dim(src_map, isl_dim_in);
1675 unsigned dst_in = isl_basic_map_dim(dst_map, isl_dim_in);
1676 unsigned src_out = isl_basic_map_dim(src_map, isl_dim_out);
1677 unsigned dst_out = isl_basic_map_dim(dst_map, isl_dim_out);
Tobias Grosser52a25232015-02-04 20:55:43 +00001678 isl_int_set(dst[0], src[0]);
1679 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
1680 if (dst_nparam > src_nparam)
1681 isl_seq_clr(dst+1+src_nparam,
1682 dst_nparam - src_nparam);
1683 isl_seq_clr(dst+1+dst_nparam, in_off);
1684 isl_seq_cpy(dst+1+dst_nparam+in_off,
1685 src+1+src_nparam,
1686 isl_min(dst_in-in_off, src_in));
1687 if (dst_in-in_off > src_in)
1688 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
1689 dst_in - in_off - src_in);
1690 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
1691 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
1692 src+1+src_nparam+src_in,
1693 isl_min(dst_out-out_off, src_out));
1694 if (dst_out-out_off > src_out)
1695 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
1696 dst_out - out_off - src_out);
1697 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
1698 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
1699 src+1+src_nparam+src_in+src_out,
1700 isl_min(dst_map->extra-div_off, src_map->n_div));
1701 if (dst_map->n_div-div_off > src_map->n_div)
1702 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
1703 div_off+src_map->n_div,
1704 dst_map->n_div - div_off - src_map->n_div);
1705}
1706
1707static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
1708 struct isl_basic_map *src_map, isl_int *src,
1709 unsigned in_off, unsigned out_off, unsigned div_off)
1710{
1711 isl_int_set(dst[0], src[0]);
1712 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
1713}
1714
1715static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
1716 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
1717{
1718 int i;
1719 unsigned div_off;
1720
1721 if (!bmap1 || !bmap2)
1722 goto error;
1723
1724 div_off = bmap1->n_div;
1725
1726 for (i = 0; i < bmap2->n_eq; ++i) {
1727 int i1 = isl_basic_map_alloc_equality(bmap1);
1728 if (i1 < 0)
1729 goto error;
1730 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
1731 i_pos, o_pos, div_off);
1732 }
1733
1734 for (i = 0; i < bmap2->n_ineq; ++i) {
1735 int i1 = isl_basic_map_alloc_inequality(bmap1);
1736 if (i1 < 0)
1737 goto error;
1738 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
1739 i_pos, o_pos, div_off);
1740 }
1741
1742 for (i = 0; i < bmap2->n_div; ++i) {
1743 int i1 = isl_basic_map_alloc_div(bmap1);
1744 if (i1 < 0)
1745 goto error;
1746 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
1747 i_pos, o_pos, div_off);
1748 }
1749
1750 isl_basic_map_free(bmap2);
1751
1752 return bmap1;
1753
1754error:
1755 isl_basic_map_free(bmap1);
1756 isl_basic_map_free(bmap2);
1757 return NULL;
1758}
1759
1760struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1761 struct isl_basic_set *bset2, unsigned pos)
1762{
Tobias Grosser06e15922016-11-16 11:06:47 +00001763 return bset_from_bmap(add_constraints(bset_to_bmap(bset1),
1764 bset_to_bmap(bset2), 0, pos));
Tobias Grosser52a25232015-02-04 20:55:43 +00001765}
1766
1767struct isl_basic_map *isl_basic_map_extend_space(struct isl_basic_map *base,
1768 __isl_take isl_space *dim, unsigned extra,
1769 unsigned n_eq, unsigned n_ineq)
1770{
1771 struct isl_basic_map *ext;
1772 unsigned flags;
1773 int dims_ok;
1774
1775 if (!dim)
1776 goto error;
1777
1778 if (!base)
1779 goto error;
1780
1781 dims_ok = isl_space_is_equal(base->dim, dim) &&
1782 base->extra >= base->n_div + extra;
1783
1784 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1785 room_for_ineq(base, n_ineq)) {
1786 isl_space_free(dim);
1787 return base;
1788 }
1789
1790 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
1791 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
1792 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
1793 extra += base->extra;
1794 n_eq += base->n_eq;
1795 n_ineq += base->n_ineq;
1796
1797 ext = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1798 dim = NULL;
1799 if (!ext)
1800 goto error;
1801
1802 if (dims_ok)
1803 ext->sample = isl_vec_copy(base->sample);
1804 flags = base->flags;
1805 ext = add_constraints(ext, base, 0, 0);
1806 if (ext) {
1807 ext->flags = flags;
1808 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1809 }
1810
1811 return ext;
1812
1813error:
1814 isl_space_free(dim);
1815 isl_basic_map_free(base);
1816 return NULL;
1817}
1818
1819struct isl_basic_set *isl_basic_set_extend_space(struct isl_basic_set *base,
1820 __isl_take isl_space *dim, unsigned extra,
1821 unsigned n_eq, unsigned n_ineq)
1822{
Tobias Grosser06e15922016-11-16 11:06:47 +00001823 return bset_from_bmap(isl_basic_map_extend_space(bset_to_bmap(base),
1824 dim, extra, n_eq, n_ineq));
Tobias Grosser52a25232015-02-04 20:55:43 +00001825}
1826
1827struct isl_basic_map *isl_basic_map_extend_constraints(
1828 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1829{
1830 if (!base)
1831 return NULL;
1832 return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1833 0, n_eq, n_ineq);
1834}
1835
1836struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1837 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1838 unsigned n_eq, unsigned n_ineq)
1839{
1840 struct isl_basic_map *bmap;
1841 isl_space *dim;
1842
1843 if (!base)
1844 return NULL;
1845 dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1846 if (!dim)
1847 goto error;
1848
1849 bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1850 return bmap;
1851error:
1852 isl_basic_map_free(base);
1853 return NULL;
1854}
1855
1856struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1857 unsigned nparam, unsigned dim, unsigned extra,
1858 unsigned n_eq, unsigned n_ineq)
1859{
Tobias Grosser06e15922016-11-16 11:06:47 +00001860 return bset_from_bmap(isl_basic_map_extend(bset_to_bmap(base),
1861 nparam, 0, dim, extra, n_eq, n_ineq));
Tobias Grosser52a25232015-02-04 20:55:43 +00001862}
1863
1864struct isl_basic_set *isl_basic_set_extend_constraints(
1865 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1866{
Tobias Grosser06e15922016-11-16 11:06:47 +00001867 isl_basic_map *bmap = bset_to_bmap(base);
1868 bmap = isl_basic_map_extend_constraints(bmap, n_eq, n_ineq);
1869 return bset_from_bmap(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +00001870}
1871
1872struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1873{
Tobias Grosser06e15922016-11-16 11:06:47 +00001874 return bset_from_bmap(isl_basic_map_cow(bset_to_bmap(bset)));
Tobias Grosser52a25232015-02-04 20:55:43 +00001875}
1876
1877struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1878{
1879 if (!bmap)
1880 return NULL;
1881
1882 if (bmap->ref > 1) {
1883 bmap->ref--;
1884 bmap = isl_basic_map_dup(bmap);
1885 }
Tobias Grosser1fa7b972015-02-16 19:33:40 +00001886 if (bmap) {
Tobias Grosser52a25232015-02-04 20:55:43 +00001887 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
Tobias Grosser1fa7b972015-02-16 19:33:40 +00001888 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
1889 }
Tobias Grosser52a25232015-02-04 20:55:43 +00001890 return bmap;
1891}
1892
Tobias Grosser07b20952016-06-12 04:30:40 +00001893/* Clear all cached information in "map", either because it is about
1894 * to be modified or because it is being freed.
1895 * Always return the same pointer that is passed in.
1896 * This is needed for the use in isl_map_free.
1897 */
1898static __isl_give isl_map *clear_caches(__isl_take isl_map *map)
Tobias Grosser52a25232015-02-04 20:55:43 +00001899{
Tobias Grosser07b20952016-06-12 04:30:40 +00001900 isl_basic_map_free(map->cached_simple_hull[0]);
1901 isl_basic_map_free(map->cached_simple_hull[1]);
1902 map->cached_simple_hull[0] = NULL;
1903 map->cached_simple_hull[1] = NULL;
1904 return map;
Tobias Grosser52a25232015-02-04 20:55:43 +00001905}
1906
Tobias Grosser07b20952016-06-12 04:30:40 +00001907struct isl_set *isl_set_cow(struct isl_set *set)
1908{
1909 return isl_map_cow(set);
1910}
1911
1912/* Return an isl_map that is equal to "map" and that has only
1913 * a single reference.
1914 *
1915 * If the original input already has only one reference, then
1916 * simply return it, but clear all cached information, since
1917 * it may be rendered invalid by the operations that will be
1918 * performed on the result.
1919 *
1920 * Otherwise, create a duplicate (without any cached information).
1921 */
Tobias Grosser52a25232015-02-04 20:55:43 +00001922struct isl_map *isl_map_cow(struct isl_map *map)
1923{
1924 if (!map)
1925 return NULL;
1926
1927 if (map->ref == 1)
Tobias Grosser07b20952016-06-12 04:30:40 +00001928 return clear_caches(map);
Tobias Grosser52a25232015-02-04 20:55:43 +00001929 map->ref--;
1930 return isl_map_dup(map);
1931}
1932
1933static void swap_vars(struct isl_blk blk, isl_int *a,
1934 unsigned a_len, unsigned b_len)
1935{
1936 isl_seq_cpy(blk.data, a+a_len, b_len);
1937 isl_seq_cpy(blk.data+b_len, a, a_len);
1938 isl_seq_cpy(a, blk.data, b_len+a_len);
1939}
1940
1941static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1942 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1943{
1944 int i;
1945 struct isl_blk blk;
1946
1947 if (!bmap)
1948 goto error;
1949
1950 isl_assert(bmap->ctx,
1951 pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error);
1952
1953 if (n1 == 0 || n2 == 0)
1954 return bmap;
1955
1956 bmap = isl_basic_map_cow(bmap);
1957 if (!bmap)
1958 return NULL;
1959
1960 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1961 if (isl_blk_is_error(blk))
1962 goto error;
1963
1964 for (i = 0; i < bmap->n_eq; ++i)
1965 swap_vars(blk,
1966 bmap->eq[i] + pos, n1, n2);
1967
1968 for (i = 0; i < bmap->n_ineq; ++i)
1969 swap_vars(blk,
1970 bmap->ineq[i] + pos, n1, n2);
1971
1972 for (i = 0; i < bmap->n_div; ++i)
1973 swap_vars(blk,
1974 bmap->div[i]+1 + pos, n1, n2);
1975
1976 isl_blk_free(bmap->ctx, blk);
1977
1978 ISL_F_CLR(bmap, ISL_BASIC_SET_NORMALIZED);
1979 bmap = isl_basic_map_gauss(bmap, NULL);
1980 return isl_basic_map_finalize(bmap);
1981error:
1982 isl_basic_map_free(bmap);
1983 return NULL;
1984}
1985
Tobias Grosser52a25232015-02-04 20:55:43 +00001986struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1987{
1988 int i = 0;
1989 unsigned total;
1990 if (!bmap)
1991 goto error;
1992 total = isl_basic_map_total_dim(bmap);
Tobias Grosser72745c22017-02-17 05:11:16 +00001993 if (isl_basic_map_free_div(bmap, bmap->n_div) < 0)
1994 return isl_basic_map_free(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +00001995 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1996 if (bmap->n_eq > 0)
1997 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1998 else {
1999 i = isl_basic_map_alloc_equality(bmap);
2000 if (i < 0)
2001 goto error;
2002 }
2003 isl_int_set_si(bmap->eq[i][0], 1);
2004 isl_seq_clr(bmap->eq[i]+1, total);
2005 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
2006 isl_vec_free(bmap->sample);
2007 bmap->sample = NULL;
2008 return isl_basic_map_finalize(bmap);
2009error:
2010 isl_basic_map_free(bmap);
2011 return NULL;
2012}
2013
2014struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
2015{
Tobias Grosser06e15922016-11-16 11:06:47 +00002016 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset)));
Tobias Grosser52a25232015-02-04 20:55:43 +00002017}
2018
Tobias Grosser0d622a42017-04-06 03:41:47 +00002019__isl_give isl_basic_map *isl_basic_map_set_rational(
2020 __isl_take isl_basic_map *bmap)
2021{
2022 if (!bmap)
2023 return NULL;
2024
2025 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
2026 return bmap;
2027
2028 bmap = isl_basic_map_cow(bmap);
2029 if (!bmap)
2030 return NULL;
2031
2032 ISL_F_SET(bmap, ISL_BASIC_MAP_RATIONAL);
2033
2034 return isl_basic_map_finalize(bmap);
2035}
2036
2037__isl_give isl_basic_set *isl_basic_set_set_rational(
2038 __isl_take isl_basic_set *bset)
2039{
2040 return isl_basic_map_set_rational(bset);
2041}
2042
2043__isl_give isl_basic_set *isl_basic_set_set_integral(
2044 __isl_take isl_basic_set *bset)
2045{
2046 if (!bset)
2047 return NULL;
2048
2049 if (!ISL_F_ISSET(bset, ISL_BASIC_MAP_RATIONAL))
2050 return bset;
2051
2052 bset = isl_basic_set_cow(bset);
2053 if (!bset)
2054 return NULL;
2055
2056 ISL_F_CLR(bset, ISL_BASIC_MAP_RATIONAL);
2057
2058 return isl_basic_set_finalize(bset);
2059}
2060
2061__isl_give isl_map *isl_map_set_rational(__isl_take isl_map *map)
2062{
2063 int i;
2064
2065 map = isl_map_cow(map);
2066 if (!map)
2067 return NULL;
2068 for (i = 0; i < map->n; ++i) {
2069 map->p[i] = isl_basic_map_set_rational(map->p[i]);
2070 if (!map->p[i])
2071 goto error;
2072 }
2073 return map;
2074error:
2075 isl_map_free(map);
2076 return NULL;
2077}
2078
2079__isl_give isl_set *isl_set_set_rational(__isl_take isl_set *set)
2080{
2081 return isl_map_set_rational(set);
2082}
2083
Tobias Grosser52a25232015-02-04 20:55:43 +00002084/* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
2085 * of "bmap").
2086 */
2087static void swap_div(__isl_keep isl_basic_map *bmap, int a, int b)
2088{
2089 isl_int *t = bmap->div[a];
2090 bmap->div[a] = bmap->div[b];
2091 bmap->div[b] = t;
2092}
2093
2094/* Swap divs "a" and "b" in "bmap" and adjust the constraints and
2095 * div definitions accordingly.
2096 */
2097void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
2098{
2099 int i;
2100 unsigned off = isl_space_dim(bmap->dim, isl_dim_all);
2101
2102 swap_div(bmap, a, b);
2103
2104 for (i = 0; i < bmap->n_eq; ++i)
2105 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
2106
2107 for (i = 0; i < bmap->n_ineq; ++i)
2108 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
2109
2110 for (i = 0; i < bmap->n_div; ++i)
2111 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
2112 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2113}
2114
Tobias Grosser932ec012016-07-06 09:11:00 +00002115/* Swap divs "a" and "b" in "bset" and adjust the constraints and
2116 * div definitions accordingly.
2117 */
2118void isl_basic_set_swap_div(__isl_keep isl_basic_set *bset, int a, int b)
2119{
2120 isl_basic_map_swap_div(bset, a, b);
2121}
2122
Tobias Grosser0d622a42017-04-06 03:41:47 +00002123static void constraint_drop_vars(isl_int *c, unsigned n, unsigned rem)
2124{
2125 isl_seq_cpy(c, c + n, rem);
2126 isl_seq_clr(c + rem, n);
2127}
2128
2129/* Drop n dimensions starting at first.
2130 *
2131 * In principle, this frees up some extra variables as the number
2132 * of columns remains constant, but we would have to extend
2133 * the div array too as the number of rows in this array is assumed
2134 * to be equal to extra.
2135 */
2136struct isl_basic_set *isl_basic_set_drop_dims(
2137 struct isl_basic_set *bset, unsigned first, unsigned n)
2138{
2139 return isl_basic_map_drop(bset_to_bmap(bset), isl_dim_set, first, n);
2140}
2141
2142/* Move "n" divs starting at "first" to the end of the list of divs.
2143 */
2144static struct isl_basic_map *move_divs_last(struct isl_basic_map *bmap,
2145 unsigned first, unsigned n)
2146{
2147 isl_int **div;
2148 int i;
2149
2150 if (first + n == bmap->n_div)
2151 return bmap;
2152
2153 div = isl_alloc_array(bmap->ctx, isl_int *, n);
2154 if (!div)
2155 goto error;
2156 for (i = 0; i < n; ++i)
2157 div[i] = bmap->div[first + i];
2158 for (i = 0; i < bmap->n_div - first - n; ++i)
2159 bmap->div[first + i] = bmap->div[first + n + i];
2160 for (i = 0; i < n; ++i)
2161 bmap->div[bmap->n_div - n + i] = div[i];
2162 free(div);
2163 return bmap;
2164error:
2165 isl_basic_map_free(bmap);
2166 return NULL;
2167}
2168
2169/* Drop "n" dimensions of type "type" starting at "first".
2170 *
2171 * In principle, this frees up some extra variables as the number
2172 * of columns remains constant, but we would have to extend
2173 * the div array too as the number of rows in this array is assumed
2174 * to be equal to extra.
2175 */
2176struct isl_basic_map *isl_basic_map_drop(struct isl_basic_map *bmap,
2177 enum isl_dim_type type, unsigned first, unsigned n)
2178{
2179 int i;
2180 unsigned dim;
2181 unsigned offset;
2182 unsigned left;
2183
2184 if (!bmap)
2185 goto error;
2186
2187 dim = isl_basic_map_dim(bmap, type);
2188 isl_assert(bmap->ctx, first + n <= dim, goto error);
2189
2190 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2191 return bmap;
2192
2193 bmap = isl_basic_map_cow(bmap);
2194 if (!bmap)
2195 return NULL;
2196
2197 offset = isl_basic_map_offset(bmap, type) + first;
2198 left = isl_basic_map_total_dim(bmap) - (offset - 1) - n;
2199 for (i = 0; i < bmap->n_eq; ++i)
2200 constraint_drop_vars(bmap->eq[i]+offset, n, left);
2201
2202 for (i = 0; i < bmap->n_ineq; ++i)
2203 constraint_drop_vars(bmap->ineq[i]+offset, n, left);
2204
2205 for (i = 0; i < bmap->n_div; ++i)
2206 constraint_drop_vars(bmap->div[i]+1+offset, n, left);
2207
2208 if (type == isl_dim_div) {
2209 bmap = move_divs_last(bmap, first, n);
2210 if (!bmap)
2211 goto error;
2212 if (isl_basic_map_free_div(bmap, n) < 0)
2213 return isl_basic_map_free(bmap);
2214 } else
2215 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
2216 if (!bmap->dim)
2217 goto error;
2218
2219 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2220 bmap = isl_basic_map_simplify(bmap);
2221 return isl_basic_map_finalize(bmap);
2222error:
2223 isl_basic_map_free(bmap);
2224 return NULL;
2225}
2226
2227__isl_give isl_basic_set *isl_basic_set_drop(__isl_take isl_basic_set *bset,
2228 enum isl_dim_type type, unsigned first, unsigned n)
2229{
2230 return bset_from_bmap(isl_basic_map_drop(bset_to_bmap(bset),
2231 type, first, n));
2232}
2233
2234struct isl_map *isl_map_drop(struct isl_map *map,
2235 enum isl_dim_type type, unsigned first, unsigned n)
2236{
2237 int i;
2238
2239 if (!map)
2240 goto error;
2241
2242 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2243
2244 if (n == 0 && !isl_space_is_named_or_nested(map->dim, type))
2245 return map;
2246 map = isl_map_cow(map);
2247 if (!map)
2248 goto error;
2249 map->dim = isl_space_drop_dims(map->dim, type, first, n);
2250 if (!map->dim)
2251 goto error;
2252
2253 for (i = 0; i < map->n; ++i) {
2254 map->p[i] = isl_basic_map_drop(map->p[i], type, first, n);
2255 if (!map->p[i])
2256 goto error;
2257 }
2258 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2259
2260 return map;
2261error:
2262 isl_map_free(map);
2263 return NULL;
2264}
2265
2266struct isl_set *isl_set_drop(struct isl_set *set,
2267 enum isl_dim_type type, unsigned first, unsigned n)
2268{
2269 return set_from_map(isl_map_drop(set_to_map(set), type, first, n));
2270}
2271
2272/*
2273 * We don't cow, as the div is assumed to be redundant.
2274 */
2275__isl_give isl_basic_map *isl_basic_map_drop_div(
2276 __isl_take isl_basic_map *bmap, unsigned div)
2277{
2278 int i;
2279 unsigned pos;
2280
2281 if (!bmap)
2282 goto error;
2283
2284 pos = 1 + isl_space_dim(bmap->dim, isl_dim_all) + div;
2285
2286 isl_assert(bmap->ctx, div < bmap->n_div, goto error);
2287
2288 for (i = 0; i < bmap->n_eq; ++i)
2289 constraint_drop_vars(bmap->eq[i]+pos, 1, bmap->extra-div-1);
2290
2291 for (i = 0; i < bmap->n_ineq; ++i) {
2292 if (!isl_int_is_zero(bmap->ineq[i][pos])) {
2293 isl_basic_map_drop_inequality(bmap, i);
2294 --i;
2295 continue;
2296 }
2297 constraint_drop_vars(bmap->ineq[i]+pos, 1, bmap->extra-div-1);
2298 }
2299
2300 for (i = 0; i < bmap->n_div; ++i)
2301 constraint_drop_vars(bmap->div[i]+1+pos, 1, bmap->extra-div-1);
2302
2303 if (div != bmap->n_div - 1) {
2304 int j;
2305 isl_int *t = bmap->div[div];
2306
2307 for (j = div; j < bmap->n_div - 1; ++j)
2308 bmap->div[j] = bmap->div[j+1];
2309
2310 bmap->div[bmap->n_div - 1] = t;
2311 }
2312 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2313 if (isl_basic_map_free_div(bmap, 1) < 0)
2314 return isl_basic_map_free(bmap);
2315
2316 return bmap;
2317error:
2318 isl_basic_map_free(bmap);
2319 return NULL;
2320}
2321
Tobias Grosser52a25232015-02-04 20:55:43 +00002322/* Eliminate the specified n dimensions starting at first from the
2323 * constraints, without removing the dimensions from the space.
2324 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2325 */
2326__isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
2327 enum isl_dim_type type, unsigned first, unsigned n)
2328{
2329 int i;
2330
2331 if (!map)
2332 return NULL;
2333 if (n == 0)
2334 return map;
2335
2336 if (first + n > isl_map_dim(map, type) || first + n < first)
2337 isl_die(map->ctx, isl_error_invalid,
2338 "index out of bounds", goto error);
2339
2340 map = isl_map_cow(map);
2341 if (!map)
2342 return NULL;
2343
2344 for (i = 0; i < map->n; ++i) {
2345 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
2346 if (!map->p[i])
2347 goto error;
2348 }
2349 return map;
2350error:
2351 isl_map_free(map);
2352 return NULL;
2353}
2354
2355/* Eliminate the specified n dimensions starting at first from the
2356 * constraints, without removing the dimensions from the space.
2357 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2358 */
2359__isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
2360 enum isl_dim_type type, unsigned first, unsigned n)
2361{
Tobias Grosser06e15922016-11-16 11:06:47 +00002362 return set_from_map(isl_map_eliminate(set_to_map(set), type, first, n));
Tobias Grosser52a25232015-02-04 20:55:43 +00002363}
2364
2365/* Eliminate the specified n dimensions starting at first from the
2366 * constraints, without removing the dimensions from the space.
2367 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2368 */
2369__isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
2370 unsigned first, unsigned n)
2371{
2372 return isl_set_eliminate(set, isl_dim_set, first, n);
2373}
2374
2375__isl_give isl_basic_map *isl_basic_map_remove_divs(
2376 __isl_take isl_basic_map *bmap)
2377{
2378 if (!bmap)
2379 return NULL;
2380 bmap = isl_basic_map_eliminate_vars(bmap,
2381 isl_space_dim(bmap->dim, isl_dim_all), bmap->n_div);
2382 if (!bmap)
2383 return NULL;
2384 bmap->n_div = 0;
2385 return isl_basic_map_finalize(bmap);
2386}
2387
2388__isl_give isl_basic_set *isl_basic_set_remove_divs(
2389 __isl_take isl_basic_set *bset)
2390{
Tobias Grosser06e15922016-11-16 11:06:47 +00002391 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset)));
Tobias Grosser52a25232015-02-04 20:55:43 +00002392}
2393
2394__isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
2395{
2396 int i;
2397
2398 if (!map)
2399 return NULL;
2400 if (map->n == 0)
2401 return map;
2402
2403 map = isl_map_cow(map);
2404 if (!map)
2405 return NULL;
2406
2407 for (i = 0; i < map->n; ++i) {
2408 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
2409 if (!map->p[i])
2410 goto error;
2411 }
2412 return map;
2413error:
2414 isl_map_free(map);
2415 return NULL;
2416}
2417
2418__isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
2419{
2420 return isl_map_remove_divs(set);
2421}
2422
2423struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap,
2424 enum isl_dim_type type, unsigned first, unsigned n)
2425{
Tobias Grosser94e53712016-12-31 07:46:11 +00002426 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2427 return isl_basic_map_free(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +00002428 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2429 return bmap;
2430 bmap = isl_basic_map_eliminate_vars(bmap,
2431 isl_basic_map_offset(bmap, type) - 1 + first, n);
2432 if (!bmap)
2433 return bmap;
2434 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
2435 return bmap;
2436 bmap = isl_basic_map_drop(bmap, type, first, n);
2437 return bmap;
Tobias Grosser52a25232015-02-04 20:55:43 +00002438}
2439
2440/* Return true if the definition of the given div (recursively) involves
2441 * any of the given variables.
2442 */
Tobias Grosser72745c22017-02-17 05:11:16 +00002443static isl_bool div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
Tobias Grosser52a25232015-02-04 20:55:43 +00002444 unsigned first, unsigned n)
2445{
2446 int i;
2447 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2448
2449 if (isl_int_is_zero(bmap->div[div][0]))
Tobias Grosser72745c22017-02-17 05:11:16 +00002450 return isl_bool_false;
Tobias Grosser52a25232015-02-04 20:55:43 +00002451 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
Tobias Grosser72745c22017-02-17 05:11:16 +00002452 return isl_bool_true;
Tobias Grosser52a25232015-02-04 20:55:43 +00002453
2454 for (i = bmap->n_div - 1; i >= 0; --i) {
Tobias Grosser72745c22017-02-17 05:11:16 +00002455 isl_bool involves;
2456
Tobias Grosser52a25232015-02-04 20:55:43 +00002457 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2458 continue;
Tobias Grosser72745c22017-02-17 05:11:16 +00002459 involves = div_involves_vars(bmap, i, first, n);
2460 if (involves < 0 || involves)
2461 return involves;
Tobias Grosser52a25232015-02-04 20:55:43 +00002462 }
2463
Tobias Grosser72745c22017-02-17 05:11:16 +00002464 return isl_bool_false;
Tobias Grosser52a25232015-02-04 20:55:43 +00002465}
2466
2467/* Try and add a lower and/or upper bound on "div" to "bmap"
2468 * based on inequality "i".
2469 * "total" is the total number of variables (excluding the divs).
2470 * "v" is a temporary object that can be used during the calculations.
2471 * If "lb" is set, then a lower bound should be constructed.
2472 * If "ub" is set, then an upper bound should be constructed.
2473 *
2474 * The calling function has already checked that the inequality does not
2475 * reference "div", but we still need to check that the inequality is
2476 * of the right form. We'll consider the case where we want to construct
2477 * a lower bound. The construction of upper bounds is similar.
2478 *
2479 * Let "div" be of the form
2480 *
2481 * q = floor((a + f(x))/d)
2482 *
2483 * We essentially check if constraint "i" is of the form
2484 *
2485 * b + f(x) >= 0
2486 *
2487 * so that we can use it to derive a lower bound on "div".
2488 * However, we allow a slightly more general form
2489 *
2490 * b + g(x) >= 0
2491 *
2492 * with the condition that the coefficients of g(x) - f(x) are all
2493 * divisible by d.
2494 * Rewriting this constraint as
2495 *
2496 * 0 >= -b - g(x)
2497 *
2498 * adding a + f(x) to both sides and dividing by d, we obtain
2499 *
2500 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2501 *
2502 * Taking the floor on both sides, we obtain
2503 *
2504 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2505 *
2506 * or
2507 *
2508 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2509 *
2510 * In the case of an upper bound, we construct the constraint
2511 *
2512 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2513 *
2514 */
2515static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
2516 __isl_take isl_basic_map *bmap, int div, int i,
2517 unsigned total, isl_int v, int lb, int ub)
2518{
2519 int j;
2520
2521 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
2522 if (lb) {
2523 isl_int_sub(v, bmap->ineq[i][1 + j],
2524 bmap->div[div][1 + 1 + j]);
2525 lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
2526 }
2527 if (ub) {
2528 isl_int_add(v, bmap->ineq[i][1 + j],
2529 bmap->div[div][1 + 1 + j]);
2530 ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
2531 }
2532 }
2533 if (!lb && !ub)
2534 return bmap;
2535
2536 bmap = isl_basic_map_cow(bmap);
2537 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
2538 if (lb) {
2539 int k = isl_basic_map_alloc_inequality(bmap);
2540 if (k < 0)
2541 goto error;
2542 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2543 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
2544 bmap->div[div][1 + j]);
2545 isl_int_cdiv_q(bmap->ineq[k][j],
2546 bmap->ineq[k][j], bmap->div[div][0]);
2547 }
2548 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
2549 }
2550 if (ub) {
2551 int k = isl_basic_map_alloc_inequality(bmap);
2552 if (k < 0)
2553 goto error;
2554 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2555 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
2556 bmap->div[div][1 + j]);
2557 isl_int_fdiv_q(bmap->ineq[k][j],
2558 bmap->ineq[k][j], bmap->div[div][0]);
2559 }
2560 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
2561 }
2562
2563 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2564 return bmap;
2565error:
2566 isl_basic_map_free(bmap);
2567 return NULL;
2568}
2569
2570/* This function is called right before "div" is eliminated from "bmap"
2571 * using Fourier-Motzkin.
2572 * Look through the constraints of "bmap" for constraints on the argument
2573 * of the integer division and use them to construct constraints on the
2574 * integer division itself. These constraints can then be combined
2575 * during the Fourier-Motzkin elimination.
2576 * Note that it is only useful to introduce lower bounds on "div"
2577 * if "bmap" already contains upper bounds on "div" as the newly
2578 * introduce lower bounds can then be combined with the pre-existing
2579 * upper bounds. Similarly for upper bounds.
2580 * We therefore first check if "bmap" contains any lower and/or upper bounds
2581 * on "div".
2582 *
2583 * It is interesting to note that the introduction of these constraints
2584 * can indeed lead to more accurate results, even when compared to
2585 * deriving constraints on the argument of "div" from constraints on "div".
2586 * Consider, for example, the set
2587 *
2588 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2589 *
2590 * The second constraint can be rewritten as
2591 *
2592 * 2 * [(-i-2j+3)/4] + k >= 0
2593 *
2594 * from which we can derive
2595 *
2596 * -i - 2j + 3 >= -2k
2597 *
2598 * or
2599 *
2600 * i + 2j <= 3 + 2k
2601 *
2602 * Combined with the first constraint, we obtain
2603 *
2604 * -3 <= 3 + 2k or k >= -3
2605 *
2606 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2607 * the first constraint, we obtain
2608 *
2609 * [(i + 2j)/4] >= [-3/4] = -1
2610 *
2611 * Combining this constraint with the second constraint, we obtain
2612 *
2613 * k >= -2
2614 */
2615static __isl_give isl_basic_map *insert_bounds_on_div(
2616 __isl_take isl_basic_map *bmap, int div)
2617{
2618 int i;
2619 int check_lb, check_ub;
2620 isl_int v;
2621 unsigned total;
2622
2623 if (!bmap)
2624 return NULL;
2625
2626 if (isl_int_is_zero(bmap->div[div][0]))
2627 return bmap;
2628
2629 total = isl_space_dim(bmap->dim, isl_dim_all);
2630
2631 check_lb = 0;
2632 check_ub = 0;
2633 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
2634 int s = isl_int_sgn(bmap->ineq[i][1 + total + div]);
2635 if (s > 0)
2636 check_ub = 1;
2637 if (s < 0)
2638 check_lb = 1;
2639 }
2640
2641 if (!check_lb && !check_ub)
2642 return bmap;
2643
2644 isl_int_init(v);
2645
2646 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
2647 if (!isl_int_is_zero(bmap->ineq[i][1 + total + div]))
2648 continue;
2649
2650 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, total, v,
2651 check_lb, check_ub);
2652 }
2653
2654 isl_int_clear(v);
2655
2656 return bmap;
2657}
2658
2659/* Remove all divs (recursively) involving any of the given dimensions
2660 * in their definitions.
2661 */
2662__isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
2663 __isl_take isl_basic_map *bmap,
2664 enum isl_dim_type type, unsigned first, unsigned n)
2665{
2666 int i;
2667
Tobias Grosser94e53712016-12-31 07:46:11 +00002668 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2669 return isl_basic_map_free(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +00002670 first += isl_basic_map_offset(bmap, type);
2671
2672 for (i = bmap->n_div - 1; i >= 0; --i) {
Tobias Grosser72745c22017-02-17 05:11:16 +00002673 isl_bool involves;
2674
2675 involves = div_involves_vars(bmap, i, first, n);
2676 if (involves < 0)
2677 return isl_basic_map_free(bmap);
2678 if (!involves)
Tobias Grosser52a25232015-02-04 20:55:43 +00002679 continue;
2680 bmap = insert_bounds_on_div(bmap, i);
2681 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2682 if (!bmap)
2683 return NULL;
2684 i = bmap->n_div;
2685 }
2686
2687 return bmap;
Tobias Grosser52a25232015-02-04 20:55:43 +00002688}
2689
2690__isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims(
2691 __isl_take isl_basic_set *bset,
2692 enum isl_dim_type type, unsigned first, unsigned n)
2693{
2694 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
2695}
2696
2697__isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
2698 enum isl_dim_type type, unsigned first, unsigned n)
2699{
2700 int i;
2701
2702 if (!map)
2703 return NULL;
2704 if (map->n == 0)
2705 return map;
2706
2707 map = isl_map_cow(map);
2708 if (!map)
2709 return NULL;
2710
2711 for (i = 0; i < map->n; ++i) {
2712 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
2713 type, first, n);
2714 if (!map->p[i])
2715 goto error;
2716 }
2717 return map;
2718error:
2719 isl_map_free(map);
2720 return NULL;
2721}
2722
2723__isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
2724 enum isl_dim_type type, unsigned first, unsigned n)
2725{
Tobias Grosser06e15922016-11-16 11:06:47 +00002726 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set),
2727 type, first, n));
Tobias Grosser52a25232015-02-04 20:55:43 +00002728}
2729
Tobias Grosser06e15922016-11-16 11:06:47 +00002730/* Does the description of "bmap" depend on the specified dimensions?
Tobias Grosser52a25232015-02-04 20:55:43 +00002731 * We also check whether the dimensions appear in any of the div definitions.
2732 * In principle there is no need for this check. If the dimensions appear
2733 * in a div definition, they also appear in the defining constraints of that
2734 * div.
2735 */
Tobias Grosserb2f39922015-05-28 13:32:11 +00002736isl_bool isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
Tobias Grosser52a25232015-02-04 20:55:43 +00002737 enum isl_dim_type type, unsigned first, unsigned n)
2738{
2739 int i;
2740
Tobias Grosser94e53712016-12-31 07:46:11 +00002741 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
Tobias Grosserb2f39922015-05-28 13:32:11 +00002742 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00002743
Tobias Grosser52a25232015-02-04 20:55:43 +00002744 first += isl_basic_map_offset(bmap, type);
2745 for (i = 0; i < bmap->n_eq; ++i)
2746 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
Tobias Grosserb2f39922015-05-28 13:32:11 +00002747 return isl_bool_true;
Tobias Grosser52a25232015-02-04 20:55:43 +00002748 for (i = 0; i < bmap->n_ineq; ++i)
2749 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
Tobias Grosserb2f39922015-05-28 13:32:11 +00002750 return isl_bool_true;
Tobias Grosser52a25232015-02-04 20:55:43 +00002751 for (i = 0; i < bmap->n_div; ++i) {
2752 if (isl_int_is_zero(bmap->div[i][0]))
2753 continue;
2754 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
Tobias Grosserb2f39922015-05-28 13:32:11 +00002755 return isl_bool_true;
Tobias Grosser52a25232015-02-04 20:55:43 +00002756 }
2757
Tobias Grosserb2f39922015-05-28 13:32:11 +00002758 return isl_bool_false;
Tobias Grosser52a25232015-02-04 20:55:43 +00002759}
2760
Tobias Grosserb2f39922015-05-28 13:32:11 +00002761isl_bool isl_map_involves_dims(__isl_keep isl_map *map,
Tobias Grosser52a25232015-02-04 20:55:43 +00002762 enum isl_dim_type type, unsigned first, unsigned n)
2763{
2764 int i;
2765
2766 if (!map)
Tobias Grosserb2f39922015-05-28 13:32:11 +00002767 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00002768
2769 if (first + n > isl_map_dim(map, type))
2770 isl_die(map->ctx, isl_error_invalid,
Tobias Grosserb2f39922015-05-28 13:32:11 +00002771 "index out of bounds", return isl_bool_error);
Tobias Grosser52a25232015-02-04 20:55:43 +00002772
2773 for (i = 0; i < map->n; ++i) {
Tobias Grosserb2f39922015-05-28 13:32:11 +00002774 isl_bool involves = isl_basic_map_involves_dims(map->p[i],
Tobias Grosser52a25232015-02-04 20:55:43 +00002775 type, first, n);
2776 if (involves < 0 || involves)
2777 return involves;
2778 }
2779
Tobias Grosserb2f39922015-05-28 13:32:11 +00002780 return isl_bool_false;
Tobias Grosser52a25232015-02-04 20:55:43 +00002781}
2782
Tobias Grosserb2f39922015-05-28 13:32:11 +00002783isl_bool isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
Tobias Grosser52a25232015-02-04 20:55:43 +00002784 enum isl_dim_type type, unsigned first, unsigned n)
2785{
2786 return isl_basic_map_involves_dims(bset, type, first, n);
2787}
2788
Tobias Grosserb2f39922015-05-28 13:32:11 +00002789isl_bool isl_set_involves_dims(__isl_keep isl_set *set,
Tobias Grosser52a25232015-02-04 20:55:43 +00002790 enum isl_dim_type type, unsigned first, unsigned n)
2791{
2792 return isl_map_involves_dims(set, type, first, n);
2793}
2794
Tobias Grosser94e53712016-12-31 07:46:11 +00002795/* Drop all constraints in bmap that involve any of the dimensions
2796 * first to first+n-1.
2797 */
2798static __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving(
2799 __isl_take isl_basic_map *bmap, unsigned first, unsigned n)
2800{
2801 int i;
2802
2803 if (n == 0)
2804 return bmap;
2805
2806 bmap = isl_basic_map_cow(bmap);
2807
2808 if (!bmap)
2809 return NULL;
2810
2811 for (i = bmap->n_eq - 1; i >= 0; --i) {
2812 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) == -1)
2813 continue;
2814 isl_basic_map_drop_equality(bmap, i);
2815 }
2816
2817 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2818 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) == -1)
2819 continue;
2820 isl_basic_map_drop_inequality(bmap, i);
2821 }
2822
2823 bmap = isl_basic_map_add_known_div_constraints(bmap);
2824 return bmap;
2825}
2826
2827/* Drop all constraints in bset that involve any of the dimensions
2828 * first to first+n-1.
2829 */
2830__isl_give isl_basic_set *isl_basic_set_drop_constraints_involving(
2831 __isl_take isl_basic_set *bset, unsigned first, unsigned n)
2832{
2833 return isl_basic_map_drop_constraints_involving(bset, first, n);
2834}
2835
2836/* Drop all constraints in bmap that do not involve any of the dimensions
2837 * first to first + n - 1 of the given type.
2838 */
2839__isl_give isl_basic_map *isl_basic_map_drop_constraints_not_involving_dims(
2840 __isl_take isl_basic_map *bmap,
2841 enum isl_dim_type type, unsigned first, unsigned n)
2842{
2843 int i;
2844
2845 if (n == 0) {
2846 isl_space *space = isl_basic_map_get_space(bmap);
2847 isl_basic_map_free(bmap);
2848 return isl_basic_map_universe(space);
2849 }
2850 bmap = isl_basic_map_cow(bmap);
2851 if (!bmap)
2852 return NULL;
2853
2854 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2855 return isl_basic_map_free(bmap);
2856
2857 first += isl_basic_map_offset(bmap, type) - 1;
2858
2859 for (i = bmap->n_eq - 1; i >= 0; --i) {
2860 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) != -1)
2861 continue;
2862 isl_basic_map_drop_equality(bmap, i);
2863 }
2864
2865 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2866 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) != -1)
2867 continue;
2868 isl_basic_map_drop_inequality(bmap, i);
2869 }
2870
2871 bmap = isl_basic_map_add_known_div_constraints(bmap);
2872 return bmap;
2873}
2874
2875/* Drop all constraints in bset that do not involve any of the dimensions
2876 * first to first + n - 1 of the given type.
2877 */
2878__isl_give isl_basic_set *isl_basic_set_drop_constraints_not_involving_dims(
2879 __isl_take isl_basic_set *bset,
2880 enum isl_dim_type type, unsigned first, unsigned n)
2881{
2882 return isl_basic_map_drop_constraints_not_involving_dims(bset,
2883 type, first, n);
2884}
2885
2886/* Drop all constraints in bmap that involve any of the dimensions
2887 * first to first + n - 1 of the given type.
2888 */
2889__isl_give isl_basic_map *isl_basic_map_drop_constraints_involving_dims(
2890 __isl_take isl_basic_map *bmap,
2891 enum isl_dim_type type, unsigned first, unsigned n)
2892{
2893 if (!bmap)
2894 return NULL;
2895 if (n == 0)
2896 return bmap;
2897
2898 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2899 return isl_basic_map_free(bmap);
2900
2901 bmap = isl_basic_map_remove_divs_involving_dims(bmap, type, first, n);
2902 first += isl_basic_map_offset(bmap, type) - 1;
2903 return isl_basic_map_drop_constraints_involving(bmap, first, n);
2904}
2905
2906/* Drop all constraints in bset that involve any of the dimensions
2907 * first to first + n - 1 of the given type.
2908 */
2909__isl_give isl_basic_set *isl_basic_set_drop_constraints_involving_dims(
2910 __isl_take isl_basic_set *bset,
2911 enum isl_dim_type type, unsigned first, unsigned n)
2912{
2913 return isl_basic_map_drop_constraints_involving_dims(bset,
2914 type, first, n);
2915}
2916
2917/* Drop constraints from "map" by applying "drop" to each basic map.
2918 */
2919static __isl_give isl_map *drop_constraints(__isl_take isl_map *map,
2920 enum isl_dim_type type, unsigned first, unsigned n,
2921 __isl_give isl_basic_map *(*drop)(__isl_take isl_basic_map *bmap,
2922 enum isl_dim_type type, unsigned first, unsigned n))
2923{
2924 int i;
2925 unsigned dim;
2926
2927 if (!map)
2928 return NULL;
2929
2930 dim = isl_map_dim(map, type);
2931 if (first + n > dim || first + n < first)
2932 isl_die(isl_map_get_ctx(map), isl_error_invalid,
2933 "index out of bounds", return isl_map_free(map));
2934
2935 map = isl_map_cow(map);
2936 if (!map)
2937 return NULL;
2938
2939 for (i = 0; i < map->n; ++i) {
2940 map->p[i] = drop(map->p[i], type, first, n);
2941 if (!map->p[i])
2942 return isl_map_free(map);
2943 }
2944
2945 if (map->n > 1)
2946 ISL_F_CLR(map, ISL_MAP_DISJOINT);
2947
2948 return map;
2949}
2950
2951/* Drop all constraints in map that involve any of the dimensions
2952 * first to first + n - 1 of the given type.
2953 */
2954__isl_give isl_map *isl_map_drop_constraints_involving_dims(
2955 __isl_take isl_map *map,
2956 enum isl_dim_type type, unsigned first, unsigned n)
2957{
2958 if (n == 0)
2959 return map;
2960 return drop_constraints(map, type, first, n,
2961 &isl_basic_map_drop_constraints_involving_dims);
2962}
2963
2964/* Drop all constraints in "map" that do not involve any of the dimensions
2965 * first to first + n - 1 of the given type.
2966 */
2967__isl_give isl_map *isl_map_drop_constraints_not_involving_dims(
2968 __isl_take isl_map *map,
2969 enum isl_dim_type type, unsigned first, unsigned n)
2970{
2971 if (n == 0) {
2972 isl_space *space = isl_map_get_space(map);
2973 isl_map_free(map);
2974 return isl_map_universe(space);
2975 }
2976 return drop_constraints(map, type, first, n,
2977 &isl_basic_map_drop_constraints_not_involving_dims);
2978}
2979
2980/* Drop all constraints in set that involve any of the dimensions
2981 * first to first + n - 1 of the given type.
2982 */
2983__isl_give isl_set *isl_set_drop_constraints_involving_dims(
2984 __isl_take isl_set *set,
2985 enum isl_dim_type type, unsigned first, unsigned n)
2986{
2987 return isl_map_drop_constraints_involving_dims(set, type, first, n);
2988}
2989
2990/* Drop all constraints in "set" that do not involve any of the dimensions
2991 * first to first + n - 1 of the given type.
2992 */
2993__isl_give isl_set *isl_set_drop_constraints_not_involving_dims(
2994 __isl_take isl_set *set,
2995 enum isl_dim_type type, unsigned first, unsigned n)
2996{
2997 return isl_map_drop_constraints_not_involving_dims(set, type, first, n);
2998}
2999
Tobias Grosser932ec012016-07-06 09:11:00 +00003000/* Does local variable "div" of "bmap" have a complete explicit representation?
3001 * Having a complete explicit representation requires not only
3002 * an explicit representation, but also that all local variables
3003 * that appear in this explicit representation in turn have
3004 * a complete explicit representation.
Tobias Grosser52a25232015-02-04 20:55:43 +00003005 */
Tobias Grosser932ec012016-07-06 09:11:00 +00003006isl_bool isl_basic_map_div_is_known(__isl_keep isl_basic_map *bmap, int div)
Tobias Grosser52a25232015-02-04 20:55:43 +00003007{
3008 int i;
3009 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
Tobias Grosser932ec012016-07-06 09:11:00 +00003010 isl_bool marked;
Tobias Grosser52a25232015-02-04 20:55:43 +00003011
Tobias Grosser932ec012016-07-06 09:11:00 +00003012 marked = isl_basic_map_div_is_marked_unknown(bmap, div);
3013 if (marked < 0 || marked)
3014 return isl_bool_not(marked);
Tobias Grosser52a25232015-02-04 20:55:43 +00003015
3016 for (i = bmap->n_div - 1; i >= 0; --i) {
Tobias Grosser932ec012016-07-06 09:11:00 +00003017 isl_bool known;
3018
Tobias Grosser52a25232015-02-04 20:55:43 +00003019 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
3020 continue;
Tobias Grosser932ec012016-07-06 09:11:00 +00003021 known = isl_basic_map_div_is_known(bmap, i);
3022 if (known < 0 || !known)
3023 return known;
Tobias Grosser52a25232015-02-04 20:55:43 +00003024 }
3025
Tobias Grosser932ec012016-07-06 09:11:00 +00003026 return isl_bool_true;
3027}
3028
Tobias Grosser52a25232015-02-04 20:55:43 +00003029/* Remove all divs that are unknown or defined in terms of unknown divs.
3030 */
3031__isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
3032 __isl_take isl_basic_map *bmap)
3033{
3034 int i;
3035
3036 if (!bmap)
3037 return NULL;
3038
3039 for (i = bmap->n_div - 1; i >= 0; --i) {
Tobias Grosser932ec012016-07-06 09:11:00 +00003040 if (isl_basic_map_div_is_known(bmap, i))
Tobias Grosser52a25232015-02-04 20:55:43 +00003041 continue;
3042 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
3043 if (!bmap)
3044 return NULL;
3045 i = bmap->n_div;
3046 }
3047
3048 return bmap;
3049}
3050
3051/* Remove all divs that are unknown or defined in terms of unknown divs.
3052 */
3053__isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
3054 __isl_take isl_basic_set *bset)
3055{
3056 return isl_basic_map_remove_unknown_divs(bset);
3057}
3058
3059__isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
3060{
3061 int i;
3062
3063 if (!map)
3064 return NULL;
3065 if (map->n == 0)
3066 return map;
3067
3068 map = isl_map_cow(map);
3069 if (!map)
3070 return NULL;
3071
3072 for (i = 0; i < map->n; ++i) {
3073 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
3074 if (!map->p[i])
3075 goto error;
3076 }
3077 return map;
3078error:
3079 isl_map_free(map);
3080 return NULL;
3081}
3082
3083__isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
3084{
Tobias Grosser06e15922016-11-16 11:06:47 +00003085 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set)));
Tobias Grosser52a25232015-02-04 20:55:43 +00003086}
3087
3088__isl_give isl_basic_set *isl_basic_set_remove_dims(
3089 __isl_take isl_basic_set *bset,
3090 enum isl_dim_type type, unsigned first, unsigned n)
3091{
Tobias Grosser06e15922016-11-16 11:06:47 +00003092 isl_basic_map *bmap = bset_to_bmap(bset);
3093 bmap = isl_basic_map_remove_dims(bmap, type, first, n);
3094 return bset_from_bmap(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +00003095}
3096
3097struct isl_map *isl_map_remove_dims(struct isl_map *map,
3098 enum isl_dim_type type, unsigned first, unsigned n)
3099{
3100 int i;
3101
3102 if (n == 0)
3103 return map;
3104
3105 map = isl_map_cow(map);
3106 if (!map)
3107 return NULL;
3108 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
3109
3110 for (i = 0; i < map->n; ++i) {
3111 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
3112 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
3113 if (!map->p[i])
3114 goto error;
3115 }
3116 map = isl_map_drop(map, type, first, n);
3117 return map;
3118error:
3119 isl_map_free(map);
3120 return NULL;
3121}
3122
3123__isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
3124 enum isl_dim_type type, unsigned first, unsigned n)
3125{
Tobias Grosser06e15922016-11-16 11:06:47 +00003126 return set_from_map(isl_map_remove_dims(set_to_map(bset),
3127 type, first, n));
Tobias Grosser52a25232015-02-04 20:55:43 +00003128}
3129
3130/* Project out n inputs starting at first using Fourier-Motzkin */
3131struct isl_map *isl_map_remove_inputs(struct isl_map *map,
3132 unsigned first, unsigned n)
3133{
3134 return isl_map_remove_dims(map, isl_dim_in, first, n);
3135}
3136
3137static void dump_term(struct isl_basic_map *bmap,
3138 isl_int c, int pos, FILE *out)
3139{
3140 const char *name;
Tobias Grosserf4fe34b2017-03-16 21:33:20 +00003141 unsigned in = isl_basic_map_dim(bmap, isl_dim_in);
3142 unsigned dim = in + isl_basic_map_dim(bmap, isl_dim_out);
3143 unsigned nparam = isl_basic_map_dim(bmap, isl_dim_param);
Tobias Grosser52a25232015-02-04 20:55:43 +00003144 if (!pos)
3145 isl_int_print(out, c, 0);
3146 else {
3147 if (!isl_int_is_one(c))
3148 isl_int_print(out, c, 0);
3149 if (pos < 1 + nparam) {
3150 name = isl_space_get_dim_name(bmap->dim,
3151 isl_dim_param, pos - 1);
3152 if (name)
3153 fprintf(out, "%s", name);
3154 else
3155 fprintf(out, "p%d", pos - 1);
3156 } else if (pos < 1 + nparam + in)
3157 fprintf(out, "i%d", pos - 1 - nparam);
3158 else if (pos < 1 + nparam + dim)
3159 fprintf(out, "o%d", pos - 1 - nparam - in);
3160 else
3161 fprintf(out, "e%d", pos - 1 - nparam - dim);
3162 }
3163}
3164
3165static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
3166 int sign, FILE *out)
3167{
3168 int i;
3169 int first;
3170 unsigned len = 1 + isl_basic_map_total_dim(bmap);
3171 isl_int v;
3172
3173 isl_int_init(v);
3174 for (i = 0, first = 1; i < len; ++i) {
3175 if (isl_int_sgn(c[i]) * sign <= 0)
3176 continue;
3177 if (!first)
3178 fprintf(out, " + ");
3179 first = 0;
3180 isl_int_abs(v, c[i]);
3181 dump_term(bmap, v, i, out);
3182 }
3183 isl_int_clear(v);
3184 if (first)
3185 fprintf(out, "0");
3186}
3187
3188static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
3189 const char *op, FILE *out, int indent)
3190{
3191 int i;
3192
3193 fprintf(out, "%*s", indent, "");
3194
3195 dump_constraint_sign(bmap, c, 1, out);
3196 fprintf(out, " %s ", op);
3197 dump_constraint_sign(bmap, c, -1, out);
3198
3199 fprintf(out, "\n");
3200
3201 for (i = bmap->n_div; i < bmap->extra; ++i) {
3202 if (isl_int_is_zero(c[1+isl_space_dim(bmap->dim, isl_dim_all)+i]))
3203 continue;
3204 fprintf(out, "%*s", indent, "");
3205 fprintf(out, "ERROR: unused div coefficient not zero\n");
3206 abort();
3207 }
3208}
3209
3210static void dump_constraints(struct isl_basic_map *bmap,
3211 isl_int **c, unsigned n,
3212 const char *op, FILE *out, int indent)
3213{
3214 int i;
3215
3216 for (i = 0; i < n; ++i)
3217 dump_constraint(bmap, c[i], op, out, indent);
3218}
3219
3220static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
3221{
3222 int j;
3223 int first = 1;
3224 unsigned total = isl_basic_map_total_dim(bmap);
3225
3226 for (j = 0; j < 1 + total; ++j) {
3227 if (isl_int_is_zero(exp[j]))
3228 continue;
3229 if (!first && isl_int_is_pos(exp[j]))
3230 fprintf(out, "+");
3231 dump_term(bmap, exp[j], j, out);
3232 first = 0;
3233 }
3234}
3235
3236static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
3237{
3238 int i;
3239
3240 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
3241 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
3242
3243 for (i = 0; i < bmap->n_div; ++i) {
3244 fprintf(out, "%*s", indent, "");
3245 fprintf(out, "e%d = [(", i);
3246 dump_affine(bmap, bmap->div[i]+1, out);
3247 fprintf(out, ")/");
3248 isl_int_print(out, bmap->div[i][0], 0);
3249 fprintf(out, "]\n");
3250 }
3251}
3252
3253void isl_basic_set_print_internal(struct isl_basic_set *bset,
3254 FILE *out, int indent)
3255{
3256 if (!bset) {
3257 fprintf(out, "null basic set\n");
3258 return;
3259 }
3260
3261 fprintf(out, "%*s", indent, "");
3262 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
3263 bset->ref, bset->dim->nparam, bset->dim->n_out,
3264 bset->extra, bset->flags);
Tobias Grosser06e15922016-11-16 11:06:47 +00003265 dump(bset_to_bmap(bset), out, indent);
Tobias Grosser52a25232015-02-04 20:55:43 +00003266}
3267
3268void isl_basic_map_print_internal(struct isl_basic_map *bmap,
3269 FILE *out, int indent)
3270{
3271 if (!bmap) {
3272 fprintf(out, "null basic map\n");
3273 return;
3274 }
3275
3276 fprintf(out, "%*s", indent, "");
3277 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
3278 "flags: %x, n_name: %d\n",
3279 bmap->ref,
3280 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
3281 bmap->extra, bmap->flags, bmap->dim->n_id);
3282 dump(bmap, out, indent);
3283}
3284
3285int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
3286{
3287 unsigned total;
3288 if (!bmap)
3289 return -1;
3290 total = isl_basic_map_total_dim(bmap);
3291 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
3292 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
3293 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
3294 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
3295 return 0;
3296}
3297
Tobias Grosser07b20952016-06-12 04:30:40 +00003298__isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *space, int n,
Tobias Grosser52a25232015-02-04 20:55:43 +00003299 unsigned flags)
3300{
Tobias Grosser07b20952016-06-12 04:30:40 +00003301 if (!space)
Tobias Grosser52a25232015-02-04 20:55:43 +00003302 return NULL;
Tobias Grosser07b20952016-06-12 04:30:40 +00003303 if (isl_space_dim(space, isl_dim_in) != 0)
3304 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3305 "set cannot have input dimensions", goto error);
3306 return isl_map_alloc_space(space, n, flags);
Tobias Grosser52a25232015-02-04 20:55:43 +00003307error:
Tobias Grosser07b20952016-06-12 04:30:40 +00003308 isl_space_free(space);
Tobias Grosser52a25232015-02-04 20:55:43 +00003309 return NULL;
3310}
3311
Tobias Grosser52a25232015-02-04 20:55:43 +00003312/* Make sure "map" has room for at least "n" more basic maps.
3313 */
3314struct isl_map *isl_map_grow(struct isl_map *map, int n)
3315{
3316 int i;
3317 struct isl_map *grown = NULL;
3318
3319 if (!map)
3320 return NULL;
3321 isl_assert(map->ctx, n >= 0, goto error);
3322 if (map->n + n <= map->size)
3323 return map;
3324 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
3325 if (!grown)
3326 goto error;
3327 for (i = 0; i < map->n; ++i) {
3328 grown->p[i] = isl_basic_map_copy(map->p[i]);
3329 if (!grown->p[i])
3330 goto error;
3331 grown->n++;
3332 }
3333 isl_map_free(map);
3334 return grown;
3335error:
3336 isl_map_free(grown);
3337 isl_map_free(map);
3338 return NULL;
3339}
3340
3341/* Make sure "set" has room for at least "n" more basic sets.
3342 */
3343struct isl_set *isl_set_grow(struct isl_set *set, int n)
3344{
Tobias Grosser06e15922016-11-16 11:06:47 +00003345 return set_from_map(isl_map_grow(set_to_map(set), n));
Tobias Grosser52a25232015-02-04 20:55:43 +00003346}
3347
Tobias Grosser52a25232015-02-04 20:55:43 +00003348struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
3349{
3350 return isl_map_from_basic_map(bset);
3351}
3352
3353struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
3354{
3355 struct isl_map *map;
3356
3357 if (!bmap)
3358 return NULL;
3359
3360 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
3361 return isl_map_add_basic_map(map, bmap);
3362}
3363
3364__isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
3365 __isl_take isl_basic_set *bset)
3366{
Tobias Grosser06e15922016-11-16 11:06:47 +00003367 return set_from_map(isl_map_add_basic_map(set_to_map(set),
3368 bset_to_bmap(bset)));
Tobias Grosser52a25232015-02-04 20:55:43 +00003369}
3370
3371__isl_null isl_set *isl_set_free(__isl_take isl_set *set)
3372{
Tobias Grosser07b20952016-06-12 04:30:40 +00003373 return isl_map_free(set);
Tobias Grosser52a25232015-02-04 20:55:43 +00003374}
3375
3376void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
3377{
3378 int i;
3379
3380 if (!set) {
3381 fprintf(out, "null set\n");
3382 return;
3383 }
3384
3385 fprintf(out, "%*s", indent, "");
3386 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
3387 set->ref, set->n, set->dim->nparam, set->dim->n_out,
3388 set->flags);
3389 for (i = 0; i < set->n; ++i) {
3390 fprintf(out, "%*s", indent, "");
3391 fprintf(out, "basic set %d:\n", i);
3392 isl_basic_set_print_internal(set->p[i], out, indent+4);
3393 }
3394}
3395
3396void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
3397{
3398 int i;
3399
3400 if (!map) {
3401 fprintf(out, "null map\n");
3402 return;
3403 }
3404
3405 fprintf(out, "%*s", indent, "");
3406 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
3407 "flags: %x, n_name: %d\n",
3408 map->ref, map->n, map->dim->nparam, map->dim->n_in,
3409 map->dim->n_out, map->flags, map->dim->n_id);
3410 for (i = 0; i < map->n; ++i) {
3411 fprintf(out, "%*s", indent, "");
3412 fprintf(out, "basic map %d:\n", i);
3413 isl_basic_map_print_internal(map->p[i], out, indent+4);
3414 }
3415}
3416
3417struct isl_basic_map *isl_basic_map_intersect_domain(
3418 struct isl_basic_map *bmap, struct isl_basic_set *bset)
3419{
3420 struct isl_basic_map *bmap_domain;
3421
Tobias Grossere5671e52017-03-10 09:17:55 +00003422 if (isl_basic_map_check_equal_params(bmap, bset_to_bmap(bset)) < 0)
Tobias Grosser52a25232015-02-04 20:55:43 +00003423 goto error;
3424
Tobias Grosser52a25232015-02-04 20:55:43 +00003425 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
3426 isl_assert(bset->ctx,
3427 isl_basic_map_compatible_domain(bmap, bset), goto error);
3428
3429 bmap = isl_basic_map_cow(bmap);
3430 if (!bmap)
3431 goto error;
3432 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
3433 bset->n_div, bset->n_eq, bset->n_ineq);
3434 bmap_domain = isl_basic_map_from_domain(bset);
3435 bmap = add_constraints(bmap, bmap_domain, 0, 0);
3436
3437 bmap = isl_basic_map_simplify(bmap);
3438 return isl_basic_map_finalize(bmap);
3439error:
3440 isl_basic_map_free(bmap);
3441 isl_basic_set_free(bset);
3442 return NULL;
3443}
3444
Tobias Grosser72745c22017-02-17 05:11:16 +00003445/* Check that the space of "bset" is the same as that of the range of "bmap".
3446 */
3447static isl_stat isl_basic_map_check_compatible_range(
3448 __isl_keep isl_basic_map *bmap, __isl_keep isl_basic_set *bset)
3449{
3450 isl_bool ok;
3451
3452 ok = isl_basic_map_compatible_range(bmap, bset);
3453 if (ok < 0)
3454 return isl_stat_error;
3455 if (!ok)
3456 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
3457 "incompatible spaces", return isl_stat_error);
3458
3459 return isl_stat_ok;
3460}
3461
Tobias Grosser52a25232015-02-04 20:55:43 +00003462struct isl_basic_map *isl_basic_map_intersect_range(
3463 struct isl_basic_map *bmap, struct isl_basic_set *bset)
3464{
3465 struct isl_basic_map *bmap_range;
3466
Tobias Grossere5671e52017-03-10 09:17:55 +00003467 if (isl_basic_map_check_equal_params(bmap, bset_to_bmap(bset)) < 0)
Tobias Grosser52a25232015-02-04 20:55:43 +00003468 goto error;
3469
Tobias Grosser72745c22017-02-17 05:11:16 +00003470 if (isl_space_dim(bset->dim, isl_dim_set) != 0 &&
3471 isl_basic_map_check_compatible_range(bmap, bset) < 0)
3472 goto error;
Tobias Grosser52a25232015-02-04 20:55:43 +00003473
Tobias Grossera67ac972016-02-26 11:35:12 +00003474 if (isl_basic_set_plain_is_universe(bset)) {
Tobias Grosser52a25232015-02-04 20:55:43 +00003475 isl_basic_set_free(bset);
3476 return bmap;
3477 }
3478
3479 bmap = isl_basic_map_cow(bmap);
3480 if (!bmap)
3481 goto error;
3482 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
3483 bset->n_div, bset->n_eq, bset->n_ineq);
Tobias Grossereab0943e2016-11-22 21:31:59 +00003484 bmap_range = bset_to_bmap(bset);
Tobias Grosser52a25232015-02-04 20:55:43 +00003485 bmap = add_constraints(bmap, bmap_range, 0, 0);
3486
3487 bmap = isl_basic_map_simplify(bmap);
3488 return isl_basic_map_finalize(bmap);
3489error:
3490 isl_basic_map_free(bmap);
3491 isl_basic_set_free(bset);
3492 return NULL;
3493}
3494
Tobias Grosserb2f39922015-05-28 13:32:11 +00003495isl_bool isl_basic_map_contains(__isl_keep isl_basic_map *bmap,
3496 __isl_keep isl_vec *vec)
Tobias Grosser52a25232015-02-04 20:55:43 +00003497{
3498 int i;
3499 unsigned total;
3500 isl_int s;
3501
3502 if (!bmap || !vec)
Tobias Grosserb2f39922015-05-28 13:32:11 +00003503 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00003504
3505 total = 1 + isl_basic_map_total_dim(bmap);
3506 if (total != vec->size)
Tobias Grosser72745c22017-02-17 05:11:16 +00003507 return isl_bool_false;
Tobias Grosser52a25232015-02-04 20:55:43 +00003508
3509 isl_int_init(s);
3510
3511 for (i = 0; i < bmap->n_eq; ++i) {
3512 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
3513 if (!isl_int_is_zero(s)) {
3514 isl_int_clear(s);
Tobias Grosserb2f39922015-05-28 13:32:11 +00003515 return isl_bool_false;
Tobias Grosser52a25232015-02-04 20:55:43 +00003516 }
3517 }
3518
3519 for (i = 0; i < bmap->n_ineq; ++i) {
3520 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
3521 if (isl_int_is_neg(s)) {
3522 isl_int_clear(s);
Tobias Grosserb2f39922015-05-28 13:32:11 +00003523 return isl_bool_false;
Tobias Grosser52a25232015-02-04 20:55:43 +00003524 }
3525 }
3526
3527 isl_int_clear(s);
3528
Tobias Grosserb2f39922015-05-28 13:32:11 +00003529 return isl_bool_true;
Tobias Grosser52a25232015-02-04 20:55:43 +00003530}
3531
Tobias Grosserb2f39922015-05-28 13:32:11 +00003532isl_bool isl_basic_set_contains(__isl_keep isl_basic_set *bset,
3533 __isl_keep isl_vec *vec)
Tobias Grosser52a25232015-02-04 20:55:43 +00003534{
Tobias Grosser06e15922016-11-16 11:06:47 +00003535 return isl_basic_map_contains(bset_to_bmap(bset), vec);
Tobias Grosser52a25232015-02-04 20:55:43 +00003536}
3537
3538struct isl_basic_map *isl_basic_map_intersect(
3539 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3540{
3541 struct isl_vec *sample = NULL;
3542
Tobias Grossere5671e52017-03-10 09:17:55 +00003543 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
Tobias Grosser52a25232015-02-04 20:55:43 +00003544 goto error;
Tobias Grosser52a25232015-02-04 20:55:43 +00003545 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
3546 isl_space_dim(bmap1->dim, isl_dim_param) &&
3547 isl_space_dim(bmap2->dim, isl_dim_all) !=
3548 isl_space_dim(bmap2->dim, isl_dim_param))
3549 return isl_basic_map_intersect(bmap2, bmap1);
3550
3551 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
3552 isl_space_dim(bmap2->dim, isl_dim_param))
3553 isl_assert(bmap1->ctx,
3554 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
3555
Tobias Grosserfb3fb0a2015-11-21 20:48:39 +00003556 if (isl_basic_map_plain_is_empty(bmap1)) {
3557 isl_basic_map_free(bmap2);
3558 return bmap1;
3559 }
3560 if (isl_basic_map_plain_is_empty(bmap2)) {
3561 isl_basic_map_free(bmap1);
3562 return bmap2;
3563 }
3564
Tobias Grosser52a25232015-02-04 20:55:43 +00003565 if (bmap1->sample &&
3566 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
3567 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
3568 sample = isl_vec_copy(bmap1->sample);
3569 else if (bmap2->sample &&
3570 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
3571 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
3572 sample = isl_vec_copy(bmap2->sample);
3573
3574 bmap1 = isl_basic_map_cow(bmap1);
3575 if (!bmap1)
3576 goto error;
3577 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
3578 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
3579 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
3580
3581 if (!bmap1)
3582 isl_vec_free(sample);
3583 else if (sample) {
3584 isl_vec_free(bmap1->sample);
3585 bmap1->sample = sample;
3586 }
3587
3588 bmap1 = isl_basic_map_simplify(bmap1);
3589 return isl_basic_map_finalize(bmap1);
3590error:
3591 if (sample)
3592 isl_vec_free(sample);
3593 isl_basic_map_free(bmap1);
3594 isl_basic_map_free(bmap2);
3595 return NULL;
3596}
3597
3598struct isl_basic_set *isl_basic_set_intersect(
3599 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3600{
Tobias Grosser06e15922016-11-16 11:06:47 +00003601 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1),
3602 bset_to_bmap(bset2)));
Tobias Grosser52a25232015-02-04 20:55:43 +00003603}
3604
3605__isl_give isl_basic_set *isl_basic_set_intersect_params(
3606 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
3607{
3608 return isl_basic_set_intersect(bset1, bset2);
3609}
3610
3611/* Special case of isl_map_intersect, where both map1 and map2
3612 * are convex, without any divs and such that either map1 or map2
3613 * contains a single constraint. This constraint is then simply
3614 * added to the other map.
3615 */
3616static __isl_give isl_map *map_intersect_add_constraint(
3617 __isl_take isl_map *map1, __isl_take isl_map *map2)
3618{
3619 isl_assert(map1->ctx, map1->n == 1, goto error);
3620 isl_assert(map2->ctx, map1->n == 1, goto error);
3621 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
3622 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
3623
3624 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
3625 return isl_map_intersect(map2, map1);
3626
Tobias Grosser52a25232015-02-04 20:55:43 +00003627 map1 = isl_map_cow(map1);
3628 if (!map1)
3629 goto error;
3630 if (isl_map_plain_is_empty(map1)) {
3631 isl_map_free(map2);
3632 return map1;
3633 }
3634 map1->p[0] = isl_basic_map_cow(map1->p[0]);
3635 if (map2->p[0]->n_eq == 1)
3636 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
3637 else
3638 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
3639 map2->p[0]->ineq[0]);
3640
3641 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
3642 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
3643 if (!map1->p[0])
3644 goto error;
3645
3646 if (isl_basic_map_plain_is_empty(map1->p[0])) {
3647 isl_basic_map_free(map1->p[0]);
3648 map1->n = 0;
3649 }
3650
3651 isl_map_free(map2);
3652
3653 return map1;
3654error:
3655 isl_map_free(map1);
3656 isl_map_free(map2);
3657 return NULL;
3658}
3659
3660/* map2 may be either a parameter domain or a map living in the same
3661 * space as map1.
3662 */
3663static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
3664 __isl_take isl_map *map2)
3665{
3666 unsigned flags = 0;
3667 isl_map *result;
3668 int i, j;
3669
3670 if (!map1 || !map2)
3671 goto error;
3672
3673 if ((isl_map_plain_is_empty(map1) ||
3674 isl_map_plain_is_universe(map2)) &&
3675 isl_space_is_equal(map1->dim, map2->dim)) {
3676 isl_map_free(map2);
3677 return map1;
3678 }
3679 if ((isl_map_plain_is_empty(map2) ||
3680 isl_map_plain_is_universe(map1)) &&
3681 isl_space_is_equal(map1->dim, map2->dim)) {
3682 isl_map_free(map1);
3683 return map2;
3684 }
3685
3686 if (map1->n == 1 && map2->n == 1 &&
3687 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
3688 isl_space_is_equal(map1->dim, map2->dim) &&
3689 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
3690 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
3691 return map_intersect_add_constraint(map1, map2);
3692
3693 if (isl_space_dim(map2->dim, isl_dim_all) !=
3694 isl_space_dim(map2->dim, isl_dim_param))
3695 isl_assert(map1->ctx,
3696 isl_space_is_equal(map1->dim, map2->dim), goto error);
3697
3698 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
3699 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3700 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3701
3702 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3703 map1->n * map2->n, flags);
3704 if (!result)
3705 goto error;
3706 for (i = 0; i < map1->n; ++i)
3707 for (j = 0; j < map2->n; ++j) {
3708 struct isl_basic_map *part;
3709 part = isl_basic_map_intersect(
3710 isl_basic_map_copy(map1->p[i]),
3711 isl_basic_map_copy(map2->p[j]));
3712 if (isl_basic_map_is_empty(part) < 0)
3713 part = isl_basic_map_free(part);
3714 result = isl_map_add_basic_map(result, part);
3715 if (!result)
3716 goto error;
3717 }
3718 isl_map_free(map1);
3719 isl_map_free(map2);
3720 return result;
3721error:
3722 isl_map_free(map1);
3723 isl_map_free(map2);
3724 return NULL;
3725}
3726
3727static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
3728 __isl_take isl_map *map2)
3729{
3730 if (!map1 || !map2)
3731 goto error;
3732 if (!isl_space_is_equal(map1->dim, map2->dim))
3733 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
3734 "spaces don't match", goto error);
3735 return map_intersect_internal(map1, map2);
3736error:
3737 isl_map_free(map1);
3738 isl_map_free(map2);
3739 return NULL;
3740}
3741
3742__isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
3743 __isl_take isl_map *map2)
3744{
3745 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
3746}
3747
3748struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
3749{
Tobias Grosser06e15922016-11-16 11:06:47 +00003750 return set_from_map(isl_map_intersect(set_to_map(set1),
3751 set_to_map(set2)));
Tobias Grosser52a25232015-02-04 20:55:43 +00003752}
3753
3754/* map_intersect_internal accepts intersections
3755 * with parameter domains, so we can just call that function.
3756 */
3757static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
3758 __isl_take isl_set *params)
3759{
3760 return map_intersect_internal(map, params);
3761}
3762
3763__isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
3764 __isl_take isl_map *map2)
3765{
3766 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
3767}
3768
3769__isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
3770 __isl_take isl_set *params)
3771{
3772 return isl_map_intersect_params(set, params);
3773}
3774
3775struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
3776{
Tobias Grosserb2f39922015-05-28 13:32:11 +00003777 isl_space *space;
3778 unsigned pos, n1, n2;
Tobias Grosser52a25232015-02-04 20:55:43 +00003779
3780 if (!bmap)
3781 return NULL;
3782 bmap = isl_basic_map_cow(bmap);
3783 if (!bmap)
3784 return NULL;
Tobias Grosserb2f39922015-05-28 13:32:11 +00003785 space = isl_space_reverse(isl_space_copy(bmap->dim));
3786 pos = isl_basic_map_offset(bmap, isl_dim_in);
3787 n1 = isl_basic_map_dim(bmap, isl_dim_in);
3788 n2 = isl_basic_map_dim(bmap, isl_dim_out);
3789 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
3790 return isl_basic_map_reset_space(bmap, space);
Tobias Grosser52a25232015-02-04 20:55:43 +00003791}
3792
3793static __isl_give isl_basic_map *basic_map_space_reset(
3794 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
3795{
3796 isl_space *space;
3797
3798 if (!bmap)
3799 return NULL;
3800 if (!isl_space_is_named_or_nested(bmap->dim, type))
3801 return bmap;
3802
3803 space = isl_basic_map_get_space(bmap);
3804 space = isl_space_reset(space, type);
3805 bmap = isl_basic_map_reset_space(bmap, space);
3806 return bmap;
3807}
3808
3809__isl_give isl_basic_map *isl_basic_map_insert_dims(
3810 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3811 unsigned pos, unsigned n)
3812{
Tobias Grosser72745c22017-02-17 05:11:16 +00003813 isl_bool rational;
Tobias Grosser52a25232015-02-04 20:55:43 +00003814 isl_space *res_dim;
3815 struct isl_basic_map *res;
3816 struct isl_dim_map *dim_map;
3817 unsigned total, off;
3818 enum isl_dim_type t;
3819
3820 if (n == 0)
3821 return basic_map_space_reset(bmap, type);
3822
3823 if (!bmap)
3824 return NULL;
3825
3826 res_dim = isl_space_insert_dims(isl_basic_map_get_space(bmap), type, pos, n);
3827
3828 total = isl_basic_map_total_dim(bmap) + n;
3829 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3830 off = 0;
3831 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3832 if (t != type) {
3833 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3834 } else {
3835 unsigned size = isl_basic_map_dim(bmap, t);
3836 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3837 0, pos, off);
3838 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3839 pos, size - pos, off + pos + n);
3840 }
3841 off += isl_space_dim(res_dim, t);
3842 }
3843 isl_dim_map_div(dim_map, bmap, off);
3844
3845 res = isl_basic_map_alloc_space(res_dim,
3846 bmap->n_div, bmap->n_eq, bmap->n_ineq);
Tobias Grosser72745c22017-02-17 05:11:16 +00003847 rational = isl_basic_map_is_rational(bmap);
3848 if (rational < 0)
3849 res = isl_basic_map_free(res);
3850 if (rational)
Tobias Grosser52a25232015-02-04 20:55:43 +00003851 res = isl_basic_map_set_rational(res);
3852 if (isl_basic_map_plain_is_empty(bmap)) {
3853 isl_basic_map_free(bmap);
3854 free(dim_map);
3855 return isl_basic_map_set_to_empty(res);
3856 }
3857 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3858 return isl_basic_map_finalize(res);
3859}
3860
3861__isl_give isl_basic_set *isl_basic_set_insert_dims(
3862 __isl_take isl_basic_set *bset,
3863 enum isl_dim_type type, unsigned pos, unsigned n)
3864{
3865 return isl_basic_map_insert_dims(bset, type, pos, n);
3866}
3867
Tobias Grossere0f8d592015-05-13 13:10:13 +00003868__isl_give isl_basic_map *isl_basic_map_add_dims(__isl_take isl_basic_map *bmap,
Tobias Grosser52a25232015-02-04 20:55:43 +00003869 enum isl_dim_type type, unsigned n)
3870{
3871 if (!bmap)
3872 return NULL;
3873 return isl_basic_map_insert_dims(bmap, type,
3874 isl_basic_map_dim(bmap, type), n);
3875}
3876
3877__isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
3878 enum isl_dim_type type, unsigned n)
3879{
3880 if (!bset)
3881 return NULL;
3882 isl_assert(bset->ctx, type != isl_dim_in, goto error);
Tobias Grossere0f8d592015-05-13 13:10:13 +00003883 return isl_basic_map_add_dims(bset, type, n);
Tobias Grosser52a25232015-02-04 20:55:43 +00003884error:
3885 isl_basic_set_free(bset);
3886 return NULL;
3887}
3888
3889static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
3890 enum isl_dim_type type)
3891{
3892 isl_space *space;
3893
3894 if (!map || !isl_space_is_named_or_nested(map->dim, type))
3895 return map;
3896
3897 space = isl_map_get_space(map);
3898 space = isl_space_reset(space, type);
3899 map = isl_map_reset_space(map, space);
3900 return map;
3901}
3902
3903__isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
3904 enum isl_dim_type type, unsigned pos, unsigned n)
3905{
3906 int i;
3907
3908 if (n == 0)
3909 return map_space_reset(map, type);
3910
3911 map = isl_map_cow(map);
3912 if (!map)
3913 return NULL;
3914
3915 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
3916 if (!map->dim)
3917 goto error;
3918
3919 for (i = 0; i < map->n; ++i) {
3920 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
3921 if (!map->p[i])
3922 goto error;
3923 }
3924
3925 return map;
3926error:
3927 isl_map_free(map);
3928 return NULL;
3929}
3930
3931__isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
3932 enum isl_dim_type type, unsigned pos, unsigned n)
3933{
3934 return isl_map_insert_dims(set, type, pos, n);
3935}
3936
3937__isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
3938 enum isl_dim_type type, unsigned n)
3939{
3940 if (!map)
3941 return NULL;
3942 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
3943}
3944
3945__isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
3946 enum isl_dim_type type, unsigned n)
3947{
3948 if (!set)
3949 return NULL;
3950 isl_assert(set->ctx, type != isl_dim_in, goto error);
Tobias Grosser06e15922016-11-16 11:06:47 +00003951 return set_from_map(isl_map_add_dims(set_to_map(set), type, n));
Tobias Grosser52a25232015-02-04 20:55:43 +00003952error:
3953 isl_set_free(set);
3954 return NULL;
3955}
3956
3957__isl_give isl_basic_map *isl_basic_map_move_dims(
3958 __isl_take isl_basic_map *bmap,
3959 enum isl_dim_type dst_type, unsigned dst_pos,
3960 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3961{
3962 struct isl_dim_map *dim_map;
3963 struct isl_basic_map *res;
3964 enum isl_dim_type t;
3965 unsigned total, off;
3966
3967 if (!bmap)
3968 return NULL;
3969 if (n == 0)
3970 return bmap;
3971
Tobias Grosser94e53712016-12-31 07:46:11 +00003972 if (isl_basic_map_check_range(bmap, src_type, src_pos, n) < 0)
3973 return isl_basic_map_free(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +00003974
3975 if (dst_type == src_type && dst_pos == src_pos)
3976 return bmap;
3977
3978 isl_assert(bmap->ctx, dst_type != src_type, goto error);
3979
3980 if (pos(bmap->dim, dst_type) + dst_pos ==
3981 pos(bmap->dim, src_type) + src_pos +
3982 ((src_type < dst_type) ? n : 0)) {
3983 bmap = isl_basic_map_cow(bmap);
3984 if (!bmap)
3985 return NULL;
3986
3987 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3988 src_type, src_pos, n);
3989 if (!bmap->dim)
3990 goto error;
3991
3992 bmap = isl_basic_map_finalize(bmap);
3993
3994 return bmap;
3995 }
3996
3997 total = isl_basic_map_total_dim(bmap);
3998 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3999
4000 off = 0;
4001 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
4002 unsigned size = isl_space_dim(bmap->dim, t);
4003 if (t == dst_type) {
4004 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4005 0, dst_pos, off);
4006 off += dst_pos;
4007 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
4008 src_pos, n, off);
4009 off += n;
4010 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4011 dst_pos, size - dst_pos, off);
4012 off += size - dst_pos;
4013 } else if (t == src_type) {
4014 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4015 0, src_pos, off);
4016 off += src_pos;
4017 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4018 src_pos + n, size - src_pos - n, off);
4019 off += size - src_pos - n;
4020 } else {
4021 isl_dim_map_dim(dim_map, bmap->dim, t, off);
4022 off += size;
4023 }
4024 }
4025 isl_dim_map_div(dim_map, bmap, off);
4026
4027 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
4028 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4029 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4030 if (!bmap)
4031 goto error;
4032
4033 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
4034 src_type, src_pos, n);
4035 if (!bmap->dim)
4036 goto error;
4037
4038 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
4039 bmap = isl_basic_map_gauss(bmap, NULL);
4040 bmap = isl_basic_map_finalize(bmap);
4041
4042 return bmap;
4043error:
4044 isl_basic_map_free(bmap);
4045 return NULL;
4046}
4047
4048__isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
4049 enum isl_dim_type dst_type, unsigned dst_pos,
4050 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4051{
Tobias Grosser06e15922016-11-16 11:06:47 +00004052 isl_basic_map *bmap = bset_to_bmap(bset);
4053 bmap = isl_basic_map_move_dims(bmap, dst_type, dst_pos,
4054 src_type, src_pos, n);
4055 return bset_from_bmap(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +00004056}
4057
4058__isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
4059 enum isl_dim_type dst_type, unsigned dst_pos,
4060 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4061{
4062 if (!set)
4063 return NULL;
4064 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
Tobias Grosser06e15922016-11-16 11:06:47 +00004065 return set_from_map(isl_map_move_dims(set_to_map(set),
4066 dst_type, dst_pos, src_type, src_pos, n));
Tobias Grosser52a25232015-02-04 20:55:43 +00004067error:
4068 isl_set_free(set);
4069 return NULL;
4070}
4071
4072__isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
4073 enum isl_dim_type dst_type, unsigned dst_pos,
4074 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4075{
4076 int i;
4077
4078 if (!map)
4079 return NULL;
4080 if (n == 0)
4081 return map;
4082
4083 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
4084 goto error);
4085
4086 if (dst_type == src_type && dst_pos == src_pos)
4087 return map;
4088
4089 isl_assert(map->ctx, dst_type != src_type, goto error);
4090
4091 map = isl_map_cow(map);
4092 if (!map)
4093 return NULL;
4094
4095 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
4096 if (!map->dim)
4097 goto error;
4098
4099 for (i = 0; i < map->n; ++i) {
4100 map->p[i] = isl_basic_map_move_dims(map->p[i],
4101 dst_type, dst_pos,
4102 src_type, src_pos, n);
4103 if (!map->p[i])
4104 goto error;
4105 }
4106
4107 return map;
4108error:
4109 isl_map_free(map);
4110 return NULL;
4111}
4112
4113/* Move the specified dimensions to the last columns right before
4114 * the divs. Don't change the dimension specification of bmap.
4115 * That's the responsibility of the caller.
4116 */
4117static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
4118 enum isl_dim_type type, unsigned first, unsigned n)
4119{
4120 struct isl_dim_map *dim_map;
4121 struct isl_basic_map *res;
4122 enum isl_dim_type t;
4123 unsigned total, off;
4124
4125 if (!bmap)
4126 return NULL;
4127 if (pos(bmap->dim, type) + first + n ==
4128 1 + isl_space_dim(bmap->dim, isl_dim_all))
4129 return bmap;
4130
4131 total = isl_basic_map_total_dim(bmap);
4132 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4133
4134 off = 0;
4135 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
4136 unsigned size = isl_space_dim(bmap->dim, t);
4137 if (t == type) {
4138 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4139 0, first, off);
4140 off += first;
4141 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4142 first, n, total - bmap->n_div - n);
4143 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4144 first + n, size - (first + n), off);
4145 off += size - (first + n);
4146 } else {
4147 isl_dim_map_dim(dim_map, bmap->dim, t, off);
4148 off += size;
4149 }
4150 }
4151 isl_dim_map_div(dim_map, bmap, off + n);
4152
4153 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
4154 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4155 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4156 return res;
4157}
4158
4159/* Insert "n" rows in the divs of "bmap".
4160 *
4161 * The number of columns is not changed, which means that the last
4162 * dimensions of "bmap" are being reintepreted as the new divs.
4163 * The space of "bmap" is not adjusted, however, which means
4164 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
4165 * from the space of "bmap" is the responsibility of the caller.
4166 */
4167static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
4168 int n)
4169{
4170 int i;
4171 size_t row_size;
4172 isl_int **new_div;
4173 isl_int *old;
4174
4175 bmap = isl_basic_map_cow(bmap);
4176 if (!bmap)
4177 return NULL;
4178
4179 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
4180 old = bmap->block2.data;
4181 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
4182 (bmap->extra + n) * (1 + row_size));
4183 if (!bmap->block2.data)
4184 return isl_basic_map_free(bmap);
4185 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
4186 if (!new_div)
4187 return isl_basic_map_free(bmap);
4188 for (i = 0; i < n; ++i) {
4189 new_div[i] = bmap->block2.data +
4190 (bmap->extra + i) * (1 + row_size);
4191 isl_seq_clr(new_div[i], 1 + row_size);
4192 }
4193 for (i = 0; i < bmap->extra; ++i)
4194 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
4195 free(bmap->div);
4196 bmap->div = new_div;
4197 bmap->n_div += n;
4198 bmap->extra += n;
4199
4200 return bmap;
4201}
4202
Tobias Grossera67ac972016-02-26 11:35:12 +00004203/* Drop constraints from "bmap" that only involve the variables
4204 * of "type" in the range [first, first + n] that are not related
4205 * to any of the variables outside that interval.
4206 * These constraints cannot influence the values for the variables
4207 * outside the interval, except in case they cause "bmap" to be empty.
4208 * Only drop the constraints if "bmap" is known to be non-empty.
4209 */
4210static __isl_give isl_basic_map *drop_irrelevant_constraints(
4211 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
4212 unsigned first, unsigned n)
4213{
4214 int i;
4215 int *groups;
4216 unsigned dim, n_div;
4217 isl_bool non_empty;
4218
4219 non_empty = isl_basic_map_plain_is_non_empty(bmap);
4220 if (non_empty < 0)
4221 return isl_basic_map_free(bmap);
4222 if (!non_empty)
4223 return bmap;
4224
4225 dim = isl_basic_map_dim(bmap, isl_dim_all);
4226 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4227 groups = isl_calloc_array(isl_basic_map_get_ctx(bmap), int, dim);
4228 if (!groups)
4229 return isl_basic_map_free(bmap);
4230 first += isl_basic_map_offset(bmap, type) - 1;
4231 for (i = 0; i < first; ++i)
4232 groups[i] = -1;
4233 for (i = first + n; i < dim - n_div; ++i)
4234 groups[i] = -1;
4235
4236 bmap = isl_basic_map_drop_unrelated_constraints(bmap, groups);
4237
4238 return bmap;
4239}
4240
Tobias Grosser52a25232015-02-04 20:55:43 +00004241/* Turn the n dimensions of type type, starting at first
4242 * into existentially quantified variables.
Tobias Grossera67ac972016-02-26 11:35:12 +00004243 *
4244 * If a subset of the projected out variables are unrelated
4245 * to any of the variables that remain, then the constraints
4246 * involving this subset are simply dropped first.
Tobias Grosser52a25232015-02-04 20:55:43 +00004247 */
4248__isl_give isl_basic_map *isl_basic_map_project_out(
4249 __isl_take isl_basic_map *bmap,
4250 enum isl_dim_type type, unsigned first, unsigned n)
4251{
Tobias Grosser1f7e7d32017-03-23 13:38:24 +00004252 isl_bool empty;
4253
Tobias Grosser52a25232015-02-04 20:55:43 +00004254 if (n == 0)
4255 return basic_map_space_reset(bmap, type);
Tobias Grossera67ac972016-02-26 11:35:12 +00004256 if (type == isl_dim_div)
4257 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4258 "cannot project out existentially quantified variables",
4259 return isl_basic_map_free(bmap));
Tobias Grosser52a25232015-02-04 20:55:43 +00004260
Tobias Grosser1f7e7d32017-03-23 13:38:24 +00004261 empty = isl_basic_map_plain_is_empty(bmap);
4262 if (empty < 0)
4263 return isl_basic_map_free(bmap);
4264 if (empty)
4265 bmap = isl_basic_map_set_to_empty(bmap);
4266
Tobias Grossera67ac972016-02-26 11:35:12 +00004267 bmap = drop_irrelevant_constraints(bmap, type, first, n);
Tobias Grosser52a25232015-02-04 20:55:43 +00004268 if (!bmap)
4269 return NULL;
4270
4271 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
4272 return isl_basic_map_remove_dims(bmap, type, first, n);
4273
Tobias Grosser94e53712016-12-31 07:46:11 +00004274 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
4275 return isl_basic_map_free(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +00004276
4277 bmap = move_last(bmap, type, first, n);
4278 bmap = isl_basic_map_cow(bmap);
4279 bmap = insert_div_rows(bmap, n);
4280 if (!bmap)
4281 return NULL;
4282
4283 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
4284 if (!bmap->dim)
4285 goto error;
4286 bmap = isl_basic_map_simplify(bmap);
4287 bmap = isl_basic_map_drop_redundant_divs(bmap);
4288 return isl_basic_map_finalize(bmap);
4289error:
4290 isl_basic_map_free(bmap);
4291 return NULL;
4292}
4293
4294/* Turn the n dimensions of type type, starting at first
4295 * into existentially quantified variables.
4296 */
4297struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
4298 enum isl_dim_type type, unsigned first, unsigned n)
4299{
Tobias Grosser06e15922016-11-16 11:06:47 +00004300 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset),
4301 type, first, n));
Tobias Grosser52a25232015-02-04 20:55:43 +00004302}
4303
4304/* Turn the n dimensions of type type, starting at first
4305 * into existentially quantified variables.
4306 */
4307__isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
4308 enum isl_dim_type type, unsigned first, unsigned n)
4309{
4310 int i;
4311
4312 if (!map)
4313 return NULL;
4314
4315 if (n == 0)
4316 return map_space_reset(map, type);
4317
4318 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
4319
4320 map = isl_map_cow(map);
4321 if (!map)
4322 return NULL;
4323
4324 map->dim = isl_space_drop_dims(map->dim, type, first, n);
4325 if (!map->dim)
4326 goto error;
4327
4328 for (i = 0; i < map->n; ++i) {
4329 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
4330 if (!map->p[i])
4331 goto error;
4332 }
4333
4334 return map;
4335error:
4336 isl_map_free(map);
4337 return NULL;
4338}
4339
4340/* Turn the n dimensions of type type, starting at first
4341 * into existentially quantified variables.
4342 */
4343__isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
4344 enum isl_dim_type type, unsigned first, unsigned n)
4345{
Tobias Grosser06e15922016-11-16 11:06:47 +00004346 return set_from_map(isl_map_project_out(set_to_map(set),
4347 type, first, n));
Tobias Grosser52a25232015-02-04 20:55:43 +00004348}
4349
Michael Krusee0b34f32016-05-04 14:41:36 +00004350/* Return a map that projects the elements in "set" onto their
4351 * "n" set dimensions starting at "first".
4352 * "type" should be equal to isl_dim_set.
4353 */
4354__isl_give isl_map *isl_set_project_onto_map(__isl_take isl_set *set,
4355 enum isl_dim_type type, unsigned first, unsigned n)
4356{
4357 int i;
4358 int dim;
4359 isl_map *map;
4360
4361 if (!set)
4362 return NULL;
4363 if (type != isl_dim_set)
4364 isl_die(isl_set_get_ctx(set), isl_error_invalid,
4365 "only set dimensions can be projected out", goto error);
4366 dim = isl_set_dim(set, isl_dim_set);
4367 if (first + n > dim || first + n < first)
4368 isl_die(isl_set_get_ctx(set), isl_error_invalid,
4369 "index out of bounds", goto error);
4370
4371 map = isl_map_from_domain(set);
4372 map = isl_map_add_dims(map, isl_dim_out, n);
4373 for (i = 0; i < n; ++i)
4374 map = isl_map_equate(map, isl_dim_in, first + i,
4375 isl_dim_out, i);
4376 return map;
4377error:
4378 isl_set_free(set);
4379 return NULL;
4380}
4381
Tobias Grosser52a25232015-02-04 20:55:43 +00004382static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
4383{
4384 int i, j;
4385
4386 for (i = 0; i < n; ++i) {
4387 j = isl_basic_map_alloc_div(bmap);
4388 if (j < 0)
4389 goto error;
4390 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
4391 }
4392 return bmap;
4393error:
4394 isl_basic_map_free(bmap);
4395 return NULL;
4396}
4397
4398struct isl_basic_map *isl_basic_map_apply_range(
4399 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4400{
4401 isl_space *dim_result = NULL;
4402 struct isl_basic_map *bmap;
4403 unsigned n_in, n_out, n, nparam, total, pos;
4404 struct isl_dim_map *dim_map1, *dim_map2;
4405
Tobias Grossere5671e52017-03-10 09:17:55 +00004406 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
Tobias Grosser52a25232015-02-04 20:55:43 +00004407 goto error;
Tobias Grosser52a25232015-02-04 20:55:43 +00004408 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_out,
4409 bmap2->dim, isl_dim_in))
4410 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4411 "spaces don't match", goto error);
4412
4413 dim_result = isl_space_join(isl_space_copy(bmap1->dim),
4414 isl_space_copy(bmap2->dim));
4415
Tobias Grosserf4fe34b2017-03-16 21:33:20 +00004416 n_in = isl_basic_map_dim(bmap1, isl_dim_in);
4417 n_out = isl_basic_map_dim(bmap2, isl_dim_out);
4418 n = isl_basic_map_dim(bmap1, isl_dim_out);
4419 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
Tobias Grosser52a25232015-02-04 20:55:43 +00004420
4421 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
4422 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4423 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
4424 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4425 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
4426 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4427 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
4428 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
4429 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4430 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4431 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4432
4433 bmap = isl_basic_map_alloc_space(dim_result,
4434 bmap1->n_div + bmap2->n_div + n,
4435 bmap1->n_eq + bmap2->n_eq,
4436 bmap1->n_ineq + bmap2->n_ineq);
4437 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4438 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4439 bmap = add_divs(bmap, n);
4440 bmap = isl_basic_map_simplify(bmap);
4441 bmap = isl_basic_map_drop_redundant_divs(bmap);
4442 return isl_basic_map_finalize(bmap);
4443error:
4444 isl_basic_map_free(bmap1);
4445 isl_basic_map_free(bmap2);
4446 return NULL;
4447}
4448
4449struct isl_basic_set *isl_basic_set_apply(
4450 struct isl_basic_set *bset, struct isl_basic_map *bmap)
4451{
4452 if (!bset || !bmap)
4453 goto error;
4454
4455 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
4456 goto error);
4457
Tobias Grosser06e15922016-11-16 11:06:47 +00004458 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset),
4459 bmap));
Tobias Grosser52a25232015-02-04 20:55:43 +00004460error:
4461 isl_basic_set_free(bset);
4462 isl_basic_map_free(bmap);
4463 return NULL;
4464}
4465
4466struct isl_basic_map *isl_basic_map_apply_domain(
4467 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4468{
Tobias Grossere5671e52017-03-10 09:17:55 +00004469 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
Tobias Grosser52a25232015-02-04 20:55:43 +00004470 goto error;
Tobias Grosser59d23bb2017-02-23 12:48:42 +00004471 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_in,
4472 bmap2->dim, isl_dim_in))
4473 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4474 "spaces don't match", goto error);
Tobias Grosser52a25232015-02-04 20:55:43 +00004475
4476 bmap1 = isl_basic_map_reverse(bmap1);
4477 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
4478 return isl_basic_map_reverse(bmap1);
4479error:
4480 isl_basic_map_free(bmap1);
4481 isl_basic_map_free(bmap2);
4482 return NULL;
4483}
4484
4485/* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
4486 * A \cap B -> f(A) + f(B)
4487 */
4488struct isl_basic_map *isl_basic_map_sum(
4489 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4490{
4491 unsigned n_in, n_out, nparam, total, pos;
4492 struct isl_basic_map *bmap = NULL;
4493 struct isl_dim_map *dim_map1, *dim_map2;
4494 int i;
4495
4496 if (!bmap1 || !bmap2)
4497 goto error;
4498
4499 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
4500 goto error);
4501
Tobias Grosserf4fe34b2017-03-16 21:33:20 +00004502 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
4503 n_in = isl_basic_map_dim(bmap1, isl_dim_in);
4504 n_out = isl_basic_map_dim(bmap1, isl_dim_out);
Tobias Grosser52a25232015-02-04 20:55:43 +00004505
4506 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
4507 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4508 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
4509 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4510 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
4511 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4512 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4513 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
4514 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4515 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4516 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
4517
4518 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
4519 bmap1->n_div + bmap2->n_div + 2 * n_out,
4520 bmap1->n_eq + bmap2->n_eq + n_out,
4521 bmap1->n_ineq + bmap2->n_ineq);
4522 for (i = 0; i < n_out; ++i) {
4523 int j = isl_basic_map_alloc_equality(bmap);
4524 if (j < 0)
4525 goto error;
4526 isl_seq_clr(bmap->eq[j], 1+total);
4527 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
4528 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
4529 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
4530 }
4531 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4532 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4533 bmap = add_divs(bmap, 2 * n_out);
4534
4535 bmap = isl_basic_map_simplify(bmap);
4536 return isl_basic_map_finalize(bmap);
4537error:
4538 isl_basic_map_free(bmap);
4539 isl_basic_map_free(bmap1);
4540 isl_basic_map_free(bmap2);
4541 return NULL;
4542}
4543
4544/* Given two maps A -> f(A) and B -> g(B), construct a map
4545 * A \cap B -> f(A) + f(B)
4546 */
4547struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
4548{
4549 struct isl_map *result;
4550 int i, j;
4551
4552 if (!map1 || !map2)
4553 goto error;
4554
4555 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
4556
4557 result = isl_map_alloc_space(isl_space_copy(map1->dim),
4558 map1->n * map2->n, 0);
4559 if (!result)
4560 goto error;
4561 for (i = 0; i < map1->n; ++i)
4562 for (j = 0; j < map2->n; ++j) {
4563 struct isl_basic_map *part;
4564 part = isl_basic_map_sum(
4565 isl_basic_map_copy(map1->p[i]),
4566 isl_basic_map_copy(map2->p[j]));
4567 if (isl_basic_map_is_empty(part))
4568 isl_basic_map_free(part);
4569 else
4570 result = isl_map_add_basic_map(result, part);
4571 if (!result)
4572 goto error;
4573 }
4574 isl_map_free(map1);
4575 isl_map_free(map2);
4576 return result;
4577error:
4578 isl_map_free(map1);
4579 isl_map_free(map2);
4580 return NULL;
4581}
4582
4583__isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
4584 __isl_take isl_set *set2)
4585{
Tobias Grosser06e15922016-11-16 11:06:47 +00004586 return set_from_map(isl_map_sum(set_to_map(set1), set_to_map(set2)));
Tobias Grosser52a25232015-02-04 20:55:43 +00004587}
4588
4589/* Given a basic map A -> f(A), construct A -> -f(A).
4590 */
4591struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
4592{
4593 int i, j;
4594 unsigned off, n;
4595
4596 bmap = isl_basic_map_cow(bmap);
4597 if (!bmap)
4598 return NULL;
4599
4600 n = isl_basic_map_dim(bmap, isl_dim_out);
4601 off = isl_basic_map_offset(bmap, isl_dim_out);
4602 for (i = 0; i < bmap->n_eq; ++i)
4603 for (j = 0; j < n; ++j)
4604 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
4605 for (i = 0; i < bmap->n_ineq; ++i)
4606 for (j = 0; j < n; ++j)
4607 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
4608 for (i = 0; i < bmap->n_div; ++i)
4609 for (j = 0; j < n; ++j)
4610 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
4611 bmap = isl_basic_map_gauss(bmap, NULL);
4612 return isl_basic_map_finalize(bmap);
4613}
4614
4615__isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
4616{
4617 return isl_basic_map_neg(bset);
4618}
4619
4620/* Given a map A -> f(A), construct A -> -f(A).
4621 */
4622struct isl_map *isl_map_neg(struct isl_map *map)
4623{
4624 int i;
4625
4626 map = isl_map_cow(map);
4627 if (!map)
4628 return NULL;
4629
4630 for (i = 0; i < map->n; ++i) {
4631 map->p[i] = isl_basic_map_neg(map->p[i]);
4632 if (!map->p[i])
4633 goto error;
4634 }
4635
4636 return map;
4637error:
4638 isl_map_free(map);
4639 return NULL;
4640}
4641
4642__isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
4643{
Tobias Grosser06e15922016-11-16 11:06:47 +00004644 return set_from_map(isl_map_neg(set_to_map(set)));
Tobias Grosser52a25232015-02-04 20:55:43 +00004645}
4646
4647/* Given a basic map A -> f(A) and an integer d, construct a basic map
4648 * A -> floor(f(A)/d).
4649 */
4650struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
4651 isl_int d)
4652{
4653 unsigned n_in, n_out, nparam, total, pos;
4654 struct isl_basic_map *result = NULL;
4655 struct isl_dim_map *dim_map;
4656 int i;
4657
4658 if (!bmap)
4659 return NULL;
4660
Tobias Grosserf4fe34b2017-03-16 21:33:20 +00004661 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4662 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4663 n_out = isl_basic_map_dim(bmap, isl_dim_out);
Tobias Grosser52a25232015-02-04 20:55:43 +00004664
4665 total = nparam + n_in + n_out + bmap->n_div + n_out;
4666 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4667 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
4668 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
4669 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
4670 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
4671
4672 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
4673 bmap->n_div + n_out,
4674 bmap->n_eq, bmap->n_ineq + 2 * n_out);
4675 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
4676 result = add_divs(result, n_out);
4677 for (i = 0; i < n_out; ++i) {
4678 int j;
4679 j = isl_basic_map_alloc_inequality(result);
4680 if (j < 0)
4681 goto error;
4682 isl_seq_clr(result->ineq[j], 1+total);
4683 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
4684 isl_int_set_si(result->ineq[j][1+pos+i], 1);
4685 j = isl_basic_map_alloc_inequality(result);
4686 if (j < 0)
4687 goto error;
4688 isl_seq_clr(result->ineq[j], 1+total);
4689 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
4690 isl_int_set_si(result->ineq[j][1+pos+i], -1);
4691 isl_int_sub_ui(result->ineq[j][0], d, 1);
4692 }
4693
4694 result = isl_basic_map_simplify(result);
4695 return isl_basic_map_finalize(result);
4696error:
4697 isl_basic_map_free(result);
4698 return NULL;
4699}
4700
4701/* Given a map A -> f(A) and an integer d, construct a map
4702 * A -> floor(f(A)/d).
4703 */
4704struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
4705{
4706 int i;
4707
4708 map = isl_map_cow(map);
4709 if (!map)
4710 return NULL;
4711
4712 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4713 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4714 for (i = 0; i < map->n; ++i) {
4715 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
4716 if (!map->p[i])
4717 goto error;
4718 }
4719
4720 return map;
4721error:
4722 isl_map_free(map);
4723 return NULL;
4724}
4725
4726/* Given a map A -> f(A) and an integer d, construct a map
4727 * A -> floor(f(A)/d).
4728 */
4729__isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
4730 __isl_take isl_val *d)
4731{
4732 if (!map || !d)
4733 goto error;
4734 if (!isl_val_is_int(d))
4735 isl_die(isl_val_get_ctx(d), isl_error_invalid,
4736 "expecting integer denominator", goto error);
4737 map = isl_map_floordiv(map, d->n);
4738 isl_val_free(d);
4739 return map;
4740error:
4741 isl_map_free(map);
4742 isl_val_free(d);
4743 return NULL;
4744}
4745
4746static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
4747{
4748 int i;
4749 unsigned nparam;
4750 unsigned n_in;
4751
4752 i = isl_basic_map_alloc_equality(bmap);
4753 if (i < 0)
4754 goto error;
Tobias Grosserf4fe34b2017-03-16 21:33:20 +00004755 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4756 n_in = isl_basic_map_dim(bmap, isl_dim_in);
Tobias Grosser52a25232015-02-04 20:55:43 +00004757 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
4758 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
4759 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
4760 return isl_basic_map_finalize(bmap);
4761error:
4762 isl_basic_map_free(bmap);
4763 return NULL;
4764}
4765
Tobias Grosser06e15922016-11-16 11:06:47 +00004766/* Add a constraint to "bmap" expressing i_pos < o_pos
Tobias Grosser52a25232015-02-04 20:55:43 +00004767 */
4768static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
4769{
4770 int i;
4771 unsigned nparam;
4772 unsigned n_in;
4773
4774 i = isl_basic_map_alloc_inequality(bmap);
4775 if (i < 0)
4776 goto error;
Tobias Grosserf4fe34b2017-03-16 21:33:20 +00004777 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4778 n_in = isl_basic_map_dim(bmap, isl_dim_in);
Tobias Grosser52a25232015-02-04 20:55:43 +00004779 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4780 isl_int_set_si(bmap->ineq[i][0], -1);
4781 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4782 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4783 return isl_basic_map_finalize(bmap);
4784error:
4785 isl_basic_map_free(bmap);
4786 return NULL;
4787}
4788
4789/* Add a constraint to "bmap" expressing i_pos <= o_pos
4790 */
4791static __isl_give isl_basic_map *var_less_or_equal(
4792 __isl_take isl_basic_map *bmap, unsigned pos)
4793{
4794 int i;
4795 unsigned nparam;
4796 unsigned n_in;
4797
4798 i = isl_basic_map_alloc_inequality(bmap);
4799 if (i < 0)
4800 goto error;
Tobias Grosserf4fe34b2017-03-16 21:33:20 +00004801 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4802 n_in = isl_basic_map_dim(bmap, isl_dim_in);
Tobias Grosser52a25232015-02-04 20:55:43 +00004803 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4804 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4805 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4806 return isl_basic_map_finalize(bmap);
4807error:
4808 isl_basic_map_free(bmap);
4809 return NULL;
4810}
4811
Tobias Grosser06e15922016-11-16 11:06:47 +00004812/* Add a constraint to "bmap" expressing i_pos > o_pos
Tobias Grosser52a25232015-02-04 20:55:43 +00004813 */
4814static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
4815{
4816 int i;
4817 unsigned nparam;
4818 unsigned n_in;
4819
4820 i = isl_basic_map_alloc_inequality(bmap);
4821 if (i < 0)
4822 goto error;
Tobias Grosserf4fe34b2017-03-16 21:33:20 +00004823 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4824 n_in = isl_basic_map_dim(bmap, isl_dim_in);
Tobias Grosser52a25232015-02-04 20:55:43 +00004825 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4826 isl_int_set_si(bmap->ineq[i][0], -1);
4827 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4828 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4829 return isl_basic_map_finalize(bmap);
4830error:
4831 isl_basic_map_free(bmap);
4832 return NULL;
4833}
4834
4835/* Add a constraint to "bmap" expressing i_pos >= o_pos
4836 */
4837static __isl_give isl_basic_map *var_more_or_equal(
4838 __isl_take isl_basic_map *bmap, unsigned pos)
4839{
4840 int i;
4841 unsigned nparam;
4842 unsigned n_in;
4843
4844 i = isl_basic_map_alloc_inequality(bmap);
4845 if (i < 0)
4846 goto error;
Tobias Grosserf4fe34b2017-03-16 21:33:20 +00004847 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4848 n_in = isl_basic_map_dim(bmap, isl_dim_in);
Tobias Grosser52a25232015-02-04 20:55:43 +00004849 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4850 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4851 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4852 return isl_basic_map_finalize(bmap);
4853error:
4854 isl_basic_map_free(bmap);
4855 return NULL;
4856}
4857
4858__isl_give isl_basic_map *isl_basic_map_equal(
4859 __isl_take isl_space *dim, unsigned n_equal)
4860{
4861 int i;
4862 struct isl_basic_map *bmap;
4863 bmap = isl_basic_map_alloc_space(dim, 0, n_equal, 0);
4864 if (!bmap)
4865 return NULL;
4866 for (i = 0; i < n_equal && bmap; ++i)
4867 bmap = var_equal(bmap, i);
4868 return isl_basic_map_finalize(bmap);
4869}
4870
4871/* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4872 */
4873__isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *dim,
4874 unsigned pos)
4875{
4876 int i;
4877 struct isl_basic_map *bmap;
4878 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4879 if (!bmap)
4880 return NULL;
4881 for (i = 0; i < pos && bmap; ++i)
4882 bmap = var_equal(bmap, i);
4883 if (bmap)
4884 bmap = var_less(bmap, pos);
4885 return isl_basic_map_finalize(bmap);
4886}
4887
Tobias Grosser06e15922016-11-16 11:06:47 +00004888/* Return a relation on "dim" expressing i_[0..pos] <<= o_[0..pos]
Tobias Grosser52a25232015-02-04 20:55:43 +00004889 */
4890__isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
4891 __isl_take isl_space *dim, unsigned pos)
4892{
4893 int i;
4894 isl_basic_map *bmap;
4895
4896 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4897 for (i = 0; i < pos; ++i)
4898 bmap = var_equal(bmap, i);
4899 bmap = var_less_or_equal(bmap, pos);
4900 return isl_basic_map_finalize(bmap);
4901}
4902
Tobias Grosser06e15922016-11-16 11:06:47 +00004903/* Return a relation on "dim" expressing i_pos > o_pos
Tobias Grosser52a25232015-02-04 20:55:43 +00004904 */
4905__isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *dim,
4906 unsigned pos)
4907{
4908 int i;
4909 struct isl_basic_map *bmap;
4910 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4911 if (!bmap)
4912 return NULL;
4913 for (i = 0; i < pos && bmap; ++i)
4914 bmap = var_equal(bmap, i);
4915 if (bmap)
4916 bmap = var_more(bmap, pos);
4917 return isl_basic_map_finalize(bmap);
4918}
4919
Tobias Grosser06e15922016-11-16 11:06:47 +00004920/* Return a relation on "dim" expressing i_[0..pos] >>= o_[0..pos]
Tobias Grosser52a25232015-02-04 20:55:43 +00004921 */
4922__isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
4923 __isl_take isl_space *dim, unsigned pos)
4924{
4925 int i;
4926 isl_basic_map *bmap;
4927
4928 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4929 for (i = 0; i < pos; ++i)
4930 bmap = var_equal(bmap, i);
4931 bmap = var_more_or_equal(bmap, pos);
4932 return isl_basic_map_finalize(bmap);
4933}
4934
4935static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *dims,
4936 unsigned n, int equal)
4937{
4938 struct isl_map *map;
4939 int i;
4940
4941 if (n == 0 && equal)
4942 return isl_map_universe(dims);
4943
4944 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4945
4946 for (i = 0; i + 1 < n; ++i)
4947 map = isl_map_add_basic_map(map,
4948 isl_basic_map_less_at(isl_space_copy(dims), i));
4949 if (n > 0) {
4950 if (equal)
4951 map = isl_map_add_basic_map(map,
4952 isl_basic_map_less_or_equal_at(dims, n - 1));
4953 else
4954 map = isl_map_add_basic_map(map,
4955 isl_basic_map_less_at(dims, n - 1));
4956 } else
4957 isl_space_free(dims);
4958
4959 return map;
4960}
4961
4962static __isl_give isl_map *map_lex_lte(__isl_take isl_space *dims, int equal)
4963{
4964 if (!dims)
4965 return NULL;
4966 return map_lex_lte_first(dims, dims->n_out, equal);
4967}
4968
4969__isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
4970{
4971 return map_lex_lte_first(dim, n, 0);
4972}
4973
4974__isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
4975{
4976 return map_lex_lte_first(dim, n, 1);
4977}
4978
4979__isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
4980{
4981 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
4982}
4983
4984__isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
4985{
4986 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
4987}
4988
4989static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *dims,
4990 unsigned n, int equal)
4991{
4992 struct isl_map *map;
4993 int i;
4994
4995 if (n == 0 && equal)
4996 return isl_map_universe(dims);
4997
4998 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4999
5000 for (i = 0; i + 1 < n; ++i)
5001 map = isl_map_add_basic_map(map,
5002 isl_basic_map_more_at(isl_space_copy(dims), i));
5003 if (n > 0) {
5004 if (equal)
5005 map = isl_map_add_basic_map(map,
5006 isl_basic_map_more_or_equal_at(dims, n - 1));
5007 else
5008 map = isl_map_add_basic_map(map,
5009 isl_basic_map_more_at(dims, n - 1));
5010 } else
5011 isl_space_free(dims);
5012
5013 return map;
5014}
5015
5016static __isl_give isl_map *map_lex_gte(__isl_take isl_space *dims, int equal)
5017{
5018 if (!dims)
5019 return NULL;
5020 return map_lex_gte_first(dims, dims->n_out, equal);
5021}
5022
5023__isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
5024{
5025 return map_lex_gte_first(dim, n, 0);
5026}
5027
5028__isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
5029{
5030 return map_lex_gte_first(dim, n, 1);
5031}
5032
5033__isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
5034{
5035 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
5036}
5037
5038__isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
5039{
5040 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
5041}
5042
5043__isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
5044 __isl_take isl_set *set2)
5045{
5046 isl_map *map;
5047 map = isl_map_lex_le(isl_set_get_space(set1));
5048 map = isl_map_intersect_domain(map, set1);
5049 map = isl_map_intersect_range(map, set2);
5050 return map;
5051}
5052
5053__isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
5054 __isl_take isl_set *set2)
5055{
5056 isl_map *map;
5057 map = isl_map_lex_lt(isl_set_get_space(set1));
5058 map = isl_map_intersect_domain(map, set1);
5059 map = isl_map_intersect_range(map, set2);
5060 return map;
5061}
5062
5063__isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
5064 __isl_take isl_set *set2)
5065{
5066 isl_map *map;
5067 map = isl_map_lex_ge(isl_set_get_space(set1));
5068 map = isl_map_intersect_domain(map, set1);
5069 map = isl_map_intersect_range(map, set2);
5070 return map;
5071}
5072
5073__isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
5074 __isl_take isl_set *set2)
5075{
5076 isl_map *map;
5077 map = isl_map_lex_gt(isl_set_get_space(set1));
5078 map = isl_map_intersect_domain(map, set1);
5079 map = isl_map_intersect_range(map, set2);
5080 return map;
5081}
5082
5083__isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
5084 __isl_take isl_map *map2)
5085{
5086 isl_map *map;
5087 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
5088 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5089 map = isl_map_apply_range(map, isl_map_reverse(map2));
5090 return map;
5091}
5092
5093__isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
5094 __isl_take isl_map *map2)
5095{
5096 isl_map *map;
5097 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
5098 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5099 map = isl_map_apply_range(map, isl_map_reverse(map2));
5100 return map;
5101}
5102
5103__isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
5104 __isl_take isl_map *map2)
5105{
5106 isl_map *map;
5107 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
5108 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5109 map = isl_map_apply_range(map, isl_map_reverse(map2));
5110 return map;
5111}
5112
5113__isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
5114 __isl_take isl_map *map2)
5115{
5116 isl_map *map;
5117 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
5118 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5119 map = isl_map_apply_range(map, isl_map_reverse(map2));
5120 return map;
5121}
5122
Tobias Grosser52a25232015-02-04 20:55:43 +00005123/* For a div d = floor(f/m), add the constraint
5124 *
5125 * f - m d >= 0
5126 */
Tobias Grosser72745c22017-02-17 05:11:16 +00005127static isl_stat add_upper_div_constraint(__isl_keep isl_basic_map *bmap,
Tobias Grosser52a25232015-02-04 20:55:43 +00005128 unsigned pos, isl_int *div)
5129{
5130 int i;
5131 unsigned total = isl_basic_map_total_dim(bmap);
5132
5133 i = isl_basic_map_alloc_inequality(bmap);
5134 if (i < 0)
Tobias Grosser72745c22017-02-17 05:11:16 +00005135 return isl_stat_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00005136 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
5137 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
5138
Tobias Grosser72745c22017-02-17 05:11:16 +00005139 return isl_stat_ok;
Tobias Grosser52a25232015-02-04 20:55:43 +00005140}
5141
5142/* For a div d = floor(f/m), add the constraint
5143 *
Tobias Grosser07b20952016-06-12 04:30:40 +00005144 * -(f-(m-1)) + m d >= 0
Tobias Grosser52a25232015-02-04 20:55:43 +00005145 */
Tobias Grosser72745c22017-02-17 05:11:16 +00005146static isl_stat add_lower_div_constraint(__isl_keep isl_basic_map *bmap,
Tobias Grosser52a25232015-02-04 20:55:43 +00005147 unsigned pos, isl_int *div)
5148{
5149 int i;
5150 unsigned total = isl_basic_map_total_dim(bmap);
5151
5152 i = isl_basic_map_alloc_inequality(bmap);
5153 if (i < 0)
Tobias Grosser72745c22017-02-17 05:11:16 +00005154 return isl_stat_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00005155 isl_seq_neg(bmap->ineq[i], div + 1, 1 + total);
5156 isl_int_set(bmap->ineq[i][1 + pos], div[0]);
5157 isl_int_add(bmap->ineq[i][0], bmap->ineq[i][0], bmap->ineq[i][1 + pos]);
5158 isl_int_sub_ui(bmap->ineq[i][0], bmap->ineq[i][0], 1);
5159
Tobias Grosser72745c22017-02-17 05:11:16 +00005160 return isl_stat_ok;
Tobias Grosser52a25232015-02-04 20:55:43 +00005161}
5162
5163/* For a div d = floor(f/m), add the constraints
5164 *
5165 * f - m d >= 0
Tobias Grosser07b20952016-06-12 04:30:40 +00005166 * -(f-(m-1)) + m d >= 0
Tobias Grosser52a25232015-02-04 20:55:43 +00005167 *
5168 * Note that the second constraint is the negation of
5169 *
Tobias Grosser07b20952016-06-12 04:30:40 +00005170 * f - m d >= m
Tobias Grosser52a25232015-02-04 20:55:43 +00005171 */
5172int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
5173 unsigned pos, isl_int *div)
5174{
5175 if (add_upper_div_constraint(bmap, pos, div) < 0)
5176 return -1;
5177 if (add_lower_div_constraint(bmap, pos, div) < 0)
5178 return -1;
5179 return 0;
5180}
5181
5182int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
5183 unsigned pos, isl_int *div)
5184{
Tobias Grosser06e15922016-11-16 11:06:47 +00005185 return isl_basic_map_add_div_constraints_var(bset_to_bmap(bset),
Tobias Grosser52a25232015-02-04 20:55:43 +00005186 pos, div);
5187}
5188
5189int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
5190{
5191 unsigned total = isl_basic_map_total_dim(bmap);
5192 unsigned div_pos = total - bmap->n_div + div;
5193
5194 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
5195 bmap->div[div]);
5196}
5197
5198/* For each known div d = floor(f/m), add the constraints
5199 *
5200 * f - m d >= 0
Tobias Grosser07b20952016-06-12 04:30:40 +00005201 * -(f-(m-1)) + m d >= 0
Michael Kruse959a8dc2016-01-15 15:54:45 +00005202 *
5203 * Remove duplicate constraints in case of some these div constraints
5204 * already appear in "bmap".
Tobias Grosser52a25232015-02-04 20:55:43 +00005205 */
5206__isl_give isl_basic_map *isl_basic_map_add_known_div_constraints(
5207 __isl_take isl_basic_map *bmap)
5208{
Tobias Grosser52a25232015-02-04 20:55:43 +00005209 unsigned n_div;
5210
5211 if (!bmap)
5212 return NULL;
5213 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5214 if (n_div == 0)
5215 return bmap;
Tobias Grosser52a25232015-02-04 20:55:43 +00005216
Michael Kruse959a8dc2016-01-15 15:54:45 +00005217 bmap = add_known_div_constraints(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +00005218 bmap = isl_basic_map_remove_duplicate_constraints(bmap, NULL, 0);
5219 bmap = isl_basic_map_finalize(bmap);
5220 return bmap;
5221}
5222
5223/* Add the div constraint of sign "sign" for div "div" of "bmap".
5224 *
5225 * In particular, if this div is of the form d = floor(f/m),
5226 * then add the constraint
5227 *
5228 * f - m d >= 0
5229 *
5230 * if sign < 0 or the constraint
5231 *
Tobias Grosser07b20952016-06-12 04:30:40 +00005232 * -(f-(m-1)) + m d >= 0
Tobias Grosser52a25232015-02-04 20:55:43 +00005233 *
5234 * if sign > 0.
5235 */
5236int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map *bmap,
5237 unsigned div, int sign)
5238{
5239 unsigned total;
5240 unsigned div_pos;
5241
5242 if (!bmap)
5243 return -1;
5244
5245 total = isl_basic_map_total_dim(bmap);
5246 div_pos = total - bmap->n_div + div;
5247
5248 if (sign < 0)
5249 return add_upper_div_constraint(bmap, div_pos, bmap->div[div]);
5250 else
5251 return add_lower_div_constraint(bmap, div_pos, bmap->div[div]);
5252}
5253
Tobias Grosser52a25232015-02-04 20:55:43 +00005254struct isl_basic_set *isl_basic_map_underlying_set(
5255 struct isl_basic_map *bmap)
5256{
5257 if (!bmap)
5258 goto error;
5259 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
5260 bmap->n_div == 0 &&
5261 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
5262 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
Tobias Grosser06e15922016-11-16 11:06:47 +00005263 return bset_from_bmap(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +00005264 bmap = isl_basic_map_cow(bmap);
5265 if (!bmap)
5266 goto error;
5267 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
5268 if (!bmap->dim)
5269 goto error;
5270 bmap->extra -= bmap->n_div;
5271 bmap->n_div = 0;
5272 bmap = isl_basic_map_finalize(bmap);
Tobias Grosser06e15922016-11-16 11:06:47 +00005273 return bset_from_bmap(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +00005274error:
5275 isl_basic_map_free(bmap);
5276 return NULL;
5277}
5278
5279__isl_give isl_basic_set *isl_basic_set_underlying_set(
5280 __isl_take isl_basic_set *bset)
5281{
Tobias Grosser06e15922016-11-16 11:06:47 +00005282 return isl_basic_map_underlying_set(bset_to_bmap(bset));
Tobias Grosser52a25232015-02-04 20:55:43 +00005283}
5284
5285/* Replace each element in "list" by the result of applying
Tobias Grosser1fa7b972015-02-16 19:33:40 +00005286 * isl_basic_map_underlying_set to the element.
Tobias Grosser52a25232015-02-04 20:55:43 +00005287 */
Tobias Grosser1fa7b972015-02-16 19:33:40 +00005288__isl_give isl_basic_set_list *isl_basic_map_list_underlying_set(
5289 __isl_take isl_basic_map_list *list)
Tobias Grosser52a25232015-02-04 20:55:43 +00005290{
5291 int i, n;
5292
5293 if (!list)
5294 return NULL;
5295
Tobias Grosser1fa7b972015-02-16 19:33:40 +00005296 n = isl_basic_map_list_n_basic_map(list);
Tobias Grosser52a25232015-02-04 20:55:43 +00005297 for (i = 0; i < n; ++i) {
Tobias Grosser1fa7b972015-02-16 19:33:40 +00005298 isl_basic_map *bmap;
Tobias Grosser52a25232015-02-04 20:55:43 +00005299 isl_basic_set *bset;
5300
Tobias Grosser1fa7b972015-02-16 19:33:40 +00005301 bmap = isl_basic_map_list_get_basic_map(list, i);
5302 bset = isl_basic_set_underlying_set(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +00005303 list = isl_basic_set_list_set_basic_set(list, i, bset);
5304 }
5305
5306 return list;
5307}
5308
5309struct isl_basic_map *isl_basic_map_overlying_set(
5310 struct isl_basic_set *bset, struct isl_basic_map *like)
5311{
5312 struct isl_basic_map *bmap;
5313 struct isl_ctx *ctx;
5314 unsigned total;
5315 int i;
5316
5317 if (!bset || !like)
5318 goto error;
5319 ctx = bset->ctx;
5320 isl_assert(ctx, bset->n_div == 0, goto error);
5321 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
5322 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
5323 goto error);
Tobias Grosser6e3ba332015-08-11 11:31:18 +00005324 if (like->n_div == 0) {
5325 isl_space *space = isl_basic_map_get_space(like);
Tobias Grosser52a25232015-02-04 20:55:43 +00005326 isl_basic_map_free(like);
Tobias Grosser6e3ba332015-08-11 11:31:18 +00005327 return isl_basic_map_reset_space(bset, space);
Tobias Grosser52a25232015-02-04 20:55:43 +00005328 }
5329 bset = isl_basic_set_cow(bset);
5330 if (!bset)
5331 goto error;
5332 total = bset->dim->n_out + bset->extra;
Tobias Grosser06e15922016-11-16 11:06:47 +00005333 bmap = bset_to_bmap(bset);
Tobias Grosser52a25232015-02-04 20:55:43 +00005334 isl_space_free(bmap->dim);
5335 bmap->dim = isl_space_copy(like->dim);
5336 if (!bmap->dim)
5337 goto error;
5338 bmap->n_div = like->n_div;
5339 bmap->extra += like->n_div;
5340 if (bmap->extra) {
5341 unsigned ltotal;
5342 isl_int **div;
5343 ltotal = total - bmap->extra + like->extra;
5344 if (ltotal > total)
5345 ltotal = total;
5346 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
5347 bmap->extra * (1 + 1 + total));
5348 if (isl_blk_is_error(bmap->block2))
5349 goto error;
5350 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
5351 if (!div)
5352 goto error;
5353 bmap->div = div;
5354 for (i = 0; i < bmap->extra; ++i)
5355 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
5356 for (i = 0; i < like->n_div; ++i) {
5357 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
5358 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
5359 }
5360 bmap = isl_basic_map_add_known_div_constraints(bmap);
5361 }
5362 isl_basic_map_free(like);
5363 bmap = isl_basic_map_simplify(bmap);
5364 bmap = isl_basic_map_finalize(bmap);
5365 return bmap;
5366error:
5367 isl_basic_map_free(like);
5368 isl_basic_set_free(bset);
5369 return NULL;
5370}
5371
5372struct isl_basic_set *isl_basic_set_from_underlying_set(
5373 struct isl_basic_set *bset, struct isl_basic_set *like)
5374{
Tobias Grosser06e15922016-11-16 11:06:47 +00005375 return bset_from_bmap(isl_basic_map_overlying_set(bset,
5376 bset_to_bmap(like)));
Tobias Grosser52a25232015-02-04 20:55:43 +00005377}
5378
Tobias Grosser52a25232015-02-04 20:55:43 +00005379struct isl_set *isl_map_underlying_set(struct isl_map *map)
5380{
5381 int i;
5382
5383 map = isl_map_cow(map);
5384 if (!map)
5385 return NULL;
5386 map->dim = isl_space_cow(map->dim);
5387 if (!map->dim)
5388 goto error;
5389
5390 for (i = 1; i < map->n; ++i)
5391 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
5392 goto error);
5393 for (i = 0; i < map->n; ++i) {
Tobias Grosser06e15922016-11-16 11:06:47 +00005394 map->p[i] = bset_to_bmap(
5395 isl_basic_map_underlying_set(map->p[i]));
Tobias Grosser52a25232015-02-04 20:55:43 +00005396 if (!map->p[i])
5397 goto error;
5398 }
5399 if (map->n == 0)
5400 map->dim = isl_space_underlying(map->dim, 0);
5401 else {
5402 isl_space_free(map->dim);
5403 map->dim = isl_space_copy(map->p[0]->dim);
5404 }
5405 if (!map->dim)
5406 goto error;
Tobias Grosser06e15922016-11-16 11:06:47 +00005407 return set_from_map(map);
Tobias Grosser52a25232015-02-04 20:55:43 +00005408error:
5409 isl_map_free(map);
5410 return NULL;
5411}
5412
Michael Kruse959a8dc2016-01-15 15:54:45 +00005413/* Replace the space of "bmap" by "space".
5414 *
5415 * If the space of "bmap" is identical to "space" (including the identifiers
5416 * of the input and output dimensions), then simply return the original input.
5417 */
Tobias Grosser52a25232015-02-04 20:55:43 +00005418__isl_give isl_basic_map *isl_basic_map_reset_space(
Tobias Grosser6e3ba332015-08-11 11:31:18 +00005419 __isl_take isl_basic_map *bmap, __isl_take isl_space *space)
Tobias Grosser52a25232015-02-04 20:55:43 +00005420{
Tobias Grosser6e3ba332015-08-11 11:31:18 +00005421 isl_bool equal;
Tobias Grossere5671e52017-03-10 09:17:55 +00005422 isl_space *bmap_space;
Tobias Grosser6e3ba332015-08-11 11:31:18 +00005423
Tobias Grossere5671e52017-03-10 09:17:55 +00005424 bmap_space = isl_basic_map_peek_space(bmap);
5425 equal = isl_space_is_equal(bmap_space, space);
Michael Kruse959a8dc2016-01-15 15:54:45 +00005426 if (equal >= 0 && equal)
Tobias Grosser593ebdf2017-03-14 07:33:26 +00005427 equal = isl_space_has_equal_ids(bmap_space, space);
Tobias Grosser6e3ba332015-08-11 11:31:18 +00005428 if (equal < 0)
5429 goto error;
5430 if (equal) {
5431 isl_space_free(space);
5432 return bmap;
5433 }
Tobias Grosser52a25232015-02-04 20:55:43 +00005434 bmap = isl_basic_map_cow(bmap);
Tobias Grosser6e3ba332015-08-11 11:31:18 +00005435 if (!bmap || !space)
Tobias Grosser52a25232015-02-04 20:55:43 +00005436 goto error;
5437
5438 isl_space_free(bmap->dim);
Tobias Grosser6e3ba332015-08-11 11:31:18 +00005439 bmap->dim = space;
Tobias Grosser52a25232015-02-04 20:55:43 +00005440
5441 bmap = isl_basic_map_finalize(bmap);
5442
5443 return bmap;
5444error:
5445 isl_basic_map_free(bmap);
Tobias Grosser6e3ba332015-08-11 11:31:18 +00005446 isl_space_free(space);
Tobias Grosser52a25232015-02-04 20:55:43 +00005447 return NULL;
5448}
5449
5450__isl_give isl_basic_set *isl_basic_set_reset_space(
5451 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
5452{
Tobias Grosser06e15922016-11-16 11:06:47 +00005453 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset),
5454 dim));
Tobias Grosser52a25232015-02-04 20:55:43 +00005455}
5456
5457__isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
5458 __isl_take isl_space *dim)
5459{
5460 int i;
5461
5462 map = isl_map_cow(map);
5463 if (!map || !dim)
5464 goto error;
5465
5466 for (i = 0; i < map->n; ++i) {
5467 map->p[i] = isl_basic_map_reset_space(map->p[i],
5468 isl_space_copy(dim));
5469 if (!map->p[i])
5470 goto error;
5471 }
5472 isl_space_free(map->dim);
5473 map->dim = dim;
5474
5475 return map;
5476error:
5477 isl_map_free(map);
5478 isl_space_free(dim);
5479 return NULL;
5480}
5481
5482__isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
5483 __isl_take isl_space *dim)
5484{
Tobias Grosser06e15922016-11-16 11:06:47 +00005485 return set_from_map(isl_map_reset_space(set_to_map(set), dim));
Tobias Grosser52a25232015-02-04 20:55:43 +00005486}
5487
5488/* Compute the parameter domain of the given basic set.
5489 */
5490__isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
5491{
Tobias Grosser72745c22017-02-17 05:11:16 +00005492 isl_bool is_params;
Tobias Grosser52a25232015-02-04 20:55:43 +00005493 isl_space *space;
5494 unsigned n;
5495
Tobias Grosser72745c22017-02-17 05:11:16 +00005496 is_params = isl_basic_set_is_params(bset);
5497 if (is_params < 0)
5498 return isl_basic_set_free(bset);
5499 if (is_params)
Tobias Grosser52a25232015-02-04 20:55:43 +00005500 return bset;
5501
5502 n = isl_basic_set_dim(bset, isl_dim_set);
5503 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
5504 space = isl_basic_set_get_space(bset);
5505 space = isl_space_params(space);
5506 bset = isl_basic_set_reset_space(bset, space);
5507 return bset;
5508}
5509
5510/* Construct a zero-dimensional basic set with the given parameter domain.
5511 */
5512__isl_give isl_basic_set *isl_basic_set_from_params(
5513 __isl_take isl_basic_set *bset)
5514{
5515 isl_space *space;
5516 space = isl_basic_set_get_space(bset);
5517 space = isl_space_set_from_params(space);
5518 bset = isl_basic_set_reset_space(bset, space);
5519 return bset;
5520}
5521
5522/* Compute the parameter domain of the given set.
5523 */
5524__isl_give isl_set *isl_set_params(__isl_take isl_set *set)
5525{
5526 isl_space *space;
5527 unsigned n;
5528
5529 if (isl_set_is_params(set))
5530 return set;
5531
5532 n = isl_set_dim(set, isl_dim_set);
5533 set = isl_set_project_out(set, isl_dim_set, 0, n);
5534 space = isl_set_get_space(set);
5535 space = isl_space_params(space);
5536 set = isl_set_reset_space(set, space);
5537 return set;
5538}
5539
5540/* Construct a zero-dimensional set with the given parameter domain.
5541 */
5542__isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
5543{
5544 isl_space *space;
5545 space = isl_set_get_space(set);
5546 space = isl_space_set_from_params(space);
5547 set = isl_set_reset_space(set, space);
5548 return set;
5549}
5550
5551/* Compute the parameter domain of the given map.
5552 */
5553__isl_give isl_set *isl_map_params(__isl_take isl_map *map)
5554{
5555 isl_space *space;
5556 unsigned n;
5557
5558 n = isl_map_dim(map, isl_dim_in);
5559 map = isl_map_project_out(map, isl_dim_in, 0, n);
5560 n = isl_map_dim(map, isl_dim_out);
5561 map = isl_map_project_out(map, isl_dim_out, 0, n);
5562 space = isl_map_get_space(map);
5563 space = isl_space_params(space);
5564 map = isl_map_reset_space(map, space);
5565 return map;
5566}
5567
5568struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
5569{
Tobias Grosserb2f39922015-05-28 13:32:11 +00005570 isl_space *space;
Tobias Grosser52a25232015-02-04 20:55:43 +00005571 unsigned n_out;
5572
5573 if (!bmap)
5574 return NULL;
Tobias Grosserb2f39922015-05-28 13:32:11 +00005575 space = isl_space_domain(isl_basic_map_get_space(bmap));
Tobias Grosser52a25232015-02-04 20:55:43 +00005576
Tobias Grosserf4fe34b2017-03-16 21:33:20 +00005577 n_out = isl_basic_map_dim(bmap, isl_dim_out);
Tobias Grosserb2f39922015-05-28 13:32:11 +00005578 bmap = isl_basic_map_project_out(bmap, isl_dim_out, 0, n_out);
Tobias Grosser52a25232015-02-04 20:55:43 +00005579
Tobias Grosserb2f39922015-05-28 13:32:11 +00005580 return isl_basic_map_reset_space(bmap, space);
Tobias Grosser52a25232015-02-04 20:55:43 +00005581}
5582
Tobias Grosser72745c22017-02-17 05:11:16 +00005583isl_bool isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
Tobias Grosser52a25232015-02-04 20:55:43 +00005584{
5585 if (!bmap)
Tobias Grosser72745c22017-02-17 05:11:16 +00005586 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00005587 return isl_space_may_be_set(bmap->dim);
5588}
5589
5590/* Is this basic map actually a set?
5591 * Users should never call this function. Outside of isl,
5592 * the type should indicate whether something is a set or a map.
5593 */
Tobias Grosser72745c22017-02-17 05:11:16 +00005594isl_bool isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
Tobias Grosser52a25232015-02-04 20:55:43 +00005595{
5596 if (!bmap)
Tobias Grosser72745c22017-02-17 05:11:16 +00005597 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00005598 return isl_space_is_set(bmap->dim);
5599}
5600
5601struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
5602{
Tobias Grosser72745c22017-02-17 05:11:16 +00005603 isl_bool is_set;
5604
5605 is_set = isl_basic_map_is_set(bmap);
5606 if (is_set < 0)
5607 goto error;
5608 if (is_set)
Tobias Grosser52a25232015-02-04 20:55:43 +00005609 return bmap;
5610 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
Tobias Grosser72745c22017-02-17 05:11:16 +00005611error:
5612 isl_basic_map_free(bmap);
5613 return NULL;
Tobias Grosser52a25232015-02-04 20:55:43 +00005614}
5615
5616__isl_give isl_basic_map *isl_basic_map_domain_map(
5617 __isl_take isl_basic_map *bmap)
5618{
Tobias Grosser72745c22017-02-17 05:11:16 +00005619 int i;
Tobias Grosser52a25232015-02-04 20:55:43 +00005620 isl_space *dim;
5621 isl_basic_map *domain;
5622 int nparam, n_in, n_out;
Tobias Grosser52a25232015-02-04 20:55:43 +00005623
5624 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5625 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5626 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5627
5628 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
5629 domain = isl_basic_map_universe(dim);
5630
5631 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5632 bmap = isl_basic_map_apply_range(bmap, domain);
5633 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
5634
Tobias Grosser72745c22017-02-17 05:11:16 +00005635 for (i = 0; i < n_in; ++i)
5636 bmap = isl_basic_map_equate(bmap, isl_dim_in, i,
5637 isl_dim_out, i);
Tobias Grosser52a25232015-02-04 20:55:43 +00005638
5639 bmap = isl_basic_map_gauss(bmap, NULL);
5640 return isl_basic_map_finalize(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +00005641}
5642
5643__isl_give isl_basic_map *isl_basic_map_range_map(
5644 __isl_take isl_basic_map *bmap)
5645{
Tobias Grosser72745c22017-02-17 05:11:16 +00005646 int i;
Tobias Grosser52a25232015-02-04 20:55:43 +00005647 isl_space *dim;
5648 isl_basic_map *range;
5649 int nparam, n_in, n_out;
Tobias Grosser52a25232015-02-04 20:55:43 +00005650
5651 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5652 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5653 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5654
5655 dim = isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap)));
5656 range = isl_basic_map_universe(dim);
5657
5658 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5659 bmap = isl_basic_map_apply_range(bmap, range);
5660 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
5661
Tobias Grosser72745c22017-02-17 05:11:16 +00005662 for (i = 0; i < n_out; ++i)
5663 bmap = isl_basic_map_equate(bmap, isl_dim_in, n_in + i,
5664 isl_dim_out, i);
Tobias Grosser52a25232015-02-04 20:55:43 +00005665
5666 bmap = isl_basic_map_gauss(bmap, NULL);
5667 return isl_basic_map_finalize(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +00005668}
5669
5670int isl_map_may_be_set(__isl_keep isl_map *map)
5671{
5672 if (!map)
5673 return -1;
5674 return isl_space_may_be_set(map->dim);
5675}
5676
5677/* Is this map actually a set?
5678 * Users should never call this function. Outside of isl,
5679 * the type should indicate whether something is a set or a map.
5680 */
Tobias Grosser72745c22017-02-17 05:11:16 +00005681isl_bool isl_map_is_set(__isl_keep isl_map *map)
Tobias Grosser52a25232015-02-04 20:55:43 +00005682{
5683 if (!map)
Tobias Grosser72745c22017-02-17 05:11:16 +00005684 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00005685 return isl_space_is_set(map->dim);
5686}
5687
5688struct isl_set *isl_map_range(struct isl_map *map)
5689{
5690 int i;
Tobias Grosser72745c22017-02-17 05:11:16 +00005691 isl_bool is_set;
Tobias Grosser52a25232015-02-04 20:55:43 +00005692 struct isl_set *set;
5693
Tobias Grosser72745c22017-02-17 05:11:16 +00005694 is_set = isl_map_is_set(map);
5695 if (is_set < 0)
Tobias Grosser52a25232015-02-04 20:55:43 +00005696 goto error;
Tobias Grosser72745c22017-02-17 05:11:16 +00005697 if (is_set)
Tobias Grosser06e15922016-11-16 11:06:47 +00005698 return set_from_map(map);
Tobias Grosser52a25232015-02-04 20:55:43 +00005699
5700 map = isl_map_cow(map);
5701 if (!map)
5702 goto error;
5703
Tobias Grosser06e15922016-11-16 11:06:47 +00005704 set = set_from_map(map);
Tobias Grosser52a25232015-02-04 20:55:43 +00005705 set->dim = isl_space_range(set->dim);
5706 if (!set->dim)
5707 goto error;
5708 for (i = 0; i < map->n; ++i) {
5709 set->p[i] = isl_basic_map_range(map->p[i]);
5710 if (!set->p[i])
5711 goto error;
5712 }
5713 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5714 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5715 return set;
5716error:
5717 isl_map_free(map);
5718 return NULL;
5719}
5720
5721__isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
5722{
5723 int i;
5724
5725 map = isl_map_cow(map);
5726 if (!map)
5727 return NULL;
5728
5729 map->dim = isl_space_domain_map(map->dim);
5730 if (!map->dim)
5731 goto error;
5732 for (i = 0; i < map->n; ++i) {
5733 map->p[i] = isl_basic_map_domain_map(map->p[i]);
5734 if (!map->p[i])
5735 goto error;
5736 }
5737 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5738 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5739 return map;
5740error:
5741 isl_map_free(map);
5742 return NULL;
5743}
5744
5745__isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
5746{
5747 int i;
5748 isl_space *range_dim;
5749
5750 map = isl_map_cow(map);
5751 if (!map)
5752 return NULL;
5753
5754 range_dim = isl_space_range(isl_map_get_space(map));
5755 range_dim = isl_space_from_range(range_dim);
5756 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
5757 map->dim = isl_space_join(map->dim, range_dim);
5758 if (!map->dim)
5759 goto error;
5760 for (i = 0; i < map->n; ++i) {
5761 map->p[i] = isl_basic_map_range_map(map->p[i]);
5762 if (!map->p[i])
5763 goto error;
5764 }
5765 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5766 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5767 return map;
5768error:
5769 isl_map_free(map);
5770 return NULL;
5771}
5772
5773/* Given a wrapped map of the form A[B -> C],
5774 * return the map A[B -> C] -> B.
5775 */
5776__isl_give isl_map *isl_set_wrapped_domain_map(__isl_take isl_set *set)
5777{
5778 isl_id *id;
5779 isl_map *map;
5780
5781 if (!set)
5782 return NULL;
5783 if (!isl_set_has_tuple_id(set))
5784 return isl_map_domain_map(isl_set_unwrap(set));
5785
5786 id = isl_set_get_tuple_id(set);
5787 map = isl_map_domain_map(isl_set_unwrap(set));
5788 map = isl_map_set_tuple_id(map, isl_dim_in, id);
5789
5790 return map;
5791}
5792
Tobias Grosser52a25232015-02-04 20:55:43 +00005793__isl_give isl_basic_map *isl_basic_map_from_domain(
5794 __isl_take isl_basic_set *bset)
5795{
5796 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
5797}
5798
5799__isl_give isl_basic_map *isl_basic_map_from_range(
5800 __isl_take isl_basic_set *bset)
5801{
5802 isl_space *space;
5803 space = isl_basic_set_get_space(bset);
5804 space = isl_space_from_range(space);
5805 bset = isl_basic_set_reset_space(bset, space);
Tobias Grosser06e15922016-11-16 11:06:47 +00005806 return bset_to_bmap(bset);
Tobias Grosser52a25232015-02-04 20:55:43 +00005807}
5808
5809/* Create a relation with the given set as range.
5810 * The domain of the created relation is a zero-dimensional
5811 * flat anonymous space.
5812 */
5813__isl_give isl_map *isl_map_from_range(__isl_take isl_set *set)
5814{
5815 isl_space *space;
5816 space = isl_set_get_space(set);
5817 space = isl_space_from_range(space);
5818 set = isl_set_reset_space(set, space);
Tobias Grosser06e15922016-11-16 11:06:47 +00005819 return set_to_map(set);
Tobias Grosser52a25232015-02-04 20:55:43 +00005820}
5821
5822/* Create a relation with the given set as domain.
5823 * The range of the created relation is a zero-dimensional
5824 * flat anonymous space.
5825 */
5826__isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
5827{
5828 return isl_map_reverse(isl_map_from_range(set));
5829}
5830
5831__isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
5832 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
5833{
5834 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
5835}
5836
5837__isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
5838 __isl_take isl_set *range)
5839{
5840 return isl_map_apply_range(isl_map_reverse(domain), range);
5841}
5842
Tobias Grosser07b20952016-06-12 04:30:40 +00005843/* Return a newly allocated isl_map with given space and flags and
5844 * room for "n" basic maps.
5845 * Make sure that all cached information is cleared.
5846 */
5847__isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *space, int n,
Tobias Grosser52a25232015-02-04 20:55:43 +00005848 unsigned flags)
5849{
5850 struct isl_map *map;
5851
Tobias Grosser07b20952016-06-12 04:30:40 +00005852 if (!space)
Tobias Grosser52a25232015-02-04 20:55:43 +00005853 return NULL;
5854 if (n < 0)
Tobias Grosser07b20952016-06-12 04:30:40 +00005855 isl_die(space->ctx, isl_error_internal,
Tobias Grosser52a25232015-02-04 20:55:43 +00005856 "negative number of basic maps", goto error);
Tobias Grosser07b20952016-06-12 04:30:40 +00005857 map = isl_calloc(space->ctx, struct isl_map,
Tobias Grosser52a25232015-02-04 20:55:43 +00005858 sizeof(struct isl_map) +
5859 (n - 1) * sizeof(struct isl_basic_map *));
5860 if (!map)
5861 goto error;
5862
Tobias Grosser07b20952016-06-12 04:30:40 +00005863 map->ctx = space->ctx;
Tobias Grosser52a25232015-02-04 20:55:43 +00005864 isl_ctx_ref(map->ctx);
5865 map->ref = 1;
5866 map->size = n;
5867 map->n = 0;
Tobias Grosser07b20952016-06-12 04:30:40 +00005868 map->dim = space;
Tobias Grosser52a25232015-02-04 20:55:43 +00005869 map->flags = flags;
5870 return map;
5871error:
Tobias Grosser07b20952016-06-12 04:30:40 +00005872 isl_space_free(space);
Tobias Grosser52a25232015-02-04 20:55:43 +00005873 return NULL;
5874}
5875
Tobias Grosser52a25232015-02-04 20:55:43 +00005876__isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *dim)
5877{
5878 struct isl_basic_map *bmap;
5879 bmap = isl_basic_map_alloc_space(dim, 0, 1, 0);
5880 bmap = isl_basic_map_set_to_empty(bmap);
5881 return bmap;
5882}
5883
5884__isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *dim)
5885{
5886 struct isl_basic_set *bset;
5887 bset = isl_basic_set_alloc_space(dim, 0, 1, 0);
5888 bset = isl_basic_set_set_to_empty(bset);
5889 return bset;
5890}
5891
Tobias Grosser52a25232015-02-04 20:55:43 +00005892__isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *dim)
5893{
5894 struct isl_basic_map *bmap;
5895 bmap = isl_basic_map_alloc_space(dim, 0, 0, 0);
5896 bmap = isl_basic_map_finalize(bmap);
5897 return bmap;
5898}
5899
5900__isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *dim)
5901{
5902 struct isl_basic_set *bset;
5903 bset = isl_basic_set_alloc_space(dim, 0, 0, 0);
5904 bset = isl_basic_set_finalize(bset);
5905 return bset;
5906}
5907
5908__isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_space *dim)
5909{
5910 int i;
5911 unsigned total = isl_space_dim(dim, isl_dim_all);
5912 isl_basic_map *bmap;
5913
5914 bmap= isl_basic_map_alloc_space(dim, 0, 0, total);
5915 for (i = 0; i < total; ++i) {
5916 int k = isl_basic_map_alloc_inequality(bmap);
5917 if (k < 0)
5918 goto error;
5919 isl_seq_clr(bmap->ineq[k], 1 + total);
5920 isl_int_set_si(bmap->ineq[k][1 + i], 1);
5921 }
5922 return bmap;
5923error:
5924 isl_basic_map_free(bmap);
5925 return NULL;
5926}
5927
5928__isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_space *dim)
5929{
5930 return isl_basic_map_nat_universe(dim);
5931}
5932
5933__isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
5934{
5935 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
5936}
5937
5938__isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
5939{
5940 return isl_map_nat_universe(dim);
5941}
5942
Tobias Grosser52a25232015-02-04 20:55:43 +00005943__isl_give isl_map *isl_map_empty(__isl_take isl_space *dim)
5944{
5945 return isl_map_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5946}
5947
Tobias Grosser52a25232015-02-04 20:55:43 +00005948__isl_give isl_set *isl_set_empty(__isl_take isl_space *dim)
5949{
5950 return isl_set_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5951}
5952
Tobias Grosser52a25232015-02-04 20:55:43 +00005953__isl_give isl_map *isl_map_universe(__isl_take isl_space *dim)
5954{
5955 struct isl_map *map;
5956 if (!dim)
5957 return NULL;
5958 map = isl_map_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5959 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
5960 return map;
5961}
5962
5963__isl_give isl_set *isl_set_universe(__isl_take isl_space *dim)
5964{
5965 struct isl_set *set;
5966 if (!dim)
5967 return NULL;
5968 set = isl_set_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5969 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
5970 return set;
5971}
5972
Tobias Grosser52a25232015-02-04 20:55:43 +00005973struct isl_map *isl_map_dup(struct isl_map *map)
5974{
5975 int i;
5976 struct isl_map *dup;
5977
5978 if (!map)
5979 return NULL;
5980 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
5981 for (i = 0; i < map->n; ++i)
5982 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
5983 return dup;
5984}
5985
5986__isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
5987 __isl_take isl_basic_map *bmap)
5988{
5989 if (!bmap || !map)
5990 goto error;
5991 if (isl_basic_map_plain_is_empty(bmap)) {
5992 isl_basic_map_free(bmap);
5993 return map;
5994 }
5995 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
5996 isl_assert(map->ctx, map->n < map->size, goto error);
5997 map->p[map->n] = bmap;
5998 map->n++;
5999 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6000 return map;
6001error:
6002 if (map)
6003 isl_map_free(map);
6004 if (bmap)
6005 isl_basic_map_free(bmap);
6006 return NULL;
6007}
6008
6009__isl_null isl_map *isl_map_free(__isl_take isl_map *map)
6010{
6011 int i;
6012
6013 if (!map)
6014 return NULL;
6015
6016 if (--map->ref > 0)
6017 return NULL;
6018
Tobias Grosser07b20952016-06-12 04:30:40 +00006019 clear_caches(map);
Tobias Grosser52a25232015-02-04 20:55:43 +00006020 isl_ctx_deref(map->ctx);
6021 for (i = 0; i < map->n; ++i)
6022 isl_basic_map_free(map->p[i]);
6023 isl_space_free(map->dim);
6024 free(map);
6025
6026 return NULL;
6027}
6028
Tobias Grosser52a25232015-02-04 20:55:43 +00006029static struct isl_basic_map *isl_basic_map_fix_pos_si(
6030 struct isl_basic_map *bmap, unsigned pos, int value)
6031{
6032 int j;
6033
6034 bmap = isl_basic_map_cow(bmap);
6035 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
6036 j = isl_basic_map_alloc_equality(bmap);
6037 if (j < 0)
6038 goto error;
6039 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
6040 isl_int_set_si(bmap->eq[j][pos], -1);
6041 isl_int_set_si(bmap->eq[j][0], value);
6042 bmap = isl_basic_map_simplify(bmap);
6043 return isl_basic_map_finalize(bmap);
6044error:
6045 isl_basic_map_free(bmap);
6046 return NULL;
6047}
6048
6049static __isl_give isl_basic_map *isl_basic_map_fix_pos(
6050 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
6051{
6052 int j;
6053
6054 bmap = isl_basic_map_cow(bmap);
6055 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
6056 j = isl_basic_map_alloc_equality(bmap);
6057 if (j < 0)
6058 goto error;
6059 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
6060 isl_int_set_si(bmap->eq[j][pos], -1);
6061 isl_int_set(bmap->eq[j][0], value);
6062 bmap = isl_basic_map_simplify(bmap);
6063 return isl_basic_map_finalize(bmap);
6064error:
6065 isl_basic_map_free(bmap);
6066 return NULL;
6067}
6068
6069struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
6070 enum isl_dim_type type, unsigned pos, int value)
6071{
Tobias Grosser94e53712016-12-31 07:46:11 +00006072 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6073 return isl_basic_map_free(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +00006074 return isl_basic_map_fix_pos_si(bmap,
6075 isl_basic_map_offset(bmap, type) + pos, value);
Tobias Grosser52a25232015-02-04 20:55:43 +00006076}
6077
6078__isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
6079 enum isl_dim_type type, unsigned pos, isl_int value)
6080{
Tobias Grosser94e53712016-12-31 07:46:11 +00006081 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6082 return isl_basic_map_free(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +00006083 return isl_basic_map_fix_pos(bmap,
6084 isl_basic_map_offset(bmap, type) + pos, value);
Tobias Grosser52a25232015-02-04 20:55:43 +00006085}
6086
6087/* Fix the value of the variable at position "pos" of type "type" of "bmap"
6088 * to be equal to "v".
6089 */
6090__isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
6091 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6092{
6093 if (!bmap || !v)
6094 goto error;
6095 if (!isl_val_is_int(v))
6096 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
6097 "expecting integer value", goto error);
Tobias Grosser94e53712016-12-31 07:46:11 +00006098 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6099 goto error;
Tobias Grosser52a25232015-02-04 20:55:43 +00006100 pos += isl_basic_map_offset(bmap, type);
6101 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
6102 isl_val_free(v);
6103 return bmap;
6104error:
6105 isl_basic_map_free(bmap);
6106 isl_val_free(v);
6107 return NULL;
6108}
6109
6110/* Fix the value of the variable at position "pos" of type "type" of "bset"
6111 * to be equal to "v".
6112 */
6113__isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
6114 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6115{
6116 return isl_basic_map_fix_val(bset, type, pos, v);
6117}
6118
6119struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
6120 enum isl_dim_type type, unsigned pos, int value)
6121{
Tobias Grosser06e15922016-11-16 11:06:47 +00006122 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
6123 type, pos, value));
Tobias Grosser52a25232015-02-04 20:55:43 +00006124}
6125
6126__isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
6127 enum isl_dim_type type, unsigned pos, isl_int value)
6128{
Tobias Grosser06e15922016-11-16 11:06:47 +00006129 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset),
6130 type, pos, value));
Tobias Grosser52a25232015-02-04 20:55:43 +00006131}
6132
6133struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
6134 unsigned input, int value)
6135{
6136 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
6137}
6138
6139struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
6140 unsigned dim, int value)
6141{
Tobias Grosser06e15922016-11-16 11:06:47 +00006142 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
6143 isl_dim_set, dim, value));
Tobias Grosser52a25232015-02-04 20:55:43 +00006144}
6145
6146static int remove_if_empty(__isl_keep isl_map *map, int i)
6147{
6148 int empty = isl_basic_map_plain_is_empty(map->p[i]);
6149
6150 if (empty < 0)
6151 return -1;
6152 if (!empty)
6153 return 0;
6154
6155 isl_basic_map_free(map->p[i]);
6156 if (i != map->n - 1) {
6157 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6158 map->p[i] = map->p[map->n - 1];
6159 }
6160 map->n--;
6161
6162 return 0;
6163}
6164
6165/* Perform "fn" on each basic map of "map", where we may not be holding
6166 * the only reference to "map".
6167 * In particular, "fn" should be a semantics preserving operation
6168 * that we want to apply to all copies of "map". We therefore need
6169 * to be careful not to modify "map" in a way that breaks "map"
6170 * in case anything goes wrong.
6171 */
6172__isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
6173 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
6174{
6175 struct isl_basic_map *bmap;
6176 int i;
6177
6178 if (!map)
6179 return NULL;
6180
6181 for (i = map->n - 1; i >= 0; --i) {
6182 bmap = isl_basic_map_copy(map->p[i]);
6183 bmap = fn(bmap);
6184 if (!bmap)
6185 goto error;
6186 isl_basic_map_free(map->p[i]);
6187 map->p[i] = bmap;
6188 if (remove_if_empty(map, i) < 0)
6189 goto error;
6190 }
6191
6192 return map;
6193error:
6194 isl_map_free(map);
6195 return NULL;
6196}
6197
6198struct isl_map *isl_map_fix_si(struct isl_map *map,
6199 enum isl_dim_type type, unsigned pos, int value)
6200{
6201 int i;
6202
6203 map = isl_map_cow(map);
6204 if (!map)
6205 return NULL;
6206
6207 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
6208 for (i = map->n - 1; i >= 0; --i) {
6209 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
6210 if (remove_if_empty(map, i) < 0)
6211 goto error;
6212 }
6213 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6214 return map;
6215error:
6216 isl_map_free(map);
6217 return NULL;
6218}
6219
6220__isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
6221 enum isl_dim_type type, unsigned pos, int value)
6222{
Tobias Grosser06e15922016-11-16 11:06:47 +00006223 return set_from_map(isl_map_fix_si(set_to_map(set), type, pos, value));
Tobias Grosser52a25232015-02-04 20:55:43 +00006224}
6225
6226__isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
6227 enum isl_dim_type type, unsigned pos, isl_int value)
6228{
6229 int i;
6230
6231 map = isl_map_cow(map);
6232 if (!map)
6233 return NULL;
6234
6235 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
6236 for (i = 0; i < map->n; ++i) {
6237 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
6238 if (!map->p[i])
6239 goto error;
6240 }
6241 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6242 return map;
6243error:
6244 isl_map_free(map);
6245 return NULL;
6246}
6247
6248__isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
6249 enum isl_dim_type type, unsigned pos, isl_int value)
6250{
Tobias Grosser06e15922016-11-16 11:06:47 +00006251 return set_from_map(isl_map_fix(set_to_map(set), type, pos, value));
Tobias Grosser52a25232015-02-04 20:55:43 +00006252}
6253
6254/* Fix the value of the variable at position "pos" of type "type" of "map"
6255 * to be equal to "v".
6256 */
6257__isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
6258 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6259{
6260 int i;
6261
6262 map = isl_map_cow(map);
6263 if (!map || !v)
6264 goto error;
6265
6266 if (!isl_val_is_int(v))
6267 isl_die(isl_map_get_ctx(map), isl_error_invalid,
6268 "expecting integer value", goto error);
6269 if (pos >= isl_map_dim(map, type))
6270 isl_die(isl_map_get_ctx(map), isl_error_invalid,
6271 "index out of bounds", goto error);
6272 for (i = map->n - 1; i >= 0; --i) {
6273 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
6274 isl_val_copy(v));
6275 if (remove_if_empty(map, i) < 0)
6276 goto error;
6277 }
6278 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6279 isl_val_free(v);
6280 return map;
6281error:
6282 isl_map_free(map);
6283 isl_val_free(v);
6284 return NULL;
6285}
6286
6287/* Fix the value of the variable at position "pos" of type "type" of "set"
6288 * to be equal to "v".
6289 */
6290__isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
6291 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6292{
6293 return isl_map_fix_val(set, type, pos, v);
6294}
6295
6296struct isl_map *isl_map_fix_input_si(struct isl_map *map,
6297 unsigned input, int value)
6298{
6299 return isl_map_fix_si(map, isl_dim_in, input, value);
6300}
6301
6302struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
6303{
Tobias Grosser06e15922016-11-16 11:06:47 +00006304 return set_from_map(isl_map_fix_si(set_to_map(set),
6305 isl_dim_set, dim, value));
Tobias Grosser52a25232015-02-04 20:55:43 +00006306}
6307
6308static __isl_give isl_basic_map *basic_map_bound_si(
6309 __isl_take isl_basic_map *bmap,
6310 enum isl_dim_type type, unsigned pos, int value, int upper)
6311{
6312 int j;
6313
Tobias Grosser94e53712016-12-31 07:46:11 +00006314 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6315 return isl_basic_map_free(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +00006316 pos += isl_basic_map_offset(bmap, type);
6317 bmap = isl_basic_map_cow(bmap);
6318 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6319 j = isl_basic_map_alloc_inequality(bmap);
6320 if (j < 0)
6321 goto error;
6322 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
6323 if (upper) {
6324 isl_int_set_si(bmap->ineq[j][pos], -1);
6325 isl_int_set_si(bmap->ineq[j][0], value);
6326 } else {
6327 isl_int_set_si(bmap->ineq[j][pos], 1);
6328 isl_int_set_si(bmap->ineq[j][0], -value);
6329 }
6330 bmap = isl_basic_map_simplify(bmap);
6331 return isl_basic_map_finalize(bmap);
6332error:
6333 isl_basic_map_free(bmap);
6334 return NULL;
6335}
6336
6337__isl_give isl_basic_map *isl_basic_map_lower_bound_si(
6338 __isl_take isl_basic_map *bmap,
6339 enum isl_dim_type type, unsigned pos, int value)
6340{
6341 return basic_map_bound_si(bmap, type, pos, value, 0);
6342}
6343
6344/* Constrain the values of the given dimension to be no greater than "value".
6345 */
6346__isl_give isl_basic_map *isl_basic_map_upper_bound_si(
6347 __isl_take isl_basic_map *bmap,
6348 enum isl_dim_type type, unsigned pos, int value)
6349{
6350 return basic_map_bound_si(bmap, type, pos, value, 1);
6351}
6352
Tobias Grosser52a25232015-02-04 20:55:43 +00006353static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
6354 enum isl_dim_type type, unsigned pos, int value, int upper)
6355{
6356 int i;
6357
6358 map = isl_map_cow(map);
6359 if (!map)
6360 return NULL;
6361
6362 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
6363 for (i = 0; i < map->n; ++i) {
6364 map->p[i] = basic_map_bound_si(map->p[i],
6365 type, pos, value, upper);
6366 if (!map->p[i])
6367 goto error;
6368 }
6369 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6370 return map;
6371error:
6372 isl_map_free(map);
6373 return NULL;
6374}
6375
6376__isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
6377 enum isl_dim_type type, unsigned pos, int value)
6378{
6379 return map_bound_si(map, type, pos, value, 0);
6380}
6381
6382__isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
6383 enum isl_dim_type type, unsigned pos, int value)
6384{
6385 return map_bound_si(map, type, pos, value, 1);
6386}
6387
6388__isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
6389 enum isl_dim_type type, unsigned pos, int value)
6390{
Tobias Grosser06e15922016-11-16 11:06:47 +00006391 return set_from_map(isl_map_lower_bound_si(set_to_map(set),
6392 type, pos, value));
Tobias Grosser52a25232015-02-04 20:55:43 +00006393}
6394
6395__isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
6396 enum isl_dim_type type, unsigned pos, int value)
6397{
6398 return isl_map_upper_bound_si(set, type, pos, value);
6399}
6400
6401/* Bound the given variable of "bmap" from below (or above is "upper"
6402 * is set) to "value".
6403 */
6404static __isl_give isl_basic_map *basic_map_bound(
6405 __isl_take isl_basic_map *bmap,
6406 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6407{
6408 int j;
6409
Tobias Grosser94e53712016-12-31 07:46:11 +00006410 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6411 return isl_basic_map_free(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +00006412 pos += isl_basic_map_offset(bmap, type);
6413 bmap = isl_basic_map_cow(bmap);
6414 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6415 j = isl_basic_map_alloc_inequality(bmap);
6416 if (j < 0)
6417 goto error;
6418 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
6419 if (upper) {
6420 isl_int_set_si(bmap->ineq[j][pos], -1);
6421 isl_int_set(bmap->ineq[j][0], value);
6422 } else {
6423 isl_int_set_si(bmap->ineq[j][pos], 1);
6424 isl_int_neg(bmap->ineq[j][0], value);
6425 }
6426 bmap = isl_basic_map_simplify(bmap);
6427 return isl_basic_map_finalize(bmap);
6428error:
6429 isl_basic_map_free(bmap);
6430 return NULL;
6431}
6432
6433/* Bound the given variable of "map" from below (or above is "upper"
6434 * is set) to "value".
6435 */
6436static __isl_give isl_map *map_bound(__isl_take isl_map *map,
6437 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6438{
6439 int i;
6440
6441 map = isl_map_cow(map);
6442 if (!map)
6443 return NULL;
6444
6445 if (pos >= isl_map_dim(map, type))
6446 isl_die(map->ctx, isl_error_invalid,
6447 "index out of bounds", goto error);
6448 for (i = map->n - 1; i >= 0; --i) {
6449 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
6450 if (remove_if_empty(map, i) < 0)
6451 goto error;
6452 }
6453 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6454 return map;
6455error:
6456 isl_map_free(map);
6457 return NULL;
6458}
6459
6460__isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
6461 enum isl_dim_type type, unsigned pos, isl_int value)
6462{
6463 return map_bound(map, type, pos, value, 0);
6464}
6465
6466__isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
6467 enum isl_dim_type type, unsigned pos, isl_int value)
6468{
6469 return map_bound(map, type, pos, value, 1);
6470}
6471
6472__isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
6473 enum isl_dim_type type, unsigned pos, isl_int value)
6474{
6475 return isl_map_lower_bound(set, type, pos, value);
6476}
6477
6478__isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
6479 enum isl_dim_type type, unsigned pos, isl_int value)
6480{
6481 return isl_map_upper_bound(set, type, pos, value);
6482}
6483
6484/* Force the values of the variable at position "pos" of type "type" of "set"
6485 * to be no smaller than "value".
6486 */
6487__isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
6488 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6489{
6490 if (!value)
6491 goto error;
6492 if (!isl_val_is_int(value))
6493 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6494 "expecting integer value", goto error);
6495 set = isl_set_lower_bound(set, type, pos, value->n);
6496 isl_val_free(value);
6497 return set;
6498error:
6499 isl_val_free(value);
6500 isl_set_free(set);
6501 return NULL;
6502}
6503
6504/* Force the values of the variable at position "pos" of type "type" of "set"
6505 * to be no greater than "value".
6506 */
6507__isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
6508 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6509{
6510 if (!value)
6511 goto error;
6512 if (!isl_val_is_int(value))
6513 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6514 "expecting integer value", goto error);
6515 set = isl_set_upper_bound(set, type, pos, value->n);
6516 isl_val_free(value);
6517 return set;
6518error:
6519 isl_val_free(value);
6520 isl_set_free(set);
6521 return NULL;
6522}
6523
Tobias Grosser52a25232015-02-04 20:55:43 +00006524struct isl_map *isl_map_reverse(struct isl_map *map)
6525{
6526 int i;
6527
6528 map = isl_map_cow(map);
6529 if (!map)
6530 return NULL;
6531
6532 map->dim = isl_space_reverse(map->dim);
6533 if (!map->dim)
6534 goto error;
6535 for (i = 0; i < map->n; ++i) {
6536 map->p[i] = isl_basic_map_reverse(map->p[i]);
6537 if (!map->p[i])
6538 goto error;
6539 }
6540 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6541 return map;
6542error:
6543 isl_map_free(map);
6544 return NULL;
6545}
6546
Tobias Grosser52a25232015-02-04 20:55:43 +00006547#undef TYPE
6548#define TYPE isl_pw_multi_aff
6549#undef SUFFIX
6550#define SUFFIX _pw_multi_aff
6551#undef EMPTY
6552#define EMPTY isl_pw_multi_aff_empty
6553#undef ADD
6554#define ADD isl_pw_multi_aff_union_add
6555#include "isl_map_lexopt_templ.c"
6556
6557/* Given a map "map", compute the lexicographically minimal
6558 * (or maximal) image element for each domain element in dom,
6559 * in the form of an isl_pw_multi_aff.
Tobias Grosser37034db2016-03-25 19:38:18 +00006560 * If "empty" is not NULL, then set *empty to those elements in dom that
6561 * do not have an image element.
Tobias Grosser932ec012016-07-06 09:11:00 +00006562 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6563 * should be computed over the domain of "map". "empty" is also NULL
6564 * in this case.
Tobias Grosser52a25232015-02-04 20:55:43 +00006565 *
6566 * We first compute the lexicographically minimal or maximal element
6567 * in the first basic map. This results in a partial solution "res"
6568 * and a subset "todo" of dom that still need to be handled.
6569 * We then consider each of the remaining maps in "map" and successively
6570 * update both "res" and "todo".
Tobias Grosser37034db2016-03-25 19:38:18 +00006571 * If "empty" is NULL, then the todo sets are not needed and therefore
6572 * also not computed.
Tobias Grosser52a25232015-02-04 20:55:43 +00006573 */
6574static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
6575 __isl_take isl_map *map, __isl_take isl_set *dom,
Tobias Grosser932ec012016-07-06 09:11:00 +00006576 __isl_give isl_set **empty, unsigned flags)
Tobias Grosser52a25232015-02-04 20:55:43 +00006577{
6578 int i;
Tobias Grosser932ec012016-07-06 09:11:00 +00006579 int full;
Tobias Grosser52a25232015-02-04 20:55:43 +00006580 isl_pw_multi_aff *res;
6581 isl_set *todo;
6582
Tobias Grosser932ec012016-07-06 09:11:00 +00006583 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6584 if (!map || (!full && !dom))
Tobias Grosser52a25232015-02-04 20:55:43 +00006585 goto error;
6586
6587 if (isl_map_plain_is_empty(map)) {
6588 if (empty)
6589 *empty = dom;
6590 else
6591 isl_set_free(dom);
6592 return isl_pw_multi_aff_from_map(map);
6593 }
6594
6595 res = basic_map_partial_lexopt_pw_multi_aff(
6596 isl_basic_map_copy(map->p[0]),
Tobias Grosser932ec012016-07-06 09:11:00 +00006597 isl_set_copy(dom), empty, flags);
Tobias Grosser52a25232015-02-04 20:55:43 +00006598
Tobias Grosser37034db2016-03-25 19:38:18 +00006599 if (empty)
6600 todo = *empty;
Tobias Grosser52a25232015-02-04 20:55:43 +00006601 for (i = 1; i < map->n; ++i) {
6602 isl_pw_multi_aff *res_i;
Tobias Grosser52a25232015-02-04 20:55:43 +00006603
6604 res_i = basic_map_partial_lexopt_pw_multi_aff(
6605 isl_basic_map_copy(map->p[i]),
Tobias Grosser932ec012016-07-06 09:11:00 +00006606 isl_set_copy(dom), empty, flags);
Tobias Grosser52a25232015-02-04 20:55:43 +00006607
Tobias Grosser932ec012016-07-06 09:11:00 +00006608 if (ISL_FL_ISSET(flags, ISL_OPT_MAX))
Tobias Grosser52a25232015-02-04 20:55:43 +00006609 res = isl_pw_multi_aff_union_lexmax(res, res_i);
6610 else
6611 res = isl_pw_multi_aff_union_lexmin(res, res_i);
6612
Tobias Grosser37034db2016-03-25 19:38:18 +00006613 if (empty)
6614 todo = isl_set_intersect(todo, *empty);
Tobias Grosser52a25232015-02-04 20:55:43 +00006615 }
6616
6617 isl_set_free(dom);
6618 isl_map_free(map);
6619
6620 if (empty)
6621 *empty = todo;
Tobias Grosser52a25232015-02-04 20:55:43 +00006622
6623 return res;
6624error:
6625 if (empty)
6626 *empty = NULL;
6627 isl_set_free(dom);
6628 isl_map_free(map);
6629 return NULL;
6630}
6631
6632#undef TYPE
6633#define TYPE isl_map
6634#undef SUFFIX
6635#define SUFFIX
6636#undef EMPTY
6637#define EMPTY isl_map_empty
6638#undef ADD
6639#define ADD isl_map_union_disjoint
6640#include "isl_map_lexopt_templ.c"
6641
6642/* Given a map "map", compute the lexicographically minimal
Michael Krusee0b34f32016-05-04 14:41:36 +00006643 * (or maximal) image element for each domain element in "dom",
6644 * in the form of an isl_map.
6645 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6646 * do not have an image element.
Tobias Grosser932ec012016-07-06 09:11:00 +00006647 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6648 * should be computed over the domain of "map". "empty" is also NULL
6649 * in this case.
Tobias Grosser52a25232015-02-04 20:55:43 +00006650 *
Michael Krusee0b34f32016-05-04 14:41:36 +00006651 * If the input consists of more than one disjunct, then first
6652 * compute the desired result in the form of an isl_pw_multi_aff and
6653 * then convert that into an isl_map.
Tobias Grosser52a25232015-02-04 20:55:43 +00006654 *
Michael Krusee0b34f32016-05-04 14:41:36 +00006655 * This function used to have an explicit implementation in terms
6656 * of isl_maps, but it would continually intersect the domains of
6657 * partial results with the complement of the domain of the next
6658 * partial solution, potentially leading to an explosion in the number
6659 * of disjuncts if there are several disjuncts in the input.
6660 * An even earlier implementation of this function would look for
6661 * better results in the domain of the partial result and for extra
6662 * results in the complement of this domain, which would lead to
6663 * even more splintering.
Tobias Grosser52a25232015-02-04 20:55:43 +00006664 */
6665static __isl_give isl_map *isl_map_partial_lexopt_aligned(
Tobias Grosser932ec012016-07-06 09:11:00 +00006666 __isl_take isl_map *map, __isl_take isl_set *dom,
6667 __isl_give isl_set **empty, unsigned flags)
Tobias Grosser52a25232015-02-04 20:55:43 +00006668{
Tobias Grosser932ec012016-07-06 09:11:00 +00006669 int full;
Tobias Grosser52a25232015-02-04 20:55:43 +00006670 struct isl_map *res;
Michael Krusee0b34f32016-05-04 14:41:36 +00006671 isl_pw_multi_aff *pma;
Tobias Grosser52a25232015-02-04 20:55:43 +00006672
Tobias Grosser932ec012016-07-06 09:11:00 +00006673 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6674 if (!map || (!full && !dom))
Tobias Grosser52a25232015-02-04 20:55:43 +00006675 goto error;
6676
6677 if (isl_map_plain_is_empty(map)) {
6678 if (empty)
6679 *empty = dom;
6680 else
6681 isl_set_free(dom);
6682 return map;
6683 }
6684
Michael Krusee0b34f32016-05-04 14:41:36 +00006685 if (map->n == 1) {
6686 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
Tobias Grosser932ec012016-07-06 09:11:00 +00006687 dom, empty, flags);
Michael Krusee0b34f32016-05-04 14:41:36 +00006688 isl_map_free(map);
6689 return res;
Tobias Grosser52a25232015-02-04 20:55:43 +00006690 }
6691
Tobias Grosser932ec012016-07-06 09:11:00 +00006692 pma = isl_map_partial_lexopt_aligned_pw_multi_aff(map, dom, empty,
6693 flags);
Michael Krusee0b34f32016-05-04 14:41:36 +00006694 return isl_map_from_pw_multi_aff(pma);
Tobias Grosser52a25232015-02-04 20:55:43 +00006695error:
6696 if (empty)
6697 *empty = NULL;
6698 isl_set_free(dom);
6699 isl_map_free(map);
6700 return NULL;
6701}
6702
6703__isl_give isl_map *isl_map_partial_lexmax(
6704 __isl_take isl_map *map, __isl_take isl_set *dom,
6705 __isl_give isl_set **empty)
6706{
Tobias Grosser932ec012016-07-06 09:11:00 +00006707 return isl_map_partial_lexopt(map, dom, empty, ISL_OPT_MAX);
Tobias Grosser52a25232015-02-04 20:55:43 +00006708}
6709
6710__isl_give isl_map *isl_map_partial_lexmin(
6711 __isl_take isl_map *map, __isl_take isl_set *dom,
6712 __isl_give isl_set **empty)
6713{
6714 return isl_map_partial_lexopt(map, dom, empty, 0);
6715}
6716
6717__isl_give isl_set *isl_set_partial_lexmin(
6718 __isl_take isl_set *set, __isl_take isl_set *dom,
6719 __isl_give isl_set **empty)
6720{
Tobias Grosser06e15922016-11-16 11:06:47 +00006721 return set_from_map(isl_map_partial_lexmin(set_to_map(set),
6722 dom, empty));
Tobias Grosser52a25232015-02-04 20:55:43 +00006723}
6724
6725__isl_give isl_set *isl_set_partial_lexmax(
6726 __isl_take isl_set *set, __isl_take isl_set *dom,
6727 __isl_give isl_set **empty)
6728{
Tobias Grosser06e15922016-11-16 11:06:47 +00006729 return set_from_map(isl_map_partial_lexmax(set_to_map(set),
6730 dom, empty));
Tobias Grosser52a25232015-02-04 20:55:43 +00006731}
6732
Tobias Grosser932ec012016-07-06 09:11:00 +00006733/* Compute the lexicographic minimum (or maximum if "flags" includes
6734 * ISL_OPT_MAX) of "bset" over its parametric domain.
Tobias Grosser52a25232015-02-04 20:55:43 +00006735 */
Tobias Grosser932ec012016-07-06 09:11:00 +00006736__isl_give isl_set *isl_basic_set_lexopt(__isl_take isl_basic_set *bset,
6737 unsigned flags)
Tobias Grosser52a25232015-02-04 20:55:43 +00006738{
Tobias Grosser932ec012016-07-06 09:11:00 +00006739 return isl_basic_map_lexopt(bset, flags);
Tobias Grosser52a25232015-02-04 20:55:43 +00006740}
6741
6742__isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
6743{
Tobias Grosser932ec012016-07-06 09:11:00 +00006744 return isl_basic_map_lexopt(bmap, ISL_OPT_MAX);
Tobias Grosser52a25232015-02-04 20:55:43 +00006745}
6746
6747__isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
6748{
Tobias Grosser06e15922016-11-16 11:06:47 +00006749 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset)));
Tobias Grosser52a25232015-02-04 20:55:43 +00006750}
6751
6752__isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
6753{
Tobias Grosser06e15922016-11-16 11:06:47 +00006754 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset)));
Tobias Grosser52a25232015-02-04 20:55:43 +00006755}
6756
Tobias Grosser932ec012016-07-06 09:11:00 +00006757/* Compute the lexicographic minimum of "bset" over its parametric domain
6758 * for the purpose of quantifier elimination.
6759 * That is, find an explicit representation for all the existentially
6760 * quantified variables in "bset" by computing their lexicographic
6761 * minimum.
6762 */
6763static __isl_give isl_set *isl_basic_set_lexmin_compute_divs(
6764 __isl_take isl_basic_set *bset)
6765{
6766 return isl_basic_set_lexopt(bset, ISL_OPT_QE);
6767}
6768
Tobias Grosser52a25232015-02-04 20:55:43 +00006769/* Extract the first and only affine expression from list
6770 * and then add it to *pwaff with the given dom.
6771 * This domain is known to be disjoint from other domains
6772 * because of the way isl_basic_map_foreach_lexmax works.
6773 */
Tobias Grosser72745c22017-02-17 05:11:16 +00006774static isl_stat update_dim_opt(__isl_take isl_basic_set *dom,
Tobias Grosser52a25232015-02-04 20:55:43 +00006775 __isl_take isl_aff_list *list, void *user)
6776{
6777 isl_ctx *ctx = isl_basic_set_get_ctx(dom);
6778 isl_aff *aff;
6779 isl_pw_aff **pwaff = user;
6780 isl_pw_aff *pwaff_i;
6781
6782 if (!list)
6783 goto error;
6784 if (isl_aff_list_n_aff(list) != 1)
6785 isl_die(ctx, isl_error_internal,
6786 "expecting single element list", goto error);
6787
6788 aff = isl_aff_list_get_aff(list, 0);
6789 pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
6790
6791 *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
6792
6793 isl_aff_list_free(list);
6794
Tobias Grosser72745c22017-02-17 05:11:16 +00006795 return isl_stat_ok;
Tobias Grosser52a25232015-02-04 20:55:43 +00006796error:
6797 isl_basic_set_free(dom);
6798 isl_aff_list_free(list);
Tobias Grosser72745c22017-02-17 05:11:16 +00006799 return isl_stat_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00006800}
6801
6802/* Given a basic map with one output dimension, compute the minimum or
6803 * maximum of that dimension as an isl_pw_aff.
6804 *
6805 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6806 * call update_dim_opt on each leaf of the result.
6807 */
6808static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
6809 int max)
6810{
6811 isl_space *dim = isl_basic_map_get_space(bmap);
6812 isl_pw_aff *pwaff;
Tobias Grosser72745c22017-02-17 05:11:16 +00006813 isl_stat r;
Tobias Grosser52a25232015-02-04 20:55:43 +00006814
6815 dim = isl_space_from_domain(isl_space_domain(dim));
6816 dim = isl_space_add_dims(dim, isl_dim_out, 1);
6817 pwaff = isl_pw_aff_empty(dim);
6818
6819 r = isl_basic_map_foreach_lexopt(bmap, max, &update_dim_opt, &pwaff);
6820 if (r < 0)
6821 return isl_pw_aff_free(pwaff);
6822
6823 return pwaff;
6824}
6825
6826/* Compute the minimum or maximum of the given output dimension
6827 * as a function of the parameters and the input dimensions,
6828 * but independently of the other output dimensions.
6829 *
6830 * We first project out the other output dimension and then compute
6831 * the "lexicographic" maximum in each basic map, combining the results
6832 * using isl_pw_aff_union_max.
6833 */
6834static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
6835 int max)
6836{
6837 int i;
6838 isl_pw_aff *pwaff;
6839 unsigned n_out;
6840
6841 n_out = isl_map_dim(map, isl_dim_out);
6842 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
6843 map = isl_map_project_out(map, isl_dim_out, 0, pos);
6844 if (!map)
6845 return NULL;
6846
6847 if (map->n == 0) {
6848 isl_space *dim = isl_map_get_space(map);
6849 isl_map_free(map);
6850 return isl_pw_aff_empty(dim);
6851 }
6852
6853 pwaff = basic_map_dim_opt(map->p[0], max);
6854 for (i = 1; i < map->n; ++i) {
6855 isl_pw_aff *pwaff_i;
6856
6857 pwaff_i = basic_map_dim_opt(map->p[i], max);
6858 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
6859 }
6860
6861 isl_map_free(map);
6862
6863 return pwaff;
6864}
6865
Tobias Grosser932ec012016-07-06 09:11:00 +00006866/* Compute the minimum of the given output dimension as a function of the
6867 * parameters and input dimensions, but independently of
6868 * the other output dimensions.
6869 */
6870__isl_give isl_pw_aff *isl_map_dim_min(__isl_take isl_map *map, int pos)
6871{
6872 return map_dim_opt(map, pos, 0);
6873}
6874
Tobias Grosser52a25232015-02-04 20:55:43 +00006875/* Compute the maximum of the given output dimension as a function of the
6876 * parameters and input dimensions, but independently of
6877 * the other output dimensions.
6878 */
6879__isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
6880{
6881 return map_dim_opt(map, pos, 1);
6882}
6883
6884/* Compute the minimum or maximum of the given set dimension
6885 * as a function of the parameters,
6886 * but independently of the other set dimensions.
6887 */
6888static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
6889 int max)
6890{
6891 return map_dim_opt(set, pos, max);
6892}
6893
6894/* Compute the maximum of the given set dimension as a function of the
6895 * parameters, but independently of the other set dimensions.
6896 */
6897__isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
6898{
6899 return set_dim_opt(set, pos, 1);
6900}
6901
6902/* Compute the minimum of the given set dimension as a function of the
6903 * parameters, but independently of the other set dimensions.
6904 */
6905__isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
6906{
6907 return set_dim_opt(set, pos, 0);
6908}
6909
6910/* Apply a preimage specified by "mat" on the parameters of "bset".
6911 * bset is assumed to have only parameters and divs.
6912 */
6913static struct isl_basic_set *basic_set_parameter_preimage(
6914 struct isl_basic_set *bset, struct isl_mat *mat)
6915{
6916 unsigned nparam;
6917
6918 if (!bset || !mat)
6919 goto error;
6920
6921 bset->dim = isl_space_cow(bset->dim);
6922 if (!bset->dim)
6923 goto error;
6924
6925 nparam = isl_basic_set_dim(bset, isl_dim_param);
6926
6927 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
6928
6929 bset->dim->nparam = 0;
6930 bset->dim->n_out = nparam;
6931 bset = isl_basic_set_preimage(bset, mat);
6932 if (bset) {
6933 bset->dim->nparam = bset->dim->n_out;
6934 bset->dim->n_out = 0;
6935 }
6936 return bset;
6937error:
6938 isl_mat_free(mat);
6939 isl_basic_set_free(bset);
6940 return NULL;
6941}
6942
6943/* Apply a preimage specified by "mat" on the parameters of "set".
6944 * set is assumed to have only parameters and divs.
6945 */
Tobias Grosser37034db2016-03-25 19:38:18 +00006946static __isl_give isl_set *set_parameter_preimage(__isl_take isl_set *set,
6947 __isl_take isl_mat *mat)
Tobias Grosser52a25232015-02-04 20:55:43 +00006948{
Tobias Grosser37034db2016-03-25 19:38:18 +00006949 isl_space *space;
Tobias Grosser52a25232015-02-04 20:55:43 +00006950 unsigned nparam;
6951
6952 if (!set || !mat)
6953 goto error;
6954
Tobias Grosser52a25232015-02-04 20:55:43 +00006955 nparam = isl_set_dim(set, isl_dim_param);
6956
Tobias Grosser37034db2016-03-25 19:38:18 +00006957 if (mat->n_row != 1 + nparam)
6958 isl_die(isl_set_get_ctx(set), isl_error_internal,
6959 "unexpected number of rows", goto error);
Tobias Grosser52a25232015-02-04 20:55:43 +00006960
Tobias Grosser37034db2016-03-25 19:38:18 +00006961 space = isl_set_get_space(set);
6962 space = isl_space_move_dims(space, isl_dim_set, 0,
6963 isl_dim_param, 0, nparam);
6964 set = isl_set_reset_space(set, space);
Tobias Grosser52a25232015-02-04 20:55:43 +00006965 set = isl_set_preimage(set, mat);
Tobias Grosser37034db2016-03-25 19:38:18 +00006966 nparam = isl_set_dim(set, isl_dim_out);
6967 space = isl_set_get_space(set);
6968 space = isl_space_move_dims(space, isl_dim_param, 0,
6969 isl_dim_out, 0, nparam);
6970 set = isl_set_reset_space(set, space);
Tobias Grosser52a25232015-02-04 20:55:43 +00006971 return set;
6972error:
Tobias Grosser52a25232015-02-04 20:55:43 +00006973 isl_mat_free(mat);
Tobias Grosser52a25232015-02-04 20:55:43 +00006974 isl_set_free(set);
6975 return NULL;
6976}
6977
6978/* Intersect the basic set "bset" with the affine space specified by the
6979 * equalities in "eq".
6980 */
6981static struct isl_basic_set *basic_set_append_equalities(
6982 struct isl_basic_set *bset, struct isl_mat *eq)
6983{
6984 int i, k;
6985 unsigned len;
6986
6987 if (!bset || !eq)
6988 goto error;
6989
6990 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
6991 eq->n_row, 0);
6992 if (!bset)
6993 goto error;
6994
6995 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
6996 for (i = 0; i < eq->n_row; ++i) {
6997 k = isl_basic_set_alloc_equality(bset);
6998 if (k < 0)
6999 goto error;
7000 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
7001 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
7002 }
7003 isl_mat_free(eq);
7004
7005 bset = isl_basic_set_gauss(bset, NULL);
7006 bset = isl_basic_set_finalize(bset);
7007
7008 return bset;
7009error:
7010 isl_mat_free(eq);
7011 isl_basic_set_free(bset);
7012 return NULL;
7013}
7014
7015/* Intersect the set "set" with the affine space specified by the
7016 * equalities in "eq".
7017 */
7018static struct isl_set *set_append_equalities(struct isl_set *set,
7019 struct isl_mat *eq)
7020{
7021 int i;
7022
7023 if (!set || !eq)
7024 goto error;
7025
7026 for (i = 0; i < set->n; ++i) {
7027 set->p[i] = basic_set_append_equalities(set->p[i],
7028 isl_mat_copy(eq));
7029 if (!set->p[i])
7030 goto error;
7031 }
7032 isl_mat_free(eq);
7033 return set;
7034error:
7035 isl_mat_free(eq);
7036 isl_set_free(set);
7037 return NULL;
7038}
7039
7040/* Given a basic set "bset" that only involves parameters and existentially
7041 * quantified variables, return the index of the first equality
7042 * that only involves parameters. If there is no such equality then
7043 * return bset->n_eq.
7044 *
7045 * This function assumes that isl_basic_set_gauss has been called on "bset".
7046 */
7047static int first_parameter_equality(__isl_keep isl_basic_set *bset)
7048{
7049 int i, j;
7050 unsigned nparam, n_div;
7051
7052 if (!bset)
7053 return -1;
7054
7055 nparam = isl_basic_set_dim(bset, isl_dim_param);
7056 n_div = isl_basic_set_dim(bset, isl_dim_div);
7057
7058 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
7059 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
7060 ++i;
7061 }
7062
7063 return i;
7064}
7065
7066/* Compute an explicit representation for the existentially quantified
7067 * variables in "bset" by computing the "minimal value" of the set
7068 * variables. Since there are no set variables, the computation of
7069 * the minimal value essentially computes an explicit representation
7070 * of the non-empty part(s) of "bset".
7071 *
7072 * The input only involves parameters and existentially quantified variables.
7073 * All equalities among parameters have been removed.
7074 *
7075 * Since the existentially quantified variables in the result are in general
7076 * going to be different from those in the input, we first replace
7077 * them by the minimal number of variables based on their equalities.
7078 * This should simplify the parametric integer programming.
7079 */
7080static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
7081{
7082 isl_morph *morph1, *morph2;
7083 isl_set *set;
7084 unsigned n;
7085
7086 if (!bset)
7087 return NULL;
7088 if (bset->n_eq == 0)
Tobias Grosser932ec012016-07-06 09:11:00 +00007089 return isl_basic_set_lexmin_compute_divs(bset);
Tobias Grosser52a25232015-02-04 20:55:43 +00007090
7091 morph1 = isl_basic_set_parameter_compression(bset);
7092 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
7093 bset = isl_basic_set_lift(bset);
7094 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
7095 bset = isl_morph_basic_set(morph2, bset);
7096 n = isl_basic_set_dim(bset, isl_dim_set);
7097 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
7098
Tobias Grosser932ec012016-07-06 09:11:00 +00007099 set = isl_basic_set_lexmin_compute_divs(bset);
Tobias Grosser52a25232015-02-04 20:55:43 +00007100
7101 set = isl_morph_set(isl_morph_inverse(morph1), set);
7102
7103 return set;
7104}
7105
7106/* Project the given basic set onto its parameter domain, possibly introducing
7107 * new, explicit, existential variables in the constraints.
7108 * The input has parameters and (possibly implicit) existential variables.
7109 * The output has the same parameters, but only
7110 * explicit existentially quantified variables.
7111 *
7112 * The actual projection is performed by pip, but pip doesn't seem
7113 * to like equalities very much, so we first remove the equalities
7114 * among the parameters by performing a variable compression on
7115 * the parameters. Afterward, an inverse transformation is performed
7116 * and the equalities among the parameters are inserted back in.
7117 *
7118 * The variable compression on the parameters may uncover additional
7119 * equalities that were only implicit before. We therefore check
7120 * if there are any new parameter equalities in the result and
7121 * if so recurse. The removal of parameter equalities is required
7122 * for the parameter compression performed by base_compute_divs.
7123 */
7124static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
7125{
7126 int i;
7127 struct isl_mat *eq;
7128 struct isl_mat *T, *T2;
7129 struct isl_set *set;
7130 unsigned nparam;
7131
7132 bset = isl_basic_set_cow(bset);
7133 if (!bset)
7134 return NULL;
7135
7136 if (bset->n_eq == 0)
7137 return base_compute_divs(bset);
7138
7139 bset = isl_basic_set_gauss(bset, NULL);
7140 if (!bset)
7141 return NULL;
7142 if (isl_basic_set_plain_is_empty(bset))
7143 return isl_set_from_basic_set(bset);
7144
7145 i = first_parameter_equality(bset);
7146 if (i == bset->n_eq)
7147 return base_compute_divs(bset);
7148
7149 nparam = isl_basic_set_dim(bset, isl_dim_param);
7150 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
7151 0, 1 + nparam);
7152 eq = isl_mat_cow(eq);
7153 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
7154 if (T && T->n_col == 0) {
7155 isl_mat_free(T);
7156 isl_mat_free(T2);
7157 isl_mat_free(eq);
7158 bset = isl_basic_set_set_to_empty(bset);
7159 return isl_set_from_basic_set(bset);
7160 }
7161 bset = basic_set_parameter_preimage(bset, T);
7162
7163 i = first_parameter_equality(bset);
7164 if (!bset)
7165 set = NULL;
7166 else if (i == bset->n_eq)
7167 set = base_compute_divs(bset);
7168 else
7169 set = parameter_compute_divs(bset);
7170 set = set_parameter_preimage(set, T2);
7171 set = set_append_equalities(set, eq);
7172 return set;
7173}
7174
7175/* Insert the divs from "ls" before those of "bmap".
7176 *
7177 * The number of columns is not changed, which means that the last
7178 * dimensions of "bmap" are being reintepreted as the divs from "ls".
7179 * The caller is responsible for removing the same number of dimensions
7180 * from the space of "bmap".
7181 */
7182static __isl_give isl_basic_map *insert_divs_from_local_space(
7183 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
7184{
7185 int i;
7186 int n_div;
7187 int old_n_div;
7188
7189 n_div = isl_local_space_dim(ls, isl_dim_div);
7190 if (n_div == 0)
7191 return bmap;
7192
7193 old_n_div = bmap->n_div;
7194 bmap = insert_div_rows(bmap, n_div);
7195 if (!bmap)
7196 return NULL;
7197
7198 for (i = 0; i < n_div; ++i) {
7199 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
7200 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
7201 }
7202
7203 return bmap;
7204}
7205
7206/* Replace the space of "bmap" by the space and divs of "ls".
7207 *
7208 * If "ls" has any divs, then we simplify the result since we may
7209 * have discovered some additional equalities that could simplify
7210 * the div expressions.
7211 */
7212static __isl_give isl_basic_map *basic_replace_space_by_local_space(
7213 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
7214{
7215 int n_div;
7216
7217 bmap = isl_basic_map_cow(bmap);
7218 if (!bmap || !ls)
7219 goto error;
7220
7221 n_div = isl_local_space_dim(ls, isl_dim_div);
7222 bmap = insert_divs_from_local_space(bmap, ls);
7223 if (!bmap)
7224 goto error;
7225
7226 isl_space_free(bmap->dim);
7227 bmap->dim = isl_local_space_get_space(ls);
7228 if (!bmap->dim)
7229 goto error;
7230
7231 isl_local_space_free(ls);
7232 if (n_div > 0)
7233 bmap = isl_basic_map_simplify(bmap);
7234 bmap = isl_basic_map_finalize(bmap);
7235 return bmap;
7236error:
7237 isl_basic_map_free(bmap);
7238 isl_local_space_free(ls);
7239 return NULL;
7240}
7241
7242/* Replace the space of "map" by the space and divs of "ls".
7243 */
7244static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
7245 __isl_take isl_local_space *ls)
7246{
7247 int i;
7248
7249 map = isl_map_cow(map);
7250 if (!map || !ls)
7251 goto error;
7252
7253 for (i = 0; i < map->n; ++i) {
7254 map->p[i] = basic_replace_space_by_local_space(map->p[i],
7255 isl_local_space_copy(ls));
7256 if (!map->p[i])
7257 goto error;
7258 }
7259 isl_space_free(map->dim);
7260 map->dim = isl_local_space_get_space(ls);
7261 if (!map->dim)
7262 goto error;
7263
7264 isl_local_space_free(ls);
7265 return map;
7266error:
7267 isl_local_space_free(ls);
7268 isl_map_free(map);
7269 return NULL;
7270}
7271
7272/* Compute an explicit representation for the existentially
7273 * quantified variables for which do not know any explicit representation yet.
7274 *
7275 * We first sort the existentially quantified variables so that the
7276 * existentially quantified variables for which we already have an explicit
7277 * representation are placed before those for which we do not.
7278 * The input dimensions, the output dimensions and the existentially
7279 * quantified variables for which we already have an explicit
7280 * representation are then turned into parameters.
7281 * compute_divs returns a map with the same parameters and
7282 * no input or output dimensions and the dimension specification
7283 * is reset to that of the input, including the existentially quantified
7284 * variables for which we already had an explicit representation.
7285 */
7286static struct isl_map *compute_divs(struct isl_basic_map *bmap)
7287{
7288 struct isl_basic_set *bset;
7289 struct isl_set *set;
7290 struct isl_map *map;
7291 isl_space *dim;
7292 isl_local_space *ls;
7293 unsigned nparam;
7294 unsigned n_in;
7295 unsigned n_out;
Tobias Grosser37034db2016-03-25 19:38:18 +00007296 int n_known;
Tobias Grosser52a25232015-02-04 20:55:43 +00007297 int i;
7298
7299 bmap = isl_basic_map_sort_divs(bmap);
7300 bmap = isl_basic_map_cow(bmap);
7301 if (!bmap)
7302 return NULL;
7303
Tobias Grosser37034db2016-03-25 19:38:18 +00007304 n_known = isl_basic_map_first_unknown_div(bmap);
7305 if (n_known < 0)
7306 return isl_map_from_basic_map(isl_basic_map_free(bmap));
Tobias Grosser52a25232015-02-04 20:55:43 +00007307
7308 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7309 n_in = isl_basic_map_dim(bmap, isl_dim_in);
7310 n_out = isl_basic_map_dim(bmap, isl_dim_out);
7311 dim = isl_space_set_alloc(bmap->ctx,
7312 nparam + n_in + n_out + n_known, 0);
7313 if (!dim)
7314 goto error;
7315
7316 ls = isl_basic_map_get_local_space(bmap);
7317 ls = isl_local_space_drop_dims(ls, isl_dim_div,
7318 n_known, bmap->n_div - n_known);
7319 if (n_known > 0) {
7320 for (i = n_known; i < bmap->n_div; ++i)
7321 swap_div(bmap, i - n_known, i);
7322 bmap->n_div -= n_known;
7323 bmap->extra -= n_known;
7324 }
7325 bmap = isl_basic_map_reset_space(bmap, dim);
Tobias Grosser06e15922016-11-16 11:06:47 +00007326 bset = bset_from_bmap(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +00007327
7328 set = parameter_compute_divs(bset);
Tobias Grosser06e15922016-11-16 11:06:47 +00007329 map = set_to_map(set);
Tobias Grosser52a25232015-02-04 20:55:43 +00007330 map = replace_space_by_local_space(map, ls);
7331
7332 return map;
7333error:
7334 isl_basic_map_free(bmap);
7335 return NULL;
7336}
7337
Tobias Grosser37034db2016-03-25 19:38:18 +00007338/* Remove the explicit representation of local variable "div",
7339 * if there is any.
7340 */
7341__isl_give isl_basic_map *isl_basic_map_mark_div_unknown(
7342 __isl_take isl_basic_map *bmap, int div)
7343{
Tobias Grosser932ec012016-07-06 09:11:00 +00007344 isl_bool unknown;
Tobias Grosser37034db2016-03-25 19:38:18 +00007345
Tobias Grosser932ec012016-07-06 09:11:00 +00007346 unknown = isl_basic_map_div_is_marked_unknown(bmap, div);
7347 if (unknown < 0)
Tobias Grosser37034db2016-03-25 19:38:18 +00007348 return isl_basic_map_free(bmap);
Tobias Grosser932ec012016-07-06 09:11:00 +00007349 if (unknown)
Tobias Grosser37034db2016-03-25 19:38:18 +00007350 return bmap;
7351
7352 bmap = isl_basic_map_cow(bmap);
7353 if (!bmap)
7354 return NULL;
7355 isl_int_set_si(bmap->div[div][0], 0);
7356 return bmap;
7357}
7358
Tobias Grosser932ec012016-07-06 09:11:00 +00007359/* Is local variable "div" of "bmap" marked as not having an explicit
7360 * representation?
7361 * Note that even if "div" is not marked in this way and therefore
7362 * has an explicit representation, this representation may still
7363 * depend (indirectly) on other local variables that do not
7364 * have an explicit representation.
Michael Kruse959a8dc2016-01-15 15:54:45 +00007365 */
Tobias Grosser932ec012016-07-06 09:11:00 +00007366isl_bool isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map *bmap,
7367 int div)
Michael Kruse959a8dc2016-01-15 15:54:45 +00007368{
Tobias Grosser94e53712016-12-31 07:46:11 +00007369 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
Michael Kruse959a8dc2016-01-15 15:54:45 +00007370 return isl_bool_error;
Tobias Grosser932ec012016-07-06 09:11:00 +00007371 return isl_int_is_zero(bmap->div[div][0]);
Michael Kruse959a8dc2016-01-15 15:54:45 +00007372}
7373
Tobias Grosser37034db2016-03-25 19:38:18 +00007374/* Return the position of the first local variable that does not
7375 * have an explicit representation.
7376 * Return the total number of local variables if they all have
7377 * an explicit representation.
7378 * Return -1 on error.
7379 */
7380int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map *bmap)
7381{
7382 int i;
7383
7384 if (!bmap)
7385 return -1;
7386
7387 for (i = 0; i < bmap->n_div; ++i) {
7388 if (!isl_basic_map_div_is_known(bmap, i))
7389 return i;
7390 }
7391 return bmap->n_div;
7392}
7393
Tobias Grosser9ec4f952016-07-20 16:53:07 +00007394/* Return the position of the first local variable that does not
7395 * have an explicit representation.
7396 * Return the total number of local variables if they all have
7397 * an explicit representation.
7398 * Return -1 on error.
7399 */
7400int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set *bset)
7401{
7402 return isl_basic_map_first_unknown_div(bset);
7403}
7404
Michael Kruse959a8dc2016-01-15 15:54:45 +00007405/* Does "bmap" have an explicit representation for all local variables?
7406 */
7407isl_bool isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
Tobias Grosser52a25232015-02-04 20:55:43 +00007408{
Tobias Grosser37034db2016-03-25 19:38:18 +00007409 int first, n;
Tobias Grosser52a25232015-02-04 20:55:43 +00007410
Tobias Grosser37034db2016-03-25 19:38:18 +00007411 n = isl_basic_map_dim(bmap, isl_dim_div);
7412 first = isl_basic_map_first_unknown_div(bmap);
7413 if (first < 0)
Michael Kruse959a8dc2016-01-15 15:54:45 +00007414 return isl_bool_error;
Tobias Grosser37034db2016-03-25 19:38:18 +00007415 return first == n;
Tobias Grosser52a25232015-02-04 20:55:43 +00007416}
7417
Michael Kruse959a8dc2016-01-15 15:54:45 +00007418/* Do all basic maps in "map" have an explicit representation
7419 * for all local variables?
7420 */
7421isl_bool isl_map_divs_known(__isl_keep isl_map *map)
Tobias Grosser52a25232015-02-04 20:55:43 +00007422{
7423 int i;
7424
7425 if (!map)
Michael Kruse959a8dc2016-01-15 15:54:45 +00007426 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00007427
7428 for (i = 0; i < map->n; ++i) {
7429 int known = isl_basic_map_divs_known(map->p[i]);
7430 if (known <= 0)
7431 return known;
7432 }
7433
Michael Kruse959a8dc2016-01-15 15:54:45 +00007434 return isl_bool_true;
Tobias Grosser52a25232015-02-04 20:55:43 +00007435}
7436
7437/* If bmap contains any unknown divs, then compute explicit
7438 * expressions for them. However, this computation may be
7439 * quite expensive, so first try to remove divs that aren't
7440 * strictly needed.
7441 */
7442struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
7443{
7444 int known;
7445 struct isl_map *map;
7446
7447 known = isl_basic_map_divs_known(bmap);
7448 if (known < 0)
7449 goto error;
7450 if (known)
7451 return isl_map_from_basic_map(bmap);
7452
7453 bmap = isl_basic_map_drop_redundant_divs(bmap);
7454
7455 known = isl_basic_map_divs_known(bmap);
7456 if (known < 0)
7457 goto error;
7458 if (known)
7459 return isl_map_from_basic_map(bmap);
7460
7461 map = compute_divs(bmap);
7462 return map;
7463error:
7464 isl_basic_map_free(bmap);
7465 return NULL;
7466}
7467
7468struct isl_map *isl_map_compute_divs(struct isl_map *map)
7469{
7470 int i;
7471 int known;
7472 struct isl_map *res;
7473
7474 if (!map)
7475 return NULL;
7476 if (map->n == 0)
7477 return map;
7478
Michael Kruse959a8dc2016-01-15 15:54:45 +00007479 known = isl_map_divs_known(map);
Tobias Grosser52a25232015-02-04 20:55:43 +00007480 if (known < 0) {
7481 isl_map_free(map);
7482 return NULL;
7483 }
7484 if (known)
7485 return map;
7486
7487 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
7488 for (i = 1 ; i < map->n; ++i) {
7489 struct isl_map *r2;
7490 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
7491 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
7492 res = isl_map_union_disjoint(res, r2);
7493 else
7494 res = isl_map_union(res, r2);
7495 }
7496 isl_map_free(map);
7497
7498 return res;
7499}
7500
7501struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
7502{
Tobias Grosser06e15922016-11-16 11:06:47 +00007503 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset)));
Tobias Grosser52a25232015-02-04 20:55:43 +00007504}
7505
7506struct isl_set *isl_set_compute_divs(struct isl_set *set)
7507{
Tobias Grosser06e15922016-11-16 11:06:47 +00007508 return set_from_map(isl_map_compute_divs(set_to_map(set)));
Tobias Grosser52a25232015-02-04 20:55:43 +00007509}
7510
7511struct isl_set *isl_map_domain(struct isl_map *map)
7512{
7513 int i;
7514 struct isl_set *set;
7515
7516 if (!map)
7517 goto error;
7518
7519 map = isl_map_cow(map);
7520 if (!map)
7521 return NULL;
7522
Tobias Grosser06e15922016-11-16 11:06:47 +00007523 set = set_from_map(map);
Tobias Grosser52a25232015-02-04 20:55:43 +00007524 set->dim = isl_space_domain(set->dim);
7525 if (!set->dim)
7526 goto error;
7527 for (i = 0; i < map->n; ++i) {
7528 set->p[i] = isl_basic_map_domain(map->p[i]);
7529 if (!set->p[i])
7530 goto error;
7531 }
7532 ISL_F_CLR(set, ISL_MAP_DISJOINT);
7533 ISL_F_CLR(set, ISL_SET_NORMALIZED);
7534 return set;
7535error:
7536 isl_map_free(map);
7537 return NULL;
7538}
7539
7540/* Return the union of "map1" and "map2", where we assume for now that
7541 * "map1" and "map2" are disjoint. Note that the basic maps inside
7542 * "map1" or "map2" may not be disjoint from each other.
7543 * Also note that this function is also called from isl_map_union,
7544 * which takes care of handling the situation where "map1" and "map2"
7545 * may not be disjoint.
7546 *
7547 * If one of the inputs is empty, we can simply return the other input.
7548 * Similarly, if one of the inputs is universal, then it is equal to the union.
7549 */
7550static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
7551 __isl_take isl_map *map2)
7552{
7553 int i;
7554 unsigned flags = 0;
7555 struct isl_map *map = NULL;
7556 int is_universe;
7557
7558 if (!map1 || !map2)
7559 goto error;
7560
7561 if (!isl_space_is_equal(map1->dim, map2->dim))
7562 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
7563 "spaces don't match", goto error);
7564
7565 if (map1->n == 0) {
7566 isl_map_free(map1);
7567 return map2;
7568 }
7569 if (map2->n == 0) {
7570 isl_map_free(map2);
7571 return map1;
7572 }
7573
7574 is_universe = isl_map_plain_is_universe(map1);
7575 if (is_universe < 0)
7576 goto error;
7577 if (is_universe) {
7578 isl_map_free(map2);
7579 return map1;
7580 }
7581
7582 is_universe = isl_map_plain_is_universe(map2);
7583 if (is_universe < 0)
7584 goto error;
7585 if (is_universe) {
7586 isl_map_free(map1);
7587 return map2;
7588 }
7589
7590 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7591 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7592 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7593
7594 map = isl_map_alloc_space(isl_space_copy(map1->dim),
7595 map1->n + map2->n, flags);
7596 if (!map)
7597 goto error;
7598 for (i = 0; i < map1->n; ++i) {
7599 map = isl_map_add_basic_map(map,
7600 isl_basic_map_copy(map1->p[i]));
7601 if (!map)
7602 goto error;
7603 }
7604 for (i = 0; i < map2->n; ++i) {
7605 map = isl_map_add_basic_map(map,
7606 isl_basic_map_copy(map2->p[i]));
7607 if (!map)
7608 goto error;
7609 }
7610 isl_map_free(map1);
7611 isl_map_free(map2);
7612 return map;
7613error:
7614 isl_map_free(map);
7615 isl_map_free(map1);
7616 isl_map_free(map2);
7617 return NULL;
7618}
7619
7620/* Return the union of "map1" and "map2", where "map1" and "map2" are
7621 * guaranteed to be disjoint by the caller.
7622 *
7623 * Note that this functions is called from within isl_map_make_disjoint,
7624 * so we have to be careful not to touch the constraints of the inputs
7625 * in any way.
7626 */
7627__isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
7628 __isl_take isl_map *map2)
7629{
7630 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
7631}
7632
7633/* Return the union of "map1" and "map2", where "map1" and "map2" may
7634 * not be disjoint. The parameters are assumed to have been aligned.
7635 *
7636 * We currently simply call map_union_disjoint, the internal operation
7637 * of which does not really depend on the inputs being disjoint.
7638 * If the result contains more than one basic map, then we clear
7639 * the disjoint flag since the result may contain basic maps from
7640 * both inputs and these are not guaranteed to be disjoint.
7641 *
7642 * As a special case, if "map1" and "map2" are obviously equal,
7643 * then we simply return "map1".
7644 */
7645static __isl_give isl_map *map_union_aligned(__isl_take isl_map *map1,
7646 __isl_take isl_map *map2)
7647{
7648 int equal;
7649
7650 if (!map1 || !map2)
7651 goto error;
7652
7653 equal = isl_map_plain_is_equal(map1, map2);
7654 if (equal < 0)
7655 goto error;
7656 if (equal) {
7657 isl_map_free(map2);
7658 return map1;
7659 }
7660
7661 map1 = map_union_disjoint(map1, map2);
7662 if (!map1)
7663 return NULL;
7664 if (map1->n > 1)
7665 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
7666 return map1;
7667error:
7668 isl_map_free(map1);
7669 isl_map_free(map2);
7670 return NULL;
7671}
7672
7673/* Return the union of "map1" and "map2", where "map1" and "map2" may
7674 * not be disjoint.
7675 */
7676__isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
7677 __isl_take isl_map *map2)
7678{
7679 return isl_map_align_params_map_map_and(map1, map2, &map_union_aligned);
7680}
7681
7682struct isl_set *isl_set_union_disjoint(
7683 struct isl_set *set1, struct isl_set *set2)
7684{
Tobias Grosser06e15922016-11-16 11:06:47 +00007685 return set_from_map(isl_map_union_disjoint(set_to_map(set1),
7686 set_to_map(set2)));
Tobias Grosser52a25232015-02-04 20:55:43 +00007687}
7688
7689struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
7690{
Tobias Grosser06e15922016-11-16 11:06:47 +00007691 return set_from_map(isl_map_union(set_to_map(set1), set_to_map(set2)));
Tobias Grosser52a25232015-02-04 20:55:43 +00007692}
7693
7694/* Apply "fn" to pairs of elements from "map" and "set" and collect
7695 * the results.
7696 *
7697 * "map" and "set" are assumed to be compatible and non-NULL.
7698 */
7699static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
7700 __isl_take isl_set *set,
7701 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
7702 __isl_take isl_basic_set *bset))
7703{
7704 unsigned flags = 0;
7705 struct isl_map *result;
7706 int i, j;
7707
7708 if (isl_set_plain_is_universe(set)) {
7709 isl_set_free(set);
7710 return map;
7711 }
7712
7713 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
7714 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
7715 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7716
7717 result = isl_map_alloc_space(isl_space_copy(map->dim),
7718 map->n * set->n, flags);
7719 for (i = 0; result && i < map->n; ++i)
7720 for (j = 0; j < set->n; ++j) {
7721 result = isl_map_add_basic_map(result,
7722 fn(isl_basic_map_copy(map->p[i]),
7723 isl_basic_set_copy(set->p[j])));
7724 if (!result)
7725 break;
7726 }
7727
7728 isl_map_free(map);
7729 isl_set_free(set);
7730 return result;
7731}
7732
7733static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
7734 __isl_take isl_set *set)
7735{
Tobias Grosser72745c22017-02-17 05:11:16 +00007736 isl_bool ok;
Tobias Grosser52a25232015-02-04 20:55:43 +00007737
Tobias Grosser72745c22017-02-17 05:11:16 +00007738 ok = isl_map_compatible_range(map, set);
7739 if (ok < 0)
7740 goto error;
7741 if (!ok)
Tobias Grosser52a25232015-02-04 20:55:43 +00007742 isl_die(set->ctx, isl_error_invalid,
7743 "incompatible spaces", goto error);
7744
7745 return map_intersect_set(map, set, &isl_basic_map_intersect_range);
7746error:
7747 isl_map_free(map);
7748 isl_set_free(set);
7749 return NULL;
7750}
7751
7752__isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
7753 __isl_take isl_set *set)
7754{
7755 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
7756}
7757
7758static __isl_give isl_map *map_intersect_domain(__isl_take isl_map *map,
7759 __isl_take isl_set *set)
7760{
Tobias Grosser72745c22017-02-17 05:11:16 +00007761 isl_bool ok;
Tobias Grosser52a25232015-02-04 20:55:43 +00007762
Tobias Grosser72745c22017-02-17 05:11:16 +00007763 ok = isl_map_compatible_domain(map, set);
7764 if (ok < 0)
7765 goto error;
7766 if (!ok)
Tobias Grosser52a25232015-02-04 20:55:43 +00007767 isl_die(set->ctx, isl_error_invalid,
7768 "incompatible spaces", goto error);
7769
7770 return map_intersect_set(map, set, &isl_basic_map_intersect_domain);
7771error:
7772 isl_map_free(map);
7773 isl_set_free(set);
7774 return NULL;
7775}
7776
7777__isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
7778 __isl_take isl_set *set)
7779{
7780 return isl_map_align_params_map_map_and(map, set,
7781 &map_intersect_domain);
7782}
7783
7784static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
7785 __isl_take isl_map *map2)
7786{
7787 if (!map1 || !map2)
7788 goto error;
7789 map1 = isl_map_reverse(map1);
7790 map1 = isl_map_apply_range(map1, map2);
7791 return isl_map_reverse(map1);
7792error:
7793 isl_map_free(map1);
7794 isl_map_free(map2);
7795 return NULL;
7796}
7797
7798__isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
7799 __isl_take isl_map *map2)
7800{
7801 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
7802}
7803
7804static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
7805 __isl_take isl_map *map2)
7806{
7807 isl_space *dim_result;
7808 struct isl_map *result;
7809 int i, j;
7810
7811 if (!map1 || !map2)
7812 goto error;
7813
7814 dim_result = isl_space_join(isl_space_copy(map1->dim),
7815 isl_space_copy(map2->dim));
7816
7817 result = isl_map_alloc_space(dim_result, map1->n * map2->n, 0);
7818 if (!result)
7819 goto error;
7820 for (i = 0; i < map1->n; ++i)
7821 for (j = 0; j < map2->n; ++j) {
7822 result = isl_map_add_basic_map(result,
7823 isl_basic_map_apply_range(
7824 isl_basic_map_copy(map1->p[i]),
7825 isl_basic_map_copy(map2->p[j])));
7826 if (!result)
7827 goto error;
7828 }
7829 isl_map_free(map1);
7830 isl_map_free(map2);
7831 if (result && result->n <= 1)
7832 ISL_F_SET(result, ISL_MAP_DISJOINT);
7833 return result;
7834error:
7835 isl_map_free(map1);
7836 isl_map_free(map2);
7837 return NULL;
7838}
7839
7840__isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
7841 __isl_take isl_map *map2)
7842{
7843 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
7844}
7845
7846/*
7847 * returns range - domain
7848 */
7849struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
7850{
Tobias Grosserb2f39922015-05-28 13:32:11 +00007851 isl_space *target_space;
Tobias Grosser52a25232015-02-04 20:55:43 +00007852 struct isl_basic_set *bset;
7853 unsigned dim;
7854 unsigned nparam;
7855 int i;
7856
7857 if (!bmap)
7858 goto error;
7859 isl_assert(bmap->ctx, isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7860 bmap->dim, isl_dim_out),
7861 goto error);
Tobias Grosserb2f39922015-05-28 13:32:11 +00007862 target_space = isl_space_domain(isl_basic_map_get_space(bmap));
Tobias Grosserf4fe34b2017-03-16 21:33:20 +00007863 dim = isl_basic_map_dim(bmap, isl_dim_in);
7864 nparam = isl_basic_map_dim(bmap, isl_dim_param);
Tobias Grosserb2f39922015-05-28 13:32:11 +00007865 bmap = isl_basic_map_from_range(isl_basic_map_wrap(bmap));
7866 bmap = isl_basic_map_add_dims(bmap, isl_dim_in, dim);
7867 bmap = isl_basic_map_extend_constraints(bmap, dim, 0);
Tobias Grosser52a25232015-02-04 20:55:43 +00007868 for (i = 0; i < dim; ++i) {
Tobias Grosserb2f39922015-05-28 13:32:11 +00007869 int j = isl_basic_map_alloc_equality(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +00007870 if (j < 0) {
Tobias Grosserb2f39922015-05-28 13:32:11 +00007871 bmap = isl_basic_map_free(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +00007872 break;
7873 }
Tobias Grosserb2f39922015-05-28 13:32:11 +00007874 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7875 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7876 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], 1);
7877 isl_int_set_si(bmap->eq[j][1+nparam+2*dim+i], -1);
Tobias Grosser52a25232015-02-04 20:55:43 +00007878 }
Tobias Grosserb2f39922015-05-28 13:32:11 +00007879 bset = isl_basic_map_domain(bmap);
7880 bset = isl_basic_set_reset_space(bset, target_space);
Tobias Grosser52a25232015-02-04 20:55:43 +00007881 return bset;
7882error:
7883 isl_basic_map_free(bmap);
7884 return NULL;
7885}
7886
7887/*
7888 * returns range - domain
7889 */
7890__isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
7891{
7892 int i;
7893 isl_space *dim;
7894 struct isl_set *result;
7895
7896 if (!map)
7897 return NULL;
7898
7899 isl_assert(map->ctx, isl_space_tuple_is_equal(map->dim, isl_dim_in,
7900 map->dim, isl_dim_out),
7901 goto error);
7902 dim = isl_map_get_space(map);
7903 dim = isl_space_domain(dim);
7904 result = isl_set_alloc_space(dim, map->n, 0);
7905 if (!result)
7906 goto error;
7907 for (i = 0; i < map->n; ++i)
7908 result = isl_set_add_basic_set(result,
7909 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
7910 isl_map_free(map);
7911 return result;
7912error:
7913 isl_map_free(map);
7914 return NULL;
7915}
7916
7917/*
7918 * returns [domain -> range] -> range - domain
7919 */
7920__isl_give isl_basic_map *isl_basic_map_deltas_map(
7921 __isl_take isl_basic_map *bmap)
7922{
7923 int i, k;
7924 isl_space *dim;
7925 isl_basic_map *domain;
7926 int nparam, n;
7927 unsigned total;
7928
7929 if (!isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7930 bmap->dim, isl_dim_out))
7931 isl_die(bmap->ctx, isl_error_invalid,
7932 "domain and range don't match", goto error);
7933
7934 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7935 n = isl_basic_map_dim(bmap, isl_dim_in);
7936
7937 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
7938 domain = isl_basic_map_universe(dim);
7939
7940 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
7941 bmap = isl_basic_map_apply_range(bmap, domain);
7942 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
7943
7944 total = isl_basic_map_total_dim(bmap);
7945
7946 for (i = 0; i < n; ++i) {
7947 k = isl_basic_map_alloc_equality(bmap);
7948 if (k < 0)
7949 goto error;
7950 isl_seq_clr(bmap->eq[k], 1 + total);
7951 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
7952 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
7953 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
7954 }
7955
7956 bmap = isl_basic_map_gauss(bmap, NULL);
7957 return isl_basic_map_finalize(bmap);
7958error:
7959 isl_basic_map_free(bmap);
7960 return NULL;
7961}
7962
7963/*
7964 * returns [domain -> range] -> range - domain
7965 */
7966__isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
7967{
7968 int i;
7969 isl_space *domain_dim;
7970
7971 if (!map)
7972 return NULL;
7973
7974 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
7975 map->dim, isl_dim_out))
7976 isl_die(map->ctx, isl_error_invalid,
7977 "domain and range don't match", goto error);
7978
7979 map = isl_map_cow(map);
7980 if (!map)
7981 return NULL;
7982
7983 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
7984 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
7985 map->dim = isl_space_join(map->dim, domain_dim);
7986 if (!map->dim)
7987 goto error;
7988 for (i = 0; i < map->n; ++i) {
7989 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
7990 if (!map->p[i])
7991 goto error;
7992 }
7993 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
7994 return map;
7995error:
7996 isl_map_free(map);
7997 return NULL;
7998}
7999
8000static __isl_give isl_basic_map *basic_map_identity(__isl_take isl_space *dims)
8001{
8002 struct isl_basic_map *bmap;
8003 unsigned nparam;
8004 unsigned dim;
8005 int i;
8006
8007 if (!dims)
8008 return NULL;
8009
8010 nparam = dims->nparam;
8011 dim = dims->n_out;
8012 bmap = isl_basic_map_alloc_space(dims, 0, dim, 0);
8013 if (!bmap)
8014 goto error;
8015
8016 for (i = 0; i < dim; ++i) {
8017 int j = isl_basic_map_alloc_equality(bmap);
8018 if (j < 0)
8019 goto error;
8020 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
8021 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
8022 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
8023 }
8024 return isl_basic_map_finalize(bmap);
8025error:
8026 isl_basic_map_free(bmap);
8027 return NULL;
8028}
8029
8030__isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *dim)
8031{
8032 if (!dim)
8033 return NULL;
8034 if (dim->n_in != dim->n_out)
8035 isl_die(dim->ctx, isl_error_invalid,
8036 "number of input and output dimensions needs to be "
8037 "the same", goto error);
8038 return basic_map_identity(dim);
8039error:
8040 isl_space_free(dim);
8041 return NULL;
8042}
8043
Tobias Grosser52a25232015-02-04 20:55:43 +00008044__isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
8045{
8046 return isl_map_from_basic_map(isl_basic_map_identity(dim));
8047}
8048
Tobias Grosser52a25232015-02-04 20:55:43 +00008049__isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
8050{
8051 isl_space *dim = isl_set_get_space(set);
8052 isl_map *id;
8053 id = isl_map_identity(isl_space_map_from_set(dim));
8054 return isl_map_intersect_range(id, set);
8055}
8056
8057/* Construct a basic set with all set dimensions having only non-negative
8058 * values.
8059 */
8060__isl_give isl_basic_set *isl_basic_set_positive_orthant(
8061 __isl_take isl_space *space)
8062{
8063 int i;
8064 unsigned nparam;
8065 unsigned dim;
8066 struct isl_basic_set *bset;
8067
8068 if (!space)
8069 return NULL;
8070 nparam = space->nparam;
8071 dim = space->n_out;
8072 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
8073 if (!bset)
8074 return NULL;
8075 for (i = 0; i < dim; ++i) {
8076 int k = isl_basic_set_alloc_inequality(bset);
8077 if (k < 0)
8078 goto error;
8079 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
8080 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
8081 }
8082 return bset;
8083error:
8084 isl_basic_set_free(bset);
8085 return NULL;
8086}
8087
8088/* Construct the half-space x_pos >= 0.
8089 */
8090static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *dim,
8091 int pos)
8092{
8093 int k;
8094 isl_basic_set *nonneg;
8095
8096 nonneg = isl_basic_set_alloc_space(dim, 0, 0, 1);
8097 k = isl_basic_set_alloc_inequality(nonneg);
8098 if (k < 0)
8099 goto error;
8100 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
8101 isl_int_set_si(nonneg->ineq[k][pos], 1);
8102
8103 return isl_basic_set_finalize(nonneg);
8104error:
8105 isl_basic_set_free(nonneg);
8106 return NULL;
8107}
8108
8109/* Construct the half-space x_pos <= -1.
8110 */
8111static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *dim, int pos)
8112{
8113 int k;
8114 isl_basic_set *neg;
8115
8116 neg = isl_basic_set_alloc_space(dim, 0, 0, 1);
8117 k = isl_basic_set_alloc_inequality(neg);
8118 if (k < 0)
8119 goto error;
8120 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
8121 isl_int_set_si(neg->ineq[k][0], -1);
8122 isl_int_set_si(neg->ineq[k][pos], -1);
8123
8124 return isl_basic_set_finalize(neg);
8125error:
8126 isl_basic_set_free(neg);
8127 return NULL;
8128}
8129
8130__isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
8131 enum isl_dim_type type, unsigned first, unsigned n)
8132{
8133 int i;
Tobias Grossere0f8d592015-05-13 13:10:13 +00008134 unsigned offset;
Tobias Grosser52a25232015-02-04 20:55:43 +00008135 isl_basic_set *nonneg;
8136 isl_basic_set *neg;
8137
8138 if (!set)
8139 return NULL;
8140 if (n == 0)
8141 return set;
8142
8143 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
8144
Tobias Grossere0f8d592015-05-13 13:10:13 +00008145 offset = pos(set->dim, type);
Tobias Grosser52a25232015-02-04 20:55:43 +00008146 for (i = 0; i < n; ++i) {
8147 nonneg = nonneg_halfspace(isl_set_get_space(set),
Tobias Grossere0f8d592015-05-13 13:10:13 +00008148 offset + first + i);
8149 neg = neg_halfspace(isl_set_get_space(set), offset + first + i);
Tobias Grosser52a25232015-02-04 20:55:43 +00008150
8151 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
8152 }
8153
8154 return set;
8155error:
8156 isl_set_free(set);
8157 return NULL;
8158}
8159
Tobias Grosser72745c22017-02-17 05:11:16 +00008160static isl_stat foreach_orthant(__isl_take isl_set *set, int *signs, int first,
8161 int len,
8162 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
Tobias Grosser52a25232015-02-04 20:55:43 +00008163 void *user)
8164{
8165 isl_set *half;
8166
8167 if (!set)
Tobias Grosser72745c22017-02-17 05:11:16 +00008168 return isl_stat_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00008169 if (isl_set_plain_is_empty(set)) {
8170 isl_set_free(set);
Tobias Grosser72745c22017-02-17 05:11:16 +00008171 return isl_stat_ok;
Tobias Grosser52a25232015-02-04 20:55:43 +00008172 }
8173 if (first == len)
8174 return fn(set, signs, user);
8175
8176 signs[first] = 1;
8177 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
8178 1 + first));
8179 half = isl_set_intersect(half, isl_set_copy(set));
8180 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
8181 goto error;
8182
8183 signs[first] = -1;
8184 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
8185 1 + first));
8186 half = isl_set_intersect(half, set);
8187 return foreach_orthant(half, signs, first + 1, len, fn, user);
8188error:
8189 isl_set_free(set);
Tobias Grosser72745c22017-02-17 05:11:16 +00008190 return isl_stat_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00008191}
8192
8193/* Call "fn" on the intersections of "set" with each of the orthants
8194 * (except for obviously empty intersections). The orthant is identified
8195 * by the signs array, with each entry having value 1 or -1 according
8196 * to the sign of the corresponding variable.
8197 */
Tobias Grosser72745c22017-02-17 05:11:16 +00008198isl_stat isl_set_foreach_orthant(__isl_keep isl_set *set,
8199 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
Tobias Grosser52a25232015-02-04 20:55:43 +00008200 void *user)
8201{
8202 unsigned nparam;
8203 unsigned nvar;
8204 int *signs;
Tobias Grosser72745c22017-02-17 05:11:16 +00008205 isl_stat r;
Tobias Grosser52a25232015-02-04 20:55:43 +00008206
8207 if (!set)
Tobias Grosser72745c22017-02-17 05:11:16 +00008208 return isl_stat_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00008209 if (isl_set_plain_is_empty(set))
Tobias Grosser72745c22017-02-17 05:11:16 +00008210 return isl_stat_ok;
Tobias Grosser52a25232015-02-04 20:55:43 +00008211
8212 nparam = isl_set_dim(set, isl_dim_param);
8213 nvar = isl_set_dim(set, isl_dim_set);
8214
8215 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
8216
8217 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
8218 fn, user);
8219
8220 free(signs);
8221
8222 return r;
8223}
8224
Tobias Grosserb2f39922015-05-28 13:32:11 +00008225isl_bool isl_set_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
Tobias Grosser52a25232015-02-04 20:55:43 +00008226{
Tobias Grosser06e15922016-11-16 11:06:47 +00008227 return isl_map_is_equal(set_to_map(set1), set_to_map(set2));
Tobias Grosser52a25232015-02-04 20:55:43 +00008228}
8229
Tobias Grosserb2f39922015-05-28 13:32:11 +00008230isl_bool isl_basic_map_is_subset(__isl_keep isl_basic_map *bmap1,
8231 __isl_keep isl_basic_map *bmap2)
Tobias Grosser52a25232015-02-04 20:55:43 +00008232{
8233 int is_subset;
8234 struct isl_map *map1;
8235 struct isl_map *map2;
8236
8237 if (!bmap1 || !bmap2)
Tobias Grosserb2f39922015-05-28 13:32:11 +00008238 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00008239
8240 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
8241 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
8242
8243 is_subset = isl_map_is_subset(map1, map2);
8244
8245 isl_map_free(map1);
8246 isl_map_free(map2);
8247
8248 return is_subset;
8249}
8250
Tobias Grosserb2f39922015-05-28 13:32:11 +00008251isl_bool isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
Tobias Grosser52a25232015-02-04 20:55:43 +00008252 __isl_keep isl_basic_set *bset2)
8253{
8254 return isl_basic_map_is_subset(bset1, bset2);
8255}
8256
Tobias Grosserb2f39922015-05-28 13:32:11 +00008257isl_bool isl_basic_map_is_equal(__isl_keep isl_basic_map *bmap1,
8258 __isl_keep isl_basic_map *bmap2)
Tobias Grosser52a25232015-02-04 20:55:43 +00008259{
Tobias Grosserb2f39922015-05-28 13:32:11 +00008260 isl_bool is_subset;
Tobias Grosser52a25232015-02-04 20:55:43 +00008261
8262 if (!bmap1 || !bmap2)
Tobias Grosserb2f39922015-05-28 13:32:11 +00008263 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00008264 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
Tobias Grosserb2f39922015-05-28 13:32:11 +00008265 if (is_subset != isl_bool_true)
Tobias Grosser52a25232015-02-04 20:55:43 +00008266 return is_subset;
8267 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
8268 return is_subset;
8269}
8270
Tobias Grosserb2f39922015-05-28 13:32:11 +00008271isl_bool isl_basic_set_is_equal(__isl_keep isl_basic_set *bset1,
8272 __isl_keep isl_basic_set *bset2)
Tobias Grosser52a25232015-02-04 20:55:43 +00008273{
8274 return isl_basic_map_is_equal(
Tobias Grosser06e15922016-11-16 11:06:47 +00008275 bset_to_bmap(bset1), bset_to_bmap(bset2));
Tobias Grosser52a25232015-02-04 20:55:43 +00008276}
8277
Tobias Grosserb2f39922015-05-28 13:32:11 +00008278isl_bool isl_map_is_empty(__isl_keep isl_map *map)
Tobias Grosser52a25232015-02-04 20:55:43 +00008279{
8280 int i;
8281 int is_empty;
8282
8283 if (!map)
Tobias Grosserb2f39922015-05-28 13:32:11 +00008284 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00008285 for (i = 0; i < map->n; ++i) {
8286 is_empty = isl_basic_map_is_empty(map->p[i]);
8287 if (is_empty < 0)
Tobias Grosserb2f39922015-05-28 13:32:11 +00008288 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00008289 if (!is_empty)
Tobias Grosserb2f39922015-05-28 13:32:11 +00008290 return isl_bool_false;
Tobias Grosser52a25232015-02-04 20:55:43 +00008291 }
Tobias Grosserb2f39922015-05-28 13:32:11 +00008292 return isl_bool_true;
Tobias Grosser52a25232015-02-04 20:55:43 +00008293}
8294
Tobias Grosserb2f39922015-05-28 13:32:11 +00008295isl_bool isl_map_plain_is_empty(__isl_keep isl_map *map)
Tobias Grosser52a25232015-02-04 20:55:43 +00008296{
Tobias Grosserb2f39922015-05-28 13:32:11 +00008297 return map ? map->n == 0 : isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00008298}
8299
Tobias Grosserb2f39922015-05-28 13:32:11 +00008300isl_bool isl_set_plain_is_empty(__isl_keep isl_set *set)
Tobias Grosser52a25232015-02-04 20:55:43 +00008301{
Tobias Grosserb2f39922015-05-28 13:32:11 +00008302 return set ? set->n == 0 : isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00008303}
8304
Tobias Grosserb2f39922015-05-28 13:32:11 +00008305isl_bool isl_set_is_empty(__isl_keep isl_set *set)
Tobias Grosser52a25232015-02-04 20:55:43 +00008306{
Tobias Grosser06e15922016-11-16 11:06:47 +00008307 return isl_map_is_empty(set_to_map(set));
Tobias Grosser52a25232015-02-04 20:55:43 +00008308}
8309
Tobias Grosser72745c22017-02-17 05:11:16 +00008310isl_bool isl_map_has_equal_space(__isl_keep isl_map *map1,
8311 __isl_keep isl_map *map2)
Tobias Grosser52a25232015-02-04 20:55:43 +00008312{
8313 if (!map1 || !map2)
Tobias Grosser72745c22017-02-17 05:11:16 +00008314 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00008315
8316 return isl_space_is_equal(map1->dim, map2->dim);
8317}
8318
Tobias Grosser72745c22017-02-17 05:11:16 +00008319isl_bool isl_set_has_equal_space(__isl_keep isl_set *set1,
8320 __isl_keep isl_set *set2)
Tobias Grosser52a25232015-02-04 20:55:43 +00008321{
8322 if (!set1 || !set2)
Tobias Grosser72745c22017-02-17 05:11:16 +00008323 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00008324
8325 return isl_space_is_equal(set1->dim, set2->dim);
8326}
8327
Tobias Grosserb2f39922015-05-28 13:32:11 +00008328static isl_bool map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
Tobias Grosser52a25232015-02-04 20:55:43 +00008329{
Tobias Grosserb2f39922015-05-28 13:32:11 +00008330 isl_bool is_subset;
Tobias Grosser52a25232015-02-04 20:55:43 +00008331
8332 if (!map1 || !map2)
Tobias Grosserb2f39922015-05-28 13:32:11 +00008333 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00008334 is_subset = isl_map_is_subset(map1, map2);
Tobias Grosserb2f39922015-05-28 13:32:11 +00008335 if (is_subset != isl_bool_true)
Tobias Grosser52a25232015-02-04 20:55:43 +00008336 return is_subset;
8337 is_subset = isl_map_is_subset(map2, map1);
8338 return is_subset;
8339}
8340
Tobias Grosser932ec012016-07-06 09:11:00 +00008341/* Is "map1" equal to "map2"?
8342 *
8343 * First check if they are obviously equal.
8344 * If not, then perform a more detailed analysis.
8345 */
Tobias Grosserb2f39922015-05-28 13:32:11 +00008346isl_bool isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
Tobias Grosser52a25232015-02-04 20:55:43 +00008347{
Tobias Grosser932ec012016-07-06 09:11:00 +00008348 isl_bool equal;
8349
8350 equal = isl_map_plain_is_equal(map1, map2);
8351 if (equal < 0 || equal)
8352 return equal;
Tobias Grosser52a25232015-02-04 20:55:43 +00008353 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
8354}
8355
Tobias Grosserb2f39922015-05-28 13:32:11 +00008356isl_bool isl_basic_map_is_strict_subset(
Tobias Grosser52a25232015-02-04 20:55:43 +00008357 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8358{
Tobias Grosserb2f39922015-05-28 13:32:11 +00008359 isl_bool is_subset;
Tobias Grosser52a25232015-02-04 20:55:43 +00008360
8361 if (!bmap1 || !bmap2)
Tobias Grosserb2f39922015-05-28 13:32:11 +00008362 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00008363 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
Tobias Grosserb2f39922015-05-28 13:32:11 +00008364 if (is_subset != isl_bool_true)
Tobias Grosser52a25232015-02-04 20:55:43 +00008365 return is_subset;
8366 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
Tobias Grosserb2f39922015-05-28 13:32:11 +00008367 if (is_subset == isl_bool_error)
Tobias Grosser52a25232015-02-04 20:55:43 +00008368 return is_subset;
8369 return !is_subset;
8370}
8371
Tobias Grosserb2f39922015-05-28 13:32:11 +00008372isl_bool isl_map_is_strict_subset(__isl_keep isl_map *map1,
8373 __isl_keep isl_map *map2)
Tobias Grosser52a25232015-02-04 20:55:43 +00008374{
Tobias Grosserb2f39922015-05-28 13:32:11 +00008375 isl_bool is_subset;
Tobias Grosser52a25232015-02-04 20:55:43 +00008376
8377 if (!map1 || !map2)
Tobias Grosserb2f39922015-05-28 13:32:11 +00008378 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00008379 is_subset = isl_map_is_subset(map1, map2);
Tobias Grosserb2f39922015-05-28 13:32:11 +00008380 if (is_subset != isl_bool_true)
Tobias Grosser52a25232015-02-04 20:55:43 +00008381 return is_subset;
8382 is_subset = isl_map_is_subset(map2, map1);
Tobias Grosserb2f39922015-05-28 13:32:11 +00008383 if (is_subset == isl_bool_error)
Tobias Grosser52a25232015-02-04 20:55:43 +00008384 return is_subset;
8385 return !is_subset;
8386}
8387
Tobias Grosserb2f39922015-05-28 13:32:11 +00008388isl_bool isl_set_is_strict_subset(__isl_keep isl_set *set1,
8389 __isl_keep isl_set *set2)
Tobias Grosser52a25232015-02-04 20:55:43 +00008390{
Tobias Grosser06e15922016-11-16 11:06:47 +00008391 return isl_map_is_strict_subset(set_to_map(set1), set_to_map(set2));
Tobias Grosser52a25232015-02-04 20:55:43 +00008392}
8393
Tobias Grossera67ac972016-02-26 11:35:12 +00008394/* Is "bmap" obviously equal to the universe with the same space?
8395 *
8396 * That is, does it not have any constraints?
8397 */
8398isl_bool isl_basic_map_plain_is_universe(__isl_keep isl_basic_map *bmap)
Tobias Grosser52a25232015-02-04 20:55:43 +00008399{
8400 if (!bmap)
Tobias Grosserb2f39922015-05-28 13:32:11 +00008401 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00008402 return bmap->n_eq == 0 && bmap->n_ineq == 0;
8403}
8404
Tobias Grossera67ac972016-02-26 11:35:12 +00008405/* Is "bset" obviously equal to the universe with the same space?
8406 */
8407isl_bool isl_basic_set_plain_is_universe(__isl_keep isl_basic_set *bset)
8408{
8409 return isl_basic_map_plain_is_universe(bset);
8410}
8411
8412/* If "c" does not involve any existentially quantified variables,
8413 * then set *univ to false and abort
8414 */
8415static isl_stat involves_divs(__isl_take isl_constraint *c, void *user)
8416{
8417 isl_bool *univ = user;
8418 unsigned n;
8419
8420 n = isl_constraint_dim(c, isl_dim_div);
8421 *univ = isl_constraint_involves_dims(c, isl_dim_div, 0, n);
8422 isl_constraint_free(c);
8423 if (*univ < 0 || !*univ)
8424 return isl_stat_error;
8425 return isl_stat_ok;
8426}
8427
8428/* Is "bmap" equal to the universe with the same space?
8429 *
8430 * First check if it is obviously equal to the universe.
8431 * If not and if there are any constraints not involving
8432 * existentially quantified variables, then it is certainly
8433 * not equal to the universe.
8434 * Otherwise, check if the universe is a subset of "bmap".
8435 */
8436isl_bool isl_basic_map_is_universe(__isl_keep isl_basic_map *bmap)
8437{
8438 isl_bool univ;
8439 isl_basic_map *test;
8440
8441 univ = isl_basic_map_plain_is_universe(bmap);
8442 if (univ < 0 || univ)
8443 return univ;
8444 if (isl_basic_map_dim(bmap, isl_dim_div) == 0)
8445 return isl_bool_false;
8446 univ = isl_bool_true;
8447 if (isl_basic_map_foreach_constraint(bmap, &involves_divs, &univ) < 0 &&
8448 univ)
8449 return isl_bool_error;
8450 if (univ < 0 || !univ)
8451 return univ;
8452 test = isl_basic_map_universe(isl_basic_map_get_space(bmap));
8453 univ = isl_basic_map_is_subset(test, bmap);
8454 isl_basic_map_free(test);
8455 return univ;
8456}
8457
8458/* Is "bset" equal to the universe with the same space?
8459 */
Tobias Grosserb2f39922015-05-28 13:32:11 +00008460isl_bool isl_basic_set_is_universe(__isl_keep isl_basic_set *bset)
Tobias Grosser52a25232015-02-04 20:55:43 +00008461{
Tobias Grossera67ac972016-02-26 11:35:12 +00008462 return isl_basic_map_is_universe(bset);
Tobias Grosser52a25232015-02-04 20:55:43 +00008463}
8464
Tobias Grosserb2f39922015-05-28 13:32:11 +00008465isl_bool isl_map_plain_is_universe(__isl_keep isl_map *map)
Tobias Grosser52a25232015-02-04 20:55:43 +00008466{
8467 int i;
8468
8469 if (!map)
Tobias Grosserb2f39922015-05-28 13:32:11 +00008470 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00008471
8472 for (i = 0; i < map->n; ++i) {
Tobias Grossera67ac972016-02-26 11:35:12 +00008473 isl_bool r = isl_basic_map_plain_is_universe(map->p[i]);
Tobias Grosser52a25232015-02-04 20:55:43 +00008474 if (r < 0 || r)
8475 return r;
8476 }
8477
Tobias Grosserb2f39922015-05-28 13:32:11 +00008478 return isl_bool_false;
Tobias Grosser52a25232015-02-04 20:55:43 +00008479}
8480
Tobias Grosserb2f39922015-05-28 13:32:11 +00008481isl_bool isl_set_plain_is_universe(__isl_keep isl_set *set)
Tobias Grosser52a25232015-02-04 20:55:43 +00008482{
Tobias Grosser06e15922016-11-16 11:06:47 +00008483 return isl_map_plain_is_universe(set_to_map(set));
Tobias Grosser52a25232015-02-04 20:55:43 +00008484}
8485
Tobias Grosserb2f39922015-05-28 13:32:11 +00008486isl_bool isl_basic_map_is_empty(__isl_keep isl_basic_map *bmap)
Tobias Grosser52a25232015-02-04 20:55:43 +00008487{
8488 struct isl_basic_set *bset = NULL;
8489 struct isl_vec *sample = NULL;
Tobias Grossera67ac972016-02-26 11:35:12 +00008490 isl_bool empty, non_empty;
Tobias Grosser52a25232015-02-04 20:55:43 +00008491
8492 if (!bmap)
Tobias Grosserb2f39922015-05-28 13:32:11 +00008493 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00008494
8495 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
Tobias Grosserb2f39922015-05-28 13:32:11 +00008496 return isl_bool_true;
Tobias Grosser52a25232015-02-04 20:55:43 +00008497
Tobias Grossera67ac972016-02-26 11:35:12 +00008498 if (isl_basic_map_plain_is_universe(bmap))
Tobias Grosserb2f39922015-05-28 13:32:11 +00008499 return isl_bool_false;
Tobias Grosser52a25232015-02-04 20:55:43 +00008500
8501 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
8502 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
8503 copy = isl_basic_map_remove_redundancies(copy);
8504 empty = isl_basic_map_plain_is_empty(copy);
8505 isl_basic_map_free(copy);
8506 return empty;
8507 }
8508
Tobias Grossera67ac972016-02-26 11:35:12 +00008509 non_empty = isl_basic_map_plain_is_non_empty(bmap);
8510 if (non_empty < 0)
8511 return isl_bool_error;
8512 if (non_empty)
8513 return isl_bool_false;
Tobias Grosser52a25232015-02-04 20:55:43 +00008514 isl_vec_free(bmap->sample);
8515 bmap->sample = NULL;
8516 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
8517 if (!bset)
Tobias Grosserb2f39922015-05-28 13:32:11 +00008518 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00008519 sample = isl_basic_set_sample_vec(bset);
8520 if (!sample)
Tobias Grosserb2f39922015-05-28 13:32:11 +00008521 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00008522 empty = sample->size == 0;
8523 isl_vec_free(bmap->sample);
8524 bmap->sample = sample;
8525 if (empty)
8526 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
8527
8528 return empty;
8529}
8530
Tobias Grosserb2f39922015-05-28 13:32:11 +00008531isl_bool isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
Tobias Grosser52a25232015-02-04 20:55:43 +00008532{
8533 if (!bmap)
Tobias Grosserb2f39922015-05-28 13:32:11 +00008534 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00008535 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
8536}
8537
Tobias Grosserb2f39922015-05-28 13:32:11 +00008538isl_bool isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
Tobias Grosser52a25232015-02-04 20:55:43 +00008539{
8540 if (!bset)
Tobias Grosserb2f39922015-05-28 13:32:11 +00008541 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00008542 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
8543}
8544
Tobias Grossera67ac972016-02-26 11:35:12 +00008545/* Is "bmap" known to be non-empty?
8546 *
8547 * That is, is the cached sample still valid?
8548 */
8549isl_bool isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map *bmap)
8550{
8551 unsigned total;
8552
8553 if (!bmap)
8554 return isl_bool_error;
8555 if (!bmap->sample)
8556 return isl_bool_false;
8557 total = 1 + isl_basic_map_total_dim(bmap);
8558 if (bmap->sample->size != total)
8559 return isl_bool_false;
8560 return isl_basic_map_contains(bmap, bmap->sample);
8561}
8562
Tobias Grosserb2f39922015-05-28 13:32:11 +00008563isl_bool isl_basic_set_is_empty(__isl_keep isl_basic_set *bset)
Tobias Grosser52a25232015-02-04 20:55:43 +00008564{
Tobias Grosser06e15922016-11-16 11:06:47 +00008565 return isl_basic_map_is_empty(bset_to_bmap(bset));
Tobias Grosser52a25232015-02-04 20:55:43 +00008566}
8567
8568struct isl_map *isl_basic_map_union(
8569 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8570{
8571 struct isl_map *map;
8572 if (!bmap1 || !bmap2)
8573 goto error;
8574
8575 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
8576
8577 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
8578 if (!map)
8579 goto error;
8580 map = isl_map_add_basic_map(map, bmap1);
8581 map = isl_map_add_basic_map(map, bmap2);
8582 return map;
8583error:
8584 isl_basic_map_free(bmap1);
8585 isl_basic_map_free(bmap2);
8586 return NULL;
8587}
8588
8589struct isl_set *isl_basic_set_union(
8590 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
8591{
Tobias Grosser06e15922016-11-16 11:06:47 +00008592 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1),
8593 bset_to_bmap(bset2)));
Tobias Grosser52a25232015-02-04 20:55:43 +00008594}
8595
8596/* Order divs such that any div only depends on previous divs */
8597struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
8598{
8599 int i;
8600 unsigned off;
8601
8602 if (!bmap)
8603 return NULL;
8604
8605 off = isl_space_dim(bmap->dim, isl_dim_all);
8606
8607 for (i = 0; i < bmap->n_div; ++i) {
8608 int pos;
8609 if (isl_int_is_zero(bmap->div[i][0]))
8610 continue;
8611 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
8612 bmap->n_div-i);
8613 if (pos == -1)
8614 continue;
Tobias Grosser37034db2016-03-25 19:38:18 +00008615 if (pos == 0)
8616 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
8617 "integer division depends on itself",
8618 return isl_basic_map_free(bmap));
Tobias Grosser52a25232015-02-04 20:55:43 +00008619 isl_basic_map_swap_div(bmap, i, i + pos);
8620 --i;
8621 }
8622 return bmap;
8623}
8624
8625struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
8626{
Tobias Grosser06e15922016-11-16 11:06:47 +00008627 return bset_from_bmap(isl_basic_map_order_divs(bset_to_bmap(bset)));
Tobias Grosser52a25232015-02-04 20:55:43 +00008628}
8629
8630__isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
8631{
8632 int i;
8633
8634 if (!map)
8635 return 0;
8636
8637 for (i = 0; i < map->n; ++i) {
8638 map->p[i] = isl_basic_map_order_divs(map->p[i]);
8639 if (!map->p[i])
8640 goto error;
8641 }
8642
8643 return map;
8644error:
8645 isl_map_free(map);
8646 return NULL;
8647}
8648
Tobias Grosser06e15922016-11-16 11:06:47 +00008649/* Sort the local variables of "bset".
8650 */
8651__isl_give isl_basic_set *isl_basic_set_sort_divs(
8652 __isl_take isl_basic_set *bset)
8653{
8654 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset)));
8655}
8656
Tobias Grosser52a25232015-02-04 20:55:43 +00008657/* Apply the expansion computed by isl_merge_divs.
8658 * The expansion itself is given by "exp" while the resulting
8659 * list of divs is given by "div".
Tobias Grossera67ac972016-02-26 11:35:12 +00008660 *
Tobias Grosser07b20952016-06-12 04:30:40 +00008661 * Move the integer divisions of "bmap" into the right position
Tobias Grossera67ac972016-02-26 11:35:12 +00008662 * according to "exp" and then introduce the additional integer
8663 * divisions, adding div constraints.
8664 * The moving should be done first to avoid moving coefficients
8665 * in the definitions of the extra integer divisions.
Tobias Grosser52a25232015-02-04 20:55:43 +00008666 */
Tobias Grosser07b20952016-06-12 04:30:40 +00008667__isl_give isl_basic_map *isl_basic_map_expand_divs(
Tobias Grosser06e15922016-11-16 11:06:47 +00008668 __isl_take isl_basic_map *bmap, __isl_take isl_mat *div, int *exp)
Tobias Grosser52a25232015-02-04 20:55:43 +00008669{
8670 int i, j;
8671 int n_div;
8672
Tobias Grosser07b20952016-06-12 04:30:40 +00008673 bmap = isl_basic_map_cow(bmap);
8674 if (!bmap || !div)
Tobias Grosser52a25232015-02-04 20:55:43 +00008675 goto error;
8676
Tobias Grosser07b20952016-06-12 04:30:40 +00008677 if (div->n_row < bmap->n_div)
Tobias Grosser52a25232015-02-04 20:55:43 +00008678 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
8679 "not an expansion", goto error);
8680
Tobias Grosser07b20952016-06-12 04:30:40 +00008681 n_div = bmap->n_div;
8682 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
Tobias Grosser52a25232015-02-04 20:55:43 +00008683 div->n_row - n_div, 0,
8684 2 * (div->n_row - n_div));
8685
8686 for (i = n_div; i < div->n_row; ++i)
Tobias Grosser07b20952016-06-12 04:30:40 +00008687 if (isl_basic_map_alloc_div(bmap) < 0)
Tobias Grosser52a25232015-02-04 20:55:43 +00008688 goto error;
8689
Tobias Grossera67ac972016-02-26 11:35:12 +00008690 for (j = n_div - 1; j >= 0; --j) {
8691 if (exp[j] == j)
8692 break;
Tobias Grosser07b20952016-06-12 04:30:40 +00008693 isl_basic_map_swap_div(bmap, j, exp[j]);
Tobias Grossera67ac972016-02-26 11:35:12 +00008694 }
8695 j = 0;
8696 for (i = 0; i < div->n_row; ++i) {
8697 if (j < n_div && exp[j] == i) {
8698 j++;
Tobias Grosser52a25232015-02-04 20:55:43 +00008699 } else {
Tobias Grosser07b20952016-06-12 04:30:40 +00008700 isl_seq_cpy(bmap->div[i], div->row[i], div->n_col);
Tobias Grosser932ec012016-07-06 09:11:00 +00008701 if (isl_basic_map_div_is_marked_unknown(bmap, i))
Tobias Grosser07b20952016-06-12 04:30:40 +00008702 continue;
8703 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
Tobias Grosser52a25232015-02-04 20:55:43 +00008704 goto error;
8705 }
8706 }
8707
8708 isl_mat_free(div);
Tobias Grosser07b20952016-06-12 04:30:40 +00008709 return bmap;
Tobias Grosser52a25232015-02-04 20:55:43 +00008710error:
Tobias Grosser07b20952016-06-12 04:30:40 +00008711 isl_basic_map_free(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +00008712 isl_mat_free(div);
8713 return NULL;
8714}
8715
Tobias Grosser07b20952016-06-12 04:30:40 +00008716/* Apply the expansion computed by isl_merge_divs.
8717 * The expansion itself is given by "exp" while the resulting
8718 * list of divs is given by "div".
8719 */
8720__isl_give isl_basic_set *isl_basic_set_expand_divs(
8721 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
8722{
8723 return isl_basic_map_expand_divs(bset, div, exp);
8724}
8725
Tobias Grosser52a25232015-02-04 20:55:43 +00008726/* Look for a div in dst that corresponds to the div "div" in src.
8727 * The divs before "div" in src and dst are assumed to be the same.
8728 *
8729 * Returns -1 if no corresponding div was found and the position
8730 * of the corresponding div in dst otherwise.
8731 */
8732static int find_div(struct isl_basic_map *dst,
8733 struct isl_basic_map *src, unsigned div)
8734{
8735 int i;
8736
8737 unsigned total = isl_space_dim(src->dim, isl_dim_all);
8738
8739 isl_assert(dst->ctx, div <= dst->n_div, return -1);
8740 for (i = div; i < dst->n_div; ++i)
8741 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
8742 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
8743 dst->n_div - div) == -1)
8744 return i;
8745 return -1;
8746}
8747
8748/* Align the divs of "dst" to those of "src", adding divs from "src"
8749 * if needed. That is, make sure that the first src->n_div divs
8750 * of the result are equal to those of src.
8751 *
8752 * The result is not finalized as by design it will have redundant
8753 * divs if any divs from "src" were copied.
8754 */
8755__isl_give isl_basic_map *isl_basic_map_align_divs(
8756 __isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src)
8757{
8758 int i;
8759 int known, extended;
8760 unsigned total;
8761
8762 if (!dst || !src)
8763 return isl_basic_map_free(dst);
8764
8765 if (src->n_div == 0)
8766 return dst;
8767
8768 known = isl_basic_map_divs_known(src);
8769 if (known < 0)
8770 return isl_basic_map_free(dst);
8771 if (!known)
8772 isl_die(isl_basic_map_get_ctx(src), isl_error_invalid,
8773 "some src divs are unknown",
8774 return isl_basic_map_free(dst));
8775
8776 src = isl_basic_map_order_divs(src);
8777
8778 extended = 0;
8779 total = isl_space_dim(src->dim, isl_dim_all);
8780 for (i = 0; i < src->n_div; ++i) {
8781 int j = find_div(dst, src, i);
8782 if (j < 0) {
8783 if (!extended) {
8784 int extra = src->n_div - i;
8785 dst = isl_basic_map_cow(dst);
8786 if (!dst)
8787 return NULL;
8788 dst = isl_basic_map_extend_space(dst,
8789 isl_space_copy(dst->dim),
8790 extra, 0, 2 * extra);
8791 extended = 1;
8792 }
8793 j = isl_basic_map_alloc_div(dst);
8794 if (j < 0)
8795 return isl_basic_map_free(dst);
8796 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
8797 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
8798 if (isl_basic_map_add_div_constraints(dst, j) < 0)
8799 return isl_basic_map_free(dst);
8800 }
8801 if (j != i)
8802 isl_basic_map_swap_div(dst, i, j);
8803 }
8804 return dst;
8805}
8806
Tobias Grosser29eaa162017-03-21 09:12:11 +00008807__isl_give isl_map *isl_map_align_divs_internal(__isl_take isl_map *map)
Tobias Grosser52a25232015-02-04 20:55:43 +00008808{
8809 int i;
8810
8811 if (!map)
8812 return NULL;
8813 if (map->n == 0)
8814 return map;
8815 map = isl_map_compute_divs(map);
8816 map = isl_map_cow(map);
8817 if (!map)
8818 return NULL;
8819
8820 for (i = 1; i < map->n; ++i)
8821 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
8822 for (i = 1; i < map->n; ++i) {
8823 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
8824 if (!map->p[i])
8825 return isl_map_free(map);
8826 }
8827
8828 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
8829 return map;
8830}
8831
Tobias Grosser29eaa162017-03-21 09:12:11 +00008832__isl_give isl_map *isl_map_align_divs(__isl_take isl_map *map)
8833{
8834 return isl_map_align_divs_internal(map);
8835}
8836
Tobias Grosser52a25232015-02-04 20:55:43 +00008837struct isl_set *isl_set_align_divs(struct isl_set *set)
8838{
Tobias Grosser29eaa162017-03-21 09:12:11 +00008839 return set_from_map(isl_map_align_divs_internal(set_to_map(set)));
Tobias Grosser52a25232015-02-04 20:55:43 +00008840}
8841
Tobias Grosser1fa7b972015-02-16 19:33:40 +00008842/* Align the divs of the basic maps in "map" to those
8843 * of the basic maps in "list", as well as to the other basic maps in "map".
Tobias Grosser52a25232015-02-04 20:55:43 +00008844 * The elements in "list" are assumed to have known divs.
8845 */
Tobias Grosser1fa7b972015-02-16 19:33:40 +00008846__isl_give isl_map *isl_map_align_divs_to_basic_map_list(
8847 __isl_take isl_map *map, __isl_keep isl_basic_map_list *list)
Tobias Grosser52a25232015-02-04 20:55:43 +00008848{
8849 int i, n;
8850
Tobias Grosser1fa7b972015-02-16 19:33:40 +00008851 map = isl_map_compute_divs(map);
8852 map = isl_map_cow(map);
8853 if (!map || !list)
8854 return isl_map_free(map);
8855 if (map->n == 0)
8856 return map;
Tobias Grosser52a25232015-02-04 20:55:43 +00008857
Tobias Grosser1fa7b972015-02-16 19:33:40 +00008858 n = isl_basic_map_list_n_basic_map(list);
Tobias Grosser52a25232015-02-04 20:55:43 +00008859 for (i = 0; i < n; ++i) {
Tobias Grosser1fa7b972015-02-16 19:33:40 +00008860 isl_basic_map *bmap;
Tobias Grosser52a25232015-02-04 20:55:43 +00008861
Tobias Grosser1fa7b972015-02-16 19:33:40 +00008862 bmap = isl_basic_map_list_get_basic_map(list, i);
8863 map->p[0] = isl_basic_map_align_divs(map->p[0], bmap);
8864 isl_basic_map_free(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +00008865 }
Tobias Grosser1fa7b972015-02-16 19:33:40 +00008866 if (!map->p[0])
8867 return isl_map_free(map);
Tobias Grosser52a25232015-02-04 20:55:43 +00008868
Tobias Grosser29eaa162017-03-21 09:12:11 +00008869 return isl_map_align_divs_internal(map);
Tobias Grosser52a25232015-02-04 20:55:43 +00008870}
8871
Tobias Grosser1fa7b972015-02-16 19:33:40 +00008872/* Align the divs of each element of "list" to those of "bmap".
8873 * Both "bmap" and the elements of "list" are assumed to have known divs.
Tobias Grosser52a25232015-02-04 20:55:43 +00008874 */
Tobias Grosser1fa7b972015-02-16 19:33:40 +00008875__isl_give isl_basic_map_list *isl_basic_map_list_align_divs_to_basic_map(
8876 __isl_take isl_basic_map_list *list, __isl_keep isl_basic_map *bmap)
Tobias Grosser52a25232015-02-04 20:55:43 +00008877{
8878 int i, n;
8879
Tobias Grosser1fa7b972015-02-16 19:33:40 +00008880 if (!list || !bmap)
8881 return isl_basic_map_list_free(list);
Tobias Grosser52a25232015-02-04 20:55:43 +00008882
Tobias Grosser1fa7b972015-02-16 19:33:40 +00008883 n = isl_basic_map_list_n_basic_map(list);
Tobias Grosser52a25232015-02-04 20:55:43 +00008884 for (i = 0; i < n; ++i) {
Tobias Grosser1fa7b972015-02-16 19:33:40 +00008885 isl_basic_map *bmap_i;
Tobias Grosser52a25232015-02-04 20:55:43 +00008886
Tobias Grosser1fa7b972015-02-16 19:33:40 +00008887 bmap_i = isl_basic_map_list_get_basic_map(list, i);
8888 bmap_i = isl_basic_map_align_divs(bmap_i, bmap);
8889 list = isl_basic_map_list_set_basic_map(list, i, bmap_i);
Tobias Grosser52a25232015-02-04 20:55:43 +00008890 }
8891
8892 return list;
8893}
8894
8895static __isl_give isl_set *set_apply( __isl_take isl_set *set,
8896 __isl_take isl_map *map)
8897{
Tobias Grosser72745c22017-02-17 05:11:16 +00008898 isl_bool ok;
8899
8900 ok = isl_map_compatible_domain(map, set);
8901 if (ok < 0)
Tobias Grosser52a25232015-02-04 20:55:43 +00008902 goto error;
Tobias Grosser72745c22017-02-17 05:11:16 +00008903 if (!ok)
8904 isl_die(isl_set_get_ctx(set), isl_error_invalid,
8905 "incompatible spaces", goto error);
Tobias Grosser52a25232015-02-04 20:55:43 +00008906 map = isl_map_intersect_domain(map, set);
8907 set = isl_map_range(map);
8908 return set;
8909error:
8910 isl_set_free(set);
8911 isl_map_free(map);
8912 return NULL;
8913}
8914
8915__isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
8916 __isl_take isl_map *map)
8917{
8918 return isl_map_align_params_map_map_and(set, map, &set_apply);
8919}
8920
8921/* There is no need to cow as removing empty parts doesn't change
8922 * the meaning of the set.
8923 */
8924struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
8925{
8926 int i;
8927
8928 if (!map)
8929 return NULL;
8930
8931 for (i = map->n - 1; i >= 0; --i)
8932 remove_if_empty(map, i);
8933
8934 return map;
8935}
8936
8937struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
8938{
Tobias Grosser06e15922016-11-16 11:06:47 +00008939 return set_from_map(isl_map_remove_empty_parts(set_to_map(set)));
Tobias Grosser52a25232015-02-04 20:55:43 +00008940}
8941
Tobias Grosser52a25232015-02-04 20:55:43 +00008942/* Given two basic sets bset1 and bset2, compute the maximal difference
8943 * between the values of dimension pos in bset1 and those in bset2
8944 * for any common value of the parameters and dimensions preceding pos.
8945 */
8946static enum isl_lp_result basic_set_maximal_difference_at(
8947 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
8948 int pos, isl_int *opt)
8949{
Tobias Grossereab0943e2016-11-22 21:31:59 +00008950 isl_basic_map *bmap1;
8951 isl_basic_map *bmap2;
Tobias Grosser52a25232015-02-04 20:55:43 +00008952 struct isl_ctx *ctx;
8953 struct isl_vec *obj;
8954 unsigned total;
8955 unsigned nparam;
Tobias Grossereab0943e2016-11-22 21:31:59 +00008956 unsigned dim1;
Tobias Grosser52a25232015-02-04 20:55:43 +00008957 enum isl_lp_result res;
8958
8959 if (!bset1 || !bset2)
8960 return isl_lp_error;
8961
8962 nparam = isl_basic_set_n_param(bset1);
8963 dim1 = isl_basic_set_n_dim(bset1);
Tobias Grossereab0943e2016-11-22 21:31:59 +00008964
8965 bmap1 = isl_basic_map_from_range(isl_basic_set_copy(bset1));
8966 bmap2 = isl_basic_map_from_range(isl_basic_set_copy(bset2));
8967 bmap1 = isl_basic_map_move_dims(bmap1, isl_dim_in, 0,
8968 isl_dim_out, 0, pos);
8969 bmap2 = isl_basic_map_move_dims(bmap2, isl_dim_in, 0,
8970 isl_dim_out, 0, pos);
8971 bmap1 = isl_basic_map_range_product(bmap1, bmap2);
Tobias Grosser52a25232015-02-04 20:55:43 +00008972 if (!bmap1)
Tobias Grossereab0943e2016-11-22 21:31:59 +00008973 return isl_lp_error;
8974
Tobias Grosser52a25232015-02-04 20:55:43 +00008975 total = isl_basic_map_total_dim(bmap1);
8976 ctx = bmap1->ctx;
8977 obj = isl_vec_alloc(ctx, 1 + total);
8978 if (!obj)
Tobias Grossereab0943e2016-11-22 21:31:59 +00008979 goto error;
Tobias Grosser52a25232015-02-04 20:55:43 +00008980 isl_seq_clr(obj->block.data, 1 + total);
8981 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
8982 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
8983 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
8984 opt, NULL, NULL);
8985 isl_basic_map_free(bmap1);
8986 isl_vec_free(obj);
8987 return res;
8988error:
Tobias Grosser52a25232015-02-04 20:55:43 +00008989 isl_basic_map_free(bmap1);
8990 return isl_lp_error;
8991}
8992
8993/* Given two _disjoint_ basic sets bset1 and bset2, check whether
8994 * for any common value of the parameters and dimensions preceding pos
8995 * in both basic sets, the values of dimension pos in bset1 are
8996 * smaller or larger than those in bset2.
8997 *
8998 * Returns
8999 * 1 if bset1 follows bset2
9000 * -1 if bset1 precedes bset2
9001 * 0 if bset1 and bset2 are incomparable
9002 * -2 if some error occurred.
9003 */
9004int isl_basic_set_compare_at(struct isl_basic_set *bset1,
9005 struct isl_basic_set *bset2, int pos)
9006{
9007 isl_int opt;
9008 enum isl_lp_result res;
9009 int cmp;
9010
9011 isl_int_init(opt);
9012
9013 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
9014
9015 if (res == isl_lp_empty)
9016 cmp = 0;
9017 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
9018 res == isl_lp_unbounded)
9019 cmp = 1;
9020 else if (res == isl_lp_ok && isl_int_is_neg(opt))
9021 cmp = -1;
9022 else
9023 cmp = -2;
9024
9025 isl_int_clear(opt);
9026 return cmp;
9027}
9028
9029/* Given two basic sets bset1 and bset2, check whether
9030 * for any common value of the parameters and dimensions preceding pos
9031 * there is a value of dimension pos in bset1 that is larger
9032 * than a value of the same dimension in bset2.
9033 *
9034 * Return
9035 * 1 if there exists such a pair
9036 * 0 if there is no such pair, but there is a pair of equal values
9037 * -1 otherwise
9038 * -2 if some error occurred.
9039 */
9040int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
9041 __isl_keep isl_basic_set *bset2, int pos)
9042{
9043 isl_int opt;
9044 enum isl_lp_result res;
9045 int cmp;
9046
9047 isl_int_init(opt);
9048
9049 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
9050
9051 if (res == isl_lp_empty)
9052 cmp = -1;
9053 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
9054 res == isl_lp_unbounded)
9055 cmp = 1;
9056 else if (res == isl_lp_ok && isl_int_is_neg(opt))
9057 cmp = -1;
9058 else if (res == isl_lp_ok)
9059 cmp = 0;
9060 else
9061 cmp = -2;
9062
9063 isl_int_clear(opt);
9064 return cmp;
9065}
9066
9067/* Given two sets set1 and set2, check whether
9068 * for any common value of the parameters and dimensions preceding pos
9069 * there is a value of dimension pos in set1 that is larger
9070 * than a value of the same dimension in set2.
9071 *
9072 * Return
9073 * 1 if there exists such a pair
9074 * 0 if there is no such pair, but there is a pair of equal values
9075 * -1 otherwise
9076 * -2 if some error occurred.
9077 */
9078int isl_set_follows_at(__isl_keep isl_set *set1,
9079 __isl_keep isl_set *set2, int pos)
9080{
9081 int i, j;
9082 int follows = -1;
9083
9084 if (!set1 || !set2)
9085 return -2;
9086
9087 for (i = 0; i < set1->n; ++i)
9088 for (j = 0; j < set2->n; ++j) {
9089 int f;
9090 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
9091 if (f == 1 || f == -2)
9092 return f;
9093 if (f > follows)
9094 follows = f;
9095 }
9096
9097 return follows;
9098}
9099
Tobias Grosser72745c22017-02-17 05:11:16 +00009100static isl_bool isl_basic_map_plain_has_fixed_var(
9101 __isl_keep isl_basic_map *bmap, unsigned pos, isl_int *val)
Tobias Grosser52a25232015-02-04 20:55:43 +00009102{
9103 int i;
9104 int d;
9105 unsigned total;
9106
9107 if (!bmap)
Tobias Grosser72745c22017-02-17 05:11:16 +00009108 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00009109 total = isl_basic_map_total_dim(bmap);
9110 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
9111 for (; d+1 > pos; --d)
9112 if (!isl_int_is_zero(bmap->eq[i][1+d]))
9113 break;
9114 if (d != pos)
9115 continue;
9116 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
Tobias Grosser72745c22017-02-17 05:11:16 +00009117 return isl_bool_false;
Tobias Grosser52a25232015-02-04 20:55:43 +00009118 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
Tobias Grosser72745c22017-02-17 05:11:16 +00009119 return isl_bool_false;
Tobias Grosser52a25232015-02-04 20:55:43 +00009120 if (!isl_int_is_one(bmap->eq[i][1+d]))
Tobias Grosser72745c22017-02-17 05:11:16 +00009121 return isl_bool_false;
Tobias Grosser52a25232015-02-04 20:55:43 +00009122 if (val)
9123 isl_int_neg(*val, bmap->eq[i][0]);
Tobias Grosser72745c22017-02-17 05:11:16 +00009124 return isl_bool_true;
Tobias Grosser52a25232015-02-04 20:55:43 +00009125 }
Tobias Grosser72745c22017-02-17 05:11:16 +00009126 return isl_bool_false;
Tobias Grosser52a25232015-02-04 20:55:43 +00009127}
9128
Tobias Grosser72745c22017-02-17 05:11:16 +00009129static isl_bool isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
Tobias Grosser52a25232015-02-04 20:55:43 +00009130 unsigned pos, isl_int *val)
9131{
9132 int i;
9133 isl_int v;
9134 isl_int tmp;
Tobias Grosser72745c22017-02-17 05:11:16 +00009135 isl_bool fixed;
Tobias Grosser52a25232015-02-04 20:55:43 +00009136
9137 if (!map)
Tobias Grosser72745c22017-02-17 05:11:16 +00009138 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00009139 if (map->n == 0)
Tobias Grosser72745c22017-02-17 05:11:16 +00009140 return isl_bool_false;
Tobias Grosser52a25232015-02-04 20:55:43 +00009141 if (map->n == 1)
9142 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
9143 isl_int_init(v);
9144 isl_int_init(tmp);
9145 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
Tobias Grosser72745c22017-02-17 05:11:16 +00009146 for (i = 1; fixed == isl_bool_true && i < map->n; ++i) {
Tobias Grosser52a25232015-02-04 20:55:43 +00009147 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
Tobias Grosser72745c22017-02-17 05:11:16 +00009148 if (fixed == isl_bool_true && isl_int_ne(tmp, v))
9149 fixed = isl_bool_false;
Tobias Grosser52a25232015-02-04 20:55:43 +00009150 }
9151 if (val)
9152 isl_int_set(*val, v);
9153 isl_int_clear(tmp);
9154 isl_int_clear(v);
9155 return fixed;
9156}
9157
Tobias Grosser72745c22017-02-17 05:11:16 +00009158static isl_bool isl_basic_set_plain_has_fixed_var(
9159 __isl_keep isl_basic_set *bset, unsigned pos, isl_int *val)
Tobias Grosser52a25232015-02-04 20:55:43 +00009160{
Tobias Grosser06e15922016-11-16 11:06:47 +00009161 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset),
Tobias Grosser52a25232015-02-04 20:55:43 +00009162 pos, val);
9163}
9164
Tobias Grosser72745c22017-02-17 05:11:16 +00009165isl_bool isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
Tobias Grosser52a25232015-02-04 20:55:43 +00009166 enum isl_dim_type type, unsigned pos, isl_int *val)
9167{
Tobias Grosser72745c22017-02-17 05:11:16 +00009168 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
9169 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00009170 return isl_basic_map_plain_has_fixed_var(bmap,
9171 isl_basic_map_offset(bmap, type) - 1 + pos, val);
9172}
9173
9174/* If "bmap" obviously lies on a hyperplane where the given dimension
9175 * has a fixed value, then return that value.
9176 * Otherwise return NaN.
9177 */
9178__isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
9179 __isl_keep isl_basic_map *bmap,
9180 enum isl_dim_type type, unsigned pos)
9181{
9182 isl_ctx *ctx;
9183 isl_val *v;
Tobias Grosser72745c22017-02-17 05:11:16 +00009184 isl_bool fixed;
Tobias Grosser52a25232015-02-04 20:55:43 +00009185
9186 if (!bmap)
9187 return NULL;
9188 ctx = isl_basic_map_get_ctx(bmap);
9189 v = isl_val_alloc(ctx);
9190 if (!v)
9191 return NULL;
9192 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
9193 if (fixed < 0)
9194 return isl_val_free(v);
9195 if (fixed) {
9196 isl_int_set_si(v->d, 1);
9197 return v;
9198 }
9199 isl_val_free(v);
9200 return isl_val_nan(ctx);
9201}
9202
Tobias Grosser72745c22017-02-17 05:11:16 +00009203isl_bool isl_map_plain_is_fixed(__isl_keep isl_map *map,
Tobias Grosser52a25232015-02-04 20:55:43 +00009204 enum isl_dim_type type, unsigned pos, isl_int *val)
9205{
9206 if (pos >= isl_map_dim(map, type))
Tobias Grosser72745c22017-02-17 05:11:16 +00009207 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9208 "position out of bounds", return isl_bool_error);
Tobias Grosser52a25232015-02-04 20:55:43 +00009209 return isl_map_plain_has_fixed_var(map,
9210 map_offset(map, type) - 1 + pos, val);
9211}
9212
9213/* If "map" obviously lies on a hyperplane where the given dimension
9214 * has a fixed value, then return that value.
9215 * Otherwise return NaN.
9216 */
9217__isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
9218 enum isl_dim_type type, unsigned pos)
9219{
9220 isl_ctx *ctx;
9221 isl_val *v;
Tobias Grosser72745c22017-02-17 05:11:16 +00009222 isl_bool fixed;
Tobias Grosser52a25232015-02-04 20:55:43 +00009223
9224 if (!map)
9225 return NULL;
9226 ctx = isl_map_get_ctx(map);
9227 v = isl_val_alloc(ctx);
9228 if (!v)
9229 return NULL;
9230 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
9231 if (fixed < 0)
9232 return isl_val_free(v);
9233 if (fixed) {
9234 isl_int_set_si(v->d, 1);
9235 return v;
9236 }
9237 isl_val_free(v);
9238 return isl_val_nan(ctx);
9239}
9240
9241/* If "set" obviously lies on a hyperplane where the given dimension
9242 * has a fixed value, then return that value.
9243 * Otherwise return NaN.
9244 */
9245__isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
9246 enum isl_dim_type type, unsigned pos)
9247{
9248 return isl_map_plain_get_val_if_fixed(set, type, pos);
9249}
9250
Tobias Grosser72745c22017-02-17 05:11:16 +00009251isl_bool isl_set_plain_is_fixed(__isl_keep isl_set *set,
Tobias Grosser52a25232015-02-04 20:55:43 +00009252 enum isl_dim_type type, unsigned pos, isl_int *val)
9253{
9254 return isl_map_plain_is_fixed(set, type, pos, val);
9255}
9256
Tobias Grosser52a25232015-02-04 20:55:43 +00009257/* Check if dimension dim has fixed value and if so and if val is not NULL,
9258 * then return this fixed value in *val.
9259 */
Tobias Grosser72745c22017-02-17 05:11:16 +00009260isl_bool isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
Tobias Grosser52a25232015-02-04 20:55:43 +00009261 unsigned dim, isl_int *val)
9262{
9263 return isl_basic_set_plain_has_fixed_var(bset,
9264 isl_basic_set_n_param(bset) + dim, val);
9265}
9266
Michael Kruse959a8dc2016-01-15 15:54:45 +00009267/* Return -1 if the constraint "c1" should be sorted before "c2"
9268 * and 1 if it should be sorted after "c2".
9269 * Return 0 if the two constraints are the same (up to the constant term).
9270 *
9271 * In particular, if a constraint involves later variables than another
9272 * then it is sorted after this other constraint.
9273 * uset_gist depends on constraints without existentially quantified
Tobias Grosser52a25232015-02-04 20:55:43 +00009274 * variables sorting first.
Michael Kruse959a8dc2016-01-15 15:54:45 +00009275 *
9276 * For constraints that have the same latest variable, those
9277 * with the same coefficient for this latest variable (first in absolute value
9278 * and then in actual value) are grouped together.
9279 * This is useful for detecting pairs of constraints that can
9280 * be chained in their printed representation.
9281 *
9282 * Finally, within a group, constraints are sorted according to
9283 * their coefficients (excluding the constant term).
Tobias Grosser52a25232015-02-04 20:55:43 +00009284 */
9285static int sort_constraint_cmp(const void *p1, const void *p2, void *arg)
9286{
9287 isl_int **c1 = (isl_int **) p1;
9288 isl_int **c2 = (isl_int **) p2;
9289 int l1, l2;
9290 unsigned size = *(unsigned *) arg;
Michael Kruse959a8dc2016-01-15 15:54:45 +00009291 int cmp;
Tobias Grosser52a25232015-02-04 20:55:43 +00009292
9293 l1 = isl_seq_last_non_zero(*c1 + 1, size);
9294 l2 = isl_seq_last_non_zero(*c2 + 1, size);
9295
9296 if (l1 != l2)
9297 return l1 - l2;
9298
Michael Kruse959a8dc2016-01-15 15:54:45 +00009299 cmp = isl_int_abs_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9300 if (cmp != 0)
9301 return cmp;
9302 cmp = isl_int_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9303 if (cmp != 0)
9304 return -cmp;
9305
Tobias Grosser52a25232015-02-04 20:55:43 +00009306 return isl_seq_cmp(*c1 + 1, *c2 + 1, size);
9307}
9308
Michael Kruse959a8dc2016-01-15 15:54:45 +00009309/* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
9310 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
9311 * and 0 if the two constraints are the same (up to the constant term).
9312 */
9313int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map *bmap,
9314 isl_int *c1, isl_int *c2)
9315{
9316 unsigned total;
9317
9318 if (!bmap)
9319 return -2;
9320 total = isl_basic_map_total_dim(bmap);
9321 return sort_constraint_cmp(&c1, &c2, &total);
9322}
9323
9324__isl_give isl_basic_map *isl_basic_map_sort_constraints(
9325 __isl_take isl_basic_map *bmap)
Tobias Grosser52a25232015-02-04 20:55:43 +00009326{
9327 unsigned total;
9328
9329 if (!bmap)
9330 return NULL;
9331 if (bmap->n_ineq == 0)
9332 return bmap;
9333 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
9334 return bmap;
9335 total = isl_basic_map_total_dim(bmap);
9336 if (isl_sort(bmap->ineq, bmap->n_ineq, sizeof(isl_int *),
9337 &sort_constraint_cmp, &total) < 0)
9338 return isl_basic_map_free(bmap);
9339 return bmap;
9340}
9341
9342__isl_give isl_basic_set *isl_basic_set_sort_constraints(
9343 __isl_take isl_basic_set *bset)
9344{
Tobias Grosser06e15922016-11-16 11:06:47 +00009345 isl_basic_map *bmap = bset_to_bmap(bset);
9346 return bset_from_bmap(isl_basic_map_sort_constraints(bmap));
Tobias Grosser52a25232015-02-04 20:55:43 +00009347}
9348
9349struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
9350{
9351 if (!bmap)
9352 return NULL;
9353 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
9354 return bmap;
9355 bmap = isl_basic_map_remove_redundancies(bmap);
9356 bmap = isl_basic_map_sort_constraints(bmap);
9357 if (bmap)
9358 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
9359 return bmap;
9360}
Tobias Grossercf66ea32017-02-28 07:06:06 +00009361int isl_basic_map_plain_cmp(__isl_keep isl_basic_map *bmap1,
9362 __isl_keep isl_basic_map *bmap2)
Tobias Grosser52a25232015-02-04 20:55:43 +00009363{
9364 int i, cmp;
9365 unsigned total;
Tobias Grosserf4fe34b2017-03-16 21:33:20 +00009366 isl_space *space1, *space2;
Tobias Grosser52a25232015-02-04 20:55:43 +00009367
Tobias Grosser3e6070e2015-05-09 09:37:30 +00009368 if (!bmap1 || !bmap2)
9369 return -1;
9370
Tobias Grosser52a25232015-02-04 20:55:43 +00009371 if (bmap1 == bmap2)
9372 return 0;
Tobias Grosserf4fe34b2017-03-16 21:33:20 +00009373 space1 = isl_basic_map_peek_space(bmap1);
9374 space2 = isl_basic_map_peek_space(bmap2);
9375 cmp = isl_space_cmp(space1, space2);
9376 if (cmp)
9377 return cmp;
Tobias Grosser52a25232015-02-04 20:55:43 +00009378 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
9379 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
9380 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
Tobias Grosser52a25232015-02-04 20:55:43 +00009381 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
9382 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9383 return 0;
9384 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
9385 return 1;
9386 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9387 return -1;
9388 if (bmap1->n_eq != bmap2->n_eq)
9389 return bmap1->n_eq - bmap2->n_eq;
9390 if (bmap1->n_ineq != bmap2->n_ineq)
9391 return bmap1->n_ineq - bmap2->n_ineq;
9392 if (bmap1->n_div != bmap2->n_div)
9393 return bmap1->n_div - bmap2->n_div;
9394 total = isl_basic_map_total_dim(bmap1);
9395 for (i = 0; i < bmap1->n_eq; ++i) {
9396 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
9397 if (cmp)
9398 return cmp;
9399 }
9400 for (i = 0; i < bmap1->n_ineq; ++i) {
9401 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
9402 if (cmp)
9403 return cmp;
9404 }
9405 for (i = 0; i < bmap1->n_div; ++i) {
9406 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
9407 if (cmp)
9408 return cmp;
9409 }
9410 return 0;
9411}
9412
Tobias Grossercf66ea32017-02-28 07:06:06 +00009413int isl_basic_set_plain_cmp(__isl_keep isl_basic_set *bset1,
9414 __isl_keep isl_basic_set *bset2)
Tobias Grosser52a25232015-02-04 20:55:43 +00009415{
9416 return isl_basic_map_plain_cmp(bset1, bset2);
9417}
9418
9419int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9420{
9421 int i, cmp;
9422
9423 if (set1 == set2)
9424 return 0;
9425 if (set1->n != set2->n)
9426 return set1->n - set2->n;
9427
9428 for (i = 0; i < set1->n; ++i) {
9429 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
9430 if (cmp)
9431 return cmp;
9432 }
9433
9434 return 0;
9435}
9436
Tobias Grosserb2f39922015-05-28 13:32:11 +00009437isl_bool isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
Tobias Grosser52a25232015-02-04 20:55:43 +00009438 __isl_keep isl_basic_map *bmap2)
9439{
Tobias Grossere0f8d592015-05-13 13:10:13 +00009440 if (!bmap1 || !bmap2)
Tobias Grosserb2f39922015-05-28 13:32:11 +00009441 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00009442 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
9443}
9444
Tobias Grosserb2f39922015-05-28 13:32:11 +00009445isl_bool isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
Tobias Grosser52a25232015-02-04 20:55:43 +00009446 __isl_keep isl_basic_set *bset2)
9447{
Tobias Grosser06e15922016-11-16 11:06:47 +00009448 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1),
9449 bset_to_bmap(bset2));
Tobias Grosser52a25232015-02-04 20:55:43 +00009450}
9451
9452static int qsort_bmap_cmp(const void *p1, const void *p2)
9453{
Tobias Grossercf66ea32017-02-28 07:06:06 +00009454 isl_basic_map *bmap1 = *(isl_basic_map **) p1;
9455 isl_basic_map *bmap2 = *(isl_basic_map **) p2;
Tobias Grosser52a25232015-02-04 20:55:43 +00009456
9457 return isl_basic_map_plain_cmp(bmap1, bmap2);
9458}
9459
9460/* Sort the basic maps of "map" and remove duplicate basic maps.
9461 *
9462 * While removing basic maps, we make sure that the basic maps remain
9463 * sorted because isl_map_normalize expects the basic maps of the result
9464 * to be sorted.
9465 */
9466static __isl_give isl_map *sort_and_remove_duplicates(__isl_take isl_map *map)
9467{
9468 int i, j;
9469
9470 map = isl_map_remove_empty_parts(map);
9471 if (!map)
9472 return NULL;
9473 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
9474 for (i = map->n - 1; i >= 1; --i) {
9475 if (!isl_basic_map_plain_is_equal(map->p[i - 1], map->p[i]))
9476 continue;
9477 isl_basic_map_free(map->p[i-1]);
9478 for (j = i; j < map->n; ++j)
9479 map->p[j - 1] = map->p[j];
9480 map->n--;
9481 }
9482
9483 return map;
9484}
9485
9486/* Remove obvious duplicates among the basic maps of "map".
9487 *
9488 * Unlike isl_map_normalize, this function does not remove redundant
9489 * constraints and only removes duplicates that have exactly the same
9490 * constraints in the input. It does sort the constraints and
9491 * the basic maps to ease the detection of duplicates.
9492 *
9493 * If "map" has already been normalized or if the basic maps are
9494 * disjoint, then there can be no duplicates.
9495 */
9496__isl_give isl_map *isl_map_remove_obvious_duplicates(__isl_take isl_map *map)
9497{
9498 int i;
9499 isl_basic_map *bmap;
9500
9501 if (!map)
9502 return NULL;
9503 if (map->n <= 1)
9504 return map;
9505 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED | ISL_MAP_DISJOINT))
9506 return map;
9507 for (i = 0; i < map->n; ++i) {
9508 bmap = isl_basic_map_copy(map->p[i]);
9509 bmap = isl_basic_map_sort_constraints(bmap);
9510 if (!bmap)
9511 return isl_map_free(map);
9512 isl_basic_map_free(map->p[i]);
9513 map->p[i] = bmap;
9514 }
9515
9516 map = sort_and_remove_duplicates(map);
9517 return map;
9518}
9519
9520/* We normalize in place, but if anything goes wrong we need
9521 * to return NULL, so we need to make sure we don't change the
9522 * meaning of any possible other copies of map.
9523 */
9524__isl_give isl_map *isl_map_normalize(__isl_take isl_map *map)
9525{
9526 int i;
9527 struct isl_basic_map *bmap;
9528
9529 if (!map)
9530 return NULL;
9531 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
9532 return map;
9533 for (i = 0; i < map->n; ++i) {
9534 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
9535 if (!bmap)
9536 goto error;
9537 isl_basic_map_free(map->p[i]);
9538 map->p[i] = bmap;
9539 }
9540
9541 map = sort_and_remove_duplicates(map);
9542 if (map)
9543 ISL_F_SET(map, ISL_MAP_NORMALIZED);
9544 return map;
9545error:
9546 isl_map_free(map);
9547 return NULL;
9548}
9549
9550struct isl_set *isl_set_normalize(struct isl_set *set)
9551{
Tobias Grosser06e15922016-11-16 11:06:47 +00009552 return set_from_map(isl_map_normalize(set_to_map(set)));
Tobias Grosser52a25232015-02-04 20:55:43 +00009553}
9554
Tobias Grosserb2f39922015-05-28 13:32:11 +00009555isl_bool isl_map_plain_is_equal(__isl_keep isl_map *map1,
9556 __isl_keep isl_map *map2)
Tobias Grosser52a25232015-02-04 20:55:43 +00009557{
9558 int i;
Tobias Grosserb2f39922015-05-28 13:32:11 +00009559 isl_bool equal;
Tobias Grosser52a25232015-02-04 20:55:43 +00009560
9561 if (!map1 || !map2)
Tobias Grosserb2f39922015-05-28 13:32:11 +00009562 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00009563
9564 if (map1 == map2)
Tobias Grosserb2f39922015-05-28 13:32:11 +00009565 return isl_bool_true;
Tobias Grosser52a25232015-02-04 20:55:43 +00009566 if (!isl_space_is_equal(map1->dim, map2->dim))
Tobias Grosserb2f39922015-05-28 13:32:11 +00009567 return isl_bool_false;
Tobias Grosser52a25232015-02-04 20:55:43 +00009568
9569 map1 = isl_map_copy(map1);
9570 map2 = isl_map_copy(map2);
9571 map1 = isl_map_normalize(map1);
9572 map2 = isl_map_normalize(map2);
9573 if (!map1 || !map2)
9574 goto error;
9575 equal = map1->n == map2->n;
9576 for (i = 0; equal && i < map1->n; ++i) {
9577 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
9578 if (equal < 0)
9579 goto error;
9580 }
9581 isl_map_free(map1);
9582 isl_map_free(map2);
9583 return equal;
9584error:
9585 isl_map_free(map1);
9586 isl_map_free(map2);
Tobias Grosserb2f39922015-05-28 13:32:11 +00009587 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00009588}
9589
Tobias Grosserb2f39922015-05-28 13:32:11 +00009590isl_bool isl_set_plain_is_equal(__isl_keep isl_set *set1,
9591 __isl_keep isl_set *set2)
Tobias Grosser52a25232015-02-04 20:55:43 +00009592{
Tobias Grosser06e15922016-11-16 11:06:47 +00009593 return isl_map_plain_is_equal(set_to_map(set1), set_to_map(set2));
Tobias Grosser52a25232015-02-04 20:55:43 +00009594}
9595
Tobias Grosser1fa7b972015-02-16 19:33:40 +00009596/* Return the basic maps in "map" as a list.
Tobias Grosser52a25232015-02-04 20:55:43 +00009597 */
Tobias Grosser1fa7b972015-02-16 19:33:40 +00009598__isl_give isl_basic_map_list *isl_map_get_basic_map_list(
9599 __isl_keep isl_map *map)
Tobias Grosser52a25232015-02-04 20:55:43 +00009600{
9601 int i;
9602 isl_ctx *ctx;
Tobias Grosser1fa7b972015-02-16 19:33:40 +00009603 isl_basic_map_list *list;
Tobias Grosser52a25232015-02-04 20:55:43 +00009604
Tobias Grosser1fa7b972015-02-16 19:33:40 +00009605 if (!map)
Tobias Grosser52a25232015-02-04 20:55:43 +00009606 return NULL;
Tobias Grosser1fa7b972015-02-16 19:33:40 +00009607 ctx = isl_map_get_ctx(map);
9608 list = isl_basic_map_list_alloc(ctx, map->n);
Tobias Grosser52a25232015-02-04 20:55:43 +00009609
Tobias Grosser1fa7b972015-02-16 19:33:40 +00009610 for (i = 0; i < map->n; ++i) {
9611 isl_basic_map *bmap;
Tobias Grosser52a25232015-02-04 20:55:43 +00009612
Tobias Grosser1fa7b972015-02-16 19:33:40 +00009613 bmap = isl_basic_map_copy(map->p[i]);
9614 list = isl_basic_map_list_add(list, bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +00009615 }
9616
9617 return list;
9618}
9619
9620/* Return the intersection of the elements in the non-empty list "list".
9621 * All elements are assumed to live in the same space.
9622 */
Tobias Grosser1fa7b972015-02-16 19:33:40 +00009623__isl_give isl_basic_map *isl_basic_map_list_intersect(
9624 __isl_take isl_basic_map_list *list)
Tobias Grosser52a25232015-02-04 20:55:43 +00009625{
9626 int i, n;
Tobias Grosser1fa7b972015-02-16 19:33:40 +00009627 isl_basic_map *bmap;
Tobias Grosser52a25232015-02-04 20:55:43 +00009628
9629 if (!list)
9630 return NULL;
Tobias Grosser1fa7b972015-02-16 19:33:40 +00009631 n = isl_basic_map_list_n_basic_map(list);
Tobias Grosser52a25232015-02-04 20:55:43 +00009632 if (n < 1)
Tobias Grosser1fa7b972015-02-16 19:33:40 +00009633 isl_die(isl_basic_map_list_get_ctx(list), isl_error_invalid,
Tobias Grosser52a25232015-02-04 20:55:43 +00009634 "expecting non-empty list", goto error);
Tobias Grosser52a25232015-02-04 20:55:43 +00009635
Tobias Grosser1fa7b972015-02-16 19:33:40 +00009636 bmap = isl_basic_map_list_get_basic_map(list, 0);
9637 for (i = 1; i < n; ++i) {
9638 isl_basic_map *bmap_i;
9639
9640 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9641 bmap = isl_basic_map_intersect(bmap, bmap_i);
Tobias Grosser52a25232015-02-04 20:55:43 +00009642 }
9643
Tobias Grosser1fa7b972015-02-16 19:33:40 +00009644 isl_basic_map_list_free(list);
9645 return bmap;
Tobias Grosser52a25232015-02-04 20:55:43 +00009646error:
Tobias Grosser1fa7b972015-02-16 19:33:40 +00009647 isl_basic_map_list_free(list);
Tobias Grosser52a25232015-02-04 20:55:43 +00009648 return NULL;
9649}
9650
Tobias Grosser1fa7b972015-02-16 19:33:40 +00009651/* Return the intersection of the elements in the non-empty list "list".
9652 * All elements are assumed to live in the same space.
9653 */
9654__isl_give isl_basic_set *isl_basic_set_list_intersect(
9655 __isl_take isl_basic_set_list *list)
9656{
9657 return isl_basic_map_list_intersect(list);
9658}
9659
Tobias Grossereab0943e2016-11-22 21:31:59 +00009660/* Return the union of the elements of "list".
9661 * The list is required to have at least one element.
9662 */
9663__isl_give isl_set *isl_basic_set_list_union(
9664 __isl_take isl_basic_set_list *list)
9665{
9666 int i, n;
9667 isl_space *space;
9668 isl_basic_set *bset;
9669 isl_set *set;
9670
9671 if (!list)
9672 return NULL;
9673 n = isl_basic_set_list_n_basic_set(list);
9674 if (n < 1)
9675 isl_die(isl_basic_set_list_get_ctx(list), isl_error_invalid,
9676 "expecting non-empty list", goto error);
9677
9678 bset = isl_basic_set_list_get_basic_set(list, 0);
9679 space = isl_basic_set_get_space(bset);
9680 isl_basic_set_free(bset);
9681
9682 set = isl_set_alloc_space(space, n, 0);
9683 for (i = 0; i < n; ++i) {
9684 bset = isl_basic_set_list_get_basic_set(list, i);
9685 set = isl_set_add_basic_set(set, bset);
9686 }
9687
9688 isl_basic_set_list_free(list);
9689 return set;
9690error:
9691 isl_basic_set_list_free(list);
9692 return NULL;
9693}
9694
Tobias Grossera67ac972016-02-26 11:35:12 +00009695/* Return the union of the elements in the non-empty list "list".
9696 * All elements are assumed to live in the same space.
9697 */
9698__isl_give isl_set *isl_set_list_union(__isl_take isl_set_list *list)
9699{
9700 int i, n;
9701 isl_set *set;
9702
9703 if (!list)
9704 return NULL;
9705 n = isl_set_list_n_set(list);
9706 if (n < 1)
9707 isl_die(isl_set_list_get_ctx(list), isl_error_invalid,
9708 "expecting non-empty list", goto error);
9709
9710 set = isl_set_list_get_set(list, 0);
9711 for (i = 1; i < n; ++i) {
9712 isl_set *set_i;
9713
9714 set_i = isl_set_list_get_set(list, i);
9715 set = isl_set_union(set, set_i);
9716 }
9717
9718 isl_set_list_free(list);
9719 return set;
9720error:
9721 isl_set_list_free(list);
9722 return NULL;
9723}
9724
Tobias Grosser52a25232015-02-04 20:55:43 +00009725struct isl_basic_map *isl_basic_map_product(
9726 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
9727{
9728 isl_space *dim_result = NULL;
9729 struct isl_basic_map *bmap;
9730 unsigned in1, in2, out1, out2, nparam, total, pos;
9731 struct isl_dim_map *dim_map1, *dim_map2;
9732
Tobias Grossere5671e52017-03-10 09:17:55 +00009733 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
Tobias Grosser52a25232015-02-04 20:55:43 +00009734 goto error;
Tobias Grosser52a25232015-02-04 20:55:43 +00009735 dim_result = isl_space_product(isl_space_copy(bmap1->dim),
9736 isl_space_copy(bmap2->dim));
9737
Tobias Grosserf4fe34b2017-03-16 21:33:20 +00009738 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9739 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9740 out1 = isl_basic_map_dim(bmap1, isl_dim_out);
9741 out2 = isl_basic_map_dim(bmap2, isl_dim_out);
9742 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
Tobias Grosser52a25232015-02-04 20:55:43 +00009743
9744 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
9745 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9746 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9747 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9748 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9749 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9750 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9751 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9752 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9753 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9754 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9755
9756 bmap = isl_basic_map_alloc_space(dim_result,
9757 bmap1->n_div + bmap2->n_div,
9758 bmap1->n_eq + bmap2->n_eq,
9759 bmap1->n_ineq + bmap2->n_ineq);
9760 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9761 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9762 bmap = isl_basic_map_simplify(bmap);
9763 return isl_basic_map_finalize(bmap);
9764error:
9765 isl_basic_map_free(bmap1);
9766 isl_basic_map_free(bmap2);
9767 return NULL;
9768}
9769
9770__isl_give isl_basic_map *isl_basic_map_flat_product(
9771 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9772{
9773 isl_basic_map *prod;
9774
9775 prod = isl_basic_map_product(bmap1, bmap2);
9776 prod = isl_basic_map_flatten(prod);
9777 return prod;
9778}
9779
9780__isl_give isl_basic_set *isl_basic_set_flat_product(
9781 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
9782{
9783 return isl_basic_map_flat_range_product(bset1, bset2);
9784}
9785
9786__isl_give isl_basic_map *isl_basic_map_domain_product(
9787 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9788{
9789 isl_space *space_result = NULL;
9790 isl_basic_map *bmap;
9791 unsigned in1, in2, out, nparam, total, pos;
9792 struct isl_dim_map *dim_map1, *dim_map2;
9793
9794 if (!bmap1 || !bmap2)
9795 goto error;
9796
9797 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
9798 isl_space_copy(bmap2->dim));
9799
9800 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9801 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9802 out = isl_basic_map_dim(bmap1, isl_dim_out);
9803 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9804
9805 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
9806 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9807 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9808 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9809 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9810 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9811 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9812 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9813 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
9814 isl_dim_map_div(dim_map1, bmap1, pos += out);
9815 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9816
9817 bmap = isl_basic_map_alloc_space(space_result,
9818 bmap1->n_div + bmap2->n_div,
9819 bmap1->n_eq + bmap2->n_eq,
9820 bmap1->n_ineq + bmap2->n_ineq);
9821 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9822 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9823 bmap = isl_basic_map_simplify(bmap);
9824 return isl_basic_map_finalize(bmap);
9825error:
9826 isl_basic_map_free(bmap1);
9827 isl_basic_map_free(bmap2);
9828 return NULL;
9829}
9830
9831__isl_give isl_basic_map *isl_basic_map_range_product(
9832 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9833{
Tobias Grosser72745c22017-02-17 05:11:16 +00009834 isl_bool rational;
Tobias Grosser52a25232015-02-04 20:55:43 +00009835 isl_space *dim_result = NULL;
9836 isl_basic_map *bmap;
9837 unsigned in, out1, out2, nparam, total, pos;
9838 struct isl_dim_map *dim_map1, *dim_map2;
9839
Tobias Grosser5652c9c2016-10-01 19:46:51 +00009840 rational = isl_basic_map_is_rational(bmap1);
9841 if (rational >= 0 && rational)
9842 rational = isl_basic_map_is_rational(bmap2);
9843 if (!bmap1 || !bmap2 || rational < 0)
Tobias Grosser52a25232015-02-04 20:55:43 +00009844 goto error;
9845
Tobias Grossere5671e52017-03-10 09:17:55 +00009846 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
9847 goto error;
Tobias Grosser52a25232015-02-04 20:55:43 +00009848
9849 dim_result = isl_space_range_product(isl_space_copy(bmap1->dim),
9850 isl_space_copy(bmap2->dim));
9851
9852 in = isl_basic_map_dim(bmap1, isl_dim_in);
Tobias Grosserf4fe34b2017-03-16 21:33:20 +00009853 out1 = isl_basic_map_dim(bmap1, isl_dim_out);
9854 out2 = isl_basic_map_dim(bmap2, isl_dim_out);
9855 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
Tobias Grosser52a25232015-02-04 20:55:43 +00009856
9857 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
9858 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9859 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9860 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9861 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9862 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9863 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
9864 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
9865 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9866 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9867 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9868
9869 bmap = isl_basic_map_alloc_space(dim_result,
9870 bmap1->n_div + bmap2->n_div,
9871 bmap1->n_eq + bmap2->n_eq,
9872 bmap1->n_ineq + bmap2->n_ineq);
9873 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9874 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
Tobias Grosser5652c9c2016-10-01 19:46:51 +00009875 if (rational)
9876 bmap = isl_basic_map_set_rational(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +00009877 bmap = isl_basic_map_simplify(bmap);
9878 return isl_basic_map_finalize(bmap);
9879error:
9880 isl_basic_map_free(bmap1);
9881 isl_basic_map_free(bmap2);
9882 return NULL;
9883}
9884
9885__isl_give isl_basic_map *isl_basic_map_flat_range_product(
9886 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9887{
9888 isl_basic_map *prod;
9889
9890 prod = isl_basic_map_range_product(bmap1, bmap2);
9891 prod = isl_basic_map_flatten_range(prod);
9892 return prod;
9893}
9894
9895/* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
9896 * and collect the results.
9897 * The result live in the space obtained by calling "space_product"
9898 * on the spaces of "map1" and "map2".
9899 * If "remove_duplicates" is set then the result may contain duplicates
9900 * (even if the inputs do not) and so we try and remove the obvious
9901 * duplicates.
9902 */
9903static __isl_give isl_map *map_product(__isl_take isl_map *map1,
9904 __isl_take isl_map *map2,
9905 __isl_give isl_space *(*space_product)(__isl_take isl_space *left,
9906 __isl_take isl_space *right),
9907 __isl_give isl_basic_map *(*basic_map_product)(
9908 __isl_take isl_basic_map *left,
9909 __isl_take isl_basic_map *right),
9910 int remove_duplicates)
9911{
9912 unsigned flags = 0;
9913 struct isl_map *result;
9914 int i, j;
Tobias Grossere5671e52017-03-10 09:17:55 +00009915 isl_bool m;
Tobias Grosser52a25232015-02-04 20:55:43 +00009916
Tobias Grossere5671e52017-03-10 09:17:55 +00009917 m = isl_map_has_equal_params(map1, map2);
9918 if (m < 0)
Tobias Grosser52a25232015-02-04 20:55:43 +00009919 goto error;
Tobias Grossere5671e52017-03-10 09:17:55 +00009920 if (!m)
9921 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
9922 "parameters don't match", goto error);
Tobias Grosser52a25232015-02-04 20:55:43 +00009923
9924 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
9925 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
9926 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
9927
9928 result = isl_map_alloc_space(space_product(isl_space_copy(map1->dim),
9929 isl_space_copy(map2->dim)),
9930 map1->n * map2->n, flags);
9931 if (!result)
9932 goto error;
9933 for (i = 0; i < map1->n; ++i)
9934 for (j = 0; j < map2->n; ++j) {
9935 struct isl_basic_map *part;
9936 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
9937 isl_basic_map_copy(map2->p[j]));
9938 if (isl_basic_map_is_empty(part))
9939 isl_basic_map_free(part);
9940 else
9941 result = isl_map_add_basic_map(result, part);
9942 if (!result)
9943 goto error;
9944 }
9945 if (remove_duplicates)
9946 result = isl_map_remove_obvious_duplicates(result);
9947 isl_map_free(map1);
9948 isl_map_free(map2);
9949 return result;
9950error:
9951 isl_map_free(map1);
9952 isl_map_free(map2);
9953 return NULL;
9954}
9955
9956/* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
9957 */
9958static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
9959 __isl_take isl_map *map2)
9960{
9961 return map_product(map1, map2, &isl_space_product,
9962 &isl_basic_map_product, 0);
9963}
9964
9965__isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
9966 __isl_take isl_map *map2)
9967{
9968 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
9969}
9970
9971/* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
9972 */
9973__isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
9974 __isl_take isl_map *map2)
9975{
9976 isl_map *prod;
9977
9978 prod = isl_map_product(map1, map2);
9979 prod = isl_map_flatten(prod);
9980 return prod;
9981}
9982
9983/* Given two set A and B, construct its Cartesian product A x B.
9984 */
9985struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
9986{
9987 return isl_map_range_product(set1, set2);
9988}
9989
9990__isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
9991 __isl_take isl_set *set2)
9992{
9993 return isl_map_flat_range_product(set1, set2);
9994}
9995
9996/* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
9997 */
9998static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
9999 __isl_take isl_map *map2)
10000{
10001 return map_product(map1, map2, &isl_space_domain_product,
10002 &isl_basic_map_domain_product, 1);
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_range_product_aligned(__isl_take isl_map *map1,
10008 __isl_take isl_map *map2)
10009{
10010 return map_product(map1, map2, &isl_space_range_product,
10011 &isl_basic_map_range_product, 1);
10012}
10013
10014__isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
10015 __isl_take isl_map *map2)
10016{
10017 return isl_map_align_params_map_map_and(map1, map2,
10018 &map_domain_product_aligned);
10019}
10020
10021__isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
10022 __isl_take isl_map *map2)
10023{
10024 return isl_map_align_params_map_map_and(map1, map2,
10025 &map_range_product_aligned);
10026}
10027
10028/* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
10029 */
10030__isl_give isl_map *isl_map_factor_domain(__isl_take isl_map *map)
10031{
10032 isl_space *space;
10033 int total1, keep1, total2, keep2;
10034
10035 if (!map)
10036 return NULL;
10037 if (!isl_space_domain_is_wrapping(map->dim) ||
10038 !isl_space_range_is_wrapping(map->dim))
10039 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10040 "not a product", return isl_map_free(map));
10041
10042 space = isl_map_get_space(map);
10043 total1 = isl_space_dim(space, isl_dim_in);
10044 total2 = isl_space_dim(space, isl_dim_out);
10045 space = isl_space_factor_domain(space);
10046 keep1 = isl_space_dim(space, isl_dim_in);
10047 keep2 = isl_space_dim(space, isl_dim_out);
10048 map = isl_map_project_out(map, isl_dim_in, keep1, total1 - keep1);
10049 map = isl_map_project_out(map, isl_dim_out, keep2, total2 - keep2);
10050 map = isl_map_reset_space(map, space);
10051
10052 return map;
10053}
10054
10055/* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
10056 */
10057__isl_give isl_map *isl_map_factor_range(__isl_take isl_map *map)
10058{
10059 isl_space *space;
10060 int total1, keep1, total2, keep2;
10061
10062 if (!map)
10063 return NULL;
10064 if (!isl_space_domain_is_wrapping(map->dim) ||
10065 !isl_space_range_is_wrapping(map->dim))
10066 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10067 "not a product", return isl_map_free(map));
10068
10069 space = isl_map_get_space(map);
10070 total1 = isl_space_dim(space, isl_dim_in);
10071 total2 = isl_space_dim(space, isl_dim_out);
10072 space = isl_space_factor_range(space);
10073 keep1 = isl_space_dim(space, isl_dim_in);
10074 keep2 = isl_space_dim(space, isl_dim_out);
10075 map = isl_map_project_out(map, isl_dim_in, 0, total1 - keep1);
10076 map = isl_map_project_out(map, isl_dim_out, 0, total2 - keep2);
10077 map = isl_map_reset_space(map, space);
10078
10079 return map;
10080}
10081
10082/* Given a map of the form [A -> B] -> C, return the map A -> C.
10083 */
10084__isl_give isl_map *isl_map_domain_factor_domain(__isl_take isl_map *map)
10085{
10086 isl_space *space;
10087 int total, keep;
10088
10089 if (!map)
10090 return NULL;
10091 if (!isl_space_domain_is_wrapping(map->dim))
10092 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10093 "domain is not a product", return isl_map_free(map));
10094
10095 space = isl_map_get_space(map);
10096 total = isl_space_dim(space, isl_dim_in);
10097 space = isl_space_domain_factor_domain(space);
10098 keep = isl_space_dim(space, isl_dim_in);
10099 map = isl_map_project_out(map, isl_dim_in, keep, total - keep);
10100 map = isl_map_reset_space(map, space);
10101
10102 return map;
10103}
10104
10105/* Given a map of the form [A -> B] -> C, return the map B -> C.
10106 */
10107__isl_give isl_map *isl_map_domain_factor_range(__isl_take isl_map *map)
10108{
10109 isl_space *space;
10110 int total, keep;
10111
10112 if (!map)
10113 return NULL;
10114 if (!isl_space_domain_is_wrapping(map->dim))
10115 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10116 "domain is not a product", return isl_map_free(map));
10117
10118 space = isl_map_get_space(map);
10119 total = isl_space_dim(space, isl_dim_in);
10120 space = isl_space_domain_factor_range(space);
10121 keep = isl_space_dim(space, isl_dim_in);
10122 map = isl_map_project_out(map, isl_dim_in, 0, total - keep);
10123 map = isl_map_reset_space(map, space);
10124
10125 return map;
10126}
10127
10128/* Given a map A -> [B -> C], extract the map A -> B.
10129 */
10130__isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
10131{
10132 isl_space *space;
10133 int total, keep;
10134
10135 if (!map)
10136 return NULL;
10137 if (!isl_space_range_is_wrapping(map->dim))
10138 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10139 "range is not a product", return isl_map_free(map));
10140
10141 space = isl_map_get_space(map);
10142 total = isl_space_dim(space, isl_dim_out);
10143 space = isl_space_range_factor_domain(space);
10144 keep = isl_space_dim(space, isl_dim_out);
10145 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
10146 map = isl_map_reset_space(map, space);
10147
10148 return map;
10149}
10150
10151/* Given a map A -> [B -> C], extract the map A -> C.
10152 */
10153__isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
10154{
10155 isl_space *space;
10156 int total, keep;
10157
10158 if (!map)
10159 return NULL;
10160 if (!isl_space_range_is_wrapping(map->dim))
10161 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10162 "range is not a product", return isl_map_free(map));
10163
10164 space = isl_map_get_space(map);
10165 total = isl_space_dim(space, isl_dim_out);
10166 space = isl_space_range_factor_range(space);
10167 keep = isl_space_dim(space, isl_dim_out);
10168 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
10169 map = isl_map_reset_space(map, space);
10170
10171 return map;
10172}
10173
10174/* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
10175 */
10176__isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
10177 __isl_take isl_map *map2)
10178{
10179 isl_map *prod;
10180
10181 prod = isl_map_domain_product(map1, map2);
10182 prod = isl_map_flatten_domain(prod);
10183 return prod;
10184}
10185
10186/* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
10187 */
10188__isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
10189 __isl_take isl_map *map2)
10190{
10191 isl_map *prod;
10192
10193 prod = isl_map_range_product(map1, map2);
10194 prod = isl_map_flatten_range(prod);
10195 return prod;
10196}
10197
10198uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
10199{
10200 int i;
10201 uint32_t hash = isl_hash_init();
10202 unsigned total;
10203
10204 if (!bmap)
10205 return 0;
10206 bmap = isl_basic_map_copy(bmap);
10207 bmap = isl_basic_map_normalize(bmap);
10208 if (!bmap)
10209 return 0;
10210 total = isl_basic_map_total_dim(bmap);
10211 isl_hash_byte(hash, bmap->n_eq & 0xFF);
10212 for (i = 0; i < bmap->n_eq; ++i) {
10213 uint32_t c_hash;
10214 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
10215 isl_hash_hash(hash, c_hash);
10216 }
10217 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
10218 for (i = 0; i < bmap->n_ineq; ++i) {
10219 uint32_t c_hash;
10220 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
10221 isl_hash_hash(hash, c_hash);
10222 }
10223 isl_hash_byte(hash, bmap->n_div & 0xFF);
10224 for (i = 0; i < bmap->n_div; ++i) {
10225 uint32_t c_hash;
10226 if (isl_int_is_zero(bmap->div[i][0]))
10227 continue;
10228 isl_hash_byte(hash, i & 0xFF);
10229 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
10230 isl_hash_hash(hash, c_hash);
10231 }
10232 isl_basic_map_free(bmap);
10233 return hash;
10234}
10235
10236uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
10237{
Tobias Grosser06e15922016-11-16 11:06:47 +000010238 return isl_basic_map_get_hash(bset_to_bmap(bset));
Tobias Grosser52a25232015-02-04 20:55:43 +000010239}
10240
10241uint32_t isl_map_get_hash(__isl_keep isl_map *map)
10242{
10243 int i;
10244 uint32_t hash;
10245
10246 if (!map)
10247 return 0;
10248 map = isl_map_copy(map);
10249 map = isl_map_normalize(map);
10250 if (!map)
10251 return 0;
10252
10253 hash = isl_hash_init();
10254 for (i = 0; i < map->n; ++i) {
10255 uint32_t bmap_hash;
10256 bmap_hash = isl_basic_map_get_hash(map->p[i]);
10257 isl_hash_hash(hash, bmap_hash);
10258 }
10259
10260 isl_map_free(map);
10261
10262 return hash;
10263}
10264
10265uint32_t isl_set_get_hash(__isl_keep isl_set *set)
10266{
Tobias Grosser06e15922016-11-16 11:06:47 +000010267 return isl_map_get_hash(set_to_map(set));
Tobias Grosser52a25232015-02-04 20:55:43 +000010268}
10269
Tobias Grosser52a25232015-02-04 20:55:43 +000010270/* Return the number of basic maps in the (current) representation of "map".
10271 */
10272int isl_map_n_basic_map(__isl_keep isl_map *map)
10273{
10274 return map ? map->n : 0;
10275}
10276
10277int isl_set_n_basic_set(__isl_keep isl_set *set)
10278{
10279 return set ? set->n : 0;
10280}
10281
Tobias Grosserb2f39922015-05-28 13:32:11 +000010282isl_stat isl_map_foreach_basic_map(__isl_keep isl_map *map,
10283 isl_stat (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
Tobias Grosser52a25232015-02-04 20:55:43 +000010284{
10285 int i;
10286
10287 if (!map)
Tobias Grosserb2f39922015-05-28 13:32:11 +000010288 return isl_stat_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000010289
10290 for (i = 0; i < map->n; ++i)
10291 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
Tobias Grosserb2f39922015-05-28 13:32:11 +000010292 return isl_stat_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000010293
Tobias Grosserb2f39922015-05-28 13:32:11 +000010294 return isl_stat_ok;
Tobias Grosser52a25232015-02-04 20:55:43 +000010295}
10296
Tobias Grosserb2f39922015-05-28 13:32:11 +000010297isl_stat isl_set_foreach_basic_set(__isl_keep isl_set *set,
10298 isl_stat (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
Tobias Grosser52a25232015-02-04 20:55:43 +000010299{
10300 int i;
10301
10302 if (!set)
Tobias Grosserb2f39922015-05-28 13:32:11 +000010303 return isl_stat_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000010304
10305 for (i = 0; i < set->n; ++i)
10306 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
Tobias Grosserb2f39922015-05-28 13:32:11 +000010307 return isl_stat_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000010308
Tobias Grosserb2f39922015-05-28 13:32:11 +000010309 return isl_stat_ok;
Tobias Grosser52a25232015-02-04 20:55:43 +000010310}
10311
Tobias Grosser4db55312015-06-30 08:22:14 +000010312/* Return a list of basic sets, the union of which is equal to "set".
10313 */
10314__isl_give isl_basic_set_list *isl_set_get_basic_set_list(
10315 __isl_keep isl_set *set)
10316{
10317 int i;
10318 isl_basic_set_list *list;
10319
10320 if (!set)
10321 return NULL;
10322
10323 list = isl_basic_set_list_alloc(isl_set_get_ctx(set), set->n);
10324 for (i = 0; i < set->n; ++i) {
10325 isl_basic_set *bset;
10326
10327 bset = isl_basic_set_copy(set->p[i]);
10328 list = isl_basic_set_list_add(list, bset);
10329 }
10330
10331 return list;
10332}
10333
Tobias Grosser52a25232015-02-04 20:55:43 +000010334__isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
10335{
10336 isl_space *dim;
10337
10338 if (!bset)
10339 return NULL;
10340
10341 bset = isl_basic_set_cow(bset);
10342 if (!bset)
10343 return NULL;
10344
10345 dim = isl_basic_set_get_space(bset);
10346 dim = isl_space_lift(dim, bset->n_div);
10347 if (!dim)
10348 goto error;
10349 isl_space_free(bset->dim);
10350 bset->dim = dim;
10351 bset->extra -= bset->n_div;
10352 bset->n_div = 0;
10353
10354 bset = isl_basic_set_finalize(bset);
10355
10356 return bset;
10357error:
10358 isl_basic_set_free(bset);
10359 return NULL;
10360}
10361
10362__isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
10363{
10364 int i;
10365 isl_space *dim;
10366 unsigned n_div;
10367
Tobias Grosser29eaa162017-03-21 09:12:11 +000010368 set = set_from_map(isl_map_align_divs_internal(set_to_map(set)));
Tobias Grosser52a25232015-02-04 20:55:43 +000010369
10370 if (!set)
10371 return NULL;
10372
10373 set = isl_set_cow(set);
10374 if (!set)
10375 return NULL;
10376
10377 n_div = set->p[0]->n_div;
10378 dim = isl_set_get_space(set);
10379 dim = isl_space_lift(dim, n_div);
10380 if (!dim)
10381 goto error;
10382 isl_space_free(set->dim);
10383 set->dim = dim;
10384
10385 for (i = 0; i < set->n; ++i) {
10386 set->p[i] = isl_basic_set_lift(set->p[i]);
10387 if (!set->p[i])
10388 goto error;
10389 }
10390
10391 return set;
10392error:
10393 isl_set_free(set);
10394 return NULL;
10395}
10396
Tobias Grosser52a25232015-02-04 20:55:43 +000010397int isl_basic_set_size(__isl_keep isl_basic_set *bset)
10398{
10399 unsigned dim;
10400 int size = 0;
10401
10402 if (!bset)
10403 return -1;
10404
10405 dim = isl_basic_set_total_dim(bset);
10406 size += bset->n_eq * (1 + dim);
10407 size += bset->n_ineq * (1 + dim);
10408 size += bset->n_div * (2 + dim);
10409
10410 return size;
10411}
10412
10413int isl_set_size(__isl_keep isl_set *set)
10414{
10415 int i;
10416 int size = 0;
10417
10418 if (!set)
10419 return -1;
10420
10421 for (i = 0; i < set->n; ++i)
10422 size += isl_basic_set_size(set->p[i]);
10423
10424 return size;
10425}
10426
10427/* Check if there is any lower bound (if lower == 0) and/or upper
10428 * bound (if upper == 0) on the specified dim.
10429 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000010430static isl_bool basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
Tobias Grosser52a25232015-02-04 20:55:43 +000010431 enum isl_dim_type type, unsigned pos, int lower, int upper)
10432{
10433 int i;
10434
Tobias Grosser94e53712016-12-31 07:46:11 +000010435 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
Tobias Grosserb2f39922015-05-28 13:32:11 +000010436 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000010437
Tobias Grosser52a25232015-02-04 20:55:43 +000010438 pos += isl_basic_map_offset(bmap, type);
10439
10440 for (i = 0; i < bmap->n_div; ++i) {
10441 if (isl_int_is_zero(bmap->div[i][0]))
10442 continue;
10443 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
Tobias Grosserb2f39922015-05-28 13:32:11 +000010444 return isl_bool_true;
Tobias Grosser52a25232015-02-04 20:55:43 +000010445 }
10446
10447 for (i = 0; i < bmap->n_eq; ++i)
10448 if (!isl_int_is_zero(bmap->eq[i][pos]))
Tobias Grosserb2f39922015-05-28 13:32:11 +000010449 return isl_bool_true;
Tobias Grosser52a25232015-02-04 20:55:43 +000010450
10451 for (i = 0; i < bmap->n_ineq; ++i) {
10452 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
10453 if (sgn > 0)
10454 lower = 1;
10455 if (sgn < 0)
10456 upper = 1;
10457 }
10458
10459 return lower && upper;
10460}
10461
Tobias Grosser72745c22017-02-17 05:11:16 +000010462isl_bool isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
Tobias Grosser52a25232015-02-04 20:55:43 +000010463 enum isl_dim_type type, unsigned pos)
10464{
10465 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
10466}
10467
Tobias Grosserb2f39922015-05-28 13:32:11 +000010468isl_bool isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
Tobias Grosser52a25232015-02-04 20:55:43 +000010469 enum isl_dim_type type, unsigned pos)
10470{
10471 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
10472}
10473
Tobias Grosserb2f39922015-05-28 13:32:11 +000010474isl_bool isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
Tobias Grosser52a25232015-02-04 20:55:43 +000010475 enum isl_dim_type type, unsigned pos)
10476{
10477 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
10478}
10479
Tobias Grosser72745c22017-02-17 05:11:16 +000010480isl_bool isl_map_dim_is_bounded(__isl_keep isl_map *map,
Tobias Grosser52a25232015-02-04 20:55:43 +000010481 enum isl_dim_type type, unsigned pos)
10482{
10483 int i;
10484
10485 if (!map)
Tobias Grosser72745c22017-02-17 05:11:16 +000010486 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000010487
10488 for (i = 0; i < map->n; ++i) {
Tobias Grosser72745c22017-02-17 05:11:16 +000010489 isl_bool bounded;
Tobias Grosser52a25232015-02-04 20:55:43 +000010490 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
10491 if (bounded < 0 || !bounded)
10492 return bounded;
10493 }
10494
Tobias Grosser72745c22017-02-17 05:11:16 +000010495 return isl_bool_true;
Tobias Grosser52a25232015-02-04 20:55:43 +000010496}
10497
Tobias Grosser72745c22017-02-17 05:11:16 +000010498/* Return true if the specified dim is involved in both an upper bound
Tobias Grosser52a25232015-02-04 20:55:43 +000010499 * and a lower bound.
10500 */
Tobias Grosser72745c22017-02-17 05:11:16 +000010501isl_bool isl_set_dim_is_bounded(__isl_keep isl_set *set,
Tobias Grosser52a25232015-02-04 20:55:43 +000010502 enum isl_dim_type type, unsigned pos)
10503{
Tobias Grosser06e15922016-11-16 11:06:47 +000010504 return isl_map_dim_is_bounded(set_to_map(set), type, pos);
Tobias Grosser52a25232015-02-04 20:55:43 +000010505}
10506
10507/* Does "map" have a bound (according to "fn") for any of its basic maps?
10508 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000010509static isl_bool has_any_bound(__isl_keep isl_map *map,
Tobias Grosser52a25232015-02-04 20:55:43 +000010510 enum isl_dim_type type, unsigned pos,
Tobias Grosserb2f39922015-05-28 13:32:11 +000010511 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
Tobias Grosser52a25232015-02-04 20:55:43 +000010512 enum isl_dim_type type, unsigned pos))
10513{
10514 int i;
10515
10516 if (!map)
Tobias Grosserb2f39922015-05-28 13:32:11 +000010517 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000010518
10519 for (i = 0; i < map->n; ++i) {
Tobias Grosserb2f39922015-05-28 13:32:11 +000010520 isl_bool bounded;
Tobias Grosser52a25232015-02-04 20:55:43 +000010521 bounded = fn(map->p[i], type, pos);
10522 if (bounded < 0 || bounded)
10523 return bounded;
10524 }
10525
Tobias Grosserb2f39922015-05-28 13:32:11 +000010526 return isl_bool_false;
Tobias Grosser52a25232015-02-04 20:55:43 +000010527}
10528
10529/* Return 1 if the specified dim is involved in any lower bound.
10530 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000010531isl_bool isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
Tobias Grosser52a25232015-02-04 20:55:43 +000010532 enum isl_dim_type type, unsigned pos)
10533{
10534 return has_any_bound(set, type, pos,
10535 &isl_basic_map_dim_has_lower_bound);
10536}
10537
10538/* Return 1 if the specified dim is involved in any upper bound.
10539 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000010540isl_bool isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
Tobias Grosser52a25232015-02-04 20:55:43 +000010541 enum isl_dim_type type, unsigned pos)
10542{
10543 return has_any_bound(set, type, pos,
10544 &isl_basic_map_dim_has_upper_bound);
10545}
10546
10547/* Does "map" have a bound (according to "fn") for all of its basic maps?
10548 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000010549static isl_bool has_bound(__isl_keep isl_map *map,
Tobias Grosser52a25232015-02-04 20:55:43 +000010550 enum isl_dim_type type, unsigned pos,
Tobias Grosserb2f39922015-05-28 13:32:11 +000010551 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
Tobias Grosser52a25232015-02-04 20:55:43 +000010552 enum isl_dim_type type, unsigned pos))
10553{
10554 int i;
10555
10556 if (!map)
Tobias Grosserb2f39922015-05-28 13:32:11 +000010557 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000010558
10559 for (i = 0; i < map->n; ++i) {
Tobias Grosserb2f39922015-05-28 13:32:11 +000010560 isl_bool bounded;
Tobias Grosser52a25232015-02-04 20:55:43 +000010561 bounded = fn(map->p[i], type, pos);
10562 if (bounded < 0 || !bounded)
10563 return bounded;
10564 }
10565
Tobias Grosserb2f39922015-05-28 13:32:11 +000010566 return isl_bool_true;
Tobias Grosser52a25232015-02-04 20:55:43 +000010567}
10568
10569/* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10570 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000010571isl_bool isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
Tobias Grosser52a25232015-02-04 20:55:43 +000010572 enum isl_dim_type type, unsigned pos)
10573{
10574 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
10575}
10576
10577/* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10578 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000010579isl_bool isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
Tobias Grosser52a25232015-02-04 20:55:43 +000010580 enum isl_dim_type type, unsigned pos)
10581{
10582 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
10583}
10584
10585/* For each of the "n" variables starting at "first", determine
10586 * the sign of the variable and put the results in the first "n"
10587 * elements of the array "signs".
10588 * Sign
10589 * 1 means that the variable is non-negative
10590 * -1 means that the variable is non-positive
10591 * 0 means the variable attains both positive and negative values.
10592 */
Tobias Grosser72745c22017-02-17 05:11:16 +000010593isl_stat isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
Tobias Grosser52a25232015-02-04 20:55:43 +000010594 unsigned first, unsigned n, int *signs)
10595{
10596 isl_vec *bound = NULL;
10597 struct isl_tab *tab = NULL;
10598 struct isl_tab_undo *snap;
10599 int i;
10600
10601 if (!bset || !signs)
Tobias Grosser72745c22017-02-17 05:11:16 +000010602 return isl_stat_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000010603
10604 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
10605 tab = isl_tab_from_basic_set(bset, 0);
10606 if (!bound || !tab)
10607 goto error;
10608
10609 isl_seq_clr(bound->el, bound->size);
10610 isl_int_set_si(bound->el[0], -1);
10611
10612 snap = isl_tab_snap(tab);
10613 for (i = 0; i < n; ++i) {
10614 int empty;
10615
10616 isl_int_set_si(bound->el[1 + first + i], -1);
10617 if (isl_tab_add_ineq(tab, bound->el) < 0)
10618 goto error;
10619 empty = tab->empty;
10620 isl_int_set_si(bound->el[1 + first + i], 0);
10621 if (isl_tab_rollback(tab, snap) < 0)
10622 goto error;
10623
10624 if (empty) {
10625 signs[i] = 1;
10626 continue;
10627 }
10628
10629 isl_int_set_si(bound->el[1 + first + i], 1);
10630 if (isl_tab_add_ineq(tab, bound->el) < 0)
10631 goto error;
10632 empty = tab->empty;
10633 isl_int_set_si(bound->el[1 + first + i], 0);
10634 if (isl_tab_rollback(tab, snap) < 0)
10635 goto error;
10636
10637 signs[i] = empty ? -1 : 0;
10638 }
10639
10640 isl_tab_free(tab);
10641 isl_vec_free(bound);
Tobias Grosser72745c22017-02-17 05:11:16 +000010642 return isl_stat_ok;
Tobias Grosser52a25232015-02-04 20:55:43 +000010643error:
10644 isl_tab_free(tab);
10645 isl_vec_free(bound);
Tobias Grosser72745c22017-02-17 05:11:16 +000010646 return isl_stat_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000010647}
10648
Tobias Grosser72745c22017-02-17 05:11:16 +000010649isl_stat isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
Tobias Grosser52a25232015-02-04 20:55:43 +000010650 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
10651{
10652 if (!bset || !signs)
Tobias Grosser72745c22017-02-17 05:11:16 +000010653 return isl_stat_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000010654 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
Tobias Grosser72745c22017-02-17 05:11:16 +000010655 return isl_stat_error);
Tobias Grosser52a25232015-02-04 20:55:43 +000010656
10657 first += pos(bset->dim, type) - 1;
10658 return isl_basic_set_vars_get_sign(bset, first, n, signs);
10659}
10660
10661/* Is it possible for the integer division "div" to depend (possibly
10662 * indirectly) on any output dimensions?
10663 *
10664 * If the div is undefined, then we conservatively assume that it
10665 * may depend on them.
10666 * Otherwise, we check if it actually depends on them or on any integer
10667 * divisions that may depend on them.
10668 */
Tobias Grosser72745c22017-02-17 05:11:16 +000010669static isl_bool div_may_involve_output(__isl_keep isl_basic_map *bmap, int div)
Tobias Grosser52a25232015-02-04 20:55:43 +000010670{
10671 int i;
10672 unsigned n_out, o_out;
10673 unsigned n_div, o_div;
10674
10675 if (isl_int_is_zero(bmap->div[div][0]))
Tobias Grosser72745c22017-02-17 05:11:16 +000010676 return isl_bool_true;
Tobias Grosser52a25232015-02-04 20:55:43 +000010677
10678 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10679 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10680
10681 if (isl_seq_first_non_zero(bmap->div[div] + 1 + o_out, n_out) != -1)
Tobias Grosser72745c22017-02-17 05:11:16 +000010682 return isl_bool_true;
Tobias Grosser52a25232015-02-04 20:55:43 +000010683
10684 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10685 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10686
10687 for (i = 0; i < n_div; ++i) {
Tobias Grosser72745c22017-02-17 05:11:16 +000010688 isl_bool may_involve;
10689
Tobias Grosser52a25232015-02-04 20:55:43 +000010690 if (isl_int_is_zero(bmap->div[div][1 + o_div + i]))
10691 continue;
Tobias Grosser72745c22017-02-17 05:11:16 +000010692 may_involve = div_may_involve_output(bmap, i);
10693 if (may_involve < 0 || may_involve)
10694 return may_involve;
Tobias Grosser52a25232015-02-04 20:55:43 +000010695 }
10696
Tobias Grosser72745c22017-02-17 05:11:16 +000010697 return isl_bool_false;
Tobias Grosser52a25232015-02-04 20:55:43 +000010698}
10699
Michael Kruse959a8dc2016-01-15 15:54:45 +000010700/* Return the first integer division of "bmap" in the range
10701 * [first, first + n[ that may depend on any output dimensions and
10702 * that has a non-zero coefficient in "c" (where the first coefficient
10703 * in "c" corresponds to integer division "first").
10704 */
10705static int first_div_may_involve_output(__isl_keep isl_basic_map *bmap,
10706 isl_int *c, int first, int n)
10707{
10708 int k;
10709
10710 if (!bmap)
10711 return -1;
10712
10713 for (k = first; k < first + n; ++k) {
Tobias Grosser72745c22017-02-17 05:11:16 +000010714 isl_bool may_involve;
10715
Michael Kruse959a8dc2016-01-15 15:54:45 +000010716 if (isl_int_is_zero(c[k]))
10717 continue;
Tobias Grosser72745c22017-02-17 05:11:16 +000010718 may_involve = div_may_involve_output(bmap, k);
10719 if (may_involve < 0)
10720 return -1;
10721 if (may_involve)
Michael Kruse959a8dc2016-01-15 15:54:45 +000010722 return k;
10723 }
10724
10725 return first + n;
10726}
10727
10728/* Look for a pair of inequality constraints in "bmap" of the form
10729 *
10730 * -l + i >= 0 or i >= l
10731 * and
10732 * n + l - i >= 0 or i <= l + n
10733 *
10734 * with n < "m" and i the output dimension at position "pos".
10735 * (Note that n >= 0 as otherwise the two constraints would conflict.)
10736 * Furthermore, "l" is only allowed to involve parameters, input dimensions
10737 * and earlier output dimensions, as well as integer divisions that do
10738 * not involve any of the output dimensions.
10739 *
10740 * Return the index of the first inequality constraint or bmap->n_ineq
10741 * if no such pair can be found.
10742 */
10743static int find_modulo_constraint_pair(__isl_keep isl_basic_map *bmap,
10744 int pos, isl_int m)
10745{
10746 int i, j;
10747 isl_ctx *ctx;
10748 unsigned total;
10749 unsigned n_div, o_div;
10750 unsigned n_out, o_out;
10751 int less;
10752
10753 if (!bmap)
10754 return -1;
10755
10756 ctx = isl_basic_map_get_ctx(bmap);
10757 total = isl_basic_map_total_dim(bmap);
10758 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10759 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10760 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10761 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10762 for (i = 0; i < bmap->n_ineq; ++i) {
10763 if (!isl_int_abs_eq(bmap->ineq[i][o_out + pos], ctx->one))
10764 continue;
10765 if (isl_seq_first_non_zero(bmap->ineq[i] + o_out + pos + 1,
10766 n_out - (pos + 1)) != -1)
10767 continue;
10768 if (first_div_may_involve_output(bmap, bmap->ineq[i] + o_div,
10769 0, n_div) < n_div)
10770 continue;
10771 for (j = i + 1; j < bmap->n_ineq; ++j) {
10772 if (!isl_int_abs_eq(bmap->ineq[j][o_out + pos],
10773 ctx->one))
10774 continue;
10775 if (!isl_seq_is_neg(bmap->ineq[i] + 1,
10776 bmap->ineq[j] + 1, total))
10777 continue;
10778 break;
10779 }
10780 if (j >= bmap->n_ineq)
10781 continue;
10782 isl_int_add(bmap->ineq[i][0],
10783 bmap->ineq[i][0], bmap->ineq[j][0]);
10784 less = isl_int_abs_lt(bmap->ineq[i][0], m);
10785 isl_int_sub(bmap->ineq[i][0],
10786 bmap->ineq[i][0], bmap->ineq[j][0]);
10787 if (!less)
10788 continue;
10789 if (isl_int_is_one(bmap->ineq[i][o_out + pos]))
10790 return i;
10791 else
10792 return j;
10793 }
10794
10795 return bmap->n_ineq;
10796}
10797
Tobias Grosser52a25232015-02-04 20:55:43 +000010798/* Return the index of the equality of "bmap" that defines
10799 * the output dimension "pos" in terms of earlier dimensions.
10800 * The equality may also involve integer divisions, as long
10801 * as those integer divisions are defined in terms of
10802 * parameters or input dimensions.
Michael Kruse959a8dc2016-01-15 15:54:45 +000010803 * In this case, *div is set to the number of integer divisions and
10804 * *ineq is set to the number of inequality constraints (provided
10805 * div and ineq are not NULL).
10806 *
10807 * The equality may also involve a single integer division involving
10808 * the output dimensions (typically only output dimension "pos") as
10809 * long as the coefficient of output dimension "pos" is 1 or -1 and
10810 * there is a pair of constraints i >= l and i <= l + n, with i referring
10811 * to output dimension "pos", l an expression involving only earlier
10812 * dimensions and n smaller than the coefficient of the integer division
10813 * in the equality. In this case, the output dimension can be defined
10814 * in terms of a modulo expression that does not involve the integer division.
10815 * *div is then set to this single integer division and
10816 * *ineq is set to the index of constraint i >= l.
10817 *
Tobias Grosser52a25232015-02-04 20:55:43 +000010818 * Return bmap->n_eq if there is no such equality.
10819 * Return -1 on error.
10820 */
10821int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map *bmap,
Michael Kruse959a8dc2016-01-15 15:54:45 +000010822 int pos, int *div, int *ineq)
Tobias Grosser52a25232015-02-04 20:55:43 +000010823{
Michael Kruse959a8dc2016-01-15 15:54:45 +000010824 int j, k, l;
Tobias Grosser52a25232015-02-04 20:55:43 +000010825 unsigned n_out, o_out;
10826 unsigned n_div, o_div;
10827
10828 if (!bmap)
10829 return -1;
10830
10831 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10832 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10833 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10834 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10835
Michael Kruse959a8dc2016-01-15 15:54:45 +000010836 if (ineq)
10837 *ineq = bmap->n_ineq;
10838 if (div)
10839 *div = n_div;
Tobias Grosser52a25232015-02-04 20:55:43 +000010840 for (j = 0; j < bmap->n_eq; ++j) {
10841 if (isl_int_is_zero(bmap->eq[j][o_out + pos]))
10842 continue;
10843 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + pos + 1,
10844 n_out - (pos + 1)) != -1)
10845 continue;
Michael Kruse959a8dc2016-01-15 15:54:45 +000010846 k = first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
10847 0, n_div);
Tobias Grosser52a25232015-02-04 20:55:43 +000010848 if (k >= n_div)
10849 return j;
Michael Kruse959a8dc2016-01-15 15:54:45 +000010850 if (!isl_int_is_one(bmap->eq[j][o_out + pos]) &&
10851 !isl_int_is_negone(bmap->eq[j][o_out + pos]))
10852 continue;
10853 if (first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
10854 k + 1, n_div - (k+1)) < n_div)
10855 continue;
10856 l = find_modulo_constraint_pair(bmap, pos,
10857 bmap->eq[j][o_div + k]);
10858 if (l < 0)
10859 return -1;
10860 if (l >= bmap->n_ineq)
10861 continue;
10862 if (div)
10863 *div = k;
10864 if (ineq)
10865 *ineq = l;
10866 return j;
Tobias Grosser52a25232015-02-04 20:55:43 +000010867 }
10868
10869 return bmap->n_eq;
10870}
10871
10872/* Check if the given basic map is obviously single-valued.
10873 * In particular, for each output dimension, check that there is
10874 * an equality that defines the output dimension in terms of
10875 * earlier dimensions.
10876 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000010877isl_bool isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
Tobias Grosser52a25232015-02-04 20:55:43 +000010878{
10879 int i;
10880 unsigned n_out;
10881
10882 if (!bmap)
Tobias Grosserb2f39922015-05-28 13:32:11 +000010883 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000010884
10885 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10886
10887 for (i = 0; i < n_out; ++i) {
10888 int eq;
10889
Michael Kruse959a8dc2016-01-15 15:54:45 +000010890 eq = isl_basic_map_output_defining_equality(bmap, i,
10891 NULL, NULL);
Tobias Grosser52a25232015-02-04 20:55:43 +000010892 if (eq < 0)
Tobias Grosserb2f39922015-05-28 13:32:11 +000010893 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000010894 if (eq >= bmap->n_eq)
Tobias Grosserb2f39922015-05-28 13:32:11 +000010895 return isl_bool_false;
Tobias Grosser52a25232015-02-04 20:55:43 +000010896 }
10897
Tobias Grosserb2f39922015-05-28 13:32:11 +000010898 return isl_bool_true;
Tobias Grosser52a25232015-02-04 20:55:43 +000010899}
10900
10901/* Check if the given basic map is single-valued.
10902 * We simply compute
10903 *
10904 * M \circ M^-1
10905 *
10906 * and check if the result is a subset of the identity mapping.
10907 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000010908isl_bool isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
Tobias Grosser52a25232015-02-04 20:55:43 +000010909{
10910 isl_space *space;
10911 isl_basic_map *test;
10912 isl_basic_map *id;
Tobias Grosserb2f39922015-05-28 13:32:11 +000010913 isl_bool sv;
Tobias Grosser52a25232015-02-04 20:55:43 +000010914
10915 sv = isl_basic_map_plain_is_single_valued(bmap);
10916 if (sv < 0 || sv)
10917 return sv;
10918
10919 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
10920 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
10921
10922 space = isl_basic_map_get_space(bmap);
10923 space = isl_space_map_from_set(isl_space_range(space));
10924 id = isl_basic_map_identity(space);
10925
10926 sv = isl_basic_map_is_subset(test, id);
10927
10928 isl_basic_map_free(test);
10929 isl_basic_map_free(id);
10930
10931 return sv;
10932}
10933
10934/* Check if the given map is obviously single-valued.
10935 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000010936isl_bool isl_map_plain_is_single_valued(__isl_keep isl_map *map)
Tobias Grosser52a25232015-02-04 20:55:43 +000010937{
10938 if (!map)
Tobias Grosserb2f39922015-05-28 13:32:11 +000010939 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000010940 if (map->n == 0)
Tobias Grosserb2f39922015-05-28 13:32:11 +000010941 return isl_bool_true;
Tobias Grosser52a25232015-02-04 20:55:43 +000010942 if (map->n >= 2)
Tobias Grosserb2f39922015-05-28 13:32:11 +000010943 return isl_bool_false;
Tobias Grosser52a25232015-02-04 20:55:43 +000010944
10945 return isl_basic_map_plain_is_single_valued(map->p[0]);
10946}
10947
10948/* Check if the given map is single-valued.
10949 * We simply compute
10950 *
10951 * M \circ M^-1
10952 *
10953 * and check if the result is a subset of the identity mapping.
10954 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000010955isl_bool isl_map_is_single_valued(__isl_keep isl_map *map)
Tobias Grosser52a25232015-02-04 20:55:43 +000010956{
10957 isl_space *dim;
10958 isl_map *test;
10959 isl_map *id;
Tobias Grosserb2f39922015-05-28 13:32:11 +000010960 isl_bool sv;
Tobias Grosser52a25232015-02-04 20:55:43 +000010961
10962 sv = isl_map_plain_is_single_valued(map);
10963 if (sv < 0 || sv)
10964 return sv;
10965
10966 test = isl_map_reverse(isl_map_copy(map));
10967 test = isl_map_apply_range(test, isl_map_copy(map));
10968
10969 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
10970 id = isl_map_identity(dim);
10971
10972 sv = isl_map_is_subset(test, id);
10973
10974 isl_map_free(test);
10975 isl_map_free(id);
10976
10977 return sv;
10978}
10979
Tobias Grosserb2f39922015-05-28 13:32:11 +000010980isl_bool isl_map_is_injective(__isl_keep isl_map *map)
Tobias Grosser52a25232015-02-04 20:55:43 +000010981{
Tobias Grosserb2f39922015-05-28 13:32:11 +000010982 isl_bool in;
Tobias Grosser52a25232015-02-04 20:55:43 +000010983
10984 map = isl_map_copy(map);
10985 map = isl_map_reverse(map);
10986 in = isl_map_is_single_valued(map);
10987 isl_map_free(map);
10988
10989 return in;
10990}
10991
10992/* Check if the given map is obviously injective.
10993 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000010994isl_bool isl_map_plain_is_injective(__isl_keep isl_map *map)
Tobias Grosser52a25232015-02-04 20:55:43 +000010995{
Tobias Grosserb2f39922015-05-28 13:32:11 +000010996 isl_bool in;
Tobias Grosser52a25232015-02-04 20:55:43 +000010997
10998 map = isl_map_copy(map);
10999 map = isl_map_reverse(map);
11000 in = isl_map_plain_is_single_valued(map);
11001 isl_map_free(map);
11002
11003 return in;
11004}
11005
Tobias Grosserb2f39922015-05-28 13:32:11 +000011006isl_bool isl_map_is_bijective(__isl_keep isl_map *map)
Tobias Grosser52a25232015-02-04 20:55:43 +000011007{
Tobias Grosserb2f39922015-05-28 13:32:11 +000011008 isl_bool sv;
Tobias Grosser52a25232015-02-04 20:55:43 +000011009
11010 sv = isl_map_is_single_valued(map);
11011 if (sv < 0 || !sv)
11012 return sv;
11013
11014 return isl_map_is_injective(map);
11015}
11016
Tobias Grosserb2f39922015-05-28 13:32:11 +000011017isl_bool isl_set_is_singleton(__isl_keep isl_set *set)
Tobias Grosser52a25232015-02-04 20:55:43 +000011018{
Tobias Grosser06e15922016-11-16 11:06:47 +000011019 return isl_map_is_single_valued(set_to_map(set));
Tobias Grosser52a25232015-02-04 20:55:43 +000011020}
11021
Michael Krusee0b34f32016-05-04 14:41:36 +000011022/* Does "map" only map elements to themselves?
11023 *
11024 * If the domain and range spaces are different, then "map"
11025 * is considered not to be an identity relation, even if it is empty.
11026 * Otherwise, construct the maximal identity relation and
11027 * check whether "map" is a subset of this relation.
11028 */
11029isl_bool isl_map_is_identity(__isl_keep isl_map *map)
11030{
11031 isl_space *space;
11032 isl_map *id;
11033 isl_bool equal, is_identity;
11034
11035 space = isl_map_get_space(map);
11036 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
11037 isl_space_free(space);
11038 if (equal < 0 || !equal)
11039 return equal;
11040
11041 id = isl_map_identity(isl_map_get_space(map));
11042 is_identity = isl_map_is_subset(map, id);
11043 isl_map_free(id);
11044
11045 return is_identity;
11046}
11047
Tobias Grosser52a25232015-02-04 20:55:43 +000011048int isl_map_is_translation(__isl_keep isl_map *map)
11049{
11050 int ok;
11051 isl_set *delta;
11052
11053 delta = isl_map_deltas(isl_map_copy(map));
11054 ok = isl_set_is_singleton(delta);
11055 isl_set_free(delta);
11056
11057 return ok;
11058}
11059
11060static int unique(isl_int *p, unsigned pos, unsigned len)
11061{
11062 if (isl_seq_first_non_zero(p, pos) != -1)
11063 return 0;
11064 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
11065 return 0;
11066 return 1;
11067}
11068
Tobias Grosser72745c22017-02-17 05:11:16 +000011069isl_bool isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
Tobias Grosser52a25232015-02-04 20:55:43 +000011070{
11071 int i, j;
11072 unsigned nvar;
11073 unsigned ovar;
11074
11075 if (!bset)
Tobias Grosser72745c22017-02-17 05:11:16 +000011076 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000011077
11078 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
Tobias Grosser72745c22017-02-17 05:11:16 +000011079 return isl_bool_false;
Tobias Grosser52a25232015-02-04 20:55:43 +000011080
11081 nvar = isl_basic_set_dim(bset, isl_dim_set);
11082 ovar = isl_space_offset(bset->dim, isl_dim_set);
11083 for (j = 0; j < nvar; ++j) {
11084 int lower = 0, upper = 0;
11085 for (i = 0; i < bset->n_eq; ++i) {
11086 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
11087 continue;
11088 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
Tobias Grosser72745c22017-02-17 05:11:16 +000011089 return isl_bool_false;
Tobias Grosser52a25232015-02-04 20:55:43 +000011090 break;
11091 }
11092 if (i < bset->n_eq)
11093 continue;
11094 for (i = 0; i < bset->n_ineq; ++i) {
11095 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
11096 continue;
11097 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
Tobias Grosser72745c22017-02-17 05:11:16 +000011098 return isl_bool_false;
Tobias Grosser52a25232015-02-04 20:55:43 +000011099 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
11100 lower = 1;
11101 else
11102 upper = 1;
11103 }
11104 if (!lower || !upper)
Tobias Grosser72745c22017-02-17 05:11:16 +000011105 return isl_bool_false;
Tobias Grosser52a25232015-02-04 20:55:43 +000011106 }
11107
Tobias Grosser72745c22017-02-17 05:11:16 +000011108 return isl_bool_true;
Tobias Grosser52a25232015-02-04 20:55:43 +000011109}
11110
Tobias Grosser72745c22017-02-17 05:11:16 +000011111isl_bool isl_set_is_box(__isl_keep isl_set *set)
Tobias Grosser52a25232015-02-04 20:55:43 +000011112{
11113 if (!set)
Tobias Grosser72745c22017-02-17 05:11:16 +000011114 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000011115 if (set->n != 1)
Tobias Grosser72745c22017-02-17 05:11:16 +000011116 return isl_bool_false;
Tobias Grosser52a25232015-02-04 20:55:43 +000011117
11118 return isl_basic_set_is_box(set->p[0]);
11119}
11120
Tobias Grosserb2f39922015-05-28 13:32:11 +000011121isl_bool isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
Tobias Grosser52a25232015-02-04 20:55:43 +000011122{
11123 if (!bset)
Tobias Grosserb2f39922015-05-28 13:32:11 +000011124 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000011125
11126 return isl_space_is_wrapping(bset->dim);
11127}
11128
Tobias Grosserb2f39922015-05-28 13:32:11 +000011129isl_bool isl_set_is_wrapping(__isl_keep isl_set *set)
Tobias Grosser52a25232015-02-04 20:55:43 +000011130{
11131 if (!set)
Tobias Grosserb2f39922015-05-28 13:32:11 +000011132 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000011133
11134 return isl_space_is_wrapping(set->dim);
11135}
11136
Michael Kruse959a8dc2016-01-15 15:54:45 +000011137/* Modify the space of "map" through a call to "change".
11138 * If "can_change" is set (not NULL), then first call it to check
11139 * if the modification is allowed, printing the error message "cannot_change"
11140 * if it is not.
11141 */
11142static __isl_give isl_map *isl_map_change_space(__isl_take isl_map *map,
11143 isl_bool (*can_change)(__isl_keep isl_map *map),
11144 const char *cannot_change,
11145 __isl_give isl_space *(*change)(__isl_take isl_space *space))
11146{
11147 isl_bool ok;
11148 isl_space *space;
11149
11150 if (!map)
11151 return NULL;
11152
11153 ok = can_change ? can_change(map) : isl_bool_true;
11154 if (ok < 0)
11155 return isl_map_free(map);
11156 if (!ok)
11157 isl_die(isl_map_get_ctx(map), isl_error_invalid, cannot_change,
11158 return isl_map_free(map));
11159
11160 space = change(isl_map_get_space(map));
11161 map = isl_map_reset_space(map, space);
11162
11163 return map;
11164}
11165
Tobias Grosser52a25232015-02-04 20:55:43 +000011166/* Is the domain of "map" a wrapped relation?
11167 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000011168isl_bool isl_map_domain_is_wrapping(__isl_keep isl_map *map)
Tobias Grosser52a25232015-02-04 20:55:43 +000011169{
11170 if (!map)
Tobias Grosserb2f39922015-05-28 13:32:11 +000011171 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000011172
11173 return isl_space_domain_is_wrapping(map->dim);
11174}
11175
11176/* Is the range of "map" a wrapped relation?
11177 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000011178isl_bool isl_map_range_is_wrapping(__isl_keep isl_map *map)
Tobias Grosser52a25232015-02-04 20:55:43 +000011179{
11180 if (!map)
Tobias Grosserb2f39922015-05-28 13:32:11 +000011181 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000011182
11183 return isl_space_range_is_wrapping(map->dim);
11184}
11185
11186__isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
11187{
11188 bmap = isl_basic_map_cow(bmap);
11189 if (!bmap)
11190 return NULL;
11191
11192 bmap->dim = isl_space_wrap(bmap->dim);
11193 if (!bmap->dim)
11194 goto error;
11195
11196 bmap = isl_basic_map_finalize(bmap);
11197
Tobias Grosser06e15922016-11-16 11:06:47 +000011198 return bset_from_bmap(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +000011199error:
11200 isl_basic_map_free(bmap);
11201 return NULL;
11202}
11203
Michael Kruse959a8dc2016-01-15 15:54:45 +000011204/* Given a map A -> B, return the set (A -> B).
11205 */
Tobias Grosser52a25232015-02-04 20:55:43 +000011206__isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
11207{
Michael Kruse959a8dc2016-01-15 15:54:45 +000011208 return isl_map_change_space(map, NULL, NULL, &isl_space_wrap);
Tobias Grosser52a25232015-02-04 20:55:43 +000011209}
11210
11211__isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
11212{
11213 bset = isl_basic_set_cow(bset);
11214 if (!bset)
11215 return NULL;
11216
11217 bset->dim = isl_space_unwrap(bset->dim);
11218 if (!bset->dim)
11219 goto error;
11220
11221 bset = isl_basic_set_finalize(bset);
11222
Tobias Grosser06e15922016-11-16 11:06:47 +000011223 return bset_to_bmap(bset);
Tobias Grosser52a25232015-02-04 20:55:43 +000011224error:
11225 isl_basic_set_free(bset);
11226 return NULL;
11227}
11228
Michael Kruse959a8dc2016-01-15 15:54:45 +000011229/* Given a set (A -> B), return the map A -> B.
11230 * Error out if "set" is not of the form (A -> B).
11231 */
Tobias Grosser52a25232015-02-04 20:55:43 +000011232__isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
11233{
Michael Kruse959a8dc2016-01-15 15:54:45 +000011234 return isl_map_change_space(set, &isl_set_is_wrapping,
11235 "not a wrapping set", &isl_space_unwrap);
Tobias Grosser52a25232015-02-04 20:55:43 +000011236}
11237
11238__isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
11239 enum isl_dim_type type)
11240{
11241 if (!bmap)
11242 return NULL;
11243
11244 if (!isl_space_is_named_or_nested(bmap->dim, type))
11245 return bmap;
11246
11247 bmap = isl_basic_map_cow(bmap);
11248 if (!bmap)
11249 return NULL;
11250
11251 bmap->dim = isl_space_reset(bmap->dim, type);
11252 if (!bmap->dim)
11253 goto error;
11254
11255 bmap = isl_basic_map_finalize(bmap);
11256
11257 return bmap;
11258error:
11259 isl_basic_map_free(bmap);
11260 return NULL;
11261}
11262
11263__isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
11264 enum isl_dim_type type)
11265{
11266 int i;
11267
11268 if (!map)
11269 return NULL;
11270
11271 if (!isl_space_is_named_or_nested(map->dim, type))
11272 return map;
11273
11274 map = isl_map_cow(map);
11275 if (!map)
11276 return NULL;
11277
11278 for (i = 0; i < map->n; ++i) {
11279 map->p[i] = isl_basic_map_reset(map->p[i], type);
11280 if (!map->p[i])
11281 goto error;
11282 }
11283 map->dim = isl_space_reset(map->dim, type);
11284 if (!map->dim)
11285 goto error;
11286
11287 return map;
11288error:
11289 isl_map_free(map);
11290 return NULL;
11291}
11292
11293__isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
11294{
11295 if (!bmap)
11296 return NULL;
11297
11298 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
11299 return bmap;
11300
11301 bmap = isl_basic_map_cow(bmap);
11302 if (!bmap)
11303 return NULL;
11304
11305 bmap->dim = isl_space_flatten(bmap->dim);
11306 if (!bmap->dim)
11307 goto error;
11308
11309 bmap = isl_basic_map_finalize(bmap);
11310
11311 return bmap;
11312error:
11313 isl_basic_map_free(bmap);
11314 return NULL;
11315}
11316
11317__isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
11318{
Tobias Grosser06e15922016-11-16 11:06:47 +000011319 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset)));
Tobias Grosser52a25232015-02-04 20:55:43 +000011320}
11321
11322__isl_give isl_basic_map *isl_basic_map_flatten_domain(
11323 __isl_take isl_basic_map *bmap)
11324{
11325 if (!bmap)
11326 return NULL;
11327
11328 if (!bmap->dim->nested[0])
11329 return bmap;
11330
11331 bmap = isl_basic_map_cow(bmap);
11332 if (!bmap)
11333 return NULL;
11334
11335 bmap->dim = isl_space_flatten_domain(bmap->dim);
11336 if (!bmap->dim)
11337 goto error;
11338
11339 bmap = isl_basic_map_finalize(bmap);
11340
11341 return bmap;
11342error:
11343 isl_basic_map_free(bmap);
11344 return NULL;
11345}
11346
11347__isl_give isl_basic_map *isl_basic_map_flatten_range(
11348 __isl_take isl_basic_map *bmap)
11349{
11350 if (!bmap)
11351 return NULL;
11352
11353 if (!bmap->dim->nested[1])
11354 return bmap;
11355
11356 bmap = isl_basic_map_cow(bmap);
11357 if (!bmap)
11358 return NULL;
11359
11360 bmap->dim = isl_space_flatten_range(bmap->dim);
11361 if (!bmap->dim)
11362 goto error;
11363
11364 bmap = isl_basic_map_finalize(bmap);
11365
11366 return bmap;
11367error:
11368 isl_basic_map_free(bmap);
11369 return NULL;
11370}
11371
Michael Kruse959a8dc2016-01-15 15:54:45 +000011372/* Remove any internal structure from the spaces of domain and range of "map".
11373 */
Tobias Grosser52a25232015-02-04 20:55:43 +000011374__isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
11375{
Tobias Grosser52a25232015-02-04 20:55:43 +000011376 if (!map)
11377 return NULL;
11378
11379 if (!map->dim->nested[0] && !map->dim->nested[1])
11380 return map;
11381
Michael Kruse959a8dc2016-01-15 15:54:45 +000011382 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten);
Tobias Grosser52a25232015-02-04 20:55:43 +000011383}
11384
11385__isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
11386{
Tobias Grosser06e15922016-11-16 11:06:47 +000011387 return set_from_map(isl_map_flatten(set_to_map(set)));
Tobias Grosser52a25232015-02-04 20:55:43 +000011388}
11389
11390__isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
11391{
11392 isl_space *dim, *flat_dim;
11393 isl_map *map;
11394
11395 dim = isl_set_get_space(set);
11396 flat_dim = isl_space_flatten(isl_space_copy(dim));
11397 map = isl_map_identity(isl_space_join(isl_space_reverse(dim), flat_dim));
11398 map = isl_map_intersect_domain(map, set);
11399
11400 return map;
11401}
11402
Michael Kruse959a8dc2016-01-15 15:54:45 +000011403/* Remove any internal structure from the space of the domain of "map".
11404 */
Tobias Grosser52a25232015-02-04 20:55:43 +000011405__isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
11406{
Tobias Grosser52a25232015-02-04 20:55:43 +000011407 if (!map)
11408 return NULL;
11409
11410 if (!map->dim->nested[0])
11411 return map;
11412
Michael Kruse959a8dc2016-01-15 15:54:45 +000011413 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_domain);
Tobias Grosser52a25232015-02-04 20:55:43 +000011414}
11415
Michael Kruse959a8dc2016-01-15 15:54:45 +000011416/* Remove any internal structure from the space of the range of "map".
11417 */
Tobias Grosser52a25232015-02-04 20:55:43 +000011418__isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
11419{
Tobias Grosser52a25232015-02-04 20:55:43 +000011420 if (!map)
11421 return NULL;
11422
11423 if (!map->dim->nested[1])
11424 return map;
11425
Michael Kruse959a8dc2016-01-15 15:54:45 +000011426 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_range);
Tobias Grosser52a25232015-02-04 20:55:43 +000011427}
11428
11429/* Reorder the dimensions of "bmap" according to the given dim_map
Tobias Grossera7f1e042016-02-04 06:19:12 +000011430 * and set the dimension specification to "dim" and
11431 * perform Gaussian elimination on the result.
Tobias Grosser52a25232015-02-04 20:55:43 +000011432 */
11433__isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
11434 __isl_take isl_space *dim, __isl_take struct isl_dim_map *dim_map)
11435{
11436 isl_basic_map *res;
11437 unsigned flags;
11438
11439 bmap = isl_basic_map_cow(bmap);
11440 if (!bmap || !dim || !dim_map)
11441 goto error;
11442
11443 flags = bmap->flags;
11444 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
11445 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED);
11446 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
11447 res = isl_basic_map_alloc_space(dim,
11448 bmap->n_div, bmap->n_eq, bmap->n_ineq);
11449 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
11450 if (res)
11451 res->flags = flags;
Tobias Grossera7f1e042016-02-04 06:19:12 +000011452 res = isl_basic_map_gauss(res, NULL);
Tobias Grosser52a25232015-02-04 20:55:43 +000011453 res = isl_basic_map_finalize(res);
11454 return res;
11455error:
11456 free(dim_map);
11457 isl_basic_map_free(bmap);
11458 isl_space_free(dim);
11459 return NULL;
11460}
11461
11462/* Reorder the dimensions of "map" according to given reordering.
11463 */
11464__isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
11465 __isl_take isl_reordering *r)
11466{
11467 int i;
11468 struct isl_dim_map *dim_map;
11469
11470 map = isl_map_cow(map);
11471 dim_map = isl_dim_map_from_reordering(r);
11472 if (!map || !r || !dim_map)
11473 goto error;
11474
11475 for (i = 0; i < map->n; ++i) {
11476 struct isl_dim_map *dim_map_i;
11477
11478 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
11479
11480 map->p[i] = isl_basic_map_realign(map->p[i],
11481 isl_space_copy(r->dim), dim_map_i);
11482
11483 if (!map->p[i])
11484 goto error;
11485 }
11486
11487 map = isl_map_reset_space(map, isl_space_copy(r->dim));
11488
11489 isl_reordering_free(r);
11490 free(dim_map);
11491 return map;
11492error:
11493 free(dim_map);
11494 isl_map_free(map);
11495 isl_reordering_free(r);
11496 return NULL;
11497}
11498
11499__isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
11500 __isl_take isl_reordering *r)
11501{
Tobias Grosser06e15922016-11-16 11:06:47 +000011502 return set_from_map(isl_map_realign(set_to_map(set), r));
Tobias Grosser52a25232015-02-04 20:55:43 +000011503}
11504
11505__isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
11506 __isl_take isl_space *model)
11507{
11508 isl_ctx *ctx;
Tobias Grossere5671e52017-03-10 09:17:55 +000011509 isl_bool aligned;
Tobias Grosser52a25232015-02-04 20:55:43 +000011510
11511 if (!map || !model)
11512 goto error;
11513
11514 ctx = isl_space_get_ctx(model);
11515 if (!isl_space_has_named_params(model))
11516 isl_die(ctx, isl_error_invalid,
11517 "model has unnamed parameters", goto error);
Tobias Grossere5671e52017-03-10 09:17:55 +000011518 if (isl_map_check_named_params(map) < 0)
11519 goto error;
11520 aligned = isl_map_space_has_equal_params(map, model);
11521 if (aligned < 0)
11522 goto error;
11523 if (!aligned) {
Tobias Grosser52a25232015-02-04 20:55:43 +000011524 isl_reordering *exp;
11525
11526 model = isl_space_drop_dims(model, isl_dim_in,
11527 0, isl_space_dim(model, isl_dim_in));
11528 model = isl_space_drop_dims(model, isl_dim_out,
11529 0, isl_space_dim(model, isl_dim_out));
11530 exp = isl_parameter_alignment_reordering(map->dim, model);
11531 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
11532 map = isl_map_realign(map, exp);
11533 }
11534
11535 isl_space_free(model);
11536 return map;
11537error:
11538 isl_space_free(model);
11539 isl_map_free(map);
11540 return NULL;
11541}
11542
11543__isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
11544 __isl_take isl_space *model)
11545{
11546 return isl_map_align_params(set, model);
11547}
11548
11549/* Align the parameters of "bmap" to those of "model", introducing
11550 * additional parameters if needed.
11551 */
11552__isl_give isl_basic_map *isl_basic_map_align_params(
11553 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
11554{
11555 isl_ctx *ctx;
Tobias Grosser593ebdf2017-03-14 07:33:26 +000011556 isl_bool equal_params;
Tobias Grosser52a25232015-02-04 20:55:43 +000011557
11558 if (!bmap || !model)
11559 goto error;
11560
11561 ctx = isl_space_get_ctx(model);
11562 if (!isl_space_has_named_params(model))
11563 isl_die(ctx, isl_error_invalid,
11564 "model has unnamed parameters", goto error);
11565 if (!isl_space_has_named_params(bmap->dim))
11566 isl_die(ctx, isl_error_invalid,
11567 "relation has unnamed parameters", goto error);
Tobias Grosser593ebdf2017-03-14 07:33:26 +000011568 equal_params = isl_space_has_equal_params(bmap->dim, model);
11569 if (equal_params < 0)
11570 goto error;
11571 if (!equal_params) {
Tobias Grosser52a25232015-02-04 20:55:43 +000011572 isl_reordering *exp;
11573 struct isl_dim_map *dim_map;
11574
11575 model = isl_space_drop_dims(model, isl_dim_in,
11576 0, isl_space_dim(model, isl_dim_in));
11577 model = isl_space_drop_dims(model, isl_dim_out,
11578 0, isl_space_dim(model, isl_dim_out));
11579 exp = isl_parameter_alignment_reordering(bmap->dim, model);
11580 exp = isl_reordering_extend_space(exp,
11581 isl_basic_map_get_space(bmap));
11582 dim_map = isl_dim_map_from_reordering(exp);
11583 bmap = isl_basic_map_realign(bmap,
11584 exp ? isl_space_copy(exp->dim) : NULL,
11585 isl_dim_map_extend(dim_map, bmap));
11586 isl_reordering_free(exp);
11587 free(dim_map);
11588 }
11589
11590 isl_space_free(model);
11591 return bmap;
11592error:
11593 isl_space_free(model);
11594 isl_basic_map_free(bmap);
11595 return NULL;
11596}
11597
Tobias Grossere5671e52017-03-10 09:17:55 +000011598/* Do "bset" and "space" have the same parameters?
11599 */
11600isl_bool isl_basic_set_space_has_equal_params(__isl_keep isl_basic_set *bset,
11601 __isl_keep isl_space *space)
11602{
11603 isl_space *bset_space;
11604
11605 bset_space = isl_basic_set_peek_space(bset);
Tobias Grosser593ebdf2017-03-14 07:33:26 +000011606 return isl_space_has_equal_params(bset_space, space);
Tobias Grossere5671e52017-03-10 09:17:55 +000011607}
11608
11609/* Do "map" and "space" have the same parameters?
11610 */
11611isl_bool isl_map_space_has_equal_params(__isl_keep isl_map *map,
11612 __isl_keep isl_space *space)
11613{
11614 isl_space *map_space;
11615
11616 map_space = isl_map_peek_space(map);
Tobias Grosser593ebdf2017-03-14 07:33:26 +000011617 return isl_space_has_equal_params(map_space, space);
Tobias Grossere5671e52017-03-10 09:17:55 +000011618}
11619
11620/* Do "set" and "space" have the same parameters?
11621 */
11622isl_bool isl_set_space_has_equal_params(__isl_keep isl_set *set,
11623 __isl_keep isl_space *space)
11624{
11625 return isl_map_space_has_equal_params(set_to_map(set), space);
11626}
11627
Tobias Grosser52a25232015-02-04 20:55:43 +000011628/* Align the parameters of "bset" to those of "model", introducing
11629 * additional parameters if needed.
11630 */
11631__isl_give isl_basic_set *isl_basic_set_align_params(
11632 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
11633{
11634 return isl_basic_map_align_params(bset, model);
11635}
11636
11637__isl_give isl_mat *isl_basic_map_equalities_matrix(
11638 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11639 enum isl_dim_type c2, enum isl_dim_type c3,
11640 enum isl_dim_type c4, enum isl_dim_type c5)
11641{
11642 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11643 struct isl_mat *mat;
11644 int i, j, k;
11645 int pos;
11646
11647 if (!bmap)
11648 return NULL;
11649 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
11650 isl_basic_map_total_dim(bmap) + 1);
11651 if (!mat)
11652 return NULL;
11653 for (i = 0; i < bmap->n_eq; ++i)
11654 for (j = 0, pos = 0; j < 5; ++j) {
11655 int off = isl_basic_map_offset(bmap, c[j]);
11656 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11657 isl_int_set(mat->row[i][pos],
11658 bmap->eq[i][off + k]);
11659 ++pos;
11660 }
11661 }
11662
11663 return mat;
11664}
11665
11666__isl_give isl_mat *isl_basic_map_inequalities_matrix(
11667 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11668 enum isl_dim_type c2, enum isl_dim_type c3,
11669 enum isl_dim_type c4, enum isl_dim_type c5)
11670{
11671 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11672 struct isl_mat *mat;
11673 int i, j, k;
11674 int pos;
11675
11676 if (!bmap)
11677 return NULL;
11678 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
11679 isl_basic_map_total_dim(bmap) + 1);
11680 if (!mat)
11681 return NULL;
11682 for (i = 0; i < bmap->n_ineq; ++i)
11683 for (j = 0, pos = 0; j < 5; ++j) {
11684 int off = isl_basic_map_offset(bmap, c[j]);
11685 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11686 isl_int_set(mat->row[i][pos],
11687 bmap->ineq[i][off + k]);
11688 ++pos;
11689 }
11690 }
11691
11692 return mat;
11693}
11694
11695__isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
11696 __isl_take isl_space *dim,
11697 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11698 enum isl_dim_type c2, enum isl_dim_type c3,
11699 enum isl_dim_type c4, enum isl_dim_type c5)
11700{
11701 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11702 isl_basic_map *bmap;
11703 unsigned total;
11704 unsigned extra;
11705 int i, j, k, l;
11706 int pos;
11707
11708 if (!dim || !eq || !ineq)
11709 goto error;
11710
11711 if (eq->n_col != ineq->n_col)
11712 isl_die(dim->ctx, isl_error_invalid,
11713 "equalities and inequalities matrices should have "
11714 "same number of columns", goto error);
11715
11716 total = 1 + isl_space_dim(dim, isl_dim_all);
11717
11718 if (eq->n_col < total)
11719 isl_die(dim->ctx, isl_error_invalid,
11720 "number of columns too small", goto error);
11721
11722 extra = eq->n_col - total;
11723
11724 bmap = isl_basic_map_alloc_space(isl_space_copy(dim), extra,
11725 eq->n_row, ineq->n_row);
11726 if (!bmap)
11727 goto error;
11728 for (i = 0; i < extra; ++i) {
11729 k = isl_basic_map_alloc_div(bmap);
11730 if (k < 0)
11731 goto error;
11732 isl_int_set_si(bmap->div[k][0], 0);
11733 }
11734 for (i = 0; i < eq->n_row; ++i) {
11735 l = isl_basic_map_alloc_equality(bmap);
11736 if (l < 0)
11737 goto error;
11738 for (j = 0, pos = 0; j < 5; ++j) {
11739 int off = isl_basic_map_offset(bmap, c[j]);
11740 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11741 isl_int_set(bmap->eq[l][off + k],
11742 eq->row[i][pos]);
11743 ++pos;
11744 }
11745 }
11746 }
11747 for (i = 0; i < ineq->n_row; ++i) {
11748 l = isl_basic_map_alloc_inequality(bmap);
11749 if (l < 0)
11750 goto error;
11751 for (j = 0, pos = 0; j < 5; ++j) {
11752 int off = isl_basic_map_offset(bmap, c[j]);
11753 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11754 isl_int_set(bmap->ineq[l][off + k],
11755 ineq->row[i][pos]);
11756 ++pos;
11757 }
11758 }
11759 }
11760
11761 isl_space_free(dim);
11762 isl_mat_free(eq);
11763 isl_mat_free(ineq);
11764
11765 bmap = isl_basic_map_simplify(bmap);
11766 return isl_basic_map_finalize(bmap);
11767error:
11768 isl_space_free(dim);
11769 isl_mat_free(eq);
11770 isl_mat_free(ineq);
11771 return NULL;
11772}
11773
11774__isl_give isl_mat *isl_basic_set_equalities_matrix(
11775 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11776 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11777{
Tobias Grosser06e15922016-11-16 11:06:47 +000011778 return isl_basic_map_equalities_matrix(bset_to_bmap(bset),
Tobias Grosser52a25232015-02-04 20:55:43 +000011779 c1, c2, c3, c4, isl_dim_in);
11780}
11781
11782__isl_give isl_mat *isl_basic_set_inequalities_matrix(
11783 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11784 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11785{
Tobias Grosser06e15922016-11-16 11:06:47 +000011786 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset),
Tobias Grosser52a25232015-02-04 20:55:43 +000011787 c1, c2, c3, c4, isl_dim_in);
11788}
11789
11790__isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
11791 __isl_take isl_space *dim,
11792 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11793 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11794{
Tobias Grosser06e15922016-11-16 11:06:47 +000011795 isl_basic_map *bmap;
11796 bmap = isl_basic_map_from_constraint_matrices(dim, eq, ineq,
Tobias Grosser52a25232015-02-04 20:55:43 +000011797 c1, c2, c3, c4, isl_dim_in);
Tobias Grosser06e15922016-11-16 11:06:47 +000011798 return bset_from_bmap(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +000011799}
11800
Tobias Grosserb2f39922015-05-28 13:32:11 +000011801isl_bool isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
Tobias Grosser52a25232015-02-04 20:55:43 +000011802{
11803 if (!bmap)
Tobias Grosserb2f39922015-05-28 13:32:11 +000011804 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000011805
11806 return isl_space_can_zip(bmap->dim);
11807}
11808
Tobias Grosserb2f39922015-05-28 13:32:11 +000011809isl_bool isl_map_can_zip(__isl_keep isl_map *map)
Tobias Grosser52a25232015-02-04 20:55:43 +000011810{
11811 if (!map)
Tobias Grosserb2f39922015-05-28 13:32:11 +000011812 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000011813
11814 return isl_space_can_zip(map->dim);
11815}
11816
11817/* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11818 * (A -> C) -> (B -> D).
11819 */
11820__isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
11821{
11822 unsigned pos;
11823 unsigned n1;
11824 unsigned n2;
11825
11826 if (!bmap)
11827 return NULL;
11828
11829 if (!isl_basic_map_can_zip(bmap))
11830 isl_die(bmap->ctx, isl_error_invalid,
11831 "basic map cannot be zipped", goto error);
11832 pos = isl_basic_map_offset(bmap, isl_dim_in) +
11833 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
11834 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
11835 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
11836 bmap = isl_basic_map_cow(bmap);
11837 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
11838 if (!bmap)
11839 return NULL;
11840 bmap->dim = isl_space_zip(bmap->dim);
11841 if (!bmap->dim)
11842 goto error;
Michael Kruse959a8dc2016-01-15 15:54:45 +000011843 bmap = isl_basic_map_mark_final(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +000011844 return bmap;
11845error:
11846 isl_basic_map_free(bmap);
11847 return NULL;
11848}
11849
11850/* Given a map (A -> B) -> (C -> D), return the corresponding map
11851 * (A -> C) -> (B -> D).
11852 */
11853__isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
11854{
11855 int i;
11856
11857 if (!map)
11858 return NULL;
11859
11860 if (!isl_map_can_zip(map))
11861 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
11862 goto error);
11863
11864 map = isl_map_cow(map);
11865 if (!map)
11866 return NULL;
11867
11868 for (i = 0; i < map->n; ++i) {
11869 map->p[i] = isl_basic_map_zip(map->p[i]);
11870 if (!map->p[i])
11871 goto error;
11872 }
11873
11874 map->dim = isl_space_zip(map->dim);
11875 if (!map->dim)
11876 goto error;
11877
11878 return map;
11879error:
11880 isl_map_free(map);
11881 return NULL;
11882}
11883
11884/* Can we apply isl_basic_map_curry to "bmap"?
11885 * That is, does it have a nested relation in its domain?
11886 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000011887isl_bool isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
Tobias Grosser52a25232015-02-04 20:55:43 +000011888{
11889 if (!bmap)
Tobias Grosserb2f39922015-05-28 13:32:11 +000011890 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000011891
11892 return isl_space_can_curry(bmap->dim);
11893}
11894
11895/* Can we apply isl_map_curry to "map"?
11896 * That is, does it have a nested relation in its domain?
11897 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000011898isl_bool isl_map_can_curry(__isl_keep isl_map *map)
Tobias Grosser52a25232015-02-04 20:55:43 +000011899{
11900 if (!map)
Tobias Grosserb2f39922015-05-28 13:32:11 +000011901 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000011902
11903 return isl_space_can_curry(map->dim);
11904}
11905
11906/* Given a basic map (A -> B) -> C, return the corresponding basic map
11907 * A -> (B -> C).
11908 */
11909__isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
11910{
11911
11912 if (!bmap)
11913 return NULL;
11914
11915 if (!isl_basic_map_can_curry(bmap))
11916 isl_die(bmap->ctx, isl_error_invalid,
11917 "basic map cannot be curried", goto error);
11918 bmap = isl_basic_map_cow(bmap);
11919 if (!bmap)
11920 return NULL;
11921 bmap->dim = isl_space_curry(bmap->dim);
11922 if (!bmap->dim)
11923 goto error;
Michael Kruse959a8dc2016-01-15 15:54:45 +000011924 bmap = isl_basic_map_mark_final(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +000011925 return bmap;
11926error:
11927 isl_basic_map_free(bmap);
11928 return NULL;
11929}
11930
11931/* Given a map (A -> B) -> C, return the corresponding map
11932 * A -> (B -> C).
11933 */
11934__isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
11935{
Michael Kruse959a8dc2016-01-15 15:54:45 +000011936 return isl_map_change_space(map, &isl_map_can_curry,
11937 "map cannot be curried", &isl_space_curry);
11938}
Tobias Grosser52a25232015-02-04 20:55:43 +000011939
Michael Kruse959a8dc2016-01-15 15:54:45 +000011940/* Can isl_map_range_curry be applied to "map"?
11941 * That is, does it have a nested relation in its range,
11942 * the domain of which is itself a nested relation?
11943 */
11944isl_bool isl_map_can_range_curry(__isl_keep isl_map *map)
11945{
Tobias Grosser52a25232015-02-04 20:55:43 +000011946 if (!map)
Michael Kruse959a8dc2016-01-15 15:54:45 +000011947 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000011948
Michael Kruse959a8dc2016-01-15 15:54:45 +000011949 return isl_space_can_range_curry(map->dim);
11950}
Tobias Grosser52a25232015-02-04 20:55:43 +000011951
Michael Kruse959a8dc2016-01-15 15:54:45 +000011952/* Given a map A -> ((B -> C) -> D), return the corresponding map
11953 * A -> (B -> (C -> D)).
11954 */
11955__isl_give isl_map *isl_map_range_curry(__isl_take isl_map *map)
11956{
11957 return isl_map_change_space(map, &isl_map_can_range_curry,
11958 "map range cannot be curried",
11959 &isl_space_range_curry);
Tobias Grosser52a25232015-02-04 20:55:43 +000011960}
11961
11962/* Can we apply isl_basic_map_uncurry to "bmap"?
11963 * That is, does it have a nested relation in its domain?
11964 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000011965isl_bool isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
Tobias Grosser52a25232015-02-04 20:55:43 +000011966{
11967 if (!bmap)
Tobias Grosserb2f39922015-05-28 13:32:11 +000011968 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000011969
11970 return isl_space_can_uncurry(bmap->dim);
11971}
11972
11973/* Can we apply isl_map_uncurry to "map"?
11974 * That is, does it have a nested relation in its domain?
11975 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000011976isl_bool isl_map_can_uncurry(__isl_keep isl_map *map)
Tobias Grosser52a25232015-02-04 20:55:43 +000011977{
11978 if (!map)
Tobias Grosserb2f39922015-05-28 13:32:11 +000011979 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000011980
11981 return isl_space_can_uncurry(map->dim);
11982}
11983
11984/* Given a basic map A -> (B -> C), return the corresponding basic map
11985 * (A -> B) -> C.
11986 */
11987__isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
11988{
11989
11990 if (!bmap)
11991 return NULL;
11992
11993 if (!isl_basic_map_can_uncurry(bmap))
11994 isl_die(bmap->ctx, isl_error_invalid,
11995 "basic map cannot be uncurried",
11996 return isl_basic_map_free(bmap));
11997 bmap = isl_basic_map_cow(bmap);
11998 if (!bmap)
11999 return NULL;
12000 bmap->dim = isl_space_uncurry(bmap->dim);
12001 if (!bmap->dim)
12002 return isl_basic_map_free(bmap);
Michael Kruse959a8dc2016-01-15 15:54:45 +000012003 bmap = isl_basic_map_mark_final(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +000012004 return bmap;
12005}
12006
12007/* Given a map A -> (B -> C), return the corresponding map
12008 * (A -> B) -> C.
12009 */
12010__isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
12011{
Michael Kruse959a8dc2016-01-15 15:54:45 +000012012 return isl_map_change_space(map, &isl_map_can_uncurry,
12013 "map cannot be uncurried", &isl_space_uncurry);
Tobias Grosser52a25232015-02-04 20:55:43 +000012014}
12015
12016/* Construct a basic map mapping the domain of the affine expression
12017 * to a one-dimensional range prescribed by the affine expression.
Tobias Grosser5652c9c2016-10-01 19:46:51 +000012018 * If "rational" is set, then construct a rational basic map.
Tobias Grosser37034db2016-03-25 19:38:18 +000012019 *
12020 * A NaN affine expression cannot be converted to a basic map.
Tobias Grosser52a25232015-02-04 20:55:43 +000012021 */
Tobias Grosser5652c9c2016-10-01 19:46:51 +000012022static __isl_give isl_basic_map *isl_basic_map_from_aff2(
12023 __isl_take isl_aff *aff, int rational)
Tobias Grosser52a25232015-02-04 20:55:43 +000012024{
12025 int k;
12026 int pos;
Tobias Grosser37034db2016-03-25 19:38:18 +000012027 isl_bool is_nan;
Tobias Grosser52a25232015-02-04 20:55:43 +000012028 isl_local_space *ls;
Tobias Grosser37034db2016-03-25 19:38:18 +000012029 isl_basic_map *bmap = NULL;
Tobias Grosser52a25232015-02-04 20:55:43 +000012030
12031 if (!aff)
12032 return NULL;
Tobias Grosser37034db2016-03-25 19:38:18 +000012033 is_nan = isl_aff_is_nan(aff);
12034 if (is_nan < 0)
12035 goto error;
12036 if (is_nan)
12037 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
12038 "cannot convert NaN", goto error);
Tobias Grosser52a25232015-02-04 20:55:43 +000012039
12040 ls = isl_aff_get_local_space(aff);
12041 bmap = isl_basic_map_from_local_space(ls);
12042 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
12043 k = isl_basic_map_alloc_equality(bmap);
12044 if (k < 0)
12045 goto error;
12046
12047 pos = isl_basic_map_offset(bmap, isl_dim_out);
12048 isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
12049 isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
12050 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
12051 aff->v->size - (pos + 1));
12052
12053 isl_aff_free(aff);
Tobias Grosser5652c9c2016-10-01 19:46:51 +000012054 if (rational)
12055 bmap = isl_basic_map_set_rational(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +000012056 bmap = isl_basic_map_finalize(bmap);
12057 return bmap;
12058error:
12059 isl_aff_free(aff);
12060 isl_basic_map_free(bmap);
12061 return NULL;
12062}
12063
Tobias Grosser5652c9c2016-10-01 19:46:51 +000012064/* Construct a basic map mapping the domain of the affine expression
12065 * to a one-dimensional range prescribed by the affine expression.
12066 */
12067__isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
12068{
12069 return isl_basic_map_from_aff2(aff, 0);
12070}
12071
Tobias Grosser52a25232015-02-04 20:55:43 +000012072/* Construct a map mapping the domain of the affine expression
12073 * to a one-dimensional range prescribed by the affine expression.
12074 */
12075__isl_give isl_map *isl_map_from_aff(__isl_take isl_aff *aff)
12076{
12077 isl_basic_map *bmap;
12078
12079 bmap = isl_basic_map_from_aff(aff);
12080 return isl_map_from_basic_map(bmap);
12081}
12082
12083/* Construct a basic map mapping the domain the multi-affine expression
12084 * to its range, with each dimension in the range equated to the
12085 * corresponding affine expression.
Tobias Grosser5652c9c2016-10-01 19:46:51 +000012086 * If "rational" is set, then construct a rational basic map.
Tobias Grosser52a25232015-02-04 20:55:43 +000012087 */
Tobias Grosser5652c9c2016-10-01 19:46:51 +000012088__isl_give isl_basic_map *isl_basic_map_from_multi_aff2(
12089 __isl_take isl_multi_aff *maff, int rational)
Tobias Grosser52a25232015-02-04 20:55:43 +000012090{
12091 int i;
12092 isl_space *space;
12093 isl_basic_map *bmap;
12094
12095 if (!maff)
12096 return NULL;
12097
12098 if (isl_space_dim(maff->space, isl_dim_out) != maff->n)
12099 isl_die(isl_multi_aff_get_ctx(maff), isl_error_internal,
12100 "invalid space", goto error);
12101
12102 space = isl_space_domain(isl_multi_aff_get_space(maff));
12103 bmap = isl_basic_map_universe(isl_space_from_domain(space));
Tobias Grosser5652c9c2016-10-01 19:46:51 +000012104 if (rational)
12105 bmap = isl_basic_map_set_rational(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +000012106
12107 for (i = 0; i < maff->n; ++i) {
12108 isl_aff *aff;
12109 isl_basic_map *bmap_i;
12110
12111 aff = isl_aff_copy(maff->p[i]);
Tobias Grosser5652c9c2016-10-01 19:46:51 +000012112 bmap_i = isl_basic_map_from_aff2(aff, rational);
Tobias Grosser52a25232015-02-04 20:55:43 +000012113
12114 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
12115 }
12116
12117 bmap = isl_basic_map_reset_space(bmap, isl_multi_aff_get_space(maff));
12118
12119 isl_multi_aff_free(maff);
12120 return bmap;
12121error:
12122 isl_multi_aff_free(maff);
12123 return NULL;
12124}
12125
Tobias Grosser5652c9c2016-10-01 19:46:51 +000012126/* Construct a basic map mapping the domain the multi-affine expression
12127 * to its range, with each dimension in the range equated to the
12128 * corresponding affine expression.
12129 */
12130__isl_give isl_basic_map *isl_basic_map_from_multi_aff(
12131 __isl_take isl_multi_aff *ma)
12132{
12133 return isl_basic_map_from_multi_aff2(ma, 0);
12134}
12135
Tobias Grosser52a25232015-02-04 20:55:43 +000012136/* Construct a map mapping the domain the multi-affine expression
12137 * to its range, with each dimension in the range equated to the
12138 * corresponding affine expression.
12139 */
12140__isl_give isl_map *isl_map_from_multi_aff(__isl_take isl_multi_aff *maff)
12141{
12142 isl_basic_map *bmap;
12143
12144 bmap = isl_basic_map_from_multi_aff(maff);
12145 return isl_map_from_basic_map(bmap);
12146}
12147
12148/* Construct a basic map mapping a domain in the given space to
12149 * to an n-dimensional range, with n the number of elements in the list,
12150 * where each coordinate in the range is prescribed by the
12151 * corresponding affine expression.
12152 * The domains of all affine expressions in the list are assumed to match
12153 * domain_dim.
12154 */
12155__isl_give isl_basic_map *isl_basic_map_from_aff_list(
12156 __isl_take isl_space *domain_dim, __isl_take isl_aff_list *list)
12157{
12158 int i;
12159 isl_space *dim;
12160 isl_basic_map *bmap;
12161
12162 if (!list)
12163 return NULL;
12164
12165 dim = isl_space_from_domain(domain_dim);
12166 bmap = isl_basic_map_universe(dim);
12167
12168 for (i = 0; i < list->n; ++i) {
12169 isl_aff *aff;
12170 isl_basic_map *bmap_i;
12171
12172 aff = isl_aff_copy(list->p[i]);
12173 bmap_i = isl_basic_map_from_aff(aff);
12174
12175 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
12176 }
12177
12178 isl_aff_list_free(list);
12179 return bmap;
12180}
12181
12182__isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
12183 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12184{
12185 return isl_map_equate(set, type1, pos1, type2, pos2);
12186}
12187
12188/* Construct a basic map where the given dimensions are equal to each other.
12189 */
12190static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
12191 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12192{
12193 isl_basic_map *bmap = NULL;
12194 int i;
12195
12196 if (!space)
12197 return NULL;
12198
12199 if (pos1 >= isl_space_dim(space, type1))
12200 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12201 "index out of bounds", goto error);
12202 if (pos2 >= isl_space_dim(space, type2))
12203 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12204 "index out of bounds", goto error);
12205
12206 if (type1 == type2 && pos1 == pos2)
12207 return isl_basic_map_universe(space);
12208
12209 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
12210 i = isl_basic_map_alloc_equality(bmap);
12211 if (i < 0)
12212 goto error;
12213 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12214 pos1 += isl_basic_map_offset(bmap, type1);
12215 pos2 += isl_basic_map_offset(bmap, type2);
12216 isl_int_set_si(bmap->eq[i][pos1], -1);
12217 isl_int_set_si(bmap->eq[i][pos2], 1);
12218 bmap = isl_basic_map_finalize(bmap);
12219 isl_space_free(space);
12220 return bmap;
12221error:
12222 isl_space_free(space);
12223 isl_basic_map_free(bmap);
12224 return NULL;
12225}
12226
12227/* Add a constraint imposing that the given two dimensions are equal.
12228 */
12229__isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
12230 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12231{
12232 isl_basic_map *eq;
12233
12234 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12235
12236 bmap = isl_basic_map_intersect(bmap, eq);
12237
12238 return bmap;
12239}
12240
12241/* Add a constraint imposing that the given two dimensions are equal.
12242 */
12243__isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
12244 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12245{
12246 isl_basic_map *bmap;
12247
12248 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
12249
12250 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12251
12252 return map;
12253}
12254
12255/* Add a constraint imposing that the given two dimensions have opposite values.
12256 */
12257__isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
12258 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12259{
12260 isl_basic_map *bmap = NULL;
12261 int i;
12262
12263 if (!map)
12264 return NULL;
12265
12266 if (pos1 >= isl_map_dim(map, type1))
12267 isl_die(map->ctx, isl_error_invalid,
12268 "index out of bounds", goto error);
12269 if (pos2 >= isl_map_dim(map, type2))
12270 isl_die(map->ctx, isl_error_invalid,
12271 "index out of bounds", goto error);
12272
12273 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
12274 i = isl_basic_map_alloc_equality(bmap);
12275 if (i < 0)
12276 goto error;
12277 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12278 pos1 += isl_basic_map_offset(bmap, type1);
12279 pos2 += isl_basic_map_offset(bmap, type2);
12280 isl_int_set_si(bmap->eq[i][pos1], 1);
12281 isl_int_set_si(bmap->eq[i][pos2], 1);
12282 bmap = isl_basic_map_finalize(bmap);
12283
12284 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12285
12286 return map;
12287error:
12288 isl_basic_map_free(bmap);
12289 isl_map_free(map);
12290 return NULL;
12291}
12292
12293/* Construct a constraint imposing that the value of the first dimension is
12294 * greater than or equal to that of the second.
12295 */
12296static __isl_give isl_constraint *constraint_order_ge(
12297 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
12298 enum isl_dim_type type2, int pos2)
12299{
12300 isl_constraint *c;
12301
12302 if (!space)
12303 return NULL;
12304
Tobias Grosserb2f39922015-05-28 13:32:11 +000012305 c = isl_constraint_alloc_inequality(isl_local_space_from_space(space));
Tobias Grosser52a25232015-02-04 20:55:43 +000012306
12307 if (pos1 >= isl_constraint_dim(c, type1))
12308 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
12309 "index out of bounds", return isl_constraint_free(c));
12310 if (pos2 >= isl_constraint_dim(c, type2))
12311 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
12312 "index out of bounds", return isl_constraint_free(c));
12313
12314 if (type1 == type2 && pos1 == pos2)
12315 return c;
12316
12317 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
12318 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
12319
12320 return c;
12321}
12322
12323/* Add a constraint imposing that the value of the first dimension is
12324 * greater than or equal to that of the second.
12325 */
12326__isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
12327 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12328{
12329 isl_constraint *c;
12330 isl_space *space;
12331
12332 if (type1 == type2 && pos1 == pos2)
12333 return bmap;
12334 space = isl_basic_map_get_space(bmap);
12335 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12336 bmap = isl_basic_map_add_constraint(bmap, c);
12337
12338 return bmap;
12339}
12340
12341/* Add a constraint imposing that the value of the first dimension is
12342 * greater than or equal to that of the second.
12343 */
12344__isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
12345 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12346{
12347 isl_constraint *c;
12348 isl_space *space;
12349
12350 if (type1 == type2 && pos1 == pos2)
12351 return map;
12352 space = isl_map_get_space(map);
12353 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12354 map = isl_map_add_constraint(map, c);
12355
12356 return map;
12357}
12358
12359/* Add a constraint imposing that the value of the first dimension is
12360 * less than or equal to that of the second.
12361 */
12362__isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
12363 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12364{
12365 return isl_map_order_ge(map, type2, pos2, type1, pos1);
12366}
12367
12368/* Construct a basic map where the value of the first dimension is
12369 * greater than that of the second.
12370 */
12371static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
12372 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12373{
12374 isl_basic_map *bmap = NULL;
12375 int i;
12376
12377 if (!space)
12378 return NULL;
12379
12380 if (pos1 >= isl_space_dim(space, type1))
12381 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12382 "index out of bounds", goto error);
12383 if (pos2 >= isl_space_dim(space, type2))
12384 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12385 "index out of bounds", goto error);
12386
12387 if (type1 == type2 && pos1 == pos2)
12388 return isl_basic_map_empty(space);
12389
12390 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
12391 i = isl_basic_map_alloc_inequality(bmap);
12392 if (i < 0)
12393 return isl_basic_map_free(bmap);
12394 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
12395 pos1 += isl_basic_map_offset(bmap, type1);
12396 pos2 += isl_basic_map_offset(bmap, type2);
12397 isl_int_set_si(bmap->ineq[i][pos1], 1);
12398 isl_int_set_si(bmap->ineq[i][pos2], -1);
12399 isl_int_set_si(bmap->ineq[i][0], -1);
12400 bmap = isl_basic_map_finalize(bmap);
12401
12402 return bmap;
12403error:
12404 isl_space_free(space);
12405 isl_basic_map_free(bmap);
12406 return NULL;
12407}
12408
12409/* Add a constraint imposing that the value of the first dimension is
12410 * greater than that of the second.
12411 */
12412__isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
12413 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12414{
12415 isl_basic_map *gt;
12416
12417 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12418
12419 bmap = isl_basic_map_intersect(bmap, gt);
12420
12421 return bmap;
12422}
12423
12424/* Add a constraint imposing that the value of the first dimension is
12425 * greater than that of the second.
12426 */
12427__isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
12428 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12429{
12430 isl_basic_map *bmap;
12431
12432 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
12433
12434 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12435
12436 return map;
12437}
12438
12439/* Add a constraint imposing that the value of the first dimension is
12440 * smaller than that of the second.
12441 */
12442__isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
12443 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12444{
12445 return isl_map_order_gt(map, type2, pos2, type1, pos1);
12446}
12447
12448__isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
12449 int pos)
12450{
12451 isl_aff *div;
12452 isl_local_space *ls;
12453
12454 if (!bmap)
12455 return NULL;
12456
12457 if (!isl_basic_map_divs_known(bmap))
12458 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12459 "some divs are unknown", return NULL);
12460
12461 ls = isl_basic_map_get_local_space(bmap);
12462 div = isl_local_space_get_div(ls, pos);
12463 isl_local_space_free(ls);
12464
12465 return div;
12466}
12467
12468__isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
12469 int pos)
12470{
12471 return isl_basic_map_get_div(bset, pos);
12472}
12473
12474/* Plug in "subs" for dimension "type", "pos" of "bset".
12475 *
12476 * Let i be the dimension to replace and let "subs" be of the form
12477 *
12478 * f/d
12479 *
12480 * Any integer division with a non-zero coefficient for i,
12481 *
12482 * floor((a i + g)/m)
12483 *
12484 * is replaced by
12485 *
12486 * floor((a f + d g)/(m d))
12487 *
12488 * Constraints of the form
12489 *
12490 * a i + g
12491 *
12492 * are replaced by
12493 *
12494 * a f + d g
12495 *
12496 * We currently require that "subs" is an integral expression.
12497 * Handling rational expressions may require us to add stride constraints
12498 * as we do in isl_basic_set_preimage_multi_aff.
12499 */
12500__isl_give isl_basic_set *isl_basic_set_substitute(
12501 __isl_take isl_basic_set *bset,
12502 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12503{
12504 int i;
12505 isl_int v;
12506 isl_ctx *ctx;
12507
12508 if (bset && isl_basic_set_plain_is_empty(bset))
12509 return bset;
12510
12511 bset = isl_basic_set_cow(bset);
12512 if (!bset || !subs)
12513 goto error;
12514
12515 ctx = isl_basic_set_get_ctx(bset);
12516 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
12517 isl_die(ctx, isl_error_invalid,
12518 "spaces don't match", goto error);
12519 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
12520 isl_die(ctx, isl_error_unsupported,
12521 "cannot handle divs yet", goto error);
12522 if (!isl_int_is_one(subs->v->el[0]))
12523 isl_die(ctx, isl_error_invalid,
12524 "can only substitute integer expressions", goto error);
12525
12526 pos += isl_basic_set_offset(bset, type);
12527
12528 isl_int_init(v);
12529
12530 for (i = 0; i < bset->n_eq; ++i) {
12531 if (isl_int_is_zero(bset->eq[i][pos]))
12532 continue;
12533 isl_int_set(v, bset->eq[i][pos]);
12534 isl_int_set_si(bset->eq[i][pos], 0);
12535 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
12536 v, subs->v->el + 1, subs->v->size - 1);
12537 }
12538
12539 for (i = 0; i < bset->n_ineq; ++i) {
12540 if (isl_int_is_zero(bset->ineq[i][pos]))
12541 continue;
12542 isl_int_set(v, bset->ineq[i][pos]);
12543 isl_int_set_si(bset->ineq[i][pos], 0);
12544 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
12545 v, subs->v->el + 1, subs->v->size - 1);
12546 }
12547
12548 for (i = 0; i < bset->n_div; ++i) {
12549 if (isl_int_is_zero(bset->div[i][1 + pos]))
12550 continue;
12551 isl_int_set(v, bset->div[i][1 + pos]);
12552 isl_int_set_si(bset->div[i][1 + pos], 0);
12553 isl_seq_combine(bset->div[i] + 1,
12554 subs->v->el[0], bset->div[i] + 1,
12555 v, subs->v->el + 1, subs->v->size - 1);
12556 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
12557 }
12558
12559 isl_int_clear(v);
12560
12561 bset = isl_basic_set_simplify(bset);
12562 return isl_basic_set_finalize(bset);
12563error:
12564 isl_basic_set_free(bset);
12565 return NULL;
12566}
12567
12568/* Plug in "subs" for dimension "type", "pos" of "set".
12569 */
12570__isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
12571 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12572{
12573 int i;
12574
12575 if (set && isl_set_plain_is_empty(set))
12576 return set;
12577
12578 set = isl_set_cow(set);
12579 if (!set || !subs)
12580 goto error;
12581
12582 for (i = set->n - 1; i >= 0; --i) {
12583 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
12584 if (remove_if_empty(set, i) < 0)
12585 goto error;
12586 }
12587
12588 return set;
12589error:
12590 isl_set_free(set);
12591 return NULL;
12592}
12593
12594/* Check if the range of "ma" is compatible with the domain or range
12595 * (depending on "type") of "bmap".
Tobias Grosser52a25232015-02-04 20:55:43 +000012596 */
Tobias Grosser72745c22017-02-17 05:11:16 +000012597static isl_stat check_basic_map_compatible_range_multi_aff(
Tobias Grosser52a25232015-02-04 20:55:43 +000012598 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
12599 __isl_keep isl_multi_aff *ma)
12600{
Tobias Grosser72745c22017-02-17 05:11:16 +000012601 isl_bool m;
Tobias Grosser52a25232015-02-04 20:55:43 +000012602 isl_space *ma_space;
12603
12604 ma_space = isl_multi_aff_get_space(ma);
12605
Tobias Grosser593ebdf2017-03-14 07:33:26 +000012606 m = isl_space_has_equal_params(bmap->dim, ma_space);
Tobias Grosser52a25232015-02-04 20:55:43 +000012607 if (m < 0)
12608 goto error;
12609 if (!m)
12610 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12611 "parameters don't match", goto error);
12612 m = isl_space_tuple_is_equal(bmap->dim, type, ma_space, isl_dim_out);
12613 if (m < 0)
12614 goto error;
12615 if (!m)
12616 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12617 "spaces don't match", goto error);
12618
12619 isl_space_free(ma_space);
Tobias Grosser72745c22017-02-17 05:11:16 +000012620 return isl_stat_ok;
Tobias Grosser52a25232015-02-04 20:55:43 +000012621error:
12622 isl_space_free(ma_space);
Tobias Grosser72745c22017-02-17 05:11:16 +000012623 return isl_stat_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000012624}
12625
12626/* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12627 * coefficients before the transformed range of dimensions,
12628 * the "n_after" coefficients after the transformed range of dimensions
12629 * and the coefficients of the other divs in "bmap".
12630 */
12631static int set_ma_divs(__isl_keep isl_basic_map *bmap,
12632 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
12633{
12634 int i;
12635 int n_param;
12636 int n_set;
12637 isl_local_space *ls;
12638
12639 if (n_div == 0)
12640 return 0;
12641
12642 ls = isl_aff_get_domain_local_space(ma->p[0]);
12643 if (!ls)
12644 return -1;
12645
12646 n_param = isl_local_space_dim(ls, isl_dim_param);
12647 n_set = isl_local_space_dim(ls, isl_dim_set);
12648 for (i = 0; i < n_div; ++i) {
12649 int o_bmap = 0, o_ls = 0;
12650
12651 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
12652 o_bmap += 1 + 1 + n_param;
12653 o_ls += 1 + 1 + n_param;
12654 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
12655 o_bmap += n_before;
12656 isl_seq_cpy(bmap->div[i] + o_bmap,
12657 ls->div->row[i] + o_ls, n_set);
12658 o_bmap += n_set;
12659 o_ls += n_set;
12660 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
12661 o_bmap += n_after;
12662 isl_seq_cpy(bmap->div[i] + o_bmap,
12663 ls->div->row[i] + o_ls, n_div);
12664 o_bmap += n_div;
12665 o_ls += n_div;
12666 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
Tobias Grosser59d23bb2017-02-23 12:48:42 +000012667 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
Tobias Grosser52a25232015-02-04 20:55:43 +000012668 goto error;
12669 }
12670
12671 isl_local_space_free(ls);
12672 return 0;
12673error:
12674 isl_local_space_free(ls);
12675 return -1;
12676}
12677
12678/* How many stride constraints does "ma" enforce?
12679 * That is, how many of the affine expressions have a denominator
12680 * different from one?
12681 */
12682static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
12683{
12684 int i;
12685 int strides = 0;
12686
12687 for (i = 0; i < ma->n; ++i)
12688 if (!isl_int_is_one(ma->p[i]->v->el[0]))
12689 strides++;
12690
12691 return strides;
12692}
12693
12694/* For each affine expression in ma of the form
12695 *
12696 * x_i = (f_i y + h_i)/m_i
12697 *
12698 * with m_i different from one, add a constraint to "bmap"
12699 * of the form
12700 *
12701 * f_i y + h_i = m_i alpha_i
12702 *
12703 * with alpha_i an additional existentially quantified variable.
Tobias Grossera67ac972016-02-26 11:35:12 +000012704 *
12705 * The input variables of "ma" correspond to a subset of the variables
12706 * of "bmap". There are "n_before" variables in "bmap" before this
12707 * subset and "n_after" variables after this subset.
12708 * The integer divisions of the affine expressions in "ma" are assumed
12709 * to have been aligned. There are "n_div_ma" of them and
12710 * they appear first in "bmap", straight after the "n_after" variables.
Tobias Grosser52a25232015-02-04 20:55:43 +000012711 */
12712static __isl_give isl_basic_map *add_ma_strides(
12713 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
Tobias Grossera67ac972016-02-26 11:35:12 +000012714 int n_before, int n_after, int n_div_ma)
Tobias Grosser52a25232015-02-04 20:55:43 +000012715{
12716 int i, k;
12717 int div;
12718 int total;
12719 int n_param;
12720 int n_in;
Tobias Grosser52a25232015-02-04 20:55:43 +000012721
12722 total = isl_basic_map_total_dim(bmap);
12723 n_param = isl_multi_aff_dim(ma, isl_dim_param);
12724 n_in = isl_multi_aff_dim(ma, isl_dim_in);
Tobias Grosser52a25232015-02-04 20:55:43 +000012725 for (i = 0; i < ma->n; ++i) {
12726 int o_bmap = 0, o_ma = 1;
12727
12728 if (isl_int_is_one(ma->p[i]->v->el[0]))
12729 continue;
12730 div = isl_basic_map_alloc_div(bmap);
12731 k = isl_basic_map_alloc_equality(bmap);
12732 if (div < 0 || k < 0)
12733 goto error;
12734 isl_int_set_si(bmap->div[div][0], 0);
12735 isl_seq_cpy(bmap->eq[k] + o_bmap,
12736 ma->p[i]->v->el + o_ma, 1 + n_param);
12737 o_bmap += 1 + n_param;
12738 o_ma += 1 + n_param;
12739 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
12740 o_bmap += n_before;
12741 isl_seq_cpy(bmap->eq[k] + o_bmap,
12742 ma->p[i]->v->el + o_ma, n_in);
12743 o_bmap += n_in;
12744 o_ma += n_in;
12745 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
12746 o_bmap += n_after;
12747 isl_seq_cpy(bmap->eq[k] + o_bmap,
Tobias Grossera67ac972016-02-26 11:35:12 +000012748 ma->p[i]->v->el + o_ma, n_div_ma);
12749 o_bmap += n_div_ma;
12750 o_ma += n_div_ma;
Tobias Grosser52a25232015-02-04 20:55:43 +000012751 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
12752 isl_int_neg(bmap->eq[k][1 + total], ma->p[i]->v->el[0]);
12753 total++;
12754 }
12755
12756 return bmap;
12757error:
12758 isl_basic_map_free(bmap);
12759 return NULL;
12760}
12761
12762/* Replace the domain or range space (depending on "type) of "space" by "set".
12763 */
12764static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
12765 enum isl_dim_type type, __isl_take isl_space *set)
12766{
12767 if (type == isl_dim_in) {
12768 space = isl_space_range(space);
12769 space = isl_space_map_from_domain_and_range(set, space);
12770 } else {
12771 space = isl_space_domain(space);
12772 space = isl_space_map_from_domain_and_range(space, set);
12773 }
12774
12775 return space;
12776}
12777
12778/* Compute the preimage of the domain or range (depending on "type")
12779 * of "bmap" under the function represented by "ma".
12780 * In other words, plug in "ma" in the domain or range of "bmap".
12781 * The result is a basic map that lives in the same space as "bmap"
12782 * except that the domain or range has been replaced by
12783 * the domain space of "ma".
12784 *
12785 * If bmap is represented by
12786 *
12787 * A(p) + S u + B x + T v + C(divs) >= 0,
12788 *
12789 * where u and x are input and output dimensions if type == isl_dim_out
12790 * while x and v are input and output dimensions if type == isl_dim_in,
12791 * and ma is represented by
12792 *
12793 * x = D(p) + F(y) + G(divs')
12794 *
12795 * then the result is
12796 *
12797 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12798 *
12799 * The divs in the input set are similarly adjusted.
12800 * In particular
12801 *
12802 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12803 *
12804 * becomes
12805 *
12806 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12807 * B_i G(divs') + c_i(divs))/n_i)
12808 *
12809 * If bmap is not a rational map and if F(y) involves any denominators
12810 *
12811 * x_i = (f_i y + h_i)/m_i
12812 *
12813 * then additional constraints are added to ensure that we only
12814 * map back integer points. That is we enforce
12815 *
12816 * f_i y + h_i = m_i alpha_i
12817 *
12818 * with alpha_i an additional existentially quantified variable.
12819 *
12820 * We first copy over the divs from "ma".
12821 * Then we add the modified constraints and divs from "bmap".
12822 * Finally, we add the stride constraints, if needed.
12823 */
12824__isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
12825 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
12826 __isl_take isl_multi_aff *ma)
12827{
12828 int i, k;
12829 isl_space *space;
12830 isl_basic_map *res = NULL;
12831 int n_before, n_after, n_div_bmap, n_div_ma;
12832 isl_int f, c1, c2, g;
Tobias Grosser72745c22017-02-17 05:11:16 +000012833 isl_bool rational;
12834 int strides;
Tobias Grosser52a25232015-02-04 20:55:43 +000012835
12836 isl_int_init(f);
12837 isl_int_init(c1);
12838 isl_int_init(c2);
12839 isl_int_init(g);
12840
12841 ma = isl_multi_aff_align_divs(ma);
12842 if (!bmap || !ma)
12843 goto error;
12844 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
12845 goto error;
12846
12847 if (type == isl_dim_in) {
12848 n_before = 0;
12849 n_after = isl_basic_map_dim(bmap, isl_dim_out);
12850 } else {
12851 n_before = isl_basic_map_dim(bmap, isl_dim_in);
12852 n_after = 0;
12853 }
12854 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
12855 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
12856
12857 space = isl_multi_aff_get_domain_space(ma);
12858 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
12859 rational = isl_basic_map_is_rational(bmap);
12860 strides = rational ? 0 : multi_aff_strides(ma);
12861 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
12862 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
12863 if (rational)
12864 res = isl_basic_map_set_rational(res);
12865
12866 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
12867 if (isl_basic_map_alloc_div(res) < 0)
12868 goto error;
12869
12870 if (set_ma_divs(res, ma, n_before, n_after, n_div_ma) < 0)
12871 goto error;
12872
12873 for (i = 0; i < bmap->n_eq; ++i) {
12874 k = isl_basic_map_alloc_equality(res);
12875 if (k < 0)
12876 goto error;
12877 isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
12878 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12879 }
12880
12881 for (i = 0; i < bmap->n_ineq; ++i) {
12882 k = isl_basic_map_alloc_inequality(res);
12883 if (k < 0)
12884 goto error;
12885 isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
12886 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12887 }
12888
12889 for (i = 0; i < bmap->n_div; ++i) {
12890 if (isl_int_is_zero(bmap->div[i][0])) {
12891 isl_int_set_si(res->div[n_div_ma + i][0], 0);
12892 continue;
12893 }
12894 isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
12895 n_before, n_after, n_div_ma, n_div_bmap,
12896 f, c1, c2, g, 1);
12897 }
12898
12899 if (strides)
Tobias Grossera67ac972016-02-26 11:35:12 +000012900 res = add_ma_strides(res, ma, n_before, n_after, n_div_ma);
Tobias Grosser52a25232015-02-04 20:55:43 +000012901
12902 isl_int_clear(f);
12903 isl_int_clear(c1);
12904 isl_int_clear(c2);
12905 isl_int_clear(g);
12906 isl_basic_map_free(bmap);
12907 isl_multi_aff_free(ma);
Tobias Grosser59d23bb2017-02-23 12:48:42 +000012908 res = isl_basic_map_simplify(res);
Tobias Grosser52a25232015-02-04 20:55:43 +000012909 return isl_basic_map_finalize(res);
12910error:
12911 isl_int_clear(f);
12912 isl_int_clear(c1);
12913 isl_int_clear(c2);
12914 isl_int_clear(g);
12915 isl_basic_map_free(bmap);
12916 isl_multi_aff_free(ma);
12917 isl_basic_map_free(res);
12918 return NULL;
12919}
12920
12921/* Compute the preimage of "bset" under the function represented by "ma".
12922 * In other words, plug in "ma" in "bset". The result is a basic set
12923 * that lives in the domain space of "ma".
12924 */
12925__isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
12926 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
12927{
12928 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
12929}
12930
12931/* Compute the preimage of the domain of "bmap" under the function
12932 * represented by "ma".
12933 * In other words, plug in "ma" in the domain of "bmap".
12934 * The result is a basic map that lives in the same space as "bmap"
12935 * except that the domain has been replaced by the domain space of "ma".
12936 */
12937__isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
12938 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12939{
12940 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
12941}
12942
12943/* Compute the preimage of the range of "bmap" under the function
12944 * represented by "ma".
12945 * In other words, plug in "ma" in the range of "bmap".
12946 * The result is a basic map that lives in the same space as "bmap"
12947 * except that the range has been replaced by the domain space of "ma".
12948 */
12949__isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
12950 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12951{
12952 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
12953}
12954
12955/* Check if the range of "ma" is compatible with the domain or range
12956 * (depending on "type") of "map".
Tobias Grosser72745c22017-02-17 05:11:16 +000012957 * Return isl_stat_error if anything is wrong.
Tobias Grosser52a25232015-02-04 20:55:43 +000012958 */
Tobias Grosser72745c22017-02-17 05:11:16 +000012959static isl_stat check_map_compatible_range_multi_aff(
Tobias Grosser52a25232015-02-04 20:55:43 +000012960 __isl_keep isl_map *map, enum isl_dim_type type,
12961 __isl_keep isl_multi_aff *ma)
12962{
Tobias Grosser72745c22017-02-17 05:11:16 +000012963 isl_bool m;
Tobias Grosser52a25232015-02-04 20:55:43 +000012964 isl_space *ma_space;
12965
12966 ma_space = isl_multi_aff_get_space(ma);
12967 m = isl_space_tuple_is_equal(map->dim, type, ma_space, isl_dim_out);
12968 isl_space_free(ma_space);
Tobias Grosser72745c22017-02-17 05:11:16 +000012969 if (m < 0)
12970 return isl_stat_error;
12971 if (!m)
Tobias Grosser52a25232015-02-04 20:55:43 +000012972 isl_die(isl_map_get_ctx(map), isl_error_invalid,
Tobias Grosser72745c22017-02-17 05:11:16 +000012973 "spaces don't match", return isl_stat_error);
12974 return isl_stat_ok;
Tobias Grosser52a25232015-02-04 20:55:43 +000012975}
12976
12977/* Compute the preimage of the domain or range (depending on "type")
12978 * of "map" under the function represented by "ma".
12979 * In other words, plug in "ma" in the domain or range of "map".
12980 * The result is a map that lives in the same space as "map"
12981 * except that the domain or range has been replaced by
12982 * the domain space of "ma".
12983 *
12984 * The parameters are assumed to have been aligned.
12985 */
12986static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
12987 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12988{
12989 int i;
12990 isl_space *space;
12991
12992 map = isl_map_cow(map);
12993 ma = isl_multi_aff_align_divs(ma);
12994 if (!map || !ma)
12995 goto error;
12996 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
12997 goto error;
12998
12999 for (i = 0; i < map->n; ++i) {
13000 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
13001 isl_multi_aff_copy(ma));
13002 if (!map->p[i])
13003 goto error;
13004 }
13005
13006 space = isl_multi_aff_get_domain_space(ma);
13007 space = isl_space_set(isl_map_get_space(map), type, space);
13008
13009 isl_space_free(map->dim);
13010 map->dim = space;
13011 if (!map->dim)
13012 goto error;
13013
13014 isl_multi_aff_free(ma);
13015 if (map->n > 1)
13016 ISL_F_CLR(map, ISL_MAP_DISJOINT);
13017 ISL_F_CLR(map, ISL_SET_NORMALIZED);
13018 return map;
13019error:
13020 isl_multi_aff_free(ma);
13021 isl_map_free(map);
13022 return NULL;
13023}
13024
13025/* Compute the preimage of the domain or range (depending on "type")
13026 * of "map" under the function represented by "ma".
13027 * In other words, plug in "ma" in the domain or range of "map".
13028 * The result is a map that lives in the same space as "map"
13029 * except that the domain or range has been replaced by
13030 * the domain space of "ma".
13031 */
13032__isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
13033 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
13034{
Tobias Grossere5671e52017-03-10 09:17:55 +000013035 isl_bool aligned;
13036
Tobias Grosser52a25232015-02-04 20:55:43 +000013037 if (!map || !ma)
13038 goto error;
13039
Tobias Grossere5671e52017-03-10 09:17:55 +000013040 aligned = isl_map_space_has_equal_params(map, ma->space);
13041 if (aligned < 0)
13042 goto error;
13043 if (aligned)
Tobias Grosser52a25232015-02-04 20:55:43 +000013044 return map_preimage_multi_aff(map, type, ma);
13045
Tobias Grossere5671e52017-03-10 09:17:55 +000013046 if (isl_map_check_named_params(map) < 0)
13047 goto error;
13048 if (!isl_space_has_named_params(ma->space))
Tobias Grosser52a25232015-02-04 20:55:43 +000013049 isl_die(map->ctx, isl_error_invalid,
13050 "unaligned unnamed parameters", goto error);
13051 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
13052 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
13053
13054 return map_preimage_multi_aff(map, type, ma);
13055error:
13056 isl_multi_aff_free(ma);
13057 return isl_map_free(map);
13058}
13059
13060/* Compute the preimage of "set" under the function represented by "ma".
13061 * In other words, plug in "ma" in "set". The result is a set
13062 * that lives in the domain space of "ma".
13063 */
13064__isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
13065 __isl_take isl_multi_aff *ma)
13066{
13067 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
13068}
13069
13070/* Compute the preimage of the domain of "map" under the function
13071 * represented by "ma".
13072 * In other words, plug in "ma" in the domain of "map".
13073 * The result is a map that lives in the same space as "map"
13074 * except that the domain has been replaced by the domain space of "ma".
13075 */
13076__isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
13077 __isl_take isl_multi_aff *ma)
13078{
13079 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
13080}
13081
13082/* Compute the preimage of the range of "map" under the function
13083 * represented by "ma".
13084 * In other words, plug in "ma" in the range of "map".
13085 * The result is a map that lives in the same space as "map"
13086 * except that the range has been replaced by the domain space of "ma".
13087 */
13088__isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
13089 __isl_take isl_multi_aff *ma)
13090{
13091 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
13092}
13093
13094/* Compute the preimage of "map" under the function represented by "pma".
13095 * In other words, plug in "pma" in the domain or range of "map".
13096 * The result is a map that lives in the same space as "map",
13097 * except that the space of type "type" has been replaced by
13098 * the domain space of "pma".
13099 *
13100 * The parameters of "map" and "pma" are assumed to have been aligned.
13101 */
13102static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
13103 __isl_take isl_map *map, enum isl_dim_type type,
13104 __isl_take isl_pw_multi_aff *pma)
13105{
13106 int i;
13107 isl_map *res;
13108
13109 if (!pma)
13110 goto error;
13111
13112 if (pma->n == 0) {
13113 isl_pw_multi_aff_free(pma);
13114 res = isl_map_empty(isl_map_get_space(map));
13115 isl_map_free(map);
13116 return res;
13117 }
13118
13119 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
13120 isl_multi_aff_copy(pma->p[0].maff));
13121 if (type == isl_dim_in)
13122 res = isl_map_intersect_domain(res,
13123 isl_map_copy(pma->p[0].set));
13124 else
13125 res = isl_map_intersect_range(res,
13126 isl_map_copy(pma->p[0].set));
13127
13128 for (i = 1; i < pma->n; ++i) {
13129 isl_map *res_i;
13130
13131 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
13132 isl_multi_aff_copy(pma->p[i].maff));
13133 if (type == isl_dim_in)
13134 res_i = isl_map_intersect_domain(res_i,
13135 isl_map_copy(pma->p[i].set));
13136 else
13137 res_i = isl_map_intersect_range(res_i,
13138 isl_map_copy(pma->p[i].set));
13139 res = isl_map_union(res, res_i);
13140 }
13141
13142 isl_pw_multi_aff_free(pma);
13143 isl_map_free(map);
13144 return res;
13145error:
13146 isl_pw_multi_aff_free(pma);
13147 isl_map_free(map);
13148 return NULL;
13149}
13150
13151/* Compute the preimage of "map" under the function represented by "pma".
13152 * In other words, plug in "pma" in the domain or range of "map".
13153 * The result is a map that lives in the same space as "map",
13154 * except that the space of type "type" has been replaced by
13155 * the domain space of "pma".
13156 */
13157__isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
13158 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
13159{
Tobias Grossere5671e52017-03-10 09:17:55 +000013160 isl_bool aligned;
13161
Tobias Grosser52a25232015-02-04 20:55:43 +000013162 if (!map || !pma)
13163 goto error;
13164
Tobias Grossere5671e52017-03-10 09:17:55 +000013165 aligned = isl_map_space_has_equal_params(map, pma->dim);
13166 if (aligned < 0)
13167 goto error;
13168 if (aligned)
Tobias Grosser52a25232015-02-04 20:55:43 +000013169 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13170
Tobias Grossere5671e52017-03-10 09:17:55 +000013171 if (isl_map_check_named_params(map) < 0)
13172 goto error;
13173 if (!isl_space_has_named_params(pma->dim))
Tobias Grosser52a25232015-02-04 20:55:43 +000013174 isl_die(map->ctx, isl_error_invalid,
13175 "unaligned unnamed parameters", goto error);
13176 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
13177 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
13178
13179 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13180error:
13181 isl_pw_multi_aff_free(pma);
13182 return isl_map_free(map);
13183}
13184
13185/* Compute the preimage of "set" under the function represented by "pma".
13186 * In other words, plug in "pma" in "set". The result is a set
13187 * that lives in the domain space of "pma".
13188 */
13189__isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
13190 __isl_take isl_pw_multi_aff *pma)
13191{
13192 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
13193}
13194
13195/* Compute the preimage of the domain of "map" under the function
13196 * represented by "pma".
13197 * In other words, plug in "pma" in the domain of "map".
13198 * The result is a map that lives in the same space as "map",
13199 * except that domain space has been replaced by the domain space of "pma".
13200 */
13201__isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
13202 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13203{
13204 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
13205}
13206
13207/* Compute the preimage of the range of "map" under the function
13208 * represented by "pma".
13209 * In other words, plug in "pma" in the range of "map".
13210 * The result is a map that lives in the same space as "map",
13211 * except that range space has been replaced by the domain space of "pma".
13212 */
13213__isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
13214 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13215{
13216 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
13217}
13218
13219/* Compute the preimage of "map" under the function represented by "mpa".
13220 * In other words, plug in "mpa" in the domain or range of "map".
13221 * The result is a map that lives in the same space as "map",
13222 * except that the space of type "type" has been replaced by
13223 * the domain space of "mpa".
13224 *
13225 * If the map does not involve any constraints that refer to the
13226 * dimensions of the substituted space, then the only possible
13227 * effect of "mpa" on the map is to map the space to a different space.
13228 * We create a separate isl_multi_aff to effectuate this change
13229 * in order to avoid spurious splitting of the map along the pieces
13230 * of "mpa".
13231 */
13232__isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
13233 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
13234{
13235 int n;
13236 isl_pw_multi_aff *pma;
13237
13238 if (!map || !mpa)
13239 goto error;
13240
13241 n = isl_map_dim(map, type);
13242 if (!isl_map_involves_dims(map, type, 0, n)) {
13243 isl_space *space;
13244 isl_multi_aff *ma;
13245
13246 space = isl_multi_pw_aff_get_space(mpa);
13247 isl_multi_pw_aff_free(mpa);
13248 ma = isl_multi_aff_zero(space);
13249 return isl_map_preimage_multi_aff(map, type, ma);
13250 }
13251
13252 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
13253 return isl_map_preimage_pw_multi_aff(map, type, pma);
13254error:
13255 isl_map_free(map);
13256 isl_multi_pw_aff_free(mpa);
13257 return NULL;
13258}
13259
13260/* Compute the preimage of "map" under the function represented by "mpa".
13261 * In other words, plug in "mpa" in the domain "map".
13262 * The result is a map that lives in the same space as "map",
13263 * except that domain space has been replaced by the domain space of "mpa".
13264 */
13265__isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
13266 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
13267{
13268 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
13269}
13270
13271/* Compute the preimage of "set" by the function represented by "mpa".
13272 * In other words, plug in "mpa" in "set".
13273 */
13274__isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
13275 __isl_take isl_multi_pw_aff *mpa)
13276{
13277 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);
13278}
Michael Kruse1b8eb412016-12-06 14:37:39 +000013279
Tobias Grosser94e53712016-12-31 07:46:11 +000013280/* Are the "n" "coefficients" starting at "first" of the integer division
13281 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
13282 * to each other?
13283 * The "coefficient" at position 0 is the denominator.
13284 * The "coefficient" at position 1 is the constant term.
13285 */
13286isl_bool isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map *bmap1,
13287 int pos1, __isl_keep isl_basic_map *bmap2, int pos2,
13288 unsigned first, unsigned n)
13289{
13290 if (isl_basic_map_check_range(bmap1, isl_dim_div, pos1, 1) < 0)
13291 return isl_bool_error;
13292 if (isl_basic_map_check_range(bmap2, isl_dim_div, pos2, 1) < 0)
13293 return isl_bool_error;
13294 return isl_seq_eq(bmap1->div[pos1] + first,
13295 bmap2->div[pos2] + first, n);
13296}
13297
13298/* Are the integer division expressions at position "pos1" in "bmap1" and
13299 * "pos2" in "bmap2" equal to each other, except that the constant terms
13300 * are different?
13301 */
13302isl_bool isl_basic_map_equal_div_expr_except_constant(
13303 __isl_keep isl_basic_map *bmap1, int pos1,
13304 __isl_keep isl_basic_map *bmap2, int pos2)
13305{
13306 isl_bool equal;
13307 unsigned total;
13308
13309 if (!bmap1 || !bmap2)
13310 return isl_bool_error;
13311 total = isl_basic_map_total_dim(bmap1);
13312 if (total != isl_basic_map_total_dim(bmap2))
13313 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
13314 "incomparable div expressions", return isl_bool_error);
13315 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13316 0, 1);
13317 if (equal < 0 || !equal)
13318 return equal;
13319 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13320 1, 1);
13321 if (equal < 0 || equal)
13322 return isl_bool_not(equal);
13323 return isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13324 2, total);
13325}
13326
13327/* Replace the numerator of the constant term of the integer division
13328 * expression at position "div" in "bmap" by "value".
13329 * The caller guarantees that this does not change the meaning
13330 * of the input.
13331 */
13332__isl_give isl_basic_map *isl_basic_map_set_div_expr_constant_num_si_inplace(
13333 __isl_take isl_basic_map *bmap, int div, int value)
13334{
13335 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
13336 return isl_basic_map_free(bmap);
13337
13338 isl_int_set_si(bmap->div[div][1], value);
13339
13340 return bmap;
13341}
13342
Michael Kruse1b8eb412016-12-06 14:37:39 +000013343/* Is the point "inner" internal to inequality constraint "ineq"
13344 * of "bset"?
13345 * The point is considered to be internal to the inequality constraint,
13346 * if it strictly lies on the positive side of the inequality constraint,
13347 * or if it lies on the constraint and the constraint is lexico-positive.
13348 */
13349static isl_bool is_internal(__isl_keep isl_vec *inner,
13350 __isl_keep isl_basic_set *bset, int ineq)
13351{
13352 isl_ctx *ctx;
13353 int pos;
13354 unsigned total;
13355
13356 if (!inner || !bset)
13357 return isl_bool_error;
13358
13359 ctx = isl_basic_set_get_ctx(bset);
13360 isl_seq_inner_product(inner->el, bset->ineq[ineq], inner->size,
13361 &ctx->normalize_gcd);
13362 if (!isl_int_is_zero(ctx->normalize_gcd))
13363 return isl_int_is_nonneg(ctx->normalize_gcd);
13364
13365 total = isl_basic_set_dim(bset, isl_dim_all);
13366 pos = isl_seq_first_non_zero(bset->ineq[ineq] + 1, total);
13367 return isl_int_is_pos(bset->ineq[ineq][1 + pos]);
13368}
13369
13370/* Tighten the inequality constraints of "bset" that are outward with respect
13371 * to the point "vec".
13372 * That is, tighten the constraints that are not satisfied by "vec".
13373 *
13374 * "vec" is a point internal to some superset S of "bset" that is used
13375 * to make the subsets of S disjoint, by tightening one half of the constraints
13376 * that separate two subsets. In particular, the constraints of S
13377 * are all satisfied by "vec" and should not be tightened.
13378 * Of the internal constraints, those that have "vec" on the outside
13379 * are tightened. The shared facet is included in the adjacent subset
13380 * with the opposite constraint.
13381 * For constraints that saturate "vec", this criterion cannot be used
13382 * to determine which of the two sides should be tightened.
13383 * Instead, the sign of the first non-zero coefficient is used
13384 * to make this choice. Note that this second criterion is never used
13385 * on the constraints of S since "vec" is interior to "S".
13386 */
13387__isl_give isl_basic_set *isl_basic_set_tighten_outward(
13388 __isl_take isl_basic_set *bset, __isl_keep isl_vec *vec)
13389{
13390 int j;
13391
13392 bset = isl_basic_set_cow(bset);
13393 if (!bset)
13394 return NULL;
13395 for (j = 0; j < bset->n_ineq; ++j) {
13396 isl_bool internal;
13397
13398 internal = is_internal(vec, bset, j);
13399 if (internal < 0)
13400 return isl_basic_set_free(bset);
13401 if (internal)
13402 continue;
13403 isl_int_sub_ui(bset->ineq[j][0], bset->ineq[j][0], 1);
13404 }
13405
13406 return bset;
13407}
Tobias Grosser0d622a42017-04-06 03:41:47 +000013408
13409/* Replace the variables x of type "type" starting at "first" in "bmap"
13410 * by x' with x = M x' with M the matrix trans.
13411 * That is, replace the corresponding coefficients c by c M.
13412 *
13413 * The transformation matrix should be a square matrix.
13414 */
13415__isl_give isl_basic_map *isl_basic_map_transform_dims(
13416 __isl_take isl_basic_map *bmap, enum isl_dim_type type, unsigned first,
13417 __isl_take isl_mat *trans)
13418{
13419 unsigned pos;
13420
13421 bmap = isl_basic_map_cow(bmap);
13422 if (!bmap || !trans)
13423 goto error;
13424
13425 if (trans->n_row != trans->n_col)
13426 isl_die(trans->ctx, isl_error_invalid,
13427 "expecting square transformation matrix", goto error);
13428 if (first + trans->n_row > isl_basic_map_dim(bmap, type))
13429 isl_die(trans->ctx, isl_error_invalid,
13430 "oversized transformation matrix", goto error);
13431
13432 pos = isl_basic_map_offset(bmap, type) + first;
13433
13434 if (isl_mat_sub_transform(bmap->eq, bmap->n_eq, pos,
13435 isl_mat_copy(trans)) < 0)
13436 goto error;
13437 if (isl_mat_sub_transform(bmap->ineq, bmap->n_ineq, pos,
13438 isl_mat_copy(trans)) < 0)
13439 goto error;
13440 if (isl_mat_sub_transform(bmap->div, bmap->n_div, 1 + pos,
13441 isl_mat_copy(trans)) < 0)
13442 goto error;
13443
13444 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
13445 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
13446
13447 isl_mat_free(trans);
13448 return bmap;
13449error:
13450 isl_mat_free(trans);
13451 isl_basic_map_free(bmap);
13452 return NULL;
13453}
13454
13455/* Replace the variables x of type "type" starting at "first" in "bset"
13456 * by x' with x = M x' with M the matrix trans.
13457 * That is, replace the corresponding coefficients c by c M.
13458 *
13459 * The transformation matrix should be a square matrix.
13460 */
13461__isl_give isl_basic_set *isl_basic_set_transform_dims(
13462 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned first,
13463 __isl_take isl_mat *trans)
13464{
13465 return isl_basic_map_transform_dims(bset, type, first, trans);
13466}