Bug Summary

File:tools/polly/lib/External/isl/isl_map.c
Warning:line 12665, column 3
Value stored to 'o_ls' is never read

Annotated Source Code

1/*
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 * Copyright 2016 INRIA Paris
7 * Copyright 2016 Sven Verdoolaege
8 *
9 * Use of this software is governed by the MIT license
10 *
11 * Written by Sven Verdoolaege, K.U.Leuven, Departement
12 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
13 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
14 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
15 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
16 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
17 * B.P. 105 - 78153 Le Chesnay, France
18 * and Centre de Recherche Inria de Paris, 2 rue Simone Iff - Voie DQ12,
19 * CS 42112, 75589 Paris Cedex 12, France
20 */
21
22#include <string.h>
23#include <isl_ctx_private.h>
24#include <isl_map_private.h>
25#include <isl_blk.h>
26#include <isl/constraint.h>
27#include "isl_space_private.h"
28#include "isl_equalities.h"
29#include <isl_lp_private.h>
30#include <isl_seq.h>
31#include <isl/set.h>
32#include <isl/map.h>
33#include <isl_reordering.h>
34#include "isl_sample.h"
35#include <isl_sort.h>
36#include "isl_tab.h"
37#include <isl/vec.h>
38#include <isl_mat_private.h>
39#include <isl_vec_private.h>
40#include <isl_dim_map.h>
41#include <isl_local_space_private.h>
42#include <isl_aff_private.h>
43#include <isl_options_private.h>
44#include <isl_morph.h>
45#include <isl_val_private.h>
46#include <isl/deprecated/map_int.h>
47#include <isl/deprecated/set_int.h>
48
49#include <bset_to_bmap.c>
50#include <bset_from_bmap.c>
51#include <set_to_map.c>
52#include <set_from_map.c>
53
54static unsigned n(__isl_keep isl_space *dim, enum isl_dim_type type)
55{
56 switch (type) {
57 case isl_dim_param: return dim->nparam;
58 case isl_dim_in: return dim->n_in;
59 case isl_dim_out: return dim->n_out;
60 case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
61 default: return 0;
62 }
63}
64
65static unsigned pos(__isl_keep isl_space *dim, enum isl_dim_type type)
66{
67 switch (type) {
68 case isl_dim_param: return 1;
69 case isl_dim_in: return 1 + dim->nparam;
70 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
71 default: return 0;
72 }
73}
74
75unsigned isl_basic_map_dim(__isl_keep isl_basic_map *bmap,
76 enum isl_dim_type type)
77{
78 if (!bmap)
79 return 0;
80 switch (type) {
81 case isl_dim_cst: return 1;
82 case isl_dim_param:
83 case isl_dim_in:
84 case isl_dim_out: return isl_space_dim(bmap->dim, type);
85 case isl_dim_div: return bmap->n_div;
86 case isl_dim_all: return isl_basic_map_total_dim(bmap);
87 default: return 0;
88 }
89}
90
91/* Return the space of "map".
92 */
93__isl_keep isl_space *isl_map_peek_space(__isl_keep const isl_map *map)
94{
95 return map ? map->dim : NULL((void*)0);
96}
97
98unsigned isl_map_dim(__isl_keep isl_map *map, enum isl_dim_type type)
99{
100 return map ? n(map->dim, type) : 0;
101}
102
103unsigned isl_set_dim(__isl_keep isl_setisl_map *set, enum isl_dim_type type)
104{
105 return set ? n(set->dim, type) : 0;
106}
107
108unsigned isl_basic_map_offset(struct isl_basic_map *bmap,
109 enum isl_dim_type type)
110{
111 isl_space *space;
112
113 if (!bmap)
114 return 0;
115
116 space = bmap->dim;
117 switch (type) {
118 case isl_dim_cst: return 0;
119 case isl_dim_param: return 1;
120 case isl_dim_in: return 1 + space->nparam;
121 case isl_dim_out: return 1 + space->nparam + space->n_in;
122 case isl_dim_div: return 1 + space->nparam + space->n_in +
123 space->n_out;
124 default: return 0;
125 }
126}
127
128unsigned isl_basic_set_offset(struct isl_basic_setisl_basic_map *bset,
129 enum isl_dim_type type)
130{
131 return isl_basic_map_offset(bset, type);
132}
133
134static unsigned map_offset(struct isl_map *map, enum isl_dim_type type)
135{
136 return pos(map->dim, type);
137}
138
139unsigned isl_basic_set_dim(__isl_keep isl_basic_setisl_basic_map *bset,
140 enum isl_dim_type type)
141{
142 return isl_basic_map_dim(bset, type);
143}
144
145unsigned isl_basic_set_n_dim(__isl_keep isl_basic_setisl_basic_map *bset)
146{
147 return isl_basic_set_dim(bset, isl_dim_set);
148}
149
150unsigned isl_basic_set_n_param(__isl_keep isl_basic_setisl_basic_map *bset)
151{
152 return isl_basic_set_dim(bset, isl_dim_param);
153}
154
155unsigned isl_basic_set_total_dim(const struct isl_basic_setisl_basic_map *bset)
156{
157 if (!bset)
158 return 0;
159 return isl_space_dim(bset->dim, isl_dim_all) + bset->n_div;
160}
161
162unsigned isl_set_n_dim(__isl_keep isl_setisl_map *set)
163{
164 return isl_set_dim(set, isl_dim_set);
165}
166
167unsigned isl_set_n_param(__isl_keep isl_setisl_map *set)
168{
169 return isl_set_dim(set, isl_dim_param);
170}
171
172unsigned isl_basic_map_n_in(__isl_keep const isl_basic_map *bmap)
173{
174 return bmap ? bmap->dim->n_in : 0;
175}
176
177unsigned isl_basic_map_n_out(__isl_keep const isl_basic_map *bmap)
178{
179 return bmap ? bmap->dim->n_out : 0;
180}
181
182unsigned isl_basic_map_n_param(__isl_keep const isl_basic_map *bmap)
183{
184 return bmap ? bmap->dim->nparam : 0;
185}
186
187unsigned isl_basic_map_n_div(__isl_keep const isl_basic_map *bmap)
188{
189 return bmap ? bmap->n_div : 0;
190}
191
192unsigned isl_basic_map_total_dim(const struct isl_basic_map *bmap)
193{
194 return bmap ? isl_space_dim(bmap->dim, isl_dim_all) + bmap->n_div : 0;
195}
196
197unsigned isl_map_n_in(const struct isl_map *map)
198{
199 return map ? map->dim->n_in : 0;
200}
201
202unsigned isl_map_n_out(const struct isl_map *map)
203{
204 return map ? map->dim->n_out : 0;
205}
206
207unsigned isl_map_n_param(const struct isl_map *map)
208{
209 return map ? map->dim->nparam : 0;
210}
211
212/* Do "bmap1" and "bmap2" have the same parameters?
213 */
214static isl_bool isl_basic_map_has_equal_params(__isl_keep isl_basic_map *bmap1,
215 __isl_keep isl_basic_map *bmap2)
216{
217 isl_space *space1, *space2;
218
219 space1 = isl_basic_map_peek_space(bmap1);
220 space2 = isl_basic_map_peek_space(bmap2);
221 return isl_space_has_equal_params(space1, space2);
222}
223
224/* Do "map1" and "map2" have the same parameters?
225 */
226isl_bool isl_map_has_equal_params(__isl_keep isl_map *map1,
227 __isl_keep isl_map *map2)
228{
229 isl_space *space1, *space2;
230
231 space1 = isl_map_peek_space(map1);
232 space2 = isl_map_peek_space(map2);
233 return isl_space_has_equal_params(space1, space2);
234}
235
236/* Do "map" and "set" have the same parameters?
237 */
238static isl_bool isl_map_set_has_equal_params(__isl_keep isl_map *map,
239 __isl_keep isl_setisl_map *set)
240{
241 return isl_map_has_equal_params(map, set_to_map(set));
242}
243
244isl_bool isl_map_compatible_domain(__isl_keep isl_map *map,
245 __isl_keep isl_setisl_map *set)
246{
247 isl_bool m;
248 if (!map || !set)
249 return isl_bool_error;
250 m = isl_map_has_equal_params(map, set_to_map(set));
251 if (m < 0 || !m)
252 return m;
253 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
254 set->dim, isl_dim_set);
255}
256
257isl_bool isl_basic_map_compatible_domain(__isl_keep isl_basic_map *bmap,
258 __isl_keep isl_basic_setisl_basic_map *bset)
259{
260 isl_bool m;
261 if (!bmap || !bset)
262 return isl_bool_error;
263 m = isl_basic_map_has_equal_params(bmap, bset_to_bmap(bset));
264 if (m < 0 || !m)
265 return m;
266 return isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
267 bset->dim, isl_dim_set);
268}
269
270isl_bool isl_map_compatible_range(__isl_keep isl_map *map,
271 __isl_keep isl_setisl_map *set)
272{
273 isl_bool m;
274 if (!map || !set)
275 return isl_bool_error;
276 m = isl_map_has_equal_params(map, set_to_map(set));
277 if (m < 0 || !m)
278 return m;
279 return isl_space_tuple_is_equal(map->dim, isl_dim_out,
280 set->dim, isl_dim_set);
281}
282
283isl_bool isl_basic_map_compatible_range(__isl_keep isl_basic_map *bmap,
284 __isl_keep isl_basic_setisl_basic_map *bset)
285{
286 isl_bool m;
287 if (!bmap || !bset)
288 return isl_bool_error;
289 m = isl_basic_map_has_equal_params(bmap, bset_to_bmap(bset));
290 if (m < 0 || !m)
291 return m;
292 return isl_space_tuple_is_equal(bmap->dim, isl_dim_out,
293 bset->dim, isl_dim_set);
294}
295
296isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
297{
298 return bmap ? bmap->ctx : NULL((void*)0);
299}
300
301isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_setisl_basic_map *bset)
302{
303 return bset ? bset->ctx : NULL((void*)0);
304}
305
306isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
307{
308 return map ? map->ctx : NULL((void*)0);
309}
310
311isl_ctx *isl_set_get_ctx(__isl_keep isl_setisl_map *set)
312{
313 return set ? set->ctx : NULL((void*)0);
314}
315
316/* Return the space of "bmap".
317 */
318__isl_keep isl_space *isl_basic_map_peek_space(
319 __isl_keep const isl_basic_map *bmap)
320{
321 return bmap ? bmap->dim : NULL((void*)0);
322}
323
324/* Return the space of "bset".
325 */
326__isl_keep isl_space *isl_basic_set_peek_space(__isl_keep isl_basic_setisl_basic_map *bset)
327{
328 return isl_basic_map_peek_space(bset_to_bmap(bset));
329}
330
331__isl_give isl_space *isl_basic_map_get_space(__isl_keep isl_basic_map *bmap)
332{
333 return isl_space_copy(isl_basic_map_peek_space(bmap));
334}
335
336__isl_give isl_space *isl_basic_set_get_space(__isl_keep isl_basic_setisl_basic_map *bset)
337{
338 return isl_basic_map_get_space(bset_to_bmap(bset));
339}
340
341/* Extract the divs in "bmap" as a matrix.
342 */
343__isl_give isl_mat *isl_basic_map_get_divs(__isl_keep isl_basic_map *bmap)
344{
345 int i;
346 isl_ctx *ctx;
347 isl_mat *div;
348 unsigned total;
349 unsigned cols;
350
351 if (!bmap)
352 return NULL((void*)0);
353
354 ctx = isl_basic_map_get_ctx(bmap);
355 total = isl_space_dim(bmap->dim, isl_dim_all);
356 cols = 1 + 1 + total + bmap->n_div;
357 div = isl_mat_alloc(ctx, bmap->n_div, cols);
358 if (!div)
359 return NULL((void*)0);
360
361 for (i = 0; i < bmap->n_div; ++i)
362 isl_seq_cpy(div->row[i], bmap->div[i], cols);
363
364 return div;
365}
366
367/* Extract the divs in "bset" as a matrix.
368 */
369__isl_give isl_mat *isl_basic_set_get_divs(__isl_keep isl_basic_setisl_basic_map *bset)
370{
371 return isl_basic_map_get_divs(bset);
372}
373
374__isl_give isl_local_space *isl_basic_map_get_local_space(
375 __isl_keep isl_basic_map *bmap)
376{
377 isl_mat *div;
378
379 if (!bmap)
380 return NULL((void*)0);
381
382 div = isl_basic_map_get_divs(bmap);
383 return isl_local_space_alloc_div(isl_space_copy(bmap->dim), div);
384}
385
386__isl_give isl_local_space *isl_basic_set_get_local_space(
387 __isl_keep isl_basic_setisl_basic_map *bset)
388{
389 return isl_basic_map_get_local_space(bset);
390}
391
392/* For each known div d = floor(f/m), add the constraints
393 *
394 * f - m d >= 0
395 * -(f-(m-1)) + m d >= 0
396 *
397 * Do not finalize the result.
398 */
399static __isl_give isl_basic_map *add_known_div_constraints(
400 __isl_take isl_basic_map *bmap)
401{
402 int i;
403 unsigned n_div;
404
405 if (!bmap)
406 return NULL((void*)0);
407 n_div = isl_basic_map_dim(bmap, isl_dim_div);
408 if (n_div == 0)
409 return bmap;
410 bmap = isl_basic_map_cow(bmap);
411 bmap = isl_basic_map_extend_constraints(bmap, 0, 2 * n_div);
412 if (!bmap)
413 return NULL((void*)0);
414 for (i = 0; i < n_div; ++i) {
415 if (isl_int_is_zero(bmap->div[i][0])(isl_sioimath_sgn(*(bmap->div[i][0])) == 0))
416 continue;
417 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
418 return isl_basic_map_free(bmap);
419 }
420
421 return bmap;
422}
423
424__isl_give isl_basic_map *isl_basic_map_from_local_space(
425 __isl_take isl_local_space *ls)
426{
427 int i;
428 int n_div;
429 isl_basic_map *bmap;
430
431 if (!ls)
432 return NULL((void*)0);
433
434 n_div = isl_local_space_dim(ls, isl_dim_div);
435 bmap = isl_basic_map_alloc_space(isl_local_space_get_space(ls),
436 n_div, 0, 2 * n_div);
437
438 for (i = 0; i < n_div; ++i)
439 if (isl_basic_map_alloc_div(bmap) < 0)
440 goto error;
441
442 for (i = 0; i < n_div; ++i)
443 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
444 bmap = add_known_div_constraints(bmap);
445
446 isl_local_space_free(ls);
447 return bmap;
448error:
449 isl_local_space_free(ls);
450 isl_basic_map_free(bmap);
451 return NULL((void*)0);
452}
453
454__isl_give isl_basic_setisl_basic_map *isl_basic_set_from_local_space(
455 __isl_take isl_local_space *ls)
456{
457 return isl_basic_map_from_local_space(ls);
458}
459
460__isl_give isl_space *isl_map_get_space(__isl_keep isl_map *map)
461{
462 return isl_space_copy(isl_map_peek_space(map));
463}
464
465__isl_give isl_space *isl_set_get_space(__isl_keep isl_setisl_map *set)
466{
467 if (!set)
468 return NULL((void*)0);
469 return isl_space_copy(set->dim);
470}
471
472__isl_give isl_basic_map *isl_basic_map_set_tuple_name(
473 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
474{
475 bmap = isl_basic_map_cow(bmap);
476 if (!bmap)
477 return NULL((void*)0);
478 bmap->dim = isl_space_set_tuple_name(bmap->dim, type, s);
479 if (!bmap->dim)
480 goto error;
481 bmap = isl_basic_map_finalize(bmap);
482 return bmap;
483error:
484 isl_basic_map_free(bmap);
485 return NULL((void*)0);
486}
487
488__isl_give isl_basic_setisl_basic_map *isl_basic_set_set_tuple_name(
489 __isl_take isl_basic_setisl_basic_map *bset, const char *s)
490{
491 return isl_basic_map_set_tuple_name(bset, isl_dim_set, s);
492}
493
494const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
495 enum isl_dim_type type)
496{
497 return bmap ? isl_space_get_tuple_name(bmap->dim, type) : NULL((void*)0);
498}
499
500__isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
501 enum isl_dim_type type, const char *s)
502{
503 int i;
504
505 map = isl_map_cow(map);
506 if (!map)
507 return NULL((void*)0);
508
509 map->dim = isl_space_set_tuple_name(map->dim, type, s);
510 if (!map->dim)
511 goto error;
512
513 for (i = 0; i < map->n; ++i) {
514 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
515 if (!map->p[i])
516 goto error;
517 }
518
519 return map;
520error:
521 isl_map_free(map);
522 return NULL((void*)0);
523}
524
525/* Replace the identifier of the tuple of type "type" by "id".
526 */
527__isl_give isl_basic_map *isl_basic_map_set_tuple_id(
528 __isl_take isl_basic_map *bmap,
529 enum isl_dim_type type, __isl_take isl_id *id)
530{
531 bmap = isl_basic_map_cow(bmap);
532 if (!bmap)
533 goto error;
534 bmap->dim = isl_space_set_tuple_id(bmap->dim, type, id);
535 if (!bmap->dim)
536 return isl_basic_map_free(bmap);
537 bmap = isl_basic_map_finalize(bmap);
538 return bmap;
539error:
540 isl_id_free(id);
541 return NULL((void*)0);
542}
543
544/* Replace the identifier of the tuple by "id".
545 */
546__isl_give isl_basic_setisl_basic_map *isl_basic_set_set_tuple_id(
547 __isl_take isl_basic_setisl_basic_map *bset, __isl_take isl_id *id)
548{
549 return isl_basic_map_set_tuple_id(bset, isl_dim_set, id);
550}
551
552/* Does the input or output tuple have a name?
553 */
554isl_bool isl_map_has_tuple_name(__isl_keep isl_map *map, enum isl_dim_type type)
555{
556 return map ? isl_space_has_tuple_name(map->dim, type) : isl_bool_error;
557}
558
559const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
560 enum isl_dim_type type)
561{
562 return map ? isl_space_get_tuple_name(map->dim, type) : NULL((void*)0);
563}
564
565__isl_give isl_setisl_map *isl_set_set_tuple_name(__isl_take isl_setisl_map *set,
566 const char *s)
567{
568 return set_from_map(isl_map_set_tuple_name(set_to_map(set),
569 isl_dim_set, s));
570}
571
572__isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map,
573 enum isl_dim_type type, __isl_take isl_id *id)
574{
575 map = isl_map_cow(map);
576 if (!map)
577 goto error;
578
579 map->dim = isl_space_set_tuple_id(map->dim, type, id);
580
581 return isl_map_reset_space(map, isl_space_copy(map->dim));
582error:
583 isl_id_free(id);
584 return NULL((void*)0);
585}
586
587__isl_give isl_setisl_map *isl_set_set_tuple_id(__isl_take isl_setisl_map *set,
588 __isl_take isl_id *id)
589{
590 return isl_map_set_tuple_id(set, isl_dim_set, id);
591}
592
593__isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map,
594 enum isl_dim_type type)
595{
596 map = isl_map_cow(map);
597 if (!map)
598 return NULL((void*)0);
599
600 map->dim = isl_space_reset_tuple_id(map->dim, type);
601
602 return isl_map_reset_space(map, isl_space_copy(map->dim));
603}
604
605__isl_give isl_setisl_map *isl_set_reset_tuple_id(__isl_take isl_setisl_map *set)
606{
607 return isl_map_reset_tuple_id(set, isl_dim_set);
608}
609
610isl_bool isl_map_has_tuple_id(__isl_keep isl_map *map, enum isl_dim_type type)
611{
612 return map ? isl_space_has_tuple_id(map->dim, type) : isl_bool_error;
613}
614
615__isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map,
616 enum isl_dim_type type)
617{
618 return map ? isl_space_get_tuple_id(map->dim, type) : NULL((void*)0);
619}
620
621isl_bool isl_set_has_tuple_id(__isl_keep isl_setisl_map *set)
622{
623 return isl_map_has_tuple_id(set, isl_dim_set);
624}
625
626__isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_setisl_map *set)
627{
628 return isl_map_get_tuple_id(set, isl_dim_set);
629}
630
631/* Does the set tuple have a name?
632 */
633isl_bool isl_set_has_tuple_name(__isl_keep isl_setisl_map *set)
634{
635 if (!set)
636 return isl_bool_error;
637 return isl_space_has_tuple_name(set->dim, isl_dim_set);
638}
639
640
641const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_setisl_basic_map *bset)
642{
643 return bset ? isl_space_get_tuple_name(bset->dim, isl_dim_set) : NULL((void*)0);
644}
645
646const char *isl_set_get_tuple_name(__isl_keep isl_setisl_map *set)
647{
648 return set ? isl_space_get_tuple_name(set->dim, isl_dim_set) : NULL((void*)0);
649}
650
651const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
652 enum isl_dim_type type, unsigned pos)
653{
654 return bmap ? isl_space_get_dim_name(bmap->dim, type, pos) : NULL((void*)0);
655}
656
657const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_setisl_basic_map *bset,
658 enum isl_dim_type type, unsigned pos)
659{
660 return bset ? isl_space_get_dim_name(bset->dim, type, pos) : NULL((void*)0);
661}
662
663/* Does the given dimension have a name?
664 */
665isl_bool isl_map_has_dim_name(__isl_keep isl_map *map,
666 enum isl_dim_type type, unsigned pos)
667{
668 if (!map)
669 return isl_bool_error;
670 return isl_space_has_dim_name(map->dim, type, pos);
671}
672
673const char *isl_map_get_dim_name(__isl_keep isl_map *map,
674 enum isl_dim_type type, unsigned pos)
675{
676 return map ? isl_space_get_dim_name(map->dim, type, pos) : NULL((void*)0);
677}
678
679const char *isl_set_get_dim_name(__isl_keep isl_setisl_map *set,
680 enum isl_dim_type type, unsigned pos)
681{
682 return set ? isl_space_get_dim_name(set->dim, type, pos) : NULL((void*)0);
683}
684
685/* Does the given dimension have a name?
686 */
687isl_bool isl_set_has_dim_name(__isl_keep isl_setisl_map *set,
688 enum isl_dim_type type, unsigned pos)
689{
690 if (!set)
691 return isl_bool_error;
692 return isl_space_has_dim_name(set->dim, type, pos);
693}
694
695__isl_give isl_basic_map *isl_basic_map_set_dim_name(
696 __isl_take isl_basic_map *bmap,
697 enum isl_dim_type type, unsigned pos, const char *s)
698{
699 bmap = isl_basic_map_cow(bmap);
700 if (!bmap)
701 return NULL((void*)0);
702 bmap->dim = isl_space_set_dim_name(bmap->dim, type, pos, s);
703 if (!bmap->dim)
704 goto error;
705 return isl_basic_map_finalize(bmap);
706error:
707 isl_basic_map_free(bmap);
708 return NULL((void*)0);
709}
710
711__isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
712 enum isl_dim_type type, unsigned pos, const char *s)
713{
714 int i;
715
716 map = isl_map_cow(map);
717 if (!map)
718 return NULL((void*)0);
719
720 map->dim = isl_space_set_dim_name(map->dim, type, pos, s);
721 if (!map->dim)
722 goto error;
723
724 for (i = 0; i < map->n; ++i) {
725 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
726 if (!map->p[i])
727 goto error;
728 }
729
730 return map;
731error:
732 isl_map_free(map);
733 return NULL((void*)0);
734}
735
736__isl_give isl_basic_setisl_basic_map *isl_basic_set_set_dim_name(
737 __isl_take isl_basic_setisl_basic_map *bset,
738 enum isl_dim_type type, unsigned pos, const char *s)
739{
740 return bset_from_bmap(isl_basic_map_set_dim_name(bset_to_bmap(bset),
741 type, pos, s));
742}
743
744__isl_give isl_setisl_map *isl_set_set_dim_name(__isl_take isl_setisl_map *set,
745 enum isl_dim_type type, unsigned pos, const char *s)
746{
747 return set_from_map(isl_map_set_dim_name(set_to_map(set),
748 type, pos, s));
749}
750
751isl_bool isl_basic_map_has_dim_id(__isl_keep isl_basic_map *bmap,
752 enum isl_dim_type type, unsigned pos)
753{
754 if (!bmap)
755 return isl_bool_error;
756 return isl_space_has_dim_id(bmap->dim, type, pos);
757}
758
759__isl_give isl_id *isl_basic_set_get_dim_id(__isl_keep isl_basic_setisl_basic_map *bset,
760 enum isl_dim_type type, unsigned pos)
761{
762 return bset ? isl_space_get_dim_id(bset->dim, type, pos) : NULL((void*)0);
763}
764
765isl_bool isl_map_has_dim_id(__isl_keep isl_map *map,
766 enum isl_dim_type type, unsigned pos)
767{
768 return map ? isl_space_has_dim_id(map->dim, type, pos) : isl_bool_error;
769}
770
771__isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
772 enum isl_dim_type type, unsigned pos)
773{
774 return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL((void*)0);
775}
776
777isl_bool isl_set_has_dim_id(__isl_keep isl_setisl_map *set,
778 enum isl_dim_type type, unsigned pos)
779{
780 return isl_map_has_dim_id(set, type, pos);
781}
782
783__isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_setisl_map *set,
784 enum isl_dim_type type, unsigned pos)
785{
786 return isl_map_get_dim_id(set, type, pos);
787}
788
789__isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
790 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
791{
792 map = isl_map_cow(map);
793 if (!map)
794 goto error;
795
796 map->dim = isl_space_set_dim_id(map->dim, type, pos, id);
797
798 return isl_map_reset_space(map, isl_space_copy(map->dim));
799error:
800 isl_id_free(id);
801 return NULL((void*)0);
802}
803
804__isl_give isl_setisl_map *isl_set_set_dim_id(__isl_take isl_setisl_map *set,
805 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
806{
807 return isl_map_set_dim_id(set, type, pos, id);
808}
809
810int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
811 __isl_keep isl_id *id)
812{
813 if (!map)
814 return -1;
815 return isl_space_find_dim_by_id(map->dim, type, id);
816}
817
818int isl_set_find_dim_by_id(__isl_keep isl_setisl_map *set, enum isl_dim_type type,
819 __isl_keep isl_id *id)
820{
821 return isl_map_find_dim_by_id(set, type, id);
822}
823
824/* Return the position of the dimension of the given type and name
825 * in "bmap".
826 * Return -1 if no such dimension can be found.
827 */
828int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map *bmap,
829 enum isl_dim_type type, const char *name)
830{
831 if (!bmap)
832 return -1;
833 return isl_space_find_dim_by_name(bmap->dim, type, name);
834}
835
836int isl_map_find_dim_by_name(__isl_keep isl_map *map, enum isl_dim_type type,
837 const char *name)
838{
839 if (!map)
840 return -1;
841 return isl_space_find_dim_by_name(map->dim, type, name);
842}
843
844int isl_set_find_dim_by_name(__isl_keep isl_setisl_map *set, enum isl_dim_type type,
845 const char *name)
846{
847 return isl_map_find_dim_by_name(set, type, name);
848}
849
850/* Check whether equality i of bset is a pure stride constraint
851 * on a single dimension, i.e., of the form
852 *
853 * v = k e
854 *
855 * with k a constant and e an existentially quantified variable.
856 */
857isl_bool isl_basic_set_eq_is_stride(__isl_keep isl_basic_setisl_basic_map *bset, int i)
858{
859 unsigned nparam;
860 unsigned d;
861 unsigned n_div;
862 int pos1;
863 int pos2;
864
865 if (!bset)
866 return isl_bool_error;
867
868 if (!isl_int_is_zero(bset->eq[i][0])(isl_sioimath_sgn(*(bset->eq[i][0])) == 0))
869 return isl_bool_false;
870
871 nparam = isl_basic_set_dim(bset, isl_dim_param);
872 d = isl_basic_set_dim(bset, isl_dim_set);
873 n_div = isl_basic_set_dim(bset, isl_dim_div);
874
875 if (isl_seq_first_non_zero(bset->eq[i] + 1, nparam) != -1)
876 return isl_bool_false;
877 pos1 = isl_seq_first_non_zero(bset->eq[i] + 1 + nparam, d);
878 if (pos1 == -1)
879 return isl_bool_false;
880 if (isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + pos1 + 1,
881 d - pos1 - 1) != -1)
882 return isl_bool_false;
883
884 pos2 = isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + d, n_div);
885 if (pos2 == -1)
886 return isl_bool_false;
887 if (isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + d + pos2 + 1,
888 n_div - pos2 - 1) != -1)
889 return isl_bool_false;
890 if (!isl_int_is_one(bset->eq[i][1 + nparam + pos1])(isl_sioimath_cmp_si(*(bset->eq[i][1 + nparam + pos1]), 1)
== 0)
&&
891 !isl_int_is_negone(bset->eq[i][1 + nparam + pos1])(isl_sioimath_cmp_si(*(bset->eq[i][1 + nparam + pos1]), -1
) == 0)
)
892 return isl_bool_false;
893
894 return isl_bool_true;
895}
896
897/* Reset the user pointer on all identifiers of parameters and tuples
898 * of the space of "map".
899 */
900__isl_give isl_map *isl_map_reset_user(__isl_take isl_map *map)
901{
902 isl_space *space;
903
904 space = isl_map_get_space(map);
905 space = isl_space_reset_user(space);
906 map = isl_map_reset_space(map, space);
907
908 return map;
909}
910
911/* Reset the user pointer on all identifiers of parameters and tuples
912 * of the space of "set".
913 */
914__isl_give isl_setisl_map *isl_set_reset_user(__isl_take isl_setisl_map *set)
915{
916 return isl_map_reset_user(set);
917}
918
919isl_bool isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
920{
921 if (!bmap)
922 return isl_bool_error;
923 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)(!!(((bmap)->flags) & ((1 << 4))));
924}
925
926/* Has "map" been marked as a rational map?
927 * In particular, have all basic maps in "map" been marked this way?
928 * An empty map is not considered to be rational.
929 * Maps where only some of the basic maps are marked rational
930 * are not allowed.
931 */
932isl_bool isl_map_is_rational(__isl_keep isl_map *map)
933{
934 int i;
935 isl_bool rational;
936
937 if (!map)
938 return isl_bool_error;
939 if (map->n == 0)
940 return isl_bool_false;
941 rational = isl_basic_map_is_rational(map->p[0]);
942 if (rational < 0)
943 return rational;
944 for (i = 1; i < map->n; ++i) {
945 isl_bool rational_i;
946
947 rational_i = isl_basic_map_is_rational(map->p[i]);
948 if (rational_i < 0)
949 return rational_i;
950 if (rational != rational_i)
951 isl_die(isl_map_get_ctx(map), isl_error_unsupported,do { isl_handle_error(isl_map_get_ctx(map), isl_error_unsupported
, "mixed rational and integer basic maps " "not supported", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 953); return isl_bool_error; } while (0)
952 "mixed rational and integer basic maps "do { isl_handle_error(isl_map_get_ctx(map), isl_error_unsupported
, "mixed rational and integer basic maps " "not supported", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 953); return isl_bool_error; } while (0)
953 "not supported", return isl_bool_error)do { isl_handle_error(isl_map_get_ctx(map), isl_error_unsupported
, "mixed rational and integer basic maps " "not supported", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 953); return isl_bool_error; } while (0)
;
954 }
955
956 return rational;
957}
958
959/* Has "set" been marked as a rational set?
960 * In particular, have all basic set in "set" been marked this way?
961 * An empty set is not considered to be rational.
962 * Sets where only some of the basic sets are marked rational
963 * are not allowed.
964 */
965isl_bool isl_set_is_rational(__isl_keep isl_setisl_map *set)
966{
967 return isl_map_is_rational(set);
968}
969
970int isl_basic_set_is_rational(__isl_keep isl_basic_setisl_basic_map *bset)
971{
972 return isl_basic_map_is_rational(bset);
973}
974
975/* Does "bmap" contain any rational points?
976 *
977 * If "bmap" has an equality for each dimension, equating the dimension
978 * to an integer constant, then it has no rational points, even if it
979 * is marked as rational.
980 */
981isl_bool isl_basic_map_has_rational(__isl_keep isl_basic_map *bmap)
982{
983 isl_bool has_rational = isl_bool_true;
984 unsigned total;
985
986 if (!bmap)
987 return isl_bool_error;
988 if (isl_basic_map_plain_is_empty(bmap))
989 return isl_bool_false;
990 if (!isl_basic_map_is_rational(bmap))
991 return isl_bool_false;
992 bmap = isl_basic_map_copy(bmap);
993 bmap = isl_basic_map_implicit_equalities(bmap);
994 if (!bmap)
995 return isl_bool_error;
996 total = isl_basic_map_total_dim(bmap);
997 if (bmap->n_eq == total) {
998 int i, j;
999 for (i = 0; i < bmap->n_eq; ++i) {
1000 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
1001 if (j < 0)
1002 break;
1003 if (!isl_int_is_one(bmap->eq[i][1 + j])(isl_sioimath_cmp_si(*(bmap->eq[i][1 + j]), 1) == 0) &&
1004 !isl_int_is_negone(bmap->eq[i][1 + j])(isl_sioimath_cmp_si(*(bmap->eq[i][1 + j]), -1) == 0))
1005 break;
1006 j = isl_seq_first_non_zero(bmap->eq[i] + 1 + j + 1,
1007 total - j - 1);
1008 if (j >= 0)
1009 break;
1010 }
1011 if (i == bmap->n_eq)
1012 has_rational = isl_bool_false;
1013 }
1014 isl_basic_map_free(bmap);
1015
1016 return has_rational;
1017}
1018
1019/* Does "map" contain any rational points?
1020 */
1021isl_bool isl_map_has_rational(__isl_keep isl_map *map)
1022{
1023 int i;
1024 isl_bool has_rational;
1025
1026 if (!map)
1027 return isl_bool_error;
1028 for (i = 0; i < map->n; ++i) {
1029 has_rational = isl_basic_map_has_rational(map->p[i]);
1030 if (has_rational < 0 || has_rational)
1031 return has_rational;
1032 }
1033 return isl_bool_false;
1034}
1035
1036/* Does "set" contain any rational points?
1037 */
1038isl_bool isl_set_has_rational(__isl_keep isl_setisl_map *set)
1039{
1040 return isl_map_has_rational(set);
1041}
1042
1043/* Is this basic set a parameter domain?
1044 */
1045isl_bool isl_basic_set_is_params(__isl_keep isl_basic_setisl_basic_map *bset)
1046{
1047 if (!bset)
1048 return isl_bool_error;
1049 return isl_space_is_params(bset->dim);
1050}
1051
1052/* Is this set a parameter domain?
1053 */
1054isl_bool isl_set_is_params(__isl_keep isl_setisl_map *set)
1055{
1056 if (!set)
1057 return isl_bool_error;
1058 return isl_space_is_params(set->dim);
1059}
1060
1061/* Is this map actually a parameter domain?
1062 * Users should never call this function. Outside of isl,
1063 * a map can never be a parameter domain.
1064 */
1065isl_bool isl_map_is_params(__isl_keep isl_map *map)
1066{
1067 if (!map)
1068 return isl_bool_error;
1069 return isl_space_is_params(map->dim);
1070}
1071
1072static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
1073 struct isl_basic_map *bmap, unsigned extra,
1074 unsigned n_eq, unsigned n_ineq)
1075{
1076 int i;
1077 size_t row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + extra;
1078
1079 bmap->ctx = ctx;
1080 isl_ctx_ref(ctx);
1081
1082 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
1083 if (isl_blk_is_error(bmap->block))
1084 goto error;
1085
1086 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq)((isl_int * *)isl_malloc_or_die(ctx, (n_ineq + n_eq)*sizeof(isl_int
*)))
;
1087 if ((n_ineq + n_eq) && !bmap->ineq)
1088 goto error;
1089
1090 if (extra == 0) {
1091 bmap->block2 = isl_blk_empty();
1092 bmap->div = NULL((void*)0);
1093 } else {
1094 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
1095 if (isl_blk_is_error(bmap->block2))
1096 goto error;
1097
1098 bmap->div = isl_alloc_array(ctx, isl_int *, extra)((isl_int * *)isl_malloc_or_die(ctx, (extra)*sizeof(isl_int *
)))
;
1099 if (!bmap->div)
1100 goto error;
1101 }
1102
1103 for (i = 0; i < n_ineq + n_eq; ++i)
1104 bmap->ineq[i] = bmap->block.data + i * row_size;
1105
1106 for (i = 0; i < extra; ++i)
1107 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
1108
1109 bmap->ref = 1;
1110 bmap->flags = 0;
1111 bmap->c_size = n_eq + n_ineq;
1112 bmap->eq = bmap->ineq + n_ineq;
1113 bmap->extra = extra;
1114 bmap->n_eq = 0;
1115 bmap->n_ineq = 0;
1116 bmap->n_div = 0;
1117 bmap->sample = NULL((void*)0);
1118
1119 return bmap;
1120error:
1121 isl_basic_map_free(bmap);
1122 return NULL((void*)0);
1123}
1124
1125struct isl_basic_setisl_basic_map *isl_basic_set_alloc(struct isl_ctx *ctx,
1126 unsigned nparam, unsigned dim, unsigned extra,
1127 unsigned n_eq, unsigned n_ineq)
1128{
1129 struct isl_basic_map *bmap;
1130 isl_space *space;
1131
1132 space = isl_space_set_alloc(ctx, nparam, dim);
1133 if (!space)
1134 return NULL((void*)0);
1135
1136 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1137 return bset_from_bmap(bmap);
1138}
1139
1140struct isl_basic_setisl_basic_map *isl_basic_set_alloc_space(__isl_take isl_space *dim,
1141 unsigned extra, unsigned n_eq, unsigned n_ineq)
1142{
1143 struct isl_basic_map *bmap;
1144 if (!dim)
1145 return NULL((void*)0);
1146 isl_assert(dim->ctx, dim->n_in == 0, goto error)do { if (dim->n_in == 0) break; do { isl_handle_error(dim->
ctx, isl_error_unknown, "Assertion \"" "dim->n_in == 0" "\" failed"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 1146); goto error; } while (0); } while (0)
;
1147 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1148 return bset_from_bmap(bmap);
1149error:
1150 isl_space_free(dim);
1151 return NULL((void*)0);
1152}
1153
1154struct isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *dim,
1155 unsigned extra, unsigned n_eq, unsigned n_ineq)
1156{
1157 struct isl_basic_map *bmap;
1158
1159 if (!dim)
1160 return NULL((void*)0);
1161 bmap = isl_calloc_type(dim->ctx, struct isl_basic_map)((struct isl_basic_map *)isl_calloc_or_die(dim->ctx, 1, sizeof
(struct isl_basic_map)))
;
1162 if (!bmap)
1163 goto error;
1164 bmap->dim = dim;
1165
1166 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
1167error:
1168 isl_space_free(dim);
1169 return NULL((void*)0);
1170}
1171
1172struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
1173 unsigned nparam, unsigned in, unsigned out, unsigned extra,
1174 unsigned n_eq, unsigned n_ineq)
1175{
1176 struct isl_basic_map *bmap;
1177 isl_space *dim;
1178
1179 dim = isl_space_alloc(ctx, nparam, in, out);
1180 if (!dim)
1181 return NULL((void*)0);
1182
1183 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1184 return bmap;
1185}
1186
1187static void dup_constraints(
1188 struct isl_basic_map *dst, struct isl_basic_map *src)
1189{
1190 int i;
1191 unsigned total = isl_basic_map_total_dim(src);
1192
1193 for (i = 0; i < src->n_eq; ++i) {
1194 int j = isl_basic_map_alloc_equality(dst);
1195 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
1196 }
1197
1198 for (i = 0; i < src->n_ineq; ++i) {
1199 int j = isl_basic_map_alloc_inequality(dst);
1200 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
1201 }
1202
1203 for (i = 0; i < src->n_div; ++i) {
1204 int j = isl_basic_map_alloc_div(dst);
1205 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
1206 }
1207 ISL_F_SET(dst, ISL_BASIC_SET_FINAL)(((dst)->flags) |= ((1 << 0)));
1208}
1209
1210struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
1211{
1212 struct isl_basic_map *dup;
1213
1214 if (!bmap)
1215 return NULL((void*)0);
1216 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
1217 bmap->n_div, bmap->n_eq, bmap->n_ineq);
1218 if (!dup)
1219 return NULL((void*)0);
1220 dup_constraints(dup, bmap);
1221 dup->flags = bmap->flags;
1222 dup->sample = isl_vec_copy(bmap->sample);
1223 return dup;
1224}
1225
1226struct isl_basic_setisl_basic_map *isl_basic_set_dup(struct isl_basic_setisl_basic_map *bset)
1227{
1228 struct isl_basic_map *dup;
1229
1230 dup = isl_basic_map_dup(bset_to_bmap(bset));
1231 return bset_from_bmap(dup);
1232}
1233
1234struct isl_basic_setisl_basic_map *isl_basic_set_copy(struct isl_basic_setisl_basic_map *bset)
1235{
1236 if (!bset)
1237 return NULL((void*)0);
1238
1239 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)(!!(((bset)->flags) & ((1 << 0))))) {
1240 bset->ref++;
1241 return bset;
1242 }
1243 return isl_basic_set_dup(bset);
1244}
1245
1246struct isl_setisl_map *isl_set_copy(struct isl_setisl_map *set)
1247{
1248 if (!set)
1249 return NULL((void*)0);
1250
1251 set->ref++;
1252 return set;
1253}
1254
1255struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
1256{
1257 if (!bmap)
1258 return NULL((void*)0);
1259
1260 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)(!!(((bmap)->flags) & ((1 << 0))))) {
1261 bmap->ref++;
1262 return bmap;
1263 }
1264 bmap = isl_basic_map_dup(bmap);
1265 if (bmap)
1266 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL)(((bmap)->flags) |= ((1 << 0)));
1267 return bmap;
1268}
1269
1270struct isl_map *isl_map_copy(struct isl_map *map)
1271{
1272 if (!map)
1273 return NULL((void*)0);
1274
1275 map->ref++;
1276 return map;
1277}
1278
1279__isl_null isl_basic_map *isl_basic_map_free(__isl_take isl_basic_map *bmap)
1280{
1281 if (!bmap)
1282 return NULL((void*)0);
1283
1284 if (--bmap->ref > 0)
1285 return NULL((void*)0);
1286
1287 isl_ctx_deref(bmap->ctx);
1288 free(bmap->div);
1289 isl_blk_free(bmap->ctx, bmap->block2);
1290 free(bmap->ineq);
1291 isl_blk_free(bmap->ctx, bmap->block);
1292 isl_vec_free(bmap->sample);
1293 isl_space_free(bmap->dim);
1294 free(bmap);
1295
1296 return NULL((void*)0);
1297}
1298
1299__isl_null isl_basic_setisl_basic_map *isl_basic_set_free(__isl_take isl_basic_setisl_basic_map *bset)
1300{
1301 return isl_basic_map_free(bset_to_bmap(bset));
1302}
1303
1304static int room_for_con(struct isl_basic_map *bmap, unsigned n)
1305{
1306 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
1307}
1308
1309/* Check that "map" has only named parameters, reporting an error
1310 * if it does not.
1311 */
1312isl_stat isl_map_check_named_params(__isl_keep isl_map *map)
1313{
1314 return isl_space_check_named_params(isl_map_peek_space(map));
1315}
1316
1317/* Check that "bmap1" and "bmap2" have the same parameters,
1318 * reporting an error if they do not.
1319 */
1320static isl_stat isl_basic_map_check_equal_params(
1321 __isl_keep isl_basic_map *bmap1, __isl_keep isl_basic_map *bmap2)
1322{
1323 isl_bool match;
1324
1325 match = isl_basic_map_has_equal_params(bmap1, bmap2);
1326 if (match < 0)
1327 return isl_stat_error;
1328 if (!match)
1329 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,do { isl_handle_error(isl_basic_map_get_ctx(bmap1), isl_error_invalid
, "parameters don't match", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 1330); return isl_stat_error; } while (0)
1330 "parameters don't match", return isl_stat_error)do { isl_handle_error(isl_basic_map_get_ctx(bmap1), isl_error_invalid
, "parameters don't match", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 1330); return isl_stat_error; } while (0)
;
1331 return isl_stat_ok;
1332}
1333
1334__isl_give isl_map *isl_map_align_params_map_map_and(
1335 __isl_take isl_map *map1, __isl_take isl_map *map2,
1336 __isl_give isl_map *(*fn)(__isl_take isl_map *map1,
1337 __isl_take isl_map *map2))
1338{
1339 if (!map1 || !map2)
1340 goto error;
1341 if (isl_map_has_equal_params(map1, map2))
1342 return fn(map1, map2);
1343 if (isl_map_check_named_params(map1) < 0)
1344 goto error;
1345 if (isl_map_check_named_params(map2) < 0)
1346 goto error;
1347 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1348 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1349 return fn(map1, map2);
1350error:
1351 isl_map_free(map1);
1352 isl_map_free(map2);
1353 return NULL((void*)0);
1354}
1355
1356isl_bool isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1,
1357 __isl_keep isl_map *map2,
1358 isl_bool (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
1359{
1360 isl_bool r;
1361
1362 if (!map1 || !map2)
1363 return isl_bool_error;
1364 if (isl_map_has_equal_params(map1, map2))
1365 return fn(map1, map2);
1366 if (isl_map_check_named_params(map1) < 0)
1367 return isl_bool_error;
1368 if (isl_map_check_named_params(map2) < 0)
1369 return isl_bool_error;
1370 map1 = isl_map_copy(map1);
1371 map2 = isl_map_copy(map2);
1372 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1373 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1374 r = fn(map1, map2);
1375 isl_map_free(map1);
1376 isl_map_free(map2);
1377 return r;
1378}
1379
1380int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
1381{
1382 struct isl_ctx *ctx;
1383 if (!bmap)
1384 return -1;
1385 ctx = bmap->ctx;
1386 isl_assert(ctx, room_for_con(bmap, 1), return -1)do { if (room_for_con(bmap, 1)) break; do { isl_handle_error(
ctx, isl_error_unknown, "Assertion \"" "room_for_con(bmap, 1)"
"\" failed", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 1386); return -1; } while (0); } while (0)
;
1387 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,do { if ((bmap->eq - bmap->ineq) + bmap->n_eq <= bmap
->c_size) break; do { isl_handle_error(ctx, isl_error_unknown
, "Assertion \"" "(bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size"
"\" failed", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 1388); return -1; } while (0); } while (0)
1388 return -1)do { if ((bmap->eq - bmap->ineq) + bmap->n_eq <= bmap
->c_size) break; do { isl_handle_error(ctx, isl_error_unknown
, "Assertion \"" "(bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size"
"\" failed", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 1388); return -1; } while (0); } while (0)
;
1389 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED)(((bmap)->flags) &= ~((1 << 5)));
1390 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT)(((bmap)->flags) &= ~((1 << 3)));
1391 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT)(((bmap)->flags) &= ~((1 << 2)));
1392 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES)(((bmap)->flags) &= ~((1 << 7)));
1393 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS)(((bmap)->flags) &= ~((1 << 6)));
1394 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
1395 isl_int *t;
1396 int j = isl_basic_map_alloc_inequality(bmap);
1397 if (j < 0)
1398 return -1;
1399 t = bmap->ineq[j];
1400 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
1401 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1402 bmap->eq[-1] = t;
1403 bmap->n_eq++;
1404 bmap->n_ineq--;
1405 bmap->eq--;
1406 return 0;
1407 }
1408 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
1409 bmap->extra - bmap->n_div);
1410 return bmap->n_eq++;
1411}
1412
1413int isl_basic_set_alloc_equality(struct isl_basic_setisl_basic_map *bset)
1414{
1415 return isl_basic_map_alloc_equality(bset_to_bmap(bset));
1416}
1417
1418int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
1419{
1420 if (!bmap)
1421 return -1;
1422 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1)do { if (n <= bmap->n_eq) break; do { isl_handle_error(
bmap->ctx, isl_error_unknown, "Assertion \"" "n <= bmap->n_eq"
"\" failed", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 1422); return -1; } while (0); } while (0)
;
1423 bmap->n_eq -= n;
1424 return 0;
1425}
1426
1427int isl_basic_set_free_equality(struct isl_basic_setisl_basic_map *bset, unsigned n)
1428{
1429 return isl_basic_map_free_equality(bset_to_bmap(bset), n);
1430}
1431
1432int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
1433{
1434 isl_int *t;
1435 if (!bmap)
1436 return -1;
1437 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1)do { if (pos < bmap->n_eq) break; do { isl_handle_error
(bmap->ctx, isl_error_unknown, "Assertion \"" "pos < bmap->n_eq"
"\" failed", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 1437); return -1; } while (0); } while (0)
;
1438
1439 if (pos != bmap->n_eq - 1) {
1440 t = bmap->eq[pos];
1441 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
1442 bmap->eq[bmap->n_eq - 1] = t;
1443 }
1444 bmap->n_eq--;
1445 return 0;
1446}
1447
1448/* Turn inequality "pos" of "bmap" into an equality.
1449 *
1450 * In particular, we move the inequality in front of the equalities
1451 * and move the last inequality in the position of the moved inequality.
1452 * Note that isl_tab_make_equalities_explicit depends on this particular
1453 * change in the ordering of the constraints.
1454 */
1455void isl_basic_map_inequality_to_equality(
1456 struct isl_basic_map *bmap, unsigned pos)
1457{
1458 isl_int *t;
1459
1460 t = bmap->ineq[pos];
1461 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1462 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1463 bmap->eq[-1] = t;
1464 bmap->n_eq++;
1465 bmap->n_ineq--;
1466 bmap->eq--;
1467 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT)(((bmap)->flags) &= ~((1 << 3)));
1468 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED)(((bmap)->flags) &= ~((1 << 5)));
1469 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS)(((bmap)->flags) &= ~((1 << 6)));
1470 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES)(((bmap)->flags) &= ~((1 << 7)));
1471}
1472
1473static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
1474{
1475 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1476}
1477
1478int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
1479{
1480 struct isl_ctx *ctx;
1481 if (!bmap)
1482 return -1;
1483 ctx = bmap->ctx;
1484 isl_assert(ctx, room_for_ineq(bmap, 1), return -1)do { if (room_for_ineq(bmap, 1)) break; do { isl_handle_error
(ctx, isl_error_unknown, "Assertion \"" "room_for_ineq(bmap, 1)"
"\" failed", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 1484); return -1; } while (0); } while (0)
;
1485 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT)(((bmap)->flags) &= ~((1 << 2)));
1486 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT)(((bmap)->flags) &= ~((1 << 3)));
1487 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED)(((bmap)->flags) &= ~((1 << 5)));
1488 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES)(((bmap)->flags) &= ~((1 << 7)));
1489 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
1490 1 + isl_basic_map_total_dim(bmap),
1491 bmap->extra - bmap->n_div);
1492 return bmap->n_ineq++;
1493}
1494
1495int isl_basic_set_alloc_inequality(struct isl_basic_setisl_basic_map *bset)
1496{
1497 return isl_basic_map_alloc_inequality(bset_to_bmap(bset));
1498}
1499
1500int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
1501{
1502 if (!bmap)
1503 return -1;
1504 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1)do { if (n <= bmap->n_ineq) break; do { isl_handle_error
(bmap->ctx, isl_error_unknown, "Assertion \"" "n <= bmap->n_ineq"
"\" failed", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 1504); return -1; } while (0); } while (0)
;
1505 bmap->n_ineq -= n;
1506 return 0;
1507}
1508
1509int isl_basic_set_free_inequality(struct isl_basic_setisl_basic_map *bset, unsigned n)
1510{
1511 return isl_basic_map_free_inequality(bset_to_bmap(bset), n);
1512}
1513
1514int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
1515{
1516 isl_int *t;
1517 if (!bmap)
1518 return -1;
1519 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1)do { if (pos < bmap->n_ineq) break; do { isl_handle_error
(bmap->ctx, isl_error_unknown, "Assertion \"" "pos < bmap->n_ineq"
"\" failed", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 1519); return -1; } while (0); } while (0)
;
1520
1521 if (pos != bmap->n_ineq - 1) {
1522 t = bmap->ineq[pos];
1523 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1524 bmap->ineq[bmap->n_ineq - 1] = t;
1525 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED)(((bmap)->flags) &= ~((1 << 5)));
1526 }
1527 bmap->n_ineq--;
1528 return 0;
1529}
1530
1531int isl_basic_set_drop_inequality(struct isl_basic_setisl_basic_map *bset, unsigned pos)
1532{
1533 return isl_basic_map_drop_inequality(bset_to_bmap(bset), pos);
1534}
1535
1536__isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1537 isl_int *eq)
1538{
1539 int k;
1540
1541 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1542 if (!bmap)
1543 return NULL((void*)0);
1544 k = isl_basic_map_alloc_equality(bmap);
1545 if (k < 0)
1546 goto error;
1547 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
1548 return bmap;
1549error:
1550 isl_basic_map_free(bmap);
1551 return NULL((void*)0);
1552}
1553
1554__isl_give isl_basic_setisl_basic_map *isl_basic_set_add_eq(__isl_take isl_basic_setisl_basic_map *bset,
1555 isl_int *eq)
1556{
1557 return bset_from_bmap(isl_basic_map_add_eq(bset_to_bmap(bset), eq));
1558}
1559
1560__isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1561 isl_int *ineq)
1562{
1563 int k;
1564
1565 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1566 if (!bmap)
1567 return NULL((void*)0);
1568 k = isl_basic_map_alloc_inequality(bmap);
1569 if (k < 0)
1570 goto error;
1571 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
1572 return bmap;
1573error:
1574 isl_basic_map_free(bmap);
1575 return NULL((void*)0);
1576}
1577
1578__isl_give isl_basic_setisl_basic_map *isl_basic_set_add_ineq(__isl_take isl_basic_setisl_basic_map *bset,
1579 isl_int *ineq)
1580{
1581 return bset_from_bmap(isl_basic_map_add_ineq(bset_to_bmap(bset), ineq));
1582}
1583
1584int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
1585{
1586 if (!bmap)
1587 return -1;
1588 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1)do { if (bmap->n_div < bmap->extra) break; do { isl_handle_error
(bmap->ctx, isl_error_unknown, "Assertion \"" "bmap->n_div < bmap->extra"
"\" failed", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 1588); return -1; } while (0); } while (0)
;
1589 isl_seq_clr(bmap->div[bmap->n_div] +
1590 1 + 1 + isl_basic_map_total_dim(bmap),
1591 bmap->extra - bmap->n_div);
1592 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS)(((bmap)->flags) &= ~((1 << 6)));
1593 return bmap->n_div++;
1594}
1595
1596int isl_basic_set_alloc_div(struct isl_basic_setisl_basic_map *bset)
1597{
1598 return isl_basic_map_alloc_div(bset_to_bmap(bset));
1599}
1600
1601/* Check that there are "n" dimensions of type "type" starting at "first"
1602 * in "bmap".
1603 */
1604static isl_stat isl_basic_map_check_range(__isl_keep isl_basic_map *bmap,
1605 enum isl_dim_type type, unsigned first, unsigned n)
1606{
1607 unsigned dim;
1608
1609 if (!bmap)
1610 return isl_stat_error;
1611 dim = isl_basic_map_dim(bmap, type);
1612 if (first + n > dim || first + n < first)
1613 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,do { isl_handle_error(isl_basic_map_get_ctx(bmap), isl_error_invalid
, "position or range out of bounds", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 1615); return isl_stat_error; } while (0)
1614 "position or range out of bounds",do { isl_handle_error(isl_basic_map_get_ctx(bmap), isl_error_invalid
, "position or range out of bounds", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 1615); return isl_stat_error; } while (0)
1615 return isl_stat_error)do { isl_handle_error(isl_basic_map_get_ctx(bmap), isl_error_invalid
, "position or range out of bounds", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 1615); return isl_stat_error; } while (0)
;
1616 return isl_stat_ok;
1617}
1618
1619/* Insert an extra integer division, prescribed by "div", to "bmap"
1620 * at (integer division) position "pos".
1621 *
1622 * The integer division is first added at the end and then moved
1623 * into the right position.
1624 */
1625__isl_give isl_basic_map *isl_basic_map_insert_div(
1626 __isl_take isl_basic_map *bmap, int pos, __isl_keep isl_vec *div)
1627{
1628 int i, k;
1629
1630 bmap = isl_basic_map_cow(bmap);
1631 if (!bmap || !div)
1632 return isl_basic_map_free(bmap);
1633
1634 if (div->size != 1 + 1 + isl_basic_map_dim(bmap, isl_dim_all))
1635 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,do { isl_handle_error(isl_basic_map_get_ctx(bmap), isl_error_invalid
, "unexpected size", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 1636); return isl_basic_map_free(bmap); } while (0)
1636 "unexpected size", return isl_basic_map_free(bmap))do { isl_handle_error(isl_basic_map_get_ctx(bmap), isl_error_invalid
, "unexpected size", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 1636); return isl_basic_map_free(bmap); } while (0)
;
1637 if (isl_basic_map_check_range(bmap, isl_dim_div, pos, 0) < 0)
1638 return isl_basic_map_free(bmap);
1639
1640 bmap = isl_basic_map_extend_space(bmap,
1641 isl_basic_map_get_space(bmap), 1, 0, 2);
1642 k = isl_basic_map_alloc_div(bmap);
1643 if (k < 0)
1644 return isl_basic_map_free(bmap);
1645 isl_seq_cpy(bmap->div[k], div->el, div->size);
1646 isl_int_set_si(bmap->div[k][div->size], 0)isl_sioimath_set_si((bmap->div[k][div->size]), 0);
1647
1648 for (i = k; i > pos; --i)
1649 isl_basic_map_swap_div(bmap, i, i - 1);
1650
1651 return bmap;
1652}
1653
1654isl_stat isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
1655{
1656 if (!bmap)
1657 return isl_stat_error;
1658 isl_assert(bmap->ctx, n <= bmap->n_div, return isl_stat_error)do { if (n <= bmap->n_div) break; do { isl_handle_error
(bmap->ctx, isl_error_unknown, "Assertion \"" "n <= bmap->n_div"
"\" failed", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 1658); return isl_stat_error; } while (0); } while (0)
;
1659 bmap->n_div -= n;
1660 return isl_stat_ok;
1661}
1662
1663/* Copy constraint from src to dst, putting the vars of src at offset
1664 * dim_off in dst and the divs of src at offset div_off in dst.
1665 * If both sets are actually map, then dim_off applies to the input
1666 * variables.
1667 */
1668static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
1669 struct isl_basic_map *src_map, isl_int *src,
1670 unsigned in_off, unsigned out_off, unsigned div_off)
1671{
1672 unsigned src_nparam = isl_basic_map_dim(src_map, isl_dim_param);
1673 unsigned dst_nparam = isl_basic_map_dim(dst_map, isl_dim_param);
1674 unsigned src_in = isl_basic_map_dim(src_map, isl_dim_in);
1675 unsigned dst_in = isl_basic_map_dim(dst_map, isl_dim_in);
1676 unsigned src_out = isl_basic_map_dim(src_map, isl_dim_out);
1677 unsigned dst_out = isl_basic_map_dim(dst_map, isl_dim_out);
1678 isl_int_set(dst[0], src[0])isl_sioimath_set((dst[0]), *(src[0]));
1679 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam)((dst_nparam < src_nparam) ? (dst_nparam) : (src_nparam)));
1680 if (dst_nparam > src_nparam)
1681 isl_seq_clr(dst+1+src_nparam,
1682 dst_nparam - src_nparam);
1683 isl_seq_clr(dst+1+dst_nparam, in_off);
1684 isl_seq_cpy(dst+1+dst_nparam+in_off,
1685 src+1+src_nparam,
1686 isl_min(dst_in-in_off, src_in)((dst_in-in_off < src_in) ? (dst_in-in_off) : (src_in)));
1687 if (dst_in-in_off > src_in)
1688 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
1689 dst_in - in_off - src_in);
1690 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
1691 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
1692 src+1+src_nparam+src_in,
1693 isl_min(dst_out-out_off, src_out)((dst_out-out_off < src_out) ? (dst_out-out_off) : (src_out
))
);
1694 if (dst_out-out_off > src_out)
1695 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
1696 dst_out - out_off - src_out);
1697 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
1698 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
1699 src+1+src_nparam+src_in+src_out,
1700 isl_min(dst_map->extra-div_off, src_map->n_div)((dst_map->extra-div_off < src_map->n_div) ? (dst_map
->extra-div_off) : (src_map->n_div))
);
1701 if (dst_map->n_div-div_off > src_map->n_div)
1702 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
1703 div_off+src_map->n_div,
1704 dst_map->n_div - div_off - src_map->n_div);
1705}
1706
1707static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
1708 struct isl_basic_map *src_map, isl_int *src,
1709 unsigned in_off, unsigned out_off, unsigned div_off)
1710{
1711 isl_int_set(dst[0], src[0])isl_sioimath_set((dst[0]), *(src[0]));
1712 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
1713}
1714
1715static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
1716 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
1717{
1718 int i;
1719 unsigned div_off;
1720
1721 if (!bmap1 || !bmap2)
1722 goto error;
1723
1724 div_off = bmap1->n_div;
1725
1726 for (i = 0; i < bmap2->n_eq; ++i) {
1727 int i1 = isl_basic_map_alloc_equality(bmap1);
1728 if (i1 < 0)
1729 goto error;
1730 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
1731 i_pos, o_pos, div_off);
1732 }
1733
1734 for (i = 0; i < bmap2->n_ineq; ++i) {
1735 int i1 = isl_basic_map_alloc_inequality(bmap1);
1736 if (i1 < 0)
1737 goto error;
1738 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
1739 i_pos, o_pos, div_off);
1740 }
1741
1742 for (i = 0; i < bmap2->n_div; ++i) {
1743 int i1 = isl_basic_map_alloc_div(bmap1);
1744 if (i1 < 0)
1745 goto error;
1746 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
1747 i_pos, o_pos, div_off);
1748 }
1749
1750 isl_basic_map_free(bmap2);
1751
1752 return bmap1;
1753
1754error:
1755 isl_basic_map_free(bmap1);
1756 isl_basic_map_free(bmap2);
1757 return NULL((void*)0);
1758}
1759
1760struct isl_basic_setisl_basic_map *isl_basic_set_add_constraints(struct isl_basic_setisl_basic_map *bset1,
1761 struct isl_basic_setisl_basic_map *bset2, unsigned pos)
1762{
1763 return bset_from_bmap(add_constraints(bset_to_bmap(bset1),
1764 bset_to_bmap(bset2), 0, pos));
1765}
1766
1767struct isl_basic_map *isl_basic_map_extend_space(struct isl_basic_map *base,
1768 __isl_take isl_space *dim, unsigned extra,
1769 unsigned n_eq, unsigned n_ineq)
1770{
1771 struct isl_basic_map *ext;
1772 unsigned flags;
1773 int dims_ok;
1774
1775 if (!dim)
1776 goto error;
1777
1778 if (!base)
1779 goto error;
1780
1781 dims_ok = isl_space_is_equal(base->dim, dim) &&
1782 base->extra >= base->n_div + extra;
1783
1784 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1785 room_for_ineq(base, n_ineq)) {
1786 isl_space_free(dim);
1787 return base;
1788 }
1789
1790 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error)do { if (base->dim->nparam <= dim->nparam) break;
do { isl_handle_error(base->ctx, isl_error_unknown, "Assertion \""
"base->dim->nparam <= dim->nparam" "\" failed", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 1790); goto error; } while (0); } while (0)
;
1791 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error)do { if (base->dim->n_in <= dim->n_in) break; do {
isl_handle_error(base->ctx, isl_error_unknown, "Assertion \""
"base->dim->n_in <= dim->n_in" "\" failed", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 1791); goto error; } while (0); } while (0)
;
1792 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error)do { if (base->dim->n_out <= dim->n_out) break; do
{ isl_handle_error(base->ctx, isl_error_unknown, "Assertion \""
"base->dim->n_out <= dim->n_out" "\" failed", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 1792); goto error; } while (0); } while (0)
;
1793 extra += base->extra;
1794 n_eq += base->n_eq;
1795 n_ineq += base->n_ineq;
1796
1797 ext = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1798 dim = NULL((void*)0);
1799 if (!ext)
1800 goto error;
1801
1802 if (dims_ok)
1803 ext->sample = isl_vec_copy(base->sample);
1804 flags = base->flags;
1805 ext = add_constraints(ext, base, 0, 0);
1806 if (ext) {
1807 ext->flags = flags;
1808 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL)(((ext)->flags) &= ~((1 << 0)));
1809 }
1810
1811 return ext;
1812
1813error:
1814 isl_space_free(dim);
1815 isl_basic_map_free(base);
1816 return NULL((void*)0);
1817}
1818
1819struct isl_basic_setisl_basic_map *isl_basic_set_extend_space(struct isl_basic_setisl_basic_map *base,
1820 __isl_take isl_space *dim, unsigned extra,
1821 unsigned n_eq, unsigned n_ineq)
1822{
1823 return bset_from_bmap(isl_basic_map_extend_space(bset_to_bmap(base),
1824 dim, extra, n_eq, n_ineq));
1825}
1826
1827struct isl_basic_map *isl_basic_map_extend_constraints(
1828 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1829{
1830 if (!base)
1831 return NULL((void*)0);
1832 return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1833 0, n_eq, n_ineq);
1834}
1835
1836struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1837 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1838 unsigned n_eq, unsigned n_ineq)
1839{
1840 struct isl_basic_map *bmap;
1841 isl_space *dim;
1842
1843 if (!base)
1844 return NULL((void*)0);
1845 dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1846 if (!dim)
1847 goto error;
1848
1849 bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1850 return bmap;
1851error:
1852 isl_basic_map_free(base);
1853 return NULL((void*)0);
1854}
1855
1856struct isl_basic_setisl_basic_map *isl_basic_set_extend(struct isl_basic_setisl_basic_map *base,
1857 unsigned nparam, unsigned dim, unsigned extra,
1858 unsigned n_eq, unsigned n_ineq)
1859{
1860 return bset_from_bmap(isl_basic_map_extend(bset_to_bmap(base),
1861 nparam, 0, dim, extra, n_eq, n_ineq));
1862}
1863
1864struct isl_basic_setisl_basic_map *isl_basic_set_extend_constraints(
1865 struct isl_basic_setisl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1866{
1867 isl_basic_map *bmap = bset_to_bmap(base);
1868 bmap = isl_basic_map_extend_constraints(bmap, n_eq, n_ineq);
1869 return bset_from_bmap(bmap);
1870}
1871
1872struct isl_basic_setisl_basic_map *isl_basic_set_cow(struct isl_basic_setisl_basic_map *bset)
1873{
1874 return bset_from_bmap(isl_basic_map_cow(bset_to_bmap(bset)));
1875}
1876
1877struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1878{
1879 if (!bmap)
1880 return NULL((void*)0);
1881
1882 if (bmap->ref > 1) {
1883 bmap->ref--;
1884 bmap = isl_basic_map_dup(bmap);
1885 }
1886 if (bmap) {
1887 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL)(((bmap)->flags) &= ~((1 << 0)));
1888 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS)(((bmap)->flags) &= ~((1 << 8)));
1889 }
1890 return bmap;
1891}
1892
1893/* Clear all cached information in "map", either because it is about
1894 * to be modified or because it is being freed.
1895 * Always return the same pointer that is passed in.
1896 * This is needed for the use in isl_map_free.
1897 */
1898static __isl_give isl_map *clear_caches(__isl_take isl_map *map)
1899{
1900 isl_basic_map_free(map->cached_simple_hull[0]);
1901 isl_basic_map_free(map->cached_simple_hull[1]);
1902 map->cached_simple_hull[0] = NULL((void*)0);
1903 map->cached_simple_hull[1] = NULL((void*)0);
1904 return map;
1905}
1906
1907struct isl_setisl_map *isl_set_cow(struct isl_setisl_map *set)
1908{
1909 return isl_map_cow(set);
1910}
1911
1912/* Return an isl_map that is equal to "map" and that has only
1913 * a single reference.
1914 *
1915 * If the original input already has only one reference, then
1916 * simply return it, but clear all cached information, since
1917 * it may be rendered invalid by the operations that will be
1918 * performed on the result.
1919 *
1920 * Otherwise, create a duplicate (without any cached information).
1921 */
1922struct isl_map *isl_map_cow(struct isl_map *map)
1923{
1924 if (!map)
1925 return NULL((void*)0);
1926
1927 if (map->ref == 1)
1928 return clear_caches(map);
1929 map->ref--;
1930 return isl_map_dup(map);
1931}
1932
1933static void swap_vars(struct isl_blk blk, isl_int *a,
1934 unsigned a_len, unsigned b_len)
1935{
1936 isl_seq_cpy(blk.data, a+a_len, b_len);
1937 isl_seq_cpy(blk.data+b_len, a, a_len);
1938 isl_seq_cpy(a, blk.data, b_len+a_len);
1939}
1940
1941static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1942 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1943{
1944 int i;
1945 struct isl_blk blk;
1946
1947 if (!bmap)
1948 goto error;
1949
1950 isl_assert(bmap->ctx,do { if (pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap
)) break; do { isl_handle_error(bmap->ctx, isl_error_unknown
, "Assertion \"" "pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap)"
"\" failed", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 1951); goto error; } while (0); } while (0)
1951 pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error)do { if (pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap
)) break; do { isl_handle_error(bmap->ctx, isl_error_unknown
, "Assertion \"" "pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap)"
"\" failed", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 1951); goto error; } while (0); } while (0)
;
1952
1953 if (n1 == 0 || n2 == 0)
1954 return bmap;
1955
1956 bmap = isl_basic_map_cow(bmap);
1957 if (!bmap)
1958 return NULL((void*)0);
1959
1960 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1961 if (isl_blk_is_error(blk))
1962 goto error;
1963
1964 for (i = 0; i < bmap->n_eq; ++i)
1965 swap_vars(blk,
1966 bmap->eq[i] + pos, n1, n2);
1967
1968 for (i = 0; i < bmap->n_ineq; ++i)
1969 swap_vars(blk,
1970 bmap->ineq[i] + pos, n1, n2);
1971
1972 for (i = 0; i < bmap->n_div; ++i)
1973 swap_vars(blk,
1974 bmap->div[i]+1 + pos, n1, n2);
1975
1976 isl_blk_free(bmap->ctx, blk);
1977
1978 ISL_F_CLR(bmap, ISL_BASIC_SET_NORMALIZED)(((bmap)->flags) &= ~((1 << 5)));
1979 bmap = isl_basic_map_gauss(bmap, NULL((void*)0));
1980 return isl_basic_map_finalize(bmap);
1981error:
1982 isl_basic_map_free(bmap);
1983 return NULL((void*)0);
1984}
1985
1986struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1987{
1988 int i = 0;
1989 unsigned total;
1990 if (!bmap)
1991 goto error;
1992 total = isl_basic_map_total_dim(bmap);
1993 if (isl_basic_map_free_div(bmap, bmap->n_div) < 0)
1994 return isl_basic_map_free(bmap);
1995 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1996 if (bmap->n_eq > 0)
1997 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1998 else {
1999 i = isl_basic_map_alloc_equality(bmap);
2000 if (i < 0)
2001 goto error;
2002 }
2003 isl_int_set_si(bmap->eq[i][0], 1)isl_sioimath_set_si((bmap->eq[i][0]), 1);
2004 isl_seq_clr(bmap->eq[i]+1, total);
2005 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY)(((bmap)->flags) |= ((1 << 1)));
2006 isl_vec_free(bmap->sample);
2007 bmap->sample = NULL((void*)0);
2008 return isl_basic_map_finalize(bmap);
2009error:
2010 isl_basic_map_free(bmap);
2011 return NULL((void*)0);
2012}
2013
2014struct isl_basic_setisl_basic_map *isl_basic_set_set_to_empty(struct isl_basic_setisl_basic_map *bset)
2015{
2016 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset)));
2017}
2018
2019__isl_give isl_basic_map *isl_basic_map_set_rational(
2020 __isl_take isl_basic_map *bmap)
2021{
2022 if (!bmap)
2023 return NULL((void*)0);
2024
2025 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)(!!(((bmap)->flags) & ((1 << 4)))))
2026 return bmap;
2027
2028 bmap = isl_basic_map_cow(bmap);
2029 if (!bmap)
2030 return NULL((void*)0);
2031
2032 ISL_F_SET(bmap, ISL_BASIC_MAP_RATIONAL)(((bmap)->flags) |= ((1 << 4)));
2033
2034 return isl_basic_map_finalize(bmap);
2035}
2036
2037__isl_give isl_basic_setisl_basic_map *isl_basic_set_set_rational(
2038 __isl_take isl_basic_setisl_basic_map *bset)
2039{
2040 return isl_basic_map_set_rational(bset);
2041}
2042
2043__isl_give isl_basic_setisl_basic_map *isl_basic_set_set_integral(
2044 __isl_take isl_basic_setisl_basic_map *bset)
2045{
2046 if (!bset)
2047 return NULL((void*)0);
2048
2049 if (!ISL_F_ISSET(bset, ISL_BASIC_MAP_RATIONAL)(!!(((bset)->flags) & ((1 << 4)))))
2050 return bset;
2051
2052 bset = isl_basic_set_cow(bset);
2053 if (!bset)
2054 return NULL((void*)0);
2055
2056 ISL_F_CLR(bset, ISL_BASIC_MAP_RATIONAL)(((bset)->flags) &= ~((1 << 4)));
2057
2058 return isl_basic_set_finalize(bset);
2059}
2060
2061__isl_give isl_map *isl_map_set_rational(__isl_take isl_map *map)
2062{
2063 int i;
2064
2065 map = isl_map_cow(map);
2066 if (!map)
2067 return NULL((void*)0);
2068 for (i = 0; i < map->n; ++i) {
2069 map->p[i] = isl_basic_map_set_rational(map->p[i]);
2070 if (!map->p[i])
2071 goto error;
2072 }
2073 return map;
2074error:
2075 isl_map_free(map);
2076 return NULL((void*)0);
2077}
2078
2079__isl_give isl_setisl_map *isl_set_set_rational(__isl_take isl_setisl_map *set)
2080{
2081 return isl_map_set_rational(set);
2082}
2083
2084/* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
2085 * of "bmap").
2086 */
2087static void swap_div(__isl_keep isl_basic_map *bmap, int a, int b)
2088{
2089 isl_int *t = bmap->div[a];
2090 bmap->div[a] = bmap->div[b];
2091 bmap->div[b] = t;
2092}
2093
2094/* Swap divs "a" and "b" in "bmap" and adjust the constraints and
2095 * div definitions accordingly.
2096 */
2097void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
2098{
2099 int i;
2100 unsigned off = isl_space_dim(bmap->dim, isl_dim_all);
2101
2102 swap_div(bmap, a, b);
2103
2104 for (i = 0; i < bmap->n_eq; ++i)
2105 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b])isl_sioimath_swap((bmap->eq[i][1+off+a]), (bmap->eq[i][
1+off+b]))
;
2106
2107 for (i = 0; i < bmap->n_ineq; ++i)
2108 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b])isl_sioimath_swap((bmap->ineq[i][1+off+a]), (bmap->ineq
[i][1+off+b]))
;
2109
2110 for (i = 0; i < bmap->n_div; ++i)
2111 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b])isl_sioimath_swap((bmap->div[i][1+1+off+a]), (bmap->div
[i][1+1+off+b]))
;
2112 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED)(((bmap)->flags) &= ~((1 << 5)));
2113}
2114
2115/* Swap divs "a" and "b" in "bset" and adjust the constraints and
2116 * div definitions accordingly.
2117 */
2118void isl_basic_set_swap_div(__isl_keep isl_basic_setisl_basic_map *bset, int a, int b)
2119{
2120 isl_basic_map_swap_div(bset, a, b);
2121}
2122
2123static void constraint_drop_vars(isl_int *c, unsigned n, unsigned rem)
2124{
2125 isl_seq_cpy(c, c + n, rem);
2126 isl_seq_clr(c + rem, n);
2127}
2128
2129/* Drop n dimensions starting at first.
2130 *
2131 * In principle, this frees up some extra variables as the number
2132 * of columns remains constant, but we would have to extend
2133 * the div array too as the number of rows in this array is assumed
2134 * to be equal to extra.
2135 */
2136struct isl_basic_setisl_basic_map *isl_basic_set_drop_dims(
2137 struct isl_basic_setisl_basic_map *bset, unsigned first, unsigned n)
2138{
2139 return isl_basic_map_drop(bset_to_bmap(bset), isl_dim_set, first, n);
2140}
2141
2142/* Move "n" divs starting at "first" to the end of the list of divs.
2143 */
2144static struct isl_basic_map *move_divs_last(struct isl_basic_map *bmap,
2145 unsigned first, unsigned n)
2146{
2147 isl_int **div;
2148 int i;
2149
2150 if (first + n == bmap->n_div)
2151 return bmap;
2152
2153 div = isl_alloc_array(bmap->ctx, isl_int *, n)((isl_int * *)isl_malloc_or_die(bmap->ctx, (n)*sizeof(isl_int
*)))
;
2154 if (!div)
2155 goto error;
2156 for (i = 0; i < n; ++i)
2157 div[i] = bmap->div[first + i];
2158 for (i = 0; i < bmap->n_div - first - n; ++i)
2159 bmap->div[first + i] = bmap->div[first + n + i];
2160 for (i = 0; i < n; ++i)
2161 bmap->div[bmap->n_div - n + i] = div[i];
2162 free(div);
2163 return bmap;
2164error:
2165 isl_basic_map_free(bmap);
2166 return NULL((void*)0);
2167}
2168
2169/* Drop "n" dimensions of type "type" starting at "first".
2170 *
2171 * In principle, this frees up some extra variables as the number
2172 * of columns remains constant, but we would have to extend
2173 * the div array too as the number of rows in this array is assumed
2174 * to be equal to extra.
2175 */
2176struct isl_basic_map *isl_basic_map_drop(struct isl_basic_map *bmap,
2177 enum isl_dim_type type, unsigned first, unsigned n)
2178{
2179 int i;
2180 unsigned dim;
2181 unsigned offset;
2182 unsigned left;
2183
2184 if (!bmap)
2185 goto error;
2186
2187 dim = isl_basic_map_dim(bmap, type);
2188 isl_assert(bmap->ctx, first + n <= dim, goto error)do { if (first + n <= dim) break; do { isl_handle_error(bmap
->ctx, isl_error_unknown, "Assertion \"" "first + n <= dim"
"\" failed", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 2188); goto error; } while (0); } while (0)
;
2189
2190 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2191 return bmap;
2192
2193 bmap = isl_basic_map_cow(bmap);
2194 if (!bmap)
2195 return NULL((void*)0);
2196
2197 offset = isl_basic_map_offset(bmap, type) + first;
2198 left = isl_basic_map_total_dim(bmap) - (offset - 1) - n;
2199 for (i = 0; i < bmap->n_eq; ++i)
2200 constraint_drop_vars(bmap->eq[i]+offset, n, left);
2201
2202 for (i = 0; i < bmap->n_ineq; ++i)
2203 constraint_drop_vars(bmap->ineq[i]+offset, n, left);
2204
2205 for (i = 0; i < bmap->n_div; ++i)
2206 constraint_drop_vars(bmap->div[i]+1+offset, n, left);
2207
2208 if (type == isl_dim_div) {
2209 bmap = move_divs_last(bmap, first, n);
2210 if (!bmap)
2211 goto error;
2212 if (isl_basic_map_free_div(bmap, n) < 0)
2213 return isl_basic_map_free(bmap);
2214 } else
2215 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
2216 if (!bmap->dim)
2217 goto error;
2218
2219 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED)(((bmap)->flags) &= ~((1 << 5)));
2220 bmap = isl_basic_map_simplify(bmap);
2221 return isl_basic_map_finalize(bmap);
2222error:
2223 isl_basic_map_free(bmap);
2224 return NULL((void*)0);
2225}
2226
2227__isl_give isl_basic_setisl_basic_map *isl_basic_set_drop(__isl_take isl_basic_setisl_basic_map *bset,
2228 enum isl_dim_type type, unsigned first, unsigned n)
2229{
2230 return bset_from_bmap(isl_basic_map_drop(bset_to_bmap(bset),
2231 type, first, n));
2232}
2233
2234struct isl_map *isl_map_drop(struct isl_map *map,
2235 enum isl_dim_type type, unsigned first, unsigned n)
2236{
2237 int i;
2238
2239 if (!map)
2240 goto error;
2241
2242 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error)do { if (first + n <= isl_map_dim(map, type)) break; do { isl_handle_error
(map->ctx, isl_error_unknown, "Assertion \"" "first + n <= isl_map_dim(map, type)"
"\" failed", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 2242); goto error; } while (0); } while (0)
;
2243
2244 if (n == 0 && !isl_space_is_named_or_nested(map->dim, type))
2245 return map;
2246 map = isl_map_cow(map);
2247 if (!map)
2248 goto error;
2249 map->dim = isl_space_drop_dims(map->dim, type, first, n);
2250 if (!map->dim)
2251 goto error;
2252
2253 for (i = 0; i < map->n; ++i) {
2254 map->p[i] = isl_basic_map_drop(map->p[i], type, first, n);
2255 if (!map->p[i])
2256 goto error;
2257 }
2258 ISL_F_CLR(map, ISL_MAP_NORMALIZED)(((map)->flags) &= ~((1 << 1)));
2259
2260 return map;
2261error:
2262 isl_map_free(map);
2263 return NULL((void*)0);
2264}
2265
2266struct isl_setisl_map *isl_set_drop(struct isl_setisl_map *set,
2267 enum isl_dim_type type, unsigned first, unsigned n)
2268{
2269 return set_from_map(isl_map_drop(set_to_map(set), type, first, n));
2270}
2271
2272/*
2273 * We don't cow, as the div is assumed to be redundant.
2274 */
2275__isl_give isl_basic_map *isl_basic_map_drop_div(
2276 __isl_take isl_basic_map *bmap, unsigned div)
2277{
2278 int i;
2279 unsigned pos;
2280
2281 if (!bmap)
2282 goto error;
2283
2284 pos = 1 + isl_space_dim(bmap->dim, isl_dim_all) + div;
2285
2286 isl_assert(bmap->ctx, div < bmap->n_div, goto error)do { if (div < bmap->n_div) break; do { isl_handle_error
(bmap->ctx, isl_error_unknown, "Assertion \"" "div < bmap->n_div"
"\" failed", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 2286); goto error; } while (0); } while (0)
;
2287
2288 for (i = 0; i < bmap->n_eq; ++i)
2289 constraint_drop_vars(bmap->eq[i]+pos, 1, bmap->extra-div-1);
2290
2291 for (i = 0; i < bmap->n_ineq; ++i) {
2292 if (!isl_int_is_zero(bmap->ineq[i][pos])(isl_sioimath_sgn(*(bmap->ineq[i][pos])) == 0)) {
2293 isl_basic_map_drop_inequality(bmap, i);
2294 --i;
2295 continue;
2296 }
2297 constraint_drop_vars(bmap->ineq[i]+pos, 1, bmap->extra-div-1);
2298 }
2299
2300 for (i = 0; i < bmap->n_div; ++i)
2301 constraint_drop_vars(bmap->div[i]+1+pos, 1, bmap->extra-div-1);
2302
2303 if (div != bmap->n_div - 1) {
2304 int j;
2305 isl_int *t = bmap->div[div];
2306
2307 for (j = div; j < bmap->n_div - 1; ++j)
2308 bmap->div[j] = bmap->div[j+1];
2309
2310 bmap->div[bmap->n_div - 1] = t;
2311 }
2312 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED)(((bmap)->flags) &= ~((1 << 5)));
2313 if (isl_basic_map_free_div(bmap, 1) < 0)
2314 return isl_basic_map_free(bmap);
2315
2316 return bmap;
2317error:
2318 isl_basic_map_free(bmap);
2319 return NULL((void*)0);
2320}
2321
2322/* Eliminate the specified n dimensions starting at first from the
2323 * constraints, without removing the dimensions from the space.
2324 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2325 */
2326__isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
2327 enum isl_dim_type type, unsigned first, unsigned n)
2328{
2329 int i;
2330
2331 if (!map)
2332 return NULL((void*)0);
2333 if (n == 0)
2334 return map;
2335
2336 if (first + n > isl_map_dim(map, type) || first + n < first)
2337 isl_die(map->ctx, isl_error_invalid,do { isl_handle_error(map->ctx, isl_error_invalid, "index out of bounds"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 2338); goto error; } while (0)
2338 "index out of bounds", goto error)do { isl_handle_error(map->ctx, isl_error_invalid, "index out of bounds"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 2338); goto error; } while (0)
;
2339
2340 map = isl_map_cow(map);
2341 if (!map)
2342 return NULL((void*)0);
2343
2344 for (i = 0; i < map->n; ++i) {
2345 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
2346 if (!map->p[i])
2347 goto error;
2348 }
2349 return map;
2350error:
2351 isl_map_free(map);
2352 return NULL((void*)0);
2353}
2354
2355/* Eliminate the specified n dimensions starting at first from the
2356 * constraints, without removing the dimensions from the space.
2357 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2358 */
2359__isl_give isl_setisl_map *isl_set_eliminate(__isl_take isl_setisl_map *set,
2360 enum isl_dim_type type, unsigned first, unsigned n)
2361{
2362 return set_from_map(isl_map_eliminate(set_to_map(set), type, first, n));
2363}
2364
2365/* Eliminate the specified n dimensions starting at first from the
2366 * constraints, without removing the dimensions from the space.
2367 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2368 */
2369__isl_give isl_setisl_map *isl_set_eliminate_dims(__isl_take isl_setisl_map *set,
2370 unsigned first, unsigned n)
2371{
2372 return isl_set_eliminate(set, isl_dim_set, first, n);
2373}
2374
2375__isl_give isl_basic_map *isl_basic_map_remove_divs(
2376 __isl_take isl_basic_map *bmap)
2377{
2378 if (!bmap)
2379 return NULL((void*)0);
2380 bmap = isl_basic_map_eliminate_vars(bmap,
2381 isl_space_dim(bmap->dim, isl_dim_all), bmap->n_div);
2382 if (!bmap)
2383 return NULL((void*)0);
2384 bmap->n_div = 0;
2385 return isl_basic_map_finalize(bmap);
2386}
2387
2388__isl_give isl_basic_setisl_basic_map *isl_basic_set_remove_divs(
2389 __isl_take isl_basic_setisl_basic_map *bset)
2390{
2391 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset)));
2392}
2393
2394__isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
2395{
2396 int i;
2397
2398 if (!map)
2399 return NULL((void*)0);
2400 if (map->n == 0)
2401 return map;
2402
2403 map = isl_map_cow(map);
2404 if (!map)
2405 return NULL((void*)0);
2406
2407 for (i = 0; i < map->n; ++i) {
2408 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
2409 if (!map->p[i])
2410 goto error;
2411 }
2412 return map;
2413error:
2414 isl_map_free(map);
2415 return NULL((void*)0);
2416}
2417
2418__isl_give isl_setisl_map *isl_set_remove_divs(__isl_take isl_setisl_map *set)
2419{
2420 return isl_map_remove_divs(set);
2421}
2422
2423struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap,
2424 enum isl_dim_type type, unsigned first, unsigned n)
2425{
2426 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2427 return isl_basic_map_free(bmap);
2428 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2429 return bmap;
2430 bmap = isl_basic_map_eliminate_vars(bmap,
2431 isl_basic_map_offset(bmap, type) - 1 + first, n);
2432 if (!bmap)
2433 return bmap;
2434 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY)(!!(((bmap)->flags) & ((1 << 1)))) && type == isl_dim_div)
2435 return bmap;
2436 bmap = isl_basic_map_drop(bmap, type, first, n);
2437 return bmap;
2438}
2439
2440/* Return true if the definition of the given div (recursively) involves
2441 * any of the given variables.
2442 */
2443static isl_bool div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
2444 unsigned first, unsigned n)
2445{
2446 int i;
2447 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2448
2449 if (isl_int_is_zero(bmap->div[div][0])(isl_sioimath_sgn(*(bmap->div[div][0])) == 0))
2450 return isl_bool_false;
2451 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
2452 return isl_bool_true;
2453
2454 for (i = bmap->n_div - 1; i >= 0; --i) {
2455 isl_bool involves;
2456
2457 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i])(isl_sioimath_sgn(*(bmap->div[div][1 + div_offset + i])) ==
0)
)
2458 continue;
2459 involves = div_involves_vars(bmap, i, first, n);
2460 if (involves < 0 || involves)
2461 return involves;
2462 }
2463
2464 return isl_bool_false;
2465}
2466
2467/* Try and add a lower and/or upper bound on "div" to "bmap"
2468 * based on inequality "i".
2469 * "total" is the total number of variables (excluding the divs).
2470 * "v" is a temporary object that can be used during the calculations.
2471 * If "lb" is set, then a lower bound should be constructed.
2472 * If "ub" is set, then an upper bound should be constructed.
2473 *
2474 * The calling function has already checked that the inequality does not
2475 * reference "div", but we still need to check that the inequality is
2476 * of the right form. We'll consider the case where we want to construct
2477 * a lower bound. The construction of upper bounds is similar.
2478 *
2479 * Let "div" be of the form
2480 *
2481 * q = floor((a + f(x))/d)
2482 *
2483 * We essentially check if constraint "i" is of the form
2484 *
2485 * b + f(x) >= 0
2486 *
2487 * so that we can use it to derive a lower bound on "div".
2488 * However, we allow a slightly more general form
2489 *
2490 * b + g(x) >= 0
2491 *
2492 * with the condition that the coefficients of g(x) - f(x) are all
2493 * divisible by d.
2494 * Rewriting this constraint as
2495 *
2496 * 0 >= -b - g(x)
2497 *
2498 * adding a + f(x) to both sides and dividing by d, we obtain
2499 *
2500 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2501 *
2502 * Taking the floor on both sides, we obtain
2503 *
2504 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2505 *
2506 * or
2507 *
2508 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2509 *
2510 * In the case of an upper bound, we construct the constraint
2511 *
2512 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2513 *
2514 */
2515static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
2516 __isl_take isl_basic_map *bmap, int div, int i,
2517 unsigned total, isl_int v, int lb, int ub)
2518{
2519 int j;
2520
2521 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
2522 if (lb) {
2523 isl_int_sub(v, bmap->ineq[i][1 + j],isl_sioimath_sub((v), *(bmap->ineq[i][1 + j]), *(bmap->
div[div][1 + 1 + j]))
2524 bmap->div[div][1 + 1 + j])isl_sioimath_sub((v), *(bmap->ineq[i][1 + j]), *(bmap->
div[div][1 + 1 + j]))
;
2525 lb = isl_int_is_divisible_by(v, bmap->div[div][0])isl_sioimath_is_divisible_by(*(v), *(bmap->div[div][0]));
2526 }
2527 if (ub) {
2528 isl_int_add(v, bmap->ineq[i][1 + j],isl_sioimath_add((v), *(bmap->ineq[i][1 + j]), *(bmap->
div[div][1 + 1 + j]))
2529 bmap->div[div][1 + 1 + j])isl_sioimath_add((v), *(bmap->ineq[i][1 + j]), *(bmap->
div[div][1 + 1 + j]))
;
2530 ub = isl_int_is_divisible_by(v, bmap->div[div][0])isl_sioimath_is_divisible_by(*(v), *(bmap->div[div][0]));
2531 }
2532 }
2533 if (!lb && !ub)
2534 return bmap;
2535
2536 bmap = isl_basic_map_cow(bmap);
2537 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
2538 if (lb) {
2539 int k = isl_basic_map_alloc_inequality(bmap);
2540 if (k < 0)
2541 goto error;
2542 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2543 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],isl_sioimath_sub((bmap->ineq[k][j]), *(bmap->ineq[i][j]
), *(bmap->div[div][1 + j]))
2544 bmap->div[div][1 + j])isl_sioimath_sub((bmap->ineq[k][j]), *(bmap->ineq[i][j]
), *(bmap->div[div][1 + j]))
;
2545 isl_int_cdiv_q(bmap->ineq[k][j],isl_sioimath_cdiv_q((bmap->ineq[k][j]), *(bmap->ineq[k]
[j]), *(bmap->div[div][0]))
2546 bmap->ineq[k][j], bmap->div[div][0])isl_sioimath_cdiv_q((bmap->ineq[k][j]), *(bmap->ineq[k]
[j]), *(bmap->div[div][0]))
;
2547 }
2548 isl_int_set_si(bmap->ineq[k][1 + total + div], 1)isl_sioimath_set_si((bmap->ineq[k][1 + total + div]), 1);
2549 }
2550 if (ub) {
2551 int k = isl_basic_map_alloc_inequality(bmap);
2552 if (k < 0)
2553 goto error;
2554 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2555 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],isl_sioimath_add((bmap->ineq[k][j]), *(bmap->ineq[i][j]
), *(bmap->div[div][1 + j]))
2556 bmap->div[div][1 + j])isl_sioimath_add((bmap->ineq[k][j]), *(bmap->ineq[i][j]
), *(bmap->div[div][1 + j]))
;
2557 isl_int_fdiv_q(bmap->ineq[k][j],isl_sioimath_fdiv_q((bmap->ineq[k][j]), *(bmap->ineq[k]
[j]), *(bmap->div[div][0]))
2558 bmap->ineq[k][j], bmap->div[div][0])isl_sioimath_fdiv_q((bmap->ineq[k][j]), *(bmap->ineq[k]
[j]), *(bmap->div[div][0]))
;
2559 }
2560 isl_int_set_si(bmap->ineq[k][1 + total + div], -1)isl_sioimath_set_si((bmap->ineq[k][1 + total + div]), -1);
2561 }
2562
2563 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED)(((bmap)->flags) &= ~((1 << 5)));
2564 return bmap;
2565error:
2566 isl_basic_map_free(bmap);
2567 return NULL((void*)0);
2568}
2569
2570/* This function is called right before "div" is eliminated from "bmap"
2571 * using Fourier-Motzkin.
2572 * Look through the constraints of "bmap" for constraints on the argument
2573 * of the integer division and use them to construct constraints on the
2574 * integer division itself. These constraints can then be combined
2575 * during the Fourier-Motzkin elimination.
2576 * Note that it is only useful to introduce lower bounds on "div"
2577 * if "bmap" already contains upper bounds on "div" as the newly
2578 * introduce lower bounds can then be combined with the pre-existing
2579 * upper bounds. Similarly for upper bounds.
2580 * We therefore first check if "bmap" contains any lower and/or upper bounds
2581 * on "div".
2582 *
2583 * It is interesting to note that the introduction of these constraints
2584 * can indeed lead to more accurate results, even when compared to
2585 * deriving constraints on the argument of "div" from constraints on "div".
2586 * Consider, for example, the set
2587 *
2588 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2589 *
2590 * The second constraint can be rewritten as
2591 *
2592 * 2 * [(-i-2j+3)/4] + k >= 0
2593 *
2594 * from which we can derive
2595 *
2596 * -i - 2j + 3 >= -2k
2597 *
2598 * or
2599 *
2600 * i + 2j <= 3 + 2k
2601 *
2602 * Combined with the first constraint, we obtain
2603 *
2604 * -3 <= 3 + 2k or k >= -3
2605 *
2606 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2607 * the first constraint, we obtain
2608 *
2609 * [(i + 2j)/4] >= [-3/4] = -1
2610 *
2611 * Combining this constraint with the second constraint, we obtain
2612 *
2613 * k >= -2
2614 */
2615static __isl_give isl_basic_map *insert_bounds_on_div(
2616 __isl_take isl_basic_map *bmap, int div)
2617{
2618 int i;
2619 int check_lb, check_ub;
2620 isl_int v;
2621 unsigned total;
2622
2623 if (!bmap)
2624 return NULL((void*)0);
2625
2626 if (isl_int_is_zero(bmap->div[div][0])(isl_sioimath_sgn(*(bmap->div[div][0])) == 0))
2627 return bmap;
2628
2629 total = isl_space_dim(bmap->dim, isl_dim_all);
2630
2631 check_lb = 0;
2632 check_ub = 0;
2633 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
2634 int s = isl_int_sgn(bmap->ineq[i][1 + total + div])isl_sioimath_sgn(*(bmap->ineq[i][1 + total + div]));
2635 if (s > 0)
2636 check_ub = 1;
2637 if (s < 0)
2638 check_lb = 1;
2639 }
2640
2641 if (!check_lb && !check_ub)
2642 return bmap;
2643
2644 isl_int_init(v)isl_sioimath_init((v));
2645
2646 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
2647 if (!isl_int_is_zero(bmap->ineq[i][1 + total + div])(isl_sioimath_sgn(*(bmap->ineq[i][1 + total + div])) == 0))
2648 continue;
2649
2650 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, total, v,
2651 check_lb, check_ub);
2652 }
2653
2654 isl_int_clear(v)isl_sioimath_clear((v));
2655
2656 return bmap;
2657}
2658
2659/* Remove all divs (recursively) involving any of the given dimensions
2660 * in their definitions.
2661 */
2662__isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
2663 __isl_take isl_basic_map *bmap,
2664 enum isl_dim_type type, unsigned first, unsigned n)
2665{
2666 int i;
2667
2668 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2669 return isl_basic_map_free(bmap);
2670 first += isl_basic_map_offset(bmap, type);
2671
2672 for (i = bmap->n_div - 1; i >= 0; --i) {
2673 isl_bool involves;
2674
2675 involves = div_involves_vars(bmap, i, first, n);
2676 if (involves < 0)
2677 return isl_basic_map_free(bmap);
2678 if (!involves)
2679 continue;
2680 bmap = insert_bounds_on_div(bmap, i);
2681 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2682 if (!bmap)
2683 return NULL((void*)0);
2684 i = bmap->n_div;
2685 }
2686
2687 return bmap;
2688}
2689
2690__isl_give isl_basic_setisl_basic_map *isl_basic_set_remove_divs_involving_dims(
2691 __isl_take isl_basic_setisl_basic_map *bset,
2692 enum isl_dim_type type, unsigned first, unsigned n)
2693{
2694 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
2695}
2696
2697__isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
2698 enum isl_dim_type type, unsigned first, unsigned n)
2699{
2700 int i;
2701
2702 if (!map)
2703 return NULL((void*)0);
2704 if (map->n == 0)
2705 return map;
2706
2707 map = isl_map_cow(map);
2708 if (!map)
2709 return NULL((void*)0);
2710
2711 for (i = 0; i < map->n; ++i) {
2712 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
2713 type, first, n);
2714 if (!map->p[i])
2715 goto error;
2716 }
2717 return map;
2718error:
2719 isl_map_free(map);
2720 return NULL((void*)0);
2721}
2722
2723__isl_give isl_setisl_map *isl_set_remove_divs_involving_dims(__isl_take isl_setisl_map *set,
2724 enum isl_dim_type type, unsigned first, unsigned n)
2725{
2726 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set),
2727 type, first, n));
2728}
2729
2730/* Does the description of "bmap" depend on the specified dimensions?
2731 * We also check whether the dimensions appear in any of the div definitions.
2732 * In principle there is no need for this check. If the dimensions appear
2733 * in a div definition, they also appear in the defining constraints of that
2734 * div.
2735 */
2736isl_bool isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
2737 enum isl_dim_type type, unsigned first, unsigned n)
2738{
2739 int i;
2740
2741 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2742 return isl_bool_error;
2743
2744 first += isl_basic_map_offset(bmap, type);
2745 for (i = 0; i < bmap->n_eq; ++i)
2746 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
2747 return isl_bool_true;
2748 for (i = 0; i < bmap->n_ineq; ++i)
2749 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
2750 return isl_bool_true;
2751 for (i = 0; i < bmap->n_div; ++i) {
2752 if (isl_int_is_zero(bmap->div[i][0])(isl_sioimath_sgn(*(bmap->div[i][0])) == 0))
2753 continue;
2754 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
2755 return isl_bool_true;
2756 }
2757
2758 return isl_bool_false;
2759}
2760
2761isl_bool isl_map_involves_dims(__isl_keep isl_map *map,
2762 enum isl_dim_type type, unsigned first, unsigned n)
2763{
2764 int i;
2765
2766 if (!map)
2767 return isl_bool_error;
2768
2769 if (first + n > isl_map_dim(map, type))
2770 isl_die(map->ctx, isl_error_invalid,do { isl_handle_error(map->ctx, isl_error_invalid, "index out of bounds"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 2771); return isl_bool_error; } while (0)
2771 "index out of bounds", return isl_bool_error)do { isl_handle_error(map->ctx, isl_error_invalid, "index out of bounds"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 2771); return isl_bool_error; } while (0)
;
2772
2773 for (i = 0; i < map->n; ++i) {
2774 isl_bool involves = isl_basic_map_involves_dims(map->p[i],
2775 type, first, n);
2776 if (involves < 0 || involves)
2777 return involves;
2778 }
2779
2780 return isl_bool_false;
2781}
2782
2783isl_bool isl_basic_set_involves_dims(__isl_keep isl_basic_setisl_basic_map *bset,
2784 enum isl_dim_type type, unsigned first, unsigned n)
2785{
2786 return isl_basic_map_involves_dims(bset, type, first, n);
2787}
2788
2789isl_bool isl_set_involves_dims(__isl_keep isl_setisl_map *set,
2790 enum isl_dim_type type, unsigned first, unsigned n)
2791{
2792 return isl_map_involves_dims(set, type, first, n);
2793}
2794
2795/* Drop all constraints in bmap that involve any of the dimensions
2796 * first to first+n-1.
2797 */
2798static __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving(
2799 __isl_take isl_basic_map *bmap, unsigned first, unsigned n)
2800{
2801 int i;
2802
2803 if (n == 0)
2804 return bmap;
2805
2806 bmap = isl_basic_map_cow(bmap);
2807
2808 if (!bmap)
2809 return NULL((void*)0);
2810
2811 for (i = bmap->n_eq - 1; i >= 0; --i) {
2812 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) == -1)
2813 continue;
2814 isl_basic_map_drop_equality(bmap, i);
2815 }
2816
2817 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2818 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) == -1)
2819 continue;
2820 isl_basic_map_drop_inequality(bmap, i);
2821 }
2822
2823 bmap = isl_basic_map_add_known_div_constraints(bmap);
2824 return bmap;
2825}
2826
2827/* Drop all constraints in bset that involve any of the dimensions
2828 * first to first+n-1.
2829 */
2830__isl_give isl_basic_setisl_basic_map *isl_basic_set_drop_constraints_involving(
2831 __isl_take isl_basic_setisl_basic_map *bset, unsigned first, unsigned n)
2832{
2833 return isl_basic_map_drop_constraints_involving(bset, first, n);
2834}
2835
2836/* Drop all constraints in bmap that do not involve any of the dimensions
2837 * first to first + n - 1 of the given type.
2838 */
2839__isl_give isl_basic_map *isl_basic_map_drop_constraints_not_involving_dims(
2840 __isl_take isl_basic_map *bmap,
2841 enum isl_dim_type type, unsigned first, unsigned n)
2842{
2843 int i;
2844
2845 if (n == 0) {
2846 isl_space *space = isl_basic_map_get_space(bmap);
2847 isl_basic_map_free(bmap);
2848 return isl_basic_map_universe(space);
2849 }
2850 bmap = isl_basic_map_cow(bmap);
2851 if (!bmap)
2852 return NULL((void*)0);
2853
2854 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2855 return isl_basic_map_free(bmap);
2856
2857 first += isl_basic_map_offset(bmap, type) - 1;
2858
2859 for (i = bmap->n_eq - 1; i >= 0; --i) {
2860 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) != -1)
2861 continue;
2862 isl_basic_map_drop_equality(bmap, i);
2863 }
2864
2865 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2866 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) != -1)
2867 continue;
2868 isl_basic_map_drop_inequality(bmap, i);
2869 }
2870
2871 bmap = isl_basic_map_add_known_div_constraints(bmap);
2872 return bmap;
2873}
2874
2875/* Drop all constraints in bset that do not involve any of the dimensions
2876 * first to first + n - 1 of the given type.
2877 */
2878__isl_give isl_basic_setisl_basic_map *isl_basic_set_drop_constraints_not_involving_dims(
2879 __isl_take isl_basic_setisl_basic_map *bset,
2880 enum isl_dim_type type, unsigned first, unsigned n)
2881{
2882 return isl_basic_map_drop_constraints_not_involving_dims(bset,
2883 type, first, n);
2884}
2885
2886/* Drop all constraints in bmap that involve any of the dimensions
2887 * first to first + n - 1 of the given type.
2888 */
2889__isl_give isl_basic_map *isl_basic_map_drop_constraints_involving_dims(
2890 __isl_take isl_basic_map *bmap,
2891 enum isl_dim_type type, unsigned first, unsigned n)
2892{
2893 if (!bmap)
2894 return NULL((void*)0);
2895 if (n == 0)
2896 return bmap;
2897
2898 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2899 return isl_basic_map_free(bmap);
2900
2901 bmap = isl_basic_map_remove_divs_involving_dims(bmap, type, first, n);
2902 first += isl_basic_map_offset(bmap, type) - 1;
2903 return isl_basic_map_drop_constraints_involving(bmap, first, n);
2904}
2905
2906/* Drop all constraints in bset that involve any of the dimensions
2907 * first to first + n - 1 of the given type.
2908 */
2909__isl_give isl_basic_setisl_basic_map *isl_basic_set_drop_constraints_involving_dims(
2910 __isl_take isl_basic_setisl_basic_map *bset,
2911 enum isl_dim_type type, unsigned first, unsigned n)
2912{
2913 return isl_basic_map_drop_constraints_involving_dims(bset,
2914 type, first, n);
2915}
2916
2917/* Drop constraints from "map" by applying "drop" to each basic map.
2918 */
2919static __isl_give isl_map *drop_constraints(__isl_take isl_map *map,
2920 enum isl_dim_type type, unsigned first, unsigned n,
2921 __isl_give isl_basic_map *(*drop)(__isl_take isl_basic_map *bmap,
2922 enum isl_dim_type type, unsigned first, unsigned n))
2923{
2924 int i;
2925 unsigned dim;
2926
2927 if (!map)
2928 return NULL((void*)0);
2929
2930 dim = isl_map_dim(map, type);
2931 if (first + n > dim || first + n < first)
2932 isl_die(isl_map_get_ctx(map), isl_error_invalid,do { isl_handle_error(isl_map_get_ctx(map), isl_error_invalid
, "index out of bounds", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 2933); return isl_map_free(map); } while (0)
2933 "index out of bounds", return isl_map_free(map))do { isl_handle_error(isl_map_get_ctx(map), isl_error_invalid
, "index out of bounds", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 2933); return isl_map_free(map); } while (0)
;
2934
2935 map = isl_map_cow(map);
2936 if (!map)
2937 return NULL((void*)0);
2938
2939 for (i = 0; i < map->n; ++i) {
2940 map->p[i] = drop(map->p[i], type, first, n);
2941 if (!map->p[i])
2942 return isl_map_free(map);
2943 }
2944
2945 if (map->n > 1)
2946 ISL_F_CLR(map, ISL_MAP_DISJOINT)(((map)->flags) &= ~((1 << 0)));
2947
2948 return map;
2949}
2950
2951/* Drop all constraints in map that involve any of the dimensions
2952 * first to first + n - 1 of the given type.
2953 */
2954__isl_give isl_map *isl_map_drop_constraints_involving_dims(
2955 __isl_take isl_map *map,
2956 enum isl_dim_type type, unsigned first, unsigned n)
2957{
2958 if (n == 0)
2959 return map;
2960 return drop_constraints(map, type, first, n,
2961 &isl_basic_map_drop_constraints_involving_dims);
2962}
2963
2964/* Drop all constraints in "map" that do not involve any of the dimensions
2965 * first to first + n - 1 of the given type.
2966 */
2967__isl_give isl_map *isl_map_drop_constraints_not_involving_dims(
2968 __isl_take isl_map *map,
2969 enum isl_dim_type type, unsigned first, unsigned n)
2970{
2971 if (n == 0) {
2972 isl_space *space = isl_map_get_space(map);
2973 isl_map_free(map);
2974 return isl_map_universe(space);
2975 }
2976 return drop_constraints(map, type, first, n,
2977 &isl_basic_map_drop_constraints_not_involving_dims);
2978}
2979
2980/* Drop all constraints in set that involve any of the dimensions
2981 * first to first + n - 1 of the given type.
2982 */
2983__isl_give isl_setisl_map *isl_set_drop_constraints_involving_dims(
2984 __isl_take isl_setisl_map *set,
2985 enum isl_dim_type type, unsigned first, unsigned n)
2986{
2987 return isl_map_drop_constraints_involving_dims(set, type, first, n);
2988}
2989
2990/* Drop all constraints in "set" that do not involve any of the dimensions
2991 * first to first + n - 1 of the given type.
2992 */
2993__isl_give isl_setisl_map *isl_set_drop_constraints_not_involving_dims(
2994 __isl_take isl_setisl_map *set,
2995 enum isl_dim_type type, unsigned first, unsigned n)
2996{
2997 return isl_map_drop_constraints_not_involving_dims(set, type, first, n);
2998}
2999
3000/* Does local variable "div" of "bmap" have a complete explicit representation?
3001 * Having a complete explicit representation requires not only
3002 * an explicit representation, but also that all local variables
3003 * that appear in this explicit representation in turn have
3004 * a complete explicit representation.
3005 */
3006isl_bool isl_basic_map_div_is_known(__isl_keep isl_basic_map *bmap, int div)
3007{
3008 int i;
3009 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
3010 isl_bool marked;
3011
3012 marked = isl_basic_map_div_is_marked_unknown(bmap, div);
3013 if (marked < 0 || marked)
3014 return isl_bool_not(marked);
3015
3016 for (i = bmap->n_div - 1; i >= 0; --i) {
3017 isl_bool known;
3018
3019 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i])(isl_sioimath_sgn(*(bmap->div[div][1 + div_offset + i])) ==
0)
)
3020 continue;
3021 known = isl_basic_map_div_is_known(bmap, i);
3022 if (known < 0 || !known)
3023 return known;
3024 }
3025
3026 return isl_bool_true;
3027}
3028
3029/* Remove all divs that are unknown or defined in terms of unknown divs.
3030 */
3031__isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
3032 __isl_take isl_basic_map *bmap)
3033{
3034 int i;
3035
3036 if (!bmap)
3037 return NULL((void*)0);
3038
3039 for (i = bmap->n_div - 1; i >= 0; --i) {
3040 if (isl_basic_map_div_is_known(bmap, i))
3041 continue;
3042 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
3043 if (!bmap)
3044 return NULL((void*)0);
3045 i = bmap->n_div;
3046 }
3047
3048 return bmap;
3049}
3050
3051/* Remove all divs that are unknown or defined in terms of unknown divs.
3052 */
3053__isl_give isl_basic_setisl_basic_map *isl_basic_set_remove_unknown_divs(
3054 __isl_take isl_basic_setisl_basic_map *bset)
3055{
3056 return isl_basic_map_remove_unknown_divs(bset);
3057}
3058
3059__isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
3060{
3061 int i;
3062
3063 if (!map)
3064 return NULL((void*)0);
3065 if (map->n == 0)
3066 return map;
3067
3068 map = isl_map_cow(map);
3069 if (!map)
3070 return NULL((void*)0);
3071
3072 for (i = 0; i < map->n; ++i) {
3073 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
3074 if (!map->p[i])
3075 goto error;
3076 }
3077 return map;
3078error:
3079 isl_map_free(map);
3080 return NULL((void*)0);
3081}
3082
3083__isl_give isl_setisl_map *isl_set_remove_unknown_divs(__isl_take isl_setisl_map *set)
3084{
3085 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set)));
3086}
3087
3088__isl_give isl_basic_setisl_basic_map *isl_basic_set_remove_dims(
3089 __isl_take isl_basic_setisl_basic_map *bset,
3090 enum isl_dim_type type, unsigned first, unsigned n)
3091{
3092 isl_basic_map *bmap = bset_to_bmap(bset);
3093 bmap = isl_basic_map_remove_dims(bmap, type, first, n);
3094 return bset_from_bmap(bmap);
3095}
3096
3097struct isl_map *isl_map_remove_dims(struct isl_map *map,
3098 enum isl_dim_type type, unsigned first, unsigned n)
3099{
3100 int i;
3101
3102 if (n == 0)
3103 return map;
3104
3105 map = isl_map_cow(map);
3106 if (!map)
3107 return NULL((void*)0);
3108 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error)do { if (first + n <= isl_map_dim(map, type)) break; do { isl_handle_error
(map->ctx, isl_error_unknown, "Assertion \"" "first + n <= isl_map_dim(map, type)"
"\" failed", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 3108); goto error; } while (0); } while (0)
;
3109
3110 for (i = 0; i < map->n; ++i) {
3111 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
3112 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
3113 if (!map->p[i])
3114 goto error;
3115 }
3116 map = isl_map_drop(map, type, first, n);
3117 return map;
3118error:
3119 isl_map_free(map);
3120 return NULL((void*)0);
3121}
3122
3123__isl_give isl_setisl_map *isl_set_remove_dims(__isl_take isl_setisl_map *bset,
3124 enum isl_dim_type type, unsigned first, unsigned n)
3125{
3126 return set_from_map(isl_map_remove_dims(set_to_map(bset),
3127 type, first, n));
3128}
3129
3130/* Project out n inputs starting at first using Fourier-Motzkin */
3131struct isl_map *isl_map_remove_inputs(struct isl_map *map,
3132 unsigned first, unsigned n)
3133{
3134 return isl_map_remove_dims(map, isl_dim_in, first, n);
3135}
3136
3137static void dump_term(struct isl_basic_map *bmap,
3138 isl_int c, int pos, FILE *out)
3139{
3140 const char *name;
3141 unsigned in = isl_basic_map_dim(bmap, isl_dim_in);
3142 unsigned dim = in + isl_basic_map_dim(bmap, isl_dim_out);
3143 unsigned nparam = isl_basic_map_dim(bmap, isl_dim_param);
3144 if (!pos)
3145 isl_int_print(out, c, 0)isl_sioimath_print(out, *(c), 0);
3146 else {
3147 if (!isl_int_is_one(c)(isl_sioimath_cmp_si(*(c), 1) == 0))
3148 isl_int_print(out, c, 0)isl_sioimath_print(out, *(c), 0);
3149 if (pos < 1 + nparam) {
3150 name = isl_space_get_dim_name(bmap->dim,
3151 isl_dim_param, pos - 1);
3152 if (name)
3153 fprintf(out, "%s", name);
3154 else
3155 fprintf(out, "p%d", pos - 1);
3156 } else if (pos < 1 + nparam + in)
3157 fprintf(out, "i%d", pos - 1 - nparam);
3158 else if (pos < 1 + nparam + dim)
3159 fprintf(out, "o%d", pos - 1 - nparam - in);
3160 else
3161 fprintf(out, "e%d", pos - 1 - nparam - dim);
3162 }
3163}
3164
3165static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
3166 int sign, FILE *out)
3167{
3168 int i;
3169 int first;
3170 unsigned len = 1 + isl_basic_map_total_dim(bmap);
3171 isl_int v;
3172
3173 isl_int_init(v)isl_sioimath_init((v));
3174 for (i = 0, first = 1; i < len; ++i) {
3175 if (isl_int_sgn(c[i])isl_sioimath_sgn(*(c[i])) * sign <= 0)
3176 continue;
3177 if (!first)
3178 fprintf(out, " + ");
3179 first = 0;
3180 isl_int_abs(v, c[i])isl_sioimath_abs((v), *(c[i]));
3181 dump_term(bmap, v, i, out);
3182 }
3183 isl_int_clear(v)isl_sioimath_clear((v));
3184 if (first)
3185 fprintf(out, "0");
3186}
3187
3188static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
3189 const char *op, FILE *out, int indent)
3190{
3191 int i;
3192
3193 fprintf(out, "%*s", indent, "");
3194
3195 dump_constraint_sign(bmap, c, 1, out);
3196 fprintf(out, " %s ", op);
3197 dump_constraint_sign(bmap, c, -1, out);
3198
3199 fprintf(out, "\n");
3200
3201 for (i = bmap->n_div; i < bmap->extra; ++i) {
3202 if (isl_int_is_zero(c[1+isl_space_dim(bmap->dim, isl_dim_all)+i])(isl_sioimath_sgn(*(c[1+isl_space_dim(bmap->dim, isl_dim_all
)+i])) == 0)
)
3203 continue;
3204 fprintf(out, "%*s", indent, "");
3205 fprintf(out, "ERROR: unused div coefficient not zero\n");
3206 abort();
3207 }
3208}
3209
3210static void dump_constraints(struct isl_basic_map *bmap,
3211 isl_int **c, unsigned n,
3212 const char *op, FILE *out, int indent)
3213{
3214 int i;
3215
3216 for (i = 0; i < n; ++i)
3217 dump_constraint(bmap, c[i], op, out, indent);
3218}
3219
3220static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
3221{
3222 int j;
3223 int first = 1;
3224 unsigned total = isl_basic_map_total_dim(bmap);
3225
3226 for (j = 0; j < 1 + total; ++j) {
3227 if (isl_int_is_zero(exp[j])(isl_sioimath_sgn(*(exp[j])) == 0))
3228 continue;
3229 if (!first && isl_int_is_pos(exp[j])(isl_sioimath_sgn(*(exp[j])) > 0))
3230 fprintf(out, "+");
3231 dump_term(bmap, exp[j], j, out);
3232 first = 0;
3233 }
3234}
3235
3236static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
3237{
3238 int i;
3239
3240 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
3241 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
3242
3243 for (i = 0; i < bmap->n_div; ++i) {
3244 fprintf(out, "%*s", indent, "");
3245 fprintf(out, "e%d = [(", i);
3246 dump_affine(bmap, bmap->div[i]+1, out);
3247 fprintf(out, ")/");
3248 isl_int_print(out, bmap->div[i][0], 0)isl_sioimath_print(out, *(bmap->div[i][0]), 0);
3249 fprintf(out, "]\n");
3250 }
3251}
3252
3253void isl_basic_set_print_internal(struct isl_basic_setisl_basic_map *bset,
3254 FILE *out, int indent)
3255{
3256 if (!bset) {
3257 fprintf(out, "null basic set\n");
3258 return;
3259 }
3260
3261 fprintf(out, "%*s", indent, "");
3262 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
3263 bset->ref, bset->dim->nparam, bset->dim->n_out,
3264 bset->extra, bset->flags);
3265 dump(bset_to_bmap(bset), out, indent);
3266}
3267
3268void isl_basic_map_print_internal(struct isl_basic_map *bmap,
3269 FILE *out, int indent)
3270{
3271 if (!bmap) {
3272 fprintf(out, "null basic map\n");
3273 return;
3274 }
3275
3276 fprintf(out, "%*s", indent, "");
3277 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
3278 "flags: %x, n_name: %d\n",
3279 bmap->ref,
3280 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
3281 bmap->extra, bmap->flags, bmap->dim->n_id);
3282 dump(bmap, out, indent);
3283}
3284
3285int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
3286{
3287 unsigned total;
3288 if (!bmap)
3289 return -1;
3290 total = isl_basic_map_total_dim(bmap);
3291 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1)do { if (pos < bmap->n_ineq) break; do { isl_handle_error
(bmap->ctx, isl_error_unknown, "Assertion \"" "pos < bmap->n_ineq"
"\" failed", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 3291); return -1; } while (0); } while (0)
;
3292 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
3293 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1)isl_sioimath_sub_ui((bmap->ineq[pos][0]), *(bmap->ineq[
pos][0]), 1)
;
3294 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED)(((bmap)->flags) &= ~((1 << 5)));
3295 return 0;
3296}
3297
3298__isl_give isl_setisl_map *isl_set_alloc_space(__isl_take isl_space *space, int n,
3299 unsigned flags)
3300{
3301 if (!space)
3302 return NULL((void*)0);
3303 if (isl_space_dim(space, isl_dim_in) != 0)
3304 isl_die(isl_space_get_ctx(space), isl_error_invalid,do { isl_handle_error(isl_space_get_ctx(space), isl_error_invalid
, "set cannot have input dimensions", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 3305); goto error; } while (0)
3305 "set cannot have input dimensions", goto error)do { isl_handle_error(isl_space_get_ctx(space), isl_error_invalid
, "set cannot have input dimensions", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 3305); goto error; } while (0)
;
3306 return isl_map_alloc_space(space, n, flags);
3307error:
3308 isl_space_free(space);
3309 return NULL((void*)0);
3310}
3311
3312/* Make sure "map" has room for at least "n" more basic maps.
3313 */
3314struct isl_map *isl_map_grow(struct isl_map *map, int n)
3315{
3316 int i;
3317 struct isl_map *grown = NULL((void*)0);
3318
3319 if (!map)
3320 return NULL((void*)0);
3321 isl_assert(map->ctx, n >= 0, goto error)do { if (n >= 0) break; do { isl_handle_error(map->ctx,
isl_error_unknown, "Assertion \"" "n >= 0" "\" failed", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 3321); goto error; } while (0); } while (0)
;
3322 if (map->n + n <= map->size)
3323 return map;
3324 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
3325 if (!grown)
3326 goto error;
3327 for (i = 0; i < map->n; ++i) {
3328 grown->p[i] = isl_basic_map_copy(map->p[i]);
3329 if (!grown->p[i])
3330 goto error;
3331 grown->n++;
3332 }
3333 isl_map_free(map);
3334 return grown;
3335error:
3336 isl_map_free(grown);
3337 isl_map_free(map);
3338 return NULL((void*)0);
3339}
3340
3341/* Make sure "set" has room for at least "n" more basic sets.
3342 */
3343struct isl_setisl_map *isl_set_grow(struct isl_setisl_map *set, int n)
3344{
3345 return set_from_map(isl_map_grow(set_to_map(set), n));
3346}
3347
3348struct isl_setisl_map *isl_set_from_basic_set(struct isl_basic_setisl_basic_map *bset)
3349{
3350 return isl_map_from_basic_map(bset);
3351}
3352
3353struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
3354{
3355 struct isl_map *map;
3356
3357 if (!bmap)
3358 return NULL((void*)0);
3359
3360 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT(1 << 0));
3361 return isl_map_add_basic_map(map, bmap);
3362}
3363
3364__isl_give isl_setisl_map *isl_set_add_basic_set(__isl_take isl_setisl_map *set,
3365 __isl_take isl_basic_setisl_basic_map *bset)
3366{
3367 return set_from_map(isl_map_add_basic_map(set_to_map(set),
3368 bset_to_bmap(bset)));
3369}
3370
3371__isl_null isl_setisl_map *isl_set_free(__isl_take isl_setisl_map *set)
3372{
3373 return isl_map_free(set);
3374}
3375
3376void isl_set_print_internal(struct isl_setisl_map *set, FILE *out, int indent)
3377{
3378 int i;
3379
3380 if (!set) {
3381 fprintf(out, "null set\n");
3382 return;
3383 }
3384
3385 fprintf(out, "%*s", indent, "");
3386 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
3387 set->ref, set->n, set->dim->nparam, set->dim->n_out,
3388 set->flags);
3389 for (i = 0; i < set->n; ++i) {
3390 fprintf(out, "%*s", indent, "");
3391 fprintf(out, "basic set %d:\n", i);
3392 isl_basic_set_print_internal(set->p[i], out, indent+4);
3393 }
3394}
3395
3396void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
3397{
3398 int i;
3399
3400 if (!map) {
3401 fprintf(out, "null map\n");
3402 return;
3403 }
3404
3405 fprintf(out, "%*s", indent, "");
3406 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
3407 "flags: %x, n_name: %d\n",
3408 map->ref, map->n, map->dim->nparam, map->dim->n_in,
3409 map->dim->n_out, map->flags, map->dim->n_id);
3410 for (i = 0; i < map->n; ++i) {
3411 fprintf(out, "%*s", indent, "");
3412 fprintf(out, "basic map %d:\n", i);
3413 isl_basic_map_print_internal(map->p[i], out, indent+4);
3414 }
3415}
3416
3417struct isl_basic_map *isl_basic_map_intersect_domain(
3418 struct isl_basic_map *bmap, struct isl_basic_setisl_basic_map *bset)
3419{
3420 struct isl_basic_map *bmap_domain;
3421
3422 if (isl_basic_map_check_equal_params(bmap, bset_to_bmap(bset)) < 0)
3423 goto error;
3424
3425 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
3426 isl_assert(bset->ctx,do { if (isl_basic_map_compatible_domain(bmap, bset)) break; do
{ isl_handle_error(bset->ctx, isl_error_unknown, "Assertion \""
"isl_basic_map_compatible_domain(bmap, bset)" "\" failed", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 3427); goto error; } while (0); } while (0)
3427 isl_basic_map_compatible_domain(bmap, bset), goto error)do { if (isl_basic_map_compatible_domain(bmap, bset)) break; do
{ isl_handle_error(bset->ctx, isl_error_unknown, "Assertion \""
"isl_basic_map_compatible_domain(bmap, bset)" "\" failed", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 3427); goto error; } while (0); } while (0)
;
3428
3429 bmap = isl_basic_map_cow(bmap);
3430 if (!bmap)
3431 goto error;
3432 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
3433 bset->n_div, bset->n_eq, bset->n_ineq);
3434 bmap_domain = isl_basic_map_from_domain(bset);
3435 bmap = add_constraints(bmap, bmap_domain, 0, 0);
3436
3437 bmap = isl_basic_map_simplify(bmap);
3438 return isl_basic_map_finalize(bmap);
3439error:
3440 isl_basic_map_free(bmap);
3441 isl_basic_set_free(bset);
3442 return NULL((void*)0);
3443}
3444
3445/* Check that the space of "bset" is the same as that of the range of "bmap".
3446 */
3447static isl_stat isl_basic_map_check_compatible_range(
3448 __isl_keep isl_basic_map *bmap, __isl_keep isl_basic_setisl_basic_map *bset)
3449{
3450 isl_bool ok;
3451
3452 ok = isl_basic_map_compatible_range(bmap, bset);
3453 if (ok < 0)
3454 return isl_stat_error;
3455 if (!ok)
3456 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,do { isl_handle_error(isl_basic_set_get_ctx(bset), isl_error_invalid
, "incompatible spaces", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 3457); return isl_stat_error; } while (0)
3457 "incompatible spaces", return isl_stat_error)do { isl_handle_error(isl_basic_set_get_ctx(bset), isl_error_invalid
, "incompatible spaces", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 3457); return isl_stat_error; } while (0)
;
3458
3459 return isl_stat_ok;
3460}
3461
3462struct isl_basic_map *isl_basic_map_intersect_range(
3463 struct isl_basic_map *bmap, struct isl_basic_setisl_basic_map *bset)
3464{
3465 struct isl_basic_map *bmap_range;
3466
3467 if (isl_basic_map_check_equal_params(bmap, bset_to_bmap(bset)) < 0)
3468 goto error;
3469
3470 if (isl_space_dim(bset->dim, isl_dim_set) != 0 &&
3471 isl_basic_map_check_compatible_range(bmap, bset) < 0)
3472 goto error;
3473
3474 if (isl_basic_set_plain_is_universe(bset)) {
3475 isl_basic_set_free(bset);
3476 return bmap;
3477 }
3478
3479 bmap = isl_basic_map_cow(bmap);
3480 if (!bmap)
3481 goto error;
3482 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
3483 bset->n_div, bset->n_eq, bset->n_ineq);
3484 bmap_range = bset_to_bmap(bset);
3485 bmap = add_constraints(bmap, bmap_range, 0, 0);
3486
3487 bmap = isl_basic_map_simplify(bmap);
3488 return isl_basic_map_finalize(bmap);
3489error:
3490 isl_basic_map_free(bmap);
3491 isl_basic_set_free(bset);
3492 return NULL((void*)0);
3493}
3494
3495isl_bool isl_basic_map_contains(__isl_keep isl_basic_map *bmap,
3496 __isl_keep isl_vec *vec)
3497{
3498 int i;
3499 unsigned total;
3500 isl_int s;
3501
3502 if (!bmap || !vec)
3503 return isl_bool_error;
3504
3505 total = 1 + isl_basic_map_total_dim(bmap);
3506 if (total != vec->size)
3507 return isl_bool_false;
3508
3509 isl_int_init(s)isl_sioimath_init((s));
3510
3511 for (i = 0; i < bmap->n_eq; ++i) {
3512 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
3513 if (!isl_int_is_zero(s)(isl_sioimath_sgn(*(s)) == 0)) {
3514 isl_int_clear(s)isl_sioimath_clear((s));
3515 return isl_bool_false;
3516 }
3517 }
3518
3519 for (i = 0; i < bmap->n_ineq; ++i) {
3520 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
3521 if (isl_int_is_neg(s)(isl_sioimath_sgn(*(s)) < 0)) {
3522 isl_int_clear(s)isl_sioimath_clear((s));
3523 return isl_bool_false;
3524 }
3525 }
3526
3527 isl_int_clear(s)isl_sioimath_clear((s));
3528
3529 return isl_bool_true;
3530}
3531
3532isl_bool isl_basic_set_contains(__isl_keep isl_basic_setisl_basic_map *bset,
3533 __isl_keep isl_vec *vec)
3534{
3535 return isl_basic_map_contains(bset_to_bmap(bset), vec);
3536}
3537
3538struct isl_basic_map *isl_basic_map_intersect(
3539 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3540{
3541 struct isl_vec *sample = NULL((void*)0);
3542
3543 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
3544 goto error;
3545 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
3546 isl_space_dim(bmap1->dim, isl_dim_param) &&
3547 isl_space_dim(bmap2->dim, isl_dim_all) !=
3548 isl_space_dim(bmap2->dim, isl_dim_param))
3549 return isl_basic_map_intersect(bmap2, bmap1);
3550
3551 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
3552 isl_space_dim(bmap2->dim, isl_dim_param))
3553 isl_assert(bmap1->ctx,do { if (isl_space_is_equal(bmap1->dim, bmap2->dim)) break
; do { isl_handle_error(bmap1->ctx, isl_error_unknown, "Assertion \""
"isl_space_is_equal(bmap1->dim, bmap2->dim)" "\" failed"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 3554); goto error; } while (0); } while (0)
3554 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error)do { if (isl_space_is_equal(bmap1->dim, bmap2->dim)) break
; do { isl_handle_error(bmap1->ctx, isl_error_unknown, "Assertion \""
"isl_space_is_equal(bmap1->dim, bmap2->dim)" "\" failed"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 3554); goto error; } while (0); } while (0)
;
3555
3556 if (isl_basic_map_plain_is_empty(bmap1)) {
3557 isl_basic_map_free(bmap2);
3558 return bmap1;
3559 }
3560 if (isl_basic_map_plain_is_empty(bmap2)) {
3561 isl_basic_map_free(bmap1);
3562 return bmap2;
3563 }
3564
3565 if (bmap1->sample &&
3566 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
3567 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
3568 sample = isl_vec_copy(bmap1->sample);
3569 else if (bmap2->sample &&
3570 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
3571 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
3572 sample = isl_vec_copy(bmap2->sample);
3573
3574 bmap1 = isl_basic_map_cow(bmap1);
3575 if (!bmap1)
3576 goto error;
3577 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
3578 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
3579 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
3580
3581 if (!bmap1)
3582 isl_vec_free(sample);
3583 else if (sample) {
3584 isl_vec_free(bmap1->sample);
3585 bmap1->sample = sample;
3586 }
3587
3588 bmap1 = isl_basic_map_simplify(bmap1);
3589 return isl_basic_map_finalize(bmap1);
3590error:
3591 if (sample)
3592 isl_vec_free(sample);
3593 isl_basic_map_free(bmap1);
3594 isl_basic_map_free(bmap2);
3595 return NULL((void*)0);
3596}
3597
3598struct isl_basic_setisl_basic_map *isl_basic_set_intersect(
3599 struct isl_basic_setisl_basic_map *bset1, struct isl_basic_setisl_basic_map *bset2)
3600{
3601 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1),
3602 bset_to_bmap(bset2)));
3603}
3604
3605__isl_give isl_basic_setisl_basic_map *isl_basic_set_intersect_params(
3606 __isl_take isl_basic_setisl_basic_map *bset1, __isl_take isl_basic_setisl_basic_map *bset2)
3607{
3608 return isl_basic_set_intersect(bset1, bset2);
3609}
3610
3611/* Special case of isl_map_intersect, where both map1 and map2
3612 * are convex, without any divs and such that either map1 or map2
3613 * contains a single constraint. This constraint is then simply
3614 * added to the other map.
3615 */
3616static __isl_give isl_map *map_intersect_add_constraint(
3617 __isl_take isl_map *map1, __isl_take isl_map *map2)
3618{
3619 isl_assert(map1->ctx, map1->n == 1, goto error)do { if (map1->n == 1) break; do { isl_handle_error(map1->
ctx, isl_error_unknown, "Assertion \"" "map1->n == 1" "\" failed"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 3619); goto error; } while (0); } while (0)
;
3620 isl_assert(map2->ctx, map1->n == 1, goto error)do { if (map1->n == 1) break; do { isl_handle_error(map2->
ctx, isl_error_unknown, "Assertion \"" "map1->n == 1" "\" failed"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 3620); goto error; } while (0); } while (0)
;
3621 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error)do { if (map1->p[0]->n_div == 0) break; do { isl_handle_error
(map1->ctx, isl_error_unknown, "Assertion \"" "map1->p[0]->n_div == 0"
"\" failed", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 3621); goto error; } while (0); } while (0)
;
3622 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error)do { if (map1->p[0]->n_div == 0) break; do { isl_handle_error
(map2->ctx, isl_error_unknown, "Assertion \"" "map1->p[0]->n_div == 0"
"\" failed", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 3622); goto error; } while (0); } while (0)
;
3623
3624 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
3625 return isl_map_intersect(map2, map1);
3626
3627 map1 = isl_map_cow(map1);
3628 if (!map1)
3629 goto error;
3630 if (isl_map_plain_is_empty(map1)) {
3631 isl_map_free(map2);
3632 return map1;
3633 }
3634 map1->p[0] = isl_basic_map_cow(map1->p[0]);
3635 if (map2->p[0]->n_eq == 1)
3636 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
3637 else
3638 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
3639 map2->p[0]->ineq[0]);
3640
3641 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
3642 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
3643 if (!map1->p[0])
3644 goto error;
3645
3646 if (isl_basic_map_plain_is_empty(map1->p[0])) {
3647 isl_basic_map_free(map1->p[0]);
3648 map1->n = 0;
3649 }
3650
3651 isl_map_free(map2);
3652
3653 return map1;
3654error:
3655 isl_map_free(map1);
3656 isl_map_free(map2);
3657 return NULL((void*)0);
3658}
3659
3660/* map2 may be either a parameter domain or a map living in the same
3661 * space as map1.
3662 */
3663static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
3664 __isl_take isl_map *map2)
3665{
3666 unsigned flags = 0;
3667 isl_map *result;
3668 int i, j;
3669
3670 if (!map1 || !map2)
3671 goto error;
3672
3673 if ((isl_map_plain_is_empty(map1) ||
3674 isl_map_plain_is_universe(map2)) &&
3675 isl_space_is_equal(map1->dim, map2->dim)) {
3676 isl_map_free(map2);
3677 return map1;
3678 }
3679 if ((isl_map_plain_is_empty(map2) ||
3680 isl_map_plain_is_universe(map1)) &&
3681 isl_space_is_equal(map1->dim, map2->dim)) {
3682 isl_map_free(map1);
3683 return map2;
3684 }
3685
3686 if (map1->n == 1 && map2->n == 1 &&
3687 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
3688 isl_space_is_equal(map1->dim, map2->dim) &&
3689 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
3690 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
3691 return map_intersect_add_constraint(map1, map2);
3692
3693 if (isl_space_dim(map2->dim, isl_dim_all) !=
3694 isl_space_dim(map2->dim, isl_dim_param))
3695 isl_assert(map1->ctx,do { if (isl_space_is_equal(map1->dim, map2->dim)) break
; do { isl_handle_error(map1->ctx, isl_error_unknown, "Assertion \""
"isl_space_is_equal(map1->dim, map2->dim)" "\" failed"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 3696); goto error; } while (0); } while (0)
3696 isl_space_is_equal(map1->dim, map2->dim), goto error)do { if (isl_space_is_equal(map1->dim, map2->dim)) break
; do { isl_handle_error(map1->ctx, isl_error_unknown, "Assertion \""
"isl_space_is_equal(map1->dim, map2->dim)" "\" failed"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 3696); goto error; } while (0); } while (0)
;
3697
3698 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT)(!!(((map1)->flags) & ((1 << 0)))) &&
3699 ISL_F_ISSET(map2, ISL_MAP_DISJOINT)(!!(((map2)->flags) & ((1 << 0)))))
3700 ISL_FL_SET(flags, ISL_MAP_DISJOINT)((flags) |= ((1 << 0)));
3701
3702 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3703 map1->n * map2->n, flags);
3704 if (!result)
3705 goto error;
3706 for (i = 0; i < map1->n; ++i)
3707 for (j = 0; j < map2->n; ++j) {
3708 struct isl_basic_map *part;
3709 part = isl_basic_map_intersect(
3710 isl_basic_map_copy(map1->p[i]),
3711 isl_basic_map_copy(map2->p[j]));
3712 if (isl_basic_map_is_empty(part) < 0)
3713 part = isl_basic_map_free(part);
3714 result = isl_map_add_basic_map(result, part);
3715 if (!result)
3716 goto error;
3717 }
3718 isl_map_free(map1);
3719 isl_map_free(map2);
3720 return result;
3721error:
3722 isl_map_free(map1);
3723 isl_map_free(map2);
3724 return NULL((void*)0);
3725}
3726
3727static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
3728 __isl_take isl_map *map2)
3729{
3730 if (!map1 || !map2)
3731 goto error;
3732 if (!isl_space_is_equal(map1->dim, map2->dim))
3733 isl_die(isl_map_get_ctx(map1), isl_error_invalid,do { isl_handle_error(isl_map_get_ctx(map1), isl_error_invalid
, "spaces don't match", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 3734); goto error; } while (0)
3734 "spaces don't match", goto error)do { isl_handle_error(isl_map_get_ctx(map1), isl_error_invalid
, "spaces don't match", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 3734); goto error; } while (0)
;
3735 return map_intersect_internal(map1, map2);
3736error:
3737 isl_map_free(map1);
3738 isl_map_free(map2);
3739 return NULL((void*)0);
3740}
3741
3742__isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
3743 __isl_take isl_map *map2)
3744{
3745 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
3746}
3747
3748struct isl_setisl_map *isl_set_intersect(struct isl_setisl_map *set1, struct isl_setisl_map *set2)
3749{
3750 return set_from_map(isl_map_intersect(set_to_map(set1),
3751 set_to_map(set2)));
3752}
3753
3754/* map_intersect_internal accepts intersections
3755 * with parameter domains, so we can just call that function.
3756 */
3757static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
3758 __isl_take isl_setisl_map *params)
3759{
3760 return map_intersect_internal(map, params);
3761}
3762
3763__isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
3764 __isl_take isl_map *map2)
3765{
3766 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
3767}
3768
3769__isl_give isl_setisl_map *isl_set_intersect_params(__isl_take isl_setisl_map *set,
3770 __isl_take isl_setisl_map *params)
3771{
3772 return isl_map_intersect_params(set, params);
3773}
3774
3775struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
3776{
3777 isl_space *space;
3778 unsigned pos, n1, n2;
3779
3780 if (!bmap)
3781 return NULL((void*)0);
3782 bmap = isl_basic_map_cow(bmap);
3783 if (!bmap)
3784 return NULL((void*)0);
3785 space = isl_space_reverse(isl_space_copy(bmap->dim));
3786 pos = isl_basic_map_offset(bmap, isl_dim_in);
3787 n1 = isl_basic_map_dim(bmap, isl_dim_in);
3788 n2 = isl_basic_map_dim(bmap, isl_dim_out);
3789 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
3790 return isl_basic_map_reset_space(bmap, space);
3791}
3792
3793static __isl_give isl_basic_map *basic_map_space_reset(
3794 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
3795{
3796 isl_space *space;
3797
3798 if (!bmap)
3799 return NULL((void*)0);
3800 if (!isl_space_is_named_or_nested(bmap->dim, type))
3801 return bmap;
3802
3803 space = isl_basic_map_get_space(bmap);
3804 space = isl_space_reset(space, type);
3805 bmap = isl_basic_map_reset_space(bmap, space);
3806 return bmap;
3807}
3808
3809__isl_give isl_basic_map *isl_basic_map_insert_dims(
3810 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3811 unsigned pos, unsigned n)
3812{
3813 isl_bool rational;
3814 isl_space *res_dim;
3815 struct isl_basic_map *res;
3816 struct isl_dim_map *dim_map;
3817 unsigned total, off;
3818 enum isl_dim_type t;
3819
3820 if (n == 0)
3821 return basic_map_space_reset(bmap, type);
3822
3823 if (!bmap)
3824 return NULL((void*)0);
3825
3826 res_dim = isl_space_insert_dims(isl_basic_map_get_space(bmap), type, pos, n);
3827
3828 total = isl_basic_map_total_dim(bmap) + n;
3829 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3830 off = 0;
3831 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3832 if (t != type) {
3833 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3834 } else {
3835 unsigned size = isl_basic_map_dim(bmap, t);
3836 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3837 0, pos, off);
3838 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3839 pos, size - pos, off + pos + n);
3840 }
3841 off += isl_space_dim(res_dim, t);
3842 }
3843 isl_dim_map_div(dim_map, bmap, off);
3844
3845 res = isl_basic_map_alloc_space(res_dim,
3846 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3847 rational = isl_basic_map_is_rational(bmap);
3848 if (rational < 0)
3849 res = isl_basic_map_free(res);
3850 if (rational)
3851 res = isl_basic_map_set_rational(res);
3852 if (isl_basic_map_plain_is_empty(bmap)) {
3853 isl_basic_map_free(bmap);
3854 free(dim_map);
3855 return isl_basic_map_set_to_empty(res);
3856 }
3857 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3858 return isl_basic_map_finalize(res);
3859}
3860
3861__isl_give isl_basic_setisl_basic_map *isl_basic_set_insert_dims(
3862 __isl_take isl_basic_setisl_basic_map *bset,
3863 enum isl_dim_type type, unsigned pos, unsigned n)
3864{
3865 return isl_basic_map_insert_dims(bset, type, pos, n);
3866}
3867
3868__isl_give isl_basic_map *isl_basic_map_add_dims(__isl_take isl_basic_map *bmap,
3869 enum isl_dim_type type, unsigned n)
3870{
3871 if (!bmap)
3872 return NULL((void*)0);
3873 return isl_basic_map_insert_dims(bmap, type,
3874 isl_basic_map_dim(bmap, type), n);
3875}
3876
3877__isl_give isl_basic_setisl_basic_map *isl_basic_set_add_dims(__isl_take isl_basic_setisl_basic_map *bset,
3878 enum isl_dim_type type, unsigned n)
3879{
3880 if (!bset)
3881 return NULL((void*)0);
3882 isl_assert(bset->ctx, type != isl_dim_in, goto error)do { if (type != isl_dim_in) break; do { isl_handle_error(bset
->ctx, isl_error_unknown, "Assertion \"" "type != isl_dim_in"
"\" failed", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 3882); goto error; } while (0); } while (0)
;
3883 return isl_basic_map_add_dims(bset, type, n);
3884error:
3885 isl_basic_set_free(bset);
3886 return NULL((void*)0);
3887}
3888
3889static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
3890 enum isl_dim_type type)
3891{
3892 isl_space *space;
3893
3894 if (!map || !isl_space_is_named_or_nested(map->dim, type))
3895 return map;
3896
3897 space = isl_map_get_space(map);
3898 space = isl_space_reset(space, type);
3899 map = isl_map_reset_space(map, space);
3900 return map;
3901}
3902
3903__isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
3904 enum isl_dim_type type, unsigned pos, unsigned n)
3905{
3906 int i;
3907
3908 if (n == 0)
3909 return map_space_reset(map, type);
3910
3911 map = isl_map_cow(map);
3912 if (!map)
3913 return NULL((void*)0);
3914
3915 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
3916 if (!map->dim)
3917 goto error;
3918
3919 for (i = 0; i < map->n; ++i) {
3920 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
3921 if (!map->p[i])
3922 goto error;
3923 }
3924
3925 return map;
3926error:
3927 isl_map_free(map);
3928 return NULL((void*)0);
3929}
3930
3931__isl_give isl_setisl_map *isl_set_insert_dims(__isl_take isl_setisl_map *set,
3932 enum isl_dim_type type, unsigned pos, unsigned n)
3933{
3934 return isl_map_insert_dims(set, type, pos, n);
3935}
3936
3937__isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
3938 enum isl_dim_type type, unsigned n)
3939{
3940 if (!map)
3941 return NULL((void*)0);
3942 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
3943}
3944
3945__isl_give isl_setisl_map *isl_set_add_dims(__isl_take isl_setisl_map *set,
3946 enum isl_dim_type type, unsigned n)
3947{
3948 if (!set)
3949 return NULL((void*)0);
3950 isl_assert(set->ctx, type != isl_dim_in, goto error)do { if (type != isl_dim_in) break; do { isl_handle_error(set
->ctx, isl_error_unknown, "Assertion \"" "type != isl_dim_in"
"\" failed", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 3950); goto error; } while (0); } while (0)
;
3951 return set_from_map(isl_map_add_dims(set_to_map(set), type, n));
3952error:
3953 isl_set_free(set);
3954 return NULL((void*)0);
3955}
3956
3957__isl_give isl_basic_map *isl_basic_map_move_dims(
3958 __isl_take isl_basic_map *bmap,
3959 enum isl_dim_type dst_type, unsigned dst_pos,
3960 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3961{
3962 struct isl_dim_map *dim_map;
3963 struct isl_basic_map *res;
3964 enum isl_dim_type t;
3965 unsigned total, off;
3966
3967 if (!bmap)
3968 return NULL((void*)0);
3969 if (n == 0)
3970 return bmap;
3971
3972 if (isl_basic_map_check_range(bmap, src_type, src_pos, n) < 0)
3973 return isl_basic_map_free(bmap);
3974
3975 if (dst_type == src_type && dst_pos == src_pos)
3976 return bmap;
3977
3978 isl_assert(bmap->ctx, dst_type != src_type, goto error)do { if (dst_type != src_type) break; do { isl_handle_error(bmap
->ctx, isl_error_unknown, "Assertion \"" "dst_type != src_type"
"\" failed", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 3978); goto error; } while (0); } while (0)
;
3979
3980 if (pos(bmap->dim, dst_type) + dst_pos ==
3981 pos(bmap->dim, src_type) + src_pos +
3982 ((src_type < dst_type) ? n : 0)) {
3983 bmap = isl_basic_map_cow(bmap);
3984 if (!bmap)
3985 return NULL((void*)0);
3986
3987 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3988 src_type, src_pos, n);
3989 if (!bmap->dim)
3990 goto error;
3991
3992 bmap = isl_basic_map_finalize(bmap);
3993
3994 return bmap;
3995 }
3996
3997 total = isl_basic_map_total_dim(bmap);
3998 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3999
4000 off = 0;
4001 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
4002 unsigned size = isl_space_dim(bmap->dim, t);
4003 if (t == dst_type) {
4004 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4005 0, dst_pos, off);
4006 off += dst_pos;
4007 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
4008 src_pos, n, off);
4009 off += n;
4010 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4011 dst_pos, size - dst_pos, off);
4012 off += size - dst_pos;
4013 } else if (t == src_type) {
4014 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4015 0, src_pos, off);
4016 off += src_pos;
4017 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4018 src_pos + n, size - src_pos - n, off);
4019 off += size - src_pos - n;
4020 } else {
4021 isl_dim_map_dim(dim_map, bmap->dim, t, off);
4022 off += size;
4023 }
4024 }
4025 isl_dim_map_div(dim_map, bmap, off);
4026
4027 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
4028 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4029 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4030 if (!bmap)
4031 goto error;
4032
4033 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
4034 src_type, src_pos, n);
4035 if (!bmap->dim)
4036 goto error;
4037
4038 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED)(((bmap)->flags) &= ~((1 << 5)));
4039 bmap = isl_basic_map_gauss(bmap, NULL((void*)0));
4040 bmap = isl_basic_map_finalize(bmap);
4041
4042 return bmap;
4043error:
4044 isl_basic_map_free(bmap);
4045 return NULL((void*)0);
4046}
4047
4048__isl_give isl_basic_setisl_basic_map *isl_basic_set_move_dims(__isl_take isl_basic_setisl_basic_map *bset,
4049 enum isl_dim_type dst_type, unsigned dst_pos,
4050 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4051{
4052 isl_basic_map *bmap = bset_to_bmap(bset);
4053 bmap = isl_basic_map_move_dims(bmap, dst_type, dst_pos,
4054 src_type, src_pos, n);
4055 return bset_from_bmap(bmap);
4056}
4057
4058__isl_give isl_setisl_map *isl_set_move_dims(__isl_take isl_setisl_map *set,
4059 enum isl_dim_type dst_type, unsigned dst_pos,
4060 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4061{
4062 if (!set)
4063 return NULL((void*)0);
4064 isl_assert(set->ctx, dst_type != isl_dim_in, goto error)do { if (dst_type != isl_dim_in) break; do { isl_handle_error
(set->ctx, isl_error_unknown, "Assertion \"" "dst_type != isl_dim_in"
"\" failed", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 4064); goto error; } while (0); } while (0)
;
4065 return set_from_map(isl_map_move_dims(set_to_map(set),
4066 dst_type, dst_pos, src_type, src_pos, n));
4067error:
4068 isl_set_free(set);
4069 return NULL((void*)0);
4070}
4071
4072__isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
4073 enum isl_dim_type dst_type, unsigned dst_pos,
4074 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4075{
4076 int i;
4077
4078 if (!map)
4079 return NULL((void*)0);
4080 if (n == 0)
4081 return map;
4082
4083 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),do { if (src_pos + n <= isl_map_dim(map, src_type)) break;
do { isl_handle_error(map->ctx, isl_error_unknown, "Assertion \""
"src_pos + n <= isl_map_dim(map, src_type)" "\" failed", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 4084); goto error; } while (0); } while (0)
4084 goto error)do { if (src_pos + n <= isl_map_dim(map, src_type)) break;
do { isl_handle_error(map->ctx, isl_error_unknown, "Assertion \""
"src_pos + n <= isl_map_dim(map, src_type)" "\" failed", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 4084); goto error; } while (0); } while (0)
;
4085
4086 if (dst_type == src_type && dst_pos == src_pos)
4087 return map;
4088
4089 isl_assert(map->ctx, dst_type != src_type, goto error)do { if (dst_type != src_type) break; do { isl_handle_error(map
->ctx, isl_error_unknown, "Assertion \"" "dst_type != src_type"
"\" failed", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 4089); goto error; } while (0); } while (0)
;
4090
4091 map = isl_map_cow(map);
4092 if (!map)
4093 return NULL((void*)0);
4094
4095 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
4096 if (!map->dim)
4097 goto error;
4098
4099 for (i = 0; i < map->n; ++i) {
4100 map->p[i] = isl_basic_map_move_dims(map->p[i],
4101 dst_type, dst_pos,
4102 src_type, src_pos, n);
4103 if (!map->p[i])
4104 goto error;
4105 }
4106
4107 return map;
4108error:
4109 isl_map_free(map);
4110 return NULL((void*)0);
4111}
4112
4113/* Move the specified dimensions to the last columns right before
4114 * the divs. Don't change the dimension specification of bmap.
4115 * That's the responsibility of the caller.
4116 */
4117static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
4118 enum isl_dim_type type, unsigned first, unsigned n)
4119{
4120 struct isl_dim_map *dim_map;
4121 struct isl_basic_map *res;
4122 enum isl_dim_type t;
4123 unsigned total, off;
4124
4125 if (!bmap)
4126 return NULL((void*)0);
4127 if (pos(bmap->dim, type) + first + n ==
4128 1 + isl_space_dim(bmap->dim, isl_dim_all))
4129 return bmap;
4130
4131 total = isl_basic_map_total_dim(bmap);
4132 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4133
4134 off = 0;
4135 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
4136 unsigned size = isl_space_dim(bmap->dim, t);
4137 if (t == type) {
4138 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4139 0, first, off);
4140 off += first;
4141 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4142 first, n, total - bmap->n_div - n);
4143 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4144 first + n, size - (first + n), off);
4145 off += size - (first + n);
4146 } else {
4147 isl_dim_map_dim(dim_map, bmap->dim, t, off);
4148 off += size;
4149 }
4150 }
4151 isl_dim_map_div(dim_map, bmap, off + n);
4152
4153 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
4154 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4155 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4156 return res;
4157}
4158
4159/* Insert "n" rows in the divs of "bmap".
4160 *
4161 * The number of columns is not changed, which means that the last
4162 * dimensions of "bmap" are being reintepreted as the new divs.
4163 * The space of "bmap" is not adjusted, however, which means
4164 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
4165 * from the space of "bmap" is the responsibility of the caller.
4166 */
4167static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
4168 int n)
4169{
4170 int i;
4171 size_t row_size;
4172 isl_int **new_div;
4173 isl_int *old;
4174
4175 bmap = isl_basic_map_cow(bmap);
4176 if (!bmap)
4177 return NULL((void*)0);
4178
4179 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
4180 old = bmap->block2.data;
4181 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
4182 (bmap->extra + n) * (1 + row_size));
4183 if (!bmap->block2.data)
4184 return isl_basic_map_free(bmap);
4185 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n)((isl_int * *)isl_malloc_or_die(bmap->ctx, (bmap->extra
+ n)*sizeof(isl_int *)))
;
4186 if (!new_div)
4187 return isl_basic_map_free(bmap);
4188 for (i = 0; i < n; ++i) {
4189 new_div[i] = bmap->block2.data +
4190 (bmap->extra + i) * (1 + row_size);
4191 isl_seq_clr(new_div[i], 1 + row_size);
4192 }
4193 for (i = 0; i < bmap->extra; ++i)
4194 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
4195 free(bmap->div);
4196 bmap->div = new_div;
4197 bmap->n_div += n;
4198 bmap->extra += n;
4199
4200 return bmap;
4201}
4202
4203/* Drop constraints from "bmap" that only involve the variables
4204 * of "type" in the range [first, first + n] that are not related
4205 * to any of the variables outside that interval.
4206 * These constraints cannot influence the values for the variables
4207 * outside the interval, except in case they cause "bmap" to be empty.
4208 * Only drop the constraints if "bmap" is known to be non-empty.
4209 */
4210static __isl_give isl_basic_map *drop_irrelevant_constraints(
4211 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
4212 unsigned first, unsigned n)
4213{
4214 int i;
4215 int *groups;
4216 unsigned dim, n_div;
4217 isl_bool non_empty;
4218
4219 non_empty = isl_basic_map_plain_is_non_empty(bmap);
4220 if (non_empty < 0)
4221 return isl_basic_map_free(bmap);
4222 if (!non_empty)
4223 return bmap;
4224
4225 dim = isl_basic_map_dim(bmap, isl_dim_all);
4226 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4227 groups = isl_calloc_array(isl_basic_map_get_ctx(bmap), int, dim)((int *)isl_calloc_or_die(isl_basic_map_get_ctx(bmap), dim, sizeof
(int)))
;
4228 if (!groups)
4229 return isl_basic_map_free(bmap);
4230 first += isl_basic_map_offset(bmap, type) - 1;
4231 for (i = 0; i < first; ++i)
4232 groups[i] = -1;
4233 for (i = first + n; i < dim - n_div; ++i)
4234 groups[i] = -1;
4235
4236 bmap = isl_basic_map_drop_unrelated_constraints(bmap, groups);
4237
4238 return bmap;
4239}
4240
4241/* Turn the n dimensions of type type, starting at first
4242 * into existentially quantified variables.
4243 *
4244 * If a subset of the projected out variables are unrelated
4245 * to any of the variables that remain, then the constraints
4246 * involving this subset are simply dropped first.
4247 */
4248__isl_give isl_basic_map *isl_basic_map_project_out(
4249 __isl_take isl_basic_map *bmap,
4250 enum isl_dim_type type, unsigned first, unsigned n)
4251{
4252 isl_bool empty;
4253
4254 if (n == 0)
4255 return basic_map_space_reset(bmap, type);
4256 if (type == isl_dim_div)
4257 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,do { isl_handle_error(isl_basic_map_get_ctx(bmap), isl_error_invalid
, "cannot project out existentially quantified variables", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 4259); return isl_basic_map_free(bmap); } while (0)
4258 "cannot project out existentially quantified variables",do { isl_handle_error(isl_basic_map_get_ctx(bmap), isl_error_invalid
, "cannot project out existentially quantified variables", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 4259); return isl_basic_map_free(bmap); } while (0)
4259 return isl_basic_map_free(bmap))do { isl_handle_error(isl_basic_map_get_ctx(bmap), isl_error_invalid
, "cannot project out existentially quantified variables", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 4259); return isl_basic_map_free(bmap); } while (0)
;
4260
4261 empty = isl_basic_map_plain_is_empty(bmap);
4262 if (empty < 0)
4263 return isl_basic_map_free(bmap);
4264 if (empty)
4265 bmap = isl_basic_map_set_to_empty(bmap);
4266
4267 bmap = drop_irrelevant_constraints(bmap, type, first, n);
4268 if (!bmap)
4269 return NULL((void*)0);
4270
4271 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)(!!(((bmap)->flags) & ((1 << 4)))))
4272 return isl_basic_map_remove_dims(bmap, type, first, n);
4273
4274 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
4275 return isl_basic_map_free(bmap);
4276
4277 bmap = move_last(bmap, type, first, n);
4278 bmap = isl_basic_map_cow(bmap);
4279 bmap = insert_div_rows(bmap, n);
4280 if (!bmap)
4281 return NULL((void*)0);
4282
4283 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
4284 if (!bmap->dim)
4285 goto error;
4286 bmap = isl_basic_map_simplify(bmap);
4287 bmap = isl_basic_map_drop_redundant_divs(bmap);
4288 return isl_basic_map_finalize(bmap);
4289error:
4290 isl_basic_map_free(bmap);
4291 return NULL((void*)0);
4292}
4293
4294/* Turn the n dimensions of type type, starting at first
4295 * into existentially quantified variables.
4296 */
4297struct isl_basic_setisl_basic_map *isl_basic_set_project_out(struct isl_basic_setisl_basic_map *bset,
4298 enum isl_dim_type type, unsigned first, unsigned n)
4299{
4300 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset),
4301 type, first, n));
4302}
4303
4304/* Turn the n dimensions of type type, starting at first
4305 * into existentially quantified variables.
4306 */
4307__isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
4308 enum isl_dim_type type, unsigned first, unsigned n)
4309{
4310 int i;
4311
4312 if (!map)
4313 return NULL((void*)0);
4314
4315 if (n == 0)
4316 return map_space_reset(map, type);
4317
4318 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error)do { if (first + n <= isl_map_dim(map, type)) break; do { isl_handle_error
(map->ctx, isl_error_unknown, "Assertion \"" "first + n <= isl_map_dim(map, type)"
"\" failed", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 4318); goto error; } while (0); } while (0)
;
4319
4320 map = isl_map_cow(map);
4321 if (!map)
4322 return NULL((void*)0);
4323
4324 map->dim = isl_space_drop_dims(map->dim, type, first, n);
4325 if (!map->dim)
4326 goto error;
4327
4328 for (i = 0; i < map->n; ++i) {
4329 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
4330 if (!map->p[i])
4331 goto error;
4332 }
4333
4334 return map;
4335error:
4336 isl_map_free(map);
4337 return NULL((void*)0);
4338}
4339
4340/* Turn the n dimensions of type type, starting at first
4341 * into existentially quantified variables.
4342 */
4343__isl_give isl_setisl_map *isl_set_project_out(__isl_take isl_setisl_map *set,
4344 enum isl_dim_type type, unsigned first, unsigned n)
4345{
4346 return set_from_map(isl_map_project_out(set_to_map(set),
4347 type, first, n));
4348}
4349
4350/* Return a map that projects the elements in "set" onto their
4351 * "n" set dimensions starting at "first".
4352 * "type" should be equal to isl_dim_set.
4353 */
4354__isl_give isl_map *isl_set_project_onto_map(__isl_take isl_setisl_map *set,
4355 enum isl_dim_type type, unsigned first, unsigned n)
4356{
4357 int i;
4358 int dim;
4359 isl_map *map;
4360
4361 if (!set)
4362 return NULL((void*)0);
4363 if (type != isl_dim_set)
4364 isl_die(isl_set_get_ctx(set), isl_error_invalid,do { isl_handle_error(isl_set_get_ctx(set), isl_error_invalid
, "only set dimensions can be projected out", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 4365); goto error; } while (0)
4365 "only set dimensions can be projected out", goto error)do { isl_handle_error(isl_set_get_ctx(set), isl_error_invalid
, "only set dimensions can be projected out", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 4365); goto error; } while (0)
;
4366 dim = isl_set_dim(set, isl_dim_set);
4367 if (first + n > dim || first + n < first)
4368 isl_die(isl_set_get_ctx(set), isl_error_invalid,do { isl_handle_error(isl_set_get_ctx(set), isl_error_invalid
, "index out of bounds", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 4369); goto error; } while (0)
4369 "index out of bounds", goto error)do { isl_handle_error(isl_set_get_ctx(set), isl_error_invalid
, "index out of bounds", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 4369); goto error; } while (0)
;
4370
4371 map = isl_map_from_domain(set);
4372 map = isl_map_add_dims(map, isl_dim_out, n);
4373 for (i = 0; i < n; ++i)
4374 map = isl_map_equate(map, isl_dim_in, first + i,
4375 isl_dim_out, i);
4376 return map;
4377error:
4378 isl_set_free(set);
4379 return NULL((void*)0);
4380}
4381
4382static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
4383{
4384 int i, j;
4385
4386 for (i = 0; i < n; ++i) {
4387 j = isl_basic_map_alloc_div(bmap);
4388 if (j < 0)
4389 goto error;
4390 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
4391 }
4392 return bmap;
4393error:
4394 isl_basic_map_free(bmap);
4395 return NULL((void*)0);
4396}
4397
4398struct isl_basic_map *isl_basic_map_apply_range(
4399 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4400{
4401 isl_space *dim_result = NULL((void*)0);
4402 struct isl_basic_map *bmap;
4403 unsigned n_in, n_out, n, nparam, total, pos;
4404 struct isl_dim_map *dim_map1, *dim_map2;
4405
4406 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
4407 goto error;
4408 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_out,
4409 bmap2->dim, isl_dim_in))
4410 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,do { isl_handle_error(isl_basic_map_get_ctx(bmap1), isl_error_invalid
, "spaces don't match", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 4411); goto error; } while (0)
4411 "spaces don't match", goto error)do { isl_handle_error(isl_basic_map_get_ctx(bmap1), isl_error_invalid
, "spaces don't match", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 4411); goto error; } while (0)
;
4412
4413 dim_result = isl_space_join(isl_space_copy(bmap1->dim),
4414 isl_space_copy(bmap2->dim));
4415
4416 n_in = isl_basic_map_dim(bmap1, isl_dim_in);
4417 n_out = isl_basic_map_dim(bmap2, isl_dim_out);
4418 n = isl_basic_map_dim(bmap1, isl_dim_out);
4419 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
4420
4421 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
4422 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4423 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
4424 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4425 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
4426 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4427 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
4428 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
4429 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4430 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4431 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4432
4433 bmap = isl_basic_map_alloc_space(dim_result,
4434 bmap1->n_div + bmap2->n_div + n,
4435 bmap1->n_eq + bmap2->n_eq,
4436 bmap1->n_ineq + bmap2->n_ineq);
4437 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4438 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4439 bmap = add_divs(bmap, n);
4440 bmap = isl_basic_map_simplify(bmap);
4441 bmap = isl_basic_map_drop_redundant_divs(bmap);
4442 return isl_basic_map_finalize(bmap);
4443error:
4444 isl_basic_map_free(bmap1);
4445 isl_basic_map_free(bmap2);
4446 return NULL((void*)0);
4447}
4448
4449struct isl_basic_setisl_basic_map *isl_basic_set_apply(
4450 struct isl_basic_setisl_basic_map *bset, struct isl_basic_map *bmap)
4451{
4452 if (!bset || !bmap)
4453 goto error;
4454
4455 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),do { if (isl_basic_map_compatible_domain(bmap, bset)) break; do
{ isl_handle_error(bset->ctx, isl_error_unknown, "Assertion \""
"isl_basic_map_compatible_domain(bmap, bset)" "\" failed", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 4456); goto error; } while (0); } while (0)
4456 goto error)do { if (isl_basic_map_compatible_domain(bmap, bset)) break; do
{ isl_handle_error(bset->ctx, isl_error_unknown, "Assertion \""
"isl_basic_map_compatible_domain(bmap, bset)" "\" failed", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 4456); goto error; } while (0); } while (0)
;
4457
4458 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset),
4459 bmap));
4460error:
4461 isl_basic_set_free(bset);
4462 isl_basic_map_free(bmap);
4463 return NULL((void*)0);
4464}
4465
4466struct isl_basic_map *isl_basic_map_apply_domain(
4467 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4468{
4469 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
4470 goto error;
4471 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_in,
4472 bmap2->dim, isl_dim_in))
4473 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,do { isl_handle_error(isl_basic_map_get_ctx(bmap1), isl_error_invalid
, "spaces don't match", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 4474); goto error; } while (0)
4474 "spaces don't match", goto error)do { isl_handle_error(isl_basic_map_get_ctx(bmap1), isl_error_invalid
, "spaces don't match", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 4474); goto error; } while (0)
;
4475
4476 bmap1 = isl_basic_map_reverse(bmap1);
4477 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
4478 return isl_basic_map_reverse(bmap1);
4479error:
4480 isl_basic_map_free(bmap1);
4481 isl_basic_map_free(bmap2);
4482 return NULL((void*)0);
4483}
4484
4485/* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
4486 * A \cap B -> f(A) + f(B)
4487 */
4488struct isl_basic_map *isl_basic_map_sum(
4489 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4490{
4491 unsigned n_in, n_out, nparam, total, pos;
4492 struct isl_basic_map *bmap = NULL((void*)0);
4493 struct isl_dim_map *dim_map1, *dim_map2;
4494 int i;
4495
4496 if (!bmap1 || !bmap2)
4497 goto error;
4498
4499 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),do { if (isl_space_is_equal(bmap1->dim, bmap2->dim)) break
; do { isl_handle_error(bmap1->ctx, isl_error_unknown, "Assertion \""
"isl_space_is_equal(bmap1->dim, bmap2->dim)" "\" failed"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 4500); goto error; } while (0); } while (0)
4500 goto error)do { if (isl_space_is_equal(bmap1->dim, bmap2->dim)) break
; do { isl_handle_error(bmap1->ctx, isl_error_unknown, "Assertion \""
"isl_space_is_equal(bmap1->dim, bmap2->dim)" "\" failed"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 4500); goto error; } while (0); } while (0)
;
4501
4502 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
4503 n_in = isl_basic_map_dim(bmap1, isl_dim_in);
4504 n_out = isl_basic_map_dim(bmap1, isl_dim_out);
4505
4506 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
4507 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4508 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
4509 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4510 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
4511 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4512 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4513 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
4514 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4515 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4516 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
4517
4518 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
4519 bmap1->n_div + bmap2->n_div + 2 * n_out,
4520 bmap1->n_eq + bmap2->n_eq + n_out,
4521 bmap1->n_ineq + bmap2->n_ineq);
4522 for (i = 0; i < n_out; ++i) {
4523 int j = isl_basic_map_alloc_equality(bmap);
4524 if (j < 0)
4525 goto error;
4526 isl_seq_clr(bmap->eq[j], 1+total);
4527 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1)isl_sioimath_set_si((bmap->eq[j][1+nparam+n_in+i]), -1);
4528 isl_int_set_si(bmap->eq[j][1+pos+i], 1)isl_sioimath_set_si((bmap->eq[j][1+pos+i]), 1);
4529 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1)isl_sioimath_set_si((bmap->eq[j][1+pos-n_out+i]), 1);
4530 }
4531 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4532 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4533 bmap = add_divs(bmap, 2 * n_out);
4534
4535 bmap = isl_basic_map_simplify(bmap);
4536 return isl_basic_map_finalize(bmap);
4537error:
4538 isl_basic_map_free(bmap);
4539 isl_basic_map_free(bmap1);
4540 isl_basic_map_free(bmap2);
4541 return NULL((void*)0);
4542}
4543
4544/* Given two maps A -> f(A) and B -> g(B), construct a map
4545 * A \cap B -> f(A) + f(B)
4546 */
4547struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
4548{
4549 struct isl_map *result;
4550 int i, j;
4551
4552 if (!map1 || !map2)
4553 goto error;
4554
4555 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error)do { if (isl_space_is_equal(map1->dim, map2->dim)) break
; do { isl_handle_error(map1->ctx, isl_error_unknown, "Assertion \""
"isl_space_is_equal(map1->dim, map2->dim)" "\" failed"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 4555); goto error; } while (0); } while (0)
;
4556
4557 result = isl_map_alloc_space(isl_space_copy(map1->dim),
4558 map1->n * map2->n, 0);
4559 if (!result)
4560 goto error;
4561 for (i = 0; i < map1->n; ++i)
4562 for (j = 0; j < map2->n; ++j) {
4563 struct isl_basic_map *part;
4564 part = isl_basic_map_sum(
4565 isl_basic_map_copy(map1->p[i]),
4566 isl_basic_map_copy(map2->p[j]));
4567 if (isl_basic_map_is_empty(part))
4568 isl_basic_map_free(part);
4569 else
4570 result = isl_map_add_basic_map(result, part);
4571 if (!result)
4572 goto error;
4573 }
4574 isl_map_free(map1);
4575 isl_map_free(map2);
4576 return result;
4577error:
4578 isl_map_free(map1);
4579 isl_map_free(map2);
4580 return NULL((void*)0);
4581}
4582
4583__isl_give isl_setisl_map *isl_set_sum(__isl_take isl_setisl_map *set1,
4584 __isl_take isl_setisl_map *set2)
4585{
4586 return set_from_map(isl_map_sum(set_to_map(set1), set_to_map(set2)));
4587}
4588
4589/* Given a basic map A -> f(A), construct A -> -f(A).
4590 */
4591struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
4592{
4593 int i, j;
4594 unsigned off, n;
4595
4596 bmap = isl_basic_map_cow(bmap);
4597 if (!bmap)
4598 return NULL((void*)0);
4599
4600 n = isl_basic_map_dim(bmap, isl_dim_out);
4601 off = isl_basic_map_offset(bmap, isl_dim_out);
4602 for (i = 0; i < bmap->n_eq; ++i)
4603 for (j = 0; j < n; ++j)
4604 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j])isl_sioimath_neg((bmap->eq[i][off+j]), *(bmap->eq[i][off
+j]))
;
4605 for (i = 0; i < bmap->n_ineq; ++i)
4606 for (j = 0; j < n; ++j)
4607 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j])isl_sioimath_neg((bmap->ineq[i][off+j]), *(bmap->ineq[i
][off+j]))
;
4608 for (i = 0; i < bmap->n_div; ++i)
4609 for (j = 0; j < n; ++j)
4610 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j])isl_sioimath_neg((bmap->div[i][1+off+j]), *(bmap->div[i
][1+off+j]))
;
4611 bmap = isl_basic_map_gauss(bmap, NULL((void*)0));
4612 return isl_basic_map_finalize(bmap);
4613}
4614
4615__isl_give isl_basic_setisl_basic_map *isl_basic_set_neg(__isl_take isl_basic_setisl_basic_map *bset)
4616{
4617 return isl_basic_map_neg(bset);
4618}
4619
4620/* Given a map A -> f(A), construct A -> -f(A).
4621 */
4622struct isl_map *isl_map_neg(struct isl_map *map)
4623{
4624 int i;
4625
4626 map = isl_map_cow(map);
4627 if (!map)
4628 return NULL((void*)0);
4629
4630 for (i = 0; i < map->n; ++i) {
4631 map->p[i] = isl_basic_map_neg(map->p[i]);
4632 if (!map->p[i])
4633 goto error;
4634 }
4635
4636 return map;
4637error:
4638 isl_map_free(map);
4639 return NULL((void*)0);
4640}
4641
4642__isl_give isl_setisl_map *isl_set_neg(__isl_take isl_setisl_map *set)
4643{
4644 return set_from_map(isl_map_neg(set_to_map(set)));
4645}
4646
4647/* Given a basic map A -> f(A) and an integer d, construct a basic map
4648 * A -> floor(f(A)/d).
4649 */
4650struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
4651 isl_int d)
4652{
4653 unsigned n_in, n_out, nparam, total, pos;
4654 struct isl_basic_map *result = NULL((void*)0);
4655 struct isl_dim_map *dim_map;
4656 int i;
4657
4658 if (!bmap)
4659 return NULL((void*)0);
4660
4661 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4662 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4663 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4664
4665 total = nparam + n_in + n_out + bmap->n_div + n_out;
4666 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4667 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
4668 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
4669 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
4670 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
4671
4672 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
4673 bmap->n_div + n_out,
4674 bmap->n_eq, bmap->n_ineq + 2 * n_out);
4675 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
4676 result = add_divs(result, n_out);
4677 for (i = 0; i < n_out; ++i) {
4678 int j;
4679 j = isl_basic_map_alloc_inequality(result);
4680 if (j < 0)
4681 goto error;
4682 isl_seq_clr(result->ineq[j], 1+total);
4683 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d)isl_sioimath_neg((result->ineq[j][1+nparam+n_in+i]), *(d));
4684 isl_int_set_si(result->ineq[j][1+pos+i], 1)isl_sioimath_set_si((result->ineq[j][1+pos+i]), 1);
4685 j = isl_basic_map_alloc_inequality(result);
4686 if (j < 0)
4687 goto error;
4688 isl_seq_clr(result->ineq[j], 1+total);
4689 isl_int_set(result->ineq[j][1+nparam+n_in+i], d)isl_sioimath_set((result->ineq[j][1+nparam+n_in+i]), *(d));
4690 isl_int_set_si(result->ineq[j][1+pos+i], -1)isl_sioimath_set_si((result->ineq[j][1+pos+i]), -1);
4691 isl_int_sub_ui(result->ineq[j][0], d, 1)isl_sioimath_sub_ui((result->ineq[j][0]), *(d), 1);
4692 }
4693
4694 result = isl_basic_map_simplify(result);
4695 return isl_basic_map_finalize(result);
4696error:
4697 isl_basic_map_free(result);
4698 return NULL((void*)0);
4699}
4700
4701/* Given a map A -> f(A) and an integer d, construct a map
4702 * A -> floor(f(A)/d).
4703 */
4704struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
4705{
4706 int i;
4707
4708 map = isl_map_cow(map);
4709 if (!map)
4710 return NULL((void*)0);
4711
4712 ISL_F_CLR(map, ISL_MAP_DISJOINT)(((map)->flags) &= ~((1 << 0)));
4713 ISL_F_CLR(map, ISL_MAP_NORMALIZED)(((map)->flags) &= ~((1 << 1)));
4714 for (i = 0; i < map->n; ++i) {
4715 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
4716 if (!map->p[i])
4717 goto error;
4718 }
4719
4720 return map;
4721error:
4722 isl_map_free(map);
4723 return NULL((void*)0);
4724}
4725
4726/* Given a map A -> f(A) and an integer d, construct a map
4727 * A -> floor(f(A)/d).
4728 */
4729__isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
4730 __isl_take isl_val *d)
4731{
4732 if (!map || !d)
4733 goto error;
4734 if (!isl_val_is_int(d))
4735 isl_die(isl_val_get_ctx(d), isl_error_invalid,do { isl_handle_error(isl_val_get_ctx(d), isl_error_invalid, "expecting integer denominator"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 4736); goto error; } while (0)
4736 "expecting integer denominator", goto error)do { isl_handle_error(isl_val_get_ctx(d), isl_error_invalid, "expecting integer denominator"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 4736); goto error; } while (0)
;
4737 map = isl_map_floordiv(map, d->n);
4738 isl_val_free(d);
4739 return map;
4740error:
4741 isl_map_free(map);
4742 isl_val_free(d);
4743 return NULL((void*)0);
4744}
4745
4746static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
4747{
4748 int i;
4749 unsigned nparam;
4750 unsigned n_in;
4751
4752 i = isl_basic_map_alloc_equality(bmap);
4753 if (i < 0)
4754 goto error;
4755 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4756 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4757 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
4758 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1)isl_sioimath_set_si((bmap->eq[i][1+nparam+pos]), -1);
4759 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1)isl_sioimath_set_si((bmap->eq[i][1+nparam+n_in+pos]), 1);
4760 return isl_basic_map_finalize(bmap);
4761error:
4762 isl_basic_map_free(bmap);
4763 return NULL((void*)0);
4764}
4765
4766/* Add a constraint to "bmap" expressing i_pos < o_pos
4767 */
4768static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
4769{
4770 int i;
4771 unsigned nparam;
4772 unsigned n_in;
4773
4774 i = isl_basic_map_alloc_inequality(bmap);
4775 if (i < 0)
4776 goto error;
4777 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4778 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4779 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4780 isl_int_set_si(bmap->ineq[i][0], -1)isl_sioimath_set_si((bmap->ineq[i][0]), -1);
4781 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1)isl_sioimath_set_si((bmap->ineq[i][1+nparam+pos]), -1);
4782 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1)isl_sioimath_set_si((bmap->ineq[i][1+nparam+n_in+pos]), 1);
4783 return isl_basic_map_finalize(bmap);
4784error:
4785 isl_basic_map_free(bmap);
4786 return NULL((void*)0);
4787}
4788
4789/* Add a constraint to "bmap" expressing i_pos <= o_pos
4790 */
4791static __isl_give isl_basic_map *var_less_or_equal(
4792 __isl_take isl_basic_map *bmap, unsigned pos)
4793{
4794 int i;
4795 unsigned nparam;
4796 unsigned n_in;
4797
4798 i = isl_basic_map_alloc_inequality(bmap);
4799 if (i < 0)
4800 goto error;
4801 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4802 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4803 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4804 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1)isl_sioimath_set_si((bmap->ineq[i][1+nparam+pos]), -1);
4805 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1)isl_sioimath_set_si((bmap->ineq[i][1+nparam+n_in+pos]), 1);
4806 return isl_basic_map_finalize(bmap);
4807error:
4808 isl_basic_map_free(bmap);
4809 return NULL((void*)0);
4810}
4811
4812/* Add a constraint to "bmap" expressing i_pos > o_pos
4813 */
4814static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
4815{
4816 int i;
4817 unsigned nparam;
4818 unsigned n_in;
4819
4820 i = isl_basic_map_alloc_inequality(bmap);
4821 if (i < 0)
4822 goto error;
4823 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4824 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4825 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4826 isl_int_set_si(bmap->ineq[i][0], -1)isl_sioimath_set_si((bmap->ineq[i][0]), -1);
4827 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1)isl_sioimath_set_si((bmap->ineq[i][1+nparam+pos]), 1);
4828 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1)isl_sioimath_set_si((bmap->ineq[i][1+nparam+n_in+pos]), -1
)
;
4829 return isl_basic_map_finalize(bmap);
4830error:
4831 isl_basic_map_free(bmap);
4832 return NULL((void*)0);
4833}
4834
4835/* Add a constraint to "bmap" expressing i_pos >= o_pos
4836 */
4837static __isl_give isl_basic_map *var_more_or_equal(
4838 __isl_take isl_basic_map *bmap, unsigned pos)
4839{
4840 int i;
4841 unsigned nparam;
4842 unsigned n_in;
4843
4844 i = isl_basic_map_alloc_inequality(bmap);
4845 if (i < 0)
4846 goto error;
4847 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4848 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4849 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4850 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1)isl_sioimath_set_si((bmap->ineq[i][1+nparam+pos]), 1);
4851 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1)isl_sioimath_set_si((bmap->ineq[i][1+nparam+n_in+pos]), -1
)
;
4852 return isl_basic_map_finalize(bmap);
4853error:
4854 isl_basic_map_free(bmap);
4855 return NULL((void*)0);
4856}
4857
4858__isl_give isl_basic_map *isl_basic_map_equal(
4859 __isl_take isl_space *dim, unsigned n_equal)
4860{
4861 int i;
4862 struct isl_basic_map *bmap;
4863 bmap = isl_basic_map_alloc_space(dim, 0, n_equal, 0);
4864 if (!bmap)
4865 return NULL((void*)0);
4866 for (i = 0; i < n_equal && bmap; ++i)
4867 bmap = var_equal(bmap, i);
4868 return isl_basic_map_finalize(bmap);
4869}
4870
4871/* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4872 */
4873__isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *dim,
4874 unsigned pos)
4875{
4876 int i;
4877 struct isl_basic_map *bmap;
4878 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4879 if (!bmap)
4880 return NULL((void*)0);
4881 for (i = 0; i < pos && bmap; ++i)
4882 bmap = var_equal(bmap, i);
4883 if (bmap)
4884 bmap = var_less(bmap, pos);
4885 return isl_basic_map_finalize(bmap);
4886}
4887
4888/* Return a relation on "dim" expressing i_[0..pos] <<= o_[0..pos]
4889 */
4890__isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
4891 __isl_take isl_space *dim, unsigned pos)
4892{
4893 int i;
4894 isl_basic_map *bmap;
4895
4896 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4897 for (i = 0; i < pos; ++i)
4898 bmap = var_equal(bmap, i);
4899 bmap = var_less_or_equal(bmap, pos);
4900 return isl_basic_map_finalize(bmap);
4901}
4902
4903/* Return a relation on "dim" expressing i_pos > o_pos
4904 */
4905__isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *dim,
4906 unsigned pos)
4907{
4908 int i;
4909 struct isl_basic_map *bmap;
4910 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4911 if (!bmap)
4912 return NULL((void*)0);
4913 for (i = 0; i < pos && bmap; ++i)
4914 bmap = var_equal(bmap, i);
4915 if (bmap)
4916 bmap = var_more(bmap, pos);
4917 return isl_basic_map_finalize(bmap);
4918}
4919
4920/* Return a relation on "dim" expressing i_[0..pos] >>= o_[0..pos]
4921 */
4922__isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
4923 __isl_take isl_space *dim, unsigned pos)
4924{
4925 int i;
4926 isl_basic_map *bmap;
4927
4928 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4929 for (i = 0; i < pos; ++i)
4930 bmap = var_equal(bmap, i);
4931 bmap = var_more_or_equal(bmap, pos);
4932 return isl_basic_map_finalize(bmap);
4933}
4934
4935static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *dims,
4936 unsigned n, int equal)
4937{
4938 struct isl_map *map;
4939 int i;
4940
4941 if (n == 0 && equal)
4942 return isl_map_universe(dims);
4943
4944 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT(1 << 0));
4945
4946 for (i = 0; i + 1 < n; ++i)
4947 map = isl_map_add_basic_map(map,
4948 isl_basic_map_less_at(isl_space_copy(dims), i));
4949 if (n > 0) {
4950 if (equal)
4951 map = isl_map_add_basic_map(map,
4952 isl_basic_map_less_or_equal_at(dims, n - 1));
4953 else
4954 map = isl_map_add_basic_map(map,
4955 isl_basic_map_less_at(dims, n - 1));
4956 } else
4957 isl_space_free(dims);
4958
4959 return map;
4960}
4961
4962static __isl_give isl_map *map_lex_lte(__isl_take isl_space *dims, int equal)
4963{
4964 if (!dims)
4965 return NULL((void*)0);
4966 return map_lex_lte_first(dims, dims->n_out, equal);
4967}
4968
4969__isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
4970{
4971 return map_lex_lte_first(dim, n, 0);
4972}
4973
4974__isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
4975{
4976 return map_lex_lte_first(dim, n, 1);
4977}
4978
4979__isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
4980{
4981 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
4982}
4983
4984__isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
4985{
4986 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
4987}
4988
4989static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *dims,
4990 unsigned n, int equal)
4991{
4992 struct isl_map *map;
4993 int i;
4994
4995 if (n == 0 && equal)
4996 return isl_map_universe(dims);
4997
4998 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT(1 << 0));
4999
5000 for (i = 0; i + 1 < n; ++i)
5001 map = isl_map_add_basic_map(map,
5002 isl_basic_map_more_at(isl_space_copy(dims), i));
5003 if (n > 0) {
5004 if (equal)
5005 map = isl_map_add_basic_map(map,
5006 isl_basic_map_more_or_equal_at(dims, n - 1));
5007 else
5008 map = isl_map_add_basic_map(map,
5009 isl_basic_map_more_at(dims, n - 1));
5010 } else
5011 isl_space_free(dims);
5012
5013 return map;
5014}
5015
5016static __isl_give isl_map *map_lex_gte(__isl_take isl_space *dims, int equal)
5017{
5018 if (!dims)
5019 return NULL((void*)0);
5020 return map_lex_gte_first(dims, dims->n_out, equal);
5021}
5022
5023__isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
5024{
5025 return map_lex_gte_first(dim, n, 0);
5026}
5027
5028__isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
5029{
5030 return map_lex_gte_first(dim, n, 1);
5031}
5032
5033__isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
5034{
5035 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
5036}
5037
5038__isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
5039{
5040 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
5041}
5042
5043__isl_give isl_map *isl_set_lex_le_set(__isl_take isl_setisl_map *set1,
5044 __isl_take isl_setisl_map *set2)
5045{
5046 isl_map *map;
5047 map = isl_map_lex_le(isl_set_get_space(set1));
5048 map = isl_map_intersect_domain(map, set1);
5049 map = isl_map_intersect_range(map, set2);
5050 return map;
5051}
5052
5053__isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_setisl_map *set1,
5054 __isl_take isl_setisl_map *set2)
5055{
5056 isl_map *map;
5057 map = isl_map_lex_lt(isl_set_get_space(set1));
5058 map = isl_map_intersect_domain(map, set1);
5059 map = isl_map_intersect_range(map, set2);
5060 return map;
5061}
5062
5063__isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_setisl_map *set1,
5064 __isl_take isl_setisl_map *set2)
5065{
5066 isl_map *map;
5067 map = isl_map_lex_ge(isl_set_get_space(set1));
5068 map = isl_map_intersect_domain(map, set1);
5069 map = isl_map_intersect_range(map, set2);
5070 return map;
5071}
5072
5073__isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_setisl_map *set1,
5074 __isl_take isl_setisl_map *set2)
5075{
5076 isl_map *map;
5077 map = isl_map_lex_gt(isl_set_get_space(set1));
5078 map = isl_map_intersect_domain(map, set1);
5079 map = isl_map_intersect_range(map, set2);
5080 return map;
5081}
5082
5083__isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
5084 __isl_take isl_map *map2)
5085{
5086 isl_map *map;
5087 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
5088 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5089 map = isl_map_apply_range(map, isl_map_reverse(map2));
5090 return map;
5091}
5092
5093__isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
5094 __isl_take isl_map *map2)
5095{
5096 isl_map *map;
5097 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
5098 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5099 map = isl_map_apply_range(map, isl_map_reverse(map2));
5100 return map;
5101}
5102
5103__isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
5104 __isl_take isl_map *map2)
5105{
5106 isl_map *map;
5107 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
5108 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5109 map = isl_map_apply_range(map, isl_map_reverse(map2));
5110 return map;
5111}
5112
5113__isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
5114 __isl_take isl_map *map2)
5115{
5116 isl_map *map;
5117 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
5118 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5119 map = isl_map_apply_range(map, isl_map_reverse(map2));
5120 return map;
5121}
5122
5123/* For a div d = floor(f/m), add the constraint
5124 *
5125 * f - m d >= 0
5126 */
5127static isl_stat add_upper_div_constraint(__isl_keep isl_basic_map *bmap,
5128 unsigned pos, isl_int *div)
5129{
5130 int i;
5131 unsigned total = isl_basic_map_total_dim(bmap);
5132
5133 i = isl_basic_map_alloc_inequality(bmap);
5134 if (i < 0)
5135 return isl_stat_error;
5136 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
5137 isl_int_neg(bmap->ineq[i][1 + pos], div[0])isl_sioimath_neg((bmap->ineq[i][1 + pos]), *(div[0]));
5138
5139 return isl_stat_ok;
5140}
5141
5142/* For a div d = floor(f/m), add the constraint
5143 *
5144 * -(f-(m-1)) + m d >= 0
5145 */
5146static isl_stat add_lower_div_constraint(__isl_keep isl_basic_map *bmap,
5147 unsigned pos, isl_int *div)
5148{
5149 int i;
5150 unsigned total = isl_basic_map_total_dim(bmap);
5151
5152 i = isl_basic_map_alloc_inequality(bmap);
5153 if (i < 0)
5154 return isl_stat_error;
5155 isl_seq_neg(bmap->ineq[i], div + 1, 1 + total);
5156 isl_int_set(bmap->ineq[i][1 + pos], div[0])isl_sioimath_set((bmap->ineq[i][1 + pos]), *(div[0]));
5157 isl_int_add(bmap->ineq[i][0], bmap->ineq[i][0], bmap->ineq[i][1 + pos])isl_sioimath_add((bmap->ineq[i][0]), *(bmap->ineq[i][0]
), *(bmap->ineq[i][1 + pos]))
;
5158 isl_int_sub_ui(bmap->ineq[i][0], bmap->ineq[i][0], 1)isl_sioimath_sub_ui((bmap->ineq[i][0]), *(bmap->ineq[i]
[0]), 1)
;
5159
5160 return isl_stat_ok;
5161}
5162
5163/* For a div d = floor(f/m), add the constraints
5164 *
5165 * f - m d >= 0
5166 * -(f-(m-1)) + m d >= 0
5167 *
5168 * Note that the second constraint is the negation of
5169 *
5170 * f - m d >= m
5171 */
5172int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
5173 unsigned pos, isl_int *div)
5174{
5175 if (add_upper_div_constraint(bmap, pos, div) < 0)
5176 return -1;
5177 if (add_lower_div_constraint(bmap, pos, div) < 0)
5178 return -1;
5179 return 0;
5180}
5181
5182int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_setisl_basic_map *bset,
5183 unsigned pos, isl_int *div)
5184{
5185 return isl_basic_map_add_div_constraints_var(bset_to_bmap(bset),
5186 pos, div);
5187}
5188
5189int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
5190{
5191 unsigned total = isl_basic_map_total_dim(bmap);
5192 unsigned div_pos = total - bmap->n_div + div;
5193
5194 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
5195 bmap->div[div]);
5196}
5197
5198/* For each known div d = floor(f/m), add the constraints
5199 *
5200 * f - m d >= 0
5201 * -(f-(m-1)) + m d >= 0
5202 *
5203 * Remove duplicate constraints in case of some these div constraints
5204 * already appear in "bmap".
5205 */
5206__isl_give isl_basic_map *isl_basic_map_add_known_div_constraints(
5207 __isl_take isl_basic_map *bmap)
5208{
5209 unsigned n_div;
5210
5211 if (!bmap)
5212 return NULL((void*)0);
5213 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5214 if (n_div == 0)
5215 return bmap;
5216
5217 bmap = add_known_div_constraints(bmap);
5218 bmap = isl_basic_map_remove_duplicate_constraints(bmap, NULL((void*)0), 0);
5219 bmap = isl_basic_map_finalize(bmap);
5220 return bmap;
5221}
5222
5223/* Add the div constraint of sign "sign" for div "div" of "bmap".
5224 *
5225 * In particular, if this div is of the form d = floor(f/m),
5226 * then add the constraint
5227 *
5228 * f - m d >= 0
5229 *
5230 * if sign < 0 or the constraint
5231 *
5232 * -(f-(m-1)) + m d >= 0
5233 *
5234 * if sign > 0.
5235 */
5236int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map *bmap,
5237 unsigned div, int sign)
5238{
5239 unsigned total;
5240 unsigned div_pos;
5241
5242 if (!bmap)
5243 return -1;
5244
5245 total = isl_basic_map_total_dim(bmap);
5246 div_pos = total - bmap->n_div + div;
5247
5248 if (sign < 0)
5249 return add_upper_div_constraint(bmap, div_pos, bmap->div[div]);
5250 else
5251 return add_lower_div_constraint(bmap, div_pos, bmap->div[div]);
5252}
5253
5254struct isl_basic_setisl_basic_map *isl_basic_map_underlying_set(
5255 struct isl_basic_map *bmap)
5256{
5257 if (!bmap)
5258 goto error;
5259 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
5260 bmap->n_div == 0 &&
5261 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
5262 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
5263 return bset_from_bmap(bmap);
5264 bmap = isl_basic_map_cow(bmap);
5265 if (!bmap)
5266 goto error;
5267 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
5268 if (!bmap->dim)
5269 goto error;
5270 bmap->extra -= bmap->n_div;
5271 bmap->n_div = 0;
5272 bmap = isl_basic_map_finalize(bmap);
5273 return bset_from_bmap(bmap);
5274error:
5275 isl_basic_map_free(bmap);
5276 return NULL((void*)0);
5277}
5278
5279__isl_give isl_basic_setisl_basic_map *isl_basic_set_underlying_set(
5280 __isl_take isl_basic_setisl_basic_map *bset)
5281{
5282 return isl_basic_map_underlying_set(bset_to_bmap(bset));
5283}
5284
5285/* Replace each element in "list" by the result of applying
5286 * isl_basic_map_underlying_set to the element.
5287 */
5288__isl_give isl_basic_set_listisl_basic_map_list *isl_basic_map_list_underlying_set(
5289 __isl_take isl_basic_map_list *list)
5290{
5291 int i, n;
5292
5293 if (!list)
5294 return NULL((void*)0);
5295
5296 n = isl_basic_map_list_n_basic_map(list);
5297 for (i = 0; i < n; ++i) {
5298 isl_basic_map *bmap;
5299 isl_basic_setisl_basic_map *bset;
5300
5301 bmap = isl_basic_map_list_get_basic_map(list, i);
5302 bset = isl_basic_set_underlying_set(bmap);
5303 list = isl_basic_set_list_set_basic_set(list, i, bset);
5304 }
5305
5306 return list;
5307}
5308
5309struct isl_basic_map *isl_basic_map_overlying_set(
5310 struct isl_basic_setisl_basic_map *bset, struct isl_basic_map *like)
5311{
5312 struct isl_basic_map *bmap;
5313 struct isl_ctx *ctx;
5314 unsigned total;
5315 int i;
5316
5317 if (!bset || !like)
5318 goto error;
5319 ctx = bset->ctx;
5320 isl_assert(ctx, bset->n_div == 0, goto error)do { if (bset->n_div == 0) break; do { isl_handle_error(ctx
, isl_error_unknown, "Assertion \"" "bset->n_div == 0" "\" failed"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 5320); goto error; } while (0); } while (0)
;
5321 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error)do { if (isl_basic_set_n_param(bset) == 0) break; do { isl_handle_error
(ctx, isl_error_unknown, "Assertion \"" "isl_basic_set_n_param(bset) == 0"
"\" failed", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 5321); goto error; } while (0); } while (0)
;
5322 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),do { if (bset->dim->n_out == isl_basic_map_total_dim(like
)) break; do { isl_handle_error(ctx, isl_error_unknown, "Assertion \""
"bset->dim->n_out == isl_basic_map_total_dim(like)" "\" failed"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 5323); goto error; } while (0); } while (0)
5323 goto error)do { if (bset->dim->n_out == isl_basic_map_total_dim(like
)) break; do { isl_handle_error(ctx, isl_error_unknown, "Assertion \""
"bset->dim->n_out == isl_basic_map_total_dim(like)" "\" failed"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 5323); goto error; } while (0); } while (0)
;
5324 if (like->n_div == 0) {
5325 isl_space *space = isl_basic_map_get_space(like);
5326 isl_basic_map_free(like);
5327 return isl_basic_map_reset_space(bset, space);
5328 }
5329 bset = isl_basic_set_cow(bset);
5330 if (!bset)
5331 goto error;
5332 total = bset->dim->n_out + bset->extra;
5333 bmap = bset_to_bmap(bset);
5334 isl_space_free(bmap->dim);
5335 bmap->dim = isl_space_copy(like->dim);
5336 if (!bmap->dim)
5337 goto error;
5338 bmap->n_div = like->n_div;
5339 bmap->extra += like->n_div;
5340 if (bmap->extra) {
5341 unsigned ltotal;
5342 isl_int **div;
5343 ltotal = total - bmap->extra + like->extra;
5344 if (ltotal > total)
5345 ltotal = total;
5346 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
5347 bmap->extra * (1 + 1 + total));
5348 if (isl_blk_is_error(bmap->block2))
5349 goto error;
5350 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra)((isl_int * *)isl_realloc_or_die(ctx, bmap->div, (bmap->
extra)*sizeof(isl_int *)))
;
5351 if (!div)
5352 goto error;
5353 bmap->div = div;
5354 for (i = 0; i < bmap->extra; ++i)
5355 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
5356 for (i = 0; i < like->n_div; ++i) {
5357 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
5358 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
5359 }
5360 bmap = isl_basic_map_add_known_div_constraints(bmap);
5361 }
5362 isl_basic_map_free(like);
5363 bmap = isl_basic_map_simplify(bmap);
5364 bmap = isl_basic_map_finalize(bmap);
5365 return bmap;
5366error:
5367 isl_basic_map_free(like);
5368 isl_basic_set_free(bset);
5369 return NULL((void*)0);
5370}
5371
5372struct isl_basic_setisl_basic_map *isl_basic_set_from_underlying_set(
5373 struct isl_basic_setisl_basic_map *bset, struct isl_basic_setisl_basic_map *like)
5374{
5375 return bset_from_bmap(isl_basic_map_overlying_set(bset,
5376 bset_to_bmap(like)));
5377}
5378
5379struct isl_setisl_map *isl_map_underlying_set(struct isl_map *map)
5380{
5381 int i;
5382
5383 map = isl_map_cow(map);
5384 if (!map)
5385 return NULL((void*)0);
5386 map->dim = isl_space_cow(map->dim);
5387 if (!map->dim)
5388 goto error;
5389
5390 for (i = 1; i < map->n; ++i)
5391 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,do { if (map->p[0]->n_div == map->p[i]->n_div) break
; do { isl_handle_error(map->ctx, isl_error_unknown, "Assertion \""
"map->p[0]->n_div == map->p[i]->n_div" "\" failed"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 5392); goto error; } while (0); } while (0)
5392 goto error)do { if (map->p[0]->n_div == map->p[i]->n_div) break
; do { isl_handle_error(map->ctx, isl_error_unknown, "Assertion \""
"map->p[0]->n_div == map->p[i]->n_div" "\" failed"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 5392); goto error; } while (0); } while (0)
;
5393 for (i = 0; i < map->n; ++i) {
5394 map->p[i] = bset_to_bmap(
5395 isl_basic_map_underlying_set(map->p[i]));
5396 if (!map->p[i])
5397 goto error;
5398 }
5399 if (map->n == 0)
5400 map->dim = isl_space_underlying(map->dim, 0);
5401 else {
5402 isl_space_free(map->dim);
5403 map->dim = isl_space_copy(map->p[0]->dim);
5404 }
5405 if (!map->dim)
5406 goto error;
5407 return set_from_map(map);
5408error:
5409 isl_map_free(map);
5410 return NULL((void*)0);
5411}
5412
5413/* Replace the space of "bmap" by "space".
5414 *
5415 * If the space of "bmap" is identical to "space" (including the identifiers
5416 * of the input and output dimensions), then simply return the original input.
5417 */
5418__isl_give isl_basic_map *isl_basic_map_reset_space(
5419 __isl_take isl_basic_map *bmap, __isl_take isl_space *space)
5420{
5421 isl_bool equal;
5422 isl_space *bmap_space;
5423
5424 bmap_space = isl_basic_map_peek_space(bmap);
5425 equal = isl_space_is_equal(bmap_space, space);
5426 if (equal >= 0 && equal)
5427 equal = isl_space_has_equal_ids(bmap_space, space);
5428 if (equal < 0)
5429 goto error;
5430 if (equal) {
5431 isl_space_free(space);
5432 return bmap;
5433 }
5434 bmap = isl_basic_map_cow(bmap);
5435 if (!bmap || !space)
5436 goto error;
5437
5438 isl_space_free(bmap->dim);
5439 bmap->dim = space;
5440
5441 bmap = isl_basic_map_finalize(bmap);
5442
5443 return bmap;
5444error:
5445 isl_basic_map_free(bmap);
5446 isl_space_free(space);
5447 return NULL((void*)0);
5448}
5449
5450__isl_give isl_basic_setisl_basic_map *isl_basic_set_reset_space(
5451 __isl_take isl_basic_setisl_basic_map *bset, __isl_take isl_space *dim)
5452{
5453 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset),
5454 dim));
5455}
5456
5457__isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
5458 __isl_take isl_space *dim)
5459{
5460 int i;
5461
5462 map = isl_map_cow(map);
5463 if (!map || !dim)
5464 goto error;
5465
5466 for (i = 0; i < map->n; ++i) {
5467 map->p[i] = isl_basic_map_reset_space(map->p[i],
5468 isl_space_copy(dim));
5469 if (!map->p[i])
5470 goto error;
5471 }
5472 isl_space_free(map->dim);
5473 map->dim = dim;
5474
5475 return map;
5476error:
5477 isl_map_free(map);
5478 isl_space_free(dim);
5479 return NULL((void*)0);
5480}
5481
5482__isl_give isl_setisl_map *isl_set_reset_space(__isl_take isl_setisl_map *set,
5483 __isl_take isl_space *dim)
5484{
5485 return set_from_map(isl_map_reset_space(set_to_map(set), dim));
5486}
5487
5488/* Compute the parameter domain of the given basic set.
5489 */
5490__isl_give isl_basic_setisl_basic_map *isl_basic_set_params(__isl_take isl_basic_setisl_basic_map *bset)
5491{
5492 isl_bool is_params;
5493 isl_space *space;
5494 unsigned n;
5495
5496 is_params = isl_basic_set_is_params(bset);
5497 if (is_params < 0)
5498 return isl_basic_set_free(bset);
5499 if (is_params)
5500 return bset;
5501
5502 n = isl_basic_set_dim(bset, isl_dim_set);
5503 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
5504 space = isl_basic_set_get_space(bset);
5505 space = isl_space_params(space);
5506 bset = isl_basic_set_reset_space(bset, space);
5507 return bset;
5508}
5509
5510/* Construct a zero-dimensional basic set with the given parameter domain.
5511 */
5512__isl_give isl_basic_setisl_basic_map *isl_basic_set_from_params(
5513 __isl_take isl_basic_setisl_basic_map *bset)
5514{
5515 isl_space *space;
5516 space = isl_basic_set_get_space(bset);
5517 space = isl_space_set_from_params(space);
5518 bset = isl_basic_set_reset_space(bset, space);
5519 return bset;
5520}
5521
5522/* Compute the parameter domain of the given set.
5523 */
5524__isl_give isl_setisl_map *isl_set_params(__isl_take isl_setisl_map *set)
5525{
5526 isl_space *space;
5527 unsigned n;
5528
5529 if (isl_set_is_params(set))
5530 return set;
5531
5532 n = isl_set_dim(set, isl_dim_set);
5533 set = isl_set_project_out(set, isl_dim_set, 0, n);
5534 space = isl_set_get_space(set);
5535 space = isl_space_params(space);
5536 set = isl_set_reset_space(set, space);
5537 return set;
5538}
5539
5540/* Construct a zero-dimensional set with the given parameter domain.
5541 */
5542__isl_give isl_setisl_map *isl_set_from_params(__isl_take isl_setisl_map *set)
5543{
5544 isl_space *space;
5545 space = isl_set_get_space(set);
5546 space = isl_space_set_from_params(space);
5547 set = isl_set_reset_space(set, space);
5548 return set;
5549}
5550
5551/* Compute the parameter domain of the given map.
5552 */
5553__isl_give isl_setisl_map *isl_map_params(__isl_take isl_map *map)
5554{
5555 isl_space *space;
5556 unsigned n;
5557
5558 n = isl_map_dim(map, isl_dim_in);
5559 map = isl_map_project_out(map, isl_dim_in, 0, n);
5560 n = isl_map_dim(map, isl_dim_out);
5561 map = isl_map_project_out(map, isl_dim_out, 0, n);
5562 space = isl_map_get_space(map);
5563 space = isl_space_params(space);
5564 map = isl_map_reset_space(map, space);
5565 return map;
5566}
5567
5568struct isl_basic_setisl_basic_map *isl_basic_map_domain(struct isl_basic_map *bmap)
5569{
5570 isl_space *space;
5571 unsigned n_out;
5572
5573 if (!bmap)
5574 return NULL((void*)0);
5575 space = isl_space_domain(isl_basic_map_get_space(bmap));
5576
5577 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5578 bmap = isl_basic_map_project_out(bmap, isl_dim_out, 0, n_out);
5579
5580 return isl_basic_map_reset_space(bmap, space);
5581}
5582
5583isl_bool isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
5584{
5585 if (!bmap)
5586 return isl_bool_error;
5587 return isl_space_may_be_set(bmap->dim);
5588}
5589
5590/* Is this basic map actually a set?
5591 * Users should never call this function. Outside of isl,
5592 * the type should indicate whether something is a set or a map.
5593 */
5594isl_bool isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
5595{
5596 if (!bmap)
5597 return isl_bool_error;
5598 return isl_space_is_set(bmap->dim);
5599}
5600
5601struct isl_basic_setisl_basic_map *isl_basic_map_range(struct isl_basic_map *bmap)
5602{
5603 isl_bool is_set;
5604
5605 is_set = isl_basic_map_is_set(bmap);
5606 if (is_set < 0)
5607 goto error;
5608 if (is_set)
5609 return bmap;
5610 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
5611error:
5612 isl_basic_map_free(bmap);
5613 return NULL((void*)0);
5614}
5615
5616__isl_give isl_basic_map *isl_basic_map_domain_map(
5617 __isl_take isl_basic_map *bmap)
5618{
5619 int i;
5620 isl_space *dim;
5621 isl_basic_map *domain;
5622 int nparam, n_in, n_out;
5623
5624 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5625 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5626 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5627
5628 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
5629 domain = isl_basic_map_universe(dim);
5630
5631 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5632 bmap = isl_basic_map_apply_range(bmap, domain);
5633 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
5634
5635 for (i = 0; i < n_in; ++i)
5636 bmap = isl_basic_map_equate(bmap, isl_dim_in, i,
5637 isl_dim_out, i);
5638
5639 bmap = isl_basic_map_gauss(bmap, NULL((void*)0));
5640 return isl_basic_map_finalize(bmap);
5641}
5642
5643__isl_give isl_basic_map *isl_basic_map_range_map(
5644 __isl_take isl_basic_map *bmap)
5645{
5646 int i;
5647 isl_space *dim;
5648 isl_basic_map *range;
5649 int nparam, n_in, n_out;
5650
5651 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5652 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5653 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5654
5655 dim = isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap)));
5656 range = isl_basic_map_universe(dim);
5657
5658 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5659 bmap = isl_basic_map_apply_range(bmap, range);
5660 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
5661
5662 for (i = 0; i < n_out; ++i)
5663 bmap = isl_basic_map_equate(bmap, isl_dim_in, n_in + i,
5664 isl_dim_out, i);
5665
5666 bmap = isl_basic_map_gauss(bmap, NULL((void*)0));
5667 return isl_basic_map_finalize(bmap);
5668}
5669
5670int isl_map_may_be_set(__isl_keep isl_map *map)
5671{
5672 if (!map)
5673 return -1;
5674 return isl_space_may_be_set(map->dim);
5675}
5676
5677/* Is this map actually a set?
5678 * Users should never call this function. Outside of isl,
5679 * the type should indicate whether something is a set or a map.
5680 */
5681isl_bool isl_map_is_set(__isl_keep isl_map *map)
5682{
5683 if (!map)
5684 return isl_bool_error;
5685 return isl_space_is_set(map->dim);
5686}
5687
5688struct isl_setisl_map *isl_map_range(struct isl_map *map)
5689{
5690 int i;
5691 isl_bool is_set;
5692 struct isl_setisl_map *set;
5693
5694 is_set = isl_map_is_set(map);
5695 if (is_set < 0)
5696 goto error;
5697 if (is_set)
5698 return set_from_map(map);
5699
5700 map = isl_map_cow(map);
5701 if (!map)
5702 goto error;
5703
5704 set = set_from_map(map);
5705 set->dim = isl_space_range(set->dim);
5706 if (!set->dim)
5707 goto error;
5708 for (i = 0; i < map->n; ++i) {
5709 set->p[i] = isl_basic_map_range(map->p[i]);
5710 if (!set->p[i])
5711 goto error;
5712 }
5713 ISL_F_CLR(set, ISL_MAP_DISJOINT)(((set)->flags) &= ~((1 << 0)));
5714 ISL_F_CLR(set, ISL_SET_NORMALIZED)(((set)->flags) &= ~((1 << 1)));
5715 return set;
5716error:
5717 isl_map_free(map);
5718 return NULL((void*)0);
5719}
5720
5721__isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
5722{
5723 int i;
5724
5725 map = isl_map_cow(map);
5726 if (!map)
5727 return NULL((void*)0);
5728
5729 map->dim = isl_space_domain_map(map->dim);
5730 if (!map->dim)
5731 goto error;
5732 for (i = 0; i < map->n; ++i) {
5733 map->p[i] = isl_basic_map_domain_map(map->p[i]);
5734 if (!map->p[i])
5735 goto error;
5736 }
5737 ISL_F_CLR(map, ISL_MAP_DISJOINT)(((map)->flags) &= ~((1 << 0)));
5738 ISL_F_CLR(map, ISL_MAP_NORMALIZED)(((map)->flags) &= ~((1 << 1)));
5739 return map;
5740error:
5741 isl_map_free(map);
5742 return NULL((void*)0);
5743}
5744
5745__isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
5746{
5747 int i;
5748 isl_space *range_dim;
5749
5750 map = isl_map_cow(map);
5751 if (!map)
5752 return NULL((void*)0);
5753
5754 range_dim = isl_space_range(isl_map_get_space(map));
5755 range_dim = isl_space_from_range(range_dim);
5756 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
5757 map->dim = isl_space_join(map->dim, range_dim);
5758 if (!map->dim)
5759 goto error;
5760 for (i = 0; i < map->n; ++i) {
5761 map->p[i] = isl_basic_map_range_map(map->p[i]);
5762 if (!map->p[i])
5763 goto error;
5764 }
5765 ISL_F_CLR(map, ISL_MAP_DISJOINT)(((map)->flags) &= ~((1 << 0)));
5766 ISL_F_CLR(map, ISL_MAP_NORMALIZED)(((map)->flags) &= ~((1 << 1)));
5767 return map;
5768error:
5769 isl_map_free(map);
5770 return NULL((void*)0);
5771}
5772
5773/* Given a wrapped map of the form A[B -> C],
5774 * return the map A[B -> C] -> B.
5775 */
5776__isl_give isl_map *isl_set_wrapped_domain_map(__isl_take isl_setisl_map *set)
5777{
5778 isl_id *id;
5779 isl_map *map;
5780
5781 if (!set)
5782 return NULL((void*)0);
5783 if (!isl_set_has_tuple_id(set))
5784 return isl_map_domain_map(isl_set_unwrap(set));
5785
5786 id = isl_set_get_tuple_id(set);
5787 map = isl_map_domain_map(isl_set_unwrap(set));
5788 map = isl_map_set_tuple_id(map, isl_dim_in, id);
5789
5790 return map;
5791}
5792
5793__isl_give isl_basic_map *isl_basic_map_from_domain(
5794 __isl_take isl_basic_setisl_basic_map *bset)
5795{
5796 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
5797}
5798
5799__isl_give isl_basic_map *isl_basic_map_from_range(
5800 __isl_take isl_basic_setisl_basic_map *bset)
5801{
5802 isl_space *space;
5803 space = isl_basic_set_get_space(bset);
5804 space = isl_space_from_range(space);
5805 bset = isl_basic_set_reset_space(bset, space);
5806 return bset_to_bmap(bset);
5807}
5808
5809/* Create a relation with the given set as range.
5810 * The domain of the created relation is a zero-dimensional
5811 * flat anonymous space.
5812 */
5813__isl_give isl_map *isl_map_from_range(__isl_take isl_setisl_map *set)
5814{
5815 isl_space *space;
5816 space = isl_set_get_space(set);
5817 space = isl_space_from_range(space);
5818 set = isl_set_reset_space(set, space);
5819 return set_to_map(set);
5820}
5821
5822/* Create a relation with the given set as domain.
5823 * The range of the created relation is a zero-dimensional
5824 * flat anonymous space.
5825 */
5826__isl_give isl_map *isl_map_from_domain(__isl_take isl_setisl_map *set)
5827{
5828 return isl_map_reverse(isl_map_from_range(set));
5829}
5830
5831__isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
5832 __isl_take isl_basic_setisl_basic_map *domain, __isl_take isl_basic_setisl_basic_map *range)
5833{
5834 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
5835}
5836
5837__isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_setisl_map *domain,
5838 __isl_take isl_setisl_map *range)
5839{
5840 return isl_map_apply_range(isl_map_reverse(domain), range);
5841}
5842
5843/* Return a newly allocated isl_map with given space and flags and
5844 * room for "n" basic maps.
5845 * Make sure that all cached information is cleared.
5846 */
5847__isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *space, int n,
5848 unsigned flags)
5849{
5850 struct isl_map *map;
5851
5852 if (!space)
5853 return NULL((void*)0);
5854 if (n < 0)
5855 isl_die(space->ctx, isl_error_internal,do { isl_handle_error(space->ctx, isl_error_internal, "negative number of basic maps"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 5856); goto error; } while (0)
5856 "negative number of basic maps", goto error)do { isl_handle_error(space->ctx, isl_error_internal, "negative number of basic maps"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 5856); goto error; } while (0)
;
5857 map = isl_calloc(space->ctx, struct isl_map,((struct isl_map *)isl_calloc_or_die(space->ctx, 1, sizeof
(struct isl_map) + (n - 1) * sizeof(struct isl_basic_map *)))
5858 sizeof(struct isl_map) +((struct isl_map *)isl_calloc_or_die(space->ctx, 1, sizeof
(struct isl_map) + (n - 1) * sizeof(struct isl_basic_map *)))
5859 (n - 1) * sizeof(struct isl_basic_map *))((struct isl_map *)isl_calloc_or_die(space->ctx, 1, sizeof
(struct isl_map) + (n - 1) * sizeof(struct isl_basic_map *)))
;
5860 if (!map)
5861 goto error;
5862
5863 map->ctx = space->ctx;
5864 isl_ctx_ref(map->ctx);
5865 map->ref = 1;
5866 map->size = n;
5867 map->n = 0;
5868 map->dim = space;
5869 map->flags = flags;
5870 return map;
5871error:
5872 isl_space_free(space);
5873 return NULL((void*)0);
5874}
5875
5876__isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *dim)
5877{
5878 struct isl_basic_map *bmap;
5879 bmap = isl_basic_map_alloc_space(dim, 0, 1, 0);
5880 bmap = isl_basic_map_set_to_empty(bmap);
5881 return bmap;
5882}
5883
5884__isl_give isl_basic_setisl_basic_map *isl_basic_set_empty(__isl_take isl_space *dim)
5885{
5886 struct isl_basic_setisl_basic_map *bset;
5887 bset = isl_basic_set_alloc_space(dim, 0, 1, 0);
5888 bset = isl_basic_set_set_to_empty(bset);
5889 return bset;
5890}
5891
5892__isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *dim)
5893{
5894 struct isl_basic_map *bmap;
5895 bmap = isl_basic_map_alloc_space(dim, 0, 0, 0);
5896 bmap = isl_basic_map_finalize(bmap);
5897 return bmap;
5898}
5899
5900__isl_give isl_basic_setisl_basic_map *isl_basic_set_universe(__isl_take isl_space *dim)
5901{
5902 struct isl_basic_setisl_basic_map *bset;
5903 bset = isl_basic_set_alloc_space(dim, 0, 0, 0);
5904 bset = isl_basic_set_finalize(bset);
5905 return bset;
5906}
5907
5908__isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_space *dim)
5909{
5910 int i;
5911 unsigned total = isl_space_dim(dim, isl_dim_all);
5912 isl_basic_map *bmap;
5913
5914 bmap= isl_basic_map_alloc_space(dim, 0, 0, total);
5915 for (i = 0; i < total; ++i) {
5916 int k = isl_basic_map_alloc_inequality(bmap);
5917 if (k < 0)
5918 goto error;
5919 isl_seq_clr(bmap->ineq[k], 1 + total);
5920 isl_int_set_si(bmap->ineq[k][1 + i], 1)isl_sioimath_set_si((bmap->ineq[k][1 + i]), 1);
5921 }
5922 return bmap;
5923error:
5924 isl_basic_map_free(bmap);
5925 return NULL((void*)0);
5926}
5927
5928__isl_give isl_basic_setisl_basic_map *isl_basic_set_nat_universe(__isl_take isl_space *dim)
5929{
5930 return isl_basic_map_nat_universe(dim);
5931}
5932
5933__isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
5934{
5935 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
5936}
5937
5938__isl_give isl_setisl_map *isl_set_nat_universe(__isl_take isl_space *dim)
5939{
5940 return isl_map_nat_universe(dim);
5941}
5942
5943__isl_give isl_map *isl_map_empty(__isl_take isl_space *dim)
5944{
5945 return isl_map_alloc_space(dim, 0, ISL_MAP_DISJOINT(1 << 0));
5946}
5947
5948__isl_give isl_setisl_map *isl_set_empty(__isl_take isl_space *dim)
5949{
5950 return isl_set_alloc_space(dim, 0, ISL_MAP_DISJOINT(1 << 0));
5951}
5952
5953__isl_give isl_map *isl_map_universe(__isl_take isl_space *dim)
5954{
5955 struct isl_map *map;
5956 if (!dim)
5957 return NULL((void*)0);
5958 map = isl_map_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT(1 << 0));
5959 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
5960 return map;
5961}
5962
5963__isl_give isl_setisl_map *isl_set_universe(__isl_take isl_space *dim)
5964{
5965 struct isl_setisl_map *set;
5966 if (!dim)
5967 return NULL((void*)0);
5968 set = isl_set_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT(1 << 0));
5969 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
5970 return set;
5971}
5972
5973struct isl_map *isl_map_dup(struct isl_map *map)
5974{
5975 int i;
5976 struct isl_map *dup;
5977
5978 if (!map)
5979 return NULL((void*)0);
5980 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
5981 for (i = 0; i < map->n; ++i)
5982 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
5983 return dup;
5984}
5985
5986__isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
5987 __isl_take isl_basic_map *bmap)
5988{
5989 if (!bmap || !map)
5990 goto error;
5991 if (isl_basic_map_plain_is_empty(bmap)) {
5992 isl_basic_map_free(bmap);
5993 return map;
5994 }
5995 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error)do { if (isl_space_is_equal(map->dim, bmap->dim)) break
; do { isl_handle_error(map->ctx, isl_error_unknown, "Assertion \""
"isl_space_is_equal(map->dim, bmap->dim)" "\" failed",
"/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 5995); goto error; } while (0); } while (0)
;
5996 isl_assert(map->ctx, map->n < map->size, goto error)do { if (map->n < map->size) break; do { isl_handle_error
(map->ctx, isl_error_unknown, "Assertion \"" "map->n < map->size"
"\" failed", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 5996); goto error; } while (0); } while (0)
;
5997 map->p[map->n] = bmap;
5998 map->n++;
5999 ISL_F_CLR(map, ISL_MAP_NORMALIZED)(((map)->flags) &= ~((1 << 1)));
6000 return map;
6001error:
6002 if (map)
6003 isl_map_free(map);
6004 if (bmap)
6005 isl_basic_map_free(bmap);
6006 return NULL((void*)0);
6007}
6008
6009__isl_null isl_map *isl_map_free(__isl_take isl_map *map)
6010{
6011 int i;
6012
6013 if (!map)
6014 return NULL((void*)0);
6015
6016 if (--map->ref > 0)
6017 return NULL((void*)0);
6018
6019 clear_caches(map);
6020 isl_ctx_deref(map->ctx);
6021 for (i = 0; i < map->n; ++i)
6022 isl_basic_map_free(map->p[i]);
6023 isl_space_free(map->dim);
6024 free(map);
6025
6026 return NULL((void*)0);
6027}
6028
6029static struct isl_basic_map *isl_basic_map_fix_pos_si(
6030 struct isl_basic_map *bmap, unsigned pos, int value)
6031{
6032 int j;
6033
6034 bmap = isl_basic_map_cow(bmap);
6035 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
6036 j = isl_basic_map_alloc_equality(bmap);
6037 if (j < 0)
6038 goto error;
6039 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
6040 isl_int_set_si(bmap->eq[j][pos], -1)isl_sioimath_set_si((bmap->eq[j][pos]), -1);
6041 isl_int_set_si(bmap->eq[j][0], value)isl_sioimath_set_si((bmap->eq[j][0]), value);
6042 bmap = isl_basic_map_simplify(bmap);
6043 return isl_basic_map_finalize(bmap);
6044error:
6045 isl_basic_map_free(bmap);
6046 return NULL((void*)0);
6047}
6048
6049static __isl_give isl_basic_map *isl_basic_map_fix_pos(
6050 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
6051{
6052 int j;
6053
6054 bmap = isl_basic_map_cow(bmap);
6055 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
6056 j = isl_basic_map_alloc_equality(bmap);
6057 if (j < 0)
6058 goto error;
6059 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
6060 isl_int_set_si(bmap->eq[j][pos], -1)isl_sioimath_set_si((bmap->eq[j][pos]), -1);
6061 isl_int_set(bmap->eq[j][0], value)isl_sioimath_set((bmap->eq[j][0]), *(value));
6062 bmap = isl_basic_map_simplify(bmap);
6063 return isl_basic_map_finalize(bmap);
6064error:
6065 isl_basic_map_free(bmap);
6066 return NULL((void*)0);
6067}
6068
6069struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
6070 enum isl_dim_type type, unsigned pos, int value)
6071{
6072 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6073 return isl_basic_map_free(bmap);
6074 return isl_basic_map_fix_pos_si(bmap,
6075 isl_basic_map_offset(bmap, type) + pos, value);
6076}
6077
6078__isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
6079 enum isl_dim_type type, unsigned pos, isl_int value)
6080{
6081 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6082 return isl_basic_map_free(bmap);
6083 return isl_basic_map_fix_pos(bmap,
6084 isl_basic_map_offset(bmap, type) + pos, value);
6085}
6086
6087/* Fix the value of the variable at position "pos" of type "type" of "bmap"
6088 * to be equal to "v".
6089 */
6090__isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
6091 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6092{
6093 if (!bmap || !v)
6094 goto error;
6095 if (!isl_val_is_int(v))
6096 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,do { isl_handle_error(isl_basic_map_get_ctx(bmap), isl_error_invalid
, "expecting integer value", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 6097); goto error; } while (0)
6097 "expecting integer value", goto error)do { isl_handle_error(isl_basic_map_get_ctx(bmap), isl_error_invalid
, "expecting integer value", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 6097); goto error; } while (0)
;
6098 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6099 goto error;
6100 pos += isl_basic_map_offset(bmap, type);
6101 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
6102 isl_val_free(v);
6103 return bmap;
6104error:
6105 isl_basic_map_free(bmap);
6106 isl_val_free(v);
6107 return NULL((void*)0);
6108}
6109
6110/* Fix the value of the variable at position "pos" of type "type" of "bset"
6111 * to be equal to "v".
6112 */
6113__isl_give isl_basic_setisl_basic_map *isl_basic_set_fix_val(__isl_take isl_basic_setisl_basic_map *bset,
6114 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6115{
6116 return isl_basic_map_fix_val(bset, type, pos, v);
6117}
6118
6119struct isl_basic_setisl_basic_map *isl_basic_set_fix_si(struct isl_basic_setisl_basic_map *bset,
6120 enum isl_dim_type type, unsigned pos, int value)
6121{
6122 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
6123 type, pos, value));
6124}
6125
6126__isl_give isl_basic_setisl_basic_map *isl_basic_set_fix(__isl_take isl_basic_setisl_basic_map *bset,
6127 enum isl_dim_type type, unsigned pos, isl_int value)
6128{
6129 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset),
6130 type, pos, value));
6131}
6132
6133struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
6134 unsigned input, int value)
6135{
6136 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
6137}
6138
6139struct isl_basic_setisl_basic_map *isl_basic_set_fix_dim_si(struct isl_basic_setisl_basic_map *bset,
6140 unsigned dim, int value)
6141{
6142 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
6143 isl_dim_set, dim, value));
6144}
6145
6146static int remove_if_empty(__isl_keep isl_map *map, int i)
6147{
6148 int empty = isl_basic_map_plain_is_empty(map->p[i]);
6149
6150 if (empty < 0)
6151 return -1;
6152 if (!empty)
6153 return 0;
6154
6155 isl_basic_map_free(map->p[i]);
6156 if (i != map->n - 1) {
6157 ISL_F_CLR(map, ISL_MAP_NORMALIZED)(((map)->flags) &= ~((1 << 1)));
6158 map->p[i] = map->p[map->n - 1];
6159 }
6160 map->n--;
6161
6162 return 0;
6163}
6164
6165/* Perform "fn" on each basic map of "map", where we may not be holding
6166 * the only reference to "map".
6167 * In particular, "fn" should be a semantics preserving operation
6168 * that we want to apply to all copies of "map". We therefore need
6169 * to be careful not to modify "map" in a way that breaks "map"
6170 * in case anything goes wrong.
6171 */
6172__isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
6173 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
6174{
6175 struct isl_basic_map *bmap;
6176 int i;
6177
6178 if (!map)
6179 return NULL((void*)0);
6180
6181 for (i = map->n - 1; i >= 0; --i) {
6182 bmap = isl_basic_map_copy(map->p[i]);
6183 bmap = fn(bmap);
6184 if (!bmap)
6185 goto error;
6186 isl_basic_map_free(map->p[i]);
6187 map->p[i] = bmap;
6188 if (remove_if_empty(map, i) < 0)
6189 goto error;
6190 }
6191
6192 return map;
6193error:
6194 isl_map_free(map);
6195 return NULL((void*)0);
6196}
6197
6198struct isl_map *isl_map_fix_si(struct isl_map *map,
6199 enum isl_dim_type type, unsigned pos, int value)
6200{
6201 int i;
6202
6203 map = isl_map_cow(map);
6204 if (!map)
6205 return NULL((void*)0);
6206
6207 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error)do { if (pos < isl_map_dim(map, type)) break; do { isl_handle_error
(map->ctx, isl_error_unknown, "Assertion \"" "pos < isl_map_dim(map, type)"
"\" failed", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 6207); goto error; } while (0); } while (0)
;
6208 for (i = map->n - 1; i >= 0; --i) {
6209 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
6210 if (remove_if_empty(map, i) < 0)
6211 goto error;
6212 }
6213 ISL_F_CLR(map, ISL_MAP_NORMALIZED)(((map)->flags) &= ~((1 << 1)));
6214 return map;
6215error:
6216 isl_map_free(map);
6217 return NULL((void*)0);
6218}
6219
6220__isl_give isl_setisl_map *isl_set_fix_si(__isl_take isl_setisl_map *set,
6221 enum isl_dim_type type, unsigned pos, int value)
6222{
6223 return set_from_map(isl_map_fix_si(set_to_map(set), type, pos, value));
6224}
6225
6226__isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
6227 enum isl_dim_type type, unsigned pos, isl_int value)
6228{
6229 int i;
6230
6231 map = isl_map_cow(map);
6232 if (!map)
6233 return NULL((void*)0);
6234
6235 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error)do { if (pos < isl_map_dim(map, type)) break; do { isl_handle_error
(map->ctx, isl_error_unknown, "Assertion \"" "pos < isl_map_dim(map, type)"
"\" failed", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 6235); goto error; } while (0); } while (0)
;
6236 for (i = 0; i < map->n; ++i) {
6237 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
6238 if (!map->p[i])
6239 goto error;
6240 }
6241 ISL_F_CLR(map, ISL_MAP_NORMALIZED)(((map)->flags) &= ~((1 << 1)));
6242 return map;
6243error:
6244 isl_map_free(map);
6245 return NULL((void*)0);
6246}
6247
6248__isl_give isl_setisl_map *isl_set_fix(__isl_take isl_setisl_map *set,
6249 enum isl_dim_type type, unsigned pos, isl_int value)
6250{
6251 return set_from_map(isl_map_fix(set_to_map(set), type, pos, value));
6252}
6253
6254/* Fix the value of the variable at position "pos" of type "type" of "map"
6255 * to be equal to "v".
6256 */
6257__isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
6258 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6259{
6260 int i;
6261
6262 map = isl_map_cow(map);
6263 if (!map || !v)
6264 goto error;
6265
6266 if (!isl_val_is_int(v))
6267 isl_die(isl_map_get_ctx(map), isl_error_invalid,do { isl_handle_error(isl_map_get_ctx(map), isl_error_invalid
, "expecting integer value", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 6268); goto error; } while (0)
6268 "expecting integer value", goto error)do { isl_handle_error(isl_map_get_ctx(map), isl_error_invalid
, "expecting integer value", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 6268); goto error; } while (0)
;
6269 if (pos >= isl_map_dim(map, type))
6270 isl_die(isl_map_get_ctx(map), isl_error_invalid,do { isl_handle_error(isl_map_get_ctx(map), isl_error_invalid
, "index out of bounds", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 6271); goto error; } while (0)
6271 "index out of bounds", goto error)do { isl_handle_error(isl_map_get_ctx(map), isl_error_invalid
, "index out of bounds", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 6271); goto error; } while (0)
;
6272 for (i = map->n - 1; i >= 0; --i) {
6273 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
6274 isl_val_copy(v));
6275 if (remove_if_empty(map, i) < 0)
6276 goto error;
6277 }
6278 ISL_F_CLR(map, ISL_MAP_NORMALIZED)(((map)->flags) &= ~((1 << 1)));
6279 isl_val_free(v);
6280 return map;
6281error:
6282 isl_map_free(map);
6283 isl_val_free(v);
6284 return NULL((void*)0);
6285}
6286
6287/* Fix the value of the variable at position "pos" of type "type" of "set"
6288 * to be equal to "v".
6289 */
6290__isl_give isl_setisl_map *isl_set_fix_val(__isl_take isl_setisl_map *set,
6291 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6292{
6293 return isl_map_fix_val(set, type, pos, v);
6294}
6295
6296struct isl_map *isl_map_fix_input_si(struct isl_map *map,
6297 unsigned input, int value)
6298{
6299 return isl_map_fix_si(map, isl_dim_in, input, value);
6300}
6301
6302struct isl_setisl_map *isl_set_fix_dim_si(struct isl_setisl_map *set, unsigned dim, int value)
6303{
6304 return set_from_map(isl_map_fix_si(set_to_map(set),
6305 isl_dim_set, dim, value));
6306}
6307
6308static __isl_give isl_basic_map *basic_map_bound_si(
6309 __isl_take isl_basic_map *bmap,
6310 enum isl_dim_type type, unsigned pos, int value, int upper)
6311{
6312 int j;
6313
6314 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6315 return isl_basic_map_free(bmap);
6316 pos += isl_basic_map_offset(bmap, type);
6317 bmap = isl_basic_map_cow(bmap);
6318 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6319 j = isl_basic_map_alloc_inequality(bmap);
6320 if (j < 0)
6321 goto error;
6322 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
6323 if (upper) {
6324 isl_int_set_si(bmap->ineq[j][pos], -1)isl_sioimath_set_si((bmap->ineq[j][pos]), -1);
6325 isl_int_set_si(bmap->ineq[j][0], value)isl_sioimath_set_si((bmap->ineq[j][0]), value);
6326 } else {
6327 isl_int_set_si(bmap->ineq[j][pos], 1)isl_sioimath_set_si((bmap->ineq[j][pos]), 1);
6328 isl_int_set_si(bmap->ineq[j][0], -value)isl_sioimath_set_si((bmap->ineq[j][0]), -value);
6329 }
6330 bmap = isl_basic_map_simplify(bmap);
6331 return isl_basic_map_finalize(bmap);
6332error:
6333 isl_basic_map_free(bmap);
6334 return NULL((void*)0);
6335}
6336
6337__isl_give isl_basic_map *isl_basic_map_lower_bound_si(
6338 __isl_take isl_basic_map *bmap,
6339 enum isl_dim_type type, unsigned pos, int value)
6340{
6341 return basic_map_bound_si(bmap, type, pos, value, 0);
6342}
6343
6344/* Constrain the values of the given dimension to be no greater than "value".
6345 */
6346__isl_give isl_basic_map *isl_basic_map_upper_bound_si(
6347 __isl_take isl_basic_map *bmap,
6348 enum isl_dim_type type, unsigned pos, int value)
6349{
6350 return basic_map_bound_si(bmap, type, pos, value, 1);
6351}
6352
6353static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
6354 enum isl_dim_type type, unsigned pos, int value, int upper)
6355{
6356 int i;
6357
6358 map = isl_map_cow(map);
6359 if (!map)
6360 return NULL((void*)0);
6361
6362 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error)do { if (pos < isl_map_dim(map, type)) break; do { isl_handle_error
(map->ctx, isl_error_unknown, "Assertion \"" "pos < isl_map_dim(map, type)"
"\" failed", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 6362); goto error; } while (0); } while (0)
;
6363 for (i = 0; i < map->n; ++i) {
6364 map->p[i] = basic_map_bound_si(map->p[i],
6365 type, pos, value, upper);
6366 if (!map->p[i])
6367 goto error;
6368 }
6369 ISL_F_CLR(map, ISL_MAP_NORMALIZED)(((map)->flags) &= ~((1 << 1)));
6370 return map;
6371error:
6372 isl_map_free(map);
6373 return NULL((void*)0);
6374}
6375
6376__isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
6377 enum isl_dim_type type, unsigned pos, int value)
6378{
6379 return map_bound_si(map, type, pos, value, 0);
6380}
6381
6382__isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
6383 enum isl_dim_type type, unsigned pos, int value)
6384{
6385 return map_bound_si(map, type, pos, value, 1);
6386}
6387
6388__isl_give isl_setisl_map *isl_set_lower_bound_si(__isl_take isl_setisl_map *set,
6389 enum isl_dim_type type, unsigned pos, int value)
6390{
6391 return set_from_map(isl_map_lower_bound_si(set_to_map(set),
6392 type, pos, value));
6393}
6394
6395__isl_give isl_setisl_map *isl_set_upper_bound_si(__isl_take isl_setisl_map *set,
6396 enum isl_dim_type type, unsigned pos, int value)
6397{
6398 return isl_map_upper_bound_si(set, type, pos, value);
6399}
6400
6401/* Bound the given variable of "bmap" from below (or above is "upper"
6402 * is set) to "value".
6403 */
6404static __isl_give isl_basic_map *basic_map_bound(
6405 __isl_take isl_basic_map *bmap,
6406 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6407{
6408 int j;
6409
6410 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6411 return isl_basic_map_free(bmap);
6412 pos += isl_basic_map_offset(bmap, type);
6413 bmap = isl_basic_map_cow(bmap);
6414 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6415 j = isl_basic_map_alloc_inequality(bmap);
6416 if (j < 0)
6417 goto error;
6418 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
6419 if (upper) {
6420 isl_int_set_si(bmap->ineq[j][pos], -1)isl_sioimath_set_si((bmap->ineq[j][pos]), -1);
6421 isl_int_set(bmap->ineq[j][0], value)isl_sioimath_set((bmap->ineq[j][0]), *(value));
6422 } else {
6423 isl_int_set_si(bmap->ineq[j][pos], 1)isl_sioimath_set_si((bmap->ineq[j][pos]), 1);
6424 isl_int_neg(bmap->ineq[j][0], value)isl_sioimath_neg((bmap->ineq[j][0]), *(value));
6425 }
6426 bmap = isl_basic_map_simplify(bmap);
6427 return isl_basic_map_finalize(bmap);
6428error:
6429 isl_basic_map_free(bmap);
6430 return NULL((void*)0);
6431}
6432
6433/* Bound the given variable of "map" from below (or above is "upper"
6434 * is set) to "value".
6435 */
6436static __isl_give isl_map *map_bound(__isl_take isl_map *map,
6437 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6438{
6439 int i;
6440
6441 map = isl_map_cow(map);
6442 if (!map)
6443 return NULL((void*)0);
6444
6445 if (pos >= isl_map_dim(map, type))
6446 isl_die(map->ctx, isl_error_invalid,do { isl_handle_error(map->ctx, isl_error_invalid, "index out of bounds"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 6447); goto error; } while (0)
6447 "index out of bounds", goto error)do { isl_handle_error(map->ctx, isl_error_invalid, "index out of bounds"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 6447); goto error; } while (0)
;
6448 for (i = map->n - 1; i >= 0; --i) {
6449 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
6450 if (remove_if_empty(map, i) < 0)
6451 goto error;
6452 }
6453 ISL_F_CLR(map, ISL_MAP_NORMALIZED)(((map)->flags) &= ~((1 << 1)));
6454 return map;
6455error:
6456 isl_map_free(map);
6457 return NULL((void*)0);
6458}
6459
6460__isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
6461 enum isl_dim_type type, unsigned pos, isl_int value)
6462{
6463 return map_bound(map, type, pos, value, 0);
6464}
6465
6466__isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
6467 enum isl_dim_type type, unsigned pos, isl_int value)
6468{
6469 return map_bound(map, type, pos, value, 1);
6470}
6471
6472__isl_give isl_setisl_map *isl_set_lower_bound(__isl_take isl_setisl_map *set,
6473 enum isl_dim_type type, unsigned pos, isl_int value)
6474{
6475 return isl_map_lower_bound(set, type, pos, value);
6476}
6477
6478__isl_give isl_setisl_map *isl_set_upper_bound(__isl_take isl_setisl_map *set,
6479 enum isl_dim_type type, unsigned pos, isl_int value)
6480{
6481 return isl_map_upper_bound(set, type, pos, value);
6482}
6483
6484/* Force the values of the variable at position "pos" of type "type" of "set"
6485 * to be no smaller than "value".
6486 */
6487__isl_give isl_setisl_map *isl_set_lower_bound_val(__isl_take isl_setisl_map *set,
6488 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6489{
6490 if (!value)
6491 goto error;
6492 if (!isl_val_is_int(value))
6493 isl_die(isl_set_get_ctx(set), isl_error_invalid,do { isl_handle_error(isl_set_get_ctx(set), isl_error_invalid
, "expecting integer value", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 6494); goto error; } while (0)
6494 "expecting integer value", goto error)do { isl_handle_error(isl_set_get_ctx(set), isl_error_invalid
, "expecting integer value", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 6494); goto error; } while (0)
;
6495 set = isl_set_lower_bound(set, type, pos, value->n);
6496 isl_val_free(value);
6497 return set;
6498error:
6499 isl_val_free(value);
6500 isl_set_free(set);
6501 return NULL((void*)0);
6502}
6503
6504/* Force the values of the variable at position "pos" of type "type" of "set"
6505 * to be no greater than "value".
6506 */
6507__isl_give isl_setisl_map *isl_set_upper_bound_val(__isl_take isl_setisl_map *set,
6508 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6509{
6510 if (!value)
6511 goto error;
6512 if (!isl_val_is_int(value))
6513 isl_die(isl_set_get_ctx(set), isl_error_invalid,do { isl_handle_error(isl_set_get_ctx(set), isl_error_invalid
, "expecting integer value", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 6514); goto error; } while (0)
6514 "expecting integer value", goto error)do { isl_handle_error(isl_set_get_ctx(set), isl_error_invalid
, "expecting integer value", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 6514); goto error; } while (0)
;
6515 set = isl_set_upper_bound(set, type, pos, value->n);
6516 isl_val_free(value);
6517 return set;
6518error:
6519 isl_val_free(value);
6520 isl_set_free(set);
6521 return NULL((void*)0);
6522}
6523
6524struct isl_map *isl_map_reverse(struct isl_map *map)
6525{
6526 int i;
6527
6528 map = isl_map_cow(map);
6529 if (!map)
6530 return NULL((void*)0);
6531
6532 map->dim = isl_space_reverse(map->dim);
6533 if (!map->dim)
6534 goto error;
6535 for (i = 0; i < map->n; ++i) {
6536 map->p[i] = isl_basic_map_reverse(map->p[i]);
6537 if (!map->p[i])
6538 goto error;
6539 }
6540 ISL_F_CLR(map, ISL_MAP_NORMALIZED)(((map)->flags) &= ~((1 << 1)));
6541 return map;
6542error:
6543 isl_map_free(map);
6544 return NULL((void*)0);
6545}
6546
6547#undef TYPEisl_map
6548#define TYPEisl_map isl_pw_multi_aff
6549#undef SUFFIX
6550#define SUFFIX _pw_multi_aff
6551#undef EMPTYisl_map_empty
6552#define EMPTYisl_map_empty isl_pw_multi_aff_empty
6553#undef ADDisl_map_union_disjoint
6554#define ADDisl_map_union_disjoint isl_pw_multi_aff_union_add
6555#include "isl_map_lexopt_templ.c"
6556
6557/* Given a map "map", compute the lexicographically minimal
6558 * (or maximal) image element for each domain element in dom,
6559 * in the form of an isl_pw_multi_aff.
6560 * If "empty" is not NULL, then set *empty to those elements in dom that
6561 * do not have an image element.
6562 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6563 * should be computed over the domain of "map". "empty" is also NULL
6564 * in this case.
6565 *
6566 * We first compute the lexicographically minimal or maximal element
6567 * in the first basic map. This results in a partial solution "res"
6568 * and a subset "todo" of dom that still need to be handled.
6569 * We then consider each of the remaining maps in "map" and successively
6570 * update both "res" and "todo".
6571 * If "empty" is NULL, then the todo sets are not needed and therefore
6572 * also not computed.
6573 */
6574static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
6575 __isl_take isl_map *map, __isl_take isl_setisl_map *dom,
6576 __isl_give isl_setisl_map **empty, unsigned flags)
6577{
6578 int i;
6579 int full;
6580 isl_pw_multi_aff *res;
6581 isl_setisl_map *todo;
6582
6583 full = ISL_FL_ISSET(flags, ISL_OPT_FULL)(!!((flags) & ((1 << 1))));
6584 if (!map || (!full && !dom))
6585 goto error;
6586
6587 if (isl_map_plain_is_empty(map)) {
6588 if (empty)
6589 *empty = dom;
6590 else
6591 isl_set_free(dom);
6592 return isl_pw_multi_aff_from_map(map);
6593 }
6594
6595 res = basic_map_partial_lexopt_pw_multi_aff(
6596 isl_basic_map_copy(map->p[0]),
6597 isl_set_copy(dom), empty, flags);
6598
6599 if (empty)
6600 todo = *empty;
6601 for (i = 1; i < map->n; ++i) {
6602 isl_pw_multi_aff *res_i;
6603
6604 res_i = basic_map_partial_lexopt_pw_multi_aff(
6605 isl_basic_map_copy(map->p[i]),
6606 isl_set_copy(dom), empty, flags);
6607
6608 if (ISL_FL_ISSET(flags, ISL_OPT_MAX)(!!((flags) & ((1 << 0)))))
6609 res = isl_pw_multi_aff_union_lexmax(res, res_i);
6610 else
6611 res = isl_pw_multi_aff_union_lexmin(res, res_i);
6612
6613 if (empty)
6614 todo = isl_set_intersect(todo, *empty);
6615 }
6616
6617 isl_set_free(dom);
6618 isl_map_free(map);
6619
6620 if (empty)
6621 *empty = todo;
6622
6623 return res;
6624error:
6625 if (empty)
6626 *empty = NULL((void*)0);
6627 isl_set_free(dom);
6628 isl_map_free(map);
6629 return NULL((void*)0);
6630}
6631
6632#undef TYPEisl_map
6633#define TYPEisl_map isl_map
6634#undef SUFFIX
6635#define SUFFIX
6636#undef EMPTYisl_map_empty
6637#define EMPTYisl_map_empty isl_map_empty
6638#undef ADDisl_map_union_disjoint
6639#define ADDisl_map_union_disjoint isl_map_union_disjoint
6640#include "isl_map_lexopt_templ.c"
6641
6642/* Given a map "map", compute the lexicographically minimal
6643 * (or maximal) image element for each domain element in "dom",
6644 * in the form of an isl_map.
6645 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6646 * do not have an image element.
6647 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6648 * should be computed over the domain of "map". "empty" is also NULL
6649 * in this case.
6650 *
6651 * If the input consists of more than one disjunct, then first
6652 * compute the desired result in the form of an isl_pw_multi_aff and
6653 * then convert that into an isl_map.
6654 *
6655 * This function used to have an explicit implementation in terms
6656 * of isl_maps, but it would continually intersect the domains of
6657 * partial results with the complement of the domain of the next
6658 * partial solution, potentially leading to an explosion in the number
6659 * of disjuncts if there are several disjuncts in the input.
6660 * An even earlier implementation of this function would look for
6661 * better results in the domain of the partial result and for extra
6662 * results in the complement of this domain, which would lead to
6663 * even more splintering.
6664 */
6665static __isl_give isl_map *isl_map_partial_lexopt_aligned(
6666 __isl_take isl_map *map, __isl_take isl_setisl_map *dom,
6667 __isl_give isl_setisl_map **empty, unsigned flags)
6668{
6669 int full;
6670 struct isl_map *res;
6671 isl_pw_multi_aff *pma;
6672
6673 full = ISL_FL_ISSET(flags, ISL_OPT_FULL)(!!((flags) & ((1 << 1))));
6674 if (!map || (!full && !dom))
6675 goto error;
6676
6677 if (isl_map_plain_is_empty(map)) {
6678 if (empty)
6679 *empty = dom;
6680 else
6681 isl_set_free(dom);
6682 return map;
6683 }
6684
6685 if (map->n == 1) {
6686 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
6687 dom, empty, flags);
6688 isl_map_free(map);
6689 return res;
6690 }
6691
6692 pma = isl_map_partial_lexopt_aligned_pw_multi_aff(map, dom, empty,
6693 flags);
6694 return isl_map_from_pw_multi_aff(pma);
6695error:
6696 if (empty)
6697 *empty = NULL((void*)0);
6698 isl_set_free(dom);
6699 isl_map_free(map);
6700 return NULL((void*)0);
6701}
6702
6703__isl_give isl_map *isl_map_partial_lexmax(
6704 __isl_take isl_map *map, __isl_take isl_setisl_map *dom,
6705 __isl_give isl_setisl_map **empty)
6706{
6707 return isl_map_partial_lexopt(map, dom, empty, ISL_OPT_MAX(1 << 0));
6708}
6709
6710__isl_give isl_map *isl_map_partial_lexmin(
6711 __isl_take isl_map *map, __isl_take isl_setisl_map *dom,
6712 __isl_give isl_setisl_map **empty)
6713{
6714 return isl_map_partial_lexopt(map, dom, empty, 0);
6715}
6716
6717__isl_give isl_setisl_map *isl_set_partial_lexmin(
6718 __isl_take isl_setisl_map *set, __isl_take isl_setisl_map *dom,
6719 __isl_give isl_setisl_map **empty)
6720{
6721 return set_from_map(isl_map_partial_lexmin(set_to_map(set),
6722 dom, empty));
6723}
6724
6725__isl_give isl_setisl_map *isl_set_partial_lexmax(
6726 __isl_take isl_setisl_map *set, __isl_take isl_setisl_map *dom,
6727 __isl_give isl_setisl_map **empty)
6728{
6729 return set_from_map(isl_map_partial_lexmax(set_to_map(set),
6730 dom, empty));
6731}
6732
6733/* Compute the lexicographic minimum (or maximum if "flags" includes
6734 * ISL_OPT_MAX) of "bset" over its parametric domain.
6735 */
6736__isl_give isl_setisl_map *isl_basic_set_lexopt(__isl_take isl_basic_setisl_basic_map *bset,
6737 unsigned flags)
6738{
6739 return isl_basic_map_lexopt(bset, flags);
6740}
6741
6742__isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
6743{
6744 return isl_basic_map_lexopt(bmap, ISL_OPT_MAX(1 << 0));
6745}
6746
6747__isl_give isl_setisl_map *isl_basic_set_lexmin(__isl_take isl_basic_setisl_basic_map *bset)
6748{
6749 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset)));
6750}
6751
6752__isl_give isl_setisl_map *isl_basic_set_lexmax(__isl_take isl_basic_setisl_basic_map *bset)
6753{
6754 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset)));
6755}
6756
6757/* Compute the lexicographic minimum of "bset" over its parametric domain
6758 * for the purpose of quantifier elimination.
6759 * That is, find an explicit representation for all the existentially
6760 * quantified variables in "bset" by computing their lexicographic
6761 * minimum.
6762 */
6763static __isl_give isl_setisl_map *isl_basic_set_lexmin_compute_divs(
6764 __isl_take isl_basic_setisl_basic_map *bset)
6765{
6766 return isl_basic_set_lexopt(bset, ISL_OPT_QE(1 << 2));
6767}
6768
6769/* Extract the first and only affine expression from list
6770 * and then add it to *pwaff with the given dom.
6771 * This domain is known to be disjoint from other domains
6772 * because of the way isl_basic_map_foreach_lexmax works.
6773 */
6774static isl_stat update_dim_opt(__isl_take isl_basic_setisl_basic_map *dom,
6775 __isl_take isl_aff_list *list, void *user)
6776{
6777 isl_ctx *ctx = isl_basic_set_get_ctx(dom);
6778 isl_aff *aff;
6779 isl_pw_aff **pwaff = user;
6780 isl_pw_aff *pwaff_i;
6781
6782 if (!list)
6783 goto error;
6784 if (isl_aff_list_n_aff(list) != 1)
6785 isl_die(ctx, isl_error_internal,do { isl_handle_error(ctx, isl_error_internal, "expecting single element list"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 6786); goto error; } while (0)
6786 "expecting single element list", goto error)do { isl_handle_error(ctx, isl_error_internal, "expecting single element list"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 6786); goto error; } while (0)
;
6787
6788 aff = isl_aff_list_get_aff(list, 0);
6789 pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
6790
6791 *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
6792
6793 isl_aff_list_free(list);
6794
6795 return isl_stat_ok;
6796error:
6797 isl_basic_set_free(dom);
6798 isl_aff_list_free(list);
6799 return isl_stat_error;
6800}
6801
6802/* Given a basic map with one output dimension, compute the minimum or
6803 * maximum of that dimension as an isl_pw_aff.
6804 *
6805 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6806 * call update_dim_opt on each leaf of the result.
6807 */
6808static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
6809 int max)
6810{
6811 isl_space *dim = isl_basic_map_get_space(bmap);
6812 isl_pw_aff *pwaff;
6813 isl_stat r;
6814
6815 dim = isl_space_from_domain(isl_space_domain(dim));
6816 dim = isl_space_add_dims(dim, isl_dim_out, 1);
6817 pwaff = isl_pw_aff_empty(dim);
6818
6819 r = isl_basic_map_foreach_lexopt(bmap, max, &update_dim_opt, &pwaff);
6820 if (r < 0)
6821 return isl_pw_aff_free(pwaff);
6822
6823 return pwaff;
6824}
6825
6826/* Compute the minimum or maximum of the given output dimension
6827 * as a function of the parameters and the input dimensions,
6828 * but independently of the other output dimensions.
6829 *
6830 * We first project out the other output dimension and then compute
6831 * the "lexicographic" maximum in each basic map, combining the results
6832 * using isl_pw_aff_union_max.
6833 */
6834static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
6835 int max)
6836{
6837 int i;
6838 isl_pw_aff *pwaff;
6839 unsigned n_out;
6840
6841 n_out = isl_map_dim(map, isl_dim_out);
6842 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
6843 map = isl_map_project_out(map, isl_dim_out, 0, pos);
6844 if (!map)
6845 return NULL((void*)0);
6846
6847 if (map->n == 0) {
6848 isl_space *dim = isl_map_get_space(map);
6849 isl_map_free(map);
6850 return isl_pw_aff_empty(dim);
6851 }
6852
6853 pwaff = basic_map_dim_opt(map->p[0], max);
6854 for (i = 1; i < map->n; ++i) {
6855 isl_pw_aff *pwaff_i;
6856
6857 pwaff_i = basic_map_dim_opt(map->p[i], max);
6858 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
6859 }
6860
6861 isl_map_free(map);
6862
6863 return pwaff;
6864}
6865
6866/* Compute the minimum of the given output dimension as a function of the
6867 * parameters and input dimensions, but independently of
6868 * the other output dimensions.
6869 */
6870__isl_give isl_pw_aff *isl_map_dim_min(__isl_take isl_map *map, int pos)
6871{
6872 return map_dim_opt(map, pos, 0);
6873}
6874
6875/* Compute the maximum of the given output dimension as a function of the
6876 * parameters and input dimensions, but independently of
6877 * the other output dimensions.
6878 */
6879__isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
6880{
6881 return map_dim_opt(map, pos, 1);
6882}
6883
6884/* Compute the minimum or maximum of the given set dimension
6885 * as a function of the parameters,
6886 * but independently of the other set dimensions.
6887 */
6888static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_setisl_map *set, int pos,
6889 int max)
6890{
6891 return map_dim_opt(set, pos, max);
6892}
6893
6894/* Compute the maximum of the given set dimension as a function of the
6895 * parameters, but independently of the other set dimensions.
6896 */
6897__isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_setisl_map *set, int pos)
6898{
6899 return set_dim_opt(set, pos, 1);
6900}
6901
6902/* Compute the minimum of the given set dimension as a function of the
6903 * parameters, but independently of the other set dimensions.
6904 */
6905__isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_setisl_map *set, int pos)
6906{
6907 return set_dim_opt(set, pos, 0);
6908}
6909
6910/* Apply a preimage specified by "mat" on the parameters of "bset".
6911 * bset is assumed to have only parameters and divs.
6912 */
6913static struct isl_basic_setisl_basic_map *basic_set_parameter_preimage(
6914 struct isl_basic_setisl_basic_map *bset, struct isl_mat *mat)
6915{
6916 unsigned nparam;
6917
6918 if (!bset || !mat)
6919 goto error;
6920
6921 bset->dim = isl_space_cow(bset->dim);
6922 if (!bset->dim)
6923 goto error;
6924
6925 nparam = isl_basic_set_dim(bset, isl_dim_param);
6926
6927 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error)do { if (mat->n_row == 1 + nparam) break; do { isl_handle_error
(bset->ctx, isl_error_unknown, "Assertion \"" "mat->n_row == 1 + nparam"
"\" failed", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 6927); goto error; } while (0); } while (0)
;
6928
6929 bset->dim->nparam = 0;
6930 bset->dim->n_out = nparam;
6931 bset = isl_basic_set_preimage(bset, mat);
6932 if (bset) {
6933 bset->dim->nparam = bset->dim->n_out;
6934 bset->dim->n_out = 0;
6935 }
6936 return bset;
6937error:
6938 isl_mat_free(mat);
6939 isl_basic_set_free(bset);
6940 return NULL((void*)0);
6941}
6942
6943/* Apply a preimage specified by "mat" on the parameters of "set".
6944 * set is assumed to have only parameters and divs.
6945 */
6946static __isl_give isl_setisl_map *set_parameter_preimage(__isl_take isl_setisl_map *set,
6947 __isl_take isl_mat *mat)
6948{
6949 isl_space *space;
6950 unsigned nparam;
6951
6952 if (!set || !mat)
6953 goto error;
6954
6955 nparam = isl_set_dim(set, isl_dim_param);
6956
6957 if (mat->n_row != 1 + nparam)
6958 isl_die(isl_set_get_ctx(set), isl_error_internal,do { isl_handle_error(isl_set_get_ctx(set), isl_error_internal
, "unexpected number of rows", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 6959); goto error; } while (0)
6959 "unexpected number of rows", goto error)do { isl_handle_error(isl_set_get_ctx(set), isl_error_internal
, "unexpected number of rows", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 6959); goto error; } while (0)
;
6960
6961 space = isl_set_get_space(set);
6962 space = isl_space_move_dims(space, isl_dim_set, 0,
6963 isl_dim_param, 0, nparam);
6964 set = isl_set_reset_space(set, space);
6965 set = isl_set_preimage(set, mat);
6966 nparam = isl_set_dim(set, isl_dim_out);
6967 space = isl_set_get_space(set);
6968 space = isl_space_move_dims(space, isl_dim_param, 0,
6969 isl_dim_out, 0, nparam);
6970 set = isl_set_reset_space(set, space);
6971 return set;
6972error:
6973 isl_mat_free(mat);
6974 isl_set_free(set);
6975 return NULL((void*)0);
6976}
6977
6978/* Intersect the basic set "bset" with the affine space specified by the
6979 * equalities in "eq".
6980 */
6981static struct isl_basic_setisl_basic_map *basic_set_append_equalities(
6982 struct isl_basic_setisl_basic_map *bset, struct isl_mat *eq)
6983{
6984 int i, k;
6985 unsigned len;
6986
6987 if (!bset || !eq)
6988 goto error;
6989
6990 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
6991 eq->n_row, 0);
6992 if (!bset)
6993 goto error;
6994
6995 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
6996 for (i = 0; i < eq->n_row; ++i) {
6997 k = isl_basic_set_alloc_equality(bset);
6998 if (k < 0)
6999 goto error;
7000 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
7001 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
7002 }
7003 isl_mat_free(eq);
7004
7005 bset = isl_basic_set_gauss(bset, NULL((void*)0));
7006 bset = isl_basic_set_finalize(bset);
7007
7008 return bset;
7009error:
7010 isl_mat_free(eq);
7011 isl_basic_set_free(bset);
7012 return NULL((void*)0);
7013}
7014
7015/* Intersect the set "set" with the affine space specified by the
7016 * equalities in "eq".
7017 */
7018static struct isl_setisl_map *set_append_equalities(struct isl_setisl_map *set,
7019 struct isl_mat *eq)
7020{
7021 int i;
7022
7023 if (!set || !eq)
7024 goto error;
7025
7026 for (i = 0; i < set->n; ++i) {
7027 set->p[i] = basic_set_append_equalities(set->p[i],
7028 isl_mat_copy(eq));
7029 if (!set->p[i])
7030 goto error;
7031 }
7032 isl_mat_free(eq);
7033 return set;
7034error:
7035 isl_mat_free(eq);
7036 isl_set_free(set);
7037 return NULL((void*)0);
7038}
7039
7040/* Given a basic set "bset" that only involves parameters and existentially
7041 * quantified variables, return the index of the first equality
7042 * that only involves parameters. If there is no such equality then
7043 * return bset->n_eq.
7044 *
7045 * This function assumes that isl_basic_set_gauss has been called on "bset".
7046 */
7047static int first_parameter_equality(__isl_keep isl_basic_setisl_basic_map *bset)
7048{
7049 int i, j;
7050 unsigned nparam, n_div;
7051
7052 if (!bset)
7053 return -1;
7054
7055 nparam = isl_basic_set_dim(bset, isl_dim_param);
7056 n_div = isl_basic_set_dim(bset, isl_dim_div);
7057
7058 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
7059 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j])(isl_sioimath_sgn(*(bset->eq[i][1 + nparam + j])) == 0))
7060 ++i;
7061 }
7062
7063 return i;
7064}
7065
7066/* Compute an explicit representation for the existentially quantified
7067 * variables in "bset" by computing the "minimal value" of the set
7068 * variables. Since there are no set variables, the computation of
7069 * the minimal value essentially computes an explicit representation
7070 * of the non-empty part(s) of "bset".
7071 *
7072 * The input only involves parameters and existentially quantified variables.
7073 * All equalities among parameters have been removed.
7074 *
7075 * Since the existentially quantified variables in the result are in general
7076 * going to be different from those in the input, we first replace
7077 * them by the minimal number of variables based on their equalities.
7078 * This should simplify the parametric integer programming.
7079 */
7080static __isl_give isl_setisl_map *base_compute_divs(__isl_take isl_basic_setisl_basic_map *bset)
7081{
7082 isl_morph *morph1, *morph2;
7083 isl_setisl_map *set;
7084 unsigned n;
7085
7086 if (!bset)
7087 return NULL((void*)0);
7088 if (bset->n_eq == 0)
7089 return isl_basic_set_lexmin_compute_divs(bset);
7090
7091 morph1 = isl_basic_set_parameter_compression(bset);
7092 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
7093 bset = isl_basic_set_lift(bset);
7094 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
7095 bset = isl_morph_basic_set(morph2, bset);
7096 n = isl_basic_set_dim(bset, isl_dim_set);
7097 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
7098
7099 set = isl_basic_set_lexmin_compute_divs(bset);
7100
7101 set = isl_morph_set(isl_morph_inverse(morph1), set);
7102
7103 return set;
7104}
7105
7106/* Project the given basic set onto its parameter domain, possibly introducing
7107 * new, explicit, existential variables in the constraints.
7108 * The input has parameters and (possibly implicit) existential variables.
7109 * The output has the same parameters, but only
7110 * explicit existentially quantified variables.
7111 *
7112 * The actual projection is performed by pip, but pip doesn't seem
7113 * to like equalities very much, so we first remove the equalities
7114 * among the parameters by performing a variable compression on
7115 * the parameters. Afterward, an inverse transformation is performed
7116 * and the equalities among the parameters are inserted back in.
7117 *
7118 * The variable compression on the parameters may uncover additional
7119 * equalities that were only implicit before. We therefore check
7120 * if there are any new parameter equalities in the result and
7121 * if so recurse. The removal of parameter equalities is required
7122 * for the parameter compression performed by base_compute_divs.
7123 */
7124static struct isl_setisl_map *parameter_compute_divs(struct isl_basic_setisl_basic_map *bset)
7125{
7126 int i;
7127 struct isl_mat *eq;
7128 struct isl_mat *T, *T2;
7129 struct isl_setisl_map *set;
7130 unsigned nparam;
7131
7132 bset = isl_basic_set_cow(bset);
7133 if (!bset)
7134 return NULL((void*)0);
7135
7136 if (bset->n_eq == 0)
7137 return base_compute_divs(bset);
7138
7139 bset = isl_basic_set_gauss(bset, NULL((void*)0));
7140 if (!bset)
7141 return NULL((void*)0);
7142 if (isl_basic_set_plain_is_empty(bset))
7143 return isl_set_from_basic_set(bset);
7144
7145 i = first_parameter_equality(bset);
7146 if (i == bset->n_eq)
7147 return base_compute_divs(bset);
7148
7149 nparam = isl_basic_set_dim(bset, isl_dim_param);
7150 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
7151 0, 1 + nparam);
7152 eq = isl_mat_cow(eq);
7153 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
7154 if (T && T->n_col == 0) {
7155 isl_mat_free(T);
7156 isl_mat_free(T2);
7157 isl_mat_free(eq);
7158 bset = isl_basic_set_set_to_empty(bset);
7159 return isl_set_from_basic_set(bset);
7160 }
7161 bset = basic_set_parameter_preimage(bset, T);
7162
7163 i = first_parameter_equality(bset);
7164 if (!bset)
7165 set = NULL((void*)0);
7166 else if (i == bset->n_eq)
7167 set = base_compute_divs(bset);
7168 else
7169 set = parameter_compute_divs(bset);
7170 set = set_parameter_preimage(set, T2);
7171 set = set_append_equalities(set, eq);
7172 return set;
7173}
7174
7175/* Insert the divs from "ls" before those of "bmap".
7176 *
7177 * The number of columns is not changed, which means that the last
7178 * dimensions of "bmap" are being reintepreted as the divs from "ls".
7179 * The caller is responsible for removing the same number of dimensions
7180 * from the space of "bmap".
7181 */
7182static __isl_give isl_basic_map *insert_divs_from_local_space(
7183 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
7184{
7185 int i;
7186 int n_div;
7187 int old_n_div;
7188
7189 n_div = isl_local_space_dim(ls, isl_dim_div);
7190 if (n_div == 0)
7191 return bmap;
7192
7193 old_n_div = bmap->n_div;
7194 bmap = insert_div_rows(bmap, n_div);
7195 if (!bmap)
7196 return NULL((void*)0);
7197
7198 for (i = 0; i < n_div; ++i) {
7199 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
7200 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
7201 }
7202
7203 return bmap;
7204}
7205
7206/* Replace the space of "bmap" by the space and divs of "ls".
7207 *
7208 * If "ls" has any divs, then we simplify the result since we may
7209 * have discovered some additional equalities that could simplify
7210 * the div expressions.
7211 */
7212static __isl_give isl_basic_map *basic_replace_space_by_local_space(
7213 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
7214{
7215 int n_div;
7216
7217 bmap = isl_basic_map_cow(bmap);
7218 if (!bmap || !ls)
7219 goto error;
7220
7221 n_div = isl_local_space_dim(ls, isl_dim_div);
7222 bmap = insert_divs_from_local_space(bmap, ls);
7223 if (!bmap)
7224 goto error;
7225
7226 isl_space_free(bmap->dim);
7227 bmap->dim = isl_local_space_get_space(ls);
7228 if (!bmap->dim)
7229 goto error;
7230
7231 isl_local_space_free(ls);
7232 if (n_div > 0)
7233 bmap = isl_basic_map_simplify(bmap);
7234 bmap = isl_basic_map_finalize(bmap);
7235 return bmap;
7236error:
7237 isl_basic_map_free(bmap);
7238 isl_local_space_free(ls);
7239 return NULL((void*)0);
7240}
7241
7242/* Replace the space of "map" by the space and divs of "ls".
7243 */
7244static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
7245 __isl_take isl_local_space *ls)
7246{
7247 int i;
7248
7249 map = isl_map_cow(map);
7250 if (!map || !ls)
7251 goto error;
7252
7253 for (i = 0; i < map->n; ++i) {
7254 map->p[i] = basic_replace_space_by_local_space(map->p[i],
7255 isl_local_space_copy(ls));
7256 if (!map->p[i])
7257 goto error;
7258 }
7259 isl_space_free(map->dim);
7260 map->dim = isl_local_space_get_space(ls);
7261 if (!map->dim)
7262 goto error;
7263
7264 isl_local_space_free(ls);
7265 return map;
7266error:
7267 isl_local_space_free(ls);
7268 isl_map_free(map);
7269 return NULL((void*)0);
7270}
7271
7272/* Compute an explicit representation for the existentially
7273 * quantified variables for which do not know any explicit representation yet.
7274 *
7275 * We first sort the existentially quantified variables so that the
7276 * existentially quantified variables for which we already have an explicit
7277 * representation are placed before those for which we do not.
7278 * The input dimensions, the output dimensions and the existentially
7279 * quantified variables for which we already have an explicit
7280 * representation are then turned into parameters.
7281 * compute_divs returns a map with the same parameters and
7282 * no input or output dimensions and the dimension specification
7283 * is reset to that of the input, including the existentially quantified
7284 * variables for which we already had an explicit representation.
7285 */
7286static struct isl_map *compute_divs(struct isl_basic_map *bmap)
7287{
7288 struct isl_basic_setisl_basic_map *bset;
7289 struct isl_setisl_map *set;
7290 struct isl_map *map;
7291 isl_space *dim;
7292 isl_local_space *ls;
7293 unsigned nparam;
7294 unsigned n_in;
7295 unsigned n_out;
7296 int n_known;
7297 int i;
7298
7299 bmap = isl_basic_map_sort_divs(bmap);
7300 bmap = isl_basic_map_cow(bmap);
7301 if (!bmap)
7302 return NULL((void*)0);
7303
7304 n_known = isl_basic_map_first_unknown_div(bmap);
7305 if (n_known < 0)
7306 return isl_map_from_basic_map(isl_basic_map_free(bmap));
7307
7308 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7309 n_in = isl_basic_map_dim(bmap, isl_dim_in);
7310 n_out = isl_basic_map_dim(bmap, isl_dim_out);
7311 dim = isl_space_set_alloc(bmap->ctx,
7312 nparam + n_in + n_out + n_known, 0);
7313 if (!dim)
7314 goto error;
7315
7316 ls = isl_basic_map_get_local_space(bmap);
7317 ls = isl_local_space_drop_dims(ls, isl_dim_div,
7318 n_known, bmap->n_div - n_known);
7319 if (n_known > 0) {
7320 for (i = n_known; i < bmap->n_div; ++i)
7321 swap_div(bmap, i - n_known, i);
7322 bmap->n_div -= n_known;
7323 bmap->extra -= n_known;
7324 }
7325 bmap = isl_basic_map_reset_space(bmap, dim);
7326 bset = bset_from_bmap(bmap);
7327
7328 set = parameter_compute_divs(bset);
7329 map = set_to_map(set);
7330 map = replace_space_by_local_space(map, ls);
7331
7332 return map;
7333error:
7334 isl_basic_map_free(bmap);
7335 return NULL((void*)0);
7336}
7337
7338/* Remove the explicit representation of local variable "div",
7339 * if there is any.
7340 */
7341__isl_give isl_basic_map *isl_basic_map_mark_div_unknown(
7342 __isl_take isl_basic_map *bmap, int div)
7343{
7344 isl_bool unknown;
7345
7346 unknown = isl_basic_map_div_is_marked_unknown(bmap, div);
7347 if (unknown < 0)
7348 return isl_basic_map_free(bmap);
7349 if (unknown)
7350 return bmap;
7351
7352 bmap = isl_basic_map_cow(bmap);
7353 if (!bmap)
7354 return NULL((void*)0);
7355 isl_int_set_si(bmap->div[div][0], 0)isl_sioimath_set_si((bmap->div[div][0]), 0);
7356 return bmap;
7357}
7358
7359/* Is local variable "div" of "bmap" marked as not having an explicit
7360 * representation?
7361 * Note that even if "div" is not marked in this way and therefore
7362 * has an explicit representation, this representation may still
7363 * depend (indirectly) on other local variables that do not
7364 * have an explicit representation.
7365 */
7366isl_bool isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map *bmap,
7367 int div)
7368{
7369 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
7370 return isl_bool_error;
7371 return isl_int_is_zero(bmap->div[div][0])(isl_sioimath_sgn(*(bmap->div[div][0])) == 0);
7372}
7373
7374/* Return the position of the first local variable that does not
7375 * have an explicit representation.
7376 * Return the total number of local variables if they all have
7377 * an explicit representation.
7378 * Return -1 on error.
7379 */
7380int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map *bmap)
7381{
7382 int i;
7383
7384 if (!bmap)
7385 return -1;
7386
7387 for (i = 0; i < bmap->n_div; ++i) {
7388 if (!isl_basic_map_div_is_known(bmap, i))
7389 return i;
7390 }
7391 return bmap->n_div;
7392}
7393
7394/* Return the position of the first local variable that does not
7395 * have an explicit representation.
7396 * Return the total number of local variables if they all have
7397 * an explicit representation.
7398 * Return -1 on error.
7399 */
7400int isl_basic_set_first_unknown_div(__isl_keep isl_basic_setisl_basic_map *bset)
7401{
7402 return isl_basic_map_first_unknown_div(bset);
7403}
7404
7405/* Does "bmap" have an explicit representation for all local variables?
7406 */
7407isl_bool isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
7408{
7409 int first, n;
7410
7411 n = isl_basic_map_dim(bmap, isl_dim_div);
7412 first = isl_basic_map_first_unknown_div(bmap);
7413 if (first < 0)
7414 return isl_bool_error;
7415 return first == n;
7416}
7417
7418/* Do all basic maps in "map" have an explicit representation
7419 * for all local variables?
7420 */
7421isl_bool isl_map_divs_known(__isl_keep isl_map *map)
7422{
7423 int i;
7424
7425 if (!map)
7426 return isl_bool_error;
7427
7428 for (i = 0; i < map->n; ++i) {
7429 int known = isl_basic_map_divs_known(map->p[i]);
7430 if (known <= 0)
7431 return known;
7432 }
7433
7434 return isl_bool_true;
7435}
7436
7437/* If bmap contains any unknown divs, then compute explicit
7438 * expressions for them. However, this computation may be
7439 * quite expensive, so first try to remove divs that aren't
7440 * strictly needed.
7441 */
7442struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
7443{
7444 int known;
7445 struct isl_map *map;
7446
7447 known = isl_basic_map_divs_known(bmap);
7448 if (known < 0)
7449 goto error;
7450 if (known)
7451 return isl_map_from_basic_map(bmap);
7452
7453 bmap = isl_basic_map_drop_redundant_divs(bmap);
7454
7455 known = isl_basic_map_divs_known(bmap);
7456 if (known < 0)
7457 goto error;
7458 if (known)
7459 return isl_map_from_basic_map(bmap);
7460
7461 map = compute_divs(bmap);
7462 return map;
7463error:
7464 isl_basic_map_free(bmap);
7465 return NULL((void*)0);
7466}
7467
7468struct isl_map *isl_map_compute_divs(struct isl_map *map)
7469{
7470 int i;
7471 int known;
7472 struct isl_map *res;
7473
7474 if (!map)
7475 return NULL((void*)0);
7476 if (map->n == 0)
7477 return map;
7478
7479 known = isl_map_divs_known(map);
7480 if (known < 0) {
7481 isl_map_free(map);
7482 return NULL((void*)0);
7483 }
7484 if (known)
7485 return map;
7486
7487 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
7488 for (i = 1 ; i < map->n; ++i) {
7489 struct isl_map *r2;
7490 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
7491 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT)(!!(((map)->flags) & ((1 << 0)))))
7492 res = isl_map_union_disjoint(res, r2);
7493 else
7494 res = isl_map_union(res, r2);
7495 }
7496 isl_map_free(map);
7497
7498 return res;
7499}
7500
7501struct isl_setisl_map *isl_basic_set_compute_divs(struct isl_basic_setisl_basic_map *bset)
7502{
7503 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset)));
7504}
7505
7506struct isl_setisl_map *isl_set_compute_divs(struct isl_setisl_map *set)
7507{
7508 return set_from_map(isl_map_compute_divs(set_to_map(set)));
7509}
7510
7511struct isl_setisl_map *isl_map_domain(struct isl_map *map)
7512{
7513 int i;
7514 struct isl_setisl_map *set;
7515
7516 if (!map)
7517 goto error;
7518
7519 map = isl_map_cow(map);
7520 if (!map)
7521 return NULL((void*)0);
7522
7523 set = set_from_map(map);
7524 set->dim = isl_space_domain(set->dim);
7525 if (!set->dim)
7526 goto error;
7527 for (i = 0; i < map->n; ++i) {
7528 set->p[i] = isl_basic_map_domain(map->p[i]);
7529 if (!set->p[i])
7530 goto error;
7531 }
7532 ISL_F_CLR(set, ISL_MAP_DISJOINT)(((set)->flags) &= ~((1 << 0)));
7533 ISL_F_CLR(set, ISL_SET_NORMALIZED)(((set)->flags) &= ~((1 << 1)));
7534 return set;
7535error:
7536 isl_map_free(map);
7537 return NULL((void*)0);
7538}
7539
7540/* Return the union of "map1" and "map2", where we assume for now that
7541 * "map1" and "map2" are disjoint. Note that the basic maps inside
7542 * "map1" or "map2" may not be disjoint from each other.
7543 * Also note that this function is also called from isl_map_union,
7544 * which takes care of handling the situation where "map1" and "map2"
7545 * may not be disjoint.
7546 *
7547 * If one of the inputs is empty, we can simply return the other input.
7548 * Similarly, if one of the inputs is universal, then it is equal to the union.
7549 */
7550static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
7551 __isl_take isl_map *map2)
7552{
7553 int i;
7554 unsigned flags = 0;
7555 struct isl_map *map = NULL((void*)0);
7556 int is_universe;
7557
7558 if (!map1 || !map2)
7559 goto error;
7560
7561 if (!isl_space_is_equal(map1->dim, map2->dim))
7562 isl_die(isl_map_get_ctx(map1), isl_error_invalid,do { isl_handle_error(isl_map_get_ctx(map1), isl_error_invalid
, "spaces don't match", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 7563); goto error; } while (0)
7563 "spaces don't match", goto error)do { isl_handle_error(isl_map_get_ctx(map1), isl_error_invalid
, "spaces don't match", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 7563); goto error; } while (0)
;
7564
7565 if (map1->n == 0) {
7566 isl_map_free(map1);
7567 return map2;
7568 }
7569 if (map2->n == 0) {
7570 isl_map_free(map2);
7571 return map1;
7572 }
7573
7574 is_universe = isl_map_plain_is_universe(map1);
7575 if (is_universe < 0)
7576 goto error;
7577 if (is_universe) {
7578 isl_map_free(map2);
7579 return map1;
7580 }
7581
7582 is_universe = isl_map_plain_is_universe(map2);
7583 if (is_universe < 0)
7584 goto error;
7585 if (is_universe) {
7586 isl_map_free(map1);
7587 return map2;
7588 }
7589
7590 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT)(!!(((map1)->flags) & ((1 << 0)))) &&
7591 ISL_F_ISSET(map2, ISL_MAP_DISJOINT)(!!(((map2)->flags) & ((1 << 0)))))
7592 ISL_FL_SET(flags, ISL_MAP_DISJOINT)((flags) |= ((1 << 0)));
7593
7594 map = isl_map_alloc_space(isl_space_copy(map1->dim),
7595 map1->n + map2->n, flags);
7596 if (!map)
7597 goto error;
7598 for (i = 0; i < map1->n; ++i) {
7599 map = isl_map_add_basic_map(map,
7600 isl_basic_map_copy(map1->p[i]));
7601 if (!map)
7602 goto error;
7603 }
7604 for (i = 0; i < map2->n; ++i) {
7605 map = isl_map_add_basic_map(map,
7606 isl_basic_map_copy(map2->p[i]));
7607 if (!map)
7608 goto error;
7609 }
7610 isl_map_free(map1);
7611 isl_map_free(map2);
7612 return map;
7613error:
7614 isl_map_free(map);
7615 isl_map_free(map1);
7616 isl_map_free(map2);
7617 return NULL((void*)0);
7618}
7619
7620/* Return the union of "map1" and "map2", where "map1" and "map2" are
7621 * guaranteed to be disjoint by the caller.
7622 *
7623 * Note that this functions is called from within isl_map_make_disjoint,
7624 * so we have to be careful not to touch the constraints of the inputs
7625 * in any way.
7626 */
7627__isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
7628 __isl_take isl_map *map2)
7629{
7630 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
7631}
7632
7633/* Return the union of "map1" and "map2", where "map1" and "map2" may
7634 * not be disjoint. The parameters are assumed to have been aligned.
7635 *
7636 * We currently simply call map_union_disjoint, the internal operation
7637 * of which does not really depend on the inputs being disjoint.
7638 * If the result contains more than one basic map, then we clear
7639 * the disjoint flag since the result may contain basic maps from
7640 * both inputs and these are not guaranteed to be disjoint.
7641 *
7642 * As a special case, if "map1" and "map2" are obviously equal,
7643 * then we simply return "map1".
7644 */
7645static __isl_give isl_map *map_union_aligned(__isl_take isl_map *map1,
7646 __isl_take isl_map *map2)
7647{
7648 int equal;
7649
7650 if (!map1 || !map2)
7651 goto error;
7652
7653 equal = isl_map_plain_is_equal(map1, map2);
7654 if (equal < 0)
7655 goto error;
7656 if (equal) {
7657 isl_map_free(map2);
7658 return map1;
7659 }
7660
7661 map1 = map_union_disjoint(map1, map2);
7662 if (!map1)
7663 return NULL((void*)0);
7664 if (map1->n > 1)
7665 ISL_F_CLR(map1, ISL_MAP_DISJOINT)(((map1)->flags) &= ~((1 << 0)));
7666 return map1;
7667error:
7668 isl_map_free(map1);
7669 isl_map_free(map2);
7670 return NULL((void*)0);
7671}
7672
7673/* Return the union of "map1" and "map2", where "map1" and "map2" may
7674 * not be disjoint.
7675 */
7676__isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
7677 __isl_take isl_map *map2)
7678{
7679 return isl_map_align_params_map_map_and(map1, map2, &map_union_aligned);
7680}
7681
7682struct isl_setisl_map *isl_set_union_disjoint(
7683 struct isl_setisl_map *set1, struct isl_setisl_map *set2)
7684{
7685 return set_from_map(isl_map_union_disjoint(set_to_map(set1),
7686 set_to_map(set2)));
7687}
7688
7689struct isl_setisl_map *isl_set_union(struct isl_setisl_map *set1, struct isl_setisl_map *set2)
7690{
7691 return set_from_map(isl_map_union(set_to_map(set1), set_to_map(set2)));
7692}
7693
7694/* Apply "fn" to pairs of elements from "map" and "set" and collect
7695 * the results.
7696 *
7697 * "map" and "set" are assumed to be compatible and non-NULL.
7698 */
7699static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
7700 __isl_take isl_setisl_map *set,
7701 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
7702 __isl_take isl_basic_setisl_basic_map *bset))
7703{
7704 unsigned flags = 0;
7705 struct isl_map *result;
7706 int i, j;
7707
7708 if (isl_set_plain_is_universe(set)) {
7709 isl_set_free(set);
7710 return map;
7711 }
7712
7713 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT)(!!(((map)->flags) & ((1 << 0)))) &&
7714 ISL_F_ISSET(set, ISL_MAP_DISJOINT)(!!(((set)->flags) & ((1 << 0)))))
7715 ISL_FL_SET(flags, ISL_MAP_DISJOINT)((flags) |= ((1 << 0)));
7716
7717 result = isl_map_alloc_space(isl_space_copy(map->dim),
7718 map->n * set->n, flags);
7719 for (i = 0; result && i < map->n; ++i)
7720 for (j = 0; j < set->n; ++j) {
7721 result = isl_map_add_basic_map(result,
7722 fn(isl_basic_map_copy(map->p[i]),
7723 isl_basic_set_copy(set->p[j])));
7724 if (!result)
7725 break;
7726 }
7727
7728 isl_map_free(map);
7729 isl_set_free(set);
7730 return result;
7731}
7732
7733static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
7734 __isl_take isl_setisl_map *set)
7735{
7736 isl_bool ok;
7737
7738 ok = isl_map_compatible_range(map, set);
7739 if (ok < 0)
7740 goto error;
7741 if (!ok)
7742 isl_die(set->ctx, isl_error_invalid,do { isl_handle_error(set->ctx, isl_error_invalid, "incompatible spaces"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 7743); goto error; } while (0)
7743 "incompatible spaces", goto error)do { isl_handle_error(set->ctx, isl_error_invalid, "incompatible spaces"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 7743); goto error; } while (0)
;
7744
7745 return map_intersect_set(map, set, &isl_basic_map_intersect_range);
7746error:
7747 isl_map_free(map);
7748 isl_set_free(set);
7749 return NULL((void*)0);
7750}
7751
7752__isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
7753 __isl_take isl_setisl_map *set)
7754{
7755 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
7756}
7757
7758static __isl_give isl_map *map_intersect_domain(__isl_take isl_map *map,
7759 __isl_take isl_setisl_map *set)
7760{
7761 isl_bool ok;
7762
7763 ok = isl_map_compatible_domain(map, set);
7764 if (ok < 0)
7765 goto error;
7766 if (!ok)
7767 isl_die(set->ctx, isl_error_invalid,do { isl_handle_error(set->ctx, isl_error_invalid, "incompatible spaces"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 7768); goto error; } while (0)
7768 "incompatible spaces", goto error)do { isl_handle_error(set->ctx, isl_error_invalid, "incompatible spaces"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 7768); goto error; } while (0)
;
7769
7770 return map_intersect_set(map, set, &isl_basic_map_intersect_domain);
7771error:
7772 isl_map_free(map);
7773 isl_set_free(set);
7774 return NULL((void*)0);
7775}
7776
7777__isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
7778 __isl_take isl_setisl_map *set)
7779{
7780 return isl_map_align_params_map_map_and(map, set,
7781 &map_intersect_domain);
7782}
7783
7784static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
7785 __isl_take isl_map *map2)
7786{
7787 if (!map1 || !map2)
7788 goto error;
7789 map1 = isl_map_reverse(map1);
7790 map1 = isl_map_apply_range(map1, map2);
7791 return isl_map_reverse(map1);
7792error:
7793 isl_map_free(map1);
7794 isl_map_free(map2);
7795 return NULL((void*)0);
7796}
7797
7798__isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
7799 __isl_take isl_map *map2)
7800{
7801 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
7802}
7803
7804static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
7805 __isl_take isl_map *map2)
7806{
7807 isl_space *dim_result;
7808 struct isl_map *result;
7809 int i, j;
7810
7811 if (!map1 || !map2)
7812 goto error;
7813
7814 dim_result = isl_space_join(isl_space_copy(map1->dim),
7815 isl_space_copy(map2->dim));
7816
7817 result = isl_map_alloc_space(dim_result, map1->n * map2->n, 0);
7818 if (!result)
7819 goto error;
7820 for (i = 0; i < map1->n; ++i)
7821 for (j = 0; j < map2->n; ++j) {
7822 result = isl_map_add_basic_map(result,
7823 isl_basic_map_apply_range(
7824 isl_basic_map_copy(map1->p[i]),
7825 isl_basic_map_copy(map2->p[j])));
7826 if (!result)
7827 goto error;
7828 }
7829 isl_map_free(map1);
7830 isl_map_free(map2);
7831 if (result && result->n <= 1)
7832 ISL_F_SET(result, ISL_MAP_DISJOINT)(((result)->flags) |= ((1 << 0)));
7833 return result;
7834error:
7835 isl_map_free(map1);
7836 isl_map_free(map2);
7837 return NULL((void*)0);
7838}
7839
7840__isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
7841 __isl_take isl_map *map2)
7842{
7843 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
7844}
7845
7846/*
7847 * returns range - domain
7848 */
7849struct isl_basic_setisl_basic_map *isl_basic_map_deltas(struct isl_basic_map *bmap)
7850{
7851 isl_space *target_space;
7852 struct isl_basic_setisl_basic_map *bset;
7853 unsigned dim;
7854 unsigned nparam;
7855 int i;
7856
7857 if (!bmap)
7858 goto error;
7859 isl_assert(bmap->ctx, isl_space_tuple_is_equal(bmap->dim, isl_dim_in,do { if (isl_space_tuple_is_equal(bmap->dim, isl_dim_in, bmap
->dim, isl_dim_out)) break; do { isl_handle_error(bmap->
ctx, isl_error_unknown, "Assertion \"" "isl_space_tuple_is_equal(bmap->dim, isl_dim_in, bmap->dim, isl_dim_out)"
"\" failed", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 7861); goto error; } while (0); } while (0)
7860 bmap->dim, isl_dim_out),do { if (isl_space_tuple_is_equal(bmap->dim, isl_dim_in, bmap
->dim, isl_dim_out)) break; do { isl_handle_error(bmap->
ctx, isl_error_unknown, "Assertion \"" "isl_space_tuple_is_equal(bmap->dim, isl_dim_in, bmap->dim, isl_dim_out)"
"\" failed", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 7861); goto error; } while (0); } while (0)
7861 goto error)do { if (isl_space_tuple_is_equal(bmap->dim, isl_dim_in, bmap
->dim, isl_dim_out)) break; do { isl_handle_error(bmap->
ctx, isl_error_unknown, "Assertion \"" "isl_space_tuple_is_equal(bmap->dim, isl_dim_in, bmap->dim, isl_dim_out)"
"\" failed", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 7861); goto error; } while (0); } while (0)
;
7862 target_space = isl_space_domain(isl_basic_map_get_space(bmap));
7863 dim = isl_basic_map_dim(bmap, isl_dim_in);
7864 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7865 bmap = isl_basic_map_from_range(isl_basic_map_wrap(bmap));
7866 bmap = isl_basic_map_add_dims(bmap, isl_dim_in, dim);
7867 bmap = isl_basic_map_extend_constraints(bmap, dim, 0);
7868 for (i = 0; i < dim; ++i) {
7869 int j = isl_basic_map_alloc_equality(bmap);
7870 if (j < 0) {
7871 bmap = isl_basic_map_free(bmap);
7872 break;
7873 }
7874 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7875 isl_int_set_si(bmap->eq[j][1+nparam+i], 1)isl_sioimath_set_si((bmap->eq[j][1+nparam+i]), 1);
7876 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], 1)isl_sioimath_set_si((bmap->eq[j][1+nparam+dim+i]), 1);
7877 isl_int_set_si(bmap->eq[j][1+nparam+2*dim+i], -1)isl_sioimath_set_si((bmap->eq[j][1+nparam+2*dim+i]), -1);
7878 }
7879 bset = isl_basic_map_domain(bmap);
7880 bset = isl_basic_set_reset_space(bset, target_space);
7881 return bset;
7882error:
7883 isl_basic_map_free(bmap);
7884 return NULL((void*)0);
7885}
7886
7887/*
7888 * returns range - domain
7889 */
7890__isl_give isl_setisl_map *isl_map_deltas(__isl_take isl_map *map)
7891{
7892 int i;
7893 isl_space *dim;
7894 struct isl_setisl_map *result;
7895
7896 if (!map)
7897 return NULL((void*)0);
7898
7899 isl_assert(map->ctx, isl_space_tuple_is_equal(map->dim, isl_dim_in,do { if (isl_space_tuple_is_equal(map->dim, isl_dim_in, map
->dim, isl_dim_out)) break; do { isl_handle_error(map->
ctx, isl_error_unknown, "Assertion \"" "isl_space_tuple_is_equal(map->dim, isl_dim_in, map->dim, isl_dim_out)"
"\" failed", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 7901); goto error; } while (0); } while (0)
7900 map->dim, isl_dim_out),do { if (isl_space_tuple_is_equal(map->dim, isl_dim_in, map
->dim, isl_dim_out)) break; do { isl_handle_error(map->
ctx, isl_error_unknown, "Assertion \"" "isl_space_tuple_is_equal(map->dim, isl_dim_in, map->dim, isl_dim_out)"
"\" failed", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 7901); goto error; } while (0); } while (0)
7901 goto error)do { if (isl_space_tuple_is_equal(map->dim, isl_dim_in, map
->dim, isl_dim_out)) break; do { isl_handle_error(map->
ctx, isl_error_unknown, "Assertion \"" "isl_space_tuple_is_equal(map->dim, isl_dim_in, map->dim, isl_dim_out)"
"\" failed", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 7901); goto error; } while (0); } while (0)
;
7902 dim = isl_map_get_space(map);
7903 dim = isl_space_domain(dim);
7904 result = isl_set_alloc_space(dim, map->n, 0);
7905 if (!result)
7906 goto error;
7907 for (i = 0; i < map->n; ++i)
7908 result = isl_set_add_basic_set(result,
7909 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
7910 isl_map_free(map);
7911 return result;
7912error:
7913 isl_map_free(map);
7914 return NULL((void*)0);
7915}
7916
7917/*
7918 * returns [domain -> range] -> range - domain
7919 */
7920__isl_give isl_basic_map *isl_basic_map_deltas_map(
7921 __isl_take isl_basic_map *bmap)
7922{
7923 int i, k;
7924 isl_space *dim;
7925 isl_basic_map *domain;
7926 int nparam, n;
7927 unsigned total;
7928
7929 if (!isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7930 bmap->dim, isl_dim_out))
7931 isl_die(bmap->ctx, isl_error_invalid,do { isl_handle_error(bmap->ctx, isl_error_invalid, "domain and range don't match"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 7932); goto error; } while (0)
7932 "domain and range don't match", goto error)do { isl_handle_error(bmap->ctx, isl_error_invalid, "domain and range don't match"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 7932); goto error; } while (0)
;
7933
7934 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7935 n = isl_basic_map_dim(bmap, isl_dim_in);
7936
7937 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
7938 domain = isl_basic_map_universe(dim);
7939
7940 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
7941 bmap = isl_basic_map_apply_range(bmap, domain);
7942 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
7943
7944 total = isl_basic_map_total_dim(bmap);
7945
7946 for (i = 0; i < n; ++i) {
7947 k = isl_basic_map_alloc_equality(bmap);
7948 if (k < 0)
7949 goto error;
7950 isl_seq_clr(bmap->eq[k], 1 + total);
7951 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1)isl_sioimath_set_si((bmap->eq[k][1 + nparam + i]), 1);
7952 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1)isl_sioimath_set_si((bmap->eq[k][1 + nparam + n + i]), -1);
7953 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1)isl_sioimath_set_si((bmap->eq[k][1 + nparam + n + n + i]),
1)
;
7954 }
7955
7956 bmap = isl_basic_map_gauss(bmap, NULL((void*)0));
7957 return isl_basic_map_finalize(bmap);
7958error:
7959 isl_basic_map_free(bmap);
7960 return NULL((void*)0);
7961}
7962
7963/*
7964 * returns [domain -> range] -> range - domain
7965 */
7966__isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
7967{
7968 int i;
7969 isl_space *domain_dim;
7970
7971 if (!map)
7972 return NULL((void*)0);
7973
7974 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
7975 map->dim, isl_dim_out))
7976 isl_die(map->ctx, isl_error_invalid,do { isl_handle_error(map->ctx, isl_error_invalid, "domain and range don't match"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 7977); goto error; } while (0)
7977 "domain and range don't match", goto error)do { isl_handle_error(map->ctx, isl_error_invalid, "domain and range don't match"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 7977); goto error; } while (0)
;
7978
7979 map = isl_map_cow(map);
7980 if (!map)
7981 return NULL((void*)0);
7982
7983 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
7984 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
7985 map->dim = isl_space_join(map->dim, domain_dim);
7986 if (!map->dim)
7987 goto error;
7988 for (i = 0; i < map->n; ++i) {
7989 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
7990 if (!map->p[i])
7991 goto error;
7992 }
7993 ISL_F_CLR(map, ISL_MAP_NORMALIZED)(((map)->flags) &= ~((1 << 1)));
7994 return map;
7995error:
7996 isl_map_free(map);
7997 return NULL((void*)0);
7998}
7999
8000static __isl_give isl_basic_map *basic_map_identity(__isl_take isl_space *dims)
8001{
8002 struct isl_basic_map *bmap;
8003 unsigned nparam;
8004 unsigned dim;
8005 int i;
8006
8007 if (!dims)
8008 return NULL((void*)0);
8009
8010 nparam = dims->nparam;
8011 dim = dims->n_out;
8012 bmap = isl_basic_map_alloc_space(dims, 0, dim, 0);
8013 if (!bmap)
8014 goto error;
8015
8016 for (i = 0; i < dim; ++i) {
8017 int j = isl_basic_map_alloc_equality(bmap);
8018 if (j < 0)
8019 goto error;
8020 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
8021 isl_int_set_si(bmap->eq[j][1+nparam+i], 1)isl_sioimath_set_si((bmap->eq[j][1+nparam+i]), 1);
8022 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1)isl_sioimath_set_si((bmap->eq[j][1+nparam+dim+i]), -1);
8023 }
8024 return isl_basic_map_finalize(bmap);
8025error:
8026 isl_basic_map_free(bmap);
8027 return NULL((void*)0);
8028}
8029
8030__isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *dim)
8031{
8032 if (!dim)
8033 return NULL((void*)0);
8034 if (dim->n_in != dim->n_out)
8035 isl_die(dim->ctx, isl_error_invalid,do { isl_handle_error(dim->ctx, isl_error_invalid, "number of input and output dimensions needs to be "
"the same", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 8037); goto error; } while (0)
8036 "number of input and output dimensions needs to be "do { isl_handle_error(dim->ctx, isl_error_invalid, "number of input and output dimensions needs to be "
"the same", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 8037); goto error; } while (0)
8037 "the same", goto error)do { isl_handle_error(dim->ctx, isl_error_invalid, "number of input and output dimensions needs to be "
"the same", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 8037); goto error; } while (0)
;
8038 return basic_map_identity(dim);
8039error:
8040 isl_space_free(dim);
8041 return NULL((void*)0);
8042}
8043
8044__isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
8045{
8046 return isl_map_from_basic_map(isl_basic_map_identity(dim));
8047}
8048
8049__isl_give isl_map *isl_set_identity(__isl_take isl_setisl_map *set)
8050{
8051 isl_space *dim = isl_set_get_space(set);
8052 isl_map *id;
8053 id = isl_map_identity(isl_space_map_from_set(dim));
8054 return isl_map_intersect_range(id, set);
8055}
8056
8057/* Construct a basic set with all set dimensions having only non-negative
8058 * values.
8059 */
8060__isl_give isl_basic_setisl_basic_map *isl_basic_set_positive_orthant(
8061 __isl_take isl_space *space)
8062{
8063 int i;
8064 unsigned nparam;
8065 unsigned dim;
8066 struct isl_basic_setisl_basic_map *bset;
8067
8068 if (!space)
8069 return NULL((void*)0);
8070 nparam = space->nparam;
8071 dim = space->n_out;
8072 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
8073 if (!bset)
8074 return NULL((void*)0);
8075 for (i = 0; i < dim; ++i) {
8076 int k = isl_basic_set_alloc_inequality(bset);
8077 if (k < 0)
8078 goto error;
8079 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
8080 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1)isl_sioimath_set_si((bset->ineq[k][1 + nparam + i]), 1);
8081 }
8082 return bset;
8083error:
8084 isl_basic_set_free(bset);
8085 return NULL((void*)0);
8086}
8087
8088/* Construct the half-space x_pos >= 0.
8089 */
8090static __isl_give isl_basic_setisl_basic_map *nonneg_halfspace(__isl_take isl_space *dim,
8091 int pos)
8092{
8093 int k;
8094 isl_basic_setisl_basic_map *nonneg;
8095
8096 nonneg = isl_basic_set_alloc_space(dim, 0, 0, 1);
8097 k = isl_basic_set_alloc_inequality(nonneg);
8098 if (k < 0)
8099 goto error;
8100 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
8101 isl_int_set_si(nonneg->ineq[k][pos], 1)isl_sioimath_set_si((nonneg->ineq[k][pos]), 1);
8102
8103 return isl_basic_set_finalize(nonneg);
8104error:
8105 isl_basic_set_free(nonneg);
8106 return NULL((void*)0);
8107}
8108
8109/* Construct the half-space x_pos <= -1.
8110 */
8111static __isl_give isl_basic_setisl_basic_map *neg_halfspace(__isl_take isl_space *dim, int pos)
8112{
8113 int k;
8114 isl_basic_setisl_basic_map *neg;
8115
8116 neg = isl_basic_set_alloc_space(dim, 0, 0, 1);
8117 k = isl_basic_set_alloc_inequality(neg);
8118 if (k < 0)
8119 goto error;
8120 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
8121 isl_int_set_si(neg->ineq[k][0], -1)isl_sioimath_set_si((neg->ineq[k][0]), -1);
8122 isl_int_set_si(neg->ineq[k][pos], -1)isl_sioimath_set_si((neg->ineq[k][pos]), -1);
8123
8124 return isl_basic_set_finalize(neg);
8125error:
8126 isl_basic_set_free(neg);
8127 return NULL((void*)0);
8128}
8129
8130__isl_give isl_setisl_map *isl_set_split_dims(__isl_take isl_setisl_map *set,
8131 enum isl_dim_type type, unsigned first, unsigned n)
8132{
8133 int i;
8134 unsigned offset;
8135 isl_basic_setisl_basic_map *nonneg;
8136 isl_basic_setisl_basic_map *neg;
8137
8138 if (!set)
8139 return NULL((void*)0);
8140 if (n == 0)
8141 return set;
8142
8143 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error)do { if (first + n <= isl_set_dim(set, type)) break; do { isl_handle_error
(set->ctx, isl_error_unknown, "Assertion \"" "first + n <= isl_set_dim(set, type)"
"\" failed", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/tools/polly/lib/External/isl/isl_map.c"
, 8143); goto error; } while (0); } while (0)
;
8144
8145 offset = pos(set->dim, type);
8146 for (i = 0; i < n; ++i) {
8147 nonneg = nonneg_halfspace(isl_set_get_space(set),
8148 offset + first + i);
8149 neg = neg_halfspace(isl_set_get_space(set), offset + first + i);
8150
8151 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
8152 }
8153
8154 return set;
8155error:
8156 isl_set_free(set);
8157 return NULL((void*)0);
8158}
8159
8160static isl_stat foreach_orthant(__isl_take isl_setisl_map *set, int *signs, int first,
8161 int len,
8162 isl_stat (*fn)(__isl_take isl_setisl_map *orthant, int *signs, void *user),
8163 void *user)
8164{
8165 isl_setisl_map *half;
8166
8167 if (!set)
8168 return isl_stat_error;
8169 if (isl_set_plain_is_empty(set)) {
8170 isl_set_free(set);
8171 return isl_stat_ok;
8172 }
8173 if (first == len)
8174 return fn(set, signs, user);
8175
8176 signs[first] = 1;
8177 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
8178 1 + first));
8179 half = isl_set_intersect(half, isl_set_copy(set));
8180 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
8181 goto error;
8182
8183 signs[first] = -1;
8184 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
8185 1 + first));
8186 half = isl_set_intersect(half, set);
8187 return foreach_orthant(half, signs, first + 1, len, fn, user);
8188error:
8189 isl_set_free(set);
8190 return isl_stat_error;
8191}
8192
8193/* Call "fn" on the intersections of "set" with each of the orthants
8194 * (except for obviously empty intersections). The orthant is identified
8195 * by the signs array, with each entry having value 1 or -1 according
8196 * to the sign of the corresponding variable.
8197 */
8198isl_stat isl_set_foreach_orthant(__isl_keep isl_setisl_map *set,
8199 isl_stat (*fn)(__isl_take isl_setisl_map *orthant, int *signs, void *user),
8200 void *user)
8201{
8202 unsigned nparam;
8203 unsigned nvar;
8204 int *signs;
8205 isl_stat r;
8206
8207 if (!set)
8208 return isl_stat_error;
8209 if (isl_set_plain_is_empty(set))
8210 return isl_stat_ok;
8211
8212 nparam = isl_set_dim(set, isl_dim_param);
8213 nvar = isl_set_dim(set, isl_dim_set);
8214
8215 signs = isl_alloc_array(set->ctx, int, nparam + nvar)((int *)isl_malloc_or_die(set->ctx, (nparam + nvar)*sizeof
(int)))
;
8216
8217 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
8218 fn, user);
8219
8220 free(signs);
8221
8222 return r;
8223}
8224
8225isl_bool isl_set_is_equal(__isl_keep isl_setisl_map *set1, __isl_keep isl_setisl_map *set2)
8226{
8227 return isl_map_is_equal(set_to_map(set1), set_to_map(set2));
8228}
8229
8230isl_bool isl_basic_map_is_subset(__isl_keep isl_basic_map *bmap1,
8231 __isl_keep isl_basic_map *bmap2)
8232{
8233 int is_subset;
8234 struct isl_map *map1;
8235 struct isl_map *map2;
8236
8237 if (!bmap1 || !bmap2)
8238 return isl_bool_error;
8239
8240 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
8241 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
8242
8243 is_subset = isl_map_is_subset(map1, map2);
8244
8245 isl_map_free(map1);
8246 isl_map_free(map2);
8247
8248 return is_subset;
8249}
8250
8251isl_bool isl_basic_set_is_subset(__isl_keep isl_basic_setisl_basic_map *bset1,
8252 __isl_keep isl_basic_setisl_basic_map *bset2)
8253{
8254 return isl_basic_map_is_subset(bset1, bset2);
8255}
8256
8257isl_bool isl_basic_map_is_equal(__isl_keep isl_basic_map *bmap1,
8258 __isl_keep isl_basic_map *bmap2)
8259{
8260 isl_bool is_subset;
8261
8262 if (!bmap1 || !bmap2)
8263 return isl_bool_error;
8264 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
8265 if (is_subset != isl_bool_true)
8266 return is_subset;
8267 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
8268 return is_subset;
8269}
8270
8271isl_bool isl_basic_set_is_equal(__isl_keep isl_basic_setisl_basic_map *bset1,
8272 __isl_keep isl_basic_setisl_basic_map *bset2)
8273{
8274 return isl_basic_map_is_equal(
8275 bset_to_bmap(bset1), bset_to_bmap(bset2));
8276}
8277
8278isl_bool isl_map_is_empty(__isl_keep isl_map *map)
8279{
8280 int i;
8281 int is_empty;
8282
8283 if (!map)
8284 return isl_bool_error;
8285 for (i = 0; i < map->n; ++i) {
8286 is_empty = isl_basic_map_is_empty(map->p[i]);
8287 if (is_empty < 0)
8288 return isl_bool_error;
8289 if (!is_empty)
8290 return isl_bool_false;
8291 }
8292 return isl_bool_true;
8293}
8294
8295isl_bool isl_map_plain_is_empty(__isl_keep isl_map *map)
8296{
8297 return map ? map->n == 0 : isl_bool_error;
8298}
8299
8300isl_bool isl_set_plain_is_empty(__isl_keep isl_setisl_map *set)
8301{
8302 return set ? set->n == 0 : isl_bool_error;
8303}
8304
8305isl_bool isl_set_is_empty(__isl_keep isl_setisl_map *set)
8306{
8307 return isl_map_is_empty(set_to_map(set));
8308}
8309
8310isl_bool isl_map_has_equal_space(__isl_keep isl_map *map1,
8311 __isl_keep isl_map *map2)
8312{
8313 if (!map1 || !map2)
8314 return isl_bool_error;
8315
8316 return isl_space_is_equal(map1->dim, map2->dim);
8317}
8318
8319isl_bool isl_set_has_equal_space(__isl_keep isl_setisl_map *set1,
8320 __isl_keep isl_setisl_map *set2)
8321{
8322 if (!set1 || !set2)
8323 return isl_bool_error;
8324
8325 return isl_space_is_equal(set1->dim, set2->dim);
8326}
8327
8328static isl_bool map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8329{
8330 isl_bool is_subset;
8331
8332 if (!map1 || !map2)
8333 return isl_bool_error;
8334 is_subset = isl_map_is_subset(map1, map2);
8335 if (is_subset != isl_bool_true)
8336 return is_subset;
8337 is_subset = isl_map_is_subset(map2, map1);
8338 return is_subset;
8339}
8340
8341/* Is "map1" equal to "map2"?
8342 *
8343 * First check if they are obviously equal.
8344 * If not, then perform a more detailed analysis.
8345 */
8346isl_bool isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8347{
8348 isl_bool equal;
8349
8350 equal = isl_map_plain_is_equal(map1, map2);
8351 if (equal < 0 || equal)
8352 return equal;
8353 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
8354}
8355
8356isl_bool isl_basic_map_is_strict_subset(
8357 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8358{
8359 isl_bool is_subset;
8360
8361 if (!bmap1 || !bmap2)
8362 return isl_bool_error;
8363 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
8364 if (is_subset != isl_bool_true)
8365 return is_subset;
8366 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
8367 if (is_subset == isl_bool_error)
8368 return is_subset;
8369 return !is_subset;
8370}
8371
8372isl_bool isl_map_is_strict_subset(__isl_keep isl_map *map1,
8373 __isl_keep isl_map *map2)
8374{
8375 isl_bool is_subset;
8376
8377 if (!map1 || !map2)
8378 return isl_bool_error;
8379 is_subset = isl_map_is_subset(map1, map2);
8380 if (is_subset != isl_bool_true)
8381 return is_subset;
8382 is_subset = isl_map_is_subset(map2, map1);
8383 if (is_subset == isl_bool_error)
8384 return is_subset;
8385 return !is_subset;
8386}
8387
8388isl_bool isl_set_is_strict_subset(__isl_keep isl_setisl_map *set1,
8389 __isl_keep isl_setisl_map *set2)
8390{
8391 return isl_map_is_strict_subset(set_to_map(set1), set_to_map(set2));
8392}
8393
8394/* Is "bmap" obviously equal to the universe with the same space?
8395 *
8396 * That is, does it not have any constraints?
8397 */
8398isl_bool isl_basic_map_plain_is_universe(__isl_keep isl_basic_map *bmap)
8399{
8400 if (!bmap)
8401 return isl_bool_error;
8402 return bmap->n_eq == 0 && bmap->n_ineq == 0;
8403}
8404
8405/* Is "bset" obviously equal to the universe with the same space?
8406 */
8407isl_bool isl_basic_set_plain_is_universe(__isl_keep isl_basic_setisl_basic_map *bset)
8408{
8409 return isl_basic_map_plain_is_universe(bset);
8410}
8411
8412/* If "c" does not involve any existentially quantified variables,
8413 * then set *univ to false and abort
8414 */
8415static isl_stat involves_divs(__isl_take isl_constraint *c, void *user)
8416{
8417 isl_bool *univ = user;
8418 unsigned n;
8419
8420 n = isl_constraint_dim(c, isl_dim_div);
8421 *univ = isl_constraint_involves_dims(c, isl_dim_div, 0, n);
8422 isl_constraint_free(c);
8423 if (*univ < 0 || !*univ)
8424 return isl_stat_error;
8425 return isl_stat_ok;
8426}
8427
8428/* Is "bmap" equal to the universe with the same space?
8429 *
8430 * First check if it is obviously equal to the universe.
8431 * If not and if there are any constraints not involving
8432 * existentially quantified variables, then it is certainly
8433 * not equal to the universe.
8434 * Otherwise, check if the universe is a subset of "bmap".
8435 */
8436isl_bool isl_basic_map_is_universe(__isl_keep isl_basic_map *bmap)
8437{
8438 isl_bool univ;
8439 isl_basic_map *test;
8440
8441 univ = isl_basic_map_plain_is_universe(bmap);
8442 if (univ < 0 || univ)
8443 return univ;
8444 if (isl_basic_map_dim(bmap, isl_dim_div) == 0)
8445 return isl_bool_false;
8446 univ = isl_bool_true;
8447 if (isl_basic_map_foreach_constraint(bmap, &involves_divs, &univ) < 0 &&
8448 univ)
8449 return isl_bool_error;
8450 if (univ < 0 || !univ)
8451 return univ;
8452 test = isl_basic_map_universe(isl_basic_map_get_space(bmap));
8453 univ = isl_basic_map_is_subset(test, bmap);
8454 isl_basic_map_free(test);
8455 return univ;
8456}
8457
8458/* Is "bset" equal to the universe with the same space?
8459 */
8460isl_bool isl_basic_set_is_universe(__isl_keep isl_basic_setisl_basic_map *bset)
8461{
8462 return isl_basic_map_is_universe(bset);
8463}
8464
8465isl_bool isl_map_plain_is_universe(__isl_keep isl_map *map)
8466{
8467 int i;
8468
8469 if (!map)
8470 return isl_bool_error;
8471
8472 for (i = 0; i < map->n; ++i) {
8473 isl_bool r = isl_basic_map_plain_is_universe(map->p[i]);
8474 if (r < 0 || r)
8475 return r;
8476 }
8477
8478 return isl_bool_false;
8479}
8480
8481isl_bool isl_set_plain_is_universe(__isl_keep isl_setisl_map *set)
8482{
8483 return isl_map_plain_is_universe(set_to_map(set));
8484}
8485
8486isl_bool isl_basic_map_is_empty(__isl_keep isl_basic_map *bmap)
8487{
8488 struct isl_basic_setisl_basic_map *bset = NULL((void*)0);
8489 struct isl_vec *sample = NULL((void*)0);
8490 isl_bool empty, non_empty;
8491
8492 if (!bmap)
8493 return isl_bool_error;
8494
8495 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY)(!!(((bmap)->flags) & ((1 << 1)))))
8496 return isl_bool_true;
8497
8498 if (isl_basic_map_plain_is_universe(bmap))
8499 return isl_bool_false;
8500
8501 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)(!!(((bmap)->flags) & ((1 << 4))))) {
8502 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
8503 copy = isl_basic_map_remove_redundancies(copy);
8504 empty = isl_basic_map_plain_is_empty(copy);
8505 isl_basic_map_free(copy);
8506 return empty;
8507 }
8508
8509 non_empty = isl_basic_map_plain_is_non_empty(bmap);
8510 if (non_empty < 0)
8511 return isl_bool_error;
8512 if (non_empty)
8513 return isl_bool_false;
8514 isl_vec_free(bmap->sample);
8515 bmap->sample = NULL((void*)0);
8516 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
8517 if (!bset)
8518 return isl_bool_error;
8519 sample = isl_basic_set_sample_vec(bset);
8520 if (!sample)
8521 return isl_bool_error;
8522 empty = sample->size == 0;
8523 isl_vec_free(bmap->sample);
8524 bmap->sample = sample;
8525 if (empty)
8526 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY)(((bmap)->flags) |= ((1 << 1)));
8527
8528 return empty;
8529}
8530
8531isl_bool isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
8532{
8533 if (!bmap)