blob: 08c52144c4976ae57c9e6f42b337e9d574f3a6e9 [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
Michael Krusee0b34f32016-05-04 14:41:36 +00006 * Copyright 2016 Sven Verdoolaege
Tobias Grosser52a25232015-02-04 20:55:43 +00007 *
8 * Use of this software is governed by the MIT license
9 *
10 * Written by Sven Verdoolaege, K.U.Leuven, Departement
11 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
12 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
13 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
14 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
15 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
16 * B.P. 105 - 78153 Le Chesnay, France
17 */
18
19#include <string.h>
20#include <isl_ctx_private.h>
21#include <isl_map_private.h>
22#include <isl_blk.h>
Tobias Grosser3e6070e2015-05-09 09:37:30 +000023#include <isl/constraint.h>
Tobias Grosser52a25232015-02-04 20:55:43 +000024#include "isl_space_private.h"
25#include "isl_equalities.h"
26#include <isl_lp_private.h>
27#include <isl_seq.h>
28#include <isl/set.h>
29#include <isl/map.h>
30#include <isl_reordering.h>
31#include "isl_sample.h"
32#include <isl_sort.h>
33#include "isl_tab.h"
34#include <isl/vec.h>
35#include <isl_mat_private.h>
36#include <isl_vec_private.h>
37#include <isl_dim_map.h>
38#include <isl_local_space_private.h>
39#include <isl_aff_private.h>
40#include <isl_options_private.h>
41#include <isl_morph.h>
42#include <isl_val_private.h>
43#include <isl/deprecated/map_int.h>
44#include <isl/deprecated/set_int.h>
45
46static unsigned n(__isl_keep isl_space *dim, enum isl_dim_type type)
47{
48 switch (type) {
49 case isl_dim_param: return dim->nparam;
50 case isl_dim_in: return dim->n_in;
51 case isl_dim_out: return dim->n_out;
52 case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
53 default: return 0;
54 }
55}
56
57static unsigned pos(__isl_keep isl_space *dim, enum isl_dim_type type)
58{
59 switch (type) {
60 case isl_dim_param: return 1;
61 case isl_dim_in: return 1 + dim->nparam;
62 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
63 default: return 0;
64 }
65}
66
67unsigned isl_basic_map_dim(__isl_keep isl_basic_map *bmap,
68 enum isl_dim_type type)
69{
70 if (!bmap)
71 return 0;
72 switch (type) {
73 case isl_dim_cst: return 1;
74 case isl_dim_param:
75 case isl_dim_in:
76 case isl_dim_out: return isl_space_dim(bmap->dim, type);
77 case isl_dim_div: return bmap->n_div;
78 case isl_dim_all: return isl_basic_map_total_dim(bmap);
79 default: return 0;
80 }
81}
82
83unsigned isl_map_dim(__isl_keep isl_map *map, enum isl_dim_type type)
84{
85 return map ? n(map->dim, type) : 0;
86}
87
88unsigned isl_set_dim(__isl_keep isl_set *set, enum isl_dim_type type)
89{
90 return set ? n(set->dim, type) : 0;
91}
92
93unsigned isl_basic_map_offset(struct isl_basic_map *bmap,
94 enum isl_dim_type type)
95{
96 isl_space *dim = bmap->dim;
97 switch (type) {
98 case isl_dim_cst: return 0;
99 case isl_dim_param: return 1;
100 case isl_dim_in: return 1 + dim->nparam;
101 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
102 case isl_dim_div: return 1 + dim->nparam + dim->n_in + dim->n_out;
103 default: return 0;
104 }
105}
106
107unsigned isl_basic_set_offset(struct isl_basic_set *bset,
108 enum isl_dim_type type)
109{
110 return isl_basic_map_offset(bset, type);
111}
112
113static unsigned map_offset(struct isl_map *map, enum isl_dim_type type)
114{
115 return pos(map->dim, type);
116}
117
118unsigned isl_basic_set_dim(__isl_keep isl_basic_set *bset,
119 enum isl_dim_type type)
120{
121 return isl_basic_map_dim(bset, type);
122}
123
124unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set *bset)
125{
126 return isl_basic_set_dim(bset, isl_dim_set);
127}
128
129unsigned isl_basic_set_n_param(__isl_keep isl_basic_set *bset)
130{
131 return isl_basic_set_dim(bset, isl_dim_param);
132}
133
134unsigned isl_basic_set_total_dim(const struct isl_basic_set *bset)
135{
136 if (!bset)
137 return 0;
138 return isl_space_dim(bset->dim, isl_dim_all) + bset->n_div;
139}
140
141unsigned isl_set_n_dim(__isl_keep isl_set *set)
142{
143 return isl_set_dim(set, isl_dim_set);
144}
145
146unsigned isl_set_n_param(__isl_keep isl_set *set)
147{
148 return isl_set_dim(set, isl_dim_param);
149}
150
151unsigned isl_basic_map_n_in(const struct isl_basic_map *bmap)
152{
153 return bmap ? bmap->dim->n_in : 0;
154}
155
156unsigned isl_basic_map_n_out(const struct isl_basic_map *bmap)
157{
158 return bmap ? bmap->dim->n_out : 0;
159}
160
161unsigned isl_basic_map_n_param(const struct isl_basic_map *bmap)
162{
163 return bmap ? bmap->dim->nparam : 0;
164}
165
166unsigned isl_basic_map_n_div(const struct isl_basic_map *bmap)
167{
168 return bmap ? bmap->n_div : 0;
169}
170
171unsigned isl_basic_map_total_dim(const struct isl_basic_map *bmap)
172{
173 return bmap ? isl_space_dim(bmap->dim, isl_dim_all) + bmap->n_div : 0;
174}
175
176unsigned isl_map_n_in(const struct isl_map *map)
177{
178 return map ? map->dim->n_in : 0;
179}
180
181unsigned isl_map_n_out(const struct isl_map *map)
182{
183 return map ? map->dim->n_out : 0;
184}
185
186unsigned isl_map_n_param(const struct isl_map *map)
187{
188 return map ? map->dim->nparam : 0;
189}
190
191int isl_map_compatible_domain(struct isl_map *map, struct isl_set *set)
192{
193 int m;
194 if (!map || !set)
195 return -1;
196 m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
197 if (m < 0 || !m)
198 return m;
199 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
200 set->dim, isl_dim_set);
201}
202
203int isl_basic_map_compatible_domain(struct isl_basic_map *bmap,
204 struct isl_basic_set *bset)
205{
206 int m;
207 if (!bmap || !bset)
208 return -1;
209 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
210 if (m < 0 || !m)
211 return m;
212 return isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
213 bset->dim, isl_dim_set);
214}
215
216int isl_map_compatible_range(__isl_keep isl_map *map, __isl_keep isl_set *set)
217{
218 int m;
219 if (!map || !set)
220 return -1;
221 m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
222 if (m < 0 || !m)
223 return m;
224 return isl_space_tuple_is_equal(map->dim, isl_dim_out,
225 set->dim, isl_dim_set);
226}
227
228int isl_basic_map_compatible_range(struct isl_basic_map *bmap,
229 struct isl_basic_set *bset)
230{
231 int m;
232 if (!bmap || !bset)
233 return -1;
234 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
235 if (m < 0 || !m)
236 return m;
237 return isl_space_tuple_is_equal(bmap->dim, isl_dim_out,
238 bset->dim, isl_dim_set);
239}
240
241isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
242{
243 return bmap ? bmap->ctx : NULL;
244}
245
246isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
247{
248 return bset ? bset->ctx : NULL;
249}
250
251isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
252{
253 return map ? map->ctx : NULL;
254}
255
256isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
257{
258 return set ? set->ctx : NULL;
259}
260
261__isl_give isl_space *isl_basic_map_get_space(__isl_keep isl_basic_map *bmap)
262{
263 if (!bmap)
264 return NULL;
265 return isl_space_copy(bmap->dim);
266}
267
268__isl_give isl_space *isl_basic_set_get_space(__isl_keep isl_basic_set *bset)
269{
270 if (!bset)
271 return NULL;
272 return isl_space_copy(bset->dim);
273}
274
275/* Extract the divs in "bmap" as a matrix.
276 */
277__isl_give isl_mat *isl_basic_map_get_divs(__isl_keep isl_basic_map *bmap)
278{
279 int i;
280 isl_ctx *ctx;
281 isl_mat *div;
282 unsigned total;
283 unsigned cols;
284
285 if (!bmap)
286 return NULL;
287
288 ctx = isl_basic_map_get_ctx(bmap);
289 total = isl_space_dim(bmap->dim, isl_dim_all);
290 cols = 1 + 1 + total + bmap->n_div;
291 div = isl_mat_alloc(ctx, bmap->n_div, cols);
292 if (!div)
293 return NULL;
294
295 for (i = 0; i < bmap->n_div; ++i)
296 isl_seq_cpy(div->row[i], bmap->div[i], cols);
297
298 return div;
299}
300
301/* Extract the divs in "bset" as a matrix.
302 */
303__isl_give isl_mat *isl_basic_set_get_divs(__isl_keep isl_basic_set *bset)
304{
305 return isl_basic_map_get_divs(bset);
306}
307
308__isl_give isl_local_space *isl_basic_map_get_local_space(
309 __isl_keep isl_basic_map *bmap)
310{
311 isl_mat *div;
312
313 if (!bmap)
314 return NULL;
315
316 div = isl_basic_map_get_divs(bmap);
317 return isl_local_space_alloc_div(isl_space_copy(bmap->dim), div);
318}
319
320__isl_give isl_local_space *isl_basic_set_get_local_space(
321 __isl_keep isl_basic_set *bset)
322{
323 return isl_basic_map_get_local_space(bset);
324}
325
Michael Kruse959a8dc2016-01-15 15:54:45 +0000326/* For each known div d = floor(f/m), add the constraints
327 *
328 * f - m d >= 0
329 * -(f-(n-1)) + m d >= 0
330 *
331 * Do not finalize the result.
332 */
333static __isl_give isl_basic_map *add_known_div_constraints(
334 __isl_take isl_basic_map *bmap)
335{
336 int i;
337 unsigned n_div;
338
339 if (!bmap)
340 return NULL;
341 n_div = isl_basic_map_dim(bmap, isl_dim_div);
342 if (n_div == 0)
343 return bmap;
344 bmap = isl_basic_map_cow(bmap);
345 bmap = isl_basic_map_extend_constraints(bmap, 0, 2 * n_div);
346 if (!bmap)
347 return NULL;
348 for (i = 0; i < n_div; ++i) {
349 if (isl_int_is_zero(bmap->div[i][0]))
350 continue;
351 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
352 return isl_basic_map_free(bmap);
353 }
354
355 return bmap;
356}
357
Tobias Grosser52a25232015-02-04 20:55:43 +0000358__isl_give isl_basic_map *isl_basic_map_from_local_space(
359 __isl_take isl_local_space *ls)
360{
361 int i;
362 int n_div;
363 isl_basic_map *bmap;
364
365 if (!ls)
366 return NULL;
367
368 n_div = isl_local_space_dim(ls, isl_dim_div);
369 bmap = isl_basic_map_alloc_space(isl_local_space_get_space(ls),
370 n_div, 0, 2 * n_div);
371
372 for (i = 0; i < n_div; ++i)
373 if (isl_basic_map_alloc_div(bmap) < 0)
374 goto error;
375
Michael Kruse959a8dc2016-01-15 15:54:45 +0000376 for (i = 0; i < n_div; ++i)
Tobias Grosser52a25232015-02-04 20:55:43 +0000377 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
Michael Kruse959a8dc2016-01-15 15:54:45 +0000378 bmap = add_known_div_constraints(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +0000379
380 isl_local_space_free(ls);
381 return bmap;
382error:
383 isl_local_space_free(ls);
384 isl_basic_map_free(bmap);
385 return NULL;
386}
387
388__isl_give isl_basic_set *isl_basic_set_from_local_space(
389 __isl_take isl_local_space *ls)
390{
391 return isl_basic_map_from_local_space(ls);
392}
393
394__isl_give isl_space *isl_map_get_space(__isl_keep isl_map *map)
395{
396 if (!map)
397 return NULL;
398 return isl_space_copy(map->dim);
399}
400
401__isl_give isl_space *isl_set_get_space(__isl_keep isl_set *set)
402{
403 if (!set)
404 return NULL;
405 return isl_space_copy(set->dim);
406}
407
408__isl_give isl_basic_map *isl_basic_map_set_tuple_name(
409 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
410{
411 bmap = isl_basic_map_cow(bmap);
412 if (!bmap)
413 return NULL;
414 bmap->dim = isl_space_set_tuple_name(bmap->dim, type, s);
415 if (!bmap->dim)
416 goto error;
417 bmap = isl_basic_map_finalize(bmap);
418 return bmap;
419error:
420 isl_basic_map_free(bmap);
421 return NULL;
422}
423
424__isl_give isl_basic_set *isl_basic_set_set_tuple_name(
425 __isl_take isl_basic_set *bset, const char *s)
426{
427 return isl_basic_map_set_tuple_name(bset, isl_dim_set, s);
428}
429
430const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
431 enum isl_dim_type type)
432{
433 return bmap ? isl_space_get_tuple_name(bmap->dim, type) : NULL;
434}
435
436__isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
437 enum isl_dim_type type, const char *s)
438{
439 int i;
440
441 map = isl_map_cow(map);
442 if (!map)
443 return NULL;
444
445 map->dim = isl_space_set_tuple_name(map->dim, type, s);
446 if (!map->dim)
447 goto error;
448
449 for (i = 0; i < map->n; ++i) {
450 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
451 if (!map->p[i])
452 goto error;
453 }
454
455 return map;
456error:
457 isl_map_free(map);
458 return NULL;
459}
460
461/* Replace the identifier of the tuple of type "type" by "id".
462 */
463__isl_give isl_basic_map *isl_basic_map_set_tuple_id(
464 __isl_take isl_basic_map *bmap,
465 enum isl_dim_type type, __isl_take isl_id *id)
466{
467 bmap = isl_basic_map_cow(bmap);
468 if (!bmap)
469 goto error;
470 bmap->dim = isl_space_set_tuple_id(bmap->dim, type, id);
471 if (!bmap->dim)
472 return isl_basic_map_free(bmap);
473 bmap = isl_basic_map_finalize(bmap);
474 return bmap;
475error:
476 isl_id_free(id);
477 return NULL;
478}
479
480/* Replace the identifier of the tuple by "id".
481 */
482__isl_give isl_basic_set *isl_basic_set_set_tuple_id(
483 __isl_take isl_basic_set *bset, __isl_take isl_id *id)
484{
485 return isl_basic_map_set_tuple_id(bset, isl_dim_set, id);
486}
487
488/* Does the input or output tuple have a name?
489 */
Tobias Grosserb2f39922015-05-28 13:32:11 +0000490isl_bool isl_map_has_tuple_name(__isl_keep isl_map *map, enum isl_dim_type type)
Tobias Grosser52a25232015-02-04 20:55:43 +0000491{
Tobias Grosserb2f39922015-05-28 13:32:11 +0000492 return map ? isl_space_has_tuple_name(map->dim, type) : isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +0000493}
494
495const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
496 enum isl_dim_type type)
497{
498 return map ? isl_space_get_tuple_name(map->dim, type) : NULL;
499}
500
501__isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
502 const char *s)
503{
504 return (isl_set *)isl_map_set_tuple_name((isl_map *)set, isl_dim_set, s);
505}
506
507__isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map,
508 enum isl_dim_type type, __isl_take isl_id *id)
509{
510 map = isl_map_cow(map);
511 if (!map)
512 goto error;
513
514 map->dim = isl_space_set_tuple_id(map->dim, type, id);
515
516 return isl_map_reset_space(map, isl_space_copy(map->dim));
517error:
518 isl_id_free(id);
519 return NULL;
520}
521
522__isl_give isl_set *isl_set_set_tuple_id(__isl_take isl_set *set,
523 __isl_take isl_id *id)
524{
525 return isl_map_set_tuple_id(set, isl_dim_set, id);
526}
527
528__isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map,
529 enum isl_dim_type type)
530{
531 map = isl_map_cow(map);
532 if (!map)
533 return NULL;
534
535 map->dim = isl_space_reset_tuple_id(map->dim, type);
536
537 return isl_map_reset_space(map, isl_space_copy(map->dim));
538}
539
540__isl_give isl_set *isl_set_reset_tuple_id(__isl_take isl_set *set)
541{
542 return isl_map_reset_tuple_id(set, isl_dim_set);
543}
544
Tobias Grosserb2f39922015-05-28 13:32:11 +0000545isl_bool isl_map_has_tuple_id(__isl_keep isl_map *map, enum isl_dim_type type)
Tobias Grosser52a25232015-02-04 20:55:43 +0000546{
Tobias Grosserb2f39922015-05-28 13:32:11 +0000547 return map ? isl_space_has_tuple_id(map->dim, type) : isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +0000548}
549
550__isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map,
551 enum isl_dim_type type)
552{
553 return map ? isl_space_get_tuple_id(map->dim, type) : NULL;
554}
555
Tobias Grosserb2f39922015-05-28 13:32:11 +0000556isl_bool isl_set_has_tuple_id(__isl_keep isl_set *set)
Tobias Grosser52a25232015-02-04 20:55:43 +0000557{
558 return isl_map_has_tuple_id(set, isl_dim_set);
559}
560
561__isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_set *set)
562{
563 return isl_map_get_tuple_id(set, isl_dim_set);
564}
565
566/* Does the set tuple have a name?
567 */
Tobias Grosserb2f39922015-05-28 13:32:11 +0000568isl_bool isl_set_has_tuple_name(__isl_keep isl_set *set)
Tobias Grosser52a25232015-02-04 20:55:43 +0000569{
Tobias Grosserb2f39922015-05-28 13:32:11 +0000570 if (!set)
571 return isl_bool_error;
572 return isl_space_has_tuple_name(set->dim, isl_dim_set);
Tobias Grosser52a25232015-02-04 20:55:43 +0000573}
574
575
576const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
577{
578 return bset ? isl_space_get_tuple_name(bset->dim, isl_dim_set) : NULL;
579}
580
581const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
582{
583 return set ? isl_space_get_tuple_name(set->dim, isl_dim_set) : NULL;
584}
585
586const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
587 enum isl_dim_type type, unsigned pos)
588{
589 return bmap ? isl_space_get_dim_name(bmap->dim, type, pos) : NULL;
590}
591
592const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
593 enum isl_dim_type type, unsigned pos)
594{
595 return bset ? isl_space_get_dim_name(bset->dim, type, pos) : NULL;
596}
597
598/* Does the given dimension have a name?
599 */
Tobias Grosserb2f39922015-05-28 13:32:11 +0000600isl_bool isl_map_has_dim_name(__isl_keep isl_map *map,
Tobias Grosser52a25232015-02-04 20:55:43 +0000601 enum isl_dim_type type, unsigned pos)
602{
Tobias Grosserb2f39922015-05-28 13:32:11 +0000603 if (!map)
604 return isl_bool_error;
605 return isl_space_has_dim_name(map->dim, type, pos);
Tobias Grosser52a25232015-02-04 20:55:43 +0000606}
607
608const char *isl_map_get_dim_name(__isl_keep isl_map *map,
609 enum isl_dim_type type, unsigned pos)
610{
611 return map ? isl_space_get_dim_name(map->dim, type, pos) : NULL;
612}
613
614const char *isl_set_get_dim_name(__isl_keep isl_set *set,
615 enum isl_dim_type type, unsigned pos)
616{
617 return set ? isl_space_get_dim_name(set->dim, type, pos) : NULL;
618}
619
620/* Does the given dimension have a name?
621 */
Tobias Grosserb2f39922015-05-28 13:32:11 +0000622isl_bool isl_set_has_dim_name(__isl_keep isl_set *set,
Tobias Grosser52a25232015-02-04 20:55:43 +0000623 enum isl_dim_type type, unsigned pos)
624{
Tobias Grosserb2f39922015-05-28 13:32:11 +0000625 if (!set)
626 return isl_bool_error;
627 return isl_space_has_dim_name(set->dim, type, pos);
Tobias Grosser52a25232015-02-04 20:55:43 +0000628}
629
630__isl_give isl_basic_map *isl_basic_map_set_dim_name(
631 __isl_take isl_basic_map *bmap,
632 enum isl_dim_type type, unsigned pos, const char *s)
633{
634 bmap = isl_basic_map_cow(bmap);
635 if (!bmap)
636 return NULL;
637 bmap->dim = isl_space_set_dim_name(bmap->dim, type, pos, s);
638 if (!bmap->dim)
639 goto error;
640 return isl_basic_map_finalize(bmap);
641error:
642 isl_basic_map_free(bmap);
643 return NULL;
644}
645
646__isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
647 enum isl_dim_type type, unsigned pos, const char *s)
648{
649 int i;
650
651 map = isl_map_cow(map);
652 if (!map)
653 return NULL;
654
655 map->dim = isl_space_set_dim_name(map->dim, type, pos, s);
656 if (!map->dim)
657 goto error;
658
659 for (i = 0; i < map->n; ++i) {
660 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
661 if (!map->p[i])
662 goto error;
663 }
664
665 return map;
666error:
667 isl_map_free(map);
668 return NULL;
669}
670
671__isl_give isl_basic_set *isl_basic_set_set_dim_name(
672 __isl_take isl_basic_set *bset,
673 enum isl_dim_type type, unsigned pos, const char *s)
674{
675 return (isl_basic_set *)isl_basic_map_set_dim_name(
676 (isl_basic_map *)bset, type, pos, s);
677}
678
679__isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
680 enum isl_dim_type type, unsigned pos, const char *s)
681{
682 return (isl_set *)isl_map_set_dim_name((isl_map *)set, type, pos, s);
683}
684
Tobias Grosserb2f39922015-05-28 13:32:11 +0000685isl_bool isl_basic_map_has_dim_id(__isl_keep isl_basic_map *bmap,
Tobias Grosser52a25232015-02-04 20:55:43 +0000686 enum isl_dim_type type, unsigned pos)
687{
Tobias Grosserb2f39922015-05-28 13:32:11 +0000688 if (!bmap)
689 return isl_bool_error;
690 return isl_space_has_dim_id(bmap->dim, type, pos);
Tobias Grosser52a25232015-02-04 20:55:43 +0000691}
692
693__isl_give isl_id *isl_basic_set_get_dim_id(__isl_keep isl_basic_set *bset,
694 enum isl_dim_type type, unsigned pos)
695{
696 return bset ? isl_space_get_dim_id(bset->dim, type, pos) : NULL;
697}
698
Tobias Grosserb2f39922015-05-28 13:32:11 +0000699isl_bool isl_map_has_dim_id(__isl_keep isl_map *map,
Tobias Grosser52a25232015-02-04 20:55:43 +0000700 enum isl_dim_type type, unsigned pos)
701{
Tobias Grosserb2f39922015-05-28 13:32:11 +0000702 return map ? isl_space_has_dim_id(map->dim, type, pos) : isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +0000703}
704
705__isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
706 enum isl_dim_type type, unsigned pos)
707{
708 return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL;
709}
710
Tobias Grosserb2f39922015-05-28 13:32:11 +0000711isl_bool isl_set_has_dim_id(__isl_keep isl_set *set,
Tobias Grosser52a25232015-02-04 20:55:43 +0000712 enum isl_dim_type type, unsigned pos)
713{
714 return isl_map_has_dim_id(set, type, pos);
715}
716
717__isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set,
718 enum isl_dim_type type, unsigned pos)
719{
720 return isl_map_get_dim_id(set, type, pos);
721}
722
723__isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
724 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
725{
726 map = isl_map_cow(map);
727 if (!map)
728 goto error;
729
730 map->dim = isl_space_set_dim_id(map->dim, type, pos, id);
731
732 return isl_map_reset_space(map, isl_space_copy(map->dim));
733error:
734 isl_id_free(id);
735 return NULL;
736}
737
738__isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set,
739 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
740{
741 return isl_map_set_dim_id(set, type, pos, id);
742}
743
744int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
745 __isl_keep isl_id *id)
746{
747 if (!map)
748 return -1;
749 return isl_space_find_dim_by_id(map->dim, type, id);
750}
751
752int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type,
753 __isl_keep isl_id *id)
754{
755 return isl_map_find_dim_by_id(set, type, id);
756}
757
758/* Return the position of the dimension of the given type and name
759 * in "bmap".
760 * Return -1 if no such dimension can be found.
761 */
762int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map *bmap,
763 enum isl_dim_type type, const char *name)
764{
765 if (!bmap)
766 return -1;
767 return isl_space_find_dim_by_name(bmap->dim, type, name);
768}
769
770int isl_map_find_dim_by_name(__isl_keep isl_map *map, enum isl_dim_type type,
771 const char *name)
772{
773 if (!map)
774 return -1;
775 return isl_space_find_dim_by_name(map->dim, type, name);
776}
777
778int isl_set_find_dim_by_name(__isl_keep isl_set *set, enum isl_dim_type type,
779 const char *name)
780{
781 return isl_map_find_dim_by_name(set, type, name);
782}
783
784/* Reset the user pointer on all identifiers of parameters and tuples
785 * of the space of "map".
786 */
787__isl_give isl_map *isl_map_reset_user(__isl_take isl_map *map)
788{
789 isl_space *space;
790
791 space = isl_map_get_space(map);
792 space = isl_space_reset_user(space);
793 map = isl_map_reset_space(map, space);
794
795 return map;
796}
797
798/* Reset the user pointer on all identifiers of parameters and tuples
799 * of the space of "set".
800 */
801__isl_give isl_set *isl_set_reset_user(__isl_take isl_set *set)
802{
803 return isl_map_reset_user(set);
804}
805
806int isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
807{
808 if (!bmap)
809 return -1;
810 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
811}
812
813int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
814{
815 return isl_basic_map_is_rational(bset);
816}
817
818/* Does "bmap" contain any rational points?
819 *
820 * If "bmap" has an equality for each dimension, equating the dimension
821 * to an integer constant, then it has no rational points, even if it
822 * is marked as rational.
823 */
824int isl_basic_map_has_rational(__isl_keep isl_basic_map *bmap)
825{
826 int has_rational = 1;
827 unsigned total;
828
829 if (!bmap)
830 return -1;
831 if (isl_basic_map_plain_is_empty(bmap))
832 return 0;
833 if (!isl_basic_map_is_rational(bmap))
834 return 0;
835 bmap = isl_basic_map_copy(bmap);
836 bmap = isl_basic_map_implicit_equalities(bmap);
837 if (!bmap)
838 return -1;
839 total = isl_basic_map_total_dim(bmap);
840 if (bmap->n_eq == total) {
841 int i, j;
842 for (i = 0; i < bmap->n_eq; ++i) {
843 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
844 if (j < 0)
845 break;
846 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
847 !isl_int_is_negone(bmap->eq[i][1 + j]))
848 break;
849 j = isl_seq_first_non_zero(bmap->eq[i] + 1 + j + 1,
850 total - j - 1);
851 if (j >= 0)
852 break;
853 }
854 if (i == bmap->n_eq)
855 has_rational = 0;
856 }
857 isl_basic_map_free(bmap);
858
859 return has_rational;
860}
861
862/* Does "map" contain any rational points?
863 */
864int isl_map_has_rational(__isl_keep isl_map *map)
865{
866 int i;
867 int has_rational;
868
869 if (!map)
870 return -1;
871 for (i = 0; i < map->n; ++i) {
872 has_rational = isl_basic_map_has_rational(map->p[i]);
873 if (has_rational < 0)
874 return -1;
875 if (has_rational)
876 return 1;
877 }
878 return 0;
879}
880
881/* Does "set" contain any rational points?
882 */
883int isl_set_has_rational(__isl_keep isl_set *set)
884{
885 return isl_map_has_rational(set);
886}
887
888/* Is this basic set a parameter domain?
889 */
890int isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
891{
892 if (!bset)
893 return -1;
894 return isl_space_is_params(bset->dim);
895}
896
897/* Is this set a parameter domain?
898 */
Tobias Grosserb2f39922015-05-28 13:32:11 +0000899isl_bool isl_set_is_params(__isl_keep isl_set *set)
Tobias Grosser52a25232015-02-04 20:55:43 +0000900{
901 if (!set)
Tobias Grosserb2f39922015-05-28 13:32:11 +0000902 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +0000903 return isl_space_is_params(set->dim);
904}
905
906/* Is this map actually a parameter domain?
907 * Users should never call this function. Outside of isl,
908 * a map can never be a parameter domain.
909 */
910int isl_map_is_params(__isl_keep isl_map *map)
911{
912 if (!map)
913 return -1;
914 return isl_space_is_params(map->dim);
915}
916
917static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
918 struct isl_basic_map *bmap, unsigned extra,
919 unsigned n_eq, unsigned n_ineq)
920{
921 int i;
922 size_t row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + extra;
923
924 bmap->ctx = ctx;
925 isl_ctx_ref(ctx);
926
927 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
928 if (isl_blk_is_error(bmap->block))
929 goto error;
930
931 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
932 if ((n_ineq + n_eq) && !bmap->ineq)
933 goto error;
934
935 if (extra == 0) {
936 bmap->block2 = isl_blk_empty();
937 bmap->div = NULL;
938 } else {
939 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
940 if (isl_blk_is_error(bmap->block2))
941 goto error;
942
943 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
944 if (!bmap->div)
945 goto error;
946 }
947
948 for (i = 0; i < n_ineq + n_eq; ++i)
949 bmap->ineq[i] = bmap->block.data + i * row_size;
950
951 for (i = 0; i < extra; ++i)
952 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
953
954 bmap->ref = 1;
955 bmap->flags = 0;
956 bmap->c_size = n_eq + n_ineq;
957 bmap->eq = bmap->ineq + n_ineq;
958 bmap->extra = extra;
959 bmap->n_eq = 0;
960 bmap->n_ineq = 0;
961 bmap->n_div = 0;
962 bmap->sample = NULL;
963
964 return bmap;
965error:
966 isl_basic_map_free(bmap);
967 return NULL;
968}
969
970struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
971 unsigned nparam, unsigned dim, unsigned extra,
972 unsigned n_eq, unsigned n_ineq)
973{
974 struct isl_basic_map *bmap;
975 isl_space *space;
976
977 space = isl_space_set_alloc(ctx, nparam, dim);
978 if (!space)
979 return NULL;
980
981 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
982 return (struct isl_basic_set *)bmap;
983}
984
985struct isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *dim,
986 unsigned extra, unsigned n_eq, unsigned n_ineq)
987{
988 struct isl_basic_map *bmap;
989 if (!dim)
990 return NULL;
991 isl_assert(dim->ctx, dim->n_in == 0, goto error);
992 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
993 return (struct isl_basic_set *)bmap;
994error:
995 isl_space_free(dim);
996 return NULL;
997}
998
999struct isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *dim,
1000 unsigned extra, unsigned n_eq, unsigned n_ineq)
1001{
1002 struct isl_basic_map *bmap;
1003
1004 if (!dim)
1005 return NULL;
1006 bmap = isl_calloc_type(dim->ctx, struct isl_basic_map);
1007 if (!bmap)
1008 goto error;
1009 bmap->dim = dim;
1010
1011 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
1012error:
1013 isl_space_free(dim);
1014 return NULL;
1015}
1016
1017struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
1018 unsigned nparam, unsigned in, unsigned out, unsigned extra,
1019 unsigned n_eq, unsigned n_ineq)
1020{
1021 struct isl_basic_map *bmap;
1022 isl_space *dim;
1023
1024 dim = isl_space_alloc(ctx, nparam, in, out);
1025 if (!dim)
1026 return NULL;
1027
1028 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1029 return bmap;
1030}
1031
1032static void dup_constraints(
1033 struct isl_basic_map *dst, struct isl_basic_map *src)
1034{
1035 int i;
1036 unsigned total = isl_basic_map_total_dim(src);
1037
1038 for (i = 0; i < src->n_eq; ++i) {
1039 int j = isl_basic_map_alloc_equality(dst);
1040 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
1041 }
1042
1043 for (i = 0; i < src->n_ineq; ++i) {
1044 int j = isl_basic_map_alloc_inequality(dst);
1045 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
1046 }
1047
1048 for (i = 0; i < src->n_div; ++i) {
1049 int j = isl_basic_map_alloc_div(dst);
1050 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
1051 }
1052 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
1053}
1054
1055struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
1056{
1057 struct isl_basic_map *dup;
1058
1059 if (!bmap)
1060 return NULL;
1061 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
1062 bmap->n_div, bmap->n_eq, bmap->n_ineq);
1063 if (!dup)
1064 return NULL;
1065 dup_constraints(dup, bmap);
1066 dup->flags = bmap->flags;
1067 dup->sample = isl_vec_copy(bmap->sample);
1068 return dup;
1069}
1070
1071struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
1072{
1073 struct isl_basic_map *dup;
1074
1075 dup = isl_basic_map_dup((struct isl_basic_map *)bset);
1076 return (struct isl_basic_set *)dup;
1077}
1078
1079struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
1080{
1081 if (!bset)
1082 return NULL;
1083
1084 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
1085 bset->ref++;
1086 return bset;
1087 }
1088 return isl_basic_set_dup(bset);
1089}
1090
1091struct isl_set *isl_set_copy(struct isl_set *set)
1092{
1093 if (!set)
1094 return NULL;
1095
1096 set->ref++;
1097 return set;
1098}
1099
1100struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
1101{
1102 if (!bmap)
1103 return NULL;
1104
1105 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
1106 bmap->ref++;
1107 return bmap;
1108 }
1109 bmap = isl_basic_map_dup(bmap);
1110 if (bmap)
1111 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1112 return bmap;
1113}
1114
1115struct isl_map *isl_map_copy(struct isl_map *map)
1116{
1117 if (!map)
1118 return NULL;
1119
1120 map->ref++;
1121 return map;
1122}
1123
1124__isl_null isl_basic_map *isl_basic_map_free(__isl_take isl_basic_map *bmap)
1125{
1126 if (!bmap)
1127 return NULL;
1128
1129 if (--bmap->ref > 0)
1130 return NULL;
1131
1132 isl_ctx_deref(bmap->ctx);
1133 free(bmap->div);
1134 isl_blk_free(bmap->ctx, bmap->block2);
1135 free(bmap->ineq);
1136 isl_blk_free(bmap->ctx, bmap->block);
1137 isl_vec_free(bmap->sample);
1138 isl_space_free(bmap->dim);
1139 free(bmap);
1140
1141 return NULL;
1142}
1143
1144__isl_null isl_basic_set *isl_basic_set_free(__isl_take isl_basic_set *bset)
1145{
1146 return isl_basic_map_free((struct isl_basic_map *)bset);
1147}
1148
1149static int room_for_con(struct isl_basic_map *bmap, unsigned n)
1150{
1151 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
1152}
1153
1154__isl_give isl_map *isl_map_align_params_map_map_and(
1155 __isl_take isl_map *map1, __isl_take isl_map *map2,
1156 __isl_give isl_map *(*fn)(__isl_take isl_map *map1,
1157 __isl_take isl_map *map2))
1158{
1159 if (!map1 || !map2)
1160 goto error;
1161 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
1162 return fn(map1, map2);
1163 if (!isl_space_has_named_params(map1->dim) ||
1164 !isl_space_has_named_params(map2->dim))
1165 isl_die(map1->ctx, isl_error_invalid,
1166 "unaligned unnamed parameters", goto error);
1167 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1168 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1169 return fn(map1, map2);
1170error:
1171 isl_map_free(map1);
1172 isl_map_free(map2);
1173 return NULL;
1174}
1175
Tobias Grosserb2f39922015-05-28 13:32:11 +00001176isl_bool isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1,
Tobias Grosser52a25232015-02-04 20:55:43 +00001177 __isl_keep isl_map *map2,
Tobias Grosserb2f39922015-05-28 13:32:11 +00001178 isl_bool (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
Tobias Grosser52a25232015-02-04 20:55:43 +00001179{
Tobias Grosserb2f39922015-05-28 13:32:11 +00001180 isl_bool r;
Tobias Grosser52a25232015-02-04 20:55:43 +00001181
1182 if (!map1 || !map2)
Tobias Grosserb2f39922015-05-28 13:32:11 +00001183 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00001184 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
1185 return fn(map1, map2);
1186 if (!isl_space_has_named_params(map1->dim) ||
1187 !isl_space_has_named_params(map2->dim))
1188 isl_die(map1->ctx, isl_error_invalid,
Tobias Grosserb2f39922015-05-28 13:32:11 +00001189 "unaligned unnamed parameters", return isl_bool_error);
Tobias Grosser52a25232015-02-04 20:55:43 +00001190 map1 = isl_map_copy(map1);
1191 map2 = isl_map_copy(map2);
1192 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1193 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1194 r = fn(map1, map2);
1195 isl_map_free(map1);
1196 isl_map_free(map2);
1197 return r;
1198}
1199
1200int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
1201{
1202 struct isl_ctx *ctx;
1203 if (!bmap)
1204 return -1;
1205 ctx = bmap->ctx;
1206 isl_assert(ctx, room_for_con(bmap, 1), return -1);
1207 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
1208 return -1);
1209 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1210 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1211 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1212 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1213 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1214 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
1215 isl_int *t;
1216 int j = isl_basic_map_alloc_inequality(bmap);
1217 if (j < 0)
1218 return -1;
1219 t = bmap->ineq[j];
1220 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
1221 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1222 bmap->eq[-1] = t;
1223 bmap->n_eq++;
1224 bmap->n_ineq--;
1225 bmap->eq--;
1226 return 0;
1227 }
1228 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
1229 bmap->extra - bmap->n_div);
1230 return bmap->n_eq++;
1231}
1232
1233int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
1234{
1235 return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
1236}
1237
1238int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
1239{
1240 if (!bmap)
1241 return -1;
1242 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
1243 bmap->n_eq -= n;
1244 return 0;
1245}
1246
1247int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
1248{
1249 return isl_basic_map_free_equality((struct isl_basic_map *)bset, n);
1250}
1251
1252int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
1253{
1254 isl_int *t;
1255 if (!bmap)
1256 return -1;
1257 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
1258
1259 if (pos != bmap->n_eq - 1) {
1260 t = bmap->eq[pos];
1261 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
1262 bmap->eq[bmap->n_eq - 1] = t;
1263 }
1264 bmap->n_eq--;
1265 return 0;
1266}
1267
1268int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
1269{
1270 return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
1271}
1272
1273/* Turn inequality "pos" of "bmap" into an equality.
1274 *
1275 * In particular, we move the inequality in front of the equalities
1276 * and move the last inequality in the position of the moved inequality.
1277 * Note that isl_tab_make_equalities_explicit depends on this particular
1278 * change in the ordering of the constraints.
1279 */
1280void isl_basic_map_inequality_to_equality(
1281 struct isl_basic_map *bmap, unsigned pos)
1282{
1283 isl_int *t;
1284
1285 t = bmap->ineq[pos];
1286 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1287 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1288 bmap->eq[-1] = t;
1289 bmap->n_eq++;
1290 bmap->n_ineq--;
1291 bmap->eq--;
1292 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1293 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1294 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1295 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1296}
1297
1298static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
1299{
1300 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1301}
1302
1303int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
1304{
1305 struct isl_ctx *ctx;
1306 if (!bmap)
1307 return -1;
1308 ctx = bmap->ctx;
1309 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1310 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1311 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1312 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1313 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1314 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
1315 1 + isl_basic_map_total_dim(bmap),
1316 bmap->extra - bmap->n_div);
1317 return bmap->n_ineq++;
1318}
1319
1320int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
1321{
1322 return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
1323}
1324
1325int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
1326{
1327 if (!bmap)
1328 return -1;
1329 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
1330 bmap->n_ineq -= n;
1331 return 0;
1332}
1333
1334int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
1335{
1336 return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
1337}
1338
1339int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
1340{
1341 isl_int *t;
1342 if (!bmap)
1343 return -1;
1344 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1345
1346 if (pos != bmap->n_ineq - 1) {
1347 t = bmap->ineq[pos];
1348 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1349 bmap->ineq[bmap->n_ineq - 1] = t;
1350 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1351 }
1352 bmap->n_ineq--;
1353 return 0;
1354}
1355
1356int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
1357{
1358 return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
1359}
1360
1361__isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1362 isl_int *eq)
1363{
1364 int k;
1365
1366 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1367 if (!bmap)
1368 return NULL;
1369 k = isl_basic_map_alloc_equality(bmap);
1370 if (k < 0)
1371 goto error;
1372 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
1373 return bmap;
1374error:
1375 isl_basic_map_free(bmap);
1376 return NULL;
1377}
1378
1379__isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1380 isl_int *eq)
1381{
1382 return (isl_basic_set *)
1383 isl_basic_map_add_eq((isl_basic_map *)bset, eq);
1384}
1385
1386__isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1387 isl_int *ineq)
1388{
1389 int k;
1390
1391 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1392 if (!bmap)
1393 return NULL;
1394 k = isl_basic_map_alloc_inequality(bmap);
1395 if (k < 0)
1396 goto error;
1397 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
1398 return bmap;
1399error:
1400 isl_basic_map_free(bmap);
1401 return NULL;
1402}
1403
1404__isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1405 isl_int *ineq)
1406{
1407 return (isl_basic_set *)
1408 isl_basic_map_add_ineq((isl_basic_map *)bset, ineq);
1409}
1410
1411int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
1412{
1413 if (!bmap)
1414 return -1;
1415 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1416 isl_seq_clr(bmap->div[bmap->n_div] +
1417 1 + 1 + isl_basic_map_total_dim(bmap),
1418 bmap->extra - bmap->n_div);
1419 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1420 return bmap->n_div++;
1421}
1422
1423int isl_basic_set_alloc_div(struct isl_basic_set *bset)
1424{
1425 return isl_basic_map_alloc_div((struct isl_basic_map *)bset);
1426}
1427
1428int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
1429{
1430 if (!bmap)
1431 return -1;
1432 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
1433 bmap->n_div -= n;
1434 return 0;
1435}
1436
1437int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n)
1438{
1439 return isl_basic_map_free_div((struct isl_basic_map *)bset, n);
1440}
1441
1442/* Copy constraint from src to dst, putting the vars of src at offset
1443 * dim_off in dst and the divs of src at offset div_off in dst.
1444 * If both sets are actually map, then dim_off applies to the input
1445 * variables.
1446 */
1447static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
1448 struct isl_basic_map *src_map, isl_int *src,
1449 unsigned in_off, unsigned out_off, unsigned div_off)
1450{
1451 unsigned src_nparam = isl_basic_map_n_param(src_map);
1452 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
1453 unsigned src_in = isl_basic_map_n_in(src_map);
1454 unsigned dst_in = isl_basic_map_n_in(dst_map);
1455 unsigned src_out = isl_basic_map_n_out(src_map);
1456 unsigned dst_out = isl_basic_map_n_out(dst_map);
1457 isl_int_set(dst[0], src[0]);
1458 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
1459 if (dst_nparam > src_nparam)
1460 isl_seq_clr(dst+1+src_nparam,
1461 dst_nparam - src_nparam);
1462 isl_seq_clr(dst+1+dst_nparam, in_off);
1463 isl_seq_cpy(dst+1+dst_nparam+in_off,
1464 src+1+src_nparam,
1465 isl_min(dst_in-in_off, src_in));
1466 if (dst_in-in_off > src_in)
1467 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
1468 dst_in - in_off - src_in);
1469 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
1470 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
1471 src+1+src_nparam+src_in,
1472 isl_min(dst_out-out_off, src_out));
1473 if (dst_out-out_off > src_out)
1474 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
1475 dst_out - out_off - src_out);
1476 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
1477 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
1478 src+1+src_nparam+src_in+src_out,
1479 isl_min(dst_map->extra-div_off, src_map->n_div));
1480 if (dst_map->n_div-div_off > src_map->n_div)
1481 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
1482 div_off+src_map->n_div,
1483 dst_map->n_div - div_off - src_map->n_div);
1484}
1485
1486static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
1487 struct isl_basic_map *src_map, isl_int *src,
1488 unsigned in_off, unsigned out_off, unsigned div_off)
1489{
1490 isl_int_set(dst[0], src[0]);
1491 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
1492}
1493
1494static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
1495 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
1496{
1497 int i;
1498 unsigned div_off;
1499
1500 if (!bmap1 || !bmap2)
1501 goto error;
1502
1503 div_off = bmap1->n_div;
1504
1505 for (i = 0; i < bmap2->n_eq; ++i) {
1506 int i1 = isl_basic_map_alloc_equality(bmap1);
1507 if (i1 < 0)
1508 goto error;
1509 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
1510 i_pos, o_pos, div_off);
1511 }
1512
1513 for (i = 0; i < bmap2->n_ineq; ++i) {
1514 int i1 = isl_basic_map_alloc_inequality(bmap1);
1515 if (i1 < 0)
1516 goto error;
1517 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
1518 i_pos, o_pos, div_off);
1519 }
1520
1521 for (i = 0; i < bmap2->n_div; ++i) {
1522 int i1 = isl_basic_map_alloc_div(bmap1);
1523 if (i1 < 0)
1524 goto error;
1525 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
1526 i_pos, o_pos, div_off);
1527 }
1528
1529 isl_basic_map_free(bmap2);
1530
1531 return bmap1;
1532
1533error:
1534 isl_basic_map_free(bmap1);
1535 isl_basic_map_free(bmap2);
1536 return NULL;
1537}
1538
1539struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1540 struct isl_basic_set *bset2, unsigned pos)
1541{
1542 return (struct isl_basic_set *)
1543 add_constraints((struct isl_basic_map *)bset1,
1544 (struct isl_basic_map *)bset2, 0, pos);
1545}
1546
1547struct isl_basic_map *isl_basic_map_extend_space(struct isl_basic_map *base,
1548 __isl_take isl_space *dim, unsigned extra,
1549 unsigned n_eq, unsigned n_ineq)
1550{
1551 struct isl_basic_map *ext;
1552 unsigned flags;
1553 int dims_ok;
1554
1555 if (!dim)
1556 goto error;
1557
1558 if (!base)
1559 goto error;
1560
1561 dims_ok = isl_space_is_equal(base->dim, dim) &&
1562 base->extra >= base->n_div + extra;
1563
1564 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1565 room_for_ineq(base, n_ineq)) {
1566 isl_space_free(dim);
1567 return base;
1568 }
1569
1570 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
1571 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
1572 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
1573 extra += base->extra;
1574 n_eq += base->n_eq;
1575 n_ineq += base->n_ineq;
1576
1577 ext = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1578 dim = NULL;
1579 if (!ext)
1580 goto error;
1581
1582 if (dims_ok)
1583 ext->sample = isl_vec_copy(base->sample);
1584 flags = base->flags;
1585 ext = add_constraints(ext, base, 0, 0);
1586 if (ext) {
1587 ext->flags = flags;
1588 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1589 }
1590
1591 return ext;
1592
1593error:
1594 isl_space_free(dim);
1595 isl_basic_map_free(base);
1596 return NULL;
1597}
1598
1599struct isl_basic_set *isl_basic_set_extend_space(struct isl_basic_set *base,
1600 __isl_take isl_space *dim, unsigned extra,
1601 unsigned n_eq, unsigned n_ineq)
1602{
1603 return (struct isl_basic_set *)
1604 isl_basic_map_extend_space((struct isl_basic_map *)base, dim,
1605 extra, n_eq, n_ineq);
1606}
1607
1608struct isl_basic_map *isl_basic_map_extend_constraints(
1609 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1610{
1611 if (!base)
1612 return NULL;
1613 return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1614 0, n_eq, n_ineq);
1615}
1616
1617struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1618 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1619 unsigned n_eq, unsigned n_ineq)
1620{
1621 struct isl_basic_map *bmap;
1622 isl_space *dim;
1623
1624 if (!base)
1625 return NULL;
1626 dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1627 if (!dim)
1628 goto error;
1629
1630 bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1631 return bmap;
1632error:
1633 isl_basic_map_free(base);
1634 return NULL;
1635}
1636
1637struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1638 unsigned nparam, unsigned dim, unsigned extra,
1639 unsigned n_eq, unsigned n_ineq)
1640{
1641 return (struct isl_basic_set *)
1642 isl_basic_map_extend((struct isl_basic_map *)base,
1643 nparam, 0, dim, extra, n_eq, n_ineq);
1644}
1645
1646struct isl_basic_set *isl_basic_set_extend_constraints(
1647 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1648{
1649 return (struct isl_basic_set *)
1650 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
1651 n_eq, n_ineq);
1652}
1653
1654struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1655{
1656 return (struct isl_basic_set *)
1657 isl_basic_map_cow((struct isl_basic_map *)bset);
1658}
1659
1660struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1661{
1662 if (!bmap)
1663 return NULL;
1664
1665 if (bmap->ref > 1) {
1666 bmap->ref--;
1667 bmap = isl_basic_map_dup(bmap);
1668 }
Tobias Grosser1fa7b972015-02-16 19:33:40 +00001669 if (bmap) {
Tobias Grosser52a25232015-02-04 20:55:43 +00001670 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
Tobias Grosser1fa7b972015-02-16 19:33:40 +00001671 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
1672 }
Tobias Grosser52a25232015-02-04 20:55:43 +00001673 return bmap;
1674}
1675
1676struct isl_set *isl_set_cow(struct isl_set *set)
1677{
1678 if (!set)
1679 return NULL;
1680
1681 if (set->ref == 1)
1682 return set;
1683 set->ref--;
1684 return isl_set_dup(set);
1685}
1686
1687struct isl_map *isl_map_cow(struct isl_map *map)
1688{
1689 if (!map)
1690 return NULL;
1691
1692 if (map->ref == 1)
1693 return map;
1694 map->ref--;
1695 return isl_map_dup(map);
1696}
1697
1698static void swap_vars(struct isl_blk blk, isl_int *a,
1699 unsigned a_len, unsigned b_len)
1700{
1701 isl_seq_cpy(blk.data, a+a_len, b_len);
1702 isl_seq_cpy(blk.data+b_len, a, a_len);
1703 isl_seq_cpy(a, blk.data, b_len+a_len);
1704}
1705
1706static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1707 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1708{
1709 int i;
1710 struct isl_blk blk;
1711
1712 if (!bmap)
1713 goto error;
1714
1715 isl_assert(bmap->ctx,
1716 pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error);
1717
1718 if (n1 == 0 || n2 == 0)
1719 return bmap;
1720
1721 bmap = isl_basic_map_cow(bmap);
1722 if (!bmap)
1723 return NULL;
1724
1725 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1726 if (isl_blk_is_error(blk))
1727 goto error;
1728
1729 for (i = 0; i < bmap->n_eq; ++i)
1730 swap_vars(blk,
1731 bmap->eq[i] + pos, n1, n2);
1732
1733 for (i = 0; i < bmap->n_ineq; ++i)
1734 swap_vars(blk,
1735 bmap->ineq[i] + pos, n1, n2);
1736
1737 for (i = 0; i < bmap->n_div; ++i)
1738 swap_vars(blk,
1739 bmap->div[i]+1 + pos, n1, n2);
1740
1741 isl_blk_free(bmap->ctx, blk);
1742
1743 ISL_F_CLR(bmap, ISL_BASIC_SET_NORMALIZED);
1744 bmap = isl_basic_map_gauss(bmap, NULL);
1745 return isl_basic_map_finalize(bmap);
1746error:
1747 isl_basic_map_free(bmap);
1748 return NULL;
1749}
1750
Tobias Grosser52a25232015-02-04 20:55:43 +00001751struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1752{
1753 int i = 0;
1754 unsigned total;
1755 if (!bmap)
1756 goto error;
1757 total = isl_basic_map_total_dim(bmap);
1758 isl_basic_map_free_div(bmap, bmap->n_div);
1759 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1760 if (bmap->n_eq > 0)
1761 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1762 else {
1763 i = isl_basic_map_alloc_equality(bmap);
1764 if (i < 0)
1765 goto error;
1766 }
1767 isl_int_set_si(bmap->eq[i][0], 1);
1768 isl_seq_clr(bmap->eq[i]+1, total);
1769 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1770 isl_vec_free(bmap->sample);
1771 bmap->sample = NULL;
1772 return isl_basic_map_finalize(bmap);
1773error:
1774 isl_basic_map_free(bmap);
1775 return NULL;
1776}
1777
1778struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1779{
1780 return (struct isl_basic_set *)
1781 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1782}
1783
1784/* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
1785 * of "bmap").
1786 */
1787static void swap_div(__isl_keep isl_basic_map *bmap, int a, int b)
1788{
1789 isl_int *t = bmap->div[a];
1790 bmap->div[a] = bmap->div[b];
1791 bmap->div[b] = t;
1792}
1793
1794/* Swap divs "a" and "b" in "bmap" and adjust the constraints and
1795 * div definitions accordingly.
1796 */
1797void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1798{
1799 int i;
1800 unsigned off = isl_space_dim(bmap->dim, isl_dim_all);
1801
1802 swap_div(bmap, a, b);
1803
1804 for (i = 0; i < bmap->n_eq; ++i)
1805 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1806
1807 for (i = 0; i < bmap->n_ineq; ++i)
1808 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1809
1810 for (i = 0; i < bmap->n_div; ++i)
1811 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1812 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1813}
1814
1815/* Eliminate the specified n dimensions starting at first from the
1816 * constraints, without removing the dimensions from the space.
1817 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1818 */
1819__isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
1820 enum isl_dim_type type, unsigned first, unsigned n)
1821{
1822 int i;
1823
1824 if (!map)
1825 return NULL;
1826 if (n == 0)
1827 return map;
1828
1829 if (first + n > isl_map_dim(map, type) || first + n < first)
1830 isl_die(map->ctx, isl_error_invalid,
1831 "index out of bounds", goto error);
1832
1833 map = isl_map_cow(map);
1834 if (!map)
1835 return NULL;
1836
1837 for (i = 0; i < map->n; ++i) {
1838 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
1839 if (!map->p[i])
1840 goto error;
1841 }
1842 return map;
1843error:
1844 isl_map_free(map);
1845 return NULL;
1846}
1847
1848/* Eliminate the specified n dimensions starting at first from the
1849 * constraints, without removing the dimensions from the space.
1850 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1851 */
1852__isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
1853 enum isl_dim_type type, unsigned first, unsigned n)
1854{
1855 return (isl_set *)isl_map_eliminate((isl_map *)set, type, first, n);
1856}
1857
1858/* Eliminate the specified n dimensions starting at first from the
1859 * constraints, without removing the dimensions from the space.
1860 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1861 */
1862__isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
1863 unsigned first, unsigned n)
1864{
1865 return isl_set_eliminate(set, isl_dim_set, first, n);
1866}
1867
1868__isl_give isl_basic_map *isl_basic_map_remove_divs(
1869 __isl_take isl_basic_map *bmap)
1870{
1871 if (!bmap)
1872 return NULL;
1873 bmap = isl_basic_map_eliminate_vars(bmap,
1874 isl_space_dim(bmap->dim, isl_dim_all), bmap->n_div);
1875 if (!bmap)
1876 return NULL;
1877 bmap->n_div = 0;
1878 return isl_basic_map_finalize(bmap);
1879}
1880
1881__isl_give isl_basic_set *isl_basic_set_remove_divs(
1882 __isl_take isl_basic_set *bset)
1883{
1884 return (struct isl_basic_set *)isl_basic_map_remove_divs(
1885 (struct isl_basic_map *)bset);
1886}
1887
1888__isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
1889{
1890 int i;
1891
1892 if (!map)
1893 return NULL;
1894 if (map->n == 0)
1895 return map;
1896
1897 map = isl_map_cow(map);
1898 if (!map)
1899 return NULL;
1900
1901 for (i = 0; i < map->n; ++i) {
1902 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
1903 if (!map->p[i])
1904 goto error;
1905 }
1906 return map;
1907error:
1908 isl_map_free(map);
1909 return NULL;
1910}
1911
1912__isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
1913{
1914 return isl_map_remove_divs(set);
1915}
1916
1917struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap,
1918 enum isl_dim_type type, unsigned first, unsigned n)
1919{
1920 if (!bmap)
1921 return NULL;
1922 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1923 goto error);
1924 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
1925 return bmap;
1926 bmap = isl_basic_map_eliminate_vars(bmap,
1927 isl_basic_map_offset(bmap, type) - 1 + first, n);
1928 if (!bmap)
1929 return bmap;
1930 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
1931 return bmap;
1932 bmap = isl_basic_map_drop(bmap, type, first, n);
1933 return bmap;
1934error:
1935 isl_basic_map_free(bmap);
1936 return NULL;
1937}
1938
1939/* Return true if the definition of the given div (recursively) involves
1940 * any of the given variables.
1941 */
1942static int div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
1943 unsigned first, unsigned n)
1944{
1945 int i;
1946 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1947
1948 if (isl_int_is_zero(bmap->div[div][0]))
1949 return 0;
1950 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
1951 return 1;
1952
1953 for (i = bmap->n_div - 1; i >= 0; --i) {
1954 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1955 continue;
1956 if (div_involves_vars(bmap, i, first, n))
1957 return 1;
1958 }
1959
1960 return 0;
1961}
1962
1963/* Try and add a lower and/or upper bound on "div" to "bmap"
1964 * based on inequality "i".
1965 * "total" is the total number of variables (excluding the divs).
1966 * "v" is a temporary object that can be used during the calculations.
1967 * If "lb" is set, then a lower bound should be constructed.
1968 * If "ub" is set, then an upper bound should be constructed.
1969 *
1970 * The calling function has already checked that the inequality does not
1971 * reference "div", but we still need to check that the inequality is
1972 * of the right form. We'll consider the case where we want to construct
1973 * a lower bound. The construction of upper bounds is similar.
1974 *
1975 * Let "div" be of the form
1976 *
1977 * q = floor((a + f(x))/d)
1978 *
1979 * We essentially check if constraint "i" is of the form
1980 *
1981 * b + f(x) >= 0
1982 *
1983 * so that we can use it to derive a lower bound on "div".
1984 * However, we allow a slightly more general form
1985 *
1986 * b + g(x) >= 0
1987 *
1988 * with the condition that the coefficients of g(x) - f(x) are all
1989 * divisible by d.
1990 * Rewriting this constraint as
1991 *
1992 * 0 >= -b - g(x)
1993 *
1994 * adding a + f(x) to both sides and dividing by d, we obtain
1995 *
1996 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
1997 *
1998 * Taking the floor on both sides, we obtain
1999 *
2000 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2001 *
2002 * or
2003 *
2004 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2005 *
2006 * In the case of an upper bound, we construct the constraint
2007 *
2008 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2009 *
2010 */
2011static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
2012 __isl_take isl_basic_map *bmap, int div, int i,
2013 unsigned total, isl_int v, int lb, int ub)
2014{
2015 int j;
2016
2017 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
2018 if (lb) {
2019 isl_int_sub(v, bmap->ineq[i][1 + j],
2020 bmap->div[div][1 + 1 + j]);
2021 lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
2022 }
2023 if (ub) {
2024 isl_int_add(v, bmap->ineq[i][1 + j],
2025 bmap->div[div][1 + 1 + j]);
2026 ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
2027 }
2028 }
2029 if (!lb && !ub)
2030 return bmap;
2031
2032 bmap = isl_basic_map_cow(bmap);
2033 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
2034 if (lb) {
2035 int k = isl_basic_map_alloc_inequality(bmap);
2036 if (k < 0)
2037 goto error;
2038 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2039 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
2040 bmap->div[div][1 + j]);
2041 isl_int_cdiv_q(bmap->ineq[k][j],
2042 bmap->ineq[k][j], bmap->div[div][0]);
2043 }
2044 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
2045 }
2046 if (ub) {
2047 int k = isl_basic_map_alloc_inequality(bmap);
2048 if (k < 0)
2049 goto error;
2050 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2051 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
2052 bmap->div[div][1 + j]);
2053 isl_int_fdiv_q(bmap->ineq[k][j],
2054 bmap->ineq[k][j], bmap->div[div][0]);
2055 }
2056 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
2057 }
2058
2059 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2060 return bmap;
2061error:
2062 isl_basic_map_free(bmap);
2063 return NULL;
2064}
2065
2066/* This function is called right before "div" is eliminated from "bmap"
2067 * using Fourier-Motzkin.
2068 * Look through the constraints of "bmap" for constraints on the argument
2069 * of the integer division and use them to construct constraints on the
2070 * integer division itself. These constraints can then be combined
2071 * during the Fourier-Motzkin elimination.
2072 * Note that it is only useful to introduce lower bounds on "div"
2073 * if "bmap" already contains upper bounds on "div" as the newly
2074 * introduce lower bounds can then be combined with the pre-existing
2075 * upper bounds. Similarly for upper bounds.
2076 * We therefore first check if "bmap" contains any lower and/or upper bounds
2077 * on "div".
2078 *
2079 * It is interesting to note that the introduction of these constraints
2080 * can indeed lead to more accurate results, even when compared to
2081 * deriving constraints on the argument of "div" from constraints on "div".
2082 * Consider, for example, the set
2083 *
2084 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2085 *
2086 * The second constraint can be rewritten as
2087 *
2088 * 2 * [(-i-2j+3)/4] + k >= 0
2089 *
2090 * from which we can derive
2091 *
2092 * -i - 2j + 3 >= -2k
2093 *
2094 * or
2095 *
2096 * i + 2j <= 3 + 2k
2097 *
2098 * Combined with the first constraint, we obtain
2099 *
2100 * -3 <= 3 + 2k or k >= -3
2101 *
2102 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2103 * the first constraint, we obtain
2104 *
2105 * [(i + 2j)/4] >= [-3/4] = -1
2106 *
2107 * Combining this constraint with the second constraint, we obtain
2108 *
2109 * k >= -2
2110 */
2111static __isl_give isl_basic_map *insert_bounds_on_div(
2112 __isl_take isl_basic_map *bmap, int div)
2113{
2114 int i;
2115 int check_lb, check_ub;
2116 isl_int v;
2117 unsigned total;
2118
2119 if (!bmap)
2120 return NULL;
2121
2122 if (isl_int_is_zero(bmap->div[div][0]))
2123 return bmap;
2124
2125 total = isl_space_dim(bmap->dim, isl_dim_all);
2126
2127 check_lb = 0;
2128 check_ub = 0;
2129 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
2130 int s = isl_int_sgn(bmap->ineq[i][1 + total + div]);
2131 if (s > 0)
2132 check_ub = 1;
2133 if (s < 0)
2134 check_lb = 1;
2135 }
2136
2137 if (!check_lb && !check_ub)
2138 return bmap;
2139
2140 isl_int_init(v);
2141
2142 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
2143 if (!isl_int_is_zero(bmap->ineq[i][1 + total + div]))
2144 continue;
2145
2146 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, total, v,
2147 check_lb, check_ub);
2148 }
2149
2150 isl_int_clear(v);
2151
2152 return bmap;
2153}
2154
2155/* Remove all divs (recursively) involving any of the given dimensions
2156 * in their definitions.
2157 */
2158__isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
2159 __isl_take isl_basic_map *bmap,
2160 enum isl_dim_type type, unsigned first, unsigned n)
2161{
2162 int i;
2163
2164 if (!bmap)
2165 return NULL;
2166 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
2167 goto error);
2168 first += isl_basic_map_offset(bmap, type);
2169
2170 for (i = bmap->n_div - 1; i >= 0; --i) {
2171 if (!div_involves_vars(bmap, i, first, n))
2172 continue;
2173 bmap = insert_bounds_on_div(bmap, i);
2174 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2175 if (!bmap)
2176 return NULL;
2177 i = bmap->n_div;
2178 }
2179
2180 return bmap;
2181error:
2182 isl_basic_map_free(bmap);
2183 return NULL;
2184}
2185
2186__isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims(
2187 __isl_take isl_basic_set *bset,
2188 enum isl_dim_type type, unsigned first, unsigned n)
2189{
2190 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
2191}
2192
2193__isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
2194 enum isl_dim_type type, unsigned first, unsigned n)
2195{
2196 int i;
2197
2198 if (!map)
2199 return NULL;
2200 if (map->n == 0)
2201 return map;
2202
2203 map = isl_map_cow(map);
2204 if (!map)
2205 return NULL;
2206
2207 for (i = 0; i < map->n; ++i) {
2208 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
2209 type, first, n);
2210 if (!map->p[i])
2211 goto error;
2212 }
2213 return map;
2214error:
2215 isl_map_free(map);
2216 return NULL;
2217}
2218
2219__isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
2220 enum isl_dim_type type, unsigned first, unsigned n)
2221{
2222 return (isl_set *)isl_map_remove_divs_involving_dims((isl_map *)set,
2223 type, first, n);
2224}
2225
2226/* Does the desciption of "bmap" depend on the specified dimensions?
2227 * We also check whether the dimensions appear in any of the div definitions.
2228 * In principle there is no need for this check. If the dimensions appear
2229 * in a div definition, they also appear in the defining constraints of that
2230 * div.
2231 */
Tobias Grosserb2f39922015-05-28 13:32:11 +00002232isl_bool isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
Tobias Grosser52a25232015-02-04 20:55:43 +00002233 enum isl_dim_type type, unsigned first, unsigned n)
2234{
2235 int i;
2236
2237 if (!bmap)
Tobias Grosserb2f39922015-05-28 13:32:11 +00002238 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00002239
2240 if (first + n > isl_basic_map_dim(bmap, type))
2241 isl_die(bmap->ctx, isl_error_invalid,
Tobias Grosserb2f39922015-05-28 13:32:11 +00002242 "index out of bounds", return isl_bool_error);
Tobias Grosser52a25232015-02-04 20:55:43 +00002243
2244 first += isl_basic_map_offset(bmap, type);
2245 for (i = 0; i < bmap->n_eq; ++i)
2246 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
Tobias Grosserb2f39922015-05-28 13:32:11 +00002247 return isl_bool_true;
Tobias Grosser52a25232015-02-04 20:55:43 +00002248 for (i = 0; i < bmap->n_ineq; ++i)
2249 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
Tobias Grosserb2f39922015-05-28 13:32:11 +00002250 return isl_bool_true;
Tobias Grosser52a25232015-02-04 20:55:43 +00002251 for (i = 0; i < bmap->n_div; ++i) {
2252 if (isl_int_is_zero(bmap->div[i][0]))
2253 continue;
2254 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
Tobias Grosserb2f39922015-05-28 13:32:11 +00002255 return isl_bool_true;
Tobias Grosser52a25232015-02-04 20:55:43 +00002256 }
2257
Tobias Grosserb2f39922015-05-28 13:32:11 +00002258 return isl_bool_false;
Tobias Grosser52a25232015-02-04 20:55:43 +00002259}
2260
Tobias Grosserb2f39922015-05-28 13:32:11 +00002261isl_bool isl_map_involves_dims(__isl_keep isl_map *map,
Tobias Grosser52a25232015-02-04 20:55:43 +00002262 enum isl_dim_type type, unsigned first, unsigned n)
2263{
2264 int i;
2265
2266 if (!map)
Tobias Grosserb2f39922015-05-28 13:32:11 +00002267 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00002268
2269 if (first + n > isl_map_dim(map, type))
2270 isl_die(map->ctx, isl_error_invalid,
Tobias Grosserb2f39922015-05-28 13:32:11 +00002271 "index out of bounds", return isl_bool_error);
Tobias Grosser52a25232015-02-04 20:55:43 +00002272
2273 for (i = 0; i < map->n; ++i) {
Tobias Grosserb2f39922015-05-28 13:32:11 +00002274 isl_bool involves = isl_basic_map_involves_dims(map->p[i],
Tobias Grosser52a25232015-02-04 20:55:43 +00002275 type, first, n);
2276 if (involves < 0 || involves)
2277 return involves;
2278 }
2279
Tobias Grosserb2f39922015-05-28 13:32:11 +00002280 return isl_bool_false;
Tobias Grosser52a25232015-02-04 20:55:43 +00002281}
2282
Tobias Grosserb2f39922015-05-28 13:32:11 +00002283isl_bool isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
Tobias Grosser52a25232015-02-04 20:55:43 +00002284 enum isl_dim_type type, unsigned first, unsigned n)
2285{
2286 return isl_basic_map_involves_dims(bset, type, first, n);
2287}
2288
Tobias Grosserb2f39922015-05-28 13:32:11 +00002289isl_bool isl_set_involves_dims(__isl_keep isl_set *set,
Tobias Grosser52a25232015-02-04 20:55:43 +00002290 enum isl_dim_type type, unsigned first, unsigned n)
2291{
2292 return isl_map_involves_dims(set, type, first, n);
2293}
2294
2295/* Return true if the definition of the given div is unknown or depends
2296 * on unknown divs.
2297 */
2298static int div_is_unknown(__isl_keep isl_basic_map *bmap, int div)
2299{
2300 int i;
2301 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2302
2303 if (isl_int_is_zero(bmap->div[div][0]))
2304 return 1;
2305
2306 for (i = bmap->n_div - 1; i >= 0; --i) {
2307 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2308 continue;
2309 if (div_is_unknown(bmap, i))
2310 return 1;
2311 }
2312
2313 return 0;
2314}
2315
2316/* Remove all divs that are unknown or defined in terms of unknown divs.
2317 */
2318__isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
2319 __isl_take isl_basic_map *bmap)
2320{
2321 int i;
2322
2323 if (!bmap)
2324 return NULL;
2325
2326 for (i = bmap->n_div - 1; i >= 0; --i) {
2327 if (!div_is_unknown(bmap, i))
2328 continue;
2329 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2330 if (!bmap)
2331 return NULL;
2332 i = bmap->n_div;
2333 }
2334
2335 return bmap;
2336}
2337
2338/* Remove all divs that are unknown or defined in terms of unknown divs.
2339 */
2340__isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
2341 __isl_take isl_basic_set *bset)
2342{
2343 return isl_basic_map_remove_unknown_divs(bset);
2344}
2345
2346__isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
2347{
2348 int i;
2349
2350 if (!map)
2351 return NULL;
2352 if (map->n == 0)
2353 return map;
2354
2355 map = isl_map_cow(map);
2356 if (!map)
2357 return NULL;
2358
2359 for (i = 0; i < map->n; ++i) {
2360 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
2361 if (!map->p[i])
2362 goto error;
2363 }
2364 return map;
2365error:
2366 isl_map_free(map);
2367 return NULL;
2368}
2369
2370__isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
2371{
2372 return (isl_set *)isl_map_remove_unknown_divs((isl_map *)set);
2373}
2374
2375__isl_give isl_basic_set *isl_basic_set_remove_dims(
2376 __isl_take isl_basic_set *bset,
2377 enum isl_dim_type type, unsigned first, unsigned n)
2378{
2379 return (isl_basic_set *)
2380 isl_basic_map_remove_dims((isl_basic_map *)bset, type, first, n);
2381}
2382
2383struct isl_map *isl_map_remove_dims(struct isl_map *map,
2384 enum isl_dim_type type, unsigned first, unsigned n)
2385{
2386 int i;
2387
2388 if (n == 0)
2389 return map;
2390
2391 map = isl_map_cow(map);
2392 if (!map)
2393 return NULL;
2394 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2395
2396 for (i = 0; i < map->n; ++i) {
2397 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
2398 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
2399 if (!map->p[i])
2400 goto error;
2401 }
2402 map = isl_map_drop(map, type, first, n);
2403 return map;
2404error:
2405 isl_map_free(map);
2406 return NULL;
2407}
2408
2409__isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
2410 enum isl_dim_type type, unsigned first, unsigned n)
2411{
2412 return (isl_set *)isl_map_remove_dims((isl_map *)bset, type, first, n);
2413}
2414
2415/* Project out n inputs starting at first using Fourier-Motzkin */
2416struct isl_map *isl_map_remove_inputs(struct isl_map *map,
2417 unsigned first, unsigned n)
2418{
2419 return isl_map_remove_dims(map, isl_dim_in, first, n);
2420}
2421
2422static void dump_term(struct isl_basic_map *bmap,
2423 isl_int c, int pos, FILE *out)
2424{
2425 const char *name;
2426 unsigned in = isl_basic_map_n_in(bmap);
2427 unsigned dim = in + isl_basic_map_n_out(bmap);
2428 unsigned nparam = isl_basic_map_n_param(bmap);
2429 if (!pos)
2430 isl_int_print(out, c, 0);
2431 else {
2432 if (!isl_int_is_one(c))
2433 isl_int_print(out, c, 0);
2434 if (pos < 1 + nparam) {
2435 name = isl_space_get_dim_name(bmap->dim,
2436 isl_dim_param, pos - 1);
2437 if (name)
2438 fprintf(out, "%s", name);
2439 else
2440 fprintf(out, "p%d", pos - 1);
2441 } else if (pos < 1 + nparam + in)
2442 fprintf(out, "i%d", pos - 1 - nparam);
2443 else if (pos < 1 + nparam + dim)
2444 fprintf(out, "o%d", pos - 1 - nparam - in);
2445 else
2446 fprintf(out, "e%d", pos - 1 - nparam - dim);
2447 }
2448}
2449
2450static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
2451 int sign, FILE *out)
2452{
2453 int i;
2454 int first;
2455 unsigned len = 1 + isl_basic_map_total_dim(bmap);
2456 isl_int v;
2457
2458 isl_int_init(v);
2459 for (i = 0, first = 1; i < len; ++i) {
2460 if (isl_int_sgn(c[i]) * sign <= 0)
2461 continue;
2462 if (!first)
2463 fprintf(out, " + ");
2464 first = 0;
2465 isl_int_abs(v, c[i]);
2466 dump_term(bmap, v, i, out);
2467 }
2468 isl_int_clear(v);
2469 if (first)
2470 fprintf(out, "0");
2471}
2472
2473static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
2474 const char *op, FILE *out, int indent)
2475{
2476 int i;
2477
2478 fprintf(out, "%*s", indent, "");
2479
2480 dump_constraint_sign(bmap, c, 1, out);
2481 fprintf(out, " %s ", op);
2482 dump_constraint_sign(bmap, c, -1, out);
2483
2484 fprintf(out, "\n");
2485
2486 for (i = bmap->n_div; i < bmap->extra; ++i) {
2487 if (isl_int_is_zero(c[1+isl_space_dim(bmap->dim, isl_dim_all)+i]))
2488 continue;
2489 fprintf(out, "%*s", indent, "");
2490 fprintf(out, "ERROR: unused div coefficient not zero\n");
2491 abort();
2492 }
2493}
2494
2495static void dump_constraints(struct isl_basic_map *bmap,
2496 isl_int **c, unsigned n,
2497 const char *op, FILE *out, int indent)
2498{
2499 int i;
2500
2501 for (i = 0; i < n; ++i)
2502 dump_constraint(bmap, c[i], op, out, indent);
2503}
2504
2505static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
2506{
2507 int j;
2508 int first = 1;
2509 unsigned total = isl_basic_map_total_dim(bmap);
2510
2511 for (j = 0; j < 1 + total; ++j) {
2512 if (isl_int_is_zero(exp[j]))
2513 continue;
2514 if (!first && isl_int_is_pos(exp[j]))
2515 fprintf(out, "+");
2516 dump_term(bmap, exp[j], j, out);
2517 first = 0;
2518 }
2519}
2520
2521static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
2522{
2523 int i;
2524
2525 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
2526 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
2527
2528 for (i = 0; i < bmap->n_div; ++i) {
2529 fprintf(out, "%*s", indent, "");
2530 fprintf(out, "e%d = [(", i);
2531 dump_affine(bmap, bmap->div[i]+1, out);
2532 fprintf(out, ")/");
2533 isl_int_print(out, bmap->div[i][0], 0);
2534 fprintf(out, "]\n");
2535 }
2536}
2537
2538void isl_basic_set_print_internal(struct isl_basic_set *bset,
2539 FILE *out, int indent)
2540{
2541 if (!bset) {
2542 fprintf(out, "null basic set\n");
2543 return;
2544 }
2545
2546 fprintf(out, "%*s", indent, "");
2547 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2548 bset->ref, bset->dim->nparam, bset->dim->n_out,
2549 bset->extra, bset->flags);
2550 dump((struct isl_basic_map *)bset, out, indent);
2551}
2552
2553void isl_basic_map_print_internal(struct isl_basic_map *bmap,
2554 FILE *out, int indent)
2555{
2556 if (!bmap) {
2557 fprintf(out, "null basic map\n");
2558 return;
2559 }
2560
2561 fprintf(out, "%*s", indent, "");
2562 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2563 "flags: %x, n_name: %d\n",
2564 bmap->ref,
2565 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
2566 bmap->extra, bmap->flags, bmap->dim->n_id);
2567 dump(bmap, out, indent);
2568}
2569
2570int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
2571{
2572 unsigned total;
2573 if (!bmap)
2574 return -1;
2575 total = isl_basic_map_total_dim(bmap);
2576 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
2577 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
2578 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
2579 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2580 return 0;
2581}
2582
2583__isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *dim, int n,
2584 unsigned flags)
2585{
2586 struct isl_set *set;
2587
2588 if (!dim)
2589 return NULL;
2590 isl_assert(dim->ctx, dim->n_in == 0, goto error);
2591 isl_assert(dim->ctx, n >= 0, goto error);
2592 set = isl_alloc(dim->ctx, struct isl_set,
2593 sizeof(struct isl_set) +
2594 (n - 1) * sizeof(struct isl_basic_set *));
2595 if (!set)
2596 goto error;
2597
2598 set->ctx = dim->ctx;
2599 isl_ctx_ref(set->ctx);
2600 set->ref = 1;
2601 set->size = n;
2602 set->n = 0;
2603 set->dim = dim;
2604 set->flags = flags;
2605 return set;
2606error:
2607 isl_space_free(dim);
2608 return NULL;
2609}
2610
2611struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
2612 unsigned nparam, unsigned dim, int n, unsigned flags)
2613{
2614 struct isl_set *set;
2615 isl_space *dims;
2616
2617 dims = isl_space_alloc(ctx, nparam, 0, dim);
2618 if (!dims)
2619 return NULL;
2620
2621 set = isl_set_alloc_space(dims, n, flags);
2622 return set;
2623}
2624
2625/* Make sure "map" has room for at least "n" more basic maps.
2626 */
2627struct isl_map *isl_map_grow(struct isl_map *map, int n)
2628{
2629 int i;
2630 struct isl_map *grown = NULL;
2631
2632 if (!map)
2633 return NULL;
2634 isl_assert(map->ctx, n >= 0, goto error);
2635 if (map->n + n <= map->size)
2636 return map;
2637 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
2638 if (!grown)
2639 goto error;
2640 for (i = 0; i < map->n; ++i) {
2641 grown->p[i] = isl_basic_map_copy(map->p[i]);
2642 if (!grown->p[i])
2643 goto error;
2644 grown->n++;
2645 }
2646 isl_map_free(map);
2647 return grown;
2648error:
2649 isl_map_free(grown);
2650 isl_map_free(map);
2651 return NULL;
2652}
2653
2654/* Make sure "set" has room for at least "n" more basic sets.
2655 */
2656struct isl_set *isl_set_grow(struct isl_set *set, int n)
2657{
2658 return (struct isl_set *)isl_map_grow((struct isl_map *)set, n);
2659}
2660
2661struct isl_set *isl_set_dup(struct isl_set *set)
2662{
2663 int i;
2664 struct isl_set *dup;
2665
2666 if (!set)
2667 return NULL;
2668
2669 dup = isl_set_alloc_space(isl_space_copy(set->dim), set->n, set->flags);
2670 if (!dup)
2671 return NULL;
2672 for (i = 0; i < set->n; ++i)
2673 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
2674 return dup;
2675}
2676
2677struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
2678{
2679 return isl_map_from_basic_map(bset);
2680}
2681
2682struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
2683{
2684 struct isl_map *map;
2685
2686 if (!bmap)
2687 return NULL;
2688
2689 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
2690 return isl_map_add_basic_map(map, bmap);
2691}
2692
2693__isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
2694 __isl_take isl_basic_set *bset)
2695{
2696 return (struct isl_set *)isl_map_add_basic_map((struct isl_map *)set,
2697 (struct isl_basic_map *)bset);
2698}
2699
2700__isl_null isl_set *isl_set_free(__isl_take isl_set *set)
2701{
2702 int i;
2703
2704 if (!set)
2705 return NULL;
2706
2707 if (--set->ref > 0)
2708 return NULL;
2709
2710 isl_ctx_deref(set->ctx);
2711 for (i = 0; i < set->n; ++i)
2712 isl_basic_set_free(set->p[i]);
2713 isl_space_free(set->dim);
2714 free(set);
2715
2716 return NULL;
2717}
2718
2719void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
2720{
2721 int i;
2722
2723 if (!set) {
2724 fprintf(out, "null set\n");
2725 return;
2726 }
2727
2728 fprintf(out, "%*s", indent, "");
2729 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2730 set->ref, set->n, set->dim->nparam, set->dim->n_out,
2731 set->flags);
2732 for (i = 0; i < set->n; ++i) {
2733 fprintf(out, "%*s", indent, "");
2734 fprintf(out, "basic set %d:\n", i);
2735 isl_basic_set_print_internal(set->p[i], out, indent+4);
2736 }
2737}
2738
2739void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
2740{
2741 int i;
2742
2743 if (!map) {
2744 fprintf(out, "null map\n");
2745 return;
2746 }
2747
2748 fprintf(out, "%*s", indent, "");
2749 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2750 "flags: %x, n_name: %d\n",
2751 map->ref, map->n, map->dim->nparam, map->dim->n_in,
2752 map->dim->n_out, map->flags, map->dim->n_id);
2753 for (i = 0; i < map->n; ++i) {
2754 fprintf(out, "%*s", indent, "");
2755 fprintf(out, "basic map %d:\n", i);
2756 isl_basic_map_print_internal(map->p[i], out, indent+4);
2757 }
2758}
2759
2760struct isl_basic_map *isl_basic_map_intersect_domain(
2761 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2762{
2763 struct isl_basic_map *bmap_domain;
2764
2765 if (!bmap || !bset)
2766 goto error;
2767
2768 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2769 bset->dim, isl_dim_param), goto error);
2770
2771 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2772 isl_assert(bset->ctx,
2773 isl_basic_map_compatible_domain(bmap, bset), goto error);
2774
2775 bmap = isl_basic_map_cow(bmap);
2776 if (!bmap)
2777 goto error;
2778 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2779 bset->n_div, bset->n_eq, bset->n_ineq);
2780 bmap_domain = isl_basic_map_from_domain(bset);
2781 bmap = add_constraints(bmap, bmap_domain, 0, 0);
2782
2783 bmap = isl_basic_map_simplify(bmap);
2784 return isl_basic_map_finalize(bmap);
2785error:
2786 isl_basic_map_free(bmap);
2787 isl_basic_set_free(bset);
2788 return NULL;
2789}
2790
2791struct isl_basic_map *isl_basic_map_intersect_range(
2792 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2793{
2794 struct isl_basic_map *bmap_range;
2795
2796 if (!bmap || !bset)
2797 goto error;
2798
2799 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2800 bset->dim, isl_dim_param), goto error);
2801
2802 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2803 isl_assert(bset->ctx,
2804 isl_basic_map_compatible_range(bmap, bset), goto error);
2805
Tobias Grossera67ac972016-02-26 11:35:12 +00002806 if (isl_basic_set_plain_is_universe(bset)) {
Tobias Grosser52a25232015-02-04 20:55:43 +00002807 isl_basic_set_free(bset);
2808 return bmap;
2809 }
2810
2811 bmap = isl_basic_map_cow(bmap);
2812 if (!bmap)
2813 goto error;
2814 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2815 bset->n_div, bset->n_eq, bset->n_ineq);
2816 bmap_range = isl_basic_map_from_basic_set(bset, isl_space_copy(bset->dim));
2817 bmap = add_constraints(bmap, bmap_range, 0, 0);
2818
2819 bmap = isl_basic_map_simplify(bmap);
2820 return isl_basic_map_finalize(bmap);
2821error:
2822 isl_basic_map_free(bmap);
2823 isl_basic_set_free(bset);
2824 return NULL;
2825}
2826
Tobias Grosserb2f39922015-05-28 13:32:11 +00002827isl_bool isl_basic_map_contains(__isl_keep isl_basic_map *bmap,
2828 __isl_keep isl_vec *vec)
Tobias Grosser52a25232015-02-04 20:55:43 +00002829{
2830 int i;
2831 unsigned total;
2832 isl_int s;
2833
2834 if (!bmap || !vec)
Tobias Grosserb2f39922015-05-28 13:32:11 +00002835 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00002836
2837 total = 1 + isl_basic_map_total_dim(bmap);
2838 if (total != vec->size)
Tobias Grosserb2f39922015-05-28 13:32:11 +00002839 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00002840
2841 isl_int_init(s);
2842
2843 for (i = 0; i < bmap->n_eq; ++i) {
2844 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
2845 if (!isl_int_is_zero(s)) {
2846 isl_int_clear(s);
Tobias Grosserb2f39922015-05-28 13:32:11 +00002847 return isl_bool_false;
Tobias Grosser52a25232015-02-04 20:55:43 +00002848 }
2849 }
2850
2851 for (i = 0; i < bmap->n_ineq; ++i) {
2852 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
2853 if (isl_int_is_neg(s)) {
2854 isl_int_clear(s);
Tobias Grosserb2f39922015-05-28 13:32:11 +00002855 return isl_bool_false;
Tobias Grosser52a25232015-02-04 20:55:43 +00002856 }
2857 }
2858
2859 isl_int_clear(s);
2860
Tobias Grosserb2f39922015-05-28 13:32:11 +00002861 return isl_bool_true;
Tobias Grosser52a25232015-02-04 20:55:43 +00002862}
2863
Tobias Grosserb2f39922015-05-28 13:32:11 +00002864isl_bool isl_basic_set_contains(__isl_keep isl_basic_set *bset,
2865 __isl_keep isl_vec *vec)
Tobias Grosser52a25232015-02-04 20:55:43 +00002866{
2867 return isl_basic_map_contains((struct isl_basic_map *)bset, vec);
2868}
2869
2870struct isl_basic_map *isl_basic_map_intersect(
2871 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2872{
2873 struct isl_vec *sample = NULL;
2874
2875 if (!bmap1 || !bmap2)
2876 goto error;
2877
2878 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
2879 bmap2->dim, isl_dim_param), goto error);
2880 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
2881 isl_space_dim(bmap1->dim, isl_dim_param) &&
2882 isl_space_dim(bmap2->dim, isl_dim_all) !=
2883 isl_space_dim(bmap2->dim, isl_dim_param))
2884 return isl_basic_map_intersect(bmap2, bmap1);
2885
2886 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
2887 isl_space_dim(bmap2->dim, isl_dim_param))
2888 isl_assert(bmap1->ctx,
2889 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
2890
Tobias Grosserfb3fb0a2015-11-21 20:48:39 +00002891 if (isl_basic_map_plain_is_empty(bmap1)) {
2892 isl_basic_map_free(bmap2);
2893 return bmap1;
2894 }
2895 if (isl_basic_map_plain_is_empty(bmap2)) {
2896 isl_basic_map_free(bmap1);
2897 return bmap2;
2898 }
2899
Tobias Grosser52a25232015-02-04 20:55:43 +00002900 if (bmap1->sample &&
2901 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
2902 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
2903 sample = isl_vec_copy(bmap1->sample);
2904 else if (bmap2->sample &&
2905 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
2906 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
2907 sample = isl_vec_copy(bmap2->sample);
2908
2909 bmap1 = isl_basic_map_cow(bmap1);
2910 if (!bmap1)
2911 goto error;
2912 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
2913 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
2914 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
2915
2916 if (!bmap1)
2917 isl_vec_free(sample);
2918 else if (sample) {
2919 isl_vec_free(bmap1->sample);
2920 bmap1->sample = sample;
2921 }
2922
2923 bmap1 = isl_basic_map_simplify(bmap1);
2924 return isl_basic_map_finalize(bmap1);
2925error:
2926 if (sample)
2927 isl_vec_free(sample);
2928 isl_basic_map_free(bmap1);
2929 isl_basic_map_free(bmap2);
2930 return NULL;
2931}
2932
2933struct isl_basic_set *isl_basic_set_intersect(
2934 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
2935{
2936 return (struct isl_basic_set *)
2937 isl_basic_map_intersect(
2938 (struct isl_basic_map *)bset1,
2939 (struct isl_basic_map *)bset2);
2940}
2941
2942__isl_give isl_basic_set *isl_basic_set_intersect_params(
2943 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
2944{
2945 return isl_basic_set_intersect(bset1, bset2);
2946}
2947
2948/* Special case of isl_map_intersect, where both map1 and map2
2949 * are convex, without any divs and such that either map1 or map2
2950 * contains a single constraint. This constraint is then simply
2951 * added to the other map.
2952 */
2953static __isl_give isl_map *map_intersect_add_constraint(
2954 __isl_take isl_map *map1, __isl_take isl_map *map2)
2955{
2956 isl_assert(map1->ctx, map1->n == 1, goto error);
2957 isl_assert(map2->ctx, map1->n == 1, goto error);
2958 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
2959 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
2960
2961 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
2962 return isl_map_intersect(map2, map1);
2963
2964 isl_assert(map2->ctx,
2965 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
2966
2967 map1 = isl_map_cow(map1);
2968 if (!map1)
2969 goto error;
2970 if (isl_map_plain_is_empty(map1)) {
2971 isl_map_free(map2);
2972 return map1;
2973 }
2974 map1->p[0] = isl_basic_map_cow(map1->p[0]);
2975 if (map2->p[0]->n_eq == 1)
2976 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
2977 else
2978 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
2979 map2->p[0]->ineq[0]);
2980
2981 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
2982 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
2983 if (!map1->p[0])
2984 goto error;
2985
2986 if (isl_basic_map_plain_is_empty(map1->p[0])) {
2987 isl_basic_map_free(map1->p[0]);
2988 map1->n = 0;
2989 }
2990
2991 isl_map_free(map2);
2992
2993 return map1;
2994error:
2995 isl_map_free(map1);
2996 isl_map_free(map2);
2997 return NULL;
2998}
2999
3000/* map2 may be either a parameter domain or a map living in the same
3001 * space as map1.
3002 */
3003static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
3004 __isl_take isl_map *map2)
3005{
3006 unsigned flags = 0;
3007 isl_map *result;
3008 int i, j;
3009
3010 if (!map1 || !map2)
3011 goto error;
3012
3013 if ((isl_map_plain_is_empty(map1) ||
3014 isl_map_plain_is_universe(map2)) &&
3015 isl_space_is_equal(map1->dim, map2->dim)) {
3016 isl_map_free(map2);
3017 return map1;
3018 }
3019 if ((isl_map_plain_is_empty(map2) ||
3020 isl_map_plain_is_universe(map1)) &&
3021 isl_space_is_equal(map1->dim, map2->dim)) {
3022 isl_map_free(map1);
3023 return map2;
3024 }
3025
3026 if (map1->n == 1 && map2->n == 1 &&
3027 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
3028 isl_space_is_equal(map1->dim, map2->dim) &&
3029 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
3030 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
3031 return map_intersect_add_constraint(map1, map2);
3032
3033 if (isl_space_dim(map2->dim, isl_dim_all) !=
3034 isl_space_dim(map2->dim, isl_dim_param))
3035 isl_assert(map1->ctx,
3036 isl_space_is_equal(map1->dim, map2->dim), goto error);
3037
3038 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
3039 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3040 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3041
3042 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3043 map1->n * map2->n, flags);
3044 if (!result)
3045 goto error;
3046 for (i = 0; i < map1->n; ++i)
3047 for (j = 0; j < map2->n; ++j) {
3048 struct isl_basic_map *part;
3049 part = isl_basic_map_intersect(
3050 isl_basic_map_copy(map1->p[i]),
3051 isl_basic_map_copy(map2->p[j]));
3052 if (isl_basic_map_is_empty(part) < 0)
3053 part = isl_basic_map_free(part);
3054 result = isl_map_add_basic_map(result, part);
3055 if (!result)
3056 goto error;
3057 }
3058 isl_map_free(map1);
3059 isl_map_free(map2);
3060 return result;
3061error:
3062 isl_map_free(map1);
3063 isl_map_free(map2);
3064 return NULL;
3065}
3066
3067static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
3068 __isl_take isl_map *map2)
3069{
3070 if (!map1 || !map2)
3071 goto error;
3072 if (!isl_space_is_equal(map1->dim, map2->dim))
3073 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
3074 "spaces don't match", goto error);
3075 return map_intersect_internal(map1, map2);
3076error:
3077 isl_map_free(map1);
3078 isl_map_free(map2);
3079 return NULL;
3080}
3081
3082__isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
3083 __isl_take isl_map *map2)
3084{
3085 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
3086}
3087
3088struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
3089{
3090 return (struct isl_set *)
3091 isl_map_intersect((struct isl_map *)set1,
3092 (struct isl_map *)set2);
3093}
3094
3095/* map_intersect_internal accepts intersections
3096 * with parameter domains, so we can just call that function.
3097 */
3098static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
3099 __isl_take isl_set *params)
3100{
3101 return map_intersect_internal(map, params);
3102}
3103
3104__isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
3105 __isl_take isl_map *map2)
3106{
3107 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
3108}
3109
3110__isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
3111 __isl_take isl_set *params)
3112{
3113 return isl_map_intersect_params(set, params);
3114}
3115
3116struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
3117{
Tobias Grosserb2f39922015-05-28 13:32:11 +00003118 isl_space *space;
3119 unsigned pos, n1, n2;
Tobias Grosser52a25232015-02-04 20:55:43 +00003120
3121 if (!bmap)
3122 return NULL;
3123 bmap = isl_basic_map_cow(bmap);
3124 if (!bmap)
3125 return NULL;
Tobias Grosserb2f39922015-05-28 13:32:11 +00003126 space = isl_space_reverse(isl_space_copy(bmap->dim));
3127 pos = isl_basic_map_offset(bmap, isl_dim_in);
3128 n1 = isl_basic_map_dim(bmap, isl_dim_in);
3129 n2 = isl_basic_map_dim(bmap, isl_dim_out);
3130 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
3131 return isl_basic_map_reset_space(bmap, space);
Tobias Grosser52a25232015-02-04 20:55:43 +00003132}
3133
3134static __isl_give isl_basic_map *basic_map_space_reset(
3135 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
3136{
3137 isl_space *space;
3138
3139 if (!bmap)
3140 return NULL;
3141 if (!isl_space_is_named_or_nested(bmap->dim, type))
3142 return bmap;
3143
3144 space = isl_basic_map_get_space(bmap);
3145 space = isl_space_reset(space, type);
3146 bmap = isl_basic_map_reset_space(bmap, space);
3147 return bmap;
3148}
3149
3150__isl_give isl_basic_map *isl_basic_map_insert_dims(
3151 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3152 unsigned pos, unsigned n)
3153{
3154 isl_space *res_dim;
3155 struct isl_basic_map *res;
3156 struct isl_dim_map *dim_map;
3157 unsigned total, off;
3158 enum isl_dim_type t;
3159
3160 if (n == 0)
3161 return basic_map_space_reset(bmap, type);
3162
3163 if (!bmap)
3164 return NULL;
3165
3166 res_dim = isl_space_insert_dims(isl_basic_map_get_space(bmap), type, pos, n);
3167
3168 total = isl_basic_map_total_dim(bmap) + n;
3169 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3170 off = 0;
3171 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3172 if (t != type) {
3173 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3174 } else {
3175 unsigned size = isl_basic_map_dim(bmap, t);
3176 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3177 0, pos, off);
3178 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3179 pos, size - pos, off + pos + n);
3180 }
3181 off += isl_space_dim(res_dim, t);
3182 }
3183 isl_dim_map_div(dim_map, bmap, off);
3184
3185 res = isl_basic_map_alloc_space(res_dim,
3186 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3187 if (isl_basic_map_is_rational(bmap))
3188 res = isl_basic_map_set_rational(res);
3189 if (isl_basic_map_plain_is_empty(bmap)) {
3190 isl_basic_map_free(bmap);
3191 free(dim_map);
3192 return isl_basic_map_set_to_empty(res);
3193 }
3194 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3195 return isl_basic_map_finalize(res);
3196}
3197
3198__isl_give isl_basic_set *isl_basic_set_insert_dims(
3199 __isl_take isl_basic_set *bset,
3200 enum isl_dim_type type, unsigned pos, unsigned n)
3201{
3202 return isl_basic_map_insert_dims(bset, type, pos, n);
3203}
3204
Tobias Grossere0f8d592015-05-13 13:10:13 +00003205__isl_give isl_basic_map *isl_basic_map_add_dims(__isl_take isl_basic_map *bmap,
Tobias Grosser52a25232015-02-04 20:55:43 +00003206 enum isl_dim_type type, unsigned n)
3207{
3208 if (!bmap)
3209 return NULL;
3210 return isl_basic_map_insert_dims(bmap, type,
3211 isl_basic_map_dim(bmap, type), n);
3212}
3213
3214__isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
3215 enum isl_dim_type type, unsigned n)
3216{
3217 if (!bset)
3218 return NULL;
3219 isl_assert(bset->ctx, type != isl_dim_in, goto error);
Tobias Grossere0f8d592015-05-13 13:10:13 +00003220 return isl_basic_map_add_dims(bset, type, n);
Tobias Grosser52a25232015-02-04 20:55:43 +00003221error:
3222 isl_basic_set_free(bset);
3223 return NULL;
3224}
3225
3226static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
3227 enum isl_dim_type type)
3228{
3229 isl_space *space;
3230
3231 if (!map || !isl_space_is_named_or_nested(map->dim, type))
3232 return map;
3233
3234 space = isl_map_get_space(map);
3235 space = isl_space_reset(space, type);
3236 map = isl_map_reset_space(map, space);
3237 return map;
3238}
3239
3240__isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
3241 enum isl_dim_type type, unsigned pos, unsigned n)
3242{
3243 int i;
3244
3245 if (n == 0)
3246 return map_space_reset(map, type);
3247
3248 map = isl_map_cow(map);
3249 if (!map)
3250 return NULL;
3251
3252 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
3253 if (!map->dim)
3254 goto error;
3255
3256 for (i = 0; i < map->n; ++i) {
3257 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
3258 if (!map->p[i])
3259 goto error;
3260 }
3261
3262 return map;
3263error:
3264 isl_map_free(map);
3265 return NULL;
3266}
3267
3268__isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
3269 enum isl_dim_type type, unsigned pos, unsigned n)
3270{
3271 return isl_map_insert_dims(set, type, pos, n);
3272}
3273
3274__isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
3275 enum isl_dim_type type, unsigned n)
3276{
3277 if (!map)
3278 return NULL;
3279 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
3280}
3281
3282__isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
3283 enum isl_dim_type type, unsigned n)
3284{
3285 if (!set)
3286 return NULL;
3287 isl_assert(set->ctx, type != isl_dim_in, goto error);
3288 return (isl_set *)isl_map_add_dims((isl_map *)set, type, n);
3289error:
3290 isl_set_free(set);
3291 return NULL;
3292}
3293
3294__isl_give isl_basic_map *isl_basic_map_move_dims(
3295 __isl_take isl_basic_map *bmap,
3296 enum isl_dim_type dst_type, unsigned dst_pos,
3297 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3298{
3299 struct isl_dim_map *dim_map;
3300 struct isl_basic_map *res;
3301 enum isl_dim_type t;
3302 unsigned total, off;
3303
3304 if (!bmap)
3305 return NULL;
3306 if (n == 0)
3307 return bmap;
3308
3309 isl_assert(bmap->ctx, src_pos + n <= isl_basic_map_dim(bmap, src_type),
3310 goto error);
3311
3312 if (dst_type == src_type && dst_pos == src_pos)
3313 return bmap;
3314
3315 isl_assert(bmap->ctx, dst_type != src_type, goto error);
3316
3317 if (pos(bmap->dim, dst_type) + dst_pos ==
3318 pos(bmap->dim, src_type) + src_pos +
3319 ((src_type < dst_type) ? n : 0)) {
3320 bmap = isl_basic_map_cow(bmap);
3321 if (!bmap)
3322 return NULL;
3323
3324 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3325 src_type, src_pos, n);
3326 if (!bmap->dim)
3327 goto error;
3328
3329 bmap = isl_basic_map_finalize(bmap);
3330
3331 return bmap;
3332 }
3333
3334 total = isl_basic_map_total_dim(bmap);
3335 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3336
3337 off = 0;
3338 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3339 unsigned size = isl_space_dim(bmap->dim, t);
3340 if (t == dst_type) {
3341 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3342 0, dst_pos, off);
3343 off += dst_pos;
3344 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
3345 src_pos, n, off);
3346 off += n;
3347 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3348 dst_pos, size - dst_pos, off);
3349 off += size - dst_pos;
3350 } else if (t == src_type) {
3351 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3352 0, src_pos, off);
3353 off += src_pos;
3354 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3355 src_pos + n, size - src_pos - n, off);
3356 off += size - src_pos - n;
3357 } else {
3358 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3359 off += size;
3360 }
3361 }
3362 isl_dim_map_div(dim_map, bmap, off);
3363
3364 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3365 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3366 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3367 if (!bmap)
3368 goto error;
3369
3370 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3371 src_type, src_pos, n);
3372 if (!bmap->dim)
3373 goto error;
3374
3375 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
3376 bmap = isl_basic_map_gauss(bmap, NULL);
3377 bmap = isl_basic_map_finalize(bmap);
3378
3379 return bmap;
3380error:
3381 isl_basic_map_free(bmap);
3382 return NULL;
3383}
3384
3385__isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
3386 enum isl_dim_type dst_type, unsigned dst_pos,
3387 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3388{
3389 return (isl_basic_set *)isl_basic_map_move_dims(
3390 (isl_basic_map *)bset, dst_type, dst_pos, src_type, src_pos, n);
3391}
3392
3393__isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
3394 enum isl_dim_type dst_type, unsigned dst_pos,
3395 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3396{
3397 if (!set)
3398 return NULL;
3399 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
3400 return (isl_set *)isl_map_move_dims((isl_map *)set, dst_type, dst_pos,
3401 src_type, src_pos, n);
3402error:
3403 isl_set_free(set);
3404 return NULL;
3405}
3406
3407__isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
3408 enum isl_dim_type dst_type, unsigned dst_pos,
3409 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3410{
3411 int i;
3412
3413 if (!map)
3414 return NULL;
3415 if (n == 0)
3416 return map;
3417
3418 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
3419 goto error);
3420
3421 if (dst_type == src_type && dst_pos == src_pos)
3422 return map;
3423
3424 isl_assert(map->ctx, dst_type != src_type, goto error);
3425
3426 map = isl_map_cow(map);
3427 if (!map)
3428 return NULL;
3429
3430 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
3431 if (!map->dim)
3432 goto error;
3433
3434 for (i = 0; i < map->n; ++i) {
3435 map->p[i] = isl_basic_map_move_dims(map->p[i],
3436 dst_type, dst_pos,
3437 src_type, src_pos, n);
3438 if (!map->p[i])
3439 goto error;
3440 }
3441
3442 return map;
3443error:
3444 isl_map_free(map);
3445 return NULL;
3446}
3447
3448/* Move the specified dimensions to the last columns right before
3449 * the divs. Don't change the dimension specification of bmap.
3450 * That's the responsibility of the caller.
3451 */
3452static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
3453 enum isl_dim_type type, unsigned first, unsigned n)
3454{
3455 struct isl_dim_map *dim_map;
3456 struct isl_basic_map *res;
3457 enum isl_dim_type t;
3458 unsigned total, off;
3459
3460 if (!bmap)
3461 return NULL;
3462 if (pos(bmap->dim, type) + first + n ==
3463 1 + isl_space_dim(bmap->dim, isl_dim_all))
3464 return bmap;
3465
3466 total = isl_basic_map_total_dim(bmap);
3467 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3468
3469 off = 0;
3470 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3471 unsigned size = isl_space_dim(bmap->dim, t);
3472 if (t == type) {
3473 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3474 0, first, off);
3475 off += first;
3476 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3477 first, n, total - bmap->n_div - n);
3478 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3479 first + n, size - (first + n), off);
3480 off += size - (first + n);
3481 } else {
3482 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3483 off += size;
3484 }
3485 }
3486 isl_dim_map_div(dim_map, bmap, off + n);
3487
3488 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3489 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3490 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3491 return res;
3492}
3493
3494/* Insert "n" rows in the divs of "bmap".
3495 *
3496 * The number of columns is not changed, which means that the last
3497 * dimensions of "bmap" are being reintepreted as the new divs.
3498 * The space of "bmap" is not adjusted, however, which means
3499 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
3500 * from the space of "bmap" is the responsibility of the caller.
3501 */
3502static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
3503 int n)
3504{
3505 int i;
3506 size_t row_size;
3507 isl_int **new_div;
3508 isl_int *old;
3509
3510 bmap = isl_basic_map_cow(bmap);
3511 if (!bmap)
3512 return NULL;
3513
3514 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
3515 old = bmap->block2.data;
3516 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
3517 (bmap->extra + n) * (1 + row_size));
3518 if (!bmap->block2.data)
3519 return isl_basic_map_free(bmap);
3520 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
3521 if (!new_div)
3522 return isl_basic_map_free(bmap);
3523 for (i = 0; i < n; ++i) {
3524 new_div[i] = bmap->block2.data +
3525 (bmap->extra + i) * (1 + row_size);
3526 isl_seq_clr(new_div[i], 1 + row_size);
3527 }
3528 for (i = 0; i < bmap->extra; ++i)
3529 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
3530 free(bmap->div);
3531 bmap->div = new_div;
3532 bmap->n_div += n;
3533 bmap->extra += n;
3534
3535 return bmap;
3536}
3537
Tobias Grossera67ac972016-02-26 11:35:12 +00003538/* Drop constraints from "bmap" that only involve the variables
3539 * of "type" in the range [first, first + n] that are not related
3540 * to any of the variables outside that interval.
3541 * These constraints cannot influence the values for the variables
3542 * outside the interval, except in case they cause "bmap" to be empty.
3543 * Only drop the constraints if "bmap" is known to be non-empty.
3544 */
3545static __isl_give isl_basic_map *drop_irrelevant_constraints(
3546 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3547 unsigned first, unsigned n)
3548{
3549 int i;
3550 int *groups;
3551 unsigned dim, n_div;
3552 isl_bool non_empty;
3553
3554 non_empty = isl_basic_map_plain_is_non_empty(bmap);
3555 if (non_empty < 0)
3556 return isl_basic_map_free(bmap);
3557 if (!non_empty)
3558 return bmap;
3559
3560 dim = isl_basic_map_dim(bmap, isl_dim_all);
3561 n_div = isl_basic_map_dim(bmap, isl_dim_div);
3562 groups = isl_calloc_array(isl_basic_map_get_ctx(bmap), int, dim);
3563 if (!groups)
3564 return isl_basic_map_free(bmap);
3565 first += isl_basic_map_offset(bmap, type) - 1;
3566 for (i = 0; i < first; ++i)
3567 groups[i] = -1;
3568 for (i = first + n; i < dim - n_div; ++i)
3569 groups[i] = -1;
3570
3571 bmap = isl_basic_map_drop_unrelated_constraints(bmap, groups);
3572
3573 return bmap;
3574}
3575
Tobias Grosser52a25232015-02-04 20:55:43 +00003576/* Turn the n dimensions of type type, starting at first
3577 * into existentially quantified variables.
Tobias Grossera67ac972016-02-26 11:35:12 +00003578 *
3579 * If a subset of the projected out variables are unrelated
3580 * to any of the variables that remain, then the constraints
3581 * involving this subset are simply dropped first.
Tobias Grosser52a25232015-02-04 20:55:43 +00003582 */
3583__isl_give isl_basic_map *isl_basic_map_project_out(
3584 __isl_take isl_basic_map *bmap,
3585 enum isl_dim_type type, unsigned first, unsigned n)
3586{
3587 if (n == 0)
3588 return basic_map_space_reset(bmap, type);
Tobias Grossera67ac972016-02-26 11:35:12 +00003589 if (type == isl_dim_div)
3590 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3591 "cannot project out existentially quantified variables",
3592 return isl_basic_map_free(bmap));
Tobias Grosser52a25232015-02-04 20:55:43 +00003593
Tobias Grossera67ac972016-02-26 11:35:12 +00003594 bmap = drop_irrelevant_constraints(bmap, type, first, n);
Tobias Grosser52a25232015-02-04 20:55:43 +00003595 if (!bmap)
3596 return NULL;
3597
3598 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
3599 return isl_basic_map_remove_dims(bmap, type, first, n);
3600
3601 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
3602 goto error);
3603
3604 bmap = move_last(bmap, type, first, n);
3605 bmap = isl_basic_map_cow(bmap);
3606 bmap = insert_div_rows(bmap, n);
3607 if (!bmap)
3608 return NULL;
3609
3610 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
3611 if (!bmap->dim)
3612 goto error;
3613 bmap = isl_basic_map_simplify(bmap);
3614 bmap = isl_basic_map_drop_redundant_divs(bmap);
3615 return isl_basic_map_finalize(bmap);
3616error:
3617 isl_basic_map_free(bmap);
3618 return NULL;
3619}
3620
3621/* Turn the n dimensions of type type, starting at first
3622 * into existentially quantified variables.
3623 */
3624struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
3625 enum isl_dim_type type, unsigned first, unsigned n)
3626{
3627 return (isl_basic_set *)isl_basic_map_project_out(
3628 (isl_basic_map *)bset, type, first, n);
3629}
3630
3631/* Turn the n dimensions of type type, starting at first
3632 * into existentially quantified variables.
3633 */
3634__isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
3635 enum isl_dim_type type, unsigned first, unsigned n)
3636{
3637 int i;
3638
3639 if (!map)
3640 return NULL;
3641
3642 if (n == 0)
3643 return map_space_reset(map, type);
3644
3645 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
3646
3647 map = isl_map_cow(map);
3648 if (!map)
3649 return NULL;
3650
3651 map->dim = isl_space_drop_dims(map->dim, type, first, n);
3652 if (!map->dim)
3653 goto error;
3654
3655 for (i = 0; i < map->n; ++i) {
3656 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
3657 if (!map->p[i])
3658 goto error;
3659 }
3660
3661 return map;
3662error:
3663 isl_map_free(map);
3664 return NULL;
3665}
3666
3667/* Turn the n dimensions of type type, starting at first
3668 * into existentially quantified variables.
3669 */
3670__isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
3671 enum isl_dim_type type, unsigned first, unsigned n)
3672{
3673 return (isl_set *)isl_map_project_out((isl_map *)set, type, first, n);
3674}
3675
Michael Krusee0b34f32016-05-04 14:41:36 +00003676/* Return a map that projects the elements in "set" onto their
3677 * "n" set dimensions starting at "first".
3678 * "type" should be equal to isl_dim_set.
3679 */
3680__isl_give isl_map *isl_set_project_onto_map(__isl_take isl_set *set,
3681 enum isl_dim_type type, unsigned first, unsigned n)
3682{
3683 int i;
3684 int dim;
3685 isl_map *map;
3686
3687 if (!set)
3688 return NULL;
3689 if (type != isl_dim_set)
3690 isl_die(isl_set_get_ctx(set), isl_error_invalid,
3691 "only set dimensions can be projected out", goto error);
3692 dim = isl_set_dim(set, isl_dim_set);
3693 if (first + n > dim || first + n < first)
3694 isl_die(isl_set_get_ctx(set), isl_error_invalid,
3695 "index out of bounds", goto error);
3696
3697 map = isl_map_from_domain(set);
3698 map = isl_map_add_dims(map, isl_dim_out, n);
3699 for (i = 0; i < n; ++i)
3700 map = isl_map_equate(map, isl_dim_in, first + i,
3701 isl_dim_out, i);
3702 return map;
3703error:
3704 isl_set_free(set);
3705 return NULL;
3706}
3707
Tobias Grosser52a25232015-02-04 20:55:43 +00003708static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
3709{
3710 int i, j;
3711
3712 for (i = 0; i < n; ++i) {
3713 j = isl_basic_map_alloc_div(bmap);
3714 if (j < 0)
3715 goto error;
3716 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
3717 }
3718 return bmap;
3719error:
3720 isl_basic_map_free(bmap);
3721 return NULL;
3722}
3723
3724struct isl_basic_map *isl_basic_map_apply_range(
3725 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3726{
3727 isl_space *dim_result = NULL;
3728 struct isl_basic_map *bmap;
3729 unsigned n_in, n_out, n, nparam, total, pos;
3730 struct isl_dim_map *dim_map1, *dim_map2;
3731
3732 if (!bmap1 || !bmap2)
3733 goto error;
3734 if (!isl_space_match(bmap1->dim, isl_dim_param,
3735 bmap2->dim, isl_dim_param))
3736 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
3737 "parameters don't match", goto error);
3738 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_out,
3739 bmap2->dim, isl_dim_in))
3740 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
3741 "spaces don't match", goto error);
3742
3743 dim_result = isl_space_join(isl_space_copy(bmap1->dim),
3744 isl_space_copy(bmap2->dim));
3745
3746 n_in = isl_basic_map_n_in(bmap1);
3747 n_out = isl_basic_map_n_out(bmap2);
3748 n = isl_basic_map_n_out(bmap1);
3749 nparam = isl_basic_map_n_param(bmap1);
3750
3751 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
3752 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3753 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
3754 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3755 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
3756 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3757 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
3758 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
3759 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3760 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3761 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3762
3763 bmap = isl_basic_map_alloc_space(dim_result,
3764 bmap1->n_div + bmap2->n_div + n,
3765 bmap1->n_eq + bmap2->n_eq,
3766 bmap1->n_ineq + bmap2->n_ineq);
3767 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3768 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3769 bmap = add_divs(bmap, n);
3770 bmap = isl_basic_map_simplify(bmap);
3771 bmap = isl_basic_map_drop_redundant_divs(bmap);
3772 return isl_basic_map_finalize(bmap);
3773error:
3774 isl_basic_map_free(bmap1);
3775 isl_basic_map_free(bmap2);
3776 return NULL;
3777}
3778
3779struct isl_basic_set *isl_basic_set_apply(
3780 struct isl_basic_set *bset, struct isl_basic_map *bmap)
3781{
3782 if (!bset || !bmap)
3783 goto error;
3784
3785 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
3786 goto error);
3787
3788 return (struct isl_basic_set *)
3789 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
3790error:
3791 isl_basic_set_free(bset);
3792 isl_basic_map_free(bmap);
3793 return NULL;
3794}
3795
3796struct isl_basic_map *isl_basic_map_apply_domain(
3797 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3798{
3799 if (!bmap1 || !bmap2)
3800 goto error;
3801
3802 isl_assert(bmap1->ctx,
3803 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
3804 isl_assert(bmap1->ctx,
3805 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
3806 goto error);
3807
3808 bmap1 = isl_basic_map_reverse(bmap1);
3809 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
3810 return isl_basic_map_reverse(bmap1);
3811error:
3812 isl_basic_map_free(bmap1);
3813 isl_basic_map_free(bmap2);
3814 return NULL;
3815}
3816
3817/* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3818 * A \cap B -> f(A) + f(B)
3819 */
3820struct isl_basic_map *isl_basic_map_sum(
3821 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3822{
3823 unsigned n_in, n_out, nparam, total, pos;
3824 struct isl_basic_map *bmap = NULL;
3825 struct isl_dim_map *dim_map1, *dim_map2;
3826 int i;
3827
3828 if (!bmap1 || !bmap2)
3829 goto error;
3830
3831 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
3832 goto error);
3833
3834 nparam = isl_basic_map_n_param(bmap1);
3835 n_in = isl_basic_map_n_in(bmap1);
3836 n_out = isl_basic_map_n_out(bmap1);
3837
3838 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
3839 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3840 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
3841 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3842 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
3843 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3844 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3845 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
3846 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3847 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3848 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
3849
3850 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
3851 bmap1->n_div + bmap2->n_div + 2 * n_out,
3852 bmap1->n_eq + bmap2->n_eq + n_out,
3853 bmap1->n_ineq + bmap2->n_ineq);
3854 for (i = 0; i < n_out; ++i) {
3855 int j = isl_basic_map_alloc_equality(bmap);
3856 if (j < 0)
3857 goto error;
3858 isl_seq_clr(bmap->eq[j], 1+total);
3859 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
3860 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
3861 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
3862 }
3863 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3864 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3865 bmap = add_divs(bmap, 2 * n_out);
3866
3867 bmap = isl_basic_map_simplify(bmap);
3868 return isl_basic_map_finalize(bmap);
3869error:
3870 isl_basic_map_free(bmap);
3871 isl_basic_map_free(bmap1);
3872 isl_basic_map_free(bmap2);
3873 return NULL;
3874}
3875
3876/* Given two maps A -> f(A) and B -> g(B), construct a map
3877 * A \cap B -> f(A) + f(B)
3878 */
3879struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
3880{
3881 struct isl_map *result;
3882 int i, j;
3883
3884 if (!map1 || !map2)
3885 goto error;
3886
3887 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
3888
3889 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3890 map1->n * map2->n, 0);
3891 if (!result)
3892 goto error;
3893 for (i = 0; i < map1->n; ++i)
3894 for (j = 0; j < map2->n; ++j) {
3895 struct isl_basic_map *part;
3896 part = isl_basic_map_sum(
3897 isl_basic_map_copy(map1->p[i]),
3898 isl_basic_map_copy(map2->p[j]));
3899 if (isl_basic_map_is_empty(part))
3900 isl_basic_map_free(part);
3901 else
3902 result = isl_map_add_basic_map(result, part);
3903 if (!result)
3904 goto error;
3905 }
3906 isl_map_free(map1);
3907 isl_map_free(map2);
3908 return result;
3909error:
3910 isl_map_free(map1);
3911 isl_map_free(map2);
3912 return NULL;
3913}
3914
3915__isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
3916 __isl_take isl_set *set2)
3917{
3918 return (isl_set *)isl_map_sum((isl_map *)set1, (isl_map *)set2);
3919}
3920
3921/* Given a basic map A -> f(A), construct A -> -f(A).
3922 */
3923struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
3924{
3925 int i, j;
3926 unsigned off, n;
3927
3928 bmap = isl_basic_map_cow(bmap);
3929 if (!bmap)
3930 return NULL;
3931
3932 n = isl_basic_map_dim(bmap, isl_dim_out);
3933 off = isl_basic_map_offset(bmap, isl_dim_out);
3934 for (i = 0; i < bmap->n_eq; ++i)
3935 for (j = 0; j < n; ++j)
3936 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
3937 for (i = 0; i < bmap->n_ineq; ++i)
3938 for (j = 0; j < n; ++j)
3939 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
3940 for (i = 0; i < bmap->n_div; ++i)
3941 for (j = 0; j < n; ++j)
3942 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
3943 bmap = isl_basic_map_gauss(bmap, NULL);
3944 return isl_basic_map_finalize(bmap);
3945}
3946
3947__isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
3948{
3949 return isl_basic_map_neg(bset);
3950}
3951
3952/* Given a map A -> f(A), construct A -> -f(A).
3953 */
3954struct isl_map *isl_map_neg(struct isl_map *map)
3955{
3956 int i;
3957
3958 map = isl_map_cow(map);
3959 if (!map)
3960 return NULL;
3961
3962 for (i = 0; i < map->n; ++i) {
3963 map->p[i] = isl_basic_map_neg(map->p[i]);
3964 if (!map->p[i])
3965 goto error;
3966 }
3967
3968 return map;
3969error:
3970 isl_map_free(map);
3971 return NULL;
3972}
3973
3974__isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
3975{
3976 return (isl_set *)isl_map_neg((isl_map *)set);
3977}
3978
3979/* Given a basic map A -> f(A) and an integer d, construct a basic map
3980 * A -> floor(f(A)/d).
3981 */
3982struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
3983 isl_int d)
3984{
3985 unsigned n_in, n_out, nparam, total, pos;
3986 struct isl_basic_map *result = NULL;
3987 struct isl_dim_map *dim_map;
3988 int i;
3989
3990 if (!bmap)
3991 return NULL;
3992
3993 nparam = isl_basic_map_n_param(bmap);
3994 n_in = isl_basic_map_n_in(bmap);
3995 n_out = isl_basic_map_n_out(bmap);
3996
3997 total = nparam + n_in + n_out + bmap->n_div + n_out;
3998 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3999 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
4000 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
4001 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
4002 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
4003
4004 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
4005 bmap->n_div + n_out,
4006 bmap->n_eq, bmap->n_ineq + 2 * n_out);
4007 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
4008 result = add_divs(result, n_out);
4009 for (i = 0; i < n_out; ++i) {
4010 int j;
4011 j = isl_basic_map_alloc_inequality(result);
4012 if (j < 0)
4013 goto error;
4014 isl_seq_clr(result->ineq[j], 1+total);
4015 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
4016 isl_int_set_si(result->ineq[j][1+pos+i], 1);
4017 j = isl_basic_map_alloc_inequality(result);
4018 if (j < 0)
4019 goto error;
4020 isl_seq_clr(result->ineq[j], 1+total);
4021 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
4022 isl_int_set_si(result->ineq[j][1+pos+i], -1);
4023 isl_int_sub_ui(result->ineq[j][0], d, 1);
4024 }
4025
4026 result = isl_basic_map_simplify(result);
4027 return isl_basic_map_finalize(result);
4028error:
4029 isl_basic_map_free(result);
4030 return NULL;
4031}
4032
4033/* Given a map A -> f(A) and an integer d, construct a map
4034 * A -> floor(f(A)/d).
4035 */
4036struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
4037{
4038 int i;
4039
4040 map = isl_map_cow(map);
4041 if (!map)
4042 return NULL;
4043
4044 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4045 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4046 for (i = 0; i < map->n; ++i) {
4047 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
4048 if (!map->p[i])
4049 goto error;
4050 }
4051
4052 return map;
4053error:
4054 isl_map_free(map);
4055 return NULL;
4056}
4057
4058/* Given a map A -> f(A) and an integer d, construct a map
4059 * A -> floor(f(A)/d).
4060 */
4061__isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
4062 __isl_take isl_val *d)
4063{
4064 if (!map || !d)
4065 goto error;
4066 if (!isl_val_is_int(d))
4067 isl_die(isl_val_get_ctx(d), isl_error_invalid,
4068 "expecting integer denominator", goto error);
4069 map = isl_map_floordiv(map, d->n);
4070 isl_val_free(d);
4071 return map;
4072error:
4073 isl_map_free(map);
4074 isl_val_free(d);
4075 return NULL;
4076}
4077
4078static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
4079{
4080 int i;
4081 unsigned nparam;
4082 unsigned n_in;
4083
4084 i = isl_basic_map_alloc_equality(bmap);
4085 if (i < 0)
4086 goto error;
4087 nparam = isl_basic_map_n_param(bmap);
4088 n_in = isl_basic_map_n_in(bmap);
4089 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
4090 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
4091 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
4092 return isl_basic_map_finalize(bmap);
4093error:
4094 isl_basic_map_free(bmap);
4095 return NULL;
4096}
4097
4098/* Add a constraints to "bmap" expressing i_pos < o_pos
4099 */
4100static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
4101{
4102 int i;
4103 unsigned nparam;
4104 unsigned n_in;
4105
4106 i = isl_basic_map_alloc_inequality(bmap);
4107 if (i < 0)
4108 goto error;
4109 nparam = isl_basic_map_n_param(bmap);
4110 n_in = isl_basic_map_n_in(bmap);
4111 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4112 isl_int_set_si(bmap->ineq[i][0], -1);
4113 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4114 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4115 return isl_basic_map_finalize(bmap);
4116error:
4117 isl_basic_map_free(bmap);
4118 return NULL;
4119}
4120
4121/* Add a constraint to "bmap" expressing i_pos <= o_pos
4122 */
4123static __isl_give isl_basic_map *var_less_or_equal(
4124 __isl_take isl_basic_map *bmap, unsigned pos)
4125{
4126 int i;
4127 unsigned nparam;
4128 unsigned n_in;
4129
4130 i = isl_basic_map_alloc_inequality(bmap);
4131 if (i < 0)
4132 goto error;
4133 nparam = isl_basic_map_n_param(bmap);
4134 n_in = isl_basic_map_n_in(bmap);
4135 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4136 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4137 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4138 return isl_basic_map_finalize(bmap);
4139error:
4140 isl_basic_map_free(bmap);
4141 return NULL;
4142}
4143
4144/* Add a constraints to "bmap" expressing i_pos > o_pos
4145 */
4146static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
4147{
4148 int i;
4149 unsigned nparam;
4150 unsigned n_in;
4151
4152 i = isl_basic_map_alloc_inequality(bmap);
4153 if (i < 0)
4154 goto error;
4155 nparam = isl_basic_map_n_param(bmap);
4156 n_in = isl_basic_map_n_in(bmap);
4157 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4158 isl_int_set_si(bmap->ineq[i][0], -1);
4159 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4160 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4161 return isl_basic_map_finalize(bmap);
4162error:
4163 isl_basic_map_free(bmap);
4164 return NULL;
4165}
4166
4167/* Add a constraint to "bmap" expressing i_pos >= o_pos
4168 */
4169static __isl_give isl_basic_map *var_more_or_equal(
4170 __isl_take isl_basic_map *bmap, unsigned pos)
4171{
4172 int i;
4173 unsigned nparam;
4174 unsigned n_in;
4175
4176 i = isl_basic_map_alloc_inequality(bmap);
4177 if (i < 0)
4178 goto error;
4179 nparam = isl_basic_map_n_param(bmap);
4180 n_in = isl_basic_map_n_in(bmap);
4181 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4182 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4183 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4184 return isl_basic_map_finalize(bmap);
4185error:
4186 isl_basic_map_free(bmap);
4187 return NULL;
4188}
4189
4190__isl_give isl_basic_map *isl_basic_map_equal(
4191 __isl_take isl_space *dim, unsigned n_equal)
4192{
4193 int i;
4194 struct isl_basic_map *bmap;
4195 bmap = isl_basic_map_alloc_space(dim, 0, n_equal, 0);
4196 if (!bmap)
4197 return NULL;
4198 for (i = 0; i < n_equal && bmap; ++i)
4199 bmap = var_equal(bmap, i);
4200 return isl_basic_map_finalize(bmap);
4201}
4202
4203/* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4204 */
4205__isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *dim,
4206 unsigned pos)
4207{
4208 int i;
4209 struct isl_basic_map *bmap;
4210 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4211 if (!bmap)
4212 return NULL;
4213 for (i = 0; i < pos && bmap; ++i)
4214 bmap = var_equal(bmap, i);
4215 if (bmap)
4216 bmap = var_less(bmap, pos);
4217 return isl_basic_map_finalize(bmap);
4218}
4219
4220/* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
4221 */
4222__isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
4223 __isl_take isl_space *dim, unsigned pos)
4224{
4225 int i;
4226 isl_basic_map *bmap;
4227
4228 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4229 for (i = 0; i < pos; ++i)
4230 bmap = var_equal(bmap, i);
4231 bmap = var_less_or_equal(bmap, pos);
4232 return isl_basic_map_finalize(bmap);
4233}
4234
4235/* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
4236 */
4237__isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *dim,
4238 unsigned pos)
4239{
4240 int i;
4241 struct isl_basic_map *bmap;
4242 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4243 if (!bmap)
4244 return NULL;
4245 for (i = 0; i < pos && bmap; ++i)
4246 bmap = var_equal(bmap, i);
4247 if (bmap)
4248 bmap = var_more(bmap, pos);
4249 return isl_basic_map_finalize(bmap);
4250}
4251
4252/* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
4253 */
4254__isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
4255 __isl_take isl_space *dim, unsigned pos)
4256{
4257 int i;
4258 isl_basic_map *bmap;
4259
4260 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4261 for (i = 0; i < pos; ++i)
4262 bmap = var_equal(bmap, i);
4263 bmap = var_more_or_equal(bmap, pos);
4264 return isl_basic_map_finalize(bmap);
4265}
4266
4267static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *dims,
4268 unsigned n, int equal)
4269{
4270 struct isl_map *map;
4271 int i;
4272
4273 if (n == 0 && equal)
4274 return isl_map_universe(dims);
4275
4276 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4277
4278 for (i = 0; i + 1 < n; ++i)
4279 map = isl_map_add_basic_map(map,
4280 isl_basic_map_less_at(isl_space_copy(dims), i));
4281 if (n > 0) {
4282 if (equal)
4283 map = isl_map_add_basic_map(map,
4284 isl_basic_map_less_or_equal_at(dims, n - 1));
4285 else
4286 map = isl_map_add_basic_map(map,
4287 isl_basic_map_less_at(dims, n - 1));
4288 } else
4289 isl_space_free(dims);
4290
4291 return map;
4292}
4293
4294static __isl_give isl_map *map_lex_lte(__isl_take isl_space *dims, int equal)
4295{
4296 if (!dims)
4297 return NULL;
4298 return map_lex_lte_first(dims, dims->n_out, equal);
4299}
4300
4301__isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
4302{
4303 return map_lex_lte_first(dim, n, 0);
4304}
4305
4306__isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
4307{
4308 return map_lex_lte_first(dim, n, 1);
4309}
4310
4311__isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
4312{
4313 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
4314}
4315
4316__isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
4317{
4318 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
4319}
4320
4321static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *dims,
4322 unsigned n, int equal)
4323{
4324 struct isl_map *map;
4325 int i;
4326
4327 if (n == 0 && equal)
4328 return isl_map_universe(dims);
4329
4330 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4331
4332 for (i = 0; i + 1 < n; ++i)
4333 map = isl_map_add_basic_map(map,
4334 isl_basic_map_more_at(isl_space_copy(dims), i));
4335 if (n > 0) {
4336 if (equal)
4337 map = isl_map_add_basic_map(map,
4338 isl_basic_map_more_or_equal_at(dims, n - 1));
4339 else
4340 map = isl_map_add_basic_map(map,
4341 isl_basic_map_more_at(dims, n - 1));
4342 } else
4343 isl_space_free(dims);
4344
4345 return map;
4346}
4347
4348static __isl_give isl_map *map_lex_gte(__isl_take isl_space *dims, int equal)
4349{
4350 if (!dims)
4351 return NULL;
4352 return map_lex_gte_first(dims, dims->n_out, equal);
4353}
4354
4355__isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
4356{
4357 return map_lex_gte_first(dim, n, 0);
4358}
4359
4360__isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
4361{
4362 return map_lex_gte_first(dim, n, 1);
4363}
4364
4365__isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
4366{
4367 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
4368}
4369
4370__isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
4371{
4372 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
4373}
4374
4375__isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
4376 __isl_take isl_set *set2)
4377{
4378 isl_map *map;
4379 map = isl_map_lex_le(isl_set_get_space(set1));
4380 map = isl_map_intersect_domain(map, set1);
4381 map = isl_map_intersect_range(map, set2);
4382 return map;
4383}
4384
4385__isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
4386 __isl_take isl_set *set2)
4387{
4388 isl_map *map;
4389 map = isl_map_lex_lt(isl_set_get_space(set1));
4390 map = isl_map_intersect_domain(map, set1);
4391 map = isl_map_intersect_range(map, set2);
4392 return map;
4393}
4394
4395__isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
4396 __isl_take isl_set *set2)
4397{
4398 isl_map *map;
4399 map = isl_map_lex_ge(isl_set_get_space(set1));
4400 map = isl_map_intersect_domain(map, set1);
4401 map = isl_map_intersect_range(map, set2);
4402 return map;
4403}
4404
4405__isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
4406 __isl_take isl_set *set2)
4407{
4408 isl_map *map;
4409 map = isl_map_lex_gt(isl_set_get_space(set1));
4410 map = isl_map_intersect_domain(map, set1);
4411 map = isl_map_intersect_range(map, set2);
4412 return map;
4413}
4414
4415__isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
4416 __isl_take isl_map *map2)
4417{
4418 isl_map *map;
4419 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
4420 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4421 map = isl_map_apply_range(map, isl_map_reverse(map2));
4422 return map;
4423}
4424
4425__isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
4426 __isl_take isl_map *map2)
4427{
4428 isl_map *map;
4429 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
4430 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4431 map = isl_map_apply_range(map, isl_map_reverse(map2));
4432 return map;
4433}
4434
4435__isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
4436 __isl_take isl_map *map2)
4437{
4438 isl_map *map;
4439 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
4440 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4441 map = isl_map_apply_range(map, isl_map_reverse(map2));
4442 return map;
4443}
4444
4445__isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
4446 __isl_take isl_map *map2)
4447{
4448 isl_map *map;
4449 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
4450 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4451 map = isl_map_apply_range(map, isl_map_reverse(map2));
4452 return map;
4453}
4454
4455__isl_give isl_basic_map *isl_basic_map_from_basic_set(
4456 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4457{
4458 struct isl_basic_map *bmap;
4459
4460 bset = isl_basic_set_cow(bset);
4461 if (!bset || !dim)
4462 goto error;
4463
4464 isl_assert(bset->ctx, isl_space_compatible(bset->dim, dim), goto error);
4465 isl_space_free(bset->dim);
4466 bmap = (struct isl_basic_map *) bset;
4467 bmap->dim = dim;
4468 return isl_basic_map_finalize(bmap);
4469error:
4470 isl_basic_set_free(bset);
4471 isl_space_free(dim);
4472 return NULL;
4473}
4474
Tobias Grosser52a25232015-02-04 20:55:43 +00004475/* For a div d = floor(f/m), add the constraint
4476 *
4477 * f - m d >= 0
4478 */
4479static int add_upper_div_constraint(__isl_keep isl_basic_map *bmap,
4480 unsigned pos, isl_int *div)
4481{
4482 int i;
4483 unsigned total = isl_basic_map_total_dim(bmap);
4484
4485 i = isl_basic_map_alloc_inequality(bmap);
4486 if (i < 0)
4487 return -1;
4488 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
4489 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
4490
4491 return 0;
4492}
4493
4494/* For a div d = floor(f/m), add the constraint
4495 *
4496 * -(f-(n-1)) + m d >= 0
4497 */
4498static int add_lower_div_constraint(__isl_keep isl_basic_map *bmap,
4499 unsigned pos, isl_int *div)
4500{
4501 int i;
4502 unsigned total = isl_basic_map_total_dim(bmap);
4503
4504 i = isl_basic_map_alloc_inequality(bmap);
4505 if (i < 0)
4506 return -1;
4507 isl_seq_neg(bmap->ineq[i], div + 1, 1 + total);
4508 isl_int_set(bmap->ineq[i][1 + pos], div[0]);
4509 isl_int_add(bmap->ineq[i][0], bmap->ineq[i][0], bmap->ineq[i][1 + pos]);
4510 isl_int_sub_ui(bmap->ineq[i][0], bmap->ineq[i][0], 1);
4511
4512 return 0;
4513}
4514
4515/* For a div d = floor(f/m), add the constraints
4516 *
4517 * f - m d >= 0
4518 * -(f-(n-1)) + m d >= 0
4519 *
4520 * Note that the second constraint is the negation of
4521 *
4522 * f - m d >= n
4523 */
4524int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
4525 unsigned pos, isl_int *div)
4526{
4527 if (add_upper_div_constraint(bmap, pos, div) < 0)
4528 return -1;
4529 if (add_lower_div_constraint(bmap, pos, div) < 0)
4530 return -1;
4531 return 0;
4532}
4533
4534int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
4535 unsigned pos, isl_int *div)
4536{
4537 return isl_basic_map_add_div_constraints_var((isl_basic_map *)bset,
4538 pos, div);
4539}
4540
4541int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
4542{
4543 unsigned total = isl_basic_map_total_dim(bmap);
4544 unsigned div_pos = total - bmap->n_div + div;
4545
4546 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
4547 bmap->div[div]);
4548}
4549
4550/* For each known div d = floor(f/m), add the constraints
4551 *
4552 * f - m d >= 0
4553 * -(f-(n-1)) + m d >= 0
Michael Kruse959a8dc2016-01-15 15:54:45 +00004554 *
4555 * Remove duplicate constraints in case of some these div constraints
4556 * already appear in "bmap".
Tobias Grosser52a25232015-02-04 20:55:43 +00004557 */
4558__isl_give isl_basic_map *isl_basic_map_add_known_div_constraints(
4559 __isl_take isl_basic_map *bmap)
4560{
Tobias Grosser52a25232015-02-04 20:55:43 +00004561 unsigned n_div;
4562
4563 if (!bmap)
4564 return NULL;
4565 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4566 if (n_div == 0)
4567 return bmap;
Tobias Grosser52a25232015-02-04 20:55:43 +00004568
Michael Kruse959a8dc2016-01-15 15:54:45 +00004569 bmap = add_known_div_constraints(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +00004570 bmap = isl_basic_map_remove_duplicate_constraints(bmap, NULL, 0);
4571 bmap = isl_basic_map_finalize(bmap);
4572 return bmap;
4573}
4574
4575/* Add the div constraint of sign "sign" for div "div" of "bmap".
4576 *
4577 * In particular, if this div is of the form d = floor(f/m),
4578 * then add the constraint
4579 *
4580 * f - m d >= 0
4581 *
4582 * if sign < 0 or the constraint
4583 *
4584 * -(f-(n-1)) + m d >= 0
4585 *
4586 * if sign > 0.
4587 */
4588int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map *bmap,
4589 unsigned div, int sign)
4590{
4591 unsigned total;
4592 unsigned div_pos;
4593
4594 if (!bmap)
4595 return -1;
4596
4597 total = isl_basic_map_total_dim(bmap);
4598 div_pos = total - bmap->n_div + div;
4599
4600 if (sign < 0)
4601 return add_upper_div_constraint(bmap, div_pos, bmap->div[div]);
4602 else
4603 return add_lower_div_constraint(bmap, div_pos, bmap->div[div]);
4604}
4605
4606int isl_basic_set_add_div_constraints(struct isl_basic_set *bset, unsigned div)
4607{
4608 return isl_basic_map_add_div_constraints(bset, div);
4609}
4610
4611struct isl_basic_set *isl_basic_map_underlying_set(
4612 struct isl_basic_map *bmap)
4613{
4614 if (!bmap)
4615 goto error;
4616 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
4617 bmap->n_div == 0 &&
4618 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
4619 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
4620 return (struct isl_basic_set *)bmap;
4621 bmap = isl_basic_map_cow(bmap);
4622 if (!bmap)
4623 goto error;
4624 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
4625 if (!bmap->dim)
4626 goto error;
4627 bmap->extra -= bmap->n_div;
4628 bmap->n_div = 0;
4629 bmap = isl_basic_map_finalize(bmap);
4630 return (struct isl_basic_set *)bmap;
4631error:
4632 isl_basic_map_free(bmap);
4633 return NULL;
4634}
4635
4636__isl_give isl_basic_set *isl_basic_set_underlying_set(
4637 __isl_take isl_basic_set *bset)
4638{
4639 return isl_basic_map_underlying_set((isl_basic_map *)bset);
4640}
4641
4642/* Replace each element in "list" by the result of applying
Tobias Grosser1fa7b972015-02-16 19:33:40 +00004643 * isl_basic_map_underlying_set to the element.
Tobias Grosser52a25232015-02-04 20:55:43 +00004644 */
Tobias Grosser1fa7b972015-02-16 19:33:40 +00004645__isl_give isl_basic_set_list *isl_basic_map_list_underlying_set(
4646 __isl_take isl_basic_map_list *list)
Tobias Grosser52a25232015-02-04 20:55:43 +00004647{
4648 int i, n;
4649
4650 if (!list)
4651 return NULL;
4652
Tobias Grosser1fa7b972015-02-16 19:33:40 +00004653 n = isl_basic_map_list_n_basic_map(list);
Tobias Grosser52a25232015-02-04 20:55:43 +00004654 for (i = 0; i < n; ++i) {
Tobias Grosser1fa7b972015-02-16 19:33:40 +00004655 isl_basic_map *bmap;
Tobias Grosser52a25232015-02-04 20:55:43 +00004656 isl_basic_set *bset;
4657
Tobias Grosser1fa7b972015-02-16 19:33:40 +00004658 bmap = isl_basic_map_list_get_basic_map(list, i);
4659 bset = isl_basic_set_underlying_set(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +00004660 list = isl_basic_set_list_set_basic_set(list, i, bset);
4661 }
4662
4663 return list;
4664}
4665
4666struct isl_basic_map *isl_basic_map_overlying_set(
4667 struct isl_basic_set *bset, struct isl_basic_map *like)
4668{
4669 struct isl_basic_map *bmap;
4670 struct isl_ctx *ctx;
4671 unsigned total;
4672 int i;
4673
4674 if (!bset || !like)
4675 goto error;
4676 ctx = bset->ctx;
4677 isl_assert(ctx, bset->n_div == 0, goto error);
4678 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
4679 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
4680 goto error);
Tobias Grosser6e3ba332015-08-11 11:31:18 +00004681 if (like->n_div == 0) {
4682 isl_space *space = isl_basic_map_get_space(like);
Tobias Grosser52a25232015-02-04 20:55:43 +00004683 isl_basic_map_free(like);
Tobias Grosser6e3ba332015-08-11 11:31:18 +00004684 return isl_basic_map_reset_space(bset, space);
Tobias Grosser52a25232015-02-04 20:55:43 +00004685 }
4686 bset = isl_basic_set_cow(bset);
4687 if (!bset)
4688 goto error;
4689 total = bset->dim->n_out + bset->extra;
4690 bmap = (struct isl_basic_map *)bset;
4691 isl_space_free(bmap->dim);
4692 bmap->dim = isl_space_copy(like->dim);
4693 if (!bmap->dim)
4694 goto error;
4695 bmap->n_div = like->n_div;
4696 bmap->extra += like->n_div;
4697 if (bmap->extra) {
4698 unsigned ltotal;
4699 isl_int **div;
4700 ltotal = total - bmap->extra + like->extra;
4701 if (ltotal > total)
4702 ltotal = total;
4703 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
4704 bmap->extra * (1 + 1 + total));
4705 if (isl_blk_is_error(bmap->block2))
4706 goto error;
4707 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
4708 if (!div)
4709 goto error;
4710 bmap->div = div;
4711 for (i = 0; i < bmap->extra; ++i)
4712 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
4713 for (i = 0; i < like->n_div; ++i) {
4714 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
4715 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
4716 }
4717 bmap = isl_basic_map_add_known_div_constraints(bmap);
4718 }
4719 isl_basic_map_free(like);
4720 bmap = isl_basic_map_simplify(bmap);
4721 bmap = isl_basic_map_finalize(bmap);
4722 return bmap;
4723error:
4724 isl_basic_map_free(like);
4725 isl_basic_set_free(bset);
4726 return NULL;
4727}
4728
4729struct isl_basic_set *isl_basic_set_from_underlying_set(
4730 struct isl_basic_set *bset, struct isl_basic_set *like)
4731{
4732 return (struct isl_basic_set *)
4733 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
4734}
4735
4736struct isl_set *isl_set_from_underlying_set(
4737 struct isl_set *set, struct isl_basic_set *like)
4738{
4739 int i;
4740
4741 if (!set || !like)
4742 goto error;
4743 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
4744 goto error);
4745 if (isl_space_is_equal(set->dim, like->dim) && like->n_div == 0) {
4746 isl_basic_set_free(like);
4747 return set;
4748 }
4749 set = isl_set_cow(set);
4750 if (!set)
4751 goto error;
4752 for (i = 0; i < set->n; ++i) {
4753 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
4754 isl_basic_set_copy(like));
4755 if (!set->p[i])
4756 goto error;
4757 }
4758 isl_space_free(set->dim);
4759 set->dim = isl_space_copy(like->dim);
4760 if (!set->dim)
4761 goto error;
4762 isl_basic_set_free(like);
4763 return set;
4764error:
4765 isl_basic_set_free(like);
4766 isl_set_free(set);
4767 return NULL;
4768}
4769
4770struct isl_set *isl_map_underlying_set(struct isl_map *map)
4771{
4772 int i;
4773
4774 map = isl_map_cow(map);
4775 if (!map)
4776 return NULL;
4777 map->dim = isl_space_cow(map->dim);
4778 if (!map->dim)
4779 goto error;
4780
4781 for (i = 1; i < map->n; ++i)
4782 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
4783 goto error);
4784 for (i = 0; i < map->n; ++i) {
4785 map->p[i] = (struct isl_basic_map *)
4786 isl_basic_map_underlying_set(map->p[i]);
4787 if (!map->p[i])
4788 goto error;
4789 }
4790 if (map->n == 0)
4791 map->dim = isl_space_underlying(map->dim, 0);
4792 else {
4793 isl_space_free(map->dim);
4794 map->dim = isl_space_copy(map->p[0]->dim);
4795 }
4796 if (!map->dim)
4797 goto error;
4798 return (struct isl_set *)map;
4799error:
4800 isl_map_free(map);
4801 return NULL;
4802}
4803
4804struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
4805{
4806 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
4807}
4808
Michael Kruse959a8dc2016-01-15 15:54:45 +00004809/* Replace the space of "bmap" by "space".
4810 *
4811 * If the space of "bmap" is identical to "space" (including the identifiers
4812 * of the input and output dimensions), then simply return the original input.
4813 */
Tobias Grosser52a25232015-02-04 20:55:43 +00004814__isl_give isl_basic_map *isl_basic_map_reset_space(
Tobias Grosser6e3ba332015-08-11 11:31:18 +00004815 __isl_take isl_basic_map *bmap, __isl_take isl_space *space)
Tobias Grosser52a25232015-02-04 20:55:43 +00004816{
Tobias Grosser6e3ba332015-08-11 11:31:18 +00004817 isl_bool equal;
4818
4819 if (!bmap)
4820 goto error;
4821 equal = isl_space_is_equal(bmap->dim, space);
Michael Kruse959a8dc2016-01-15 15:54:45 +00004822 if (equal >= 0 && equal)
4823 equal = isl_space_match(bmap->dim, isl_dim_in,
4824 space, isl_dim_in);
4825 if (equal >= 0 && equal)
4826 equal = isl_space_match(bmap->dim, isl_dim_out,
4827 space, isl_dim_out);
Tobias Grosser6e3ba332015-08-11 11:31:18 +00004828 if (equal < 0)
4829 goto error;
4830 if (equal) {
4831 isl_space_free(space);
4832 return bmap;
4833 }
Tobias Grosser52a25232015-02-04 20:55:43 +00004834 bmap = isl_basic_map_cow(bmap);
Tobias Grosser6e3ba332015-08-11 11:31:18 +00004835 if (!bmap || !space)
Tobias Grosser52a25232015-02-04 20:55:43 +00004836 goto error;
4837
4838 isl_space_free(bmap->dim);
Tobias Grosser6e3ba332015-08-11 11:31:18 +00004839 bmap->dim = space;
Tobias Grosser52a25232015-02-04 20:55:43 +00004840
4841 bmap = isl_basic_map_finalize(bmap);
4842
4843 return bmap;
4844error:
4845 isl_basic_map_free(bmap);
Tobias Grosser6e3ba332015-08-11 11:31:18 +00004846 isl_space_free(space);
Tobias Grosser52a25232015-02-04 20:55:43 +00004847 return NULL;
4848}
4849
4850__isl_give isl_basic_set *isl_basic_set_reset_space(
4851 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4852{
4853 return (isl_basic_set *)isl_basic_map_reset_space((isl_basic_map *)bset,
4854 dim);
4855}
4856
4857__isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
4858 __isl_take isl_space *dim)
4859{
4860 int i;
4861
4862 map = isl_map_cow(map);
4863 if (!map || !dim)
4864 goto error;
4865
4866 for (i = 0; i < map->n; ++i) {
4867 map->p[i] = isl_basic_map_reset_space(map->p[i],
4868 isl_space_copy(dim));
4869 if (!map->p[i])
4870 goto error;
4871 }
4872 isl_space_free(map->dim);
4873 map->dim = dim;
4874
4875 return map;
4876error:
4877 isl_map_free(map);
4878 isl_space_free(dim);
4879 return NULL;
4880}
4881
4882__isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
4883 __isl_take isl_space *dim)
4884{
4885 return (struct isl_set *) isl_map_reset_space((struct isl_map *)set, dim);
4886}
4887
4888/* Compute the parameter domain of the given basic set.
4889 */
4890__isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
4891{
4892 isl_space *space;
4893 unsigned n;
4894
4895 if (isl_basic_set_is_params(bset))
4896 return bset;
4897
4898 n = isl_basic_set_dim(bset, isl_dim_set);
4899 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
4900 space = isl_basic_set_get_space(bset);
4901 space = isl_space_params(space);
4902 bset = isl_basic_set_reset_space(bset, space);
4903 return bset;
4904}
4905
4906/* Construct a zero-dimensional basic set with the given parameter domain.
4907 */
4908__isl_give isl_basic_set *isl_basic_set_from_params(
4909 __isl_take isl_basic_set *bset)
4910{
4911 isl_space *space;
4912 space = isl_basic_set_get_space(bset);
4913 space = isl_space_set_from_params(space);
4914 bset = isl_basic_set_reset_space(bset, space);
4915 return bset;
4916}
4917
4918/* Compute the parameter domain of the given set.
4919 */
4920__isl_give isl_set *isl_set_params(__isl_take isl_set *set)
4921{
4922 isl_space *space;
4923 unsigned n;
4924
4925 if (isl_set_is_params(set))
4926 return set;
4927
4928 n = isl_set_dim(set, isl_dim_set);
4929 set = isl_set_project_out(set, isl_dim_set, 0, n);
4930 space = isl_set_get_space(set);
4931 space = isl_space_params(space);
4932 set = isl_set_reset_space(set, space);
4933 return set;
4934}
4935
4936/* Construct a zero-dimensional set with the given parameter domain.
4937 */
4938__isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
4939{
4940 isl_space *space;
4941 space = isl_set_get_space(set);
4942 space = isl_space_set_from_params(space);
4943 set = isl_set_reset_space(set, space);
4944 return set;
4945}
4946
4947/* Compute the parameter domain of the given map.
4948 */
4949__isl_give isl_set *isl_map_params(__isl_take isl_map *map)
4950{
4951 isl_space *space;
4952 unsigned n;
4953
4954 n = isl_map_dim(map, isl_dim_in);
4955 map = isl_map_project_out(map, isl_dim_in, 0, n);
4956 n = isl_map_dim(map, isl_dim_out);
4957 map = isl_map_project_out(map, isl_dim_out, 0, n);
4958 space = isl_map_get_space(map);
4959 space = isl_space_params(space);
4960 map = isl_map_reset_space(map, space);
4961 return map;
4962}
4963
4964struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
4965{
Tobias Grosserb2f39922015-05-28 13:32:11 +00004966 isl_space *space;
Tobias Grosser52a25232015-02-04 20:55:43 +00004967 unsigned n_out;
4968
4969 if (!bmap)
4970 return NULL;
Tobias Grosserb2f39922015-05-28 13:32:11 +00004971 space = isl_space_domain(isl_basic_map_get_space(bmap));
Tobias Grosser52a25232015-02-04 20:55:43 +00004972
Tobias Grosser52a25232015-02-04 20:55:43 +00004973 n_out = isl_basic_map_n_out(bmap);
Tobias Grosserb2f39922015-05-28 13:32:11 +00004974 bmap = isl_basic_map_project_out(bmap, isl_dim_out, 0, n_out);
Tobias Grosser52a25232015-02-04 20:55:43 +00004975
Tobias Grosserb2f39922015-05-28 13:32:11 +00004976 return isl_basic_map_reset_space(bmap, space);
Tobias Grosser52a25232015-02-04 20:55:43 +00004977}
4978
4979int isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
4980{
4981 if (!bmap)
4982 return -1;
4983 return isl_space_may_be_set(bmap->dim);
4984}
4985
4986/* Is this basic map actually a set?
4987 * Users should never call this function. Outside of isl,
4988 * the type should indicate whether something is a set or a map.
4989 */
4990int isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
4991{
4992 if (!bmap)
4993 return -1;
4994 return isl_space_is_set(bmap->dim);
4995}
4996
4997struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
4998{
4999 if (!bmap)
5000 return NULL;
5001 if (isl_basic_map_is_set(bmap))
5002 return bmap;
5003 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
5004}
5005
5006__isl_give isl_basic_map *isl_basic_map_domain_map(
5007 __isl_take isl_basic_map *bmap)
5008{
5009 int i, k;
5010 isl_space *dim;
5011 isl_basic_map *domain;
5012 int nparam, n_in, n_out;
5013 unsigned total;
5014
5015 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5016 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5017 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5018
5019 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
5020 domain = isl_basic_map_universe(dim);
5021
5022 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5023 bmap = isl_basic_map_apply_range(bmap, domain);
5024 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
5025
5026 total = isl_basic_map_total_dim(bmap);
5027
5028 for (i = 0; i < n_in; ++i) {
5029 k = isl_basic_map_alloc_equality(bmap);
5030 if (k < 0)
5031 goto error;
5032 isl_seq_clr(bmap->eq[k], 1 + total);
5033 isl_int_set_si(bmap->eq[k][1 + nparam + i], -1);
5034 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
5035 }
5036
5037 bmap = isl_basic_map_gauss(bmap, NULL);
5038 return isl_basic_map_finalize(bmap);
5039error:
5040 isl_basic_map_free(bmap);
5041 return NULL;
5042}
5043
5044__isl_give isl_basic_map *isl_basic_map_range_map(
5045 __isl_take isl_basic_map *bmap)
5046{
5047 int i, k;
5048 isl_space *dim;
5049 isl_basic_map *range;
5050 int nparam, n_in, n_out;
5051 unsigned total;
5052
5053 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5054 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5055 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5056
5057 dim = isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap)));
5058 range = isl_basic_map_universe(dim);
5059
5060 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5061 bmap = isl_basic_map_apply_range(bmap, range);
5062 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
5063
5064 total = isl_basic_map_total_dim(bmap);
5065
5066 for (i = 0; i < n_out; ++i) {
5067 k = isl_basic_map_alloc_equality(bmap);
5068 if (k < 0)
5069 goto error;
5070 isl_seq_clr(bmap->eq[k], 1 + total);
5071 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + i], -1);
5072 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
5073 }
5074
5075 bmap = isl_basic_map_gauss(bmap, NULL);
5076 return isl_basic_map_finalize(bmap);
5077error:
5078 isl_basic_map_free(bmap);
5079 return NULL;
5080}
5081
5082int isl_map_may_be_set(__isl_keep isl_map *map)
5083{
5084 if (!map)
5085 return -1;
5086 return isl_space_may_be_set(map->dim);
5087}
5088
5089/* Is this map actually a set?
5090 * Users should never call this function. Outside of isl,
5091 * the type should indicate whether something is a set or a map.
5092 */
5093int isl_map_is_set(__isl_keep isl_map *map)
5094{
5095 if (!map)
5096 return -1;
5097 return isl_space_is_set(map->dim);
5098}
5099
5100struct isl_set *isl_map_range(struct isl_map *map)
5101{
5102 int i;
5103 struct isl_set *set;
5104
5105 if (!map)
5106 goto error;
5107 if (isl_map_is_set(map))
5108 return (isl_set *)map;
5109
5110 map = isl_map_cow(map);
5111 if (!map)
5112 goto error;
5113
5114 set = (struct isl_set *) map;
5115 set->dim = isl_space_range(set->dim);
5116 if (!set->dim)
5117 goto error;
5118 for (i = 0; i < map->n; ++i) {
5119 set->p[i] = isl_basic_map_range(map->p[i]);
5120 if (!set->p[i])
5121 goto error;
5122 }
5123 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5124 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5125 return set;
5126error:
5127 isl_map_free(map);
5128 return NULL;
5129}
5130
5131__isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
5132{
5133 int i;
5134
5135 map = isl_map_cow(map);
5136 if (!map)
5137 return NULL;
5138
5139 map->dim = isl_space_domain_map(map->dim);
5140 if (!map->dim)
5141 goto error;
5142 for (i = 0; i < map->n; ++i) {
5143 map->p[i] = isl_basic_map_domain_map(map->p[i]);
5144 if (!map->p[i])
5145 goto error;
5146 }
5147 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5148 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5149 return map;
5150error:
5151 isl_map_free(map);
5152 return NULL;
5153}
5154
5155__isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
5156{
5157 int i;
5158 isl_space *range_dim;
5159
5160 map = isl_map_cow(map);
5161 if (!map)
5162 return NULL;
5163
5164 range_dim = isl_space_range(isl_map_get_space(map));
5165 range_dim = isl_space_from_range(range_dim);
5166 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
5167 map->dim = isl_space_join(map->dim, range_dim);
5168 if (!map->dim)
5169 goto error;
5170 for (i = 0; i < map->n; ++i) {
5171 map->p[i] = isl_basic_map_range_map(map->p[i]);
5172 if (!map->p[i])
5173 goto error;
5174 }
5175 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5176 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5177 return map;
5178error:
5179 isl_map_free(map);
5180 return NULL;
5181}
5182
5183/* Given a wrapped map of the form A[B -> C],
5184 * return the map A[B -> C] -> B.
5185 */
5186__isl_give isl_map *isl_set_wrapped_domain_map(__isl_take isl_set *set)
5187{
5188 isl_id *id;
5189 isl_map *map;
5190
5191 if (!set)
5192 return NULL;
5193 if (!isl_set_has_tuple_id(set))
5194 return isl_map_domain_map(isl_set_unwrap(set));
5195
5196 id = isl_set_get_tuple_id(set);
5197 map = isl_map_domain_map(isl_set_unwrap(set));
5198 map = isl_map_set_tuple_id(map, isl_dim_in, id);
5199
5200 return map;
5201}
5202
5203__isl_give isl_map *isl_map_from_set(__isl_take isl_set *set,
5204 __isl_take isl_space *dim)
5205{
5206 int i;
5207 struct isl_map *map = NULL;
5208
5209 set = isl_set_cow(set);
5210 if (!set || !dim)
5211 goto error;
5212 isl_assert(set->ctx, isl_space_compatible(set->dim, dim), goto error);
5213 map = (struct isl_map *)set;
5214 for (i = 0; i < set->n; ++i) {
5215 map->p[i] = isl_basic_map_from_basic_set(
5216 set->p[i], isl_space_copy(dim));
5217 if (!map->p[i])
5218 goto error;
5219 }
5220 isl_space_free(map->dim);
5221 map->dim = dim;
5222 return map;
5223error:
5224 isl_space_free(dim);
5225 isl_set_free(set);
5226 return NULL;
5227}
5228
5229__isl_give isl_basic_map *isl_basic_map_from_domain(
5230 __isl_take isl_basic_set *bset)
5231{
5232 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
5233}
5234
5235__isl_give isl_basic_map *isl_basic_map_from_range(
5236 __isl_take isl_basic_set *bset)
5237{
5238 isl_space *space;
5239 space = isl_basic_set_get_space(bset);
5240 space = isl_space_from_range(space);
5241 bset = isl_basic_set_reset_space(bset, space);
5242 return (isl_basic_map *)bset;
5243}
5244
5245/* Create a relation with the given set as range.
5246 * The domain of the created relation is a zero-dimensional
5247 * flat anonymous space.
5248 */
5249__isl_give isl_map *isl_map_from_range(__isl_take isl_set *set)
5250{
5251 isl_space *space;
5252 space = isl_set_get_space(set);
5253 space = isl_space_from_range(space);
5254 set = isl_set_reset_space(set, space);
5255 return (struct isl_map *)set;
5256}
5257
5258/* Create a relation with the given set as domain.
5259 * The range of the created relation is a zero-dimensional
5260 * flat anonymous space.
5261 */
5262__isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
5263{
5264 return isl_map_reverse(isl_map_from_range(set));
5265}
5266
5267__isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
5268 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
5269{
5270 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
5271}
5272
5273__isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
5274 __isl_take isl_set *range)
5275{
5276 return isl_map_apply_range(isl_map_reverse(domain), range);
5277}
5278
Tobias Grosser52a25232015-02-04 20:55:43 +00005279__isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *dim, int n,
5280 unsigned flags)
5281{
5282 struct isl_map *map;
5283
5284 if (!dim)
5285 return NULL;
5286 if (n < 0)
5287 isl_die(dim->ctx, isl_error_internal,
5288 "negative number of basic maps", goto error);
5289 map = isl_alloc(dim->ctx, struct isl_map,
5290 sizeof(struct isl_map) +
5291 (n - 1) * sizeof(struct isl_basic_map *));
5292 if (!map)
5293 goto error;
5294
5295 map->ctx = dim->ctx;
5296 isl_ctx_ref(map->ctx);
5297 map->ref = 1;
5298 map->size = n;
5299 map->n = 0;
5300 map->dim = dim;
5301 map->flags = flags;
5302 return map;
5303error:
5304 isl_space_free(dim);
5305 return NULL;
5306}
5307
5308struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
5309 unsigned nparam, unsigned in, unsigned out, int n,
5310 unsigned flags)
5311{
5312 struct isl_map *map;
5313 isl_space *dims;
5314
5315 dims = isl_space_alloc(ctx, nparam, in, out);
5316 if (!dims)
5317 return NULL;
5318
5319 map = isl_map_alloc_space(dims, n, flags);
5320 return map;
5321}
5322
5323__isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *dim)
5324{
5325 struct isl_basic_map *bmap;
5326 bmap = isl_basic_map_alloc_space(dim, 0, 1, 0);
5327 bmap = isl_basic_map_set_to_empty(bmap);
5328 return bmap;
5329}
5330
5331__isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *dim)
5332{
5333 struct isl_basic_set *bset;
5334 bset = isl_basic_set_alloc_space(dim, 0, 1, 0);
5335 bset = isl_basic_set_set_to_empty(bset);
5336 return bset;
5337}
5338
Tobias Grosser52a25232015-02-04 20:55:43 +00005339__isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *dim)
5340{
5341 struct isl_basic_map *bmap;
5342 bmap = isl_basic_map_alloc_space(dim, 0, 0, 0);
5343 bmap = isl_basic_map_finalize(bmap);
5344 return bmap;
5345}
5346
5347__isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *dim)
5348{
5349 struct isl_basic_set *bset;
5350 bset = isl_basic_set_alloc_space(dim, 0, 0, 0);
5351 bset = isl_basic_set_finalize(bset);
5352 return bset;
5353}
5354
5355__isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_space *dim)
5356{
5357 int i;
5358 unsigned total = isl_space_dim(dim, isl_dim_all);
5359 isl_basic_map *bmap;
5360
5361 bmap= isl_basic_map_alloc_space(dim, 0, 0, total);
5362 for (i = 0; i < total; ++i) {
5363 int k = isl_basic_map_alloc_inequality(bmap);
5364 if (k < 0)
5365 goto error;
5366 isl_seq_clr(bmap->ineq[k], 1 + total);
5367 isl_int_set_si(bmap->ineq[k][1 + i], 1);
5368 }
5369 return bmap;
5370error:
5371 isl_basic_map_free(bmap);
5372 return NULL;
5373}
5374
5375__isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_space *dim)
5376{
5377 return isl_basic_map_nat_universe(dim);
5378}
5379
5380__isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
5381{
5382 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
5383}
5384
5385__isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
5386{
5387 return isl_map_nat_universe(dim);
5388}
5389
Tobias Grosser52a25232015-02-04 20:55:43 +00005390__isl_give isl_map *isl_map_empty(__isl_take isl_space *dim)
5391{
5392 return isl_map_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5393}
5394
Tobias Grosser52a25232015-02-04 20:55:43 +00005395__isl_give isl_set *isl_set_empty(__isl_take isl_space *dim)
5396{
5397 return isl_set_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5398}
5399
Tobias Grosser52a25232015-02-04 20:55:43 +00005400__isl_give isl_map *isl_map_universe(__isl_take isl_space *dim)
5401{
5402 struct isl_map *map;
5403 if (!dim)
5404 return NULL;
5405 map = isl_map_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5406 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
5407 return map;
5408}
5409
5410__isl_give isl_set *isl_set_universe(__isl_take isl_space *dim)
5411{
5412 struct isl_set *set;
5413 if (!dim)
5414 return NULL;
5415 set = isl_set_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5416 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
5417 return set;
5418}
5419
Tobias Grosser52a25232015-02-04 20:55:43 +00005420struct isl_map *isl_map_dup(struct isl_map *map)
5421{
5422 int i;
5423 struct isl_map *dup;
5424
5425 if (!map)
5426 return NULL;
5427 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
5428 for (i = 0; i < map->n; ++i)
5429 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
5430 return dup;
5431}
5432
5433__isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
5434 __isl_take isl_basic_map *bmap)
5435{
5436 if (!bmap || !map)
5437 goto error;
5438 if (isl_basic_map_plain_is_empty(bmap)) {
5439 isl_basic_map_free(bmap);
5440 return map;
5441 }
5442 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
5443 isl_assert(map->ctx, map->n < map->size, goto error);
5444 map->p[map->n] = bmap;
5445 map->n++;
5446 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5447 return map;
5448error:
5449 if (map)
5450 isl_map_free(map);
5451 if (bmap)
5452 isl_basic_map_free(bmap);
5453 return NULL;
5454}
5455
5456__isl_null isl_map *isl_map_free(__isl_take isl_map *map)
5457{
5458 int i;
5459
5460 if (!map)
5461 return NULL;
5462
5463 if (--map->ref > 0)
5464 return NULL;
5465
5466 isl_ctx_deref(map->ctx);
5467 for (i = 0; i < map->n; ++i)
5468 isl_basic_map_free(map->p[i]);
5469 isl_space_free(map->dim);
5470 free(map);
5471
5472 return NULL;
5473}
5474
Tobias Grosser52a25232015-02-04 20:55:43 +00005475static struct isl_basic_map *isl_basic_map_fix_pos_si(
5476 struct isl_basic_map *bmap, unsigned pos, int value)
5477{
5478 int j;
5479
5480 bmap = isl_basic_map_cow(bmap);
5481 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5482 j = isl_basic_map_alloc_equality(bmap);
5483 if (j < 0)
5484 goto error;
5485 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5486 isl_int_set_si(bmap->eq[j][pos], -1);
5487 isl_int_set_si(bmap->eq[j][0], value);
5488 bmap = isl_basic_map_simplify(bmap);
5489 return isl_basic_map_finalize(bmap);
5490error:
5491 isl_basic_map_free(bmap);
5492 return NULL;
5493}
5494
5495static __isl_give isl_basic_map *isl_basic_map_fix_pos(
5496 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
5497{
5498 int j;
5499
5500 bmap = isl_basic_map_cow(bmap);
5501 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5502 j = isl_basic_map_alloc_equality(bmap);
5503 if (j < 0)
5504 goto error;
5505 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5506 isl_int_set_si(bmap->eq[j][pos], -1);
5507 isl_int_set(bmap->eq[j][0], value);
5508 bmap = isl_basic_map_simplify(bmap);
5509 return isl_basic_map_finalize(bmap);
5510error:
5511 isl_basic_map_free(bmap);
5512 return NULL;
5513}
5514
5515struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
5516 enum isl_dim_type type, unsigned pos, int value)
5517{
5518 if (!bmap)
5519 return NULL;
5520 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5521 return isl_basic_map_fix_pos_si(bmap,
5522 isl_basic_map_offset(bmap, type) + pos, value);
5523error:
5524 isl_basic_map_free(bmap);
5525 return NULL;
5526}
5527
5528__isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
5529 enum isl_dim_type type, unsigned pos, isl_int value)
5530{
5531 if (!bmap)
5532 return NULL;
5533 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5534 return isl_basic_map_fix_pos(bmap,
5535 isl_basic_map_offset(bmap, type) + pos, value);
5536error:
5537 isl_basic_map_free(bmap);
5538 return NULL;
5539}
5540
5541/* Fix the value of the variable at position "pos" of type "type" of "bmap"
5542 * to be equal to "v".
5543 */
5544__isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
5545 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5546{
5547 if (!bmap || !v)
5548 goto error;
5549 if (!isl_val_is_int(v))
5550 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5551 "expecting integer value", goto error);
5552 if (pos >= isl_basic_map_dim(bmap, type))
5553 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5554 "index out of bounds", goto error);
5555 pos += isl_basic_map_offset(bmap, type);
5556 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
5557 isl_val_free(v);
5558 return bmap;
5559error:
5560 isl_basic_map_free(bmap);
5561 isl_val_free(v);
5562 return NULL;
5563}
5564
5565/* Fix the value of the variable at position "pos" of type "type" of "bset"
5566 * to be equal to "v".
5567 */
5568__isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
5569 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5570{
5571 return isl_basic_map_fix_val(bset, type, pos, v);
5572}
5573
5574struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
5575 enum isl_dim_type type, unsigned pos, int value)
5576{
5577 return (struct isl_basic_set *)
5578 isl_basic_map_fix_si((struct isl_basic_map *)bset,
5579 type, pos, value);
5580}
5581
5582__isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
5583 enum isl_dim_type type, unsigned pos, isl_int value)
5584{
5585 return (struct isl_basic_set *)
5586 isl_basic_map_fix((struct isl_basic_map *)bset,
5587 type, pos, value);
5588}
5589
5590struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
5591 unsigned input, int value)
5592{
5593 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
5594}
5595
5596struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
5597 unsigned dim, int value)
5598{
5599 return (struct isl_basic_set *)
5600 isl_basic_map_fix_si((struct isl_basic_map *)bset,
5601 isl_dim_set, dim, value);
5602}
5603
5604static int remove_if_empty(__isl_keep isl_map *map, int i)
5605{
5606 int empty = isl_basic_map_plain_is_empty(map->p[i]);
5607
5608 if (empty < 0)
5609 return -1;
5610 if (!empty)
5611 return 0;
5612
5613 isl_basic_map_free(map->p[i]);
5614 if (i != map->n - 1) {
5615 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5616 map->p[i] = map->p[map->n - 1];
5617 }
5618 map->n--;
5619
5620 return 0;
5621}
5622
5623/* Perform "fn" on each basic map of "map", where we may not be holding
5624 * the only reference to "map".
5625 * In particular, "fn" should be a semantics preserving operation
5626 * that we want to apply to all copies of "map". We therefore need
5627 * to be careful not to modify "map" in a way that breaks "map"
5628 * in case anything goes wrong.
5629 */
5630__isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
5631 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
5632{
5633 struct isl_basic_map *bmap;
5634 int i;
5635
5636 if (!map)
5637 return NULL;
5638
5639 for (i = map->n - 1; i >= 0; --i) {
5640 bmap = isl_basic_map_copy(map->p[i]);
5641 bmap = fn(bmap);
5642 if (!bmap)
5643 goto error;
5644 isl_basic_map_free(map->p[i]);
5645 map->p[i] = bmap;
5646 if (remove_if_empty(map, i) < 0)
5647 goto error;
5648 }
5649
5650 return map;
5651error:
5652 isl_map_free(map);
5653 return NULL;
5654}
5655
5656struct isl_map *isl_map_fix_si(struct isl_map *map,
5657 enum isl_dim_type type, unsigned pos, int value)
5658{
5659 int i;
5660
5661 map = isl_map_cow(map);
5662 if (!map)
5663 return NULL;
5664
5665 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5666 for (i = map->n - 1; i >= 0; --i) {
5667 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
5668 if (remove_if_empty(map, i) < 0)
5669 goto error;
5670 }
5671 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5672 return map;
5673error:
5674 isl_map_free(map);
5675 return NULL;
5676}
5677
5678__isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
5679 enum isl_dim_type type, unsigned pos, int value)
5680{
5681 return (struct isl_set *)
5682 isl_map_fix_si((struct isl_map *)set, type, pos, value);
5683}
5684
5685__isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
5686 enum isl_dim_type type, unsigned pos, isl_int value)
5687{
5688 int i;
5689
5690 map = isl_map_cow(map);
5691 if (!map)
5692 return NULL;
5693
5694 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5695 for (i = 0; i < map->n; ++i) {
5696 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
5697 if (!map->p[i])
5698 goto error;
5699 }
5700 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5701 return map;
5702error:
5703 isl_map_free(map);
5704 return NULL;
5705}
5706
5707__isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
5708 enum isl_dim_type type, unsigned pos, isl_int value)
5709{
5710 return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
5711}
5712
5713/* Fix the value of the variable at position "pos" of type "type" of "map"
5714 * to be equal to "v".
5715 */
5716__isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
5717 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5718{
5719 int i;
5720
5721 map = isl_map_cow(map);
5722 if (!map || !v)
5723 goto error;
5724
5725 if (!isl_val_is_int(v))
5726 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5727 "expecting integer value", goto error);
5728 if (pos >= isl_map_dim(map, type))
5729 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5730 "index out of bounds", goto error);
5731 for (i = map->n - 1; i >= 0; --i) {
5732 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
5733 isl_val_copy(v));
5734 if (remove_if_empty(map, i) < 0)
5735 goto error;
5736 }
5737 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5738 isl_val_free(v);
5739 return map;
5740error:
5741 isl_map_free(map);
5742 isl_val_free(v);
5743 return NULL;
5744}
5745
5746/* Fix the value of the variable at position "pos" of type "type" of "set"
5747 * to be equal to "v".
5748 */
5749__isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
5750 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5751{
5752 return isl_map_fix_val(set, type, pos, v);
5753}
5754
5755struct isl_map *isl_map_fix_input_si(struct isl_map *map,
5756 unsigned input, int value)
5757{
5758 return isl_map_fix_si(map, isl_dim_in, input, value);
5759}
5760
5761struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
5762{
5763 return (struct isl_set *)
5764 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
5765}
5766
5767static __isl_give isl_basic_map *basic_map_bound_si(
5768 __isl_take isl_basic_map *bmap,
5769 enum isl_dim_type type, unsigned pos, int value, int upper)
5770{
5771 int j;
5772
5773 if (!bmap)
5774 return NULL;
5775 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5776 pos += isl_basic_map_offset(bmap, type);
5777 bmap = isl_basic_map_cow(bmap);
5778 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5779 j = isl_basic_map_alloc_inequality(bmap);
5780 if (j < 0)
5781 goto error;
5782 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5783 if (upper) {
5784 isl_int_set_si(bmap->ineq[j][pos], -1);
5785 isl_int_set_si(bmap->ineq[j][0], value);
5786 } else {
5787 isl_int_set_si(bmap->ineq[j][pos], 1);
5788 isl_int_set_si(bmap->ineq[j][0], -value);
5789 }
5790 bmap = isl_basic_map_simplify(bmap);
5791 return isl_basic_map_finalize(bmap);
5792error:
5793 isl_basic_map_free(bmap);
5794 return NULL;
5795}
5796
5797__isl_give isl_basic_map *isl_basic_map_lower_bound_si(
5798 __isl_take isl_basic_map *bmap,
5799 enum isl_dim_type type, unsigned pos, int value)
5800{
5801 return basic_map_bound_si(bmap, type, pos, value, 0);
5802}
5803
5804/* Constrain the values of the given dimension to be no greater than "value".
5805 */
5806__isl_give isl_basic_map *isl_basic_map_upper_bound_si(
5807 __isl_take isl_basic_map *bmap,
5808 enum isl_dim_type type, unsigned pos, int value)
5809{
5810 return basic_map_bound_si(bmap, type, pos, value, 1);
5811}
5812
5813struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
5814 unsigned dim, isl_int value)
5815{
5816 int j;
5817
5818 bset = isl_basic_set_cow(bset);
5819 bset = isl_basic_set_extend_constraints(bset, 0, 1);
5820 j = isl_basic_set_alloc_inequality(bset);
5821 if (j < 0)
5822 goto error;
5823 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
5824 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
5825 isl_int_neg(bset->ineq[j][0], value);
5826 bset = isl_basic_set_simplify(bset);
5827 return isl_basic_set_finalize(bset);
5828error:
5829 isl_basic_set_free(bset);
5830 return NULL;
5831}
5832
5833static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
5834 enum isl_dim_type type, unsigned pos, int value, int upper)
5835{
5836 int i;
5837
5838 map = isl_map_cow(map);
5839 if (!map)
5840 return NULL;
5841
5842 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5843 for (i = 0; i < map->n; ++i) {
5844 map->p[i] = basic_map_bound_si(map->p[i],
5845 type, pos, value, upper);
5846 if (!map->p[i])
5847 goto error;
5848 }
5849 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5850 return map;
5851error:
5852 isl_map_free(map);
5853 return NULL;
5854}
5855
5856__isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
5857 enum isl_dim_type type, unsigned pos, int value)
5858{
5859 return map_bound_si(map, type, pos, value, 0);
5860}
5861
5862__isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
5863 enum isl_dim_type type, unsigned pos, int value)
5864{
5865 return map_bound_si(map, type, pos, value, 1);
5866}
5867
5868__isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
5869 enum isl_dim_type type, unsigned pos, int value)
5870{
5871 return (struct isl_set *)
5872 isl_map_lower_bound_si((struct isl_map *)set, type, pos, value);
5873}
5874
5875__isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
5876 enum isl_dim_type type, unsigned pos, int value)
5877{
5878 return isl_map_upper_bound_si(set, type, pos, value);
5879}
5880
5881/* Bound the given variable of "bmap" from below (or above is "upper"
5882 * is set) to "value".
5883 */
5884static __isl_give isl_basic_map *basic_map_bound(
5885 __isl_take isl_basic_map *bmap,
5886 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5887{
5888 int j;
5889
5890 if (!bmap)
5891 return NULL;
5892 if (pos >= isl_basic_map_dim(bmap, type))
5893 isl_die(bmap->ctx, isl_error_invalid,
5894 "index out of bounds", goto error);
5895 pos += isl_basic_map_offset(bmap, type);
5896 bmap = isl_basic_map_cow(bmap);
5897 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5898 j = isl_basic_map_alloc_inequality(bmap);
5899 if (j < 0)
5900 goto error;
5901 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5902 if (upper) {
5903 isl_int_set_si(bmap->ineq[j][pos], -1);
5904 isl_int_set(bmap->ineq[j][0], value);
5905 } else {
5906 isl_int_set_si(bmap->ineq[j][pos], 1);
5907 isl_int_neg(bmap->ineq[j][0], value);
5908 }
5909 bmap = isl_basic_map_simplify(bmap);
5910 return isl_basic_map_finalize(bmap);
5911error:
5912 isl_basic_map_free(bmap);
5913 return NULL;
5914}
5915
5916/* Bound the given variable of "map" from below (or above is "upper"
5917 * is set) to "value".
5918 */
5919static __isl_give isl_map *map_bound(__isl_take isl_map *map,
5920 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5921{
5922 int i;
5923
5924 map = isl_map_cow(map);
5925 if (!map)
5926 return NULL;
5927
5928 if (pos >= isl_map_dim(map, type))
5929 isl_die(map->ctx, isl_error_invalid,
5930 "index out of bounds", goto error);
5931 for (i = map->n - 1; i >= 0; --i) {
5932 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
5933 if (remove_if_empty(map, i) < 0)
5934 goto error;
5935 }
5936 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5937 return map;
5938error:
5939 isl_map_free(map);
5940 return NULL;
5941}
5942
5943__isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
5944 enum isl_dim_type type, unsigned pos, isl_int value)
5945{
5946 return map_bound(map, type, pos, value, 0);
5947}
5948
5949__isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
5950 enum isl_dim_type type, unsigned pos, isl_int value)
5951{
5952 return map_bound(map, type, pos, value, 1);
5953}
5954
5955__isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
5956 enum isl_dim_type type, unsigned pos, isl_int value)
5957{
5958 return isl_map_lower_bound(set, type, pos, value);
5959}
5960
5961__isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
5962 enum isl_dim_type type, unsigned pos, isl_int value)
5963{
5964 return isl_map_upper_bound(set, type, pos, value);
5965}
5966
5967/* Force the values of the variable at position "pos" of type "type" of "set"
5968 * to be no smaller than "value".
5969 */
5970__isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
5971 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
5972{
5973 if (!value)
5974 goto error;
5975 if (!isl_val_is_int(value))
5976 isl_die(isl_set_get_ctx(set), isl_error_invalid,
5977 "expecting integer value", goto error);
5978 set = isl_set_lower_bound(set, type, pos, value->n);
5979 isl_val_free(value);
5980 return set;
5981error:
5982 isl_val_free(value);
5983 isl_set_free(set);
5984 return NULL;
5985}
5986
5987/* Force the values of the variable at position "pos" of type "type" of "set"
5988 * to be no greater than "value".
5989 */
5990__isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
5991 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
5992{
5993 if (!value)
5994 goto error;
5995 if (!isl_val_is_int(value))
5996 isl_die(isl_set_get_ctx(set), isl_error_invalid,
5997 "expecting integer value", goto error);
5998 set = isl_set_upper_bound(set, type, pos, value->n);
5999 isl_val_free(value);
6000 return set;
6001error:
6002 isl_val_free(value);
6003 isl_set_free(set);
6004 return NULL;
6005}
6006
6007struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
6008 isl_int value)
6009{
6010 int i;
6011
6012 set = isl_set_cow(set);
6013 if (!set)
6014 return NULL;
6015
6016 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
6017 for (i = 0; i < set->n; ++i) {
6018 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
6019 if (!set->p[i])
6020 goto error;
6021 }
6022 return set;
6023error:
6024 isl_set_free(set);
6025 return NULL;
6026}
6027
6028struct isl_map *isl_map_reverse(struct isl_map *map)
6029{
6030 int i;
6031
6032 map = isl_map_cow(map);
6033 if (!map)
6034 return NULL;
6035
6036 map->dim = isl_space_reverse(map->dim);
6037 if (!map->dim)
6038 goto error;
6039 for (i = 0; i < map->n; ++i) {
6040 map->p[i] = isl_basic_map_reverse(map->p[i]);
6041 if (!map->p[i])
6042 goto error;
6043 }
6044 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6045 return map;
6046error:
6047 isl_map_free(map);
6048 return NULL;
6049}
6050
6051static struct isl_map *isl_basic_map_partial_lexopt(
6052 struct isl_basic_map *bmap, struct isl_basic_set *dom,
6053 struct isl_set **empty, int max)
6054{
6055 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
6056}
6057
6058struct isl_map *isl_basic_map_partial_lexmax(
6059 struct isl_basic_map *bmap, struct isl_basic_set *dom,
6060 struct isl_set **empty)
6061{
6062 return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
6063}
6064
6065struct isl_map *isl_basic_map_partial_lexmin(
6066 struct isl_basic_map *bmap, struct isl_basic_set *dom,
6067 struct isl_set **empty)
6068{
6069 return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
6070}
6071
6072struct isl_set *isl_basic_set_partial_lexmin(
6073 struct isl_basic_set *bset, struct isl_basic_set *dom,
6074 struct isl_set **empty)
6075{
6076 return (struct isl_set *)
6077 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
6078 dom, empty);
6079}
6080
6081struct isl_set *isl_basic_set_partial_lexmax(
6082 struct isl_basic_set *bset, struct isl_basic_set *dom,
6083 struct isl_set **empty)
6084{
6085 return (struct isl_set *)
6086 isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
6087 dom, empty);
6088}
6089
6090__isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmin_pw_multi_aff(
6091 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
6092 __isl_give isl_set **empty)
6093{
6094 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 0);
6095}
6096
6097__isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmax_pw_multi_aff(
6098 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
6099 __isl_give isl_set **empty)
6100{
6101 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 1);
6102}
6103
6104__isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmin_pw_multi_aff(
6105 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
6106 __isl_give isl_set **empty)
6107{
6108 return isl_basic_map_partial_lexmin_pw_multi_aff(bset, dom, empty);
6109}
6110
6111__isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmax_pw_multi_aff(
6112 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
6113 __isl_give isl_set **empty)
6114{
6115 return isl_basic_map_partial_lexmax_pw_multi_aff(bset, dom, empty);
6116}
6117
6118__isl_give isl_pw_multi_aff *isl_basic_map_lexopt_pw_multi_aff(
6119 __isl_take isl_basic_map *bmap, int max)
6120{
6121 isl_basic_set *dom = NULL;
6122 isl_space *dom_space;
6123
6124 if (!bmap)
6125 goto error;
6126 dom_space = isl_space_domain(isl_space_copy(bmap->dim));
6127 dom = isl_basic_set_universe(dom_space);
6128 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, NULL, max);
6129error:
6130 isl_basic_map_free(bmap);
6131 return NULL;
6132}
6133
6134__isl_give isl_pw_multi_aff *isl_basic_map_lexmin_pw_multi_aff(
6135 __isl_take isl_basic_map *bmap)
6136{
6137 return isl_basic_map_lexopt_pw_multi_aff(bmap, 0);
6138}
6139
6140#undef TYPE
6141#define TYPE isl_pw_multi_aff
6142#undef SUFFIX
6143#define SUFFIX _pw_multi_aff
6144#undef EMPTY
6145#define EMPTY isl_pw_multi_aff_empty
6146#undef ADD
6147#define ADD isl_pw_multi_aff_union_add
6148#include "isl_map_lexopt_templ.c"
6149
6150/* Given a map "map", compute the lexicographically minimal
6151 * (or maximal) image element for each domain element in dom,
6152 * in the form of an isl_pw_multi_aff.
Tobias Grosser37034db2016-03-25 19:38:18 +00006153 * If "empty" is not NULL, then set *empty to those elements in dom that
6154 * do not have an image element.
Tobias Grosser52a25232015-02-04 20:55:43 +00006155 *
6156 * We first compute the lexicographically minimal or maximal element
6157 * in the first basic map. This results in a partial solution "res"
6158 * and a subset "todo" of dom that still need to be handled.
6159 * We then consider each of the remaining maps in "map" and successively
6160 * update both "res" and "todo".
Tobias Grosser37034db2016-03-25 19:38:18 +00006161 * If "empty" is NULL, then the todo sets are not needed and therefore
6162 * also not computed.
Tobias Grosser52a25232015-02-04 20:55:43 +00006163 */
6164static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
6165 __isl_take isl_map *map, __isl_take isl_set *dom,
6166 __isl_give isl_set **empty, int max)
6167{
6168 int i;
6169 isl_pw_multi_aff *res;
6170 isl_set *todo;
6171
6172 if (!map || !dom)
6173 goto error;
6174
6175 if (isl_map_plain_is_empty(map)) {
6176 if (empty)
6177 *empty = dom;
6178 else
6179 isl_set_free(dom);
6180 return isl_pw_multi_aff_from_map(map);
6181 }
6182
6183 res = basic_map_partial_lexopt_pw_multi_aff(
6184 isl_basic_map_copy(map->p[0]),
Tobias Grosser37034db2016-03-25 19:38:18 +00006185 isl_set_copy(dom), empty, max);
Tobias Grosser52a25232015-02-04 20:55:43 +00006186
Tobias Grosser37034db2016-03-25 19:38:18 +00006187 if (empty)
6188 todo = *empty;
Tobias Grosser52a25232015-02-04 20:55:43 +00006189 for (i = 1; i < map->n; ++i) {
6190 isl_pw_multi_aff *res_i;
Tobias Grosser52a25232015-02-04 20:55:43 +00006191
6192 res_i = basic_map_partial_lexopt_pw_multi_aff(
6193 isl_basic_map_copy(map->p[i]),
Tobias Grosser37034db2016-03-25 19:38:18 +00006194 isl_set_copy(dom), empty, max);
Tobias Grosser52a25232015-02-04 20:55:43 +00006195
6196 if (max)
6197 res = isl_pw_multi_aff_union_lexmax(res, res_i);
6198 else
6199 res = isl_pw_multi_aff_union_lexmin(res, res_i);
6200
Tobias Grosser37034db2016-03-25 19:38:18 +00006201 if (empty)
6202 todo = isl_set_intersect(todo, *empty);
Tobias Grosser52a25232015-02-04 20:55:43 +00006203 }
6204
6205 isl_set_free(dom);
6206 isl_map_free(map);
6207
6208 if (empty)
6209 *empty = todo;
Tobias Grosser52a25232015-02-04 20:55:43 +00006210
6211 return res;
6212error:
6213 if (empty)
6214 *empty = NULL;
6215 isl_set_free(dom);
6216 isl_map_free(map);
6217 return NULL;
6218}
6219
6220#undef TYPE
6221#define TYPE isl_map
6222#undef SUFFIX
6223#define SUFFIX
6224#undef EMPTY
6225#define EMPTY isl_map_empty
6226#undef ADD
6227#define ADD isl_map_union_disjoint
6228#include "isl_map_lexopt_templ.c"
6229
6230/* Given a map "map", compute the lexicographically minimal
Michael Krusee0b34f32016-05-04 14:41:36 +00006231 * (or maximal) image element for each domain element in "dom",
6232 * in the form of an isl_map.
6233 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6234 * do not have an image element.
Tobias Grosser52a25232015-02-04 20:55:43 +00006235 *
Michael Krusee0b34f32016-05-04 14:41:36 +00006236 * If the input consists of more than one disjunct, then first
6237 * compute the desired result in the form of an isl_pw_multi_aff and
6238 * then convert that into an isl_map.
Tobias Grosser52a25232015-02-04 20:55:43 +00006239 *
Michael Krusee0b34f32016-05-04 14:41:36 +00006240 * This function used to have an explicit implementation in terms
6241 * of isl_maps, but it would continually intersect the domains of
6242 * partial results with the complement of the domain of the next
6243 * partial solution, potentially leading to an explosion in the number
6244 * of disjuncts if there are several disjuncts in the input.
6245 * An even earlier implementation of this function would look for
6246 * better results in the domain of the partial result and for extra
6247 * results in the complement of this domain, which would lead to
6248 * even more splintering.
Tobias Grosser52a25232015-02-04 20:55:43 +00006249 */
6250static __isl_give isl_map *isl_map_partial_lexopt_aligned(
6251 __isl_take isl_map *map, __isl_take isl_set *dom,
6252 __isl_give isl_set **empty, int max)
6253{
Tobias Grosser52a25232015-02-04 20:55:43 +00006254 struct isl_map *res;
Michael Krusee0b34f32016-05-04 14:41:36 +00006255 isl_pw_multi_aff *pma;
Tobias Grosser52a25232015-02-04 20:55:43 +00006256
6257 if (!map || !dom)
6258 goto error;
6259
6260 if (isl_map_plain_is_empty(map)) {
6261 if (empty)
6262 *empty = dom;
6263 else
6264 isl_set_free(dom);
6265 return map;
6266 }
6267
Michael Krusee0b34f32016-05-04 14:41:36 +00006268 if (map->n == 1) {
6269 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
6270 dom, empty, max);
6271 isl_map_free(map);
6272 return res;
Tobias Grosser52a25232015-02-04 20:55:43 +00006273 }
6274
Michael Krusee0b34f32016-05-04 14:41:36 +00006275 pma = isl_map_partial_lexopt_aligned_pw_multi_aff(map, dom, empty, max);
6276 return isl_map_from_pw_multi_aff(pma);
Tobias Grosser52a25232015-02-04 20:55:43 +00006277error:
6278 if (empty)
6279 *empty = NULL;
6280 isl_set_free(dom);
6281 isl_map_free(map);
6282 return NULL;
6283}
6284
6285__isl_give isl_map *isl_map_partial_lexmax(
6286 __isl_take isl_map *map, __isl_take isl_set *dom,
6287 __isl_give isl_set **empty)
6288{
6289 return isl_map_partial_lexopt(map, dom, empty, 1);
6290}
6291
6292__isl_give isl_map *isl_map_partial_lexmin(
6293 __isl_take isl_map *map, __isl_take isl_set *dom,
6294 __isl_give isl_set **empty)
6295{
6296 return isl_map_partial_lexopt(map, dom, empty, 0);
6297}
6298
6299__isl_give isl_set *isl_set_partial_lexmin(
6300 __isl_take isl_set *set, __isl_take isl_set *dom,
6301 __isl_give isl_set **empty)
6302{
6303 return (struct isl_set *)
6304 isl_map_partial_lexmin((struct isl_map *)set,
6305 dom, empty);
6306}
6307
6308__isl_give isl_set *isl_set_partial_lexmax(
6309 __isl_take isl_set *set, __isl_take isl_set *dom,
6310 __isl_give isl_set **empty)
6311{
6312 return (struct isl_set *)
6313 isl_map_partial_lexmax((struct isl_map *)set,
6314 dom, empty);
6315}
6316
6317/* Compute the lexicographic minimum (or maximum if "max" is set)
6318 * of "bmap" over its domain.
6319 *
6320 * Since we are not interested in the part of the domain space where
6321 * there is no solution, we initialize the domain to those constraints
6322 * of "bmap" that only involve the parameters and the input dimensions.
6323 * This relieves the parametric programming engine from detecting those
6324 * inequalities and transferring them to the context. More importantly,
6325 * it ensures that those inequalities are transferred first and not
6326 * intermixed with inequalities that actually split the domain.
6327 */
6328__isl_give isl_map *isl_basic_map_lexopt(__isl_take isl_basic_map *bmap, int max)
6329{
6330 int n_div;
6331 int n_out;
6332 isl_basic_map *copy;
6333 isl_basic_set *dom;
6334
6335 n_div = isl_basic_map_dim(bmap, isl_dim_div);
6336 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6337 copy = isl_basic_map_copy(bmap);
6338 copy = isl_basic_map_drop_constraints_involving_dims(copy,
6339 isl_dim_div, 0, n_div);
6340 copy = isl_basic_map_drop_constraints_involving_dims(copy,
6341 isl_dim_out, 0, n_out);
6342 dom = isl_basic_map_domain(copy);
6343 return isl_basic_map_partial_lexopt(bmap, dom, NULL, max);
6344}
6345
6346__isl_give isl_map *isl_basic_map_lexmin(__isl_take isl_basic_map *bmap)
6347{
6348 return isl_basic_map_lexopt(bmap, 0);
6349}
6350
6351__isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
6352{
6353 return isl_basic_map_lexopt(bmap, 1);
6354}
6355
6356__isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
6357{
6358 return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
6359}
6360
6361__isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
6362{
6363 return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset);
6364}
6365
6366/* Extract the first and only affine expression from list
6367 * and then add it to *pwaff with the given dom.
6368 * This domain is known to be disjoint from other domains
6369 * because of the way isl_basic_map_foreach_lexmax works.
6370 */
6371static int update_dim_opt(__isl_take isl_basic_set *dom,
6372 __isl_take isl_aff_list *list, void *user)
6373{
6374 isl_ctx *ctx = isl_basic_set_get_ctx(dom);
6375 isl_aff *aff;
6376 isl_pw_aff **pwaff = user;
6377 isl_pw_aff *pwaff_i;
6378
6379 if (!list)
6380 goto error;
6381 if (isl_aff_list_n_aff(list) != 1)
6382 isl_die(ctx, isl_error_internal,
6383 "expecting single element list", goto error);
6384
6385 aff = isl_aff_list_get_aff(list, 0);
6386 pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
6387
6388 *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
6389
6390 isl_aff_list_free(list);
6391
6392 return 0;
6393error:
6394 isl_basic_set_free(dom);
6395 isl_aff_list_free(list);
6396 return -1;
6397}
6398
6399/* Given a basic map with one output dimension, compute the minimum or
6400 * maximum of that dimension as an isl_pw_aff.
6401 *
6402 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6403 * call update_dim_opt on each leaf of the result.
6404 */
6405static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
6406 int max)
6407{
6408 isl_space *dim = isl_basic_map_get_space(bmap);
6409 isl_pw_aff *pwaff;
6410 int r;
6411
6412 dim = isl_space_from_domain(isl_space_domain(dim));
6413 dim = isl_space_add_dims(dim, isl_dim_out, 1);
6414 pwaff = isl_pw_aff_empty(dim);
6415
6416 r = isl_basic_map_foreach_lexopt(bmap, max, &update_dim_opt, &pwaff);
6417 if (r < 0)
6418 return isl_pw_aff_free(pwaff);
6419
6420 return pwaff;
6421}
6422
6423/* Compute the minimum or maximum of the given output dimension
6424 * as a function of the parameters and the input dimensions,
6425 * but independently of the other output dimensions.
6426 *
6427 * We first project out the other output dimension and then compute
6428 * the "lexicographic" maximum in each basic map, combining the results
6429 * using isl_pw_aff_union_max.
6430 */
6431static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
6432 int max)
6433{
6434 int i;
6435 isl_pw_aff *pwaff;
6436 unsigned n_out;
6437
6438 n_out = isl_map_dim(map, isl_dim_out);
6439 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
6440 map = isl_map_project_out(map, isl_dim_out, 0, pos);
6441 if (!map)
6442 return NULL;
6443
6444 if (map->n == 0) {
6445 isl_space *dim = isl_map_get_space(map);
6446 isl_map_free(map);
6447 return isl_pw_aff_empty(dim);
6448 }
6449
6450 pwaff = basic_map_dim_opt(map->p[0], max);
6451 for (i = 1; i < map->n; ++i) {
6452 isl_pw_aff *pwaff_i;
6453
6454 pwaff_i = basic_map_dim_opt(map->p[i], max);
6455 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
6456 }
6457
6458 isl_map_free(map);
6459
6460 return pwaff;
6461}
6462
6463/* Compute the maximum of the given output dimension as a function of the
6464 * parameters and input dimensions, but independently of
6465 * the other output dimensions.
6466 */
6467__isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
6468{
6469 return map_dim_opt(map, pos, 1);
6470}
6471
6472/* Compute the minimum or maximum of the given set dimension
6473 * as a function of the parameters,
6474 * but independently of the other set dimensions.
6475 */
6476static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
6477 int max)
6478{
6479 return map_dim_opt(set, pos, max);
6480}
6481
6482/* Compute the maximum of the given set dimension as a function of the
6483 * parameters, but independently of the other set dimensions.
6484 */
6485__isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
6486{
6487 return set_dim_opt(set, pos, 1);
6488}
6489
6490/* Compute the minimum of the given set dimension as a function of the
6491 * parameters, but independently of the other set dimensions.
6492 */
6493__isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
6494{
6495 return set_dim_opt(set, pos, 0);
6496}
6497
6498/* Apply a preimage specified by "mat" on the parameters of "bset".
6499 * bset is assumed to have only parameters and divs.
6500 */
6501static struct isl_basic_set *basic_set_parameter_preimage(
6502 struct isl_basic_set *bset, struct isl_mat *mat)
6503{
6504 unsigned nparam;
6505
6506 if (!bset || !mat)
6507 goto error;
6508
6509 bset->dim = isl_space_cow(bset->dim);
6510 if (!bset->dim)
6511 goto error;
6512
6513 nparam = isl_basic_set_dim(bset, isl_dim_param);
6514
6515 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
6516
6517 bset->dim->nparam = 0;
6518 bset->dim->n_out = nparam;
6519 bset = isl_basic_set_preimage(bset, mat);
6520 if (bset) {
6521 bset->dim->nparam = bset->dim->n_out;
6522 bset->dim->n_out = 0;
6523 }
6524 return bset;
6525error:
6526 isl_mat_free(mat);
6527 isl_basic_set_free(bset);
6528 return NULL;
6529}
6530
6531/* Apply a preimage specified by "mat" on the parameters of "set".
6532 * set is assumed to have only parameters and divs.
6533 */
Tobias Grosser37034db2016-03-25 19:38:18 +00006534static __isl_give isl_set *set_parameter_preimage(__isl_take isl_set *set,
6535 __isl_take isl_mat *mat)
Tobias Grosser52a25232015-02-04 20:55:43 +00006536{
Tobias Grosser37034db2016-03-25 19:38:18 +00006537 isl_space *space;
Tobias Grosser52a25232015-02-04 20:55:43 +00006538 unsigned nparam;
6539
6540 if (!set || !mat)
6541 goto error;
6542
Tobias Grosser52a25232015-02-04 20:55:43 +00006543 nparam = isl_set_dim(set, isl_dim_param);
6544
Tobias Grosser37034db2016-03-25 19:38:18 +00006545 if (mat->n_row != 1 + nparam)
6546 isl_die(isl_set_get_ctx(set), isl_error_internal,
6547 "unexpected number of rows", goto error);
Tobias Grosser52a25232015-02-04 20:55:43 +00006548
Tobias Grosser37034db2016-03-25 19:38:18 +00006549 space = isl_set_get_space(set);
6550 space = isl_space_move_dims(space, isl_dim_set, 0,
6551 isl_dim_param, 0, nparam);
6552 set = isl_set_reset_space(set, space);
Tobias Grosser52a25232015-02-04 20:55:43 +00006553 set = isl_set_preimage(set, mat);
Tobias Grosser37034db2016-03-25 19:38:18 +00006554 nparam = isl_set_dim(set, isl_dim_out);
6555 space = isl_set_get_space(set);
6556 space = isl_space_move_dims(space, isl_dim_param, 0,
6557 isl_dim_out, 0, nparam);
6558 set = isl_set_reset_space(set, space);
Tobias Grosser52a25232015-02-04 20:55:43 +00006559 return set;
6560error:
Tobias Grosser52a25232015-02-04 20:55:43 +00006561 isl_mat_free(mat);
Tobias Grosser52a25232015-02-04 20:55:43 +00006562 isl_set_free(set);
6563 return NULL;
6564}
6565
6566/* Intersect the basic set "bset" with the affine space specified by the
6567 * equalities in "eq".
6568 */
6569static struct isl_basic_set *basic_set_append_equalities(
6570 struct isl_basic_set *bset, struct isl_mat *eq)
6571{
6572 int i, k;
6573 unsigned len;
6574
6575 if (!bset || !eq)
6576 goto error;
6577
6578 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
6579 eq->n_row, 0);
6580 if (!bset)
6581 goto error;
6582
6583 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
6584 for (i = 0; i < eq->n_row; ++i) {
6585 k = isl_basic_set_alloc_equality(bset);
6586 if (k < 0)
6587 goto error;
6588 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
6589 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
6590 }
6591 isl_mat_free(eq);
6592
6593 bset = isl_basic_set_gauss(bset, NULL);
6594 bset = isl_basic_set_finalize(bset);
6595
6596 return bset;
6597error:
6598 isl_mat_free(eq);
6599 isl_basic_set_free(bset);
6600 return NULL;
6601}
6602
6603/* Intersect the set "set" with the affine space specified by the
6604 * equalities in "eq".
6605 */
6606static struct isl_set *set_append_equalities(struct isl_set *set,
6607 struct isl_mat *eq)
6608{
6609 int i;
6610
6611 if (!set || !eq)
6612 goto error;
6613
6614 for (i = 0; i < set->n; ++i) {
6615 set->p[i] = basic_set_append_equalities(set->p[i],
6616 isl_mat_copy(eq));
6617 if (!set->p[i])
6618 goto error;
6619 }
6620 isl_mat_free(eq);
6621 return set;
6622error:
6623 isl_mat_free(eq);
6624 isl_set_free(set);
6625 return NULL;
6626}
6627
6628/* Given a basic set "bset" that only involves parameters and existentially
6629 * quantified variables, return the index of the first equality
6630 * that only involves parameters. If there is no such equality then
6631 * return bset->n_eq.
6632 *
6633 * This function assumes that isl_basic_set_gauss has been called on "bset".
6634 */
6635static int first_parameter_equality(__isl_keep isl_basic_set *bset)
6636{
6637 int i, j;
6638 unsigned nparam, n_div;
6639
6640 if (!bset)
6641 return -1;
6642
6643 nparam = isl_basic_set_dim(bset, isl_dim_param);
6644 n_div = isl_basic_set_dim(bset, isl_dim_div);
6645
6646 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
6647 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
6648 ++i;
6649 }
6650
6651 return i;
6652}
6653
6654/* Compute an explicit representation for the existentially quantified
6655 * variables in "bset" by computing the "minimal value" of the set
6656 * variables. Since there are no set variables, the computation of
6657 * the minimal value essentially computes an explicit representation
6658 * of the non-empty part(s) of "bset".
6659 *
6660 * The input only involves parameters and existentially quantified variables.
6661 * All equalities among parameters have been removed.
6662 *
6663 * Since the existentially quantified variables in the result are in general
6664 * going to be different from those in the input, we first replace
6665 * them by the minimal number of variables based on their equalities.
6666 * This should simplify the parametric integer programming.
6667 */
6668static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
6669{
6670 isl_morph *morph1, *morph2;
6671 isl_set *set;
6672 unsigned n;
6673
6674 if (!bset)
6675 return NULL;
6676 if (bset->n_eq == 0)
6677 return isl_basic_set_lexmin(bset);
6678
6679 morph1 = isl_basic_set_parameter_compression(bset);
6680 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
6681 bset = isl_basic_set_lift(bset);
6682 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
6683 bset = isl_morph_basic_set(morph2, bset);
6684 n = isl_basic_set_dim(bset, isl_dim_set);
6685 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
6686
6687 set = isl_basic_set_lexmin(bset);
6688
6689 set = isl_morph_set(isl_morph_inverse(morph1), set);
6690
6691 return set;
6692}
6693
6694/* Project the given basic set onto its parameter domain, possibly introducing
6695 * new, explicit, existential variables in the constraints.
6696 * The input has parameters and (possibly implicit) existential variables.
6697 * The output has the same parameters, but only
6698 * explicit existentially quantified variables.
6699 *
6700 * The actual projection is performed by pip, but pip doesn't seem
6701 * to like equalities very much, so we first remove the equalities
6702 * among the parameters by performing a variable compression on
6703 * the parameters. Afterward, an inverse transformation is performed
6704 * and the equalities among the parameters are inserted back in.
6705 *
6706 * The variable compression on the parameters may uncover additional
6707 * equalities that were only implicit before. We therefore check
6708 * if there are any new parameter equalities in the result and
6709 * if so recurse. The removal of parameter equalities is required
6710 * for the parameter compression performed by base_compute_divs.
6711 */
6712static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
6713{
6714 int i;
6715 struct isl_mat *eq;
6716 struct isl_mat *T, *T2;
6717 struct isl_set *set;
6718 unsigned nparam;
6719
6720 bset = isl_basic_set_cow(bset);
6721 if (!bset)
6722 return NULL;
6723
6724 if (bset->n_eq == 0)
6725 return base_compute_divs(bset);
6726
6727 bset = isl_basic_set_gauss(bset, NULL);
6728 if (!bset)
6729 return NULL;
6730 if (isl_basic_set_plain_is_empty(bset))
6731 return isl_set_from_basic_set(bset);
6732
6733 i = first_parameter_equality(bset);
6734 if (i == bset->n_eq)
6735 return base_compute_divs(bset);
6736
6737 nparam = isl_basic_set_dim(bset, isl_dim_param);
6738 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
6739 0, 1 + nparam);
6740 eq = isl_mat_cow(eq);
6741 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
6742 if (T && T->n_col == 0) {
6743 isl_mat_free(T);
6744 isl_mat_free(T2);
6745 isl_mat_free(eq);
6746 bset = isl_basic_set_set_to_empty(bset);
6747 return isl_set_from_basic_set(bset);
6748 }
6749 bset = basic_set_parameter_preimage(bset, T);
6750
6751 i = first_parameter_equality(bset);
6752 if (!bset)
6753 set = NULL;
6754 else if (i == bset->n_eq)
6755 set = base_compute_divs(bset);
6756 else
6757 set = parameter_compute_divs(bset);
6758 set = set_parameter_preimage(set, T2);
6759 set = set_append_equalities(set, eq);
6760 return set;
6761}
6762
6763/* Insert the divs from "ls" before those of "bmap".
6764 *
6765 * The number of columns is not changed, which means that the last
6766 * dimensions of "bmap" are being reintepreted as the divs from "ls".
6767 * The caller is responsible for removing the same number of dimensions
6768 * from the space of "bmap".
6769 */
6770static __isl_give isl_basic_map *insert_divs_from_local_space(
6771 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
6772{
6773 int i;
6774 int n_div;
6775 int old_n_div;
6776
6777 n_div = isl_local_space_dim(ls, isl_dim_div);
6778 if (n_div == 0)
6779 return bmap;
6780
6781 old_n_div = bmap->n_div;
6782 bmap = insert_div_rows(bmap, n_div);
6783 if (!bmap)
6784 return NULL;
6785
6786 for (i = 0; i < n_div; ++i) {
6787 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
6788 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
6789 }
6790
6791 return bmap;
6792}
6793
6794/* Replace the space of "bmap" by the space and divs of "ls".
6795 *
6796 * If "ls" has any divs, then we simplify the result since we may
6797 * have discovered some additional equalities that could simplify
6798 * the div expressions.
6799 */
6800static __isl_give isl_basic_map *basic_replace_space_by_local_space(
6801 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
6802{
6803 int n_div;
6804
6805 bmap = isl_basic_map_cow(bmap);
6806 if (!bmap || !ls)
6807 goto error;
6808
6809 n_div = isl_local_space_dim(ls, isl_dim_div);
6810 bmap = insert_divs_from_local_space(bmap, ls);
6811 if (!bmap)
6812 goto error;
6813
6814 isl_space_free(bmap->dim);
6815 bmap->dim = isl_local_space_get_space(ls);
6816 if (!bmap->dim)
6817 goto error;
6818
6819 isl_local_space_free(ls);
6820 if (n_div > 0)
6821 bmap = isl_basic_map_simplify(bmap);
6822 bmap = isl_basic_map_finalize(bmap);
6823 return bmap;
6824error:
6825 isl_basic_map_free(bmap);
6826 isl_local_space_free(ls);
6827 return NULL;
6828}
6829
6830/* Replace the space of "map" by the space and divs of "ls".
6831 */
6832static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
6833 __isl_take isl_local_space *ls)
6834{
6835 int i;
6836
6837 map = isl_map_cow(map);
6838 if (!map || !ls)
6839 goto error;
6840
6841 for (i = 0; i < map->n; ++i) {
6842 map->p[i] = basic_replace_space_by_local_space(map->p[i],
6843 isl_local_space_copy(ls));
6844 if (!map->p[i])
6845 goto error;
6846 }
6847 isl_space_free(map->dim);
6848 map->dim = isl_local_space_get_space(ls);
6849 if (!map->dim)
6850 goto error;
6851
6852 isl_local_space_free(ls);
6853 return map;
6854error:
6855 isl_local_space_free(ls);
6856 isl_map_free(map);
6857 return NULL;
6858}
6859
6860/* Compute an explicit representation for the existentially
6861 * quantified variables for which do not know any explicit representation yet.
6862 *
6863 * We first sort the existentially quantified variables so that the
6864 * existentially quantified variables for which we already have an explicit
6865 * representation are placed before those for which we do not.
6866 * The input dimensions, the output dimensions and the existentially
6867 * quantified variables for which we already have an explicit
6868 * representation are then turned into parameters.
6869 * compute_divs returns a map with the same parameters and
6870 * no input or output dimensions and the dimension specification
6871 * is reset to that of the input, including the existentially quantified
6872 * variables for which we already had an explicit representation.
6873 */
6874static struct isl_map *compute_divs(struct isl_basic_map *bmap)
6875{
6876 struct isl_basic_set *bset;
6877 struct isl_set *set;
6878 struct isl_map *map;
6879 isl_space *dim;
6880 isl_local_space *ls;
6881 unsigned nparam;
6882 unsigned n_in;
6883 unsigned n_out;
Tobias Grosser37034db2016-03-25 19:38:18 +00006884 int n_known;
Tobias Grosser52a25232015-02-04 20:55:43 +00006885 int i;
6886
6887 bmap = isl_basic_map_sort_divs(bmap);
6888 bmap = isl_basic_map_cow(bmap);
6889 if (!bmap)
6890 return NULL;
6891
Tobias Grosser37034db2016-03-25 19:38:18 +00006892 n_known = isl_basic_map_first_unknown_div(bmap);
6893 if (n_known < 0)
6894 return isl_map_from_basic_map(isl_basic_map_free(bmap));
Tobias Grosser52a25232015-02-04 20:55:43 +00006895
6896 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6897 n_in = isl_basic_map_dim(bmap, isl_dim_in);
6898 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6899 dim = isl_space_set_alloc(bmap->ctx,
6900 nparam + n_in + n_out + n_known, 0);
6901 if (!dim)
6902 goto error;
6903
6904 ls = isl_basic_map_get_local_space(bmap);
6905 ls = isl_local_space_drop_dims(ls, isl_dim_div,
6906 n_known, bmap->n_div - n_known);
6907 if (n_known > 0) {
6908 for (i = n_known; i < bmap->n_div; ++i)
6909 swap_div(bmap, i - n_known, i);
6910 bmap->n_div -= n_known;
6911 bmap->extra -= n_known;
6912 }
6913 bmap = isl_basic_map_reset_space(bmap, dim);
6914 bset = (struct isl_basic_set *)bmap;
6915
6916 set = parameter_compute_divs(bset);
6917 map = (struct isl_map *)set;
6918 map = replace_space_by_local_space(map, ls);
6919
6920 return map;
6921error:
6922 isl_basic_map_free(bmap);
6923 return NULL;
6924}
6925
Tobias Grosser37034db2016-03-25 19:38:18 +00006926/* Remove the explicit representation of local variable "div",
6927 * if there is any.
6928 */
6929__isl_give isl_basic_map *isl_basic_map_mark_div_unknown(
6930 __isl_take isl_basic_map *bmap, int div)
6931{
6932 isl_bool known;
6933
6934 known = isl_basic_map_div_is_known(bmap, div);
6935 if (known < 0)
6936 return isl_basic_map_free(bmap);
6937 if (!known)
6938 return bmap;
6939
6940 bmap = isl_basic_map_cow(bmap);
6941 if (!bmap)
6942 return NULL;
6943 isl_int_set_si(bmap->div[div][0], 0);
6944 return bmap;
6945}
6946
Michael Kruse959a8dc2016-01-15 15:54:45 +00006947/* Does local variable "div" of "bmap" have an explicit representation?
6948 */
6949isl_bool isl_basic_map_div_is_known(__isl_keep isl_basic_map *bmap, int div)
6950{
6951 if (!bmap)
6952 return isl_bool_error;
6953 if (div < 0 || div >= isl_basic_map_dim(bmap, isl_dim_div))
6954 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
6955 "position out of bounds", return isl_bool_error);
6956 return !isl_int_is_zero(bmap->div[div][0]);
6957}
6958
Tobias Grosser37034db2016-03-25 19:38:18 +00006959/* Return the position of the first local variable that does not
6960 * have an explicit representation.
6961 * Return the total number of local variables if they all have
6962 * an explicit representation.
6963 * Return -1 on error.
6964 */
6965int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map *bmap)
6966{
6967 int i;
6968
6969 if (!bmap)
6970 return -1;
6971
6972 for (i = 0; i < bmap->n_div; ++i) {
6973 if (!isl_basic_map_div_is_known(bmap, i))
6974 return i;
6975 }
6976 return bmap->n_div;
6977}
6978
Michael Kruse959a8dc2016-01-15 15:54:45 +00006979/* Does "bmap" have an explicit representation for all local variables?
6980 */
6981isl_bool isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
Tobias Grosser52a25232015-02-04 20:55:43 +00006982{
Tobias Grosser37034db2016-03-25 19:38:18 +00006983 int first, n;
Tobias Grosser52a25232015-02-04 20:55:43 +00006984
Tobias Grosser37034db2016-03-25 19:38:18 +00006985 n = isl_basic_map_dim(bmap, isl_dim_div);
6986 first = isl_basic_map_first_unknown_div(bmap);
6987 if (first < 0)
Michael Kruse959a8dc2016-01-15 15:54:45 +00006988 return isl_bool_error;
Tobias Grosser37034db2016-03-25 19:38:18 +00006989 return first == n;
Tobias Grosser52a25232015-02-04 20:55:43 +00006990}
6991
Michael Kruse959a8dc2016-01-15 15:54:45 +00006992/* Do all basic maps in "map" have an explicit representation
6993 * for all local variables?
6994 */
6995isl_bool isl_map_divs_known(__isl_keep isl_map *map)
Tobias Grosser52a25232015-02-04 20:55:43 +00006996{
6997 int i;
6998
6999 if (!map)
Michael Kruse959a8dc2016-01-15 15:54:45 +00007000 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00007001
7002 for (i = 0; i < map->n; ++i) {
7003 int known = isl_basic_map_divs_known(map->p[i]);
7004 if (known <= 0)
7005 return known;
7006 }
7007
Michael Kruse959a8dc2016-01-15 15:54:45 +00007008 return isl_bool_true;
Tobias Grosser52a25232015-02-04 20:55:43 +00007009}
7010
7011/* If bmap contains any unknown divs, then compute explicit
7012 * expressions for them. However, this computation may be
7013 * quite expensive, so first try to remove divs that aren't
7014 * strictly needed.
7015 */
7016struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
7017{
7018 int known;
7019 struct isl_map *map;
7020
7021 known = isl_basic_map_divs_known(bmap);
7022 if (known < 0)
7023 goto error;
7024 if (known)
7025 return isl_map_from_basic_map(bmap);
7026
7027 bmap = isl_basic_map_drop_redundant_divs(bmap);
7028
7029 known = isl_basic_map_divs_known(bmap);
7030 if (known < 0)
7031 goto error;
7032 if (known)
7033 return isl_map_from_basic_map(bmap);
7034
7035 map = compute_divs(bmap);
7036 return map;
7037error:
7038 isl_basic_map_free(bmap);
7039 return NULL;
7040}
7041
7042struct isl_map *isl_map_compute_divs(struct isl_map *map)
7043{
7044 int i;
7045 int known;
7046 struct isl_map *res;
7047
7048 if (!map)
7049 return NULL;
7050 if (map->n == 0)
7051 return map;
7052
Michael Kruse959a8dc2016-01-15 15:54:45 +00007053 known = isl_map_divs_known(map);
Tobias Grosser52a25232015-02-04 20:55:43 +00007054 if (known < 0) {
7055 isl_map_free(map);
7056 return NULL;
7057 }
7058 if (known)
7059 return map;
7060
7061 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
7062 for (i = 1 ; i < map->n; ++i) {
7063 struct isl_map *r2;
7064 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
7065 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
7066 res = isl_map_union_disjoint(res, r2);
7067 else
7068 res = isl_map_union(res, r2);
7069 }
7070 isl_map_free(map);
7071
7072 return res;
7073}
7074
7075struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
7076{
7077 return (struct isl_set *)
7078 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
7079}
7080
7081struct isl_set *isl_set_compute_divs(struct isl_set *set)
7082{
7083 return (struct isl_set *)
7084 isl_map_compute_divs((struct isl_map *)set);
7085}
7086
7087struct isl_set *isl_map_domain(struct isl_map *map)
7088{
7089 int i;
7090 struct isl_set *set;
7091
7092 if (!map)
7093 goto error;
7094
7095 map = isl_map_cow(map);
7096 if (!map)
7097 return NULL;
7098
7099 set = (struct isl_set *)map;
7100 set->dim = isl_space_domain(set->dim);
7101 if (!set->dim)
7102 goto error;
7103 for (i = 0; i < map->n; ++i) {
7104 set->p[i] = isl_basic_map_domain(map->p[i]);
7105 if (!set->p[i])
7106 goto error;
7107 }
7108 ISL_F_CLR(set, ISL_MAP_DISJOINT);
7109 ISL_F_CLR(set, ISL_SET_NORMALIZED);
7110 return set;
7111error:
7112 isl_map_free(map);
7113 return NULL;
7114}
7115
7116/* Return the union of "map1" and "map2", where we assume for now that
7117 * "map1" and "map2" are disjoint. Note that the basic maps inside
7118 * "map1" or "map2" may not be disjoint from each other.
7119 * Also note that this function is also called from isl_map_union,
7120 * which takes care of handling the situation where "map1" and "map2"
7121 * may not be disjoint.
7122 *
7123 * If one of the inputs is empty, we can simply return the other input.
7124 * Similarly, if one of the inputs is universal, then it is equal to the union.
7125 */
7126static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
7127 __isl_take isl_map *map2)
7128{
7129 int i;
7130 unsigned flags = 0;
7131 struct isl_map *map = NULL;
7132 int is_universe;
7133
7134 if (!map1 || !map2)
7135 goto error;
7136
7137 if (!isl_space_is_equal(map1->dim, map2->dim))
7138 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
7139 "spaces don't match", goto error);
7140
7141 if (map1->n == 0) {
7142 isl_map_free(map1);
7143 return map2;
7144 }
7145 if (map2->n == 0) {
7146 isl_map_free(map2);
7147 return map1;
7148 }
7149
7150 is_universe = isl_map_plain_is_universe(map1);
7151 if (is_universe < 0)
7152 goto error;
7153 if (is_universe) {
7154 isl_map_free(map2);
7155 return map1;
7156 }
7157
7158 is_universe = isl_map_plain_is_universe(map2);
7159 if (is_universe < 0)
7160 goto error;
7161 if (is_universe) {
7162 isl_map_free(map1);
7163 return map2;
7164 }
7165
7166 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7167 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7168 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7169
7170 map = isl_map_alloc_space(isl_space_copy(map1->dim),
7171 map1->n + map2->n, flags);
7172 if (!map)
7173 goto error;
7174 for (i = 0; i < map1->n; ++i) {
7175 map = isl_map_add_basic_map(map,
7176 isl_basic_map_copy(map1->p[i]));
7177 if (!map)
7178 goto error;
7179 }
7180 for (i = 0; i < map2->n; ++i) {
7181 map = isl_map_add_basic_map(map,
7182 isl_basic_map_copy(map2->p[i]));
7183 if (!map)
7184 goto error;
7185 }
7186 isl_map_free(map1);
7187 isl_map_free(map2);
7188 return map;
7189error:
7190 isl_map_free(map);
7191 isl_map_free(map1);
7192 isl_map_free(map2);
7193 return NULL;
7194}
7195
7196/* Return the union of "map1" and "map2", where "map1" and "map2" are
7197 * guaranteed to be disjoint by the caller.
7198 *
7199 * Note that this functions is called from within isl_map_make_disjoint,
7200 * so we have to be careful not to touch the constraints of the inputs
7201 * in any way.
7202 */
7203__isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
7204 __isl_take isl_map *map2)
7205{
7206 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
7207}
7208
7209/* Return the union of "map1" and "map2", where "map1" and "map2" may
7210 * not be disjoint. The parameters are assumed to have been aligned.
7211 *
7212 * We currently simply call map_union_disjoint, the internal operation
7213 * of which does not really depend on the inputs being disjoint.
7214 * If the result contains more than one basic map, then we clear
7215 * the disjoint flag since the result may contain basic maps from
7216 * both inputs and these are not guaranteed to be disjoint.
7217 *
7218 * As a special case, if "map1" and "map2" are obviously equal,
7219 * then we simply return "map1".
7220 */
7221static __isl_give isl_map *map_union_aligned(__isl_take isl_map *map1,
7222 __isl_take isl_map *map2)
7223{
7224 int equal;
7225
7226 if (!map1 || !map2)
7227 goto error;
7228
7229 equal = isl_map_plain_is_equal(map1, map2);
7230 if (equal < 0)
7231 goto error;
7232 if (equal) {
7233 isl_map_free(map2);
7234 return map1;
7235 }
7236
7237 map1 = map_union_disjoint(map1, map2);
7238 if (!map1)
7239 return NULL;
7240 if (map1->n > 1)
7241 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
7242 return map1;
7243error:
7244 isl_map_free(map1);
7245 isl_map_free(map2);
7246 return NULL;
7247}
7248
7249/* Return the union of "map1" and "map2", where "map1" and "map2" may
7250 * not be disjoint.
7251 */
7252__isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
7253 __isl_take isl_map *map2)
7254{
7255 return isl_map_align_params_map_map_and(map1, map2, &map_union_aligned);
7256}
7257
7258struct isl_set *isl_set_union_disjoint(
7259 struct isl_set *set1, struct isl_set *set2)
7260{
7261 return (struct isl_set *)
7262 isl_map_union_disjoint(
7263 (struct isl_map *)set1, (struct isl_map *)set2);
7264}
7265
7266struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
7267{
7268 return (struct isl_set *)
7269 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
7270}
7271
7272/* Apply "fn" to pairs of elements from "map" and "set" and collect
7273 * the results.
7274 *
7275 * "map" and "set" are assumed to be compatible and non-NULL.
7276 */
7277static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
7278 __isl_take isl_set *set,
7279 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
7280 __isl_take isl_basic_set *bset))
7281{
7282 unsigned flags = 0;
7283 struct isl_map *result;
7284 int i, j;
7285
7286 if (isl_set_plain_is_universe(set)) {
7287 isl_set_free(set);
7288 return map;
7289 }
7290
7291 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
7292 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
7293 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7294
7295 result = isl_map_alloc_space(isl_space_copy(map->dim),
7296 map->n * set->n, flags);
7297 for (i = 0; result && i < map->n; ++i)
7298 for (j = 0; j < set->n; ++j) {
7299 result = isl_map_add_basic_map(result,
7300 fn(isl_basic_map_copy(map->p[i]),
7301 isl_basic_set_copy(set->p[j])));
7302 if (!result)
7303 break;
7304 }
7305
7306 isl_map_free(map);
7307 isl_set_free(set);
7308 return result;
7309}
7310
7311static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
7312 __isl_take isl_set *set)
7313{
7314 if (!map || !set)
7315 goto error;
7316
7317 if (!isl_map_compatible_range(map, set))
7318 isl_die(set->ctx, isl_error_invalid,
7319 "incompatible spaces", goto error);
7320
7321 return map_intersect_set(map, set, &isl_basic_map_intersect_range);
7322error:
7323 isl_map_free(map);
7324 isl_set_free(set);
7325 return NULL;
7326}
7327
7328__isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
7329 __isl_take isl_set *set)
7330{
7331 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
7332}
7333
7334static __isl_give isl_map *map_intersect_domain(__isl_take isl_map *map,
7335 __isl_take isl_set *set)
7336{
7337 if (!map || !set)
7338 goto error;
7339
7340 if (!isl_map_compatible_domain(map, set))
7341 isl_die(set->ctx, isl_error_invalid,
7342 "incompatible spaces", goto error);
7343
7344 return map_intersect_set(map, set, &isl_basic_map_intersect_domain);
7345error:
7346 isl_map_free(map);
7347 isl_set_free(set);
7348 return NULL;
7349}
7350
7351__isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
7352 __isl_take isl_set *set)
7353{
7354 return isl_map_align_params_map_map_and(map, set,
7355 &map_intersect_domain);
7356}
7357
7358static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
7359 __isl_take isl_map *map2)
7360{
7361 if (!map1 || !map2)
7362 goto error;
7363 map1 = isl_map_reverse(map1);
7364 map1 = isl_map_apply_range(map1, map2);
7365 return isl_map_reverse(map1);
7366error:
7367 isl_map_free(map1);
7368 isl_map_free(map2);
7369 return NULL;
7370}
7371
7372__isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
7373 __isl_take isl_map *map2)
7374{
7375 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
7376}
7377
7378static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
7379 __isl_take isl_map *map2)
7380{
7381 isl_space *dim_result;
7382 struct isl_map *result;
7383 int i, j;
7384
7385 if (!map1 || !map2)
7386 goto error;
7387
7388 dim_result = isl_space_join(isl_space_copy(map1->dim),
7389 isl_space_copy(map2->dim));
7390
7391 result = isl_map_alloc_space(dim_result, map1->n * map2->n, 0);
7392 if (!result)
7393 goto error;
7394 for (i = 0; i < map1->n; ++i)
7395 for (j = 0; j < map2->n; ++j) {
7396 result = isl_map_add_basic_map(result,
7397 isl_basic_map_apply_range(
7398 isl_basic_map_copy(map1->p[i]),
7399 isl_basic_map_copy(map2->p[j])));
7400 if (!result)
7401 goto error;
7402 }
7403 isl_map_free(map1);
7404 isl_map_free(map2);
7405 if (result && result->n <= 1)
7406 ISL_F_SET(result, ISL_MAP_DISJOINT);
7407 return result;
7408error:
7409 isl_map_free(map1);
7410 isl_map_free(map2);
7411 return NULL;
7412}
7413
7414__isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
7415 __isl_take isl_map *map2)
7416{
7417 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
7418}
7419
7420/*
7421 * returns range - domain
7422 */
7423struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
7424{
Tobias Grosserb2f39922015-05-28 13:32:11 +00007425 isl_space *target_space;
Tobias Grosser52a25232015-02-04 20:55:43 +00007426 struct isl_basic_set *bset;
7427 unsigned dim;
7428 unsigned nparam;
7429 int i;
7430
7431 if (!bmap)
7432 goto error;
7433 isl_assert(bmap->ctx, isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7434 bmap->dim, isl_dim_out),
7435 goto error);
Tobias Grosserb2f39922015-05-28 13:32:11 +00007436 target_space = isl_space_domain(isl_basic_map_get_space(bmap));
Tobias Grosser52a25232015-02-04 20:55:43 +00007437 dim = isl_basic_map_n_in(bmap);
7438 nparam = isl_basic_map_n_param(bmap);
Tobias Grosserb2f39922015-05-28 13:32:11 +00007439 bmap = isl_basic_map_from_range(isl_basic_map_wrap(bmap));
7440 bmap = isl_basic_map_add_dims(bmap, isl_dim_in, dim);
7441 bmap = isl_basic_map_extend_constraints(bmap, dim, 0);
Tobias Grosser52a25232015-02-04 20:55:43 +00007442 for (i = 0; i < dim; ++i) {
Tobias Grosserb2f39922015-05-28 13:32:11 +00007443 int j = isl_basic_map_alloc_equality(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +00007444 if (j < 0) {
Tobias Grosserb2f39922015-05-28 13:32:11 +00007445 bmap = isl_basic_map_free(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +00007446 break;
7447 }
Tobias Grosserb2f39922015-05-28 13:32:11 +00007448 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7449 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7450 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], 1);
7451 isl_int_set_si(bmap->eq[j][1+nparam+2*dim+i], -1);
Tobias Grosser52a25232015-02-04 20:55:43 +00007452 }
Tobias Grosserb2f39922015-05-28 13:32:11 +00007453 bset = isl_basic_map_domain(bmap);
7454 bset = isl_basic_set_reset_space(bset, target_space);
Tobias Grosser52a25232015-02-04 20:55:43 +00007455 return bset;
7456error:
7457 isl_basic_map_free(bmap);
7458 return NULL;
7459}
7460
7461/*
7462 * returns range - domain
7463 */
7464__isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
7465{
7466 int i;
7467 isl_space *dim;
7468 struct isl_set *result;
7469
7470 if (!map)
7471 return NULL;
7472
7473 isl_assert(map->ctx, isl_space_tuple_is_equal(map->dim, isl_dim_in,
7474 map->dim, isl_dim_out),
7475 goto error);
7476 dim = isl_map_get_space(map);
7477 dim = isl_space_domain(dim);
7478 result = isl_set_alloc_space(dim, map->n, 0);
7479 if (!result)
7480 goto error;
7481 for (i = 0; i < map->n; ++i)
7482 result = isl_set_add_basic_set(result,
7483 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
7484 isl_map_free(map);
7485 return result;
7486error:
7487 isl_map_free(map);
7488 return NULL;
7489}
7490
7491/*
7492 * returns [domain -> range] -> range - domain
7493 */
7494__isl_give isl_basic_map *isl_basic_map_deltas_map(
7495 __isl_take isl_basic_map *bmap)
7496{
7497 int i, k;
7498 isl_space *dim;
7499 isl_basic_map *domain;
7500 int nparam, n;
7501 unsigned total;
7502
7503 if (!isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7504 bmap->dim, isl_dim_out))
7505 isl_die(bmap->ctx, isl_error_invalid,
7506 "domain and range don't match", goto error);
7507
7508 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7509 n = isl_basic_map_dim(bmap, isl_dim_in);
7510
7511 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
7512 domain = isl_basic_map_universe(dim);
7513
7514 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
7515 bmap = isl_basic_map_apply_range(bmap, domain);
7516 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
7517
7518 total = isl_basic_map_total_dim(bmap);
7519
7520 for (i = 0; i < n; ++i) {
7521 k = isl_basic_map_alloc_equality(bmap);
7522 if (k < 0)
7523 goto error;
7524 isl_seq_clr(bmap->eq[k], 1 + total);
7525 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
7526 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
7527 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
7528 }
7529
7530 bmap = isl_basic_map_gauss(bmap, NULL);
7531 return isl_basic_map_finalize(bmap);
7532error:
7533 isl_basic_map_free(bmap);
7534 return NULL;
7535}
7536
7537/*
7538 * returns [domain -> range] -> range - domain
7539 */
7540__isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
7541{
7542 int i;
7543 isl_space *domain_dim;
7544
7545 if (!map)
7546 return NULL;
7547
7548 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
7549 map->dim, isl_dim_out))
7550 isl_die(map->ctx, isl_error_invalid,
7551 "domain and range don't match", goto error);
7552
7553 map = isl_map_cow(map);
7554 if (!map)
7555 return NULL;
7556
7557 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
7558 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
7559 map->dim = isl_space_join(map->dim, domain_dim);
7560 if (!map->dim)
7561 goto error;
7562 for (i = 0; i < map->n; ++i) {
7563 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
7564 if (!map->p[i])
7565 goto error;
7566 }
7567 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
7568 return map;
7569error:
7570 isl_map_free(map);
7571 return NULL;
7572}
7573
7574static __isl_give isl_basic_map *basic_map_identity(__isl_take isl_space *dims)
7575{
7576 struct isl_basic_map *bmap;
7577 unsigned nparam;
7578 unsigned dim;
7579 int i;
7580
7581 if (!dims)
7582 return NULL;
7583
7584 nparam = dims->nparam;
7585 dim = dims->n_out;
7586 bmap = isl_basic_map_alloc_space(dims, 0, dim, 0);
7587 if (!bmap)
7588 goto error;
7589
7590 for (i = 0; i < dim; ++i) {
7591 int j = isl_basic_map_alloc_equality(bmap);
7592 if (j < 0)
7593 goto error;
7594 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7595 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7596 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
7597 }
7598 return isl_basic_map_finalize(bmap);
7599error:
7600 isl_basic_map_free(bmap);
7601 return NULL;
7602}
7603
7604__isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *dim)
7605{
7606 if (!dim)
7607 return NULL;
7608 if (dim->n_in != dim->n_out)
7609 isl_die(dim->ctx, isl_error_invalid,
7610 "number of input and output dimensions needs to be "
7611 "the same", goto error);
7612 return basic_map_identity(dim);
7613error:
7614 isl_space_free(dim);
7615 return NULL;
7616}
7617
Tobias Grosser52a25232015-02-04 20:55:43 +00007618__isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
7619{
7620 return isl_map_from_basic_map(isl_basic_map_identity(dim));
7621}
7622
Tobias Grosser52a25232015-02-04 20:55:43 +00007623__isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
7624{
7625 isl_space *dim = isl_set_get_space(set);
7626 isl_map *id;
7627 id = isl_map_identity(isl_space_map_from_set(dim));
7628 return isl_map_intersect_range(id, set);
7629}
7630
7631/* Construct a basic set with all set dimensions having only non-negative
7632 * values.
7633 */
7634__isl_give isl_basic_set *isl_basic_set_positive_orthant(
7635 __isl_take isl_space *space)
7636{
7637 int i;
7638 unsigned nparam;
7639 unsigned dim;
7640 struct isl_basic_set *bset;
7641
7642 if (!space)
7643 return NULL;
7644 nparam = space->nparam;
7645 dim = space->n_out;
7646 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
7647 if (!bset)
7648 return NULL;
7649 for (i = 0; i < dim; ++i) {
7650 int k = isl_basic_set_alloc_inequality(bset);
7651 if (k < 0)
7652 goto error;
7653 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
7654 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
7655 }
7656 return bset;
7657error:
7658 isl_basic_set_free(bset);
7659 return NULL;
7660}
7661
7662/* Construct the half-space x_pos >= 0.
7663 */
7664static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *dim,
7665 int pos)
7666{
7667 int k;
7668 isl_basic_set *nonneg;
7669
7670 nonneg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7671 k = isl_basic_set_alloc_inequality(nonneg);
7672 if (k < 0)
7673 goto error;
7674 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
7675 isl_int_set_si(nonneg->ineq[k][pos], 1);
7676
7677 return isl_basic_set_finalize(nonneg);
7678error:
7679 isl_basic_set_free(nonneg);
7680 return NULL;
7681}
7682
7683/* Construct the half-space x_pos <= -1.
7684 */
7685static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *dim, int pos)
7686{
7687 int k;
7688 isl_basic_set *neg;
7689
7690 neg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7691 k = isl_basic_set_alloc_inequality(neg);
7692 if (k < 0)
7693 goto error;
7694 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
7695 isl_int_set_si(neg->ineq[k][0], -1);
7696 isl_int_set_si(neg->ineq[k][pos], -1);
7697
7698 return isl_basic_set_finalize(neg);
7699error:
7700 isl_basic_set_free(neg);
7701 return NULL;
7702}
7703
7704__isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
7705 enum isl_dim_type type, unsigned first, unsigned n)
7706{
7707 int i;
Tobias Grossere0f8d592015-05-13 13:10:13 +00007708 unsigned offset;
Tobias Grosser52a25232015-02-04 20:55:43 +00007709 isl_basic_set *nonneg;
7710 isl_basic_set *neg;
7711
7712 if (!set)
7713 return NULL;
7714 if (n == 0)
7715 return set;
7716
7717 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
7718
Tobias Grossere0f8d592015-05-13 13:10:13 +00007719 offset = pos(set->dim, type);
Tobias Grosser52a25232015-02-04 20:55:43 +00007720 for (i = 0; i < n; ++i) {
7721 nonneg = nonneg_halfspace(isl_set_get_space(set),
Tobias Grossere0f8d592015-05-13 13:10:13 +00007722 offset + first + i);
7723 neg = neg_halfspace(isl_set_get_space(set), offset + first + i);
Tobias Grosser52a25232015-02-04 20:55:43 +00007724
7725 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
7726 }
7727
7728 return set;
7729error:
7730 isl_set_free(set);
7731 return NULL;
7732}
7733
7734static int foreach_orthant(__isl_take isl_set *set, int *signs, int first,
7735 int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7736 void *user)
7737{
7738 isl_set *half;
7739
7740 if (!set)
7741 return -1;
7742 if (isl_set_plain_is_empty(set)) {
7743 isl_set_free(set);
7744 return 0;
7745 }
7746 if (first == len)
7747 return fn(set, signs, user);
7748
7749 signs[first] = 1;
7750 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
7751 1 + first));
7752 half = isl_set_intersect(half, isl_set_copy(set));
7753 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
7754 goto error;
7755
7756 signs[first] = -1;
7757 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
7758 1 + first));
7759 half = isl_set_intersect(half, set);
7760 return foreach_orthant(half, signs, first + 1, len, fn, user);
7761error:
7762 isl_set_free(set);
7763 return -1;
7764}
7765
7766/* Call "fn" on the intersections of "set" with each of the orthants
7767 * (except for obviously empty intersections). The orthant is identified
7768 * by the signs array, with each entry having value 1 or -1 according
7769 * to the sign of the corresponding variable.
7770 */
7771int isl_set_foreach_orthant(__isl_keep isl_set *set,
7772 int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7773 void *user)
7774{
7775 unsigned nparam;
7776 unsigned nvar;
7777 int *signs;
7778 int r;
7779
7780 if (!set)
7781 return -1;
7782 if (isl_set_plain_is_empty(set))
7783 return 0;
7784
7785 nparam = isl_set_dim(set, isl_dim_param);
7786 nvar = isl_set_dim(set, isl_dim_set);
7787
7788 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
7789
7790 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
7791 fn, user);
7792
7793 free(signs);
7794
7795 return r;
7796}
7797
Tobias Grosserb2f39922015-05-28 13:32:11 +00007798isl_bool isl_set_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
Tobias Grosser52a25232015-02-04 20:55:43 +00007799{
7800 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
7801}
7802
Tobias Grosserb2f39922015-05-28 13:32:11 +00007803isl_bool isl_basic_map_is_subset(__isl_keep isl_basic_map *bmap1,
7804 __isl_keep isl_basic_map *bmap2)
Tobias Grosser52a25232015-02-04 20:55:43 +00007805{
7806 int is_subset;
7807 struct isl_map *map1;
7808 struct isl_map *map2;
7809
7810 if (!bmap1 || !bmap2)
Tobias Grosserb2f39922015-05-28 13:32:11 +00007811 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00007812
7813 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
7814 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
7815
7816 is_subset = isl_map_is_subset(map1, map2);
7817
7818 isl_map_free(map1);
7819 isl_map_free(map2);
7820
7821 return is_subset;
7822}
7823
Tobias Grosserb2f39922015-05-28 13:32:11 +00007824isl_bool isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
Tobias Grosser52a25232015-02-04 20:55:43 +00007825 __isl_keep isl_basic_set *bset2)
7826{
7827 return isl_basic_map_is_subset(bset1, bset2);
7828}
7829
Tobias Grosserb2f39922015-05-28 13:32:11 +00007830isl_bool isl_basic_map_is_equal(__isl_keep isl_basic_map *bmap1,
7831 __isl_keep isl_basic_map *bmap2)
Tobias Grosser52a25232015-02-04 20:55:43 +00007832{
Tobias Grosserb2f39922015-05-28 13:32:11 +00007833 isl_bool is_subset;
Tobias Grosser52a25232015-02-04 20:55:43 +00007834
7835 if (!bmap1 || !bmap2)
Tobias Grosserb2f39922015-05-28 13:32:11 +00007836 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00007837 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
Tobias Grosserb2f39922015-05-28 13:32:11 +00007838 if (is_subset != isl_bool_true)
Tobias Grosser52a25232015-02-04 20:55:43 +00007839 return is_subset;
7840 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7841 return is_subset;
7842}
7843
Tobias Grosserb2f39922015-05-28 13:32:11 +00007844isl_bool isl_basic_set_is_equal(__isl_keep isl_basic_set *bset1,
7845 __isl_keep isl_basic_set *bset2)
Tobias Grosser52a25232015-02-04 20:55:43 +00007846{
7847 return isl_basic_map_is_equal(
7848 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
7849}
7850
Tobias Grosserb2f39922015-05-28 13:32:11 +00007851isl_bool isl_map_is_empty(__isl_keep isl_map *map)
Tobias Grosser52a25232015-02-04 20:55:43 +00007852{
7853 int i;
7854 int is_empty;
7855
7856 if (!map)
Tobias Grosserb2f39922015-05-28 13:32:11 +00007857 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00007858 for (i = 0; i < map->n; ++i) {
7859 is_empty = isl_basic_map_is_empty(map->p[i]);
7860 if (is_empty < 0)
Tobias Grosserb2f39922015-05-28 13:32:11 +00007861 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00007862 if (!is_empty)
Tobias Grosserb2f39922015-05-28 13:32:11 +00007863 return isl_bool_false;
Tobias Grosser52a25232015-02-04 20:55:43 +00007864 }
Tobias Grosserb2f39922015-05-28 13:32:11 +00007865 return isl_bool_true;
Tobias Grosser52a25232015-02-04 20:55:43 +00007866}
7867
Tobias Grosserb2f39922015-05-28 13:32:11 +00007868isl_bool isl_map_plain_is_empty(__isl_keep isl_map *map)
Tobias Grosser52a25232015-02-04 20:55:43 +00007869{
Tobias Grosserb2f39922015-05-28 13:32:11 +00007870 return map ? map->n == 0 : isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00007871}
7872
Tobias Grosserb2f39922015-05-28 13:32:11 +00007873isl_bool isl_set_plain_is_empty(__isl_keep isl_set *set)
Tobias Grosser52a25232015-02-04 20:55:43 +00007874{
Tobias Grosserb2f39922015-05-28 13:32:11 +00007875 return set ? set->n == 0 : isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00007876}
7877
Tobias Grosserb2f39922015-05-28 13:32:11 +00007878isl_bool isl_set_is_empty(__isl_keep isl_set *set)
Tobias Grosser52a25232015-02-04 20:55:43 +00007879{
7880 return isl_map_is_empty((struct isl_map *)set);
7881}
7882
7883int isl_map_has_equal_space(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7884{
7885 if (!map1 || !map2)
7886 return -1;
7887
7888 return isl_space_is_equal(map1->dim, map2->dim);
7889}
7890
7891int isl_set_has_equal_space(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7892{
7893 if (!set1 || !set2)
7894 return -1;
7895
7896 return isl_space_is_equal(set1->dim, set2->dim);
7897}
7898
Tobias Grosserb2f39922015-05-28 13:32:11 +00007899static isl_bool map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
Tobias Grosser52a25232015-02-04 20:55:43 +00007900{
Tobias Grosserb2f39922015-05-28 13:32:11 +00007901 isl_bool is_subset;
Tobias Grosser52a25232015-02-04 20:55:43 +00007902
7903 if (!map1 || !map2)
Tobias Grosserb2f39922015-05-28 13:32:11 +00007904 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00007905 is_subset = isl_map_is_subset(map1, map2);
Tobias Grosserb2f39922015-05-28 13:32:11 +00007906 if (is_subset != isl_bool_true)
Tobias Grosser52a25232015-02-04 20:55:43 +00007907 return is_subset;
7908 is_subset = isl_map_is_subset(map2, map1);
7909 return is_subset;
7910}
7911
Tobias Grosserb2f39922015-05-28 13:32:11 +00007912isl_bool isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
Tobias Grosser52a25232015-02-04 20:55:43 +00007913{
7914 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
7915}
7916
Tobias Grosserb2f39922015-05-28 13:32:11 +00007917isl_bool isl_basic_map_is_strict_subset(
Tobias Grosser52a25232015-02-04 20:55:43 +00007918 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7919{
Tobias Grosserb2f39922015-05-28 13:32:11 +00007920 isl_bool is_subset;
Tobias Grosser52a25232015-02-04 20:55:43 +00007921
7922 if (!bmap1 || !bmap2)
Tobias Grosserb2f39922015-05-28 13:32:11 +00007923 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00007924 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
Tobias Grosserb2f39922015-05-28 13:32:11 +00007925 if (is_subset != isl_bool_true)
Tobias Grosser52a25232015-02-04 20:55:43 +00007926 return is_subset;
7927 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
Tobias Grosserb2f39922015-05-28 13:32:11 +00007928 if (is_subset == isl_bool_error)
Tobias Grosser52a25232015-02-04 20:55:43 +00007929 return is_subset;
7930 return !is_subset;
7931}
7932
Tobias Grosserb2f39922015-05-28 13:32:11 +00007933isl_bool isl_map_is_strict_subset(__isl_keep isl_map *map1,
7934 __isl_keep isl_map *map2)
Tobias Grosser52a25232015-02-04 20:55:43 +00007935{
Tobias Grosserb2f39922015-05-28 13:32:11 +00007936 isl_bool is_subset;
Tobias Grosser52a25232015-02-04 20:55:43 +00007937
7938 if (!map1 || !map2)
Tobias Grosserb2f39922015-05-28 13:32:11 +00007939 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00007940 is_subset = isl_map_is_subset(map1, map2);
Tobias Grosserb2f39922015-05-28 13:32:11 +00007941 if (is_subset != isl_bool_true)
Tobias Grosser52a25232015-02-04 20:55:43 +00007942 return is_subset;
7943 is_subset = isl_map_is_subset(map2, map1);
Tobias Grosserb2f39922015-05-28 13:32:11 +00007944 if (is_subset == isl_bool_error)
Tobias Grosser52a25232015-02-04 20:55:43 +00007945 return is_subset;
7946 return !is_subset;
7947}
7948
Tobias Grosserb2f39922015-05-28 13:32:11 +00007949isl_bool isl_set_is_strict_subset(__isl_keep isl_set *set1,
7950 __isl_keep isl_set *set2)
Tobias Grosser52a25232015-02-04 20:55:43 +00007951{
7952 return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
7953}
7954
Tobias Grossera67ac972016-02-26 11:35:12 +00007955/* Is "bmap" obviously equal to the universe with the same space?
7956 *
7957 * That is, does it not have any constraints?
7958 */
7959isl_bool isl_basic_map_plain_is_universe(__isl_keep isl_basic_map *bmap)
Tobias Grosser52a25232015-02-04 20:55:43 +00007960{
7961 if (!bmap)
Tobias Grosserb2f39922015-05-28 13:32:11 +00007962 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00007963 return bmap->n_eq == 0 && bmap->n_ineq == 0;
7964}
7965
Tobias Grossera67ac972016-02-26 11:35:12 +00007966/* Is "bset" obviously equal to the universe with the same space?
7967 */
7968isl_bool isl_basic_set_plain_is_universe(__isl_keep isl_basic_set *bset)
7969{
7970 return isl_basic_map_plain_is_universe(bset);
7971}
7972
7973/* If "c" does not involve any existentially quantified variables,
7974 * then set *univ to false and abort
7975 */
7976static isl_stat involves_divs(__isl_take isl_constraint *c, void *user)
7977{
7978 isl_bool *univ = user;
7979 unsigned n;
7980
7981 n = isl_constraint_dim(c, isl_dim_div);
7982 *univ = isl_constraint_involves_dims(c, isl_dim_div, 0, n);
7983 isl_constraint_free(c);
7984 if (*univ < 0 || !*univ)
7985 return isl_stat_error;
7986 return isl_stat_ok;
7987}
7988
7989/* Is "bmap" equal to the universe with the same space?
7990 *
7991 * First check if it is obviously equal to the universe.
7992 * If not and if there are any constraints not involving
7993 * existentially quantified variables, then it is certainly
7994 * not equal to the universe.
7995 * Otherwise, check if the universe is a subset of "bmap".
7996 */
7997isl_bool isl_basic_map_is_universe(__isl_keep isl_basic_map *bmap)
7998{
7999 isl_bool univ;
8000 isl_basic_map *test;
8001
8002 univ = isl_basic_map_plain_is_universe(bmap);
8003 if (univ < 0 || univ)
8004 return univ;
8005 if (isl_basic_map_dim(bmap, isl_dim_div) == 0)
8006 return isl_bool_false;
8007 univ = isl_bool_true;
8008 if (isl_basic_map_foreach_constraint(bmap, &involves_divs, &univ) < 0 &&
8009 univ)
8010 return isl_bool_error;
8011 if (univ < 0 || !univ)
8012 return univ;
8013 test = isl_basic_map_universe(isl_basic_map_get_space(bmap));
8014 univ = isl_basic_map_is_subset(test, bmap);
8015 isl_basic_map_free(test);
8016 return univ;
8017}
8018
8019/* Is "bset" equal to the universe with the same space?
8020 */
Tobias Grosserb2f39922015-05-28 13:32:11 +00008021isl_bool isl_basic_set_is_universe(__isl_keep isl_basic_set *bset)
Tobias Grosser52a25232015-02-04 20:55:43 +00008022{
Tobias Grossera67ac972016-02-26 11:35:12 +00008023 return isl_basic_map_is_universe(bset);
Tobias Grosser52a25232015-02-04 20:55:43 +00008024}
8025
Tobias Grosserb2f39922015-05-28 13:32:11 +00008026isl_bool isl_map_plain_is_universe(__isl_keep isl_map *map)
Tobias Grosser52a25232015-02-04 20:55:43 +00008027{
8028 int i;
8029
8030 if (!map)
Tobias Grosserb2f39922015-05-28 13:32:11 +00008031 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00008032
8033 for (i = 0; i < map->n; ++i) {
Tobias Grossera67ac972016-02-26 11:35:12 +00008034 isl_bool r = isl_basic_map_plain_is_universe(map->p[i]);
Tobias Grosser52a25232015-02-04 20:55:43 +00008035 if (r < 0 || r)
8036 return r;
8037 }
8038
Tobias Grosserb2f39922015-05-28 13:32:11 +00008039 return isl_bool_false;
Tobias Grosser52a25232015-02-04 20:55:43 +00008040}
8041
Tobias Grosserb2f39922015-05-28 13:32:11 +00008042isl_bool isl_set_plain_is_universe(__isl_keep isl_set *set)
Tobias Grosser52a25232015-02-04 20:55:43 +00008043{
8044 return isl_map_plain_is_universe((isl_map *) set);
8045}
8046
Tobias Grosserb2f39922015-05-28 13:32:11 +00008047isl_bool isl_basic_map_is_empty(__isl_keep isl_basic_map *bmap)
Tobias Grosser52a25232015-02-04 20:55:43 +00008048{
8049 struct isl_basic_set *bset = NULL;
8050 struct isl_vec *sample = NULL;
Tobias Grossera67ac972016-02-26 11:35:12 +00008051 isl_bool empty, non_empty;
Tobias Grosser52a25232015-02-04 20:55:43 +00008052
8053 if (!bmap)
Tobias Grosserb2f39922015-05-28 13:32:11 +00008054 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00008055
8056 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
Tobias Grosserb2f39922015-05-28 13:32:11 +00008057 return isl_bool_true;
Tobias Grosser52a25232015-02-04 20:55:43 +00008058
Tobias Grossera67ac972016-02-26 11:35:12 +00008059 if (isl_basic_map_plain_is_universe(bmap))
Tobias Grosserb2f39922015-05-28 13:32:11 +00008060 return isl_bool_false;
Tobias Grosser52a25232015-02-04 20:55:43 +00008061
8062 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
8063 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
8064 copy = isl_basic_map_remove_redundancies(copy);
8065 empty = isl_basic_map_plain_is_empty(copy);
8066 isl_basic_map_free(copy);
8067 return empty;
8068 }
8069
Tobias Grossera67ac972016-02-26 11:35:12 +00008070 non_empty = isl_basic_map_plain_is_non_empty(bmap);
8071 if (non_empty < 0)
8072 return isl_bool_error;
8073 if (non_empty)
8074 return isl_bool_false;
Tobias Grosser52a25232015-02-04 20:55:43 +00008075 isl_vec_free(bmap->sample);
8076 bmap->sample = NULL;
8077 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
8078 if (!bset)
Tobias Grosserb2f39922015-05-28 13:32:11 +00008079 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00008080 sample = isl_basic_set_sample_vec(bset);
8081 if (!sample)
Tobias Grosserb2f39922015-05-28 13:32:11 +00008082 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00008083 empty = sample->size == 0;
8084 isl_vec_free(bmap->sample);
8085 bmap->sample = sample;
8086 if (empty)
8087 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
8088
8089 return empty;
8090}
8091
Tobias Grosserb2f39922015-05-28 13:32:11 +00008092isl_bool isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
Tobias Grosser52a25232015-02-04 20:55:43 +00008093{
8094 if (!bmap)
Tobias Grosserb2f39922015-05-28 13:32:11 +00008095 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00008096 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
8097}
8098
Tobias Grosserb2f39922015-05-28 13:32:11 +00008099isl_bool isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
Tobias Grosser52a25232015-02-04 20:55:43 +00008100{
8101 if (!bset)
Tobias Grosserb2f39922015-05-28 13:32:11 +00008102 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00008103 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
8104}
8105
Tobias Grossera67ac972016-02-26 11:35:12 +00008106/* Is "bmap" known to be non-empty?
8107 *
8108 * That is, is the cached sample still valid?
8109 */
8110isl_bool isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map *bmap)
8111{
8112 unsigned total;
8113
8114 if (!bmap)
8115 return isl_bool_error;
8116 if (!bmap->sample)
8117 return isl_bool_false;
8118 total = 1 + isl_basic_map_total_dim(bmap);
8119 if (bmap->sample->size != total)
8120 return isl_bool_false;
8121 return isl_basic_map_contains(bmap, bmap->sample);
8122}
8123
Tobias Grosserb2f39922015-05-28 13:32:11 +00008124isl_bool isl_basic_set_is_empty(__isl_keep isl_basic_set *bset)
Tobias Grosser52a25232015-02-04 20:55:43 +00008125{
8126 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
8127}
8128
8129struct isl_map *isl_basic_map_union(
8130 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8131{
8132 struct isl_map *map;
8133 if (!bmap1 || !bmap2)
8134 goto error;
8135
8136 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
8137
8138 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
8139 if (!map)
8140 goto error;
8141 map = isl_map_add_basic_map(map, bmap1);
8142 map = isl_map_add_basic_map(map, bmap2);
8143 return map;
8144error:
8145 isl_basic_map_free(bmap1);
8146 isl_basic_map_free(bmap2);
8147 return NULL;
8148}
8149
8150struct isl_set *isl_basic_set_union(
8151 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
8152{
8153 return (struct isl_set *)isl_basic_map_union(
8154 (struct isl_basic_map *)bset1,
8155 (struct isl_basic_map *)bset2);
8156}
8157
8158/* Order divs such that any div only depends on previous divs */
8159struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
8160{
8161 int i;
8162 unsigned off;
8163
8164 if (!bmap)
8165 return NULL;
8166
8167 off = isl_space_dim(bmap->dim, isl_dim_all);
8168
8169 for (i = 0; i < bmap->n_div; ++i) {
8170 int pos;
8171 if (isl_int_is_zero(bmap->div[i][0]))
8172 continue;
8173 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
8174 bmap->n_div-i);
8175 if (pos == -1)
8176 continue;
Tobias Grosser37034db2016-03-25 19:38:18 +00008177 if (pos == 0)
8178 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
8179 "integer division depends on itself",
8180 return isl_basic_map_free(bmap));
Tobias Grosser52a25232015-02-04 20:55:43 +00008181 isl_basic_map_swap_div(bmap, i, i + pos);
8182 --i;
8183 }
8184 return bmap;
8185}
8186
8187struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
8188{
8189 return (struct isl_basic_set *)
8190 isl_basic_map_order_divs((struct isl_basic_map *)bset);
8191}
8192
8193__isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
8194{
8195 int i;
8196
8197 if (!map)
8198 return 0;
8199
8200 for (i = 0; i < map->n; ++i) {
8201 map->p[i] = isl_basic_map_order_divs(map->p[i]);
8202 if (!map->p[i])
8203 goto error;
8204 }
8205
8206 return map;
8207error:
8208 isl_map_free(map);
8209 return NULL;
8210}
8211
8212/* Apply the expansion computed by isl_merge_divs.
8213 * The expansion itself is given by "exp" while the resulting
8214 * list of divs is given by "div".
Tobias Grossera67ac972016-02-26 11:35:12 +00008215 *
8216 * Move the integer divisions of "bset" into the right position
8217 * according to "exp" and then introduce the additional integer
8218 * divisions, adding div constraints.
8219 * The moving should be done first to avoid moving coefficients
8220 * in the definitions of the extra integer divisions.
Tobias Grosser52a25232015-02-04 20:55:43 +00008221 */
8222__isl_give isl_basic_set *isl_basic_set_expand_divs(
8223 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
8224{
8225 int i, j;
8226 int n_div;
8227
8228 bset = isl_basic_set_cow(bset);
8229 if (!bset || !div)
8230 goto error;
8231
8232 if (div->n_row < bset->n_div)
8233 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
8234 "not an expansion", goto error);
8235
8236 n_div = bset->n_div;
8237 bset = isl_basic_map_extend_space(bset, isl_space_copy(bset->dim),
8238 div->n_row - n_div, 0,
8239 2 * (div->n_row - n_div));
8240
8241 for (i = n_div; i < div->n_row; ++i)
8242 if (isl_basic_set_alloc_div(bset) < 0)
8243 goto error;
8244
Tobias Grossera67ac972016-02-26 11:35:12 +00008245 for (j = n_div - 1; j >= 0; --j) {
8246 if (exp[j] == j)
8247 break;
8248 isl_basic_map_swap_div(bset, j, exp[j]);
8249 }
8250 j = 0;
8251 for (i = 0; i < div->n_row; ++i) {
8252 if (j < n_div && exp[j] == i) {
8253 j++;
Tobias Grosser52a25232015-02-04 20:55:43 +00008254 } else {
8255 isl_seq_cpy(bset->div[i], div->row[i], div->n_col);
8256 if (isl_basic_map_add_div_constraints(bset, i) < 0)
8257 goto error;
8258 }
8259 }
8260
8261 isl_mat_free(div);
8262 return bset;
8263error:
8264 isl_basic_set_free(bset);
8265 isl_mat_free(div);
8266 return NULL;
8267}
8268
8269/* Look for a div in dst that corresponds to the div "div" in src.
8270 * The divs before "div" in src and dst are assumed to be the same.
8271 *
8272 * Returns -1 if no corresponding div was found and the position
8273 * of the corresponding div in dst otherwise.
8274 */
8275static int find_div(struct isl_basic_map *dst,
8276 struct isl_basic_map *src, unsigned div)
8277{
8278 int i;
8279
8280 unsigned total = isl_space_dim(src->dim, isl_dim_all);
8281
8282 isl_assert(dst->ctx, div <= dst->n_div, return -1);
8283 for (i = div; i < dst->n_div; ++i)
8284 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
8285 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
8286 dst->n_div - div) == -1)
8287 return i;
8288 return -1;
8289}
8290
8291/* Align the divs of "dst" to those of "src", adding divs from "src"
8292 * if needed. That is, make sure that the first src->n_div divs
8293 * of the result are equal to those of src.
8294 *
8295 * The result is not finalized as by design it will have redundant
8296 * divs if any divs from "src" were copied.
8297 */
8298__isl_give isl_basic_map *isl_basic_map_align_divs(
8299 __isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src)
8300{
8301 int i;
8302 int known, extended;
8303 unsigned total;
8304
8305 if (!dst || !src)
8306 return isl_basic_map_free(dst);
8307
8308 if (src->n_div == 0)
8309 return dst;
8310
8311 known = isl_basic_map_divs_known(src);
8312 if (known < 0)
8313 return isl_basic_map_free(dst);
8314 if (!known)
8315 isl_die(isl_basic_map_get_ctx(src), isl_error_invalid,
8316 "some src divs are unknown",
8317 return isl_basic_map_free(dst));
8318
8319 src = isl_basic_map_order_divs(src);
8320
8321 extended = 0;
8322 total = isl_space_dim(src->dim, isl_dim_all);
8323 for (i = 0; i < src->n_div; ++i) {
8324 int j = find_div(dst, src, i);
8325 if (j < 0) {
8326 if (!extended) {
8327 int extra = src->n_div - i;
8328 dst = isl_basic_map_cow(dst);
8329 if (!dst)
8330 return NULL;
8331 dst = isl_basic_map_extend_space(dst,
8332 isl_space_copy(dst->dim),
8333 extra, 0, 2 * extra);
8334 extended = 1;
8335 }
8336 j = isl_basic_map_alloc_div(dst);
8337 if (j < 0)
8338 return isl_basic_map_free(dst);
8339 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
8340 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
8341 if (isl_basic_map_add_div_constraints(dst, j) < 0)
8342 return isl_basic_map_free(dst);
8343 }
8344 if (j != i)
8345 isl_basic_map_swap_div(dst, i, j);
8346 }
8347 return dst;
8348}
8349
8350struct isl_basic_set *isl_basic_set_align_divs(
8351 struct isl_basic_set *dst, struct isl_basic_set *src)
8352{
8353 return (struct isl_basic_set *)isl_basic_map_align_divs(
8354 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
8355}
8356
8357struct isl_map *isl_map_align_divs(struct isl_map *map)
8358{
8359 int i;
8360
8361 if (!map)
8362 return NULL;
8363 if (map->n == 0)
8364 return map;
8365 map = isl_map_compute_divs(map);
8366 map = isl_map_cow(map);
8367 if (!map)
8368 return NULL;
8369
8370 for (i = 1; i < map->n; ++i)
8371 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
8372 for (i = 1; i < map->n; ++i) {
8373 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
8374 if (!map->p[i])
8375 return isl_map_free(map);
8376 }
8377
8378 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
8379 return map;
8380}
8381
8382struct isl_set *isl_set_align_divs(struct isl_set *set)
8383{
8384 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
8385}
8386
Tobias Grosser1fa7b972015-02-16 19:33:40 +00008387/* Align the divs of the basic maps in "map" to those
8388 * of the basic maps in "list", as well as to the other basic maps in "map".
Tobias Grosser52a25232015-02-04 20:55:43 +00008389 * The elements in "list" are assumed to have known divs.
8390 */
Tobias Grosser1fa7b972015-02-16 19:33:40 +00008391__isl_give isl_map *isl_map_align_divs_to_basic_map_list(
8392 __isl_take isl_map *map, __isl_keep isl_basic_map_list *list)
Tobias Grosser52a25232015-02-04 20:55:43 +00008393{
8394 int i, n;
8395
Tobias Grosser1fa7b972015-02-16 19:33:40 +00008396 map = isl_map_compute_divs(map);
8397 map = isl_map_cow(map);
8398 if (!map || !list)
8399 return isl_map_free(map);
8400 if (map->n == 0)
8401 return map;
Tobias Grosser52a25232015-02-04 20:55:43 +00008402
Tobias Grosser1fa7b972015-02-16 19:33:40 +00008403 n = isl_basic_map_list_n_basic_map(list);
Tobias Grosser52a25232015-02-04 20:55:43 +00008404 for (i = 0; i < n; ++i) {
Tobias Grosser1fa7b972015-02-16 19:33:40 +00008405 isl_basic_map *bmap;
Tobias Grosser52a25232015-02-04 20:55:43 +00008406
Tobias Grosser1fa7b972015-02-16 19:33:40 +00008407 bmap = isl_basic_map_list_get_basic_map(list, i);
8408 map->p[0] = isl_basic_map_align_divs(map->p[0], bmap);
8409 isl_basic_map_free(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +00008410 }
Tobias Grosser1fa7b972015-02-16 19:33:40 +00008411 if (!map->p[0])
8412 return isl_map_free(map);
Tobias Grosser52a25232015-02-04 20:55:43 +00008413
Tobias Grosser1fa7b972015-02-16 19:33:40 +00008414 return isl_map_align_divs(map);
Tobias Grosser52a25232015-02-04 20:55:43 +00008415}
8416
Tobias Grosser1fa7b972015-02-16 19:33:40 +00008417/* Align the divs of each element of "list" to those of "bmap".
8418 * Both "bmap" and the elements of "list" are assumed to have known divs.
Tobias Grosser52a25232015-02-04 20:55:43 +00008419 */
Tobias Grosser1fa7b972015-02-16 19:33:40 +00008420__isl_give isl_basic_map_list *isl_basic_map_list_align_divs_to_basic_map(
8421 __isl_take isl_basic_map_list *list, __isl_keep isl_basic_map *bmap)
Tobias Grosser52a25232015-02-04 20:55:43 +00008422{
8423 int i, n;
8424
Tobias Grosser1fa7b972015-02-16 19:33:40 +00008425 if (!list || !bmap)
8426 return isl_basic_map_list_free(list);
Tobias Grosser52a25232015-02-04 20:55:43 +00008427
Tobias Grosser1fa7b972015-02-16 19:33:40 +00008428 n = isl_basic_map_list_n_basic_map(list);
Tobias Grosser52a25232015-02-04 20:55:43 +00008429 for (i = 0; i < n; ++i) {
Tobias Grosser1fa7b972015-02-16 19:33:40 +00008430 isl_basic_map *bmap_i;
Tobias Grosser52a25232015-02-04 20:55:43 +00008431
Tobias Grosser1fa7b972015-02-16 19:33:40 +00008432 bmap_i = isl_basic_map_list_get_basic_map(list, i);
8433 bmap_i = isl_basic_map_align_divs(bmap_i, bmap);
8434 list = isl_basic_map_list_set_basic_map(list, i, bmap_i);
Tobias Grosser52a25232015-02-04 20:55:43 +00008435 }
8436
8437 return list;
8438}
8439
8440static __isl_give isl_set *set_apply( __isl_take isl_set *set,
8441 __isl_take isl_map *map)
8442{
8443 if (!set || !map)
8444 goto error;
8445 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
8446 map = isl_map_intersect_domain(map, set);
8447 set = isl_map_range(map);
8448 return set;
8449error:
8450 isl_set_free(set);
8451 isl_map_free(map);
8452 return NULL;
8453}
8454
8455__isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
8456 __isl_take isl_map *map)
8457{
8458 return isl_map_align_params_map_map_and(set, map, &set_apply);
8459}
8460
8461/* There is no need to cow as removing empty parts doesn't change
8462 * the meaning of the set.
8463 */
8464struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
8465{
8466 int i;
8467
8468 if (!map)
8469 return NULL;
8470
8471 for (i = map->n - 1; i >= 0; --i)
8472 remove_if_empty(map, i);
8473
8474 return map;
8475}
8476
8477struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
8478{
8479 return (struct isl_set *)
8480 isl_map_remove_empty_parts((struct isl_map *)set);
8481}
8482
8483struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
8484{
8485 struct isl_basic_map *bmap;
8486 if (!map || map->n == 0)
8487 return NULL;
8488 bmap = map->p[map->n-1];
8489 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
8490 return isl_basic_map_copy(bmap);
8491}
8492
8493struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
8494{
8495 return (struct isl_basic_set *)
8496 isl_map_copy_basic_map((struct isl_map *)set);
8497}
8498
8499__isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
8500 __isl_keep isl_basic_map *bmap)
8501{
8502 int i;
8503
8504 if (!map || !bmap)
8505 goto error;
8506 for (i = map->n-1; i >= 0; --i) {
8507 if (map->p[i] != bmap)
8508 continue;
8509 map = isl_map_cow(map);
8510 if (!map)
8511 goto error;
8512 isl_basic_map_free(map->p[i]);
8513 if (i != map->n-1) {
8514 ISL_F_CLR(map, ISL_SET_NORMALIZED);
8515 map->p[i] = map->p[map->n-1];
8516 }
8517 map->n--;
8518 return map;
8519 }
8520 return map;
8521error:
8522 isl_map_free(map);
8523 return NULL;
8524}
8525
8526struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
8527 struct isl_basic_set *bset)
8528{
8529 return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
8530 (struct isl_basic_map *)bset);
8531}
8532
8533/* Given two basic sets bset1 and bset2, compute the maximal difference
8534 * between the values of dimension pos in bset1 and those in bset2
8535 * for any common value of the parameters and dimensions preceding pos.
8536 */
8537static enum isl_lp_result basic_set_maximal_difference_at(
8538 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
8539 int pos, isl_int *opt)
8540{
8541 isl_space *dims;
8542 struct isl_basic_map *bmap1 = NULL;
8543 struct isl_basic_map *bmap2 = NULL;
8544 struct isl_ctx *ctx;
8545 struct isl_vec *obj;
8546 unsigned total;
8547 unsigned nparam;
8548 unsigned dim1, dim2;
8549 enum isl_lp_result res;
8550
8551 if (!bset1 || !bset2)
8552 return isl_lp_error;
8553
8554 nparam = isl_basic_set_n_param(bset1);
8555 dim1 = isl_basic_set_n_dim(bset1);
8556 dim2 = isl_basic_set_n_dim(bset2);
8557 dims = isl_space_alloc(bset1->ctx, nparam, pos, dim1 - pos);
8558 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
8559 dims = isl_space_alloc(bset2->ctx, nparam, pos, dim2 - pos);
8560 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
8561 if (!bmap1 || !bmap2)
8562 goto error;
8563 bmap1 = isl_basic_map_cow(bmap1);
8564 bmap1 = isl_basic_map_extend(bmap1, nparam,
8565 pos, (dim1 - pos) + (dim2 - pos),
8566 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
8567 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
8568 if (!bmap1)
8569 goto error2;
8570 total = isl_basic_map_total_dim(bmap1);
8571 ctx = bmap1->ctx;
8572 obj = isl_vec_alloc(ctx, 1 + total);
8573 if (!obj)
8574 goto error2;
8575 isl_seq_clr(obj->block.data, 1 + total);
8576 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
8577 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
8578 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
8579 opt, NULL, NULL);
8580 isl_basic_map_free(bmap1);
8581 isl_vec_free(obj);
8582 return res;
8583error:
8584 isl_basic_map_free(bmap2);
8585error2:
8586 isl_basic_map_free(bmap1);
8587 return isl_lp_error;
8588}
8589
8590/* Given two _disjoint_ basic sets bset1 and bset2, check whether
8591 * for any common value of the parameters and dimensions preceding pos
8592 * in both basic sets, the values of dimension pos in bset1 are
8593 * smaller or larger than those in bset2.
8594 *
8595 * Returns
8596 * 1 if bset1 follows bset2
8597 * -1 if bset1 precedes bset2
8598 * 0 if bset1 and bset2 are incomparable
8599 * -2 if some error occurred.
8600 */
8601int isl_basic_set_compare_at(struct isl_basic_set *bset1,
8602 struct isl_basic_set *bset2, int pos)
8603{
8604 isl_int opt;
8605 enum isl_lp_result res;
8606 int cmp;
8607
8608 isl_int_init(opt);
8609
8610 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8611
8612 if (res == isl_lp_empty)
8613 cmp = 0;
8614 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8615 res == isl_lp_unbounded)
8616 cmp = 1;
8617 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8618 cmp = -1;
8619 else
8620 cmp = -2;
8621
8622 isl_int_clear(opt);
8623 return cmp;
8624}
8625
8626/* Given two basic sets bset1 and bset2, check whether
8627 * for any common value of the parameters and dimensions preceding pos
8628 * there is a value of dimension pos in bset1 that is larger
8629 * than a value of the same dimension in bset2.
8630 *
8631 * Return
8632 * 1 if there exists such a pair
8633 * 0 if there is no such pair, but there is a pair of equal values
8634 * -1 otherwise
8635 * -2 if some error occurred.
8636 */
8637int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
8638 __isl_keep isl_basic_set *bset2, int pos)
8639{
8640 isl_int opt;
8641 enum isl_lp_result res;
8642 int cmp;
8643
8644 isl_int_init(opt);
8645
8646 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8647
8648 if (res == isl_lp_empty)
8649 cmp = -1;
8650 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8651 res == isl_lp_unbounded)
8652 cmp = 1;
8653 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8654 cmp = -1;
8655 else if (res == isl_lp_ok)
8656 cmp = 0;
8657 else
8658 cmp = -2;
8659
8660 isl_int_clear(opt);
8661 return cmp;
8662}
8663
8664/* Given two sets set1 and set2, check whether
8665 * for any common value of the parameters and dimensions preceding pos
8666 * there is a value of dimension pos in set1 that is larger
8667 * than a value of the same dimension in set2.
8668 *
8669 * Return
8670 * 1 if there exists such a pair
8671 * 0 if there is no such pair, but there is a pair of equal values
8672 * -1 otherwise
8673 * -2 if some error occurred.
8674 */
8675int isl_set_follows_at(__isl_keep isl_set *set1,
8676 __isl_keep isl_set *set2, int pos)
8677{
8678 int i, j;
8679 int follows = -1;
8680
8681 if (!set1 || !set2)
8682 return -2;
8683
8684 for (i = 0; i < set1->n; ++i)
8685 for (j = 0; j < set2->n; ++j) {
8686 int f;
8687 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
8688 if (f == 1 || f == -2)
8689 return f;
8690 if (f > follows)
8691 follows = f;
8692 }
8693
8694 return follows;
8695}
8696
8697static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map *bmap,
8698 unsigned pos, isl_int *val)
8699{
8700 int i;
8701 int d;
8702 unsigned total;
8703
8704 if (!bmap)
8705 return -1;
8706 total = isl_basic_map_total_dim(bmap);
8707 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
8708 for (; d+1 > pos; --d)
8709 if (!isl_int_is_zero(bmap->eq[i][1+d]))
8710 break;
8711 if (d != pos)
8712 continue;
8713 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
8714 return 0;
8715 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
8716 return 0;
8717 if (!isl_int_is_one(bmap->eq[i][1+d]))
8718 return 0;
8719 if (val)
8720 isl_int_neg(*val, bmap->eq[i][0]);
8721 return 1;
8722 }
8723 return 0;
8724}
8725
8726static int isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
8727 unsigned pos, isl_int *val)
8728{
8729 int i;
8730 isl_int v;
8731 isl_int tmp;
8732 int fixed;
8733
8734 if (!map)
8735 return -1;
8736 if (map->n == 0)
8737 return 0;
8738 if (map->n == 1)
8739 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
8740 isl_int_init(v);
8741 isl_int_init(tmp);
8742 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
8743 for (i = 1; fixed == 1 && i < map->n; ++i) {
8744 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
8745 if (fixed == 1 && isl_int_ne(tmp, v))
8746 fixed = 0;
8747 }
8748 if (val)
8749 isl_int_set(*val, v);
8750 isl_int_clear(tmp);
8751 isl_int_clear(v);
8752 return fixed;
8753}
8754
8755static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set *bset,
8756 unsigned pos, isl_int *val)
8757{
8758 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map *)bset,
8759 pos, val);
8760}
8761
8762static int isl_set_plain_has_fixed_var(__isl_keep isl_set *set, unsigned pos,
8763 isl_int *val)
8764{
8765 return isl_map_plain_has_fixed_var((struct isl_map *)set, pos, val);
8766}
8767
8768int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
8769 enum isl_dim_type type, unsigned pos, isl_int *val)
8770{
8771 if (pos >= isl_basic_map_dim(bmap, type))
8772 return -1;
8773 return isl_basic_map_plain_has_fixed_var(bmap,
8774 isl_basic_map_offset(bmap, type) - 1 + pos, val);
8775}
8776
8777/* If "bmap" obviously lies on a hyperplane where the given dimension
8778 * has a fixed value, then return that value.
8779 * Otherwise return NaN.
8780 */
8781__isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
8782 __isl_keep isl_basic_map *bmap,
8783 enum isl_dim_type type, unsigned pos)
8784{
8785 isl_ctx *ctx;
8786 isl_val *v;
8787 int fixed;
8788
8789 if (!bmap)
8790 return NULL;
8791 ctx = isl_basic_map_get_ctx(bmap);
8792 v = isl_val_alloc(ctx);
8793 if (!v)
8794 return NULL;
8795 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
8796 if (fixed < 0)
8797 return isl_val_free(v);
8798 if (fixed) {
8799 isl_int_set_si(v->d, 1);
8800 return v;
8801 }
8802 isl_val_free(v);
8803 return isl_val_nan(ctx);
8804}
8805
8806int isl_map_plain_is_fixed(__isl_keep isl_map *map,
8807 enum isl_dim_type type, unsigned pos, isl_int *val)
8808{
8809 if (pos >= isl_map_dim(map, type))
8810 return -1;
8811 return isl_map_plain_has_fixed_var(map,
8812 map_offset(map, type) - 1 + pos, val);
8813}
8814
8815/* If "map" obviously lies on a hyperplane where the given dimension
8816 * has a fixed value, then return that value.
8817 * Otherwise return NaN.
8818 */
8819__isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
8820 enum isl_dim_type type, unsigned pos)
8821{
8822 isl_ctx *ctx;
8823 isl_val *v;
8824 int fixed;
8825
8826 if (!map)
8827 return NULL;
8828 ctx = isl_map_get_ctx(map);
8829 v = isl_val_alloc(ctx);
8830 if (!v)
8831 return NULL;
8832 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
8833 if (fixed < 0)
8834 return isl_val_free(v);
8835 if (fixed) {
8836 isl_int_set_si(v->d, 1);
8837 return v;
8838 }
8839 isl_val_free(v);
8840 return isl_val_nan(ctx);
8841}
8842
8843/* If "set" obviously lies on a hyperplane where the given dimension
8844 * has a fixed value, then return that value.
8845 * Otherwise return NaN.
8846 */
8847__isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
8848 enum isl_dim_type type, unsigned pos)
8849{
8850 return isl_map_plain_get_val_if_fixed(set, type, pos);
8851}
8852
8853int isl_set_plain_is_fixed(__isl_keep isl_set *set,
8854 enum isl_dim_type type, unsigned pos, isl_int *val)
8855{
8856 return isl_map_plain_is_fixed(set, type, pos, val);
8857}
8858
Tobias Grosser52a25232015-02-04 20:55:43 +00008859/* Check if dimension dim has fixed value and if so and if val is not NULL,
8860 * then return this fixed value in *val.
8861 */
8862int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
8863 unsigned dim, isl_int *val)
8864{
8865 return isl_basic_set_plain_has_fixed_var(bset,
8866 isl_basic_set_n_param(bset) + dim, val);
8867}
8868
8869/* Check if dimension dim has fixed value and if so and if val is not NULL,
8870 * then return this fixed value in *val.
8871 */
8872int isl_set_plain_dim_is_fixed(__isl_keep isl_set *set,
8873 unsigned dim, isl_int *val)
8874{
8875 return isl_set_plain_has_fixed_var(set, isl_set_n_param(set) + dim, val);
8876}
8877
Tobias Grosser52a25232015-02-04 20:55:43 +00008878/* Check if input variable in has fixed value and if so and if val is not NULL,
8879 * then return this fixed value in *val.
8880 */
8881int isl_map_plain_input_is_fixed(__isl_keep isl_map *map,
8882 unsigned in, isl_int *val)
8883{
8884 return isl_map_plain_has_fixed_var(map, isl_map_n_param(map) + in, val);
8885}
8886
8887/* Check if dimension dim has an (obvious) fixed lower bound and if so
8888 * and if val is not NULL, then return this lower bound in *val.
8889 */
8890int isl_basic_set_plain_dim_has_fixed_lower_bound(
8891 __isl_keep isl_basic_set *bset, unsigned dim, isl_int *val)
8892{
8893 int i, i_eq = -1, i_ineq = -1;
8894 isl_int *c;
8895 unsigned total;
8896 unsigned nparam;
8897
8898 if (!bset)
8899 return -1;
8900 total = isl_basic_set_total_dim(bset);
8901 nparam = isl_basic_set_n_param(bset);
8902 for (i = 0; i < bset->n_eq; ++i) {
8903 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
8904 continue;
8905 if (i_eq != -1)
8906 return 0;
8907 i_eq = i;
8908 }
8909 for (i = 0; i < bset->n_ineq; ++i) {
8910 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
8911 continue;
8912 if (i_eq != -1 || i_ineq != -1)
8913 return 0;
8914 i_ineq = i;
8915 }
8916 if (i_eq == -1 && i_ineq == -1)
8917 return 0;
8918 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
8919 /* The coefficient should always be one due to normalization. */
8920 if (!isl_int_is_one(c[1+nparam+dim]))
8921 return 0;
8922 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
8923 return 0;
8924 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
8925 total - nparam - dim - 1) != -1)
8926 return 0;
8927 if (val)
8928 isl_int_neg(*val, c[0]);
8929 return 1;
8930}
8931
8932int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set *set,
8933 unsigned dim, isl_int *val)
8934{
8935 int i;
8936 isl_int v;
8937 isl_int tmp;
8938 int fixed;
8939
8940 if (!set)
8941 return -1;
8942 if (set->n == 0)
8943 return 0;
8944 if (set->n == 1)
8945 return isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
8946 dim, val);
8947 isl_int_init(v);
8948 isl_int_init(tmp);
8949 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
8950 dim, &v);
8951 for (i = 1; fixed == 1 && i < set->n; ++i) {
8952 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[i],
8953 dim, &tmp);
8954 if (fixed == 1 && isl_int_ne(tmp, v))
8955 fixed = 0;
8956 }
8957 if (val)
8958 isl_int_set(*val, v);
8959 isl_int_clear(tmp);
8960 isl_int_clear(v);
8961 return fixed;
8962}
8963
Michael Kruse959a8dc2016-01-15 15:54:45 +00008964/* Return -1 if the constraint "c1" should be sorted before "c2"
8965 * and 1 if it should be sorted after "c2".
8966 * Return 0 if the two constraints are the same (up to the constant term).
8967 *
8968 * In particular, if a constraint involves later variables than another
8969 * then it is sorted after this other constraint.
8970 * uset_gist depends on constraints without existentially quantified
Tobias Grosser52a25232015-02-04 20:55:43 +00008971 * variables sorting first.
Michael Kruse959a8dc2016-01-15 15:54:45 +00008972 *
8973 * For constraints that have the same latest variable, those
8974 * with the same coefficient for this latest variable (first in absolute value
8975 * and then in actual value) are grouped together.
8976 * This is useful for detecting pairs of constraints that can
8977 * be chained in their printed representation.
8978 *
8979 * Finally, within a group, constraints are sorted according to
8980 * their coefficients (excluding the constant term).
Tobias Grosser52a25232015-02-04 20:55:43 +00008981 */
8982static int sort_constraint_cmp(const void *p1, const void *p2, void *arg)
8983{
8984 isl_int **c1 = (isl_int **) p1;
8985 isl_int **c2 = (isl_int **) p2;
8986 int l1, l2;
8987 unsigned size = *(unsigned *) arg;
Michael Kruse959a8dc2016-01-15 15:54:45 +00008988 int cmp;
Tobias Grosser52a25232015-02-04 20:55:43 +00008989
8990 l1 = isl_seq_last_non_zero(*c1 + 1, size);
8991 l2 = isl_seq_last_non_zero(*c2 + 1, size);
8992
8993 if (l1 != l2)
8994 return l1 - l2;
8995
Michael Kruse959a8dc2016-01-15 15:54:45 +00008996 cmp = isl_int_abs_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
8997 if (cmp != 0)
8998 return cmp;
8999 cmp = isl_int_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9000 if (cmp != 0)
9001 return -cmp;
9002
Tobias Grosser52a25232015-02-04 20:55:43 +00009003 return isl_seq_cmp(*c1 + 1, *c2 + 1, size);
9004}
9005
Michael Kruse959a8dc2016-01-15 15:54:45 +00009006/* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
9007 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
9008 * and 0 if the two constraints are the same (up to the constant term).
9009 */
9010int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map *bmap,
9011 isl_int *c1, isl_int *c2)
9012{
9013 unsigned total;
9014
9015 if (!bmap)
9016 return -2;
9017 total = isl_basic_map_total_dim(bmap);
9018 return sort_constraint_cmp(&c1, &c2, &total);
9019}
9020
9021__isl_give isl_basic_map *isl_basic_map_sort_constraints(
9022 __isl_take isl_basic_map *bmap)
Tobias Grosser52a25232015-02-04 20:55:43 +00009023{
9024 unsigned total;
9025
9026 if (!bmap)
9027 return NULL;
9028 if (bmap->n_ineq == 0)
9029 return bmap;
9030 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
9031 return bmap;
9032 total = isl_basic_map_total_dim(bmap);
9033 if (isl_sort(bmap->ineq, bmap->n_ineq, sizeof(isl_int *),
9034 &sort_constraint_cmp, &total) < 0)
9035 return isl_basic_map_free(bmap);
9036 return bmap;
9037}
9038
9039__isl_give isl_basic_set *isl_basic_set_sort_constraints(
9040 __isl_take isl_basic_set *bset)
9041{
9042 return (struct isl_basic_set *)isl_basic_map_sort_constraints(
9043 (struct isl_basic_map *)bset);
9044}
9045
9046struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
9047{
9048 if (!bmap)
9049 return NULL;
9050 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
9051 return bmap;
9052 bmap = isl_basic_map_remove_redundancies(bmap);
9053 bmap = isl_basic_map_sort_constraints(bmap);
9054 if (bmap)
9055 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
9056 return bmap;
9057}
9058
9059struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
9060{
9061 return (struct isl_basic_set *)isl_basic_map_normalize(
9062 (struct isl_basic_map *)bset);
9063}
9064
9065int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map *bmap1,
9066 const __isl_keep isl_basic_map *bmap2)
9067{
9068 int i, cmp;
9069 unsigned total;
9070
Tobias Grosser3e6070e2015-05-09 09:37:30 +00009071 if (!bmap1 || !bmap2)
9072 return -1;
9073
Tobias Grosser52a25232015-02-04 20:55:43 +00009074 if (bmap1 == bmap2)
9075 return 0;
9076 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
9077 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
9078 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
9079 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
9080 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
9081 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
9082 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
9083 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
9084 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
9085 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
9086 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9087 return 0;
9088 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
9089 return 1;
9090 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9091 return -1;
9092 if (bmap1->n_eq != bmap2->n_eq)
9093 return bmap1->n_eq - bmap2->n_eq;
9094 if (bmap1->n_ineq != bmap2->n_ineq)
9095 return bmap1->n_ineq - bmap2->n_ineq;
9096 if (bmap1->n_div != bmap2->n_div)
9097 return bmap1->n_div - bmap2->n_div;
9098 total = isl_basic_map_total_dim(bmap1);
9099 for (i = 0; i < bmap1->n_eq; ++i) {
9100 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
9101 if (cmp)
9102 return cmp;
9103 }
9104 for (i = 0; i < bmap1->n_ineq; ++i) {
9105 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
9106 if (cmp)
9107 return cmp;
9108 }
9109 for (i = 0; i < bmap1->n_div; ++i) {
9110 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
9111 if (cmp)
9112 return cmp;
9113 }
9114 return 0;
9115}
9116
9117int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set *bset1,
9118 const __isl_keep isl_basic_set *bset2)
9119{
9120 return isl_basic_map_plain_cmp(bset1, bset2);
9121}
9122
9123int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9124{
9125 int i, cmp;
9126
9127 if (set1 == set2)
9128 return 0;
9129 if (set1->n != set2->n)
9130 return set1->n - set2->n;
9131
9132 for (i = 0; i < set1->n; ++i) {
9133 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
9134 if (cmp)
9135 return cmp;
9136 }
9137
9138 return 0;
9139}
9140
Tobias Grosserb2f39922015-05-28 13:32:11 +00009141isl_bool isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
Tobias Grosser52a25232015-02-04 20:55:43 +00009142 __isl_keep isl_basic_map *bmap2)
9143{
Tobias Grossere0f8d592015-05-13 13:10:13 +00009144 if (!bmap1 || !bmap2)
Tobias Grosserb2f39922015-05-28 13:32:11 +00009145 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00009146 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
9147}
9148
Tobias Grosserb2f39922015-05-28 13:32:11 +00009149isl_bool isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
Tobias Grosser52a25232015-02-04 20:55:43 +00009150 __isl_keep isl_basic_set *bset2)
9151{
9152 return isl_basic_map_plain_is_equal((isl_basic_map *)bset1,
9153 (isl_basic_map *)bset2);
9154}
9155
9156static int qsort_bmap_cmp(const void *p1, const void *p2)
9157{
9158 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
9159 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
9160
9161 return isl_basic_map_plain_cmp(bmap1, bmap2);
9162}
9163
9164/* Sort the basic maps of "map" and remove duplicate basic maps.
9165 *
9166 * While removing basic maps, we make sure that the basic maps remain
9167 * sorted because isl_map_normalize expects the basic maps of the result
9168 * to be sorted.
9169 */
9170static __isl_give isl_map *sort_and_remove_duplicates(__isl_take isl_map *map)
9171{
9172 int i, j;
9173
9174 map = isl_map_remove_empty_parts(map);
9175 if (!map)
9176 return NULL;
9177 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
9178 for (i = map->n - 1; i >= 1; --i) {
9179 if (!isl_basic_map_plain_is_equal(map->p[i - 1], map->p[i]))
9180 continue;
9181 isl_basic_map_free(map->p[i-1]);
9182 for (j = i; j < map->n; ++j)
9183 map->p[j - 1] = map->p[j];
9184 map->n--;
9185 }
9186
9187 return map;
9188}
9189
9190/* Remove obvious duplicates among the basic maps of "map".
9191 *
9192 * Unlike isl_map_normalize, this function does not remove redundant
9193 * constraints and only removes duplicates that have exactly the same
9194 * constraints in the input. It does sort the constraints and
9195 * the basic maps to ease the detection of duplicates.
9196 *
9197 * If "map" has already been normalized or if the basic maps are
9198 * disjoint, then there can be no duplicates.
9199 */
9200__isl_give isl_map *isl_map_remove_obvious_duplicates(__isl_take isl_map *map)
9201{
9202 int i;
9203 isl_basic_map *bmap;
9204
9205 if (!map)
9206 return NULL;
9207 if (map->n <= 1)
9208 return map;
9209 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED | ISL_MAP_DISJOINT))
9210 return map;
9211 for (i = 0; i < map->n; ++i) {
9212 bmap = isl_basic_map_copy(map->p[i]);
9213 bmap = isl_basic_map_sort_constraints(bmap);
9214 if (!bmap)
9215 return isl_map_free(map);
9216 isl_basic_map_free(map->p[i]);
9217 map->p[i] = bmap;
9218 }
9219
9220 map = sort_and_remove_duplicates(map);
9221 return map;
9222}
9223
9224/* We normalize in place, but if anything goes wrong we need
9225 * to return NULL, so we need to make sure we don't change the
9226 * meaning of any possible other copies of map.
9227 */
9228__isl_give isl_map *isl_map_normalize(__isl_take isl_map *map)
9229{
9230 int i;
9231 struct isl_basic_map *bmap;
9232
9233 if (!map)
9234 return NULL;
9235 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
9236 return map;
9237 for (i = 0; i < map->n; ++i) {
9238 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
9239 if (!bmap)
9240 goto error;
9241 isl_basic_map_free(map->p[i]);
9242 map->p[i] = bmap;
9243 }
9244
9245 map = sort_and_remove_duplicates(map);
9246 if (map)
9247 ISL_F_SET(map, ISL_MAP_NORMALIZED);
9248 return map;
9249error:
9250 isl_map_free(map);
9251 return NULL;
9252}
9253
9254struct isl_set *isl_set_normalize(struct isl_set *set)
9255{
9256 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
9257}
9258
Tobias Grosserb2f39922015-05-28 13:32:11 +00009259isl_bool isl_map_plain_is_equal(__isl_keep isl_map *map1,
9260 __isl_keep isl_map *map2)
Tobias Grosser52a25232015-02-04 20:55:43 +00009261{
9262 int i;
Tobias Grosserb2f39922015-05-28 13:32:11 +00009263 isl_bool equal;
Tobias Grosser52a25232015-02-04 20:55:43 +00009264
9265 if (!map1 || !map2)
Tobias Grosserb2f39922015-05-28 13:32:11 +00009266 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00009267
9268 if (map1 == map2)
Tobias Grosserb2f39922015-05-28 13:32:11 +00009269 return isl_bool_true;
Tobias Grosser52a25232015-02-04 20:55:43 +00009270 if (!isl_space_is_equal(map1->dim, map2->dim))
Tobias Grosserb2f39922015-05-28 13:32:11 +00009271 return isl_bool_false;
Tobias Grosser52a25232015-02-04 20:55:43 +00009272
9273 map1 = isl_map_copy(map1);
9274 map2 = isl_map_copy(map2);
9275 map1 = isl_map_normalize(map1);
9276 map2 = isl_map_normalize(map2);
9277 if (!map1 || !map2)
9278 goto error;
9279 equal = map1->n == map2->n;
9280 for (i = 0; equal && i < map1->n; ++i) {
9281 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
9282 if (equal < 0)
9283 goto error;
9284 }
9285 isl_map_free(map1);
9286 isl_map_free(map2);
9287 return equal;
9288error:
9289 isl_map_free(map1);
9290 isl_map_free(map2);
Tobias Grosserb2f39922015-05-28 13:32:11 +00009291 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00009292}
9293
Tobias Grosserb2f39922015-05-28 13:32:11 +00009294isl_bool isl_set_plain_is_equal(__isl_keep isl_set *set1,
9295 __isl_keep isl_set *set2)
Tobias Grosser52a25232015-02-04 20:55:43 +00009296{
9297 return isl_map_plain_is_equal((struct isl_map *)set1,
9298 (struct isl_map *)set2);
9299}
9300
Tobias Grosser52a25232015-02-04 20:55:43 +00009301/* Return an interval that ranges from min to max (inclusive)
9302 */
9303struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
9304 isl_int min, isl_int max)
9305{
9306 int k;
9307 struct isl_basic_set *bset = NULL;
9308
9309 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
9310 if (!bset)
9311 goto error;
9312
9313 k = isl_basic_set_alloc_inequality(bset);
9314 if (k < 0)
9315 goto error;
9316 isl_int_set_si(bset->ineq[k][1], 1);
9317 isl_int_neg(bset->ineq[k][0], min);
9318
9319 k = isl_basic_set_alloc_inequality(bset);
9320 if (k < 0)
9321 goto error;
9322 isl_int_set_si(bset->ineq[k][1], -1);
9323 isl_int_set(bset->ineq[k][0], max);
9324
9325 return bset;
9326error:
9327 isl_basic_set_free(bset);
9328 return NULL;
9329}
9330
Tobias Grosser1fa7b972015-02-16 19:33:40 +00009331/* Return the basic maps in "map" as a list.
Tobias Grosser52a25232015-02-04 20:55:43 +00009332 */
Tobias Grosser1fa7b972015-02-16 19:33:40 +00009333__isl_give isl_basic_map_list *isl_map_get_basic_map_list(
9334 __isl_keep isl_map *map)
Tobias Grosser52a25232015-02-04 20:55:43 +00009335{
9336 int i;
9337 isl_ctx *ctx;
Tobias Grosser1fa7b972015-02-16 19:33:40 +00009338 isl_basic_map_list *list;
Tobias Grosser52a25232015-02-04 20:55:43 +00009339
Tobias Grosser1fa7b972015-02-16 19:33:40 +00009340 if (!map)
Tobias Grosser52a25232015-02-04 20:55:43 +00009341 return NULL;
Tobias Grosser1fa7b972015-02-16 19:33:40 +00009342 ctx = isl_map_get_ctx(map);
9343 list = isl_basic_map_list_alloc(ctx, map->n);
Tobias Grosser52a25232015-02-04 20:55:43 +00009344
Tobias Grosser1fa7b972015-02-16 19:33:40 +00009345 for (i = 0; i < map->n; ++i) {
9346 isl_basic_map *bmap;
Tobias Grosser52a25232015-02-04 20:55:43 +00009347
Tobias Grosser1fa7b972015-02-16 19:33:40 +00009348 bmap = isl_basic_map_copy(map->p[i]);
9349 list = isl_basic_map_list_add(list, bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +00009350 }
9351
9352 return list;
9353}
9354
9355/* Return the intersection of the elements in the non-empty list "list".
9356 * All elements are assumed to live in the same space.
9357 */
Tobias Grosser1fa7b972015-02-16 19:33:40 +00009358__isl_give isl_basic_map *isl_basic_map_list_intersect(
9359 __isl_take isl_basic_map_list *list)
Tobias Grosser52a25232015-02-04 20:55:43 +00009360{
9361 int i, n;
Tobias Grosser1fa7b972015-02-16 19:33:40 +00009362 isl_basic_map *bmap;
Tobias Grosser52a25232015-02-04 20:55:43 +00009363
9364 if (!list)
9365 return NULL;
Tobias Grosser1fa7b972015-02-16 19:33:40 +00009366 n = isl_basic_map_list_n_basic_map(list);
Tobias Grosser52a25232015-02-04 20:55:43 +00009367 if (n < 1)
Tobias Grosser1fa7b972015-02-16 19:33:40 +00009368 isl_die(isl_basic_map_list_get_ctx(list), isl_error_invalid,
Tobias Grosser52a25232015-02-04 20:55:43 +00009369 "expecting non-empty list", goto error);
Tobias Grosser52a25232015-02-04 20:55:43 +00009370
Tobias Grosser1fa7b972015-02-16 19:33:40 +00009371 bmap = isl_basic_map_list_get_basic_map(list, 0);
9372 for (i = 1; i < n; ++i) {
9373 isl_basic_map *bmap_i;
9374
9375 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9376 bmap = isl_basic_map_intersect(bmap, bmap_i);
Tobias Grosser52a25232015-02-04 20:55:43 +00009377 }
9378
Tobias Grosser1fa7b972015-02-16 19:33:40 +00009379 isl_basic_map_list_free(list);
9380 return bmap;
Tobias Grosser52a25232015-02-04 20:55:43 +00009381error:
Tobias Grosser1fa7b972015-02-16 19:33:40 +00009382 isl_basic_map_list_free(list);
Tobias Grosser52a25232015-02-04 20:55:43 +00009383 return NULL;
9384}
9385
Tobias Grosser1fa7b972015-02-16 19:33:40 +00009386/* Return the intersection of the elements in the non-empty list "list".
9387 * All elements are assumed to live in the same space.
9388 */
9389__isl_give isl_basic_set *isl_basic_set_list_intersect(
9390 __isl_take isl_basic_set_list *list)
9391{
9392 return isl_basic_map_list_intersect(list);
9393}
9394
Tobias Grossera67ac972016-02-26 11:35:12 +00009395/* Return the union of the elements in the non-empty list "list".
9396 * All elements are assumed to live in the same space.
9397 */
9398__isl_give isl_set *isl_set_list_union(__isl_take isl_set_list *list)
9399{
9400 int i, n;
9401 isl_set *set;
9402
9403 if (!list)
9404 return NULL;
9405 n = isl_set_list_n_set(list);
9406 if (n < 1)
9407 isl_die(isl_set_list_get_ctx(list), isl_error_invalid,
9408 "expecting non-empty list", goto error);
9409
9410 set = isl_set_list_get_set(list, 0);
9411 for (i = 1; i < n; ++i) {
9412 isl_set *set_i;
9413
9414 set_i = isl_set_list_get_set(list, i);
9415 set = isl_set_union(set, set_i);
9416 }
9417
9418 isl_set_list_free(list);
9419 return set;
9420error:
9421 isl_set_list_free(list);
9422 return NULL;
9423}
9424
Tobias Grosser52a25232015-02-04 20:55:43 +00009425/* Return the Cartesian product of the basic sets in list (in the given order).
9426 */
9427__isl_give isl_basic_set *isl_basic_set_list_product(
9428 __isl_take struct isl_basic_set_list *list)
9429{
9430 int i;
9431 unsigned dim;
9432 unsigned nparam;
9433 unsigned extra;
9434 unsigned n_eq;
9435 unsigned n_ineq;
9436 struct isl_basic_set *product = NULL;
9437
9438 if (!list)
9439 goto error;
9440 isl_assert(list->ctx, list->n > 0, goto error);
9441 isl_assert(list->ctx, list->p[0], goto error);
9442 nparam = isl_basic_set_n_param(list->p[0]);
9443 dim = isl_basic_set_n_dim(list->p[0]);
9444 extra = list->p[0]->n_div;
9445 n_eq = list->p[0]->n_eq;
9446 n_ineq = list->p[0]->n_ineq;
9447 for (i = 1; i < list->n; ++i) {
9448 isl_assert(list->ctx, list->p[i], goto error);
9449 isl_assert(list->ctx,
9450 nparam == isl_basic_set_n_param(list->p[i]), goto error);
9451 dim += isl_basic_set_n_dim(list->p[i]);
9452 extra += list->p[i]->n_div;
9453 n_eq += list->p[i]->n_eq;
9454 n_ineq += list->p[i]->n_ineq;
9455 }
9456 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
9457 n_eq, n_ineq);
9458 if (!product)
9459 goto error;
9460 dim = 0;
9461 for (i = 0; i < list->n; ++i) {
9462 isl_basic_set_add_constraints(product,
9463 isl_basic_set_copy(list->p[i]), dim);
9464 dim += isl_basic_set_n_dim(list->p[i]);
9465 }
9466 isl_basic_set_list_free(list);
9467 return product;
9468error:
9469 isl_basic_set_free(product);
9470 isl_basic_set_list_free(list);
9471 return NULL;
9472}
9473
9474struct isl_basic_map *isl_basic_map_product(
9475 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
9476{
9477 isl_space *dim_result = NULL;
9478 struct isl_basic_map *bmap;
9479 unsigned in1, in2, out1, out2, nparam, total, pos;
9480 struct isl_dim_map *dim_map1, *dim_map2;
9481
9482 if (!bmap1 || !bmap2)
9483 goto error;
9484
9485 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
9486 bmap2->dim, isl_dim_param), goto error);
9487 dim_result = isl_space_product(isl_space_copy(bmap1->dim),
9488 isl_space_copy(bmap2->dim));
9489
9490 in1 = isl_basic_map_n_in(bmap1);
9491 in2 = isl_basic_map_n_in(bmap2);
9492 out1 = isl_basic_map_n_out(bmap1);
9493 out2 = isl_basic_map_n_out(bmap2);
9494 nparam = isl_basic_map_n_param(bmap1);
9495
9496 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
9497 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9498 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9499 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9500 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9501 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9502 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9503 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9504 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9505 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9506 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9507
9508 bmap = isl_basic_map_alloc_space(dim_result,
9509 bmap1->n_div + bmap2->n_div,
9510 bmap1->n_eq + bmap2->n_eq,
9511 bmap1->n_ineq + bmap2->n_ineq);
9512 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9513 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9514 bmap = isl_basic_map_simplify(bmap);
9515 return isl_basic_map_finalize(bmap);
9516error:
9517 isl_basic_map_free(bmap1);
9518 isl_basic_map_free(bmap2);
9519 return NULL;
9520}
9521
9522__isl_give isl_basic_map *isl_basic_map_flat_product(
9523 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9524{
9525 isl_basic_map *prod;
9526
9527 prod = isl_basic_map_product(bmap1, bmap2);
9528 prod = isl_basic_map_flatten(prod);
9529 return prod;
9530}
9531
9532__isl_give isl_basic_set *isl_basic_set_flat_product(
9533 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
9534{
9535 return isl_basic_map_flat_range_product(bset1, bset2);
9536}
9537
9538__isl_give isl_basic_map *isl_basic_map_domain_product(
9539 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9540{
9541 isl_space *space_result = NULL;
9542 isl_basic_map *bmap;
9543 unsigned in1, in2, out, nparam, total, pos;
9544 struct isl_dim_map *dim_map1, *dim_map2;
9545
9546 if (!bmap1 || !bmap2)
9547 goto error;
9548
9549 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
9550 isl_space_copy(bmap2->dim));
9551
9552 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9553 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9554 out = isl_basic_map_dim(bmap1, isl_dim_out);
9555 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9556
9557 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
9558 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9559 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9560 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9561 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9562 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9563 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9564 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9565 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
9566 isl_dim_map_div(dim_map1, bmap1, pos += out);
9567 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9568
9569 bmap = isl_basic_map_alloc_space(space_result,
9570 bmap1->n_div + bmap2->n_div,
9571 bmap1->n_eq + bmap2->n_eq,
9572 bmap1->n_ineq + bmap2->n_ineq);
9573 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9574 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9575 bmap = isl_basic_map_simplify(bmap);
9576 return isl_basic_map_finalize(bmap);
9577error:
9578 isl_basic_map_free(bmap1);
9579 isl_basic_map_free(bmap2);
9580 return NULL;
9581}
9582
9583__isl_give isl_basic_map *isl_basic_map_range_product(
9584 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9585{
9586 isl_space *dim_result = NULL;
9587 isl_basic_map *bmap;
9588 unsigned in, out1, out2, nparam, total, pos;
9589 struct isl_dim_map *dim_map1, *dim_map2;
9590
9591 if (!bmap1 || !bmap2)
9592 goto error;
9593
9594 if (!isl_space_match(bmap1->dim, isl_dim_param,
9595 bmap2->dim, isl_dim_param))
9596 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
9597 "parameters don't match", goto error);
9598
9599 dim_result = isl_space_range_product(isl_space_copy(bmap1->dim),
9600 isl_space_copy(bmap2->dim));
9601
9602 in = isl_basic_map_dim(bmap1, isl_dim_in);
9603 out1 = isl_basic_map_n_out(bmap1);
9604 out2 = isl_basic_map_n_out(bmap2);
9605 nparam = isl_basic_map_n_param(bmap1);
9606
9607 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
9608 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9609 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9610 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9611 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9612 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9613 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
9614 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
9615 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9616 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9617 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9618
9619 bmap = isl_basic_map_alloc_space(dim_result,
9620 bmap1->n_div + bmap2->n_div,
9621 bmap1->n_eq + bmap2->n_eq,
9622 bmap1->n_ineq + bmap2->n_ineq);
9623 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9624 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9625 bmap = isl_basic_map_simplify(bmap);
9626 return isl_basic_map_finalize(bmap);
9627error:
9628 isl_basic_map_free(bmap1);
9629 isl_basic_map_free(bmap2);
9630 return NULL;
9631}
9632
9633__isl_give isl_basic_map *isl_basic_map_flat_range_product(
9634 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9635{
9636 isl_basic_map *prod;
9637
9638 prod = isl_basic_map_range_product(bmap1, bmap2);
9639 prod = isl_basic_map_flatten_range(prod);
9640 return prod;
9641}
9642
9643/* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
9644 * and collect the results.
9645 * The result live in the space obtained by calling "space_product"
9646 * on the spaces of "map1" and "map2".
9647 * If "remove_duplicates" is set then the result may contain duplicates
9648 * (even if the inputs do not) and so we try and remove the obvious
9649 * duplicates.
9650 */
9651static __isl_give isl_map *map_product(__isl_take isl_map *map1,
9652 __isl_take isl_map *map2,
9653 __isl_give isl_space *(*space_product)(__isl_take isl_space *left,
9654 __isl_take isl_space *right),
9655 __isl_give isl_basic_map *(*basic_map_product)(
9656 __isl_take isl_basic_map *left,
9657 __isl_take isl_basic_map *right),
9658 int remove_duplicates)
9659{
9660 unsigned flags = 0;
9661 struct isl_map *result;
9662 int i, j;
9663
9664 if (!map1 || !map2)
9665 goto error;
9666
9667 isl_assert(map1->ctx, isl_space_match(map1->dim, isl_dim_param,
9668 map2->dim, isl_dim_param), goto error);
9669
9670 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
9671 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
9672 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
9673
9674 result = isl_map_alloc_space(space_product(isl_space_copy(map1->dim),
9675 isl_space_copy(map2->dim)),
9676 map1->n * map2->n, flags);
9677 if (!result)
9678 goto error;
9679 for (i = 0; i < map1->n; ++i)
9680 for (j = 0; j < map2->n; ++j) {
9681 struct isl_basic_map *part;
9682 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
9683 isl_basic_map_copy(map2->p[j]));
9684 if (isl_basic_map_is_empty(part))
9685 isl_basic_map_free(part);
9686 else
9687 result = isl_map_add_basic_map(result, part);
9688 if (!result)
9689 goto error;
9690 }
9691 if (remove_duplicates)
9692 result = isl_map_remove_obvious_duplicates(result);
9693 isl_map_free(map1);
9694 isl_map_free(map2);
9695 return result;
9696error:
9697 isl_map_free(map1);
9698 isl_map_free(map2);
9699 return NULL;
9700}
9701
9702/* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
9703 */
9704static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
9705 __isl_take isl_map *map2)
9706{
9707 return map_product(map1, map2, &isl_space_product,
9708 &isl_basic_map_product, 0);
9709}
9710
9711__isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
9712 __isl_take isl_map *map2)
9713{
9714 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
9715}
9716
9717/* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
9718 */
9719__isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
9720 __isl_take isl_map *map2)
9721{
9722 isl_map *prod;
9723
9724 prod = isl_map_product(map1, map2);
9725 prod = isl_map_flatten(prod);
9726 return prod;
9727}
9728
9729/* Given two set A and B, construct its Cartesian product A x B.
9730 */
9731struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
9732{
9733 return isl_map_range_product(set1, set2);
9734}
9735
9736__isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
9737 __isl_take isl_set *set2)
9738{
9739 return isl_map_flat_range_product(set1, set2);
9740}
9741
9742/* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
9743 */
9744static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
9745 __isl_take isl_map *map2)
9746{
9747 return map_product(map1, map2, &isl_space_domain_product,
9748 &isl_basic_map_domain_product, 1);
9749}
9750
9751/* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
9752 */
9753static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
9754 __isl_take isl_map *map2)
9755{
9756 return map_product(map1, map2, &isl_space_range_product,
9757 &isl_basic_map_range_product, 1);
9758}
9759
9760__isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
9761 __isl_take isl_map *map2)
9762{
9763 return isl_map_align_params_map_map_and(map1, map2,
9764 &map_domain_product_aligned);
9765}
9766
9767__isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
9768 __isl_take isl_map *map2)
9769{
9770 return isl_map_align_params_map_map_and(map1, map2,
9771 &map_range_product_aligned);
9772}
9773
9774/* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
9775 */
9776__isl_give isl_map *isl_map_factor_domain(__isl_take isl_map *map)
9777{
9778 isl_space *space;
9779 int total1, keep1, total2, keep2;
9780
9781 if (!map)
9782 return NULL;
9783 if (!isl_space_domain_is_wrapping(map->dim) ||
9784 !isl_space_range_is_wrapping(map->dim))
9785 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9786 "not a product", return isl_map_free(map));
9787
9788 space = isl_map_get_space(map);
9789 total1 = isl_space_dim(space, isl_dim_in);
9790 total2 = isl_space_dim(space, isl_dim_out);
9791 space = isl_space_factor_domain(space);
9792 keep1 = isl_space_dim(space, isl_dim_in);
9793 keep2 = isl_space_dim(space, isl_dim_out);
9794 map = isl_map_project_out(map, isl_dim_in, keep1, total1 - keep1);
9795 map = isl_map_project_out(map, isl_dim_out, keep2, total2 - keep2);
9796 map = isl_map_reset_space(map, space);
9797
9798 return map;
9799}
9800
9801/* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
9802 */
9803__isl_give isl_map *isl_map_factor_range(__isl_take isl_map *map)
9804{
9805 isl_space *space;
9806 int total1, keep1, total2, keep2;
9807
9808 if (!map)
9809 return NULL;
9810 if (!isl_space_domain_is_wrapping(map->dim) ||
9811 !isl_space_range_is_wrapping(map->dim))
9812 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9813 "not a product", return isl_map_free(map));
9814
9815 space = isl_map_get_space(map);
9816 total1 = isl_space_dim(space, isl_dim_in);
9817 total2 = isl_space_dim(space, isl_dim_out);
9818 space = isl_space_factor_range(space);
9819 keep1 = isl_space_dim(space, isl_dim_in);
9820 keep2 = isl_space_dim(space, isl_dim_out);
9821 map = isl_map_project_out(map, isl_dim_in, 0, total1 - keep1);
9822 map = isl_map_project_out(map, isl_dim_out, 0, total2 - keep2);
9823 map = isl_map_reset_space(map, space);
9824
9825 return map;
9826}
9827
9828/* Given a map of the form [A -> B] -> C, return the map A -> C.
9829 */
9830__isl_give isl_map *isl_map_domain_factor_domain(__isl_take isl_map *map)
9831{
9832 isl_space *space;
9833 int total, keep;
9834
9835 if (!map)
9836 return NULL;
9837 if (!isl_space_domain_is_wrapping(map->dim))
9838 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9839 "domain is not a product", return isl_map_free(map));
9840
9841 space = isl_map_get_space(map);
9842 total = isl_space_dim(space, isl_dim_in);
9843 space = isl_space_domain_factor_domain(space);
9844 keep = isl_space_dim(space, isl_dim_in);
9845 map = isl_map_project_out(map, isl_dim_in, keep, total - keep);
9846 map = isl_map_reset_space(map, space);
9847
9848 return map;
9849}
9850
9851/* Given a map of the form [A -> B] -> C, return the map B -> C.
9852 */
9853__isl_give isl_map *isl_map_domain_factor_range(__isl_take isl_map *map)
9854{
9855 isl_space *space;
9856 int total, keep;
9857
9858 if (!map)
9859 return NULL;
9860 if (!isl_space_domain_is_wrapping(map->dim))
9861 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9862 "domain is not a product", return isl_map_free(map));
9863
9864 space = isl_map_get_space(map);
9865 total = isl_space_dim(space, isl_dim_in);
9866 space = isl_space_domain_factor_range(space);
9867 keep = isl_space_dim(space, isl_dim_in);
9868 map = isl_map_project_out(map, isl_dim_in, 0, total - keep);
9869 map = isl_map_reset_space(map, space);
9870
9871 return map;
9872}
9873
9874/* Given a map A -> [B -> C], extract the map A -> B.
9875 */
9876__isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
9877{
9878 isl_space *space;
9879 int total, keep;
9880
9881 if (!map)
9882 return NULL;
9883 if (!isl_space_range_is_wrapping(map->dim))
9884 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9885 "range is not a product", return isl_map_free(map));
9886
9887 space = isl_map_get_space(map);
9888 total = isl_space_dim(space, isl_dim_out);
9889 space = isl_space_range_factor_domain(space);
9890 keep = isl_space_dim(space, isl_dim_out);
9891 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
9892 map = isl_map_reset_space(map, space);
9893
9894 return map;
9895}
9896
9897/* Given a map A -> [B -> C], extract the map A -> C.
9898 */
9899__isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
9900{
9901 isl_space *space;
9902 int total, keep;
9903
9904 if (!map)
9905 return NULL;
9906 if (!isl_space_range_is_wrapping(map->dim))
9907 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9908 "range is not a product", return isl_map_free(map));
9909
9910 space = isl_map_get_space(map);
9911 total = isl_space_dim(space, isl_dim_out);
9912 space = isl_space_range_factor_range(space);
9913 keep = isl_space_dim(space, isl_dim_out);
9914 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
9915 map = isl_map_reset_space(map, space);
9916
9917 return map;
9918}
9919
9920/* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
9921 */
9922__isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
9923 __isl_take isl_map *map2)
9924{
9925 isl_map *prod;
9926
9927 prod = isl_map_domain_product(map1, map2);
9928 prod = isl_map_flatten_domain(prod);
9929 return prod;
9930}
9931
9932/* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
9933 */
9934__isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
9935 __isl_take isl_map *map2)
9936{
9937 isl_map *prod;
9938
9939 prod = isl_map_range_product(map1, map2);
9940 prod = isl_map_flatten_range(prod);
9941 return prod;
9942}
9943
9944uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
9945{
9946 int i;
9947 uint32_t hash = isl_hash_init();
9948 unsigned total;
9949
9950 if (!bmap)
9951 return 0;
9952 bmap = isl_basic_map_copy(bmap);
9953 bmap = isl_basic_map_normalize(bmap);
9954 if (!bmap)
9955 return 0;
9956 total = isl_basic_map_total_dim(bmap);
9957 isl_hash_byte(hash, bmap->n_eq & 0xFF);
9958 for (i = 0; i < bmap->n_eq; ++i) {
9959 uint32_t c_hash;
9960 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
9961 isl_hash_hash(hash, c_hash);
9962 }
9963 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
9964 for (i = 0; i < bmap->n_ineq; ++i) {
9965 uint32_t c_hash;
9966 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
9967 isl_hash_hash(hash, c_hash);
9968 }
9969 isl_hash_byte(hash, bmap->n_div & 0xFF);
9970 for (i = 0; i < bmap->n_div; ++i) {
9971 uint32_t c_hash;
9972 if (isl_int_is_zero(bmap->div[i][0]))
9973 continue;
9974 isl_hash_byte(hash, i & 0xFF);
9975 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
9976 isl_hash_hash(hash, c_hash);
9977 }
9978 isl_basic_map_free(bmap);
9979 return hash;
9980}
9981
9982uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
9983{
9984 return isl_basic_map_get_hash((isl_basic_map *)bset);
9985}
9986
9987uint32_t isl_map_get_hash(__isl_keep isl_map *map)
9988{
9989 int i;
9990 uint32_t hash;
9991
9992 if (!map)
9993 return 0;
9994 map = isl_map_copy(map);
9995 map = isl_map_normalize(map);
9996 if (!map)
9997 return 0;
9998
9999 hash = isl_hash_init();
10000 for (i = 0; i < map->n; ++i) {
10001 uint32_t bmap_hash;
10002 bmap_hash = isl_basic_map_get_hash(map->p[i]);
10003 isl_hash_hash(hash, bmap_hash);
10004 }
10005
10006 isl_map_free(map);
10007
10008 return hash;
10009}
10010
10011uint32_t isl_set_get_hash(__isl_keep isl_set *set)
10012{
10013 return isl_map_get_hash((isl_map *)set);
10014}
10015
10016/* Check if the value for dimension dim is completely determined
10017 * by the values of the other parameters and variables.
10018 * That is, check if dimension dim is involved in an equality.
10019 */
10020int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
10021{
10022 int i;
10023 unsigned nparam;
10024
10025 if (!bset)
10026 return -1;
10027 nparam = isl_basic_set_n_param(bset);
10028 for (i = 0; i < bset->n_eq; ++i)
10029 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
10030 return 1;
10031 return 0;
10032}
10033
10034/* Check if the value for dimension dim is completely determined
10035 * by the values of the other parameters and variables.
10036 * That is, check if dimension dim is involved in an equality
10037 * for each of the subsets.
10038 */
10039int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
10040{
10041 int i;
10042
10043 if (!set)
10044 return -1;
10045 for (i = 0; i < set->n; ++i) {
10046 int unique;
10047 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
10048 if (unique != 1)
10049 return unique;
10050 }
10051 return 1;
10052}
10053
10054/* Return the number of basic maps in the (current) representation of "map".
10055 */
10056int isl_map_n_basic_map(__isl_keep isl_map *map)
10057{
10058 return map ? map->n : 0;
10059}
10060
10061int isl_set_n_basic_set(__isl_keep isl_set *set)
10062{
10063 return set ? set->n : 0;
10064}
10065
Tobias Grosserb2f39922015-05-28 13:32:11 +000010066isl_stat isl_map_foreach_basic_map(__isl_keep isl_map *map,
10067 isl_stat (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
Tobias Grosser52a25232015-02-04 20:55:43 +000010068{
10069 int i;
10070
10071 if (!map)
Tobias Grosserb2f39922015-05-28 13:32:11 +000010072 return isl_stat_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000010073
10074 for (i = 0; i < map->n; ++i)
10075 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
Tobias Grosserb2f39922015-05-28 13:32:11 +000010076 return isl_stat_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000010077
Tobias Grosserb2f39922015-05-28 13:32:11 +000010078 return isl_stat_ok;
Tobias Grosser52a25232015-02-04 20:55:43 +000010079}
10080
Tobias Grosserb2f39922015-05-28 13:32:11 +000010081isl_stat isl_set_foreach_basic_set(__isl_keep isl_set *set,
10082 isl_stat (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
Tobias Grosser52a25232015-02-04 20:55:43 +000010083{
10084 int i;
10085
10086 if (!set)
Tobias Grosserb2f39922015-05-28 13:32:11 +000010087 return isl_stat_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000010088
10089 for (i = 0; i < set->n; ++i)
10090 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
Tobias Grosserb2f39922015-05-28 13:32:11 +000010091 return isl_stat_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000010092
Tobias Grosserb2f39922015-05-28 13:32:11 +000010093 return isl_stat_ok;
Tobias Grosser52a25232015-02-04 20:55:43 +000010094}
10095
Tobias Grosser4db55312015-06-30 08:22:14 +000010096/* Return a list of basic sets, the union of which is equal to "set".
10097 */
10098__isl_give isl_basic_set_list *isl_set_get_basic_set_list(
10099 __isl_keep isl_set *set)
10100{
10101 int i;
10102 isl_basic_set_list *list;
10103
10104 if (!set)
10105 return NULL;
10106
10107 list = isl_basic_set_list_alloc(isl_set_get_ctx(set), set->n);
10108 for (i = 0; i < set->n; ++i) {
10109 isl_basic_set *bset;
10110
10111 bset = isl_basic_set_copy(set->p[i]);
10112 list = isl_basic_set_list_add(list, bset);
10113 }
10114
10115 return list;
10116}
10117
Tobias Grosser52a25232015-02-04 20:55:43 +000010118__isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
10119{
10120 isl_space *dim;
10121
10122 if (!bset)
10123 return NULL;
10124
10125 bset = isl_basic_set_cow(bset);
10126 if (!bset)
10127 return NULL;
10128
10129 dim = isl_basic_set_get_space(bset);
10130 dim = isl_space_lift(dim, bset->n_div);
10131 if (!dim)
10132 goto error;
10133 isl_space_free(bset->dim);
10134 bset->dim = dim;
10135 bset->extra -= bset->n_div;
10136 bset->n_div = 0;
10137
10138 bset = isl_basic_set_finalize(bset);
10139
10140 return bset;
10141error:
10142 isl_basic_set_free(bset);
10143 return NULL;
10144}
10145
10146__isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
10147{
10148 int i;
10149 isl_space *dim;
10150 unsigned n_div;
10151
10152 set = isl_set_align_divs(set);
10153
10154 if (!set)
10155 return NULL;
10156
10157 set = isl_set_cow(set);
10158 if (!set)
10159 return NULL;
10160
10161 n_div = set->p[0]->n_div;
10162 dim = isl_set_get_space(set);
10163 dim = isl_space_lift(dim, n_div);
10164 if (!dim)
10165 goto error;
10166 isl_space_free(set->dim);
10167 set->dim = dim;
10168
10169 for (i = 0; i < set->n; ++i) {
10170 set->p[i] = isl_basic_set_lift(set->p[i]);
10171 if (!set->p[i])
10172 goto error;
10173 }
10174
10175 return set;
10176error:
10177 isl_set_free(set);
10178 return NULL;
10179}
10180
10181__isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
10182{
10183 isl_space *dim;
10184 struct isl_basic_map *bmap;
10185 unsigned n_set;
10186 unsigned n_div;
10187 unsigned n_param;
10188 unsigned total;
10189 int i, k, l;
10190
10191 set = isl_set_align_divs(set);
10192
10193 if (!set)
10194 return NULL;
10195
10196 dim = isl_set_get_space(set);
10197 if (set->n == 0 || set->p[0]->n_div == 0) {
10198 isl_set_free(set);
10199 return isl_map_identity(isl_space_map_from_set(dim));
10200 }
10201
10202 n_div = set->p[0]->n_div;
10203 dim = isl_space_map_from_set(dim);
10204 n_param = isl_space_dim(dim, isl_dim_param);
10205 n_set = isl_space_dim(dim, isl_dim_in);
10206 dim = isl_space_extend(dim, n_param, n_set, n_set + n_div);
10207 bmap = isl_basic_map_alloc_space(dim, 0, n_set, 2 * n_div);
10208 for (i = 0; i < n_set; ++i)
10209 bmap = var_equal(bmap, i);
10210
10211 total = n_param + n_set + n_set + n_div;
10212 for (i = 0; i < n_div; ++i) {
10213 k = isl_basic_map_alloc_inequality(bmap);
10214 if (k < 0)
10215 goto error;
10216 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
10217 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
10218 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
10219 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
10220 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
10221 set->p[0]->div[i][0]);
10222
10223 l = isl_basic_map_alloc_inequality(bmap);
10224 if (l < 0)
10225 goto error;
10226 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
10227 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
10228 set->p[0]->div[i][0]);
10229 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
10230 }
10231
10232 isl_set_free(set);
10233 bmap = isl_basic_map_simplify(bmap);
10234 bmap = isl_basic_map_finalize(bmap);
10235 return isl_map_from_basic_map(bmap);
10236error:
10237 isl_set_free(set);
10238 isl_basic_map_free(bmap);
10239 return NULL;
10240}
10241
10242int isl_basic_set_size(__isl_keep isl_basic_set *bset)
10243{
10244 unsigned dim;
10245 int size = 0;
10246
10247 if (!bset)
10248 return -1;
10249
10250 dim = isl_basic_set_total_dim(bset);
10251 size += bset->n_eq * (1 + dim);
10252 size += bset->n_ineq * (1 + dim);
10253 size += bset->n_div * (2 + dim);
10254
10255 return size;
10256}
10257
10258int isl_set_size(__isl_keep isl_set *set)
10259{
10260 int i;
10261 int size = 0;
10262
10263 if (!set)
10264 return -1;
10265
10266 for (i = 0; i < set->n; ++i)
10267 size += isl_basic_set_size(set->p[i]);
10268
10269 return size;
10270}
10271
10272/* Check if there is any lower bound (if lower == 0) and/or upper
10273 * bound (if upper == 0) on the specified dim.
10274 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000010275static isl_bool basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
Tobias Grosser52a25232015-02-04 20:55:43 +000010276 enum isl_dim_type type, unsigned pos, int lower, int upper)
10277{
10278 int i;
10279
10280 if (!bmap)
Tobias Grosserb2f39922015-05-28 13:32:11 +000010281 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000010282
Tobias Grosserb2f39922015-05-28 13:32:11 +000010283 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type),
10284 return isl_bool_error);
Tobias Grosser52a25232015-02-04 20:55:43 +000010285
10286 pos += isl_basic_map_offset(bmap, type);
10287
10288 for (i = 0; i < bmap->n_div; ++i) {
10289 if (isl_int_is_zero(bmap->div[i][0]))
10290 continue;
10291 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
Tobias Grosserb2f39922015-05-28 13:32:11 +000010292 return isl_bool_true;
Tobias Grosser52a25232015-02-04 20:55:43 +000010293 }
10294
10295 for (i = 0; i < bmap->n_eq; ++i)
10296 if (!isl_int_is_zero(bmap->eq[i][pos]))
Tobias Grosserb2f39922015-05-28 13:32:11 +000010297 return isl_bool_true;
Tobias Grosser52a25232015-02-04 20:55:43 +000010298
10299 for (i = 0; i < bmap->n_ineq; ++i) {
10300 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
10301 if (sgn > 0)
10302 lower = 1;
10303 if (sgn < 0)
10304 upper = 1;
10305 }
10306
10307 return lower && upper;
10308}
10309
10310int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10311 enum isl_dim_type type, unsigned pos)
10312{
10313 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
10314}
10315
Tobias Grosserb2f39922015-05-28 13:32:11 +000010316isl_bool isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
Tobias Grosser52a25232015-02-04 20:55:43 +000010317 enum isl_dim_type type, unsigned pos)
10318{
10319 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
10320}
10321
Tobias Grosserb2f39922015-05-28 13:32:11 +000010322isl_bool isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
Tobias Grosser52a25232015-02-04 20:55:43 +000010323 enum isl_dim_type type, unsigned pos)
10324{
10325 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
10326}
10327
10328int isl_map_dim_is_bounded(__isl_keep isl_map *map,
10329 enum isl_dim_type type, unsigned pos)
10330{
10331 int i;
10332
10333 if (!map)
10334 return -1;
10335
10336 for (i = 0; i < map->n; ++i) {
10337 int bounded;
10338 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
10339 if (bounded < 0 || !bounded)
10340 return bounded;
10341 }
10342
10343 return 1;
10344}
10345
10346/* Return 1 if the specified dim is involved in both an upper bound
10347 * and a lower bound.
10348 */
10349int isl_set_dim_is_bounded(__isl_keep isl_set *set,
10350 enum isl_dim_type type, unsigned pos)
10351{
10352 return isl_map_dim_is_bounded((isl_map *)set, type, pos);
10353}
10354
10355/* Does "map" have a bound (according to "fn") for any of its basic maps?
10356 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000010357static isl_bool has_any_bound(__isl_keep isl_map *map,
Tobias Grosser52a25232015-02-04 20:55:43 +000010358 enum isl_dim_type type, unsigned pos,
Tobias Grosserb2f39922015-05-28 13:32:11 +000010359 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
Tobias Grosser52a25232015-02-04 20:55:43 +000010360 enum isl_dim_type type, unsigned pos))
10361{
10362 int i;
10363
10364 if (!map)
Tobias Grosserb2f39922015-05-28 13:32:11 +000010365 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000010366
10367 for (i = 0; i < map->n; ++i) {
Tobias Grosserb2f39922015-05-28 13:32:11 +000010368 isl_bool bounded;
Tobias Grosser52a25232015-02-04 20:55:43 +000010369 bounded = fn(map->p[i], type, pos);
10370 if (bounded < 0 || bounded)
10371 return bounded;
10372 }
10373
Tobias Grosserb2f39922015-05-28 13:32:11 +000010374 return isl_bool_false;
Tobias Grosser52a25232015-02-04 20:55:43 +000010375}
10376
10377/* Return 1 if the specified dim is involved in any lower bound.
10378 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000010379isl_bool isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
Tobias Grosser52a25232015-02-04 20:55:43 +000010380 enum isl_dim_type type, unsigned pos)
10381{
10382 return has_any_bound(set, type, pos,
10383 &isl_basic_map_dim_has_lower_bound);
10384}
10385
10386/* Return 1 if the specified dim is involved in any upper bound.
10387 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000010388isl_bool isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
Tobias Grosser52a25232015-02-04 20:55:43 +000010389 enum isl_dim_type type, unsigned pos)
10390{
10391 return has_any_bound(set, type, pos,
10392 &isl_basic_map_dim_has_upper_bound);
10393}
10394
10395/* Does "map" have a bound (according to "fn") for all of its basic maps?
10396 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000010397static isl_bool has_bound(__isl_keep isl_map *map,
Tobias Grosser52a25232015-02-04 20:55:43 +000010398 enum isl_dim_type type, unsigned pos,
Tobias Grosserb2f39922015-05-28 13:32:11 +000010399 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
Tobias Grosser52a25232015-02-04 20:55:43 +000010400 enum isl_dim_type type, unsigned pos))
10401{
10402 int i;
10403
10404 if (!map)
Tobias Grosserb2f39922015-05-28 13:32:11 +000010405 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000010406
10407 for (i = 0; i < map->n; ++i) {
Tobias Grosserb2f39922015-05-28 13:32:11 +000010408 isl_bool bounded;
Tobias Grosser52a25232015-02-04 20:55:43 +000010409 bounded = fn(map->p[i], type, pos);
10410 if (bounded < 0 || !bounded)
10411 return bounded;
10412 }
10413
Tobias Grosserb2f39922015-05-28 13:32:11 +000010414 return isl_bool_true;
Tobias Grosser52a25232015-02-04 20:55:43 +000010415}
10416
10417/* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10418 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000010419isl_bool isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
Tobias Grosser52a25232015-02-04 20:55:43 +000010420 enum isl_dim_type type, unsigned pos)
10421{
10422 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
10423}
10424
10425/* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10426 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000010427isl_bool isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
Tobias Grosser52a25232015-02-04 20:55:43 +000010428 enum isl_dim_type type, unsigned pos)
10429{
10430 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
10431}
10432
10433/* For each of the "n" variables starting at "first", determine
10434 * the sign of the variable and put the results in the first "n"
10435 * elements of the array "signs".
10436 * Sign
10437 * 1 means that the variable is non-negative
10438 * -1 means that the variable is non-positive
10439 * 0 means the variable attains both positive and negative values.
10440 */
10441int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
10442 unsigned first, unsigned n, int *signs)
10443{
10444 isl_vec *bound = NULL;
10445 struct isl_tab *tab = NULL;
10446 struct isl_tab_undo *snap;
10447 int i;
10448
10449 if (!bset || !signs)
10450 return -1;
10451
10452 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
10453 tab = isl_tab_from_basic_set(bset, 0);
10454 if (!bound || !tab)
10455 goto error;
10456
10457 isl_seq_clr(bound->el, bound->size);
10458 isl_int_set_si(bound->el[0], -1);
10459
10460 snap = isl_tab_snap(tab);
10461 for (i = 0; i < n; ++i) {
10462 int empty;
10463
10464 isl_int_set_si(bound->el[1 + first + i], -1);
10465 if (isl_tab_add_ineq(tab, bound->el) < 0)
10466 goto error;
10467 empty = tab->empty;
10468 isl_int_set_si(bound->el[1 + first + i], 0);
10469 if (isl_tab_rollback(tab, snap) < 0)
10470 goto error;
10471
10472 if (empty) {
10473 signs[i] = 1;
10474 continue;
10475 }
10476
10477 isl_int_set_si(bound->el[1 + first + i], 1);
10478 if (isl_tab_add_ineq(tab, bound->el) < 0)
10479 goto error;
10480 empty = tab->empty;
10481 isl_int_set_si(bound->el[1 + first + i], 0);
10482 if (isl_tab_rollback(tab, snap) < 0)
10483 goto error;
10484
10485 signs[i] = empty ? -1 : 0;
10486 }
10487
10488 isl_tab_free(tab);
10489 isl_vec_free(bound);
10490 return 0;
10491error:
10492 isl_tab_free(tab);
10493 isl_vec_free(bound);
10494 return -1;
10495}
10496
10497int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
10498 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
10499{
10500 if (!bset || !signs)
10501 return -1;
10502 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
10503 return -1);
10504
10505 first += pos(bset->dim, type) - 1;
10506 return isl_basic_set_vars_get_sign(bset, first, n, signs);
10507}
10508
10509/* Is it possible for the integer division "div" to depend (possibly
10510 * indirectly) on any output dimensions?
10511 *
10512 * If the div is undefined, then we conservatively assume that it
10513 * may depend on them.
10514 * Otherwise, we check if it actually depends on them or on any integer
10515 * divisions that may depend on them.
10516 */
10517static int div_may_involve_output(__isl_keep isl_basic_map *bmap, int div)
10518{
10519 int i;
10520 unsigned n_out, o_out;
10521 unsigned n_div, o_div;
10522
10523 if (isl_int_is_zero(bmap->div[div][0]))
10524 return 1;
10525
10526 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10527 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10528
10529 if (isl_seq_first_non_zero(bmap->div[div] + 1 + o_out, n_out) != -1)
10530 return 1;
10531
10532 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10533 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10534
10535 for (i = 0; i < n_div; ++i) {
10536 if (isl_int_is_zero(bmap->div[div][1 + o_div + i]))
10537 continue;
10538 if (div_may_involve_output(bmap, i))
10539 return 1;
10540 }
10541
10542 return 0;
10543}
10544
Michael Kruse959a8dc2016-01-15 15:54:45 +000010545/* Return the first integer division of "bmap" in the range
10546 * [first, first + n[ that may depend on any output dimensions and
10547 * that has a non-zero coefficient in "c" (where the first coefficient
10548 * in "c" corresponds to integer division "first").
10549 */
10550static int first_div_may_involve_output(__isl_keep isl_basic_map *bmap,
10551 isl_int *c, int first, int n)
10552{
10553 int k;
10554
10555 if (!bmap)
10556 return -1;
10557
10558 for (k = first; k < first + n; ++k) {
10559 if (isl_int_is_zero(c[k]))
10560 continue;
10561 if (div_may_involve_output(bmap, k))
10562 return k;
10563 }
10564
10565 return first + n;
10566}
10567
10568/* Look for a pair of inequality constraints in "bmap" of the form
10569 *
10570 * -l + i >= 0 or i >= l
10571 * and
10572 * n + l - i >= 0 or i <= l + n
10573 *
10574 * with n < "m" and i the output dimension at position "pos".
10575 * (Note that n >= 0 as otherwise the two constraints would conflict.)
10576 * Furthermore, "l" is only allowed to involve parameters, input dimensions
10577 * and earlier output dimensions, as well as integer divisions that do
10578 * not involve any of the output dimensions.
10579 *
10580 * Return the index of the first inequality constraint or bmap->n_ineq
10581 * if no such pair can be found.
10582 */
10583static int find_modulo_constraint_pair(__isl_keep isl_basic_map *bmap,
10584 int pos, isl_int m)
10585{
10586 int i, j;
10587 isl_ctx *ctx;
10588 unsigned total;
10589 unsigned n_div, o_div;
10590 unsigned n_out, o_out;
10591 int less;
10592
10593 if (!bmap)
10594 return -1;
10595
10596 ctx = isl_basic_map_get_ctx(bmap);
10597 total = isl_basic_map_total_dim(bmap);
10598 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10599 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10600 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10601 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10602 for (i = 0; i < bmap->n_ineq; ++i) {
10603 if (!isl_int_abs_eq(bmap->ineq[i][o_out + pos], ctx->one))
10604 continue;
10605 if (isl_seq_first_non_zero(bmap->ineq[i] + o_out + pos + 1,
10606 n_out - (pos + 1)) != -1)
10607 continue;
10608 if (first_div_may_involve_output(bmap, bmap->ineq[i] + o_div,
10609 0, n_div) < n_div)
10610 continue;
10611 for (j = i + 1; j < bmap->n_ineq; ++j) {
10612 if (!isl_int_abs_eq(bmap->ineq[j][o_out + pos],
10613 ctx->one))
10614 continue;
10615 if (!isl_seq_is_neg(bmap->ineq[i] + 1,
10616 bmap->ineq[j] + 1, total))
10617 continue;
10618 break;
10619 }
10620 if (j >= bmap->n_ineq)
10621 continue;
10622 isl_int_add(bmap->ineq[i][0],
10623 bmap->ineq[i][0], bmap->ineq[j][0]);
10624 less = isl_int_abs_lt(bmap->ineq[i][0], m);
10625 isl_int_sub(bmap->ineq[i][0],
10626 bmap->ineq[i][0], bmap->ineq[j][0]);
10627 if (!less)
10628 continue;
10629 if (isl_int_is_one(bmap->ineq[i][o_out + pos]))
10630 return i;
10631 else
10632 return j;
10633 }
10634
10635 return bmap->n_ineq;
10636}
10637
Tobias Grosser52a25232015-02-04 20:55:43 +000010638/* Return the index of the equality of "bmap" that defines
10639 * the output dimension "pos" in terms of earlier dimensions.
10640 * The equality may also involve integer divisions, as long
10641 * as those integer divisions are defined in terms of
10642 * parameters or input dimensions.
Michael Kruse959a8dc2016-01-15 15:54:45 +000010643 * In this case, *div is set to the number of integer divisions and
10644 * *ineq is set to the number of inequality constraints (provided
10645 * div and ineq are not NULL).
10646 *
10647 * The equality may also involve a single integer division involving
10648 * the output dimensions (typically only output dimension "pos") as
10649 * long as the coefficient of output dimension "pos" is 1 or -1 and
10650 * there is a pair of constraints i >= l and i <= l + n, with i referring
10651 * to output dimension "pos", l an expression involving only earlier
10652 * dimensions and n smaller than the coefficient of the integer division
10653 * in the equality. In this case, the output dimension can be defined
10654 * in terms of a modulo expression that does not involve the integer division.
10655 * *div is then set to this single integer division and
10656 * *ineq is set to the index of constraint i >= l.
10657 *
Tobias Grosser52a25232015-02-04 20:55:43 +000010658 * Return bmap->n_eq if there is no such equality.
10659 * Return -1 on error.
10660 */
10661int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map *bmap,
Michael Kruse959a8dc2016-01-15 15:54:45 +000010662 int pos, int *div, int *ineq)
Tobias Grosser52a25232015-02-04 20:55:43 +000010663{
Michael Kruse959a8dc2016-01-15 15:54:45 +000010664 int j, k, l;
Tobias Grosser52a25232015-02-04 20:55:43 +000010665 unsigned n_out, o_out;
10666 unsigned n_div, o_div;
10667
10668 if (!bmap)
10669 return -1;
10670
10671 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10672 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10673 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10674 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10675
Michael Kruse959a8dc2016-01-15 15:54:45 +000010676 if (ineq)
10677 *ineq = bmap->n_ineq;
10678 if (div)
10679 *div = n_div;
Tobias Grosser52a25232015-02-04 20:55:43 +000010680 for (j = 0; j < bmap->n_eq; ++j) {
10681 if (isl_int_is_zero(bmap->eq[j][o_out + pos]))
10682 continue;
10683 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + pos + 1,
10684 n_out - (pos + 1)) != -1)
10685 continue;
Michael Kruse959a8dc2016-01-15 15:54:45 +000010686 k = first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
10687 0, n_div);
Tobias Grosser52a25232015-02-04 20:55:43 +000010688 if (k >= n_div)
10689 return j;
Michael Kruse959a8dc2016-01-15 15:54:45 +000010690 if (!isl_int_is_one(bmap->eq[j][o_out + pos]) &&
10691 !isl_int_is_negone(bmap->eq[j][o_out + pos]))
10692 continue;
10693 if (first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
10694 k + 1, n_div - (k+1)) < n_div)
10695 continue;
10696 l = find_modulo_constraint_pair(bmap, pos,
10697 bmap->eq[j][o_div + k]);
10698 if (l < 0)
10699 return -1;
10700 if (l >= bmap->n_ineq)
10701 continue;
10702 if (div)
10703 *div = k;
10704 if (ineq)
10705 *ineq = l;
10706 return j;
Tobias Grosser52a25232015-02-04 20:55:43 +000010707 }
10708
10709 return bmap->n_eq;
10710}
10711
10712/* Check if the given basic map is obviously single-valued.
10713 * In particular, for each output dimension, check that there is
10714 * an equality that defines the output dimension in terms of
10715 * earlier dimensions.
10716 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000010717isl_bool isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
Tobias Grosser52a25232015-02-04 20:55:43 +000010718{
10719 int i;
10720 unsigned n_out;
10721
10722 if (!bmap)
Tobias Grosserb2f39922015-05-28 13:32:11 +000010723 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000010724
10725 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10726
10727 for (i = 0; i < n_out; ++i) {
10728 int eq;
10729
Michael Kruse959a8dc2016-01-15 15:54:45 +000010730 eq = isl_basic_map_output_defining_equality(bmap, i,
10731 NULL, NULL);
Tobias Grosser52a25232015-02-04 20:55:43 +000010732 if (eq < 0)
Tobias Grosserb2f39922015-05-28 13:32:11 +000010733 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000010734 if (eq >= bmap->n_eq)
Tobias Grosserb2f39922015-05-28 13:32:11 +000010735 return isl_bool_false;
Tobias Grosser52a25232015-02-04 20:55:43 +000010736 }
10737
Tobias Grosserb2f39922015-05-28 13:32:11 +000010738 return isl_bool_true;
Tobias Grosser52a25232015-02-04 20:55:43 +000010739}
10740
10741/* Check if the given basic map is single-valued.
10742 * We simply compute
10743 *
10744 * M \circ M^-1
10745 *
10746 * and check if the result is a subset of the identity mapping.
10747 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000010748isl_bool isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
Tobias Grosser52a25232015-02-04 20:55:43 +000010749{
10750 isl_space *space;
10751 isl_basic_map *test;
10752 isl_basic_map *id;
Tobias Grosserb2f39922015-05-28 13:32:11 +000010753 isl_bool sv;
Tobias Grosser52a25232015-02-04 20:55:43 +000010754
10755 sv = isl_basic_map_plain_is_single_valued(bmap);
10756 if (sv < 0 || sv)
10757 return sv;
10758
10759 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
10760 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
10761
10762 space = isl_basic_map_get_space(bmap);
10763 space = isl_space_map_from_set(isl_space_range(space));
10764 id = isl_basic_map_identity(space);
10765
10766 sv = isl_basic_map_is_subset(test, id);
10767
10768 isl_basic_map_free(test);
10769 isl_basic_map_free(id);
10770
10771 return sv;
10772}
10773
10774/* Check if the given map is obviously single-valued.
10775 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000010776isl_bool isl_map_plain_is_single_valued(__isl_keep isl_map *map)
Tobias Grosser52a25232015-02-04 20:55:43 +000010777{
10778 if (!map)
Tobias Grosserb2f39922015-05-28 13:32:11 +000010779 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000010780 if (map->n == 0)
Tobias Grosserb2f39922015-05-28 13:32:11 +000010781 return isl_bool_true;
Tobias Grosser52a25232015-02-04 20:55:43 +000010782 if (map->n >= 2)
Tobias Grosserb2f39922015-05-28 13:32:11 +000010783 return isl_bool_false;
Tobias Grosser52a25232015-02-04 20:55:43 +000010784
10785 return isl_basic_map_plain_is_single_valued(map->p[0]);
10786}
10787
10788/* Check if the given map is single-valued.
10789 * We simply compute
10790 *
10791 * M \circ M^-1
10792 *
10793 * and check if the result is a subset of the identity mapping.
10794 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000010795isl_bool isl_map_is_single_valued(__isl_keep isl_map *map)
Tobias Grosser52a25232015-02-04 20:55:43 +000010796{
10797 isl_space *dim;
10798 isl_map *test;
10799 isl_map *id;
Tobias Grosserb2f39922015-05-28 13:32:11 +000010800 isl_bool sv;
Tobias Grosser52a25232015-02-04 20:55:43 +000010801
10802 sv = isl_map_plain_is_single_valued(map);
10803 if (sv < 0 || sv)
10804 return sv;
10805
10806 test = isl_map_reverse(isl_map_copy(map));
10807 test = isl_map_apply_range(test, isl_map_copy(map));
10808
10809 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
10810 id = isl_map_identity(dim);
10811
10812 sv = isl_map_is_subset(test, id);
10813
10814 isl_map_free(test);
10815 isl_map_free(id);
10816
10817 return sv;
10818}
10819
Tobias Grosserb2f39922015-05-28 13:32:11 +000010820isl_bool isl_map_is_injective(__isl_keep isl_map *map)
Tobias Grosser52a25232015-02-04 20:55:43 +000010821{
Tobias Grosserb2f39922015-05-28 13:32:11 +000010822 isl_bool in;
Tobias Grosser52a25232015-02-04 20:55:43 +000010823
10824 map = isl_map_copy(map);
10825 map = isl_map_reverse(map);
10826 in = isl_map_is_single_valued(map);
10827 isl_map_free(map);
10828
10829 return in;
10830}
10831
10832/* Check if the given map is obviously injective.
10833 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000010834isl_bool isl_map_plain_is_injective(__isl_keep isl_map *map)
Tobias Grosser52a25232015-02-04 20:55:43 +000010835{
Tobias Grosserb2f39922015-05-28 13:32:11 +000010836 isl_bool in;
Tobias Grosser52a25232015-02-04 20:55:43 +000010837
10838 map = isl_map_copy(map);
10839 map = isl_map_reverse(map);
10840 in = isl_map_plain_is_single_valued(map);
10841 isl_map_free(map);
10842
10843 return in;
10844}
10845
Tobias Grosserb2f39922015-05-28 13:32:11 +000010846isl_bool isl_map_is_bijective(__isl_keep isl_map *map)
Tobias Grosser52a25232015-02-04 20:55:43 +000010847{
Tobias Grosserb2f39922015-05-28 13:32:11 +000010848 isl_bool sv;
Tobias Grosser52a25232015-02-04 20:55:43 +000010849
10850 sv = isl_map_is_single_valued(map);
10851 if (sv < 0 || !sv)
10852 return sv;
10853
10854 return isl_map_is_injective(map);
10855}
10856
Tobias Grosserb2f39922015-05-28 13:32:11 +000010857isl_bool isl_set_is_singleton(__isl_keep isl_set *set)
Tobias Grosser52a25232015-02-04 20:55:43 +000010858{
10859 return isl_map_is_single_valued((isl_map *)set);
10860}
10861
Michael Krusee0b34f32016-05-04 14:41:36 +000010862/* Does "map" only map elements to themselves?
10863 *
10864 * If the domain and range spaces are different, then "map"
10865 * is considered not to be an identity relation, even if it is empty.
10866 * Otherwise, construct the maximal identity relation and
10867 * check whether "map" is a subset of this relation.
10868 */
10869isl_bool isl_map_is_identity(__isl_keep isl_map *map)
10870{
10871 isl_space *space;
10872 isl_map *id;
10873 isl_bool equal, is_identity;
10874
10875 space = isl_map_get_space(map);
10876 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
10877 isl_space_free(space);
10878 if (equal < 0 || !equal)
10879 return equal;
10880
10881 id = isl_map_identity(isl_map_get_space(map));
10882 is_identity = isl_map_is_subset(map, id);
10883 isl_map_free(id);
10884
10885 return is_identity;
10886}
10887
Tobias Grosser52a25232015-02-04 20:55:43 +000010888int isl_map_is_translation(__isl_keep isl_map *map)
10889{
10890 int ok;
10891 isl_set *delta;
10892
10893 delta = isl_map_deltas(isl_map_copy(map));
10894 ok = isl_set_is_singleton(delta);
10895 isl_set_free(delta);
10896
10897 return ok;
10898}
10899
10900static int unique(isl_int *p, unsigned pos, unsigned len)
10901{
10902 if (isl_seq_first_non_zero(p, pos) != -1)
10903 return 0;
10904 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
10905 return 0;
10906 return 1;
10907}
10908
10909int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
10910{
10911 int i, j;
10912 unsigned nvar;
10913 unsigned ovar;
10914
10915 if (!bset)
10916 return -1;
10917
10918 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
10919 return 0;
10920
10921 nvar = isl_basic_set_dim(bset, isl_dim_set);
10922 ovar = isl_space_offset(bset->dim, isl_dim_set);
10923 for (j = 0; j < nvar; ++j) {
10924 int lower = 0, upper = 0;
10925 for (i = 0; i < bset->n_eq; ++i) {
10926 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
10927 continue;
10928 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
10929 return 0;
10930 break;
10931 }
10932 if (i < bset->n_eq)
10933 continue;
10934 for (i = 0; i < bset->n_ineq; ++i) {
10935 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
10936 continue;
10937 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
10938 return 0;
10939 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
10940 lower = 1;
10941 else
10942 upper = 1;
10943 }
10944 if (!lower || !upper)
10945 return 0;
10946 }
10947
10948 return 1;
10949}
10950
10951int isl_set_is_box(__isl_keep isl_set *set)
10952{
10953 if (!set)
10954 return -1;
10955 if (set->n != 1)
10956 return 0;
10957
10958 return isl_basic_set_is_box(set->p[0]);
10959}
10960
Tobias Grosserb2f39922015-05-28 13:32:11 +000010961isl_bool isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
Tobias Grosser52a25232015-02-04 20:55:43 +000010962{
10963 if (!bset)
Tobias Grosserb2f39922015-05-28 13:32:11 +000010964 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000010965
10966 return isl_space_is_wrapping(bset->dim);
10967}
10968
Tobias Grosserb2f39922015-05-28 13:32:11 +000010969isl_bool isl_set_is_wrapping(__isl_keep isl_set *set)
Tobias Grosser52a25232015-02-04 20:55:43 +000010970{
10971 if (!set)
Tobias Grosserb2f39922015-05-28 13:32:11 +000010972 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000010973
10974 return isl_space_is_wrapping(set->dim);
10975}
10976
Michael Kruse959a8dc2016-01-15 15:54:45 +000010977/* Modify the space of "map" through a call to "change".
10978 * If "can_change" is set (not NULL), then first call it to check
10979 * if the modification is allowed, printing the error message "cannot_change"
10980 * if it is not.
10981 */
10982static __isl_give isl_map *isl_map_change_space(__isl_take isl_map *map,
10983 isl_bool (*can_change)(__isl_keep isl_map *map),
10984 const char *cannot_change,
10985 __isl_give isl_space *(*change)(__isl_take isl_space *space))
10986{
10987 isl_bool ok;
10988 isl_space *space;
10989
10990 if (!map)
10991 return NULL;
10992
10993 ok = can_change ? can_change(map) : isl_bool_true;
10994 if (ok < 0)
10995 return isl_map_free(map);
10996 if (!ok)
10997 isl_die(isl_map_get_ctx(map), isl_error_invalid, cannot_change,
10998 return isl_map_free(map));
10999
11000 space = change(isl_map_get_space(map));
11001 map = isl_map_reset_space(map, space);
11002
11003 return map;
11004}
11005
Tobias Grosser52a25232015-02-04 20:55:43 +000011006/* Is the domain of "map" a wrapped relation?
11007 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000011008isl_bool isl_map_domain_is_wrapping(__isl_keep isl_map *map)
Tobias Grosser52a25232015-02-04 20:55:43 +000011009{
11010 if (!map)
Tobias Grosserb2f39922015-05-28 13:32:11 +000011011 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000011012
11013 return isl_space_domain_is_wrapping(map->dim);
11014}
11015
11016/* Is the range of "map" a wrapped relation?
11017 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000011018isl_bool isl_map_range_is_wrapping(__isl_keep isl_map *map)
Tobias Grosser52a25232015-02-04 20:55:43 +000011019{
11020 if (!map)
Tobias Grosserb2f39922015-05-28 13:32:11 +000011021 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000011022
11023 return isl_space_range_is_wrapping(map->dim);
11024}
11025
11026__isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
11027{
11028 bmap = isl_basic_map_cow(bmap);
11029 if (!bmap)
11030 return NULL;
11031
11032 bmap->dim = isl_space_wrap(bmap->dim);
11033 if (!bmap->dim)
11034 goto error;
11035
11036 bmap = isl_basic_map_finalize(bmap);
11037
11038 return (isl_basic_set *)bmap;
11039error:
11040 isl_basic_map_free(bmap);
11041 return NULL;
11042}
11043
Michael Kruse959a8dc2016-01-15 15:54:45 +000011044/* Given a map A -> B, return the set (A -> B).
11045 */
Tobias Grosser52a25232015-02-04 20:55:43 +000011046__isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
11047{
Michael Kruse959a8dc2016-01-15 15:54:45 +000011048 return isl_map_change_space(map, NULL, NULL, &isl_space_wrap);
Tobias Grosser52a25232015-02-04 20:55:43 +000011049}
11050
11051__isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
11052{
11053 bset = isl_basic_set_cow(bset);
11054 if (!bset)
11055 return NULL;
11056
11057 bset->dim = isl_space_unwrap(bset->dim);
11058 if (!bset->dim)
11059 goto error;
11060
11061 bset = isl_basic_set_finalize(bset);
11062
11063 return (isl_basic_map *)bset;
11064error:
11065 isl_basic_set_free(bset);
11066 return NULL;
11067}
11068
Michael Kruse959a8dc2016-01-15 15:54:45 +000011069/* Given a set (A -> B), return the map A -> B.
11070 * Error out if "set" is not of the form (A -> B).
11071 */
Tobias Grosser52a25232015-02-04 20:55:43 +000011072__isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
11073{
Michael Kruse959a8dc2016-01-15 15:54:45 +000011074 return isl_map_change_space(set, &isl_set_is_wrapping,
11075 "not a wrapping set", &isl_space_unwrap);
Tobias Grosser52a25232015-02-04 20:55:43 +000011076}
11077
11078__isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
11079 enum isl_dim_type type)
11080{
11081 if (!bmap)
11082 return NULL;
11083
11084 if (!isl_space_is_named_or_nested(bmap->dim, type))
11085 return bmap;
11086
11087 bmap = isl_basic_map_cow(bmap);
11088 if (!bmap)
11089 return NULL;
11090
11091 bmap->dim = isl_space_reset(bmap->dim, type);
11092 if (!bmap->dim)
11093 goto error;
11094
11095 bmap = isl_basic_map_finalize(bmap);
11096
11097 return bmap;
11098error:
11099 isl_basic_map_free(bmap);
11100 return NULL;
11101}
11102
11103__isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
11104 enum isl_dim_type type)
11105{
11106 int i;
11107
11108 if (!map)
11109 return NULL;
11110
11111 if (!isl_space_is_named_or_nested(map->dim, type))
11112 return map;
11113
11114 map = isl_map_cow(map);
11115 if (!map)
11116 return NULL;
11117
11118 for (i = 0; i < map->n; ++i) {
11119 map->p[i] = isl_basic_map_reset(map->p[i], type);
11120 if (!map->p[i])
11121 goto error;
11122 }
11123 map->dim = isl_space_reset(map->dim, type);
11124 if (!map->dim)
11125 goto error;
11126
11127 return map;
11128error:
11129 isl_map_free(map);
11130 return NULL;
11131}
11132
11133__isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
11134{
11135 if (!bmap)
11136 return NULL;
11137
11138 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
11139 return bmap;
11140
11141 bmap = isl_basic_map_cow(bmap);
11142 if (!bmap)
11143 return NULL;
11144
11145 bmap->dim = isl_space_flatten(bmap->dim);
11146 if (!bmap->dim)
11147 goto error;
11148
11149 bmap = isl_basic_map_finalize(bmap);
11150
11151 return bmap;
11152error:
11153 isl_basic_map_free(bmap);
11154 return NULL;
11155}
11156
11157__isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
11158{
11159 return (isl_basic_set *)isl_basic_map_flatten((isl_basic_map *)bset);
11160}
11161
11162__isl_give isl_basic_map *isl_basic_map_flatten_domain(
11163 __isl_take isl_basic_map *bmap)
11164{
11165 if (!bmap)
11166 return NULL;
11167
11168 if (!bmap->dim->nested[0])
11169 return bmap;
11170
11171 bmap = isl_basic_map_cow(bmap);
11172 if (!bmap)
11173 return NULL;
11174
11175 bmap->dim = isl_space_flatten_domain(bmap->dim);
11176 if (!bmap->dim)
11177 goto error;
11178
11179 bmap = isl_basic_map_finalize(bmap);
11180
11181 return bmap;
11182error:
11183 isl_basic_map_free(bmap);
11184 return NULL;
11185}
11186
11187__isl_give isl_basic_map *isl_basic_map_flatten_range(
11188 __isl_take isl_basic_map *bmap)
11189{
11190 if (!bmap)
11191 return NULL;
11192
11193 if (!bmap->dim->nested[1])
11194 return bmap;
11195
11196 bmap = isl_basic_map_cow(bmap);
11197 if (!bmap)
11198 return NULL;
11199
11200 bmap->dim = isl_space_flatten_range(bmap->dim);
11201 if (!bmap->dim)
11202 goto error;
11203
11204 bmap = isl_basic_map_finalize(bmap);
11205
11206 return bmap;
11207error:
11208 isl_basic_map_free(bmap);
11209 return NULL;
11210}
11211
Michael Kruse959a8dc2016-01-15 15:54:45 +000011212/* Remove any internal structure from the spaces of domain and range of "map".
11213 */
Tobias Grosser52a25232015-02-04 20:55:43 +000011214__isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
11215{
Tobias Grosser52a25232015-02-04 20:55:43 +000011216 if (!map)
11217 return NULL;
11218
11219 if (!map->dim->nested[0] && !map->dim->nested[1])
11220 return map;
11221
Michael Kruse959a8dc2016-01-15 15:54:45 +000011222 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten);
Tobias Grosser52a25232015-02-04 20:55:43 +000011223}
11224
11225__isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
11226{
11227 return (isl_set *)isl_map_flatten((isl_map *)set);
11228}
11229
11230__isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
11231{
11232 isl_space *dim, *flat_dim;
11233 isl_map *map;
11234
11235 dim = isl_set_get_space(set);
11236 flat_dim = isl_space_flatten(isl_space_copy(dim));
11237 map = isl_map_identity(isl_space_join(isl_space_reverse(dim), flat_dim));
11238 map = isl_map_intersect_domain(map, set);
11239
11240 return map;
11241}
11242
Michael Kruse959a8dc2016-01-15 15:54:45 +000011243/* Remove any internal structure from the space of the domain of "map".
11244 */
Tobias Grosser52a25232015-02-04 20:55:43 +000011245__isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
11246{
Tobias Grosser52a25232015-02-04 20:55:43 +000011247 if (!map)
11248 return NULL;
11249
11250 if (!map->dim->nested[0])
11251 return map;
11252
Michael Kruse959a8dc2016-01-15 15:54:45 +000011253 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_domain);
Tobias Grosser52a25232015-02-04 20:55:43 +000011254}
11255
Michael Kruse959a8dc2016-01-15 15:54:45 +000011256/* Remove any internal structure from the space of the range of "map".
11257 */
Tobias Grosser52a25232015-02-04 20:55:43 +000011258__isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
11259{
Tobias Grosser52a25232015-02-04 20:55:43 +000011260 if (!map)
11261 return NULL;
11262
11263 if (!map->dim->nested[1])
11264 return map;
11265
Michael Kruse959a8dc2016-01-15 15:54:45 +000011266 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_range);
Tobias Grosser52a25232015-02-04 20:55:43 +000011267}
11268
11269/* Reorder the dimensions of "bmap" according to the given dim_map
Tobias Grossera7f1e042016-02-04 06:19:12 +000011270 * and set the dimension specification to "dim" and
11271 * perform Gaussian elimination on the result.
Tobias Grosser52a25232015-02-04 20:55:43 +000011272 */
11273__isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
11274 __isl_take isl_space *dim, __isl_take struct isl_dim_map *dim_map)
11275{
11276 isl_basic_map *res;
11277 unsigned flags;
11278
11279 bmap = isl_basic_map_cow(bmap);
11280 if (!bmap || !dim || !dim_map)
11281 goto error;
11282
11283 flags = bmap->flags;
11284 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
11285 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED);
11286 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
11287 res = isl_basic_map_alloc_space(dim,
11288 bmap->n_div, bmap->n_eq, bmap->n_ineq);
11289 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
11290 if (res)
11291 res->flags = flags;
Tobias Grossera7f1e042016-02-04 06:19:12 +000011292 res = isl_basic_map_gauss(res, NULL);
Tobias Grosser52a25232015-02-04 20:55:43 +000011293 res = isl_basic_map_finalize(res);
11294 return res;
11295error:
11296 free(dim_map);
11297 isl_basic_map_free(bmap);
11298 isl_space_free(dim);
11299 return NULL;
11300}
11301
11302/* Reorder the dimensions of "map" according to given reordering.
11303 */
11304__isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
11305 __isl_take isl_reordering *r)
11306{
11307 int i;
11308 struct isl_dim_map *dim_map;
11309
11310 map = isl_map_cow(map);
11311 dim_map = isl_dim_map_from_reordering(r);
11312 if (!map || !r || !dim_map)
11313 goto error;
11314
11315 for (i = 0; i < map->n; ++i) {
11316 struct isl_dim_map *dim_map_i;
11317
11318 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
11319
11320 map->p[i] = isl_basic_map_realign(map->p[i],
11321 isl_space_copy(r->dim), dim_map_i);
11322
11323 if (!map->p[i])
11324 goto error;
11325 }
11326
11327 map = isl_map_reset_space(map, isl_space_copy(r->dim));
11328
11329 isl_reordering_free(r);
11330 free(dim_map);
11331 return map;
11332error:
11333 free(dim_map);
11334 isl_map_free(map);
11335 isl_reordering_free(r);
11336 return NULL;
11337}
11338
11339__isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
11340 __isl_take isl_reordering *r)
11341{
11342 return (isl_set *)isl_map_realign((isl_map *)set, r);
11343}
11344
11345__isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
11346 __isl_take isl_space *model)
11347{
11348 isl_ctx *ctx;
11349
11350 if (!map || !model)
11351 goto error;
11352
11353 ctx = isl_space_get_ctx(model);
11354 if (!isl_space_has_named_params(model))
11355 isl_die(ctx, isl_error_invalid,
11356 "model has unnamed parameters", goto error);
11357 if (!isl_space_has_named_params(map->dim))
11358 isl_die(ctx, isl_error_invalid,
11359 "relation has unnamed parameters", goto error);
11360 if (!isl_space_match(map->dim, isl_dim_param, model, isl_dim_param)) {
11361 isl_reordering *exp;
11362
11363 model = isl_space_drop_dims(model, isl_dim_in,
11364 0, isl_space_dim(model, isl_dim_in));
11365 model = isl_space_drop_dims(model, isl_dim_out,
11366 0, isl_space_dim(model, isl_dim_out));
11367 exp = isl_parameter_alignment_reordering(map->dim, model);
11368 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
11369 map = isl_map_realign(map, exp);
11370 }
11371
11372 isl_space_free(model);
11373 return map;
11374error:
11375 isl_space_free(model);
11376 isl_map_free(map);
11377 return NULL;
11378}
11379
11380__isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
11381 __isl_take isl_space *model)
11382{
11383 return isl_map_align_params(set, model);
11384}
11385
11386/* Align the parameters of "bmap" to those of "model", introducing
11387 * additional parameters if needed.
11388 */
11389__isl_give isl_basic_map *isl_basic_map_align_params(
11390 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
11391{
11392 isl_ctx *ctx;
11393
11394 if (!bmap || !model)
11395 goto error;
11396
11397 ctx = isl_space_get_ctx(model);
11398 if (!isl_space_has_named_params(model))
11399 isl_die(ctx, isl_error_invalid,
11400 "model has unnamed parameters", goto error);
11401 if (!isl_space_has_named_params(bmap->dim))
11402 isl_die(ctx, isl_error_invalid,
11403 "relation has unnamed parameters", goto error);
11404 if (!isl_space_match(bmap->dim, isl_dim_param, model, isl_dim_param)) {
11405 isl_reordering *exp;
11406 struct isl_dim_map *dim_map;
11407
11408 model = isl_space_drop_dims(model, isl_dim_in,
11409 0, isl_space_dim(model, isl_dim_in));
11410 model = isl_space_drop_dims(model, isl_dim_out,
11411 0, isl_space_dim(model, isl_dim_out));
11412 exp = isl_parameter_alignment_reordering(bmap->dim, model);
11413 exp = isl_reordering_extend_space(exp,
11414 isl_basic_map_get_space(bmap));
11415 dim_map = isl_dim_map_from_reordering(exp);
11416 bmap = isl_basic_map_realign(bmap,
11417 exp ? isl_space_copy(exp->dim) : NULL,
11418 isl_dim_map_extend(dim_map, bmap));
11419 isl_reordering_free(exp);
11420 free(dim_map);
11421 }
11422
11423 isl_space_free(model);
11424 return bmap;
11425error:
11426 isl_space_free(model);
11427 isl_basic_map_free(bmap);
11428 return NULL;
11429}
11430
11431/* Align the parameters of "bset" to those of "model", introducing
11432 * additional parameters if needed.
11433 */
11434__isl_give isl_basic_set *isl_basic_set_align_params(
11435 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
11436{
11437 return isl_basic_map_align_params(bset, model);
11438}
11439
11440__isl_give isl_mat *isl_basic_map_equalities_matrix(
11441 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11442 enum isl_dim_type c2, enum isl_dim_type c3,
11443 enum isl_dim_type c4, enum isl_dim_type c5)
11444{
11445 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11446 struct isl_mat *mat;
11447 int i, j, k;
11448 int pos;
11449
11450 if (!bmap)
11451 return NULL;
11452 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
11453 isl_basic_map_total_dim(bmap) + 1);
11454 if (!mat)
11455 return NULL;
11456 for (i = 0; i < bmap->n_eq; ++i)
11457 for (j = 0, pos = 0; j < 5; ++j) {
11458 int off = isl_basic_map_offset(bmap, c[j]);
11459 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11460 isl_int_set(mat->row[i][pos],
11461 bmap->eq[i][off + k]);
11462 ++pos;
11463 }
11464 }
11465
11466 return mat;
11467}
11468
11469__isl_give isl_mat *isl_basic_map_inequalities_matrix(
11470 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11471 enum isl_dim_type c2, enum isl_dim_type c3,
11472 enum isl_dim_type c4, enum isl_dim_type c5)
11473{
11474 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11475 struct isl_mat *mat;
11476 int i, j, k;
11477 int pos;
11478
11479 if (!bmap)
11480 return NULL;
11481 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
11482 isl_basic_map_total_dim(bmap) + 1);
11483 if (!mat)
11484 return NULL;
11485 for (i = 0; i < bmap->n_ineq; ++i)
11486 for (j = 0, pos = 0; j < 5; ++j) {
11487 int off = isl_basic_map_offset(bmap, c[j]);
11488 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11489 isl_int_set(mat->row[i][pos],
11490 bmap->ineq[i][off + k]);
11491 ++pos;
11492 }
11493 }
11494
11495 return mat;
11496}
11497
11498__isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
11499 __isl_take isl_space *dim,
11500 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11501 enum isl_dim_type c2, enum isl_dim_type c3,
11502 enum isl_dim_type c4, enum isl_dim_type c5)
11503{
11504 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11505 isl_basic_map *bmap;
11506 unsigned total;
11507 unsigned extra;
11508 int i, j, k, l;
11509 int pos;
11510
11511 if (!dim || !eq || !ineq)
11512 goto error;
11513
11514 if (eq->n_col != ineq->n_col)
11515 isl_die(dim->ctx, isl_error_invalid,
11516 "equalities and inequalities matrices should have "
11517 "same number of columns", goto error);
11518
11519 total = 1 + isl_space_dim(dim, isl_dim_all);
11520
11521 if (eq->n_col < total)
11522 isl_die(dim->ctx, isl_error_invalid,
11523 "number of columns too small", goto error);
11524
11525 extra = eq->n_col - total;
11526
11527 bmap = isl_basic_map_alloc_space(isl_space_copy(dim), extra,
11528 eq->n_row, ineq->n_row);
11529 if (!bmap)
11530 goto error;
11531 for (i = 0; i < extra; ++i) {
11532 k = isl_basic_map_alloc_div(bmap);
11533 if (k < 0)
11534 goto error;
11535 isl_int_set_si(bmap->div[k][0], 0);
11536 }
11537 for (i = 0; i < eq->n_row; ++i) {
11538 l = isl_basic_map_alloc_equality(bmap);
11539 if (l < 0)
11540 goto error;
11541 for (j = 0, pos = 0; j < 5; ++j) {
11542 int off = isl_basic_map_offset(bmap, c[j]);
11543 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11544 isl_int_set(bmap->eq[l][off + k],
11545 eq->row[i][pos]);
11546 ++pos;
11547 }
11548 }
11549 }
11550 for (i = 0; i < ineq->n_row; ++i) {
11551 l = isl_basic_map_alloc_inequality(bmap);
11552 if (l < 0)
11553 goto error;
11554 for (j = 0, pos = 0; j < 5; ++j) {
11555 int off = isl_basic_map_offset(bmap, c[j]);
11556 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11557 isl_int_set(bmap->ineq[l][off + k],
11558 ineq->row[i][pos]);
11559 ++pos;
11560 }
11561 }
11562 }
11563
11564 isl_space_free(dim);
11565 isl_mat_free(eq);
11566 isl_mat_free(ineq);
11567
11568 bmap = isl_basic_map_simplify(bmap);
11569 return isl_basic_map_finalize(bmap);
11570error:
11571 isl_space_free(dim);
11572 isl_mat_free(eq);
11573 isl_mat_free(ineq);
11574 return NULL;
11575}
11576
11577__isl_give isl_mat *isl_basic_set_equalities_matrix(
11578 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11579 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11580{
11581 return isl_basic_map_equalities_matrix((isl_basic_map *)bset,
11582 c1, c2, c3, c4, isl_dim_in);
11583}
11584
11585__isl_give isl_mat *isl_basic_set_inequalities_matrix(
11586 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11587 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11588{
11589 return isl_basic_map_inequalities_matrix((isl_basic_map *)bset,
11590 c1, c2, c3, c4, isl_dim_in);
11591}
11592
11593__isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
11594 __isl_take isl_space *dim,
11595 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11596 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11597{
11598 return (isl_basic_set*)
11599 isl_basic_map_from_constraint_matrices(dim, eq, ineq,
11600 c1, c2, c3, c4, isl_dim_in);
11601}
11602
Tobias Grosserb2f39922015-05-28 13:32:11 +000011603isl_bool isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
Tobias Grosser52a25232015-02-04 20:55:43 +000011604{
11605 if (!bmap)
Tobias Grosserb2f39922015-05-28 13:32:11 +000011606 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000011607
11608 return isl_space_can_zip(bmap->dim);
11609}
11610
Tobias Grosserb2f39922015-05-28 13:32:11 +000011611isl_bool isl_map_can_zip(__isl_keep isl_map *map)
Tobias Grosser52a25232015-02-04 20:55:43 +000011612{
11613 if (!map)
Tobias Grosserb2f39922015-05-28 13:32:11 +000011614 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000011615
11616 return isl_space_can_zip(map->dim);
11617}
11618
11619/* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11620 * (A -> C) -> (B -> D).
11621 */
11622__isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
11623{
11624 unsigned pos;
11625 unsigned n1;
11626 unsigned n2;
11627
11628 if (!bmap)
11629 return NULL;
11630
11631 if (!isl_basic_map_can_zip(bmap))
11632 isl_die(bmap->ctx, isl_error_invalid,
11633 "basic map cannot be zipped", goto error);
11634 pos = isl_basic_map_offset(bmap, isl_dim_in) +
11635 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
11636 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
11637 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
11638 bmap = isl_basic_map_cow(bmap);
11639 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
11640 if (!bmap)
11641 return NULL;
11642 bmap->dim = isl_space_zip(bmap->dim);
11643 if (!bmap->dim)
11644 goto error;
Michael Kruse959a8dc2016-01-15 15:54:45 +000011645 bmap = isl_basic_map_mark_final(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +000011646 return bmap;
11647error:
11648 isl_basic_map_free(bmap);
11649 return NULL;
11650}
11651
11652/* Given a map (A -> B) -> (C -> D), return the corresponding map
11653 * (A -> C) -> (B -> D).
11654 */
11655__isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
11656{
11657 int i;
11658
11659 if (!map)
11660 return NULL;
11661
11662 if (!isl_map_can_zip(map))
11663 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
11664 goto error);
11665
11666 map = isl_map_cow(map);
11667 if (!map)
11668 return NULL;
11669
11670 for (i = 0; i < map->n; ++i) {
11671 map->p[i] = isl_basic_map_zip(map->p[i]);
11672 if (!map->p[i])
11673 goto error;
11674 }
11675
11676 map->dim = isl_space_zip(map->dim);
11677 if (!map->dim)
11678 goto error;
11679
11680 return map;
11681error:
11682 isl_map_free(map);
11683 return NULL;
11684}
11685
11686/* Can we apply isl_basic_map_curry to "bmap"?
11687 * That is, does it have a nested relation in its domain?
11688 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000011689isl_bool isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
Tobias Grosser52a25232015-02-04 20:55:43 +000011690{
11691 if (!bmap)
Tobias Grosserb2f39922015-05-28 13:32:11 +000011692 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000011693
11694 return isl_space_can_curry(bmap->dim);
11695}
11696
11697/* Can we apply isl_map_curry to "map"?
11698 * That is, does it have a nested relation in its domain?
11699 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000011700isl_bool isl_map_can_curry(__isl_keep isl_map *map)
Tobias Grosser52a25232015-02-04 20:55:43 +000011701{
11702 if (!map)
Tobias Grosserb2f39922015-05-28 13:32:11 +000011703 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000011704
11705 return isl_space_can_curry(map->dim);
11706}
11707
11708/* Given a basic map (A -> B) -> C, return the corresponding basic map
11709 * A -> (B -> C).
11710 */
11711__isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
11712{
11713
11714 if (!bmap)
11715 return NULL;
11716
11717 if (!isl_basic_map_can_curry(bmap))
11718 isl_die(bmap->ctx, isl_error_invalid,
11719 "basic map cannot be curried", goto error);
11720 bmap = isl_basic_map_cow(bmap);
11721 if (!bmap)
11722 return NULL;
11723 bmap->dim = isl_space_curry(bmap->dim);
11724 if (!bmap->dim)
11725 goto error;
Michael Kruse959a8dc2016-01-15 15:54:45 +000011726 bmap = isl_basic_map_mark_final(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +000011727 return bmap;
11728error:
11729 isl_basic_map_free(bmap);
11730 return NULL;
11731}
11732
11733/* Given a map (A -> B) -> C, return the corresponding map
11734 * A -> (B -> C).
11735 */
11736__isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
11737{
Michael Kruse959a8dc2016-01-15 15:54:45 +000011738 return isl_map_change_space(map, &isl_map_can_curry,
11739 "map cannot be curried", &isl_space_curry);
11740}
Tobias Grosser52a25232015-02-04 20:55:43 +000011741
Michael Kruse959a8dc2016-01-15 15:54:45 +000011742/* Can isl_map_range_curry be applied to "map"?
11743 * That is, does it have a nested relation in its range,
11744 * the domain of which is itself a nested relation?
11745 */
11746isl_bool isl_map_can_range_curry(__isl_keep isl_map *map)
11747{
Tobias Grosser52a25232015-02-04 20:55:43 +000011748 if (!map)
Michael Kruse959a8dc2016-01-15 15:54:45 +000011749 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000011750
Michael Kruse959a8dc2016-01-15 15:54:45 +000011751 return isl_space_can_range_curry(map->dim);
11752}
Tobias Grosser52a25232015-02-04 20:55:43 +000011753
Michael Kruse959a8dc2016-01-15 15:54:45 +000011754/* Given a map A -> ((B -> C) -> D), return the corresponding map
11755 * A -> (B -> (C -> D)).
11756 */
11757__isl_give isl_map *isl_map_range_curry(__isl_take isl_map *map)
11758{
11759 return isl_map_change_space(map, &isl_map_can_range_curry,
11760 "map range cannot be curried",
11761 &isl_space_range_curry);
Tobias Grosser52a25232015-02-04 20:55:43 +000011762}
11763
11764/* Can we apply isl_basic_map_uncurry to "bmap"?
11765 * That is, does it have a nested relation in its domain?
11766 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000011767isl_bool isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
Tobias Grosser52a25232015-02-04 20:55:43 +000011768{
11769 if (!bmap)
Tobias Grosserb2f39922015-05-28 13:32:11 +000011770 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000011771
11772 return isl_space_can_uncurry(bmap->dim);
11773}
11774
11775/* Can we apply isl_map_uncurry to "map"?
11776 * That is, does it have a nested relation in its domain?
11777 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000011778isl_bool isl_map_can_uncurry(__isl_keep isl_map *map)
Tobias Grosser52a25232015-02-04 20:55:43 +000011779{
11780 if (!map)
Tobias Grosserb2f39922015-05-28 13:32:11 +000011781 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000011782
11783 return isl_space_can_uncurry(map->dim);
11784}
11785
11786/* Given a basic map A -> (B -> C), return the corresponding basic map
11787 * (A -> B) -> C.
11788 */
11789__isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
11790{
11791
11792 if (!bmap)
11793 return NULL;
11794
11795 if (!isl_basic_map_can_uncurry(bmap))
11796 isl_die(bmap->ctx, isl_error_invalid,
11797 "basic map cannot be uncurried",
11798 return isl_basic_map_free(bmap));
11799 bmap = isl_basic_map_cow(bmap);
11800 if (!bmap)
11801 return NULL;
11802 bmap->dim = isl_space_uncurry(bmap->dim);
11803 if (!bmap->dim)
11804 return isl_basic_map_free(bmap);
Michael Kruse959a8dc2016-01-15 15:54:45 +000011805 bmap = isl_basic_map_mark_final(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +000011806 return bmap;
11807}
11808
11809/* Given a map A -> (B -> C), return the corresponding map
11810 * (A -> B) -> C.
11811 */
11812__isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
11813{
Michael Kruse959a8dc2016-01-15 15:54:45 +000011814 return isl_map_change_space(map, &isl_map_can_uncurry,
11815 "map cannot be uncurried", &isl_space_uncurry);
Tobias Grosser52a25232015-02-04 20:55:43 +000011816}
11817
11818/* Construct a basic map mapping the domain of the affine expression
11819 * to a one-dimensional range prescribed by the affine expression.
Tobias Grosser37034db2016-03-25 19:38:18 +000011820 *
11821 * A NaN affine expression cannot be converted to a basic map.
Tobias Grosser52a25232015-02-04 20:55:43 +000011822 */
11823__isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
11824{
11825 int k;
11826 int pos;
Tobias Grosser37034db2016-03-25 19:38:18 +000011827 isl_bool is_nan;
Tobias Grosser52a25232015-02-04 20:55:43 +000011828 isl_local_space *ls;
Tobias Grosser37034db2016-03-25 19:38:18 +000011829 isl_basic_map *bmap = NULL;
Tobias Grosser52a25232015-02-04 20:55:43 +000011830
11831 if (!aff)
11832 return NULL;
Tobias Grosser37034db2016-03-25 19:38:18 +000011833 is_nan = isl_aff_is_nan(aff);
11834 if (is_nan < 0)
11835 goto error;
11836 if (is_nan)
11837 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
11838 "cannot convert NaN", goto error);
Tobias Grosser52a25232015-02-04 20:55:43 +000011839
11840 ls = isl_aff_get_local_space(aff);
11841 bmap = isl_basic_map_from_local_space(ls);
11842 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
11843 k = isl_basic_map_alloc_equality(bmap);
11844 if (k < 0)
11845 goto error;
11846
11847 pos = isl_basic_map_offset(bmap, isl_dim_out);
11848 isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
11849 isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
11850 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
11851 aff->v->size - (pos + 1));
11852
11853 isl_aff_free(aff);
11854 bmap = isl_basic_map_finalize(bmap);
11855 return bmap;
11856error:
11857 isl_aff_free(aff);
11858 isl_basic_map_free(bmap);
11859 return NULL;
11860}
11861
11862/* Construct a map mapping the domain of the affine expression
11863 * to a one-dimensional range prescribed by the affine expression.
11864 */
11865__isl_give isl_map *isl_map_from_aff(__isl_take isl_aff *aff)
11866{
11867 isl_basic_map *bmap;
11868
11869 bmap = isl_basic_map_from_aff(aff);
11870 return isl_map_from_basic_map(bmap);
11871}
11872
11873/* Construct a basic map mapping the domain the multi-affine expression
11874 * to its range, with each dimension in the range equated to the
11875 * corresponding affine expression.
11876 */
11877__isl_give isl_basic_map *isl_basic_map_from_multi_aff(
11878 __isl_take isl_multi_aff *maff)
11879{
11880 int i;
11881 isl_space *space;
11882 isl_basic_map *bmap;
11883
11884 if (!maff)
11885 return NULL;
11886
11887 if (isl_space_dim(maff->space, isl_dim_out) != maff->n)
11888 isl_die(isl_multi_aff_get_ctx(maff), isl_error_internal,
11889 "invalid space", goto error);
11890
11891 space = isl_space_domain(isl_multi_aff_get_space(maff));
11892 bmap = isl_basic_map_universe(isl_space_from_domain(space));
11893
11894 for (i = 0; i < maff->n; ++i) {
11895 isl_aff *aff;
11896 isl_basic_map *bmap_i;
11897
11898 aff = isl_aff_copy(maff->p[i]);
11899 bmap_i = isl_basic_map_from_aff(aff);
11900
11901 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
11902 }
11903
11904 bmap = isl_basic_map_reset_space(bmap, isl_multi_aff_get_space(maff));
11905
11906 isl_multi_aff_free(maff);
11907 return bmap;
11908error:
11909 isl_multi_aff_free(maff);
11910 return NULL;
11911}
11912
11913/* Construct a map mapping the domain the multi-affine expression
11914 * to its range, with each dimension in the range equated to the
11915 * corresponding affine expression.
11916 */
11917__isl_give isl_map *isl_map_from_multi_aff(__isl_take isl_multi_aff *maff)
11918{
11919 isl_basic_map *bmap;
11920
11921 bmap = isl_basic_map_from_multi_aff(maff);
11922 return isl_map_from_basic_map(bmap);
11923}
11924
11925/* Construct a basic map mapping a domain in the given space to
11926 * to an n-dimensional range, with n the number of elements in the list,
11927 * where each coordinate in the range is prescribed by the
11928 * corresponding affine expression.
11929 * The domains of all affine expressions in the list are assumed to match
11930 * domain_dim.
11931 */
11932__isl_give isl_basic_map *isl_basic_map_from_aff_list(
11933 __isl_take isl_space *domain_dim, __isl_take isl_aff_list *list)
11934{
11935 int i;
11936 isl_space *dim;
11937 isl_basic_map *bmap;
11938
11939 if (!list)
11940 return NULL;
11941
11942 dim = isl_space_from_domain(domain_dim);
11943 bmap = isl_basic_map_universe(dim);
11944
11945 for (i = 0; i < list->n; ++i) {
11946 isl_aff *aff;
11947 isl_basic_map *bmap_i;
11948
11949 aff = isl_aff_copy(list->p[i]);
11950 bmap_i = isl_basic_map_from_aff(aff);
11951
11952 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
11953 }
11954
11955 isl_aff_list_free(list);
11956 return bmap;
11957}
11958
11959__isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
11960 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11961{
11962 return isl_map_equate(set, type1, pos1, type2, pos2);
11963}
11964
11965/* Construct a basic map where the given dimensions are equal to each other.
11966 */
11967static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
11968 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11969{
11970 isl_basic_map *bmap = NULL;
11971 int i;
11972
11973 if (!space)
11974 return NULL;
11975
11976 if (pos1 >= isl_space_dim(space, type1))
11977 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11978 "index out of bounds", goto error);
11979 if (pos2 >= isl_space_dim(space, type2))
11980 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11981 "index out of bounds", goto error);
11982
11983 if (type1 == type2 && pos1 == pos2)
11984 return isl_basic_map_universe(space);
11985
11986 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
11987 i = isl_basic_map_alloc_equality(bmap);
11988 if (i < 0)
11989 goto error;
11990 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
11991 pos1 += isl_basic_map_offset(bmap, type1);
11992 pos2 += isl_basic_map_offset(bmap, type2);
11993 isl_int_set_si(bmap->eq[i][pos1], -1);
11994 isl_int_set_si(bmap->eq[i][pos2], 1);
11995 bmap = isl_basic_map_finalize(bmap);
11996 isl_space_free(space);
11997 return bmap;
11998error:
11999 isl_space_free(space);
12000 isl_basic_map_free(bmap);
12001 return NULL;
12002}
12003
12004/* Add a constraint imposing that the given two dimensions are equal.
12005 */
12006__isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
12007 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12008{
12009 isl_basic_map *eq;
12010
12011 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12012
12013 bmap = isl_basic_map_intersect(bmap, eq);
12014
12015 return bmap;
12016}
12017
12018/* Add a constraint imposing that the given two dimensions are equal.
12019 */
12020__isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
12021 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12022{
12023 isl_basic_map *bmap;
12024
12025 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
12026
12027 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12028
12029 return map;
12030}
12031
12032/* Add a constraint imposing that the given two dimensions have opposite values.
12033 */
12034__isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
12035 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12036{
12037 isl_basic_map *bmap = NULL;
12038 int i;
12039
12040 if (!map)
12041 return NULL;
12042
12043 if (pos1 >= isl_map_dim(map, type1))
12044 isl_die(map->ctx, isl_error_invalid,
12045 "index out of bounds", goto error);
12046 if (pos2 >= isl_map_dim(map, type2))
12047 isl_die(map->ctx, isl_error_invalid,
12048 "index out of bounds", goto error);
12049
12050 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
12051 i = isl_basic_map_alloc_equality(bmap);
12052 if (i < 0)
12053 goto error;
12054 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12055 pos1 += isl_basic_map_offset(bmap, type1);
12056 pos2 += isl_basic_map_offset(bmap, type2);
12057 isl_int_set_si(bmap->eq[i][pos1], 1);
12058 isl_int_set_si(bmap->eq[i][pos2], 1);
12059 bmap = isl_basic_map_finalize(bmap);
12060
12061 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12062
12063 return map;
12064error:
12065 isl_basic_map_free(bmap);
12066 isl_map_free(map);
12067 return NULL;
12068}
12069
12070/* Construct a constraint imposing that the value of the first dimension is
12071 * greater than or equal to that of the second.
12072 */
12073static __isl_give isl_constraint *constraint_order_ge(
12074 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
12075 enum isl_dim_type type2, int pos2)
12076{
12077 isl_constraint *c;
12078
12079 if (!space)
12080 return NULL;
12081
Tobias Grosserb2f39922015-05-28 13:32:11 +000012082 c = isl_constraint_alloc_inequality(isl_local_space_from_space(space));
Tobias Grosser52a25232015-02-04 20:55:43 +000012083
12084 if (pos1 >= isl_constraint_dim(c, type1))
12085 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
12086 "index out of bounds", return isl_constraint_free(c));
12087 if (pos2 >= isl_constraint_dim(c, type2))
12088 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
12089 "index out of bounds", return isl_constraint_free(c));
12090
12091 if (type1 == type2 && pos1 == pos2)
12092 return c;
12093
12094 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
12095 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
12096
12097 return c;
12098}
12099
12100/* Add a constraint imposing that the value of the first dimension is
12101 * greater than or equal to that of the second.
12102 */
12103__isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
12104 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12105{
12106 isl_constraint *c;
12107 isl_space *space;
12108
12109 if (type1 == type2 && pos1 == pos2)
12110 return bmap;
12111 space = isl_basic_map_get_space(bmap);
12112 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12113 bmap = isl_basic_map_add_constraint(bmap, c);
12114
12115 return bmap;
12116}
12117
12118/* Add a constraint imposing that the value of the first dimension is
12119 * greater than or equal to that of the second.
12120 */
12121__isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
12122 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12123{
12124 isl_constraint *c;
12125 isl_space *space;
12126
12127 if (type1 == type2 && pos1 == pos2)
12128 return map;
12129 space = isl_map_get_space(map);
12130 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12131 map = isl_map_add_constraint(map, c);
12132
12133 return map;
12134}
12135
12136/* Add a constraint imposing that the value of the first dimension is
12137 * less than or equal to that of the second.
12138 */
12139__isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
12140 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12141{
12142 return isl_map_order_ge(map, type2, pos2, type1, pos1);
12143}
12144
12145/* Construct a basic map where the value of the first dimension is
12146 * greater than that of the second.
12147 */
12148static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
12149 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12150{
12151 isl_basic_map *bmap = NULL;
12152 int i;
12153
12154 if (!space)
12155 return NULL;
12156
12157 if (pos1 >= isl_space_dim(space, type1))
12158 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12159 "index out of bounds", goto error);
12160 if (pos2 >= isl_space_dim(space, type2))
12161 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12162 "index out of bounds", goto error);
12163
12164 if (type1 == type2 && pos1 == pos2)
12165 return isl_basic_map_empty(space);
12166
12167 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
12168 i = isl_basic_map_alloc_inequality(bmap);
12169 if (i < 0)
12170 return isl_basic_map_free(bmap);
12171 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
12172 pos1 += isl_basic_map_offset(bmap, type1);
12173 pos2 += isl_basic_map_offset(bmap, type2);
12174 isl_int_set_si(bmap->ineq[i][pos1], 1);
12175 isl_int_set_si(bmap->ineq[i][pos2], -1);
12176 isl_int_set_si(bmap->ineq[i][0], -1);
12177 bmap = isl_basic_map_finalize(bmap);
12178
12179 return bmap;
12180error:
12181 isl_space_free(space);
12182 isl_basic_map_free(bmap);
12183 return NULL;
12184}
12185
12186/* Add a constraint imposing that the value of the first dimension is
12187 * greater than that of the second.
12188 */
12189__isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
12190 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12191{
12192 isl_basic_map *gt;
12193
12194 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12195
12196 bmap = isl_basic_map_intersect(bmap, gt);
12197
12198 return bmap;
12199}
12200
12201/* Add a constraint imposing that the value of the first dimension is
12202 * greater than that of the second.
12203 */
12204__isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
12205 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12206{
12207 isl_basic_map *bmap;
12208
12209 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
12210
12211 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12212
12213 return map;
12214}
12215
12216/* Add a constraint imposing that the value of the first dimension is
12217 * smaller than that of the second.
12218 */
12219__isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
12220 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12221{
12222 return isl_map_order_gt(map, type2, pos2, type1, pos1);
12223}
12224
12225__isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
12226 int pos)
12227{
12228 isl_aff *div;
12229 isl_local_space *ls;
12230
12231 if (!bmap)
12232 return NULL;
12233
12234 if (!isl_basic_map_divs_known(bmap))
12235 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12236 "some divs are unknown", return NULL);
12237
12238 ls = isl_basic_map_get_local_space(bmap);
12239 div = isl_local_space_get_div(ls, pos);
12240 isl_local_space_free(ls);
12241
12242 return div;
12243}
12244
12245__isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
12246 int pos)
12247{
12248 return isl_basic_map_get_div(bset, pos);
12249}
12250
12251/* Plug in "subs" for dimension "type", "pos" of "bset".
12252 *
12253 * Let i be the dimension to replace and let "subs" be of the form
12254 *
12255 * f/d
12256 *
12257 * Any integer division with a non-zero coefficient for i,
12258 *
12259 * floor((a i + g)/m)
12260 *
12261 * is replaced by
12262 *
12263 * floor((a f + d g)/(m d))
12264 *
12265 * Constraints of the form
12266 *
12267 * a i + g
12268 *
12269 * are replaced by
12270 *
12271 * a f + d g
12272 *
12273 * We currently require that "subs" is an integral expression.
12274 * Handling rational expressions may require us to add stride constraints
12275 * as we do in isl_basic_set_preimage_multi_aff.
12276 */
12277__isl_give isl_basic_set *isl_basic_set_substitute(
12278 __isl_take isl_basic_set *bset,
12279 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12280{
12281 int i;
12282 isl_int v;
12283 isl_ctx *ctx;
12284
12285 if (bset && isl_basic_set_plain_is_empty(bset))
12286 return bset;
12287
12288 bset = isl_basic_set_cow(bset);
12289 if (!bset || !subs)
12290 goto error;
12291
12292 ctx = isl_basic_set_get_ctx(bset);
12293 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
12294 isl_die(ctx, isl_error_invalid,
12295 "spaces don't match", goto error);
12296 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
12297 isl_die(ctx, isl_error_unsupported,
12298 "cannot handle divs yet", goto error);
12299 if (!isl_int_is_one(subs->v->el[0]))
12300 isl_die(ctx, isl_error_invalid,
12301 "can only substitute integer expressions", goto error);
12302
12303 pos += isl_basic_set_offset(bset, type);
12304
12305 isl_int_init(v);
12306
12307 for (i = 0; i < bset->n_eq; ++i) {
12308 if (isl_int_is_zero(bset->eq[i][pos]))
12309 continue;
12310 isl_int_set(v, bset->eq[i][pos]);
12311 isl_int_set_si(bset->eq[i][pos], 0);
12312 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
12313 v, subs->v->el + 1, subs->v->size - 1);
12314 }
12315
12316 for (i = 0; i < bset->n_ineq; ++i) {
12317 if (isl_int_is_zero(bset->ineq[i][pos]))
12318 continue;
12319 isl_int_set(v, bset->ineq[i][pos]);
12320 isl_int_set_si(bset->ineq[i][pos], 0);
12321 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
12322 v, subs->v->el + 1, subs->v->size - 1);
12323 }
12324
12325 for (i = 0; i < bset->n_div; ++i) {
12326 if (isl_int_is_zero(bset->div[i][1 + pos]))
12327 continue;
12328 isl_int_set(v, bset->div[i][1 + pos]);
12329 isl_int_set_si(bset->div[i][1 + pos], 0);
12330 isl_seq_combine(bset->div[i] + 1,
12331 subs->v->el[0], bset->div[i] + 1,
12332 v, subs->v->el + 1, subs->v->size - 1);
12333 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
12334 }
12335
12336 isl_int_clear(v);
12337
12338 bset = isl_basic_set_simplify(bset);
12339 return isl_basic_set_finalize(bset);
12340error:
12341 isl_basic_set_free(bset);
12342 return NULL;
12343}
12344
12345/* Plug in "subs" for dimension "type", "pos" of "set".
12346 */
12347__isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
12348 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12349{
12350 int i;
12351
12352 if (set && isl_set_plain_is_empty(set))
12353 return set;
12354
12355 set = isl_set_cow(set);
12356 if (!set || !subs)
12357 goto error;
12358
12359 for (i = set->n - 1; i >= 0; --i) {
12360 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
12361 if (remove_if_empty(set, i) < 0)
12362 goto error;
12363 }
12364
12365 return set;
12366error:
12367 isl_set_free(set);
12368 return NULL;
12369}
12370
12371/* Check if the range of "ma" is compatible with the domain or range
12372 * (depending on "type") of "bmap".
12373 * Return -1 if anything is wrong.
12374 */
12375static int check_basic_map_compatible_range_multi_aff(
12376 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
12377 __isl_keep isl_multi_aff *ma)
12378{
12379 int m;
12380 isl_space *ma_space;
12381
12382 ma_space = isl_multi_aff_get_space(ma);
12383
12384 m = isl_space_match(bmap->dim, isl_dim_param, ma_space, isl_dim_param);
12385 if (m < 0)
12386 goto error;
12387 if (!m)
12388 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12389 "parameters don't match", goto error);
12390 m = isl_space_tuple_is_equal(bmap->dim, type, ma_space, isl_dim_out);
12391 if (m < 0)
12392 goto error;
12393 if (!m)
12394 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12395 "spaces don't match", goto error);
12396
12397 isl_space_free(ma_space);
12398 return m;
12399error:
12400 isl_space_free(ma_space);
12401 return -1;
12402}
12403
12404/* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12405 * coefficients before the transformed range of dimensions,
12406 * the "n_after" coefficients after the transformed range of dimensions
12407 * and the coefficients of the other divs in "bmap".
12408 */
12409static int set_ma_divs(__isl_keep isl_basic_map *bmap,
12410 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
12411{
12412 int i;
12413 int n_param;
12414 int n_set;
12415 isl_local_space *ls;
12416
12417 if (n_div == 0)
12418 return 0;
12419
12420 ls = isl_aff_get_domain_local_space(ma->p[0]);
12421 if (!ls)
12422 return -1;
12423
12424 n_param = isl_local_space_dim(ls, isl_dim_param);
12425 n_set = isl_local_space_dim(ls, isl_dim_set);
12426 for (i = 0; i < n_div; ++i) {
12427 int o_bmap = 0, o_ls = 0;
12428
12429 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
12430 o_bmap += 1 + 1 + n_param;
12431 o_ls += 1 + 1 + n_param;
12432 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
12433 o_bmap += n_before;
12434 isl_seq_cpy(bmap->div[i] + o_bmap,
12435 ls->div->row[i] + o_ls, n_set);
12436 o_bmap += n_set;
12437 o_ls += n_set;
12438 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
12439 o_bmap += n_after;
12440 isl_seq_cpy(bmap->div[i] + o_bmap,
12441 ls->div->row[i] + o_ls, n_div);
12442 o_bmap += n_div;
12443 o_ls += n_div;
12444 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
12445 if (isl_basic_set_add_div_constraints(bmap, i) < 0)
12446 goto error;
12447 }
12448
12449 isl_local_space_free(ls);
12450 return 0;
12451error:
12452 isl_local_space_free(ls);
12453 return -1;
12454}
12455
12456/* How many stride constraints does "ma" enforce?
12457 * That is, how many of the affine expressions have a denominator
12458 * different from one?
12459 */
12460static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
12461{
12462 int i;
12463 int strides = 0;
12464
12465 for (i = 0; i < ma->n; ++i)
12466 if (!isl_int_is_one(ma->p[i]->v->el[0]))
12467 strides++;
12468
12469 return strides;
12470}
12471
12472/* For each affine expression in ma of the form
12473 *
12474 * x_i = (f_i y + h_i)/m_i
12475 *
12476 * with m_i different from one, add a constraint to "bmap"
12477 * of the form
12478 *
12479 * f_i y + h_i = m_i alpha_i
12480 *
12481 * with alpha_i an additional existentially quantified variable.
Tobias Grossera67ac972016-02-26 11:35:12 +000012482 *
12483 * The input variables of "ma" correspond to a subset of the variables
12484 * of "bmap". There are "n_before" variables in "bmap" before this
12485 * subset and "n_after" variables after this subset.
12486 * The integer divisions of the affine expressions in "ma" are assumed
12487 * to have been aligned. There are "n_div_ma" of them and
12488 * they appear first in "bmap", straight after the "n_after" variables.
Tobias Grosser52a25232015-02-04 20:55:43 +000012489 */
12490static __isl_give isl_basic_map *add_ma_strides(
12491 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
Tobias Grossera67ac972016-02-26 11:35:12 +000012492 int n_before, int n_after, int n_div_ma)
Tobias Grosser52a25232015-02-04 20:55:43 +000012493{
12494 int i, k;
12495 int div;
12496 int total;
12497 int n_param;
12498 int n_in;
Tobias Grosser52a25232015-02-04 20:55:43 +000012499
12500 total = isl_basic_map_total_dim(bmap);
12501 n_param = isl_multi_aff_dim(ma, isl_dim_param);
12502 n_in = isl_multi_aff_dim(ma, isl_dim_in);
Tobias Grosser52a25232015-02-04 20:55:43 +000012503 for (i = 0; i < ma->n; ++i) {
12504 int o_bmap = 0, o_ma = 1;
12505
12506 if (isl_int_is_one(ma->p[i]->v->el[0]))
12507 continue;
12508 div = isl_basic_map_alloc_div(bmap);
12509 k = isl_basic_map_alloc_equality(bmap);
12510 if (div < 0 || k < 0)
12511 goto error;
12512 isl_int_set_si(bmap->div[div][0], 0);
12513 isl_seq_cpy(bmap->eq[k] + o_bmap,
12514 ma->p[i]->v->el + o_ma, 1 + n_param);
12515 o_bmap += 1 + n_param;
12516 o_ma += 1 + n_param;
12517 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
12518 o_bmap += n_before;
12519 isl_seq_cpy(bmap->eq[k] + o_bmap,
12520 ma->p[i]->v->el + o_ma, n_in);
12521 o_bmap += n_in;
12522 o_ma += n_in;
12523 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
12524 o_bmap += n_after;
12525 isl_seq_cpy(bmap->eq[k] + o_bmap,
Tobias Grossera67ac972016-02-26 11:35:12 +000012526 ma->p[i]->v->el + o_ma, n_div_ma);
12527 o_bmap += n_div_ma;
12528 o_ma += n_div_ma;
Tobias Grosser52a25232015-02-04 20:55:43 +000012529 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
12530 isl_int_neg(bmap->eq[k][1 + total], ma->p[i]->v->el[0]);
12531 total++;
12532 }
12533
12534 return bmap;
12535error:
12536 isl_basic_map_free(bmap);
12537 return NULL;
12538}
12539
12540/* Replace the domain or range space (depending on "type) of "space" by "set".
12541 */
12542static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
12543 enum isl_dim_type type, __isl_take isl_space *set)
12544{
12545 if (type == isl_dim_in) {
12546 space = isl_space_range(space);
12547 space = isl_space_map_from_domain_and_range(set, space);
12548 } else {
12549 space = isl_space_domain(space);
12550 space = isl_space_map_from_domain_and_range(space, set);
12551 }
12552
12553 return space;
12554}
12555
12556/* Compute the preimage of the domain or range (depending on "type")
12557 * of "bmap" under the function represented by "ma".
12558 * In other words, plug in "ma" in the domain or range of "bmap".
12559 * The result is a basic map that lives in the same space as "bmap"
12560 * except that the domain or range has been replaced by
12561 * the domain space of "ma".
12562 *
12563 * If bmap is represented by
12564 *
12565 * A(p) + S u + B x + T v + C(divs) >= 0,
12566 *
12567 * where u and x are input and output dimensions if type == isl_dim_out
12568 * while x and v are input and output dimensions if type == isl_dim_in,
12569 * and ma is represented by
12570 *
12571 * x = D(p) + F(y) + G(divs')
12572 *
12573 * then the result is
12574 *
12575 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12576 *
12577 * The divs in the input set are similarly adjusted.
12578 * In particular
12579 *
12580 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12581 *
12582 * becomes
12583 *
12584 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12585 * B_i G(divs') + c_i(divs))/n_i)
12586 *
12587 * If bmap is not a rational map and if F(y) involves any denominators
12588 *
12589 * x_i = (f_i y + h_i)/m_i
12590 *
12591 * then additional constraints are added to ensure that we only
12592 * map back integer points. That is we enforce
12593 *
12594 * f_i y + h_i = m_i alpha_i
12595 *
12596 * with alpha_i an additional existentially quantified variable.
12597 *
12598 * We first copy over the divs from "ma".
12599 * Then we add the modified constraints and divs from "bmap".
12600 * Finally, we add the stride constraints, if needed.
12601 */
12602__isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
12603 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
12604 __isl_take isl_multi_aff *ma)
12605{
12606 int i, k;
12607 isl_space *space;
12608 isl_basic_map *res = NULL;
12609 int n_before, n_after, n_div_bmap, n_div_ma;
12610 isl_int f, c1, c2, g;
12611 int rational, strides;
12612
12613 isl_int_init(f);
12614 isl_int_init(c1);
12615 isl_int_init(c2);
12616 isl_int_init(g);
12617
12618 ma = isl_multi_aff_align_divs(ma);
12619 if (!bmap || !ma)
12620 goto error;
12621 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
12622 goto error;
12623
12624 if (type == isl_dim_in) {
12625 n_before = 0;
12626 n_after = isl_basic_map_dim(bmap, isl_dim_out);
12627 } else {
12628 n_before = isl_basic_map_dim(bmap, isl_dim_in);
12629 n_after = 0;
12630 }
12631 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
12632 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
12633
12634 space = isl_multi_aff_get_domain_space(ma);
12635 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
12636 rational = isl_basic_map_is_rational(bmap);
12637 strides = rational ? 0 : multi_aff_strides(ma);
12638 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
12639 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
12640 if (rational)
12641 res = isl_basic_map_set_rational(res);
12642
12643 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
12644 if (isl_basic_map_alloc_div(res) < 0)
12645 goto error;
12646
12647 if (set_ma_divs(res, ma, n_before, n_after, n_div_ma) < 0)
12648 goto error;
12649
12650 for (i = 0; i < bmap->n_eq; ++i) {
12651 k = isl_basic_map_alloc_equality(res);
12652 if (k < 0)
12653 goto error;
12654 isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
12655 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12656 }
12657
12658 for (i = 0; i < bmap->n_ineq; ++i) {
12659 k = isl_basic_map_alloc_inequality(res);
12660 if (k < 0)
12661 goto error;
12662 isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
12663 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12664 }
12665
12666 for (i = 0; i < bmap->n_div; ++i) {
12667 if (isl_int_is_zero(bmap->div[i][0])) {
12668 isl_int_set_si(res->div[n_div_ma + i][0], 0);
12669 continue;
12670 }
12671 isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
12672 n_before, n_after, n_div_ma, n_div_bmap,
12673 f, c1, c2, g, 1);
12674 }
12675
12676 if (strides)
Tobias Grossera67ac972016-02-26 11:35:12 +000012677 res = add_ma_strides(res, ma, n_before, n_after, n_div_ma);
Tobias Grosser52a25232015-02-04 20:55:43 +000012678
12679 isl_int_clear(f);
12680 isl_int_clear(c1);
12681 isl_int_clear(c2);
12682 isl_int_clear(g);
12683 isl_basic_map_free(bmap);
12684 isl_multi_aff_free(ma);
12685 res = isl_basic_set_simplify(res);
12686 return isl_basic_map_finalize(res);
12687error:
12688 isl_int_clear(f);
12689 isl_int_clear(c1);
12690 isl_int_clear(c2);
12691 isl_int_clear(g);
12692 isl_basic_map_free(bmap);
12693 isl_multi_aff_free(ma);
12694 isl_basic_map_free(res);
12695 return NULL;
12696}
12697
12698/* Compute the preimage of "bset" under the function represented by "ma".
12699 * In other words, plug in "ma" in "bset". The result is a basic set
12700 * that lives in the domain space of "ma".
12701 */
12702__isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
12703 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
12704{
12705 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
12706}
12707
12708/* Compute the preimage of the domain of "bmap" under the function
12709 * represented by "ma".
12710 * In other words, plug in "ma" in the domain of "bmap".
12711 * The result is a basic map that lives in the same space as "bmap"
12712 * except that the domain has been replaced by the domain space of "ma".
12713 */
12714__isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
12715 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12716{
12717 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
12718}
12719
12720/* Compute the preimage of the range of "bmap" under the function
12721 * represented by "ma".
12722 * In other words, plug in "ma" in the range of "bmap".
12723 * The result is a basic map that lives in the same space as "bmap"
12724 * except that the range has been replaced by the domain space of "ma".
12725 */
12726__isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
12727 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12728{
12729 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
12730}
12731
12732/* Check if the range of "ma" is compatible with the domain or range
12733 * (depending on "type") of "map".
12734 * Return -1 if anything is wrong.
12735 */
12736static int check_map_compatible_range_multi_aff(
12737 __isl_keep isl_map *map, enum isl_dim_type type,
12738 __isl_keep isl_multi_aff *ma)
12739{
12740 int m;
12741 isl_space *ma_space;
12742
12743 ma_space = isl_multi_aff_get_space(ma);
12744 m = isl_space_tuple_is_equal(map->dim, type, ma_space, isl_dim_out);
12745 isl_space_free(ma_space);
12746 if (m >= 0 && !m)
12747 isl_die(isl_map_get_ctx(map), isl_error_invalid,
12748 "spaces don't match", return -1);
12749 return m;
12750}
12751
12752/* Compute the preimage of the domain or range (depending on "type")
12753 * of "map" under the function represented by "ma".
12754 * In other words, plug in "ma" in the domain or range of "map".
12755 * The result is a map that lives in the same space as "map"
12756 * except that the domain or range has been replaced by
12757 * the domain space of "ma".
12758 *
12759 * The parameters are assumed to have been aligned.
12760 */
12761static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
12762 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12763{
12764 int i;
12765 isl_space *space;
12766
12767 map = isl_map_cow(map);
12768 ma = isl_multi_aff_align_divs(ma);
12769 if (!map || !ma)
12770 goto error;
12771 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
12772 goto error;
12773
12774 for (i = 0; i < map->n; ++i) {
12775 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
12776 isl_multi_aff_copy(ma));
12777 if (!map->p[i])
12778 goto error;
12779 }
12780
12781 space = isl_multi_aff_get_domain_space(ma);
12782 space = isl_space_set(isl_map_get_space(map), type, space);
12783
12784 isl_space_free(map->dim);
12785 map->dim = space;
12786 if (!map->dim)
12787 goto error;
12788
12789 isl_multi_aff_free(ma);
12790 if (map->n > 1)
12791 ISL_F_CLR(map, ISL_MAP_DISJOINT);
12792 ISL_F_CLR(map, ISL_SET_NORMALIZED);
12793 return map;
12794error:
12795 isl_multi_aff_free(ma);
12796 isl_map_free(map);
12797 return NULL;
12798}
12799
12800/* Compute the preimage of the domain or range (depending on "type")
12801 * of "map" under the function represented by "ma".
12802 * In other words, plug in "ma" in the domain or range of "map".
12803 * The result is a map that lives in the same space as "map"
12804 * except that the domain or range has been replaced by
12805 * the domain space of "ma".
12806 */
12807__isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
12808 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12809{
12810 if (!map || !ma)
12811 goto error;
12812
12813 if (isl_space_match(map->dim, isl_dim_param, ma->space, isl_dim_param))
12814 return map_preimage_multi_aff(map, type, ma);
12815
12816 if (!isl_space_has_named_params(map->dim) ||
12817 !isl_space_has_named_params(ma->space))
12818 isl_die(map->ctx, isl_error_invalid,
12819 "unaligned unnamed parameters", goto error);
12820 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
12821 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
12822
12823 return map_preimage_multi_aff(map, type, ma);
12824error:
12825 isl_multi_aff_free(ma);
12826 return isl_map_free(map);
12827}
12828
12829/* Compute the preimage of "set" under the function represented by "ma".
12830 * In other words, plug in "ma" in "set". The result is a set
12831 * that lives in the domain space of "ma".
12832 */
12833__isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
12834 __isl_take isl_multi_aff *ma)
12835{
12836 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
12837}
12838
12839/* Compute the preimage of the domain of "map" under the function
12840 * represented by "ma".
12841 * In other words, plug in "ma" in the domain of "map".
12842 * The result is a map that lives in the same space as "map"
12843 * except that the domain has been replaced by the domain space of "ma".
12844 */
12845__isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
12846 __isl_take isl_multi_aff *ma)
12847{
12848 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
12849}
12850
12851/* Compute the preimage of the range of "map" under the function
12852 * represented by "ma".
12853 * In other words, plug in "ma" in the range of "map".
12854 * The result is a map that lives in the same space as "map"
12855 * except that the range has been replaced by the domain space of "ma".
12856 */
12857__isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
12858 __isl_take isl_multi_aff *ma)
12859{
12860 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
12861}
12862
12863/* Compute the preimage of "map" under the function represented by "pma".
12864 * In other words, plug in "pma" in the domain or range of "map".
12865 * The result is a map that lives in the same space as "map",
12866 * except that the space of type "type" has been replaced by
12867 * the domain space of "pma".
12868 *
12869 * The parameters of "map" and "pma" are assumed to have been aligned.
12870 */
12871static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
12872 __isl_take isl_map *map, enum isl_dim_type type,
12873 __isl_take isl_pw_multi_aff *pma)
12874{
12875 int i;
12876 isl_map *res;
12877
12878 if (!pma)
12879 goto error;
12880
12881 if (pma->n == 0) {
12882 isl_pw_multi_aff_free(pma);
12883 res = isl_map_empty(isl_map_get_space(map));
12884 isl_map_free(map);
12885 return res;
12886 }
12887
12888 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
12889 isl_multi_aff_copy(pma->p[0].maff));
12890 if (type == isl_dim_in)
12891 res = isl_map_intersect_domain(res,
12892 isl_map_copy(pma->p[0].set));
12893 else
12894 res = isl_map_intersect_range(res,
12895 isl_map_copy(pma->p[0].set));
12896
12897 for (i = 1; i < pma->n; ++i) {
12898 isl_map *res_i;
12899
12900 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
12901 isl_multi_aff_copy(pma->p[i].maff));
12902 if (type == isl_dim_in)
12903 res_i = isl_map_intersect_domain(res_i,
12904 isl_map_copy(pma->p[i].set));
12905 else
12906 res_i = isl_map_intersect_range(res_i,
12907 isl_map_copy(pma->p[i].set));
12908 res = isl_map_union(res, res_i);
12909 }
12910
12911 isl_pw_multi_aff_free(pma);
12912 isl_map_free(map);
12913 return res;
12914error:
12915 isl_pw_multi_aff_free(pma);
12916 isl_map_free(map);
12917 return NULL;
12918}
12919
12920/* Compute the preimage of "map" under the function represented by "pma".
12921 * In other words, plug in "pma" in the domain or range of "map".
12922 * The result is a map that lives in the same space as "map",
12923 * except that the space of type "type" has been replaced by
12924 * the domain space of "pma".
12925 */
12926__isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
12927 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
12928{
12929 if (!map || !pma)
12930 goto error;
12931
12932 if (isl_space_match(map->dim, isl_dim_param, pma->dim, isl_dim_param))
12933 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
12934
12935 if (!isl_space_has_named_params(map->dim) ||
12936 !isl_space_has_named_params(pma->dim))
12937 isl_die(map->ctx, isl_error_invalid,
12938 "unaligned unnamed parameters", goto error);
12939 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
12940 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
12941
12942 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
12943error:
12944 isl_pw_multi_aff_free(pma);
12945 return isl_map_free(map);
12946}
12947
12948/* Compute the preimage of "set" under the function represented by "pma".
12949 * In other words, plug in "pma" in "set". The result is a set
12950 * that lives in the domain space of "pma".
12951 */
12952__isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
12953 __isl_take isl_pw_multi_aff *pma)
12954{
12955 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
12956}
12957
12958/* Compute the preimage of the domain of "map" under the function
12959 * represented by "pma".
12960 * In other words, plug in "pma" in the domain of "map".
12961 * The result is a map that lives in the same space as "map",
12962 * except that domain space has been replaced by the domain space of "pma".
12963 */
12964__isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
12965 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
12966{
12967 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
12968}
12969
12970/* Compute the preimage of the range of "map" under the function
12971 * represented by "pma".
12972 * In other words, plug in "pma" in the range of "map".
12973 * The result is a map that lives in the same space as "map",
12974 * except that range space has been replaced by the domain space of "pma".
12975 */
12976__isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
12977 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
12978{
12979 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
12980}
12981
12982/* Compute the preimage of "map" under the function represented by "mpa".
12983 * In other words, plug in "mpa" in the domain or range of "map".
12984 * The result is a map that lives in the same space as "map",
12985 * except that the space of type "type" has been replaced by
12986 * the domain space of "mpa".
12987 *
12988 * If the map does not involve any constraints that refer to the
12989 * dimensions of the substituted space, then the only possible
12990 * effect of "mpa" on the map is to map the space to a different space.
12991 * We create a separate isl_multi_aff to effectuate this change
12992 * in order to avoid spurious splitting of the map along the pieces
12993 * of "mpa".
12994 */
12995__isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
12996 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
12997{
12998 int n;
12999 isl_pw_multi_aff *pma;
13000
13001 if (!map || !mpa)
13002 goto error;
13003
13004 n = isl_map_dim(map, type);
13005 if (!isl_map_involves_dims(map, type, 0, n)) {
13006 isl_space *space;
13007 isl_multi_aff *ma;
13008
13009 space = isl_multi_pw_aff_get_space(mpa);
13010 isl_multi_pw_aff_free(mpa);
13011 ma = isl_multi_aff_zero(space);
13012 return isl_map_preimage_multi_aff(map, type, ma);
13013 }
13014
13015 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
13016 return isl_map_preimage_pw_multi_aff(map, type, pma);
13017error:
13018 isl_map_free(map);
13019 isl_multi_pw_aff_free(mpa);
13020 return NULL;
13021}
13022
13023/* Compute the preimage of "map" under the function represented by "mpa".
13024 * In other words, plug in "mpa" in the domain "map".
13025 * The result is a map that lives in the same space as "map",
13026 * except that domain space has been replaced by the domain space of "mpa".
13027 */
13028__isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
13029 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
13030{
13031 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
13032}
13033
13034/* Compute the preimage of "set" by the function represented by "mpa".
13035 * In other words, plug in "mpa" in "set".
13036 */
13037__isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
13038 __isl_take isl_multi_pw_aff *mpa)
13039{
13040 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);
13041}