blob: 4898e6dc55b04121ede8da5e688de32f08285cff [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
6 *
7 * Use of this software is governed by the MIT license
8 *
9 * Written by Sven Verdoolaege, K.U.Leuven, Departement
10 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
11 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
12 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
13 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
14 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
15 * B.P. 105 - 78153 Le Chesnay, France
16 */
17
18#include <string.h>
19#include <isl_ctx_private.h>
20#include <isl_map_private.h>
21#include <isl_blk.h>
Tobias Grosser3e6070e2015-05-09 09:37:30 +000022#include <isl/constraint.h>
Tobias Grosser52a25232015-02-04 20:55:43 +000023#include "isl_space_private.h"
24#include "isl_equalities.h"
25#include <isl_lp_private.h>
26#include <isl_seq.h>
27#include <isl/set.h>
28#include <isl/map.h>
29#include <isl_reordering.h>
30#include "isl_sample.h"
31#include <isl_sort.h>
32#include "isl_tab.h"
33#include <isl/vec.h>
34#include <isl_mat_private.h>
35#include <isl_vec_private.h>
36#include <isl_dim_map.h>
37#include <isl_local_space_private.h>
38#include <isl_aff_private.h>
39#include <isl_options_private.h>
40#include <isl_morph.h>
41#include <isl_val_private.h>
42#include <isl/deprecated/map_int.h>
43#include <isl/deprecated/set_int.h>
44
45static unsigned n(__isl_keep isl_space *dim, enum isl_dim_type type)
46{
47 switch (type) {
48 case isl_dim_param: return dim->nparam;
49 case isl_dim_in: return dim->n_in;
50 case isl_dim_out: return dim->n_out;
51 case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
52 default: return 0;
53 }
54}
55
56static unsigned pos(__isl_keep isl_space *dim, enum isl_dim_type type)
57{
58 switch (type) {
59 case isl_dim_param: return 1;
60 case isl_dim_in: return 1 + dim->nparam;
61 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
62 default: return 0;
63 }
64}
65
66unsigned isl_basic_map_dim(__isl_keep isl_basic_map *bmap,
67 enum isl_dim_type type)
68{
69 if (!bmap)
70 return 0;
71 switch (type) {
72 case isl_dim_cst: return 1;
73 case isl_dim_param:
74 case isl_dim_in:
75 case isl_dim_out: return isl_space_dim(bmap->dim, type);
76 case isl_dim_div: return bmap->n_div;
77 case isl_dim_all: return isl_basic_map_total_dim(bmap);
78 default: return 0;
79 }
80}
81
82unsigned isl_map_dim(__isl_keep isl_map *map, enum isl_dim_type type)
83{
84 return map ? n(map->dim, type) : 0;
85}
86
87unsigned isl_set_dim(__isl_keep isl_set *set, enum isl_dim_type type)
88{
89 return set ? n(set->dim, type) : 0;
90}
91
92unsigned isl_basic_map_offset(struct isl_basic_map *bmap,
93 enum isl_dim_type type)
94{
95 isl_space *dim = bmap->dim;
96 switch (type) {
97 case isl_dim_cst: return 0;
98 case isl_dim_param: return 1;
99 case isl_dim_in: return 1 + dim->nparam;
100 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
101 case isl_dim_div: return 1 + dim->nparam + dim->n_in + dim->n_out;
102 default: return 0;
103 }
104}
105
106unsigned isl_basic_set_offset(struct isl_basic_set *bset,
107 enum isl_dim_type type)
108{
109 return isl_basic_map_offset(bset, type);
110}
111
112static unsigned map_offset(struct isl_map *map, enum isl_dim_type type)
113{
114 return pos(map->dim, type);
115}
116
117unsigned isl_basic_set_dim(__isl_keep isl_basic_set *bset,
118 enum isl_dim_type type)
119{
120 return isl_basic_map_dim(bset, type);
121}
122
123unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set *bset)
124{
125 return isl_basic_set_dim(bset, isl_dim_set);
126}
127
128unsigned isl_basic_set_n_param(__isl_keep isl_basic_set *bset)
129{
130 return isl_basic_set_dim(bset, isl_dim_param);
131}
132
133unsigned isl_basic_set_total_dim(const struct isl_basic_set *bset)
134{
135 if (!bset)
136 return 0;
137 return isl_space_dim(bset->dim, isl_dim_all) + bset->n_div;
138}
139
140unsigned isl_set_n_dim(__isl_keep isl_set *set)
141{
142 return isl_set_dim(set, isl_dim_set);
143}
144
145unsigned isl_set_n_param(__isl_keep isl_set *set)
146{
147 return isl_set_dim(set, isl_dim_param);
148}
149
150unsigned isl_basic_map_n_in(const struct isl_basic_map *bmap)
151{
152 return bmap ? bmap->dim->n_in : 0;
153}
154
155unsigned isl_basic_map_n_out(const struct isl_basic_map *bmap)
156{
157 return bmap ? bmap->dim->n_out : 0;
158}
159
160unsigned isl_basic_map_n_param(const struct isl_basic_map *bmap)
161{
162 return bmap ? bmap->dim->nparam : 0;
163}
164
165unsigned isl_basic_map_n_div(const struct isl_basic_map *bmap)
166{
167 return bmap ? bmap->n_div : 0;
168}
169
170unsigned isl_basic_map_total_dim(const struct isl_basic_map *bmap)
171{
172 return bmap ? isl_space_dim(bmap->dim, isl_dim_all) + bmap->n_div : 0;
173}
174
175unsigned isl_map_n_in(const struct isl_map *map)
176{
177 return map ? map->dim->n_in : 0;
178}
179
180unsigned isl_map_n_out(const struct isl_map *map)
181{
182 return map ? map->dim->n_out : 0;
183}
184
185unsigned isl_map_n_param(const struct isl_map *map)
186{
187 return map ? map->dim->nparam : 0;
188}
189
190int isl_map_compatible_domain(struct isl_map *map, struct isl_set *set)
191{
192 int m;
193 if (!map || !set)
194 return -1;
195 m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
196 if (m < 0 || !m)
197 return m;
198 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
199 set->dim, isl_dim_set);
200}
201
202int isl_basic_map_compatible_domain(struct isl_basic_map *bmap,
203 struct isl_basic_set *bset)
204{
205 int m;
206 if (!bmap || !bset)
207 return -1;
208 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
209 if (m < 0 || !m)
210 return m;
211 return isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
212 bset->dim, isl_dim_set);
213}
214
215int isl_map_compatible_range(__isl_keep isl_map *map, __isl_keep isl_set *set)
216{
217 int m;
218 if (!map || !set)
219 return -1;
220 m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
221 if (m < 0 || !m)
222 return m;
223 return isl_space_tuple_is_equal(map->dim, isl_dim_out,
224 set->dim, isl_dim_set);
225}
226
227int isl_basic_map_compatible_range(struct isl_basic_map *bmap,
228 struct isl_basic_set *bset)
229{
230 int m;
231 if (!bmap || !bset)
232 return -1;
233 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
234 if (m < 0 || !m)
235 return m;
236 return isl_space_tuple_is_equal(bmap->dim, isl_dim_out,
237 bset->dim, isl_dim_set);
238}
239
240isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
241{
242 return bmap ? bmap->ctx : NULL;
243}
244
245isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
246{
247 return bset ? bset->ctx : NULL;
248}
249
250isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
251{
252 return map ? map->ctx : NULL;
253}
254
255isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
256{
257 return set ? set->ctx : NULL;
258}
259
260__isl_give isl_space *isl_basic_map_get_space(__isl_keep isl_basic_map *bmap)
261{
262 if (!bmap)
263 return NULL;
264 return isl_space_copy(bmap->dim);
265}
266
267__isl_give isl_space *isl_basic_set_get_space(__isl_keep isl_basic_set *bset)
268{
269 if (!bset)
270 return NULL;
271 return isl_space_copy(bset->dim);
272}
273
274/* Extract the divs in "bmap" as a matrix.
275 */
276__isl_give isl_mat *isl_basic_map_get_divs(__isl_keep isl_basic_map *bmap)
277{
278 int i;
279 isl_ctx *ctx;
280 isl_mat *div;
281 unsigned total;
282 unsigned cols;
283
284 if (!bmap)
285 return NULL;
286
287 ctx = isl_basic_map_get_ctx(bmap);
288 total = isl_space_dim(bmap->dim, isl_dim_all);
289 cols = 1 + 1 + total + bmap->n_div;
290 div = isl_mat_alloc(ctx, bmap->n_div, cols);
291 if (!div)
292 return NULL;
293
294 for (i = 0; i < bmap->n_div; ++i)
295 isl_seq_cpy(div->row[i], bmap->div[i], cols);
296
297 return div;
298}
299
300/* Extract the divs in "bset" as a matrix.
301 */
302__isl_give isl_mat *isl_basic_set_get_divs(__isl_keep isl_basic_set *bset)
303{
304 return isl_basic_map_get_divs(bset);
305}
306
307__isl_give isl_local_space *isl_basic_map_get_local_space(
308 __isl_keep isl_basic_map *bmap)
309{
310 isl_mat *div;
311
312 if (!bmap)
313 return NULL;
314
315 div = isl_basic_map_get_divs(bmap);
316 return isl_local_space_alloc_div(isl_space_copy(bmap->dim), div);
317}
318
319__isl_give isl_local_space *isl_basic_set_get_local_space(
320 __isl_keep isl_basic_set *bset)
321{
322 return isl_basic_map_get_local_space(bset);
323}
324
325__isl_give isl_basic_map *isl_basic_map_from_local_space(
326 __isl_take isl_local_space *ls)
327{
328 int i;
329 int n_div;
330 isl_basic_map *bmap;
331
332 if (!ls)
333 return NULL;
334
335 n_div = isl_local_space_dim(ls, isl_dim_div);
336 bmap = isl_basic_map_alloc_space(isl_local_space_get_space(ls),
337 n_div, 0, 2 * n_div);
338
339 for (i = 0; i < n_div; ++i)
340 if (isl_basic_map_alloc_div(bmap) < 0)
341 goto error;
342
343 for (i = 0; i < n_div; ++i) {
344 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
345 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
346 goto error;
347 }
348
349 isl_local_space_free(ls);
350 return bmap;
351error:
352 isl_local_space_free(ls);
353 isl_basic_map_free(bmap);
354 return NULL;
355}
356
357__isl_give isl_basic_set *isl_basic_set_from_local_space(
358 __isl_take isl_local_space *ls)
359{
360 return isl_basic_map_from_local_space(ls);
361}
362
363__isl_give isl_space *isl_map_get_space(__isl_keep isl_map *map)
364{
365 if (!map)
366 return NULL;
367 return isl_space_copy(map->dim);
368}
369
370__isl_give isl_space *isl_set_get_space(__isl_keep isl_set *set)
371{
372 if (!set)
373 return NULL;
374 return isl_space_copy(set->dim);
375}
376
377__isl_give isl_basic_map *isl_basic_map_set_tuple_name(
378 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
379{
380 bmap = isl_basic_map_cow(bmap);
381 if (!bmap)
382 return NULL;
383 bmap->dim = isl_space_set_tuple_name(bmap->dim, type, s);
384 if (!bmap->dim)
385 goto error;
386 bmap = isl_basic_map_finalize(bmap);
387 return bmap;
388error:
389 isl_basic_map_free(bmap);
390 return NULL;
391}
392
393__isl_give isl_basic_set *isl_basic_set_set_tuple_name(
394 __isl_take isl_basic_set *bset, const char *s)
395{
396 return isl_basic_map_set_tuple_name(bset, isl_dim_set, s);
397}
398
399const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
400 enum isl_dim_type type)
401{
402 return bmap ? isl_space_get_tuple_name(bmap->dim, type) : NULL;
403}
404
405__isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
406 enum isl_dim_type type, const char *s)
407{
408 int i;
409
410 map = isl_map_cow(map);
411 if (!map)
412 return NULL;
413
414 map->dim = isl_space_set_tuple_name(map->dim, type, s);
415 if (!map->dim)
416 goto error;
417
418 for (i = 0; i < map->n; ++i) {
419 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
420 if (!map->p[i])
421 goto error;
422 }
423
424 return map;
425error:
426 isl_map_free(map);
427 return NULL;
428}
429
430/* Replace the identifier of the tuple of type "type" by "id".
431 */
432__isl_give isl_basic_map *isl_basic_map_set_tuple_id(
433 __isl_take isl_basic_map *bmap,
434 enum isl_dim_type type, __isl_take isl_id *id)
435{
436 bmap = isl_basic_map_cow(bmap);
437 if (!bmap)
438 goto error;
439 bmap->dim = isl_space_set_tuple_id(bmap->dim, type, id);
440 if (!bmap->dim)
441 return isl_basic_map_free(bmap);
442 bmap = isl_basic_map_finalize(bmap);
443 return bmap;
444error:
445 isl_id_free(id);
446 return NULL;
447}
448
449/* Replace the identifier of the tuple by "id".
450 */
451__isl_give isl_basic_set *isl_basic_set_set_tuple_id(
452 __isl_take isl_basic_set *bset, __isl_take isl_id *id)
453{
454 return isl_basic_map_set_tuple_id(bset, isl_dim_set, id);
455}
456
457/* Does the input or output tuple have a name?
458 */
Tobias Grosserb2f39922015-05-28 13:32:11 +0000459isl_bool isl_map_has_tuple_name(__isl_keep isl_map *map, enum isl_dim_type type)
Tobias Grosser52a25232015-02-04 20:55:43 +0000460{
Tobias Grosserb2f39922015-05-28 13:32:11 +0000461 return map ? isl_space_has_tuple_name(map->dim, type) : isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +0000462}
463
464const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
465 enum isl_dim_type type)
466{
467 return map ? isl_space_get_tuple_name(map->dim, type) : NULL;
468}
469
470__isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
471 const char *s)
472{
473 return (isl_set *)isl_map_set_tuple_name((isl_map *)set, isl_dim_set, s);
474}
475
476__isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map,
477 enum isl_dim_type type, __isl_take isl_id *id)
478{
479 map = isl_map_cow(map);
480 if (!map)
481 goto error;
482
483 map->dim = isl_space_set_tuple_id(map->dim, type, id);
484
485 return isl_map_reset_space(map, isl_space_copy(map->dim));
486error:
487 isl_id_free(id);
488 return NULL;
489}
490
491__isl_give isl_set *isl_set_set_tuple_id(__isl_take isl_set *set,
492 __isl_take isl_id *id)
493{
494 return isl_map_set_tuple_id(set, isl_dim_set, id);
495}
496
497__isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map,
498 enum isl_dim_type type)
499{
500 map = isl_map_cow(map);
501 if (!map)
502 return NULL;
503
504 map->dim = isl_space_reset_tuple_id(map->dim, type);
505
506 return isl_map_reset_space(map, isl_space_copy(map->dim));
507}
508
509__isl_give isl_set *isl_set_reset_tuple_id(__isl_take isl_set *set)
510{
511 return isl_map_reset_tuple_id(set, isl_dim_set);
512}
513
Tobias Grosserb2f39922015-05-28 13:32:11 +0000514isl_bool isl_map_has_tuple_id(__isl_keep isl_map *map, enum isl_dim_type type)
Tobias Grosser52a25232015-02-04 20:55:43 +0000515{
Tobias Grosserb2f39922015-05-28 13:32:11 +0000516 return map ? isl_space_has_tuple_id(map->dim, type) : isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +0000517}
518
519__isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map,
520 enum isl_dim_type type)
521{
522 return map ? isl_space_get_tuple_id(map->dim, type) : NULL;
523}
524
Tobias Grosserb2f39922015-05-28 13:32:11 +0000525isl_bool isl_set_has_tuple_id(__isl_keep isl_set *set)
Tobias Grosser52a25232015-02-04 20:55:43 +0000526{
527 return isl_map_has_tuple_id(set, isl_dim_set);
528}
529
530__isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_set *set)
531{
532 return isl_map_get_tuple_id(set, isl_dim_set);
533}
534
535/* Does the set tuple have a name?
536 */
Tobias Grosserb2f39922015-05-28 13:32:11 +0000537isl_bool isl_set_has_tuple_name(__isl_keep isl_set *set)
Tobias Grosser52a25232015-02-04 20:55:43 +0000538{
Tobias Grosserb2f39922015-05-28 13:32:11 +0000539 if (!set)
540 return isl_bool_error;
541 return isl_space_has_tuple_name(set->dim, isl_dim_set);
Tobias Grosser52a25232015-02-04 20:55:43 +0000542}
543
544
545const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
546{
547 return bset ? isl_space_get_tuple_name(bset->dim, isl_dim_set) : NULL;
548}
549
550const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
551{
552 return set ? isl_space_get_tuple_name(set->dim, isl_dim_set) : NULL;
553}
554
555const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
556 enum isl_dim_type type, unsigned pos)
557{
558 return bmap ? isl_space_get_dim_name(bmap->dim, type, pos) : NULL;
559}
560
561const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
562 enum isl_dim_type type, unsigned pos)
563{
564 return bset ? isl_space_get_dim_name(bset->dim, type, pos) : NULL;
565}
566
567/* Does the given dimension have a name?
568 */
Tobias Grosserb2f39922015-05-28 13:32:11 +0000569isl_bool isl_map_has_dim_name(__isl_keep isl_map *map,
Tobias Grosser52a25232015-02-04 20:55:43 +0000570 enum isl_dim_type type, unsigned pos)
571{
Tobias Grosserb2f39922015-05-28 13:32:11 +0000572 if (!map)
573 return isl_bool_error;
574 return isl_space_has_dim_name(map->dim, type, pos);
Tobias Grosser52a25232015-02-04 20:55:43 +0000575}
576
577const char *isl_map_get_dim_name(__isl_keep isl_map *map,
578 enum isl_dim_type type, unsigned pos)
579{
580 return map ? isl_space_get_dim_name(map->dim, type, pos) : NULL;
581}
582
583const char *isl_set_get_dim_name(__isl_keep isl_set *set,
584 enum isl_dim_type type, unsigned pos)
585{
586 return set ? isl_space_get_dim_name(set->dim, type, pos) : NULL;
587}
588
589/* Does the given dimension have a name?
590 */
Tobias Grosserb2f39922015-05-28 13:32:11 +0000591isl_bool isl_set_has_dim_name(__isl_keep isl_set *set,
Tobias Grosser52a25232015-02-04 20:55:43 +0000592 enum isl_dim_type type, unsigned pos)
593{
Tobias Grosserb2f39922015-05-28 13:32:11 +0000594 if (!set)
595 return isl_bool_error;
596 return isl_space_has_dim_name(set->dim, type, pos);
Tobias Grosser52a25232015-02-04 20:55:43 +0000597}
598
599__isl_give isl_basic_map *isl_basic_map_set_dim_name(
600 __isl_take isl_basic_map *bmap,
601 enum isl_dim_type type, unsigned pos, const char *s)
602{
603 bmap = isl_basic_map_cow(bmap);
604 if (!bmap)
605 return NULL;
606 bmap->dim = isl_space_set_dim_name(bmap->dim, type, pos, s);
607 if (!bmap->dim)
608 goto error;
609 return isl_basic_map_finalize(bmap);
610error:
611 isl_basic_map_free(bmap);
612 return NULL;
613}
614
615__isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
616 enum isl_dim_type type, unsigned pos, const char *s)
617{
618 int i;
619
620 map = isl_map_cow(map);
621 if (!map)
622 return NULL;
623
624 map->dim = isl_space_set_dim_name(map->dim, type, pos, s);
625 if (!map->dim)
626 goto error;
627
628 for (i = 0; i < map->n; ++i) {
629 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
630 if (!map->p[i])
631 goto error;
632 }
633
634 return map;
635error:
636 isl_map_free(map);
637 return NULL;
638}
639
640__isl_give isl_basic_set *isl_basic_set_set_dim_name(
641 __isl_take isl_basic_set *bset,
642 enum isl_dim_type type, unsigned pos, const char *s)
643{
644 return (isl_basic_set *)isl_basic_map_set_dim_name(
645 (isl_basic_map *)bset, type, pos, s);
646}
647
648__isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
649 enum isl_dim_type type, unsigned pos, const char *s)
650{
651 return (isl_set *)isl_map_set_dim_name((isl_map *)set, type, pos, s);
652}
653
Tobias Grosserb2f39922015-05-28 13:32:11 +0000654isl_bool isl_basic_map_has_dim_id(__isl_keep isl_basic_map *bmap,
Tobias Grosser52a25232015-02-04 20:55:43 +0000655 enum isl_dim_type type, unsigned pos)
656{
Tobias Grosserb2f39922015-05-28 13:32:11 +0000657 if (!bmap)
658 return isl_bool_error;
659 return isl_space_has_dim_id(bmap->dim, type, pos);
Tobias Grosser52a25232015-02-04 20:55:43 +0000660}
661
662__isl_give isl_id *isl_basic_set_get_dim_id(__isl_keep isl_basic_set *bset,
663 enum isl_dim_type type, unsigned pos)
664{
665 return bset ? isl_space_get_dim_id(bset->dim, type, pos) : NULL;
666}
667
Tobias Grosserb2f39922015-05-28 13:32:11 +0000668isl_bool isl_map_has_dim_id(__isl_keep isl_map *map,
Tobias Grosser52a25232015-02-04 20:55:43 +0000669 enum isl_dim_type type, unsigned pos)
670{
Tobias Grosserb2f39922015-05-28 13:32:11 +0000671 return map ? isl_space_has_dim_id(map->dim, type, pos) : isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +0000672}
673
674__isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
675 enum isl_dim_type type, unsigned pos)
676{
677 return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL;
678}
679
Tobias Grosserb2f39922015-05-28 13:32:11 +0000680isl_bool isl_set_has_dim_id(__isl_keep isl_set *set,
Tobias Grosser52a25232015-02-04 20:55:43 +0000681 enum isl_dim_type type, unsigned pos)
682{
683 return isl_map_has_dim_id(set, type, pos);
684}
685
686__isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set,
687 enum isl_dim_type type, unsigned pos)
688{
689 return isl_map_get_dim_id(set, type, pos);
690}
691
692__isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
693 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
694{
695 map = isl_map_cow(map);
696 if (!map)
697 goto error;
698
699 map->dim = isl_space_set_dim_id(map->dim, type, pos, id);
700
701 return isl_map_reset_space(map, isl_space_copy(map->dim));
702error:
703 isl_id_free(id);
704 return NULL;
705}
706
707__isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set,
708 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
709{
710 return isl_map_set_dim_id(set, type, pos, id);
711}
712
713int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
714 __isl_keep isl_id *id)
715{
716 if (!map)
717 return -1;
718 return isl_space_find_dim_by_id(map->dim, type, id);
719}
720
721int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type,
722 __isl_keep isl_id *id)
723{
724 return isl_map_find_dim_by_id(set, type, id);
725}
726
727/* Return the position of the dimension of the given type and name
728 * in "bmap".
729 * Return -1 if no such dimension can be found.
730 */
731int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map *bmap,
732 enum isl_dim_type type, const char *name)
733{
734 if (!bmap)
735 return -1;
736 return isl_space_find_dim_by_name(bmap->dim, type, name);
737}
738
739int isl_map_find_dim_by_name(__isl_keep isl_map *map, enum isl_dim_type type,
740 const char *name)
741{
742 if (!map)
743 return -1;
744 return isl_space_find_dim_by_name(map->dim, type, name);
745}
746
747int isl_set_find_dim_by_name(__isl_keep isl_set *set, enum isl_dim_type type,
748 const char *name)
749{
750 return isl_map_find_dim_by_name(set, type, name);
751}
752
753/* Reset the user pointer on all identifiers of parameters and tuples
754 * of the space of "map".
755 */
756__isl_give isl_map *isl_map_reset_user(__isl_take isl_map *map)
757{
758 isl_space *space;
759
760 space = isl_map_get_space(map);
761 space = isl_space_reset_user(space);
762 map = isl_map_reset_space(map, space);
763
764 return map;
765}
766
767/* Reset the user pointer on all identifiers of parameters and tuples
768 * of the space of "set".
769 */
770__isl_give isl_set *isl_set_reset_user(__isl_take isl_set *set)
771{
772 return isl_map_reset_user(set);
773}
774
775int isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
776{
777 if (!bmap)
778 return -1;
779 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
780}
781
782int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
783{
784 return isl_basic_map_is_rational(bset);
785}
786
787/* Does "bmap" contain any rational points?
788 *
789 * If "bmap" has an equality for each dimension, equating the dimension
790 * to an integer constant, then it has no rational points, even if it
791 * is marked as rational.
792 */
793int isl_basic_map_has_rational(__isl_keep isl_basic_map *bmap)
794{
795 int has_rational = 1;
796 unsigned total;
797
798 if (!bmap)
799 return -1;
800 if (isl_basic_map_plain_is_empty(bmap))
801 return 0;
802 if (!isl_basic_map_is_rational(bmap))
803 return 0;
804 bmap = isl_basic_map_copy(bmap);
805 bmap = isl_basic_map_implicit_equalities(bmap);
806 if (!bmap)
807 return -1;
808 total = isl_basic_map_total_dim(bmap);
809 if (bmap->n_eq == total) {
810 int i, j;
811 for (i = 0; i < bmap->n_eq; ++i) {
812 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
813 if (j < 0)
814 break;
815 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
816 !isl_int_is_negone(bmap->eq[i][1 + j]))
817 break;
818 j = isl_seq_first_non_zero(bmap->eq[i] + 1 + j + 1,
819 total - j - 1);
820 if (j >= 0)
821 break;
822 }
823 if (i == bmap->n_eq)
824 has_rational = 0;
825 }
826 isl_basic_map_free(bmap);
827
828 return has_rational;
829}
830
831/* Does "map" contain any rational points?
832 */
833int isl_map_has_rational(__isl_keep isl_map *map)
834{
835 int i;
836 int has_rational;
837
838 if (!map)
839 return -1;
840 for (i = 0; i < map->n; ++i) {
841 has_rational = isl_basic_map_has_rational(map->p[i]);
842 if (has_rational < 0)
843 return -1;
844 if (has_rational)
845 return 1;
846 }
847 return 0;
848}
849
850/* Does "set" contain any rational points?
851 */
852int isl_set_has_rational(__isl_keep isl_set *set)
853{
854 return isl_map_has_rational(set);
855}
856
857/* Is this basic set a parameter domain?
858 */
859int isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
860{
861 if (!bset)
862 return -1;
863 return isl_space_is_params(bset->dim);
864}
865
866/* Is this set a parameter domain?
867 */
Tobias Grosserb2f39922015-05-28 13:32:11 +0000868isl_bool isl_set_is_params(__isl_keep isl_set *set)
Tobias Grosser52a25232015-02-04 20:55:43 +0000869{
870 if (!set)
Tobias Grosserb2f39922015-05-28 13:32:11 +0000871 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +0000872 return isl_space_is_params(set->dim);
873}
874
875/* Is this map actually a parameter domain?
876 * Users should never call this function. Outside of isl,
877 * a map can never be a parameter domain.
878 */
879int isl_map_is_params(__isl_keep isl_map *map)
880{
881 if (!map)
882 return -1;
883 return isl_space_is_params(map->dim);
884}
885
886static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
887 struct isl_basic_map *bmap, unsigned extra,
888 unsigned n_eq, unsigned n_ineq)
889{
890 int i;
891 size_t row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + extra;
892
893 bmap->ctx = ctx;
894 isl_ctx_ref(ctx);
895
896 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
897 if (isl_blk_is_error(bmap->block))
898 goto error;
899
900 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
901 if ((n_ineq + n_eq) && !bmap->ineq)
902 goto error;
903
904 if (extra == 0) {
905 bmap->block2 = isl_blk_empty();
906 bmap->div = NULL;
907 } else {
908 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
909 if (isl_blk_is_error(bmap->block2))
910 goto error;
911
912 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
913 if (!bmap->div)
914 goto error;
915 }
916
917 for (i = 0; i < n_ineq + n_eq; ++i)
918 bmap->ineq[i] = bmap->block.data + i * row_size;
919
920 for (i = 0; i < extra; ++i)
921 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
922
923 bmap->ref = 1;
924 bmap->flags = 0;
925 bmap->c_size = n_eq + n_ineq;
926 bmap->eq = bmap->ineq + n_ineq;
927 bmap->extra = extra;
928 bmap->n_eq = 0;
929 bmap->n_ineq = 0;
930 bmap->n_div = 0;
931 bmap->sample = NULL;
932
933 return bmap;
934error:
935 isl_basic_map_free(bmap);
936 return NULL;
937}
938
939struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
940 unsigned nparam, unsigned dim, unsigned extra,
941 unsigned n_eq, unsigned n_ineq)
942{
943 struct isl_basic_map *bmap;
944 isl_space *space;
945
946 space = isl_space_set_alloc(ctx, nparam, dim);
947 if (!space)
948 return NULL;
949
950 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
951 return (struct isl_basic_set *)bmap;
952}
953
954struct isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *dim,
955 unsigned extra, unsigned n_eq, unsigned n_ineq)
956{
957 struct isl_basic_map *bmap;
958 if (!dim)
959 return NULL;
960 isl_assert(dim->ctx, dim->n_in == 0, goto error);
961 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
962 return (struct isl_basic_set *)bmap;
963error:
964 isl_space_free(dim);
965 return NULL;
966}
967
968struct isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *dim,
969 unsigned extra, unsigned n_eq, unsigned n_ineq)
970{
971 struct isl_basic_map *bmap;
972
973 if (!dim)
974 return NULL;
975 bmap = isl_calloc_type(dim->ctx, struct isl_basic_map);
976 if (!bmap)
977 goto error;
978 bmap->dim = dim;
979
980 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
981error:
982 isl_space_free(dim);
983 return NULL;
984}
985
986struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
987 unsigned nparam, unsigned in, unsigned out, unsigned extra,
988 unsigned n_eq, unsigned n_ineq)
989{
990 struct isl_basic_map *bmap;
991 isl_space *dim;
992
993 dim = isl_space_alloc(ctx, nparam, in, out);
994 if (!dim)
995 return NULL;
996
997 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
998 return bmap;
999}
1000
1001static void dup_constraints(
1002 struct isl_basic_map *dst, struct isl_basic_map *src)
1003{
1004 int i;
1005 unsigned total = isl_basic_map_total_dim(src);
1006
1007 for (i = 0; i < src->n_eq; ++i) {
1008 int j = isl_basic_map_alloc_equality(dst);
1009 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
1010 }
1011
1012 for (i = 0; i < src->n_ineq; ++i) {
1013 int j = isl_basic_map_alloc_inequality(dst);
1014 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
1015 }
1016
1017 for (i = 0; i < src->n_div; ++i) {
1018 int j = isl_basic_map_alloc_div(dst);
1019 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
1020 }
1021 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
1022}
1023
1024struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
1025{
1026 struct isl_basic_map *dup;
1027
1028 if (!bmap)
1029 return NULL;
1030 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
1031 bmap->n_div, bmap->n_eq, bmap->n_ineq);
1032 if (!dup)
1033 return NULL;
1034 dup_constraints(dup, bmap);
1035 dup->flags = bmap->flags;
1036 dup->sample = isl_vec_copy(bmap->sample);
1037 return dup;
1038}
1039
1040struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
1041{
1042 struct isl_basic_map *dup;
1043
1044 dup = isl_basic_map_dup((struct isl_basic_map *)bset);
1045 return (struct isl_basic_set *)dup;
1046}
1047
1048struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
1049{
1050 if (!bset)
1051 return NULL;
1052
1053 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
1054 bset->ref++;
1055 return bset;
1056 }
1057 return isl_basic_set_dup(bset);
1058}
1059
1060struct isl_set *isl_set_copy(struct isl_set *set)
1061{
1062 if (!set)
1063 return NULL;
1064
1065 set->ref++;
1066 return set;
1067}
1068
1069struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
1070{
1071 if (!bmap)
1072 return NULL;
1073
1074 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
1075 bmap->ref++;
1076 return bmap;
1077 }
1078 bmap = isl_basic_map_dup(bmap);
1079 if (bmap)
1080 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1081 return bmap;
1082}
1083
1084struct isl_map *isl_map_copy(struct isl_map *map)
1085{
1086 if (!map)
1087 return NULL;
1088
1089 map->ref++;
1090 return map;
1091}
1092
1093__isl_null isl_basic_map *isl_basic_map_free(__isl_take isl_basic_map *bmap)
1094{
1095 if (!bmap)
1096 return NULL;
1097
1098 if (--bmap->ref > 0)
1099 return NULL;
1100
1101 isl_ctx_deref(bmap->ctx);
1102 free(bmap->div);
1103 isl_blk_free(bmap->ctx, bmap->block2);
1104 free(bmap->ineq);
1105 isl_blk_free(bmap->ctx, bmap->block);
1106 isl_vec_free(bmap->sample);
1107 isl_space_free(bmap->dim);
1108 free(bmap);
1109
1110 return NULL;
1111}
1112
1113__isl_null isl_basic_set *isl_basic_set_free(__isl_take isl_basic_set *bset)
1114{
1115 return isl_basic_map_free((struct isl_basic_map *)bset);
1116}
1117
1118static int room_for_con(struct isl_basic_map *bmap, unsigned n)
1119{
1120 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
1121}
1122
1123__isl_give isl_map *isl_map_align_params_map_map_and(
1124 __isl_take isl_map *map1, __isl_take isl_map *map2,
1125 __isl_give isl_map *(*fn)(__isl_take isl_map *map1,
1126 __isl_take isl_map *map2))
1127{
1128 if (!map1 || !map2)
1129 goto error;
1130 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
1131 return fn(map1, map2);
1132 if (!isl_space_has_named_params(map1->dim) ||
1133 !isl_space_has_named_params(map2->dim))
1134 isl_die(map1->ctx, isl_error_invalid,
1135 "unaligned unnamed parameters", goto error);
1136 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1137 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1138 return fn(map1, map2);
1139error:
1140 isl_map_free(map1);
1141 isl_map_free(map2);
1142 return NULL;
1143}
1144
Tobias Grosserb2f39922015-05-28 13:32:11 +00001145isl_bool isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1,
Tobias Grosser52a25232015-02-04 20:55:43 +00001146 __isl_keep isl_map *map2,
Tobias Grosserb2f39922015-05-28 13:32:11 +00001147 isl_bool (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
Tobias Grosser52a25232015-02-04 20:55:43 +00001148{
Tobias Grosserb2f39922015-05-28 13:32:11 +00001149 isl_bool r;
Tobias Grosser52a25232015-02-04 20:55:43 +00001150
1151 if (!map1 || !map2)
Tobias Grosserb2f39922015-05-28 13:32:11 +00001152 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00001153 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
1154 return fn(map1, map2);
1155 if (!isl_space_has_named_params(map1->dim) ||
1156 !isl_space_has_named_params(map2->dim))
1157 isl_die(map1->ctx, isl_error_invalid,
Tobias Grosserb2f39922015-05-28 13:32:11 +00001158 "unaligned unnamed parameters", return isl_bool_error);
Tobias Grosser52a25232015-02-04 20:55:43 +00001159 map1 = isl_map_copy(map1);
1160 map2 = isl_map_copy(map2);
1161 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1162 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1163 r = fn(map1, map2);
1164 isl_map_free(map1);
1165 isl_map_free(map2);
1166 return r;
1167}
1168
1169int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
1170{
1171 struct isl_ctx *ctx;
1172 if (!bmap)
1173 return -1;
1174 ctx = bmap->ctx;
1175 isl_assert(ctx, room_for_con(bmap, 1), return -1);
1176 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
1177 return -1);
1178 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1179 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1180 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1181 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1182 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1183 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
1184 isl_int *t;
1185 int j = isl_basic_map_alloc_inequality(bmap);
1186 if (j < 0)
1187 return -1;
1188 t = bmap->ineq[j];
1189 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
1190 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1191 bmap->eq[-1] = t;
1192 bmap->n_eq++;
1193 bmap->n_ineq--;
1194 bmap->eq--;
1195 return 0;
1196 }
1197 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
1198 bmap->extra - bmap->n_div);
1199 return bmap->n_eq++;
1200}
1201
1202int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
1203{
1204 return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
1205}
1206
1207int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
1208{
1209 if (!bmap)
1210 return -1;
1211 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
1212 bmap->n_eq -= n;
1213 return 0;
1214}
1215
1216int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
1217{
1218 return isl_basic_map_free_equality((struct isl_basic_map *)bset, n);
1219}
1220
1221int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
1222{
1223 isl_int *t;
1224 if (!bmap)
1225 return -1;
1226 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
1227
1228 if (pos != bmap->n_eq - 1) {
1229 t = bmap->eq[pos];
1230 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
1231 bmap->eq[bmap->n_eq - 1] = t;
1232 }
1233 bmap->n_eq--;
1234 return 0;
1235}
1236
1237int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
1238{
1239 return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
1240}
1241
1242/* Turn inequality "pos" of "bmap" into an equality.
1243 *
1244 * In particular, we move the inequality in front of the equalities
1245 * and move the last inequality in the position of the moved inequality.
1246 * Note that isl_tab_make_equalities_explicit depends on this particular
1247 * change in the ordering of the constraints.
1248 */
1249void isl_basic_map_inequality_to_equality(
1250 struct isl_basic_map *bmap, unsigned pos)
1251{
1252 isl_int *t;
1253
1254 t = bmap->ineq[pos];
1255 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1256 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1257 bmap->eq[-1] = t;
1258 bmap->n_eq++;
1259 bmap->n_ineq--;
1260 bmap->eq--;
1261 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1262 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1263 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1264 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1265}
1266
1267static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
1268{
1269 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1270}
1271
1272int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
1273{
1274 struct isl_ctx *ctx;
1275 if (!bmap)
1276 return -1;
1277 ctx = bmap->ctx;
1278 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1279 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1280 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1281 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1282 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1283 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
1284 1 + isl_basic_map_total_dim(bmap),
1285 bmap->extra - bmap->n_div);
1286 return bmap->n_ineq++;
1287}
1288
1289int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
1290{
1291 return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
1292}
1293
1294int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
1295{
1296 if (!bmap)
1297 return -1;
1298 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
1299 bmap->n_ineq -= n;
1300 return 0;
1301}
1302
1303int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
1304{
1305 return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
1306}
1307
1308int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
1309{
1310 isl_int *t;
1311 if (!bmap)
1312 return -1;
1313 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1314
1315 if (pos != bmap->n_ineq - 1) {
1316 t = bmap->ineq[pos];
1317 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1318 bmap->ineq[bmap->n_ineq - 1] = t;
1319 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1320 }
1321 bmap->n_ineq--;
1322 return 0;
1323}
1324
1325int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
1326{
1327 return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
1328}
1329
1330__isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1331 isl_int *eq)
1332{
1333 int k;
1334
1335 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1336 if (!bmap)
1337 return NULL;
1338 k = isl_basic_map_alloc_equality(bmap);
1339 if (k < 0)
1340 goto error;
1341 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
1342 return bmap;
1343error:
1344 isl_basic_map_free(bmap);
1345 return NULL;
1346}
1347
1348__isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1349 isl_int *eq)
1350{
1351 return (isl_basic_set *)
1352 isl_basic_map_add_eq((isl_basic_map *)bset, eq);
1353}
1354
1355__isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1356 isl_int *ineq)
1357{
1358 int k;
1359
1360 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1361 if (!bmap)
1362 return NULL;
1363 k = isl_basic_map_alloc_inequality(bmap);
1364 if (k < 0)
1365 goto error;
1366 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
1367 return bmap;
1368error:
1369 isl_basic_map_free(bmap);
1370 return NULL;
1371}
1372
1373__isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1374 isl_int *ineq)
1375{
1376 return (isl_basic_set *)
1377 isl_basic_map_add_ineq((isl_basic_map *)bset, ineq);
1378}
1379
1380int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
1381{
1382 if (!bmap)
1383 return -1;
1384 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1385 isl_seq_clr(bmap->div[bmap->n_div] +
1386 1 + 1 + isl_basic_map_total_dim(bmap),
1387 bmap->extra - bmap->n_div);
1388 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1389 return bmap->n_div++;
1390}
1391
1392int isl_basic_set_alloc_div(struct isl_basic_set *bset)
1393{
1394 return isl_basic_map_alloc_div((struct isl_basic_map *)bset);
1395}
1396
1397int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
1398{
1399 if (!bmap)
1400 return -1;
1401 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
1402 bmap->n_div -= n;
1403 return 0;
1404}
1405
1406int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n)
1407{
1408 return isl_basic_map_free_div((struct isl_basic_map *)bset, n);
1409}
1410
1411/* Copy constraint from src to dst, putting the vars of src at offset
1412 * dim_off in dst and the divs of src at offset div_off in dst.
1413 * If both sets are actually map, then dim_off applies to the input
1414 * variables.
1415 */
1416static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
1417 struct isl_basic_map *src_map, isl_int *src,
1418 unsigned in_off, unsigned out_off, unsigned div_off)
1419{
1420 unsigned src_nparam = isl_basic_map_n_param(src_map);
1421 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
1422 unsigned src_in = isl_basic_map_n_in(src_map);
1423 unsigned dst_in = isl_basic_map_n_in(dst_map);
1424 unsigned src_out = isl_basic_map_n_out(src_map);
1425 unsigned dst_out = isl_basic_map_n_out(dst_map);
1426 isl_int_set(dst[0], src[0]);
1427 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
1428 if (dst_nparam > src_nparam)
1429 isl_seq_clr(dst+1+src_nparam,
1430 dst_nparam - src_nparam);
1431 isl_seq_clr(dst+1+dst_nparam, in_off);
1432 isl_seq_cpy(dst+1+dst_nparam+in_off,
1433 src+1+src_nparam,
1434 isl_min(dst_in-in_off, src_in));
1435 if (dst_in-in_off > src_in)
1436 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
1437 dst_in - in_off - src_in);
1438 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
1439 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
1440 src+1+src_nparam+src_in,
1441 isl_min(dst_out-out_off, src_out));
1442 if (dst_out-out_off > src_out)
1443 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
1444 dst_out - out_off - src_out);
1445 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
1446 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
1447 src+1+src_nparam+src_in+src_out,
1448 isl_min(dst_map->extra-div_off, src_map->n_div));
1449 if (dst_map->n_div-div_off > src_map->n_div)
1450 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
1451 div_off+src_map->n_div,
1452 dst_map->n_div - div_off - src_map->n_div);
1453}
1454
1455static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
1456 struct isl_basic_map *src_map, isl_int *src,
1457 unsigned in_off, unsigned out_off, unsigned div_off)
1458{
1459 isl_int_set(dst[0], src[0]);
1460 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
1461}
1462
1463static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
1464 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
1465{
1466 int i;
1467 unsigned div_off;
1468
1469 if (!bmap1 || !bmap2)
1470 goto error;
1471
1472 div_off = bmap1->n_div;
1473
1474 for (i = 0; i < bmap2->n_eq; ++i) {
1475 int i1 = isl_basic_map_alloc_equality(bmap1);
1476 if (i1 < 0)
1477 goto error;
1478 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
1479 i_pos, o_pos, div_off);
1480 }
1481
1482 for (i = 0; i < bmap2->n_ineq; ++i) {
1483 int i1 = isl_basic_map_alloc_inequality(bmap1);
1484 if (i1 < 0)
1485 goto error;
1486 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
1487 i_pos, o_pos, div_off);
1488 }
1489
1490 for (i = 0; i < bmap2->n_div; ++i) {
1491 int i1 = isl_basic_map_alloc_div(bmap1);
1492 if (i1 < 0)
1493 goto error;
1494 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
1495 i_pos, o_pos, div_off);
1496 }
1497
1498 isl_basic_map_free(bmap2);
1499
1500 return bmap1;
1501
1502error:
1503 isl_basic_map_free(bmap1);
1504 isl_basic_map_free(bmap2);
1505 return NULL;
1506}
1507
1508struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1509 struct isl_basic_set *bset2, unsigned pos)
1510{
1511 return (struct isl_basic_set *)
1512 add_constraints((struct isl_basic_map *)bset1,
1513 (struct isl_basic_map *)bset2, 0, pos);
1514}
1515
1516struct isl_basic_map *isl_basic_map_extend_space(struct isl_basic_map *base,
1517 __isl_take isl_space *dim, unsigned extra,
1518 unsigned n_eq, unsigned n_ineq)
1519{
1520 struct isl_basic_map *ext;
1521 unsigned flags;
1522 int dims_ok;
1523
1524 if (!dim)
1525 goto error;
1526
1527 if (!base)
1528 goto error;
1529
1530 dims_ok = isl_space_is_equal(base->dim, dim) &&
1531 base->extra >= base->n_div + extra;
1532
1533 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1534 room_for_ineq(base, n_ineq)) {
1535 isl_space_free(dim);
1536 return base;
1537 }
1538
1539 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
1540 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
1541 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
1542 extra += base->extra;
1543 n_eq += base->n_eq;
1544 n_ineq += base->n_ineq;
1545
1546 ext = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1547 dim = NULL;
1548 if (!ext)
1549 goto error;
1550
1551 if (dims_ok)
1552 ext->sample = isl_vec_copy(base->sample);
1553 flags = base->flags;
1554 ext = add_constraints(ext, base, 0, 0);
1555 if (ext) {
1556 ext->flags = flags;
1557 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1558 }
1559
1560 return ext;
1561
1562error:
1563 isl_space_free(dim);
1564 isl_basic_map_free(base);
1565 return NULL;
1566}
1567
1568struct isl_basic_set *isl_basic_set_extend_space(struct isl_basic_set *base,
1569 __isl_take isl_space *dim, unsigned extra,
1570 unsigned n_eq, unsigned n_ineq)
1571{
1572 return (struct isl_basic_set *)
1573 isl_basic_map_extend_space((struct isl_basic_map *)base, dim,
1574 extra, n_eq, n_ineq);
1575}
1576
1577struct isl_basic_map *isl_basic_map_extend_constraints(
1578 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1579{
1580 if (!base)
1581 return NULL;
1582 return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1583 0, n_eq, n_ineq);
1584}
1585
1586struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1587 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1588 unsigned n_eq, unsigned n_ineq)
1589{
1590 struct isl_basic_map *bmap;
1591 isl_space *dim;
1592
1593 if (!base)
1594 return NULL;
1595 dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1596 if (!dim)
1597 goto error;
1598
1599 bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1600 return bmap;
1601error:
1602 isl_basic_map_free(base);
1603 return NULL;
1604}
1605
1606struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1607 unsigned nparam, unsigned dim, unsigned extra,
1608 unsigned n_eq, unsigned n_ineq)
1609{
1610 return (struct isl_basic_set *)
1611 isl_basic_map_extend((struct isl_basic_map *)base,
1612 nparam, 0, dim, extra, n_eq, n_ineq);
1613}
1614
1615struct isl_basic_set *isl_basic_set_extend_constraints(
1616 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1617{
1618 return (struct isl_basic_set *)
1619 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
1620 n_eq, n_ineq);
1621}
1622
1623struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1624{
1625 return (struct isl_basic_set *)
1626 isl_basic_map_cow((struct isl_basic_map *)bset);
1627}
1628
1629struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1630{
1631 if (!bmap)
1632 return NULL;
1633
1634 if (bmap->ref > 1) {
1635 bmap->ref--;
1636 bmap = isl_basic_map_dup(bmap);
1637 }
Tobias Grosser1fa7b972015-02-16 19:33:40 +00001638 if (bmap) {
Tobias Grosser52a25232015-02-04 20:55:43 +00001639 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
Tobias Grosser1fa7b972015-02-16 19:33:40 +00001640 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
1641 }
Tobias Grosser52a25232015-02-04 20:55:43 +00001642 return bmap;
1643}
1644
1645struct isl_set *isl_set_cow(struct isl_set *set)
1646{
1647 if (!set)
1648 return NULL;
1649
1650 if (set->ref == 1)
1651 return set;
1652 set->ref--;
1653 return isl_set_dup(set);
1654}
1655
1656struct isl_map *isl_map_cow(struct isl_map *map)
1657{
1658 if (!map)
1659 return NULL;
1660
1661 if (map->ref == 1)
1662 return map;
1663 map->ref--;
1664 return isl_map_dup(map);
1665}
1666
1667static void swap_vars(struct isl_blk blk, isl_int *a,
1668 unsigned a_len, unsigned b_len)
1669{
1670 isl_seq_cpy(blk.data, a+a_len, b_len);
1671 isl_seq_cpy(blk.data+b_len, a, a_len);
1672 isl_seq_cpy(a, blk.data, b_len+a_len);
1673}
1674
1675static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1676 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1677{
1678 int i;
1679 struct isl_blk blk;
1680
1681 if (!bmap)
1682 goto error;
1683
1684 isl_assert(bmap->ctx,
1685 pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error);
1686
1687 if (n1 == 0 || n2 == 0)
1688 return bmap;
1689
1690 bmap = isl_basic_map_cow(bmap);
1691 if (!bmap)
1692 return NULL;
1693
1694 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1695 if (isl_blk_is_error(blk))
1696 goto error;
1697
1698 for (i = 0; i < bmap->n_eq; ++i)
1699 swap_vars(blk,
1700 bmap->eq[i] + pos, n1, n2);
1701
1702 for (i = 0; i < bmap->n_ineq; ++i)
1703 swap_vars(blk,
1704 bmap->ineq[i] + pos, n1, n2);
1705
1706 for (i = 0; i < bmap->n_div; ++i)
1707 swap_vars(blk,
1708 bmap->div[i]+1 + pos, n1, n2);
1709
1710 isl_blk_free(bmap->ctx, blk);
1711
1712 ISL_F_CLR(bmap, ISL_BASIC_SET_NORMALIZED);
1713 bmap = isl_basic_map_gauss(bmap, NULL);
1714 return isl_basic_map_finalize(bmap);
1715error:
1716 isl_basic_map_free(bmap);
1717 return NULL;
1718}
1719
Tobias Grosser52a25232015-02-04 20:55:43 +00001720struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1721{
1722 int i = 0;
1723 unsigned total;
1724 if (!bmap)
1725 goto error;
1726 total = isl_basic_map_total_dim(bmap);
1727 isl_basic_map_free_div(bmap, bmap->n_div);
1728 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1729 if (bmap->n_eq > 0)
1730 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1731 else {
1732 i = isl_basic_map_alloc_equality(bmap);
1733 if (i < 0)
1734 goto error;
1735 }
1736 isl_int_set_si(bmap->eq[i][0], 1);
1737 isl_seq_clr(bmap->eq[i]+1, total);
1738 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1739 isl_vec_free(bmap->sample);
1740 bmap->sample = NULL;
1741 return isl_basic_map_finalize(bmap);
1742error:
1743 isl_basic_map_free(bmap);
1744 return NULL;
1745}
1746
1747struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1748{
1749 return (struct isl_basic_set *)
1750 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1751}
1752
1753/* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
1754 * of "bmap").
1755 */
1756static void swap_div(__isl_keep isl_basic_map *bmap, int a, int b)
1757{
1758 isl_int *t = bmap->div[a];
1759 bmap->div[a] = bmap->div[b];
1760 bmap->div[b] = t;
1761}
1762
1763/* Swap divs "a" and "b" in "bmap" and adjust the constraints and
1764 * div definitions accordingly.
1765 */
1766void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1767{
1768 int i;
1769 unsigned off = isl_space_dim(bmap->dim, isl_dim_all);
1770
1771 swap_div(bmap, a, b);
1772
1773 for (i = 0; i < bmap->n_eq; ++i)
1774 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1775
1776 for (i = 0; i < bmap->n_ineq; ++i)
1777 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1778
1779 for (i = 0; i < bmap->n_div; ++i)
1780 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1781 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1782}
1783
1784/* Eliminate the specified n dimensions starting at first from the
1785 * constraints, without removing the dimensions from the space.
1786 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1787 */
1788__isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
1789 enum isl_dim_type type, unsigned first, unsigned n)
1790{
1791 int i;
1792
1793 if (!map)
1794 return NULL;
1795 if (n == 0)
1796 return map;
1797
1798 if (first + n > isl_map_dim(map, type) || first + n < first)
1799 isl_die(map->ctx, isl_error_invalid,
1800 "index out of bounds", goto error);
1801
1802 map = isl_map_cow(map);
1803 if (!map)
1804 return NULL;
1805
1806 for (i = 0; i < map->n; ++i) {
1807 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
1808 if (!map->p[i])
1809 goto error;
1810 }
1811 return map;
1812error:
1813 isl_map_free(map);
1814 return NULL;
1815}
1816
1817/* Eliminate the specified n dimensions starting at first from the
1818 * constraints, without removing the dimensions from the space.
1819 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1820 */
1821__isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
1822 enum isl_dim_type type, unsigned first, unsigned n)
1823{
1824 return (isl_set *)isl_map_eliminate((isl_map *)set, type, first, n);
1825}
1826
1827/* Eliminate the specified n dimensions starting at first from the
1828 * constraints, without removing the dimensions from the space.
1829 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1830 */
1831__isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
1832 unsigned first, unsigned n)
1833{
1834 return isl_set_eliminate(set, isl_dim_set, first, n);
1835}
1836
1837__isl_give isl_basic_map *isl_basic_map_remove_divs(
1838 __isl_take isl_basic_map *bmap)
1839{
1840 if (!bmap)
1841 return NULL;
1842 bmap = isl_basic_map_eliminate_vars(bmap,
1843 isl_space_dim(bmap->dim, isl_dim_all), bmap->n_div);
1844 if (!bmap)
1845 return NULL;
1846 bmap->n_div = 0;
1847 return isl_basic_map_finalize(bmap);
1848}
1849
1850__isl_give isl_basic_set *isl_basic_set_remove_divs(
1851 __isl_take isl_basic_set *bset)
1852{
1853 return (struct isl_basic_set *)isl_basic_map_remove_divs(
1854 (struct isl_basic_map *)bset);
1855}
1856
1857__isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
1858{
1859 int i;
1860
1861 if (!map)
1862 return NULL;
1863 if (map->n == 0)
1864 return map;
1865
1866 map = isl_map_cow(map);
1867 if (!map)
1868 return NULL;
1869
1870 for (i = 0; i < map->n; ++i) {
1871 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
1872 if (!map->p[i])
1873 goto error;
1874 }
1875 return map;
1876error:
1877 isl_map_free(map);
1878 return NULL;
1879}
1880
1881__isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
1882{
1883 return isl_map_remove_divs(set);
1884}
1885
1886struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap,
1887 enum isl_dim_type type, unsigned first, unsigned n)
1888{
1889 if (!bmap)
1890 return NULL;
1891 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1892 goto error);
1893 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
1894 return bmap;
1895 bmap = isl_basic_map_eliminate_vars(bmap,
1896 isl_basic_map_offset(bmap, type) - 1 + first, n);
1897 if (!bmap)
1898 return bmap;
1899 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
1900 return bmap;
1901 bmap = isl_basic_map_drop(bmap, type, first, n);
1902 return bmap;
1903error:
1904 isl_basic_map_free(bmap);
1905 return NULL;
1906}
1907
1908/* Return true if the definition of the given div (recursively) involves
1909 * any of the given variables.
1910 */
1911static int div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
1912 unsigned first, unsigned n)
1913{
1914 int i;
1915 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1916
1917 if (isl_int_is_zero(bmap->div[div][0]))
1918 return 0;
1919 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
1920 return 1;
1921
1922 for (i = bmap->n_div - 1; i >= 0; --i) {
1923 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1924 continue;
1925 if (div_involves_vars(bmap, i, first, n))
1926 return 1;
1927 }
1928
1929 return 0;
1930}
1931
1932/* Try and add a lower and/or upper bound on "div" to "bmap"
1933 * based on inequality "i".
1934 * "total" is the total number of variables (excluding the divs).
1935 * "v" is a temporary object that can be used during the calculations.
1936 * If "lb" is set, then a lower bound should be constructed.
1937 * If "ub" is set, then an upper bound should be constructed.
1938 *
1939 * The calling function has already checked that the inequality does not
1940 * reference "div", but we still need to check that the inequality is
1941 * of the right form. We'll consider the case where we want to construct
1942 * a lower bound. The construction of upper bounds is similar.
1943 *
1944 * Let "div" be of the form
1945 *
1946 * q = floor((a + f(x))/d)
1947 *
1948 * We essentially check if constraint "i" is of the form
1949 *
1950 * b + f(x) >= 0
1951 *
1952 * so that we can use it to derive a lower bound on "div".
1953 * However, we allow a slightly more general form
1954 *
1955 * b + g(x) >= 0
1956 *
1957 * with the condition that the coefficients of g(x) - f(x) are all
1958 * divisible by d.
1959 * Rewriting this constraint as
1960 *
1961 * 0 >= -b - g(x)
1962 *
1963 * adding a + f(x) to both sides and dividing by d, we obtain
1964 *
1965 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
1966 *
1967 * Taking the floor on both sides, we obtain
1968 *
1969 * q >= floor((a-b)/d) + (f(x)-g(x))/d
1970 *
1971 * or
1972 *
1973 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
1974 *
1975 * In the case of an upper bound, we construct the constraint
1976 *
1977 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
1978 *
1979 */
1980static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
1981 __isl_take isl_basic_map *bmap, int div, int i,
1982 unsigned total, isl_int v, int lb, int ub)
1983{
1984 int j;
1985
1986 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
1987 if (lb) {
1988 isl_int_sub(v, bmap->ineq[i][1 + j],
1989 bmap->div[div][1 + 1 + j]);
1990 lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
1991 }
1992 if (ub) {
1993 isl_int_add(v, bmap->ineq[i][1 + j],
1994 bmap->div[div][1 + 1 + j]);
1995 ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
1996 }
1997 }
1998 if (!lb && !ub)
1999 return bmap;
2000
2001 bmap = isl_basic_map_cow(bmap);
2002 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
2003 if (lb) {
2004 int k = isl_basic_map_alloc_inequality(bmap);
2005 if (k < 0)
2006 goto error;
2007 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2008 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
2009 bmap->div[div][1 + j]);
2010 isl_int_cdiv_q(bmap->ineq[k][j],
2011 bmap->ineq[k][j], bmap->div[div][0]);
2012 }
2013 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
2014 }
2015 if (ub) {
2016 int k = isl_basic_map_alloc_inequality(bmap);
2017 if (k < 0)
2018 goto error;
2019 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2020 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
2021 bmap->div[div][1 + j]);
2022 isl_int_fdiv_q(bmap->ineq[k][j],
2023 bmap->ineq[k][j], bmap->div[div][0]);
2024 }
2025 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
2026 }
2027
2028 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2029 return bmap;
2030error:
2031 isl_basic_map_free(bmap);
2032 return NULL;
2033}
2034
2035/* This function is called right before "div" is eliminated from "bmap"
2036 * using Fourier-Motzkin.
2037 * Look through the constraints of "bmap" for constraints on the argument
2038 * of the integer division and use them to construct constraints on the
2039 * integer division itself. These constraints can then be combined
2040 * during the Fourier-Motzkin elimination.
2041 * Note that it is only useful to introduce lower bounds on "div"
2042 * if "bmap" already contains upper bounds on "div" as the newly
2043 * introduce lower bounds can then be combined with the pre-existing
2044 * upper bounds. Similarly for upper bounds.
2045 * We therefore first check if "bmap" contains any lower and/or upper bounds
2046 * on "div".
2047 *
2048 * It is interesting to note that the introduction of these constraints
2049 * can indeed lead to more accurate results, even when compared to
2050 * deriving constraints on the argument of "div" from constraints on "div".
2051 * Consider, for example, the set
2052 *
2053 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2054 *
2055 * The second constraint can be rewritten as
2056 *
2057 * 2 * [(-i-2j+3)/4] + k >= 0
2058 *
2059 * from which we can derive
2060 *
2061 * -i - 2j + 3 >= -2k
2062 *
2063 * or
2064 *
2065 * i + 2j <= 3 + 2k
2066 *
2067 * Combined with the first constraint, we obtain
2068 *
2069 * -3 <= 3 + 2k or k >= -3
2070 *
2071 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2072 * the first constraint, we obtain
2073 *
2074 * [(i + 2j)/4] >= [-3/4] = -1
2075 *
2076 * Combining this constraint with the second constraint, we obtain
2077 *
2078 * k >= -2
2079 */
2080static __isl_give isl_basic_map *insert_bounds_on_div(
2081 __isl_take isl_basic_map *bmap, int div)
2082{
2083 int i;
2084 int check_lb, check_ub;
2085 isl_int v;
2086 unsigned total;
2087
2088 if (!bmap)
2089 return NULL;
2090
2091 if (isl_int_is_zero(bmap->div[div][0]))
2092 return bmap;
2093
2094 total = isl_space_dim(bmap->dim, isl_dim_all);
2095
2096 check_lb = 0;
2097 check_ub = 0;
2098 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
2099 int s = isl_int_sgn(bmap->ineq[i][1 + total + div]);
2100 if (s > 0)
2101 check_ub = 1;
2102 if (s < 0)
2103 check_lb = 1;
2104 }
2105
2106 if (!check_lb && !check_ub)
2107 return bmap;
2108
2109 isl_int_init(v);
2110
2111 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
2112 if (!isl_int_is_zero(bmap->ineq[i][1 + total + div]))
2113 continue;
2114
2115 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, total, v,
2116 check_lb, check_ub);
2117 }
2118
2119 isl_int_clear(v);
2120
2121 return bmap;
2122}
2123
2124/* Remove all divs (recursively) involving any of the given dimensions
2125 * in their definitions.
2126 */
2127__isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
2128 __isl_take isl_basic_map *bmap,
2129 enum isl_dim_type type, unsigned first, unsigned n)
2130{
2131 int i;
2132
2133 if (!bmap)
2134 return NULL;
2135 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
2136 goto error);
2137 first += isl_basic_map_offset(bmap, type);
2138
2139 for (i = bmap->n_div - 1; i >= 0; --i) {
2140 if (!div_involves_vars(bmap, i, first, n))
2141 continue;
2142 bmap = insert_bounds_on_div(bmap, i);
2143 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2144 if (!bmap)
2145 return NULL;
2146 i = bmap->n_div;
2147 }
2148
2149 return bmap;
2150error:
2151 isl_basic_map_free(bmap);
2152 return NULL;
2153}
2154
2155__isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims(
2156 __isl_take isl_basic_set *bset,
2157 enum isl_dim_type type, unsigned first, unsigned n)
2158{
2159 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
2160}
2161
2162__isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
2163 enum isl_dim_type type, unsigned first, unsigned n)
2164{
2165 int i;
2166
2167 if (!map)
2168 return NULL;
2169 if (map->n == 0)
2170 return map;
2171
2172 map = isl_map_cow(map);
2173 if (!map)
2174 return NULL;
2175
2176 for (i = 0; i < map->n; ++i) {
2177 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
2178 type, first, n);
2179 if (!map->p[i])
2180 goto error;
2181 }
2182 return map;
2183error:
2184 isl_map_free(map);
2185 return NULL;
2186}
2187
2188__isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
2189 enum isl_dim_type type, unsigned first, unsigned n)
2190{
2191 return (isl_set *)isl_map_remove_divs_involving_dims((isl_map *)set,
2192 type, first, n);
2193}
2194
2195/* Does the desciption of "bmap" depend on the specified dimensions?
2196 * We also check whether the dimensions appear in any of the div definitions.
2197 * In principle there is no need for this check. If the dimensions appear
2198 * in a div definition, they also appear in the defining constraints of that
2199 * div.
2200 */
Tobias Grosserb2f39922015-05-28 13:32:11 +00002201isl_bool isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
Tobias Grosser52a25232015-02-04 20:55:43 +00002202 enum isl_dim_type type, unsigned first, unsigned n)
2203{
2204 int i;
2205
2206 if (!bmap)
Tobias Grosserb2f39922015-05-28 13:32:11 +00002207 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00002208
2209 if (first + n > isl_basic_map_dim(bmap, type))
2210 isl_die(bmap->ctx, isl_error_invalid,
Tobias Grosserb2f39922015-05-28 13:32:11 +00002211 "index out of bounds", return isl_bool_error);
Tobias Grosser52a25232015-02-04 20:55:43 +00002212
2213 first += isl_basic_map_offset(bmap, type);
2214 for (i = 0; i < bmap->n_eq; ++i)
2215 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
Tobias Grosserb2f39922015-05-28 13:32:11 +00002216 return isl_bool_true;
Tobias Grosser52a25232015-02-04 20:55:43 +00002217 for (i = 0; i < bmap->n_ineq; ++i)
2218 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
Tobias Grosserb2f39922015-05-28 13:32:11 +00002219 return isl_bool_true;
Tobias Grosser52a25232015-02-04 20:55:43 +00002220 for (i = 0; i < bmap->n_div; ++i) {
2221 if (isl_int_is_zero(bmap->div[i][0]))
2222 continue;
2223 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
Tobias Grosserb2f39922015-05-28 13:32:11 +00002224 return isl_bool_true;
Tobias Grosser52a25232015-02-04 20:55:43 +00002225 }
2226
Tobias Grosserb2f39922015-05-28 13:32:11 +00002227 return isl_bool_false;
Tobias Grosser52a25232015-02-04 20:55:43 +00002228}
2229
Tobias Grosserb2f39922015-05-28 13:32:11 +00002230isl_bool isl_map_involves_dims(__isl_keep isl_map *map,
Tobias Grosser52a25232015-02-04 20:55:43 +00002231 enum isl_dim_type type, unsigned first, unsigned n)
2232{
2233 int i;
2234
2235 if (!map)
Tobias Grosserb2f39922015-05-28 13:32:11 +00002236 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00002237
2238 if (first + n > isl_map_dim(map, type))
2239 isl_die(map->ctx, isl_error_invalid,
Tobias Grosserb2f39922015-05-28 13:32:11 +00002240 "index out of bounds", return isl_bool_error);
Tobias Grosser52a25232015-02-04 20:55:43 +00002241
2242 for (i = 0; i < map->n; ++i) {
Tobias Grosserb2f39922015-05-28 13:32:11 +00002243 isl_bool involves = isl_basic_map_involves_dims(map->p[i],
Tobias Grosser52a25232015-02-04 20:55:43 +00002244 type, first, n);
2245 if (involves < 0 || involves)
2246 return involves;
2247 }
2248
Tobias Grosserb2f39922015-05-28 13:32:11 +00002249 return isl_bool_false;
Tobias Grosser52a25232015-02-04 20:55:43 +00002250}
2251
Tobias Grosserb2f39922015-05-28 13:32:11 +00002252isl_bool isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
Tobias Grosser52a25232015-02-04 20:55:43 +00002253 enum isl_dim_type type, unsigned first, unsigned n)
2254{
2255 return isl_basic_map_involves_dims(bset, type, first, n);
2256}
2257
Tobias Grosserb2f39922015-05-28 13:32:11 +00002258isl_bool isl_set_involves_dims(__isl_keep isl_set *set,
Tobias Grosser52a25232015-02-04 20:55:43 +00002259 enum isl_dim_type type, unsigned first, unsigned n)
2260{
2261 return isl_map_involves_dims(set, type, first, n);
2262}
2263
2264/* Return true if the definition of the given div is unknown or depends
2265 * on unknown divs.
2266 */
2267static int div_is_unknown(__isl_keep isl_basic_map *bmap, int div)
2268{
2269 int i;
2270 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2271
2272 if (isl_int_is_zero(bmap->div[div][0]))
2273 return 1;
2274
2275 for (i = bmap->n_div - 1; i >= 0; --i) {
2276 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2277 continue;
2278 if (div_is_unknown(bmap, i))
2279 return 1;
2280 }
2281
2282 return 0;
2283}
2284
2285/* Remove all divs that are unknown or defined in terms of unknown divs.
2286 */
2287__isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
2288 __isl_take isl_basic_map *bmap)
2289{
2290 int i;
2291
2292 if (!bmap)
2293 return NULL;
2294
2295 for (i = bmap->n_div - 1; i >= 0; --i) {
2296 if (!div_is_unknown(bmap, i))
2297 continue;
2298 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2299 if (!bmap)
2300 return NULL;
2301 i = bmap->n_div;
2302 }
2303
2304 return bmap;
2305}
2306
2307/* Remove all divs that are unknown or defined in terms of unknown divs.
2308 */
2309__isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
2310 __isl_take isl_basic_set *bset)
2311{
2312 return isl_basic_map_remove_unknown_divs(bset);
2313}
2314
2315__isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
2316{
2317 int i;
2318
2319 if (!map)
2320 return NULL;
2321 if (map->n == 0)
2322 return map;
2323
2324 map = isl_map_cow(map);
2325 if (!map)
2326 return NULL;
2327
2328 for (i = 0; i < map->n; ++i) {
2329 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
2330 if (!map->p[i])
2331 goto error;
2332 }
2333 return map;
2334error:
2335 isl_map_free(map);
2336 return NULL;
2337}
2338
2339__isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
2340{
2341 return (isl_set *)isl_map_remove_unknown_divs((isl_map *)set);
2342}
2343
2344__isl_give isl_basic_set *isl_basic_set_remove_dims(
2345 __isl_take isl_basic_set *bset,
2346 enum isl_dim_type type, unsigned first, unsigned n)
2347{
2348 return (isl_basic_set *)
2349 isl_basic_map_remove_dims((isl_basic_map *)bset, type, first, n);
2350}
2351
2352struct isl_map *isl_map_remove_dims(struct isl_map *map,
2353 enum isl_dim_type type, unsigned first, unsigned n)
2354{
2355 int i;
2356
2357 if (n == 0)
2358 return map;
2359
2360 map = isl_map_cow(map);
2361 if (!map)
2362 return NULL;
2363 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2364
2365 for (i = 0; i < map->n; ++i) {
2366 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
2367 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
2368 if (!map->p[i])
2369 goto error;
2370 }
2371 map = isl_map_drop(map, type, first, n);
2372 return map;
2373error:
2374 isl_map_free(map);
2375 return NULL;
2376}
2377
2378__isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
2379 enum isl_dim_type type, unsigned first, unsigned n)
2380{
2381 return (isl_set *)isl_map_remove_dims((isl_map *)bset, type, first, n);
2382}
2383
2384/* Project out n inputs starting at first using Fourier-Motzkin */
2385struct isl_map *isl_map_remove_inputs(struct isl_map *map,
2386 unsigned first, unsigned n)
2387{
2388 return isl_map_remove_dims(map, isl_dim_in, first, n);
2389}
2390
2391static void dump_term(struct isl_basic_map *bmap,
2392 isl_int c, int pos, FILE *out)
2393{
2394 const char *name;
2395 unsigned in = isl_basic_map_n_in(bmap);
2396 unsigned dim = in + isl_basic_map_n_out(bmap);
2397 unsigned nparam = isl_basic_map_n_param(bmap);
2398 if (!pos)
2399 isl_int_print(out, c, 0);
2400 else {
2401 if (!isl_int_is_one(c))
2402 isl_int_print(out, c, 0);
2403 if (pos < 1 + nparam) {
2404 name = isl_space_get_dim_name(bmap->dim,
2405 isl_dim_param, pos - 1);
2406 if (name)
2407 fprintf(out, "%s", name);
2408 else
2409 fprintf(out, "p%d", pos - 1);
2410 } else if (pos < 1 + nparam + in)
2411 fprintf(out, "i%d", pos - 1 - nparam);
2412 else if (pos < 1 + nparam + dim)
2413 fprintf(out, "o%d", pos - 1 - nparam - in);
2414 else
2415 fprintf(out, "e%d", pos - 1 - nparam - dim);
2416 }
2417}
2418
2419static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
2420 int sign, FILE *out)
2421{
2422 int i;
2423 int first;
2424 unsigned len = 1 + isl_basic_map_total_dim(bmap);
2425 isl_int v;
2426
2427 isl_int_init(v);
2428 for (i = 0, first = 1; i < len; ++i) {
2429 if (isl_int_sgn(c[i]) * sign <= 0)
2430 continue;
2431 if (!first)
2432 fprintf(out, " + ");
2433 first = 0;
2434 isl_int_abs(v, c[i]);
2435 dump_term(bmap, v, i, out);
2436 }
2437 isl_int_clear(v);
2438 if (first)
2439 fprintf(out, "0");
2440}
2441
2442static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
2443 const char *op, FILE *out, int indent)
2444{
2445 int i;
2446
2447 fprintf(out, "%*s", indent, "");
2448
2449 dump_constraint_sign(bmap, c, 1, out);
2450 fprintf(out, " %s ", op);
2451 dump_constraint_sign(bmap, c, -1, out);
2452
2453 fprintf(out, "\n");
2454
2455 for (i = bmap->n_div; i < bmap->extra; ++i) {
2456 if (isl_int_is_zero(c[1+isl_space_dim(bmap->dim, isl_dim_all)+i]))
2457 continue;
2458 fprintf(out, "%*s", indent, "");
2459 fprintf(out, "ERROR: unused div coefficient not zero\n");
2460 abort();
2461 }
2462}
2463
2464static void dump_constraints(struct isl_basic_map *bmap,
2465 isl_int **c, unsigned n,
2466 const char *op, FILE *out, int indent)
2467{
2468 int i;
2469
2470 for (i = 0; i < n; ++i)
2471 dump_constraint(bmap, c[i], op, out, indent);
2472}
2473
2474static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
2475{
2476 int j;
2477 int first = 1;
2478 unsigned total = isl_basic_map_total_dim(bmap);
2479
2480 for (j = 0; j < 1 + total; ++j) {
2481 if (isl_int_is_zero(exp[j]))
2482 continue;
2483 if (!first && isl_int_is_pos(exp[j]))
2484 fprintf(out, "+");
2485 dump_term(bmap, exp[j], j, out);
2486 first = 0;
2487 }
2488}
2489
2490static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
2491{
2492 int i;
2493
2494 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
2495 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
2496
2497 for (i = 0; i < bmap->n_div; ++i) {
2498 fprintf(out, "%*s", indent, "");
2499 fprintf(out, "e%d = [(", i);
2500 dump_affine(bmap, bmap->div[i]+1, out);
2501 fprintf(out, ")/");
2502 isl_int_print(out, bmap->div[i][0], 0);
2503 fprintf(out, "]\n");
2504 }
2505}
2506
2507void isl_basic_set_print_internal(struct isl_basic_set *bset,
2508 FILE *out, int indent)
2509{
2510 if (!bset) {
2511 fprintf(out, "null basic set\n");
2512 return;
2513 }
2514
2515 fprintf(out, "%*s", indent, "");
2516 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2517 bset->ref, bset->dim->nparam, bset->dim->n_out,
2518 bset->extra, bset->flags);
2519 dump((struct isl_basic_map *)bset, out, indent);
2520}
2521
2522void isl_basic_map_print_internal(struct isl_basic_map *bmap,
2523 FILE *out, int indent)
2524{
2525 if (!bmap) {
2526 fprintf(out, "null basic map\n");
2527 return;
2528 }
2529
2530 fprintf(out, "%*s", indent, "");
2531 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2532 "flags: %x, n_name: %d\n",
2533 bmap->ref,
2534 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
2535 bmap->extra, bmap->flags, bmap->dim->n_id);
2536 dump(bmap, out, indent);
2537}
2538
2539int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
2540{
2541 unsigned total;
2542 if (!bmap)
2543 return -1;
2544 total = isl_basic_map_total_dim(bmap);
2545 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
2546 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
2547 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
2548 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2549 return 0;
2550}
2551
2552__isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *dim, int n,
2553 unsigned flags)
2554{
2555 struct isl_set *set;
2556
2557 if (!dim)
2558 return NULL;
2559 isl_assert(dim->ctx, dim->n_in == 0, goto error);
2560 isl_assert(dim->ctx, n >= 0, goto error);
2561 set = isl_alloc(dim->ctx, struct isl_set,
2562 sizeof(struct isl_set) +
2563 (n - 1) * sizeof(struct isl_basic_set *));
2564 if (!set)
2565 goto error;
2566
2567 set->ctx = dim->ctx;
2568 isl_ctx_ref(set->ctx);
2569 set->ref = 1;
2570 set->size = n;
2571 set->n = 0;
2572 set->dim = dim;
2573 set->flags = flags;
2574 return set;
2575error:
2576 isl_space_free(dim);
2577 return NULL;
2578}
2579
2580struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
2581 unsigned nparam, unsigned dim, int n, unsigned flags)
2582{
2583 struct isl_set *set;
2584 isl_space *dims;
2585
2586 dims = isl_space_alloc(ctx, nparam, 0, dim);
2587 if (!dims)
2588 return NULL;
2589
2590 set = isl_set_alloc_space(dims, n, flags);
2591 return set;
2592}
2593
2594/* Make sure "map" has room for at least "n" more basic maps.
2595 */
2596struct isl_map *isl_map_grow(struct isl_map *map, int n)
2597{
2598 int i;
2599 struct isl_map *grown = NULL;
2600
2601 if (!map)
2602 return NULL;
2603 isl_assert(map->ctx, n >= 0, goto error);
2604 if (map->n + n <= map->size)
2605 return map;
2606 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
2607 if (!grown)
2608 goto error;
2609 for (i = 0; i < map->n; ++i) {
2610 grown->p[i] = isl_basic_map_copy(map->p[i]);
2611 if (!grown->p[i])
2612 goto error;
2613 grown->n++;
2614 }
2615 isl_map_free(map);
2616 return grown;
2617error:
2618 isl_map_free(grown);
2619 isl_map_free(map);
2620 return NULL;
2621}
2622
2623/* Make sure "set" has room for at least "n" more basic sets.
2624 */
2625struct isl_set *isl_set_grow(struct isl_set *set, int n)
2626{
2627 return (struct isl_set *)isl_map_grow((struct isl_map *)set, n);
2628}
2629
2630struct isl_set *isl_set_dup(struct isl_set *set)
2631{
2632 int i;
2633 struct isl_set *dup;
2634
2635 if (!set)
2636 return NULL;
2637
2638 dup = isl_set_alloc_space(isl_space_copy(set->dim), set->n, set->flags);
2639 if (!dup)
2640 return NULL;
2641 for (i = 0; i < set->n; ++i)
2642 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
2643 return dup;
2644}
2645
2646struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
2647{
2648 return isl_map_from_basic_map(bset);
2649}
2650
2651struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
2652{
2653 struct isl_map *map;
2654
2655 if (!bmap)
2656 return NULL;
2657
2658 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
2659 return isl_map_add_basic_map(map, bmap);
2660}
2661
2662__isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
2663 __isl_take isl_basic_set *bset)
2664{
2665 return (struct isl_set *)isl_map_add_basic_map((struct isl_map *)set,
2666 (struct isl_basic_map *)bset);
2667}
2668
2669__isl_null isl_set *isl_set_free(__isl_take isl_set *set)
2670{
2671 int i;
2672
2673 if (!set)
2674 return NULL;
2675
2676 if (--set->ref > 0)
2677 return NULL;
2678
2679 isl_ctx_deref(set->ctx);
2680 for (i = 0; i < set->n; ++i)
2681 isl_basic_set_free(set->p[i]);
2682 isl_space_free(set->dim);
2683 free(set);
2684
2685 return NULL;
2686}
2687
2688void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
2689{
2690 int i;
2691
2692 if (!set) {
2693 fprintf(out, "null set\n");
2694 return;
2695 }
2696
2697 fprintf(out, "%*s", indent, "");
2698 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2699 set->ref, set->n, set->dim->nparam, set->dim->n_out,
2700 set->flags);
2701 for (i = 0; i < set->n; ++i) {
2702 fprintf(out, "%*s", indent, "");
2703 fprintf(out, "basic set %d:\n", i);
2704 isl_basic_set_print_internal(set->p[i], out, indent+4);
2705 }
2706}
2707
2708void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
2709{
2710 int i;
2711
2712 if (!map) {
2713 fprintf(out, "null map\n");
2714 return;
2715 }
2716
2717 fprintf(out, "%*s", indent, "");
2718 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2719 "flags: %x, n_name: %d\n",
2720 map->ref, map->n, map->dim->nparam, map->dim->n_in,
2721 map->dim->n_out, map->flags, map->dim->n_id);
2722 for (i = 0; i < map->n; ++i) {
2723 fprintf(out, "%*s", indent, "");
2724 fprintf(out, "basic map %d:\n", i);
2725 isl_basic_map_print_internal(map->p[i], out, indent+4);
2726 }
2727}
2728
2729struct isl_basic_map *isl_basic_map_intersect_domain(
2730 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2731{
2732 struct isl_basic_map *bmap_domain;
2733
2734 if (!bmap || !bset)
2735 goto error;
2736
2737 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2738 bset->dim, isl_dim_param), goto error);
2739
2740 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2741 isl_assert(bset->ctx,
2742 isl_basic_map_compatible_domain(bmap, bset), goto error);
2743
2744 bmap = isl_basic_map_cow(bmap);
2745 if (!bmap)
2746 goto error;
2747 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2748 bset->n_div, bset->n_eq, bset->n_ineq);
2749 bmap_domain = isl_basic_map_from_domain(bset);
2750 bmap = add_constraints(bmap, bmap_domain, 0, 0);
2751
2752 bmap = isl_basic_map_simplify(bmap);
2753 return isl_basic_map_finalize(bmap);
2754error:
2755 isl_basic_map_free(bmap);
2756 isl_basic_set_free(bset);
2757 return NULL;
2758}
2759
2760struct isl_basic_map *isl_basic_map_intersect_range(
2761 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2762{
2763 struct isl_basic_map *bmap_range;
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_range(bmap, bset), goto error);
2774
2775 if (isl_basic_set_is_universe(bset)) {
2776 isl_basic_set_free(bset);
2777 return bmap;
2778 }
2779
2780 bmap = isl_basic_map_cow(bmap);
2781 if (!bmap)
2782 goto error;
2783 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2784 bset->n_div, bset->n_eq, bset->n_ineq);
2785 bmap_range = isl_basic_map_from_basic_set(bset, isl_space_copy(bset->dim));
2786 bmap = add_constraints(bmap, bmap_range, 0, 0);
2787
2788 bmap = isl_basic_map_simplify(bmap);
2789 return isl_basic_map_finalize(bmap);
2790error:
2791 isl_basic_map_free(bmap);
2792 isl_basic_set_free(bset);
2793 return NULL;
2794}
2795
Tobias Grosserb2f39922015-05-28 13:32:11 +00002796isl_bool isl_basic_map_contains(__isl_keep isl_basic_map *bmap,
2797 __isl_keep isl_vec *vec)
Tobias Grosser52a25232015-02-04 20:55:43 +00002798{
2799 int i;
2800 unsigned total;
2801 isl_int s;
2802
2803 if (!bmap || !vec)
Tobias Grosserb2f39922015-05-28 13:32:11 +00002804 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00002805
2806 total = 1 + isl_basic_map_total_dim(bmap);
2807 if (total != vec->size)
Tobias Grosserb2f39922015-05-28 13:32:11 +00002808 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00002809
2810 isl_int_init(s);
2811
2812 for (i = 0; i < bmap->n_eq; ++i) {
2813 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
2814 if (!isl_int_is_zero(s)) {
2815 isl_int_clear(s);
Tobias Grosserb2f39922015-05-28 13:32:11 +00002816 return isl_bool_false;
Tobias Grosser52a25232015-02-04 20:55:43 +00002817 }
2818 }
2819
2820 for (i = 0; i < bmap->n_ineq; ++i) {
2821 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
2822 if (isl_int_is_neg(s)) {
2823 isl_int_clear(s);
Tobias Grosserb2f39922015-05-28 13:32:11 +00002824 return isl_bool_false;
Tobias Grosser52a25232015-02-04 20:55:43 +00002825 }
2826 }
2827
2828 isl_int_clear(s);
2829
Tobias Grosserb2f39922015-05-28 13:32:11 +00002830 return isl_bool_true;
Tobias Grosser52a25232015-02-04 20:55:43 +00002831}
2832
Tobias Grosserb2f39922015-05-28 13:32:11 +00002833isl_bool isl_basic_set_contains(__isl_keep isl_basic_set *bset,
2834 __isl_keep isl_vec *vec)
Tobias Grosser52a25232015-02-04 20:55:43 +00002835{
2836 return isl_basic_map_contains((struct isl_basic_map *)bset, vec);
2837}
2838
2839struct isl_basic_map *isl_basic_map_intersect(
2840 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2841{
2842 struct isl_vec *sample = NULL;
2843
2844 if (!bmap1 || !bmap2)
2845 goto error;
2846
2847 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
2848 bmap2->dim, isl_dim_param), goto error);
2849 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
2850 isl_space_dim(bmap1->dim, isl_dim_param) &&
2851 isl_space_dim(bmap2->dim, isl_dim_all) !=
2852 isl_space_dim(bmap2->dim, isl_dim_param))
2853 return isl_basic_map_intersect(bmap2, bmap1);
2854
2855 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
2856 isl_space_dim(bmap2->dim, isl_dim_param))
2857 isl_assert(bmap1->ctx,
2858 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
2859
2860 if (bmap1->sample &&
2861 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
2862 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
2863 sample = isl_vec_copy(bmap1->sample);
2864 else if (bmap2->sample &&
2865 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
2866 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
2867 sample = isl_vec_copy(bmap2->sample);
2868
2869 bmap1 = isl_basic_map_cow(bmap1);
2870 if (!bmap1)
2871 goto error;
2872 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
2873 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
2874 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
2875
2876 if (!bmap1)
2877 isl_vec_free(sample);
2878 else if (sample) {
2879 isl_vec_free(bmap1->sample);
2880 bmap1->sample = sample;
2881 }
2882
2883 bmap1 = isl_basic_map_simplify(bmap1);
2884 return isl_basic_map_finalize(bmap1);
2885error:
2886 if (sample)
2887 isl_vec_free(sample);
2888 isl_basic_map_free(bmap1);
2889 isl_basic_map_free(bmap2);
2890 return NULL;
2891}
2892
2893struct isl_basic_set *isl_basic_set_intersect(
2894 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
2895{
2896 return (struct isl_basic_set *)
2897 isl_basic_map_intersect(
2898 (struct isl_basic_map *)bset1,
2899 (struct isl_basic_map *)bset2);
2900}
2901
2902__isl_give isl_basic_set *isl_basic_set_intersect_params(
2903 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
2904{
2905 return isl_basic_set_intersect(bset1, bset2);
2906}
2907
2908/* Special case of isl_map_intersect, where both map1 and map2
2909 * are convex, without any divs and such that either map1 or map2
2910 * contains a single constraint. This constraint is then simply
2911 * added to the other map.
2912 */
2913static __isl_give isl_map *map_intersect_add_constraint(
2914 __isl_take isl_map *map1, __isl_take isl_map *map2)
2915{
2916 isl_assert(map1->ctx, map1->n == 1, goto error);
2917 isl_assert(map2->ctx, map1->n == 1, goto error);
2918 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
2919 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
2920
2921 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
2922 return isl_map_intersect(map2, map1);
2923
2924 isl_assert(map2->ctx,
2925 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
2926
2927 map1 = isl_map_cow(map1);
2928 if (!map1)
2929 goto error;
2930 if (isl_map_plain_is_empty(map1)) {
2931 isl_map_free(map2);
2932 return map1;
2933 }
2934 map1->p[0] = isl_basic_map_cow(map1->p[0]);
2935 if (map2->p[0]->n_eq == 1)
2936 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
2937 else
2938 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
2939 map2->p[0]->ineq[0]);
2940
2941 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
2942 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
2943 if (!map1->p[0])
2944 goto error;
2945
2946 if (isl_basic_map_plain_is_empty(map1->p[0])) {
2947 isl_basic_map_free(map1->p[0]);
2948 map1->n = 0;
2949 }
2950
2951 isl_map_free(map2);
2952
2953 return map1;
2954error:
2955 isl_map_free(map1);
2956 isl_map_free(map2);
2957 return NULL;
2958}
2959
2960/* map2 may be either a parameter domain or a map living in the same
2961 * space as map1.
2962 */
2963static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
2964 __isl_take isl_map *map2)
2965{
2966 unsigned flags = 0;
2967 isl_map *result;
2968 int i, j;
2969
2970 if (!map1 || !map2)
2971 goto error;
2972
2973 if ((isl_map_plain_is_empty(map1) ||
2974 isl_map_plain_is_universe(map2)) &&
2975 isl_space_is_equal(map1->dim, map2->dim)) {
2976 isl_map_free(map2);
2977 return map1;
2978 }
2979 if ((isl_map_plain_is_empty(map2) ||
2980 isl_map_plain_is_universe(map1)) &&
2981 isl_space_is_equal(map1->dim, map2->dim)) {
2982 isl_map_free(map1);
2983 return map2;
2984 }
2985
2986 if (map1->n == 1 && map2->n == 1 &&
2987 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
2988 isl_space_is_equal(map1->dim, map2->dim) &&
2989 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
2990 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
2991 return map_intersect_add_constraint(map1, map2);
2992
2993 if (isl_space_dim(map2->dim, isl_dim_all) !=
2994 isl_space_dim(map2->dim, isl_dim_param))
2995 isl_assert(map1->ctx,
2996 isl_space_is_equal(map1->dim, map2->dim), goto error);
2997
2998 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
2999 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3000 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3001
3002 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3003 map1->n * map2->n, flags);
3004 if (!result)
3005 goto error;
3006 for (i = 0; i < map1->n; ++i)
3007 for (j = 0; j < map2->n; ++j) {
3008 struct isl_basic_map *part;
3009 part = isl_basic_map_intersect(
3010 isl_basic_map_copy(map1->p[i]),
3011 isl_basic_map_copy(map2->p[j]));
3012 if (isl_basic_map_is_empty(part) < 0)
3013 part = isl_basic_map_free(part);
3014 result = isl_map_add_basic_map(result, part);
3015 if (!result)
3016 goto error;
3017 }
3018 isl_map_free(map1);
3019 isl_map_free(map2);
3020 return result;
3021error:
3022 isl_map_free(map1);
3023 isl_map_free(map2);
3024 return NULL;
3025}
3026
3027static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
3028 __isl_take isl_map *map2)
3029{
3030 if (!map1 || !map2)
3031 goto error;
3032 if (!isl_space_is_equal(map1->dim, map2->dim))
3033 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
3034 "spaces don't match", goto error);
3035 return map_intersect_internal(map1, map2);
3036error:
3037 isl_map_free(map1);
3038 isl_map_free(map2);
3039 return NULL;
3040}
3041
3042__isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
3043 __isl_take isl_map *map2)
3044{
3045 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
3046}
3047
3048struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
3049{
3050 return (struct isl_set *)
3051 isl_map_intersect((struct isl_map *)set1,
3052 (struct isl_map *)set2);
3053}
3054
3055/* map_intersect_internal accepts intersections
3056 * with parameter domains, so we can just call that function.
3057 */
3058static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
3059 __isl_take isl_set *params)
3060{
3061 return map_intersect_internal(map, params);
3062}
3063
3064__isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
3065 __isl_take isl_map *map2)
3066{
3067 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
3068}
3069
3070__isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
3071 __isl_take isl_set *params)
3072{
3073 return isl_map_intersect_params(set, params);
3074}
3075
3076struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
3077{
Tobias Grosserb2f39922015-05-28 13:32:11 +00003078 isl_space *space;
3079 unsigned pos, n1, n2;
Tobias Grosser52a25232015-02-04 20:55:43 +00003080
3081 if (!bmap)
3082 return NULL;
3083 bmap = isl_basic_map_cow(bmap);
3084 if (!bmap)
3085 return NULL;
Tobias Grosserb2f39922015-05-28 13:32:11 +00003086 space = isl_space_reverse(isl_space_copy(bmap->dim));
3087 pos = isl_basic_map_offset(bmap, isl_dim_in);
3088 n1 = isl_basic_map_dim(bmap, isl_dim_in);
3089 n2 = isl_basic_map_dim(bmap, isl_dim_out);
3090 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
3091 return isl_basic_map_reset_space(bmap, space);
Tobias Grosser52a25232015-02-04 20:55:43 +00003092}
3093
3094static __isl_give isl_basic_map *basic_map_space_reset(
3095 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
3096{
3097 isl_space *space;
3098
3099 if (!bmap)
3100 return NULL;
3101 if (!isl_space_is_named_or_nested(bmap->dim, type))
3102 return bmap;
3103
3104 space = isl_basic_map_get_space(bmap);
3105 space = isl_space_reset(space, type);
3106 bmap = isl_basic_map_reset_space(bmap, space);
3107 return bmap;
3108}
3109
3110__isl_give isl_basic_map *isl_basic_map_insert_dims(
3111 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3112 unsigned pos, unsigned n)
3113{
3114 isl_space *res_dim;
3115 struct isl_basic_map *res;
3116 struct isl_dim_map *dim_map;
3117 unsigned total, off;
3118 enum isl_dim_type t;
3119
3120 if (n == 0)
3121 return basic_map_space_reset(bmap, type);
3122
3123 if (!bmap)
3124 return NULL;
3125
3126 res_dim = isl_space_insert_dims(isl_basic_map_get_space(bmap), type, pos, n);
3127
3128 total = isl_basic_map_total_dim(bmap) + n;
3129 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3130 off = 0;
3131 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3132 if (t != type) {
3133 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3134 } else {
3135 unsigned size = isl_basic_map_dim(bmap, t);
3136 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3137 0, pos, off);
3138 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3139 pos, size - pos, off + pos + n);
3140 }
3141 off += isl_space_dim(res_dim, t);
3142 }
3143 isl_dim_map_div(dim_map, bmap, off);
3144
3145 res = isl_basic_map_alloc_space(res_dim,
3146 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3147 if (isl_basic_map_is_rational(bmap))
3148 res = isl_basic_map_set_rational(res);
3149 if (isl_basic_map_plain_is_empty(bmap)) {
3150 isl_basic_map_free(bmap);
3151 free(dim_map);
3152 return isl_basic_map_set_to_empty(res);
3153 }
3154 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3155 return isl_basic_map_finalize(res);
3156}
3157
3158__isl_give isl_basic_set *isl_basic_set_insert_dims(
3159 __isl_take isl_basic_set *bset,
3160 enum isl_dim_type type, unsigned pos, unsigned n)
3161{
3162 return isl_basic_map_insert_dims(bset, type, pos, n);
3163}
3164
Tobias Grossere0f8d592015-05-13 13:10:13 +00003165__isl_give isl_basic_map *isl_basic_map_add_dims(__isl_take isl_basic_map *bmap,
Tobias Grosser52a25232015-02-04 20:55:43 +00003166 enum isl_dim_type type, unsigned n)
3167{
3168 if (!bmap)
3169 return NULL;
3170 return isl_basic_map_insert_dims(bmap, type,
3171 isl_basic_map_dim(bmap, type), n);
3172}
3173
3174__isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
3175 enum isl_dim_type type, unsigned n)
3176{
3177 if (!bset)
3178 return NULL;
3179 isl_assert(bset->ctx, type != isl_dim_in, goto error);
Tobias Grossere0f8d592015-05-13 13:10:13 +00003180 return isl_basic_map_add_dims(bset, type, n);
Tobias Grosser52a25232015-02-04 20:55:43 +00003181error:
3182 isl_basic_set_free(bset);
3183 return NULL;
3184}
3185
3186static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
3187 enum isl_dim_type type)
3188{
3189 isl_space *space;
3190
3191 if (!map || !isl_space_is_named_or_nested(map->dim, type))
3192 return map;
3193
3194 space = isl_map_get_space(map);
3195 space = isl_space_reset(space, type);
3196 map = isl_map_reset_space(map, space);
3197 return map;
3198}
3199
3200__isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
3201 enum isl_dim_type type, unsigned pos, unsigned n)
3202{
3203 int i;
3204
3205 if (n == 0)
3206 return map_space_reset(map, type);
3207
3208 map = isl_map_cow(map);
3209 if (!map)
3210 return NULL;
3211
3212 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
3213 if (!map->dim)
3214 goto error;
3215
3216 for (i = 0; i < map->n; ++i) {
3217 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
3218 if (!map->p[i])
3219 goto error;
3220 }
3221
3222 return map;
3223error:
3224 isl_map_free(map);
3225 return NULL;
3226}
3227
3228__isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
3229 enum isl_dim_type type, unsigned pos, unsigned n)
3230{
3231 return isl_map_insert_dims(set, type, pos, n);
3232}
3233
3234__isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
3235 enum isl_dim_type type, unsigned n)
3236{
3237 if (!map)
3238 return NULL;
3239 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
3240}
3241
3242__isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
3243 enum isl_dim_type type, unsigned n)
3244{
3245 if (!set)
3246 return NULL;
3247 isl_assert(set->ctx, type != isl_dim_in, goto error);
3248 return (isl_set *)isl_map_add_dims((isl_map *)set, type, n);
3249error:
3250 isl_set_free(set);
3251 return NULL;
3252}
3253
3254__isl_give isl_basic_map *isl_basic_map_move_dims(
3255 __isl_take isl_basic_map *bmap,
3256 enum isl_dim_type dst_type, unsigned dst_pos,
3257 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3258{
3259 struct isl_dim_map *dim_map;
3260 struct isl_basic_map *res;
3261 enum isl_dim_type t;
3262 unsigned total, off;
3263
3264 if (!bmap)
3265 return NULL;
3266 if (n == 0)
3267 return bmap;
3268
3269 isl_assert(bmap->ctx, src_pos + n <= isl_basic_map_dim(bmap, src_type),
3270 goto error);
3271
3272 if (dst_type == src_type && dst_pos == src_pos)
3273 return bmap;
3274
3275 isl_assert(bmap->ctx, dst_type != src_type, goto error);
3276
3277 if (pos(bmap->dim, dst_type) + dst_pos ==
3278 pos(bmap->dim, src_type) + src_pos +
3279 ((src_type < dst_type) ? n : 0)) {
3280 bmap = isl_basic_map_cow(bmap);
3281 if (!bmap)
3282 return NULL;
3283
3284 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3285 src_type, src_pos, n);
3286 if (!bmap->dim)
3287 goto error;
3288
3289 bmap = isl_basic_map_finalize(bmap);
3290
3291 return bmap;
3292 }
3293
3294 total = isl_basic_map_total_dim(bmap);
3295 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3296
3297 off = 0;
3298 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3299 unsigned size = isl_space_dim(bmap->dim, t);
3300 if (t == dst_type) {
3301 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3302 0, dst_pos, off);
3303 off += dst_pos;
3304 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
3305 src_pos, n, off);
3306 off += n;
3307 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3308 dst_pos, size - dst_pos, off);
3309 off += size - dst_pos;
3310 } else if (t == src_type) {
3311 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3312 0, src_pos, off);
3313 off += src_pos;
3314 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3315 src_pos + n, size - src_pos - n, off);
3316 off += size - src_pos - n;
3317 } else {
3318 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3319 off += size;
3320 }
3321 }
3322 isl_dim_map_div(dim_map, bmap, off);
3323
3324 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3325 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3326 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3327 if (!bmap)
3328 goto error;
3329
3330 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3331 src_type, src_pos, n);
3332 if (!bmap->dim)
3333 goto error;
3334
3335 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
3336 bmap = isl_basic_map_gauss(bmap, NULL);
3337 bmap = isl_basic_map_finalize(bmap);
3338
3339 return bmap;
3340error:
3341 isl_basic_map_free(bmap);
3342 return NULL;
3343}
3344
3345__isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
3346 enum isl_dim_type dst_type, unsigned dst_pos,
3347 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3348{
3349 return (isl_basic_set *)isl_basic_map_move_dims(
3350 (isl_basic_map *)bset, dst_type, dst_pos, src_type, src_pos, n);
3351}
3352
3353__isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
3354 enum isl_dim_type dst_type, unsigned dst_pos,
3355 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3356{
3357 if (!set)
3358 return NULL;
3359 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
3360 return (isl_set *)isl_map_move_dims((isl_map *)set, dst_type, dst_pos,
3361 src_type, src_pos, n);
3362error:
3363 isl_set_free(set);
3364 return NULL;
3365}
3366
3367__isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
3368 enum isl_dim_type dst_type, unsigned dst_pos,
3369 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3370{
3371 int i;
3372
3373 if (!map)
3374 return NULL;
3375 if (n == 0)
3376 return map;
3377
3378 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
3379 goto error);
3380
3381 if (dst_type == src_type && dst_pos == src_pos)
3382 return map;
3383
3384 isl_assert(map->ctx, dst_type != src_type, goto error);
3385
3386 map = isl_map_cow(map);
3387 if (!map)
3388 return NULL;
3389
3390 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
3391 if (!map->dim)
3392 goto error;
3393
3394 for (i = 0; i < map->n; ++i) {
3395 map->p[i] = isl_basic_map_move_dims(map->p[i],
3396 dst_type, dst_pos,
3397 src_type, src_pos, n);
3398 if (!map->p[i])
3399 goto error;
3400 }
3401
3402 return map;
3403error:
3404 isl_map_free(map);
3405 return NULL;
3406}
3407
3408/* Move the specified dimensions to the last columns right before
3409 * the divs. Don't change the dimension specification of bmap.
3410 * That's the responsibility of the caller.
3411 */
3412static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
3413 enum isl_dim_type type, unsigned first, unsigned n)
3414{
3415 struct isl_dim_map *dim_map;
3416 struct isl_basic_map *res;
3417 enum isl_dim_type t;
3418 unsigned total, off;
3419
3420 if (!bmap)
3421 return NULL;
3422 if (pos(bmap->dim, type) + first + n ==
3423 1 + isl_space_dim(bmap->dim, isl_dim_all))
3424 return bmap;
3425
3426 total = isl_basic_map_total_dim(bmap);
3427 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3428
3429 off = 0;
3430 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3431 unsigned size = isl_space_dim(bmap->dim, t);
3432 if (t == type) {
3433 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3434 0, first, off);
3435 off += first;
3436 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3437 first, n, total - bmap->n_div - n);
3438 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3439 first + n, size - (first + n), off);
3440 off += size - (first + n);
3441 } else {
3442 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3443 off += size;
3444 }
3445 }
3446 isl_dim_map_div(dim_map, bmap, off + n);
3447
3448 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3449 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3450 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3451 return res;
3452}
3453
3454/* Insert "n" rows in the divs of "bmap".
3455 *
3456 * The number of columns is not changed, which means that the last
3457 * dimensions of "bmap" are being reintepreted as the new divs.
3458 * The space of "bmap" is not adjusted, however, which means
3459 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
3460 * from the space of "bmap" is the responsibility of the caller.
3461 */
3462static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
3463 int n)
3464{
3465 int i;
3466 size_t row_size;
3467 isl_int **new_div;
3468 isl_int *old;
3469
3470 bmap = isl_basic_map_cow(bmap);
3471 if (!bmap)
3472 return NULL;
3473
3474 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
3475 old = bmap->block2.data;
3476 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
3477 (bmap->extra + n) * (1 + row_size));
3478 if (!bmap->block2.data)
3479 return isl_basic_map_free(bmap);
3480 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
3481 if (!new_div)
3482 return isl_basic_map_free(bmap);
3483 for (i = 0; i < n; ++i) {
3484 new_div[i] = bmap->block2.data +
3485 (bmap->extra + i) * (1 + row_size);
3486 isl_seq_clr(new_div[i], 1 + row_size);
3487 }
3488 for (i = 0; i < bmap->extra; ++i)
3489 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
3490 free(bmap->div);
3491 bmap->div = new_div;
3492 bmap->n_div += n;
3493 bmap->extra += n;
3494
3495 return bmap;
3496}
3497
3498/* Turn the n dimensions of type type, starting at first
3499 * into existentially quantified variables.
3500 */
3501__isl_give isl_basic_map *isl_basic_map_project_out(
3502 __isl_take isl_basic_map *bmap,
3503 enum isl_dim_type type, unsigned first, unsigned n)
3504{
3505 if (n == 0)
3506 return basic_map_space_reset(bmap, type);
3507
3508 if (!bmap)
3509 return NULL;
3510
3511 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
3512 return isl_basic_map_remove_dims(bmap, type, first, n);
3513
3514 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
3515 goto error);
3516
3517 bmap = move_last(bmap, type, first, n);
3518 bmap = isl_basic_map_cow(bmap);
3519 bmap = insert_div_rows(bmap, n);
3520 if (!bmap)
3521 return NULL;
3522
3523 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
3524 if (!bmap->dim)
3525 goto error;
3526 bmap = isl_basic_map_simplify(bmap);
3527 bmap = isl_basic_map_drop_redundant_divs(bmap);
3528 return isl_basic_map_finalize(bmap);
3529error:
3530 isl_basic_map_free(bmap);
3531 return NULL;
3532}
3533
3534/* Turn the n dimensions of type type, starting at first
3535 * into existentially quantified variables.
3536 */
3537struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
3538 enum isl_dim_type type, unsigned first, unsigned n)
3539{
3540 return (isl_basic_set *)isl_basic_map_project_out(
3541 (isl_basic_map *)bset, type, first, n);
3542}
3543
3544/* Turn the n dimensions of type type, starting at first
3545 * into existentially quantified variables.
3546 */
3547__isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
3548 enum isl_dim_type type, unsigned first, unsigned n)
3549{
3550 int i;
3551
3552 if (!map)
3553 return NULL;
3554
3555 if (n == 0)
3556 return map_space_reset(map, type);
3557
3558 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
3559
3560 map = isl_map_cow(map);
3561 if (!map)
3562 return NULL;
3563
3564 map->dim = isl_space_drop_dims(map->dim, type, first, n);
3565 if (!map->dim)
3566 goto error;
3567
3568 for (i = 0; i < map->n; ++i) {
3569 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
3570 if (!map->p[i])
3571 goto error;
3572 }
3573
3574 return map;
3575error:
3576 isl_map_free(map);
3577 return NULL;
3578}
3579
3580/* Turn the n dimensions of type type, starting at first
3581 * into existentially quantified variables.
3582 */
3583__isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
3584 enum isl_dim_type type, unsigned first, unsigned n)
3585{
3586 return (isl_set *)isl_map_project_out((isl_map *)set, type, first, n);
3587}
3588
3589static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
3590{
3591 int i, j;
3592
3593 for (i = 0; i < n; ++i) {
3594 j = isl_basic_map_alloc_div(bmap);
3595 if (j < 0)
3596 goto error;
3597 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
3598 }
3599 return bmap;
3600error:
3601 isl_basic_map_free(bmap);
3602 return NULL;
3603}
3604
3605struct isl_basic_map *isl_basic_map_apply_range(
3606 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3607{
3608 isl_space *dim_result = NULL;
3609 struct isl_basic_map *bmap;
3610 unsigned n_in, n_out, n, nparam, total, pos;
3611 struct isl_dim_map *dim_map1, *dim_map2;
3612
3613 if (!bmap1 || !bmap2)
3614 goto error;
3615 if (!isl_space_match(bmap1->dim, isl_dim_param,
3616 bmap2->dim, isl_dim_param))
3617 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
3618 "parameters don't match", goto error);
3619 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_out,
3620 bmap2->dim, isl_dim_in))
3621 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
3622 "spaces don't match", goto error);
3623
3624 dim_result = isl_space_join(isl_space_copy(bmap1->dim),
3625 isl_space_copy(bmap2->dim));
3626
3627 n_in = isl_basic_map_n_in(bmap1);
3628 n_out = isl_basic_map_n_out(bmap2);
3629 n = isl_basic_map_n_out(bmap1);
3630 nparam = isl_basic_map_n_param(bmap1);
3631
3632 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
3633 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3634 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
3635 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3636 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
3637 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3638 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
3639 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
3640 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3641 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3642 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3643
3644 bmap = isl_basic_map_alloc_space(dim_result,
3645 bmap1->n_div + bmap2->n_div + n,
3646 bmap1->n_eq + bmap2->n_eq,
3647 bmap1->n_ineq + bmap2->n_ineq);
3648 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3649 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3650 bmap = add_divs(bmap, n);
3651 bmap = isl_basic_map_simplify(bmap);
3652 bmap = isl_basic_map_drop_redundant_divs(bmap);
3653 return isl_basic_map_finalize(bmap);
3654error:
3655 isl_basic_map_free(bmap1);
3656 isl_basic_map_free(bmap2);
3657 return NULL;
3658}
3659
3660struct isl_basic_set *isl_basic_set_apply(
3661 struct isl_basic_set *bset, struct isl_basic_map *bmap)
3662{
3663 if (!bset || !bmap)
3664 goto error;
3665
3666 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
3667 goto error);
3668
3669 return (struct isl_basic_set *)
3670 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
3671error:
3672 isl_basic_set_free(bset);
3673 isl_basic_map_free(bmap);
3674 return NULL;
3675}
3676
3677struct isl_basic_map *isl_basic_map_apply_domain(
3678 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3679{
3680 if (!bmap1 || !bmap2)
3681 goto error;
3682
3683 isl_assert(bmap1->ctx,
3684 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
3685 isl_assert(bmap1->ctx,
3686 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
3687 goto error);
3688
3689 bmap1 = isl_basic_map_reverse(bmap1);
3690 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
3691 return isl_basic_map_reverse(bmap1);
3692error:
3693 isl_basic_map_free(bmap1);
3694 isl_basic_map_free(bmap2);
3695 return NULL;
3696}
3697
3698/* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3699 * A \cap B -> f(A) + f(B)
3700 */
3701struct isl_basic_map *isl_basic_map_sum(
3702 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3703{
3704 unsigned n_in, n_out, nparam, total, pos;
3705 struct isl_basic_map *bmap = NULL;
3706 struct isl_dim_map *dim_map1, *dim_map2;
3707 int i;
3708
3709 if (!bmap1 || !bmap2)
3710 goto error;
3711
3712 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
3713 goto error);
3714
3715 nparam = isl_basic_map_n_param(bmap1);
3716 n_in = isl_basic_map_n_in(bmap1);
3717 n_out = isl_basic_map_n_out(bmap1);
3718
3719 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
3720 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3721 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
3722 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3723 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
3724 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3725 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3726 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
3727 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3728 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3729 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
3730
3731 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
3732 bmap1->n_div + bmap2->n_div + 2 * n_out,
3733 bmap1->n_eq + bmap2->n_eq + n_out,
3734 bmap1->n_ineq + bmap2->n_ineq);
3735 for (i = 0; i < n_out; ++i) {
3736 int j = isl_basic_map_alloc_equality(bmap);
3737 if (j < 0)
3738 goto error;
3739 isl_seq_clr(bmap->eq[j], 1+total);
3740 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
3741 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
3742 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
3743 }
3744 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3745 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3746 bmap = add_divs(bmap, 2 * n_out);
3747
3748 bmap = isl_basic_map_simplify(bmap);
3749 return isl_basic_map_finalize(bmap);
3750error:
3751 isl_basic_map_free(bmap);
3752 isl_basic_map_free(bmap1);
3753 isl_basic_map_free(bmap2);
3754 return NULL;
3755}
3756
3757/* Given two maps A -> f(A) and B -> g(B), construct a map
3758 * A \cap B -> f(A) + f(B)
3759 */
3760struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
3761{
3762 struct isl_map *result;
3763 int i, j;
3764
3765 if (!map1 || !map2)
3766 goto error;
3767
3768 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
3769
3770 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3771 map1->n * map2->n, 0);
3772 if (!result)
3773 goto error;
3774 for (i = 0; i < map1->n; ++i)
3775 for (j = 0; j < map2->n; ++j) {
3776 struct isl_basic_map *part;
3777 part = isl_basic_map_sum(
3778 isl_basic_map_copy(map1->p[i]),
3779 isl_basic_map_copy(map2->p[j]));
3780 if (isl_basic_map_is_empty(part))
3781 isl_basic_map_free(part);
3782 else
3783 result = isl_map_add_basic_map(result, part);
3784 if (!result)
3785 goto error;
3786 }
3787 isl_map_free(map1);
3788 isl_map_free(map2);
3789 return result;
3790error:
3791 isl_map_free(map1);
3792 isl_map_free(map2);
3793 return NULL;
3794}
3795
3796__isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
3797 __isl_take isl_set *set2)
3798{
3799 return (isl_set *)isl_map_sum((isl_map *)set1, (isl_map *)set2);
3800}
3801
3802/* Given a basic map A -> f(A), construct A -> -f(A).
3803 */
3804struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
3805{
3806 int i, j;
3807 unsigned off, n;
3808
3809 bmap = isl_basic_map_cow(bmap);
3810 if (!bmap)
3811 return NULL;
3812
3813 n = isl_basic_map_dim(bmap, isl_dim_out);
3814 off = isl_basic_map_offset(bmap, isl_dim_out);
3815 for (i = 0; i < bmap->n_eq; ++i)
3816 for (j = 0; j < n; ++j)
3817 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
3818 for (i = 0; i < bmap->n_ineq; ++i)
3819 for (j = 0; j < n; ++j)
3820 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
3821 for (i = 0; i < bmap->n_div; ++i)
3822 for (j = 0; j < n; ++j)
3823 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
3824 bmap = isl_basic_map_gauss(bmap, NULL);
3825 return isl_basic_map_finalize(bmap);
3826}
3827
3828__isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
3829{
3830 return isl_basic_map_neg(bset);
3831}
3832
3833/* Given a map A -> f(A), construct A -> -f(A).
3834 */
3835struct isl_map *isl_map_neg(struct isl_map *map)
3836{
3837 int i;
3838
3839 map = isl_map_cow(map);
3840 if (!map)
3841 return NULL;
3842
3843 for (i = 0; i < map->n; ++i) {
3844 map->p[i] = isl_basic_map_neg(map->p[i]);
3845 if (!map->p[i])
3846 goto error;
3847 }
3848
3849 return map;
3850error:
3851 isl_map_free(map);
3852 return NULL;
3853}
3854
3855__isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
3856{
3857 return (isl_set *)isl_map_neg((isl_map *)set);
3858}
3859
3860/* Given a basic map A -> f(A) and an integer d, construct a basic map
3861 * A -> floor(f(A)/d).
3862 */
3863struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
3864 isl_int d)
3865{
3866 unsigned n_in, n_out, nparam, total, pos;
3867 struct isl_basic_map *result = NULL;
3868 struct isl_dim_map *dim_map;
3869 int i;
3870
3871 if (!bmap)
3872 return NULL;
3873
3874 nparam = isl_basic_map_n_param(bmap);
3875 n_in = isl_basic_map_n_in(bmap);
3876 n_out = isl_basic_map_n_out(bmap);
3877
3878 total = nparam + n_in + n_out + bmap->n_div + n_out;
3879 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3880 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
3881 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
3882 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
3883 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
3884
3885 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
3886 bmap->n_div + n_out,
3887 bmap->n_eq, bmap->n_ineq + 2 * n_out);
3888 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
3889 result = add_divs(result, n_out);
3890 for (i = 0; i < n_out; ++i) {
3891 int j;
3892 j = isl_basic_map_alloc_inequality(result);
3893 if (j < 0)
3894 goto error;
3895 isl_seq_clr(result->ineq[j], 1+total);
3896 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
3897 isl_int_set_si(result->ineq[j][1+pos+i], 1);
3898 j = isl_basic_map_alloc_inequality(result);
3899 if (j < 0)
3900 goto error;
3901 isl_seq_clr(result->ineq[j], 1+total);
3902 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
3903 isl_int_set_si(result->ineq[j][1+pos+i], -1);
3904 isl_int_sub_ui(result->ineq[j][0], d, 1);
3905 }
3906
3907 result = isl_basic_map_simplify(result);
3908 return isl_basic_map_finalize(result);
3909error:
3910 isl_basic_map_free(result);
3911 return NULL;
3912}
3913
3914/* Given a map A -> f(A) and an integer d, construct a map
3915 * A -> floor(f(A)/d).
3916 */
3917struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
3918{
3919 int i;
3920
3921 map = isl_map_cow(map);
3922 if (!map)
3923 return NULL;
3924
3925 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3926 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3927 for (i = 0; i < map->n; ++i) {
3928 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
3929 if (!map->p[i])
3930 goto error;
3931 }
3932
3933 return map;
3934error:
3935 isl_map_free(map);
3936 return NULL;
3937}
3938
3939/* Given a map A -> f(A) and an integer d, construct a map
3940 * A -> floor(f(A)/d).
3941 */
3942__isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
3943 __isl_take isl_val *d)
3944{
3945 if (!map || !d)
3946 goto error;
3947 if (!isl_val_is_int(d))
3948 isl_die(isl_val_get_ctx(d), isl_error_invalid,
3949 "expecting integer denominator", goto error);
3950 map = isl_map_floordiv(map, d->n);
3951 isl_val_free(d);
3952 return map;
3953error:
3954 isl_map_free(map);
3955 isl_val_free(d);
3956 return NULL;
3957}
3958
3959static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
3960{
3961 int i;
3962 unsigned nparam;
3963 unsigned n_in;
3964
3965 i = isl_basic_map_alloc_equality(bmap);
3966 if (i < 0)
3967 goto error;
3968 nparam = isl_basic_map_n_param(bmap);
3969 n_in = isl_basic_map_n_in(bmap);
3970 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
3971 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
3972 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
3973 return isl_basic_map_finalize(bmap);
3974error:
3975 isl_basic_map_free(bmap);
3976 return NULL;
3977}
3978
3979/* Add a constraints to "bmap" expressing i_pos < o_pos
3980 */
3981static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
3982{
3983 int i;
3984 unsigned nparam;
3985 unsigned n_in;
3986
3987 i = isl_basic_map_alloc_inequality(bmap);
3988 if (i < 0)
3989 goto error;
3990 nparam = isl_basic_map_n_param(bmap);
3991 n_in = isl_basic_map_n_in(bmap);
3992 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3993 isl_int_set_si(bmap->ineq[i][0], -1);
3994 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3995 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3996 return isl_basic_map_finalize(bmap);
3997error:
3998 isl_basic_map_free(bmap);
3999 return NULL;
4000}
4001
4002/* Add a constraint to "bmap" expressing i_pos <= o_pos
4003 */
4004static __isl_give isl_basic_map *var_less_or_equal(
4005 __isl_take isl_basic_map *bmap, unsigned pos)
4006{
4007 int i;
4008 unsigned nparam;
4009 unsigned n_in;
4010
4011 i = isl_basic_map_alloc_inequality(bmap);
4012 if (i < 0)
4013 goto error;
4014 nparam = isl_basic_map_n_param(bmap);
4015 n_in = isl_basic_map_n_in(bmap);
4016 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4017 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4018 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4019 return isl_basic_map_finalize(bmap);
4020error:
4021 isl_basic_map_free(bmap);
4022 return NULL;
4023}
4024
4025/* Add a constraints to "bmap" expressing i_pos > o_pos
4026 */
4027static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
4028{
4029 int i;
4030 unsigned nparam;
4031 unsigned n_in;
4032
4033 i = isl_basic_map_alloc_inequality(bmap);
4034 if (i < 0)
4035 goto error;
4036 nparam = isl_basic_map_n_param(bmap);
4037 n_in = isl_basic_map_n_in(bmap);
4038 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4039 isl_int_set_si(bmap->ineq[i][0], -1);
4040 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4041 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4042 return isl_basic_map_finalize(bmap);
4043error:
4044 isl_basic_map_free(bmap);
4045 return NULL;
4046}
4047
4048/* Add a constraint to "bmap" expressing i_pos >= o_pos
4049 */
4050static __isl_give isl_basic_map *var_more_or_equal(
4051 __isl_take isl_basic_map *bmap, unsigned pos)
4052{
4053 int i;
4054 unsigned nparam;
4055 unsigned n_in;
4056
4057 i = isl_basic_map_alloc_inequality(bmap);
4058 if (i < 0)
4059 goto error;
4060 nparam = isl_basic_map_n_param(bmap);
4061 n_in = isl_basic_map_n_in(bmap);
4062 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4063 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4064 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4065 return isl_basic_map_finalize(bmap);
4066error:
4067 isl_basic_map_free(bmap);
4068 return NULL;
4069}
4070
4071__isl_give isl_basic_map *isl_basic_map_equal(
4072 __isl_take isl_space *dim, unsigned n_equal)
4073{
4074 int i;
4075 struct isl_basic_map *bmap;
4076 bmap = isl_basic_map_alloc_space(dim, 0, n_equal, 0);
4077 if (!bmap)
4078 return NULL;
4079 for (i = 0; i < n_equal && bmap; ++i)
4080 bmap = var_equal(bmap, i);
4081 return isl_basic_map_finalize(bmap);
4082}
4083
4084/* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4085 */
4086__isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *dim,
4087 unsigned pos)
4088{
4089 int i;
4090 struct isl_basic_map *bmap;
4091 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4092 if (!bmap)
4093 return NULL;
4094 for (i = 0; i < pos && bmap; ++i)
4095 bmap = var_equal(bmap, i);
4096 if (bmap)
4097 bmap = var_less(bmap, pos);
4098 return isl_basic_map_finalize(bmap);
4099}
4100
4101/* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
4102 */
4103__isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
4104 __isl_take isl_space *dim, unsigned pos)
4105{
4106 int i;
4107 isl_basic_map *bmap;
4108
4109 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4110 for (i = 0; i < pos; ++i)
4111 bmap = var_equal(bmap, i);
4112 bmap = var_less_or_equal(bmap, pos);
4113 return isl_basic_map_finalize(bmap);
4114}
4115
4116/* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
4117 */
4118__isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *dim,
4119 unsigned pos)
4120{
4121 int i;
4122 struct isl_basic_map *bmap;
4123 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4124 if (!bmap)
4125 return NULL;
4126 for (i = 0; i < pos && bmap; ++i)
4127 bmap = var_equal(bmap, i);
4128 if (bmap)
4129 bmap = var_more(bmap, pos);
4130 return isl_basic_map_finalize(bmap);
4131}
4132
4133/* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
4134 */
4135__isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
4136 __isl_take isl_space *dim, unsigned pos)
4137{
4138 int i;
4139 isl_basic_map *bmap;
4140
4141 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4142 for (i = 0; i < pos; ++i)
4143 bmap = var_equal(bmap, i);
4144 bmap = var_more_or_equal(bmap, pos);
4145 return isl_basic_map_finalize(bmap);
4146}
4147
4148static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *dims,
4149 unsigned n, int equal)
4150{
4151 struct isl_map *map;
4152 int i;
4153
4154 if (n == 0 && equal)
4155 return isl_map_universe(dims);
4156
4157 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4158
4159 for (i = 0; i + 1 < n; ++i)
4160 map = isl_map_add_basic_map(map,
4161 isl_basic_map_less_at(isl_space_copy(dims), i));
4162 if (n > 0) {
4163 if (equal)
4164 map = isl_map_add_basic_map(map,
4165 isl_basic_map_less_or_equal_at(dims, n - 1));
4166 else
4167 map = isl_map_add_basic_map(map,
4168 isl_basic_map_less_at(dims, n - 1));
4169 } else
4170 isl_space_free(dims);
4171
4172 return map;
4173}
4174
4175static __isl_give isl_map *map_lex_lte(__isl_take isl_space *dims, int equal)
4176{
4177 if (!dims)
4178 return NULL;
4179 return map_lex_lte_first(dims, dims->n_out, equal);
4180}
4181
4182__isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
4183{
4184 return map_lex_lte_first(dim, n, 0);
4185}
4186
4187__isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
4188{
4189 return map_lex_lte_first(dim, n, 1);
4190}
4191
4192__isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
4193{
4194 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
4195}
4196
4197__isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
4198{
4199 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
4200}
4201
4202static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *dims,
4203 unsigned n, int equal)
4204{
4205 struct isl_map *map;
4206 int i;
4207
4208 if (n == 0 && equal)
4209 return isl_map_universe(dims);
4210
4211 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4212
4213 for (i = 0; i + 1 < n; ++i)
4214 map = isl_map_add_basic_map(map,
4215 isl_basic_map_more_at(isl_space_copy(dims), i));
4216 if (n > 0) {
4217 if (equal)
4218 map = isl_map_add_basic_map(map,
4219 isl_basic_map_more_or_equal_at(dims, n - 1));
4220 else
4221 map = isl_map_add_basic_map(map,
4222 isl_basic_map_more_at(dims, n - 1));
4223 } else
4224 isl_space_free(dims);
4225
4226 return map;
4227}
4228
4229static __isl_give isl_map *map_lex_gte(__isl_take isl_space *dims, int equal)
4230{
4231 if (!dims)
4232 return NULL;
4233 return map_lex_gte_first(dims, dims->n_out, equal);
4234}
4235
4236__isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
4237{
4238 return map_lex_gte_first(dim, n, 0);
4239}
4240
4241__isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
4242{
4243 return map_lex_gte_first(dim, n, 1);
4244}
4245
4246__isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
4247{
4248 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
4249}
4250
4251__isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
4252{
4253 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
4254}
4255
4256__isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
4257 __isl_take isl_set *set2)
4258{
4259 isl_map *map;
4260 map = isl_map_lex_le(isl_set_get_space(set1));
4261 map = isl_map_intersect_domain(map, set1);
4262 map = isl_map_intersect_range(map, set2);
4263 return map;
4264}
4265
4266__isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
4267 __isl_take isl_set *set2)
4268{
4269 isl_map *map;
4270 map = isl_map_lex_lt(isl_set_get_space(set1));
4271 map = isl_map_intersect_domain(map, set1);
4272 map = isl_map_intersect_range(map, set2);
4273 return map;
4274}
4275
4276__isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
4277 __isl_take isl_set *set2)
4278{
4279 isl_map *map;
4280 map = isl_map_lex_ge(isl_set_get_space(set1));
4281 map = isl_map_intersect_domain(map, set1);
4282 map = isl_map_intersect_range(map, set2);
4283 return map;
4284}
4285
4286__isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
4287 __isl_take isl_set *set2)
4288{
4289 isl_map *map;
4290 map = isl_map_lex_gt(isl_set_get_space(set1));
4291 map = isl_map_intersect_domain(map, set1);
4292 map = isl_map_intersect_range(map, set2);
4293 return map;
4294}
4295
4296__isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
4297 __isl_take isl_map *map2)
4298{
4299 isl_map *map;
4300 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
4301 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4302 map = isl_map_apply_range(map, isl_map_reverse(map2));
4303 return map;
4304}
4305
4306__isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
4307 __isl_take isl_map *map2)
4308{
4309 isl_map *map;
4310 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
4311 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4312 map = isl_map_apply_range(map, isl_map_reverse(map2));
4313 return map;
4314}
4315
4316__isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
4317 __isl_take isl_map *map2)
4318{
4319 isl_map *map;
4320 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
4321 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4322 map = isl_map_apply_range(map, isl_map_reverse(map2));
4323 return map;
4324}
4325
4326__isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
4327 __isl_take isl_map *map2)
4328{
4329 isl_map *map;
4330 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
4331 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4332 map = isl_map_apply_range(map, isl_map_reverse(map2));
4333 return map;
4334}
4335
4336__isl_give isl_basic_map *isl_basic_map_from_basic_set(
4337 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4338{
4339 struct isl_basic_map *bmap;
4340
4341 bset = isl_basic_set_cow(bset);
4342 if (!bset || !dim)
4343 goto error;
4344
4345 isl_assert(bset->ctx, isl_space_compatible(bset->dim, dim), goto error);
4346 isl_space_free(bset->dim);
4347 bmap = (struct isl_basic_map *) bset;
4348 bmap->dim = dim;
4349 return isl_basic_map_finalize(bmap);
4350error:
4351 isl_basic_set_free(bset);
4352 isl_space_free(dim);
4353 return NULL;
4354}
4355
Tobias Grosser52a25232015-02-04 20:55:43 +00004356/* For a div d = floor(f/m), add the constraint
4357 *
4358 * f - m d >= 0
4359 */
4360static int add_upper_div_constraint(__isl_keep isl_basic_map *bmap,
4361 unsigned pos, isl_int *div)
4362{
4363 int i;
4364 unsigned total = isl_basic_map_total_dim(bmap);
4365
4366 i = isl_basic_map_alloc_inequality(bmap);
4367 if (i < 0)
4368 return -1;
4369 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
4370 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
4371
4372 return 0;
4373}
4374
4375/* For a div d = floor(f/m), add the constraint
4376 *
4377 * -(f-(n-1)) + m d >= 0
4378 */
4379static int add_lower_div_constraint(__isl_keep isl_basic_map *bmap,
4380 unsigned pos, isl_int *div)
4381{
4382 int i;
4383 unsigned total = isl_basic_map_total_dim(bmap);
4384
4385 i = isl_basic_map_alloc_inequality(bmap);
4386 if (i < 0)
4387 return -1;
4388 isl_seq_neg(bmap->ineq[i], div + 1, 1 + total);
4389 isl_int_set(bmap->ineq[i][1 + pos], div[0]);
4390 isl_int_add(bmap->ineq[i][0], bmap->ineq[i][0], bmap->ineq[i][1 + pos]);
4391 isl_int_sub_ui(bmap->ineq[i][0], bmap->ineq[i][0], 1);
4392
4393 return 0;
4394}
4395
4396/* For a div d = floor(f/m), add the constraints
4397 *
4398 * f - m d >= 0
4399 * -(f-(n-1)) + m d >= 0
4400 *
4401 * Note that the second constraint is the negation of
4402 *
4403 * f - m d >= n
4404 */
4405int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
4406 unsigned pos, isl_int *div)
4407{
4408 if (add_upper_div_constraint(bmap, pos, div) < 0)
4409 return -1;
4410 if (add_lower_div_constraint(bmap, pos, div) < 0)
4411 return -1;
4412 return 0;
4413}
4414
4415int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
4416 unsigned pos, isl_int *div)
4417{
4418 return isl_basic_map_add_div_constraints_var((isl_basic_map *)bset,
4419 pos, div);
4420}
4421
4422int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
4423{
4424 unsigned total = isl_basic_map_total_dim(bmap);
4425 unsigned div_pos = total - bmap->n_div + div;
4426
4427 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
4428 bmap->div[div]);
4429}
4430
4431/* For each known div d = floor(f/m), add the constraints
4432 *
4433 * f - m d >= 0
4434 * -(f-(n-1)) + m d >= 0
4435 */
4436__isl_give isl_basic_map *isl_basic_map_add_known_div_constraints(
4437 __isl_take isl_basic_map *bmap)
4438{
4439 int i;
4440 unsigned n_div;
4441
4442 if (!bmap)
4443 return NULL;
4444 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4445 if (n_div == 0)
4446 return bmap;
4447 bmap = isl_basic_map_cow(bmap);
4448 bmap = isl_basic_map_extend_constraints(bmap, 0, 2 * n_div);
4449 if (!bmap)
4450 return NULL;
4451 for (i = 0; i < n_div; ++i) {
4452 if (isl_int_is_zero(bmap->div[i][0]))
4453 continue;
4454 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
4455 return isl_basic_map_free(bmap);
4456 }
4457
4458 bmap = isl_basic_map_remove_duplicate_constraints(bmap, NULL, 0);
4459 bmap = isl_basic_map_finalize(bmap);
4460 return bmap;
4461}
4462
4463/* Add the div constraint of sign "sign" for div "div" of "bmap".
4464 *
4465 * In particular, if this div is of the form d = floor(f/m),
4466 * then add the constraint
4467 *
4468 * f - m d >= 0
4469 *
4470 * if sign < 0 or the constraint
4471 *
4472 * -(f-(n-1)) + m d >= 0
4473 *
4474 * if sign > 0.
4475 */
4476int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map *bmap,
4477 unsigned div, int sign)
4478{
4479 unsigned total;
4480 unsigned div_pos;
4481
4482 if (!bmap)
4483 return -1;
4484
4485 total = isl_basic_map_total_dim(bmap);
4486 div_pos = total - bmap->n_div + div;
4487
4488 if (sign < 0)
4489 return add_upper_div_constraint(bmap, div_pos, bmap->div[div]);
4490 else
4491 return add_lower_div_constraint(bmap, div_pos, bmap->div[div]);
4492}
4493
4494int isl_basic_set_add_div_constraints(struct isl_basic_set *bset, unsigned div)
4495{
4496 return isl_basic_map_add_div_constraints(bset, div);
4497}
4498
4499struct isl_basic_set *isl_basic_map_underlying_set(
4500 struct isl_basic_map *bmap)
4501{
4502 if (!bmap)
4503 goto error;
4504 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
4505 bmap->n_div == 0 &&
4506 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
4507 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
4508 return (struct isl_basic_set *)bmap;
4509 bmap = isl_basic_map_cow(bmap);
4510 if (!bmap)
4511 goto error;
4512 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
4513 if (!bmap->dim)
4514 goto error;
4515 bmap->extra -= bmap->n_div;
4516 bmap->n_div = 0;
4517 bmap = isl_basic_map_finalize(bmap);
4518 return (struct isl_basic_set *)bmap;
4519error:
4520 isl_basic_map_free(bmap);
4521 return NULL;
4522}
4523
4524__isl_give isl_basic_set *isl_basic_set_underlying_set(
4525 __isl_take isl_basic_set *bset)
4526{
4527 return isl_basic_map_underlying_set((isl_basic_map *)bset);
4528}
4529
4530/* Replace each element in "list" by the result of applying
Tobias Grosser1fa7b972015-02-16 19:33:40 +00004531 * isl_basic_map_underlying_set to the element.
Tobias Grosser52a25232015-02-04 20:55:43 +00004532 */
Tobias Grosser1fa7b972015-02-16 19:33:40 +00004533__isl_give isl_basic_set_list *isl_basic_map_list_underlying_set(
4534 __isl_take isl_basic_map_list *list)
Tobias Grosser52a25232015-02-04 20:55:43 +00004535{
4536 int i, n;
4537
4538 if (!list)
4539 return NULL;
4540
Tobias Grosser1fa7b972015-02-16 19:33:40 +00004541 n = isl_basic_map_list_n_basic_map(list);
Tobias Grosser52a25232015-02-04 20:55:43 +00004542 for (i = 0; i < n; ++i) {
Tobias Grosser1fa7b972015-02-16 19:33:40 +00004543 isl_basic_map *bmap;
Tobias Grosser52a25232015-02-04 20:55:43 +00004544 isl_basic_set *bset;
4545
Tobias Grosser1fa7b972015-02-16 19:33:40 +00004546 bmap = isl_basic_map_list_get_basic_map(list, i);
4547 bset = isl_basic_set_underlying_set(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +00004548 list = isl_basic_set_list_set_basic_set(list, i, bset);
4549 }
4550
4551 return list;
4552}
4553
4554struct isl_basic_map *isl_basic_map_overlying_set(
4555 struct isl_basic_set *bset, struct isl_basic_map *like)
4556{
4557 struct isl_basic_map *bmap;
4558 struct isl_ctx *ctx;
4559 unsigned total;
4560 int i;
4561
4562 if (!bset || !like)
4563 goto error;
4564 ctx = bset->ctx;
4565 isl_assert(ctx, bset->n_div == 0, goto error);
4566 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
4567 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
4568 goto error);
4569 if (isl_space_is_equal(bset->dim, like->dim) && like->n_div == 0) {
4570 isl_basic_map_free(like);
4571 return (struct isl_basic_map *)bset;
4572 }
4573 bset = isl_basic_set_cow(bset);
4574 if (!bset)
4575 goto error;
4576 total = bset->dim->n_out + bset->extra;
4577 bmap = (struct isl_basic_map *)bset;
4578 isl_space_free(bmap->dim);
4579 bmap->dim = isl_space_copy(like->dim);
4580 if (!bmap->dim)
4581 goto error;
4582 bmap->n_div = like->n_div;
4583 bmap->extra += like->n_div;
4584 if (bmap->extra) {
4585 unsigned ltotal;
4586 isl_int **div;
4587 ltotal = total - bmap->extra + like->extra;
4588 if (ltotal > total)
4589 ltotal = total;
4590 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
4591 bmap->extra * (1 + 1 + total));
4592 if (isl_blk_is_error(bmap->block2))
4593 goto error;
4594 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
4595 if (!div)
4596 goto error;
4597 bmap->div = div;
4598 for (i = 0; i < bmap->extra; ++i)
4599 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
4600 for (i = 0; i < like->n_div; ++i) {
4601 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
4602 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
4603 }
4604 bmap = isl_basic_map_add_known_div_constraints(bmap);
4605 }
4606 isl_basic_map_free(like);
4607 bmap = isl_basic_map_simplify(bmap);
4608 bmap = isl_basic_map_finalize(bmap);
4609 return bmap;
4610error:
4611 isl_basic_map_free(like);
4612 isl_basic_set_free(bset);
4613 return NULL;
4614}
4615
4616struct isl_basic_set *isl_basic_set_from_underlying_set(
4617 struct isl_basic_set *bset, struct isl_basic_set *like)
4618{
4619 return (struct isl_basic_set *)
4620 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
4621}
4622
4623struct isl_set *isl_set_from_underlying_set(
4624 struct isl_set *set, struct isl_basic_set *like)
4625{
4626 int i;
4627
4628 if (!set || !like)
4629 goto error;
4630 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
4631 goto error);
4632 if (isl_space_is_equal(set->dim, like->dim) && like->n_div == 0) {
4633 isl_basic_set_free(like);
4634 return set;
4635 }
4636 set = isl_set_cow(set);
4637 if (!set)
4638 goto error;
4639 for (i = 0; i < set->n; ++i) {
4640 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
4641 isl_basic_set_copy(like));
4642 if (!set->p[i])
4643 goto error;
4644 }
4645 isl_space_free(set->dim);
4646 set->dim = isl_space_copy(like->dim);
4647 if (!set->dim)
4648 goto error;
4649 isl_basic_set_free(like);
4650 return set;
4651error:
4652 isl_basic_set_free(like);
4653 isl_set_free(set);
4654 return NULL;
4655}
4656
4657struct isl_set *isl_map_underlying_set(struct isl_map *map)
4658{
4659 int i;
4660
4661 map = isl_map_cow(map);
4662 if (!map)
4663 return NULL;
4664 map->dim = isl_space_cow(map->dim);
4665 if (!map->dim)
4666 goto error;
4667
4668 for (i = 1; i < map->n; ++i)
4669 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
4670 goto error);
4671 for (i = 0; i < map->n; ++i) {
4672 map->p[i] = (struct isl_basic_map *)
4673 isl_basic_map_underlying_set(map->p[i]);
4674 if (!map->p[i])
4675 goto error;
4676 }
4677 if (map->n == 0)
4678 map->dim = isl_space_underlying(map->dim, 0);
4679 else {
4680 isl_space_free(map->dim);
4681 map->dim = isl_space_copy(map->p[0]->dim);
4682 }
4683 if (!map->dim)
4684 goto error;
4685 return (struct isl_set *)map;
4686error:
4687 isl_map_free(map);
4688 return NULL;
4689}
4690
4691struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
4692{
4693 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
4694}
4695
4696__isl_give isl_basic_map *isl_basic_map_reset_space(
4697 __isl_take isl_basic_map *bmap, __isl_take isl_space *dim)
4698{
4699 bmap = isl_basic_map_cow(bmap);
4700 if (!bmap || !dim)
4701 goto error;
4702
4703 isl_space_free(bmap->dim);
4704 bmap->dim = dim;
4705
4706 bmap = isl_basic_map_finalize(bmap);
4707
4708 return bmap;
4709error:
4710 isl_basic_map_free(bmap);
4711 isl_space_free(dim);
4712 return NULL;
4713}
4714
4715__isl_give isl_basic_set *isl_basic_set_reset_space(
4716 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4717{
4718 return (isl_basic_set *)isl_basic_map_reset_space((isl_basic_map *)bset,
4719 dim);
4720}
4721
4722__isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
4723 __isl_take isl_space *dim)
4724{
4725 int i;
4726
4727 map = isl_map_cow(map);
4728 if (!map || !dim)
4729 goto error;
4730
4731 for (i = 0; i < map->n; ++i) {
4732 map->p[i] = isl_basic_map_reset_space(map->p[i],
4733 isl_space_copy(dim));
4734 if (!map->p[i])
4735 goto error;
4736 }
4737 isl_space_free(map->dim);
4738 map->dim = dim;
4739
4740 return map;
4741error:
4742 isl_map_free(map);
4743 isl_space_free(dim);
4744 return NULL;
4745}
4746
4747__isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
4748 __isl_take isl_space *dim)
4749{
4750 return (struct isl_set *) isl_map_reset_space((struct isl_map *)set, dim);
4751}
4752
4753/* Compute the parameter domain of the given basic set.
4754 */
4755__isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
4756{
4757 isl_space *space;
4758 unsigned n;
4759
4760 if (isl_basic_set_is_params(bset))
4761 return bset;
4762
4763 n = isl_basic_set_dim(bset, isl_dim_set);
4764 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
4765 space = isl_basic_set_get_space(bset);
4766 space = isl_space_params(space);
4767 bset = isl_basic_set_reset_space(bset, space);
4768 return bset;
4769}
4770
4771/* Construct a zero-dimensional basic set with the given parameter domain.
4772 */
4773__isl_give isl_basic_set *isl_basic_set_from_params(
4774 __isl_take isl_basic_set *bset)
4775{
4776 isl_space *space;
4777 space = isl_basic_set_get_space(bset);
4778 space = isl_space_set_from_params(space);
4779 bset = isl_basic_set_reset_space(bset, space);
4780 return bset;
4781}
4782
4783/* Compute the parameter domain of the given set.
4784 */
4785__isl_give isl_set *isl_set_params(__isl_take isl_set *set)
4786{
4787 isl_space *space;
4788 unsigned n;
4789
4790 if (isl_set_is_params(set))
4791 return set;
4792
4793 n = isl_set_dim(set, isl_dim_set);
4794 set = isl_set_project_out(set, isl_dim_set, 0, n);
4795 space = isl_set_get_space(set);
4796 space = isl_space_params(space);
4797 set = isl_set_reset_space(set, space);
4798 return set;
4799}
4800
4801/* Construct a zero-dimensional set with the given parameter domain.
4802 */
4803__isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
4804{
4805 isl_space *space;
4806 space = isl_set_get_space(set);
4807 space = isl_space_set_from_params(space);
4808 set = isl_set_reset_space(set, space);
4809 return set;
4810}
4811
4812/* Compute the parameter domain of the given map.
4813 */
4814__isl_give isl_set *isl_map_params(__isl_take isl_map *map)
4815{
4816 isl_space *space;
4817 unsigned n;
4818
4819 n = isl_map_dim(map, isl_dim_in);
4820 map = isl_map_project_out(map, isl_dim_in, 0, n);
4821 n = isl_map_dim(map, isl_dim_out);
4822 map = isl_map_project_out(map, isl_dim_out, 0, n);
4823 space = isl_map_get_space(map);
4824 space = isl_space_params(space);
4825 map = isl_map_reset_space(map, space);
4826 return map;
4827}
4828
4829struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
4830{
Tobias Grosserb2f39922015-05-28 13:32:11 +00004831 isl_space *space;
Tobias Grosser52a25232015-02-04 20:55:43 +00004832 unsigned n_out;
4833
4834 if (!bmap)
4835 return NULL;
Tobias Grosserb2f39922015-05-28 13:32:11 +00004836 space = isl_space_domain(isl_basic_map_get_space(bmap));
Tobias Grosser52a25232015-02-04 20:55:43 +00004837
Tobias Grosser52a25232015-02-04 20:55:43 +00004838 n_out = isl_basic_map_n_out(bmap);
Tobias Grosserb2f39922015-05-28 13:32:11 +00004839 bmap = isl_basic_map_project_out(bmap, isl_dim_out, 0, n_out);
Tobias Grosser52a25232015-02-04 20:55:43 +00004840
Tobias Grosserb2f39922015-05-28 13:32:11 +00004841 return isl_basic_map_reset_space(bmap, space);
Tobias Grosser52a25232015-02-04 20:55:43 +00004842}
4843
4844int isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
4845{
4846 if (!bmap)
4847 return -1;
4848 return isl_space_may_be_set(bmap->dim);
4849}
4850
4851/* Is this basic map actually a set?
4852 * Users should never call this function. Outside of isl,
4853 * the type should indicate whether something is a set or a map.
4854 */
4855int isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
4856{
4857 if (!bmap)
4858 return -1;
4859 return isl_space_is_set(bmap->dim);
4860}
4861
4862struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
4863{
4864 if (!bmap)
4865 return NULL;
4866 if (isl_basic_map_is_set(bmap))
4867 return bmap;
4868 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
4869}
4870
4871__isl_give isl_basic_map *isl_basic_map_domain_map(
4872 __isl_take isl_basic_map *bmap)
4873{
4874 int i, k;
4875 isl_space *dim;
4876 isl_basic_map *domain;
4877 int nparam, n_in, n_out;
4878 unsigned total;
4879
4880 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4881 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4882 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4883
4884 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
4885 domain = isl_basic_map_universe(dim);
4886
4887 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4888 bmap = isl_basic_map_apply_range(bmap, domain);
4889 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
4890
4891 total = isl_basic_map_total_dim(bmap);
4892
4893 for (i = 0; i < n_in; ++i) {
4894 k = isl_basic_map_alloc_equality(bmap);
4895 if (k < 0)
4896 goto error;
4897 isl_seq_clr(bmap->eq[k], 1 + total);
4898 isl_int_set_si(bmap->eq[k][1 + nparam + i], -1);
4899 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4900 }
4901
4902 bmap = isl_basic_map_gauss(bmap, NULL);
4903 return isl_basic_map_finalize(bmap);
4904error:
4905 isl_basic_map_free(bmap);
4906 return NULL;
4907}
4908
4909__isl_give isl_basic_map *isl_basic_map_range_map(
4910 __isl_take isl_basic_map *bmap)
4911{
4912 int i, k;
4913 isl_space *dim;
4914 isl_basic_map *range;
4915 int nparam, n_in, n_out;
4916 unsigned total;
4917
4918 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4919 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4920 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4921
4922 dim = isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap)));
4923 range = isl_basic_map_universe(dim);
4924
4925 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4926 bmap = isl_basic_map_apply_range(bmap, range);
4927 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
4928
4929 total = isl_basic_map_total_dim(bmap);
4930
4931 for (i = 0; i < n_out; ++i) {
4932 k = isl_basic_map_alloc_equality(bmap);
4933 if (k < 0)
4934 goto error;
4935 isl_seq_clr(bmap->eq[k], 1 + total);
4936 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + i], -1);
4937 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4938 }
4939
4940 bmap = isl_basic_map_gauss(bmap, NULL);
4941 return isl_basic_map_finalize(bmap);
4942error:
4943 isl_basic_map_free(bmap);
4944 return NULL;
4945}
4946
4947int isl_map_may_be_set(__isl_keep isl_map *map)
4948{
4949 if (!map)
4950 return -1;
4951 return isl_space_may_be_set(map->dim);
4952}
4953
4954/* Is this map actually a set?
4955 * Users should never call this function. Outside of isl,
4956 * the type should indicate whether something is a set or a map.
4957 */
4958int isl_map_is_set(__isl_keep isl_map *map)
4959{
4960 if (!map)
4961 return -1;
4962 return isl_space_is_set(map->dim);
4963}
4964
4965struct isl_set *isl_map_range(struct isl_map *map)
4966{
4967 int i;
4968 struct isl_set *set;
4969
4970 if (!map)
4971 goto error;
4972 if (isl_map_is_set(map))
4973 return (isl_set *)map;
4974
4975 map = isl_map_cow(map);
4976 if (!map)
4977 goto error;
4978
4979 set = (struct isl_set *) map;
4980 set->dim = isl_space_range(set->dim);
4981 if (!set->dim)
4982 goto error;
4983 for (i = 0; i < map->n; ++i) {
4984 set->p[i] = isl_basic_map_range(map->p[i]);
4985 if (!set->p[i])
4986 goto error;
4987 }
4988 ISL_F_CLR(set, ISL_MAP_DISJOINT);
4989 ISL_F_CLR(set, ISL_SET_NORMALIZED);
4990 return set;
4991error:
4992 isl_map_free(map);
4993 return NULL;
4994}
4995
4996__isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
4997{
4998 int i;
4999
5000 map = isl_map_cow(map);
5001 if (!map)
5002 return NULL;
5003
5004 map->dim = isl_space_domain_map(map->dim);
5005 if (!map->dim)
5006 goto error;
5007 for (i = 0; i < map->n; ++i) {
5008 map->p[i] = isl_basic_map_domain_map(map->p[i]);
5009 if (!map->p[i])
5010 goto error;
5011 }
5012 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5013 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5014 return map;
5015error:
5016 isl_map_free(map);
5017 return NULL;
5018}
5019
5020__isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
5021{
5022 int i;
5023 isl_space *range_dim;
5024
5025 map = isl_map_cow(map);
5026 if (!map)
5027 return NULL;
5028
5029 range_dim = isl_space_range(isl_map_get_space(map));
5030 range_dim = isl_space_from_range(range_dim);
5031 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
5032 map->dim = isl_space_join(map->dim, range_dim);
5033 if (!map->dim)
5034 goto error;
5035 for (i = 0; i < map->n; ++i) {
5036 map->p[i] = isl_basic_map_range_map(map->p[i]);
5037 if (!map->p[i])
5038 goto error;
5039 }
5040 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5041 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5042 return map;
5043error:
5044 isl_map_free(map);
5045 return NULL;
5046}
5047
5048/* Given a wrapped map of the form A[B -> C],
5049 * return the map A[B -> C] -> B.
5050 */
5051__isl_give isl_map *isl_set_wrapped_domain_map(__isl_take isl_set *set)
5052{
5053 isl_id *id;
5054 isl_map *map;
5055
5056 if (!set)
5057 return NULL;
5058 if (!isl_set_has_tuple_id(set))
5059 return isl_map_domain_map(isl_set_unwrap(set));
5060
5061 id = isl_set_get_tuple_id(set);
5062 map = isl_map_domain_map(isl_set_unwrap(set));
5063 map = isl_map_set_tuple_id(map, isl_dim_in, id);
5064
5065 return map;
5066}
5067
5068__isl_give isl_map *isl_map_from_set(__isl_take isl_set *set,
5069 __isl_take isl_space *dim)
5070{
5071 int i;
5072 struct isl_map *map = NULL;
5073
5074 set = isl_set_cow(set);
5075 if (!set || !dim)
5076 goto error;
5077 isl_assert(set->ctx, isl_space_compatible(set->dim, dim), goto error);
5078 map = (struct isl_map *)set;
5079 for (i = 0; i < set->n; ++i) {
5080 map->p[i] = isl_basic_map_from_basic_set(
5081 set->p[i], isl_space_copy(dim));
5082 if (!map->p[i])
5083 goto error;
5084 }
5085 isl_space_free(map->dim);
5086 map->dim = dim;
5087 return map;
5088error:
5089 isl_space_free(dim);
5090 isl_set_free(set);
5091 return NULL;
5092}
5093
5094__isl_give isl_basic_map *isl_basic_map_from_domain(
5095 __isl_take isl_basic_set *bset)
5096{
5097 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
5098}
5099
5100__isl_give isl_basic_map *isl_basic_map_from_range(
5101 __isl_take isl_basic_set *bset)
5102{
5103 isl_space *space;
5104 space = isl_basic_set_get_space(bset);
5105 space = isl_space_from_range(space);
5106 bset = isl_basic_set_reset_space(bset, space);
5107 return (isl_basic_map *)bset;
5108}
5109
5110/* Create a relation with the given set as range.
5111 * The domain of the created relation is a zero-dimensional
5112 * flat anonymous space.
5113 */
5114__isl_give isl_map *isl_map_from_range(__isl_take isl_set *set)
5115{
5116 isl_space *space;
5117 space = isl_set_get_space(set);
5118 space = isl_space_from_range(space);
5119 set = isl_set_reset_space(set, space);
5120 return (struct isl_map *)set;
5121}
5122
5123/* Create a relation with the given set as domain.
5124 * The range of the created relation is a zero-dimensional
5125 * flat anonymous space.
5126 */
5127__isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
5128{
5129 return isl_map_reverse(isl_map_from_range(set));
5130}
5131
5132__isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
5133 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
5134{
5135 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
5136}
5137
5138__isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
5139 __isl_take isl_set *range)
5140{
5141 return isl_map_apply_range(isl_map_reverse(domain), range);
5142}
5143
Tobias Grosser52a25232015-02-04 20:55:43 +00005144__isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *dim, int n,
5145 unsigned flags)
5146{
5147 struct isl_map *map;
5148
5149 if (!dim)
5150 return NULL;
5151 if (n < 0)
5152 isl_die(dim->ctx, isl_error_internal,
5153 "negative number of basic maps", goto error);
5154 map = isl_alloc(dim->ctx, struct isl_map,
5155 sizeof(struct isl_map) +
5156 (n - 1) * sizeof(struct isl_basic_map *));
5157 if (!map)
5158 goto error;
5159
5160 map->ctx = dim->ctx;
5161 isl_ctx_ref(map->ctx);
5162 map->ref = 1;
5163 map->size = n;
5164 map->n = 0;
5165 map->dim = dim;
5166 map->flags = flags;
5167 return map;
5168error:
5169 isl_space_free(dim);
5170 return NULL;
5171}
5172
5173struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
5174 unsigned nparam, unsigned in, unsigned out, int n,
5175 unsigned flags)
5176{
5177 struct isl_map *map;
5178 isl_space *dims;
5179
5180 dims = isl_space_alloc(ctx, nparam, in, out);
5181 if (!dims)
5182 return NULL;
5183
5184 map = isl_map_alloc_space(dims, n, flags);
5185 return map;
5186}
5187
5188__isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *dim)
5189{
5190 struct isl_basic_map *bmap;
5191 bmap = isl_basic_map_alloc_space(dim, 0, 1, 0);
5192 bmap = isl_basic_map_set_to_empty(bmap);
5193 return bmap;
5194}
5195
5196__isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *dim)
5197{
5198 struct isl_basic_set *bset;
5199 bset = isl_basic_set_alloc_space(dim, 0, 1, 0);
5200 bset = isl_basic_set_set_to_empty(bset);
5201 return bset;
5202}
5203
Tobias Grosser52a25232015-02-04 20:55:43 +00005204__isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *dim)
5205{
5206 struct isl_basic_map *bmap;
5207 bmap = isl_basic_map_alloc_space(dim, 0, 0, 0);
5208 bmap = isl_basic_map_finalize(bmap);
5209 return bmap;
5210}
5211
5212__isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *dim)
5213{
5214 struct isl_basic_set *bset;
5215 bset = isl_basic_set_alloc_space(dim, 0, 0, 0);
5216 bset = isl_basic_set_finalize(bset);
5217 return bset;
5218}
5219
5220__isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_space *dim)
5221{
5222 int i;
5223 unsigned total = isl_space_dim(dim, isl_dim_all);
5224 isl_basic_map *bmap;
5225
5226 bmap= isl_basic_map_alloc_space(dim, 0, 0, total);
5227 for (i = 0; i < total; ++i) {
5228 int k = isl_basic_map_alloc_inequality(bmap);
5229 if (k < 0)
5230 goto error;
5231 isl_seq_clr(bmap->ineq[k], 1 + total);
5232 isl_int_set_si(bmap->ineq[k][1 + i], 1);
5233 }
5234 return bmap;
5235error:
5236 isl_basic_map_free(bmap);
5237 return NULL;
5238}
5239
5240__isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_space *dim)
5241{
5242 return isl_basic_map_nat_universe(dim);
5243}
5244
5245__isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
5246{
5247 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
5248}
5249
5250__isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
5251{
5252 return isl_map_nat_universe(dim);
5253}
5254
Tobias Grosser52a25232015-02-04 20:55:43 +00005255__isl_give isl_map *isl_map_empty(__isl_take isl_space *dim)
5256{
5257 return isl_map_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5258}
5259
Tobias Grosser52a25232015-02-04 20:55:43 +00005260__isl_give isl_set *isl_set_empty(__isl_take isl_space *dim)
5261{
5262 return isl_set_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5263}
5264
Tobias Grosser52a25232015-02-04 20:55:43 +00005265__isl_give isl_map *isl_map_universe(__isl_take isl_space *dim)
5266{
5267 struct isl_map *map;
5268 if (!dim)
5269 return NULL;
5270 map = isl_map_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5271 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
5272 return map;
5273}
5274
5275__isl_give isl_set *isl_set_universe(__isl_take isl_space *dim)
5276{
5277 struct isl_set *set;
5278 if (!dim)
5279 return NULL;
5280 set = isl_set_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5281 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
5282 return set;
5283}
5284
Tobias Grosser52a25232015-02-04 20:55:43 +00005285struct isl_map *isl_map_dup(struct isl_map *map)
5286{
5287 int i;
5288 struct isl_map *dup;
5289
5290 if (!map)
5291 return NULL;
5292 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
5293 for (i = 0; i < map->n; ++i)
5294 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
5295 return dup;
5296}
5297
5298__isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
5299 __isl_take isl_basic_map *bmap)
5300{
5301 if (!bmap || !map)
5302 goto error;
5303 if (isl_basic_map_plain_is_empty(bmap)) {
5304 isl_basic_map_free(bmap);
5305 return map;
5306 }
5307 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
5308 isl_assert(map->ctx, map->n < map->size, goto error);
5309 map->p[map->n] = bmap;
5310 map->n++;
5311 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5312 return map;
5313error:
5314 if (map)
5315 isl_map_free(map);
5316 if (bmap)
5317 isl_basic_map_free(bmap);
5318 return NULL;
5319}
5320
5321__isl_null isl_map *isl_map_free(__isl_take isl_map *map)
5322{
5323 int i;
5324
5325 if (!map)
5326 return NULL;
5327
5328 if (--map->ref > 0)
5329 return NULL;
5330
5331 isl_ctx_deref(map->ctx);
5332 for (i = 0; i < map->n; ++i)
5333 isl_basic_map_free(map->p[i]);
5334 isl_space_free(map->dim);
5335 free(map);
5336
5337 return NULL;
5338}
5339
Tobias Grosser52a25232015-02-04 20:55:43 +00005340static struct isl_basic_map *isl_basic_map_fix_pos_si(
5341 struct isl_basic_map *bmap, unsigned pos, int value)
5342{
5343 int j;
5344
5345 bmap = isl_basic_map_cow(bmap);
5346 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5347 j = isl_basic_map_alloc_equality(bmap);
5348 if (j < 0)
5349 goto error;
5350 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5351 isl_int_set_si(bmap->eq[j][pos], -1);
5352 isl_int_set_si(bmap->eq[j][0], value);
5353 bmap = isl_basic_map_simplify(bmap);
5354 return isl_basic_map_finalize(bmap);
5355error:
5356 isl_basic_map_free(bmap);
5357 return NULL;
5358}
5359
5360static __isl_give isl_basic_map *isl_basic_map_fix_pos(
5361 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
5362{
5363 int j;
5364
5365 bmap = isl_basic_map_cow(bmap);
5366 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5367 j = isl_basic_map_alloc_equality(bmap);
5368 if (j < 0)
5369 goto error;
5370 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5371 isl_int_set_si(bmap->eq[j][pos], -1);
5372 isl_int_set(bmap->eq[j][0], value);
5373 bmap = isl_basic_map_simplify(bmap);
5374 return isl_basic_map_finalize(bmap);
5375error:
5376 isl_basic_map_free(bmap);
5377 return NULL;
5378}
5379
5380struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
5381 enum isl_dim_type type, unsigned pos, int value)
5382{
5383 if (!bmap)
5384 return NULL;
5385 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5386 return isl_basic_map_fix_pos_si(bmap,
5387 isl_basic_map_offset(bmap, type) + pos, value);
5388error:
5389 isl_basic_map_free(bmap);
5390 return NULL;
5391}
5392
5393__isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
5394 enum isl_dim_type type, unsigned pos, isl_int value)
5395{
5396 if (!bmap)
5397 return NULL;
5398 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5399 return isl_basic_map_fix_pos(bmap,
5400 isl_basic_map_offset(bmap, type) + pos, value);
5401error:
5402 isl_basic_map_free(bmap);
5403 return NULL;
5404}
5405
5406/* Fix the value of the variable at position "pos" of type "type" of "bmap"
5407 * to be equal to "v".
5408 */
5409__isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
5410 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5411{
5412 if (!bmap || !v)
5413 goto error;
5414 if (!isl_val_is_int(v))
5415 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5416 "expecting integer value", goto error);
5417 if (pos >= isl_basic_map_dim(bmap, type))
5418 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5419 "index out of bounds", goto error);
5420 pos += isl_basic_map_offset(bmap, type);
5421 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
5422 isl_val_free(v);
5423 return bmap;
5424error:
5425 isl_basic_map_free(bmap);
5426 isl_val_free(v);
5427 return NULL;
5428}
5429
5430/* Fix the value of the variable at position "pos" of type "type" of "bset"
5431 * to be equal to "v".
5432 */
5433__isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
5434 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5435{
5436 return isl_basic_map_fix_val(bset, type, pos, v);
5437}
5438
5439struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
5440 enum isl_dim_type type, unsigned pos, int value)
5441{
5442 return (struct isl_basic_set *)
5443 isl_basic_map_fix_si((struct isl_basic_map *)bset,
5444 type, pos, value);
5445}
5446
5447__isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
5448 enum isl_dim_type type, unsigned pos, isl_int value)
5449{
5450 return (struct isl_basic_set *)
5451 isl_basic_map_fix((struct isl_basic_map *)bset,
5452 type, pos, value);
5453}
5454
5455struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
5456 unsigned input, int value)
5457{
5458 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
5459}
5460
5461struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
5462 unsigned dim, int value)
5463{
5464 return (struct isl_basic_set *)
5465 isl_basic_map_fix_si((struct isl_basic_map *)bset,
5466 isl_dim_set, dim, value);
5467}
5468
5469static int remove_if_empty(__isl_keep isl_map *map, int i)
5470{
5471 int empty = isl_basic_map_plain_is_empty(map->p[i]);
5472
5473 if (empty < 0)
5474 return -1;
5475 if (!empty)
5476 return 0;
5477
5478 isl_basic_map_free(map->p[i]);
5479 if (i != map->n - 1) {
5480 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5481 map->p[i] = map->p[map->n - 1];
5482 }
5483 map->n--;
5484
5485 return 0;
5486}
5487
5488/* Perform "fn" on each basic map of "map", where we may not be holding
5489 * the only reference to "map".
5490 * In particular, "fn" should be a semantics preserving operation
5491 * that we want to apply to all copies of "map". We therefore need
5492 * to be careful not to modify "map" in a way that breaks "map"
5493 * in case anything goes wrong.
5494 */
5495__isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
5496 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
5497{
5498 struct isl_basic_map *bmap;
5499 int i;
5500
5501 if (!map)
5502 return NULL;
5503
5504 for (i = map->n - 1; i >= 0; --i) {
5505 bmap = isl_basic_map_copy(map->p[i]);
5506 bmap = fn(bmap);
5507 if (!bmap)
5508 goto error;
5509 isl_basic_map_free(map->p[i]);
5510 map->p[i] = bmap;
5511 if (remove_if_empty(map, i) < 0)
5512 goto error;
5513 }
5514
5515 return map;
5516error:
5517 isl_map_free(map);
5518 return NULL;
5519}
5520
5521struct isl_map *isl_map_fix_si(struct isl_map *map,
5522 enum isl_dim_type type, unsigned pos, int value)
5523{
5524 int i;
5525
5526 map = isl_map_cow(map);
5527 if (!map)
5528 return NULL;
5529
5530 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5531 for (i = map->n - 1; i >= 0; --i) {
5532 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
5533 if (remove_if_empty(map, i) < 0)
5534 goto error;
5535 }
5536 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5537 return map;
5538error:
5539 isl_map_free(map);
5540 return NULL;
5541}
5542
5543__isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
5544 enum isl_dim_type type, unsigned pos, int value)
5545{
5546 return (struct isl_set *)
5547 isl_map_fix_si((struct isl_map *)set, type, pos, value);
5548}
5549
5550__isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
5551 enum isl_dim_type type, unsigned pos, isl_int value)
5552{
5553 int i;
5554
5555 map = isl_map_cow(map);
5556 if (!map)
5557 return NULL;
5558
5559 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5560 for (i = 0; i < map->n; ++i) {
5561 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
5562 if (!map->p[i])
5563 goto error;
5564 }
5565 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5566 return map;
5567error:
5568 isl_map_free(map);
5569 return NULL;
5570}
5571
5572__isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
5573 enum isl_dim_type type, unsigned pos, isl_int value)
5574{
5575 return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
5576}
5577
5578/* Fix the value of the variable at position "pos" of type "type" of "map"
5579 * to be equal to "v".
5580 */
5581__isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
5582 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5583{
5584 int i;
5585
5586 map = isl_map_cow(map);
5587 if (!map || !v)
5588 goto error;
5589
5590 if (!isl_val_is_int(v))
5591 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5592 "expecting integer value", goto error);
5593 if (pos >= isl_map_dim(map, type))
5594 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5595 "index out of bounds", goto error);
5596 for (i = map->n - 1; i >= 0; --i) {
5597 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
5598 isl_val_copy(v));
5599 if (remove_if_empty(map, i) < 0)
5600 goto error;
5601 }
5602 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5603 isl_val_free(v);
5604 return map;
5605error:
5606 isl_map_free(map);
5607 isl_val_free(v);
5608 return NULL;
5609}
5610
5611/* Fix the value of the variable at position "pos" of type "type" of "set"
5612 * to be equal to "v".
5613 */
5614__isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
5615 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5616{
5617 return isl_map_fix_val(set, type, pos, v);
5618}
5619
5620struct isl_map *isl_map_fix_input_si(struct isl_map *map,
5621 unsigned input, int value)
5622{
5623 return isl_map_fix_si(map, isl_dim_in, input, value);
5624}
5625
5626struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
5627{
5628 return (struct isl_set *)
5629 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
5630}
5631
5632static __isl_give isl_basic_map *basic_map_bound_si(
5633 __isl_take isl_basic_map *bmap,
5634 enum isl_dim_type type, unsigned pos, int value, int upper)
5635{
5636 int j;
5637
5638 if (!bmap)
5639 return NULL;
5640 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5641 pos += isl_basic_map_offset(bmap, type);
5642 bmap = isl_basic_map_cow(bmap);
5643 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5644 j = isl_basic_map_alloc_inequality(bmap);
5645 if (j < 0)
5646 goto error;
5647 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5648 if (upper) {
5649 isl_int_set_si(bmap->ineq[j][pos], -1);
5650 isl_int_set_si(bmap->ineq[j][0], value);
5651 } else {
5652 isl_int_set_si(bmap->ineq[j][pos], 1);
5653 isl_int_set_si(bmap->ineq[j][0], -value);
5654 }
5655 bmap = isl_basic_map_simplify(bmap);
5656 return isl_basic_map_finalize(bmap);
5657error:
5658 isl_basic_map_free(bmap);
5659 return NULL;
5660}
5661
5662__isl_give isl_basic_map *isl_basic_map_lower_bound_si(
5663 __isl_take isl_basic_map *bmap,
5664 enum isl_dim_type type, unsigned pos, int value)
5665{
5666 return basic_map_bound_si(bmap, type, pos, value, 0);
5667}
5668
5669/* Constrain the values of the given dimension to be no greater than "value".
5670 */
5671__isl_give isl_basic_map *isl_basic_map_upper_bound_si(
5672 __isl_take isl_basic_map *bmap,
5673 enum isl_dim_type type, unsigned pos, int value)
5674{
5675 return basic_map_bound_si(bmap, type, pos, value, 1);
5676}
5677
5678struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
5679 unsigned dim, isl_int value)
5680{
5681 int j;
5682
5683 bset = isl_basic_set_cow(bset);
5684 bset = isl_basic_set_extend_constraints(bset, 0, 1);
5685 j = isl_basic_set_alloc_inequality(bset);
5686 if (j < 0)
5687 goto error;
5688 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
5689 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
5690 isl_int_neg(bset->ineq[j][0], value);
5691 bset = isl_basic_set_simplify(bset);
5692 return isl_basic_set_finalize(bset);
5693error:
5694 isl_basic_set_free(bset);
5695 return NULL;
5696}
5697
5698static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
5699 enum isl_dim_type type, unsigned pos, int value, int upper)
5700{
5701 int i;
5702
5703 map = isl_map_cow(map);
5704 if (!map)
5705 return NULL;
5706
5707 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5708 for (i = 0; i < map->n; ++i) {
5709 map->p[i] = basic_map_bound_si(map->p[i],
5710 type, pos, value, upper);
5711 if (!map->p[i])
5712 goto error;
5713 }
5714 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5715 return map;
5716error:
5717 isl_map_free(map);
5718 return NULL;
5719}
5720
5721__isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
5722 enum isl_dim_type type, unsigned pos, int value)
5723{
5724 return map_bound_si(map, type, pos, value, 0);
5725}
5726
5727__isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
5728 enum isl_dim_type type, unsigned pos, int value)
5729{
5730 return map_bound_si(map, type, pos, value, 1);
5731}
5732
5733__isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
5734 enum isl_dim_type type, unsigned pos, int value)
5735{
5736 return (struct isl_set *)
5737 isl_map_lower_bound_si((struct isl_map *)set, type, pos, value);
5738}
5739
5740__isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
5741 enum isl_dim_type type, unsigned pos, int value)
5742{
5743 return isl_map_upper_bound_si(set, type, pos, value);
5744}
5745
5746/* Bound the given variable of "bmap" from below (or above is "upper"
5747 * is set) to "value".
5748 */
5749static __isl_give isl_basic_map *basic_map_bound(
5750 __isl_take isl_basic_map *bmap,
5751 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5752{
5753 int j;
5754
5755 if (!bmap)
5756 return NULL;
5757 if (pos >= isl_basic_map_dim(bmap, type))
5758 isl_die(bmap->ctx, isl_error_invalid,
5759 "index out of bounds", goto error);
5760 pos += isl_basic_map_offset(bmap, type);
5761 bmap = isl_basic_map_cow(bmap);
5762 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5763 j = isl_basic_map_alloc_inequality(bmap);
5764 if (j < 0)
5765 goto error;
5766 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5767 if (upper) {
5768 isl_int_set_si(bmap->ineq[j][pos], -1);
5769 isl_int_set(bmap->ineq[j][0], value);
5770 } else {
5771 isl_int_set_si(bmap->ineq[j][pos], 1);
5772 isl_int_neg(bmap->ineq[j][0], value);
5773 }
5774 bmap = isl_basic_map_simplify(bmap);
5775 return isl_basic_map_finalize(bmap);
5776error:
5777 isl_basic_map_free(bmap);
5778 return NULL;
5779}
5780
5781/* Bound the given variable of "map" from below (or above is "upper"
5782 * is set) to "value".
5783 */
5784static __isl_give isl_map *map_bound(__isl_take isl_map *map,
5785 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5786{
5787 int i;
5788
5789 map = isl_map_cow(map);
5790 if (!map)
5791 return NULL;
5792
5793 if (pos >= isl_map_dim(map, type))
5794 isl_die(map->ctx, isl_error_invalid,
5795 "index out of bounds", goto error);
5796 for (i = map->n - 1; i >= 0; --i) {
5797 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
5798 if (remove_if_empty(map, i) < 0)
5799 goto error;
5800 }
5801 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5802 return map;
5803error:
5804 isl_map_free(map);
5805 return NULL;
5806}
5807
5808__isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
5809 enum isl_dim_type type, unsigned pos, isl_int value)
5810{
5811 return map_bound(map, type, pos, value, 0);
5812}
5813
5814__isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
5815 enum isl_dim_type type, unsigned pos, isl_int value)
5816{
5817 return map_bound(map, type, pos, value, 1);
5818}
5819
5820__isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
5821 enum isl_dim_type type, unsigned pos, isl_int value)
5822{
5823 return isl_map_lower_bound(set, type, pos, value);
5824}
5825
5826__isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
5827 enum isl_dim_type type, unsigned pos, isl_int value)
5828{
5829 return isl_map_upper_bound(set, type, pos, value);
5830}
5831
5832/* Force the values of the variable at position "pos" of type "type" of "set"
5833 * to be no smaller than "value".
5834 */
5835__isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
5836 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
5837{
5838 if (!value)
5839 goto error;
5840 if (!isl_val_is_int(value))
5841 isl_die(isl_set_get_ctx(set), isl_error_invalid,
5842 "expecting integer value", goto error);
5843 set = isl_set_lower_bound(set, type, pos, value->n);
5844 isl_val_free(value);
5845 return set;
5846error:
5847 isl_val_free(value);
5848 isl_set_free(set);
5849 return NULL;
5850}
5851
5852/* Force the values of the variable at position "pos" of type "type" of "set"
5853 * to be no greater than "value".
5854 */
5855__isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
5856 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
5857{
5858 if (!value)
5859 goto error;
5860 if (!isl_val_is_int(value))
5861 isl_die(isl_set_get_ctx(set), isl_error_invalid,
5862 "expecting integer value", goto error);
5863 set = isl_set_upper_bound(set, type, pos, value->n);
5864 isl_val_free(value);
5865 return set;
5866error:
5867 isl_val_free(value);
5868 isl_set_free(set);
5869 return NULL;
5870}
5871
5872struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
5873 isl_int value)
5874{
5875 int i;
5876
5877 set = isl_set_cow(set);
5878 if (!set)
5879 return NULL;
5880
5881 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
5882 for (i = 0; i < set->n; ++i) {
5883 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
5884 if (!set->p[i])
5885 goto error;
5886 }
5887 return set;
5888error:
5889 isl_set_free(set);
5890 return NULL;
5891}
5892
5893struct isl_map *isl_map_reverse(struct isl_map *map)
5894{
5895 int i;
5896
5897 map = isl_map_cow(map);
5898 if (!map)
5899 return NULL;
5900
5901 map->dim = isl_space_reverse(map->dim);
5902 if (!map->dim)
5903 goto error;
5904 for (i = 0; i < map->n; ++i) {
5905 map->p[i] = isl_basic_map_reverse(map->p[i]);
5906 if (!map->p[i])
5907 goto error;
5908 }
5909 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5910 return map;
5911error:
5912 isl_map_free(map);
5913 return NULL;
5914}
5915
5916static struct isl_map *isl_basic_map_partial_lexopt(
5917 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5918 struct isl_set **empty, int max)
5919{
5920 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
5921}
5922
5923struct isl_map *isl_basic_map_partial_lexmax(
5924 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5925 struct isl_set **empty)
5926{
5927 return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
5928}
5929
5930struct isl_map *isl_basic_map_partial_lexmin(
5931 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5932 struct isl_set **empty)
5933{
5934 return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
5935}
5936
5937struct isl_set *isl_basic_set_partial_lexmin(
5938 struct isl_basic_set *bset, struct isl_basic_set *dom,
5939 struct isl_set **empty)
5940{
5941 return (struct isl_set *)
5942 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
5943 dom, empty);
5944}
5945
5946struct isl_set *isl_basic_set_partial_lexmax(
5947 struct isl_basic_set *bset, struct isl_basic_set *dom,
5948 struct isl_set **empty)
5949{
5950 return (struct isl_set *)
5951 isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
5952 dom, empty);
5953}
5954
5955__isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmin_pw_multi_aff(
5956 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5957 __isl_give isl_set **empty)
5958{
5959 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 0);
5960}
5961
5962__isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmax_pw_multi_aff(
5963 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5964 __isl_give isl_set **empty)
5965{
5966 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 1);
5967}
5968
5969__isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmin_pw_multi_aff(
5970 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
5971 __isl_give isl_set **empty)
5972{
5973 return isl_basic_map_partial_lexmin_pw_multi_aff(bset, dom, empty);
5974}
5975
5976__isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmax_pw_multi_aff(
5977 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
5978 __isl_give isl_set **empty)
5979{
5980 return isl_basic_map_partial_lexmax_pw_multi_aff(bset, dom, empty);
5981}
5982
5983__isl_give isl_pw_multi_aff *isl_basic_map_lexopt_pw_multi_aff(
5984 __isl_take isl_basic_map *bmap, int max)
5985{
5986 isl_basic_set *dom = NULL;
5987 isl_space *dom_space;
5988
5989 if (!bmap)
5990 goto error;
5991 dom_space = isl_space_domain(isl_space_copy(bmap->dim));
5992 dom = isl_basic_set_universe(dom_space);
5993 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, NULL, max);
5994error:
5995 isl_basic_map_free(bmap);
5996 return NULL;
5997}
5998
5999__isl_give isl_pw_multi_aff *isl_basic_map_lexmin_pw_multi_aff(
6000 __isl_take isl_basic_map *bmap)
6001{
6002 return isl_basic_map_lexopt_pw_multi_aff(bmap, 0);
6003}
6004
6005#undef TYPE
6006#define TYPE isl_pw_multi_aff
6007#undef SUFFIX
6008#define SUFFIX _pw_multi_aff
6009#undef EMPTY
6010#define EMPTY isl_pw_multi_aff_empty
6011#undef ADD
6012#define ADD isl_pw_multi_aff_union_add
6013#include "isl_map_lexopt_templ.c"
6014
6015/* Given a map "map", compute the lexicographically minimal
6016 * (or maximal) image element for each domain element in dom,
6017 * in the form of an isl_pw_multi_aff.
6018 * Set *empty to those elements in dom that do not have an image element.
6019 *
6020 * We first compute the lexicographically minimal or maximal element
6021 * in the first basic map. This results in a partial solution "res"
6022 * and a subset "todo" of dom that still need to be handled.
6023 * We then consider each of the remaining maps in "map" and successively
6024 * update both "res" and "todo".
6025 */
6026static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
6027 __isl_take isl_map *map, __isl_take isl_set *dom,
6028 __isl_give isl_set **empty, int max)
6029{
6030 int i;
6031 isl_pw_multi_aff *res;
6032 isl_set *todo;
6033
6034 if (!map || !dom)
6035 goto error;
6036
6037 if (isl_map_plain_is_empty(map)) {
6038 if (empty)
6039 *empty = dom;
6040 else
6041 isl_set_free(dom);
6042 return isl_pw_multi_aff_from_map(map);
6043 }
6044
6045 res = basic_map_partial_lexopt_pw_multi_aff(
6046 isl_basic_map_copy(map->p[0]),
6047 isl_set_copy(dom), &todo, max);
6048
6049 for (i = 1; i < map->n; ++i) {
6050 isl_pw_multi_aff *res_i;
6051 isl_set *todo_i;
6052
6053 res_i = basic_map_partial_lexopt_pw_multi_aff(
6054 isl_basic_map_copy(map->p[i]),
6055 isl_set_copy(dom), &todo_i, max);
6056
6057 if (max)
6058 res = isl_pw_multi_aff_union_lexmax(res, res_i);
6059 else
6060 res = isl_pw_multi_aff_union_lexmin(res, res_i);
6061
6062 todo = isl_set_intersect(todo, todo_i);
6063 }
6064
6065 isl_set_free(dom);
6066 isl_map_free(map);
6067
6068 if (empty)
6069 *empty = todo;
6070 else
6071 isl_set_free(todo);
6072
6073 return res;
6074error:
6075 if (empty)
6076 *empty = NULL;
6077 isl_set_free(dom);
6078 isl_map_free(map);
6079 return NULL;
6080}
6081
6082#undef TYPE
6083#define TYPE isl_map
6084#undef SUFFIX
6085#define SUFFIX
6086#undef EMPTY
6087#define EMPTY isl_map_empty
6088#undef ADD
6089#define ADD isl_map_union_disjoint
6090#include "isl_map_lexopt_templ.c"
6091
6092/* Given a map "map", compute the lexicographically minimal
6093 * (or maximal) image element for each domain element in dom.
6094 * Set *empty to those elements in dom that do not have an image element.
6095 *
6096 * We first compute the lexicographically minimal or maximal element
6097 * in the first basic map. This results in a partial solution "res"
6098 * and a subset "todo" of dom that still need to be handled.
6099 * We then consider each of the remaining maps in "map" and successively
6100 * update both "res" and "todo".
6101 *
6102 * Let res^k and todo^k be the results after k steps and let i = k + 1.
6103 * Assume we are computing the lexicographical maximum.
6104 * We first compute the lexicographically maximal element in basic map i.
6105 * This results in a partial solution res_i and a subset todo_i.
6106 * Then we combine these results with those obtain for the first k basic maps
6107 * to obtain a result that is valid for the first k+1 basic maps.
6108 * In particular, the set where there is no solution is the set where
6109 * there is no solution for the first k basic maps and also no solution
6110 * for the ith basic map, i.e.,
6111 *
6112 * todo^i = todo^k * todo_i
6113 *
6114 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
6115 * solutions, arbitrarily breaking ties in favor of res^k.
6116 * That is, when res^k(a) >= res_i(a), we pick res^k and
6117 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
6118 * the lexicographic order.)
6119 * In practice, we compute
6120 *
6121 * res^k * (res_i . "<=")
6122 *
6123 * and
6124 *
6125 * res_i * (res^k . "<")
6126 *
6127 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
6128 * where only one of res^k and res_i provides a solution and we simply pick
6129 * that one, i.e.,
6130 *
6131 * res^k * todo_i
6132 * and
6133 * res_i * todo^k
6134 *
6135 * Note that we only compute these intersections when dom(res^k) intersects
6136 * dom(res_i). Otherwise, the only effect of these intersections is to
6137 * potentially break up res^k and res_i into smaller pieces.
6138 * We want to avoid such splintering as much as possible.
6139 * In fact, an earlier implementation of this function would look for
6140 * better results in the domain of res^k and for extra results in todo^k,
6141 * but this would always result in a splintering according to todo^k,
6142 * even when the domain of basic map i is disjoint from the domains of
6143 * the previous basic maps.
6144 */
6145static __isl_give isl_map *isl_map_partial_lexopt_aligned(
6146 __isl_take isl_map *map, __isl_take isl_set *dom,
6147 __isl_give isl_set **empty, int max)
6148{
6149 int i;
6150 struct isl_map *res;
6151 struct isl_set *todo;
6152
6153 if (!map || !dom)
6154 goto error;
6155
6156 if (isl_map_plain_is_empty(map)) {
6157 if (empty)
6158 *empty = dom;
6159 else
6160 isl_set_free(dom);
6161 return map;
6162 }
6163
6164 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
6165 isl_set_copy(dom), &todo, max);
6166
6167 for (i = 1; i < map->n; ++i) {
6168 isl_map *lt, *le;
6169 isl_map *res_i;
6170 isl_set *todo_i;
6171 isl_space *dim = isl_space_range(isl_map_get_space(res));
6172
6173 res_i = basic_map_partial_lexopt(isl_basic_map_copy(map->p[i]),
6174 isl_set_copy(dom), &todo_i, max);
6175
6176 if (max) {
6177 lt = isl_map_lex_lt(isl_space_copy(dim));
6178 le = isl_map_lex_le(dim);
6179 } else {
6180 lt = isl_map_lex_gt(isl_space_copy(dim));
6181 le = isl_map_lex_ge(dim);
6182 }
6183 lt = isl_map_apply_range(isl_map_copy(res), lt);
6184 lt = isl_map_intersect(lt, isl_map_copy(res_i));
6185 le = isl_map_apply_range(isl_map_copy(res_i), le);
6186 le = isl_map_intersect(le, isl_map_copy(res));
6187
6188 if (!isl_map_is_empty(lt) || !isl_map_is_empty(le)) {
6189 res = isl_map_intersect_domain(res,
6190 isl_set_copy(todo_i));
6191 res_i = isl_map_intersect_domain(res_i,
6192 isl_set_copy(todo));
6193 }
6194
6195 res = isl_map_union_disjoint(res, res_i);
6196 res = isl_map_union_disjoint(res, lt);
6197 res = isl_map_union_disjoint(res, le);
6198
6199 todo = isl_set_intersect(todo, todo_i);
6200 }
6201
6202 isl_set_free(dom);
6203 isl_map_free(map);
6204
6205 if (empty)
6206 *empty = todo;
6207 else
6208 isl_set_free(todo);
6209
6210 return res;
6211error:
6212 if (empty)
6213 *empty = NULL;
6214 isl_set_free(dom);
6215 isl_map_free(map);
6216 return NULL;
6217}
6218
6219__isl_give isl_map *isl_map_partial_lexmax(
6220 __isl_take isl_map *map, __isl_take isl_set *dom,
6221 __isl_give isl_set **empty)
6222{
6223 return isl_map_partial_lexopt(map, dom, empty, 1);
6224}
6225
6226__isl_give isl_map *isl_map_partial_lexmin(
6227 __isl_take isl_map *map, __isl_take isl_set *dom,
6228 __isl_give isl_set **empty)
6229{
6230 return isl_map_partial_lexopt(map, dom, empty, 0);
6231}
6232
6233__isl_give isl_set *isl_set_partial_lexmin(
6234 __isl_take isl_set *set, __isl_take isl_set *dom,
6235 __isl_give isl_set **empty)
6236{
6237 return (struct isl_set *)
6238 isl_map_partial_lexmin((struct isl_map *)set,
6239 dom, empty);
6240}
6241
6242__isl_give isl_set *isl_set_partial_lexmax(
6243 __isl_take isl_set *set, __isl_take isl_set *dom,
6244 __isl_give isl_set **empty)
6245{
6246 return (struct isl_set *)
6247 isl_map_partial_lexmax((struct isl_map *)set,
6248 dom, empty);
6249}
6250
6251/* Compute the lexicographic minimum (or maximum if "max" is set)
6252 * of "bmap" over its domain.
6253 *
6254 * Since we are not interested in the part of the domain space where
6255 * there is no solution, we initialize the domain to those constraints
6256 * of "bmap" that only involve the parameters and the input dimensions.
6257 * This relieves the parametric programming engine from detecting those
6258 * inequalities and transferring them to the context. More importantly,
6259 * it ensures that those inequalities are transferred first and not
6260 * intermixed with inequalities that actually split the domain.
6261 */
6262__isl_give isl_map *isl_basic_map_lexopt(__isl_take isl_basic_map *bmap, int max)
6263{
6264 int n_div;
6265 int n_out;
6266 isl_basic_map *copy;
6267 isl_basic_set *dom;
6268
6269 n_div = isl_basic_map_dim(bmap, isl_dim_div);
6270 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6271 copy = isl_basic_map_copy(bmap);
6272 copy = isl_basic_map_drop_constraints_involving_dims(copy,
6273 isl_dim_div, 0, n_div);
6274 copy = isl_basic_map_drop_constraints_involving_dims(copy,
6275 isl_dim_out, 0, n_out);
6276 dom = isl_basic_map_domain(copy);
6277 return isl_basic_map_partial_lexopt(bmap, dom, NULL, max);
6278}
6279
6280__isl_give isl_map *isl_basic_map_lexmin(__isl_take isl_basic_map *bmap)
6281{
6282 return isl_basic_map_lexopt(bmap, 0);
6283}
6284
6285__isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
6286{
6287 return isl_basic_map_lexopt(bmap, 1);
6288}
6289
6290__isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
6291{
6292 return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
6293}
6294
6295__isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
6296{
6297 return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset);
6298}
6299
6300/* Extract the first and only affine expression from list
6301 * and then add it to *pwaff with the given dom.
6302 * This domain is known to be disjoint from other domains
6303 * because of the way isl_basic_map_foreach_lexmax works.
6304 */
6305static int update_dim_opt(__isl_take isl_basic_set *dom,
6306 __isl_take isl_aff_list *list, void *user)
6307{
6308 isl_ctx *ctx = isl_basic_set_get_ctx(dom);
6309 isl_aff *aff;
6310 isl_pw_aff **pwaff = user;
6311 isl_pw_aff *pwaff_i;
6312
6313 if (!list)
6314 goto error;
6315 if (isl_aff_list_n_aff(list) != 1)
6316 isl_die(ctx, isl_error_internal,
6317 "expecting single element list", goto error);
6318
6319 aff = isl_aff_list_get_aff(list, 0);
6320 pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
6321
6322 *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
6323
6324 isl_aff_list_free(list);
6325
6326 return 0;
6327error:
6328 isl_basic_set_free(dom);
6329 isl_aff_list_free(list);
6330 return -1;
6331}
6332
6333/* Given a basic map with one output dimension, compute the minimum or
6334 * maximum of that dimension as an isl_pw_aff.
6335 *
6336 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6337 * call update_dim_opt on each leaf of the result.
6338 */
6339static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
6340 int max)
6341{
6342 isl_space *dim = isl_basic_map_get_space(bmap);
6343 isl_pw_aff *pwaff;
6344 int r;
6345
6346 dim = isl_space_from_domain(isl_space_domain(dim));
6347 dim = isl_space_add_dims(dim, isl_dim_out, 1);
6348 pwaff = isl_pw_aff_empty(dim);
6349
6350 r = isl_basic_map_foreach_lexopt(bmap, max, &update_dim_opt, &pwaff);
6351 if (r < 0)
6352 return isl_pw_aff_free(pwaff);
6353
6354 return pwaff;
6355}
6356
6357/* Compute the minimum or maximum of the given output dimension
6358 * as a function of the parameters and the input dimensions,
6359 * but independently of the other output dimensions.
6360 *
6361 * We first project out the other output dimension and then compute
6362 * the "lexicographic" maximum in each basic map, combining the results
6363 * using isl_pw_aff_union_max.
6364 */
6365static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
6366 int max)
6367{
6368 int i;
6369 isl_pw_aff *pwaff;
6370 unsigned n_out;
6371
6372 n_out = isl_map_dim(map, isl_dim_out);
6373 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
6374 map = isl_map_project_out(map, isl_dim_out, 0, pos);
6375 if (!map)
6376 return NULL;
6377
6378 if (map->n == 0) {
6379 isl_space *dim = isl_map_get_space(map);
6380 isl_map_free(map);
6381 return isl_pw_aff_empty(dim);
6382 }
6383
6384 pwaff = basic_map_dim_opt(map->p[0], max);
6385 for (i = 1; i < map->n; ++i) {
6386 isl_pw_aff *pwaff_i;
6387
6388 pwaff_i = basic_map_dim_opt(map->p[i], max);
6389 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
6390 }
6391
6392 isl_map_free(map);
6393
6394 return pwaff;
6395}
6396
6397/* Compute the maximum of the given output dimension as a function of the
6398 * parameters and input dimensions, but independently of
6399 * the other output dimensions.
6400 */
6401__isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
6402{
6403 return map_dim_opt(map, pos, 1);
6404}
6405
6406/* Compute the minimum or maximum of the given set dimension
6407 * as a function of the parameters,
6408 * but independently of the other set dimensions.
6409 */
6410static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
6411 int max)
6412{
6413 return map_dim_opt(set, pos, max);
6414}
6415
6416/* Compute the maximum of the given set dimension as a function of the
6417 * parameters, but independently of the other set dimensions.
6418 */
6419__isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
6420{
6421 return set_dim_opt(set, pos, 1);
6422}
6423
6424/* Compute the minimum of the given set dimension as a function of the
6425 * parameters, but independently of the other set dimensions.
6426 */
6427__isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
6428{
6429 return set_dim_opt(set, pos, 0);
6430}
6431
6432/* Apply a preimage specified by "mat" on the parameters of "bset".
6433 * bset is assumed to have only parameters and divs.
6434 */
6435static struct isl_basic_set *basic_set_parameter_preimage(
6436 struct isl_basic_set *bset, struct isl_mat *mat)
6437{
6438 unsigned nparam;
6439
6440 if (!bset || !mat)
6441 goto error;
6442
6443 bset->dim = isl_space_cow(bset->dim);
6444 if (!bset->dim)
6445 goto error;
6446
6447 nparam = isl_basic_set_dim(bset, isl_dim_param);
6448
6449 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
6450
6451 bset->dim->nparam = 0;
6452 bset->dim->n_out = nparam;
6453 bset = isl_basic_set_preimage(bset, mat);
6454 if (bset) {
6455 bset->dim->nparam = bset->dim->n_out;
6456 bset->dim->n_out = 0;
6457 }
6458 return bset;
6459error:
6460 isl_mat_free(mat);
6461 isl_basic_set_free(bset);
6462 return NULL;
6463}
6464
6465/* Apply a preimage specified by "mat" on the parameters of "set".
6466 * set is assumed to have only parameters and divs.
6467 */
6468static struct isl_set *set_parameter_preimage(
6469 struct isl_set *set, struct isl_mat *mat)
6470{
6471 isl_space *dim = NULL;
6472 unsigned nparam;
6473
6474 if (!set || !mat)
6475 goto error;
6476
6477 dim = isl_space_copy(set->dim);
6478 dim = isl_space_cow(dim);
6479 if (!dim)
6480 goto error;
6481
6482 nparam = isl_set_dim(set, isl_dim_param);
6483
6484 isl_assert(set->ctx, mat->n_row == 1 + nparam, goto error);
6485
6486 dim->nparam = 0;
6487 dim->n_out = nparam;
6488 isl_set_reset_space(set, dim);
6489 set = isl_set_preimage(set, mat);
6490 if (!set)
6491 goto error2;
6492 dim = isl_space_copy(set->dim);
6493 dim = isl_space_cow(dim);
6494 if (!dim)
6495 goto error2;
6496 dim->nparam = dim->n_out;
6497 dim->n_out = 0;
6498 isl_set_reset_space(set, dim);
6499 return set;
6500error:
6501 isl_space_free(dim);
6502 isl_mat_free(mat);
6503error2:
6504 isl_set_free(set);
6505 return NULL;
6506}
6507
6508/* Intersect the basic set "bset" with the affine space specified by the
6509 * equalities in "eq".
6510 */
6511static struct isl_basic_set *basic_set_append_equalities(
6512 struct isl_basic_set *bset, struct isl_mat *eq)
6513{
6514 int i, k;
6515 unsigned len;
6516
6517 if (!bset || !eq)
6518 goto error;
6519
6520 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
6521 eq->n_row, 0);
6522 if (!bset)
6523 goto error;
6524
6525 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
6526 for (i = 0; i < eq->n_row; ++i) {
6527 k = isl_basic_set_alloc_equality(bset);
6528 if (k < 0)
6529 goto error;
6530 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
6531 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
6532 }
6533 isl_mat_free(eq);
6534
6535 bset = isl_basic_set_gauss(bset, NULL);
6536 bset = isl_basic_set_finalize(bset);
6537
6538 return bset;
6539error:
6540 isl_mat_free(eq);
6541 isl_basic_set_free(bset);
6542 return NULL;
6543}
6544
6545/* Intersect the set "set" with the affine space specified by the
6546 * equalities in "eq".
6547 */
6548static struct isl_set *set_append_equalities(struct isl_set *set,
6549 struct isl_mat *eq)
6550{
6551 int i;
6552
6553 if (!set || !eq)
6554 goto error;
6555
6556 for (i = 0; i < set->n; ++i) {
6557 set->p[i] = basic_set_append_equalities(set->p[i],
6558 isl_mat_copy(eq));
6559 if (!set->p[i])
6560 goto error;
6561 }
6562 isl_mat_free(eq);
6563 return set;
6564error:
6565 isl_mat_free(eq);
6566 isl_set_free(set);
6567 return NULL;
6568}
6569
6570/* Given a basic set "bset" that only involves parameters and existentially
6571 * quantified variables, return the index of the first equality
6572 * that only involves parameters. If there is no such equality then
6573 * return bset->n_eq.
6574 *
6575 * This function assumes that isl_basic_set_gauss has been called on "bset".
6576 */
6577static int first_parameter_equality(__isl_keep isl_basic_set *bset)
6578{
6579 int i, j;
6580 unsigned nparam, n_div;
6581
6582 if (!bset)
6583 return -1;
6584
6585 nparam = isl_basic_set_dim(bset, isl_dim_param);
6586 n_div = isl_basic_set_dim(bset, isl_dim_div);
6587
6588 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
6589 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
6590 ++i;
6591 }
6592
6593 return i;
6594}
6595
6596/* Compute an explicit representation for the existentially quantified
6597 * variables in "bset" by computing the "minimal value" of the set
6598 * variables. Since there are no set variables, the computation of
6599 * the minimal value essentially computes an explicit representation
6600 * of the non-empty part(s) of "bset".
6601 *
6602 * The input only involves parameters and existentially quantified variables.
6603 * All equalities among parameters have been removed.
6604 *
6605 * Since the existentially quantified variables in the result are in general
6606 * going to be different from those in the input, we first replace
6607 * them by the minimal number of variables based on their equalities.
6608 * This should simplify the parametric integer programming.
6609 */
6610static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
6611{
6612 isl_morph *morph1, *morph2;
6613 isl_set *set;
6614 unsigned n;
6615
6616 if (!bset)
6617 return NULL;
6618 if (bset->n_eq == 0)
6619 return isl_basic_set_lexmin(bset);
6620
6621 morph1 = isl_basic_set_parameter_compression(bset);
6622 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
6623 bset = isl_basic_set_lift(bset);
6624 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
6625 bset = isl_morph_basic_set(morph2, bset);
6626 n = isl_basic_set_dim(bset, isl_dim_set);
6627 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
6628
6629 set = isl_basic_set_lexmin(bset);
6630
6631 set = isl_morph_set(isl_morph_inverse(morph1), set);
6632
6633 return set;
6634}
6635
6636/* Project the given basic set onto its parameter domain, possibly introducing
6637 * new, explicit, existential variables in the constraints.
6638 * The input has parameters and (possibly implicit) existential variables.
6639 * The output has the same parameters, but only
6640 * explicit existentially quantified variables.
6641 *
6642 * The actual projection is performed by pip, but pip doesn't seem
6643 * to like equalities very much, so we first remove the equalities
6644 * among the parameters by performing a variable compression on
6645 * the parameters. Afterward, an inverse transformation is performed
6646 * and the equalities among the parameters are inserted back in.
6647 *
6648 * The variable compression on the parameters may uncover additional
6649 * equalities that were only implicit before. We therefore check
6650 * if there are any new parameter equalities in the result and
6651 * if so recurse. The removal of parameter equalities is required
6652 * for the parameter compression performed by base_compute_divs.
6653 */
6654static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
6655{
6656 int i;
6657 struct isl_mat *eq;
6658 struct isl_mat *T, *T2;
6659 struct isl_set *set;
6660 unsigned nparam;
6661
6662 bset = isl_basic_set_cow(bset);
6663 if (!bset)
6664 return NULL;
6665
6666 if (bset->n_eq == 0)
6667 return base_compute_divs(bset);
6668
6669 bset = isl_basic_set_gauss(bset, NULL);
6670 if (!bset)
6671 return NULL;
6672 if (isl_basic_set_plain_is_empty(bset))
6673 return isl_set_from_basic_set(bset);
6674
6675 i = first_parameter_equality(bset);
6676 if (i == bset->n_eq)
6677 return base_compute_divs(bset);
6678
6679 nparam = isl_basic_set_dim(bset, isl_dim_param);
6680 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
6681 0, 1 + nparam);
6682 eq = isl_mat_cow(eq);
6683 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
6684 if (T && T->n_col == 0) {
6685 isl_mat_free(T);
6686 isl_mat_free(T2);
6687 isl_mat_free(eq);
6688 bset = isl_basic_set_set_to_empty(bset);
6689 return isl_set_from_basic_set(bset);
6690 }
6691 bset = basic_set_parameter_preimage(bset, T);
6692
6693 i = first_parameter_equality(bset);
6694 if (!bset)
6695 set = NULL;
6696 else if (i == bset->n_eq)
6697 set = base_compute_divs(bset);
6698 else
6699 set = parameter_compute_divs(bset);
6700 set = set_parameter_preimage(set, T2);
6701 set = set_append_equalities(set, eq);
6702 return set;
6703}
6704
6705/* Insert the divs from "ls" before those of "bmap".
6706 *
6707 * The number of columns is not changed, which means that the last
6708 * dimensions of "bmap" are being reintepreted as the divs from "ls".
6709 * The caller is responsible for removing the same number of dimensions
6710 * from the space of "bmap".
6711 */
6712static __isl_give isl_basic_map *insert_divs_from_local_space(
6713 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
6714{
6715 int i;
6716 int n_div;
6717 int old_n_div;
6718
6719 n_div = isl_local_space_dim(ls, isl_dim_div);
6720 if (n_div == 0)
6721 return bmap;
6722
6723 old_n_div = bmap->n_div;
6724 bmap = insert_div_rows(bmap, n_div);
6725 if (!bmap)
6726 return NULL;
6727
6728 for (i = 0; i < n_div; ++i) {
6729 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
6730 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
6731 }
6732
6733 return bmap;
6734}
6735
6736/* Replace the space of "bmap" by the space and divs of "ls".
6737 *
6738 * If "ls" has any divs, then we simplify the result since we may
6739 * have discovered some additional equalities that could simplify
6740 * the div expressions.
6741 */
6742static __isl_give isl_basic_map *basic_replace_space_by_local_space(
6743 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
6744{
6745 int n_div;
6746
6747 bmap = isl_basic_map_cow(bmap);
6748 if (!bmap || !ls)
6749 goto error;
6750
6751 n_div = isl_local_space_dim(ls, isl_dim_div);
6752 bmap = insert_divs_from_local_space(bmap, ls);
6753 if (!bmap)
6754 goto error;
6755
6756 isl_space_free(bmap->dim);
6757 bmap->dim = isl_local_space_get_space(ls);
6758 if (!bmap->dim)
6759 goto error;
6760
6761 isl_local_space_free(ls);
6762 if (n_div > 0)
6763 bmap = isl_basic_map_simplify(bmap);
6764 bmap = isl_basic_map_finalize(bmap);
6765 return bmap;
6766error:
6767 isl_basic_map_free(bmap);
6768 isl_local_space_free(ls);
6769 return NULL;
6770}
6771
6772/* Replace the space of "map" by the space and divs of "ls".
6773 */
6774static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
6775 __isl_take isl_local_space *ls)
6776{
6777 int i;
6778
6779 map = isl_map_cow(map);
6780 if (!map || !ls)
6781 goto error;
6782
6783 for (i = 0; i < map->n; ++i) {
6784 map->p[i] = basic_replace_space_by_local_space(map->p[i],
6785 isl_local_space_copy(ls));
6786 if (!map->p[i])
6787 goto error;
6788 }
6789 isl_space_free(map->dim);
6790 map->dim = isl_local_space_get_space(ls);
6791 if (!map->dim)
6792 goto error;
6793
6794 isl_local_space_free(ls);
6795 return map;
6796error:
6797 isl_local_space_free(ls);
6798 isl_map_free(map);
6799 return NULL;
6800}
6801
6802/* Compute an explicit representation for the existentially
6803 * quantified variables for which do not know any explicit representation yet.
6804 *
6805 * We first sort the existentially quantified variables so that the
6806 * existentially quantified variables for which we already have an explicit
6807 * representation are placed before those for which we do not.
6808 * The input dimensions, the output dimensions and the existentially
6809 * quantified variables for which we already have an explicit
6810 * representation are then turned into parameters.
6811 * compute_divs returns a map with the same parameters and
6812 * no input or output dimensions and the dimension specification
6813 * is reset to that of the input, including the existentially quantified
6814 * variables for which we already had an explicit representation.
6815 */
6816static struct isl_map *compute_divs(struct isl_basic_map *bmap)
6817{
6818 struct isl_basic_set *bset;
6819 struct isl_set *set;
6820 struct isl_map *map;
6821 isl_space *dim;
6822 isl_local_space *ls;
6823 unsigned nparam;
6824 unsigned n_in;
6825 unsigned n_out;
6826 unsigned n_known;
6827 int i;
6828
6829 bmap = isl_basic_map_sort_divs(bmap);
6830 bmap = isl_basic_map_cow(bmap);
6831 if (!bmap)
6832 return NULL;
6833
6834 for (n_known = 0; n_known < bmap->n_div; ++n_known)
6835 if (isl_int_is_zero(bmap->div[n_known][0]))
6836 break;
6837
6838 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6839 n_in = isl_basic_map_dim(bmap, isl_dim_in);
6840 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6841 dim = isl_space_set_alloc(bmap->ctx,
6842 nparam + n_in + n_out + n_known, 0);
6843 if (!dim)
6844 goto error;
6845
6846 ls = isl_basic_map_get_local_space(bmap);
6847 ls = isl_local_space_drop_dims(ls, isl_dim_div,
6848 n_known, bmap->n_div - n_known);
6849 if (n_known > 0) {
6850 for (i = n_known; i < bmap->n_div; ++i)
6851 swap_div(bmap, i - n_known, i);
6852 bmap->n_div -= n_known;
6853 bmap->extra -= n_known;
6854 }
6855 bmap = isl_basic_map_reset_space(bmap, dim);
6856 bset = (struct isl_basic_set *)bmap;
6857
6858 set = parameter_compute_divs(bset);
6859 map = (struct isl_map *)set;
6860 map = replace_space_by_local_space(map, ls);
6861
6862 return map;
6863error:
6864 isl_basic_map_free(bmap);
6865 return NULL;
6866}
6867
6868int isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
6869{
6870 int i;
6871 unsigned off;
6872
6873 if (!bmap)
6874 return -1;
6875
6876 off = isl_space_dim(bmap->dim, isl_dim_all);
6877 for (i = 0; i < bmap->n_div; ++i) {
6878 if (isl_int_is_zero(bmap->div[i][0]))
6879 return 0;
6880 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
6881 return -1);
6882 }
6883 return 1;
6884}
6885
6886static int map_divs_known(__isl_keep isl_map *map)
6887{
6888 int i;
6889
6890 if (!map)
6891 return -1;
6892
6893 for (i = 0; i < map->n; ++i) {
6894 int known = isl_basic_map_divs_known(map->p[i]);
6895 if (known <= 0)
6896 return known;
6897 }
6898
6899 return 1;
6900}
6901
6902/* If bmap contains any unknown divs, then compute explicit
6903 * expressions for them. However, this computation may be
6904 * quite expensive, so first try to remove divs that aren't
6905 * strictly needed.
6906 */
6907struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
6908{
6909 int known;
6910 struct isl_map *map;
6911
6912 known = isl_basic_map_divs_known(bmap);
6913 if (known < 0)
6914 goto error;
6915 if (known)
6916 return isl_map_from_basic_map(bmap);
6917
6918 bmap = isl_basic_map_drop_redundant_divs(bmap);
6919
6920 known = isl_basic_map_divs_known(bmap);
6921 if (known < 0)
6922 goto error;
6923 if (known)
6924 return isl_map_from_basic_map(bmap);
6925
6926 map = compute_divs(bmap);
6927 return map;
6928error:
6929 isl_basic_map_free(bmap);
6930 return NULL;
6931}
6932
6933struct isl_map *isl_map_compute_divs(struct isl_map *map)
6934{
6935 int i;
6936 int known;
6937 struct isl_map *res;
6938
6939 if (!map)
6940 return NULL;
6941 if (map->n == 0)
6942 return map;
6943
6944 known = map_divs_known(map);
6945 if (known < 0) {
6946 isl_map_free(map);
6947 return NULL;
6948 }
6949 if (known)
6950 return map;
6951
6952 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
6953 for (i = 1 ; i < map->n; ++i) {
6954 struct isl_map *r2;
6955 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
6956 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
6957 res = isl_map_union_disjoint(res, r2);
6958 else
6959 res = isl_map_union(res, r2);
6960 }
6961 isl_map_free(map);
6962
6963 return res;
6964}
6965
6966struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
6967{
6968 return (struct isl_set *)
6969 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
6970}
6971
6972struct isl_set *isl_set_compute_divs(struct isl_set *set)
6973{
6974 return (struct isl_set *)
6975 isl_map_compute_divs((struct isl_map *)set);
6976}
6977
6978struct isl_set *isl_map_domain(struct isl_map *map)
6979{
6980 int i;
6981 struct isl_set *set;
6982
6983 if (!map)
6984 goto error;
6985
6986 map = isl_map_cow(map);
6987 if (!map)
6988 return NULL;
6989
6990 set = (struct isl_set *)map;
6991 set->dim = isl_space_domain(set->dim);
6992 if (!set->dim)
6993 goto error;
6994 for (i = 0; i < map->n; ++i) {
6995 set->p[i] = isl_basic_map_domain(map->p[i]);
6996 if (!set->p[i])
6997 goto error;
6998 }
6999 ISL_F_CLR(set, ISL_MAP_DISJOINT);
7000 ISL_F_CLR(set, ISL_SET_NORMALIZED);
7001 return set;
7002error:
7003 isl_map_free(map);
7004 return NULL;
7005}
7006
7007/* Return the union of "map1" and "map2", where we assume for now that
7008 * "map1" and "map2" are disjoint. Note that the basic maps inside
7009 * "map1" or "map2" may not be disjoint from each other.
7010 * Also note that this function is also called from isl_map_union,
7011 * which takes care of handling the situation where "map1" and "map2"
7012 * may not be disjoint.
7013 *
7014 * If one of the inputs is empty, we can simply return the other input.
7015 * Similarly, if one of the inputs is universal, then it is equal to the union.
7016 */
7017static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
7018 __isl_take isl_map *map2)
7019{
7020 int i;
7021 unsigned flags = 0;
7022 struct isl_map *map = NULL;
7023 int is_universe;
7024
7025 if (!map1 || !map2)
7026 goto error;
7027
7028 if (!isl_space_is_equal(map1->dim, map2->dim))
7029 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
7030 "spaces don't match", goto error);
7031
7032 if (map1->n == 0) {
7033 isl_map_free(map1);
7034 return map2;
7035 }
7036 if (map2->n == 0) {
7037 isl_map_free(map2);
7038 return map1;
7039 }
7040
7041 is_universe = isl_map_plain_is_universe(map1);
7042 if (is_universe < 0)
7043 goto error;
7044 if (is_universe) {
7045 isl_map_free(map2);
7046 return map1;
7047 }
7048
7049 is_universe = isl_map_plain_is_universe(map2);
7050 if (is_universe < 0)
7051 goto error;
7052 if (is_universe) {
7053 isl_map_free(map1);
7054 return map2;
7055 }
7056
7057 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7058 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7059 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7060
7061 map = isl_map_alloc_space(isl_space_copy(map1->dim),
7062 map1->n + map2->n, flags);
7063 if (!map)
7064 goto error;
7065 for (i = 0; i < map1->n; ++i) {
7066 map = isl_map_add_basic_map(map,
7067 isl_basic_map_copy(map1->p[i]));
7068 if (!map)
7069 goto error;
7070 }
7071 for (i = 0; i < map2->n; ++i) {
7072 map = isl_map_add_basic_map(map,
7073 isl_basic_map_copy(map2->p[i]));
7074 if (!map)
7075 goto error;
7076 }
7077 isl_map_free(map1);
7078 isl_map_free(map2);
7079 return map;
7080error:
7081 isl_map_free(map);
7082 isl_map_free(map1);
7083 isl_map_free(map2);
7084 return NULL;
7085}
7086
7087/* Return the union of "map1" and "map2", where "map1" and "map2" are
7088 * guaranteed to be disjoint by the caller.
7089 *
7090 * Note that this functions is called from within isl_map_make_disjoint,
7091 * so we have to be careful not to touch the constraints of the inputs
7092 * in any way.
7093 */
7094__isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
7095 __isl_take isl_map *map2)
7096{
7097 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
7098}
7099
7100/* Return the union of "map1" and "map2", where "map1" and "map2" may
7101 * not be disjoint. The parameters are assumed to have been aligned.
7102 *
7103 * We currently simply call map_union_disjoint, the internal operation
7104 * of which does not really depend on the inputs being disjoint.
7105 * If the result contains more than one basic map, then we clear
7106 * the disjoint flag since the result may contain basic maps from
7107 * both inputs and these are not guaranteed to be disjoint.
7108 *
7109 * As a special case, if "map1" and "map2" are obviously equal,
7110 * then we simply return "map1".
7111 */
7112static __isl_give isl_map *map_union_aligned(__isl_take isl_map *map1,
7113 __isl_take isl_map *map2)
7114{
7115 int equal;
7116
7117 if (!map1 || !map2)
7118 goto error;
7119
7120 equal = isl_map_plain_is_equal(map1, map2);
7121 if (equal < 0)
7122 goto error;
7123 if (equal) {
7124 isl_map_free(map2);
7125 return map1;
7126 }
7127
7128 map1 = map_union_disjoint(map1, map2);
7129 if (!map1)
7130 return NULL;
7131 if (map1->n > 1)
7132 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
7133 return map1;
7134error:
7135 isl_map_free(map1);
7136 isl_map_free(map2);
7137 return NULL;
7138}
7139
7140/* Return the union of "map1" and "map2", where "map1" and "map2" may
7141 * not be disjoint.
7142 */
7143__isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
7144 __isl_take isl_map *map2)
7145{
7146 return isl_map_align_params_map_map_and(map1, map2, &map_union_aligned);
7147}
7148
7149struct isl_set *isl_set_union_disjoint(
7150 struct isl_set *set1, struct isl_set *set2)
7151{
7152 return (struct isl_set *)
7153 isl_map_union_disjoint(
7154 (struct isl_map *)set1, (struct isl_map *)set2);
7155}
7156
7157struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
7158{
7159 return (struct isl_set *)
7160 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
7161}
7162
7163/* Apply "fn" to pairs of elements from "map" and "set" and collect
7164 * the results.
7165 *
7166 * "map" and "set" are assumed to be compatible and non-NULL.
7167 */
7168static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
7169 __isl_take isl_set *set,
7170 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
7171 __isl_take isl_basic_set *bset))
7172{
7173 unsigned flags = 0;
7174 struct isl_map *result;
7175 int i, j;
7176
7177 if (isl_set_plain_is_universe(set)) {
7178 isl_set_free(set);
7179 return map;
7180 }
7181
7182 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
7183 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
7184 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7185
7186 result = isl_map_alloc_space(isl_space_copy(map->dim),
7187 map->n * set->n, flags);
7188 for (i = 0; result && i < map->n; ++i)
7189 for (j = 0; j < set->n; ++j) {
7190 result = isl_map_add_basic_map(result,
7191 fn(isl_basic_map_copy(map->p[i]),
7192 isl_basic_set_copy(set->p[j])));
7193 if (!result)
7194 break;
7195 }
7196
7197 isl_map_free(map);
7198 isl_set_free(set);
7199 return result;
7200}
7201
7202static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
7203 __isl_take isl_set *set)
7204{
7205 if (!map || !set)
7206 goto error;
7207
7208 if (!isl_map_compatible_range(map, set))
7209 isl_die(set->ctx, isl_error_invalid,
7210 "incompatible spaces", goto error);
7211
7212 return map_intersect_set(map, set, &isl_basic_map_intersect_range);
7213error:
7214 isl_map_free(map);
7215 isl_set_free(set);
7216 return NULL;
7217}
7218
7219__isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
7220 __isl_take isl_set *set)
7221{
7222 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
7223}
7224
7225static __isl_give isl_map *map_intersect_domain(__isl_take isl_map *map,
7226 __isl_take isl_set *set)
7227{
7228 if (!map || !set)
7229 goto error;
7230
7231 if (!isl_map_compatible_domain(map, set))
7232 isl_die(set->ctx, isl_error_invalid,
7233 "incompatible spaces", goto error);
7234
7235 return map_intersect_set(map, set, &isl_basic_map_intersect_domain);
7236error:
7237 isl_map_free(map);
7238 isl_set_free(set);
7239 return NULL;
7240}
7241
7242__isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
7243 __isl_take isl_set *set)
7244{
7245 return isl_map_align_params_map_map_and(map, set,
7246 &map_intersect_domain);
7247}
7248
7249static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
7250 __isl_take isl_map *map2)
7251{
7252 if (!map1 || !map2)
7253 goto error;
7254 map1 = isl_map_reverse(map1);
7255 map1 = isl_map_apply_range(map1, map2);
7256 return isl_map_reverse(map1);
7257error:
7258 isl_map_free(map1);
7259 isl_map_free(map2);
7260 return NULL;
7261}
7262
7263__isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
7264 __isl_take isl_map *map2)
7265{
7266 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
7267}
7268
7269static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
7270 __isl_take isl_map *map2)
7271{
7272 isl_space *dim_result;
7273 struct isl_map *result;
7274 int i, j;
7275
7276 if (!map1 || !map2)
7277 goto error;
7278
7279 dim_result = isl_space_join(isl_space_copy(map1->dim),
7280 isl_space_copy(map2->dim));
7281
7282 result = isl_map_alloc_space(dim_result, map1->n * map2->n, 0);
7283 if (!result)
7284 goto error;
7285 for (i = 0; i < map1->n; ++i)
7286 for (j = 0; j < map2->n; ++j) {
7287 result = isl_map_add_basic_map(result,
7288 isl_basic_map_apply_range(
7289 isl_basic_map_copy(map1->p[i]),
7290 isl_basic_map_copy(map2->p[j])));
7291 if (!result)
7292 goto error;
7293 }
7294 isl_map_free(map1);
7295 isl_map_free(map2);
7296 if (result && result->n <= 1)
7297 ISL_F_SET(result, ISL_MAP_DISJOINT);
7298 return result;
7299error:
7300 isl_map_free(map1);
7301 isl_map_free(map2);
7302 return NULL;
7303}
7304
7305__isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
7306 __isl_take isl_map *map2)
7307{
7308 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
7309}
7310
7311/*
7312 * returns range - domain
7313 */
7314struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
7315{
Tobias Grosserb2f39922015-05-28 13:32:11 +00007316 isl_space *target_space;
Tobias Grosser52a25232015-02-04 20:55:43 +00007317 struct isl_basic_set *bset;
7318 unsigned dim;
7319 unsigned nparam;
7320 int i;
7321
7322 if (!bmap)
7323 goto error;
7324 isl_assert(bmap->ctx, isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7325 bmap->dim, isl_dim_out),
7326 goto error);
Tobias Grosserb2f39922015-05-28 13:32:11 +00007327 target_space = isl_space_domain(isl_basic_map_get_space(bmap));
Tobias Grosser52a25232015-02-04 20:55:43 +00007328 dim = isl_basic_map_n_in(bmap);
7329 nparam = isl_basic_map_n_param(bmap);
Tobias Grosserb2f39922015-05-28 13:32:11 +00007330 bmap = isl_basic_map_from_range(isl_basic_map_wrap(bmap));
7331 bmap = isl_basic_map_add_dims(bmap, isl_dim_in, dim);
7332 bmap = isl_basic_map_extend_constraints(bmap, dim, 0);
Tobias Grosser52a25232015-02-04 20:55:43 +00007333 for (i = 0; i < dim; ++i) {
Tobias Grosserb2f39922015-05-28 13:32:11 +00007334 int j = isl_basic_map_alloc_equality(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +00007335 if (j < 0) {
Tobias Grosserb2f39922015-05-28 13:32:11 +00007336 bmap = isl_basic_map_free(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +00007337 break;
7338 }
Tobias Grosserb2f39922015-05-28 13:32:11 +00007339 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7340 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7341 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], 1);
7342 isl_int_set_si(bmap->eq[j][1+nparam+2*dim+i], -1);
Tobias Grosser52a25232015-02-04 20:55:43 +00007343 }
Tobias Grosserb2f39922015-05-28 13:32:11 +00007344 bset = isl_basic_map_domain(bmap);
7345 bset = isl_basic_set_reset_space(bset, target_space);
Tobias Grosser52a25232015-02-04 20:55:43 +00007346 return bset;
7347error:
7348 isl_basic_map_free(bmap);
7349 return NULL;
7350}
7351
7352/*
7353 * returns range - domain
7354 */
7355__isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
7356{
7357 int i;
7358 isl_space *dim;
7359 struct isl_set *result;
7360
7361 if (!map)
7362 return NULL;
7363
7364 isl_assert(map->ctx, isl_space_tuple_is_equal(map->dim, isl_dim_in,
7365 map->dim, isl_dim_out),
7366 goto error);
7367 dim = isl_map_get_space(map);
7368 dim = isl_space_domain(dim);
7369 result = isl_set_alloc_space(dim, map->n, 0);
7370 if (!result)
7371 goto error;
7372 for (i = 0; i < map->n; ++i)
7373 result = isl_set_add_basic_set(result,
7374 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
7375 isl_map_free(map);
7376 return result;
7377error:
7378 isl_map_free(map);
7379 return NULL;
7380}
7381
7382/*
7383 * returns [domain -> range] -> range - domain
7384 */
7385__isl_give isl_basic_map *isl_basic_map_deltas_map(
7386 __isl_take isl_basic_map *bmap)
7387{
7388 int i, k;
7389 isl_space *dim;
7390 isl_basic_map *domain;
7391 int nparam, n;
7392 unsigned total;
7393
7394 if (!isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7395 bmap->dim, isl_dim_out))
7396 isl_die(bmap->ctx, isl_error_invalid,
7397 "domain and range don't match", goto error);
7398
7399 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7400 n = isl_basic_map_dim(bmap, isl_dim_in);
7401
7402 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
7403 domain = isl_basic_map_universe(dim);
7404
7405 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
7406 bmap = isl_basic_map_apply_range(bmap, domain);
7407 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
7408
7409 total = isl_basic_map_total_dim(bmap);
7410
7411 for (i = 0; i < n; ++i) {
7412 k = isl_basic_map_alloc_equality(bmap);
7413 if (k < 0)
7414 goto error;
7415 isl_seq_clr(bmap->eq[k], 1 + total);
7416 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
7417 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
7418 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
7419 }
7420
7421 bmap = isl_basic_map_gauss(bmap, NULL);
7422 return isl_basic_map_finalize(bmap);
7423error:
7424 isl_basic_map_free(bmap);
7425 return NULL;
7426}
7427
7428/*
7429 * returns [domain -> range] -> range - domain
7430 */
7431__isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
7432{
7433 int i;
7434 isl_space *domain_dim;
7435
7436 if (!map)
7437 return NULL;
7438
7439 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
7440 map->dim, isl_dim_out))
7441 isl_die(map->ctx, isl_error_invalid,
7442 "domain and range don't match", goto error);
7443
7444 map = isl_map_cow(map);
7445 if (!map)
7446 return NULL;
7447
7448 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
7449 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
7450 map->dim = isl_space_join(map->dim, domain_dim);
7451 if (!map->dim)
7452 goto error;
7453 for (i = 0; i < map->n; ++i) {
7454 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
7455 if (!map->p[i])
7456 goto error;
7457 }
7458 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
7459 return map;
7460error:
7461 isl_map_free(map);
7462 return NULL;
7463}
7464
7465static __isl_give isl_basic_map *basic_map_identity(__isl_take isl_space *dims)
7466{
7467 struct isl_basic_map *bmap;
7468 unsigned nparam;
7469 unsigned dim;
7470 int i;
7471
7472 if (!dims)
7473 return NULL;
7474
7475 nparam = dims->nparam;
7476 dim = dims->n_out;
7477 bmap = isl_basic_map_alloc_space(dims, 0, dim, 0);
7478 if (!bmap)
7479 goto error;
7480
7481 for (i = 0; i < dim; ++i) {
7482 int j = isl_basic_map_alloc_equality(bmap);
7483 if (j < 0)
7484 goto error;
7485 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7486 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7487 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
7488 }
7489 return isl_basic_map_finalize(bmap);
7490error:
7491 isl_basic_map_free(bmap);
7492 return NULL;
7493}
7494
7495__isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *dim)
7496{
7497 if (!dim)
7498 return NULL;
7499 if (dim->n_in != dim->n_out)
7500 isl_die(dim->ctx, isl_error_invalid,
7501 "number of input and output dimensions needs to be "
7502 "the same", goto error);
7503 return basic_map_identity(dim);
7504error:
7505 isl_space_free(dim);
7506 return NULL;
7507}
7508
Tobias Grosser52a25232015-02-04 20:55:43 +00007509__isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
7510{
7511 return isl_map_from_basic_map(isl_basic_map_identity(dim));
7512}
7513
Tobias Grosser52a25232015-02-04 20:55:43 +00007514__isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
7515{
7516 isl_space *dim = isl_set_get_space(set);
7517 isl_map *id;
7518 id = isl_map_identity(isl_space_map_from_set(dim));
7519 return isl_map_intersect_range(id, set);
7520}
7521
7522/* Construct a basic set with all set dimensions having only non-negative
7523 * values.
7524 */
7525__isl_give isl_basic_set *isl_basic_set_positive_orthant(
7526 __isl_take isl_space *space)
7527{
7528 int i;
7529 unsigned nparam;
7530 unsigned dim;
7531 struct isl_basic_set *bset;
7532
7533 if (!space)
7534 return NULL;
7535 nparam = space->nparam;
7536 dim = space->n_out;
7537 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
7538 if (!bset)
7539 return NULL;
7540 for (i = 0; i < dim; ++i) {
7541 int k = isl_basic_set_alloc_inequality(bset);
7542 if (k < 0)
7543 goto error;
7544 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
7545 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
7546 }
7547 return bset;
7548error:
7549 isl_basic_set_free(bset);
7550 return NULL;
7551}
7552
7553/* Construct the half-space x_pos >= 0.
7554 */
7555static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *dim,
7556 int pos)
7557{
7558 int k;
7559 isl_basic_set *nonneg;
7560
7561 nonneg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7562 k = isl_basic_set_alloc_inequality(nonneg);
7563 if (k < 0)
7564 goto error;
7565 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
7566 isl_int_set_si(nonneg->ineq[k][pos], 1);
7567
7568 return isl_basic_set_finalize(nonneg);
7569error:
7570 isl_basic_set_free(nonneg);
7571 return NULL;
7572}
7573
7574/* Construct the half-space x_pos <= -1.
7575 */
7576static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *dim, int pos)
7577{
7578 int k;
7579 isl_basic_set *neg;
7580
7581 neg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7582 k = isl_basic_set_alloc_inequality(neg);
7583 if (k < 0)
7584 goto error;
7585 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
7586 isl_int_set_si(neg->ineq[k][0], -1);
7587 isl_int_set_si(neg->ineq[k][pos], -1);
7588
7589 return isl_basic_set_finalize(neg);
7590error:
7591 isl_basic_set_free(neg);
7592 return NULL;
7593}
7594
7595__isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
7596 enum isl_dim_type type, unsigned first, unsigned n)
7597{
7598 int i;
Tobias Grossere0f8d592015-05-13 13:10:13 +00007599 unsigned offset;
Tobias Grosser52a25232015-02-04 20:55:43 +00007600 isl_basic_set *nonneg;
7601 isl_basic_set *neg;
7602
7603 if (!set)
7604 return NULL;
7605 if (n == 0)
7606 return set;
7607
7608 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
7609
Tobias Grossere0f8d592015-05-13 13:10:13 +00007610 offset = pos(set->dim, type);
Tobias Grosser52a25232015-02-04 20:55:43 +00007611 for (i = 0; i < n; ++i) {
7612 nonneg = nonneg_halfspace(isl_set_get_space(set),
Tobias Grossere0f8d592015-05-13 13:10:13 +00007613 offset + first + i);
7614 neg = neg_halfspace(isl_set_get_space(set), offset + first + i);
Tobias Grosser52a25232015-02-04 20:55:43 +00007615
7616 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
7617 }
7618
7619 return set;
7620error:
7621 isl_set_free(set);
7622 return NULL;
7623}
7624
7625static int foreach_orthant(__isl_take isl_set *set, int *signs, int first,
7626 int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7627 void *user)
7628{
7629 isl_set *half;
7630
7631 if (!set)
7632 return -1;
7633 if (isl_set_plain_is_empty(set)) {
7634 isl_set_free(set);
7635 return 0;
7636 }
7637 if (first == len)
7638 return fn(set, signs, user);
7639
7640 signs[first] = 1;
7641 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
7642 1 + first));
7643 half = isl_set_intersect(half, isl_set_copy(set));
7644 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
7645 goto error;
7646
7647 signs[first] = -1;
7648 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
7649 1 + first));
7650 half = isl_set_intersect(half, set);
7651 return foreach_orthant(half, signs, first + 1, len, fn, user);
7652error:
7653 isl_set_free(set);
7654 return -1;
7655}
7656
7657/* Call "fn" on the intersections of "set" with each of the orthants
7658 * (except for obviously empty intersections). The orthant is identified
7659 * by the signs array, with each entry having value 1 or -1 according
7660 * to the sign of the corresponding variable.
7661 */
7662int isl_set_foreach_orthant(__isl_keep isl_set *set,
7663 int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7664 void *user)
7665{
7666 unsigned nparam;
7667 unsigned nvar;
7668 int *signs;
7669 int r;
7670
7671 if (!set)
7672 return -1;
7673 if (isl_set_plain_is_empty(set))
7674 return 0;
7675
7676 nparam = isl_set_dim(set, isl_dim_param);
7677 nvar = isl_set_dim(set, isl_dim_set);
7678
7679 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
7680
7681 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
7682 fn, user);
7683
7684 free(signs);
7685
7686 return r;
7687}
7688
Tobias Grosserb2f39922015-05-28 13:32:11 +00007689isl_bool isl_set_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
Tobias Grosser52a25232015-02-04 20:55:43 +00007690{
7691 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
7692}
7693
Tobias Grosserb2f39922015-05-28 13:32:11 +00007694isl_bool isl_basic_map_is_subset(__isl_keep isl_basic_map *bmap1,
7695 __isl_keep isl_basic_map *bmap2)
Tobias Grosser52a25232015-02-04 20:55:43 +00007696{
7697 int is_subset;
7698 struct isl_map *map1;
7699 struct isl_map *map2;
7700
7701 if (!bmap1 || !bmap2)
Tobias Grosserb2f39922015-05-28 13:32:11 +00007702 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00007703
7704 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
7705 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
7706
7707 is_subset = isl_map_is_subset(map1, map2);
7708
7709 isl_map_free(map1);
7710 isl_map_free(map2);
7711
7712 return is_subset;
7713}
7714
Tobias Grosserb2f39922015-05-28 13:32:11 +00007715isl_bool isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
Tobias Grosser52a25232015-02-04 20:55:43 +00007716 __isl_keep isl_basic_set *bset2)
7717{
7718 return isl_basic_map_is_subset(bset1, bset2);
7719}
7720
Tobias Grosserb2f39922015-05-28 13:32:11 +00007721isl_bool isl_basic_map_is_equal(__isl_keep isl_basic_map *bmap1,
7722 __isl_keep isl_basic_map *bmap2)
Tobias Grosser52a25232015-02-04 20:55:43 +00007723{
Tobias Grosserb2f39922015-05-28 13:32:11 +00007724 isl_bool is_subset;
Tobias Grosser52a25232015-02-04 20:55:43 +00007725
7726 if (!bmap1 || !bmap2)
Tobias Grosserb2f39922015-05-28 13:32:11 +00007727 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00007728 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
Tobias Grosserb2f39922015-05-28 13:32:11 +00007729 if (is_subset != isl_bool_true)
Tobias Grosser52a25232015-02-04 20:55:43 +00007730 return is_subset;
7731 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7732 return is_subset;
7733}
7734
Tobias Grosserb2f39922015-05-28 13:32:11 +00007735isl_bool isl_basic_set_is_equal(__isl_keep isl_basic_set *bset1,
7736 __isl_keep isl_basic_set *bset2)
Tobias Grosser52a25232015-02-04 20:55:43 +00007737{
7738 return isl_basic_map_is_equal(
7739 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
7740}
7741
Tobias Grosserb2f39922015-05-28 13:32:11 +00007742isl_bool isl_map_is_empty(__isl_keep isl_map *map)
Tobias Grosser52a25232015-02-04 20:55:43 +00007743{
7744 int i;
7745 int is_empty;
7746
7747 if (!map)
Tobias Grosserb2f39922015-05-28 13:32:11 +00007748 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00007749 for (i = 0; i < map->n; ++i) {
7750 is_empty = isl_basic_map_is_empty(map->p[i]);
7751 if (is_empty < 0)
Tobias Grosserb2f39922015-05-28 13:32:11 +00007752 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00007753 if (!is_empty)
Tobias Grosserb2f39922015-05-28 13:32:11 +00007754 return isl_bool_false;
Tobias Grosser52a25232015-02-04 20:55:43 +00007755 }
Tobias Grosserb2f39922015-05-28 13:32:11 +00007756 return isl_bool_true;
Tobias Grosser52a25232015-02-04 20:55:43 +00007757}
7758
Tobias Grosserb2f39922015-05-28 13:32:11 +00007759isl_bool isl_map_plain_is_empty(__isl_keep isl_map *map)
Tobias Grosser52a25232015-02-04 20:55:43 +00007760{
Tobias Grosserb2f39922015-05-28 13:32:11 +00007761 return map ? map->n == 0 : isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00007762}
7763
Tobias Grosserb2f39922015-05-28 13:32:11 +00007764isl_bool isl_set_plain_is_empty(__isl_keep isl_set *set)
Tobias Grosser52a25232015-02-04 20:55:43 +00007765{
Tobias Grosserb2f39922015-05-28 13:32:11 +00007766 return set ? set->n == 0 : isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00007767}
7768
Tobias Grosserb2f39922015-05-28 13:32:11 +00007769isl_bool isl_set_is_empty(__isl_keep isl_set *set)
Tobias Grosser52a25232015-02-04 20:55:43 +00007770{
7771 return isl_map_is_empty((struct isl_map *)set);
7772}
7773
7774int isl_map_has_equal_space(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7775{
7776 if (!map1 || !map2)
7777 return -1;
7778
7779 return isl_space_is_equal(map1->dim, map2->dim);
7780}
7781
7782int isl_set_has_equal_space(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7783{
7784 if (!set1 || !set2)
7785 return -1;
7786
7787 return isl_space_is_equal(set1->dim, set2->dim);
7788}
7789
Tobias Grosserb2f39922015-05-28 13:32:11 +00007790static isl_bool map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
Tobias Grosser52a25232015-02-04 20:55:43 +00007791{
Tobias Grosserb2f39922015-05-28 13:32:11 +00007792 isl_bool is_subset;
Tobias Grosser52a25232015-02-04 20:55:43 +00007793
7794 if (!map1 || !map2)
Tobias Grosserb2f39922015-05-28 13:32:11 +00007795 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00007796 is_subset = isl_map_is_subset(map1, map2);
Tobias Grosserb2f39922015-05-28 13:32:11 +00007797 if (is_subset != isl_bool_true)
Tobias Grosser52a25232015-02-04 20:55:43 +00007798 return is_subset;
7799 is_subset = isl_map_is_subset(map2, map1);
7800 return is_subset;
7801}
7802
Tobias Grosserb2f39922015-05-28 13:32:11 +00007803isl_bool isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
Tobias Grosser52a25232015-02-04 20:55:43 +00007804{
7805 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
7806}
7807
Tobias Grosserb2f39922015-05-28 13:32:11 +00007808isl_bool isl_basic_map_is_strict_subset(
Tobias Grosser52a25232015-02-04 20:55:43 +00007809 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7810{
Tobias Grosserb2f39922015-05-28 13:32:11 +00007811 isl_bool is_subset;
Tobias Grosser52a25232015-02-04 20:55:43 +00007812
7813 if (!bmap1 || !bmap2)
Tobias Grosserb2f39922015-05-28 13:32:11 +00007814 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00007815 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
Tobias Grosserb2f39922015-05-28 13:32:11 +00007816 if (is_subset != isl_bool_true)
Tobias Grosser52a25232015-02-04 20:55:43 +00007817 return is_subset;
7818 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
Tobias Grosserb2f39922015-05-28 13:32:11 +00007819 if (is_subset == isl_bool_error)
Tobias Grosser52a25232015-02-04 20:55:43 +00007820 return is_subset;
7821 return !is_subset;
7822}
7823
Tobias Grosserb2f39922015-05-28 13:32:11 +00007824isl_bool isl_map_is_strict_subset(__isl_keep isl_map *map1,
7825 __isl_keep isl_map *map2)
Tobias Grosser52a25232015-02-04 20:55:43 +00007826{
Tobias Grosserb2f39922015-05-28 13:32:11 +00007827 isl_bool is_subset;
Tobias Grosser52a25232015-02-04 20:55:43 +00007828
7829 if (!map1 || !map2)
Tobias Grosserb2f39922015-05-28 13:32:11 +00007830 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00007831 is_subset = isl_map_is_subset(map1, map2);
Tobias Grosserb2f39922015-05-28 13:32:11 +00007832 if (is_subset != isl_bool_true)
Tobias Grosser52a25232015-02-04 20:55:43 +00007833 return is_subset;
7834 is_subset = isl_map_is_subset(map2, map1);
Tobias Grosserb2f39922015-05-28 13:32:11 +00007835 if (is_subset == isl_bool_error)
Tobias Grosser52a25232015-02-04 20:55:43 +00007836 return is_subset;
7837 return !is_subset;
7838}
7839
Tobias Grosserb2f39922015-05-28 13:32:11 +00007840isl_bool isl_set_is_strict_subset(__isl_keep isl_set *set1,
7841 __isl_keep isl_set *set2)
Tobias Grosser52a25232015-02-04 20:55:43 +00007842{
7843 return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
7844}
7845
Tobias Grosserb2f39922015-05-28 13:32:11 +00007846isl_bool isl_basic_map_is_universe(__isl_keep isl_basic_map *bmap)
Tobias Grosser52a25232015-02-04 20:55:43 +00007847{
7848 if (!bmap)
Tobias Grosserb2f39922015-05-28 13:32:11 +00007849 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00007850 return bmap->n_eq == 0 && bmap->n_ineq == 0;
7851}
7852
Tobias Grosserb2f39922015-05-28 13:32:11 +00007853isl_bool isl_basic_set_is_universe(__isl_keep isl_basic_set *bset)
Tobias Grosser52a25232015-02-04 20:55:43 +00007854{
7855 if (!bset)
Tobias Grosserb2f39922015-05-28 13:32:11 +00007856 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00007857 return bset->n_eq == 0 && bset->n_ineq == 0;
7858}
7859
Tobias Grosserb2f39922015-05-28 13:32:11 +00007860isl_bool isl_map_plain_is_universe(__isl_keep isl_map *map)
Tobias Grosser52a25232015-02-04 20:55:43 +00007861{
7862 int i;
7863
7864 if (!map)
Tobias Grosserb2f39922015-05-28 13:32:11 +00007865 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00007866
7867 for (i = 0; i < map->n; ++i) {
Tobias Grosserb2f39922015-05-28 13:32:11 +00007868 isl_bool r = isl_basic_map_is_universe(map->p[i]);
Tobias Grosser52a25232015-02-04 20:55:43 +00007869 if (r < 0 || r)
7870 return r;
7871 }
7872
Tobias Grosserb2f39922015-05-28 13:32:11 +00007873 return isl_bool_false;
Tobias Grosser52a25232015-02-04 20:55:43 +00007874}
7875
Tobias Grosserb2f39922015-05-28 13:32:11 +00007876isl_bool isl_set_plain_is_universe(__isl_keep isl_set *set)
Tobias Grosser52a25232015-02-04 20:55:43 +00007877{
7878 return isl_map_plain_is_universe((isl_map *) set);
7879}
7880
Tobias Grosserb2f39922015-05-28 13:32:11 +00007881isl_bool isl_basic_map_is_empty(__isl_keep isl_basic_map *bmap)
Tobias Grosser52a25232015-02-04 20:55:43 +00007882{
7883 struct isl_basic_set *bset = NULL;
7884 struct isl_vec *sample = NULL;
Tobias Grosserb2f39922015-05-28 13:32:11 +00007885 isl_bool empty;
Tobias Grosser52a25232015-02-04 20:55:43 +00007886 unsigned total;
7887
7888 if (!bmap)
Tobias Grosserb2f39922015-05-28 13:32:11 +00007889 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00007890
7891 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
Tobias Grosserb2f39922015-05-28 13:32:11 +00007892 return isl_bool_true;
Tobias Grosser52a25232015-02-04 20:55:43 +00007893
7894 if (isl_basic_map_is_universe(bmap))
Tobias Grosserb2f39922015-05-28 13:32:11 +00007895 return isl_bool_false;
Tobias Grosser52a25232015-02-04 20:55:43 +00007896
7897 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
7898 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
7899 copy = isl_basic_map_remove_redundancies(copy);
7900 empty = isl_basic_map_plain_is_empty(copy);
7901 isl_basic_map_free(copy);
7902 return empty;
7903 }
7904
7905 total = 1 + isl_basic_map_total_dim(bmap);
7906 if (bmap->sample && bmap->sample->size == total) {
7907 int contains = isl_basic_map_contains(bmap, bmap->sample);
7908 if (contains < 0)
Tobias Grosserb2f39922015-05-28 13:32:11 +00007909 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00007910 if (contains)
Tobias Grosserb2f39922015-05-28 13:32:11 +00007911 return isl_bool_false;
Tobias Grosser52a25232015-02-04 20:55:43 +00007912 }
7913 isl_vec_free(bmap->sample);
7914 bmap->sample = NULL;
7915 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
7916 if (!bset)
Tobias Grosserb2f39922015-05-28 13:32:11 +00007917 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00007918 sample = isl_basic_set_sample_vec(bset);
7919 if (!sample)
Tobias Grosserb2f39922015-05-28 13:32:11 +00007920 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00007921 empty = sample->size == 0;
7922 isl_vec_free(bmap->sample);
7923 bmap->sample = sample;
7924 if (empty)
7925 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
7926
7927 return empty;
7928}
7929
Tobias Grosserb2f39922015-05-28 13:32:11 +00007930isl_bool isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
Tobias Grosser52a25232015-02-04 20:55:43 +00007931{
7932 if (!bmap)
Tobias Grosserb2f39922015-05-28 13:32:11 +00007933 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00007934 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
7935}
7936
Tobias Grosserb2f39922015-05-28 13:32:11 +00007937isl_bool isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
Tobias Grosser52a25232015-02-04 20:55:43 +00007938{
7939 if (!bset)
Tobias Grosserb2f39922015-05-28 13:32:11 +00007940 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00007941 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
7942}
7943
Tobias Grosserb2f39922015-05-28 13:32:11 +00007944isl_bool isl_basic_set_is_empty(__isl_keep isl_basic_set *bset)
Tobias Grosser52a25232015-02-04 20:55:43 +00007945{
7946 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
7947}
7948
7949struct isl_map *isl_basic_map_union(
7950 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7951{
7952 struct isl_map *map;
7953 if (!bmap1 || !bmap2)
7954 goto error;
7955
7956 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
7957
7958 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
7959 if (!map)
7960 goto error;
7961 map = isl_map_add_basic_map(map, bmap1);
7962 map = isl_map_add_basic_map(map, bmap2);
7963 return map;
7964error:
7965 isl_basic_map_free(bmap1);
7966 isl_basic_map_free(bmap2);
7967 return NULL;
7968}
7969
7970struct isl_set *isl_basic_set_union(
7971 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
7972{
7973 return (struct isl_set *)isl_basic_map_union(
7974 (struct isl_basic_map *)bset1,
7975 (struct isl_basic_map *)bset2);
7976}
7977
7978/* Order divs such that any div only depends on previous divs */
7979struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
7980{
7981 int i;
7982 unsigned off;
7983
7984 if (!bmap)
7985 return NULL;
7986
7987 off = isl_space_dim(bmap->dim, isl_dim_all);
7988
7989 for (i = 0; i < bmap->n_div; ++i) {
7990 int pos;
7991 if (isl_int_is_zero(bmap->div[i][0]))
7992 continue;
7993 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
7994 bmap->n_div-i);
7995 if (pos == -1)
7996 continue;
7997 isl_basic_map_swap_div(bmap, i, i + pos);
7998 --i;
7999 }
8000 return bmap;
8001}
8002
8003struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
8004{
8005 return (struct isl_basic_set *)
8006 isl_basic_map_order_divs((struct isl_basic_map *)bset);
8007}
8008
8009__isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
8010{
8011 int i;
8012
8013 if (!map)
8014 return 0;
8015
8016 for (i = 0; i < map->n; ++i) {
8017 map->p[i] = isl_basic_map_order_divs(map->p[i]);
8018 if (!map->p[i])
8019 goto error;
8020 }
8021
8022 return map;
8023error:
8024 isl_map_free(map);
8025 return NULL;
8026}
8027
8028/* Apply the expansion computed by isl_merge_divs.
8029 * The expansion itself is given by "exp" while the resulting
8030 * list of divs is given by "div".
8031 */
8032__isl_give isl_basic_set *isl_basic_set_expand_divs(
8033 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
8034{
8035 int i, j;
8036 int n_div;
8037
8038 bset = isl_basic_set_cow(bset);
8039 if (!bset || !div)
8040 goto error;
8041
8042 if (div->n_row < bset->n_div)
8043 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
8044 "not an expansion", goto error);
8045
8046 n_div = bset->n_div;
8047 bset = isl_basic_map_extend_space(bset, isl_space_copy(bset->dim),
8048 div->n_row - n_div, 0,
8049 2 * (div->n_row - n_div));
8050
8051 for (i = n_div; i < div->n_row; ++i)
8052 if (isl_basic_set_alloc_div(bset) < 0)
8053 goto error;
8054
8055 j = n_div - 1;
8056 for (i = div->n_row - 1; i >= 0; --i) {
8057 if (j >= 0 && exp[j] == i) {
8058 if (i != j)
8059 isl_basic_map_swap_div(bset, i, j);
8060 j--;
8061 } else {
8062 isl_seq_cpy(bset->div[i], div->row[i], div->n_col);
8063 if (isl_basic_map_add_div_constraints(bset, i) < 0)
8064 goto error;
8065 }
8066 }
8067
8068 isl_mat_free(div);
8069 return bset;
8070error:
8071 isl_basic_set_free(bset);
8072 isl_mat_free(div);
8073 return NULL;
8074}
8075
8076/* Look for a div in dst that corresponds to the div "div" in src.
8077 * The divs before "div" in src and dst are assumed to be the same.
8078 *
8079 * Returns -1 if no corresponding div was found and the position
8080 * of the corresponding div in dst otherwise.
8081 */
8082static int find_div(struct isl_basic_map *dst,
8083 struct isl_basic_map *src, unsigned div)
8084{
8085 int i;
8086
8087 unsigned total = isl_space_dim(src->dim, isl_dim_all);
8088
8089 isl_assert(dst->ctx, div <= dst->n_div, return -1);
8090 for (i = div; i < dst->n_div; ++i)
8091 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
8092 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
8093 dst->n_div - div) == -1)
8094 return i;
8095 return -1;
8096}
8097
8098/* Align the divs of "dst" to those of "src", adding divs from "src"
8099 * if needed. That is, make sure that the first src->n_div divs
8100 * of the result are equal to those of src.
8101 *
8102 * The result is not finalized as by design it will have redundant
8103 * divs if any divs from "src" were copied.
8104 */
8105__isl_give isl_basic_map *isl_basic_map_align_divs(
8106 __isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src)
8107{
8108 int i;
8109 int known, extended;
8110 unsigned total;
8111
8112 if (!dst || !src)
8113 return isl_basic_map_free(dst);
8114
8115 if (src->n_div == 0)
8116 return dst;
8117
8118 known = isl_basic_map_divs_known(src);
8119 if (known < 0)
8120 return isl_basic_map_free(dst);
8121 if (!known)
8122 isl_die(isl_basic_map_get_ctx(src), isl_error_invalid,
8123 "some src divs are unknown",
8124 return isl_basic_map_free(dst));
8125
8126 src = isl_basic_map_order_divs(src);
8127
8128 extended = 0;
8129 total = isl_space_dim(src->dim, isl_dim_all);
8130 for (i = 0; i < src->n_div; ++i) {
8131 int j = find_div(dst, src, i);
8132 if (j < 0) {
8133 if (!extended) {
8134 int extra = src->n_div - i;
8135 dst = isl_basic_map_cow(dst);
8136 if (!dst)
8137 return NULL;
8138 dst = isl_basic_map_extend_space(dst,
8139 isl_space_copy(dst->dim),
8140 extra, 0, 2 * extra);
8141 extended = 1;
8142 }
8143 j = isl_basic_map_alloc_div(dst);
8144 if (j < 0)
8145 return isl_basic_map_free(dst);
8146 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
8147 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
8148 if (isl_basic_map_add_div_constraints(dst, j) < 0)
8149 return isl_basic_map_free(dst);
8150 }
8151 if (j != i)
8152 isl_basic_map_swap_div(dst, i, j);
8153 }
8154 return dst;
8155}
8156
8157struct isl_basic_set *isl_basic_set_align_divs(
8158 struct isl_basic_set *dst, struct isl_basic_set *src)
8159{
8160 return (struct isl_basic_set *)isl_basic_map_align_divs(
8161 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
8162}
8163
8164struct isl_map *isl_map_align_divs(struct isl_map *map)
8165{
8166 int i;
8167
8168 if (!map)
8169 return NULL;
8170 if (map->n == 0)
8171 return map;
8172 map = isl_map_compute_divs(map);
8173 map = isl_map_cow(map);
8174 if (!map)
8175 return NULL;
8176
8177 for (i = 1; i < map->n; ++i)
8178 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
8179 for (i = 1; i < map->n; ++i) {
8180 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
8181 if (!map->p[i])
8182 return isl_map_free(map);
8183 }
8184
8185 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
8186 return map;
8187}
8188
8189struct isl_set *isl_set_align_divs(struct isl_set *set)
8190{
8191 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
8192}
8193
Tobias Grosser1fa7b972015-02-16 19:33:40 +00008194/* Align the divs of the basic maps in "map" to those
8195 * of the basic maps in "list", as well as to the other basic maps in "map".
Tobias Grosser52a25232015-02-04 20:55:43 +00008196 * The elements in "list" are assumed to have known divs.
8197 */
Tobias Grosser1fa7b972015-02-16 19:33:40 +00008198__isl_give isl_map *isl_map_align_divs_to_basic_map_list(
8199 __isl_take isl_map *map, __isl_keep isl_basic_map_list *list)
Tobias Grosser52a25232015-02-04 20:55:43 +00008200{
8201 int i, n;
8202
Tobias Grosser1fa7b972015-02-16 19:33:40 +00008203 map = isl_map_compute_divs(map);
8204 map = isl_map_cow(map);
8205 if (!map || !list)
8206 return isl_map_free(map);
8207 if (map->n == 0)
8208 return map;
Tobias Grosser52a25232015-02-04 20:55:43 +00008209
Tobias Grosser1fa7b972015-02-16 19:33:40 +00008210 n = isl_basic_map_list_n_basic_map(list);
Tobias Grosser52a25232015-02-04 20:55:43 +00008211 for (i = 0; i < n; ++i) {
Tobias Grosser1fa7b972015-02-16 19:33:40 +00008212 isl_basic_map *bmap;
Tobias Grosser52a25232015-02-04 20:55:43 +00008213
Tobias Grosser1fa7b972015-02-16 19:33:40 +00008214 bmap = isl_basic_map_list_get_basic_map(list, i);
8215 map->p[0] = isl_basic_map_align_divs(map->p[0], bmap);
8216 isl_basic_map_free(bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +00008217 }
Tobias Grosser1fa7b972015-02-16 19:33:40 +00008218 if (!map->p[0])
8219 return isl_map_free(map);
Tobias Grosser52a25232015-02-04 20:55:43 +00008220
Tobias Grosser1fa7b972015-02-16 19:33:40 +00008221 return isl_map_align_divs(map);
Tobias Grosser52a25232015-02-04 20:55:43 +00008222}
8223
Tobias Grosser1fa7b972015-02-16 19:33:40 +00008224/* Align the divs of each element of "list" to those of "bmap".
8225 * Both "bmap" and the elements of "list" are assumed to have known divs.
Tobias Grosser52a25232015-02-04 20:55:43 +00008226 */
Tobias Grosser1fa7b972015-02-16 19:33:40 +00008227__isl_give isl_basic_map_list *isl_basic_map_list_align_divs_to_basic_map(
8228 __isl_take isl_basic_map_list *list, __isl_keep isl_basic_map *bmap)
Tobias Grosser52a25232015-02-04 20:55:43 +00008229{
8230 int i, n;
8231
Tobias Grosser1fa7b972015-02-16 19:33:40 +00008232 if (!list || !bmap)
8233 return isl_basic_map_list_free(list);
Tobias Grosser52a25232015-02-04 20:55:43 +00008234
Tobias Grosser1fa7b972015-02-16 19:33:40 +00008235 n = isl_basic_map_list_n_basic_map(list);
Tobias Grosser52a25232015-02-04 20:55:43 +00008236 for (i = 0; i < n; ++i) {
Tobias Grosser1fa7b972015-02-16 19:33:40 +00008237 isl_basic_map *bmap_i;
Tobias Grosser52a25232015-02-04 20:55:43 +00008238
Tobias Grosser1fa7b972015-02-16 19:33:40 +00008239 bmap_i = isl_basic_map_list_get_basic_map(list, i);
8240 bmap_i = isl_basic_map_align_divs(bmap_i, bmap);
8241 list = isl_basic_map_list_set_basic_map(list, i, bmap_i);
Tobias Grosser52a25232015-02-04 20:55:43 +00008242 }
8243
8244 return list;
8245}
8246
8247static __isl_give isl_set *set_apply( __isl_take isl_set *set,
8248 __isl_take isl_map *map)
8249{
8250 if (!set || !map)
8251 goto error;
8252 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
8253 map = isl_map_intersect_domain(map, set);
8254 set = isl_map_range(map);
8255 return set;
8256error:
8257 isl_set_free(set);
8258 isl_map_free(map);
8259 return NULL;
8260}
8261
8262__isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
8263 __isl_take isl_map *map)
8264{
8265 return isl_map_align_params_map_map_and(set, map, &set_apply);
8266}
8267
8268/* There is no need to cow as removing empty parts doesn't change
8269 * the meaning of the set.
8270 */
8271struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
8272{
8273 int i;
8274
8275 if (!map)
8276 return NULL;
8277
8278 for (i = map->n - 1; i >= 0; --i)
8279 remove_if_empty(map, i);
8280
8281 return map;
8282}
8283
8284struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
8285{
8286 return (struct isl_set *)
8287 isl_map_remove_empty_parts((struct isl_map *)set);
8288}
8289
8290struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
8291{
8292 struct isl_basic_map *bmap;
8293 if (!map || map->n == 0)
8294 return NULL;
8295 bmap = map->p[map->n-1];
8296 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
8297 return isl_basic_map_copy(bmap);
8298}
8299
8300struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
8301{
8302 return (struct isl_basic_set *)
8303 isl_map_copy_basic_map((struct isl_map *)set);
8304}
8305
8306__isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
8307 __isl_keep isl_basic_map *bmap)
8308{
8309 int i;
8310
8311 if (!map || !bmap)
8312 goto error;
8313 for (i = map->n-1; i >= 0; --i) {
8314 if (map->p[i] != bmap)
8315 continue;
8316 map = isl_map_cow(map);
8317 if (!map)
8318 goto error;
8319 isl_basic_map_free(map->p[i]);
8320 if (i != map->n-1) {
8321 ISL_F_CLR(map, ISL_SET_NORMALIZED);
8322 map->p[i] = map->p[map->n-1];
8323 }
8324 map->n--;
8325 return map;
8326 }
8327 return map;
8328error:
8329 isl_map_free(map);
8330 return NULL;
8331}
8332
8333struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
8334 struct isl_basic_set *bset)
8335{
8336 return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
8337 (struct isl_basic_map *)bset);
8338}
8339
8340/* Given two basic sets bset1 and bset2, compute the maximal difference
8341 * between the values of dimension pos in bset1 and those in bset2
8342 * for any common value of the parameters and dimensions preceding pos.
8343 */
8344static enum isl_lp_result basic_set_maximal_difference_at(
8345 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
8346 int pos, isl_int *opt)
8347{
8348 isl_space *dims;
8349 struct isl_basic_map *bmap1 = NULL;
8350 struct isl_basic_map *bmap2 = NULL;
8351 struct isl_ctx *ctx;
8352 struct isl_vec *obj;
8353 unsigned total;
8354 unsigned nparam;
8355 unsigned dim1, dim2;
8356 enum isl_lp_result res;
8357
8358 if (!bset1 || !bset2)
8359 return isl_lp_error;
8360
8361 nparam = isl_basic_set_n_param(bset1);
8362 dim1 = isl_basic_set_n_dim(bset1);
8363 dim2 = isl_basic_set_n_dim(bset2);
8364 dims = isl_space_alloc(bset1->ctx, nparam, pos, dim1 - pos);
8365 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
8366 dims = isl_space_alloc(bset2->ctx, nparam, pos, dim2 - pos);
8367 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
8368 if (!bmap1 || !bmap2)
8369 goto error;
8370 bmap1 = isl_basic_map_cow(bmap1);
8371 bmap1 = isl_basic_map_extend(bmap1, nparam,
8372 pos, (dim1 - pos) + (dim2 - pos),
8373 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
8374 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
8375 if (!bmap1)
8376 goto error2;
8377 total = isl_basic_map_total_dim(bmap1);
8378 ctx = bmap1->ctx;
8379 obj = isl_vec_alloc(ctx, 1 + total);
8380 if (!obj)
8381 goto error2;
8382 isl_seq_clr(obj->block.data, 1 + total);
8383 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
8384 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
8385 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
8386 opt, NULL, NULL);
8387 isl_basic_map_free(bmap1);
8388 isl_vec_free(obj);
8389 return res;
8390error:
8391 isl_basic_map_free(bmap2);
8392error2:
8393 isl_basic_map_free(bmap1);
8394 return isl_lp_error;
8395}
8396
8397/* Given two _disjoint_ basic sets bset1 and bset2, check whether
8398 * for any common value of the parameters and dimensions preceding pos
8399 * in both basic sets, the values of dimension pos in bset1 are
8400 * smaller or larger than those in bset2.
8401 *
8402 * Returns
8403 * 1 if bset1 follows bset2
8404 * -1 if bset1 precedes bset2
8405 * 0 if bset1 and bset2 are incomparable
8406 * -2 if some error occurred.
8407 */
8408int isl_basic_set_compare_at(struct isl_basic_set *bset1,
8409 struct isl_basic_set *bset2, int pos)
8410{
8411 isl_int opt;
8412 enum isl_lp_result res;
8413 int cmp;
8414
8415 isl_int_init(opt);
8416
8417 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8418
8419 if (res == isl_lp_empty)
8420 cmp = 0;
8421 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8422 res == isl_lp_unbounded)
8423 cmp = 1;
8424 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8425 cmp = -1;
8426 else
8427 cmp = -2;
8428
8429 isl_int_clear(opt);
8430 return cmp;
8431}
8432
8433/* Given two basic sets bset1 and bset2, check whether
8434 * for any common value of the parameters and dimensions preceding pos
8435 * there is a value of dimension pos in bset1 that is larger
8436 * than a value of the same dimension in bset2.
8437 *
8438 * Return
8439 * 1 if there exists such a pair
8440 * 0 if there is no such pair, but there is a pair of equal values
8441 * -1 otherwise
8442 * -2 if some error occurred.
8443 */
8444int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
8445 __isl_keep isl_basic_set *bset2, int pos)
8446{
8447 isl_int opt;
8448 enum isl_lp_result res;
8449 int cmp;
8450
8451 isl_int_init(opt);
8452
8453 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8454
8455 if (res == isl_lp_empty)
8456 cmp = -1;
8457 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8458 res == isl_lp_unbounded)
8459 cmp = 1;
8460 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8461 cmp = -1;
8462 else if (res == isl_lp_ok)
8463 cmp = 0;
8464 else
8465 cmp = -2;
8466
8467 isl_int_clear(opt);
8468 return cmp;
8469}
8470
8471/* Given two sets set1 and set2, check whether
8472 * for any common value of the parameters and dimensions preceding pos
8473 * there is a value of dimension pos in set1 that is larger
8474 * than a value of the same dimension in set2.
8475 *
8476 * Return
8477 * 1 if there exists such a pair
8478 * 0 if there is no such pair, but there is a pair of equal values
8479 * -1 otherwise
8480 * -2 if some error occurred.
8481 */
8482int isl_set_follows_at(__isl_keep isl_set *set1,
8483 __isl_keep isl_set *set2, int pos)
8484{
8485 int i, j;
8486 int follows = -1;
8487
8488 if (!set1 || !set2)
8489 return -2;
8490
8491 for (i = 0; i < set1->n; ++i)
8492 for (j = 0; j < set2->n; ++j) {
8493 int f;
8494 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
8495 if (f == 1 || f == -2)
8496 return f;
8497 if (f > follows)
8498 follows = f;
8499 }
8500
8501 return follows;
8502}
8503
8504static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map *bmap,
8505 unsigned pos, isl_int *val)
8506{
8507 int i;
8508 int d;
8509 unsigned total;
8510
8511 if (!bmap)
8512 return -1;
8513 total = isl_basic_map_total_dim(bmap);
8514 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
8515 for (; d+1 > pos; --d)
8516 if (!isl_int_is_zero(bmap->eq[i][1+d]))
8517 break;
8518 if (d != pos)
8519 continue;
8520 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
8521 return 0;
8522 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
8523 return 0;
8524 if (!isl_int_is_one(bmap->eq[i][1+d]))
8525 return 0;
8526 if (val)
8527 isl_int_neg(*val, bmap->eq[i][0]);
8528 return 1;
8529 }
8530 return 0;
8531}
8532
8533static int isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
8534 unsigned pos, isl_int *val)
8535{
8536 int i;
8537 isl_int v;
8538 isl_int tmp;
8539 int fixed;
8540
8541 if (!map)
8542 return -1;
8543 if (map->n == 0)
8544 return 0;
8545 if (map->n == 1)
8546 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
8547 isl_int_init(v);
8548 isl_int_init(tmp);
8549 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
8550 for (i = 1; fixed == 1 && i < map->n; ++i) {
8551 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
8552 if (fixed == 1 && isl_int_ne(tmp, v))
8553 fixed = 0;
8554 }
8555 if (val)
8556 isl_int_set(*val, v);
8557 isl_int_clear(tmp);
8558 isl_int_clear(v);
8559 return fixed;
8560}
8561
8562static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set *bset,
8563 unsigned pos, isl_int *val)
8564{
8565 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map *)bset,
8566 pos, val);
8567}
8568
8569static int isl_set_plain_has_fixed_var(__isl_keep isl_set *set, unsigned pos,
8570 isl_int *val)
8571{
8572 return isl_map_plain_has_fixed_var((struct isl_map *)set, pos, val);
8573}
8574
8575int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
8576 enum isl_dim_type type, unsigned pos, isl_int *val)
8577{
8578 if (pos >= isl_basic_map_dim(bmap, type))
8579 return -1;
8580 return isl_basic_map_plain_has_fixed_var(bmap,
8581 isl_basic_map_offset(bmap, type) - 1 + pos, val);
8582}
8583
8584/* If "bmap" obviously lies on a hyperplane where the given dimension
8585 * has a fixed value, then return that value.
8586 * Otherwise return NaN.
8587 */
8588__isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
8589 __isl_keep isl_basic_map *bmap,
8590 enum isl_dim_type type, unsigned pos)
8591{
8592 isl_ctx *ctx;
8593 isl_val *v;
8594 int fixed;
8595
8596 if (!bmap)
8597 return NULL;
8598 ctx = isl_basic_map_get_ctx(bmap);
8599 v = isl_val_alloc(ctx);
8600 if (!v)
8601 return NULL;
8602 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
8603 if (fixed < 0)
8604 return isl_val_free(v);
8605 if (fixed) {
8606 isl_int_set_si(v->d, 1);
8607 return v;
8608 }
8609 isl_val_free(v);
8610 return isl_val_nan(ctx);
8611}
8612
8613int isl_map_plain_is_fixed(__isl_keep isl_map *map,
8614 enum isl_dim_type type, unsigned pos, isl_int *val)
8615{
8616 if (pos >= isl_map_dim(map, type))
8617 return -1;
8618 return isl_map_plain_has_fixed_var(map,
8619 map_offset(map, type) - 1 + pos, val);
8620}
8621
8622/* If "map" obviously lies on a hyperplane where the given dimension
8623 * has a fixed value, then return that value.
8624 * Otherwise return NaN.
8625 */
8626__isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
8627 enum isl_dim_type type, unsigned pos)
8628{
8629 isl_ctx *ctx;
8630 isl_val *v;
8631 int fixed;
8632
8633 if (!map)
8634 return NULL;
8635 ctx = isl_map_get_ctx(map);
8636 v = isl_val_alloc(ctx);
8637 if (!v)
8638 return NULL;
8639 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
8640 if (fixed < 0)
8641 return isl_val_free(v);
8642 if (fixed) {
8643 isl_int_set_si(v->d, 1);
8644 return v;
8645 }
8646 isl_val_free(v);
8647 return isl_val_nan(ctx);
8648}
8649
8650/* If "set" obviously lies on a hyperplane where the given dimension
8651 * has a fixed value, then return that value.
8652 * Otherwise return NaN.
8653 */
8654__isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
8655 enum isl_dim_type type, unsigned pos)
8656{
8657 return isl_map_plain_get_val_if_fixed(set, type, pos);
8658}
8659
8660int isl_set_plain_is_fixed(__isl_keep isl_set *set,
8661 enum isl_dim_type type, unsigned pos, isl_int *val)
8662{
8663 return isl_map_plain_is_fixed(set, type, pos, val);
8664}
8665
Tobias Grosser52a25232015-02-04 20:55:43 +00008666/* Check if dimension dim has fixed value and if so and if val is not NULL,
8667 * then return this fixed value in *val.
8668 */
8669int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
8670 unsigned dim, isl_int *val)
8671{
8672 return isl_basic_set_plain_has_fixed_var(bset,
8673 isl_basic_set_n_param(bset) + dim, val);
8674}
8675
8676/* Check if dimension dim has fixed value and if so and if val is not NULL,
8677 * then return this fixed value in *val.
8678 */
8679int isl_set_plain_dim_is_fixed(__isl_keep isl_set *set,
8680 unsigned dim, isl_int *val)
8681{
8682 return isl_set_plain_has_fixed_var(set, isl_set_n_param(set) + dim, val);
8683}
8684
Tobias Grosser52a25232015-02-04 20:55:43 +00008685/* Check if input variable in has fixed value and if so and if val is not NULL,
8686 * then return this fixed value in *val.
8687 */
8688int isl_map_plain_input_is_fixed(__isl_keep isl_map *map,
8689 unsigned in, isl_int *val)
8690{
8691 return isl_map_plain_has_fixed_var(map, isl_map_n_param(map) + in, val);
8692}
8693
8694/* Check if dimension dim has an (obvious) fixed lower bound and if so
8695 * and if val is not NULL, then return this lower bound in *val.
8696 */
8697int isl_basic_set_plain_dim_has_fixed_lower_bound(
8698 __isl_keep isl_basic_set *bset, unsigned dim, isl_int *val)
8699{
8700 int i, i_eq = -1, i_ineq = -1;
8701 isl_int *c;
8702 unsigned total;
8703 unsigned nparam;
8704
8705 if (!bset)
8706 return -1;
8707 total = isl_basic_set_total_dim(bset);
8708 nparam = isl_basic_set_n_param(bset);
8709 for (i = 0; i < bset->n_eq; ++i) {
8710 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
8711 continue;
8712 if (i_eq != -1)
8713 return 0;
8714 i_eq = i;
8715 }
8716 for (i = 0; i < bset->n_ineq; ++i) {
8717 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
8718 continue;
8719 if (i_eq != -1 || i_ineq != -1)
8720 return 0;
8721 i_ineq = i;
8722 }
8723 if (i_eq == -1 && i_ineq == -1)
8724 return 0;
8725 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
8726 /* The coefficient should always be one due to normalization. */
8727 if (!isl_int_is_one(c[1+nparam+dim]))
8728 return 0;
8729 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
8730 return 0;
8731 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
8732 total - nparam - dim - 1) != -1)
8733 return 0;
8734 if (val)
8735 isl_int_neg(*val, c[0]);
8736 return 1;
8737}
8738
8739int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set *set,
8740 unsigned dim, isl_int *val)
8741{
8742 int i;
8743 isl_int v;
8744 isl_int tmp;
8745 int fixed;
8746
8747 if (!set)
8748 return -1;
8749 if (set->n == 0)
8750 return 0;
8751 if (set->n == 1)
8752 return isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
8753 dim, val);
8754 isl_int_init(v);
8755 isl_int_init(tmp);
8756 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
8757 dim, &v);
8758 for (i = 1; fixed == 1 && i < set->n; ++i) {
8759 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[i],
8760 dim, &tmp);
8761 if (fixed == 1 && isl_int_ne(tmp, v))
8762 fixed = 0;
8763 }
8764 if (val)
8765 isl_int_set(*val, v);
8766 isl_int_clear(tmp);
8767 isl_int_clear(v);
8768 return fixed;
8769}
8770
8771/* uset_gist depends on constraints without existentially quantified
8772 * variables sorting first.
8773 */
8774static int sort_constraint_cmp(const void *p1, const void *p2, void *arg)
8775{
8776 isl_int **c1 = (isl_int **) p1;
8777 isl_int **c2 = (isl_int **) p2;
8778 int l1, l2;
8779 unsigned size = *(unsigned *) arg;
8780
8781 l1 = isl_seq_last_non_zero(*c1 + 1, size);
8782 l2 = isl_seq_last_non_zero(*c2 + 1, size);
8783
8784 if (l1 != l2)
8785 return l1 - l2;
8786
8787 return isl_seq_cmp(*c1 + 1, *c2 + 1, size);
8788}
8789
8790static struct isl_basic_map *isl_basic_map_sort_constraints(
8791 struct isl_basic_map *bmap)
8792{
8793 unsigned total;
8794
8795 if (!bmap)
8796 return NULL;
8797 if (bmap->n_ineq == 0)
8798 return bmap;
8799 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
8800 return bmap;
8801 total = isl_basic_map_total_dim(bmap);
8802 if (isl_sort(bmap->ineq, bmap->n_ineq, sizeof(isl_int *),
8803 &sort_constraint_cmp, &total) < 0)
8804 return isl_basic_map_free(bmap);
8805 return bmap;
8806}
8807
8808__isl_give isl_basic_set *isl_basic_set_sort_constraints(
8809 __isl_take isl_basic_set *bset)
8810{
8811 return (struct isl_basic_set *)isl_basic_map_sort_constraints(
8812 (struct isl_basic_map *)bset);
8813}
8814
8815struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
8816{
8817 if (!bmap)
8818 return NULL;
8819 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
8820 return bmap;
8821 bmap = isl_basic_map_remove_redundancies(bmap);
8822 bmap = isl_basic_map_sort_constraints(bmap);
8823 if (bmap)
8824 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
8825 return bmap;
8826}
8827
8828struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
8829{
8830 return (struct isl_basic_set *)isl_basic_map_normalize(
8831 (struct isl_basic_map *)bset);
8832}
8833
8834int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map *bmap1,
8835 const __isl_keep isl_basic_map *bmap2)
8836{
8837 int i, cmp;
8838 unsigned total;
8839
Tobias Grosser3e6070e2015-05-09 09:37:30 +00008840 if (!bmap1 || !bmap2)
8841 return -1;
8842
Tobias Grosser52a25232015-02-04 20:55:43 +00008843 if (bmap1 == bmap2)
8844 return 0;
8845 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
8846 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
8847 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
8848 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
8849 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
8850 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
8851 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
8852 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
8853 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
8854 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
8855 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
8856 return 0;
8857 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
8858 return 1;
8859 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
8860 return -1;
8861 if (bmap1->n_eq != bmap2->n_eq)
8862 return bmap1->n_eq - bmap2->n_eq;
8863 if (bmap1->n_ineq != bmap2->n_ineq)
8864 return bmap1->n_ineq - bmap2->n_ineq;
8865 if (bmap1->n_div != bmap2->n_div)
8866 return bmap1->n_div - bmap2->n_div;
8867 total = isl_basic_map_total_dim(bmap1);
8868 for (i = 0; i < bmap1->n_eq; ++i) {
8869 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
8870 if (cmp)
8871 return cmp;
8872 }
8873 for (i = 0; i < bmap1->n_ineq; ++i) {
8874 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
8875 if (cmp)
8876 return cmp;
8877 }
8878 for (i = 0; i < bmap1->n_div; ++i) {
8879 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
8880 if (cmp)
8881 return cmp;
8882 }
8883 return 0;
8884}
8885
8886int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set *bset1,
8887 const __isl_keep isl_basic_set *bset2)
8888{
8889 return isl_basic_map_plain_cmp(bset1, bset2);
8890}
8891
8892int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8893{
8894 int i, cmp;
8895
8896 if (set1 == set2)
8897 return 0;
8898 if (set1->n != set2->n)
8899 return set1->n - set2->n;
8900
8901 for (i = 0; i < set1->n; ++i) {
8902 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
8903 if (cmp)
8904 return cmp;
8905 }
8906
8907 return 0;
8908}
8909
Tobias Grosserb2f39922015-05-28 13:32:11 +00008910isl_bool isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
Tobias Grosser52a25232015-02-04 20:55:43 +00008911 __isl_keep isl_basic_map *bmap2)
8912{
Tobias Grossere0f8d592015-05-13 13:10:13 +00008913 if (!bmap1 || !bmap2)
Tobias Grosserb2f39922015-05-28 13:32:11 +00008914 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00008915 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
8916}
8917
Tobias Grosserb2f39922015-05-28 13:32:11 +00008918isl_bool isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
Tobias Grosser52a25232015-02-04 20:55:43 +00008919 __isl_keep isl_basic_set *bset2)
8920{
8921 return isl_basic_map_plain_is_equal((isl_basic_map *)bset1,
8922 (isl_basic_map *)bset2);
8923}
8924
8925static int qsort_bmap_cmp(const void *p1, const void *p2)
8926{
8927 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
8928 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
8929
8930 return isl_basic_map_plain_cmp(bmap1, bmap2);
8931}
8932
8933/* Sort the basic maps of "map" and remove duplicate basic maps.
8934 *
8935 * While removing basic maps, we make sure that the basic maps remain
8936 * sorted because isl_map_normalize expects the basic maps of the result
8937 * to be sorted.
8938 */
8939static __isl_give isl_map *sort_and_remove_duplicates(__isl_take isl_map *map)
8940{
8941 int i, j;
8942
8943 map = isl_map_remove_empty_parts(map);
8944 if (!map)
8945 return NULL;
8946 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
8947 for (i = map->n - 1; i >= 1; --i) {
8948 if (!isl_basic_map_plain_is_equal(map->p[i - 1], map->p[i]))
8949 continue;
8950 isl_basic_map_free(map->p[i-1]);
8951 for (j = i; j < map->n; ++j)
8952 map->p[j - 1] = map->p[j];
8953 map->n--;
8954 }
8955
8956 return map;
8957}
8958
8959/* Remove obvious duplicates among the basic maps of "map".
8960 *
8961 * Unlike isl_map_normalize, this function does not remove redundant
8962 * constraints and only removes duplicates that have exactly the same
8963 * constraints in the input. It does sort the constraints and
8964 * the basic maps to ease the detection of duplicates.
8965 *
8966 * If "map" has already been normalized or if the basic maps are
8967 * disjoint, then there can be no duplicates.
8968 */
8969__isl_give isl_map *isl_map_remove_obvious_duplicates(__isl_take isl_map *map)
8970{
8971 int i;
8972 isl_basic_map *bmap;
8973
8974 if (!map)
8975 return NULL;
8976 if (map->n <= 1)
8977 return map;
8978 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED | ISL_MAP_DISJOINT))
8979 return map;
8980 for (i = 0; i < map->n; ++i) {
8981 bmap = isl_basic_map_copy(map->p[i]);
8982 bmap = isl_basic_map_sort_constraints(bmap);
8983 if (!bmap)
8984 return isl_map_free(map);
8985 isl_basic_map_free(map->p[i]);
8986 map->p[i] = bmap;
8987 }
8988
8989 map = sort_and_remove_duplicates(map);
8990 return map;
8991}
8992
8993/* We normalize in place, but if anything goes wrong we need
8994 * to return NULL, so we need to make sure we don't change the
8995 * meaning of any possible other copies of map.
8996 */
8997__isl_give isl_map *isl_map_normalize(__isl_take isl_map *map)
8998{
8999 int i;
9000 struct isl_basic_map *bmap;
9001
9002 if (!map)
9003 return NULL;
9004 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
9005 return map;
9006 for (i = 0; i < map->n; ++i) {
9007 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
9008 if (!bmap)
9009 goto error;
9010 isl_basic_map_free(map->p[i]);
9011 map->p[i] = bmap;
9012 }
9013
9014 map = sort_and_remove_duplicates(map);
9015 if (map)
9016 ISL_F_SET(map, ISL_MAP_NORMALIZED);
9017 return map;
9018error:
9019 isl_map_free(map);
9020 return NULL;
9021}
9022
9023struct isl_set *isl_set_normalize(struct isl_set *set)
9024{
9025 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
9026}
9027
Tobias Grosserb2f39922015-05-28 13:32:11 +00009028isl_bool isl_map_plain_is_equal(__isl_keep isl_map *map1,
9029 __isl_keep isl_map *map2)
Tobias Grosser52a25232015-02-04 20:55:43 +00009030{
9031 int i;
Tobias Grosserb2f39922015-05-28 13:32:11 +00009032 isl_bool equal;
Tobias Grosser52a25232015-02-04 20:55:43 +00009033
9034 if (!map1 || !map2)
Tobias Grosserb2f39922015-05-28 13:32:11 +00009035 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00009036
9037 if (map1 == map2)
Tobias Grosserb2f39922015-05-28 13:32:11 +00009038 return isl_bool_true;
Tobias Grosser52a25232015-02-04 20:55:43 +00009039 if (!isl_space_is_equal(map1->dim, map2->dim))
Tobias Grosserb2f39922015-05-28 13:32:11 +00009040 return isl_bool_false;
Tobias Grosser52a25232015-02-04 20:55:43 +00009041
9042 map1 = isl_map_copy(map1);
9043 map2 = isl_map_copy(map2);
9044 map1 = isl_map_normalize(map1);
9045 map2 = isl_map_normalize(map2);
9046 if (!map1 || !map2)
9047 goto error;
9048 equal = map1->n == map2->n;
9049 for (i = 0; equal && i < map1->n; ++i) {
9050 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
9051 if (equal < 0)
9052 goto error;
9053 }
9054 isl_map_free(map1);
9055 isl_map_free(map2);
9056 return equal;
9057error:
9058 isl_map_free(map1);
9059 isl_map_free(map2);
Tobias Grosserb2f39922015-05-28 13:32:11 +00009060 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00009061}
9062
Tobias Grosserb2f39922015-05-28 13:32:11 +00009063isl_bool isl_set_plain_is_equal(__isl_keep isl_set *set1,
9064 __isl_keep isl_set *set2)
Tobias Grosser52a25232015-02-04 20:55:43 +00009065{
9066 return isl_map_plain_is_equal((struct isl_map *)set1,
9067 (struct isl_map *)set2);
9068}
9069
Tobias Grosser52a25232015-02-04 20:55:43 +00009070/* Return an interval that ranges from min to max (inclusive)
9071 */
9072struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
9073 isl_int min, isl_int max)
9074{
9075 int k;
9076 struct isl_basic_set *bset = NULL;
9077
9078 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
9079 if (!bset)
9080 goto error;
9081
9082 k = isl_basic_set_alloc_inequality(bset);
9083 if (k < 0)
9084 goto error;
9085 isl_int_set_si(bset->ineq[k][1], 1);
9086 isl_int_neg(bset->ineq[k][0], min);
9087
9088 k = isl_basic_set_alloc_inequality(bset);
9089 if (k < 0)
9090 goto error;
9091 isl_int_set_si(bset->ineq[k][1], -1);
9092 isl_int_set(bset->ineq[k][0], max);
9093
9094 return bset;
9095error:
9096 isl_basic_set_free(bset);
9097 return NULL;
9098}
9099
Tobias Grosser1fa7b972015-02-16 19:33:40 +00009100/* Return the basic maps in "map" as a list.
Tobias Grosser52a25232015-02-04 20:55:43 +00009101 */
Tobias Grosser1fa7b972015-02-16 19:33:40 +00009102__isl_give isl_basic_map_list *isl_map_get_basic_map_list(
9103 __isl_keep isl_map *map)
Tobias Grosser52a25232015-02-04 20:55:43 +00009104{
9105 int i;
9106 isl_ctx *ctx;
Tobias Grosser1fa7b972015-02-16 19:33:40 +00009107 isl_basic_map_list *list;
Tobias Grosser52a25232015-02-04 20:55:43 +00009108
Tobias Grosser1fa7b972015-02-16 19:33:40 +00009109 if (!map)
Tobias Grosser52a25232015-02-04 20:55:43 +00009110 return NULL;
Tobias Grosser1fa7b972015-02-16 19:33:40 +00009111 ctx = isl_map_get_ctx(map);
9112 list = isl_basic_map_list_alloc(ctx, map->n);
Tobias Grosser52a25232015-02-04 20:55:43 +00009113
Tobias Grosser1fa7b972015-02-16 19:33:40 +00009114 for (i = 0; i < map->n; ++i) {
9115 isl_basic_map *bmap;
Tobias Grosser52a25232015-02-04 20:55:43 +00009116
Tobias Grosser1fa7b972015-02-16 19:33:40 +00009117 bmap = isl_basic_map_copy(map->p[i]);
9118 list = isl_basic_map_list_add(list, bmap);
Tobias Grosser52a25232015-02-04 20:55:43 +00009119 }
9120
9121 return list;
9122}
9123
9124/* Return the intersection of the elements in the non-empty list "list".
9125 * All elements are assumed to live in the same space.
9126 */
Tobias Grosser1fa7b972015-02-16 19:33:40 +00009127__isl_give isl_basic_map *isl_basic_map_list_intersect(
9128 __isl_take isl_basic_map_list *list)
Tobias Grosser52a25232015-02-04 20:55:43 +00009129{
9130 int i, n;
Tobias Grosser1fa7b972015-02-16 19:33:40 +00009131 isl_basic_map *bmap;
Tobias Grosser52a25232015-02-04 20:55:43 +00009132
9133 if (!list)
9134 return NULL;
Tobias Grosser1fa7b972015-02-16 19:33:40 +00009135 n = isl_basic_map_list_n_basic_map(list);
Tobias Grosser52a25232015-02-04 20:55:43 +00009136 if (n < 1)
Tobias Grosser1fa7b972015-02-16 19:33:40 +00009137 isl_die(isl_basic_map_list_get_ctx(list), isl_error_invalid,
Tobias Grosser52a25232015-02-04 20:55:43 +00009138 "expecting non-empty list", goto error);
Tobias Grosser52a25232015-02-04 20:55:43 +00009139
Tobias Grosser1fa7b972015-02-16 19:33:40 +00009140 bmap = isl_basic_map_list_get_basic_map(list, 0);
9141 for (i = 1; i < n; ++i) {
9142 isl_basic_map *bmap_i;
9143
9144 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9145 bmap = isl_basic_map_intersect(bmap, bmap_i);
Tobias Grosser52a25232015-02-04 20:55:43 +00009146 }
9147
Tobias Grosser1fa7b972015-02-16 19:33:40 +00009148 isl_basic_map_list_free(list);
9149 return bmap;
Tobias Grosser52a25232015-02-04 20:55:43 +00009150error:
Tobias Grosser1fa7b972015-02-16 19:33:40 +00009151 isl_basic_map_list_free(list);
Tobias Grosser52a25232015-02-04 20:55:43 +00009152 return NULL;
9153}
9154
Tobias Grosser1fa7b972015-02-16 19:33:40 +00009155/* Return the intersection of the elements in the non-empty list "list".
9156 * All elements are assumed to live in the same space.
9157 */
9158__isl_give isl_basic_set *isl_basic_set_list_intersect(
9159 __isl_take isl_basic_set_list *list)
9160{
9161 return isl_basic_map_list_intersect(list);
9162}
9163
Tobias Grosser52a25232015-02-04 20:55:43 +00009164/* Return the Cartesian product of the basic sets in list (in the given order).
9165 */
9166__isl_give isl_basic_set *isl_basic_set_list_product(
9167 __isl_take struct isl_basic_set_list *list)
9168{
9169 int i;
9170 unsigned dim;
9171 unsigned nparam;
9172 unsigned extra;
9173 unsigned n_eq;
9174 unsigned n_ineq;
9175 struct isl_basic_set *product = NULL;
9176
9177 if (!list)
9178 goto error;
9179 isl_assert(list->ctx, list->n > 0, goto error);
9180 isl_assert(list->ctx, list->p[0], goto error);
9181 nparam = isl_basic_set_n_param(list->p[0]);
9182 dim = isl_basic_set_n_dim(list->p[0]);
9183 extra = list->p[0]->n_div;
9184 n_eq = list->p[0]->n_eq;
9185 n_ineq = list->p[0]->n_ineq;
9186 for (i = 1; i < list->n; ++i) {
9187 isl_assert(list->ctx, list->p[i], goto error);
9188 isl_assert(list->ctx,
9189 nparam == isl_basic_set_n_param(list->p[i]), goto error);
9190 dim += isl_basic_set_n_dim(list->p[i]);
9191 extra += list->p[i]->n_div;
9192 n_eq += list->p[i]->n_eq;
9193 n_ineq += list->p[i]->n_ineq;
9194 }
9195 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
9196 n_eq, n_ineq);
9197 if (!product)
9198 goto error;
9199 dim = 0;
9200 for (i = 0; i < list->n; ++i) {
9201 isl_basic_set_add_constraints(product,
9202 isl_basic_set_copy(list->p[i]), dim);
9203 dim += isl_basic_set_n_dim(list->p[i]);
9204 }
9205 isl_basic_set_list_free(list);
9206 return product;
9207error:
9208 isl_basic_set_free(product);
9209 isl_basic_set_list_free(list);
9210 return NULL;
9211}
9212
9213struct isl_basic_map *isl_basic_map_product(
9214 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
9215{
9216 isl_space *dim_result = NULL;
9217 struct isl_basic_map *bmap;
9218 unsigned in1, in2, out1, out2, nparam, total, pos;
9219 struct isl_dim_map *dim_map1, *dim_map2;
9220
9221 if (!bmap1 || !bmap2)
9222 goto error;
9223
9224 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
9225 bmap2->dim, isl_dim_param), goto error);
9226 dim_result = isl_space_product(isl_space_copy(bmap1->dim),
9227 isl_space_copy(bmap2->dim));
9228
9229 in1 = isl_basic_map_n_in(bmap1);
9230 in2 = isl_basic_map_n_in(bmap2);
9231 out1 = isl_basic_map_n_out(bmap1);
9232 out2 = isl_basic_map_n_out(bmap2);
9233 nparam = isl_basic_map_n_param(bmap1);
9234
9235 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
9236 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9237 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9238 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9239 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9240 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9241 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9242 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9243 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9244 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9245 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9246
9247 bmap = isl_basic_map_alloc_space(dim_result,
9248 bmap1->n_div + bmap2->n_div,
9249 bmap1->n_eq + bmap2->n_eq,
9250 bmap1->n_ineq + bmap2->n_ineq);
9251 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9252 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9253 bmap = isl_basic_map_simplify(bmap);
9254 return isl_basic_map_finalize(bmap);
9255error:
9256 isl_basic_map_free(bmap1);
9257 isl_basic_map_free(bmap2);
9258 return NULL;
9259}
9260
9261__isl_give isl_basic_map *isl_basic_map_flat_product(
9262 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9263{
9264 isl_basic_map *prod;
9265
9266 prod = isl_basic_map_product(bmap1, bmap2);
9267 prod = isl_basic_map_flatten(prod);
9268 return prod;
9269}
9270
9271__isl_give isl_basic_set *isl_basic_set_flat_product(
9272 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
9273{
9274 return isl_basic_map_flat_range_product(bset1, bset2);
9275}
9276
9277__isl_give isl_basic_map *isl_basic_map_domain_product(
9278 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9279{
9280 isl_space *space_result = NULL;
9281 isl_basic_map *bmap;
9282 unsigned in1, in2, out, nparam, total, pos;
9283 struct isl_dim_map *dim_map1, *dim_map2;
9284
9285 if (!bmap1 || !bmap2)
9286 goto error;
9287
9288 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
9289 isl_space_copy(bmap2->dim));
9290
9291 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9292 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9293 out = isl_basic_map_dim(bmap1, isl_dim_out);
9294 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9295
9296 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
9297 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9298 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9299 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9300 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9301 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9302 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9303 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9304 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
9305 isl_dim_map_div(dim_map1, bmap1, pos += out);
9306 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9307
9308 bmap = isl_basic_map_alloc_space(space_result,
9309 bmap1->n_div + bmap2->n_div,
9310 bmap1->n_eq + bmap2->n_eq,
9311 bmap1->n_ineq + bmap2->n_ineq);
9312 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9313 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9314 bmap = isl_basic_map_simplify(bmap);
9315 return isl_basic_map_finalize(bmap);
9316error:
9317 isl_basic_map_free(bmap1);
9318 isl_basic_map_free(bmap2);
9319 return NULL;
9320}
9321
9322__isl_give isl_basic_map *isl_basic_map_range_product(
9323 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9324{
9325 isl_space *dim_result = NULL;
9326 isl_basic_map *bmap;
9327 unsigned in, out1, out2, nparam, total, pos;
9328 struct isl_dim_map *dim_map1, *dim_map2;
9329
9330 if (!bmap1 || !bmap2)
9331 goto error;
9332
9333 if (!isl_space_match(bmap1->dim, isl_dim_param,
9334 bmap2->dim, isl_dim_param))
9335 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
9336 "parameters don't match", goto error);
9337
9338 dim_result = isl_space_range_product(isl_space_copy(bmap1->dim),
9339 isl_space_copy(bmap2->dim));
9340
9341 in = isl_basic_map_dim(bmap1, isl_dim_in);
9342 out1 = isl_basic_map_n_out(bmap1);
9343 out2 = isl_basic_map_n_out(bmap2);
9344 nparam = isl_basic_map_n_param(bmap1);
9345
9346 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
9347 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9348 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9349 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9350 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9351 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9352 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
9353 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
9354 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9355 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9356 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9357
9358 bmap = isl_basic_map_alloc_space(dim_result,
9359 bmap1->n_div + bmap2->n_div,
9360 bmap1->n_eq + bmap2->n_eq,
9361 bmap1->n_ineq + bmap2->n_ineq);
9362 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9363 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9364 bmap = isl_basic_map_simplify(bmap);
9365 return isl_basic_map_finalize(bmap);
9366error:
9367 isl_basic_map_free(bmap1);
9368 isl_basic_map_free(bmap2);
9369 return NULL;
9370}
9371
9372__isl_give isl_basic_map *isl_basic_map_flat_range_product(
9373 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9374{
9375 isl_basic_map *prod;
9376
9377 prod = isl_basic_map_range_product(bmap1, bmap2);
9378 prod = isl_basic_map_flatten_range(prod);
9379 return prod;
9380}
9381
9382/* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
9383 * and collect the results.
9384 * The result live in the space obtained by calling "space_product"
9385 * on the spaces of "map1" and "map2".
9386 * If "remove_duplicates" is set then the result may contain duplicates
9387 * (even if the inputs do not) and so we try and remove the obvious
9388 * duplicates.
9389 */
9390static __isl_give isl_map *map_product(__isl_take isl_map *map1,
9391 __isl_take isl_map *map2,
9392 __isl_give isl_space *(*space_product)(__isl_take isl_space *left,
9393 __isl_take isl_space *right),
9394 __isl_give isl_basic_map *(*basic_map_product)(
9395 __isl_take isl_basic_map *left,
9396 __isl_take isl_basic_map *right),
9397 int remove_duplicates)
9398{
9399 unsigned flags = 0;
9400 struct isl_map *result;
9401 int i, j;
9402
9403 if (!map1 || !map2)
9404 goto error;
9405
9406 isl_assert(map1->ctx, isl_space_match(map1->dim, isl_dim_param,
9407 map2->dim, isl_dim_param), goto error);
9408
9409 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
9410 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
9411 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
9412
9413 result = isl_map_alloc_space(space_product(isl_space_copy(map1->dim),
9414 isl_space_copy(map2->dim)),
9415 map1->n * map2->n, flags);
9416 if (!result)
9417 goto error;
9418 for (i = 0; i < map1->n; ++i)
9419 for (j = 0; j < map2->n; ++j) {
9420 struct isl_basic_map *part;
9421 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
9422 isl_basic_map_copy(map2->p[j]));
9423 if (isl_basic_map_is_empty(part))
9424 isl_basic_map_free(part);
9425 else
9426 result = isl_map_add_basic_map(result, part);
9427 if (!result)
9428 goto error;
9429 }
9430 if (remove_duplicates)
9431 result = isl_map_remove_obvious_duplicates(result);
9432 isl_map_free(map1);
9433 isl_map_free(map2);
9434 return result;
9435error:
9436 isl_map_free(map1);
9437 isl_map_free(map2);
9438 return NULL;
9439}
9440
9441/* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
9442 */
9443static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
9444 __isl_take isl_map *map2)
9445{
9446 return map_product(map1, map2, &isl_space_product,
9447 &isl_basic_map_product, 0);
9448}
9449
9450__isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
9451 __isl_take isl_map *map2)
9452{
9453 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
9454}
9455
9456/* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
9457 */
9458__isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
9459 __isl_take isl_map *map2)
9460{
9461 isl_map *prod;
9462
9463 prod = isl_map_product(map1, map2);
9464 prod = isl_map_flatten(prod);
9465 return prod;
9466}
9467
9468/* Given two set A and B, construct its Cartesian product A x B.
9469 */
9470struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
9471{
9472 return isl_map_range_product(set1, set2);
9473}
9474
9475__isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
9476 __isl_take isl_set *set2)
9477{
9478 return isl_map_flat_range_product(set1, set2);
9479}
9480
9481/* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
9482 */
9483static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
9484 __isl_take isl_map *map2)
9485{
9486 return map_product(map1, map2, &isl_space_domain_product,
9487 &isl_basic_map_domain_product, 1);
9488}
9489
9490/* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
9491 */
9492static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
9493 __isl_take isl_map *map2)
9494{
9495 return map_product(map1, map2, &isl_space_range_product,
9496 &isl_basic_map_range_product, 1);
9497}
9498
9499__isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
9500 __isl_take isl_map *map2)
9501{
9502 return isl_map_align_params_map_map_and(map1, map2,
9503 &map_domain_product_aligned);
9504}
9505
9506__isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
9507 __isl_take isl_map *map2)
9508{
9509 return isl_map_align_params_map_map_and(map1, map2,
9510 &map_range_product_aligned);
9511}
9512
9513/* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
9514 */
9515__isl_give isl_map *isl_map_factor_domain(__isl_take isl_map *map)
9516{
9517 isl_space *space;
9518 int total1, keep1, total2, keep2;
9519
9520 if (!map)
9521 return NULL;
9522 if (!isl_space_domain_is_wrapping(map->dim) ||
9523 !isl_space_range_is_wrapping(map->dim))
9524 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9525 "not a product", return isl_map_free(map));
9526
9527 space = isl_map_get_space(map);
9528 total1 = isl_space_dim(space, isl_dim_in);
9529 total2 = isl_space_dim(space, isl_dim_out);
9530 space = isl_space_factor_domain(space);
9531 keep1 = isl_space_dim(space, isl_dim_in);
9532 keep2 = isl_space_dim(space, isl_dim_out);
9533 map = isl_map_project_out(map, isl_dim_in, keep1, total1 - keep1);
9534 map = isl_map_project_out(map, isl_dim_out, keep2, total2 - keep2);
9535 map = isl_map_reset_space(map, space);
9536
9537 return map;
9538}
9539
9540/* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
9541 */
9542__isl_give isl_map *isl_map_factor_range(__isl_take isl_map *map)
9543{
9544 isl_space *space;
9545 int total1, keep1, total2, keep2;
9546
9547 if (!map)
9548 return NULL;
9549 if (!isl_space_domain_is_wrapping(map->dim) ||
9550 !isl_space_range_is_wrapping(map->dim))
9551 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9552 "not a product", return isl_map_free(map));
9553
9554 space = isl_map_get_space(map);
9555 total1 = isl_space_dim(space, isl_dim_in);
9556 total2 = isl_space_dim(space, isl_dim_out);
9557 space = isl_space_factor_range(space);
9558 keep1 = isl_space_dim(space, isl_dim_in);
9559 keep2 = isl_space_dim(space, isl_dim_out);
9560 map = isl_map_project_out(map, isl_dim_in, 0, total1 - keep1);
9561 map = isl_map_project_out(map, isl_dim_out, 0, total2 - keep2);
9562 map = isl_map_reset_space(map, space);
9563
9564 return map;
9565}
9566
9567/* Given a map of the form [A -> B] -> C, return the map A -> C.
9568 */
9569__isl_give isl_map *isl_map_domain_factor_domain(__isl_take isl_map *map)
9570{
9571 isl_space *space;
9572 int total, keep;
9573
9574 if (!map)
9575 return NULL;
9576 if (!isl_space_domain_is_wrapping(map->dim))
9577 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9578 "domain is not a product", return isl_map_free(map));
9579
9580 space = isl_map_get_space(map);
9581 total = isl_space_dim(space, isl_dim_in);
9582 space = isl_space_domain_factor_domain(space);
9583 keep = isl_space_dim(space, isl_dim_in);
9584 map = isl_map_project_out(map, isl_dim_in, keep, total - keep);
9585 map = isl_map_reset_space(map, space);
9586
9587 return map;
9588}
9589
9590/* Given a map of the form [A -> B] -> C, return the map B -> C.
9591 */
9592__isl_give isl_map *isl_map_domain_factor_range(__isl_take isl_map *map)
9593{
9594 isl_space *space;
9595 int total, keep;
9596
9597 if (!map)
9598 return NULL;
9599 if (!isl_space_domain_is_wrapping(map->dim))
9600 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9601 "domain is not a product", return isl_map_free(map));
9602
9603 space = isl_map_get_space(map);
9604 total = isl_space_dim(space, isl_dim_in);
9605 space = isl_space_domain_factor_range(space);
9606 keep = isl_space_dim(space, isl_dim_in);
9607 map = isl_map_project_out(map, isl_dim_in, 0, total - keep);
9608 map = isl_map_reset_space(map, space);
9609
9610 return map;
9611}
9612
9613/* Given a map A -> [B -> C], extract the map A -> B.
9614 */
9615__isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
9616{
9617 isl_space *space;
9618 int total, keep;
9619
9620 if (!map)
9621 return NULL;
9622 if (!isl_space_range_is_wrapping(map->dim))
9623 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9624 "range is not a product", return isl_map_free(map));
9625
9626 space = isl_map_get_space(map);
9627 total = isl_space_dim(space, isl_dim_out);
9628 space = isl_space_range_factor_domain(space);
9629 keep = isl_space_dim(space, isl_dim_out);
9630 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
9631 map = isl_map_reset_space(map, space);
9632
9633 return map;
9634}
9635
9636/* Given a map A -> [B -> C], extract the map A -> C.
9637 */
9638__isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
9639{
9640 isl_space *space;
9641 int total, keep;
9642
9643 if (!map)
9644 return NULL;
9645 if (!isl_space_range_is_wrapping(map->dim))
9646 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9647 "range is not a product", return isl_map_free(map));
9648
9649 space = isl_map_get_space(map);
9650 total = isl_space_dim(space, isl_dim_out);
9651 space = isl_space_range_factor_range(space);
9652 keep = isl_space_dim(space, isl_dim_out);
9653 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
9654 map = isl_map_reset_space(map, space);
9655
9656 return map;
9657}
9658
9659/* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
9660 */
9661__isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
9662 __isl_take isl_map *map2)
9663{
9664 isl_map *prod;
9665
9666 prod = isl_map_domain_product(map1, map2);
9667 prod = isl_map_flatten_domain(prod);
9668 return prod;
9669}
9670
9671/* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
9672 */
9673__isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
9674 __isl_take isl_map *map2)
9675{
9676 isl_map *prod;
9677
9678 prod = isl_map_range_product(map1, map2);
9679 prod = isl_map_flatten_range(prod);
9680 return prod;
9681}
9682
9683uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
9684{
9685 int i;
9686 uint32_t hash = isl_hash_init();
9687 unsigned total;
9688
9689 if (!bmap)
9690 return 0;
9691 bmap = isl_basic_map_copy(bmap);
9692 bmap = isl_basic_map_normalize(bmap);
9693 if (!bmap)
9694 return 0;
9695 total = isl_basic_map_total_dim(bmap);
9696 isl_hash_byte(hash, bmap->n_eq & 0xFF);
9697 for (i = 0; i < bmap->n_eq; ++i) {
9698 uint32_t c_hash;
9699 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
9700 isl_hash_hash(hash, c_hash);
9701 }
9702 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
9703 for (i = 0; i < bmap->n_ineq; ++i) {
9704 uint32_t c_hash;
9705 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
9706 isl_hash_hash(hash, c_hash);
9707 }
9708 isl_hash_byte(hash, bmap->n_div & 0xFF);
9709 for (i = 0; i < bmap->n_div; ++i) {
9710 uint32_t c_hash;
9711 if (isl_int_is_zero(bmap->div[i][0]))
9712 continue;
9713 isl_hash_byte(hash, i & 0xFF);
9714 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
9715 isl_hash_hash(hash, c_hash);
9716 }
9717 isl_basic_map_free(bmap);
9718 return hash;
9719}
9720
9721uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
9722{
9723 return isl_basic_map_get_hash((isl_basic_map *)bset);
9724}
9725
9726uint32_t isl_map_get_hash(__isl_keep isl_map *map)
9727{
9728 int i;
9729 uint32_t hash;
9730
9731 if (!map)
9732 return 0;
9733 map = isl_map_copy(map);
9734 map = isl_map_normalize(map);
9735 if (!map)
9736 return 0;
9737
9738 hash = isl_hash_init();
9739 for (i = 0; i < map->n; ++i) {
9740 uint32_t bmap_hash;
9741 bmap_hash = isl_basic_map_get_hash(map->p[i]);
9742 isl_hash_hash(hash, bmap_hash);
9743 }
9744
9745 isl_map_free(map);
9746
9747 return hash;
9748}
9749
9750uint32_t isl_set_get_hash(__isl_keep isl_set *set)
9751{
9752 return isl_map_get_hash((isl_map *)set);
9753}
9754
9755/* Check if the value for dimension dim is completely determined
9756 * by the values of the other parameters and variables.
9757 * That is, check if dimension dim is involved in an equality.
9758 */
9759int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
9760{
9761 int i;
9762 unsigned nparam;
9763
9764 if (!bset)
9765 return -1;
9766 nparam = isl_basic_set_n_param(bset);
9767 for (i = 0; i < bset->n_eq; ++i)
9768 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
9769 return 1;
9770 return 0;
9771}
9772
9773/* Check if the value for dimension dim is completely determined
9774 * by the values of the other parameters and variables.
9775 * That is, check if dimension dim is involved in an equality
9776 * for each of the subsets.
9777 */
9778int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
9779{
9780 int i;
9781
9782 if (!set)
9783 return -1;
9784 for (i = 0; i < set->n; ++i) {
9785 int unique;
9786 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
9787 if (unique != 1)
9788 return unique;
9789 }
9790 return 1;
9791}
9792
9793/* Return the number of basic maps in the (current) representation of "map".
9794 */
9795int isl_map_n_basic_map(__isl_keep isl_map *map)
9796{
9797 return map ? map->n : 0;
9798}
9799
9800int isl_set_n_basic_set(__isl_keep isl_set *set)
9801{
9802 return set ? set->n : 0;
9803}
9804
Tobias Grosserb2f39922015-05-28 13:32:11 +00009805isl_stat isl_map_foreach_basic_map(__isl_keep isl_map *map,
9806 isl_stat (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
Tobias Grosser52a25232015-02-04 20:55:43 +00009807{
9808 int i;
9809
9810 if (!map)
Tobias Grosserb2f39922015-05-28 13:32:11 +00009811 return isl_stat_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00009812
9813 for (i = 0; i < map->n; ++i)
9814 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
Tobias Grosserb2f39922015-05-28 13:32:11 +00009815 return isl_stat_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00009816
Tobias Grosserb2f39922015-05-28 13:32:11 +00009817 return isl_stat_ok;
Tobias Grosser52a25232015-02-04 20:55:43 +00009818}
9819
Tobias Grosserb2f39922015-05-28 13:32:11 +00009820isl_stat isl_set_foreach_basic_set(__isl_keep isl_set *set,
9821 isl_stat (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
Tobias Grosser52a25232015-02-04 20:55:43 +00009822{
9823 int i;
9824
9825 if (!set)
Tobias Grosserb2f39922015-05-28 13:32:11 +00009826 return isl_stat_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00009827
9828 for (i = 0; i < set->n; ++i)
9829 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
Tobias Grosserb2f39922015-05-28 13:32:11 +00009830 return isl_stat_error;
Tobias Grosser52a25232015-02-04 20:55:43 +00009831
Tobias Grosserb2f39922015-05-28 13:32:11 +00009832 return isl_stat_ok;
Tobias Grosser52a25232015-02-04 20:55:43 +00009833}
9834
Tobias Grosser4db55312015-06-30 08:22:14 +00009835/* Return a list of basic sets, the union of which is equal to "set".
9836 */
9837__isl_give isl_basic_set_list *isl_set_get_basic_set_list(
9838 __isl_keep isl_set *set)
9839{
9840 int i;
9841 isl_basic_set_list *list;
9842
9843 if (!set)
9844 return NULL;
9845
9846 list = isl_basic_set_list_alloc(isl_set_get_ctx(set), set->n);
9847 for (i = 0; i < set->n; ++i) {
9848 isl_basic_set *bset;
9849
9850 bset = isl_basic_set_copy(set->p[i]);
9851 list = isl_basic_set_list_add(list, bset);
9852 }
9853
9854 return list;
9855}
9856
Tobias Grosser52a25232015-02-04 20:55:43 +00009857__isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
9858{
9859 isl_space *dim;
9860
9861 if (!bset)
9862 return NULL;
9863
9864 bset = isl_basic_set_cow(bset);
9865 if (!bset)
9866 return NULL;
9867
9868 dim = isl_basic_set_get_space(bset);
9869 dim = isl_space_lift(dim, bset->n_div);
9870 if (!dim)
9871 goto error;
9872 isl_space_free(bset->dim);
9873 bset->dim = dim;
9874 bset->extra -= bset->n_div;
9875 bset->n_div = 0;
9876
9877 bset = isl_basic_set_finalize(bset);
9878
9879 return bset;
9880error:
9881 isl_basic_set_free(bset);
9882 return NULL;
9883}
9884
9885__isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
9886{
9887 int i;
9888 isl_space *dim;
9889 unsigned n_div;
9890
9891 set = isl_set_align_divs(set);
9892
9893 if (!set)
9894 return NULL;
9895
9896 set = isl_set_cow(set);
9897 if (!set)
9898 return NULL;
9899
9900 n_div = set->p[0]->n_div;
9901 dim = isl_set_get_space(set);
9902 dim = isl_space_lift(dim, n_div);
9903 if (!dim)
9904 goto error;
9905 isl_space_free(set->dim);
9906 set->dim = dim;
9907
9908 for (i = 0; i < set->n; ++i) {
9909 set->p[i] = isl_basic_set_lift(set->p[i]);
9910 if (!set->p[i])
9911 goto error;
9912 }
9913
9914 return set;
9915error:
9916 isl_set_free(set);
9917 return NULL;
9918}
9919
9920__isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
9921{
9922 isl_space *dim;
9923 struct isl_basic_map *bmap;
9924 unsigned n_set;
9925 unsigned n_div;
9926 unsigned n_param;
9927 unsigned total;
9928 int i, k, l;
9929
9930 set = isl_set_align_divs(set);
9931
9932 if (!set)
9933 return NULL;
9934
9935 dim = isl_set_get_space(set);
9936 if (set->n == 0 || set->p[0]->n_div == 0) {
9937 isl_set_free(set);
9938 return isl_map_identity(isl_space_map_from_set(dim));
9939 }
9940
9941 n_div = set->p[0]->n_div;
9942 dim = isl_space_map_from_set(dim);
9943 n_param = isl_space_dim(dim, isl_dim_param);
9944 n_set = isl_space_dim(dim, isl_dim_in);
9945 dim = isl_space_extend(dim, n_param, n_set, n_set + n_div);
9946 bmap = isl_basic_map_alloc_space(dim, 0, n_set, 2 * n_div);
9947 for (i = 0; i < n_set; ++i)
9948 bmap = var_equal(bmap, i);
9949
9950 total = n_param + n_set + n_set + n_div;
9951 for (i = 0; i < n_div; ++i) {
9952 k = isl_basic_map_alloc_inequality(bmap);
9953 if (k < 0)
9954 goto error;
9955 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
9956 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
9957 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
9958 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
9959 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
9960 set->p[0]->div[i][0]);
9961
9962 l = isl_basic_map_alloc_inequality(bmap);
9963 if (l < 0)
9964 goto error;
9965 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
9966 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
9967 set->p[0]->div[i][0]);
9968 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
9969 }
9970
9971 isl_set_free(set);
9972 bmap = isl_basic_map_simplify(bmap);
9973 bmap = isl_basic_map_finalize(bmap);
9974 return isl_map_from_basic_map(bmap);
9975error:
9976 isl_set_free(set);
9977 isl_basic_map_free(bmap);
9978 return NULL;
9979}
9980
9981int isl_basic_set_size(__isl_keep isl_basic_set *bset)
9982{
9983 unsigned dim;
9984 int size = 0;
9985
9986 if (!bset)
9987 return -1;
9988
9989 dim = isl_basic_set_total_dim(bset);
9990 size += bset->n_eq * (1 + dim);
9991 size += bset->n_ineq * (1 + dim);
9992 size += bset->n_div * (2 + dim);
9993
9994 return size;
9995}
9996
9997int isl_set_size(__isl_keep isl_set *set)
9998{
9999 int i;
10000 int size = 0;
10001
10002 if (!set)
10003 return -1;
10004
10005 for (i = 0; i < set->n; ++i)
10006 size += isl_basic_set_size(set->p[i]);
10007
10008 return size;
10009}
10010
10011/* Check if there is any lower bound (if lower == 0) and/or upper
10012 * bound (if upper == 0) on the specified dim.
10013 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000010014static isl_bool basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
Tobias Grosser52a25232015-02-04 20:55:43 +000010015 enum isl_dim_type type, unsigned pos, int lower, int upper)
10016{
10017 int i;
10018
10019 if (!bmap)
Tobias Grosserb2f39922015-05-28 13:32:11 +000010020 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000010021
Tobias Grosserb2f39922015-05-28 13:32:11 +000010022 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type),
10023 return isl_bool_error);
Tobias Grosser52a25232015-02-04 20:55:43 +000010024
10025 pos += isl_basic_map_offset(bmap, type);
10026
10027 for (i = 0; i < bmap->n_div; ++i) {
10028 if (isl_int_is_zero(bmap->div[i][0]))
10029 continue;
10030 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
Tobias Grosserb2f39922015-05-28 13:32:11 +000010031 return isl_bool_true;
Tobias Grosser52a25232015-02-04 20:55:43 +000010032 }
10033
10034 for (i = 0; i < bmap->n_eq; ++i)
10035 if (!isl_int_is_zero(bmap->eq[i][pos]))
Tobias Grosserb2f39922015-05-28 13:32:11 +000010036 return isl_bool_true;
Tobias Grosser52a25232015-02-04 20:55:43 +000010037
10038 for (i = 0; i < bmap->n_ineq; ++i) {
10039 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
10040 if (sgn > 0)
10041 lower = 1;
10042 if (sgn < 0)
10043 upper = 1;
10044 }
10045
10046 return lower && upper;
10047}
10048
10049int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10050 enum isl_dim_type type, unsigned pos)
10051{
10052 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
10053}
10054
Tobias Grosserb2f39922015-05-28 13:32:11 +000010055isl_bool isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
Tobias Grosser52a25232015-02-04 20:55:43 +000010056 enum isl_dim_type type, unsigned pos)
10057{
10058 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
10059}
10060
Tobias Grosserb2f39922015-05-28 13:32:11 +000010061isl_bool isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
Tobias Grosser52a25232015-02-04 20:55:43 +000010062 enum isl_dim_type type, unsigned pos)
10063{
10064 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
10065}
10066
10067int isl_map_dim_is_bounded(__isl_keep isl_map *map,
10068 enum isl_dim_type type, unsigned pos)
10069{
10070 int i;
10071
10072 if (!map)
10073 return -1;
10074
10075 for (i = 0; i < map->n; ++i) {
10076 int bounded;
10077 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
10078 if (bounded < 0 || !bounded)
10079 return bounded;
10080 }
10081
10082 return 1;
10083}
10084
10085/* Return 1 if the specified dim is involved in both an upper bound
10086 * and a lower bound.
10087 */
10088int isl_set_dim_is_bounded(__isl_keep isl_set *set,
10089 enum isl_dim_type type, unsigned pos)
10090{
10091 return isl_map_dim_is_bounded((isl_map *)set, type, pos);
10092}
10093
10094/* Does "map" have a bound (according to "fn") for any of its basic maps?
10095 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000010096static isl_bool has_any_bound(__isl_keep isl_map *map,
Tobias Grosser52a25232015-02-04 20:55:43 +000010097 enum isl_dim_type type, unsigned pos,
Tobias Grosserb2f39922015-05-28 13:32:11 +000010098 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
Tobias Grosser52a25232015-02-04 20:55:43 +000010099 enum isl_dim_type type, unsigned pos))
10100{
10101 int i;
10102
10103 if (!map)
Tobias Grosserb2f39922015-05-28 13:32:11 +000010104 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000010105
10106 for (i = 0; i < map->n; ++i) {
Tobias Grosserb2f39922015-05-28 13:32:11 +000010107 isl_bool bounded;
Tobias Grosser52a25232015-02-04 20:55:43 +000010108 bounded = fn(map->p[i], type, pos);
10109 if (bounded < 0 || bounded)
10110 return bounded;
10111 }
10112
Tobias Grosserb2f39922015-05-28 13:32:11 +000010113 return isl_bool_false;
Tobias Grosser52a25232015-02-04 20:55:43 +000010114}
10115
10116/* Return 1 if the specified dim is involved in any lower bound.
10117 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000010118isl_bool isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
Tobias Grosser52a25232015-02-04 20:55:43 +000010119 enum isl_dim_type type, unsigned pos)
10120{
10121 return has_any_bound(set, type, pos,
10122 &isl_basic_map_dim_has_lower_bound);
10123}
10124
10125/* Return 1 if the specified dim is involved in any upper bound.
10126 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000010127isl_bool isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
Tobias Grosser52a25232015-02-04 20:55:43 +000010128 enum isl_dim_type type, unsigned pos)
10129{
10130 return has_any_bound(set, type, pos,
10131 &isl_basic_map_dim_has_upper_bound);
10132}
10133
10134/* Does "map" have a bound (according to "fn") for all of its basic maps?
10135 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000010136static isl_bool has_bound(__isl_keep isl_map *map,
Tobias Grosser52a25232015-02-04 20:55:43 +000010137 enum isl_dim_type type, unsigned pos,
Tobias Grosserb2f39922015-05-28 13:32:11 +000010138 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
Tobias Grosser52a25232015-02-04 20:55:43 +000010139 enum isl_dim_type type, unsigned pos))
10140{
10141 int i;
10142
10143 if (!map)
Tobias Grosserb2f39922015-05-28 13:32:11 +000010144 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000010145
10146 for (i = 0; i < map->n; ++i) {
Tobias Grosserb2f39922015-05-28 13:32:11 +000010147 isl_bool bounded;
Tobias Grosser52a25232015-02-04 20:55:43 +000010148 bounded = fn(map->p[i], type, pos);
10149 if (bounded < 0 || !bounded)
10150 return bounded;
10151 }
10152
Tobias Grosserb2f39922015-05-28 13:32:11 +000010153 return isl_bool_true;
Tobias Grosser52a25232015-02-04 20:55:43 +000010154}
10155
10156/* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10157 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000010158isl_bool isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
Tobias Grosser52a25232015-02-04 20:55:43 +000010159 enum isl_dim_type type, unsigned pos)
10160{
10161 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
10162}
10163
10164/* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10165 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000010166isl_bool isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
Tobias Grosser52a25232015-02-04 20:55:43 +000010167 enum isl_dim_type type, unsigned pos)
10168{
10169 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
10170}
10171
10172/* For each of the "n" variables starting at "first", determine
10173 * the sign of the variable and put the results in the first "n"
10174 * elements of the array "signs".
10175 * Sign
10176 * 1 means that the variable is non-negative
10177 * -1 means that the variable is non-positive
10178 * 0 means the variable attains both positive and negative values.
10179 */
10180int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
10181 unsigned first, unsigned n, int *signs)
10182{
10183 isl_vec *bound = NULL;
10184 struct isl_tab *tab = NULL;
10185 struct isl_tab_undo *snap;
10186 int i;
10187
10188 if (!bset || !signs)
10189 return -1;
10190
10191 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
10192 tab = isl_tab_from_basic_set(bset, 0);
10193 if (!bound || !tab)
10194 goto error;
10195
10196 isl_seq_clr(bound->el, bound->size);
10197 isl_int_set_si(bound->el[0], -1);
10198
10199 snap = isl_tab_snap(tab);
10200 for (i = 0; i < n; ++i) {
10201 int empty;
10202
10203 isl_int_set_si(bound->el[1 + first + i], -1);
10204 if (isl_tab_add_ineq(tab, bound->el) < 0)
10205 goto error;
10206 empty = tab->empty;
10207 isl_int_set_si(bound->el[1 + first + i], 0);
10208 if (isl_tab_rollback(tab, snap) < 0)
10209 goto error;
10210
10211 if (empty) {
10212 signs[i] = 1;
10213 continue;
10214 }
10215
10216 isl_int_set_si(bound->el[1 + first + i], 1);
10217 if (isl_tab_add_ineq(tab, bound->el) < 0)
10218 goto error;
10219 empty = tab->empty;
10220 isl_int_set_si(bound->el[1 + first + i], 0);
10221 if (isl_tab_rollback(tab, snap) < 0)
10222 goto error;
10223
10224 signs[i] = empty ? -1 : 0;
10225 }
10226
10227 isl_tab_free(tab);
10228 isl_vec_free(bound);
10229 return 0;
10230error:
10231 isl_tab_free(tab);
10232 isl_vec_free(bound);
10233 return -1;
10234}
10235
10236int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
10237 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
10238{
10239 if (!bset || !signs)
10240 return -1;
10241 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
10242 return -1);
10243
10244 first += pos(bset->dim, type) - 1;
10245 return isl_basic_set_vars_get_sign(bset, first, n, signs);
10246}
10247
10248/* Is it possible for the integer division "div" to depend (possibly
10249 * indirectly) on any output dimensions?
10250 *
10251 * If the div is undefined, then we conservatively assume that it
10252 * may depend on them.
10253 * Otherwise, we check if it actually depends on them or on any integer
10254 * divisions that may depend on them.
10255 */
10256static int div_may_involve_output(__isl_keep isl_basic_map *bmap, int div)
10257{
10258 int i;
10259 unsigned n_out, o_out;
10260 unsigned n_div, o_div;
10261
10262 if (isl_int_is_zero(bmap->div[div][0]))
10263 return 1;
10264
10265 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10266 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10267
10268 if (isl_seq_first_non_zero(bmap->div[div] + 1 + o_out, n_out) != -1)
10269 return 1;
10270
10271 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10272 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10273
10274 for (i = 0; i < n_div; ++i) {
10275 if (isl_int_is_zero(bmap->div[div][1 + o_div + i]))
10276 continue;
10277 if (div_may_involve_output(bmap, i))
10278 return 1;
10279 }
10280
10281 return 0;
10282}
10283
10284/* Return the index of the equality of "bmap" that defines
10285 * the output dimension "pos" in terms of earlier dimensions.
10286 * The equality may also involve integer divisions, as long
10287 * as those integer divisions are defined in terms of
10288 * parameters or input dimensions.
10289 * Return bmap->n_eq if there is no such equality.
10290 * Return -1 on error.
10291 */
10292int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map *bmap,
10293 int pos)
10294{
10295 int j, k;
10296 unsigned n_out, o_out;
10297 unsigned n_div, o_div;
10298
10299 if (!bmap)
10300 return -1;
10301
10302 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10303 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10304 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10305 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10306
10307 for (j = 0; j < bmap->n_eq; ++j) {
10308 if (isl_int_is_zero(bmap->eq[j][o_out + pos]))
10309 continue;
10310 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + pos + 1,
10311 n_out - (pos + 1)) != -1)
10312 continue;
10313 for (k = 0; k < n_div; ++k) {
10314 if (isl_int_is_zero(bmap->eq[j][o_div + k]))
10315 continue;
10316 if (div_may_involve_output(bmap, k))
10317 break;
10318 }
10319 if (k >= n_div)
10320 return j;
10321 }
10322
10323 return bmap->n_eq;
10324}
10325
10326/* Check if the given basic map is obviously single-valued.
10327 * In particular, for each output dimension, check that there is
10328 * an equality that defines the output dimension in terms of
10329 * earlier dimensions.
10330 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000010331isl_bool isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
Tobias Grosser52a25232015-02-04 20:55:43 +000010332{
10333 int i;
10334 unsigned n_out;
10335
10336 if (!bmap)
Tobias Grosserb2f39922015-05-28 13:32:11 +000010337 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000010338
10339 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10340
10341 for (i = 0; i < n_out; ++i) {
10342 int eq;
10343
10344 eq = isl_basic_map_output_defining_equality(bmap, i);
10345 if (eq < 0)
Tobias Grosserb2f39922015-05-28 13:32:11 +000010346 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000010347 if (eq >= bmap->n_eq)
Tobias Grosserb2f39922015-05-28 13:32:11 +000010348 return isl_bool_false;
Tobias Grosser52a25232015-02-04 20:55:43 +000010349 }
10350
Tobias Grosserb2f39922015-05-28 13:32:11 +000010351 return isl_bool_true;
Tobias Grosser52a25232015-02-04 20:55:43 +000010352}
10353
10354/* Check if the given basic map is single-valued.
10355 * We simply compute
10356 *
10357 * M \circ M^-1
10358 *
10359 * and check if the result is a subset of the identity mapping.
10360 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000010361isl_bool isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
Tobias Grosser52a25232015-02-04 20:55:43 +000010362{
10363 isl_space *space;
10364 isl_basic_map *test;
10365 isl_basic_map *id;
Tobias Grosserb2f39922015-05-28 13:32:11 +000010366 isl_bool sv;
Tobias Grosser52a25232015-02-04 20:55:43 +000010367
10368 sv = isl_basic_map_plain_is_single_valued(bmap);
10369 if (sv < 0 || sv)
10370 return sv;
10371
10372 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
10373 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
10374
10375 space = isl_basic_map_get_space(bmap);
10376 space = isl_space_map_from_set(isl_space_range(space));
10377 id = isl_basic_map_identity(space);
10378
10379 sv = isl_basic_map_is_subset(test, id);
10380
10381 isl_basic_map_free(test);
10382 isl_basic_map_free(id);
10383
10384 return sv;
10385}
10386
10387/* Check if the given map is obviously single-valued.
10388 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000010389isl_bool isl_map_plain_is_single_valued(__isl_keep isl_map *map)
Tobias Grosser52a25232015-02-04 20:55:43 +000010390{
10391 if (!map)
Tobias Grosserb2f39922015-05-28 13:32:11 +000010392 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000010393 if (map->n == 0)
Tobias Grosserb2f39922015-05-28 13:32:11 +000010394 return isl_bool_true;
Tobias Grosser52a25232015-02-04 20:55:43 +000010395 if (map->n >= 2)
Tobias Grosserb2f39922015-05-28 13:32:11 +000010396 return isl_bool_false;
Tobias Grosser52a25232015-02-04 20:55:43 +000010397
10398 return isl_basic_map_plain_is_single_valued(map->p[0]);
10399}
10400
10401/* Check if the given map is single-valued.
10402 * We simply compute
10403 *
10404 * M \circ M^-1
10405 *
10406 * and check if the result is a subset of the identity mapping.
10407 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000010408isl_bool isl_map_is_single_valued(__isl_keep isl_map *map)
Tobias Grosser52a25232015-02-04 20:55:43 +000010409{
10410 isl_space *dim;
10411 isl_map *test;
10412 isl_map *id;
Tobias Grosserb2f39922015-05-28 13:32:11 +000010413 isl_bool sv;
Tobias Grosser52a25232015-02-04 20:55:43 +000010414
10415 sv = isl_map_plain_is_single_valued(map);
10416 if (sv < 0 || sv)
10417 return sv;
10418
10419 test = isl_map_reverse(isl_map_copy(map));
10420 test = isl_map_apply_range(test, isl_map_copy(map));
10421
10422 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
10423 id = isl_map_identity(dim);
10424
10425 sv = isl_map_is_subset(test, id);
10426
10427 isl_map_free(test);
10428 isl_map_free(id);
10429
10430 return sv;
10431}
10432
Tobias Grosserb2f39922015-05-28 13:32:11 +000010433isl_bool isl_map_is_injective(__isl_keep isl_map *map)
Tobias Grosser52a25232015-02-04 20:55:43 +000010434{
Tobias Grosserb2f39922015-05-28 13:32:11 +000010435 isl_bool in;
Tobias Grosser52a25232015-02-04 20:55:43 +000010436
10437 map = isl_map_copy(map);
10438 map = isl_map_reverse(map);
10439 in = isl_map_is_single_valued(map);
10440 isl_map_free(map);
10441
10442 return in;
10443}
10444
10445/* Check if the given map is obviously injective.
10446 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000010447isl_bool isl_map_plain_is_injective(__isl_keep isl_map *map)
Tobias Grosser52a25232015-02-04 20:55:43 +000010448{
Tobias Grosserb2f39922015-05-28 13:32:11 +000010449 isl_bool in;
Tobias Grosser52a25232015-02-04 20:55:43 +000010450
10451 map = isl_map_copy(map);
10452 map = isl_map_reverse(map);
10453 in = isl_map_plain_is_single_valued(map);
10454 isl_map_free(map);
10455
10456 return in;
10457}
10458
Tobias Grosserb2f39922015-05-28 13:32:11 +000010459isl_bool isl_map_is_bijective(__isl_keep isl_map *map)
Tobias Grosser52a25232015-02-04 20:55:43 +000010460{
Tobias Grosserb2f39922015-05-28 13:32:11 +000010461 isl_bool sv;
Tobias Grosser52a25232015-02-04 20:55:43 +000010462
10463 sv = isl_map_is_single_valued(map);
10464 if (sv < 0 || !sv)
10465 return sv;
10466
10467 return isl_map_is_injective(map);
10468}
10469
Tobias Grosserb2f39922015-05-28 13:32:11 +000010470isl_bool isl_set_is_singleton(__isl_keep isl_set *set)
Tobias Grosser52a25232015-02-04 20:55:43 +000010471{
10472 return isl_map_is_single_valued((isl_map *)set);
10473}
10474
10475int isl_map_is_translation(__isl_keep isl_map *map)
10476{
10477 int ok;
10478 isl_set *delta;
10479
10480 delta = isl_map_deltas(isl_map_copy(map));
10481 ok = isl_set_is_singleton(delta);
10482 isl_set_free(delta);
10483
10484 return ok;
10485}
10486
10487static int unique(isl_int *p, unsigned pos, unsigned len)
10488{
10489 if (isl_seq_first_non_zero(p, pos) != -1)
10490 return 0;
10491 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
10492 return 0;
10493 return 1;
10494}
10495
10496int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
10497{
10498 int i, j;
10499 unsigned nvar;
10500 unsigned ovar;
10501
10502 if (!bset)
10503 return -1;
10504
10505 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
10506 return 0;
10507
10508 nvar = isl_basic_set_dim(bset, isl_dim_set);
10509 ovar = isl_space_offset(bset->dim, isl_dim_set);
10510 for (j = 0; j < nvar; ++j) {
10511 int lower = 0, upper = 0;
10512 for (i = 0; i < bset->n_eq; ++i) {
10513 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
10514 continue;
10515 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
10516 return 0;
10517 break;
10518 }
10519 if (i < bset->n_eq)
10520 continue;
10521 for (i = 0; i < bset->n_ineq; ++i) {
10522 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
10523 continue;
10524 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
10525 return 0;
10526 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
10527 lower = 1;
10528 else
10529 upper = 1;
10530 }
10531 if (!lower || !upper)
10532 return 0;
10533 }
10534
10535 return 1;
10536}
10537
10538int isl_set_is_box(__isl_keep isl_set *set)
10539{
10540 if (!set)
10541 return -1;
10542 if (set->n != 1)
10543 return 0;
10544
10545 return isl_basic_set_is_box(set->p[0]);
10546}
10547
Tobias Grosserb2f39922015-05-28 13:32:11 +000010548isl_bool isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
Tobias Grosser52a25232015-02-04 20:55:43 +000010549{
10550 if (!bset)
Tobias Grosserb2f39922015-05-28 13:32:11 +000010551 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000010552
10553 return isl_space_is_wrapping(bset->dim);
10554}
10555
Tobias Grosserb2f39922015-05-28 13:32:11 +000010556isl_bool isl_set_is_wrapping(__isl_keep isl_set *set)
Tobias Grosser52a25232015-02-04 20:55:43 +000010557{
10558 if (!set)
Tobias Grosserb2f39922015-05-28 13:32:11 +000010559 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000010560
10561 return isl_space_is_wrapping(set->dim);
10562}
10563
10564/* Is the domain of "map" a wrapped relation?
10565 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000010566isl_bool isl_map_domain_is_wrapping(__isl_keep isl_map *map)
Tobias Grosser52a25232015-02-04 20:55:43 +000010567{
10568 if (!map)
Tobias Grosserb2f39922015-05-28 13:32:11 +000010569 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000010570
10571 return isl_space_domain_is_wrapping(map->dim);
10572}
10573
10574/* Is the range of "map" a wrapped relation?
10575 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000010576isl_bool isl_map_range_is_wrapping(__isl_keep isl_map *map)
Tobias Grosser52a25232015-02-04 20:55:43 +000010577{
10578 if (!map)
Tobias Grosserb2f39922015-05-28 13:32:11 +000010579 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000010580
10581 return isl_space_range_is_wrapping(map->dim);
10582}
10583
10584__isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
10585{
10586 bmap = isl_basic_map_cow(bmap);
10587 if (!bmap)
10588 return NULL;
10589
10590 bmap->dim = isl_space_wrap(bmap->dim);
10591 if (!bmap->dim)
10592 goto error;
10593
10594 bmap = isl_basic_map_finalize(bmap);
10595
10596 return (isl_basic_set *)bmap;
10597error:
10598 isl_basic_map_free(bmap);
10599 return NULL;
10600}
10601
10602__isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
10603{
10604 int i;
10605
10606 map = isl_map_cow(map);
10607 if (!map)
10608 return NULL;
10609
10610 for (i = 0; i < map->n; ++i) {
10611 map->p[i] = (isl_basic_map *)isl_basic_map_wrap(map->p[i]);
10612 if (!map->p[i])
10613 goto error;
10614 }
10615 map->dim = isl_space_wrap(map->dim);
10616 if (!map->dim)
10617 goto error;
10618
10619 return (isl_set *)map;
10620error:
10621 isl_map_free(map);
10622 return NULL;
10623}
10624
10625__isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
10626{
10627 bset = isl_basic_set_cow(bset);
10628 if (!bset)
10629 return NULL;
10630
10631 bset->dim = isl_space_unwrap(bset->dim);
10632 if (!bset->dim)
10633 goto error;
10634
10635 bset = isl_basic_set_finalize(bset);
10636
10637 return (isl_basic_map *)bset;
10638error:
10639 isl_basic_set_free(bset);
10640 return NULL;
10641}
10642
10643__isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
10644{
10645 int i;
10646
10647 if (!set)
10648 return NULL;
10649
10650 if (!isl_set_is_wrapping(set))
10651 isl_die(set->ctx, isl_error_invalid, "not a wrapping set",
10652 goto error);
10653
10654 set = isl_set_cow(set);
10655 if (!set)
10656 return NULL;
10657
10658 for (i = 0; i < set->n; ++i) {
10659 set->p[i] = (isl_basic_set *)isl_basic_set_unwrap(set->p[i]);
10660 if (!set->p[i])
10661 goto error;
10662 }
10663
10664 set->dim = isl_space_unwrap(set->dim);
10665 if (!set->dim)
10666 goto error;
10667
10668 return (isl_map *)set;
10669error:
10670 isl_set_free(set);
10671 return NULL;
10672}
10673
10674__isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
10675 enum isl_dim_type type)
10676{
10677 if (!bmap)
10678 return NULL;
10679
10680 if (!isl_space_is_named_or_nested(bmap->dim, type))
10681 return bmap;
10682
10683 bmap = isl_basic_map_cow(bmap);
10684 if (!bmap)
10685 return NULL;
10686
10687 bmap->dim = isl_space_reset(bmap->dim, type);
10688 if (!bmap->dim)
10689 goto error;
10690
10691 bmap = isl_basic_map_finalize(bmap);
10692
10693 return bmap;
10694error:
10695 isl_basic_map_free(bmap);
10696 return NULL;
10697}
10698
10699__isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
10700 enum isl_dim_type type)
10701{
10702 int i;
10703
10704 if (!map)
10705 return NULL;
10706
10707 if (!isl_space_is_named_or_nested(map->dim, type))
10708 return map;
10709
10710 map = isl_map_cow(map);
10711 if (!map)
10712 return NULL;
10713
10714 for (i = 0; i < map->n; ++i) {
10715 map->p[i] = isl_basic_map_reset(map->p[i], type);
10716 if (!map->p[i])
10717 goto error;
10718 }
10719 map->dim = isl_space_reset(map->dim, type);
10720 if (!map->dim)
10721 goto error;
10722
10723 return map;
10724error:
10725 isl_map_free(map);
10726 return NULL;
10727}
10728
10729__isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
10730{
10731 if (!bmap)
10732 return NULL;
10733
10734 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
10735 return bmap;
10736
10737 bmap = isl_basic_map_cow(bmap);
10738 if (!bmap)
10739 return NULL;
10740
10741 bmap->dim = isl_space_flatten(bmap->dim);
10742 if (!bmap->dim)
10743 goto error;
10744
10745 bmap = isl_basic_map_finalize(bmap);
10746
10747 return bmap;
10748error:
10749 isl_basic_map_free(bmap);
10750 return NULL;
10751}
10752
10753__isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
10754{
10755 return (isl_basic_set *)isl_basic_map_flatten((isl_basic_map *)bset);
10756}
10757
10758__isl_give isl_basic_map *isl_basic_map_flatten_domain(
10759 __isl_take isl_basic_map *bmap)
10760{
10761 if (!bmap)
10762 return NULL;
10763
10764 if (!bmap->dim->nested[0])
10765 return bmap;
10766
10767 bmap = isl_basic_map_cow(bmap);
10768 if (!bmap)
10769 return NULL;
10770
10771 bmap->dim = isl_space_flatten_domain(bmap->dim);
10772 if (!bmap->dim)
10773 goto error;
10774
10775 bmap = isl_basic_map_finalize(bmap);
10776
10777 return bmap;
10778error:
10779 isl_basic_map_free(bmap);
10780 return NULL;
10781}
10782
10783__isl_give isl_basic_map *isl_basic_map_flatten_range(
10784 __isl_take isl_basic_map *bmap)
10785{
10786 if (!bmap)
10787 return NULL;
10788
10789 if (!bmap->dim->nested[1])
10790 return bmap;
10791
10792 bmap = isl_basic_map_cow(bmap);
10793 if (!bmap)
10794 return NULL;
10795
10796 bmap->dim = isl_space_flatten_range(bmap->dim);
10797 if (!bmap->dim)
10798 goto error;
10799
10800 bmap = isl_basic_map_finalize(bmap);
10801
10802 return bmap;
10803error:
10804 isl_basic_map_free(bmap);
10805 return NULL;
10806}
10807
10808__isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
10809{
10810 int i;
10811
10812 if (!map)
10813 return NULL;
10814
10815 if (!map->dim->nested[0] && !map->dim->nested[1])
10816 return map;
10817
10818 map = isl_map_cow(map);
10819 if (!map)
10820 return NULL;
10821
10822 for (i = 0; i < map->n; ++i) {
10823 map->p[i] = isl_basic_map_flatten(map->p[i]);
10824 if (!map->p[i])
10825 goto error;
10826 }
10827 map->dim = isl_space_flatten(map->dim);
10828 if (!map->dim)
10829 goto error;
10830
10831 return map;
10832error:
10833 isl_map_free(map);
10834 return NULL;
10835}
10836
10837__isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
10838{
10839 return (isl_set *)isl_map_flatten((isl_map *)set);
10840}
10841
10842__isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
10843{
10844 isl_space *dim, *flat_dim;
10845 isl_map *map;
10846
10847 dim = isl_set_get_space(set);
10848 flat_dim = isl_space_flatten(isl_space_copy(dim));
10849 map = isl_map_identity(isl_space_join(isl_space_reverse(dim), flat_dim));
10850 map = isl_map_intersect_domain(map, set);
10851
10852 return map;
10853}
10854
10855__isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
10856{
10857 int i;
10858
10859 if (!map)
10860 return NULL;
10861
10862 if (!map->dim->nested[0])
10863 return map;
10864
10865 map = isl_map_cow(map);
10866 if (!map)
10867 return NULL;
10868
10869 for (i = 0; i < map->n; ++i) {
10870 map->p[i] = isl_basic_map_flatten_domain(map->p[i]);
10871 if (!map->p[i])
10872 goto error;
10873 }
10874 map->dim = isl_space_flatten_domain(map->dim);
10875 if (!map->dim)
10876 goto error;
10877
10878 return map;
10879error:
10880 isl_map_free(map);
10881 return NULL;
10882}
10883
10884__isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
10885{
10886 int i;
10887
10888 if (!map)
10889 return NULL;
10890
10891 if (!map->dim->nested[1])
10892 return map;
10893
10894 map = isl_map_cow(map);
10895 if (!map)
10896 return NULL;
10897
10898 for (i = 0; i < map->n; ++i) {
10899 map->p[i] = isl_basic_map_flatten_range(map->p[i]);
10900 if (!map->p[i])
10901 goto error;
10902 }
10903 map->dim = isl_space_flatten_range(map->dim);
10904 if (!map->dim)
10905 goto error;
10906
10907 return map;
10908error:
10909 isl_map_free(map);
10910 return NULL;
10911}
10912
10913/* Reorder the dimensions of "bmap" according to the given dim_map
10914 * and set the dimension specification to "dim".
10915 */
10916__isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
10917 __isl_take isl_space *dim, __isl_take struct isl_dim_map *dim_map)
10918{
10919 isl_basic_map *res;
10920 unsigned flags;
10921
10922 bmap = isl_basic_map_cow(bmap);
10923 if (!bmap || !dim || !dim_map)
10924 goto error;
10925
10926 flags = bmap->flags;
10927 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
10928 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED);
10929 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
10930 res = isl_basic_map_alloc_space(dim,
10931 bmap->n_div, bmap->n_eq, bmap->n_ineq);
10932 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
10933 if (res)
10934 res->flags = flags;
10935 res = isl_basic_map_finalize(res);
10936 return res;
10937error:
10938 free(dim_map);
10939 isl_basic_map_free(bmap);
10940 isl_space_free(dim);
10941 return NULL;
10942}
10943
10944/* Reorder the dimensions of "map" according to given reordering.
10945 */
10946__isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
10947 __isl_take isl_reordering *r)
10948{
10949 int i;
10950 struct isl_dim_map *dim_map;
10951
10952 map = isl_map_cow(map);
10953 dim_map = isl_dim_map_from_reordering(r);
10954 if (!map || !r || !dim_map)
10955 goto error;
10956
10957 for (i = 0; i < map->n; ++i) {
10958 struct isl_dim_map *dim_map_i;
10959
10960 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
10961
10962 map->p[i] = isl_basic_map_realign(map->p[i],
10963 isl_space_copy(r->dim), dim_map_i);
10964
10965 if (!map->p[i])
10966 goto error;
10967 }
10968
10969 map = isl_map_reset_space(map, isl_space_copy(r->dim));
10970
10971 isl_reordering_free(r);
10972 free(dim_map);
10973 return map;
10974error:
10975 free(dim_map);
10976 isl_map_free(map);
10977 isl_reordering_free(r);
10978 return NULL;
10979}
10980
10981__isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
10982 __isl_take isl_reordering *r)
10983{
10984 return (isl_set *)isl_map_realign((isl_map *)set, r);
10985}
10986
10987__isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
10988 __isl_take isl_space *model)
10989{
10990 isl_ctx *ctx;
10991
10992 if (!map || !model)
10993 goto error;
10994
10995 ctx = isl_space_get_ctx(model);
10996 if (!isl_space_has_named_params(model))
10997 isl_die(ctx, isl_error_invalid,
10998 "model has unnamed parameters", goto error);
10999 if (!isl_space_has_named_params(map->dim))
11000 isl_die(ctx, isl_error_invalid,
11001 "relation has unnamed parameters", goto error);
11002 if (!isl_space_match(map->dim, isl_dim_param, model, isl_dim_param)) {
11003 isl_reordering *exp;
11004
11005 model = isl_space_drop_dims(model, isl_dim_in,
11006 0, isl_space_dim(model, isl_dim_in));
11007 model = isl_space_drop_dims(model, isl_dim_out,
11008 0, isl_space_dim(model, isl_dim_out));
11009 exp = isl_parameter_alignment_reordering(map->dim, model);
11010 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
11011 map = isl_map_realign(map, exp);
11012 }
11013
11014 isl_space_free(model);
11015 return map;
11016error:
11017 isl_space_free(model);
11018 isl_map_free(map);
11019 return NULL;
11020}
11021
11022__isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
11023 __isl_take isl_space *model)
11024{
11025 return isl_map_align_params(set, model);
11026}
11027
11028/* Align the parameters of "bmap" to those of "model", introducing
11029 * additional parameters if needed.
11030 */
11031__isl_give isl_basic_map *isl_basic_map_align_params(
11032 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
11033{
11034 isl_ctx *ctx;
11035
11036 if (!bmap || !model)
11037 goto error;
11038
11039 ctx = isl_space_get_ctx(model);
11040 if (!isl_space_has_named_params(model))
11041 isl_die(ctx, isl_error_invalid,
11042 "model has unnamed parameters", goto error);
11043 if (!isl_space_has_named_params(bmap->dim))
11044 isl_die(ctx, isl_error_invalid,
11045 "relation has unnamed parameters", goto error);
11046 if (!isl_space_match(bmap->dim, isl_dim_param, model, isl_dim_param)) {
11047 isl_reordering *exp;
11048 struct isl_dim_map *dim_map;
11049
11050 model = isl_space_drop_dims(model, isl_dim_in,
11051 0, isl_space_dim(model, isl_dim_in));
11052 model = isl_space_drop_dims(model, isl_dim_out,
11053 0, isl_space_dim(model, isl_dim_out));
11054 exp = isl_parameter_alignment_reordering(bmap->dim, model);
11055 exp = isl_reordering_extend_space(exp,
11056 isl_basic_map_get_space(bmap));
11057 dim_map = isl_dim_map_from_reordering(exp);
11058 bmap = isl_basic_map_realign(bmap,
11059 exp ? isl_space_copy(exp->dim) : NULL,
11060 isl_dim_map_extend(dim_map, bmap));
11061 isl_reordering_free(exp);
11062 free(dim_map);
11063 }
11064
11065 isl_space_free(model);
11066 return bmap;
11067error:
11068 isl_space_free(model);
11069 isl_basic_map_free(bmap);
11070 return NULL;
11071}
11072
11073/* Align the parameters of "bset" to those of "model", introducing
11074 * additional parameters if needed.
11075 */
11076__isl_give isl_basic_set *isl_basic_set_align_params(
11077 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
11078{
11079 return isl_basic_map_align_params(bset, model);
11080}
11081
11082__isl_give isl_mat *isl_basic_map_equalities_matrix(
11083 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11084 enum isl_dim_type c2, enum isl_dim_type c3,
11085 enum isl_dim_type c4, enum isl_dim_type c5)
11086{
11087 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11088 struct isl_mat *mat;
11089 int i, j, k;
11090 int pos;
11091
11092 if (!bmap)
11093 return NULL;
11094 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
11095 isl_basic_map_total_dim(bmap) + 1);
11096 if (!mat)
11097 return NULL;
11098 for (i = 0; i < bmap->n_eq; ++i)
11099 for (j = 0, pos = 0; j < 5; ++j) {
11100 int off = isl_basic_map_offset(bmap, c[j]);
11101 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11102 isl_int_set(mat->row[i][pos],
11103 bmap->eq[i][off + k]);
11104 ++pos;
11105 }
11106 }
11107
11108 return mat;
11109}
11110
11111__isl_give isl_mat *isl_basic_map_inequalities_matrix(
11112 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11113 enum isl_dim_type c2, enum isl_dim_type c3,
11114 enum isl_dim_type c4, enum isl_dim_type c5)
11115{
11116 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11117 struct isl_mat *mat;
11118 int i, j, k;
11119 int pos;
11120
11121 if (!bmap)
11122 return NULL;
11123 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
11124 isl_basic_map_total_dim(bmap) + 1);
11125 if (!mat)
11126 return NULL;
11127 for (i = 0; i < bmap->n_ineq; ++i)
11128 for (j = 0, pos = 0; j < 5; ++j) {
11129 int off = isl_basic_map_offset(bmap, c[j]);
11130 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11131 isl_int_set(mat->row[i][pos],
11132 bmap->ineq[i][off + k]);
11133 ++pos;
11134 }
11135 }
11136
11137 return mat;
11138}
11139
11140__isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
11141 __isl_take isl_space *dim,
11142 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11143 enum isl_dim_type c2, enum isl_dim_type c3,
11144 enum isl_dim_type c4, enum isl_dim_type c5)
11145{
11146 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11147 isl_basic_map *bmap;
11148 unsigned total;
11149 unsigned extra;
11150 int i, j, k, l;
11151 int pos;
11152
11153 if (!dim || !eq || !ineq)
11154 goto error;
11155
11156 if (eq->n_col != ineq->n_col)
11157 isl_die(dim->ctx, isl_error_invalid,
11158 "equalities and inequalities matrices should have "
11159 "same number of columns", goto error);
11160
11161 total = 1 + isl_space_dim(dim, isl_dim_all);
11162
11163 if (eq->n_col < total)
11164 isl_die(dim->ctx, isl_error_invalid,
11165 "number of columns too small", goto error);
11166
11167 extra = eq->n_col - total;
11168
11169 bmap = isl_basic_map_alloc_space(isl_space_copy(dim), extra,
11170 eq->n_row, ineq->n_row);
11171 if (!bmap)
11172 goto error;
11173 for (i = 0; i < extra; ++i) {
11174 k = isl_basic_map_alloc_div(bmap);
11175 if (k < 0)
11176 goto error;
11177 isl_int_set_si(bmap->div[k][0], 0);
11178 }
11179 for (i = 0; i < eq->n_row; ++i) {
11180 l = isl_basic_map_alloc_equality(bmap);
11181 if (l < 0)
11182 goto error;
11183 for (j = 0, pos = 0; j < 5; ++j) {
11184 int off = isl_basic_map_offset(bmap, c[j]);
11185 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11186 isl_int_set(bmap->eq[l][off + k],
11187 eq->row[i][pos]);
11188 ++pos;
11189 }
11190 }
11191 }
11192 for (i = 0; i < ineq->n_row; ++i) {
11193 l = isl_basic_map_alloc_inequality(bmap);
11194 if (l < 0)
11195 goto error;
11196 for (j = 0, pos = 0; j < 5; ++j) {
11197 int off = isl_basic_map_offset(bmap, c[j]);
11198 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11199 isl_int_set(bmap->ineq[l][off + k],
11200 ineq->row[i][pos]);
11201 ++pos;
11202 }
11203 }
11204 }
11205
11206 isl_space_free(dim);
11207 isl_mat_free(eq);
11208 isl_mat_free(ineq);
11209
11210 bmap = isl_basic_map_simplify(bmap);
11211 return isl_basic_map_finalize(bmap);
11212error:
11213 isl_space_free(dim);
11214 isl_mat_free(eq);
11215 isl_mat_free(ineq);
11216 return NULL;
11217}
11218
11219__isl_give isl_mat *isl_basic_set_equalities_matrix(
11220 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11221 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11222{
11223 return isl_basic_map_equalities_matrix((isl_basic_map *)bset,
11224 c1, c2, c3, c4, isl_dim_in);
11225}
11226
11227__isl_give isl_mat *isl_basic_set_inequalities_matrix(
11228 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11229 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11230{
11231 return isl_basic_map_inequalities_matrix((isl_basic_map *)bset,
11232 c1, c2, c3, c4, isl_dim_in);
11233}
11234
11235__isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
11236 __isl_take isl_space *dim,
11237 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11238 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11239{
11240 return (isl_basic_set*)
11241 isl_basic_map_from_constraint_matrices(dim, eq, ineq,
11242 c1, c2, c3, c4, isl_dim_in);
11243}
11244
Tobias Grosserb2f39922015-05-28 13:32:11 +000011245isl_bool isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
Tobias Grosser52a25232015-02-04 20:55:43 +000011246{
11247 if (!bmap)
Tobias Grosserb2f39922015-05-28 13:32:11 +000011248 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000011249
11250 return isl_space_can_zip(bmap->dim);
11251}
11252
Tobias Grosserb2f39922015-05-28 13:32:11 +000011253isl_bool isl_map_can_zip(__isl_keep isl_map *map)
Tobias Grosser52a25232015-02-04 20:55:43 +000011254{
11255 if (!map)
Tobias Grosserb2f39922015-05-28 13:32:11 +000011256 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000011257
11258 return isl_space_can_zip(map->dim);
11259}
11260
11261/* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11262 * (A -> C) -> (B -> D).
11263 */
11264__isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
11265{
11266 unsigned pos;
11267 unsigned n1;
11268 unsigned n2;
11269
11270 if (!bmap)
11271 return NULL;
11272
11273 if (!isl_basic_map_can_zip(bmap))
11274 isl_die(bmap->ctx, isl_error_invalid,
11275 "basic map cannot be zipped", goto error);
11276 pos = isl_basic_map_offset(bmap, isl_dim_in) +
11277 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
11278 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
11279 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
11280 bmap = isl_basic_map_cow(bmap);
11281 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
11282 if (!bmap)
11283 return NULL;
11284 bmap->dim = isl_space_zip(bmap->dim);
11285 if (!bmap->dim)
11286 goto error;
11287 return bmap;
11288error:
11289 isl_basic_map_free(bmap);
11290 return NULL;
11291}
11292
11293/* Given a map (A -> B) -> (C -> D), return the corresponding map
11294 * (A -> C) -> (B -> D).
11295 */
11296__isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
11297{
11298 int i;
11299
11300 if (!map)
11301 return NULL;
11302
11303 if (!isl_map_can_zip(map))
11304 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
11305 goto error);
11306
11307 map = isl_map_cow(map);
11308 if (!map)
11309 return NULL;
11310
11311 for (i = 0; i < map->n; ++i) {
11312 map->p[i] = isl_basic_map_zip(map->p[i]);
11313 if (!map->p[i])
11314 goto error;
11315 }
11316
11317 map->dim = isl_space_zip(map->dim);
11318 if (!map->dim)
11319 goto error;
11320
11321 return map;
11322error:
11323 isl_map_free(map);
11324 return NULL;
11325}
11326
11327/* Can we apply isl_basic_map_curry to "bmap"?
11328 * That is, does it have a nested relation in its domain?
11329 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000011330isl_bool isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
Tobias Grosser52a25232015-02-04 20:55:43 +000011331{
11332 if (!bmap)
Tobias Grosserb2f39922015-05-28 13:32:11 +000011333 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000011334
11335 return isl_space_can_curry(bmap->dim);
11336}
11337
11338/* Can we apply isl_map_curry to "map"?
11339 * That is, does it have a nested relation in its domain?
11340 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000011341isl_bool isl_map_can_curry(__isl_keep isl_map *map)
Tobias Grosser52a25232015-02-04 20:55:43 +000011342{
11343 if (!map)
Tobias Grosserb2f39922015-05-28 13:32:11 +000011344 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000011345
11346 return isl_space_can_curry(map->dim);
11347}
11348
11349/* Given a basic map (A -> B) -> C, return the corresponding basic map
11350 * A -> (B -> C).
11351 */
11352__isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
11353{
11354
11355 if (!bmap)
11356 return NULL;
11357
11358 if (!isl_basic_map_can_curry(bmap))
11359 isl_die(bmap->ctx, isl_error_invalid,
11360 "basic map cannot be curried", goto error);
11361 bmap = isl_basic_map_cow(bmap);
11362 if (!bmap)
11363 return NULL;
11364 bmap->dim = isl_space_curry(bmap->dim);
11365 if (!bmap->dim)
11366 goto error;
11367 return bmap;
11368error:
11369 isl_basic_map_free(bmap);
11370 return NULL;
11371}
11372
11373/* Given a map (A -> B) -> C, return the corresponding map
11374 * A -> (B -> C).
11375 */
11376__isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
11377{
11378 int i;
11379
11380 if (!map)
11381 return NULL;
11382
11383 if (!isl_map_can_curry(map))
11384 isl_die(map->ctx, isl_error_invalid, "map cannot be curried",
11385 goto error);
11386
11387 map = isl_map_cow(map);
11388 if (!map)
11389 return NULL;
11390
11391 for (i = 0; i < map->n; ++i) {
11392 map->p[i] = isl_basic_map_curry(map->p[i]);
11393 if (!map->p[i])
11394 goto error;
11395 }
11396
11397 map->dim = isl_space_curry(map->dim);
11398 if (!map->dim)
11399 goto error;
11400
11401 return map;
11402error:
11403 isl_map_free(map);
11404 return NULL;
11405}
11406
11407/* Can we apply isl_basic_map_uncurry to "bmap"?
11408 * That is, does it have a nested relation in its domain?
11409 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000011410isl_bool isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
Tobias Grosser52a25232015-02-04 20:55:43 +000011411{
11412 if (!bmap)
Tobias Grosserb2f39922015-05-28 13:32:11 +000011413 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000011414
11415 return isl_space_can_uncurry(bmap->dim);
11416}
11417
11418/* Can we apply isl_map_uncurry to "map"?
11419 * That is, does it have a nested relation in its domain?
11420 */
Tobias Grosserb2f39922015-05-28 13:32:11 +000011421isl_bool isl_map_can_uncurry(__isl_keep isl_map *map)
Tobias Grosser52a25232015-02-04 20:55:43 +000011422{
11423 if (!map)
Tobias Grosserb2f39922015-05-28 13:32:11 +000011424 return isl_bool_error;
Tobias Grosser52a25232015-02-04 20:55:43 +000011425
11426 return isl_space_can_uncurry(map->dim);
11427}
11428
11429/* Given a basic map A -> (B -> C), return the corresponding basic map
11430 * (A -> B) -> C.
11431 */
11432__isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
11433{
11434
11435 if (!bmap)
11436 return NULL;
11437
11438 if (!isl_basic_map_can_uncurry(bmap))
11439 isl_die(bmap->ctx, isl_error_invalid,
11440 "basic map cannot be uncurried",
11441 return isl_basic_map_free(bmap));
11442 bmap = isl_basic_map_cow(bmap);
11443 if (!bmap)
11444 return NULL;
11445 bmap->dim = isl_space_uncurry(bmap->dim);
11446 if (!bmap->dim)
11447 return isl_basic_map_free(bmap);
11448 return bmap;
11449}
11450
11451/* Given a map A -> (B -> C), return the corresponding map
11452 * (A -> B) -> C.
11453 */
11454__isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
11455{
11456 int i;
11457
11458 if (!map)
11459 return NULL;
11460
11461 if (!isl_map_can_uncurry(map))
11462 isl_die(map->ctx, isl_error_invalid, "map cannot be uncurried",
11463 return isl_map_free(map));
11464
11465 map = isl_map_cow(map);
11466 if (!map)
11467 return NULL;
11468
11469 for (i = 0; i < map->n; ++i) {
11470 map->p[i] = isl_basic_map_uncurry(map->p[i]);
11471 if (!map->p[i])
11472 return isl_map_free(map);
11473 }
11474
11475 map->dim = isl_space_uncurry(map->dim);
11476 if (!map->dim)
11477 return isl_map_free(map);
11478
11479 return map;
11480}
11481
11482/* Construct a basic map mapping the domain of the affine expression
11483 * to a one-dimensional range prescribed by the affine expression.
11484 */
11485__isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
11486{
11487 int k;
11488 int pos;
11489 isl_local_space *ls;
11490 isl_basic_map *bmap;
11491
11492 if (!aff)
11493 return NULL;
11494
11495 ls = isl_aff_get_local_space(aff);
11496 bmap = isl_basic_map_from_local_space(ls);
11497 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
11498 k = isl_basic_map_alloc_equality(bmap);
11499 if (k < 0)
11500 goto error;
11501
11502 pos = isl_basic_map_offset(bmap, isl_dim_out);
11503 isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
11504 isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
11505 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
11506 aff->v->size - (pos + 1));
11507
11508 isl_aff_free(aff);
11509 bmap = isl_basic_map_finalize(bmap);
11510 return bmap;
11511error:
11512 isl_aff_free(aff);
11513 isl_basic_map_free(bmap);
11514 return NULL;
11515}
11516
11517/* Construct a map mapping the domain of the affine expression
11518 * to a one-dimensional range prescribed by the affine expression.
11519 */
11520__isl_give isl_map *isl_map_from_aff(__isl_take isl_aff *aff)
11521{
11522 isl_basic_map *bmap;
11523
11524 bmap = isl_basic_map_from_aff(aff);
11525 return isl_map_from_basic_map(bmap);
11526}
11527
11528/* Construct a basic map mapping the domain the multi-affine expression
11529 * to its range, with each dimension in the range equated to the
11530 * corresponding affine expression.
11531 */
11532__isl_give isl_basic_map *isl_basic_map_from_multi_aff(
11533 __isl_take isl_multi_aff *maff)
11534{
11535 int i;
11536 isl_space *space;
11537 isl_basic_map *bmap;
11538
11539 if (!maff)
11540 return NULL;
11541
11542 if (isl_space_dim(maff->space, isl_dim_out) != maff->n)
11543 isl_die(isl_multi_aff_get_ctx(maff), isl_error_internal,
11544 "invalid space", goto error);
11545
11546 space = isl_space_domain(isl_multi_aff_get_space(maff));
11547 bmap = isl_basic_map_universe(isl_space_from_domain(space));
11548
11549 for (i = 0; i < maff->n; ++i) {
11550 isl_aff *aff;
11551 isl_basic_map *bmap_i;
11552
11553 aff = isl_aff_copy(maff->p[i]);
11554 bmap_i = isl_basic_map_from_aff(aff);
11555
11556 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
11557 }
11558
11559 bmap = isl_basic_map_reset_space(bmap, isl_multi_aff_get_space(maff));
11560
11561 isl_multi_aff_free(maff);
11562 return bmap;
11563error:
11564 isl_multi_aff_free(maff);
11565 return NULL;
11566}
11567
11568/* Construct a map mapping the domain the multi-affine expression
11569 * to its range, with each dimension in the range equated to the
11570 * corresponding affine expression.
11571 */
11572__isl_give isl_map *isl_map_from_multi_aff(__isl_take isl_multi_aff *maff)
11573{
11574 isl_basic_map *bmap;
11575
11576 bmap = isl_basic_map_from_multi_aff(maff);
11577 return isl_map_from_basic_map(bmap);
11578}
11579
11580/* Construct a basic map mapping a domain in the given space to
11581 * to an n-dimensional range, with n the number of elements in the list,
11582 * where each coordinate in the range is prescribed by the
11583 * corresponding affine expression.
11584 * The domains of all affine expressions in the list are assumed to match
11585 * domain_dim.
11586 */
11587__isl_give isl_basic_map *isl_basic_map_from_aff_list(
11588 __isl_take isl_space *domain_dim, __isl_take isl_aff_list *list)
11589{
11590 int i;
11591 isl_space *dim;
11592 isl_basic_map *bmap;
11593
11594 if (!list)
11595 return NULL;
11596
11597 dim = isl_space_from_domain(domain_dim);
11598 bmap = isl_basic_map_universe(dim);
11599
11600 for (i = 0; i < list->n; ++i) {
11601 isl_aff *aff;
11602 isl_basic_map *bmap_i;
11603
11604 aff = isl_aff_copy(list->p[i]);
11605 bmap_i = isl_basic_map_from_aff(aff);
11606
11607 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
11608 }
11609
11610 isl_aff_list_free(list);
11611 return bmap;
11612}
11613
11614__isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
11615 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11616{
11617 return isl_map_equate(set, type1, pos1, type2, pos2);
11618}
11619
11620/* Construct a basic map where the given dimensions are equal to each other.
11621 */
11622static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
11623 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11624{
11625 isl_basic_map *bmap = NULL;
11626 int i;
11627
11628 if (!space)
11629 return NULL;
11630
11631 if (pos1 >= isl_space_dim(space, type1))
11632 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11633 "index out of bounds", goto error);
11634 if (pos2 >= isl_space_dim(space, type2))
11635 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11636 "index out of bounds", goto error);
11637
11638 if (type1 == type2 && pos1 == pos2)
11639 return isl_basic_map_universe(space);
11640
11641 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
11642 i = isl_basic_map_alloc_equality(bmap);
11643 if (i < 0)
11644 goto error;
11645 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
11646 pos1 += isl_basic_map_offset(bmap, type1);
11647 pos2 += isl_basic_map_offset(bmap, type2);
11648 isl_int_set_si(bmap->eq[i][pos1], -1);
11649 isl_int_set_si(bmap->eq[i][pos2], 1);
11650 bmap = isl_basic_map_finalize(bmap);
11651 isl_space_free(space);
11652 return bmap;
11653error:
11654 isl_space_free(space);
11655 isl_basic_map_free(bmap);
11656 return NULL;
11657}
11658
11659/* Add a constraint imposing that the given two dimensions are equal.
11660 */
11661__isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
11662 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11663{
11664 isl_basic_map *eq;
11665
11666 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
11667
11668 bmap = isl_basic_map_intersect(bmap, eq);
11669
11670 return bmap;
11671}
11672
11673/* Add a constraint imposing that the given two dimensions are equal.
11674 */
11675__isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
11676 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11677{
11678 isl_basic_map *bmap;
11679
11680 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
11681
11682 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
11683
11684 return map;
11685}
11686
11687/* Add a constraint imposing that the given two dimensions have opposite values.
11688 */
11689__isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
11690 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11691{
11692 isl_basic_map *bmap = NULL;
11693 int i;
11694
11695 if (!map)
11696 return NULL;
11697
11698 if (pos1 >= isl_map_dim(map, type1))
11699 isl_die(map->ctx, isl_error_invalid,
11700 "index out of bounds", goto error);
11701 if (pos2 >= isl_map_dim(map, type2))
11702 isl_die(map->ctx, isl_error_invalid,
11703 "index out of bounds", goto error);
11704
11705 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
11706 i = isl_basic_map_alloc_equality(bmap);
11707 if (i < 0)
11708 goto error;
11709 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
11710 pos1 += isl_basic_map_offset(bmap, type1);
11711 pos2 += isl_basic_map_offset(bmap, type2);
11712 isl_int_set_si(bmap->eq[i][pos1], 1);
11713 isl_int_set_si(bmap->eq[i][pos2], 1);
11714 bmap = isl_basic_map_finalize(bmap);
11715
11716 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
11717
11718 return map;
11719error:
11720 isl_basic_map_free(bmap);
11721 isl_map_free(map);
11722 return NULL;
11723}
11724
11725/* Construct a constraint imposing that the value of the first dimension is
11726 * greater than or equal to that of the second.
11727 */
11728static __isl_give isl_constraint *constraint_order_ge(
11729 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
11730 enum isl_dim_type type2, int pos2)
11731{
11732 isl_constraint *c;
11733
11734 if (!space)
11735 return NULL;
11736
Tobias Grosserb2f39922015-05-28 13:32:11 +000011737 c = isl_constraint_alloc_inequality(isl_local_space_from_space(space));
Tobias Grosser52a25232015-02-04 20:55:43 +000011738
11739 if (pos1 >= isl_constraint_dim(c, type1))
11740 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
11741 "index out of bounds", return isl_constraint_free(c));
11742 if (pos2 >= isl_constraint_dim(c, type2))
11743 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
11744 "index out of bounds", return isl_constraint_free(c));
11745
11746 if (type1 == type2 && pos1 == pos2)
11747 return c;
11748
11749 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
11750 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
11751
11752 return c;
11753}
11754
11755/* Add a constraint imposing that the value of the first dimension is
11756 * greater than or equal to that of the second.
11757 */
11758__isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
11759 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11760{
11761 isl_constraint *c;
11762 isl_space *space;
11763
11764 if (type1 == type2 && pos1 == pos2)
11765 return bmap;
11766 space = isl_basic_map_get_space(bmap);
11767 c = constraint_order_ge(space, type1, pos1, type2, pos2);
11768 bmap = isl_basic_map_add_constraint(bmap, c);
11769
11770 return bmap;
11771}
11772
11773/* Add a constraint imposing that the value of the first dimension is
11774 * greater than or equal to that of the second.
11775 */
11776__isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
11777 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11778{
11779 isl_constraint *c;
11780 isl_space *space;
11781
11782 if (type1 == type2 && pos1 == pos2)
11783 return map;
11784 space = isl_map_get_space(map);
11785 c = constraint_order_ge(space, type1, pos1, type2, pos2);
11786 map = isl_map_add_constraint(map, c);
11787
11788 return map;
11789}
11790
11791/* Add a constraint imposing that the value of the first dimension is
11792 * less than or equal to that of the second.
11793 */
11794__isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
11795 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11796{
11797 return isl_map_order_ge(map, type2, pos2, type1, pos1);
11798}
11799
11800/* Construct a basic map where the value of the first dimension is
11801 * greater than that of the second.
11802 */
11803static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
11804 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11805{
11806 isl_basic_map *bmap = NULL;
11807 int i;
11808
11809 if (!space)
11810 return NULL;
11811
11812 if (pos1 >= isl_space_dim(space, type1))
11813 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11814 "index out of bounds", goto error);
11815 if (pos2 >= isl_space_dim(space, type2))
11816 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11817 "index out of bounds", goto error);
11818
11819 if (type1 == type2 && pos1 == pos2)
11820 return isl_basic_map_empty(space);
11821
11822 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
11823 i = isl_basic_map_alloc_inequality(bmap);
11824 if (i < 0)
11825 return isl_basic_map_free(bmap);
11826 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
11827 pos1 += isl_basic_map_offset(bmap, type1);
11828 pos2 += isl_basic_map_offset(bmap, type2);
11829 isl_int_set_si(bmap->ineq[i][pos1], 1);
11830 isl_int_set_si(bmap->ineq[i][pos2], -1);
11831 isl_int_set_si(bmap->ineq[i][0], -1);
11832 bmap = isl_basic_map_finalize(bmap);
11833
11834 return bmap;
11835error:
11836 isl_space_free(space);
11837 isl_basic_map_free(bmap);
11838 return NULL;
11839}
11840
11841/* Add a constraint imposing that the value of the first dimension is
11842 * greater than that of the second.
11843 */
11844__isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
11845 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11846{
11847 isl_basic_map *gt;
11848
11849 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
11850
11851 bmap = isl_basic_map_intersect(bmap, gt);
11852
11853 return bmap;
11854}
11855
11856/* Add a constraint imposing that the value of the first dimension is
11857 * greater than that of the second.
11858 */
11859__isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
11860 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11861{
11862 isl_basic_map *bmap;
11863
11864 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
11865
11866 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
11867
11868 return map;
11869}
11870
11871/* Add a constraint imposing that the value of the first dimension is
11872 * smaller than that of the second.
11873 */
11874__isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
11875 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11876{
11877 return isl_map_order_gt(map, type2, pos2, type1, pos1);
11878}
11879
11880__isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
11881 int pos)
11882{
11883 isl_aff *div;
11884 isl_local_space *ls;
11885
11886 if (!bmap)
11887 return NULL;
11888
11889 if (!isl_basic_map_divs_known(bmap))
11890 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
11891 "some divs are unknown", return NULL);
11892
11893 ls = isl_basic_map_get_local_space(bmap);
11894 div = isl_local_space_get_div(ls, pos);
11895 isl_local_space_free(ls);
11896
11897 return div;
11898}
11899
11900__isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
11901 int pos)
11902{
11903 return isl_basic_map_get_div(bset, pos);
11904}
11905
11906/* Plug in "subs" for dimension "type", "pos" of "bset".
11907 *
11908 * Let i be the dimension to replace and let "subs" be of the form
11909 *
11910 * f/d
11911 *
11912 * Any integer division with a non-zero coefficient for i,
11913 *
11914 * floor((a i + g)/m)
11915 *
11916 * is replaced by
11917 *
11918 * floor((a f + d g)/(m d))
11919 *
11920 * Constraints of the form
11921 *
11922 * a i + g
11923 *
11924 * are replaced by
11925 *
11926 * a f + d g
11927 *
11928 * We currently require that "subs" is an integral expression.
11929 * Handling rational expressions may require us to add stride constraints
11930 * as we do in isl_basic_set_preimage_multi_aff.
11931 */
11932__isl_give isl_basic_set *isl_basic_set_substitute(
11933 __isl_take isl_basic_set *bset,
11934 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
11935{
11936 int i;
11937 isl_int v;
11938 isl_ctx *ctx;
11939
11940 if (bset && isl_basic_set_plain_is_empty(bset))
11941 return bset;
11942
11943 bset = isl_basic_set_cow(bset);
11944 if (!bset || !subs)
11945 goto error;
11946
11947 ctx = isl_basic_set_get_ctx(bset);
11948 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
11949 isl_die(ctx, isl_error_invalid,
11950 "spaces don't match", goto error);
11951 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
11952 isl_die(ctx, isl_error_unsupported,
11953 "cannot handle divs yet", goto error);
11954 if (!isl_int_is_one(subs->v->el[0]))
11955 isl_die(ctx, isl_error_invalid,
11956 "can only substitute integer expressions", goto error);
11957
11958 pos += isl_basic_set_offset(bset, type);
11959
11960 isl_int_init(v);
11961
11962 for (i = 0; i < bset->n_eq; ++i) {
11963 if (isl_int_is_zero(bset->eq[i][pos]))
11964 continue;
11965 isl_int_set(v, bset->eq[i][pos]);
11966 isl_int_set_si(bset->eq[i][pos], 0);
11967 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
11968 v, subs->v->el + 1, subs->v->size - 1);
11969 }
11970
11971 for (i = 0; i < bset->n_ineq; ++i) {
11972 if (isl_int_is_zero(bset->ineq[i][pos]))
11973 continue;
11974 isl_int_set(v, bset->ineq[i][pos]);
11975 isl_int_set_si(bset->ineq[i][pos], 0);
11976 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
11977 v, subs->v->el + 1, subs->v->size - 1);
11978 }
11979
11980 for (i = 0; i < bset->n_div; ++i) {
11981 if (isl_int_is_zero(bset->div[i][1 + pos]))
11982 continue;
11983 isl_int_set(v, bset->div[i][1 + pos]);
11984 isl_int_set_si(bset->div[i][1 + pos], 0);
11985 isl_seq_combine(bset->div[i] + 1,
11986 subs->v->el[0], bset->div[i] + 1,
11987 v, subs->v->el + 1, subs->v->size - 1);
11988 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
11989 }
11990
11991 isl_int_clear(v);
11992
11993 bset = isl_basic_set_simplify(bset);
11994 return isl_basic_set_finalize(bset);
11995error:
11996 isl_basic_set_free(bset);
11997 return NULL;
11998}
11999
12000/* Plug in "subs" for dimension "type", "pos" of "set".
12001 */
12002__isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
12003 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12004{
12005 int i;
12006
12007 if (set && isl_set_plain_is_empty(set))
12008 return set;
12009
12010 set = isl_set_cow(set);
12011 if (!set || !subs)
12012 goto error;
12013
12014 for (i = set->n - 1; i >= 0; --i) {
12015 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
12016 if (remove_if_empty(set, i) < 0)
12017 goto error;
12018 }
12019
12020 return set;
12021error:
12022 isl_set_free(set);
12023 return NULL;
12024}
12025
12026/* Check if the range of "ma" is compatible with the domain or range
12027 * (depending on "type") of "bmap".
12028 * Return -1 if anything is wrong.
12029 */
12030static int check_basic_map_compatible_range_multi_aff(
12031 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
12032 __isl_keep isl_multi_aff *ma)
12033{
12034 int m;
12035 isl_space *ma_space;
12036
12037 ma_space = isl_multi_aff_get_space(ma);
12038
12039 m = isl_space_match(bmap->dim, isl_dim_param, ma_space, isl_dim_param);
12040 if (m < 0)
12041 goto error;
12042 if (!m)
12043 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12044 "parameters don't match", goto error);
12045 m = isl_space_tuple_is_equal(bmap->dim, type, ma_space, isl_dim_out);
12046 if (m < 0)
12047 goto error;
12048 if (!m)
12049 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12050 "spaces don't match", goto error);
12051
12052 isl_space_free(ma_space);
12053 return m;
12054error:
12055 isl_space_free(ma_space);
12056 return -1;
12057}
12058
12059/* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12060 * coefficients before the transformed range of dimensions,
12061 * the "n_after" coefficients after the transformed range of dimensions
12062 * and the coefficients of the other divs in "bmap".
12063 */
12064static int set_ma_divs(__isl_keep isl_basic_map *bmap,
12065 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
12066{
12067 int i;
12068 int n_param;
12069 int n_set;
12070 isl_local_space *ls;
12071
12072 if (n_div == 0)
12073 return 0;
12074
12075 ls = isl_aff_get_domain_local_space(ma->p[0]);
12076 if (!ls)
12077 return -1;
12078
12079 n_param = isl_local_space_dim(ls, isl_dim_param);
12080 n_set = isl_local_space_dim(ls, isl_dim_set);
12081 for (i = 0; i < n_div; ++i) {
12082 int o_bmap = 0, o_ls = 0;
12083
12084 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
12085 o_bmap += 1 + 1 + n_param;
12086 o_ls += 1 + 1 + n_param;
12087 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
12088 o_bmap += n_before;
12089 isl_seq_cpy(bmap->div[i] + o_bmap,
12090 ls->div->row[i] + o_ls, n_set);
12091 o_bmap += n_set;
12092 o_ls += n_set;
12093 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
12094 o_bmap += n_after;
12095 isl_seq_cpy(bmap->div[i] + o_bmap,
12096 ls->div->row[i] + o_ls, n_div);
12097 o_bmap += n_div;
12098 o_ls += n_div;
12099 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
12100 if (isl_basic_set_add_div_constraints(bmap, i) < 0)
12101 goto error;
12102 }
12103
12104 isl_local_space_free(ls);
12105 return 0;
12106error:
12107 isl_local_space_free(ls);
12108 return -1;
12109}
12110
12111/* How many stride constraints does "ma" enforce?
12112 * That is, how many of the affine expressions have a denominator
12113 * different from one?
12114 */
12115static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
12116{
12117 int i;
12118 int strides = 0;
12119
12120 for (i = 0; i < ma->n; ++i)
12121 if (!isl_int_is_one(ma->p[i]->v->el[0]))
12122 strides++;
12123
12124 return strides;
12125}
12126
12127/* For each affine expression in ma of the form
12128 *
12129 * x_i = (f_i y + h_i)/m_i
12130 *
12131 * with m_i different from one, add a constraint to "bmap"
12132 * of the form
12133 *
12134 * f_i y + h_i = m_i alpha_i
12135 *
12136 * with alpha_i an additional existentially quantified variable.
12137 */
12138static __isl_give isl_basic_map *add_ma_strides(
12139 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
12140 int n_before, int n_after)
12141{
12142 int i, k;
12143 int div;
12144 int total;
12145 int n_param;
12146 int n_in;
12147 int n_div;
12148
12149 total = isl_basic_map_total_dim(bmap);
12150 n_param = isl_multi_aff_dim(ma, isl_dim_param);
12151 n_in = isl_multi_aff_dim(ma, isl_dim_in);
12152 n_div = isl_multi_aff_dim(ma, isl_dim_div);
12153 for (i = 0; i < ma->n; ++i) {
12154 int o_bmap = 0, o_ma = 1;
12155
12156 if (isl_int_is_one(ma->p[i]->v->el[0]))
12157 continue;
12158 div = isl_basic_map_alloc_div(bmap);
12159 k = isl_basic_map_alloc_equality(bmap);
12160 if (div < 0 || k < 0)
12161 goto error;
12162 isl_int_set_si(bmap->div[div][0], 0);
12163 isl_seq_cpy(bmap->eq[k] + o_bmap,
12164 ma->p[i]->v->el + o_ma, 1 + n_param);
12165 o_bmap += 1 + n_param;
12166 o_ma += 1 + n_param;
12167 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
12168 o_bmap += n_before;
12169 isl_seq_cpy(bmap->eq[k] + o_bmap,
12170 ma->p[i]->v->el + o_ma, n_in);
12171 o_bmap += n_in;
12172 o_ma += n_in;
12173 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
12174 o_bmap += n_after;
12175 isl_seq_cpy(bmap->eq[k] + o_bmap,
12176 ma->p[i]->v->el + o_ma, n_div);
12177 o_bmap += n_div;
12178 o_ma += n_div;
12179 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
12180 isl_int_neg(bmap->eq[k][1 + total], ma->p[i]->v->el[0]);
12181 total++;
12182 }
12183
12184 return bmap;
12185error:
12186 isl_basic_map_free(bmap);
12187 return NULL;
12188}
12189
12190/* Replace the domain or range space (depending on "type) of "space" by "set".
12191 */
12192static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
12193 enum isl_dim_type type, __isl_take isl_space *set)
12194{
12195 if (type == isl_dim_in) {
12196 space = isl_space_range(space);
12197 space = isl_space_map_from_domain_and_range(set, space);
12198 } else {
12199 space = isl_space_domain(space);
12200 space = isl_space_map_from_domain_and_range(space, set);
12201 }
12202
12203 return space;
12204}
12205
12206/* Compute the preimage of the domain or range (depending on "type")
12207 * of "bmap" under the function represented by "ma".
12208 * In other words, plug in "ma" in the domain or range of "bmap".
12209 * The result is a basic map that lives in the same space as "bmap"
12210 * except that the domain or range has been replaced by
12211 * the domain space of "ma".
12212 *
12213 * If bmap is represented by
12214 *
12215 * A(p) + S u + B x + T v + C(divs) >= 0,
12216 *
12217 * where u and x are input and output dimensions if type == isl_dim_out
12218 * while x and v are input and output dimensions if type == isl_dim_in,
12219 * and ma is represented by
12220 *
12221 * x = D(p) + F(y) + G(divs')
12222 *
12223 * then the result is
12224 *
12225 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12226 *
12227 * The divs in the input set are similarly adjusted.
12228 * In particular
12229 *
12230 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12231 *
12232 * becomes
12233 *
12234 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12235 * B_i G(divs') + c_i(divs))/n_i)
12236 *
12237 * If bmap is not a rational map and if F(y) involves any denominators
12238 *
12239 * x_i = (f_i y + h_i)/m_i
12240 *
12241 * then additional constraints are added to ensure that we only
12242 * map back integer points. That is we enforce
12243 *
12244 * f_i y + h_i = m_i alpha_i
12245 *
12246 * with alpha_i an additional existentially quantified variable.
12247 *
12248 * We first copy over the divs from "ma".
12249 * Then we add the modified constraints and divs from "bmap".
12250 * Finally, we add the stride constraints, if needed.
12251 */
12252__isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
12253 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
12254 __isl_take isl_multi_aff *ma)
12255{
12256 int i, k;
12257 isl_space *space;
12258 isl_basic_map *res = NULL;
12259 int n_before, n_after, n_div_bmap, n_div_ma;
12260 isl_int f, c1, c2, g;
12261 int rational, strides;
12262
12263 isl_int_init(f);
12264 isl_int_init(c1);
12265 isl_int_init(c2);
12266 isl_int_init(g);
12267
12268 ma = isl_multi_aff_align_divs(ma);
12269 if (!bmap || !ma)
12270 goto error;
12271 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
12272 goto error;
12273
12274 if (type == isl_dim_in) {
12275 n_before = 0;
12276 n_after = isl_basic_map_dim(bmap, isl_dim_out);
12277 } else {
12278 n_before = isl_basic_map_dim(bmap, isl_dim_in);
12279 n_after = 0;
12280 }
12281 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
12282 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
12283
12284 space = isl_multi_aff_get_domain_space(ma);
12285 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
12286 rational = isl_basic_map_is_rational(bmap);
12287 strides = rational ? 0 : multi_aff_strides(ma);
12288 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
12289 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
12290 if (rational)
12291 res = isl_basic_map_set_rational(res);
12292
12293 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
12294 if (isl_basic_map_alloc_div(res) < 0)
12295 goto error;
12296
12297 if (set_ma_divs(res, ma, n_before, n_after, n_div_ma) < 0)
12298 goto error;
12299
12300 for (i = 0; i < bmap->n_eq; ++i) {
12301 k = isl_basic_map_alloc_equality(res);
12302 if (k < 0)
12303 goto error;
12304 isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
12305 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12306 }
12307
12308 for (i = 0; i < bmap->n_ineq; ++i) {
12309 k = isl_basic_map_alloc_inequality(res);
12310 if (k < 0)
12311 goto error;
12312 isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
12313 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12314 }
12315
12316 for (i = 0; i < bmap->n_div; ++i) {
12317 if (isl_int_is_zero(bmap->div[i][0])) {
12318 isl_int_set_si(res->div[n_div_ma + i][0], 0);
12319 continue;
12320 }
12321 isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
12322 n_before, n_after, n_div_ma, n_div_bmap,
12323 f, c1, c2, g, 1);
12324 }
12325
12326 if (strides)
12327 res = add_ma_strides(res, ma, n_before, n_after);
12328
12329 isl_int_clear(f);
12330 isl_int_clear(c1);
12331 isl_int_clear(c2);
12332 isl_int_clear(g);
12333 isl_basic_map_free(bmap);
12334 isl_multi_aff_free(ma);
12335 res = isl_basic_set_simplify(res);
12336 return isl_basic_map_finalize(res);
12337error:
12338 isl_int_clear(f);
12339 isl_int_clear(c1);
12340 isl_int_clear(c2);
12341 isl_int_clear(g);
12342 isl_basic_map_free(bmap);
12343 isl_multi_aff_free(ma);
12344 isl_basic_map_free(res);
12345 return NULL;
12346}
12347
12348/* Compute the preimage of "bset" under the function represented by "ma".
12349 * In other words, plug in "ma" in "bset". The result is a basic set
12350 * that lives in the domain space of "ma".
12351 */
12352__isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
12353 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
12354{
12355 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
12356}
12357
12358/* Compute the preimage of the domain of "bmap" under the function
12359 * represented by "ma".
12360 * In other words, plug in "ma" in the domain of "bmap".
12361 * The result is a basic map that lives in the same space as "bmap"
12362 * except that the domain has been replaced by the domain space of "ma".
12363 */
12364__isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
12365 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12366{
12367 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
12368}
12369
12370/* Compute the preimage of the range of "bmap" under the function
12371 * represented by "ma".
12372 * In other words, plug in "ma" in the range of "bmap".
12373 * The result is a basic map that lives in the same space as "bmap"
12374 * except that the range has been replaced by the domain space of "ma".
12375 */
12376__isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
12377 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12378{
12379 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
12380}
12381
12382/* Check if the range of "ma" is compatible with the domain or range
12383 * (depending on "type") of "map".
12384 * Return -1 if anything is wrong.
12385 */
12386static int check_map_compatible_range_multi_aff(
12387 __isl_keep isl_map *map, enum isl_dim_type type,
12388 __isl_keep isl_multi_aff *ma)
12389{
12390 int m;
12391 isl_space *ma_space;
12392
12393 ma_space = isl_multi_aff_get_space(ma);
12394 m = isl_space_tuple_is_equal(map->dim, type, ma_space, isl_dim_out);
12395 isl_space_free(ma_space);
12396 if (m >= 0 && !m)
12397 isl_die(isl_map_get_ctx(map), isl_error_invalid,
12398 "spaces don't match", return -1);
12399 return m;
12400}
12401
12402/* Compute the preimage of the domain or range (depending on "type")
12403 * of "map" under the function represented by "ma".
12404 * In other words, plug in "ma" in the domain or range of "map".
12405 * The result is a map that lives in the same space as "map"
12406 * except that the domain or range has been replaced by
12407 * the domain space of "ma".
12408 *
12409 * The parameters are assumed to have been aligned.
12410 */
12411static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
12412 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12413{
12414 int i;
12415 isl_space *space;
12416
12417 map = isl_map_cow(map);
12418 ma = isl_multi_aff_align_divs(ma);
12419 if (!map || !ma)
12420 goto error;
12421 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
12422 goto error;
12423
12424 for (i = 0; i < map->n; ++i) {
12425 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
12426 isl_multi_aff_copy(ma));
12427 if (!map->p[i])
12428 goto error;
12429 }
12430
12431 space = isl_multi_aff_get_domain_space(ma);
12432 space = isl_space_set(isl_map_get_space(map), type, space);
12433
12434 isl_space_free(map->dim);
12435 map->dim = space;
12436 if (!map->dim)
12437 goto error;
12438
12439 isl_multi_aff_free(ma);
12440 if (map->n > 1)
12441 ISL_F_CLR(map, ISL_MAP_DISJOINT);
12442 ISL_F_CLR(map, ISL_SET_NORMALIZED);
12443 return map;
12444error:
12445 isl_multi_aff_free(ma);
12446 isl_map_free(map);
12447 return NULL;
12448}
12449
12450/* Compute the preimage of the domain or range (depending on "type")
12451 * of "map" under the function represented by "ma".
12452 * In other words, plug in "ma" in the domain or range of "map".
12453 * The result is a map that lives in the same space as "map"
12454 * except that the domain or range has been replaced by
12455 * the domain space of "ma".
12456 */
12457__isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
12458 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12459{
12460 if (!map || !ma)
12461 goto error;
12462
12463 if (isl_space_match(map->dim, isl_dim_param, ma->space, isl_dim_param))
12464 return map_preimage_multi_aff(map, type, ma);
12465
12466 if (!isl_space_has_named_params(map->dim) ||
12467 !isl_space_has_named_params(ma->space))
12468 isl_die(map->ctx, isl_error_invalid,
12469 "unaligned unnamed parameters", goto error);
12470 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
12471 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
12472
12473 return map_preimage_multi_aff(map, type, ma);
12474error:
12475 isl_multi_aff_free(ma);
12476 return isl_map_free(map);
12477}
12478
12479/* Compute the preimage of "set" under the function represented by "ma".
12480 * In other words, plug in "ma" in "set". The result is a set
12481 * that lives in the domain space of "ma".
12482 */
12483__isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
12484 __isl_take isl_multi_aff *ma)
12485{
12486 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
12487}
12488
12489/* Compute the preimage of the domain of "map" under the function
12490 * represented by "ma".
12491 * In other words, plug in "ma" in the domain of "map".
12492 * The result is a map that lives in the same space as "map"
12493 * except that the domain has been replaced by the domain space of "ma".
12494 */
12495__isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
12496 __isl_take isl_multi_aff *ma)
12497{
12498 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
12499}
12500
12501/* Compute the preimage of the range of "map" under the function
12502 * represented by "ma".
12503 * In other words, plug in "ma" in the range of "map".
12504 * The result is a map that lives in the same space as "map"
12505 * except that the range has been replaced by the domain space of "ma".
12506 */
12507__isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
12508 __isl_take isl_multi_aff *ma)
12509{
12510 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
12511}
12512
12513/* Compute the preimage of "map" under the function represented by "pma".
12514 * In other words, plug in "pma" in the domain or range of "map".
12515 * The result is a map that lives in the same space as "map",
12516 * except that the space of type "type" has been replaced by
12517 * the domain space of "pma".
12518 *
12519 * The parameters of "map" and "pma" are assumed to have been aligned.
12520 */
12521static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
12522 __isl_take isl_map *map, enum isl_dim_type type,
12523 __isl_take isl_pw_multi_aff *pma)
12524{
12525 int i;
12526 isl_map *res;
12527
12528 if (!pma)
12529 goto error;
12530
12531 if (pma->n == 0) {
12532 isl_pw_multi_aff_free(pma);
12533 res = isl_map_empty(isl_map_get_space(map));
12534 isl_map_free(map);
12535 return res;
12536 }
12537
12538 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
12539 isl_multi_aff_copy(pma->p[0].maff));
12540 if (type == isl_dim_in)
12541 res = isl_map_intersect_domain(res,
12542 isl_map_copy(pma->p[0].set));
12543 else
12544 res = isl_map_intersect_range(res,
12545 isl_map_copy(pma->p[0].set));
12546
12547 for (i = 1; i < pma->n; ++i) {
12548 isl_map *res_i;
12549
12550 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
12551 isl_multi_aff_copy(pma->p[i].maff));
12552 if (type == isl_dim_in)
12553 res_i = isl_map_intersect_domain(res_i,
12554 isl_map_copy(pma->p[i].set));
12555 else
12556 res_i = isl_map_intersect_range(res_i,
12557 isl_map_copy(pma->p[i].set));
12558 res = isl_map_union(res, res_i);
12559 }
12560
12561 isl_pw_multi_aff_free(pma);
12562 isl_map_free(map);
12563 return res;
12564error:
12565 isl_pw_multi_aff_free(pma);
12566 isl_map_free(map);
12567 return NULL;
12568}
12569
12570/* Compute the preimage of "map" under the function represented by "pma".
12571 * In other words, plug in "pma" in the domain or range of "map".
12572 * The result is a map that lives in the same space as "map",
12573 * except that the space of type "type" has been replaced by
12574 * the domain space of "pma".
12575 */
12576__isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
12577 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
12578{
12579 if (!map || !pma)
12580 goto error;
12581
12582 if (isl_space_match(map->dim, isl_dim_param, pma->dim, isl_dim_param))
12583 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
12584
12585 if (!isl_space_has_named_params(map->dim) ||
12586 !isl_space_has_named_params(pma->dim))
12587 isl_die(map->ctx, isl_error_invalid,
12588 "unaligned unnamed parameters", goto error);
12589 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
12590 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
12591
12592 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
12593error:
12594 isl_pw_multi_aff_free(pma);
12595 return isl_map_free(map);
12596}
12597
12598/* Compute the preimage of "set" under the function represented by "pma".
12599 * In other words, plug in "pma" in "set". The result is a set
12600 * that lives in the domain space of "pma".
12601 */
12602__isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
12603 __isl_take isl_pw_multi_aff *pma)
12604{
12605 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
12606}
12607
12608/* Compute the preimage of the domain of "map" under the function
12609 * represented by "pma".
12610 * In other words, plug in "pma" in the domain of "map".
12611 * The result is a map that lives in the same space as "map",
12612 * except that domain space has been replaced by the domain space of "pma".
12613 */
12614__isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
12615 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
12616{
12617 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
12618}
12619
12620/* Compute the preimage of the range of "map" under the function
12621 * represented by "pma".
12622 * In other words, plug in "pma" in the range of "map".
12623 * The result is a map that lives in the same space as "map",
12624 * except that range space has been replaced by the domain space of "pma".
12625 */
12626__isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
12627 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
12628{
12629 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
12630}
12631
12632/* Compute the preimage of "map" under the function represented by "mpa".
12633 * In other words, plug in "mpa" in the domain or range of "map".
12634 * The result is a map that lives in the same space as "map",
12635 * except that the space of type "type" has been replaced by
12636 * the domain space of "mpa".
12637 *
12638 * If the map does not involve any constraints that refer to the
12639 * dimensions of the substituted space, then the only possible
12640 * effect of "mpa" on the map is to map the space to a different space.
12641 * We create a separate isl_multi_aff to effectuate this change
12642 * in order to avoid spurious splitting of the map along the pieces
12643 * of "mpa".
12644 */
12645__isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
12646 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
12647{
12648 int n;
12649 isl_pw_multi_aff *pma;
12650
12651 if (!map || !mpa)
12652 goto error;
12653
12654 n = isl_map_dim(map, type);
12655 if (!isl_map_involves_dims(map, type, 0, n)) {
12656 isl_space *space;
12657 isl_multi_aff *ma;
12658
12659 space = isl_multi_pw_aff_get_space(mpa);
12660 isl_multi_pw_aff_free(mpa);
12661 ma = isl_multi_aff_zero(space);
12662 return isl_map_preimage_multi_aff(map, type, ma);
12663 }
12664
12665 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
12666 return isl_map_preimage_pw_multi_aff(map, type, pma);
12667error:
12668 isl_map_free(map);
12669 isl_multi_pw_aff_free(mpa);
12670 return NULL;
12671}
12672
12673/* Compute the preimage of "map" under the function represented by "mpa".
12674 * In other words, plug in "mpa" in the domain "map".
12675 * The result is a map that lives in the same space as "map",
12676 * except that domain space has been replaced by the domain space of "mpa".
12677 */
12678__isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
12679 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
12680{
12681 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
12682}
12683
12684/* Compute the preimage of "set" by the function represented by "mpa".
12685 * In other words, plug in "mpa" in "set".
12686 */
12687__isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
12688 __isl_take isl_multi_pw_aff *mpa)
12689{
12690 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);
12691}