Bug Summary

File:tools/polly/lib/External/isl/isl_map.c
Warning:line 4614, column 9
Use of memory after it is freed

Annotated Source Code

[?] Use j/k keys for keyboard navigation

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", "/build/llvm-toolchain-snapshot-6.0~svn321639/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", "/build/llvm-toolchain-snapshot-6.0~svn321639/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", "/build/llvm-toolchain-snapshot-6.0~svn321639/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
1176__isl_give 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"
, "/build/llvm-toolchain-snapshot-6.0~svn321639/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)
11
Assuming 'bmap' is non-null
12
Taking false branch
1294 return NULL((void*)0);
1295
1296 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)(!!(((bmap)->flags) & ((1 << 0))))) {
13
Taking true branch
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)
18
Taking false branch
1318 return NULL((void*)0);
1319
1320 if (--bmap->ref > 0)
19
Assuming the condition is false
20
Taking false branch
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);
21
Memory is released
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", "/build/llvm-toolchain-snapshot-6.0~svn321639/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", "/build/llvm-toolchain-snapshot-6.0~svn321639/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", "/build/llvm-toolchain-snapshot-6.0~svn321639/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", "/build/llvm-toolchain-snapshot-6.0~svn321639/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", "/build/llvm-toolchain-snapshot-6.0~svn321639/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", "/build/llvm-toolchain-snapshot-6.0~svn321639/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", "/build/llvm-toolchain-snapshot-6.0~svn321639/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", "/build/llvm-toolchain-snapshot-6.0~svn321639/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", "/build/llvm-toolchain-snapshot-6.0~svn321639/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", "/build/llvm-toolchain-snapshot-6.0~svn321639/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", "/build/llvm-toolchain-snapshot-6.0~svn321639/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", "/build/llvm-toolchain-snapshot-6.0~svn321639/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", "/build/llvm-toolchain-snapshot-6.0~svn321639/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", "/build/llvm-toolchain-snapshot-6.0~svn321639/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", "/build/llvm-toolchain-snapshot-6.0~svn321639/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", "/build/llvm-toolchain-snapshot-6.0~svn321639/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", "/build/llvm-toolchain-snapshot-6.0~svn321639/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", "/build/llvm-toolchain-snapshot-6.0~svn321639/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", "/build/llvm-toolchain-snapshot-6.0~svn321639/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", "/build/llvm-toolchain-snapshot-6.0~svn321639/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
1856__isl_give isl_basic_setisl_basic_map *isl_basic_set_extend_space(
1857 __isl_take isl_basic_setisl_basic_map *base,
1858 __isl_take isl_space *dim, unsigned extra,
1859 unsigned n_eq, unsigned n_ineq)
1860{
1861 return bset_from_bmap(isl_basic_map_extend_space(bset_to_bmap(base),
1862 dim, extra, n_eq, n_ineq));
1863}
1864
1865struct isl_basic_map *isl_basic_map_extend_constraints(
1866 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1867{
1868 if (!base)
1869 return NULL((void*)0);
1870 return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1871 0, n_eq, n_ineq);
1872}
1873
1874struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1875 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1876 unsigned n_eq, unsigned n_ineq)
1877{
1878 struct isl_basic_map *bmap;
1879 isl_space *dim;
1880
1881 if (!base)
1882 return NULL((void*)0);
1883 dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1884 if (!dim)
1885 goto error;
1886
1887 bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1888 return bmap;
1889error:
1890 isl_basic_map_free(base);
1891 return NULL((void*)0);
1892}
1893
1894struct isl_basic_setisl_basic_map *isl_basic_set_extend(struct isl_basic_setisl_basic_map *base,
1895 unsigned nparam, unsigned dim, unsigned extra,
1896 unsigned n_eq, unsigned n_ineq)
1897{
1898 return bset_from_bmap(isl_basic_map_extend(bset_to_bmap(base),
1899 nparam, 0, dim, extra, n_eq, n_ineq));
1900}
1901
1902struct isl_basic_setisl_basic_map *isl_basic_set_extend_constraints(
1903 struct isl_basic_setisl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1904{
1905 isl_basic_map *bmap = bset_to_bmap(base);
1906 bmap = isl_basic_map_extend_constraints(bmap, n_eq, n_ineq);
1907 return bset_from_bmap(bmap);
1908}
1909
1910__isl_give isl_basic_setisl_basic_map *isl_basic_set_cow(__isl_take isl_basic_setisl_basic_map *bset)
1911{
1912 return bset_from_bmap(isl_basic_map_cow(bset_to_bmap(bset)));
1913}
1914
1915__isl_give isl_basic_map *isl_basic_map_cow(__isl_take isl_basic_map *bmap)
1916{
1917 if (!bmap)
1918 return NULL((void*)0);
1919
1920 if (bmap->ref > 1) {
1921 bmap->ref--;
1922 bmap = isl_basic_map_dup(bmap);
1923 }
1924 if (bmap) {
1925 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL)(((bmap)->flags) &= ~((1 << 0)));
1926 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS)(((bmap)->flags) &= ~((1 << 8)));
1927 }
1928 return bmap;
1929}
1930
1931/* Clear all cached information in "map", either because it is about
1932 * to be modified or because it is being freed.
1933 * Always return the same pointer that is passed in.
1934 * This is needed for the use in isl_map_free.
1935 */
1936static __isl_give isl_map *clear_caches(__isl_take isl_map *map)
1937{
1938 isl_basic_map_free(map->cached_simple_hull[0]);
1939 isl_basic_map_free(map->cached_simple_hull[1]);
1940 map->cached_simple_hull[0] = NULL((void*)0);
1941 map->cached_simple_hull[1] = NULL((void*)0);
1942 return map;
1943}
1944
1945__isl_give isl_setisl_map *isl_set_cow(__isl_take isl_setisl_map *set)
1946{
1947 return isl_map_cow(set);
1948}
1949
1950/* Return an isl_map that is equal to "map" and that has only
1951 * a single reference.
1952 *
1953 * If the original input already has only one reference, then
1954 * simply return it, but clear all cached information, since
1955 * it may be rendered invalid by the operations that will be
1956 * performed on the result.
1957 *
1958 * Otherwise, create a duplicate (without any cached information).
1959 */
1960__isl_give isl_map *isl_map_cow(__isl_take isl_map *map)
1961{
1962 if (!map)
1963 return NULL((void*)0);
1964
1965 if (map->ref == 1)
1966 return clear_caches(map);
1967 map->ref--;
1968 return isl_map_dup(map);
1969}
1970
1971static void swap_vars(struct isl_blk blk, isl_int *a,
1972 unsigned a_len, unsigned b_len)
1973{
1974 isl_seq_cpy(blk.data, a+a_len, b_len);
1975 isl_seq_cpy(blk.data+b_len, a, a_len);
1976 isl_seq_cpy(a, blk.data, b_len+a_len);
1977}
1978
1979static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1980 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1981{
1982 int i;
1983 struct isl_blk blk;
1984
1985 if (!bmap)
1986 goto error;
1987
1988 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", "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 1989); goto error; } while (0); } while (0)
1989 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", "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 1989); goto error; } while (0); } while (0)
;
1990
1991 if (n1 == 0 || n2 == 0)
1992 return bmap;
1993
1994 bmap = isl_basic_map_cow(bmap);
1995 if (!bmap)
1996 return NULL((void*)0);
1997
1998 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1999 if (isl_blk_is_error(blk))
2000 goto error;
2001
2002 for (i = 0; i < bmap->n_eq; ++i)
2003 swap_vars(blk,
2004 bmap->eq[i] + pos, n1, n2);
2005
2006 for (i = 0; i < bmap->n_ineq; ++i)
2007 swap_vars(blk,
2008 bmap->ineq[i] + pos, n1, n2);
2009
2010 for (i = 0; i < bmap->n_div; ++i)
2011 swap_vars(blk,
2012 bmap->div[i]+1 + pos, n1, n2);
2013
2014 isl_blk_free(bmap->ctx, blk);
2015
2016 ISL_F_CLR(bmap, ISL_BASIC_SET_NORMALIZED)(((bmap)->flags) &= ~((1 << 5)));
2017 bmap = isl_basic_map_gauss(bmap, NULL((void*)0));
2018 return isl_basic_map_finalize(bmap);
2019error:
2020 isl_basic_map_free(bmap);
2021 return NULL((void*)0);
2022}
2023
2024__isl_give isl_basic_map *isl_basic_map_set_to_empty(
2025 __isl_take isl_basic_map *bmap)
2026{
2027 int i = 0;
2028 unsigned total;
2029 if (!bmap)
2030 goto error;
2031 total = isl_basic_map_total_dim(bmap);
2032 if (isl_basic_map_free_div(bmap, bmap->n_div) < 0)
2033 return isl_basic_map_free(bmap);
2034 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
2035 if (bmap->n_eq > 0)
2036 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
2037 else {
2038 i = isl_basic_map_alloc_equality(bmap);
2039 if (i < 0)
2040 goto error;
2041 }
2042 isl_int_set_si(bmap->eq[i][0], 1)isl_sioimath_set_si((bmap->eq[i][0]), 1);
2043 isl_seq_clr(bmap->eq[i]+1, total);
2044 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY)(((bmap)->flags) |= ((1 << 1)));
2045 isl_vec_free(bmap->sample);
2046 bmap->sample = NULL((void*)0);
2047 return isl_basic_map_finalize(bmap);
2048error:
2049 isl_basic_map_free(bmap);
2050 return NULL((void*)0);
2051}
2052
2053__isl_give isl_basic_setisl_basic_map *isl_basic_set_set_to_empty(
2054 __isl_take isl_basic_setisl_basic_map *bset)
2055{
2056 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset)));
2057}
2058
2059__isl_give isl_basic_map *isl_basic_map_set_rational(
2060 __isl_take isl_basic_map *bmap)
2061{
2062 if (!bmap)
2063 return NULL((void*)0);
2064
2065 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)(!!(((bmap)->flags) & ((1 << 4)))))
2066 return bmap;
2067
2068 bmap = isl_basic_map_cow(bmap);
2069 if (!bmap)
2070 return NULL((void*)0);
2071
2072 ISL_F_SET(bmap, ISL_BASIC_MAP_RATIONAL)(((bmap)->flags) |= ((1 << 4)));
2073
2074 return isl_basic_map_finalize(bmap);
2075}
2076
2077__isl_give isl_basic_setisl_basic_map *isl_basic_set_set_rational(
2078 __isl_take isl_basic_setisl_basic_map *bset)
2079{
2080 return isl_basic_map_set_rational(bset);
2081}
2082
2083__isl_give isl_basic_setisl_basic_map *isl_basic_set_set_integral(
2084 __isl_take isl_basic_setisl_basic_map *bset)
2085{
2086 if (!bset)
2087 return NULL((void*)0);
2088
2089 if (!ISL_F_ISSET(bset, ISL_BASIC_MAP_RATIONAL)(!!(((bset)->flags) & ((1 << 4)))))
2090 return bset;
2091
2092 bset = isl_basic_set_cow(bset);
2093 if (!bset)
2094 return NULL((void*)0);
2095
2096 ISL_F_CLR(bset, ISL_BASIC_MAP_RATIONAL)(((bset)->flags) &= ~((1 << 4)));
2097
2098 return isl_basic_set_finalize(bset);
2099}
2100
2101__isl_give isl_map *isl_map_set_rational(__isl_take isl_map *map)
2102{
2103 int i;
2104
2105 map = isl_map_cow(map);
2106 if (!map)
2107 return NULL((void*)0);
2108 for (i = 0; i < map->n; ++i) {
2109 map->p[i] = isl_basic_map_set_rational(map->p[i]);
2110 if (!map->p[i])
2111 goto error;
2112 }
2113 return map;
2114error:
2115 isl_map_free(map);
2116 return NULL((void*)0);
2117}
2118
2119__isl_give isl_setisl_map *isl_set_set_rational(__isl_take isl_setisl_map *set)
2120{
2121 return isl_map_set_rational(set);
2122}
2123
2124/* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
2125 * of "bmap").
2126 */
2127static void swap_div(__isl_keep isl_basic_map *bmap, int a, int b)
2128{
2129 isl_int *t = bmap->div[a];
2130 bmap->div[a] = bmap->div[b];
2131 bmap->div[b] = t;
2132}
2133
2134/* Swap divs "a" and "b" in "bmap" and adjust the constraints and
2135 * div definitions accordingly.
2136 */
2137void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
2138{
2139 int i;
2140 unsigned off = isl_space_dim(bmap->dim, isl_dim_all);
2141
2142 swap_div(bmap, a, b);
2143
2144 for (i = 0; i < bmap->n_eq; ++i)
2145 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]))
;
2146
2147 for (i = 0; i < bmap->n_ineq; ++i)
2148 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]))
;
2149
2150 for (i = 0; i < bmap->n_div; ++i)
2151 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]))
;
2152 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED)(((bmap)->flags) &= ~((1 << 5)));
2153}
2154
2155/* Swap divs "a" and "b" in "bset" and adjust the constraints and
2156 * div definitions accordingly.
2157 */
2158void isl_basic_set_swap_div(__isl_keep isl_basic_setisl_basic_map *bset, int a, int b)
2159{
2160 isl_basic_map_swap_div(bset, a, b);
2161}
2162
2163static void constraint_drop_vars(isl_int *c, unsigned n, unsigned rem)
2164{
2165 isl_seq_cpy(c, c + n, rem);
2166 isl_seq_clr(c + rem, n);
2167}
2168
2169/* Drop n dimensions starting at first.
2170 *
2171 * In principle, this frees up some extra variables as the number
2172 * of columns remains constant, but we would have to extend
2173 * the div array too as the number of rows in this array is assumed
2174 * to be equal to extra.
2175 */
2176__isl_give isl_basic_setisl_basic_map *isl_basic_set_drop_dims(
2177 __isl_take isl_basic_setisl_basic_map *bset, unsigned first, unsigned n)
2178{
2179 return isl_basic_map_drop(bset_to_bmap(bset), isl_dim_set, first, n);
2180}
2181
2182/* Move "n" divs starting at "first" to the end of the list of divs.
2183 */
2184static struct isl_basic_map *move_divs_last(struct isl_basic_map *bmap,
2185 unsigned first, unsigned n)
2186{
2187 isl_int **div;
2188 int i;
2189
2190 if (first + n == bmap->n_div)
2191 return bmap;
2192
2193 div = isl_alloc_array(bmap->ctx, isl_int *, n)((isl_int * *)isl_malloc_or_die(bmap->ctx, (n)*sizeof(isl_int
*)))
;
2194 if (!div)
2195 goto error;
2196 for (i = 0; i < n; ++i)
2197 div[i] = bmap->div[first + i];
2198 for (i = 0; i < bmap->n_div - first - n; ++i)
2199 bmap->div[first + i] = bmap->div[first + n + i];
2200 for (i = 0; i < n; ++i)
2201 bmap->div[bmap->n_div - n + i] = div[i];
2202 free(div);
2203 return bmap;
2204error:
2205 isl_basic_map_free(bmap);
2206 return NULL((void*)0);
2207}
2208
2209/* Drop "n" dimensions of type "type" starting at "first".
2210 *
2211 * In principle, this frees up some extra variables as the number
2212 * of columns remains constant, but we would have to extend
2213 * the div array too as the number of rows in this array is assumed
2214 * to be equal to extra.
2215 */
2216__isl_give isl_basic_map *isl_basic_map_drop(__isl_take isl_basic_map *bmap,
2217 enum isl_dim_type type, unsigned first, unsigned n)
2218{
2219 int i;
2220 unsigned dim;
2221 unsigned offset;
2222 unsigned left;
2223
2224 if (!bmap)
2225 goto error;
2226
2227 dim = isl_basic_map_dim(bmap, type);
2228 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", "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 2228); goto error; } while (0); } while (0)
;
2229
2230 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2231 return bmap;
2232
2233 bmap = isl_basic_map_cow(bmap);
2234 if (!bmap)
2235 return NULL((void*)0);
2236
2237 offset = isl_basic_map_offset(bmap, type) + first;
2238 left = isl_basic_map_total_dim(bmap) - (offset - 1) - n;
2239 for (i = 0; i < bmap->n_eq; ++i)
2240 constraint_drop_vars(bmap->eq[i]+offset, n, left);
2241
2242 for (i = 0; i < bmap->n_ineq; ++i)
2243 constraint_drop_vars(bmap->ineq[i]+offset, n, left);
2244
2245 for (i = 0; i < bmap->n_div; ++i)
2246 constraint_drop_vars(bmap->div[i]+1+offset, n, left);
2247
2248 if (type == isl_dim_div) {
2249 bmap = move_divs_last(bmap, first, n);
2250 if (!bmap)
2251 goto error;
2252 if (isl_basic_map_free_div(bmap, n) < 0)
2253 return isl_basic_map_free(bmap);
2254 } else
2255 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
2256 if (!bmap->dim)
2257 goto error;
2258
2259 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED)(((bmap)->flags) &= ~((1 << 5)));
2260 bmap = isl_basic_map_simplify(bmap);
2261 return isl_basic_map_finalize(bmap);
2262error:
2263 isl_basic_map_free(bmap);
2264 return NULL((void*)0);
2265}
2266
2267__isl_give isl_basic_setisl_basic_map *isl_basic_set_drop(__isl_take isl_basic_setisl_basic_map *bset,
2268 enum isl_dim_type type, unsigned first, unsigned n)
2269{
2270 return bset_from_bmap(isl_basic_map_drop(bset_to_bmap(bset),
2271 type, first, n));
2272}
2273
2274__isl_give isl_map *isl_map_drop(__isl_take isl_map *map,
2275 enum isl_dim_type type, unsigned first, unsigned n)
2276{
2277 int i;
2278
2279 if (!map)
2280 goto error;
2281
2282 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", "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 2282); goto error; } while (0); } while (0)
;
2283
2284 if (n == 0 && !isl_space_is_named_or_nested(map->dim, type))
2285 return map;
2286 map = isl_map_cow(map);
2287 if (!map)
2288 goto error;
2289 map->dim = isl_space_drop_dims(map->dim, type, first, n);
2290 if (!map->dim)
2291 goto error;
2292
2293 for (i = 0; i < map->n; ++i) {
2294 map->p[i] = isl_basic_map_drop(map->p[i], type, first, n);
2295 if (!map->p[i])
2296 goto error;
2297 }
2298 ISL_F_CLR(map, ISL_MAP_NORMALIZED)(((map)->flags) &= ~((1 << 1)));
2299
2300 return map;
2301error:
2302 isl_map_free(map);
2303 return NULL((void*)0);
2304}
2305
2306__isl_give isl_setisl_map *isl_set_drop(__isl_take isl_setisl_map *set,
2307 enum isl_dim_type type, unsigned first, unsigned n)
2308{
2309 return set_from_map(isl_map_drop(set_to_map(set), type, first, n));
2310}
2311
2312/*
2313 * We don't cow, as the div is assumed to be redundant.
2314 */
2315__isl_give isl_basic_map *isl_basic_map_drop_div(
2316 __isl_take isl_basic_map *bmap, unsigned div)
2317{
2318 int i;
2319 unsigned pos;
2320
2321 if (!bmap)
2322 goto error;
2323
2324 pos = 1 + isl_space_dim(bmap->dim, isl_dim_all) + div;
2325
2326 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", "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 2326); goto error; } while (0); } while (0)
;
2327
2328 for (i = 0; i < bmap->n_eq; ++i)
2329 constraint_drop_vars(bmap->eq[i]+pos, 1, bmap->extra-div-1);
2330
2331 for (i = 0; i < bmap->n_ineq; ++i) {
2332 if (!isl_int_is_zero(bmap->ineq[i][pos])(isl_sioimath_sgn(*(bmap->ineq[i][pos])) == 0)) {
2333 isl_basic_map_drop_inequality(bmap, i);
2334 --i;
2335 continue;
2336 }
2337 constraint_drop_vars(bmap->ineq[i]+pos, 1, bmap->extra-div-1);
2338 }
2339
2340 for (i = 0; i < bmap->n_div; ++i)
2341 constraint_drop_vars(bmap->div[i]+1+pos, 1, bmap->extra-div-1);
2342
2343 if (div != bmap->n_div - 1) {
2344 int j;
2345 isl_int *t = bmap->div[div];
2346
2347 for (j = div; j < bmap->n_div - 1; ++j)
2348 bmap->div[j] = bmap->div[j+1];
2349
2350 bmap->div[bmap->n_div - 1] = t;
2351 }
2352 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED)(((bmap)->flags) &= ~((1 << 5)));
2353 if (isl_basic_map_free_div(bmap, 1) < 0)
2354 return isl_basic_map_free(bmap);
2355
2356 return bmap;
2357error:
2358 isl_basic_map_free(bmap);
2359 return NULL((void*)0);
2360}
2361
2362/* Eliminate the specified n dimensions starting at first from the
2363 * constraints, without removing the dimensions from the space.
2364 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2365 */
2366__isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
2367 enum isl_dim_type type, unsigned first, unsigned n)
2368{
2369 int i;
2370
2371 if (!map)
2372 return NULL((void*)0);
2373 if (n == 0)
2374 return map;
2375
2376 if (first + n > isl_map_dim(map, type) || first + n < first)
2377 isl_die(map->ctx, isl_error_invalid,do { isl_handle_error(map->ctx, isl_error_invalid, "index out of bounds"
, "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 2378); goto error; } while (0)
2378 "index out of bounds", goto error)do { isl_handle_error(map->ctx, isl_error_invalid, "index out of bounds"
, "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 2378); goto error; } while (0)
;
2379
2380 map = isl_map_cow(map);
2381 if (!map)
2382 return NULL((void*)0);
2383
2384 for (i = 0; i < map->n; ++i) {
2385 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
2386 if (!map->p[i])
2387 goto error;
2388 }
2389 return map;
2390error:
2391 isl_map_free(map);
2392 return NULL((void*)0);
2393}
2394
2395/* Eliminate the specified n dimensions starting at first from the
2396 * constraints, without removing the dimensions from the space.
2397 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2398 */
2399__isl_give isl_setisl_map *isl_set_eliminate(__isl_take isl_setisl_map *set,
2400 enum isl_dim_type type, unsigned first, unsigned n)
2401{
2402 return set_from_map(isl_map_eliminate(set_to_map(set), type, first, n));
2403}
2404
2405/* Eliminate the specified n dimensions starting at first from the
2406 * constraints, without removing the dimensions from the space.
2407 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2408 */
2409__isl_give isl_setisl_map *isl_set_eliminate_dims(__isl_take isl_setisl_map *set,
2410 unsigned first, unsigned n)
2411{
2412 return isl_set_eliminate(set, isl_dim_set, first, n);
2413}
2414
2415__isl_give isl_basic_map *isl_basic_map_remove_divs(
2416 __isl_take isl_basic_map *bmap)
2417{
2418 if (!bmap)
2419 return NULL((void*)0);
2420 bmap = isl_basic_map_eliminate_vars(bmap,
2421 isl_space_dim(bmap->dim, isl_dim_all), bmap->n_div);
2422 if (!bmap)
2423 return NULL((void*)0);
2424 bmap->n_div = 0;
2425 return isl_basic_map_finalize(bmap);
2426}
2427
2428__isl_give isl_basic_setisl_basic_map *isl_basic_set_remove_divs(
2429 __isl_take isl_basic_setisl_basic_map *bset)
2430{
2431 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset)));
2432}
2433
2434__isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
2435{
2436 int i;
2437
2438 if (!map)
2439 return NULL((void*)0);
2440 if (map->n == 0)
2441 return map;
2442
2443 map = isl_map_cow(map);
2444 if (!map)
2445 return NULL((void*)0);
2446
2447 for (i = 0; i < map->n; ++i) {
2448 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
2449 if (!map->p[i])
2450 goto error;
2451 }
2452 return map;
2453error:
2454 isl_map_free(map);
2455 return NULL((void*)0);
2456}
2457
2458__isl_give isl_setisl_map *isl_set_remove_divs(__isl_take isl_setisl_map *set)
2459{
2460 return isl_map_remove_divs(set);
2461}
2462
2463__isl_give isl_basic_map *isl_basic_map_remove_dims(
2464 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
2465 unsigned first, unsigned n)
2466{
2467 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2468 return isl_basic_map_free(bmap);
2469 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2470 return bmap;
2471 bmap = isl_basic_map_eliminate_vars(bmap,
2472 isl_basic_map_offset(bmap, type) - 1 + first, n);
2473 if (!bmap)
2474 return bmap;
2475 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY)(!!(((bmap)->flags) & ((1 << 1)))) && type == isl_dim_div)
2476 return bmap;
2477 bmap = isl_basic_map_drop(bmap, type, first, n);
2478 return bmap;
2479}
2480
2481/* Return true if the definition of the given div (recursively) involves
2482 * any of the given variables.
2483 */
2484static isl_bool div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
2485 unsigned first, unsigned n)
2486{
2487 int i;
2488 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2489
2490 if (isl_int_is_zero(bmap->div[div][0])(isl_sioimath_sgn(*(bmap->div[div][0])) == 0))
2491 return isl_bool_false;
2492 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
2493 return isl_bool_true;
2494
2495 for (i = bmap->n_div - 1; i >= 0; --i) {
2496 isl_bool involves;
2497
2498 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i])(isl_sioimath_sgn(*(bmap->div[div][1 + div_offset + i])) ==
0)
)
2499 continue;
2500 involves = div_involves_vars(bmap, i, first, n);
2501 if (involves < 0 || involves)
2502 return involves;
2503 }
2504
2505 return isl_bool_false;
2506}
2507
2508/* Try and add a lower and/or upper bound on "div" to "bmap"
2509 * based on inequality "i".
2510 * "total" is the total number of variables (excluding the divs).
2511 * "v" is a temporary object that can be used during the calculations.
2512 * If "lb" is set, then a lower bound should be constructed.
2513 * If "ub" is set, then an upper bound should be constructed.
2514 *
2515 * The calling function has already checked that the inequality does not
2516 * reference "div", but we still need to check that the inequality is
2517 * of the right form. We'll consider the case where we want to construct
2518 * a lower bound. The construction of upper bounds is similar.
2519 *
2520 * Let "div" be of the form
2521 *
2522 * q = floor((a + f(x))/d)
2523 *
2524 * We essentially check if constraint "i" is of the form
2525 *
2526 * b + f(x) >= 0
2527 *
2528 * so that we can use it to derive a lower bound on "div".
2529 * However, we allow a slightly more general form
2530 *
2531 * b + g(x) >= 0
2532 *
2533 * with the condition that the coefficients of g(x) - f(x) are all
2534 * divisible by d.
2535 * Rewriting this constraint as
2536 *
2537 * 0 >= -b - g(x)
2538 *
2539 * adding a + f(x) to both sides and dividing by d, we obtain
2540 *
2541 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2542 *
2543 * Taking the floor on both sides, we obtain
2544 *
2545 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2546 *
2547 * or
2548 *
2549 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2550 *
2551 * In the case of an upper bound, we construct the constraint
2552 *
2553 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2554 *
2555 */
2556static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
2557 __isl_take isl_basic_map *bmap, int div, int i,
2558 unsigned total, isl_int v, int lb, int ub)
2559{
2560 int j;
2561
2562 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
2563 if (lb) {
2564 isl_int_sub(v, bmap->ineq[i][1 + j],isl_sioimath_sub((v), *(bmap->ineq[i][1 + j]), *(bmap->
div[div][1 + 1 + j]))
2565 bmap->div[div][1 + 1 + j])isl_sioimath_sub((v), *(bmap->ineq[i][1 + j]), *(bmap->
div[div][1 + 1 + j]))
;
2566 lb = isl_int_is_divisible_by(v, bmap->div[div][0])isl_sioimath_is_divisible_by(*(v), *(bmap->div[div][0]));
2567 }
2568 if (ub) {
2569 isl_int_add(v, bmap->ineq[i][1 + j],isl_sioimath_add((v), *(bmap->ineq[i][1 + j]), *(bmap->
div[div][1 + 1 + j]))
2570 bmap->div[div][1 + 1 + j])isl_sioimath_add((v), *(bmap->ineq[i][1 + j]), *(bmap->
div[div][1 + 1 + j]))
;
2571 ub = isl_int_is_divisible_by(v, bmap->div[div][0])isl_sioimath_is_divisible_by(*(v), *(bmap->div[div][0]));
2572 }
2573 }
2574 if (!lb && !ub)
2575 return bmap;
2576
2577 bmap = isl_basic_map_cow(bmap);
2578 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
2579 if (lb) {
2580 int k = isl_basic_map_alloc_inequality(bmap);
2581 if (k < 0)
2582 goto error;
2583 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2584 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]))
2585 bmap->div[div][1 + j])isl_sioimath_sub((bmap->ineq[k][j]), *(bmap->ineq[i][j]
), *(bmap->div[div][1 + j]))
;
2586 isl_int_cdiv_q(bmap->ineq[k][j],isl_sioimath_cdiv_q((bmap->ineq[k][j]), *(bmap->ineq[k]
[j]), *(bmap->div[div][0]))
2587 bmap->ineq[k][j], bmap->div[div][0])isl_sioimath_cdiv_q((bmap->ineq[k][j]), *(bmap->ineq[k]
[j]), *(bmap->div[div][0]))
;
2588 }
2589 isl_int_set_si(bmap->ineq[k][1 + total + div], 1)isl_sioimath_set_si((bmap->ineq[k][1 + total + div]), 1);
2590 }
2591 if (ub) {
2592 int k = isl_basic_map_alloc_inequality(bmap);
2593 if (k < 0)
2594 goto error;
2595 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2596 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]))
2597 bmap->div[div][1 + j])isl_sioimath_add((bmap->ineq[k][j]), *(bmap->ineq[i][j]
), *(bmap->div[div][1 + j]))
;
2598 isl_int_fdiv_q(bmap->ineq[k][j],isl_sioimath_fdiv_q((bmap->ineq[k][j]), *(bmap->ineq[k]
[j]), *(bmap->div[div][0]))
2599 bmap->ineq[k][j], bmap->div[div][0])isl_sioimath_fdiv_q((bmap->ineq[k][j]), *(bmap->ineq[k]
[j]), *(bmap->div[div][0]))
;
2600 }
2601 isl_int_set_si(bmap->ineq[k][1 + total + div], -1)isl_sioimath_set_si((bmap->ineq[k][1 + total + div]), -1);
2602 }
2603
2604 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED)(((bmap)->flags) &= ~((1 << 5)));
2605 return bmap;
2606error:
2607 isl_basic_map_free(bmap);
2608 return NULL((void*)0);
2609}
2610
2611/* This function is called right before "div" is eliminated from "bmap"
2612 * using Fourier-Motzkin.
2613 * Look through the constraints of "bmap" for constraints on the argument
2614 * of the integer division and use them to construct constraints on the
2615 * integer division itself. These constraints can then be combined
2616 * during the Fourier-Motzkin elimination.
2617 * Note that it is only useful to introduce lower bounds on "div"
2618 * if "bmap" already contains upper bounds on "div" as the newly
2619 * introduce lower bounds can then be combined with the pre-existing
2620 * upper bounds. Similarly for upper bounds.
2621 * We therefore first check if "bmap" contains any lower and/or upper bounds
2622 * on "div".
2623 *
2624 * It is interesting to note that the introduction of these constraints
2625 * can indeed lead to more accurate results, even when compared to
2626 * deriving constraints on the argument of "div" from constraints on "div".
2627 * Consider, for example, the set
2628 *
2629 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2630 *
2631 * The second constraint can be rewritten as
2632 *
2633 * 2 * [(-i-2j+3)/4] + k >= 0
2634 *
2635 * from which we can derive
2636 *
2637 * -i - 2j + 3 >= -2k
2638 *
2639 * or
2640 *
2641 * i + 2j <= 3 + 2k
2642 *
2643 * Combined with the first constraint, we obtain
2644 *
2645 * -3 <= 3 + 2k or k >= -3
2646 *
2647 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2648 * the first constraint, we obtain
2649 *
2650 * [(i + 2j)/4] >= [-3/4] = -1
2651 *
2652 * Combining this constraint with the second constraint, we obtain
2653 *
2654 * k >= -2
2655 */
2656static __isl_give isl_basic_map *insert_bounds_on_div(
2657 __isl_take isl_basic_map *bmap, int div)
2658{
2659 int i;
2660 int check_lb, check_ub;
2661 isl_int v;
2662 unsigned total;
2663
2664 if (!bmap)
2665 return NULL((void*)0);
2666
2667 if (isl_int_is_zero(bmap->div[div][0])(isl_sioimath_sgn(*(bmap->div[div][0])) == 0))
2668 return bmap;
2669
2670 total = isl_space_dim(bmap->dim, isl_dim_all);
2671
2672 check_lb = 0;
2673 check_ub = 0;
2674 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
2675 int s = isl_int_sgn(bmap->ineq[i][1 + total + div])isl_sioimath_sgn(*(bmap->ineq[i][1 + total + div]));
2676 if (s > 0)
2677 check_ub = 1;
2678 if (s < 0)
2679 check_lb = 1;
2680 }
2681
2682 if (!check_lb && !check_ub)
2683 return bmap;
2684
2685 isl_int_init(v)isl_sioimath_init((v));
2686
2687 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
2688 if (!isl_int_is_zero(bmap->ineq[i][1 + total + div])(isl_sioimath_sgn(*(bmap->ineq[i][1 + total + div])) == 0))
2689 continue;
2690
2691 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, total, v,
2692 check_lb, check_ub);
2693 }
2694
2695 isl_int_clear(v)isl_sioimath_clear((v));
2696
2697 return bmap;
2698}
2699
2700/* Remove all divs (recursively) involving any of the given dimensions
2701 * in their definitions.
2702 */
2703__isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
2704 __isl_take isl_basic_map *bmap,
2705 enum isl_dim_type type, unsigned first, unsigned n)
2706{
2707 int i;
2708
2709 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2710 return isl_basic_map_free(bmap);
2711 first += isl_basic_map_offset(bmap, type);
2712
2713 for (i = bmap->n_div - 1; i >= 0; --i) {
2714 isl_bool involves;
2715
2716 involves = div_involves_vars(bmap, i, first, n);
2717 if (involves < 0)
2718 return isl_basic_map_free(bmap);
2719 if (!involves)
2720 continue;
2721 bmap = insert_bounds_on_div(bmap, i);
2722 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2723 if (!bmap)
2724 return NULL((void*)0);
2725 i = bmap->n_div;
2726 }
2727
2728 return bmap;
2729}
2730
2731__isl_give isl_basic_setisl_basic_map *isl_basic_set_remove_divs_involving_dims(
2732 __isl_take isl_basic_setisl_basic_map *bset,
2733 enum isl_dim_type type, unsigned first, unsigned n)
2734{
2735 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
2736}
2737
2738__isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
2739 enum isl_dim_type type, unsigned first, unsigned n)
2740{
2741 int i;
2742
2743 if (!map)
2744 return NULL((void*)0);
2745 if (map->n == 0)
2746 return map;
2747
2748 map = isl_map_cow(map);
2749 if (!map)
2750 return NULL((void*)0);
2751
2752 for (i = 0; i < map->n; ++i) {
2753 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
2754 type, first, n);
2755 if (!map->p[i])
2756 goto error;
2757 }
2758 return map;
2759error:
2760 isl_map_free(map);
2761 return NULL((void*)0);
2762}
2763
2764__isl_give isl_setisl_map *isl_set_remove_divs_involving_dims(__isl_take isl_setisl_map *set,
2765 enum isl_dim_type type, unsigned first, unsigned n)
2766{
2767 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set),
2768 type, first, n));
2769}
2770
2771/* Does the description of "bmap" depend on the specified dimensions?
2772 * We also check whether the dimensions appear in any of the div definitions.
2773 * In principle there is no need for this check. If the dimensions appear
2774 * in a div definition, they also appear in the defining constraints of that
2775 * div.
2776 */
2777isl_bool isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
2778 enum isl_dim_type type, unsigned first, unsigned n)
2779{
2780 int i;
2781
2782 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2783 return isl_bool_error;
2784
2785 first += isl_basic_map_offset(bmap, type);
2786 for (i = 0; i < bmap->n_eq; ++i)
2787 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
2788 return isl_bool_true;
2789 for (i = 0; i < bmap->n_ineq; ++i)
2790 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
2791 return isl_bool_true;
2792 for (i = 0; i < bmap->n_div; ++i) {
2793 if (isl_int_is_zero(bmap->div[i][0])(isl_sioimath_sgn(*(bmap->div[i][0])) == 0))
2794 continue;
2795 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
2796 return isl_bool_true;
2797 }
2798
2799 return isl_bool_false;
2800}
2801
2802isl_bool isl_map_involves_dims(__isl_keep isl_map *map,
2803 enum isl_dim_type type, unsigned first, unsigned n)
2804{
2805 int i;
2806
2807 if (!map)
2808 return isl_bool_error;
2809
2810 if (first + n > isl_map_dim(map, type))
2811 isl_die(map->ctx, isl_error_invalid,do { isl_handle_error(map->ctx, isl_error_invalid, "index out of bounds"
, "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 2812); return isl_bool_error; } while (0)
2812 "index out of bounds", return isl_bool_error)do { isl_handle_error(map->ctx, isl_error_invalid, "index out of bounds"
, "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 2812); return isl_bool_error; } while (0)
;
2813
2814 for (i = 0; i < map->n; ++i) {
2815 isl_bool involves = isl_basic_map_involves_dims(map->p[i],
2816 type, first, n);
2817 if (involves < 0 || involves)
2818 return involves;
2819 }
2820
2821 return isl_bool_false;
2822}
2823
2824isl_bool isl_basic_set_involves_dims(__isl_keep isl_basic_setisl_basic_map *bset,
2825 enum isl_dim_type type, unsigned first, unsigned n)
2826{
2827 return isl_basic_map_involves_dims(bset, type, first, n);
2828}
2829
2830isl_bool isl_set_involves_dims(__isl_keep isl_setisl_map *set,
2831 enum isl_dim_type type, unsigned first, unsigned n)
2832{
2833 return isl_map_involves_dims(set, type, first, n);
2834}
2835
2836/* Drop all constraints in bmap that involve any of the dimensions
2837 * first to first+n-1.
2838 */
2839static __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving(
2840 __isl_take isl_basic_map *bmap, unsigned first, unsigned n)
2841{
2842 int i;
2843
2844 if (n == 0)
2845 return bmap;
2846
2847 bmap = isl_basic_map_cow(bmap);
2848
2849 if (!bmap)
2850 return NULL((void*)0);
2851
2852 for (i = bmap->n_eq - 1; i >= 0; --i) {
2853 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) == -1)
2854 continue;
2855 isl_basic_map_drop_equality(bmap, i);
2856 }
2857
2858 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2859 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) == -1)
2860 continue;
2861 isl_basic_map_drop_inequality(bmap, i);
2862 }
2863
2864 bmap = isl_basic_map_add_known_div_constraints(bmap);
2865 return bmap;
2866}
2867
2868/* Drop all constraints in bset that involve any of the dimensions
2869 * first to first+n-1.
2870 */
2871__isl_give isl_basic_setisl_basic_map *isl_basic_set_drop_constraints_involving(
2872 __isl_take isl_basic_setisl_basic_map *bset, unsigned first, unsigned n)
2873{
2874 return isl_basic_map_drop_constraints_involving(bset, first, n);
2875}
2876
2877/* Drop all constraints in bmap that do not involve any of the dimensions
2878 * first to first + n - 1 of the given type.
2879 */
2880__isl_give isl_basic_map *isl_basic_map_drop_constraints_not_involving_dims(
2881 __isl_take isl_basic_map *bmap,
2882 enum isl_dim_type type, unsigned first, unsigned n)
2883{
2884 int i;
2885
2886 if (n == 0) {
2887 isl_space *space = isl_basic_map_get_space(bmap);
2888 isl_basic_map_free(bmap);
2889 return isl_basic_map_universe(space);
2890 }
2891 bmap = isl_basic_map_cow(bmap);
2892 if (!bmap)
2893 return NULL((void*)0);
2894
2895 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2896 return isl_basic_map_free(bmap);
2897
2898 first += isl_basic_map_offset(bmap, type) - 1;
2899
2900 for (i = bmap->n_eq - 1; i >= 0; --i) {
2901 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) != -1)
2902 continue;
2903 isl_basic_map_drop_equality(bmap, i);
2904 }
2905
2906 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2907 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) != -1)
2908 continue;
2909 isl_basic_map_drop_inequality(bmap, i);
2910 }
2911
2912 bmap = isl_basic_map_add_known_div_constraints(bmap);
2913 return bmap;
2914}
2915
2916/* Drop all constraints in bset that do not involve any of the dimensions
2917 * first to first + n - 1 of the given type.
2918 */
2919__isl_give isl_basic_setisl_basic_map *isl_basic_set_drop_constraints_not_involving_dims(
2920 __isl_take isl_basic_setisl_basic_map *bset,
2921 enum isl_dim_type type, unsigned first, unsigned n)
2922{
2923 return isl_basic_map_drop_constraints_not_involving_dims(bset,
2924 type, first, n);
2925}
2926
2927/* Drop all constraints in bmap that involve any of the dimensions
2928 * first to first + n - 1 of the given type.
2929 */
2930__isl_give isl_basic_map *isl_basic_map_drop_constraints_involving_dims(
2931 __isl_take isl_basic_map *bmap,
2932 enum isl_dim_type type, unsigned first, unsigned n)
2933{
2934 if (!bmap)
2935 return NULL((void*)0);
2936 if (n == 0)
2937 return bmap;
2938
2939 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2940 return isl_basic_map_free(bmap);
2941
2942 bmap = isl_basic_map_remove_divs_involving_dims(bmap, type, first, n);
2943 first += isl_basic_map_offset(bmap, type) - 1;
2944 return isl_basic_map_drop_constraints_involving(bmap, first, n);
2945}
2946
2947/* Drop all constraints in bset that involve any of the dimensions
2948 * first to first + n - 1 of the given type.
2949 */
2950__isl_give isl_basic_setisl_basic_map *isl_basic_set_drop_constraints_involving_dims(
2951 __isl_take isl_basic_setisl_basic_map *bset,
2952 enum isl_dim_type type, unsigned first, unsigned n)
2953{
2954 return isl_basic_map_drop_constraints_involving_dims(bset,
2955 type, first, n);
2956}
2957
2958/* Drop constraints from "map" by applying "drop" to each basic map.
2959 */
2960static __isl_give isl_map *drop_constraints(__isl_take isl_map *map,
2961 enum isl_dim_type type, unsigned first, unsigned n,
2962 __isl_give isl_basic_map *(*drop)(__isl_take isl_basic_map *bmap,
2963 enum isl_dim_type type, unsigned first, unsigned n))
2964{
2965 int i;
2966 unsigned dim;
2967
2968 if (!map)
2969 return NULL((void*)0);
2970
2971 dim = isl_map_dim(map, type);
2972 if (first + n > dim || first + n < first)
2973 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", "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 2974); return isl_map_free(map); } while (0)
2974 "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", "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 2974); return isl_map_free(map); } while (0)
;
2975
2976 map = isl_map_cow(map);
2977 if (!map)
2978 return NULL((void*)0);
2979
2980 for (i = 0; i < map->n; ++i) {
2981 map->p[i] = drop(map->p[i], type, first, n);
2982 if (!map->p[i])
2983 return isl_map_free(map);
2984 }
2985
2986 if (map->n > 1)
2987 ISL_F_CLR(map, ISL_MAP_DISJOINT)(((map)->flags) &= ~((1 << 0)));
2988
2989 return map;
2990}
2991
2992/* Drop all constraints in map that involve any of the dimensions
2993 * first to first + n - 1 of the given type.
2994 */
2995__isl_give isl_map *isl_map_drop_constraints_involving_dims(
2996 __isl_take isl_map *map,
2997 enum isl_dim_type type, unsigned first, unsigned n)
2998{
2999 if (n == 0)
3000 return map;
3001 return drop_constraints(map, type, first, n,
3002 &isl_basic_map_drop_constraints_involving_dims);
3003}
3004
3005/* Drop all constraints in "map" that do not involve any of the dimensions
3006 * first to first + n - 1 of the given type.
3007 */
3008__isl_give isl_map *isl_map_drop_constraints_not_involving_dims(
3009 __isl_take isl_map *map,
3010 enum isl_dim_type type, unsigned first, unsigned n)
3011{
3012 if (n == 0) {
3013 isl_space *space = isl_map_get_space(map);
3014 isl_map_free(map);
3015 return isl_map_universe(space);
3016 }
3017 return drop_constraints(map, type, first, n,
3018 &isl_basic_map_drop_constraints_not_involving_dims);
3019}
3020
3021/* Drop all constraints in set that involve any of the dimensions
3022 * first to first + n - 1 of the given type.
3023 */
3024__isl_give isl_setisl_map *isl_set_drop_constraints_involving_dims(
3025 __isl_take isl_setisl_map *set,
3026 enum isl_dim_type type, unsigned first, unsigned n)
3027{
3028 return isl_map_drop_constraints_involving_dims(set, type, first, n);
3029}
3030
3031/* Drop all constraints in "set" that do not involve any of the dimensions
3032 * first to first + n - 1 of the given type.
3033 */
3034__isl_give isl_setisl_map *isl_set_drop_constraints_not_involving_dims(
3035 __isl_take isl_setisl_map *set,
3036 enum isl_dim_type type, unsigned first, unsigned n)
3037{
3038 return isl_map_drop_constraints_not_involving_dims(set, type, first, n);
3039}
3040
3041/* Does local variable "div" of "bmap" have a complete explicit representation?
3042 * Having a complete explicit representation requires not only
3043 * an explicit representation, but also that all local variables
3044 * that appear in this explicit representation in turn have
3045 * a complete explicit representation.
3046 */
3047isl_bool isl_basic_map_div_is_known(__isl_keep isl_basic_map *bmap, int div)
3048{
3049 int i;
3050 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
3051 isl_bool marked;
3052
3053 marked = isl_basic_map_div_is_marked_unknown(bmap, div);
3054 if (marked < 0 || marked)
3055 return isl_bool_not(marked);
3056
3057 for (i = bmap->n_div - 1; i >= 0; --i) {
3058 isl_bool known;
3059
3060 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i])(isl_sioimath_sgn(*(bmap->div[div][1 + div_offset + i])) ==
0)
)
3061 continue;
3062 known = isl_basic_map_div_is_known(bmap, i);
3063 if (known < 0 || !known)
3064 return known;
3065 }
3066
3067 return isl_bool_true;
3068}
3069
3070/* Remove all divs that are unknown or defined in terms of unknown divs.
3071 */
3072__isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
3073 __isl_take isl_basic_map *bmap)
3074{
3075 int i;
3076
3077 if (!bmap)
3078 return NULL((void*)0);
3079
3080 for (i = bmap->n_div - 1; i >= 0; --i) {
3081 if (isl_basic_map_div_is_known(bmap, i))
3082 continue;
3083 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
3084 if (!bmap)
3085 return NULL((void*)0);
3086 i = bmap->n_div;
3087 }
3088
3089 return bmap;
3090}
3091
3092/* Remove all divs that are unknown or defined in terms of unknown divs.
3093 */
3094__isl_give isl_basic_setisl_basic_map *isl_basic_set_remove_unknown_divs(
3095 __isl_take isl_basic_setisl_basic_map *bset)
3096{
3097 return isl_basic_map_remove_unknown_divs(bset);
3098}
3099
3100__isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
3101{
3102 int i;
3103
3104 if (!map)
3105 return NULL((void*)0);
3106 if (map->n == 0)
3107 return map;
3108
3109 map = isl_map_cow(map);
3110 if (!map)
3111 return NULL((void*)0);
3112
3113 for (i = 0; i < map->n; ++i) {
3114 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
3115 if (!map->p[i])
3116 goto error;
3117 }
3118 return map;
3119error:
3120 isl_map_free(map);
3121 return NULL((void*)0);
3122}
3123
3124__isl_give isl_setisl_map *isl_set_remove_unknown_divs(__isl_take isl_setisl_map *set)
3125{
3126 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set)));
3127}
3128
3129__isl_give isl_basic_setisl_basic_map *isl_basic_set_remove_dims(
3130 __isl_take isl_basic_setisl_basic_map *bset,
3131 enum isl_dim_type type, unsigned first, unsigned n)
3132{
3133 isl_basic_map *bmap = bset_to_bmap(bset);
3134 bmap = isl_basic_map_remove_dims(bmap, type, first, n);
3135 return bset_from_bmap(bmap);
3136}
3137
3138__isl_give isl_map *isl_map_remove_dims(__isl_take isl_map *map,
3139 enum isl_dim_type type, unsigned first, unsigned n)
3140{
3141 int i;
3142
3143 if (n == 0)
3144 return map;
3145
3146 map = isl_map_cow(map);
3147 if (!map)
3148 return NULL((void*)0);
3149 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", "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 3149); goto error; } while (0); } while (0)
;
3150
3151 for (i = 0; i < map->n; ++i) {
3152 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
3153 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
3154 if (!map->p[i])
3155 goto error;
3156 }
3157 map = isl_map_drop(map, type, first, n);
3158 return map;
3159error:
3160 isl_map_free(map);
3161 return NULL((void*)0);
3162}
3163
3164__isl_give isl_setisl_map *isl_set_remove_dims(__isl_take isl_setisl_map *bset,
3165 enum isl_dim_type type, unsigned first, unsigned n)
3166{
3167 return set_from_map(isl_map_remove_dims(set_to_map(bset),
3168 type, first, n));
3169}
3170
3171/* Project out n inputs starting at first using Fourier-Motzkin */
3172struct isl_map *isl_map_remove_inputs(struct isl_map *map,
3173 unsigned first, unsigned n)
3174{
3175 return isl_map_remove_dims(map, isl_dim_in, first, n);
3176}
3177
3178static void dump_term(struct isl_basic_map *bmap,
3179 isl_int c, int pos, FILE *out)
3180{
3181 const char *name;
3182 unsigned in = isl_basic_map_dim(bmap, isl_dim_in);
3183 unsigned dim = in + isl_basic_map_dim(bmap, isl_dim_out);
3184 unsigned nparam = isl_basic_map_dim(bmap, isl_dim_param);
3185 if (!pos)
3186 isl_int_print(out, c, 0)isl_sioimath_print(out, *(c), 0);
3187 else {
3188 if (!isl_int_is_one(c)(isl_sioimath_cmp_si(*(c), 1) == 0))
3189 isl_int_print(out, c, 0)isl_sioimath_print(out, *(c), 0);
3190 if (pos < 1 + nparam) {
3191 name = isl_space_get_dim_name(bmap->dim,
3192 isl_dim_param, pos - 1);
3193 if (name)
3194 fprintf(out, "%s", name);
3195 else
3196 fprintf(out, "p%d", pos - 1);
3197 } else if (pos < 1 + nparam + in)
3198 fprintf(out, "i%d", pos - 1 - nparam);
3199 else if (pos < 1 + nparam + dim)
3200 fprintf(out, "o%d", pos - 1 - nparam - in);
3201 else
3202 fprintf(out, "e%d", pos - 1 - nparam - dim);
3203 }
3204}
3205
3206static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
3207 int sign, FILE *out)
3208{
3209 int i;
3210 int first;
3211 unsigned len = 1 + isl_basic_map_total_dim(bmap);
3212 isl_int v;
3213
3214 isl_int_init(v)isl_sioimath_init((v));
3215 for (i = 0, first = 1; i < len; ++i) {
3216 if (isl_int_sgn(c[i])isl_sioimath_sgn(*(c[i])) * sign <= 0)
3217 continue;
3218 if (!first)
3219 fprintf(out, " + ");
3220 first = 0;
3221 isl_int_abs(v, c[i])isl_sioimath_abs((v), *(c[i]));
3222 dump_term(bmap, v, i, out);
3223 }
3224 isl_int_clear(v)isl_sioimath_clear((v));
3225 if (first)
3226 fprintf(out, "0");
3227}
3228
3229static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
3230 const char *op, FILE *out, int indent)
3231{
3232 int i;
3233
3234 fprintf(out, "%*s", indent, "");
3235
3236 dump_constraint_sign(bmap, c, 1, out);
3237 fprintf(out, " %s ", op);
3238 dump_constraint_sign(bmap, c, -1, out);
3239
3240 fprintf(out, "\n");
3241
3242 for (i = bmap->n_div; i < bmap->extra; ++i) {
3243 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)
)
3244 continue;
3245 fprintf(out, "%*s", indent, "");
3246 fprintf(out, "ERROR: unused div coefficient not zero\n");
3247 abort();
3248 }
3249}
3250
3251static void dump_constraints(struct isl_basic_map *bmap,
3252 isl_int **c, unsigned n,
3253 const char *op, FILE *out, int indent)
3254{
3255 int i;
3256
3257 for (i = 0; i < n; ++i)
3258 dump_constraint(bmap, c[i], op, out, indent);
3259}
3260
3261static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
3262{
3263 int j;
3264 int first = 1;
3265 unsigned total = isl_basic_map_total_dim(bmap);
3266
3267 for (j = 0; j < 1 + total; ++j) {
3268 if (isl_int_is_zero(exp[j])(isl_sioimath_sgn(*(exp[j])) == 0))
3269 continue;
3270 if (!first && isl_int_is_pos(exp[j])(isl_sioimath_sgn(*(exp[j])) > 0))
3271 fprintf(out, "+");
3272 dump_term(bmap, exp[j], j, out);
3273 first = 0;
3274 }
3275}
3276
3277static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
3278{
3279 int i;
3280
3281 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
3282 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
3283
3284 for (i = 0; i < bmap->n_div; ++i) {
3285 fprintf(out, "%*s", indent, "");
3286 fprintf(out, "e%d = [(", i);
3287 dump_affine(bmap, bmap->div[i]+1, out);
3288 fprintf(out, ")/");
3289 isl_int_print(out, bmap->div[i][0], 0)isl_sioimath_print(out, *(bmap->div[i][0]), 0);
3290 fprintf(out, "]\n");
3291 }
3292}
3293
3294void isl_basic_set_print_internal(struct isl_basic_setisl_basic_map *bset,
3295 FILE *out, int indent)
3296{
3297 if (!bset) {
3298 fprintf(out, "null basic set\n");
3299 return;
3300 }
3301
3302 fprintf(out, "%*s", indent, "");
3303 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
3304 bset->ref, bset->dim->nparam, bset->dim->n_out,
3305 bset->extra, bset->flags);
3306 dump(bset_to_bmap(bset), out, indent);
3307}
3308
3309void isl_basic_map_print_internal(struct isl_basic_map *bmap,
3310 FILE *out, int indent)
3311{
3312 if (!bmap) {
3313 fprintf(out, "null basic map\n");
3314 return;
3315 }
3316
3317 fprintf(out, "%*s", indent, "");
3318 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
3319 "flags: %x, n_name: %d\n",
3320 bmap->ref,
3321 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
3322 bmap->extra, bmap->flags, bmap->dim->n_id);
3323 dump(bmap, out, indent);
3324}
3325
3326int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
3327{
3328 unsigned total;
3329 if (!bmap)
3330 return -1;
3331 total = isl_basic_map_total_dim(bmap);
3332 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", "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 3332); return -1; } while (0); } while (0)
;
3333 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
3334 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)
;
3335 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED)(((bmap)->flags) &= ~((1 << 5)));
3336 return 0;
3337}
3338
3339__isl_give isl_setisl_map *isl_set_alloc_space(__isl_take isl_space *space, int n,
3340 unsigned flags)
3341{
3342 if (!space)
3343 return NULL((void*)0);
3344 if (isl_space_dim(space, isl_dim_in) != 0)
3345 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", "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 3346); goto error; } while (0)
3346 "set cannot have input dimensions", goto error)do { isl_handle_error(isl_space_get_ctx(space), isl_error_invalid
, "set cannot have input dimensions", "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 3346); goto error; } while (0)
;
3347 return isl_map_alloc_space(space, n, flags);
3348error:
3349 isl_space_free(space);
3350 return NULL((void*)0);
3351}
3352
3353/* Make sure "map" has room for at least "n" more basic maps.
3354 */
3355__isl_give isl_map *isl_map_grow(__isl_take isl_map *map, int n)
3356{
3357 int i;
3358 struct isl_map *grown = NULL((void*)0);
3359
3360 if (!map)
3361 return NULL((void*)0);
3362 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", "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 3362); goto error; } while (0); } while (0)
;
3363 if (map->n + n <= map->size)
3364 return map;
3365 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
3366 if (!grown)
3367 goto error;
3368 for (i = 0; i < map->n; ++i) {
3369 grown->p[i] = isl_basic_map_copy(map->p[i]);
3370 if (!grown->p[i])
3371 goto error;
3372 grown->n++;
3373 }
3374 isl_map_free(map);
3375 return grown;
3376error:
3377 isl_map_free(grown);
3378 isl_map_free(map);
3379 return NULL((void*)0);
3380}
3381
3382/* Make sure "set" has room for at least "n" more basic sets.
3383 */
3384struct isl_setisl_map *isl_set_grow(struct isl_setisl_map *set, int n)
3385{
3386 return set_from_map(isl_map_grow(set_to_map(set), n));
3387}
3388
3389__isl_give isl_setisl_map *isl_set_from_basic_set(__isl_take isl_basic_setisl_basic_map *bset)
3390{
3391 return isl_map_from_basic_map(bset);
3392}
3393
3394__isl_give isl_map *isl_map_from_basic_map(__isl_take isl_basic_map *bmap)
3395{
3396 struct isl_map *map;
3397
3398 if (!bmap)
3399 return NULL((void*)0);
3400
3401 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT(1 << 0));
3402 return isl_map_add_basic_map(map, bmap);
3403}
3404
3405__isl_give isl_setisl_map *isl_set_add_basic_set(__isl_take isl_setisl_map *set,
3406 __isl_take isl_basic_setisl_basic_map *bset)
3407{
3408 return set_from_map(isl_map_add_basic_map(set_to_map(set),
3409 bset_to_bmap(bset)));
3410}
3411
3412__isl_null isl_setisl_map *isl_set_free(__isl_take isl_setisl_map *set)
3413{
3414 return isl_map_free(set);
3415}
3416
3417void isl_set_print_internal(struct isl_setisl_map *set, FILE *out, int indent)
3418{
3419 int i;
3420
3421 if (!set) {
3422 fprintf(out, "null set\n");
3423 return;
3424 }
3425
3426 fprintf(out, "%*s", indent, "");
3427 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
3428 set->ref, set->n, set->dim->nparam, set->dim->n_out,
3429 set->flags);
3430 for (i = 0; i < set->n; ++i) {
3431 fprintf(out, "%*s", indent, "");
3432 fprintf(out, "basic set %d:\n", i);
3433 isl_basic_set_print_internal(set->p[i], out, indent+4);
3434 }
3435}
3436
3437void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
3438{
3439 int i;
3440
3441 if (!map) {
3442 fprintf(out, "null map\n");
3443 return;
3444 }
3445
3446 fprintf(out, "%*s", indent, "");
3447 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
3448 "flags: %x, n_name: %d\n",
3449 map->ref, map->n, map->dim->nparam, map->dim->n_in,
3450 map->dim->n_out, map->flags, map->dim->n_id);
3451 for (i = 0; i < map->n; ++i) {
3452 fprintf(out, "%*s", indent, "");
3453 fprintf(out, "basic map %d:\n", i);
3454 isl_basic_map_print_internal(map->p[i], out, indent+4);
3455 }
3456}
3457
3458__isl_give isl_basic_map *isl_basic_map_intersect_domain(
3459 __isl_take isl_basic_map *bmap, __isl_take isl_basic_setisl_basic_map *bset)
3460{
3461 struct isl_basic_map *bmap_domain;
3462
3463 if (isl_basic_map_check_equal_params(bmap, bset_to_bmap(bset)) < 0)
3464 goto error;
3465
3466 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
3467 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", "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 3468); goto error; } while (0); } while (0)
3468 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", "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 3468); goto error; } while (0); } while (0)
;
3469
3470 bmap = isl_basic_map_cow(bmap);
3471 if (!bmap)
3472 goto error;
3473 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
3474 bset->n_div, bset->n_eq, bset->n_ineq);
3475 bmap_domain = isl_basic_map_from_domain(bset);
3476 bmap = add_constraints(bmap, bmap_domain, 0, 0);
3477
3478 bmap = isl_basic_map_simplify(bmap);
3479 return isl_basic_map_finalize(bmap);
3480error:
3481 isl_basic_map_free(bmap);
3482 isl_basic_set_free(bset);
3483 return NULL((void*)0);
3484}
3485
3486/* Check that the space of "bset" is the same as that of the range of "bmap".
3487 */
3488static isl_stat isl_basic_map_check_compatible_range(
3489 __isl_keep isl_basic_map *bmap, __isl_keep isl_basic_setisl_basic_map *bset)
3490{
3491 isl_bool ok;
3492
3493 ok = isl_basic_map_compatible_range(bmap, bset);
3494 if (ok < 0)
3495 return isl_stat_error;
3496 if (!ok)
3497 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", "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 3498); return isl_stat_error; } while (0)
3498 "incompatible spaces", return isl_stat_error)do { isl_handle_error(isl_basic_set_get_ctx(bset), isl_error_invalid
, "incompatible spaces", "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 3498); return isl_stat_error; } while (0)
;
3499
3500 return isl_stat_ok;
3501}
3502
3503__isl_give isl_basic_map *isl_basic_map_intersect_range(
3504 __isl_take isl_basic_map *bmap, __isl_take isl_basic_setisl_basic_map *bset)
3505{
3506 struct isl_basic_map *bmap_range;
3507
3508 if (isl_basic_map_check_equal_params(bmap, bset_to_bmap(bset)) < 0)
3509 goto error;
3510
3511 if (isl_space_dim(bset->dim, isl_dim_set) != 0 &&
3512 isl_basic_map_check_compatible_range(bmap, bset) < 0)
3513 goto error;
3514
3515 if (isl_basic_set_plain_is_universe(bset)) {
3516 isl_basic_set_free(bset);
3517 return bmap;
3518 }
3519
3520 bmap = isl_basic_map_cow(bmap);
3521 if (!bmap)
3522 goto error;
3523 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
3524 bset->n_div, bset->n_eq, bset->n_ineq);
3525 bmap_range = bset_to_bmap(bset);
3526 bmap = add_constraints(bmap, bmap_range, 0, 0);
3527
3528 bmap = isl_basic_map_simplify(bmap);
3529 return isl_basic_map_finalize(bmap);
3530error:
3531 isl_basic_map_free(bmap);
3532 isl_basic_set_free(bset);
3533 return NULL((void*)0);
3534}
3535
3536isl_bool isl_basic_map_contains(__isl_keep isl_basic_map *bmap,
3537 __isl_keep isl_vec *vec)
3538{
3539 int i;
3540 unsigned total;
3541 isl_int s;
3542
3543 if (!bmap || !vec)
3544 return isl_bool_error;
3545
3546 total = 1 + isl_basic_map_total_dim(bmap);
3547 if (total != vec->size)
3548 return isl_bool_false;
3549
3550 isl_int_init(s)isl_sioimath_init((s));
3551
3552 for (i = 0; i < bmap->n_eq; ++i) {
3553 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
3554 if (!isl_int_is_zero(s)(isl_sioimath_sgn(*(s)) == 0)) {
3555 isl_int_clear(s)isl_sioimath_clear((s));
3556 return isl_bool_false;
3557 }
3558 }
3559
3560 for (i = 0; i < bmap->n_ineq; ++i) {
3561 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
3562 if (isl_int_is_neg(s)(isl_sioimath_sgn(*(s)) < 0)) {
3563 isl_int_clear(s)isl_sioimath_clear((s));
3564 return isl_bool_false;
3565 }
3566 }
3567
3568 isl_int_clear(s)isl_sioimath_clear((s));
3569
3570 return isl_bool_true;
3571}
3572
3573isl_bool isl_basic_set_contains(__isl_keep isl_basic_setisl_basic_map *bset,
3574 __isl_keep isl_vec *vec)
3575{
3576 return isl_basic_map_contains(bset_to_bmap(bset), vec);
3577}
3578
3579__isl_give isl_basic_map *isl_basic_map_intersect(
3580 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
3581{
3582 struct isl_vec *sample = NULL((void*)0);
3583
3584 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
3585 goto error;
3586 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
3587 isl_space_dim(bmap1->dim, isl_dim_param) &&
3588 isl_space_dim(bmap2->dim, isl_dim_all) !=
3589 isl_space_dim(bmap2->dim, isl_dim_param))
3590 return isl_basic_map_intersect(bmap2, bmap1);
3591
3592 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
3593 isl_space_dim(bmap2->dim, isl_dim_param))
3594 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"
, "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 3595); goto error; } while (0); } while (0)
3595 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"
, "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 3595); goto error; } while (0); } while (0)
;
3596
3597 if (isl_basic_map_plain_is_empty(bmap1)) {
3598 isl_basic_map_free(bmap2);
3599 return bmap1;
3600 }
3601 if (isl_basic_map_plain_is_empty(bmap2)) {
3602 isl_basic_map_free(bmap1);
3603 return bmap2;
3604 }
3605
3606 if (bmap1->sample &&
3607 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
3608 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
3609 sample = isl_vec_copy(bmap1->sample);
3610 else if (bmap2->sample &&
3611 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
3612 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
3613 sample = isl_vec_copy(bmap2->sample);
3614
3615 bmap1 = isl_basic_map_cow(bmap1);
3616 if (!bmap1)
3617 goto error;
3618 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
3619 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
3620 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
3621
3622 if (!bmap1)
3623 isl_vec_free(sample);
3624 else if (sample) {
3625 isl_vec_free(bmap1->sample);
3626 bmap1->sample = sample;
3627 }
3628
3629 bmap1 = isl_basic_map_simplify(bmap1);
3630 return isl_basic_map_finalize(bmap1);
3631error:
3632 if (sample)
3633 isl_vec_free(sample);
3634 isl_basic_map_free(bmap1);
3635 isl_basic_map_free(bmap2);
3636 return NULL((void*)0);
3637}
3638
3639struct isl_basic_setisl_basic_map *isl_basic_set_intersect(
3640 struct isl_basic_setisl_basic_map *bset1, struct isl_basic_setisl_basic_map *bset2)
3641{
3642 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1),
3643 bset_to_bmap(bset2)));
3644}
3645
3646__isl_give isl_basic_setisl_basic_map *isl_basic_set_intersect_params(
3647 __isl_take isl_basic_setisl_basic_map *bset1, __isl_take isl_basic_setisl_basic_map *bset2)
3648{
3649 return isl_basic_set_intersect(bset1, bset2);
3650}
3651
3652/* Special case of isl_map_intersect, where both map1 and map2
3653 * are convex, without any divs and such that either map1 or map2
3654 * contains a single constraint. This constraint is then simply
3655 * added to the other map.
3656 */
3657static __isl_give isl_map *map_intersect_add_constraint(
3658 __isl_take isl_map *map1, __isl_take isl_map *map2)
3659{
3660 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"
, "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 3660); goto error; } while (0); } while (0)
;
3661 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"
, "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 3661); goto error; } while (0); } while (0)
;
3662 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", "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 3662); goto error; } while (0); } while (0)
;
3663 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", "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 3663); goto error; } while (0); } while (0)
;
3664
3665 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
3666 return isl_map_intersect(map2, map1);
3667
3668 map1 = isl_map_cow(map1);
3669 if (!map1)
3670 goto error;
3671 if (isl_map_plain_is_empty(map1)) {
3672 isl_map_free(map2);
3673 return map1;
3674 }
3675 map1->p[0] = isl_basic_map_cow(map1->p[0]);
3676 if (map2->p[0]->n_eq == 1)
3677 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
3678 else
3679 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
3680 map2->p[0]->ineq[0]);
3681
3682 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
3683 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
3684 if (!map1->p[0])
3685 goto error;
3686
3687 if (isl_basic_map_plain_is_empty(map1->p[0])) {
3688 isl_basic_map_free(map1->p[0]);
3689 map1->n = 0;
3690 }
3691
3692 isl_map_free(map2);
3693
3694 return map1;
3695error:
3696 isl_map_free(map1);
3697 isl_map_free(map2);
3698 return NULL((void*)0);
3699}
3700
3701/* map2 may be either a parameter domain or a map living in the same
3702 * space as map1.
3703 */
3704static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
3705 __isl_take isl_map *map2)
3706{
3707 unsigned flags = 0;
3708 isl_map *result;
3709 int i, j;
3710
3711 if (!map1 || !map2)
3712 goto error;
3713
3714 if ((isl_map_plain_is_empty(map1) ||
3715 isl_map_plain_is_universe(map2)) &&
3716 isl_space_is_equal(map1->dim, map2->dim)) {
3717 isl_map_free(map2);
3718 return map1;
3719 }
3720 if ((isl_map_plain_is_empty(map2) ||
3721 isl_map_plain_is_universe(map1)) &&
3722 isl_space_is_equal(map1->dim, map2->dim)) {
3723 isl_map_free(map1);
3724 return map2;
3725 }
3726
3727 if (map1->n == 1 && map2->n == 1 &&
3728 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
3729 isl_space_is_equal(map1->dim, map2->dim) &&
3730 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
3731 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
3732 return map_intersect_add_constraint(map1, map2);
3733
3734 if (isl_space_dim(map2->dim, isl_dim_all) !=
3735 isl_space_dim(map2->dim, isl_dim_param))
3736 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"
, "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 3737); goto error; } while (0); } while (0)
3737 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"
, "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 3737); goto error; } while (0); } while (0)
;
3738
3739 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT)(!!(((map1)->flags) & ((1 << 0)))) &&
3740 ISL_F_ISSET(map2, ISL_MAP_DISJOINT)(!!(((map2)->flags) & ((1 << 0)))))
3741 ISL_FL_SET(flags, ISL_MAP_DISJOINT)((flags) |= ((1 << 0)));
3742
3743 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3744 map1->n * map2->n, flags);
3745 if (!result)
3746 goto error;
3747 for (i = 0; i < map1->n; ++i)
3748 for (j = 0; j < map2->n; ++j) {
3749 struct isl_basic_map *part;
3750 part = isl_basic_map_intersect(
3751 isl_basic_map_copy(map1->p[i]),
3752 isl_basic_map_copy(map2->p[j]));
3753 if (isl_basic_map_is_empty(part) < 0)
3754 part = isl_basic_map_free(part);
3755 result = isl_map_add_basic_map(result, part);
3756 if (!result)
3757 goto error;
3758 }
3759 isl_map_free(map1);
3760 isl_map_free(map2);
3761 return result;
3762error:
3763 isl_map_free(map1);
3764 isl_map_free(map2);
3765 return NULL((void*)0);
3766}
3767
3768static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
3769 __isl_take isl_map *map2)
3770{
3771 if (!map1 || !map2)
3772 goto error;
3773 if (!isl_space_is_equal(map1->dim, map2->dim))
3774 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", "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 3775); goto error; } while (0)
3775 "spaces don't match", goto error)do { isl_handle_error(isl_map_get_ctx(map1), isl_error_invalid
, "spaces don't match", "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 3775); goto error; } while (0)
;
3776 return map_intersect_internal(map1, map2);
3777error:
3778 isl_map_free(map1);
3779 isl_map_free(map2);
3780 return NULL((void*)0);
3781}
3782
3783__isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
3784 __isl_take isl_map *map2)
3785{
3786 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
3787}
3788
3789struct isl_setisl_map *isl_set_intersect(struct isl_setisl_map *set1, struct isl_setisl_map *set2)
3790{
3791 return set_from_map(isl_map_intersect(set_to_map(set1),
3792 set_to_map(set2)));
3793}
3794
3795/* map_intersect_internal accepts intersections
3796 * with parameter domains, so we can just call that function.
3797 */
3798static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
3799 __isl_take isl_setisl_map *params)
3800{
3801 return map_intersect_internal(map, params);
3802}
3803
3804__isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
3805 __isl_take isl_map *map2)
3806{
3807 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
3808}
3809
3810__isl_give isl_setisl_map *isl_set_intersect_params(__isl_take isl_setisl_map *set,
3811 __isl_take isl_setisl_map *params)
3812{
3813 return isl_map_intersect_params(set, params);
3814}
3815
3816__isl_give isl_basic_map *isl_basic_map_reverse(__isl_take isl_basic_map *bmap)
3817{
3818 isl_space *space;
3819 unsigned pos, n1, n2;
3820
3821 if (!bmap)
3822 return NULL((void*)0);
3823 bmap = isl_basic_map_cow(bmap);
3824 if (!bmap)
3825 return NULL((void*)0);
3826 space = isl_space_reverse(isl_space_copy(bmap->dim));
3827 pos = isl_basic_map_offset(bmap, isl_dim_in);
3828 n1 = isl_basic_map_dim(bmap, isl_dim_in);
3829 n2 = isl_basic_map_dim(bmap, isl_dim_out);
3830 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
3831 return isl_basic_map_reset_space(bmap, space);
3832}
3833
3834static __isl_give isl_basic_map *basic_map_space_reset(
3835 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
3836{
3837 isl_space *space;
3838
3839 if (!bmap)
3840 return NULL((void*)0);
3841 if (!isl_space_is_named_or_nested(bmap->dim, type))
3842 return bmap;
3843
3844 space = isl_basic_map_get_space(bmap);
3845 space = isl_space_reset(space, type);
3846 bmap = isl_basic_map_reset_space(bmap, space);
3847 return bmap;
3848}
3849
3850__isl_give isl_basic_map *isl_basic_map_insert_dims(
3851 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3852 unsigned pos, unsigned n)
3853{
3854 isl_bool rational;
3855 isl_space *res_dim;
3856 struct isl_basic_map *res;
3857 struct isl_dim_map *dim_map;
3858 unsigned total, off;
3859 enum isl_dim_type t;
3860
3861 if (n == 0)
3862 return basic_map_space_reset(bmap, type);
3863
3864 if (!bmap)
3865 return NULL((void*)0);
3866
3867 res_dim = isl_space_insert_dims(isl_basic_map_get_space(bmap), type, pos, n);
3868
3869 total = isl_basic_map_total_dim(bmap) + n;
3870 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3871 off = 0;
3872 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3873 if (t != type) {
3874 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3875 } else {
3876 unsigned size = isl_basic_map_dim(bmap, t);
3877 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3878 0, pos, off);
3879 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3880 pos, size - pos, off + pos + n);
3881 }
3882 off += isl_space_dim(res_dim, t);
3883 }
3884 isl_dim_map_div(dim_map, bmap, off);
3885
3886 res = isl_basic_map_alloc_space(res_dim,
3887 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3888 rational = isl_basic_map_is_rational(bmap);
3889 if (rational < 0)
3890 res = isl_basic_map_free(res);
3891 if (rational)
3892 res = isl_basic_map_set_rational(res);
3893 if (isl_basic_map_plain_is_empty(bmap)) {
3894 isl_basic_map_free(bmap);
3895 free(dim_map);
3896 return isl_basic_map_set_to_empty(res);
3897 }
3898 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3899 return isl_basic_map_finalize(res);
3900}
3901
3902__isl_give isl_basic_setisl_basic_map *isl_basic_set_insert_dims(
3903 __isl_take isl_basic_setisl_basic_map *bset,
3904 enum isl_dim_type type, unsigned pos, unsigned n)
3905{
3906 return isl_basic_map_insert_dims(bset, type, pos, n);
3907}
3908
3909__isl_give isl_basic_map *isl_basic_map_add_dims(__isl_take isl_basic_map *bmap,
3910 enum isl_dim_type type, unsigned n)
3911{
3912 if (!bmap)
3913 return NULL((void*)0);
3914 return isl_basic_map_insert_dims(bmap, type,
3915 isl_basic_map_dim(bmap, type), n);
3916}
3917
3918__isl_give isl_basic_setisl_basic_map *isl_basic_set_add_dims(__isl_take isl_basic_setisl_basic_map *bset,
3919 enum isl_dim_type type, unsigned n)
3920{
3921 if (!bset)
3922 return NULL((void*)0);
3923 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", "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 3923); goto error; } while (0); } while (0)
;
3924 return isl_basic_map_add_dims(bset, type, n);
3925error:
3926 isl_basic_set_free(bset);
3927 return NULL((void*)0);
3928}
3929
3930static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
3931 enum isl_dim_type type)
3932{
3933 isl_space *space;
3934
3935 if (!map || !isl_space_is_named_or_nested(map->dim, type))
3936 return map;
3937
3938 space = isl_map_get_space(map);
3939 space = isl_space_reset(space, type);
3940 map = isl_map_reset_space(map, space);
3941 return map;
3942}
3943
3944__isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
3945 enum isl_dim_type type, unsigned pos, unsigned n)
3946{
3947 int i;
3948
3949 if (n == 0)
3950 return map_space_reset(map, type);
3951
3952 map = isl_map_cow(map);
3953 if (!map)
3954 return NULL((void*)0);
3955
3956 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
3957 if (!map->dim)
3958 goto error;
3959
3960 for (i = 0; i < map->n; ++i) {
3961 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
3962 if (!map->p[i])
3963 goto error;
3964 }
3965
3966 return map;
3967error:
3968 isl_map_free(map);
3969 return NULL((void*)0);
3970}
3971
3972__isl_give isl_setisl_map *isl_set_insert_dims(__isl_take isl_setisl_map *set,
3973 enum isl_dim_type type, unsigned pos, unsigned n)
3974{
3975 return isl_map_insert_dims(set, type, pos, n);
3976}
3977
3978__isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
3979 enum isl_dim_type type, unsigned n)
3980{
3981 if (!map)
3982 return NULL((void*)0);
3983 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
3984}
3985
3986__isl_give isl_setisl_map *isl_set_add_dims(__isl_take isl_setisl_map *set,
3987 enum isl_dim_type type, unsigned n)
3988{
3989 if (!set)
3990 return NULL((void*)0);
3991 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", "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 3991); goto error; } while (0); } while (0)
;
3992 return set_from_map(isl_map_add_dims(set_to_map(set), type, n));
3993error:
3994 isl_set_free(set);
3995 return NULL((void*)0);
3996}
3997
3998__isl_give isl_basic_map *isl_basic_map_move_dims(
3999 __isl_take isl_basic_map *bmap,
4000 enum isl_dim_type dst_type, unsigned dst_pos,
4001 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4002{
4003 struct isl_dim_map *dim_map;
4004 struct isl_basic_map *res;
4005 enum isl_dim_type t;
4006 unsigned total, off;
4007
4008 if (!bmap)
4009 return NULL((void*)0);
4010 if (n == 0) {
4011 bmap = isl_basic_map_reset(bmap, src_type);
4012 bmap = isl_basic_map_reset(bmap, dst_type);
4013 return bmap;
4014 }
4015
4016 if (isl_basic_map_check_range(bmap, src_type, src_pos, n) < 0)
4017 return isl_basic_map_free(bmap);
4018
4019 if (dst_type == src_type && dst_pos == src_pos)
4020 return bmap;
4021
4022 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", "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 4022); goto error; } while (0); } while (0)
;
4023
4024 if (pos(bmap->dim, dst_type) + dst_pos ==
4025 pos(bmap->dim, src_type) + src_pos +
4026 ((src_type < dst_type) ? n : 0)) {
4027 bmap = isl_basic_map_cow(bmap);
4028 if (!bmap)
4029 return NULL((void*)0);
4030
4031 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
4032 src_type, src_pos, n);
4033 if (!bmap->dim)
4034 goto error;
4035
4036 bmap = isl_basic_map_finalize(bmap);
4037
4038 return bmap;
4039 }
4040
4041 total = isl_basic_map_total_dim(bmap);
4042 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4043
4044 off = 0;
4045 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
4046 unsigned size = isl_space_dim(bmap->dim, t);
4047 if (t == dst_type) {
4048 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4049 0, dst_pos, off);
4050 off += dst_pos;
4051 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
4052 src_pos, n, off);
4053 off += n;
4054 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4055 dst_pos, size - dst_pos, off);
4056 off += size - dst_pos;
4057 } else if (t == src_type) {
4058 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4059 0, src_pos, off);
4060 off += src_pos;
4061 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4062 src_pos + n, size - src_pos - n, off);
4063 off += size - src_pos - n;
4064 } else {
4065 isl_dim_map_dim(dim_map, bmap->dim, t, off);
4066 off += size;
4067 }
4068 }
4069 isl_dim_map_div(dim_map, bmap, off);
4070
4071 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
4072 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4073 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4074 if (!bmap)
4075 goto error;
4076
4077 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
4078 src_type, src_pos, n);
4079 if (!bmap->dim)
4080 goto error;
4081
4082 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED)(((bmap)->flags) &= ~((1 << 5)));
4083 bmap = isl_basic_map_gauss(bmap, NULL((void*)0));
4084 bmap = isl_basic_map_finalize(bmap);
4085
4086 return bmap;
4087error:
4088 isl_basic_map_free(bmap);
4089 return NULL((void*)0);
4090}
4091
4092__isl_give isl_basic_setisl_basic_map *isl_basic_set_move_dims(__isl_take isl_basic_setisl_basic_map *bset,
4093 enum isl_dim_type dst_type, unsigned dst_pos,
4094 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4095{
4096 isl_basic_map *bmap = bset_to_bmap(bset);
4097 bmap = isl_basic_map_move_dims(bmap, dst_type, dst_pos,
4098 src_type, src_pos, n);
4099 return bset_from_bmap(bmap);
4100}
4101
4102__isl_give isl_setisl_map *isl_set_move_dims(__isl_take isl_setisl_map *set,
4103 enum isl_dim_type dst_type, unsigned dst_pos,
4104 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4105{
4106 if (!set)
4107 return NULL((void*)0);
4108 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", "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 4108); goto error; } while (0); } while (0)
;
4109 return set_from_map(isl_map_move_dims(set_to_map(set),
4110 dst_type, dst_pos, src_type, src_pos, n));
4111error:
4112 isl_set_free(set);
4113 return NULL((void*)0);
4114}
4115
4116__isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
4117 enum isl_dim_type dst_type, unsigned dst_pos,
4118 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4119{
4120 int i;
4121
4122 if (!map)
4123 return NULL((void*)0);
4124 if (n == 0) {
4125 map = isl_map_reset(map, src_type);
4126 map = isl_map_reset(map, dst_type);
4127 return map;
4128 }
4129
4130 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", "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 4131); goto error; } while (0); } while (0)
4131 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", "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 4131); goto error; } while (0); } while (0)
;
4132
4133 if (dst_type == src_type && dst_pos == src_pos)
4134 return map;
4135
4136 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", "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 4136); goto error; } while (0); } while (0)
;
4137
4138 map = isl_map_cow(map);
4139 if (!map)
4140 return NULL((void*)0);
4141
4142 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
4143 if (!map->dim)
4144 goto error;
4145
4146 for (i = 0; i < map->n; ++i) {
4147 map->p[i] = isl_basic_map_move_dims(map->p[i],
4148 dst_type, dst_pos,
4149 src_type, src_pos, n);
4150 if (!map->p[i])
4151 goto error;
4152 }
4153
4154 return map;
4155error:
4156 isl_map_free(map);
4157 return NULL((void*)0);
4158}
4159
4160/* Move the specified dimensions to the last columns right before
4161 * the divs. Don't change the dimension specification of bmap.
4162 * That's the responsibility of the caller.
4163 */
4164static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
4165 enum isl_dim_type type, unsigned first, unsigned n)
4166{
4167 struct isl_dim_map *dim_map;
4168 struct isl_basic_map *res;
4169 enum isl_dim_type t;
4170 unsigned total, off;
4171
4172 if (!bmap)
4173 return NULL((void*)0);
4174 if (pos(bmap->dim, type) + first + n ==
4175 1 + isl_space_dim(bmap->dim, isl_dim_all))
4176 return bmap;
4177
4178 total = isl_basic_map_total_dim(bmap);
4179 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4180
4181 off = 0;
4182 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
4183 unsigned size = isl_space_dim(bmap->dim, t);
4184 if (t == type) {
4185 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4186 0, first, off);
4187 off += first;
4188 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4189 first, n, total - bmap->n_div - n);
4190 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4191 first + n, size - (first + n), off);
4192 off += size - (first + n);
4193 } else {
4194 isl_dim_map_dim(dim_map, bmap->dim, t, off);
4195 off += size;
4196 }
4197 }
4198 isl_dim_map_div(dim_map, bmap, off + n);
4199
4200 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
4201 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4202 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4203 return res;
4204}
4205
4206/* Insert "n" rows in the divs of "bmap".
4207 *
4208 * The number of columns is not changed, which means that the last
4209 * dimensions of "bmap" are being reintepreted as the new divs.
4210 * The space of "bmap" is not adjusted, however, which means
4211 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
4212 * from the space of "bmap" is the responsibility of the caller.
4213 */
4214static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
4215 int n)
4216{
4217 int i;
4218 size_t row_size;
4219 isl_int **new_div;
4220 isl_int *old;
4221
4222 bmap = isl_basic_map_cow(bmap);
4223 if (!bmap)
4224 return NULL((void*)0);
4225
4226 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
4227 old = bmap->block2.data;
4228 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
4229 (bmap->extra + n) * (1 + row_size));
4230 if (!bmap->block2.data)
4231 return isl_basic_map_free(bmap);
4232 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 *)))
;
4233 if (!new_div)
4234 return isl_basic_map_free(bmap);
4235 for (i = 0; i < n; ++i) {
4236 new_div[i] = bmap->block2.data +
4237 (bmap->extra + i) * (1 + row_size);
4238 isl_seq_clr(new_div[i], 1 + row_size);
4239 }
4240 for (i = 0; i < bmap->extra; ++i)
4241 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
4242 free(bmap->div);
4243 bmap->div = new_div;
4244 bmap->n_div += n;
4245 bmap->extra += n;
4246
4247 return bmap;
4248}
4249
4250/* Drop constraints from "bmap" that only involve the variables
4251 * of "type" in the range [first, first + n] that are not related
4252 * to any of the variables outside that interval.
4253 * These constraints cannot influence the values for the variables
4254 * outside the interval, except in case they cause "bmap" to be empty.
4255 * Only drop the constraints if "bmap" is known to be non-empty.
4256 */
4257static __isl_give isl_basic_map *drop_irrelevant_constraints(
4258 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
4259 unsigned first, unsigned n)
4260{
4261 int i;
4262 int *groups;
4263 unsigned dim, n_div;
4264 isl_bool non_empty;
4265
4266 non_empty = isl_basic_map_plain_is_non_empty(bmap);
4267 if (non_empty < 0)
4268 return isl_basic_map_free(bmap);
4269 if (!non_empty)
4270 return bmap;
4271
4272 dim = isl_basic_map_dim(bmap, isl_dim_all);
4273 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4274 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)))
;
4275 if (!groups)
4276 return isl_basic_map_free(bmap);
4277 first += isl_basic_map_offset(bmap, type) - 1;
4278 for (i = 0; i < first; ++i)
4279 groups[i] = -1;
4280 for (i = first + n; i < dim - n_div; ++i)
4281 groups[i] = -1;
4282
4283 bmap = isl_basic_map_drop_unrelated_constraints(bmap, groups);
4284
4285 return bmap;
4286}
4287
4288/* Turn the n dimensions of type type, starting at first
4289 * into existentially quantified variables.
4290 *
4291 * If a subset of the projected out variables are unrelated
4292 * to any of the variables that remain, then the constraints
4293 * involving this subset are simply dropped first.
4294 */
4295__isl_give isl_basic_map *isl_basic_map_project_out(
4296 __isl_take isl_basic_map *bmap,
4297 enum isl_dim_type type, unsigned first, unsigned n)
4298{
4299 isl_bool empty;
4300
4301 if (n == 0)
4302 return basic_map_space_reset(bmap, type);
4303 if (type == isl_dim_div)
4304 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", "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 4306); return isl_basic_map_free(bmap); } while (0)
4305 "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", "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 4306); return isl_basic_map_free(bmap); } while (0)
4306 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", "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 4306); return isl_basic_map_free(bmap); } while (0)
;
4307
4308 empty = isl_basic_map_plain_is_empty(bmap);
4309 if (empty < 0)
4310 return isl_basic_map_free(bmap);
4311 if (empty)
4312 bmap = isl_basic_map_set_to_empty(bmap);
4313
4314 bmap = drop_irrelevant_constraints(bmap, type, first, n);
4315 if (!bmap)
4316 return NULL((void*)0);
4317
4318 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)(!!(((bmap)->flags) & ((1 << 4)))))
4319 return isl_basic_map_remove_dims(bmap, type, first, n);
4320
4321 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
4322 return isl_basic_map_free(bmap);
4323
4324 bmap = move_last(bmap, type, first, n);
4325 bmap = isl_basic_map_cow(bmap);
4326 bmap = insert_div_rows(bmap, n);
4327 if (!bmap)
4328 return NULL((void*)0);
4329
4330 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
4331 if (!bmap->dim)
4332 goto error;
4333 bmap = isl_basic_map_simplify(bmap);
4334 bmap = isl_basic_map_drop_redundant_divs(bmap);
4335 return isl_basic_map_finalize(bmap);
4336error:
4337 isl_basic_map_free(bmap);
4338 return NULL((void*)0);
4339}
4340
4341/* Turn the n dimensions of type type, starting at first
4342 * into existentially quantified variables.
4343 */
4344struct isl_basic_setisl_basic_map *isl_basic_set_project_out(struct isl_basic_setisl_basic_map *bset,
4345 enum isl_dim_type type, unsigned first, unsigned n)
4346{
4347 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset),
4348 type, first, n));
4349}
4350
4351/* Turn the n dimensions of type type, starting at first
4352 * into existentially quantified variables.
4353 */
4354__isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
4355 enum isl_dim_type type, unsigned first, unsigned n)
4356{
4357 int i;
4358
4359 if (!map)
4360 return NULL((void*)0);
4361
4362 if (n == 0)
4363 return map_space_reset(map, type);
4364
4365 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", "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 4365); goto error; } while (0); } while (0)
;
4366
4367 map = isl_map_cow(map);
4368 if (!map)
4369 return NULL((void*)0);
4370
4371 map->dim = isl_space_drop_dims(map->dim, type, first, n);
4372 if (!map->dim)
4373 goto error;
4374
4375 for (i = 0; i < map->n; ++i) {
4376 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
4377 if (!map->p[i])
4378 goto error;
4379 }
4380
4381 return map;
4382error:
4383 isl_map_free(map);
4384 return NULL((void*)0);
4385}
4386
4387/* Turn the n dimensions of type type, starting at first
4388 * into existentially quantified variables.
4389 */
4390__isl_give isl_setisl_map *isl_set_project_out(__isl_take isl_setisl_map *set,
4391 enum isl_dim_type type, unsigned first, unsigned n)
4392{
4393 return set_from_map(isl_map_project_out(set_to_map(set),
4394 type, first, n));
4395}
4396
4397/* Return a map that projects the elements in "set" onto their
4398 * "n" set dimensions starting at "first".
4399 * "type" should be equal to isl_dim_set.
4400 */
4401__isl_give isl_map *isl_set_project_onto_map(__isl_take isl_setisl_map *set,
4402 enum isl_dim_type type, unsigned first, unsigned n)
4403{
4404 int i;
4405 int dim;
4406 isl_map *map;
4407
4408 if (!set)
4409 return NULL((void*)0);
4410 if (type != isl_dim_set)
4411 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", "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 4412); goto error; } while (0)
4412 "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", "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 4412); goto error; } while (0)
;
4413 dim = isl_set_dim(set, isl_dim_set);
4414 if (first + n > dim || first + n < first)
4415 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", "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 4416); goto error; } while (0)
4416 "index out of bounds", goto error)do { isl_handle_error(isl_set_get_ctx(set), isl_error_invalid
, "index out of bounds", "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 4416); goto error; } while (0)
;
4417
4418 map = isl_map_from_domain(set);
4419 map = isl_map_add_dims(map, isl_dim_out, n);
4420 for (i = 0; i < n; ++i)
4421 map = isl_map_equate(map, isl_dim_in, first + i,
4422 isl_dim_out, i);
4423 return map;
4424error:
4425 isl_set_free(set);
4426 return NULL((void*)0);
4427}
4428
4429static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
4430{
4431 int i, j;
4432
4433 for (i = 0; i < n; ++i) {
4434 j = isl_basic_map_alloc_div(bmap);
4435 if (j < 0)
4436 goto error;
4437 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
4438 }
4439 return bmap;
4440error:
4441 isl_basic_map_free(bmap);
4442 return NULL((void*)0);
4443}
4444
4445struct isl_basic_map *isl_basic_map_apply_range(
4446 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4447{
4448 isl_space *dim_result = NULL((void*)0);
4449 struct isl_basic_map *bmap;
4450 unsigned n_in, n_out, n, nparam, total, pos;
4451 struct isl_dim_map *dim_map1, *dim_map2;
4452
4453 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
4454 goto error;
4455 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_out,
4456 bmap2->dim, isl_dim_in))
4457 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", "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 4458); goto error; } while (0)
4458 "spaces don't match", goto error)do { isl_handle_error(isl_basic_map_get_ctx(bmap1), isl_error_invalid
, "spaces don't match", "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 4458); goto error; } while (0)
;
4459
4460 dim_result = isl_space_join(isl_space_copy(bmap1->dim),
4461 isl_space_copy(bmap2->dim));
4462
4463 n_in = isl_basic_map_dim(bmap1, isl_dim_in);
4464 n_out = isl_basic_map_dim(bmap2, isl_dim_out);
4465 n = isl_basic_map_dim(bmap1, isl_dim_out);
4466 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
4467
4468 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
4469 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4470 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
4471 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4472 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
4473 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4474 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
4475 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
4476 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4477 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4478 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4479
4480 bmap = isl_basic_map_alloc_space(dim_result,
4481 bmap1->n_div + bmap2->n_div + n,
4482 bmap1->n_eq + bmap2->n_eq,
4483 bmap1->n_ineq + bmap2->n_ineq);
4484 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4485 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4486 bmap = add_divs(bmap, n);
4487 bmap = isl_basic_map_simplify(bmap);
4488 bmap = isl_basic_map_drop_redundant_divs(bmap);
4489 return isl_basic_map_finalize(bmap);
4490error:
4491 isl_basic_map_free(bmap1);
4492 isl_basic_map_free(bmap2);
4493 return NULL((void*)0);
4494}
4495
4496struct isl_basic_setisl_basic_map *isl_basic_set_apply(
4497 struct isl_basic_setisl_basic_map *bset, struct isl_basic_map *bmap)
4498{
4499 if (!bset || !bmap)
4500 goto error;
4501
4502 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", "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 4503); goto error; } while (0); } while (0)
4503 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", "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 4503); goto error; } while (0); } while (0)
;
4504
4505 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset),
4506 bmap));
4507error:
4508 isl_basic_set_free(bset);
4509 isl_basic_map_free(bmap);
4510 return NULL((void*)0);
4511}
4512
4513struct isl_basic_map *isl_basic_map_apply_domain(
4514 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4515{
4516 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
4517 goto error;
4518 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_in,
4519 bmap2->dim, isl_dim_in))
4520 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", "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 4521); goto error; } while (0)
4521 "spaces don't match", goto error)do { isl_handle_error(isl_basic_map_get_ctx(bmap1), isl_error_invalid
, "spaces don't match", "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 4521); goto error; } while (0)
;
4522
4523 bmap1 = isl_basic_map_reverse(bmap1);
4524 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
4525 return isl_basic_map_reverse(bmap1);
4526error:
4527 isl_basic_map_free(bmap1);
4528 isl_basic_map_free(bmap2);
4529 return NULL((void*)0);
4530}
4531
4532/* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
4533 * A \cap B -> f(A) + f(B)
4534 */
4535__isl_give isl_basic_map *isl_basic_map_sum(__isl_take isl_basic_map *bmap1,
4536 __isl_take isl_basic_map *bmap2)
4537{
4538 unsigned n_in, n_out, nparam, total, pos;
4539 struct isl_basic_map *bmap = NULL((void*)0);
4540 struct isl_dim_map *dim_map1, *dim_map2;
4541 int i;
4542
4543 if (!bmap1 || !bmap2)
16
Taking false branch
4544 goto error;
4545
4546 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"
, "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 4547); goto error; } while (0); } while (0)
4547 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"
, "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 4547); goto error; } while (0); } while (0)
;
4548
4549 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
4550 n_in = isl_basic_map_dim(bmap1, isl_dim_in);
4551 n_out = isl_basic_map_dim(bmap1, isl_dim_out);
4552
4553 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
4554 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4555 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
4556 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4557 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
4558 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4559 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4560 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
4561 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4562 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4563 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
4564
4565 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
4566 bmap1->n_div + bmap2->n_div + 2 * n_out,
4567 bmap1->n_eq + bmap2->n_eq + n_out,
4568 bmap1->n_ineq + bmap2->n_ineq);
4569 for (i = 0; i < n_out; ++i) {
4570 int j = isl_basic_map_alloc_equality(bmap);
4571 if (j < 0)
4572 goto error;
4573 isl_seq_clr(bmap->eq[j], 1+total);
4574 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);
4575 isl_int_set_si(bmap->eq[j][1+pos+i], 1)isl_sioimath_set_si((bmap->eq[j][1+pos+i]), 1);
4576 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);
4577 }
4578 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4579 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4580 bmap = add_divs(bmap, 2 * n_out);
4581
4582 bmap = isl_basic_map_simplify(bmap);
4583 return isl_basic_map_finalize(bmap);
4584error:
4585 isl_basic_map_free(bmap);
4586 isl_basic_map_free(bmap1);
4587 isl_basic_map_free(bmap2);
17
Calling 'isl_basic_map_free'
22
Returning; memory was released via 1st parameter
4588 return NULL((void*)0);
4589}
4590
4591/* Given two maps A -> f(A) and B -> g(B), construct a map
4592 * A \cap B -> f(A) + f(B)
4593 */
4594__isl_give isl_map *isl_map_sum(__isl_take isl_map *map1,
4595 __isl_take isl_map *map2)
4596{
4597 struct isl_map *result;
4598 int i, j;
4599
4600 if (!map1 || !map2)
2
Assuming 'map1' is non-null
3
Assuming 'map2' is non-null
4
Taking false branch
4601 goto error;
4602
4603 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"
, "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 4603); goto error; } while (0); } while (0)
;
4604
4605 result = isl_map_alloc_space(isl_space_copy(map1->dim),
4606 map1->n * map2->n, 0);
4607 if (!result)
5
Taking false branch
4608 goto error;
4609 for (i = 0; i < map1->n; ++i)
6
Assuming the condition is true
7
Loop condition is true. Entering loop body
30
Assuming the condition is true
31
Loop condition is true. Entering loop body
4610 for (j = 0; j < map2->n; ++j) {
8
Assuming the condition is true
9
Loop condition is true. Entering loop body
28
Assuming the condition is false
29
Loop condition is false. Execution continues on line 4609
32
Loop condition is true. Entering loop body
4611 struct isl_basic_map *part;
4612 part = isl_basic_map_sum(
15
Calling 'isl_basic_map_sum'
23
Returning; memory was released via 2nd parameter
4613 isl_basic_map_copy(map1->p[i]),
4614 isl_basic_map_copy(map2->p[j]));
10
Calling 'isl_basic_map_copy'
14
Returning from 'isl_basic_map_copy'
33
Use of memory after it is freed
4615 if (isl_basic_map_is_empty(part))
24
Assuming the condition is false
25
Taking false branch
4616 isl_basic_map_free(part);
4617 else
4618 result = isl_map_add_basic_map(result, part);
4619 if (!result)
26
Assuming 'result' is non-null
27
Taking false branch
4620 goto error;
4621 }
4622 isl_map_free(map1);
4623 isl_map_free(map2);
4624 return result;
4625error:
4626 isl_map_free(map1);
4627 isl_map_free(map2);
4628 return NULL((void*)0);
4629}
4630
4631__isl_give isl_setisl_map *isl_set_sum(__isl_take isl_setisl_map *set1,
4632 __isl_take isl_setisl_map *set2)
4633{
4634 return set_from_map(isl_map_sum(set_to_map(set1), set_to_map(set2)));
1
Calling 'isl_map_sum'
4635}
4636
4637/* Given a basic map A -> f(A), construct A -> -f(A).
4638 */
4639__isl_give isl_basic_map *isl_basic_map_neg(__isl_take isl_basic_map *bmap)
4640{
4641 int i, j;
4642 unsigned off, n;
4643
4644 bmap = isl_basic_map_cow(bmap);
4645 if (!bmap)
4646 return NULL((void*)0);
4647
4648 n = isl_basic_map_dim(bmap, isl_dim_out);
4649 off = isl_basic_map_offset(bmap, isl_dim_out);
4650 for (i = 0; i < bmap->n_eq; ++i)
4651 for (j = 0; j < n; ++j)
4652 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]))
;
4653 for (i = 0; i < bmap->n_ineq; ++i)
4654 for (j = 0; j < n; ++j)
4655 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]))
;
4656 for (i = 0; i < bmap->n_div; ++i)
4657 for (j = 0; j < n; ++j)
4658 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]))
;
4659 bmap = isl_basic_map_gauss(bmap, NULL((void*)0));
4660 return isl_basic_map_finalize(bmap);
4661}
4662
4663__isl_give isl_basic_setisl_basic_map *isl_basic_set_neg(__isl_take isl_basic_setisl_basic_map *bset)
4664{
4665 return isl_basic_map_neg(bset);
4666}
4667
4668/* Given a map A -> f(A), construct A -> -f(A).
4669 */
4670__isl_give isl_map *isl_map_neg(__isl_take isl_map *map)
4671{
4672 int i;
4673
4674 map = isl_map_cow(map);
4675 if (!map)
4676 return NULL((void*)0);
4677
4678 for (i = 0; i < map->n; ++i) {
4679 map->p[i] = isl_basic_map_neg(map->p[i]);
4680 if (!map->p[i])
4681 goto error;
4682 }
4683
4684 return map;
4685error:
4686 isl_map_free(map);
4687 return NULL((void*)0);
4688}
4689
4690__isl_give isl_setisl_map *isl_set_neg(__isl_take isl_setisl_map *set)
4691{
4692 return set_from_map(isl_map_neg(set_to_map(set)));
4693}
4694
4695/* Given a basic map A -> f(A) and an integer d, construct a basic map
4696 * A -> floor(f(A)/d).
4697 */
4698__isl_give isl_basic_map *isl_basic_map_floordiv(__isl_take isl_basic_map *bmap,
4699 isl_int d)
4700{
4701 unsigned n_in, n_out, nparam, total, pos;
4702 struct isl_basic_map *result = NULL((void*)0);
4703 struct isl_dim_map *dim_map;
4704 int i;
4705
4706 if (!bmap)
4707 return NULL((void*)0);
4708
4709 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4710 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4711 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4712
4713 total = nparam + n_in + n_out + bmap->n_div + n_out;
4714 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4715 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
4716 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
4717 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
4718 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
4719
4720 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
4721 bmap->n_div + n_out,
4722 bmap->n_eq, bmap->n_ineq + 2 * n_out);
4723 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
4724 result = add_divs(result, n_out);
4725 for (i = 0; i < n_out; ++i) {
4726 int j;
4727 j = isl_basic_map_alloc_inequality(result);
4728 if (j < 0)
4729 goto error;
4730 isl_seq_clr(result->ineq[j], 1+total);
4731 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d)isl_sioimath_neg((result->ineq[j][1+nparam+n_in+i]), *(d));
4732 isl_int_set_si(result->ineq[j][1+pos+i], 1)isl_sioimath_set_si((result->ineq[j][1+pos+i]), 1);
4733 j = isl_basic_map_alloc_inequality(result);
4734 if (j < 0)
4735 goto error;
4736 isl_seq_clr(result->ineq[j], 1+total);
4737 isl_int_set(result->ineq[j][1+nparam+n_in+i], d)isl_sioimath_set((result->ineq[j][1+nparam+n_in+i]), *(d));
4738 isl_int_set_si(result->ineq[j][1+pos+i], -1)isl_sioimath_set_si((result->ineq[j][1+pos+i]), -1);
4739 isl_int_sub_ui(result->ineq[j][0], d, 1)isl_sioimath_sub_ui((result->ineq[j][0]), *(d), 1);
4740 }
4741
4742 result = isl_basic_map_simplify(result);
4743 return isl_basic_map_finalize(result);
4744error:
4745 isl_basic_map_free(result);
4746 return NULL((void*)0);
4747}
4748
4749/* Given a map A -> f(A) and an integer d, construct a map
4750 * A -> floor(f(A)/d).
4751 */
4752__isl_give isl_map *isl_map_floordiv(__isl_take isl_map *map, isl_int d)
4753{
4754 int i;
4755
4756 map = isl_map_cow(map);
4757 if (!map)
4758 return NULL((void*)0);
4759
4760 ISL_F_CLR(map, ISL_MAP_DISJOINT)(((map)->flags) &= ~((1 << 0)));
4761 ISL_F_CLR(map, ISL_MAP_NORMALIZED)(((map)->flags) &= ~((1 << 1)));
4762 for (i = 0; i < map->n; ++i) {
4763 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
4764 if (!map->p[i])
4765 goto error;
4766 }
4767
4768 return map;
4769error:
4770 isl_map_free(map);
4771 return NULL((void*)0);
4772}
4773
4774/* Given a map A -> f(A) and an integer d, construct a map
4775 * A -> floor(f(A)/d).
4776 */
4777__isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
4778 __isl_take isl_val *d)
4779{
4780 if (!map || !d)
4781 goto error;
4782 if (!isl_val_is_int(d))
4783 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"
, "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 4784); goto error; } while (0)
4784 "expecting integer denominator", goto error)do { isl_handle_error(isl_val_get_ctx(d), isl_error_invalid, "expecting integer denominator"
, "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 4784); goto error; } while (0)
;
4785 map = isl_map_floordiv(map, d->n);
4786 isl_val_free(d);
4787 return map;
4788error:
4789 isl_map_free(map);
4790 isl_val_free(d);
4791 return NULL((void*)0);
4792}
4793
4794static __isl_give isl_basic_map *var_equal(__isl_take isl_basic_map *bmap,
4795 unsigned pos)
4796{
4797 int i;
4798 unsigned nparam;
4799 unsigned n_in;
4800
4801 i = isl_basic_map_alloc_equality(bmap);
4802 if (i < 0)
4803 goto error;
4804 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4805 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4806 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
4807 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1)isl_sioimath_set_si((bmap->eq[i][1+nparam+pos]), -1);
4808 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);
4809 return isl_basic_map_finalize(bmap);
4810error:
4811 isl_basic_map_free(bmap);
4812 return NULL((void*)0);
4813}
4814
4815/* Add a constraint to "bmap" expressing i_pos < o_pos
4816 */
4817static __isl_give isl_basic_map *var_less(__isl_take isl_basic_map *bmap,
4818 unsigned pos)
4819{
4820 int i;
4821 unsigned nparam;
4822 unsigned n_in;
4823
4824 i = isl_basic_map_alloc_inequality(bmap);
4825 if (i < 0)
4826 goto error;
4827 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4828 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4829 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4830 isl_int_set_si(bmap->ineq[i][0], -1)isl_sioimath_set_si((bmap->ineq[i][0]), -1);
4831 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1)isl_sioimath_set_si((bmap->ineq[i][1+nparam+pos]), -1);
4832 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);
4833 return isl_basic_map_finalize(bmap);
4834error:
4835 isl_basic_map_free(bmap);
4836 return NULL((void*)0);
4837}
4838
4839/* Add a constraint to "bmap" expressing i_pos <= o_pos
4840 */
4841static __isl_give isl_basic_map *var_less_or_equal(
4842 __isl_take isl_basic_map *bmap, unsigned pos)
4843{
4844 int i;
4845 unsigned nparam;
4846 unsigned n_in;
4847
4848 i = isl_basic_map_alloc_inequality(bmap);
4849 if (i < 0)
4850 goto error;
4851 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4852 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4853 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4854 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1)isl_sioimath_set_si((bmap->ineq[i][1+nparam+pos]), -1);
4855 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);
4856 return isl_basic_map_finalize(bmap);
4857error:
4858 isl_basic_map_free(bmap);
4859 return NULL((void*)0);
4860}
4861
4862/* Add a constraint to "bmap" expressing i_pos > o_pos
4863 */
4864static __isl_give isl_basic_map *var_more(__isl_take isl_basic_map *bmap,
4865 unsigned pos)
4866{
4867 int i;
4868 unsigned nparam;
4869 unsigned n_in;
4870
4871 i = isl_basic_map_alloc_inequality(bmap);
4872 if (i < 0)
4873 goto error;
4874 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4875 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4876 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4877 isl_int_set_si(bmap->ineq[i][0], -1)isl_sioimath_set_si((bmap->ineq[i][0]), -1);
4878 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1)isl_sioimath_set_si((bmap->ineq[i][1+nparam+pos]), 1);
4879 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
)
;
4880 return isl_basic_map_finalize(bmap);
4881error:
4882 isl_basic_map_free(bmap);
4883 return NULL((void*)0);
4884}
4885
4886/* Add a constraint to "bmap" expressing i_pos >= o_pos
4887 */
4888static __isl_give isl_basic_map *var_more_or_equal(
4889 __isl_take isl_basic_map *bmap, unsigned pos)
4890{
4891 int i;
4892 unsigned nparam;
4893 unsigned n_in;
4894
4895 i = isl_basic_map_alloc_inequality(bmap);
4896 if (i < 0)
4897 goto error;
4898 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4899 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4900 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4901 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1)isl_sioimath_set_si((bmap->ineq[i][1+nparam+pos]), 1);
4902 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
)
;
4903 return isl_basic_map_finalize(bmap);
4904error:
4905 isl_basic_map_free(bmap);
4906 return NULL((void*)0);
4907}
4908
4909__isl_give isl_basic_map *isl_basic_map_equal(
4910 __isl_take isl_space *dim, unsigned n_equal)
4911{
4912 int i;
4913 struct isl_basic_map *bmap;
4914 bmap = isl_basic_map_alloc_space(dim, 0, n_equal, 0);
4915 if (!bmap)
4916 return NULL((void*)0);
4917 for (i = 0; i < n_equal && bmap; ++i)
4918 bmap = var_equal(bmap, i);
4919 return isl_basic_map_finalize(bmap);
4920}
4921
4922/* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4923 */
4924__isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *dim,
4925 unsigned pos)
4926{
4927 int i;
4928 struct isl_basic_map *bmap;
4929 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4930 if (!bmap)
4931 return NULL((void*)0);
4932 for (i = 0; i < pos && bmap; ++i)
4933 bmap = var_equal(bmap, i);
4934 if (bmap)
4935 bmap = var_less(bmap, pos);
4936 return isl_basic_map_finalize(bmap);
4937}
4938
4939/* Return a relation on "dim" expressing i_[0..pos] <<= o_[0..pos]
4940 */
4941__isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
4942 __isl_take isl_space *dim, unsigned pos)
4943{
4944 int i;
4945 isl_basic_map *bmap;
4946
4947 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4948 for (i = 0; i < pos; ++i)
4949 bmap = var_equal(bmap, i);
4950 bmap = var_less_or_equal(bmap, pos);
4951 return isl_basic_map_finalize(bmap);
4952}
4953
4954/* Return a relation on "dim" expressing i_pos > o_pos
4955 */
4956__isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *dim,
4957 unsigned pos)
4958{
4959 int i;
4960 struct isl_basic_map *bmap;
4961 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4962 if (!bmap)
4963 return NULL((void*)0);
4964 for (i = 0; i < pos && bmap; ++i)
4965 bmap = var_equal(bmap, i);
4966 if (bmap)
4967 bmap = var_more(bmap, pos);
4968 return isl_basic_map_finalize(bmap);
4969}
4970
4971/* Return a relation on "dim" expressing i_[0..pos] >>= o_[0..pos]
4972 */
4973__isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
4974 __isl_take isl_space *dim, unsigned pos)
4975{
4976 int i;
4977 isl_basic_map *bmap;
4978
4979 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4980 for (i = 0; i < pos; ++i)
4981 bmap = var_equal(bmap, i);
4982 bmap = var_more_or_equal(bmap, pos);
4983 return isl_basic_map_finalize(bmap);
4984}
4985
4986static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *dims,
4987 unsigned n, int equal)
4988{
4989 struct isl_map *map;
4990 int i;
4991
4992 if (n == 0 && equal)
4993 return isl_map_universe(dims);
4994
4995 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT(1 << 0));
4996
4997 for (i = 0; i + 1 < n; ++i)
4998 map = isl_map_add_basic_map(map,
4999 isl_basic_map_less_at(isl_space_copy(dims), i));
5000 if (n > 0) {
5001 if (equal)
5002 map = isl_map_add_basic_map(map,
5003 isl_basic_map_less_or_equal_at(dims, n - 1));
5004 else
5005 map = isl_map_add_basic_map(map,
5006 isl_basic_map_less_at(dims, n - 1));
5007 } else
5008 isl_space_free(dims);
5009
5010 return map;
5011}
5012
5013static __isl_give isl_map *map_lex_lte(__isl_take isl_space *dims, int equal)
5014{
5015 if (!dims)
5016 return NULL((void*)0);
5017 return map_lex_lte_first(dims, dims->n_out, equal);
5018}
5019
5020__isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
5021{
5022 return map_lex_lte_first(dim, n, 0);
5023}
5024
5025__isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
5026{
5027 return map_lex_lte_first(dim, n, 1);
5028}
5029
5030__isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
5031{
5032 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
5033}
5034
5035__isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
5036{
5037 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
5038}
5039
5040static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *dims,
5041 unsigned n, int equal)
5042{
5043 struct isl_map *map;
5044 int i;
5045
5046 if (n == 0 && equal)
5047 return isl_map_universe(dims);
5048
5049 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT(1 << 0));
5050
5051 for (i = 0; i + 1 < n; ++i)
5052 map = isl_map_add_basic_map(map,
5053 isl_basic_map_more_at(isl_space_copy(dims), i));
5054 if (n > 0) {
5055 if (equal)
5056 map = isl_map_add_basic_map(map,
5057 isl_basic_map_more_or_equal_at(dims, n - 1));
5058 else
5059 map = isl_map_add_basic_map(map,
5060 isl_basic_map_more_at(dims, n - 1));
5061 } else
5062 isl_space_free(dims);
5063
5064 return map;
5065}
5066
5067static __isl_give isl_map *map_lex_gte(__isl_take isl_space *dims, int equal)
5068{
5069 if (!dims)
5070 return NULL((void*)0);
5071 return map_lex_gte_first(dims, dims->n_out, equal);
5072}
5073
5074__isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
5075{
5076 return map_lex_gte_first(dim, n, 0);
5077}
5078
5079__isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
5080{
5081 return map_lex_gte_first(dim, n, 1);
5082}
5083
5084__isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
5085{
5086 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
5087}
5088
5089__isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
5090{
5091 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
5092}
5093
5094__isl_give isl_map *isl_set_lex_le_set(__isl_take isl_setisl_map *set1,
5095 __isl_take isl_setisl_map *set2)
5096{
5097 isl_map *map;
5098 map = isl_map_lex_le(isl_set_get_space(set1));
5099 map = isl_map_intersect_domain(map, set1);
5100 map = isl_map_intersect_range(map, set2);
5101 return map;
5102}
5103
5104__isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_setisl_map *set1,
5105 __isl_take isl_setisl_map *set2)
5106{
5107 isl_map *map;
5108 map = isl_map_lex_lt(isl_set_get_space(set1));
5109 map = isl_map_intersect_domain(map, set1);
5110 map = isl_map_intersect_range(map, set2);
5111 return map;
5112}
5113
5114__isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_setisl_map *set1,
5115 __isl_take isl_setisl_map *set2)
5116{
5117 isl_map *map;
5118 map = isl_map_lex_ge(isl_set_get_space(set1));
5119 map = isl_map_intersect_domain(map, set1);
5120 map = isl_map_intersect_range(map, set2);
5121 return map;
5122}
5123
5124__isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_setisl_map *set1,
5125 __isl_take isl_setisl_map *set2)
5126{
5127 isl_map *map;
5128 map = isl_map_lex_gt(isl_set_get_space(set1));
5129 map = isl_map_intersect_domain(map, set1);
5130 map = isl_map_intersect_range(map, set2);
5131 return map;
5132}
5133
5134__isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
5135 __isl_take isl_map *map2)
5136{
5137 isl_map *map;
5138 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
5139 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5140 map = isl_map_apply_range(map, isl_map_reverse(map2));
5141 return map;
5142}
5143
5144__isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
5145 __isl_take isl_map *map2)
5146{
5147 isl_map *map;
5148 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
5149 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5150 map = isl_map_apply_range(map, isl_map_reverse(map2));
5151 return map;
5152}
5153
5154__isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
5155 __isl_take isl_map *map2)
5156{
5157 isl_map *map;
5158 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
5159 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5160 map = isl_map_apply_range(map, isl_map_reverse(map2));
5161 return map;
5162}
5163
5164__isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
5165 __isl_take isl_map *map2)
5166{
5167 isl_map *map;
5168 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
5169 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5170 map = isl_map_apply_range(map, isl_map_reverse(map2));
5171 return map;
5172}
5173
5174/* For a div d = floor(f/m), add the constraint
5175 *
5176 * f - m d >= 0
5177 */
5178static isl_stat add_upper_div_constraint(__isl_keep isl_basic_map *bmap,
5179 unsigned pos, isl_int *div)
5180{
5181 int i;
5182 unsigned total = isl_basic_map_total_dim(bmap);
5183
5184 i = isl_basic_map_alloc_inequality(bmap);
5185 if (i < 0)
5186 return isl_stat_error;
5187 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
5188 isl_int_neg(bmap->ineq[i][1 + pos], div[0])isl_sioimath_neg((bmap->ineq[i][1 + pos]), *(div[0]));
5189
5190 return isl_stat_ok;
5191}
5192
5193/* For a div d = floor(f/m), add the constraint
5194 *
5195 * -(f-(m-1)) + m d >= 0
5196 */
5197static isl_stat add_lower_div_constraint(__isl_keep isl_basic_map *bmap,
5198 unsigned pos, isl_int *div)
5199{
5200 int i;
5201 unsigned total = isl_basic_map_total_dim(bmap);
5202
5203 i = isl_basic_map_alloc_inequality(bmap);
5204 if (i < 0)
5205 return isl_stat_error;
5206 isl_seq_neg(bmap->ineq[i], div + 1, 1 + total);
5207 isl_int_set(bmap->ineq[i][1 + pos], div[0])isl_sioimath_set((bmap->ineq[i][1 + pos]), *(div[0]));
5208 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]))
;
5209 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)
;
5210
5211 return isl_stat_ok;
5212}
5213
5214/* For a div d = floor(f/m), add the constraints
5215 *
5216 * f - m d >= 0
5217 * -(f-(m-1)) + m d >= 0
5218 *
5219 * Note that the second constraint is the negation of
5220 *
5221 * f - m d >= m
5222 */
5223int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
5224 unsigned pos, isl_int *div)
5225{
5226 if (add_upper_div_constraint(bmap, pos, div) < 0)
5227 return -1;
5228 if (add_lower_div_constraint(bmap, pos, div) < 0)
5229 return -1;
5230 return 0;
5231}
5232
5233int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_setisl_basic_map *bset,
5234 unsigned pos, isl_int *div)
5235{
5236 return isl_basic_map_add_div_constraints_var(bset_to_bmap(bset),
5237 pos, div);
5238}
5239
5240int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
5241{
5242 unsigned total = isl_basic_map_total_dim(bmap);
5243 unsigned div_pos = total - bmap->n_div + div;
5244
5245 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
5246 bmap->div[div]);
5247}
5248
5249/* For each known div d = floor(f/m), add the constraints
5250 *
5251 * f - m d >= 0
5252 * -(f-(m-1)) + m d >= 0
5253 *
5254 * Remove duplicate constraints in case of some these div constraints
5255 * already appear in "bmap".
5256 */
5257__isl_give isl_basic_map *isl_basic_map_add_known_div_constraints(
5258 __isl_take isl_basic_map *bmap)
5259{
5260 unsigned n_div;
5261
5262 if (!bmap)
5263 return NULL((void*)0);
5264 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5265 if (n_div == 0)
5266 return bmap;
5267
5268 bmap = add_known_div_constraints(bmap);
5269 bmap = isl_basic_map_remove_duplicate_constraints(bmap, NULL((void*)0), 0);
5270 bmap = isl_basic_map_finalize(bmap);
5271 return bmap;
5272}
5273
5274/* Add the div constraint of sign "sign" for div "div" of "bmap".
5275 *
5276 * In particular, if this div is of the form d = floor(f/m),
5277 * then add the constraint
5278 *
5279 * f - m d >= 0
5280 *
5281 * if sign < 0 or the constraint
5282 *
5283 * -(f-(m-1)) + m d >= 0
5284 *
5285 * if sign > 0.
5286 */
5287int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map *bmap,
5288 unsigned div, int sign)
5289{
5290 unsigned total;
5291 unsigned div_pos;
5292
5293 if (!bmap)
5294 return -1;
5295
5296 total = isl_basic_map_total_dim(bmap);
5297 div_pos = total - bmap->n_div + div;
5298
5299 if (sign < 0)
5300 return add_upper_div_constraint(bmap, div_pos, bmap->div[div]);
5301 else
5302 return add_lower_div_constraint(bmap, div_pos, bmap->div[div]);
5303}
5304
5305__isl_give isl_basic_setisl_basic_map *isl_basic_map_underlying_set(
5306 __isl_take isl_basic_map *bmap)
5307{
5308 if (!bmap)
5309 goto error;
5310 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
5311 bmap->n_div == 0 &&
5312 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
5313 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
5314 return bset_from_bmap(bmap);
5315 bmap = isl_basic_map_cow(bmap);
5316 if (!bmap)
5317 goto error;
5318 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
5319 if (!bmap->dim)
5320 goto error;
5321 bmap->extra -= bmap->n_div;
5322 bmap->n_div = 0;
5323 bmap = isl_basic_map_finalize(bmap);
5324 return bset_from_bmap(bmap);
5325error:
5326 isl_basic_map_free(bmap);
5327 return NULL((void*)0);
5328}
5329
5330__isl_give isl_basic_setisl_basic_map *isl_basic_set_underlying_set(
5331 __isl_take isl_basic_setisl_basic_map *bset)
5332{
5333 return isl_basic_map_underlying_set(bset_to_bmap(bset));
5334}
5335
5336/* Replace each element in "list" by the result of applying
5337 * isl_basic_map_underlying_set to the element.
5338 */
5339__isl_give isl_basic_set_listisl_basic_map_list *isl_basic_map_list_underlying_set(
5340 __isl_take isl_basic_map_list *list)
5341{
5342 int i, n;
5343
5344 if (!list)
5345 return NULL((void*)0);
5346
5347 n = isl_basic_map_list_n_basic_map(list);
5348 for (i = 0; i < n; ++i) {
5349 isl_basic_map *bmap;
5350 isl_basic_setisl_basic_map *bset;
5351
5352 bmap = isl_basic_map_list_get_basic_map(list, i);
5353 bset = isl_basic_set_underlying_set(bmap);
5354 list = isl_basic_set_list_set_basic_set(list, i, bset);
5355 }
5356
5357 return list;
5358}
5359
5360__isl_give isl_basic_map *isl_basic_map_overlying_set(
5361 __isl_take isl_basic_setisl_basic_map *bset, __isl_take isl_basic_map *like)
5362{
5363 struct isl_basic_map *bmap;
5364 struct isl_ctx *ctx;
5365 unsigned total;
5366 int i;
5367
5368 if (!bset || !like)
5369 goto error;
5370 ctx = bset->ctx;
5371 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"
, "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 5371); goto error; } while (0); } while (0)
;
5372 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", "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 5372); goto error; } while (0); } while (0)
;
5373 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"
, "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 5374); goto error; } while (0); } while (0)
5374 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"
, "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 5374); goto error; } while (0); } while (0)
;
5375 if (like->n_div == 0) {
5376 isl_space *space = isl_basic_map_get_space(like);
5377 isl_basic_map_free(like);
5378 return isl_basic_map_reset_space(bset, space);
5379 }
5380 bset = isl_basic_set_cow(bset);
5381 if (!bset)
5382 goto error;
5383 total = bset->dim->n_out + bset->extra;
5384 bmap = bset_to_bmap(bset);
5385 isl_space_free(bmap->dim);
5386 bmap->dim = isl_space_copy(like->dim);
5387 if (!bmap->dim)
5388 goto error;
5389 bmap->n_div = like->n_div;
5390 bmap->extra += like->n_div;
5391 if (bmap->extra) {
5392 unsigned ltotal;
5393 isl_int **div;
5394 ltotal = total - bmap->extra + like->extra;
5395 if (ltotal > total)
5396 ltotal = total;
5397 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
5398 bmap->extra * (1 + 1 + total));
5399 if (isl_blk_is_error(bmap->block2))
5400 goto error;
5401 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 *)))
;
5402 if (!div)
5403 goto error;
5404 bmap->div = div;
5405 for (i = 0; i < bmap->extra; ++i)
5406 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
5407 for (i = 0; i < like->n_div; ++i) {
5408 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
5409 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
5410 }
5411 bmap = isl_basic_map_add_known_div_constraints(bmap);
5412 }
5413 isl_basic_map_free(like);
5414 bmap = isl_basic_map_simplify(bmap);
5415 bmap = isl_basic_map_finalize(bmap);
5416 return bmap;
5417error:
5418 isl_basic_map_free(like);
5419 isl_basic_set_free(bset);
5420 return NULL((void*)0);
5421}
5422
5423struct isl_basic_setisl_basic_map *isl_basic_set_from_underlying_set(
5424 struct isl_basic_setisl_basic_map *bset, struct isl_basic_setisl_basic_map *like)
5425{
5426 return bset_from_bmap(isl_basic_map_overlying_set(bset,
5427 bset_to_bmap(like)));
5428}
5429
5430__isl_give isl_setisl_map *isl_map_underlying_set(__isl_take isl_map *map)
5431{
5432 int i;
5433
5434 map = isl_map_cow(map);
5435 if (!map)
5436 return NULL((void*)0);
5437 map->dim = isl_space_cow(map->dim);
5438 if (!map->dim)
5439 goto error;
5440
5441 for (i = 1; i < map->n; ++i)
5442 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"
, "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 5443); goto error; } while (0); } while (0)
5443 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"
, "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 5443); goto error; } while (0); } while (0)
;
5444 for (i = 0; i < map->n; ++i) {
5445 map->p[i] = bset_to_bmap(
5446 isl_basic_map_underlying_set(map->p[i]));
5447 if (!map->p[i])
5448 goto error;
5449 }
5450 if (map->n == 0)
5451 map->dim = isl_space_underlying(map->dim, 0);
5452 else {
5453 isl_space_free(map->dim);
5454 map->dim = isl_space_copy(map->p[0]->dim);
5455 }
5456 if (!map->dim)
5457 goto error;
5458 return set_from_map(map);
5459error:
5460 isl_map_free(map);
5461 return NULL((void*)0);
5462}
5463
5464/* Replace the space of "bmap" by "space".
5465 *
5466 * If the space of "bmap" is identical to "space" (including the identifiers
5467 * of the input and output dimensions), then simply return the original input.
5468 */
5469__isl_give isl_basic_map *isl_basic_map_reset_space(
5470 __isl_take isl_basic_map *bmap, __isl_take isl_space *space)
5471{
5472 isl_bool equal;
5473 isl_space *bmap_space;
5474
5475 bmap_space = isl_basic_map_peek_space(bmap);
5476 equal = isl_space_is_equal(bmap_space, space);
5477 if (equal >= 0 && equal)
5478 equal = isl_space_has_equal_ids(bmap_space, space);
5479 if (equal < 0)
5480 goto error;
5481 if (equal) {
5482 isl_space_free(space);
5483 return bmap;
5484 }
5485 bmap = isl_basic_map_cow(bmap);
5486 if (!bmap || !space)
5487 goto error;
5488
5489 isl_space_free(bmap->dim);
5490 bmap->dim = space;
5491
5492 bmap = isl_basic_map_finalize(bmap);
5493
5494 return bmap;
5495error:
5496 isl_basic_map_free(bmap);
5497 isl_space_free(space);
5498 return NULL((void*)0);
5499}
5500
5501__isl_give isl_basic_setisl_basic_map *isl_basic_set_reset_space(
5502 __isl_take isl_basic_setisl_basic_map *bset, __isl_take isl_space *dim)
5503{
5504 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset),
5505 dim));
5506}
5507
5508__isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
5509 __isl_take isl_space *dim)
5510{
5511 int i;
5512
5513 map = isl_map_cow(map);
5514 if (!map || !dim)
5515 goto error;
5516
5517 for (i = 0; i < map->n; ++i) {
5518 map->p[i] = isl_basic_map_reset_space(map->p[i],
5519 isl_space_copy(dim));
5520 if (!map->p[i])
5521 goto error;
5522 }
5523 isl_space_free(map->dim);
5524 map->dim = dim;
5525
5526 return map;
5527error:
5528 isl_map_free(map);
5529 isl_space_free(dim);
5530 return NULL((void*)0);
5531}
5532
5533__isl_give isl_setisl_map *isl_set_reset_space(__isl_take isl_setisl_map *set,
5534 __isl_take isl_space *dim)
5535{
5536 return set_from_map(isl_map_reset_space(set_to_map(set), dim));
5537}
5538
5539/* Compute the parameter domain of the given basic set.
5540 */
5541__isl_give isl_basic_setisl_basic_map *isl_basic_set_params(__isl_take isl_basic_setisl_basic_map *bset)
5542{
5543 isl_bool is_params;
5544 isl_space *space;
5545 unsigned n;
5546
5547 is_params = isl_basic_set_is_params(bset);
5548 if (is_params < 0)
5549 return isl_basic_set_free(bset);
5550 if (is_params)
5551 return bset;
5552
5553 n = isl_basic_set_dim(bset, isl_dim_set);
5554 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
5555 space = isl_basic_set_get_space(bset);
5556 space = isl_space_params(space);
5557 bset = isl_basic_set_reset_space(bset, space);
5558 return bset;
5559}
5560
5561/* Construct a zero-dimensional basic set with the given parameter domain.
5562 */
5563__isl_give isl_basic_setisl_basic_map *isl_basic_set_from_params(
5564 __isl_take isl_basic_setisl_basic_map *bset)
5565{
5566 isl_space *space;
5567 space = isl_basic_set_get_space(bset);
5568 space = isl_space_set_from_params(space);
5569 bset = isl_basic_set_reset_space(bset, space);
5570 return bset;
5571}
5572
5573/* Compute the parameter domain of the given set.
5574 */
5575__isl_give isl_setisl_map *isl_set_params(__isl_take isl_setisl_map *set)
5576{
5577 isl_space *space;
5578 unsigned n;
5579
5580 if (isl_set_is_params(set))
5581 return set;
5582
5583 n = isl_set_dim(set, isl_dim_set);
5584 set = isl_set_project_out(set, isl_dim_set, 0, n);
5585 space = isl_set_get_space(set);
5586 space = isl_space_params(space);
5587 set = isl_set_reset_space(set, space);
5588 return set;
5589}
5590
5591/* Construct a zero-dimensional set with the given parameter domain.
5592 */
5593__isl_give isl_setisl_map *isl_set_from_params(__isl_take isl_setisl_map *set)
5594{
5595 isl_space *space;
5596 space = isl_set_get_space(set);
5597 space = isl_space_set_from_params(space);
5598 set = isl_set_reset_space(set, space);
5599 return set;
5600}
5601
5602/* Compute the parameter domain of the given map.
5603 */
5604__isl_give isl_setisl_map *isl_map_params(__isl_take isl_map *map)
5605{
5606 isl_space *space;
5607 unsigned n;
5608
5609 n = isl_map_dim(map, isl_dim_in);
5610 map = isl_map_project_out(map, isl_dim_in, 0, n);
5611 n = isl_map_dim(map, isl_dim_out);
5612 map = isl_map_project_out(map, isl_dim_out, 0, n);
5613 space = isl_map_get_space(map);
5614 space = isl_space_params(space);
5615 map = isl_map_reset_space(map, space);
5616 return map;
5617}
5618
5619struct isl_basic_setisl_basic_map *isl_basic_map_domain(struct isl_basic_map *bmap)
5620{
5621 isl_space *space;
5622 unsigned n_out;
5623
5624 if (!bmap)
5625 return NULL((void*)0);
5626 space = isl_space_domain(isl_basic_map_get_space(bmap));
5627
5628 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5629 bmap = isl_basic_map_project_out(bmap, isl_dim_out, 0, n_out);
5630
5631 return isl_basic_map_reset_space(bmap, space);
5632}
5633
5634isl_bool isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
5635{
5636 if (!bmap)
5637 return isl_bool_error;
5638 return isl_space_may_be_set(bmap->dim);
5639}
5640
5641/* Is this basic map actually a set?
5642 * Users should never call this function. Outside of isl,
5643 * the type should indicate whether something is a set or a map.
5644 */
5645isl_bool isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
5646{
5647 if (!bmap)
5648 return isl_bool_error;
5649 return isl_space_is_set(bmap->dim);
5650}
5651
5652struct isl_basic_setisl_basic_map *isl_basic_map_range(struct isl_basic_map *bmap)
5653{
5654 isl_bool is_set;
5655
5656 is_set = isl_basic_map_is_set(bmap);
5657 if (is_set < 0)
5658 goto error;
5659 if (is_set)
5660 return bmap;
5661 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
5662error:
5663 isl_basic_map_free(bmap);
5664 return NULL((void*)0);
5665}
5666
5667__isl_give isl_basic_map *isl_basic_map_domain_map(
5668 __isl_take isl_basic_map *bmap)
5669{
5670 int i;
5671 isl_space *dim;
5672 isl_basic_map *domain;
5673 int nparam, n_in, n_out;
5674
5675 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5676 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5677 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5678
5679 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
5680 domain = isl_basic_map_universe(dim);
5681
5682 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5683 bmap = isl_basic_map_apply_range(bmap, domain);
5684 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
5685
5686 for (i = 0; i < n_in; ++i)
5687 bmap = isl_basic_map_equate(bmap, isl_dim_in, i,
5688 isl_dim_out, i);
5689
5690 bmap = isl_basic_map_gauss(bmap, NULL((void*)0));
5691 return isl_basic_map_finalize(bmap);
5692}
5693
5694__isl_give isl_basic_map *isl_basic_map_range_map(
5695 __isl_take isl_basic_map *bmap)
5696{
5697 int i;
5698 isl_space *dim;
5699 isl_basic_map *range;
5700 int nparam, n_in, n_out;
5701
5702 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5703 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5704 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5705
5706 dim = isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap)));
5707 range = isl_basic_map_universe(dim);
5708
5709 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5710 bmap = isl_basic_map_apply_range(bmap, range);
5711 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
5712
5713 for (i = 0; i < n_out; ++i)
5714 bmap = isl_basic_map_equate(bmap, isl_dim_in, n_in + i,
5715 isl_dim_out, i);
5716
5717 bmap = isl_basic_map_gauss(bmap, NULL((void*)0));
5718 return isl_basic_map_finalize(bmap);
5719}
5720
5721int isl_map_may_be_set(__isl_keep isl_map *map)
5722{
5723 if (!map)
5724 return -1;
5725 return isl_space_may_be_set(map->dim);
5726}
5727
5728/* Is this map actually a set?
5729 * Users should never call this function. Outside of isl,
5730 * the type should indicate whether something is a set or a map.
5731 */
5732isl_bool isl_map_is_set(__isl_keep isl_map *map)
5733{
5734 if (!map)
5735 return isl_bool_error;
5736 return isl_space_is_set(map->dim);
5737}
5738
5739__isl_give isl_setisl_map *isl_map_range(__isl_take isl_map *map)
5740{
5741 int i;
5742 isl_bool is_set;
5743 struct isl_setisl_map *set;
5744
5745 is_set = isl_map_is_set(map);
5746 if (is_set < 0)
5747 goto error;
5748 if (is_set)
5749 return set_from_map(map);
5750
5751 map = isl_map_cow(map);
5752 if (!map)
5753 goto error;
5754
5755 set = set_from_map(map);
5756 set->dim = isl_space_range(set->dim);
5757 if (!set->dim)
5758 goto error;
5759 for (i = 0; i < map->n; ++i) {
5760 set->p[i] = isl_basic_map_range(map->p[i]);
5761 if (!set->p[i])
5762 goto error;
5763 }
5764 ISL_F_CLR(set, ISL_MAP_DISJOINT)(((set)->flags) &= ~((1 << 0)));
5765 ISL_F_CLR(set, ISL_SET_NORMALIZED)(((set)->flags) &= ~((1 << 1)));
5766 return set;
5767error:
5768 isl_map_free(map);
5769 return NULL((void*)0);
5770}
5771
5772__isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
5773{
5774 int i;
5775
5776 map = isl_map_cow(map);
5777 if (!map)
5778 return NULL((void*)0);
5779
5780 map->dim = isl_space_domain_map(map->dim);
5781 if (!map->dim)
5782 goto error;
5783 for (i = 0; i < map->n; ++i) {
5784 map->p[i] = isl_basic_map_domain_map(map->p[i]);
5785 if (!map->p[i])
5786 goto error;
5787 }
5788 ISL_F_CLR(map, ISL_MAP_DISJOINT)(((map)->flags) &= ~((1 << 0)));
5789 ISL_F_CLR(map, ISL_MAP_NORMALIZED)(((map)->flags) &= ~((1 << 1)));
5790 return map;
5791error:
5792 isl_map_free(map);
5793 return NULL((void*)0);
5794}
5795
5796__isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
5797{
5798 int i;
5799 isl_space *range_dim;
5800
5801 map = isl_map_cow(map);
5802 if (!map)
5803 return NULL((void*)0);
5804
5805 range_dim = isl_space_range(isl_map_get_space(map));
5806 range_dim = isl_space_from_range(range_dim);
5807 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
5808 map->dim = isl_space_join(map->dim, range_dim);
5809 if (!map->dim)
5810 goto error;
5811 for (i = 0; i < map->n; ++i) {
5812 map->p[i] = isl_basic_map_range_map(map->p[i]);
5813 if (!map->p[i])
5814 goto error;
5815 }
5816 ISL_F_CLR(map, ISL_MAP_DISJOINT)(((map)->flags) &= ~((1 << 0)));
5817 ISL_F_CLR(map, ISL_MAP_NORMALIZED)(((map)->flags) &= ~((1 << 1)));
5818 return map;
5819error:
5820 isl_map_free(map);
5821 return NULL((void*)0);
5822}
5823
5824/* Given a wrapped map of the form A[B -> C],
5825 * return the map A[B -> C] -> B.
5826 */
5827__isl_give isl_map *isl_set_wrapped_domain_map(__isl_take isl_setisl_map *set)
5828{
5829 isl_id *id;
5830 isl_map *map;
5831
5832 if (!set)
5833 return NULL((void*)0);
5834 if (!isl_set_has_tuple_id(set))
5835 return isl_map_domain_map(isl_set_unwrap(set));
5836
5837 id = isl_set_get_tuple_id(set);
5838 map = isl_map_domain_map(isl_set_unwrap(set));
5839 map = isl_map_set_tuple_id(map, isl_dim_in, id);
5840
5841 return map;
5842}
5843
5844__isl_give isl_basic_map *isl_basic_map_from_domain(
5845 __isl_take isl_basic_setisl_basic_map *bset)
5846{
5847 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
5848}
5849
5850__isl_give isl_basic_map *isl_basic_map_from_range(
5851 __isl_take isl_basic_setisl_basic_map *bset)
5852{
5853 isl_space *space;
5854 space = isl_basic_set_get_space(bset);
5855 space = isl_space_from_range(space);
5856 bset = isl_basic_set_reset_space(bset, space);
5857 return bset_to_bmap(bset);
5858}
5859
5860/* Create a relation with the given set as range.
5861 * The domain of the created relation is a zero-dimensional
5862 * flat anonymous space.
5863 */
5864__isl_give isl_map *isl_map_from_range(__isl_take isl_setisl_map *set)
5865{
5866 isl_space *space;
5867 space = isl_set_get_space(set);
5868 space = isl_space_from_range(space);
5869 set = isl_set_reset_space(set, space);
5870 return set_to_map(set);
5871}
5872
5873/* Create a relation with the given set as domain.
5874 * The range of the created relation is a zero-dimensional
5875 * flat anonymous space.
5876 */
5877__isl_give isl_map *isl_map_from_domain(__isl_take isl_setisl_map *set)
5878{
5879 return isl_map_reverse(isl_map_from_range(set));
5880}
5881
5882__isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
5883 __isl_take isl_basic_setisl_basic_map *domain, __isl_take isl_basic_setisl_basic_map *range)
5884{
5885 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
5886}
5887
5888__isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_setisl_map *domain,
5889 __isl_take isl_setisl_map *range)
5890{
5891 return isl_map_apply_range(isl_map_reverse(domain), range);
5892}
5893
5894/* Return a newly allocated isl_map with given space and flags and
5895 * room for "n" basic maps.
5896 * Make sure that all cached information is cleared.
5897 */
5898__isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *space, int n,
5899 unsigned flags)
5900{
5901 struct isl_map *map;
5902
5903 if (!space)
5904 return NULL((void*)0);
5905 if (n < 0)
5906 isl_die(space->ctx, isl_error_internal,do { isl_handle_error(space->ctx, isl_error_internal, "negative number of basic maps"
, "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 5907); goto error; } while (0)
5907 "negative number of basic maps", goto error)do { isl_handle_error(space->ctx, isl_error_internal, "negative number of basic maps"
, "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 5907); goto error; } while (0)
;
5908 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 *)))
5909 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 *)))
5910 (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 *)))
;
5911 if (!map)
5912 goto error;
5913
5914 map->ctx = space->ctx;
5915 isl_ctx_ref(map->ctx);
5916 map->ref = 1;
5917 map->size = n;
5918 map->n = 0;
5919 map->dim = space;
5920 map->flags = flags;
5921 return map;
5922error:
5923 isl_space_free(space);
5924 return NULL((void*)0);
5925}
5926
5927__isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *dim)
5928{
5929 struct isl_basic_map *bmap;
5930 bmap = isl_basic_map_alloc_space(dim, 0, 1, 0);
5931 bmap = isl_basic_map_set_to_empty(bmap);
5932 return bmap;
5933}
5934
5935__isl_give isl_basic_setisl_basic_map *isl_basic_set_empty(__isl_take isl_space *dim)
5936{
5937 struct isl_basic_setisl_basic_map *bset;
5938 bset = isl_basic_set_alloc_space(dim, 0, 1, 0);
5939 bset = isl_basic_set_set_to_empty(bset);
5940 return bset;
5941}
5942
5943__isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *dim)
5944{
5945 struct isl_basic_map *bmap;
5946 bmap = isl_basic_map_alloc_space(dim, 0, 0, 0);
5947 bmap = isl_basic_map_finalize(bmap);
5948 return bmap;
5949}
5950
5951__isl_give isl_basic_setisl_basic_map *isl_basic_set_universe(__isl_take isl_space *dim)
5952{
5953 struct isl_basic_setisl_basic_map *bset;
5954 bset = isl_basic_set_alloc_space(dim, 0, 0, 0);
5955 bset = isl_basic_set_finalize(bset);
5956 return bset;
5957}
5958
5959__isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_space *dim)
5960{
5961 int i;
5962 unsigned total = isl_space_dim(dim, isl_dim_all);
5963 isl_basic_map *bmap;
5964
5965 bmap= isl_basic_map_alloc_space(dim, 0, 0, total);
5966 for (i = 0; i < total; ++i) {
5967 int k = isl_basic_map_alloc_inequality(bmap);
5968 if (k < 0)
5969 goto error;
5970 isl_seq_clr(bmap->ineq[k], 1 + total);
5971 isl_int_set_si(bmap->ineq[k][1 + i], 1)isl_sioimath_set_si((bmap->ineq[k][1 + i]), 1);
5972 }
5973 return bmap;
5974error:
5975 isl_basic_map_free(bmap);
5976 return NULL((void*)0);
5977}
5978
5979__isl_give isl_basic_setisl_basic_map *isl_basic_set_nat_universe(__isl_take isl_space *dim)
5980{
5981 return isl_basic_map_nat_universe(dim);
5982}
5983
5984__isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
5985{
5986 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
5987}
5988
5989__isl_give isl_setisl_map *isl_set_nat_universe(__isl_take isl_space *dim)
5990{
5991 return isl_map_nat_universe(dim);
5992}
5993
5994__isl_give isl_map *isl_map_empty(__isl_take isl_space *dim)
5995{
5996 return isl_map_alloc_space(dim, 0, ISL_MAP_DISJOINT(1 << 0));
5997}
5998
5999__isl_give isl_setisl_map *isl_set_empty(__isl_take isl_space *dim)
6000{
6001 return isl_set_alloc_space(dim, 0, ISL_MAP_DISJOINT(1 << 0));
6002}
6003
6004__isl_give isl_map *isl_map_universe(__isl_take isl_space *dim)
6005{
6006 struct isl_map *map;
6007 if (!dim)
6008 return NULL((void*)0);
6009 map = isl_map_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT(1 << 0));
6010 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
6011 return map;
6012}
6013
6014__isl_give isl_setisl_map *isl_set_universe(__isl_take isl_space *dim)
6015{
6016 struct isl_setisl_map *set;
6017 if (!dim)
6018 return NULL((void*)0);
6019 set = isl_set_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT(1 << 0));
6020 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
6021 return set;
6022}
6023
6024struct isl_map *isl_map_dup(struct isl_map *map)
6025{
6026 int i;
6027 struct isl_map *dup;
6028
6029 if (!map)
6030 return NULL((void*)0);
6031 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
6032 for (i = 0; i < map->n; ++i)
6033 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
6034 return dup;
6035}
6036
6037__isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
6038 __isl_take isl_basic_map *bmap)
6039{
6040 if (!bmap || !map)
6041 goto error;
6042 if (isl_basic_map_plain_is_empty(bmap)) {
6043 isl_basic_map_free(bmap);
6044 return map;
6045 }
6046 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",
"/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 6046); goto error; } while (0); } while (0)
;
6047 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", "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 6047); goto error; } while (0); } while (0)
;
6048 map->p[map->n] = bmap;
6049 map->n++;
6050 ISL_F_CLR(map, ISL_MAP_NORMALIZED)(((map)->flags) &= ~((1 << 1)));
6051 return map;
6052error:
6053 if (map)
6054 isl_map_free(map);
6055 if (bmap)
6056 isl_basic_map_free(bmap);
6057 return NULL((void*)0);
6058}
6059
6060__isl_null isl_map *isl_map_free(__isl_take isl_map *map)
6061{
6062 int i;
6063
6064 if (!map)
6065 return NULL((void*)0);
6066
6067 if (--map->ref > 0)
6068 return NULL((void*)0);
6069
6070 clear_caches(map);
6071 isl_ctx_deref(map->ctx);
6072 for (i = 0; i < map->n; ++i)
6073 isl_basic_map_free(map->p[i]);
6074 isl_space_free(map->dim);
6075 free(map);
6076
6077 return NULL((void*)0);
6078}
6079
6080static struct isl_basic_map *isl_basic_map_fix_pos_si(
6081 struct isl_basic_map *bmap, unsigned pos, int value)
6082{
6083 int j;
6084
6085 bmap = isl_basic_map_cow(bmap);
6086 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
6087 j = isl_basic_map_alloc_equality(bmap);
6088 if (j < 0)
6089 goto error;
6090 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
6091 isl_int_set_si(bmap->eq[j][pos], -1)isl_sioimath_set_si((bmap->eq[j][pos]), -1);
6092 isl_int_set_si(bmap->eq[j][0], value)isl_sioimath_set_si((bmap->eq[j][0]), value);
6093 bmap = isl_basic_map_simplify(bmap);
6094 return isl_basic_map_finalize(bmap);
6095error:
6096 isl_basic_map_free(bmap);
6097 return NULL((void*)0);
6098}
6099
6100static __isl_give isl_basic_map *isl_basic_map_fix_pos(
6101 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
6102{
6103 int j;
6104
6105 bmap = isl_basic_map_cow(bmap);
6106 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
6107 j = isl_basic_map_alloc_equality(bmap);
6108 if (j < 0)
6109 goto error;
6110 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
6111 isl_int_set_si(bmap->eq[j][pos], -1)isl_sioimath_set_si((bmap->eq[j][pos]), -1);
6112 isl_int_set(bmap->eq[j][0], value)isl_sioimath_set((bmap->eq[j][0]), *(value));
6113 bmap = isl_basic_map_simplify(bmap);
6114 return isl_basic_map_finalize(bmap);
6115error:
6116 isl_basic_map_free(bmap);
6117 return NULL((void*)0);
6118}
6119
6120__isl_give isl_basic_map *isl_basic_map_fix_si(__isl_take isl_basic_map *bmap,
6121 enum isl_dim_type type, unsigned pos, int value)
6122{
6123 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6124 return isl_basic_map_free(bmap);
6125 return isl_basic_map_fix_pos_si(bmap,
6126 isl_basic_map_offset(bmap, type) + pos, value);
6127}
6128
6129__isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
6130 enum isl_dim_type type, unsigned pos, isl_int value)
6131{
6132 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6133 return isl_basic_map_free(bmap);
6134 return isl_basic_map_fix_pos(bmap,
6135 isl_basic_map_offset(bmap, type) + pos, value);
6136}
6137
6138/* Fix the value of the variable at position "pos" of type "type" of "bmap"
6139 * to be equal to "v".
6140 */
6141__isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
6142 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6143{
6144 if (!bmap || !v)
6145 goto error;
6146 if (!isl_val_is_int(v))
6147 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", "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 6148); goto error; } while (0)
6148 "expecting integer value", goto error)do { isl_handle_error(isl_basic_map_get_ctx(bmap), isl_error_invalid
, "expecting integer value", "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 6148); goto error; } while (0)
;
6149 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6150 goto error;
6151 pos += isl_basic_map_offset(bmap, type);
6152 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
6153 isl_val_free(v);
6154 return bmap;
6155error:
6156 isl_basic_map_free(bmap);
6157 isl_val_free(v);
6158 return NULL((void*)0);
6159}
6160
6161/* Fix the value of the variable at position "pos" of type "type" of "bset"
6162 * to be equal to "v".
6163 */
6164__isl_give isl_basic_setisl_basic_map *isl_basic_set_fix_val(__isl_take isl_basic_setisl_basic_map *bset,
6165 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6166{
6167 return isl_basic_map_fix_val(bset, type, pos, v);
6168}
6169
6170struct isl_basic_setisl_basic_map *isl_basic_set_fix_si(struct isl_basic_setisl_basic_map *bset,
6171 enum isl_dim_type type, unsigned pos, int value)
6172{
6173 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
6174 type, pos, value));
6175}
6176
6177__isl_give isl_basic_setisl_basic_map *isl_basic_set_fix(__isl_take isl_basic_setisl_basic_map *bset,
6178 enum isl_dim_type type, unsigned pos, isl_int value)
6179{
6180 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset),
6181 type, pos, value));
6182}
6183
6184struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
6185 unsigned input, int value)
6186{
6187 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
6188}
6189
6190struct isl_basic_setisl_basic_map *isl_basic_set_fix_dim_si(struct isl_basic_setisl_basic_map *bset,
6191 unsigned dim, int value)
6192{
6193 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
6194 isl_dim_set, dim, value));
6195}
6196
6197static int remove_if_empty(__isl_keep isl_map *map, int i)
6198{
6199 int empty = isl_basic_map_plain_is_empty(map->p[i]);
6200
6201 if (empty < 0)
6202 return -1;
6203 if (!empty)
6204 return 0;
6205
6206 isl_basic_map_free(map->p[i]);
6207 if (i != map->n - 1) {
6208 ISL_F_CLR(map, ISL_MAP_NORMALIZED)(((map)->flags) &= ~((1 << 1)));
6209 map->p[i] = map->p[map->n - 1];
6210 }
6211 map->n--;
6212
6213 return 0;
6214}
6215
6216/* Perform "fn" on each basic map of "map", where we may not be holding
6217 * the only reference to "map".
6218 * In particular, "fn" should be a semantics preserving operation
6219 * that we want to apply to all copies of "map". We therefore need
6220 * to be careful not to modify "map" in a way that breaks "map"
6221 * in case anything goes wrong.
6222 */
6223__isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
6224 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
6225{
6226 struct isl_basic_map *bmap;
6227 int i;
6228
6229 if (!map)
6230 return NULL((void*)0);
6231
6232 for (i = map->n - 1; i >= 0; --i) {
6233 bmap = isl_basic_map_copy(map->p[i]);
6234 bmap = fn(bmap);
6235 if (!bmap)
6236 goto error;
6237 isl_basic_map_free(map->p[i]);
6238 map->p[i] = bmap;
6239 if (remove_if_empty(map, i) < 0)
6240 goto error;
6241 }
6242
6243 return map;
6244error:
6245 isl_map_free(map);
6246 return NULL((void*)0);
6247}
6248
6249__isl_give isl_map *isl_map_fix_si(__isl_take isl_map *map,
6250 enum isl_dim_type type, unsigned pos, int value)
6251{
6252 int i;
6253
6254 map = isl_map_cow(map);
6255 if (!map)
6256 return NULL((void*)0);
6257
6258 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", "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 6258); goto error; } while (0); } while (0)
;
6259 for (i = map->n - 1; i >= 0; --i) {
6260 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
6261 if (remove_if_empty(map, i) < 0)
6262 goto error;
6263 }
6264 ISL_F_CLR(map, ISL_MAP_NORMALIZED)(((map)->flags) &= ~((1 << 1)));
6265 return map;
6266error:
6267 isl_map_free(map);
6268 return NULL((void*)0);
6269}
6270
6271__isl_give isl_setisl_map *isl_set_fix_si(__isl_take isl_setisl_map *set,
6272 enum isl_dim_type type, unsigned pos, int value)
6273{
6274 return set_from_map(isl_map_fix_si(set_to_map(set), type, pos, value));
6275}
6276
6277__isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
6278 enum isl_dim_type type, unsigned pos, isl_int value)
6279{
6280 int i;
6281
6282 map = isl_map_cow(map);
6283 if (!map)
6284 return NULL((void*)0);
6285
6286 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", "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 6286); goto error; } while (0); } while (0)
;
6287 for (i = 0; i < map->n; ++i) {
6288 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
6289 if (!map->p[i])
6290 goto error;
6291 }
6292 ISL_F_CLR(map, ISL_MAP_NORMALIZED)(((map)->flags) &= ~((1 << 1)));
6293 return map;
6294error:
6295 isl_map_free(map);
6296 return NULL((void*)0);
6297}
6298
6299__isl_give isl_setisl_map *isl_set_fix(__isl_take isl_setisl_map *set,
6300 enum isl_dim_type type, unsigned pos, isl_int value)
6301{
6302 return set_from_map(isl_map_fix(set_to_map(set), type, pos, value));
6303}
6304
6305/* Fix the value of the variable at position "pos" of type "type" of "map"
6306 * to be equal to "v".
6307 */
6308__isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
6309 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6310{
6311 int i;
6312
6313 map = isl_map_cow(map);
6314 if (!map || !v)
6315 goto error;
6316
6317 if (!isl_val_is_int(v))
6318 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", "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 6319); goto error; } while (0)
6319 "expecting integer value", goto error)do { isl_handle_error(isl_map_get_ctx(map), isl_error_invalid
, "expecting integer value", "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 6319); goto error; } while (0)
;
6320 if (pos >= isl_map_dim(map, type))
6321 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", "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 6322); goto error; } while (0)
6322 "index out of bounds", goto error)do { isl_handle_error(isl_map_get_ctx(map), isl_error_invalid
, "index out of bounds", "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 6322); goto error; } while (0)
;
6323 for (i = map->n - 1; i >= 0; --i) {
6324 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
6325 isl_val_copy(v));
6326 if (remove_if_empty(map, i) < 0)
6327 goto error;
6328 }
6329 ISL_F_CLR(map, ISL_MAP_NORMALIZED)(((map)->flags) &= ~((1 << 1)));
6330 isl_val_free(v);
6331 return map;
6332error:
6333 isl_map_free(map);
6334 isl_val_free(v);
6335 return NULL((void*)0);
6336}
6337
6338/* Fix the value of the variable at position "pos" of type "type" of "set"
6339 * to be equal to "v".
6340 */
6341__isl_give isl_setisl_map *isl_set_fix_val(__isl_take isl_setisl_map *set,
6342 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6343{
6344 return isl_map_fix_val(set, type, pos, v);
6345}
6346
6347struct isl_map *isl_map_fix_input_si(struct isl_map *map,
6348 unsigned input, int value)
6349{
6350 return isl_map_fix_si(map, isl_dim_in, input, value);
6351}
6352
6353struct isl_setisl_map *isl_set_fix_dim_si(struct isl_setisl_map *set, unsigned dim, int value)
6354{
6355 return set_from_map(isl_map_fix_si(set_to_map(set),
6356 isl_dim_set, dim, value));
6357}
6358
6359static __isl_give isl_basic_map *basic_map_bound_si(
6360 __isl_take isl_basic_map *bmap,
6361 enum isl_dim_type type, unsigned pos, int value, int upper)
6362{
6363 int j;
6364
6365 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6366 return isl_basic_map_free(bmap);
6367 pos += isl_basic_map_offset(bmap, type);
6368 bmap = isl_basic_map_cow(bmap);
6369 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6370 j = isl_basic_map_alloc_inequality(bmap);
6371 if (j < 0)
6372 goto error;
6373 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
6374 if (upper) {
6375 isl_int_set_si(bmap->ineq[j][pos], -1)isl_sioimath_set_si((bmap->ineq[j][pos]), -1);
6376 isl_int_set_si(bmap->ineq[j][0], value)isl_sioimath_set_si((bmap->ineq[j][0]), value);
6377 } else {
6378 isl_int_set_si(bmap->ineq[j][pos], 1)isl_sioimath_set_si((bmap->ineq[j][pos]), 1);
6379 isl_int_set_si(bmap->ineq[j][0], -value)isl_sioimath_set_si((bmap->ineq[j][0]), -value);
6380 }
6381 bmap = isl_basic_map_simplify(bmap);
6382 return isl_basic_map_finalize(bmap);
6383error:
6384 isl_basic_map_free(bmap);
6385 return NULL((void*)0);
6386}
6387
6388__isl_give isl_basic_map *isl_basic_map_lower_bound_si(
6389 __isl_take isl_basic_map *bmap,
6390 enum isl_dim_type type, unsigned pos, int value)
6391{
6392 return basic_map_bound_si(bmap, type, pos, value, 0);
6393}
6394
6395/* Constrain the values of the given dimension to be no greater than "value".
6396 */
6397__isl_give isl_basic_map *isl_basic_map_upper_bound_si(
6398 __isl_take isl_basic_map *bmap,
6399 enum isl_dim_type type, unsigned pos, int value)
6400{
6401 return basic_map_bound_si(bmap, type, pos, value, 1);
6402}
6403
6404static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
6405 enum isl_dim_type type, unsigned pos, int value, int upper)
6406{
6407 int i;
6408
6409 map = isl_map_cow(map);
6410 if (!map)
6411 return NULL((void*)0);
6412
6413 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", "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 6413); goto error; } while (0); } while (0)
;
6414 for (i = 0; i < map->n; ++i) {
6415 map->p[i] = basic_map_bound_si(map->p[i],
6416 type, pos, value, upper);
6417 if (!map->p[i])
6418 goto error;
6419 }
6420 ISL_F_CLR(map, ISL_MAP_NORMALIZED)(((map)->flags) &= ~((1 << 1)));
6421 return map;
6422error:
6423 isl_map_free(map);
6424 return NULL((void*)0);
6425}
6426
6427__isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
6428 enum isl_dim_type type, unsigned pos, int value)
6429{
6430 return map_bound_si(map, type, pos, value, 0);
6431}
6432
6433__isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
6434 enum isl_dim_type type, unsigned pos, int value)
6435{
6436 return map_bound_si(map, type, pos, value, 1);
6437}
6438
6439__isl_give isl_setisl_map *isl_set_lower_bound_si(__isl_take isl_setisl_map *set,
6440 enum isl_dim_type type, unsigned pos, int value)
6441{
6442 return set_from_map(isl_map_lower_bound_si(set_to_map(set),
6443 type, pos, value));
6444}
6445
6446__isl_give isl_setisl_map *isl_set_upper_bound_si(__isl_take isl_setisl_map *set,
6447 enum isl_dim_type type, unsigned pos, int value)
6448{
6449 return isl_map_upper_bound_si(set, type, pos, value);
6450}
6451
6452/* Bound the given variable of "bmap" from below (or above is "upper"
6453 * is set) to "value".
6454 */
6455static __isl_give isl_basic_map *basic_map_bound(
6456 __isl_take isl_basic_map *bmap,
6457 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6458{
6459 int j;
6460
6461 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6462 return isl_basic_map_free(bmap);
6463 pos += isl_basic_map_offset(bmap, type);
6464 bmap = isl_basic_map_cow(bmap);
6465 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6466 j = isl_basic_map_alloc_inequality(bmap);
6467 if (j < 0)
6468 goto error;
6469 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
6470 if (upper) {
6471 isl_int_set_si(bmap->ineq[j][pos], -1)isl_sioimath_set_si((bmap->ineq[j][pos]), -1);
6472 isl_int_set(bmap->ineq[j][0], value)isl_sioimath_set((bmap->ineq[j][0]), *(value));
6473 } else {
6474 isl_int_set_si(bmap->ineq[j][pos], 1)isl_sioimath_set_si((bmap->ineq[j][pos]), 1);
6475 isl_int_neg(bmap->ineq[j][0], value)isl_sioimath_neg((bmap->ineq[j][0]), *(value));
6476 }
6477 bmap = isl_basic_map_simplify(bmap);
6478 return isl_basic_map_finalize(bmap);
6479error:
6480 isl_basic_map_free(bmap);
6481 return NULL((void*)0);
6482}
6483
6484/* Bound the given variable of "map" from below (or above is "upper"
6485 * is set) to "value".
6486 */
6487static __isl_give isl_map *map_bound(__isl_take isl_map *map,
6488 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6489{
6490 int i;
6491
6492 map = isl_map_cow(map);
6493 if (!map)
6494 return NULL((void*)0);
6495
6496 if (pos >= isl_map_dim(map, type))
6497 isl_die(map->ctx, isl_error_invalid,do { isl_handle_error(map->ctx, isl_error_invalid, "index out of bounds"
, "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 6498); goto error; } while (0)
6498 "index out of bounds", goto error)do { isl_handle_error(map->ctx, isl_error_invalid, "index out of bounds"
, "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 6498); goto error; } while (0)
;
6499 for (i = map->n - 1; i >= 0; --i) {
6500 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
6501 if (remove_if_empty(map, i) < 0)
6502 goto error;
6503 }
6504 ISL_F_CLR(map, ISL_MAP_NORMALIZED)(((map)->flags) &= ~((1 << 1)));
6505 return map;
6506error:
6507 isl_map_free(map);
6508 return NULL((void*)0);
6509}
6510
6511__isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
6512 enum isl_dim_type type, unsigned pos, isl_int value)
6513{
6514 return map_bound(map, type, pos, value, 0);
6515}
6516
6517__isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
6518 enum isl_dim_type type, unsigned pos, isl_int value)
6519{
6520 return map_bound(map, type, pos, value, 1);
6521}
6522
6523__isl_give isl_setisl_map *isl_set_lower_bound(__isl_take isl_setisl_map *set,
6524 enum isl_dim_type type, unsigned pos, isl_int value)
6525{
6526 return isl_map_lower_bound(set, type, pos, value);
6527}
6528
6529__isl_give isl_setisl_map *isl_set_upper_bound(__isl_take isl_setisl_map *set,
6530 enum isl_dim_type type, unsigned pos, isl_int value)
6531{
6532 return isl_map_upper_bound(set, type, pos, value);
6533}
6534
6535/* Force the values of the variable at position "pos" of type "type" of "set"
6536 * to be no smaller than "value".
6537 */
6538__isl_give isl_setisl_map *isl_set_lower_bound_val(__isl_take isl_setisl_map *set,
6539 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6540{
6541 if (!value)
6542 goto error;
6543 if (!isl_val_is_int(value))
6544 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", "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 6545); goto error; } while (0)
6545 "expecting integer value", goto error)do { isl_handle_error(isl_set_get_ctx(set), isl_error_invalid
, "expecting integer value", "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 6545); goto error; } while (0)
;
6546 set = isl_set_lower_bound(set, type, pos, value->n);
6547 isl_val_free(value);
6548 return set;
6549error:
6550 isl_val_free(value);
6551 isl_set_free(set);
6552 return NULL((void*)0);
6553}
6554
6555/* Force the values of the variable at position "pos" of type "type" of "set"
6556 * to be no greater than "value".
6557 */
6558__isl_give isl_setisl_map *isl_set_upper_bound_val(__isl_take isl_setisl_map *set,
6559 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6560{
6561 if (!value)
6562 goto error;
6563 if (!isl_val_is_int(value))
6564 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", "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 6565); goto error; } while (0)
6565 "expecting integer value", goto error)do { isl_handle_error(isl_set_get_ctx(set), isl_error_invalid
, "expecting integer value", "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 6565); goto error; } while (0)
;
6566 set = isl_set_upper_bound(set, type, pos, value->n);
6567 isl_val_free(value);
6568 return set;
6569error:
6570 isl_val_free(value);
6571 isl_set_free(set);
6572 return NULL((void*)0);
6573}
6574
6575/* Bound the given variable of "bset" from below (or above is "upper"
6576 * is set) to "value".
6577 */
6578static __isl_give isl_basic_setisl_basic_map *isl_basic_set_bound(
6579 __isl_take isl_basic_setisl_basic_map *bset, enum isl_dim_type type, unsigned pos,
6580 isl_int value, int upper)
6581{
6582 return bset_from_bmap(basic_map_bound(bset_to_bmap(bset),
6583 type, pos, value, upper));
6584}
6585
6586/* Bound the given variable of "bset" from below (or above is "upper"
6587 * is set) to "value".
6588 */
6589static __isl_give isl_basic_setisl_basic_map *isl_basic_set_bound_val(
6590 __isl_take isl_basic_setisl_basic_map *bset, enum isl_dim_type type, unsigned pos,
6591 __isl_take isl_val *value, int upper)
6592{
6593 if (!value)
6594 goto error;
6595 if (!isl_val_is_int(value))
6596 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,do { isl_handle_error(isl_basic_set_get_ctx(bset), isl_error_invalid
, "expecting integer value", "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 6597); goto error; } while (0)
6597 "expecting integer value", goto error)do { isl_handle_error(isl_basic_set_get_ctx(bset), isl_error_invalid
, "expecting integer value", "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 6597); goto error; } while (0)
;
6598 bset = isl_basic_set_bound(bset, type, pos, value->n, upper);
6599 isl_val_free(value);
6600 return bset;
6601error:
6602 isl_val_free(value);
6603 isl_basic_set_free(bset);
6604 return NULL((void*)0);
6605}
6606
6607/* Bound the given variable of "bset" from below to "value".
6608 */
6609__isl_give isl_basic_setisl_basic_map *isl_basic_set_lower_bound_val(
6610 __isl_take isl_basic_setisl_basic_map *bset, enum isl_dim_type type, unsigned pos,
6611 __isl_take isl_val *value)
6612{
6613 return isl_basic_set_bound_val(bset, type, pos, value, 0);
6614}
6615
6616/* Bound the given variable of "bset" from above to "value".
6617 */
6618__isl_give isl_basic_setisl_basic_map *isl_basic_set_upper_bound_val(
6619 __isl_take isl_basic_setisl_basic_map *bset, enum isl_dim_type type, unsigned pos,
6620 __isl_take isl_val *value)
6621{
6622 return isl_basic_set_bound_val(bset, type, pos, value, 1);
6623}
6624
6625__isl_give isl_map *isl_map_reverse(__isl_take isl_map *map)
6626{
6627 int i;
6628
6629 map = isl_map_cow(map);
6630 if (!map)
6631 return NULL((void*)0);
6632
6633 map->dim = isl_space_reverse(map->dim);
6634 if (!map->dim)
6635 goto error;
6636 for (i = 0; i < map->n; ++i) {
6637 map->p[i] = isl_basic_map_reverse(map->p[i]);
6638 if (!map->p[i])
6639 goto error;
6640 }
6641 ISL_F_CLR(map, ISL_MAP_NORMALIZED)(((map)->flags) &= ~((1 << 1)));
6642 return map;
6643error:
6644 isl_map_free(map);
6645 return NULL((void*)0);
6646}
6647
6648#undef TYPEisl_map
6649#define TYPEisl_map isl_pw_multi_aff
6650#undef SUFFIX
6651#define SUFFIX _pw_multi_aff
6652#undef EMPTYisl_map_empty
6653#define EMPTYisl_map_empty isl_pw_multi_aff_empty
6654#undef ADDisl_map_union_disjoint
6655#define ADDisl_map_union_disjoint isl_pw_multi_aff_union_add
6656#include "isl_map_lexopt_templ.c"
6657
6658/* Given a map "map", compute the lexicographically minimal
6659 * (or maximal) image element for each domain element in dom,
6660 * in the form of an isl_pw_multi_aff.
6661 * If "empty" is not NULL, then set *empty to those elements in dom that
6662 * do not have an image element.
6663 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6664 * should be computed over the domain of "map". "empty" is also NULL
6665 * in this case.
6666 *
6667 * We first compute the lexicographically minimal or maximal element
6668 * in the first basic map. This results in a partial solution "res"
6669 * and a subset "todo" of dom that still need to be handled.
6670 * We then consider each of the remaining maps in "map" and successively
6671 * update both "res" and "todo".
6672 * If "empty" is NULL, then the todo sets are not needed and therefore
6673 * also not computed.
6674 */
6675static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
6676 __isl_take isl_map *map, __isl_take isl_setisl_map *dom,
6677 __isl_give isl_setisl_map **empty, unsigned flags)
6678{
6679 int i;
6680 int full;
6681 isl_pw_multi_aff *res;
6682 isl_setisl_map *todo;
6683
6684 full = ISL_FL_ISSET(flags, ISL_OPT_FULL)(!!((flags) & ((1 << 1))));
6685 if (!map || (!full && !dom))
6686 goto error;
6687
6688 if (isl_map_plain_is_empty(map)) {
6689 if (empty)
6690 *empty = dom;
6691 else
6692 isl_set_free(dom);
6693 return isl_pw_multi_aff_from_map(map);
6694 }
6695
6696 res = basic_map_partial_lexopt_pw_multi_aff(
6697 isl_basic_map_copy(map->p[0]),
6698 isl_set_copy(dom), empty, flags);
6699
6700 if (empty)
6701 todo = *empty;
6702 for (i = 1; i < map->n; ++i) {
6703 isl_pw_multi_aff *res_i;
6704
6705 res_i = basic_map_partial_lexopt_pw_multi_aff(
6706 isl_basic_map_copy(map->p[i]),
6707 isl_set_copy(dom), empty, flags);
6708
6709 if (ISL_FL_ISSET(flags, ISL_OPT_MAX)(!!((flags) & ((1 << 0)))))
6710 res = isl_pw_multi_aff_union_lexmax(res, res_i);
6711 else
6712 res = isl_pw_multi_aff_union_lexmin(res, res_i);
6713
6714 if (empty)
6715 todo = isl_set_intersect(todo, *empty);
6716 }
6717
6718 isl_set_free(dom);
6719 isl_map_free(map);
6720
6721 if (empty)
6722 *empty = todo;
6723
6724 return res;
6725error:
6726 if (empty)
6727 *empty = NULL((void*)0);
6728 isl_set_free(dom);
6729 isl_map_free(map);
6730 return NULL((void*)0);
6731}
6732
6733#undef TYPEisl_map
6734#define TYPEisl_map isl_map
6735#undef SUFFIX
6736#define SUFFIX
6737#undef EMPTYisl_map_empty
6738#define EMPTYisl_map_empty isl_map_empty
6739#undef ADDisl_map_union_disjoint
6740#define ADDisl_map_union_disjoint isl_map_union_disjoint
6741#include "isl_map_lexopt_templ.c"
6742
6743/* Given a map "map", compute the lexicographically minimal
6744 * (or maximal) image element for each domain element in "dom",
6745 * in the form of an isl_map.
6746 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6747 * do not have an image element.
6748 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6749 * should be computed over the domain of "map". "empty" is also NULL
6750 * in this case.
6751 *
6752 * If the input consists of more than one disjunct, then first
6753 * compute the desired result in the form of an isl_pw_multi_aff and
6754 * then convert that into an isl_map.
6755 *
6756 * This function used to have an explicit implementation in terms
6757 * of isl_maps, but it would continually intersect the domains of
6758 * partial results with the complement of the domain of the next
6759 * partial solution, potentially leading to an explosion in the number
6760 * of disjuncts if there are several disjuncts in the input.
6761 * An even earlier implementation of this function would look for
6762 * better results in the domain of the partial result and for extra
6763 * results in the complement of this domain, which would lead to
6764 * even more splintering.
6765 */
6766static __isl_give isl_map *isl_map_partial_lexopt_aligned(
6767 __isl_take isl_map *map, __isl_take isl_setisl_map *dom,
6768 __isl_give isl_setisl_map **empty, unsigned flags)
6769{
6770 int full;
6771 struct isl_map *res;
6772 isl_pw_multi_aff *pma;
6773
6774 full = ISL_FL_ISSET(flags, ISL_OPT_FULL)(!!((flags) & ((1 << 1))));
6775 if (!map || (!full && !dom))
6776 goto error;
6777
6778 if (isl_map_plain_is_empty(map)) {
6779 if (empty)
6780 *empty = dom;
6781 else
6782 isl_set_free(dom);
6783 return map;
6784 }
6785
6786 if (map->n == 1) {
6787 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
6788 dom, empty, flags);
6789 isl_map_free(map);
6790 return res;
6791 }
6792
6793 pma = isl_map_partial_lexopt_aligned_pw_multi_aff(map, dom, empty,
6794 flags);
6795 return isl_map_from_pw_multi_aff(pma);
6796error:
6797 if (empty)
6798 *empty = NULL((void*)0);
6799 isl_set_free(dom);
6800 isl_map_free(map);
6801 return NULL((void*)0);
6802}
6803
6804__isl_give isl_map *isl_map_partial_lexmax(
6805 __isl_take isl_map *map, __isl_take isl_setisl_map *dom,
6806 __isl_give isl_setisl_map **empty)
6807{
6808 return isl_map_partial_lexopt(map, dom, empty, ISL_OPT_MAX(1 << 0));
6809}
6810
6811__isl_give isl_map *isl_map_partial_lexmin(
6812 __isl_take isl_map *map, __isl_take isl_setisl_map *dom,
6813 __isl_give isl_setisl_map **empty)
6814{
6815 return isl_map_partial_lexopt(map, dom, empty, 0);
6816}
6817
6818__isl_give isl_setisl_map *isl_set_partial_lexmin(
6819 __isl_take isl_setisl_map *set, __isl_take isl_setisl_map *dom,
6820 __isl_give isl_setisl_map **empty)
6821{
6822 return set_from_map(isl_map_partial_lexmin(set_to_map(set),
6823 dom, empty));
6824}
6825
6826__isl_give isl_setisl_map *isl_set_partial_lexmax(
6827 __isl_take isl_setisl_map *set, __isl_take isl_setisl_map *dom,
6828 __isl_give isl_setisl_map **empty)
6829{
6830 return set_from_map(isl_map_partial_lexmax(set_to_map(set),
6831 dom, empty));
6832}
6833
6834/* Compute the lexicographic minimum (or maximum if "flags" includes
6835 * ISL_OPT_MAX) of "bset" over its parametric domain.
6836 */
6837__isl_give isl_setisl_map *isl_basic_set_lexopt(__isl_take isl_basic_setisl_basic_map *bset,
6838 unsigned flags)
6839{
6840 return isl_basic_map_lexopt(bset, flags);
6841}
6842
6843__isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
6844{
6845 return isl_basic_map_lexopt(bmap, ISL_OPT_MAX(1 << 0));
6846}
6847
6848__isl_give isl_setisl_map *isl_basic_set_lexmin(__isl_take isl_basic_setisl_basic_map *bset)
6849{
6850 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset)));
6851}
6852
6853__isl_give isl_setisl_map *isl_basic_set_lexmax(__isl_take isl_basic_setisl_basic_map *bset)
6854{
6855 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset)));
6856}
6857
6858/* Compute the lexicographic minimum of "bset" over its parametric domain
6859 * for the purpose of quantifier elimination.
6860 * That is, find an explicit representation for all the existentially
6861 * quantified variables in "bset" by computing their lexicographic
6862 * minimum.
6863 */
6864static __isl_give isl_setisl_map *isl_basic_set_lexmin_compute_divs(
6865 __isl_take isl_basic_setisl_basic_map *bset)
6866{
6867 return isl_basic_set_lexopt(bset, ISL_OPT_QE(1 << 2));
6868}
6869
6870/* Extract the first and only affine expression from list
6871 * and then add it to *pwaff with the given dom.
6872 * This domain is known to be disjoint from other domains
6873 * because of the way isl_basic_map_foreach_lexmax works.
6874 */
6875static isl_stat update_dim_opt(__isl_take isl_basic_setisl_basic_map *dom,
6876 __isl_take isl_aff_list *list, void *user)
6877{
6878 isl_ctx *ctx = isl_basic_set_get_ctx(dom);
6879 isl_aff *aff;
6880 isl_pw_aff **pwaff = user;
6881 isl_pw_aff *pwaff_i;
6882
6883 if (!list)
6884 goto error;
6885 if (isl_aff_list_n_aff(list) != 1)
6886 isl_die(ctx, isl_error_internal,do { isl_handle_error(ctx, isl_error_internal, "expecting single element list"
, "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 6887); goto error; } while (0)
6887 "expecting single element list", goto error)do { isl_handle_error(ctx, isl_error_internal, "expecting single element list"
, "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 6887); goto error; } while (0)
;
6888
6889 aff = isl_aff_list_get_aff(list, 0);
6890 pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
6891
6892 *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
6893
6894 isl_aff_list_free(list);
6895
6896 return isl_stat_ok;
6897error:
6898 isl_basic_set_free(dom);
6899 isl_aff_list_free(list);
6900 return isl_stat_error;
6901}
6902
6903/* Given a basic map with one output dimension, compute the minimum or
6904 * maximum of that dimension as an isl_pw_aff.
6905 *
6906 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6907 * call update_dim_opt on each leaf of the result.
6908 */
6909static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
6910 int max)
6911{
6912 isl_space *dim = isl_basic_map_get_space(bmap);
6913 isl_pw_aff *pwaff;
6914 isl_stat r;
6915
6916 dim = isl_space_from_domain(isl_space_domain(dim));
6917 dim = isl_space_add_dims(dim, isl_dim_out, 1);
6918 pwaff = isl_pw_aff_empty(dim);
6919
6920 r = isl_basic_map_foreach_lexopt(bmap, max, &update_dim_opt, &pwaff);
6921 if (r < 0)
6922 return isl_pw_aff_free(pwaff);
6923
6924 return pwaff;
6925}
6926
6927/* Compute the minimum or maximum of the given output dimension
6928 * as a function of the parameters and the input dimensions,
6929 * but independently of the other output dimensions.
6930 *
6931 * We first project out the other output dimension and then compute
6932 * the "lexicographic" maximum in each basic map, combining the results
6933 * using isl_pw_aff_union_max.
6934 */
6935static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
6936 int max)
6937{
6938 int i;
6939 isl_pw_aff *pwaff;
6940 unsigned n_out;
6941
6942 n_out = isl_map_dim(map, isl_dim_out);
6943 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
6944 map = isl_map_project_out(map, isl_dim_out, 0, pos);
6945 if (!map)
6946 return NULL((void*)0);
6947
6948 if (map->n == 0) {
6949 isl_space *dim = isl_map_get_space(map);
6950 isl_map_free(map);
6951 return isl_pw_aff_empty(dim);
6952 }
6953
6954 pwaff = basic_map_dim_opt(map->p[0], max);
6955 for (i = 1; i < map->n; ++i) {
6956 isl_pw_aff *pwaff_i;
6957
6958 pwaff_i = basic_map_dim_opt(map->p[i], max);
6959 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
6960 }
6961
6962 isl_map_free(map);
6963
6964 return pwaff;
6965}
6966
6967/* Compute the minimum of the given output dimension as a function of the
6968 * parameters and input dimensions, but independently of
6969 * the other output dimensions.
6970 */
6971__isl_give isl_pw_aff *isl_map_dim_min(__isl_take isl_map *map, int pos)
6972{
6973 return map_dim_opt(map, pos, 0);
6974}
6975
6976/* Compute the maximum of the given output dimension as a function of the
6977 * parameters and input dimensions, but independently of
6978 * the other output dimensions.
6979 */
6980__isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
6981{
6982 return map_dim_opt(map, pos, 1);
6983}
6984
6985/* Compute the minimum or maximum of the given set dimension
6986 * as a function of the parameters,
6987 * but independently of the other set dimensions.
6988 */
6989static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_setisl_map *set, int pos,
6990 int max)
6991{
6992 return map_dim_opt(set, pos, max);
6993}
6994
6995/* Compute the maximum of the given set dimension as a function of the
6996 * parameters, but independently of the other set dimensions.
6997 */
6998__isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_setisl_map *set, int pos)
6999{
7000 return set_dim_opt(set, pos, 1);
7001}
7002
7003/* Compute the minimum of the given set dimension as a function of the
7004 * parameters, but independently of the other set dimensions.
7005 */
7006__isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_setisl_map *set, int pos)
7007{
7008 return set_dim_opt(set, pos, 0);
7009}
7010
7011/* Apply a preimage specified by "mat" on the parameters of "bset".
7012 * bset is assumed to have only parameters and divs.
7013 */
7014static __isl_give isl_basic_setisl_basic_map *basic_set_parameter_preimage(
7015 __isl_take isl_basic_setisl_basic_map *bset, __isl_take isl_mat *mat)
7016{
7017 unsigned nparam;
7018
7019 if (!bset || !mat)
7020 goto error;
7021
7022 bset->dim = isl_space_cow(bset->dim);
7023 if (!bset->dim)
7024 goto error;
7025
7026 nparam = isl_basic_set_dim(bset, isl_dim_param);
7027
7028 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", "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 7028); goto error; } while (0); } while (0)
;
7029
7030 bset->dim->nparam = 0;
7031 bset->dim->n_out = nparam;
7032 bset = isl_basic_set_preimage(bset, mat);
7033 if (bset) {
7034 bset->dim->nparam = bset->dim->n_out;
7035 bset->dim->n_out = 0;
7036 }
7037 return bset;
7038error:
7039 isl_mat_free(mat);
7040 isl_basic_set_free(bset);
7041 return NULL((void*)0);
7042}
7043
7044/* Apply a preimage specified by "mat" on the parameters of "set".
7045 * set is assumed to have only parameters and divs.
7046 */
7047static __isl_give isl_setisl_map *set_parameter_preimage(__isl_take isl_setisl_map *set,
7048 __isl_take isl_mat *mat)
7049{
7050 isl_space *space;
7051 unsigned nparam;
7052
7053 if (!set || !mat)
7054 goto error;
7055
7056 nparam = isl_set_dim(set, isl_dim_param);
7057
7058 if (mat->n_row != 1 + nparam)
7059 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", "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 7060); goto error; } while (0)
7060 "unexpected number of rows", goto error)do { isl_handle_error(isl_set_get_ctx(set), isl_error_internal
, "unexpected number of rows", "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 7060); goto error; } while (0)
;
7061
7062 space = isl_set_get_space(set);
7063 space = isl_space_move_dims(space, isl_dim_set, 0,
7064 isl_dim_param, 0, nparam);
7065 set = isl_set_reset_space(set, space);
7066 set = isl_set_preimage(set, mat);
7067 nparam = isl_set_dim(set, isl_dim_out);
7068 space = isl_set_get_space(set);
7069 space = isl_space_move_dims(space, isl_dim_param, 0,
7070 isl_dim_out, 0, nparam);
7071 set = isl_set_reset_space(set, space);
7072 return set;
7073error:
7074 isl_mat_free(mat);
7075 isl_set_free(set);
7076 return NULL((void*)0);
7077}
7078
7079/* Intersect the basic set "bset" with the affine space specified by the
7080 * equalities in "eq".
7081 */
7082static __isl_give isl_basic_setisl_basic_map *basic_set_append_equalities(
7083 __isl_take isl_basic_setisl_basic_map *bset, __isl_take isl_mat *eq)
7084{
7085 int i, k;
7086 unsigned len;
7087
7088 if (!bset || !eq)
7089 goto error;
7090
7091 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
7092 eq->n_row, 0);
7093 if (!bset)
7094 goto error;
7095
7096 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
7097 for (i = 0; i < eq->n_row; ++i) {
7098 k = isl_basic_set_alloc_equality(bset);
7099 if (k < 0)
7100 goto error;
7101 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
7102 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
7103 }
7104 isl_mat_free(eq);
7105
7106 bset = isl_basic_set_gauss(bset, NULL((void*)0));
7107 bset = isl_basic_set_finalize(bset);
7108
7109 return bset;
7110error:
7111 isl_mat_free(eq);
7112 isl_basic_set_free(bset);
7113 return NULL((void*)0);
7114}
7115
7116/* Intersect the set "set" with the affine space specified by the
7117 * equalities in "eq".
7118 */
7119static struct isl_setisl_map *set_append_equalities(struct isl_setisl_map *set,
7120 struct isl_mat *eq)
7121{
7122 int i;
7123
7124 if (!set || !eq)
7125 goto error;
7126
7127 for (i = 0; i < set->n; ++i) {
7128 set->p[i] = basic_set_append_equalities(set->p[i],
7129 isl_mat_copy(eq));
7130 if (!set->p[i])
7131 goto error;
7132 }
7133 isl_mat_free(eq);
7134 return set;
7135error:
7136 isl_mat_free(eq);
7137 isl_set_free(set);
7138 return NULL((void*)0);
7139}
7140
7141/* Given a basic set "bset" that only involves parameters and existentially
7142 * quantified variables, return the index of the first equality
7143 * that only involves parameters. If there is no such equality then
7144 * return bset->n_eq.
7145 *
7146 * This function assumes that isl_basic_set_gauss has been called on "bset".
7147 */
7148static int first_parameter_equality(__isl_keep isl_basic_setisl_basic_map *bset)
7149{
7150 int i, j;
7151 unsigned nparam, n_div;
7152
7153 if (!bset)
7154 return -1;
7155
7156 nparam = isl_basic_set_dim(bset, isl_dim_param);
7157 n_div = isl_basic_set_dim(bset, isl_dim_div);
7158
7159 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
7160 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j])(isl_sioimath_sgn(*(bset->eq[i][1 + nparam + j])) == 0))
7161 ++i;
7162 }
7163
7164 return i;
7165}
7166
7167/* Compute an explicit representation for the existentially quantified
7168 * variables in "bset" by computing the "minimal value" of the set
7169 * variables. Since there are no set variables, the computation of
7170 * the minimal value essentially computes an explicit representation
7171 * of the non-empty part(s) of "bset".
7172 *
7173 * The input only involves parameters and existentially quantified variables.
7174 * All equalities among parameters have been removed.
7175 *
7176 * Since the existentially quantified variables in the result are in general
7177 * going to be different from those in the input, we first replace
7178 * them by the minimal number of variables based on their equalities.
7179 * This should simplify the parametric integer programming.
7180 */
7181static __isl_give isl_setisl_map *base_compute_divs(__isl_take isl_basic_setisl_basic_map *bset)
7182{
7183 isl_morph *morph1, *morph2;
7184 isl_setisl_map *set;
7185 unsigned n;
7186
7187 if (!bset)
7188 return NULL((void*)0);
7189 if (bset->n_eq == 0)
7190 return isl_basic_set_lexmin_compute_divs(bset);
7191
7192 morph1 = isl_basic_set_parameter_compression(bset);
7193 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
7194 bset = isl_basic_set_lift(bset);
7195 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
7196 bset = isl_morph_basic_set(morph2, bset);
7197 n = isl_basic_set_dim(bset, isl_dim_set);
7198 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
7199
7200 set = isl_basic_set_lexmin_compute_divs(bset);
7201
7202 set = isl_morph_set(isl_morph_inverse(morph1), set);
7203
7204 return set;
7205}
7206
7207/* Project the given basic set onto its parameter domain, possibly introducing
7208 * new, explicit, existential variables in the constraints.
7209 * The input has parameters and (possibly implicit) existential variables.
7210 * The output has the same parameters, but only
7211 * explicit existentially quantified variables.
7212 *
7213 * The actual projection is performed by pip, but pip doesn't seem
7214 * to like equalities very much, so we first remove the equalities
7215 * among the parameters by performing a variable compression on
7216 * the parameters. Afterward, an inverse transformation is performed
7217 * and the equalities among the parameters are inserted back in.
7218 *
7219 * The variable compression on the parameters may uncover additional
7220 * equalities that were only implicit before. We therefore check
7221 * if there are any new parameter equalities in the result and
7222 * if so recurse. The removal of parameter equalities is required
7223 * for the parameter compression performed by base_compute_divs.
7224 */
7225static struct isl_setisl_map *parameter_compute_divs(struct isl_basic_setisl_basic_map *bset)
7226{
7227 int i;
7228 struct isl_mat *eq;
7229 struct isl_mat *T, *T2;
7230 struct isl_setisl_map *set;
7231 unsigned nparam;
7232
7233 bset = isl_basic_set_cow(bset);
7234 if (!bset)
7235 return NULL((void*)0);
7236
7237 if (bset->n_eq == 0)
7238 return base_compute_divs(bset);
7239
7240 bset = isl_basic_set_gauss(bset, NULL((void*)0));
7241 if (!bset)
7242 return NULL((void*)0);
7243 if (isl_basic_set_plain_is_empty(bset))
7244 return isl_set_from_basic_set(bset);
7245
7246 i = first_parameter_equality(bset);
7247 if (i == bset->n_eq)
7248 return base_compute_divs(bset);
7249
7250 nparam = isl_basic_set_dim(bset, isl_dim_param);
7251 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
7252 0, 1 + nparam);
7253 eq = isl_mat_cow(eq);
7254 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
7255 if (T && T->n_col == 0) {
7256 isl_mat_free(T);
7257 isl_mat_free(T2);
7258 isl_mat_free(eq);
7259 bset = isl_basic_set_set_to_empty(bset);
7260 return isl_set_from_basic_set(bset);
7261 }
7262 bset = basic_set_parameter_preimage(bset, T);
7263
7264 i = first_parameter_equality(bset);
7265 if (!bset)
7266 set = NULL((void*)0);
7267 else if (i == bset->n_eq)
7268 set = base_compute_divs(bset);
7269 else
7270 set = parameter_compute_divs(bset);
7271 set = set_parameter_preimage(set, T2);
7272 set = set_append_equalities(set, eq);
7273 return set;
7274}
7275
7276/* Insert the divs from "ls" before those of "bmap".
7277 *
7278 * The number of columns is not changed, which means that the last
7279 * dimensions of "bmap" are being reintepreted as the divs from "ls".
7280 * The caller is responsible for removing the same number of dimensions
7281 * from the space of "bmap".
7282 */
7283static __isl_give isl_basic_map *insert_divs_from_local_space(
7284 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
7285{
7286 int i;
7287 int n_div;
7288 int old_n_div;
7289
7290 n_div = isl_local_space_dim(ls, isl_dim_div);
7291 if (n_div == 0)
7292 return bmap;
7293
7294 old_n_div = bmap->n_div;
7295 bmap = insert_div_rows(bmap, n_div);
7296 if (!bmap)
7297 return NULL((void*)0);
7298
7299 for (i = 0; i < n_div; ++i) {
7300 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
7301 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
7302 }
7303
7304 return bmap;
7305}
7306
7307/* Replace the space of "bmap" by the space and divs of "ls".
7308 *
7309 * If "ls" has any divs, then we simplify the result since we may
7310 * have discovered some additional equalities that could simplify
7311 * the div expressions.
7312 */
7313static __isl_give isl_basic_map *basic_replace_space_by_local_space(
7314 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
7315{
7316 int n_div;
7317
7318 bmap = isl_basic_map_cow(bmap);
7319 if (!bmap || !ls)
7320 goto error;
7321
7322 n_div = isl_local_space_dim(ls, isl_dim_div);
7323 bmap = insert_divs_from_local_space(bmap, ls);
7324 if (!bmap)
7325 goto error;
7326
7327 isl_space_free(bmap->dim);
7328 bmap->dim = isl_local_space_get_space(ls);
7329 if (!bmap->dim)
7330 goto error;
7331
7332 isl_local_space_free(ls);
7333 if (n_div > 0)
7334 bmap = isl_basic_map_simplify(bmap);
7335 bmap = isl_basic_map_finalize(bmap);
7336 return bmap;
7337error:
7338 isl_basic_map_free(bmap);
7339 isl_local_space_free(ls);
7340 return NULL((void*)0);
7341}
7342
7343/* Replace the space of "map" by the space and divs of "ls".
7344 */
7345static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
7346 __isl_take isl_local_space *ls)
7347{
7348 int i;
7349
7350 map = isl_map_cow(map);
7351 if (!map || !ls)
7352 goto error;
7353
7354 for (i = 0; i < map->n; ++i) {
7355 map->p[i] = basic_replace_space_by_local_space(map->p[i],
7356 isl_local_space_copy(ls));
7357 if (!map->p[i])
7358 goto error;
7359 }
7360 isl_space_free(map->dim);
7361 map->dim = isl_local_space_get_space(ls);
7362 if (!map->dim)
7363 goto error;
7364
7365 isl_local_space_free(ls);
7366 return map;
7367error:
7368 isl_local_space_free(ls);
7369 isl_map_free(map);
7370 return NULL((void*)0);
7371}
7372
7373/* Compute an explicit representation for the existentially
7374 * quantified variables for which do not know any explicit representation yet.
7375 *
7376 * We first sort the existentially quantified variables so that the
7377 * existentially quantified variables for which we already have an explicit
7378 * representation are placed before those for which we do not.
7379 * The input dimensions, the output dimensions and the existentially
7380 * quantified variables for which we already have an explicit
7381 * representation are then turned into parameters.
7382 * compute_divs returns a map with the same parameters and
7383 * no input or output dimensions and the dimension specification
7384 * is reset to that of the input, including the existentially quantified
7385 * variables for which we already had an explicit representation.
7386 */
7387static struct isl_map *compute_divs(struct isl_basic_map *bmap)
7388{
7389 struct isl_basic_setisl_basic_map *bset;
7390 struct isl_setisl_map *set;
7391 struct isl_map *map;
7392 isl_space *dim;
7393 isl_local_space *ls;
7394 unsigned nparam;
7395 unsigned n_in;
7396 unsigned n_out;
7397 int n_known;
7398 int i;
7399
7400 bmap = isl_basic_map_sort_divs(bmap);
7401 bmap = isl_basic_map_cow(bmap);
7402 if (!bmap)
7403 return NULL((void*)0);
7404
7405 n_known = isl_basic_map_first_unknown_div(bmap);
7406 if (n_known < 0)
7407 return isl_map_from_basic_map(isl_basic_map_free(bmap));
7408
7409 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7410 n_in = isl_basic_map_dim(bmap, isl_dim_in);
7411 n_out = isl_basic_map_dim(bmap, isl_dim_out);
7412 dim = isl_space_set_alloc(bmap->ctx,
7413 nparam + n_in + n_out + n_known, 0);
7414 if (!dim)
7415 goto error;
7416
7417 ls = isl_basic_map_get_local_space(bmap);
7418 ls = isl_local_space_drop_dims(ls, isl_dim_div,
7419 n_known, bmap->n_div - n_known);
7420 if (n_known > 0) {
7421 for (i = n_known; i < bmap->n_div; ++i)
7422 swap_div(bmap, i - n_known, i);
7423 bmap->n_div -= n_known;
7424 bmap->extra -= n_known;
7425 }
7426 bmap = isl_basic_map_reset_space(bmap, dim);
7427 bset = bset_from_bmap(bmap);
7428
7429 set = parameter_compute_divs(bset);
7430 map = set_to_map(set);
7431 map = replace_space_by_local_space(map, ls);
7432
7433 return map;
7434error:
7435 isl_basic_map_free(bmap);
7436 return NULL((void*)0);
7437}
7438
7439/* Remove the explicit representation of local variable "div",
7440 * if there is any.
7441 */
7442__isl_give isl_basic_map *isl_basic_map_mark_div_unknown(
7443 __isl_take isl_basic_map *bmap, int div)
7444{
7445 isl_bool unknown;
7446
7447 unknown = isl_basic_map_div_is_marked_unknown(bmap, div);
7448 if (unknown < 0)
7449 return isl_basic_map_free(bmap);
7450 if (unknown)
7451 return bmap;
7452
7453 bmap = isl_basic_map_cow(bmap);
7454 if (!bmap)
7455 return NULL((void*)0);
7456 isl_int_set_si(bmap->div[div][0], 0)isl_sioimath_set_si((bmap->div[div][0]), 0);
7457 return bmap;
7458}
7459
7460/* Is local variable "div" of "bmap" marked as not having an explicit
7461 * representation?
7462 * Note that even if "div" is not marked in this way and therefore
7463 * has an explicit representation, this representation may still
7464 * depend (indirectly) on other local variables that do not
7465 * have an explicit representation.
7466 */
7467isl_bool isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map *bmap,
7468 int div)
7469{
7470 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
7471 return isl_bool_error;
7472 return isl_int_is_zero(bmap->div[div][0])(isl_sioimath_sgn(*(bmap->div[div][0])) == 0);
7473}
7474
7475/* Return the position of the first local variable that does not
7476 * have an explicit representation.
7477 * Return the total number of local variables if they all have
7478 * an explicit representation.
7479 * Return -1 on error.
7480 */
7481int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map *bmap)
7482{
7483 int i;
7484
7485 if (!bmap)
7486 return -1;
7487
7488 for (i = 0; i < bmap->n_div; ++i) {
7489 if (!isl_basic_map_div_is_known(bmap, i))
7490 return i;
7491 }
7492 return bmap->n_div;
7493}
7494
7495/* Return the position of the first local variable that does not
7496 * have an explicit representation.
7497 * Return the total number of local variables if they all have
7498 * an explicit representation.
7499 * Return -1 on error.
7500 */
7501int isl_basic_set_first_unknown_div(__isl_keep isl_basic_setisl_basic_map *bset)
7502{
7503 return isl_basic_map_first_unknown_div(bset);
7504}
7505
7506/* Does "bmap" have an explicit representation for all local variables?
7507 */
7508isl_bool isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
7509{
7510 int first, n;
7511
7512 n = isl_basic_map_dim(bmap, isl_dim_div);
7513 first = isl_basic_map_first_unknown_div(bmap);
7514 if (first < 0)
7515 return isl_bool_error;
7516 return first == n;
7517}
7518
7519/* Do all basic maps in "map" have an explicit representation
7520 * for all local variables?
7521 */
7522isl_bool isl_map_divs_known(__isl_keep isl_map *map)
7523{
7524 int i;
7525
7526 if (!map)
7527 return isl_bool_error;
7528
7529 for (i = 0; i < map->n; ++i) {
7530 int known = isl_basic_map_divs_known(map->p[i]);
7531 if (known <= 0)
7532 return known;
7533 }
7534
7535 return isl_bool_true;
7536}
7537
7538/* If bmap contains any unknown divs, then compute explicit
7539 * expressions for them. However, this computation may be
7540 * quite expensive, so first try to remove divs that aren't
7541 * strictly needed.
7542 */
7543__isl_give isl_map *isl_basic_map_compute_divs(__isl_take isl_basic_map *bmap)
7544{
7545 int known;
7546 struct isl_map *map;
7547
7548 known = isl_basic_map_divs_known(bmap);
7549 if (known < 0)
7550 goto error;
7551 if (known)
7552 return isl_map_from_basic_map(bmap);
7553
7554 bmap = isl_basic_map_drop_redundant_divs(bmap);
7555
7556 known = isl_basic_map_divs_known(bmap);
7557 if (known < 0)
7558 goto error;
7559 if (known)
7560 return isl_map_from_basic_map(bmap);
7561
7562 map = compute_divs(bmap);
7563 return map;
7564error:
7565 isl_basic_map_free(bmap);
7566 return NULL((void*)0);
7567}
7568
7569__isl_give isl_map *isl_map_compute_divs(__isl_take isl_map *map)
7570{
7571 int i;
7572 int known;
7573 struct isl_map *res;
7574
7575 if (!map)
7576 return NULL((void*)0);
7577 if (map->n == 0)
7578 return map;
7579
7580 known = isl_map_divs_known(map);
7581 if (known < 0) {
7582 isl_map_free(map);
7583 return NULL((void*)0);
7584 }
7585 if (known)
7586 return map;
7587
7588 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
7589 for (i = 1 ; i < map->n; ++i) {
7590 struct isl_map *r2;
7591 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
7592 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT)(!!(((map)->flags) & ((1 << 0)))))
7593 res = isl_map_union_disjoint(res, r2);
7594 else
7595 res = isl_map_union(res, r2);
7596 }
7597 isl_map_free(map);
7598
7599 return res;
7600}
7601
7602__isl_give isl_setisl_map *isl_basic_set_compute_divs(__isl_take isl_basic_setisl_basic_map *bset)
7603{
7604 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset)));
7605}
7606
7607struct isl_setisl_map *isl_set_compute_divs(struct isl_setisl_map *set)
7608{
7609 return set_from_map(isl_map_compute_divs(set_to_map(set)));
7610}
7611
7612__isl_give isl_setisl_map *isl_map_domain(__isl_take isl_map *map)
7613{
7614 int i;
7615 struct isl_setisl_map *set;
7616
7617 if (!map)
7618 goto error;
7619
7620 map = isl_map_cow(map);
7621 if (!map)
7622 return NULL((void*)0);
7623
7624 set = set_from_map(map);
7625 set->dim = isl_space_domain(set->dim);
7626 if (!set->dim)
7627 goto error;
7628 for (i = 0; i < map->n; ++i) {
7629 set->p[i] = isl_basic_map_domain(map->p[i]);
7630 if (!set->p[i])
7631 goto error;
7632 }
7633 ISL_F_CLR(set, ISL_MAP_DISJOINT)(((set)->flags) &= ~((1 << 0)));
7634 ISL_F_CLR(set, ISL_SET_NORMALIZED)(((set)->flags) &= ~((1 << 1)));
7635 return set;
7636error:
7637 isl_map_free(map);
7638 return NULL((void*)0);
7639}
7640
7641/* Return the union of "map1" and "map2", where we assume for now that
7642 * "map1" and "map2" are disjoint. Note that the basic maps inside
7643 * "map1" or "map2" may not be disjoint from each other.
7644 * Also note that this function is also called from isl_map_union,
7645 * which takes care of handling the situation where "map1" and "map2"
7646 * may not be disjoint.
7647 *
7648 * If one of the inputs is empty, we can simply return the other input.
7649 * Similarly, if one of the inputs is universal, then it is equal to the union.
7650 */
7651static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
7652 __isl_take isl_map *map2)
7653{
7654 int i;
7655 unsigned flags = 0;
7656 struct isl_map *map = NULL((void*)0);
7657 int is_universe;
7658
7659 if (!map1 || !map2)
7660 goto error;
7661
7662 if (!isl_space_is_equal(map1->dim, map2->dim))
7663 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", "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 7664); goto error; } while (0)
7664 "spaces don't match", goto error)do { isl_handle_error(isl_map_get_ctx(map1), isl_error_invalid
, "spaces don't match", "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 7664); goto error; } while (0)
;
7665
7666 if (map1->n == 0) {
7667 isl_map_free(map1);
7668 return map2;
7669 }
7670 if (map2->n == 0) {
7671 isl_map_free(map2);
7672 return map1;
7673 }
7674
7675 is_universe = isl_map_plain_is_universe(map1);
7676 if (is_universe < 0)
7677 goto error;
7678 if (is_universe) {
7679 isl_map_free(map2);
7680 return map1;
7681 }
7682
7683 is_universe = isl_map_plain_is_universe(map2);
7684 if (is_universe < 0)
7685 goto error;
7686 if (is_universe) {
7687 isl_map_free(map1);
7688 return map2;
7689 }
7690
7691 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT)(!!(((map1)->flags) & ((1 << 0)))) &&
7692 ISL_F_ISSET(map2, ISL_MAP_DISJOINT)(!!(((map2)->flags) & ((1 << 0)))))
7693 ISL_FL_SET(flags, ISL_MAP_DISJOINT)((flags) |= ((1 << 0)));
7694
7695 map = isl_map_alloc_space(isl_space_copy(map1->dim),
7696 map1->n + map2->n, flags);
7697 if (!map)
7698 goto error;
7699 for (i = 0; i < map1->n; ++i) {
7700 map = isl_map_add_basic_map(map,
7701 isl_basic_map_copy(map1->p[i]));
7702 if (!map)
7703 goto error;
7704 }
7705 for (i = 0; i < map2->n; ++i) {
7706 map = isl_map_add_basic_map(map,
7707 isl_basic_map_copy(map2->p[i]));
7708 if (!map)
7709 goto error;
7710 }
7711 isl_map_free(map1);
7712 isl_map_free(map2);
7713 return map;
7714error:
7715 isl_map_free(map);
7716 isl_map_free(map1);
7717 isl_map_free(map2);
7718 return NULL((void*)0);
7719}
7720
7721/* Return the union of "map1" and "map2", where "map1" and "map2" are
7722 * guaranteed to be disjoint by the caller.
7723 *
7724 * Note that this functions is called from within isl_map_make_disjoint,
7725 * so we have to be careful not to touch the constraints of the inputs
7726 * in any way.
7727 */
7728__isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
7729 __isl_take isl_map *map2)
7730{
7731 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
7732}
7733
7734/* Return the union of "map1" and "map2", where "map1" and "map2" may
7735 * not be disjoint. The parameters are assumed to have been aligned.
7736 *
7737 * We currently simply call map_union_disjoint, the internal operation
7738 * of which does not really depend on the inputs being disjoint.
7739 * If the result contains more than one basic map, then we clear
7740 * the disjoint flag since the result may contain basic maps from
7741 * both inputs and these are not guaranteed to be disjoint.
7742 *
7743 * As a special case, if "map1" and "map2" are obviously equal,
7744 * then we simply return "map1".
7745 */
7746static __isl_give isl_map *map_union_aligned(__isl_take isl_map *map1,
7747 __isl_take isl_map *map2)
7748{
7749 int equal;
7750
7751 if (!map1 || !map2)
7752 goto error;
7753
7754 equal = isl_map_plain_is_equal(map1, map2);
7755 if (equal < 0)
7756 goto error;
7757 if (equal) {
7758 isl_map_free(map2);
7759 return map1;
7760 }
7761
7762 map1 = map_union_disjoint(map1, map2);
7763 if (!map1)
7764 return NULL((void*)0);
7765 if (map1->n > 1)
7766 ISL_F_CLR(map1, ISL_MAP_DISJOINT)(((map1)->flags) &= ~((1 << 0)));
7767 return map1;
7768error:
7769 isl_map_free(map1);
7770 isl_map_free(map2);
7771 return NULL((void*)0);
7772}
7773
7774/* Return the union of "map1" and "map2", where "map1" and "map2" may
7775 * not be disjoint.
7776 */
7777__isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
7778 __isl_take isl_map *map2)
7779{
7780 return isl_map_align_params_map_map_and(map1, map2, &map_union_aligned);
7781}
7782
7783__isl_give isl_setisl_map *isl_set_union_disjoint(
7784 __isl_take isl_setisl_map *set1, __isl_take isl_setisl_map *set2)
7785{
7786 return set_from_map(isl_map_union_disjoint(set_to_map(set1),
7787 set_to_map(set2)));
7788}
7789
7790struct isl_setisl_map *isl_set_union(struct isl_setisl_map *set1, struct isl_setisl_map *set2)
7791{
7792 return set_from_map(isl_map_union(set_to_map(set1), set_to_map(set2)));
7793}
7794
7795/* Apply "fn" to pairs of elements from "map" and "set" and collect
7796 * the results.
7797 *
7798 * "map" and "set" are assumed to be compatible and non-NULL.
7799 */
7800static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
7801 __isl_take isl_setisl_map *set,
7802 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
7803 __isl_take isl_basic_setisl_basic_map *bset))
7804{
7805 unsigned flags = 0;
7806 struct isl_map *result;
7807 int i, j;
7808
7809 if (isl_set_plain_is_universe(set)) {
7810 isl_set_free(set);
7811 return map;
7812 }
7813
7814 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT)(!!(((map)->flags) & ((1 << 0)))) &&
7815 ISL_F_ISSET(set, ISL_MAP_DISJOINT)(!!(((set)->flags) & ((1 << 0)))))
7816 ISL_FL_SET(flags, ISL_MAP_DISJOINT)((flags) |= ((1 << 0)));
7817
7818 result = isl_map_alloc_space(isl_space_copy(map->dim),
7819 map->n * set->n, flags);
7820 for (i = 0; result && i < map->n; ++i)
7821 for (j = 0; j < set->n; ++j) {
7822 result = isl_map_add_basic_map(result,
7823 fn(isl_basic_map_copy(map->p[i]),
7824 isl_basic_set_copy(set->p[j])));
7825 if (!result)
7826 break;
7827 }
7828
7829 isl_map_free(map);
7830 isl_set_free(set);
7831 return result;
7832}
7833
7834static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
7835 __isl_take isl_setisl_map *set)
7836{
7837 isl_bool ok;
7838
7839 ok = isl_map_compatible_range(map, set);
7840 if (ok < 0)
7841 goto error;
7842 if (!ok)
7843 isl_die(set->ctx, isl_error_invalid,do { isl_handle_error(set->ctx, isl_error_invalid, "incompatible spaces"
, "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 7844); goto error; } while (0)
7844 "incompatible spaces", goto error)do { isl_handle_error(set->ctx, isl_error_invalid, "incompatible spaces"
, "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 7844); goto error; } while (0)
;
7845
7846 return map_intersect_set(map, set, &isl_basic_map_intersect_range);
7847error:
7848 isl_map_free(map);
7849 isl_set_free(set);
7850 return NULL((void*)0);
7851}
7852
7853__isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
7854 __isl_take isl_setisl_map *set)
7855{
7856 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
7857}
7858
7859static __isl_give isl_map *map_intersect_domain(__isl_take isl_map *map,
7860 __isl_take isl_setisl_map *set)
7861{
7862 isl_bool ok;
7863
7864 ok = isl_map_compatible_domain(map, set);
7865 if (ok < 0)
7866 goto error;
7867 if (!ok)
7868 isl_die(set->ctx, isl_error_invalid,do { isl_handle_error(set->ctx, isl_error_invalid, "incompatible spaces"
, "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 7869); goto error; } while (0)
7869 "incompatible spaces", goto error)do { isl_handle_error(set->ctx, isl_error_invalid, "incompatible spaces"
, "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 7869); goto error; } while (0)
;
7870
7871 return map_intersect_set(map, set, &isl_basic_map_intersect_domain);
7872error:
7873 isl_map_free(map);
7874 isl_set_free(set);
7875 return NULL((void*)0);
7876}
7877
7878__isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
7879 __isl_take isl_setisl_map *set)
7880{
7881 return isl_map_align_params_map_map_and(map, set,
7882 &map_intersect_domain);
7883}
7884
7885/* Given a map "map" in a space [A -> B] -> C and a map "factor"
7886 * in the space B -> C, return the intersection.
7887 * The parameters are assumed to have been aligned.
7888 *
7889 * The map "factor" is first extended to a map living in the space
7890 * [A -> B] -> C and then a regular intersection is computed.
7891 */
7892static __isl_give isl_map *map_intersect_domain_factor_range(
7893 __isl_take isl_map *map, __isl_take isl_map *factor)
7894{
7895 isl_space *space;
7896 isl_map *ext_factor;
7897
7898 space = isl_space_domain_factor_domain(isl_map_get_space(map));
7899 ext_factor = isl_map_universe(space);
7900 ext_factor = isl_map_domain_product(ext_factor, factor);
7901 return map_intersect(map, ext_factor);
7902}
7903
7904/* Given a map "map" in a space [A -> B] -> C and a map "factor"
7905 * in the space B -> C, return the intersection.
7906 */
7907__isl_give isl_map *isl_map_intersect_domain_factor_range(
7908 __isl_take isl_map *map, __isl_take isl_map *factor)
7909{
7910 return isl_map_align_params_map_map_and(map, factor,
7911 &map_intersect_domain_factor_range);
7912}
7913
7914/* Given a map "map" in a space A -> [B -> C] and a map "factor"
7915 * in the space A -> C, return the intersection.
7916 *
7917 * The map "factor" is first extended to a map living in the space
7918 * A -> [B -> C] and then a regular intersection is computed.
7919 */
7920static __isl_give isl_map *map_intersect_range_factor_range(
7921 __isl_take isl_map *map, __isl_take isl_map *factor)
7922{
7923 isl_space *space;
7924 isl_map *ext_factor;
7925
7926 space = isl_space_range_factor_domain(isl_map_get_space(map));
7927 ext_factor = isl_map_universe(space);
7928 ext_factor = isl_map_range_product(ext_factor, factor);
7929 return isl_map_intersect(map, ext_factor);
7930}
7931
7932/* Given a map "map" in a space A -> [B -> C] and a map "factor"
7933 * in the space A -> C, return the intersection.
7934 */
7935__isl_give isl_map *isl_map_intersect_range_factor_range(
7936 __isl_take isl_map *map, __isl_take isl_map *factor)
7937{
7938 return isl_map_align_params_map_map_and(map, factor,
7939 &map_intersect_range_factor_range);
7940}
7941
7942static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
7943 __isl_take isl_map *map2)
7944{
7945 if (!map1 || !map2)
7946 goto error;
7947 map1 = isl_map_reverse(map1);
7948 map1 = isl_map_apply_range(map1, map2);
7949 return isl_map_reverse(map1);
7950error:
7951 isl_map_free(map1);
7952 isl_map_free(map2);
7953 return NULL((void*)0);
7954}
7955
7956__isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
7957 __isl_take isl_map *map2)
7958{
7959 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
7960}
7961
7962static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
7963 __isl_take isl_map *map2)
7964{
7965 isl_space *dim_result;
7966 struct isl_map *result;
7967 int i, j;
7968
7969 if (!map1 || !map2)
7970 goto error;
7971
7972 dim_result = isl_space_join(isl_space_copy(map1->dim),
7973 isl_space_copy(map2->dim));
7974
7975 result = isl_map_alloc_space(dim_result, map1->n * map2->n, 0);
7976 if (!result)
7977 goto error;
7978 for (i = 0; i < map1->n; ++i)
7979 for (j = 0; j < map2->n; ++j) {
7980 result = isl_map_add_basic_map(result,
7981 isl_basic_map_apply_range(
7982 isl_basic_map_copy(map1->p[i]),
7983 isl_basic_map_copy(map2->p[j])));
7984 if (!result)
7985 goto error;
7986 }
7987 isl_map_free(map1);
7988 isl_map_free(map2);
7989 if (result && result->n <= 1)
7990 ISL_F_SET(result, ISL_MAP_DISJOINT)(((result)->flags) |= ((1 << 0)));
7991 return result;
7992error:
7993 isl_map_free(map1);
7994 isl_map_free(map2);
7995 return NULL((void*)0);
7996}
7997
7998__isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
7999 __isl_take isl_map *map2)
8000{
8001 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
8002}
8003
8004/*
8005 * returns range - domain
8006 */
8007__isl_give isl_basic_setisl_basic_map *isl_basic_map_deltas(__isl_take isl_basic_map *bmap)
8008{
8009 isl_space *target_space;
8010 struct isl_basic_setisl_basic_map *bset;
8011 unsigned dim;
8012 unsigned nparam;
8013 int i;
8014
8015 if (!bmap)
8016 goto error;
8017 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", "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 8019); goto error; } while (0); } while (0)
8018 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", "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 8019); goto error; } while (0); } while (0)
8019 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", "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 8019); goto error; } while (0); } while (0)
;
8020 target_space = isl_space_domain(isl_basic_map_get_space(bmap));
8021 dim = isl_basic_map_dim(bmap, isl_dim_in);
8022 nparam = isl_basic_map_dim(bmap, isl_dim_param);
8023 bmap = isl_basic_map_from_range(isl_basic_map_wrap(bmap));
8024 bmap = isl_basic_map_add_dims(bmap, isl_dim_in, dim);
8025 bmap = isl_basic_map_extend_constraints(bmap, dim, 0);
8026 for (i = 0; i < dim; ++i) {
8027 int j = isl_basic_map_alloc_equality(bmap);
8028 if (j < 0) {
8029 bmap = isl_basic_map_free(bmap);
8030 break;
8031 }
8032 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
8033 isl_int_set_si(bmap->eq[j][1+nparam+i], 1)isl_sioimath_set_si((bmap->eq[j][1+nparam+i]), 1);
8034 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], 1)isl_sioimath_set_si((bmap->eq[j][1+nparam+dim+i]), 1);
8035 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);
8036 }
8037 bset = isl_basic_map_domain(bmap);
8038 bset = isl_basic_set_reset_space(bset, target_space);
8039 return bset;
8040error:
8041 isl_basic_map_free(bmap);
8042 return NULL((void*)0);
8043}
8044
8045/*
8046 * returns range - domain
8047 */
8048__isl_give isl_setisl_map *isl_map_deltas(__isl_take isl_map *map)
8049{
8050 int i;
8051 isl_space *dim;
8052 struct isl_setisl_map *result;
8053
8054 if (!map)
8055 return NULL((void*)0);
8056
8057 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", "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 8059); goto error; } while (0); } while (0)
8058 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", "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 8059); goto error; } while (0); } while (0)
8059 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", "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 8059); goto error; } while (0); } while (0)
;
8060 dim = isl_map_get_space(map);
8061 dim = isl_space_domain(dim);
8062 result = isl_set_alloc_space(dim, map->n, 0);
8063 if (!result)
8064 goto error;
8065 for (i = 0; i < map->n; ++i)
8066 result = isl_set_add_basic_set(result,
8067 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
8068 isl_map_free(map);
8069 return result;
8070error:
8071 isl_map_free(map);
8072 return NULL((void*)0);
8073}
8074
8075/*
8076 * returns [domain -> range] -> range - domain
8077 */
8078__isl_give isl_basic_map *isl_basic_map_deltas_map(
8079 __isl_take isl_basic_map *bmap)
8080{
8081 int i, k;
8082 isl_space *dim;
8083 isl_basic_map *domain;
8084 int nparam, n;
8085 unsigned total;
8086
8087 if (!isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
8088 bmap->dim, isl_dim_out))
8089 isl_die(bmap->ctx, isl_error_invalid,do { isl_handle_error(bmap->ctx, isl_error_invalid, "domain and range don't match"
, "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 8090); goto error; } while (0)
8090 "domain and range don't match", goto error)do { isl_handle_error(bmap->ctx, isl_error_invalid, "domain and range don't match"
, "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 8090); goto error; } while (0)
;
8091
8092 nparam = isl_basic_map_dim(bmap, isl_dim_param);
8093 n = isl_basic_map_dim(bmap, isl_dim_in);
8094
8095 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
8096 domain = isl_basic_map_universe(dim);
8097
8098 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
8099 bmap = isl_basic_map_apply_range(bmap, domain);
8100 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
8101
8102 total = isl_basic_map_total_dim(bmap);
8103
8104 for (i = 0; i < n; ++i) {
8105 k = isl_basic_map_alloc_equality(bmap);
8106 if (k < 0)
8107 goto error;
8108 isl_seq_clr(bmap->eq[k], 1 + total);
8109 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1)isl_sioimath_set_si((bmap->eq[k][1 + nparam + i]), 1);
8110 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1)isl_sioimath_set_si((bmap->eq[k][1 + nparam + n + i]), -1);
8111 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)
;
8112 }
8113
8114 bmap = isl_basic_map_gauss(bmap, NULL((void*)0));
8115 return isl_basic_map_finalize(bmap);
8116error:
8117 isl_basic_map_free(bmap);
8118 return NULL((void*)0);
8119}
8120
8121/*
8122 * returns [domain -> range] -> range - domain
8123 */
8124__isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
8125{
8126 int i;
8127 isl_space *domain_dim;
8128
8129 if (!map)
8130 return NULL((void*)0);
8131
8132 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
8133 map->dim, isl_dim_out))
8134 isl_die(map->ctx, isl_error_invalid,do { isl_handle_error(map->ctx, isl_error_invalid, "domain and range don't match"
, "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 8135); goto error; } while (0)
8135 "domain and range don't match", goto error)do { isl_handle_error(map->ctx, isl_error_invalid, "domain and range don't match"
, "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 8135); goto error; } while (0)
;
8136
8137 map = isl_map_cow(map);
8138 if (!map)
8139 return NULL((void*)0);
8140
8141 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
8142 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
8143 map->dim = isl_space_join(map->dim, domain_dim);
8144 if (!map->dim)
8145 goto error;
8146 for (i = 0; i < map->n; ++i) {
8147 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
8148 if (!map->p[i])
8149 goto error;
8150 }
8151 ISL_F_CLR(map, ISL_MAP_NORMALIZED)(((map)->flags) &= ~((1 << 1)));
8152 return map;
8153error:
8154 isl_map_free(map);
8155 return NULL((void*)0);
8156}
8157
8158static __isl_give isl_basic_map *basic_map_identity(__isl_take isl_space *dims)
8159{
8160 struct isl_basic_map *bmap;
8161 unsigned nparam;
8162 unsigned dim;
8163 int i;
8164
8165 if (!dims)
8166 return NULL((void*)0);
8167
8168 nparam = dims->nparam;
8169 dim = dims->n_out;
8170 bmap = isl_basic_map_alloc_space(dims, 0, dim, 0);
8171 if (!bmap)
8172 goto error;
8173
8174 for (i = 0; i < dim; ++i) {
8175 int j = isl_basic_map_alloc_equality(bmap);
8176 if (j < 0)
8177 goto error;
8178 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
8179 isl_int_set_si(bmap->eq[j][1+nparam+i], 1)isl_sioimath_set_si((bmap->eq[j][1+nparam+i]), 1);
8180 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1)isl_sioimath_set_si((bmap->eq[j][1+nparam+dim+i]), -1);
8181 }
8182 return isl_basic_map_finalize(bmap);
8183error:
8184 isl_basic_map_free(bmap);
8185 return NULL((void*)0);
8186}
8187
8188__isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *dim)
8189{
8190 if (!dim)
8191 return NULL((void*)0);
8192 if (dim->n_in != dim->n_out)
8193 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", "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 8195); goto error; } while (0)
8194 "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", "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 8195); goto error; } while (0)
8195 "the same", goto error)do { isl_handle_error(dim->ctx, isl_error_invalid, "number of input and output dimensions needs to be "
"the same", "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 8195); goto error; } while (0)
;
8196 return basic_map_identity(dim);
8197error:
8198 isl_space_free(dim);
8199 return NULL((void*)0);
8200}
8201
8202__isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
8203{
8204 return isl_map_from_basic_map(isl_basic_map_identity(dim));
8205}
8206
8207__isl_give isl_map *isl_set_identity(__isl_take isl_setisl_map *set)
8208{
8209 isl_space *dim = isl_set_get_space(set);
8210 isl_map *id;
8211 id = isl_map_identity(isl_space_map_from_set(dim));
8212 return isl_map_intersect_range(id, set);
8213}
8214
8215/* Construct a basic set with all set dimensions having only non-negative
8216 * values.
8217 */
8218__isl_give isl_basic_setisl_basic_map *isl_basic_set_positive_orthant(
8219 __isl_take isl_space *space)
8220{
8221 int i;
8222 unsigned nparam;
8223 unsigned dim;
8224 struct isl_basic_setisl_basic_map *bset;
8225
8226 if (!space)
8227 return NULL((void*)0);
8228 nparam = space->nparam;
8229 dim = space->n_out;
8230 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
8231 if (!bset)
8232 return NULL((void*)0);
8233 for (i = 0; i < dim; ++i) {
8234 int k = isl_basic_set_alloc_inequality(bset);
8235 if (k < 0)
8236 goto error;
8237 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
8238 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1)isl_sioimath_set_si((bset->ineq[k][1 + nparam + i]), 1);
8239 }
8240 return bset;
8241error:
8242 isl_basic_set_free(bset);
8243 return NULL((void*)0);
8244}
8245
8246/* Construct the half-space x_pos >= 0.
8247 */
8248static __isl_give isl_basic_setisl_basic_map *nonneg_halfspace(__isl_take isl_space *dim,
8249 int pos)
8250{
8251 int k;
8252 isl_basic_setisl_basic_map *nonneg;
8253
8254 nonneg = isl_basic_set_alloc_space(dim, 0, 0, 1);
8255 k = isl_basic_set_alloc_inequality(nonneg);
8256 if (k < 0)
8257 goto error;
8258 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
8259 isl_int_set_si(nonneg->ineq[k][pos], 1)isl_sioimath_set_si((nonneg->ineq[k][pos]), 1);
8260
8261 return isl_basic_set_finalize(nonneg);
8262error:
8263 isl_basic_set_free(nonneg);
8264 return NULL((void*)0);
8265}
8266
8267/* Construct the half-space x_pos <= -1.
8268 */
8269static __isl_give isl_basic_setisl_basic_map *neg_halfspace(__isl_take isl_space *dim, int pos)
8270{
8271 int k;
8272 isl_basic_setisl_basic_map *neg;
8273
8274 neg = isl_basic_set_alloc_space(dim, 0, 0, 1);
8275 k = isl_basic_set_alloc_inequality(neg);
8276 if (k < 0)
8277 goto error;
8278 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
8279 isl_int_set_si(neg->ineq[k][0], -1)isl_sioimath_set_si((neg->ineq[k][0]), -1);
8280 isl_int_set_si(neg->ineq[k][pos], -1)isl_sioimath_set_si((neg->ineq[k][pos]), -1);
8281
8282 return isl_basic_set_finalize(neg);
8283error:
8284 isl_basic_set_free(neg);
8285 return NULL((void*)0);
8286}
8287
8288__isl_give isl_setisl_map *isl_set_split_dims(__isl_take isl_setisl_map *set,
8289 enum isl_dim_type type, unsigned first, unsigned n)
8290{
8291 int i;
8292 unsigned offset;
8293 isl_basic_setisl_basic_map *nonneg;
8294 isl_basic_setisl_basic_map *neg;
8295
8296 if (!set)
8297 return NULL((void*)0);
8298 if (n == 0)
8299 return set;
8300
8301 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", "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/polly/lib/External/isl/isl_map.c"
, 8301); goto error; } while (0); } while (0)
;
8302
8303 offset = pos(set->dim, type);
8304 for (i = 0; i < n; ++i) {
8305 nonneg = nonneg_halfspace(isl_set_get_space(set),
8306 offset + first + i);
8307 neg = neg_halfspace(isl_set_get_space(set), offset + first + i);
8308
8309 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
8310 }
8311
8312 return set;
8313error:
8314 isl_set_free(set);
8315 return NULL((void*)0);
8316}
8317
8318static isl_stat foreach_orthant(__isl_take isl_setisl_map *set, int *signs, int first,
8319 int len,
8320 isl_stat (*fn)(__isl_take isl_setisl_map *orthant, int *signs, void *user),
8321 void *user)
8322{
8323 isl_setisl_map *half;
8324
8325 if (!set)
8326 return isl_stat_error;
8327 if (isl_set_plain_is_empty(set)) {
8328 isl_set_free(set);
8329 return isl_stat_ok;
8330 }
8331 if (first == len)
8332 return fn(set, signs, user);
8333
8334 signs[first] = 1;
8335 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
8336 1 + first));
8337 half = isl_set_intersect(half, isl_set_copy(set));
8338 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
8339 goto error;
8340
8341 signs[first] = -1;
8342 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
8343 1 + first));
8344 half = isl_set_intersect(half, set);
8345 return foreach_orthant(half, signs, first + 1, len, fn, user);
8346error:
8347 isl_set_free(set);
8348 return isl_stat_error;
8349}
8350
8351/* Call "fn" on the intersections of "set" with each of the orthants
8352 * (except for obviously empty intersections). The orthant is identified
8353 * by the signs array, with each entry having value 1 or -1 according
8354 * to the sign of the corresponding variable.
8355 */
8356isl_stat isl_set_foreach_orthant(__isl_keep isl_setisl_map *set,
8357 isl_stat (*fn)(__isl_take isl_setisl_map *orthant, int *signs, void *user),
8358 void *user)
8359{
8360 unsigned nparam;
8361 unsigned nvar;
8362 int *signs;
8363 isl_stat r;
8364
8365 if (!set)
8366 return isl_stat_error;
8367 if (isl_set_plain_is_empty(set))
8368 return isl_stat_ok;
8369
8370 nparam = isl_set_dim(set, isl_dim_param);
8371 nvar = isl_set_dim(set, isl_dim_set);
8372
8373 signs = isl_alloc_array(set->ctx, int, nparam + nvar)((int *)isl_malloc_or_die(set->ctx, (nparam + nvar)*sizeof
(int)))
;
8374
8375 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
8376 fn, user);
8377
8378 free(signs);
8379
8380 return r;
8381}
8382
8383isl_bool isl_set_is_equal(__isl_keep isl_setisl_map *set1, __isl_keep isl_setisl_map *set2)
8384{
8385 return isl_map_is_equal(set_to_map(set1), set_to_map(set2));
8386}
8387
8388isl_bool isl_basic_map_is_subset(__isl_keep isl_basic_map *bmap1,
8389 __isl_keep isl_basic_map *bmap2)
8390