Bug Summary

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

Annotated Source Code

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