Bug Summary

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