Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 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 |
Michael Kruse | e0b34f3 | 2016-05-04 14:41:36 +0000 | [diff] [blame] | 6 | * Copyright 2016 Sven Verdoolaege |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7 | * |
| 8 | * Use of this software is governed by the MIT license |
| 9 | * |
| 10 | * Written by Sven Verdoolaege, K.U.Leuven, Departement |
| 11 | * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium |
| 12 | * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite, |
| 13 | * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France |
| 14 | * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France |
| 15 | * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt, |
| 16 | * B.P. 105 - 78153 Le Chesnay, France |
| 17 | */ |
| 18 | |
| 19 | #include <string.h> |
| 20 | #include <isl_ctx_private.h> |
| 21 | #include <isl_map_private.h> |
| 22 | #include <isl_blk.h> |
Tobias Grosser | 3e6070e | 2015-05-09 09:37:30 +0000 | [diff] [blame] | 23 | #include <isl/constraint.h> |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 24 | #include "isl_space_private.h" |
| 25 | #include "isl_equalities.h" |
| 26 | #include <isl_lp_private.h> |
| 27 | #include <isl_seq.h> |
| 28 | #include <isl/set.h> |
| 29 | #include <isl/map.h> |
| 30 | #include <isl_reordering.h> |
| 31 | #include "isl_sample.h" |
| 32 | #include <isl_sort.h> |
| 33 | #include "isl_tab.h" |
| 34 | #include <isl/vec.h> |
| 35 | #include <isl_mat_private.h> |
| 36 | #include <isl_vec_private.h> |
| 37 | #include <isl_dim_map.h> |
| 38 | #include <isl_local_space_private.h> |
| 39 | #include <isl_aff_private.h> |
| 40 | #include <isl_options_private.h> |
| 41 | #include <isl_morph.h> |
| 42 | #include <isl_val_private.h> |
| 43 | #include <isl/deprecated/map_int.h> |
| 44 | #include <isl/deprecated/set_int.h> |
| 45 | |
| 46 | static unsigned n(__isl_keep isl_space *dim, enum isl_dim_type type) |
| 47 | { |
| 48 | switch (type) { |
| 49 | case isl_dim_param: return dim->nparam; |
| 50 | case isl_dim_in: return dim->n_in; |
| 51 | case isl_dim_out: return dim->n_out; |
| 52 | case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out; |
| 53 | default: return 0; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | static unsigned pos(__isl_keep isl_space *dim, enum isl_dim_type type) |
| 58 | { |
| 59 | switch (type) { |
| 60 | case isl_dim_param: return 1; |
| 61 | case isl_dim_in: return 1 + dim->nparam; |
| 62 | case isl_dim_out: return 1 + dim->nparam + dim->n_in; |
| 63 | default: return 0; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | unsigned isl_basic_map_dim(__isl_keep isl_basic_map *bmap, |
| 68 | enum isl_dim_type type) |
| 69 | { |
| 70 | if (!bmap) |
| 71 | return 0; |
| 72 | switch (type) { |
| 73 | case isl_dim_cst: return 1; |
| 74 | case isl_dim_param: |
| 75 | case isl_dim_in: |
| 76 | case isl_dim_out: return isl_space_dim(bmap->dim, type); |
| 77 | case isl_dim_div: return bmap->n_div; |
| 78 | case isl_dim_all: return isl_basic_map_total_dim(bmap); |
| 79 | default: return 0; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | unsigned isl_map_dim(__isl_keep isl_map *map, enum isl_dim_type type) |
| 84 | { |
| 85 | return map ? n(map->dim, type) : 0; |
| 86 | } |
| 87 | |
| 88 | unsigned isl_set_dim(__isl_keep isl_set *set, enum isl_dim_type type) |
| 89 | { |
| 90 | return set ? n(set->dim, type) : 0; |
| 91 | } |
| 92 | |
| 93 | unsigned isl_basic_map_offset(struct isl_basic_map *bmap, |
| 94 | enum isl_dim_type type) |
| 95 | { |
| 96 | isl_space *dim = bmap->dim; |
| 97 | switch (type) { |
| 98 | case isl_dim_cst: return 0; |
| 99 | case isl_dim_param: return 1; |
| 100 | case isl_dim_in: return 1 + dim->nparam; |
| 101 | case isl_dim_out: return 1 + dim->nparam + dim->n_in; |
| 102 | case isl_dim_div: return 1 + dim->nparam + dim->n_in + dim->n_out; |
| 103 | default: return 0; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | unsigned isl_basic_set_offset(struct isl_basic_set *bset, |
| 108 | enum isl_dim_type type) |
| 109 | { |
| 110 | return isl_basic_map_offset(bset, type); |
| 111 | } |
| 112 | |
| 113 | static unsigned map_offset(struct isl_map *map, enum isl_dim_type type) |
| 114 | { |
| 115 | return pos(map->dim, type); |
| 116 | } |
| 117 | |
| 118 | unsigned isl_basic_set_dim(__isl_keep isl_basic_set *bset, |
| 119 | enum isl_dim_type type) |
| 120 | { |
| 121 | return isl_basic_map_dim(bset, type); |
| 122 | } |
| 123 | |
| 124 | unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set *bset) |
| 125 | { |
| 126 | return isl_basic_set_dim(bset, isl_dim_set); |
| 127 | } |
| 128 | |
| 129 | unsigned isl_basic_set_n_param(__isl_keep isl_basic_set *bset) |
| 130 | { |
| 131 | return isl_basic_set_dim(bset, isl_dim_param); |
| 132 | } |
| 133 | |
| 134 | unsigned isl_basic_set_total_dim(const struct isl_basic_set *bset) |
| 135 | { |
| 136 | if (!bset) |
| 137 | return 0; |
| 138 | return isl_space_dim(bset->dim, isl_dim_all) + bset->n_div; |
| 139 | } |
| 140 | |
| 141 | unsigned isl_set_n_dim(__isl_keep isl_set *set) |
| 142 | { |
| 143 | return isl_set_dim(set, isl_dim_set); |
| 144 | } |
| 145 | |
| 146 | unsigned isl_set_n_param(__isl_keep isl_set *set) |
| 147 | { |
| 148 | return isl_set_dim(set, isl_dim_param); |
| 149 | } |
| 150 | |
| 151 | unsigned isl_basic_map_n_in(const struct isl_basic_map *bmap) |
| 152 | { |
| 153 | return bmap ? bmap->dim->n_in : 0; |
| 154 | } |
| 155 | |
| 156 | unsigned isl_basic_map_n_out(const struct isl_basic_map *bmap) |
| 157 | { |
| 158 | return bmap ? bmap->dim->n_out : 0; |
| 159 | } |
| 160 | |
| 161 | unsigned isl_basic_map_n_param(const struct isl_basic_map *bmap) |
| 162 | { |
| 163 | return bmap ? bmap->dim->nparam : 0; |
| 164 | } |
| 165 | |
| 166 | unsigned isl_basic_map_n_div(const struct isl_basic_map *bmap) |
| 167 | { |
| 168 | return bmap ? bmap->n_div : 0; |
| 169 | } |
| 170 | |
| 171 | unsigned isl_basic_map_total_dim(const struct isl_basic_map *bmap) |
| 172 | { |
| 173 | return bmap ? isl_space_dim(bmap->dim, isl_dim_all) + bmap->n_div : 0; |
| 174 | } |
| 175 | |
| 176 | unsigned isl_map_n_in(const struct isl_map *map) |
| 177 | { |
| 178 | return map ? map->dim->n_in : 0; |
| 179 | } |
| 180 | |
| 181 | unsigned isl_map_n_out(const struct isl_map *map) |
| 182 | { |
| 183 | return map ? map->dim->n_out : 0; |
| 184 | } |
| 185 | |
| 186 | unsigned isl_map_n_param(const struct isl_map *map) |
| 187 | { |
| 188 | return map ? map->dim->nparam : 0; |
| 189 | } |
| 190 | |
| 191 | int isl_map_compatible_domain(struct isl_map *map, struct isl_set *set) |
| 192 | { |
| 193 | int m; |
| 194 | if (!map || !set) |
| 195 | return -1; |
| 196 | m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param); |
| 197 | if (m < 0 || !m) |
| 198 | return m; |
| 199 | return isl_space_tuple_is_equal(map->dim, isl_dim_in, |
| 200 | set->dim, isl_dim_set); |
| 201 | } |
| 202 | |
| 203 | int isl_basic_map_compatible_domain(struct isl_basic_map *bmap, |
| 204 | struct isl_basic_set *bset) |
| 205 | { |
| 206 | int m; |
| 207 | if (!bmap || !bset) |
| 208 | return -1; |
| 209 | m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param); |
| 210 | if (m < 0 || !m) |
| 211 | return m; |
| 212 | return isl_space_tuple_is_equal(bmap->dim, isl_dim_in, |
| 213 | bset->dim, isl_dim_set); |
| 214 | } |
| 215 | |
| 216 | int isl_map_compatible_range(__isl_keep isl_map *map, __isl_keep isl_set *set) |
| 217 | { |
| 218 | int m; |
| 219 | if (!map || !set) |
| 220 | return -1; |
| 221 | m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param); |
| 222 | if (m < 0 || !m) |
| 223 | return m; |
| 224 | return isl_space_tuple_is_equal(map->dim, isl_dim_out, |
| 225 | set->dim, isl_dim_set); |
| 226 | } |
| 227 | |
| 228 | int isl_basic_map_compatible_range(struct isl_basic_map *bmap, |
| 229 | struct isl_basic_set *bset) |
| 230 | { |
| 231 | int m; |
| 232 | if (!bmap || !bset) |
| 233 | return -1; |
| 234 | m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param); |
| 235 | if (m < 0 || !m) |
| 236 | return m; |
| 237 | return isl_space_tuple_is_equal(bmap->dim, isl_dim_out, |
| 238 | bset->dim, isl_dim_set); |
| 239 | } |
| 240 | |
| 241 | isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap) |
| 242 | { |
| 243 | return bmap ? bmap->ctx : NULL; |
| 244 | } |
| 245 | |
| 246 | isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset) |
| 247 | { |
| 248 | return bset ? bset->ctx : NULL; |
| 249 | } |
| 250 | |
| 251 | isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map) |
| 252 | { |
| 253 | return map ? map->ctx : NULL; |
| 254 | } |
| 255 | |
| 256 | isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set) |
| 257 | { |
| 258 | return set ? set->ctx : NULL; |
| 259 | } |
| 260 | |
| 261 | __isl_give isl_space *isl_basic_map_get_space(__isl_keep isl_basic_map *bmap) |
| 262 | { |
| 263 | if (!bmap) |
| 264 | return NULL; |
| 265 | return isl_space_copy(bmap->dim); |
| 266 | } |
| 267 | |
| 268 | __isl_give isl_space *isl_basic_set_get_space(__isl_keep isl_basic_set *bset) |
| 269 | { |
| 270 | if (!bset) |
| 271 | return NULL; |
| 272 | return isl_space_copy(bset->dim); |
| 273 | } |
| 274 | |
| 275 | /* Extract the divs in "bmap" as a matrix. |
| 276 | */ |
| 277 | __isl_give isl_mat *isl_basic_map_get_divs(__isl_keep isl_basic_map *bmap) |
| 278 | { |
| 279 | int i; |
| 280 | isl_ctx *ctx; |
| 281 | isl_mat *div; |
| 282 | unsigned total; |
| 283 | unsigned cols; |
| 284 | |
| 285 | if (!bmap) |
| 286 | return NULL; |
| 287 | |
| 288 | ctx = isl_basic_map_get_ctx(bmap); |
| 289 | total = isl_space_dim(bmap->dim, isl_dim_all); |
| 290 | cols = 1 + 1 + total + bmap->n_div; |
| 291 | div = isl_mat_alloc(ctx, bmap->n_div, cols); |
| 292 | if (!div) |
| 293 | return NULL; |
| 294 | |
| 295 | for (i = 0; i < bmap->n_div; ++i) |
| 296 | isl_seq_cpy(div->row[i], bmap->div[i], cols); |
| 297 | |
| 298 | return div; |
| 299 | } |
| 300 | |
| 301 | /* Extract the divs in "bset" as a matrix. |
| 302 | */ |
| 303 | __isl_give isl_mat *isl_basic_set_get_divs(__isl_keep isl_basic_set *bset) |
| 304 | { |
| 305 | return isl_basic_map_get_divs(bset); |
| 306 | } |
| 307 | |
| 308 | __isl_give isl_local_space *isl_basic_map_get_local_space( |
| 309 | __isl_keep isl_basic_map *bmap) |
| 310 | { |
| 311 | isl_mat *div; |
| 312 | |
| 313 | if (!bmap) |
| 314 | return NULL; |
| 315 | |
| 316 | div = isl_basic_map_get_divs(bmap); |
| 317 | return isl_local_space_alloc_div(isl_space_copy(bmap->dim), div); |
| 318 | } |
| 319 | |
| 320 | __isl_give isl_local_space *isl_basic_set_get_local_space( |
| 321 | __isl_keep isl_basic_set *bset) |
| 322 | { |
| 323 | return isl_basic_map_get_local_space(bset); |
| 324 | } |
| 325 | |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 326 | /* For each known div d = floor(f/m), add the constraints |
| 327 | * |
| 328 | * f - m d >= 0 |
Tobias Grosser | 07b2095 | 2016-06-12 04:30:40 +0000 | [diff] [blame^] | 329 | * -(f-(m-1)) + m d >= 0 |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 330 | * |
| 331 | * Do not finalize the result. |
| 332 | */ |
| 333 | static __isl_give isl_basic_map *add_known_div_constraints( |
| 334 | __isl_take isl_basic_map *bmap) |
| 335 | { |
| 336 | int i; |
| 337 | unsigned n_div; |
| 338 | |
| 339 | if (!bmap) |
| 340 | return NULL; |
| 341 | n_div = isl_basic_map_dim(bmap, isl_dim_div); |
| 342 | if (n_div == 0) |
| 343 | return bmap; |
| 344 | bmap = isl_basic_map_cow(bmap); |
| 345 | bmap = isl_basic_map_extend_constraints(bmap, 0, 2 * n_div); |
| 346 | if (!bmap) |
| 347 | return NULL; |
| 348 | for (i = 0; i < n_div; ++i) { |
| 349 | if (isl_int_is_zero(bmap->div[i][0])) |
| 350 | continue; |
| 351 | if (isl_basic_map_add_div_constraints(bmap, i) < 0) |
| 352 | return isl_basic_map_free(bmap); |
| 353 | } |
| 354 | |
| 355 | return bmap; |
| 356 | } |
| 357 | |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 358 | __isl_give isl_basic_map *isl_basic_map_from_local_space( |
| 359 | __isl_take isl_local_space *ls) |
| 360 | { |
| 361 | int i; |
| 362 | int n_div; |
| 363 | isl_basic_map *bmap; |
| 364 | |
| 365 | if (!ls) |
| 366 | return NULL; |
| 367 | |
| 368 | n_div = isl_local_space_dim(ls, isl_dim_div); |
| 369 | bmap = isl_basic_map_alloc_space(isl_local_space_get_space(ls), |
| 370 | n_div, 0, 2 * n_div); |
| 371 | |
| 372 | for (i = 0; i < n_div; ++i) |
| 373 | if (isl_basic_map_alloc_div(bmap) < 0) |
| 374 | goto error; |
| 375 | |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 376 | for (i = 0; i < n_div; ++i) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 377 | isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col); |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 378 | bmap = add_known_div_constraints(bmap); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 379 | |
| 380 | isl_local_space_free(ls); |
| 381 | return bmap; |
| 382 | error: |
| 383 | isl_local_space_free(ls); |
| 384 | isl_basic_map_free(bmap); |
| 385 | return NULL; |
| 386 | } |
| 387 | |
| 388 | __isl_give isl_basic_set *isl_basic_set_from_local_space( |
| 389 | __isl_take isl_local_space *ls) |
| 390 | { |
| 391 | return isl_basic_map_from_local_space(ls); |
| 392 | } |
| 393 | |
| 394 | __isl_give isl_space *isl_map_get_space(__isl_keep isl_map *map) |
| 395 | { |
| 396 | if (!map) |
| 397 | return NULL; |
| 398 | return isl_space_copy(map->dim); |
| 399 | } |
| 400 | |
| 401 | __isl_give isl_space *isl_set_get_space(__isl_keep isl_set *set) |
| 402 | { |
| 403 | if (!set) |
| 404 | return NULL; |
| 405 | return isl_space_copy(set->dim); |
| 406 | } |
| 407 | |
| 408 | __isl_give isl_basic_map *isl_basic_map_set_tuple_name( |
| 409 | __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s) |
| 410 | { |
| 411 | bmap = isl_basic_map_cow(bmap); |
| 412 | if (!bmap) |
| 413 | return NULL; |
| 414 | bmap->dim = isl_space_set_tuple_name(bmap->dim, type, s); |
| 415 | if (!bmap->dim) |
| 416 | goto error; |
| 417 | bmap = isl_basic_map_finalize(bmap); |
| 418 | return bmap; |
| 419 | error: |
| 420 | isl_basic_map_free(bmap); |
| 421 | return NULL; |
| 422 | } |
| 423 | |
| 424 | __isl_give isl_basic_set *isl_basic_set_set_tuple_name( |
| 425 | __isl_take isl_basic_set *bset, const char *s) |
| 426 | { |
| 427 | return isl_basic_map_set_tuple_name(bset, isl_dim_set, s); |
| 428 | } |
| 429 | |
| 430 | const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap, |
| 431 | enum isl_dim_type type) |
| 432 | { |
| 433 | return bmap ? isl_space_get_tuple_name(bmap->dim, type) : NULL; |
| 434 | } |
| 435 | |
| 436 | __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map, |
| 437 | enum isl_dim_type type, const char *s) |
| 438 | { |
| 439 | int i; |
| 440 | |
| 441 | map = isl_map_cow(map); |
| 442 | if (!map) |
| 443 | return NULL; |
| 444 | |
| 445 | map->dim = isl_space_set_tuple_name(map->dim, type, s); |
| 446 | if (!map->dim) |
| 447 | goto error; |
| 448 | |
| 449 | for (i = 0; i < map->n; ++i) { |
| 450 | map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s); |
| 451 | if (!map->p[i]) |
| 452 | goto error; |
| 453 | } |
| 454 | |
| 455 | return map; |
| 456 | error: |
| 457 | isl_map_free(map); |
| 458 | return NULL; |
| 459 | } |
| 460 | |
| 461 | /* Replace the identifier of the tuple of type "type" by "id". |
| 462 | */ |
| 463 | __isl_give isl_basic_map *isl_basic_map_set_tuple_id( |
| 464 | __isl_take isl_basic_map *bmap, |
| 465 | enum isl_dim_type type, __isl_take isl_id *id) |
| 466 | { |
| 467 | bmap = isl_basic_map_cow(bmap); |
| 468 | if (!bmap) |
| 469 | goto error; |
| 470 | bmap->dim = isl_space_set_tuple_id(bmap->dim, type, id); |
| 471 | if (!bmap->dim) |
| 472 | return isl_basic_map_free(bmap); |
| 473 | bmap = isl_basic_map_finalize(bmap); |
| 474 | return bmap; |
| 475 | error: |
| 476 | isl_id_free(id); |
| 477 | return NULL; |
| 478 | } |
| 479 | |
| 480 | /* Replace the identifier of the tuple by "id". |
| 481 | */ |
| 482 | __isl_give isl_basic_set *isl_basic_set_set_tuple_id( |
| 483 | __isl_take isl_basic_set *bset, __isl_take isl_id *id) |
| 484 | { |
| 485 | return isl_basic_map_set_tuple_id(bset, isl_dim_set, id); |
| 486 | } |
| 487 | |
| 488 | /* Does the input or output tuple have a name? |
| 489 | */ |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 490 | isl_bool isl_map_has_tuple_name(__isl_keep isl_map *map, enum isl_dim_type type) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 491 | { |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 492 | return map ? isl_space_has_tuple_name(map->dim, type) : isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | const char *isl_map_get_tuple_name(__isl_keep isl_map *map, |
| 496 | enum isl_dim_type type) |
| 497 | { |
| 498 | return map ? isl_space_get_tuple_name(map->dim, type) : NULL; |
| 499 | } |
| 500 | |
| 501 | __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set, |
| 502 | const char *s) |
| 503 | { |
| 504 | return (isl_set *)isl_map_set_tuple_name((isl_map *)set, isl_dim_set, s); |
| 505 | } |
| 506 | |
| 507 | __isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map, |
| 508 | enum isl_dim_type type, __isl_take isl_id *id) |
| 509 | { |
| 510 | map = isl_map_cow(map); |
| 511 | if (!map) |
| 512 | goto error; |
| 513 | |
| 514 | map->dim = isl_space_set_tuple_id(map->dim, type, id); |
| 515 | |
| 516 | return isl_map_reset_space(map, isl_space_copy(map->dim)); |
| 517 | error: |
| 518 | isl_id_free(id); |
| 519 | return NULL; |
| 520 | } |
| 521 | |
| 522 | __isl_give isl_set *isl_set_set_tuple_id(__isl_take isl_set *set, |
| 523 | __isl_take isl_id *id) |
| 524 | { |
| 525 | return isl_map_set_tuple_id(set, isl_dim_set, id); |
| 526 | } |
| 527 | |
| 528 | __isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map, |
| 529 | enum isl_dim_type type) |
| 530 | { |
| 531 | map = isl_map_cow(map); |
| 532 | if (!map) |
| 533 | return NULL; |
| 534 | |
| 535 | map->dim = isl_space_reset_tuple_id(map->dim, type); |
| 536 | |
| 537 | return isl_map_reset_space(map, isl_space_copy(map->dim)); |
| 538 | } |
| 539 | |
| 540 | __isl_give isl_set *isl_set_reset_tuple_id(__isl_take isl_set *set) |
| 541 | { |
| 542 | return isl_map_reset_tuple_id(set, isl_dim_set); |
| 543 | } |
| 544 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 545 | isl_bool isl_map_has_tuple_id(__isl_keep isl_map *map, enum isl_dim_type type) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 546 | { |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 547 | return map ? isl_space_has_tuple_id(map->dim, type) : isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | __isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map, |
| 551 | enum isl_dim_type type) |
| 552 | { |
| 553 | return map ? isl_space_get_tuple_id(map->dim, type) : NULL; |
| 554 | } |
| 555 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 556 | isl_bool isl_set_has_tuple_id(__isl_keep isl_set *set) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 557 | { |
| 558 | return isl_map_has_tuple_id(set, isl_dim_set); |
| 559 | } |
| 560 | |
| 561 | __isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_set *set) |
| 562 | { |
| 563 | return isl_map_get_tuple_id(set, isl_dim_set); |
| 564 | } |
| 565 | |
| 566 | /* Does the set tuple have a name? |
| 567 | */ |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 568 | isl_bool isl_set_has_tuple_name(__isl_keep isl_set *set) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 569 | { |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 570 | if (!set) |
| 571 | return isl_bool_error; |
| 572 | return isl_space_has_tuple_name(set->dim, isl_dim_set); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | |
| 576 | const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset) |
| 577 | { |
| 578 | return bset ? isl_space_get_tuple_name(bset->dim, isl_dim_set) : NULL; |
| 579 | } |
| 580 | |
| 581 | const char *isl_set_get_tuple_name(__isl_keep isl_set *set) |
| 582 | { |
| 583 | return set ? isl_space_get_tuple_name(set->dim, isl_dim_set) : NULL; |
| 584 | } |
| 585 | |
| 586 | const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap, |
| 587 | enum isl_dim_type type, unsigned pos) |
| 588 | { |
| 589 | return bmap ? isl_space_get_dim_name(bmap->dim, type, pos) : NULL; |
| 590 | } |
| 591 | |
| 592 | const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset, |
| 593 | enum isl_dim_type type, unsigned pos) |
| 594 | { |
| 595 | return bset ? isl_space_get_dim_name(bset->dim, type, pos) : NULL; |
| 596 | } |
| 597 | |
| 598 | /* Does the given dimension have a name? |
| 599 | */ |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 600 | isl_bool isl_map_has_dim_name(__isl_keep isl_map *map, |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 601 | enum isl_dim_type type, unsigned pos) |
| 602 | { |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 603 | if (!map) |
| 604 | return isl_bool_error; |
| 605 | return isl_space_has_dim_name(map->dim, type, pos); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | const char *isl_map_get_dim_name(__isl_keep isl_map *map, |
| 609 | enum isl_dim_type type, unsigned pos) |
| 610 | { |
| 611 | return map ? isl_space_get_dim_name(map->dim, type, pos) : NULL; |
| 612 | } |
| 613 | |
| 614 | const char *isl_set_get_dim_name(__isl_keep isl_set *set, |
| 615 | enum isl_dim_type type, unsigned pos) |
| 616 | { |
| 617 | return set ? isl_space_get_dim_name(set->dim, type, pos) : NULL; |
| 618 | } |
| 619 | |
| 620 | /* Does the given dimension have a name? |
| 621 | */ |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 622 | isl_bool isl_set_has_dim_name(__isl_keep isl_set *set, |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 623 | enum isl_dim_type type, unsigned pos) |
| 624 | { |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 625 | if (!set) |
| 626 | return isl_bool_error; |
| 627 | return isl_space_has_dim_name(set->dim, type, pos); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | __isl_give isl_basic_map *isl_basic_map_set_dim_name( |
| 631 | __isl_take isl_basic_map *bmap, |
| 632 | enum isl_dim_type type, unsigned pos, const char *s) |
| 633 | { |
| 634 | bmap = isl_basic_map_cow(bmap); |
| 635 | if (!bmap) |
| 636 | return NULL; |
| 637 | bmap->dim = isl_space_set_dim_name(bmap->dim, type, pos, s); |
| 638 | if (!bmap->dim) |
| 639 | goto error; |
| 640 | return isl_basic_map_finalize(bmap); |
| 641 | error: |
| 642 | isl_basic_map_free(bmap); |
| 643 | return NULL; |
| 644 | } |
| 645 | |
| 646 | __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map, |
| 647 | enum isl_dim_type type, unsigned pos, const char *s) |
| 648 | { |
| 649 | int i; |
| 650 | |
| 651 | map = isl_map_cow(map); |
| 652 | if (!map) |
| 653 | return NULL; |
| 654 | |
| 655 | map->dim = isl_space_set_dim_name(map->dim, type, pos, s); |
| 656 | if (!map->dim) |
| 657 | goto error; |
| 658 | |
| 659 | for (i = 0; i < map->n; ++i) { |
| 660 | map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s); |
| 661 | if (!map->p[i]) |
| 662 | goto error; |
| 663 | } |
| 664 | |
| 665 | return map; |
| 666 | error: |
| 667 | isl_map_free(map); |
| 668 | return NULL; |
| 669 | } |
| 670 | |
| 671 | __isl_give isl_basic_set *isl_basic_set_set_dim_name( |
| 672 | __isl_take isl_basic_set *bset, |
| 673 | enum isl_dim_type type, unsigned pos, const char *s) |
| 674 | { |
| 675 | return (isl_basic_set *)isl_basic_map_set_dim_name( |
| 676 | (isl_basic_map *)bset, type, pos, s); |
| 677 | } |
| 678 | |
| 679 | __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set, |
| 680 | enum isl_dim_type type, unsigned pos, const char *s) |
| 681 | { |
| 682 | return (isl_set *)isl_map_set_dim_name((isl_map *)set, type, pos, s); |
| 683 | } |
| 684 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 685 | isl_bool isl_basic_map_has_dim_id(__isl_keep isl_basic_map *bmap, |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 686 | enum isl_dim_type type, unsigned pos) |
| 687 | { |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 688 | if (!bmap) |
| 689 | return isl_bool_error; |
| 690 | return isl_space_has_dim_id(bmap->dim, type, pos); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | __isl_give isl_id *isl_basic_set_get_dim_id(__isl_keep isl_basic_set *bset, |
| 694 | enum isl_dim_type type, unsigned pos) |
| 695 | { |
| 696 | return bset ? isl_space_get_dim_id(bset->dim, type, pos) : NULL; |
| 697 | } |
| 698 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 699 | isl_bool isl_map_has_dim_id(__isl_keep isl_map *map, |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 700 | enum isl_dim_type type, unsigned pos) |
| 701 | { |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 702 | return map ? isl_space_has_dim_id(map->dim, type, pos) : isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | __isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map, |
| 706 | enum isl_dim_type type, unsigned pos) |
| 707 | { |
| 708 | return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL; |
| 709 | } |
| 710 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 711 | isl_bool isl_set_has_dim_id(__isl_keep isl_set *set, |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 712 | enum isl_dim_type type, unsigned pos) |
| 713 | { |
| 714 | return isl_map_has_dim_id(set, type, pos); |
| 715 | } |
| 716 | |
| 717 | __isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set, |
| 718 | enum isl_dim_type type, unsigned pos) |
| 719 | { |
| 720 | return isl_map_get_dim_id(set, type, pos); |
| 721 | } |
| 722 | |
| 723 | __isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map, |
| 724 | enum isl_dim_type type, unsigned pos, __isl_take isl_id *id) |
| 725 | { |
| 726 | map = isl_map_cow(map); |
| 727 | if (!map) |
| 728 | goto error; |
| 729 | |
| 730 | map->dim = isl_space_set_dim_id(map->dim, type, pos, id); |
| 731 | |
| 732 | return isl_map_reset_space(map, isl_space_copy(map->dim)); |
| 733 | error: |
| 734 | isl_id_free(id); |
| 735 | return NULL; |
| 736 | } |
| 737 | |
| 738 | __isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set, |
| 739 | enum isl_dim_type type, unsigned pos, __isl_take isl_id *id) |
| 740 | { |
| 741 | return isl_map_set_dim_id(set, type, pos, id); |
| 742 | } |
| 743 | |
| 744 | int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type, |
| 745 | __isl_keep isl_id *id) |
| 746 | { |
| 747 | if (!map) |
| 748 | return -1; |
| 749 | return isl_space_find_dim_by_id(map->dim, type, id); |
| 750 | } |
| 751 | |
| 752 | int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type, |
| 753 | __isl_keep isl_id *id) |
| 754 | { |
| 755 | return isl_map_find_dim_by_id(set, type, id); |
| 756 | } |
| 757 | |
| 758 | /* Return the position of the dimension of the given type and name |
| 759 | * in "bmap". |
| 760 | * Return -1 if no such dimension can be found. |
| 761 | */ |
| 762 | int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map *bmap, |
| 763 | enum isl_dim_type type, const char *name) |
| 764 | { |
| 765 | if (!bmap) |
| 766 | return -1; |
| 767 | return isl_space_find_dim_by_name(bmap->dim, type, name); |
| 768 | } |
| 769 | |
| 770 | int isl_map_find_dim_by_name(__isl_keep isl_map *map, enum isl_dim_type type, |
| 771 | const char *name) |
| 772 | { |
| 773 | if (!map) |
| 774 | return -1; |
| 775 | return isl_space_find_dim_by_name(map->dim, type, name); |
| 776 | } |
| 777 | |
| 778 | int isl_set_find_dim_by_name(__isl_keep isl_set *set, enum isl_dim_type type, |
| 779 | const char *name) |
| 780 | { |
| 781 | return isl_map_find_dim_by_name(set, type, name); |
| 782 | } |
| 783 | |
| 784 | /* Reset the user pointer on all identifiers of parameters and tuples |
| 785 | * of the space of "map". |
| 786 | */ |
| 787 | __isl_give isl_map *isl_map_reset_user(__isl_take isl_map *map) |
| 788 | { |
| 789 | isl_space *space; |
| 790 | |
| 791 | space = isl_map_get_space(map); |
| 792 | space = isl_space_reset_user(space); |
| 793 | map = isl_map_reset_space(map, space); |
| 794 | |
| 795 | return map; |
| 796 | } |
| 797 | |
| 798 | /* Reset the user pointer on all identifiers of parameters and tuples |
| 799 | * of the space of "set". |
| 800 | */ |
| 801 | __isl_give isl_set *isl_set_reset_user(__isl_take isl_set *set) |
| 802 | { |
| 803 | return isl_map_reset_user(set); |
| 804 | } |
| 805 | |
| 806 | int isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap) |
| 807 | { |
| 808 | if (!bmap) |
| 809 | return -1; |
| 810 | return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL); |
| 811 | } |
| 812 | |
| 813 | int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset) |
| 814 | { |
| 815 | return isl_basic_map_is_rational(bset); |
| 816 | } |
| 817 | |
| 818 | /* Does "bmap" contain any rational points? |
| 819 | * |
| 820 | * If "bmap" has an equality for each dimension, equating the dimension |
| 821 | * to an integer constant, then it has no rational points, even if it |
| 822 | * is marked as rational. |
| 823 | */ |
| 824 | int isl_basic_map_has_rational(__isl_keep isl_basic_map *bmap) |
| 825 | { |
| 826 | int has_rational = 1; |
| 827 | unsigned total; |
| 828 | |
| 829 | if (!bmap) |
| 830 | return -1; |
| 831 | if (isl_basic_map_plain_is_empty(bmap)) |
| 832 | return 0; |
| 833 | if (!isl_basic_map_is_rational(bmap)) |
| 834 | return 0; |
| 835 | bmap = isl_basic_map_copy(bmap); |
| 836 | bmap = isl_basic_map_implicit_equalities(bmap); |
| 837 | if (!bmap) |
| 838 | return -1; |
| 839 | total = isl_basic_map_total_dim(bmap); |
| 840 | if (bmap->n_eq == total) { |
| 841 | int i, j; |
| 842 | for (i = 0; i < bmap->n_eq; ++i) { |
| 843 | j = isl_seq_first_non_zero(bmap->eq[i] + 1, total); |
| 844 | if (j < 0) |
| 845 | break; |
| 846 | if (!isl_int_is_one(bmap->eq[i][1 + j]) && |
| 847 | !isl_int_is_negone(bmap->eq[i][1 + j])) |
| 848 | break; |
| 849 | j = isl_seq_first_non_zero(bmap->eq[i] + 1 + j + 1, |
| 850 | total - j - 1); |
| 851 | if (j >= 0) |
| 852 | break; |
| 853 | } |
| 854 | if (i == bmap->n_eq) |
| 855 | has_rational = 0; |
| 856 | } |
| 857 | isl_basic_map_free(bmap); |
| 858 | |
| 859 | return has_rational; |
| 860 | } |
| 861 | |
| 862 | /* Does "map" contain any rational points? |
| 863 | */ |
| 864 | int isl_map_has_rational(__isl_keep isl_map *map) |
| 865 | { |
| 866 | int i; |
| 867 | int has_rational; |
| 868 | |
| 869 | if (!map) |
| 870 | return -1; |
| 871 | for (i = 0; i < map->n; ++i) { |
| 872 | has_rational = isl_basic_map_has_rational(map->p[i]); |
| 873 | if (has_rational < 0) |
| 874 | return -1; |
| 875 | if (has_rational) |
| 876 | return 1; |
| 877 | } |
| 878 | return 0; |
| 879 | } |
| 880 | |
| 881 | /* Does "set" contain any rational points? |
| 882 | */ |
| 883 | int isl_set_has_rational(__isl_keep isl_set *set) |
| 884 | { |
| 885 | return isl_map_has_rational(set); |
| 886 | } |
| 887 | |
| 888 | /* Is this basic set a parameter domain? |
| 889 | */ |
| 890 | int isl_basic_set_is_params(__isl_keep isl_basic_set *bset) |
| 891 | { |
| 892 | if (!bset) |
| 893 | return -1; |
| 894 | return isl_space_is_params(bset->dim); |
| 895 | } |
| 896 | |
| 897 | /* Is this set a parameter domain? |
| 898 | */ |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 899 | isl_bool isl_set_is_params(__isl_keep isl_set *set) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 900 | { |
| 901 | if (!set) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 902 | return isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 903 | return isl_space_is_params(set->dim); |
| 904 | } |
| 905 | |
| 906 | /* Is this map actually a parameter domain? |
| 907 | * Users should never call this function. Outside of isl, |
| 908 | * a map can never be a parameter domain. |
| 909 | */ |
| 910 | int isl_map_is_params(__isl_keep isl_map *map) |
| 911 | { |
| 912 | if (!map) |
| 913 | return -1; |
| 914 | return isl_space_is_params(map->dim); |
| 915 | } |
| 916 | |
| 917 | static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx, |
| 918 | struct isl_basic_map *bmap, unsigned extra, |
| 919 | unsigned n_eq, unsigned n_ineq) |
| 920 | { |
| 921 | int i; |
| 922 | size_t row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + extra; |
| 923 | |
| 924 | bmap->ctx = ctx; |
| 925 | isl_ctx_ref(ctx); |
| 926 | |
| 927 | bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size); |
| 928 | if (isl_blk_is_error(bmap->block)) |
| 929 | goto error; |
| 930 | |
| 931 | bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq); |
| 932 | if ((n_ineq + n_eq) && !bmap->ineq) |
| 933 | goto error; |
| 934 | |
| 935 | if (extra == 0) { |
| 936 | bmap->block2 = isl_blk_empty(); |
| 937 | bmap->div = NULL; |
| 938 | } else { |
| 939 | bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size)); |
| 940 | if (isl_blk_is_error(bmap->block2)) |
| 941 | goto error; |
| 942 | |
| 943 | bmap->div = isl_alloc_array(ctx, isl_int *, extra); |
| 944 | if (!bmap->div) |
| 945 | goto error; |
| 946 | } |
| 947 | |
| 948 | for (i = 0; i < n_ineq + n_eq; ++i) |
| 949 | bmap->ineq[i] = bmap->block.data + i * row_size; |
| 950 | |
| 951 | for (i = 0; i < extra; ++i) |
| 952 | bmap->div[i] = bmap->block2.data + i * (1 + row_size); |
| 953 | |
| 954 | bmap->ref = 1; |
| 955 | bmap->flags = 0; |
| 956 | bmap->c_size = n_eq + n_ineq; |
| 957 | bmap->eq = bmap->ineq + n_ineq; |
| 958 | bmap->extra = extra; |
| 959 | bmap->n_eq = 0; |
| 960 | bmap->n_ineq = 0; |
| 961 | bmap->n_div = 0; |
| 962 | bmap->sample = NULL; |
| 963 | |
| 964 | return bmap; |
| 965 | error: |
| 966 | isl_basic_map_free(bmap); |
| 967 | return NULL; |
| 968 | } |
| 969 | |
| 970 | struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx, |
| 971 | unsigned nparam, unsigned dim, unsigned extra, |
| 972 | unsigned n_eq, unsigned n_ineq) |
| 973 | { |
| 974 | struct isl_basic_map *bmap; |
| 975 | isl_space *space; |
| 976 | |
| 977 | space = isl_space_set_alloc(ctx, nparam, dim); |
| 978 | if (!space) |
| 979 | return NULL; |
| 980 | |
| 981 | bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq); |
| 982 | return (struct isl_basic_set *)bmap; |
| 983 | } |
| 984 | |
| 985 | struct isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *dim, |
| 986 | unsigned extra, unsigned n_eq, unsigned n_ineq) |
| 987 | { |
| 988 | struct isl_basic_map *bmap; |
| 989 | if (!dim) |
| 990 | return NULL; |
| 991 | isl_assert(dim->ctx, dim->n_in == 0, goto error); |
| 992 | bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq); |
| 993 | return (struct isl_basic_set *)bmap; |
| 994 | error: |
| 995 | isl_space_free(dim); |
| 996 | return NULL; |
| 997 | } |
| 998 | |
| 999 | struct isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *dim, |
| 1000 | unsigned extra, unsigned n_eq, unsigned n_ineq) |
| 1001 | { |
| 1002 | struct isl_basic_map *bmap; |
| 1003 | |
| 1004 | if (!dim) |
| 1005 | return NULL; |
| 1006 | bmap = isl_calloc_type(dim->ctx, struct isl_basic_map); |
| 1007 | if (!bmap) |
| 1008 | goto error; |
| 1009 | bmap->dim = dim; |
| 1010 | |
| 1011 | return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq); |
| 1012 | error: |
| 1013 | isl_space_free(dim); |
| 1014 | return NULL; |
| 1015 | } |
| 1016 | |
| 1017 | struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx, |
| 1018 | unsigned nparam, unsigned in, unsigned out, unsigned extra, |
| 1019 | unsigned n_eq, unsigned n_ineq) |
| 1020 | { |
| 1021 | struct isl_basic_map *bmap; |
| 1022 | isl_space *dim; |
| 1023 | |
| 1024 | dim = isl_space_alloc(ctx, nparam, in, out); |
| 1025 | if (!dim) |
| 1026 | return NULL; |
| 1027 | |
| 1028 | bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq); |
| 1029 | return bmap; |
| 1030 | } |
| 1031 | |
| 1032 | static void dup_constraints( |
| 1033 | struct isl_basic_map *dst, struct isl_basic_map *src) |
| 1034 | { |
| 1035 | int i; |
| 1036 | unsigned total = isl_basic_map_total_dim(src); |
| 1037 | |
| 1038 | for (i = 0; i < src->n_eq; ++i) { |
| 1039 | int j = isl_basic_map_alloc_equality(dst); |
| 1040 | isl_seq_cpy(dst->eq[j], src->eq[i], 1+total); |
| 1041 | } |
| 1042 | |
| 1043 | for (i = 0; i < src->n_ineq; ++i) { |
| 1044 | int j = isl_basic_map_alloc_inequality(dst); |
| 1045 | isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total); |
| 1046 | } |
| 1047 | |
| 1048 | for (i = 0; i < src->n_div; ++i) { |
| 1049 | int j = isl_basic_map_alloc_div(dst); |
| 1050 | isl_seq_cpy(dst->div[j], src->div[i], 1+1+total); |
| 1051 | } |
| 1052 | ISL_F_SET(dst, ISL_BASIC_SET_FINAL); |
| 1053 | } |
| 1054 | |
| 1055 | struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap) |
| 1056 | { |
| 1057 | struct isl_basic_map *dup; |
| 1058 | |
| 1059 | if (!bmap) |
| 1060 | return NULL; |
| 1061 | dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim), |
| 1062 | bmap->n_div, bmap->n_eq, bmap->n_ineq); |
| 1063 | if (!dup) |
| 1064 | return NULL; |
| 1065 | dup_constraints(dup, bmap); |
| 1066 | dup->flags = bmap->flags; |
| 1067 | dup->sample = isl_vec_copy(bmap->sample); |
| 1068 | return dup; |
| 1069 | } |
| 1070 | |
| 1071 | struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset) |
| 1072 | { |
| 1073 | struct isl_basic_map *dup; |
| 1074 | |
| 1075 | dup = isl_basic_map_dup((struct isl_basic_map *)bset); |
| 1076 | return (struct isl_basic_set *)dup; |
| 1077 | } |
| 1078 | |
| 1079 | struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset) |
| 1080 | { |
| 1081 | if (!bset) |
| 1082 | return NULL; |
| 1083 | |
| 1084 | if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) { |
| 1085 | bset->ref++; |
| 1086 | return bset; |
| 1087 | } |
| 1088 | return isl_basic_set_dup(bset); |
| 1089 | } |
| 1090 | |
| 1091 | struct isl_set *isl_set_copy(struct isl_set *set) |
| 1092 | { |
| 1093 | if (!set) |
| 1094 | return NULL; |
| 1095 | |
| 1096 | set->ref++; |
| 1097 | return set; |
| 1098 | } |
| 1099 | |
| 1100 | struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap) |
| 1101 | { |
| 1102 | if (!bmap) |
| 1103 | return NULL; |
| 1104 | |
| 1105 | if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) { |
| 1106 | bmap->ref++; |
| 1107 | return bmap; |
| 1108 | } |
| 1109 | bmap = isl_basic_map_dup(bmap); |
| 1110 | if (bmap) |
| 1111 | ISL_F_SET(bmap, ISL_BASIC_SET_FINAL); |
| 1112 | return bmap; |
| 1113 | } |
| 1114 | |
| 1115 | struct isl_map *isl_map_copy(struct isl_map *map) |
| 1116 | { |
| 1117 | if (!map) |
| 1118 | return NULL; |
| 1119 | |
| 1120 | map->ref++; |
| 1121 | return map; |
| 1122 | } |
| 1123 | |
| 1124 | __isl_null isl_basic_map *isl_basic_map_free(__isl_take isl_basic_map *bmap) |
| 1125 | { |
| 1126 | if (!bmap) |
| 1127 | return NULL; |
| 1128 | |
| 1129 | if (--bmap->ref > 0) |
| 1130 | return NULL; |
| 1131 | |
| 1132 | isl_ctx_deref(bmap->ctx); |
| 1133 | free(bmap->div); |
| 1134 | isl_blk_free(bmap->ctx, bmap->block2); |
| 1135 | free(bmap->ineq); |
| 1136 | isl_blk_free(bmap->ctx, bmap->block); |
| 1137 | isl_vec_free(bmap->sample); |
| 1138 | isl_space_free(bmap->dim); |
| 1139 | free(bmap); |
| 1140 | |
| 1141 | return NULL; |
| 1142 | } |
| 1143 | |
| 1144 | __isl_null isl_basic_set *isl_basic_set_free(__isl_take isl_basic_set *bset) |
| 1145 | { |
| 1146 | return isl_basic_map_free((struct isl_basic_map *)bset); |
| 1147 | } |
| 1148 | |
| 1149 | static int room_for_con(struct isl_basic_map *bmap, unsigned n) |
| 1150 | { |
| 1151 | return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size; |
| 1152 | } |
| 1153 | |
| 1154 | __isl_give isl_map *isl_map_align_params_map_map_and( |
| 1155 | __isl_take isl_map *map1, __isl_take isl_map *map2, |
| 1156 | __isl_give isl_map *(*fn)(__isl_take isl_map *map1, |
| 1157 | __isl_take isl_map *map2)) |
| 1158 | { |
| 1159 | if (!map1 || !map2) |
| 1160 | goto error; |
| 1161 | if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param)) |
| 1162 | return fn(map1, map2); |
| 1163 | if (!isl_space_has_named_params(map1->dim) || |
| 1164 | !isl_space_has_named_params(map2->dim)) |
| 1165 | isl_die(map1->ctx, isl_error_invalid, |
| 1166 | "unaligned unnamed parameters", goto error); |
| 1167 | map1 = isl_map_align_params(map1, isl_map_get_space(map2)); |
| 1168 | map2 = isl_map_align_params(map2, isl_map_get_space(map1)); |
| 1169 | return fn(map1, map2); |
| 1170 | error: |
| 1171 | isl_map_free(map1); |
| 1172 | isl_map_free(map2); |
| 1173 | return NULL; |
| 1174 | } |
| 1175 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 1176 | isl_bool isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1, |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 1177 | __isl_keep isl_map *map2, |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 1178 | isl_bool (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2)) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 1179 | { |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 1180 | isl_bool r; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 1181 | |
| 1182 | if (!map1 || !map2) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 1183 | return isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 1184 | if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param)) |
| 1185 | return fn(map1, map2); |
| 1186 | if (!isl_space_has_named_params(map1->dim) || |
| 1187 | !isl_space_has_named_params(map2->dim)) |
| 1188 | isl_die(map1->ctx, isl_error_invalid, |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 1189 | "unaligned unnamed parameters", return isl_bool_error); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 1190 | map1 = isl_map_copy(map1); |
| 1191 | map2 = isl_map_copy(map2); |
| 1192 | map1 = isl_map_align_params(map1, isl_map_get_space(map2)); |
| 1193 | map2 = isl_map_align_params(map2, isl_map_get_space(map1)); |
| 1194 | r = fn(map1, map2); |
| 1195 | isl_map_free(map1); |
| 1196 | isl_map_free(map2); |
| 1197 | return r; |
| 1198 | } |
| 1199 | |
| 1200 | int isl_basic_map_alloc_equality(struct isl_basic_map *bmap) |
| 1201 | { |
| 1202 | struct isl_ctx *ctx; |
| 1203 | if (!bmap) |
| 1204 | return -1; |
| 1205 | ctx = bmap->ctx; |
| 1206 | isl_assert(ctx, room_for_con(bmap, 1), return -1); |
| 1207 | isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size, |
| 1208 | return -1); |
| 1209 | ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED); |
| 1210 | ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT); |
| 1211 | ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT); |
| 1212 | ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES); |
| 1213 | ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS); |
| 1214 | if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) { |
| 1215 | isl_int *t; |
| 1216 | int j = isl_basic_map_alloc_inequality(bmap); |
| 1217 | if (j < 0) |
| 1218 | return -1; |
| 1219 | t = bmap->ineq[j]; |
| 1220 | bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1]; |
| 1221 | bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1]; |
| 1222 | bmap->eq[-1] = t; |
| 1223 | bmap->n_eq++; |
| 1224 | bmap->n_ineq--; |
| 1225 | bmap->eq--; |
| 1226 | return 0; |
| 1227 | } |
| 1228 | isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap), |
| 1229 | bmap->extra - bmap->n_div); |
| 1230 | return bmap->n_eq++; |
| 1231 | } |
| 1232 | |
| 1233 | int isl_basic_set_alloc_equality(struct isl_basic_set *bset) |
| 1234 | { |
| 1235 | return isl_basic_map_alloc_equality((struct isl_basic_map *)bset); |
| 1236 | } |
| 1237 | |
| 1238 | int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n) |
| 1239 | { |
| 1240 | if (!bmap) |
| 1241 | return -1; |
| 1242 | isl_assert(bmap->ctx, n <= bmap->n_eq, return -1); |
| 1243 | bmap->n_eq -= n; |
| 1244 | return 0; |
| 1245 | } |
| 1246 | |
| 1247 | int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n) |
| 1248 | { |
| 1249 | return isl_basic_map_free_equality((struct isl_basic_map *)bset, n); |
| 1250 | } |
| 1251 | |
| 1252 | int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos) |
| 1253 | { |
| 1254 | isl_int *t; |
| 1255 | if (!bmap) |
| 1256 | return -1; |
| 1257 | isl_assert(bmap->ctx, pos < bmap->n_eq, return -1); |
| 1258 | |
| 1259 | if (pos != bmap->n_eq - 1) { |
| 1260 | t = bmap->eq[pos]; |
| 1261 | bmap->eq[pos] = bmap->eq[bmap->n_eq - 1]; |
| 1262 | bmap->eq[bmap->n_eq - 1] = t; |
| 1263 | } |
| 1264 | bmap->n_eq--; |
| 1265 | return 0; |
| 1266 | } |
| 1267 | |
| 1268 | int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos) |
| 1269 | { |
| 1270 | return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos); |
| 1271 | } |
| 1272 | |
| 1273 | /* Turn inequality "pos" of "bmap" into an equality. |
| 1274 | * |
| 1275 | * In particular, we move the inequality in front of the equalities |
| 1276 | * and move the last inequality in the position of the moved inequality. |
| 1277 | * Note that isl_tab_make_equalities_explicit depends on this particular |
| 1278 | * change in the ordering of the constraints. |
| 1279 | */ |
| 1280 | void isl_basic_map_inequality_to_equality( |
| 1281 | struct isl_basic_map *bmap, unsigned pos) |
| 1282 | { |
| 1283 | isl_int *t; |
| 1284 | |
| 1285 | t = bmap->ineq[pos]; |
| 1286 | bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1]; |
| 1287 | bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1]; |
| 1288 | bmap->eq[-1] = t; |
| 1289 | bmap->n_eq++; |
| 1290 | bmap->n_ineq--; |
| 1291 | bmap->eq--; |
| 1292 | ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT); |
| 1293 | ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED); |
| 1294 | ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS); |
| 1295 | ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES); |
| 1296 | } |
| 1297 | |
| 1298 | static int room_for_ineq(struct isl_basic_map *bmap, unsigned n) |
| 1299 | { |
| 1300 | return bmap->n_ineq + n <= bmap->eq - bmap->ineq; |
| 1301 | } |
| 1302 | |
| 1303 | int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap) |
| 1304 | { |
| 1305 | struct isl_ctx *ctx; |
| 1306 | if (!bmap) |
| 1307 | return -1; |
| 1308 | ctx = bmap->ctx; |
| 1309 | isl_assert(ctx, room_for_ineq(bmap, 1), return -1); |
| 1310 | ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT); |
| 1311 | ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT); |
| 1312 | ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED); |
| 1313 | ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES); |
| 1314 | isl_seq_clr(bmap->ineq[bmap->n_ineq] + |
| 1315 | 1 + isl_basic_map_total_dim(bmap), |
| 1316 | bmap->extra - bmap->n_div); |
| 1317 | return bmap->n_ineq++; |
| 1318 | } |
| 1319 | |
| 1320 | int isl_basic_set_alloc_inequality(struct isl_basic_set *bset) |
| 1321 | { |
| 1322 | return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset); |
| 1323 | } |
| 1324 | |
| 1325 | int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n) |
| 1326 | { |
| 1327 | if (!bmap) |
| 1328 | return -1; |
| 1329 | isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1); |
| 1330 | bmap->n_ineq -= n; |
| 1331 | return 0; |
| 1332 | } |
| 1333 | |
| 1334 | int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n) |
| 1335 | { |
| 1336 | return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n); |
| 1337 | } |
| 1338 | |
| 1339 | int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos) |
| 1340 | { |
| 1341 | isl_int *t; |
| 1342 | if (!bmap) |
| 1343 | return -1; |
| 1344 | isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1); |
| 1345 | |
| 1346 | if (pos != bmap->n_ineq - 1) { |
| 1347 | t = bmap->ineq[pos]; |
| 1348 | bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1]; |
| 1349 | bmap->ineq[bmap->n_ineq - 1] = t; |
| 1350 | ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED); |
| 1351 | } |
| 1352 | bmap->n_ineq--; |
| 1353 | return 0; |
| 1354 | } |
| 1355 | |
| 1356 | int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos) |
| 1357 | { |
| 1358 | return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos); |
| 1359 | } |
| 1360 | |
| 1361 | __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap, |
| 1362 | isl_int *eq) |
| 1363 | { |
| 1364 | int k; |
| 1365 | |
| 1366 | bmap = isl_basic_map_extend_constraints(bmap, 1, 0); |
| 1367 | if (!bmap) |
| 1368 | return NULL; |
| 1369 | k = isl_basic_map_alloc_equality(bmap); |
| 1370 | if (k < 0) |
| 1371 | goto error; |
| 1372 | isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap)); |
| 1373 | return bmap; |
| 1374 | error: |
| 1375 | isl_basic_map_free(bmap); |
| 1376 | return NULL; |
| 1377 | } |
| 1378 | |
| 1379 | __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset, |
| 1380 | isl_int *eq) |
| 1381 | { |
| 1382 | return (isl_basic_set *) |
| 1383 | isl_basic_map_add_eq((isl_basic_map *)bset, eq); |
| 1384 | } |
| 1385 | |
| 1386 | __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap, |
| 1387 | isl_int *ineq) |
| 1388 | { |
| 1389 | int k; |
| 1390 | |
| 1391 | bmap = isl_basic_map_extend_constraints(bmap, 0, 1); |
| 1392 | if (!bmap) |
| 1393 | return NULL; |
| 1394 | k = isl_basic_map_alloc_inequality(bmap); |
| 1395 | if (k < 0) |
| 1396 | goto error; |
| 1397 | isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap)); |
| 1398 | return bmap; |
| 1399 | error: |
| 1400 | isl_basic_map_free(bmap); |
| 1401 | return NULL; |
| 1402 | } |
| 1403 | |
| 1404 | __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset, |
| 1405 | isl_int *ineq) |
| 1406 | { |
| 1407 | return (isl_basic_set *) |
| 1408 | isl_basic_map_add_ineq((isl_basic_map *)bset, ineq); |
| 1409 | } |
| 1410 | |
| 1411 | int isl_basic_map_alloc_div(struct isl_basic_map *bmap) |
| 1412 | { |
| 1413 | if (!bmap) |
| 1414 | return -1; |
| 1415 | isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1); |
| 1416 | isl_seq_clr(bmap->div[bmap->n_div] + |
| 1417 | 1 + 1 + isl_basic_map_total_dim(bmap), |
| 1418 | bmap->extra - bmap->n_div); |
| 1419 | ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS); |
| 1420 | return bmap->n_div++; |
| 1421 | } |
| 1422 | |
| 1423 | int isl_basic_set_alloc_div(struct isl_basic_set *bset) |
| 1424 | { |
| 1425 | return isl_basic_map_alloc_div((struct isl_basic_map *)bset); |
| 1426 | } |
| 1427 | |
| 1428 | int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n) |
| 1429 | { |
| 1430 | if (!bmap) |
| 1431 | return -1; |
| 1432 | isl_assert(bmap->ctx, n <= bmap->n_div, return -1); |
| 1433 | bmap->n_div -= n; |
| 1434 | return 0; |
| 1435 | } |
| 1436 | |
| 1437 | int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n) |
| 1438 | { |
| 1439 | return isl_basic_map_free_div((struct isl_basic_map *)bset, n); |
| 1440 | } |
| 1441 | |
| 1442 | /* Copy constraint from src to dst, putting the vars of src at offset |
| 1443 | * dim_off in dst and the divs of src at offset div_off in dst. |
| 1444 | * If both sets are actually map, then dim_off applies to the input |
| 1445 | * variables. |
| 1446 | */ |
| 1447 | static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst, |
| 1448 | struct isl_basic_map *src_map, isl_int *src, |
| 1449 | unsigned in_off, unsigned out_off, unsigned div_off) |
| 1450 | { |
| 1451 | unsigned src_nparam = isl_basic_map_n_param(src_map); |
| 1452 | unsigned dst_nparam = isl_basic_map_n_param(dst_map); |
| 1453 | unsigned src_in = isl_basic_map_n_in(src_map); |
| 1454 | unsigned dst_in = isl_basic_map_n_in(dst_map); |
| 1455 | unsigned src_out = isl_basic_map_n_out(src_map); |
| 1456 | unsigned dst_out = isl_basic_map_n_out(dst_map); |
| 1457 | isl_int_set(dst[0], src[0]); |
| 1458 | isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam)); |
| 1459 | if (dst_nparam > src_nparam) |
| 1460 | isl_seq_clr(dst+1+src_nparam, |
| 1461 | dst_nparam - src_nparam); |
| 1462 | isl_seq_clr(dst+1+dst_nparam, in_off); |
| 1463 | isl_seq_cpy(dst+1+dst_nparam+in_off, |
| 1464 | src+1+src_nparam, |
| 1465 | isl_min(dst_in-in_off, src_in)); |
| 1466 | if (dst_in-in_off > src_in) |
| 1467 | isl_seq_clr(dst+1+dst_nparam+in_off+src_in, |
| 1468 | dst_in - in_off - src_in); |
| 1469 | isl_seq_clr(dst+1+dst_nparam+dst_in, out_off); |
| 1470 | isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off, |
| 1471 | src+1+src_nparam+src_in, |
| 1472 | isl_min(dst_out-out_off, src_out)); |
| 1473 | if (dst_out-out_off > src_out) |
| 1474 | isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out, |
| 1475 | dst_out - out_off - src_out); |
| 1476 | isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off); |
| 1477 | isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off, |
| 1478 | src+1+src_nparam+src_in+src_out, |
| 1479 | isl_min(dst_map->extra-div_off, src_map->n_div)); |
| 1480 | if (dst_map->n_div-div_off > src_map->n_div) |
| 1481 | isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+ |
| 1482 | div_off+src_map->n_div, |
| 1483 | dst_map->n_div - div_off - src_map->n_div); |
| 1484 | } |
| 1485 | |
| 1486 | static void copy_div(struct isl_basic_map *dst_map, isl_int *dst, |
| 1487 | struct isl_basic_map *src_map, isl_int *src, |
| 1488 | unsigned in_off, unsigned out_off, unsigned div_off) |
| 1489 | { |
| 1490 | isl_int_set(dst[0], src[0]); |
| 1491 | copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off); |
| 1492 | } |
| 1493 | |
| 1494 | static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1, |
| 1495 | struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos) |
| 1496 | { |
| 1497 | int i; |
| 1498 | unsigned div_off; |
| 1499 | |
| 1500 | if (!bmap1 || !bmap2) |
| 1501 | goto error; |
| 1502 | |
| 1503 | div_off = bmap1->n_div; |
| 1504 | |
| 1505 | for (i = 0; i < bmap2->n_eq; ++i) { |
| 1506 | int i1 = isl_basic_map_alloc_equality(bmap1); |
| 1507 | if (i1 < 0) |
| 1508 | goto error; |
| 1509 | copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i], |
| 1510 | i_pos, o_pos, div_off); |
| 1511 | } |
| 1512 | |
| 1513 | for (i = 0; i < bmap2->n_ineq; ++i) { |
| 1514 | int i1 = isl_basic_map_alloc_inequality(bmap1); |
| 1515 | if (i1 < 0) |
| 1516 | goto error; |
| 1517 | copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i], |
| 1518 | i_pos, o_pos, div_off); |
| 1519 | } |
| 1520 | |
| 1521 | for (i = 0; i < bmap2->n_div; ++i) { |
| 1522 | int i1 = isl_basic_map_alloc_div(bmap1); |
| 1523 | if (i1 < 0) |
| 1524 | goto error; |
| 1525 | copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i], |
| 1526 | i_pos, o_pos, div_off); |
| 1527 | } |
| 1528 | |
| 1529 | isl_basic_map_free(bmap2); |
| 1530 | |
| 1531 | return bmap1; |
| 1532 | |
| 1533 | error: |
| 1534 | isl_basic_map_free(bmap1); |
| 1535 | isl_basic_map_free(bmap2); |
| 1536 | return NULL; |
| 1537 | } |
| 1538 | |
| 1539 | struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1, |
| 1540 | struct isl_basic_set *bset2, unsigned pos) |
| 1541 | { |
| 1542 | return (struct isl_basic_set *) |
| 1543 | add_constraints((struct isl_basic_map *)bset1, |
| 1544 | (struct isl_basic_map *)bset2, 0, pos); |
| 1545 | } |
| 1546 | |
| 1547 | struct isl_basic_map *isl_basic_map_extend_space(struct isl_basic_map *base, |
| 1548 | __isl_take isl_space *dim, unsigned extra, |
| 1549 | unsigned n_eq, unsigned n_ineq) |
| 1550 | { |
| 1551 | struct isl_basic_map *ext; |
| 1552 | unsigned flags; |
| 1553 | int dims_ok; |
| 1554 | |
| 1555 | if (!dim) |
| 1556 | goto error; |
| 1557 | |
| 1558 | if (!base) |
| 1559 | goto error; |
| 1560 | |
| 1561 | dims_ok = isl_space_is_equal(base->dim, dim) && |
| 1562 | base->extra >= base->n_div + extra; |
| 1563 | |
| 1564 | if (dims_ok && room_for_con(base, n_eq + n_ineq) && |
| 1565 | room_for_ineq(base, n_ineq)) { |
| 1566 | isl_space_free(dim); |
| 1567 | return base; |
| 1568 | } |
| 1569 | |
| 1570 | isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error); |
| 1571 | isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error); |
| 1572 | isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error); |
| 1573 | extra += base->extra; |
| 1574 | n_eq += base->n_eq; |
| 1575 | n_ineq += base->n_ineq; |
| 1576 | |
| 1577 | ext = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq); |
| 1578 | dim = NULL; |
| 1579 | if (!ext) |
| 1580 | goto error; |
| 1581 | |
| 1582 | if (dims_ok) |
| 1583 | ext->sample = isl_vec_copy(base->sample); |
| 1584 | flags = base->flags; |
| 1585 | ext = add_constraints(ext, base, 0, 0); |
| 1586 | if (ext) { |
| 1587 | ext->flags = flags; |
| 1588 | ISL_F_CLR(ext, ISL_BASIC_SET_FINAL); |
| 1589 | } |
| 1590 | |
| 1591 | return ext; |
| 1592 | |
| 1593 | error: |
| 1594 | isl_space_free(dim); |
| 1595 | isl_basic_map_free(base); |
| 1596 | return NULL; |
| 1597 | } |
| 1598 | |
| 1599 | struct isl_basic_set *isl_basic_set_extend_space(struct isl_basic_set *base, |
| 1600 | __isl_take isl_space *dim, unsigned extra, |
| 1601 | unsigned n_eq, unsigned n_ineq) |
| 1602 | { |
| 1603 | return (struct isl_basic_set *) |
| 1604 | isl_basic_map_extend_space((struct isl_basic_map *)base, dim, |
| 1605 | extra, n_eq, n_ineq); |
| 1606 | } |
| 1607 | |
| 1608 | struct isl_basic_map *isl_basic_map_extend_constraints( |
| 1609 | struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq) |
| 1610 | { |
| 1611 | if (!base) |
| 1612 | return NULL; |
| 1613 | return isl_basic_map_extend_space(base, isl_space_copy(base->dim), |
| 1614 | 0, n_eq, n_ineq); |
| 1615 | } |
| 1616 | |
| 1617 | struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base, |
| 1618 | unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra, |
| 1619 | unsigned n_eq, unsigned n_ineq) |
| 1620 | { |
| 1621 | struct isl_basic_map *bmap; |
| 1622 | isl_space *dim; |
| 1623 | |
| 1624 | if (!base) |
| 1625 | return NULL; |
| 1626 | dim = isl_space_alloc(base->ctx, nparam, n_in, n_out); |
| 1627 | if (!dim) |
| 1628 | goto error; |
| 1629 | |
| 1630 | bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq); |
| 1631 | return bmap; |
| 1632 | error: |
| 1633 | isl_basic_map_free(base); |
| 1634 | return NULL; |
| 1635 | } |
| 1636 | |
| 1637 | struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base, |
| 1638 | unsigned nparam, unsigned dim, unsigned extra, |
| 1639 | unsigned n_eq, unsigned n_ineq) |
| 1640 | { |
| 1641 | return (struct isl_basic_set *) |
| 1642 | isl_basic_map_extend((struct isl_basic_map *)base, |
| 1643 | nparam, 0, dim, extra, n_eq, n_ineq); |
| 1644 | } |
| 1645 | |
| 1646 | struct isl_basic_set *isl_basic_set_extend_constraints( |
| 1647 | struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq) |
| 1648 | { |
| 1649 | return (struct isl_basic_set *) |
| 1650 | isl_basic_map_extend_constraints((struct isl_basic_map *)base, |
| 1651 | n_eq, n_ineq); |
| 1652 | } |
| 1653 | |
| 1654 | struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset) |
| 1655 | { |
| 1656 | return (struct isl_basic_set *) |
| 1657 | isl_basic_map_cow((struct isl_basic_map *)bset); |
| 1658 | } |
| 1659 | |
| 1660 | struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap) |
| 1661 | { |
| 1662 | if (!bmap) |
| 1663 | return NULL; |
| 1664 | |
| 1665 | if (bmap->ref > 1) { |
| 1666 | bmap->ref--; |
| 1667 | bmap = isl_basic_map_dup(bmap); |
| 1668 | } |
Tobias Grosser | 1fa7b97 | 2015-02-16 19:33:40 +0000 | [diff] [blame] | 1669 | if (bmap) { |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 1670 | ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL); |
Tobias Grosser | 1fa7b97 | 2015-02-16 19:33:40 +0000 | [diff] [blame] | 1671 | ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS); |
| 1672 | } |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 1673 | return bmap; |
| 1674 | } |
| 1675 | |
Tobias Grosser | 07b2095 | 2016-06-12 04:30:40 +0000 | [diff] [blame^] | 1676 | /* Clear all cached information in "map", either because it is about |
| 1677 | * to be modified or because it is being freed. |
| 1678 | * Always return the same pointer that is passed in. |
| 1679 | * This is needed for the use in isl_map_free. |
| 1680 | */ |
| 1681 | static __isl_give isl_map *clear_caches(__isl_take isl_map *map) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 1682 | { |
Tobias Grosser | 07b2095 | 2016-06-12 04:30:40 +0000 | [diff] [blame^] | 1683 | isl_basic_map_free(map->cached_simple_hull[0]); |
| 1684 | isl_basic_map_free(map->cached_simple_hull[1]); |
| 1685 | map->cached_simple_hull[0] = NULL; |
| 1686 | map->cached_simple_hull[1] = NULL; |
| 1687 | return map; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 1688 | } |
| 1689 | |
Tobias Grosser | 07b2095 | 2016-06-12 04:30:40 +0000 | [diff] [blame^] | 1690 | struct isl_set *isl_set_cow(struct isl_set *set) |
| 1691 | { |
| 1692 | return isl_map_cow(set); |
| 1693 | } |
| 1694 | |
| 1695 | /* Return an isl_map that is equal to "map" and that has only |
| 1696 | * a single reference. |
| 1697 | * |
| 1698 | * If the original input already has only one reference, then |
| 1699 | * simply return it, but clear all cached information, since |
| 1700 | * it may be rendered invalid by the operations that will be |
| 1701 | * performed on the result. |
| 1702 | * |
| 1703 | * Otherwise, create a duplicate (without any cached information). |
| 1704 | */ |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 1705 | struct isl_map *isl_map_cow(struct isl_map *map) |
| 1706 | { |
| 1707 | if (!map) |
| 1708 | return NULL; |
| 1709 | |
| 1710 | if (map->ref == 1) |
Tobias Grosser | 07b2095 | 2016-06-12 04:30:40 +0000 | [diff] [blame^] | 1711 | return clear_caches(map); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 1712 | map->ref--; |
| 1713 | return isl_map_dup(map); |
| 1714 | } |
| 1715 | |
| 1716 | static void swap_vars(struct isl_blk blk, isl_int *a, |
| 1717 | unsigned a_len, unsigned b_len) |
| 1718 | { |
| 1719 | isl_seq_cpy(blk.data, a+a_len, b_len); |
| 1720 | isl_seq_cpy(blk.data+b_len, a, a_len); |
| 1721 | isl_seq_cpy(a, blk.data, b_len+a_len); |
| 1722 | } |
| 1723 | |
| 1724 | static __isl_give isl_basic_map *isl_basic_map_swap_vars( |
| 1725 | __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2) |
| 1726 | { |
| 1727 | int i; |
| 1728 | struct isl_blk blk; |
| 1729 | |
| 1730 | if (!bmap) |
| 1731 | goto error; |
| 1732 | |
| 1733 | isl_assert(bmap->ctx, |
| 1734 | pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error); |
| 1735 | |
| 1736 | if (n1 == 0 || n2 == 0) |
| 1737 | return bmap; |
| 1738 | |
| 1739 | bmap = isl_basic_map_cow(bmap); |
| 1740 | if (!bmap) |
| 1741 | return NULL; |
| 1742 | |
| 1743 | blk = isl_blk_alloc(bmap->ctx, n1 + n2); |
| 1744 | if (isl_blk_is_error(blk)) |
| 1745 | goto error; |
| 1746 | |
| 1747 | for (i = 0; i < bmap->n_eq; ++i) |
| 1748 | swap_vars(blk, |
| 1749 | bmap->eq[i] + pos, n1, n2); |
| 1750 | |
| 1751 | for (i = 0; i < bmap->n_ineq; ++i) |
| 1752 | swap_vars(blk, |
| 1753 | bmap->ineq[i] + pos, n1, n2); |
| 1754 | |
| 1755 | for (i = 0; i < bmap->n_div; ++i) |
| 1756 | swap_vars(blk, |
| 1757 | bmap->div[i]+1 + pos, n1, n2); |
| 1758 | |
| 1759 | isl_blk_free(bmap->ctx, blk); |
| 1760 | |
| 1761 | ISL_F_CLR(bmap, ISL_BASIC_SET_NORMALIZED); |
| 1762 | bmap = isl_basic_map_gauss(bmap, NULL); |
| 1763 | return isl_basic_map_finalize(bmap); |
| 1764 | error: |
| 1765 | isl_basic_map_free(bmap); |
| 1766 | return NULL; |
| 1767 | } |
| 1768 | |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 1769 | struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap) |
| 1770 | { |
| 1771 | int i = 0; |
| 1772 | unsigned total; |
| 1773 | if (!bmap) |
| 1774 | goto error; |
| 1775 | total = isl_basic_map_total_dim(bmap); |
| 1776 | isl_basic_map_free_div(bmap, bmap->n_div); |
| 1777 | isl_basic_map_free_inequality(bmap, bmap->n_ineq); |
| 1778 | if (bmap->n_eq > 0) |
| 1779 | isl_basic_map_free_equality(bmap, bmap->n_eq-1); |
| 1780 | else { |
| 1781 | i = isl_basic_map_alloc_equality(bmap); |
| 1782 | if (i < 0) |
| 1783 | goto error; |
| 1784 | } |
| 1785 | isl_int_set_si(bmap->eq[i][0], 1); |
| 1786 | isl_seq_clr(bmap->eq[i]+1, total); |
| 1787 | ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY); |
| 1788 | isl_vec_free(bmap->sample); |
| 1789 | bmap->sample = NULL; |
| 1790 | return isl_basic_map_finalize(bmap); |
| 1791 | error: |
| 1792 | isl_basic_map_free(bmap); |
| 1793 | return NULL; |
| 1794 | } |
| 1795 | |
| 1796 | struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset) |
| 1797 | { |
| 1798 | return (struct isl_basic_set *) |
| 1799 | isl_basic_map_set_to_empty((struct isl_basic_map *)bset); |
| 1800 | } |
| 1801 | |
| 1802 | /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints |
| 1803 | * of "bmap"). |
| 1804 | */ |
| 1805 | static void swap_div(__isl_keep isl_basic_map *bmap, int a, int b) |
| 1806 | { |
| 1807 | isl_int *t = bmap->div[a]; |
| 1808 | bmap->div[a] = bmap->div[b]; |
| 1809 | bmap->div[b] = t; |
| 1810 | } |
| 1811 | |
| 1812 | /* Swap divs "a" and "b" in "bmap" and adjust the constraints and |
| 1813 | * div definitions accordingly. |
| 1814 | */ |
| 1815 | void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b) |
| 1816 | { |
| 1817 | int i; |
| 1818 | unsigned off = isl_space_dim(bmap->dim, isl_dim_all); |
| 1819 | |
| 1820 | swap_div(bmap, a, b); |
| 1821 | |
| 1822 | for (i = 0; i < bmap->n_eq; ++i) |
| 1823 | isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]); |
| 1824 | |
| 1825 | for (i = 0; i < bmap->n_ineq; ++i) |
| 1826 | isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]); |
| 1827 | |
| 1828 | for (i = 0; i < bmap->n_div; ++i) |
| 1829 | isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]); |
| 1830 | ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED); |
| 1831 | } |
| 1832 | |
| 1833 | /* Eliminate the specified n dimensions starting at first from the |
| 1834 | * constraints, without removing the dimensions from the space. |
| 1835 | * If the set is rational, the dimensions are eliminated using Fourier-Motzkin. |
| 1836 | */ |
| 1837 | __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map, |
| 1838 | enum isl_dim_type type, unsigned first, unsigned n) |
| 1839 | { |
| 1840 | int i; |
| 1841 | |
| 1842 | if (!map) |
| 1843 | return NULL; |
| 1844 | if (n == 0) |
| 1845 | return map; |
| 1846 | |
| 1847 | if (first + n > isl_map_dim(map, type) || first + n < first) |
| 1848 | isl_die(map->ctx, isl_error_invalid, |
| 1849 | "index out of bounds", goto error); |
| 1850 | |
| 1851 | map = isl_map_cow(map); |
| 1852 | if (!map) |
| 1853 | return NULL; |
| 1854 | |
| 1855 | for (i = 0; i < map->n; ++i) { |
| 1856 | map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n); |
| 1857 | if (!map->p[i]) |
| 1858 | goto error; |
| 1859 | } |
| 1860 | return map; |
| 1861 | error: |
| 1862 | isl_map_free(map); |
| 1863 | return NULL; |
| 1864 | } |
| 1865 | |
| 1866 | /* Eliminate the specified n dimensions starting at first from the |
| 1867 | * constraints, without removing the dimensions from the space. |
| 1868 | * If the set is rational, the dimensions are eliminated using Fourier-Motzkin. |
| 1869 | */ |
| 1870 | __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set, |
| 1871 | enum isl_dim_type type, unsigned first, unsigned n) |
| 1872 | { |
| 1873 | return (isl_set *)isl_map_eliminate((isl_map *)set, type, first, n); |
| 1874 | } |
| 1875 | |
| 1876 | /* Eliminate the specified n dimensions starting at first from the |
| 1877 | * constraints, without removing the dimensions from the space. |
| 1878 | * If the set is rational, the dimensions are eliminated using Fourier-Motzkin. |
| 1879 | */ |
| 1880 | __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set, |
| 1881 | unsigned first, unsigned n) |
| 1882 | { |
| 1883 | return isl_set_eliminate(set, isl_dim_set, first, n); |
| 1884 | } |
| 1885 | |
| 1886 | __isl_give isl_basic_map *isl_basic_map_remove_divs( |
| 1887 | __isl_take isl_basic_map *bmap) |
| 1888 | { |
| 1889 | if (!bmap) |
| 1890 | return NULL; |
| 1891 | bmap = isl_basic_map_eliminate_vars(bmap, |
| 1892 | isl_space_dim(bmap->dim, isl_dim_all), bmap->n_div); |
| 1893 | if (!bmap) |
| 1894 | return NULL; |
| 1895 | bmap->n_div = 0; |
| 1896 | return isl_basic_map_finalize(bmap); |
| 1897 | } |
| 1898 | |
| 1899 | __isl_give isl_basic_set *isl_basic_set_remove_divs( |
| 1900 | __isl_take isl_basic_set *bset) |
| 1901 | { |
| 1902 | return (struct isl_basic_set *)isl_basic_map_remove_divs( |
| 1903 | (struct isl_basic_map *)bset); |
| 1904 | } |
| 1905 | |
| 1906 | __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map) |
| 1907 | { |
| 1908 | int i; |
| 1909 | |
| 1910 | if (!map) |
| 1911 | return NULL; |
| 1912 | if (map->n == 0) |
| 1913 | return map; |
| 1914 | |
| 1915 | map = isl_map_cow(map); |
| 1916 | if (!map) |
| 1917 | return NULL; |
| 1918 | |
| 1919 | for (i = 0; i < map->n; ++i) { |
| 1920 | map->p[i] = isl_basic_map_remove_divs(map->p[i]); |
| 1921 | if (!map->p[i]) |
| 1922 | goto error; |
| 1923 | } |
| 1924 | return map; |
| 1925 | error: |
| 1926 | isl_map_free(map); |
| 1927 | return NULL; |
| 1928 | } |
| 1929 | |
| 1930 | __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set) |
| 1931 | { |
| 1932 | return isl_map_remove_divs(set); |
| 1933 | } |
| 1934 | |
| 1935 | struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap, |
| 1936 | enum isl_dim_type type, unsigned first, unsigned n) |
| 1937 | { |
| 1938 | if (!bmap) |
| 1939 | return NULL; |
| 1940 | isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type), |
| 1941 | goto error); |
| 1942 | if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type)) |
| 1943 | return bmap; |
| 1944 | bmap = isl_basic_map_eliminate_vars(bmap, |
| 1945 | isl_basic_map_offset(bmap, type) - 1 + first, n); |
| 1946 | if (!bmap) |
| 1947 | return bmap; |
| 1948 | if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div) |
| 1949 | return bmap; |
| 1950 | bmap = isl_basic_map_drop(bmap, type, first, n); |
| 1951 | return bmap; |
| 1952 | error: |
| 1953 | isl_basic_map_free(bmap); |
| 1954 | return NULL; |
| 1955 | } |
| 1956 | |
| 1957 | /* Return true if the definition of the given div (recursively) involves |
| 1958 | * any of the given variables. |
| 1959 | */ |
| 1960 | static int div_involves_vars(__isl_keep isl_basic_map *bmap, int div, |
| 1961 | unsigned first, unsigned n) |
| 1962 | { |
| 1963 | int i; |
| 1964 | unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div); |
| 1965 | |
| 1966 | if (isl_int_is_zero(bmap->div[div][0])) |
| 1967 | return 0; |
| 1968 | if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0) |
| 1969 | return 1; |
| 1970 | |
| 1971 | for (i = bmap->n_div - 1; i >= 0; --i) { |
| 1972 | if (isl_int_is_zero(bmap->div[div][1 + div_offset + i])) |
| 1973 | continue; |
| 1974 | if (div_involves_vars(bmap, i, first, n)) |
| 1975 | return 1; |
| 1976 | } |
| 1977 | |
| 1978 | return 0; |
| 1979 | } |
| 1980 | |
| 1981 | /* Try and add a lower and/or upper bound on "div" to "bmap" |
| 1982 | * based on inequality "i". |
| 1983 | * "total" is the total number of variables (excluding the divs). |
| 1984 | * "v" is a temporary object that can be used during the calculations. |
| 1985 | * If "lb" is set, then a lower bound should be constructed. |
| 1986 | * If "ub" is set, then an upper bound should be constructed. |
| 1987 | * |
| 1988 | * The calling function has already checked that the inequality does not |
| 1989 | * reference "div", but we still need to check that the inequality is |
| 1990 | * of the right form. We'll consider the case where we want to construct |
| 1991 | * a lower bound. The construction of upper bounds is similar. |
| 1992 | * |
| 1993 | * Let "div" be of the form |
| 1994 | * |
| 1995 | * q = floor((a + f(x))/d) |
| 1996 | * |
| 1997 | * We essentially check if constraint "i" is of the form |
| 1998 | * |
| 1999 | * b + f(x) >= 0 |
| 2000 | * |
| 2001 | * so that we can use it to derive a lower bound on "div". |
| 2002 | * However, we allow a slightly more general form |
| 2003 | * |
| 2004 | * b + g(x) >= 0 |
| 2005 | * |
| 2006 | * with the condition that the coefficients of g(x) - f(x) are all |
| 2007 | * divisible by d. |
| 2008 | * Rewriting this constraint as |
| 2009 | * |
| 2010 | * 0 >= -b - g(x) |
| 2011 | * |
| 2012 | * adding a + f(x) to both sides and dividing by d, we obtain |
| 2013 | * |
| 2014 | * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d |
| 2015 | * |
| 2016 | * Taking the floor on both sides, we obtain |
| 2017 | * |
| 2018 | * q >= floor((a-b)/d) + (f(x)-g(x))/d |
| 2019 | * |
| 2020 | * or |
| 2021 | * |
| 2022 | * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0 |
| 2023 | * |
| 2024 | * In the case of an upper bound, we construct the constraint |
| 2025 | * |
| 2026 | * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0 |
| 2027 | * |
| 2028 | */ |
| 2029 | static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq( |
| 2030 | __isl_take isl_basic_map *bmap, int div, int i, |
| 2031 | unsigned total, isl_int v, int lb, int ub) |
| 2032 | { |
| 2033 | int j; |
| 2034 | |
| 2035 | for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) { |
| 2036 | if (lb) { |
| 2037 | isl_int_sub(v, bmap->ineq[i][1 + j], |
| 2038 | bmap->div[div][1 + 1 + j]); |
| 2039 | lb = isl_int_is_divisible_by(v, bmap->div[div][0]); |
| 2040 | } |
| 2041 | if (ub) { |
| 2042 | isl_int_add(v, bmap->ineq[i][1 + j], |
| 2043 | bmap->div[div][1 + 1 + j]); |
| 2044 | ub = isl_int_is_divisible_by(v, bmap->div[div][0]); |
| 2045 | } |
| 2046 | } |
| 2047 | if (!lb && !ub) |
| 2048 | return bmap; |
| 2049 | |
| 2050 | bmap = isl_basic_map_cow(bmap); |
| 2051 | bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub); |
| 2052 | if (lb) { |
| 2053 | int k = isl_basic_map_alloc_inequality(bmap); |
| 2054 | if (k < 0) |
| 2055 | goto error; |
| 2056 | for (j = 0; j < 1 + total + bmap->n_div; ++j) { |
| 2057 | isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j], |
| 2058 | bmap->div[div][1 + j]); |
| 2059 | isl_int_cdiv_q(bmap->ineq[k][j], |
| 2060 | bmap->ineq[k][j], bmap->div[div][0]); |
| 2061 | } |
| 2062 | isl_int_set_si(bmap->ineq[k][1 + total + div], 1); |
| 2063 | } |
| 2064 | if (ub) { |
| 2065 | int k = isl_basic_map_alloc_inequality(bmap); |
| 2066 | if (k < 0) |
| 2067 | goto error; |
| 2068 | for (j = 0; j < 1 + total + bmap->n_div; ++j) { |
| 2069 | isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j], |
| 2070 | bmap->div[div][1 + j]); |
| 2071 | isl_int_fdiv_q(bmap->ineq[k][j], |
| 2072 | bmap->ineq[k][j], bmap->div[div][0]); |
| 2073 | } |
| 2074 | isl_int_set_si(bmap->ineq[k][1 + total + div], -1); |
| 2075 | } |
| 2076 | |
| 2077 | ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED); |
| 2078 | return bmap; |
| 2079 | error: |
| 2080 | isl_basic_map_free(bmap); |
| 2081 | return NULL; |
| 2082 | } |
| 2083 | |
| 2084 | /* This function is called right before "div" is eliminated from "bmap" |
| 2085 | * using Fourier-Motzkin. |
| 2086 | * Look through the constraints of "bmap" for constraints on the argument |
| 2087 | * of the integer division and use them to construct constraints on the |
| 2088 | * integer division itself. These constraints can then be combined |
| 2089 | * during the Fourier-Motzkin elimination. |
| 2090 | * Note that it is only useful to introduce lower bounds on "div" |
| 2091 | * if "bmap" already contains upper bounds on "div" as the newly |
| 2092 | * introduce lower bounds can then be combined with the pre-existing |
| 2093 | * upper bounds. Similarly for upper bounds. |
| 2094 | * We therefore first check if "bmap" contains any lower and/or upper bounds |
| 2095 | * on "div". |
| 2096 | * |
| 2097 | * It is interesting to note that the introduction of these constraints |
| 2098 | * can indeed lead to more accurate results, even when compared to |
| 2099 | * deriving constraints on the argument of "div" from constraints on "div". |
| 2100 | * Consider, for example, the set |
| 2101 | * |
| 2102 | * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k } |
| 2103 | * |
| 2104 | * The second constraint can be rewritten as |
| 2105 | * |
| 2106 | * 2 * [(-i-2j+3)/4] + k >= 0 |
| 2107 | * |
| 2108 | * from which we can derive |
| 2109 | * |
| 2110 | * -i - 2j + 3 >= -2k |
| 2111 | * |
| 2112 | * or |
| 2113 | * |
| 2114 | * i + 2j <= 3 + 2k |
| 2115 | * |
| 2116 | * Combined with the first constraint, we obtain |
| 2117 | * |
| 2118 | * -3 <= 3 + 2k or k >= -3 |
| 2119 | * |
| 2120 | * If, on the other hand we derive a constraint on [(i+2j)/4] from |
| 2121 | * the first constraint, we obtain |
| 2122 | * |
| 2123 | * [(i + 2j)/4] >= [-3/4] = -1 |
| 2124 | * |
| 2125 | * Combining this constraint with the second constraint, we obtain |
| 2126 | * |
| 2127 | * k >= -2 |
| 2128 | */ |
| 2129 | static __isl_give isl_basic_map *insert_bounds_on_div( |
| 2130 | __isl_take isl_basic_map *bmap, int div) |
| 2131 | { |
| 2132 | int i; |
| 2133 | int check_lb, check_ub; |
| 2134 | isl_int v; |
| 2135 | unsigned total; |
| 2136 | |
| 2137 | if (!bmap) |
| 2138 | return NULL; |
| 2139 | |
| 2140 | if (isl_int_is_zero(bmap->div[div][0])) |
| 2141 | return bmap; |
| 2142 | |
| 2143 | total = isl_space_dim(bmap->dim, isl_dim_all); |
| 2144 | |
| 2145 | check_lb = 0; |
| 2146 | check_ub = 0; |
| 2147 | for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) { |
| 2148 | int s = isl_int_sgn(bmap->ineq[i][1 + total + div]); |
| 2149 | if (s > 0) |
| 2150 | check_ub = 1; |
| 2151 | if (s < 0) |
| 2152 | check_lb = 1; |
| 2153 | } |
| 2154 | |
| 2155 | if (!check_lb && !check_ub) |
| 2156 | return bmap; |
| 2157 | |
| 2158 | isl_int_init(v); |
| 2159 | |
| 2160 | for (i = 0; bmap && i < bmap->n_ineq; ++i) { |
| 2161 | if (!isl_int_is_zero(bmap->ineq[i][1 + total + div])) |
| 2162 | continue; |
| 2163 | |
| 2164 | bmap = insert_bounds_on_div_from_ineq(bmap, div, i, total, v, |
| 2165 | check_lb, check_ub); |
| 2166 | } |
| 2167 | |
| 2168 | isl_int_clear(v); |
| 2169 | |
| 2170 | return bmap; |
| 2171 | } |
| 2172 | |
| 2173 | /* Remove all divs (recursively) involving any of the given dimensions |
| 2174 | * in their definitions. |
| 2175 | */ |
| 2176 | __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims( |
| 2177 | __isl_take isl_basic_map *bmap, |
| 2178 | enum isl_dim_type type, unsigned first, unsigned n) |
| 2179 | { |
| 2180 | int i; |
| 2181 | |
| 2182 | if (!bmap) |
| 2183 | return NULL; |
| 2184 | isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type), |
| 2185 | goto error); |
| 2186 | first += isl_basic_map_offset(bmap, type); |
| 2187 | |
| 2188 | for (i = bmap->n_div - 1; i >= 0; --i) { |
| 2189 | if (!div_involves_vars(bmap, i, first, n)) |
| 2190 | continue; |
| 2191 | bmap = insert_bounds_on_div(bmap, i); |
| 2192 | bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1); |
| 2193 | if (!bmap) |
| 2194 | return NULL; |
| 2195 | i = bmap->n_div; |
| 2196 | } |
| 2197 | |
| 2198 | return bmap; |
| 2199 | error: |
| 2200 | isl_basic_map_free(bmap); |
| 2201 | return NULL; |
| 2202 | } |
| 2203 | |
| 2204 | __isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims( |
| 2205 | __isl_take isl_basic_set *bset, |
| 2206 | enum isl_dim_type type, unsigned first, unsigned n) |
| 2207 | { |
| 2208 | return isl_basic_map_remove_divs_involving_dims(bset, type, first, n); |
| 2209 | } |
| 2210 | |
| 2211 | __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map, |
| 2212 | enum isl_dim_type type, unsigned first, unsigned n) |
| 2213 | { |
| 2214 | int i; |
| 2215 | |
| 2216 | if (!map) |
| 2217 | return NULL; |
| 2218 | if (map->n == 0) |
| 2219 | return map; |
| 2220 | |
| 2221 | map = isl_map_cow(map); |
| 2222 | if (!map) |
| 2223 | return NULL; |
| 2224 | |
| 2225 | for (i = 0; i < map->n; ++i) { |
| 2226 | map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i], |
| 2227 | type, first, n); |
| 2228 | if (!map->p[i]) |
| 2229 | goto error; |
| 2230 | } |
| 2231 | return map; |
| 2232 | error: |
| 2233 | isl_map_free(map); |
| 2234 | return NULL; |
| 2235 | } |
| 2236 | |
| 2237 | __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set, |
| 2238 | enum isl_dim_type type, unsigned first, unsigned n) |
| 2239 | { |
| 2240 | return (isl_set *)isl_map_remove_divs_involving_dims((isl_map *)set, |
| 2241 | type, first, n); |
| 2242 | } |
| 2243 | |
| 2244 | /* Does the desciption of "bmap" depend on the specified dimensions? |
| 2245 | * We also check whether the dimensions appear in any of the div definitions. |
| 2246 | * In principle there is no need for this check. If the dimensions appear |
| 2247 | * in a div definition, they also appear in the defining constraints of that |
| 2248 | * div. |
| 2249 | */ |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 2250 | isl_bool isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap, |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 2251 | enum isl_dim_type type, unsigned first, unsigned n) |
| 2252 | { |
| 2253 | int i; |
| 2254 | |
| 2255 | if (!bmap) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 2256 | return isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 2257 | |
| 2258 | if (first + n > isl_basic_map_dim(bmap, type)) |
| 2259 | isl_die(bmap->ctx, isl_error_invalid, |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 2260 | "index out of bounds", return isl_bool_error); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 2261 | |
| 2262 | first += isl_basic_map_offset(bmap, type); |
| 2263 | for (i = 0; i < bmap->n_eq; ++i) |
| 2264 | if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 2265 | return isl_bool_true; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 2266 | for (i = 0; i < bmap->n_ineq; ++i) |
| 2267 | if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 2268 | return isl_bool_true; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 2269 | for (i = 0; i < bmap->n_div; ++i) { |
| 2270 | if (isl_int_is_zero(bmap->div[i][0])) |
| 2271 | continue; |
| 2272 | if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 2273 | return isl_bool_true; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 2274 | } |
| 2275 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 2276 | return isl_bool_false; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 2277 | } |
| 2278 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 2279 | isl_bool isl_map_involves_dims(__isl_keep isl_map *map, |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 2280 | enum isl_dim_type type, unsigned first, unsigned n) |
| 2281 | { |
| 2282 | int i; |
| 2283 | |
| 2284 | if (!map) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 2285 | return isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 2286 | |
| 2287 | if (first + n > isl_map_dim(map, type)) |
| 2288 | isl_die(map->ctx, isl_error_invalid, |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 2289 | "index out of bounds", return isl_bool_error); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 2290 | |
| 2291 | for (i = 0; i < map->n; ++i) { |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 2292 | isl_bool involves = isl_basic_map_involves_dims(map->p[i], |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 2293 | type, first, n); |
| 2294 | if (involves < 0 || involves) |
| 2295 | return involves; |
| 2296 | } |
| 2297 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 2298 | return isl_bool_false; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 2299 | } |
| 2300 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 2301 | isl_bool isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset, |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 2302 | enum isl_dim_type type, unsigned first, unsigned n) |
| 2303 | { |
| 2304 | return isl_basic_map_involves_dims(bset, type, first, n); |
| 2305 | } |
| 2306 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 2307 | isl_bool isl_set_involves_dims(__isl_keep isl_set *set, |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 2308 | enum isl_dim_type type, unsigned first, unsigned n) |
| 2309 | { |
| 2310 | return isl_map_involves_dims(set, type, first, n); |
| 2311 | } |
| 2312 | |
| 2313 | /* Return true if the definition of the given div is unknown or depends |
| 2314 | * on unknown divs. |
| 2315 | */ |
| 2316 | static int div_is_unknown(__isl_keep isl_basic_map *bmap, int div) |
| 2317 | { |
| 2318 | int i; |
| 2319 | unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div); |
| 2320 | |
| 2321 | if (isl_int_is_zero(bmap->div[div][0])) |
| 2322 | return 1; |
| 2323 | |
| 2324 | for (i = bmap->n_div - 1; i >= 0; --i) { |
| 2325 | if (isl_int_is_zero(bmap->div[div][1 + div_offset + i])) |
| 2326 | continue; |
| 2327 | if (div_is_unknown(bmap, i)) |
| 2328 | return 1; |
| 2329 | } |
| 2330 | |
| 2331 | return 0; |
| 2332 | } |
| 2333 | |
| 2334 | /* Remove all divs that are unknown or defined in terms of unknown divs. |
| 2335 | */ |
| 2336 | __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs( |
| 2337 | __isl_take isl_basic_map *bmap) |
| 2338 | { |
| 2339 | int i; |
| 2340 | |
| 2341 | if (!bmap) |
| 2342 | return NULL; |
| 2343 | |
| 2344 | for (i = bmap->n_div - 1; i >= 0; --i) { |
| 2345 | if (!div_is_unknown(bmap, i)) |
| 2346 | continue; |
| 2347 | bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1); |
| 2348 | if (!bmap) |
| 2349 | return NULL; |
| 2350 | i = bmap->n_div; |
| 2351 | } |
| 2352 | |
| 2353 | return bmap; |
| 2354 | } |
| 2355 | |
| 2356 | /* Remove all divs that are unknown or defined in terms of unknown divs. |
| 2357 | */ |
| 2358 | __isl_give isl_basic_set *isl_basic_set_remove_unknown_divs( |
| 2359 | __isl_take isl_basic_set *bset) |
| 2360 | { |
| 2361 | return isl_basic_map_remove_unknown_divs(bset); |
| 2362 | } |
| 2363 | |
| 2364 | __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map) |
| 2365 | { |
| 2366 | int i; |
| 2367 | |
| 2368 | if (!map) |
| 2369 | return NULL; |
| 2370 | if (map->n == 0) |
| 2371 | return map; |
| 2372 | |
| 2373 | map = isl_map_cow(map); |
| 2374 | if (!map) |
| 2375 | return NULL; |
| 2376 | |
| 2377 | for (i = 0; i < map->n; ++i) { |
| 2378 | map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]); |
| 2379 | if (!map->p[i]) |
| 2380 | goto error; |
| 2381 | } |
| 2382 | return map; |
| 2383 | error: |
| 2384 | isl_map_free(map); |
| 2385 | return NULL; |
| 2386 | } |
| 2387 | |
| 2388 | __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set) |
| 2389 | { |
| 2390 | return (isl_set *)isl_map_remove_unknown_divs((isl_map *)set); |
| 2391 | } |
| 2392 | |
| 2393 | __isl_give isl_basic_set *isl_basic_set_remove_dims( |
| 2394 | __isl_take isl_basic_set *bset, |
| 2395 | enum isl_dim_type type, unsigned first, unsigned n) |
| 2396 | { |
| 2397 | return (isl_basic_set *) |
| 2398 | isl_basic_map_remove_dims((isl_basic_map *)bset, type, first, n); |
| 2399 | } |
| 2400 | |
| 2401 | struct isl_map *isl_map_remove_dims(struct isl_map *map, |
| 2402 | enum isl_dim_type type, unsigned first, unsigned n) |
| 2403 | { |
| 2404 | int i; |
| 2405 | |
| 2406 | if (n == 0) |
| 2407 | return map; |
| 2408 | |
| 2409 | map = isl_map_cow(map); |
| 2410 | if (!map) |
| 2411 | return NULL; |
| 2412 | isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error); |
| 2413 | |
| 2414 | for (i = 0; i < map->n; ++i) { |
| 2415 | map->p[i] = isl_basic_map_eliminate_vars(map->p[i], |
| 2416 | isl_basic_map_offset(map->p[i], type) - 1 + first, n); |
| 2417 | if (!map->p[i]) |
| 2418 | goto error; |
| 2419 | } |
| 2420 | map = isl_map_drop(map, type, first, n); |
| 2421 | return map; |
| 2422 | error: |
| 2423 | isl_map_free(map); |
| 2424 | return NULL; |
| 2425 | } |
| 2426 | |
| 2427 | __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset, |
| 2428 | enum isl_dim_type type, unsigned first, unsigned n) |
| 2429 | { |
| 2430 | return (isl_set *)isl_map_remove_dims((isl_map *)bset, type, first, n); |
| 2431 | } |
| 2432 | |
| 2433 | /* Project out n inputs starting at first using Fourier-Motzkin */ |
| 2434 | struct isl_map *isl_map_remove_inputs(struct isl_map *map, |
| 2435 | unsigned first, unsigned n) |
| 2436 | { |
| 2437 | return isl_map_remove_dims(map, isl_dim_in, first, n); |
| 2438 | } |
| 2439 | |
| 2440 | static void dump_term(struct isl_basic_map *bmap, |
| 2441 | isl_int c, int pos, FILE *out) |
| 2442 | { |
| 2443 | const char *name; |
| 2444 | unsigned in = isl_basic_map_n_in(bmap); |
| 2445 | unsigned dim = in + isl_basic_map_n_out(bmap); |
| 2446 | unsigned nparam = isl_basic_map_n_param(bmap); |
| 2447 | if (!pos) |
| 2448 | isl_int_print(out, c, 0); |
| 2449 | else { |
| 2450 | if (!isl_int_is_one(c)) |
| 2451 | isl_int_print(out, c, 0); |
| 2452 | if (pos < 1 + nparam) { |
| 2453 | name = isl_space_get_dim_name(bmap->dim, |
| 2454 | isl_dim_param, pos - 1); |
| 2455 | if (name) |
| 2456 | fprintf(out, "%s", name); |
| 2457 | else |
| 2458 | fprintf(out, "p%d", pos - 1); |
| 2459 | } else if (pos < 1 + nparam + in) |
| 2460 | fprintf(out, "i%d", pos - 1 - nparam); |
| 2461 | else if (pos < 1 + nparam + dim) |
| 2462 | fprintf(out, "o%d", pos - 1 - nparam - in); |
| 2463 | else |
| 2464 | fprintf(out, "e%d", pos - 1 - nparam - dim); |
| 2465 | } |
| 2466 | } |
| 2467 | |
| 2468 | static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c, |
| 2469 | int sign, FILE *out) |
| 2470 | { |
| 2471 | int i; |
| 2472 | int first; |
| 2473 | unsigned len = 1 + isl_basic_map_total_dim(bmap); |
| 2474 | isl_int v; |
| 2475 | |
| 2476 | isl_int_init(v); |
| 2477 | for (i = 0, first = 1; i < len; ++i) { |
| 2478 | if (isl_int_sgn(c[i]) * sign <= 0) |
| 2479 | continue; |
| 2480 | if (!first) |
| 2481 | fprintf(out, " + "); |
| 2482 | first = 0; |
| 2483 | isl_int_abs(v, c[i]); |
| 2484 | dump_term(bmap, v, i, out); |
| 2485 | } |
| 2486 | isl_int_clear(v); |
| 2487 | if (first) |
| 2488 | fprintf(out, "0"); |
| 2489 | } |
| 2490 | |
| 2491 | static void dump_constraint(struct isl_basic_map *bmap, isl_int *c, |
| 2492 | const char *op, FILE *out, int indent) |
| 2493 | { |
| 2494 | int i; |
| 2495 | |
| 2496 | fprintf(out, "%*s", indent, ""); |
| 2497 | |
| 2498 | dump_constraint_sign(bmap, c, 1, out); |
| 2499 | fprintf(out, " %s ", op); |
| 2500 | dump_constraint_sign(bmap, c, -1, out); |
| 2501 | |
| 2502 | fprintf(out, "\n"); |
| 2503 | |
| 2504 | for (i = bmap->n_div; i < bmap->extra; ++i) { |
| 2505 | if (isl_int_is_zero(c[1+isl_space_dim(bmap->dim, isl_dim_all)+i])) |
| 2506 | continue; |
| 2507 | fprintf(out, "%*s", indent, ""); |
| 2508 | fprintf(out, "ERROR: unused div coefficient not zero\n"); |
| 2509 | abort(); |
| 2510 | } |
| 2511 | } |
| 2512 | |
| 2513 | static void dump_constraints(struct isl_basic_map *bmap, |
| 2514 | isl_int **c, unsigned n, |
| 2515 | const char *op, FILE *out, int indent) |
| 2516 | { |
| 2517 | int i; |
| 2518 | |
| 2519 | for (i = 0; i < n; ++i) |
| 2520 | dump_constraint(bmap, c[i], op, out, indent); |
| 2521 | } |
| 2522 | |
| 2523 | static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out) |
| 2524 | { |
| 2525 | int j; |
| 2526 | int first = 1; |
| 2527 | unsigned total = isl_basic_map_total_dim(bmap); |
| 2528 | |
| 2529 | for (j = 0; j < 1 + total; ++j) { |
| 2530 | if (isl_int_is_zero(exp[j])) |
| 2531 | continue; |
| 2532 | if (!first && isl_int_is_pos(exp[j])) |
| 2533 | fprintf(out, "+"); |
| 2534 | dump_term(bmap, exp[j], j, out); |
| 2535 | first = 0; |
| 2536 | } |
| 2537 | } |
| 2538 | |
| 2539 | static void dump(struct isl_basic_map *bmap, FILE *out, int indent) |
| 2540 | { |
| 2541 | int i; |
| 2542 | |
| 2543 | dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent); |
| 2544 | dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent); |
| 2545 | |
| 2546 | for (i = 0; i < bmap->n_div; ++i) { |
| 2547 | fprintf(out, "%*s", indent, ""); |
| 2548 | fprintf(out, "e%d = [(", i); |
| 2549 | dump_affine(bmap, bmap->div[i]+1, out); |
| 2550 | fprintf(out, ")/"); |
| 2551 | isl_int_print(out, bmap->div[i][0], 0); |
| 2552 | fprintf(out, "]\n"); |
| 2553 | } |
| 2554 | } |
| 2555 | |
| 2556 | void isl_basic_set_print_internal(struct isl_basic_set *bset, |
| 2557 | FILE *out, int indent) |
| 2558 | { |
| 2559 | if (!bset) { |
| 2560 | fprintf(out, "null basic set\n"); |
| 2561 | return; |
| 2562 | } |
| 2563 | |
| 2564 | fprintf(out, "%*s", indent, ""); |
| 2565 | fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n", |
| 2566 | bset->ref, bset->dim->nparam, bset->dim->n_out, |
| 2567 | bset->extra, bset->flags); |
| 2568 | dump((struct isl_basic_map *)bset, out, indent); |
| 2569 | } |
| 2570 | |
| 2571 | void isl_basic_map_print_internal(struct isl_basic_map *bmap, |
| 2572 | FILE *out, int indent) |
| 2573 | { |
| 2574 | if (!bmap) { |
| 2575 | fprintf(out, "null basic map\n"); |
| 2576 | return; |
| 2577 | } |
| 2578 | |
| 2579 | fprintf(out, "%*s", indent, ""); |
| 2580 | fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, " |
| 2581 | "flags: %x, n_name: %d\n", |
| 2582 | bmap->ref, |
| 2583 | bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out, |
| 2584 | bmap->extra, bmap->flags, bmap->dim->n_id); |
| 2585 | dump(bmap, out, indent); |
| 2586 | } |
| 2587 | |
| 2588 | int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos) |
| 2589 | { |
| 2590 | unsigned total; |
| 2591 | if (!bmap) |
| 2592 | return -1; |
| 2593 | total = isl_basic_map_total_dim(bmap); |
| 2594 | isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1); |
| 2595 | isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total); |
| 2596 | isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1); |
| 2597 | ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED); |
| 2598 | return 0; |
| 2599 | } |
| 2600 | |
Tobias Grosser | 07b2095 | 2016-06-12 04:30:40 +0000 | [diff] [blame^] | 2601 | __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *space, int n, |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 2602 | unsigned flags) |
| 2603 | { |
Tobias Grosser | 07b2095 | 2016-06-12 04:30:40 +0000 | [diff] [blame^] | 2604 | if (!space) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 2605 | return NULL; |
Tobias Grosser | 07b2095 | 2016-06-12 04:30:40 +0000 | [diff] [blame^] | 2606 | if (isl_space_dim(space, isl_dim_in) != 0) |
| 2607 | isl_die(isl_space_get_ctx(space), isl_error_invalid, |
| 2608 | "set cannot have input dimensions", goto error); |
| 2609 | return isl_map_alloc_space(space, n, flags); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 2610 | error: |
Tobias Grosser | 07b2095 | 2016-06-12 04:30:40 +0000 | [diff] [blame^] | 2611 | isl_space_free(space); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 2612 | return NULL; |
| 2613 | } |
| 2614 | |
| 2615 | struct isl_set *isl_set_alloc(struct isl_ctx *ctx, |
| 2616 | unsigned nparam, unsigned dim, int n, unsigned flags) |
| 2617 | { |
| 2618 | struct isl_set *set; |
| 2619 | isl_space *dims; |
| 2620 | |
| 2621 | dims = isl_space_alloc(ctx, nparam, 0, dim); |
| 2622 | if (!dims) |
| 2623 | return NULL; |
| 2624 | |
| 2625 | set = isl_set_alloc_space(dims, n, flags); |
| 2626 | return set; |
| 2627 | } |
| 2628 | |
| 2629 | /* Make sure "map" has room for at least "n" more basic maps. |
| 2630 | */ |
| 2631 | struct isl_map *isl_map_grow(struct isl_map *map, int n) |
| 2632 | { |
| 2633 | int i; |
| 2634 | struct isl_map *grown = NULL; |
| 2635 | |
| 2636 | if (!map) |
| 2637 | return NULL; |
| 2638 | isl_assert(map->ctx, n >= 0, goto error); |
| 2639 | if (map->n + n <= map->size) |
| 2640 | return map; |
| 2641 | grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags); |
| 2642 | if (!grown) |
| 2643 | goto error; |
| 2644 | for (i = 0; i < map->n; ++i) { |
| 2645 | grown->p[i] = isl_basic_map_copy(map->p[i]); |
| 2646 | if (!grown->p[i]) |
| 2647 | goto error; |
| 2648 | grown->n++; |
| 2649 | } |
| 2650 | isl_map_free(map); |
| 2651 | return grown; |
| 2652 | error: |
| 2653 | isl_map_free(grown); |
| 2654 | isl_map_free(map); |
| 2655 | return NULL; |
| 2656 | } |
| 2657 | |
| 2658 | /* Make sure "set" has room for at least "n" more basic sets. |
| 2659 | */ |
| 2660 | struct isl_set *isl_set_grow(struct isl_set *set, int n) |
| 2661 | { |
| 2662 | return (struct isl_set *)isl_map_grow((struct isl_map *)set, n); |
| 2663 | } |
| 2664 | |
| 2665 | struct isl_set *isl_set_dup(struct isl_set *set) |
| 2666 | { |
| 2667 | int i; |
| 2668 | struct isl_set *dup; |
| 2669 | |
| 2670 | if (!set) |
| 2671 | return NULL; |
| 2672 | |
| 2673 | dup = isl_set_alloc_space(isl_space_copy(set->dim), set->n, set->flags); |
| 2674 | if (!dup) |
| 2675 | return NULL; |
| 2676 | for (i = 0; i < set->n; ++i) |
| 2677 | dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i])); |
| 2678 | return dup; |
| 2679 | } |
| 2680 | |
| 2681 | struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset) |
| 2682 | { |
| 2683 | return isl_map_from_basic_map(bset); |
| 2684 | } |
| 2685 | |
| 2686 | struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap) |
| 2687 | { |
| 2688 | struct isl_map *map; |
| 2689 | |
| 2690 | if (!bmap) |
| 2691 | return NULL; |
| 2692 | |
| 2693 | map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT); |
| 2694 | return isl_map_add_basic_map(map, bmap); |
| 2695 | } |
| 2696 | |
| 2697 | __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set, |
| 2698 | __isl_take isl_basic_set *bset) |
| 2699 | { |
| 2700 | return (struct isl_set *)isl_map_add_basic_map((struct isl_map *)set, |
| 2701 | (struct isl_basic_map *)bset); |
| 2702 | } |
| 2703 | |
| 2704 | __isl_null isl_set *isl_set_free(__isl_take isl_set *set) |
| 2705 | { |
Tobias Grosser | 07b2095 | 2016-06-12 04:30:40 +0000 | [diff] [blame^] | 2706 | return isl_map_free(set); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 2707 | } |
| 2708 | |
| 2709 | void isl_set_print_internal(struct isl_set *set, FILE *out, int indent) |
| 2710 | { |
| 2711 | int i; |
| 2712 | |
| 2713 | if (!set) { |
| 2714 | fprintf(out, "null set\n"); |
| 2715 | return; |
| 2716 | } |
| 2717 | |
| 2718 | fprintf(out, "%*s", indent, ""); |
| 2719 | fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n", |
| 2720 | set->ref, set->n, set->dim->nparam, set->dim->n_out, |
| 2721 | set->flags); |
| 2722 | for (i = 0; i < set->n; ++i) { |
| 2723 | fprintf(out, "%*s", indent, ""); |
| 2724 | fprintf(out, "basic set %d:\n", i); |
| 2725 | isl_basic_set_print_internal(set->p[i], out, indent+4); |
| 2726 | } |
| 2727 | } |
| 2728 | |
| 2729 | void isl_map_print_internal(struct isl_map *map, FILE *out, int indent) |
| 2730 | { |
| 2731 | int i; |
| 2732 | |
| 2733 | if (!map) { |
| 2734 | fprintf(out, "null map\n"); |
| 2735 | return; |
| 2736 | } |
| 2737 | |
| 2738 | fprintf(out, "%*s", indent, ""); |
| 2739 | fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, " |
| 2740 | "flags: %x, n_name: %d\n", |
| 2741 | map->ref, map->n, map->dim->nparam, map->dim->n_in, |
| 2742 | map->dim->n_out, map->flags, map->dim->n_id); |
| 2743 | for (i = 0; i < map->n; ++i) { |
| 2744 | fprintf(out, "%*s", indent, ""); |
| 2745 | fprintf(out, "basic map %d:\n", i); |
| 2746 | isl_basic_map_print_internal(map->p[i], out, indent+4); |
| 2747 | } |
| 2748 | } |
| 2749 | |
| 2750 | struct isl_basic_map *isl_basic_map_intersect_domain( |
| 2751 | struct isl_basic_map *bmap, struct isl_basic_set *bset) |
| 2752 | { |
| 2753 | struct isl_basic_map *bmap_domain; |
| 2754 | |
| 2755 | if (!bmap || !bset) |
| 2756 | goto error; |
| 2757 | |
| 2758 | isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param, |
| 2759 | bset->dim, isl_dim_param), goto error); |
| 2760 | |
| 2761 | if (isl_space_dim(bset->dim, isl_dim_set) != 0) |
| 2762 | isl_assert(bset->ctx, |
| 2763 | isl_basic_map_compatible_domain(bmap, bset), goto error); |
| 2764 | |
| 2765 | bmap = isl_basic_map_cow(bmap); |
| 2766 | if (!bmap) |
| 2767 | goto error; |
| 2768 | bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim), |
| 2769 | bset->n_div, bset->n_eq, bset->n_ineq); |
| 2770 | bmap_domain = isl_basic_map_from_domain(bset); |
| 2771 | bmap = add_constraints(bmap, bmap_domain, 0, 0); |
| 2772 | |
| 2773 | bmap = isl_basic_map_simplify(bmap); |
| 2774 | return isl_basic_map_finalize(bmap); |
| 2775 | error: |
| 2776 | isl_basic_map_free(bmap); |
| 2777 | isl_basic_set_free(bset); |
| 2778 | return NULL; |
| 2779 | } |
| 2780 | |
| 2781 | struct isl_basic_map *isl_basic_map_intersect_range( |
| 2782 | struct isl_basic_map *bmap, struct isl_basic_set *bset) |
| 2783 | { |
| 2784 | struct isl_basic_map *bmap_range; |
| 2785 | |
| 2786 | if (!bmap || !bset) |
| 2787 | goto error; |
| 2788 | |
| 2789 | isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param, |
| 2790 | bset->dim, isl_dim_param), goto error); |
| 2791 | |
| 2792 | if (isl_space_dim(bset->dim, isl_dim_set) != 0) |
| 2793 | isl_assert(bset->ctx, |
| 2794 | isl_basic_map_compatible_range(bmap, bset), goto error); |
| 2795 | |
Tobias Grosser | a67ac97 | 2016-02-26 11:35:12 +0000 | [diff] [blame] | 2796 | if (isl_basic_set_plain_is_universe(bset)) { |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 2797 | isl_basic_set_free(bset); |
| 2798 | return bmap; |
| 2799 | } |
| 2800 | |
| 2801 | bmap = isl_basic_map_cow(bmap); |
| 2802 | if (!bmap) |
| 2803 | goto error; |
| 2804 | bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim), |
| 2805 | bset->n_div, bset->n_eq, bset->n_ineq); |
| 2806 | bmap_range = isl_basic_map_from_basic_set(bset, isl_space_copy(bset->dim)); |
| 2807 | bmap = add_constraints(bmap, bmap_range, 0, 0); |
| 2808 | |
| 2809 | bmap = isl_basic_map_simplify(bmap); |
| 2810 | return isl_basic_map_finalize(bmap); |
| 2811 | error: |
| 2812 | isl_basic_map_free(bmap); |
| 2813 | isl_basic_set_free(bset); |
| 2814 | return NULL; |
| 2815 | } |
| 2816 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 2817 | isl_bool isl_basic_map_contains(__isl_keep isl_basic_map *bmap, |
| 2818 | __isl_keep isl_vec *vec) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 2819 | { |
| 2820 | int i; |
| 2821 | unsigned total; |
| 2822 | isl_int s; |
| 2823 | |
| 2824 | if (!bmap || !vec) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 2825 | return isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 2826 | |
| 2827 | total = 1 + isl_basic_map_total_dim(bmap); |
| 2828 | if (total != vec->size) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 2829 | return isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 2830 | |
| 2831 | isl_int_init(s); |
| 2832 | |
| 2833 | for (i = 0; i < bmap->n_eq; ++i) { |
| 2834 | isl_seq_inner_product(vec->el, bmap->eq[i], total, &s); |
| 2835 | if (!isl_int_is_zero(s)) { |
| 2836 | isl_int_clear(s); |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 2837 | return isl_bool_false; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 2838 | } |
| 2839 | } |
| 2840 | |
| 2841 | for (i = 0; i < bmap->n_ineq; ++i) { |
| 2842 | isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s); |
| 2843 | if (isl_int_is_neg(s)) { |
| 2844 | isl_int_clear(s); |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 2845 | return isl_bool_false; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 2846 | } |
| 2847 | } |
| 2848 | |
| 2849 | isl_int_clear(s); |
| 2850 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 2851 | return isl_bool_true; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 2852 | } |
| 2853 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 2854 | isl_bool isl_basic_set_contains(__isl_keep isl_basic_set *bset, |
| 2855 | __isl_keep isl_vec *vec) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 2856 | { |
| 2857 | return isl_basic_map_contains((struct isl_basic_map *)bset, vec); |
| 2858 | } |
| 2859 | |
| 2860 | struct isl_basic_map *isl_basic_map_intersect( |
| 2861 | struct isl_basic_map *bmap1, struct isl_basic_map *bmap2) |
| 2862 | { |
| 2863 | struct isl_vec *sample = NULL; |
| 2864 | |
| 2865 | if (!bmap1 || !bmap2) |
| 2866 | goto error; |
| 2867 | |
| 2868 | isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param, |
| 2869 | bmap2->dim, isl_dim_param), goto error); |
| 2870 | if (isl_space_dim(bmap1->dim, isl_dim_all) == |
| 2871 | isl_space_dim(bmap1->dim, isl_dim_param) && |
| 2872 | isl_space_dim(bmap2->dim, isl_dim_all) != |
| 2873 | isl_space_dim(bmap2->dim, isl_dim_param)) |
| 2874 | return isl_basic_map_intersect(bmap2, bmap1); |
| 2875 | |
| 2876 | if (isl_space_dim(bmap2->dim, isl_dim_all) != |
| 2877 | isl_space_dim(bmap2->dim, isl_dim_param)) |
| 2878 | isl_assert(bmap1->ctx, |
| 2879 | isl_space_is_equal(bmap1->dim, bmap2->dim), goto error); |
| 2880 | |
Tobias Grosser | fb3fb0a | 2015-11-21 20:48:39 +0000 | [diff] [blame] | 2881 | if (isl_basic_map_plain_is_empty(bmap1)) { |
| 2882 | isl_basic_map_free(bmap2); |
| 2883 | return bmap1; |
| 2884 | } |
| 2885 | if (isl_basic_map_plain_is_empty(bmap2)) { |
| 2886 | isl_basic_map_free(bmap1); |
| 2887 | return bmap2; |
| 2888 | } |
| 2889 | |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 2890 | if (bmap1->sample && |
| 2891 | isl_basic_map_contains(bmap1, bmap1->sample) > 0 && |
| 2892 | isl_basic_map_contains(bmap2, bmap1->sample) > 0) |
| 2893 | sample = isl_vec_copy(bmap1->sample); |
| 2894 | else if (bmap2->sample && |
| 2895 | isl_basic_map_contains(bmap1, bmap2->sample) > 0 && |
| 2896 | isl_basic_map_contains(bmap2, bmap2->sample) > 0) |
| 2897 | sample = isl_vec_copy(bmap2->sample); |
| 2898 | |
| 2899 | bmap1 = isl_basic_map_cow(bmap1); |
| 2900 | if (!bmap1) |
| 2901 | goto error; |
| 2902 | bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim), |
| 2903 | bmap2->n_div, bmap2->n_eq, bmap2->n_ineq); |
| 2904 | bmap1 = add_constraints(bmap1, bmap2, 0, 0); |
| 2905 | |
| 2906 | if (!bmap1) |
| 2907 | isl_vec_free(sample); |
| 2908 | else if (sample) { |
| 2909 | isl_vec_free(bmap1->sample); |
| 2910 | bmap1->sample = sample; |
| 2911 | } |
| 2912 | |
| 2913 | bmap1 = isl_basic_map_simplify(bmap1); |
| 2914 | return isl_basic_map_finalize(bmap1); |
| 2915 | error: |
| 2916 | if (sample) |
| 2917 | isl_vec_free(sample); |
| 2918 | isl_basic_map_free(bmap1); |
| 2919 | isl_basic_map_free(bmap2); |
| 2920 | return NULL; |
| 2921 | } |
| 2922 | |
| 2923 | struct isl_basic_set *isl_basic_set_intersect( |
| 2924 | struct isl_basic_set *bset1, struct isl_basic_set *bset2) |
| 2925 | { |
| 2926 | return (struct isl_basic_set *) |
| 2927 | isl_basic_map_intersect( |
| 2928 | (struct isl_basic_map *)bset1, |
| 2929 | (struct isl_basic_map *)bset2); |
| 2930 | } |
| 2931 | |
| 2932 | __isl_give isl_basic_set *isl_basic_set_intersect_params( |
| 2933 | __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2) |
| 2934 | { |
| 2935 | return isl_basic_set_intersect(bset1, bset2); |
| 2936 | } |
| 2937 | |
| 2938 | /* Special case of isl_map_intersect, where both map1 and map2 |
| 2939 | * are convex, without any divs and such that either map1 or map2 |
| 2940 | * contains a single constraint. This constraint is then simply |
| 2941 | * added to the other map. |
| 2942 | */ |
| 2943 | static __isl_give isl_map *map_intersect_add_constraint( |
| 2944 | __isl_take isl_map *map1, __isl_take isl_map *map2) |
| 2945 | { |
| 2946 | isl_assert(map1->ctx, map1->n == 1, goto error); |
| 2947 | isl_assert(map2->ctx, map1->n == 1, goto error); |
| 2948 | isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error); |
| 2949 | isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error); |
| 2950 | |
| 2951 | if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1) |
| 2952 | return isl_map_intersect(map2, map1); |
| 2953 | |
| 2954 | isl_assert(map2->ctx, |
| 2955 | map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error); |
| 2956 | |
| 2957 | map1 = isl_map_cow(map1); |
| 2958 | if (!map1) |
| 2959 | goto error; |
| 2960 | if (isl_map_plain_is_empty(map1)) { |
| 2961 | isl_map_free(map2); |
| 2962 | return map1; |
| 2963 | } |
| 2964 | map1->p[0] = isl_basic_map_cow(map1->p[0]); |
| 2965 | if (map2->p[0]->n_eq == 1) |
| 2966 | map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]); |
| 2967 | else |
| 2968 | map1->p[0] = isl_basic_map_add_ineq(map1->p[0], |
| 2969 | map2->p[0]->ineq[0]); |
| 2970 | |
| 2971 | map1->p[0] = isl_basic_map_simplify(map1->p[0]); |
| 2972 | map1->p[0] = isl_basic_map_finalize(map1->p[0]); |
| 2973 | if (!map1->p[0]) |
| 2974 | goto error; |
| 2975 | |
| 2976 | if (isl_basic_map_plain_is_empty(map1->p[0])) { |
| 2977 | isl_basic_map_free(map1->p[0]); |
| 2978 | map1->n = 0; |
| 2979 | } |
| 2980 | |
| 2981 | isl_map_free(map2); |
| 2982 | |
| 2983 | return map1; |
| 2984 | error: |
| 2985 | isl_map_free(map1); |
| 2986 | isl_map_free(map2); |
| 2987 | return NULL; |
| 2988 | } |
| 2989 | |
| 2990 | /* map2 may be either a parameter domain or a map living in the same |
| 2991 | * space as map1. |
| 2992 | */ |
| 2993 | static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1, |
| 2994 | __isl_take isl_map *map2) |
| 2995 | { |
| 2996 | unsigned flags = 0; |
| 2997 | isl_map *result; |
| 2998 | int i, j; |
| 2999 | |
| 3000 | if (!map1 || !map2) |
| 3001 | goto error; |
| 3002 | |
| 3003 | if ((isl_map_plain_is_empty(map1) || |
| 3004 | isl_map_plain_is_universe(map2)) && |
| 3005 | isl_space_is_equal(map1->dim, map2->dim)) { |
| 3006 | isl_map_free(map2); |
| 3007 | return map1; |
| 3008 | } |
| 3009 | if ((isl_map_plain_is_empty(map2) || |
| 3010 | isl_map_plain_is_universe(map1)) && |
| 3011 | isl_space_is_equal(map1->dim, map2->dim)) { |
| 3012 | isl_map_free(map1); |
| 3013 | return map2; |
| 3014 | } |
| 3015 | |
| 3016 | if (map1->n == 1 && map2->n == 1 && |
| 3017 | map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 && |
| 3018 | isl_space_is_equal(map1->dim, map2->dim) && |
| 3019 | (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 || |
| 3020 | map2->p[0]->n_eq + map2->p[0]->n_ineq == 1)) |
| 3021 | return map_intersect_add_constraint(map1, map2); |
| 3022 | |
| 3023 | if (isl_space_dim(map2->dim, isl_dim_all) != |
| 3024 | isl_space_dim(map2->dim, isl_dim_param)) |
| 3025 | isl_assert(map1->ctx, |
| 3026 | isl_space_is_equal(map1->dim, map2->dim), goto error); |
| 3027 | |
| 3028 | if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) && |
| 3029 | ISL_F_ISSET(map2, ISL_MAP_DISJOINT)) |
| 3030 | ISL_FL_SET(flags, ISL_MAP_DISJOINT); |
| 3031 | |
| 3032 | result = isl_map_alloc_space(isl_space_copy(map1->dim), |
| 3033 | map1->n * map2->n, flags); |
| 3034 | if (!result) |
| 3035 | goto error; |
| 3036 | for (i = 0; i < map1->n; ++i) |
| 3037 | for (j = 0; j < map2->n; ++j) { |
| 3038 | struct isl_basic_map *part; |
| 3039 | part = isl_basic_map_intersect( |
| 3040 | isl_basic_map_copy(map1->p[i]), |
| 3041 | isl_basic_map_copy(map2->p[j])); |
| 3042 | if (isl_basic_map_is_empty(part) < 0) |
| 3043 | part = isl_basic_map_free(part); |
| 3044 | result = isl_map_add_basic_map(result, part); |
| 3045 | if (!result) |
| 3046 | goto error; |
| 3047 | } |
| 3048 | isl_map_free(map1); |
| 3049 | isl_map_free(map2); |
| 3050 | return result; |
| 3051 | error: |
| 3052 | isl_map_free(map1); |
| 3053 | isl_map_free(map2); |
| 3054 | return NULL; |
| 3055 | } |
| 3056 | |
| 3057 | static __isl_give isl_map *map_intersect(__isl_take isl_map *map1, |
| 3058 | __isl_take isl_map *map2) |
| 3059 | { |
| 3060 | if (!map1 || !map2) |
| 3061 | goto error; |
| 3062 | if (!isl_space_is_equal(map1->dim, map2->dim)) |
| 3063 | isl_die(isl_map_get_ctx(map1), isl_error_invalid, |
| 3064 | "spaces don't match", goto error); |
| 3065 | return map_intersect_internal(map1, map2); |
| 3066 | error: |
| 3067 | isl_map_free(map1); |
| 3068 | isl_map_free(map2); |
| 3069 | return NULL; |
| 3070 | } |
| 3071 | |
| 3072 | __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1, |
| 3073 | __isl_take isl_map *map2) |
| 3074 | { |
| 3075 | return isl_map_align_params_map_map_and(map1, map2, &map_intersect); |
| 3076 | } |
| 3077 | |
| 3078 | struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2) |
| 3079 | { |
| 3080 | return (struct isl_set *) |
| 3081 | isl_map_intersect((struct isl_map *)set1, |
| 3082 | (struct isl_map *)set2); |
| 3083 | } |
| 3084 | |
| 3085 | /* map_intersect_internal accepts intersections |
| 3086 | * with parameter domains, so we can just call that function. |
| 3087 | */ |
| 3088 | static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map, |
| 3089 | __isl_take isl_set *params) |
| 3090 | { |
| 3091 | return map_intersect_internal(map, params); |
| 3092 | } |
| 3093 | |
| 3094 | __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1, |
| 3095 | __isl_take isl_map *map2) |
| 3096 | { |
| 3097 | return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params); |
| 3098 | } |
| 3099 | |
| 3100 | __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set, |
| 3101 | __isl_take isl_set *params) |
| 3102 | { |
| 3103 | return isl_map_intersect_params(set, params); |
| 3104 | } |
| 3105 | |
| 3106 | struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap) |
| 3107 | { |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 3108 | isl_space *space; |
| 3109 | unsigned pos, n1, n2; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 3110 | |
| 3111 | if (!bmap) |
| 3112 | return NULL; |
| 3113 | bmap = isl_basic_map_cow(bmap); |
| 3114 | if (!bmap) |
| 3115 | return NULL; |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 3116 | space = isl_space_reverse(isl_space_copy(bmap->dim)); |
| 3117 | pos = isl_basic_map_offset(bmap, isl_dim_in); |
| 3118 | n1 = isl_basic_map_dim(bmap, isl_dim_in); |
| 3119 | n2 = isl_basic_map_dim(bmap, isl_dim_out); |
| 3120 | bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2); |
| 3121 | return isl_basic_map_reset_space(bmap, space); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 3122 | } |
| 3123 | |
| 3124 | static __isl_give isl_basic_map *basic_map_space_reset( |
| 3125 | __isl_take isl_basic_map *bmap, enum isl_dim_type type) |
| 3126 | { |
| 3127 | isl_space *space; |
| 3128 | |
| 3129 | if (!bmap) |
| 3130 | return NULL; |
| 3131 | if (!isl_space_is_named_or_nested(bmap->dim, type)) |
| 3132 | return bmap; |
| 3133 | |
| 3134 | space = isl_basic_map_get_space(bmap); |
| 3135 | space = isl_space_reset(space, type); |
| 3136 | bmap = isl_basic_map_reset_space(bmap, space); |
| 3137 | return bmap; |
| 3138 | } |
| 3139 | |
| 3140 | __isl_give isl_basic_map *isl_basic_map_insert_dims( |
| 3141 | __isl_take isl_basic_map *bmap, enum isl_dim_type type, |
| 3142 | unsigned pos, unsigned n) |
| 3143 | { |
| 3144 | isl_space *res_dim; |
| 3145 | struct isl_basic_map *res; |
| 3146 | struct isl_dim_map *dim_map; |
| 3147 | unsigned total, off; |
| 3148 | enum isl_dim_type t; |
| 3149 | |
| 3150 | if (n == 0) |
| 3151 | return basic_map_space_reset(bmap, type); |
| 3152 | |
| 3153 | if (!bmap) |
| 3154 | return NULL; |
| 3155 | |
| 3156 | res_dim = isl_space_insert_dims(isl_basic_map_get_space(bmap), type, pos, n); |
| 3157 | |
| 3158 | total = isl_basic_map_total_dim(bmap) + n; |
| 3159 | dim_map = isl_dim_map_alloc(bmap->ctx, total); |
| 3160 | off = 0; |
| 3161 | for (t = isl_dim_param; t <= isl_dim_out; ++t) { |
| 3162 | if (t != type) { |
| 3163 | isl_dim_map_dim(dim_map, bmap->dim, t, off); |
| 3164 | } else { |
| 3165 | unsigned size = isl_basic_map_dim(bmap, t); |
| 3166 | isl_dim_map_dim_range(dim_map, bmap->dim, t, |
| 3167 | 0, pos, off); |
| 3168 | isl_dim_map_dim_range(dim_map, bmap->dim, t, |
| 3169 | pos, size - pos, off + pos + n); |
| 3170 | } |
| 3171 | off += isl_space_dim(res_dim, t); |
| 3172 | } |
| 3173 | isl_dim_map_div(dim_map, bmap, off); |
| 3174 | |
| 3175 | res = isl_basic_map_alloc_space(res_dim, |
| 3176 | bmap->n_div, bmap->n_eq, bmap->n_ineq); |
| 3177 | if (isl_basic_map_is_rational(bmap)) |
| 3178 | res = isl_basic_map_set_rational(res); |
| 3179 | if (isl_basic_map_plain_is_empty(bmap)) { |
| 3180 | isl_basic_map_free(bmap); |
| 3181 | free(dim_map); |
| 3182 | return isl_basic_map_set_to_empty(res); |
| 3183 | } |
| 3184 | res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map); |
| 3185 | return isl_basic_map_finalize(res); |
| 3186 | } |
| 3187 | |
| 3188 | __isl_give isl_basic_set *isl_basic_set_insert_dims( |
| 3189 | __isl_take isl_basic_set *bset, |
| 3190 | enum isl_dim_type type, unsigned pos, unsigned n) |
| 3191 | { |
| 3192 | return isl_basic_map_insert_dims(bset, type, pos, n); |
| 3193 | } |
| 3194 | |
Tobias Grosser | e0f8d59 | 2015-05-13 13:10:13 +0000 | [diff] [blame] | 3195 | __isl_give isl_basic_map *isl_basic_map_add_dims(__isl_take isl_basic_map *bmap, |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 3196 | enum isl_dim_type type, unsigned n) |
| 3197 | { |
| 3198 | if (!bmap) |
| 3199 | return NULL; |
| 3200 | return isl_basic_map_insert_dims(bmap, type, |
| 3201 | isl_basic_map_dim(bmap, type), n); |
| 3202 | } |
| 3203 | |
| 3204 | __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset, |
| 3205 | enum isl_dim_type type, unsigned n) |
| 3206 | { |
| 3207 | if (!bset) |
| 3208 | return NULL; |
| 3209 | isl_assert(bset->ctx, type != isl_dim_in, goto error); |
Tobias Grosser | e0f8d59 | 2015-05-13 13:10:13 +0000 | [diff] [blame] | 3210 | return isl_basic_map_add_dims(bset, type, n); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 3211 | error: |
| 3212 | isl_basic_set_free(bset); |
| 3213 | return NULL; |
| 3214 | } |
| 3215 | |
| 3216 | static __isl_give isl_map *map_space_reset(__isl_take isl_map *map, |
| 3217 | enum isl_dim_type type) |
| 3218 | { |
| 3219 | isl_space *space; |
| 3220 | |
| 3221 | if (!map || !isl_space_is_named_or_nested(map->dim, type)) |
| 3222 | return map; |
| 3223 | |
| 3224 | space = isl_map_get_space(map); |
| 3225 | space = isl_space_reset(space, type); |
| 3226 | map = isl_map_reset_space(map, space); |
| 3227 | return map; |
| 3228 | } |
| 3229 | |
| 3230 | __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map, |
| 3231 | enum isl_dim_type type, unsigned pos, unsigned n) |
| 3232 | { |
| 3233 | int i; |
| 3234 | |
| 3235 | if (n == 0) |
| 3236 | return map_space_reset(map, type); |
| 3237 | |
| 3238 | map = isl_map_cow(map); |
| 3239 | if (!map) |
| 3240 | return NULL; |
| 3241 | |
| 3242 | map->dim = isl_space_insert_dims(map->dim, type, pos, n); |
| 3243 | if (!map->dim) |
| 3244 | goto error; |
| 3245 | |
| 3246 | for (i = 0; i < map->n; ++i) { |
| 3247 | map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n); |
| 3248 | if (!map->p[i]) |
| 3249 | goto error; |
| 3250 | } |
| 3251 | |
| 3252 | return map; |
| 3253 | error: |
| 3254 | isl_map_free(map); |
| 3255 | return NULL; |
| 3256 | } |
| 3257 | |
| 3258 | __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set, |
| 3259 | enum isl_dim_type type, unsigned pos, unsigned n) |
| 3260 | { |
| 3261 | return isl_map_insert_dims(set, type, pos, n); |
| 3262 | } |
| 3263 | |
| 3264 | __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map, |
| 3265 | enum isl_dim_type type, unsigned n) |
| 3266 | { |
| 3267 | if (!map) |
| 3268 | return NULL; |
| 3269 | return isl_map_insert_dims(map, type, isl_map_dim(map, type), n); |
| 3270 | } |
| 3271 | |
| 3272 | __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set, |
| 3273 | enum isl_dim_type type, unsigned n) |
| 3274 | { |
| 3275 | if (!set) |
| 3276 | return NULL; |
| 3277 | isl_assert(set->ctx, type != isl_dim_in, goto error); |
| 3278 | return (isl_set *)isl_map_add_dims((isl_map *)set, type, n); |
| 3279 | error: |
| 3280 | isl_set_free(set); |
| 3281 | return NULL; |
| 3282 | } |
| 3283 | |
| 3284 | __isl_give isl_basic_map *isl_basic_map_move_dims( |
| 3285 | __isl_take isl_basic_map *bmap, |
| 3286 | enum isl_dim_type dst_type, unsigned dst_pos, |
| 3287 | enum isl_dim_type src_type, unsigned src_pos, unsigned n) |
| 3288 | { |
| 3289 | struct isl_dim_map *dim_map; |
| 3290 | struct isl_basic_map *res; |
| 3291 | enum isl_dim_type t; |
| 3292 | unsigned total, off; |
| 3293 | |
| 3294 | if (!bmap) |
| 3295 | return NULL; |
| 3296 | if (n == 0) |
| 3297 | return bmap; |
| 3298 | |
| 3299 | isl_assert(bmap->ctx, src_pos + n <= isl_basic_map_dim(bmap, src_type), |
| 3300 | goto error); |
| 3301 | |
| 3302 | if (dst_type == src_type && dst_pos == src_pos) |
| 3303 | return bmap; |
| 3304 | |
| 3305 | isl_assert(bmap->ctx, dst_type != src_type, goto error); |
| 3306 | |
| 3307 | if (pos(bmap->dim, dst_type) + dst_pos == |
| 3308 | pos(bmap->dim, src_type) + src_pos + |
| 3309 | ((src_type < dst_type) ? n : 0)) { |
| 3310 | bmap = isl_basic_map_cow(bmap); |
| 3311 | if (!bmap) |
| 3312 | return NULL; |
| 3313 | |
| 3314 | bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos, |
| 3315 | src_type, src_pos, n); |
| 3316 | if (!bmap->dim) |
| 3317 | goto error; |
| 3318 | |
| 3319 | bmap = isl_basic_map_finalize(bmap); |
| 3320 | |
| 3321 | return bmap; |
| 3322 | } |
| 3323 | |
| 3324 | total = isl_basic_map_total_dim(bmap); |
| 3325 | dim_map = isl_dim_map_alloc(bmap->ctx, total); |
| 3326 | |
| 3327 | off = 0; |
| 3328 | for (t = isl_dim_param; t <= isl_dim_out; ++t) { |
| 3329 | unsigned size = isl_space_dim(bmap->dim, t); |
| 3330 | if (t == dst_type) { |
| 3331 | isl_dim_map_dim_range(dim_map, bmap->dim, t, |
| 3332 | 0, dst_pos, off); |
| 3333 | off += dst_pos; |
| 3334 | isl_dim_map_dim_range(dim_map, bmap->dim, src_type, |
| 3335 | src_pos, n, off); |
| 3336 | off += n; |
| 3337 | isl_dim_map_dim_range(dim_map, bmap->dim, t, |
| 3338 | dst_pos, size - dst_pos, off); |
| 3339 | off += size - dst_pos; |
| 3340 | } else if (t == src_type) { |
| 3341 | isl_dim_map_dim_range(dim_map, bmap->dim, t, |
| 3342 | 0, src_pos, off); |
| 3343 | off += src_pos; |
| 3344 | isl_dim_map_dim_range(dim_map, bmap->dim, t, |
| 3345 | src_pos + n, size - src_pos - n, off); |
| 3346 | off += size - src_pos - n; |
| 3347 | } else { |
| 3348 | isl_dim_map_dim(dim_map, bmap->dim, t, off); |
| 3349 | off += size; |
| 3350 | } |
| 3351 | } |
| 3352 | isl_dim_map_div(dim_map, bmap, off); |
| 3353 | |
| 3354 | res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap), |
| 3355 | bmap->n_div, bmap->n_eq, bmap->n_ineq); |
| 3356 | bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map); |
| 3357 | if (!bmap) |
| 3358 | goto error; |
| 3359 | |
| 3360 | bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos, |
| 3361 | src_type, src_pos, n); |
| 3362 | if (!bmap->dim) |
| 3363 | goto error; |
| 3364 | |
| 3365 | ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED); |
| 3366 | bmap = isl_basic_map_gauss(bmap, NULL); |
| 3367 | bmap = isl_basic_map_finalize(bmap); |
| 3368 | |
| 3369 | return bmap; |
| 3370 | error: |
| 3371 | isl_basic_map_free(bmap); |
| 3372 | return NULL; |
| 3373 | } |
| 3374 | |
| 3375 | __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset, |
| 3376 | enum isl_dim_type dst_type, unsigned dst_pos, |
| 3377 | enum isl_dim_type src_type, unsigned src_pos, unsigned n) |
| 3378 | { |
| 3379 | return (isl_basic_set *)isl_basic_map_move_dims( |
| 3380 | (isl_basic_map *)bset, dst_type, dst_pos, src_type, src_pos, n); |
| 3381 | } |
| 3382 | |
| 3383 | __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set, |
| 3384 | enum isl_dim_type dst_type, unsigned dst_pos, |
| 3385 | enum isl_dim_type src_type, unsigned src_pos, unsigned n) |
| 3386 | { |
| 3387 | if (!set) |
| 3388 | return NULL; |
| 3389 | isl_assert(set->ctx, dst_type != isl_dim_in, goto error); |
| 3390 | return (isl_set *)isl_map_move_dims((isl_map *)set, dst_type, dst_pos, |
| 3391 | src_type, src_pos, n); |
| 3392 | error: |
| 3393 | isl_set_free(set); |
| 3394 | return NULL; |
| 3395 | } |
| 3396 | |
| 3397 | __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map, |
| 3398 | enum isl_dim_type dst_type, unsigned dst_pos, |
| 3399 | enum isl_dim_type src_type, unsigned src_pos, unsigned n) |
| 3400 | { |
| 3401 | int i; |
| 3402 | |
| 3403 | if (!map) |
| 3404 | return NULL; |
| 3405 | if (n == 0) |
| 3406 | return map; |
| 3407 | |
| 3408 | isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type), |
| 3409 | goto error); |
| 3410 | |
| 3411 | if (dst_type == src_type && dst_pos == src_pos) |
| 3412 | return map; |
| 3413 | |
| 3414 | isl_assert(map->ctx, dst_type != src_type, goto error); |
| 3415 | |
| 3416 | map = isl_map_cow(map); |
| 3417 | if (!map) |
| 3418 | return NULL; |
| 3419 | |
| 3420 | map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n); |
| 3421 | if (!map->dim) |
| 3422 | goto error; |
| 3423 | |
| 3424 | for (i = 0; i < map->n; ++i) { |
| 3425 | map->p[i] = isl_basic_map_move_dims(map->p[i], |
| 3426 | dst_type, dst_pos, |
| 3427 | src_type, src_pos, n); |
| 3428 | if (!map->p[i]) |
| 3429 | goto error; |
| 3430 | } |
| 3431 | |
| 3432 | return map; |
| 3433 | error: |
| 3434 | isl_map_free(map); |
| 3435 | return NULL; |
| 3436 | } |
| 3437 | |
| 3438 | /* Move the specified dimensions to the last columns right before |
| 3439 | * the divs. Don't change the dimension specification of bmap. |
| 3440 | * That's the responsibility of the caller. |
| 3441 | */ |
| 3442 | static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap, |
| 3443 | enum isl_dim_type type, unsigned first, unsigned n) |
| 3444 | { |
| 3445 | struct isl_dim_map *dim_map; |
| 3446 | struct isl_basic_map *res; |
| 3447 | enum isl_dim_type t; |
| 3448 | unsigned total, off; |
| 3449 | |
| 3450 | if (!bmap) |
| 3451 | return NULL; |
| 3452 | if (pos(bmap->dim, type) + first + n == |
| 3453 | 1 + isl_space_dim(bmap->dim, isl_dim_all)) |
| 3454 | return bmap; |
| 3455 | |
| 3456 | total = isl_basic_map_total_dim(bmap); |
| 3457 | dim_map = isl_dim_map_alloc(bmap->ctx, total); |
| 3458 | |
| 3459 | off = 0; |
| 3460 | for (t = isl_dim_param; t <= isl_dim_out; ++t) { |
| 3461 | unsigned size = isl_space_dim(bmap->dim, t); |
| 3462 | if (t == type) { |
| 3463 | isl_dim_map_dim_range(dim_map, bmap->dim, t, |
| 3464 | 0, first, off); |
| 3465 | off += first; |
| 3466 | isl_dim_map_dim_range(dim_map, bmap->dim, t, |
| 3467 | first, n, total - bmap->n_div - n); |
| 3468 | isl_dim_map_dim_range(dim_map, bmap->dim, t, |
| 3469 | first + n, size - (first + n), off); |
| 3470 | off += size - (first + n); |
| 3471 | } else { |
| 3472 | isl_dim_map_dim(dim_map, bmap->dim, t, off); |
| 3473 | off += size; |
| 3474 | } |
| 3475 | } |
| 3476 | isl_dim_map_div(dim_map, bmap, off + n); |
| 3477 | |
| 3478 | res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap), |
| 3479 | bmap->n_div, bmap->n_eq, bmap->n_ineq); |
| 3480 | res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map); |
| 3481 | return res; |
| 3482 | } |
| 3483 | |
| 3484 | /* Insert "n" rows in the divs of "bmap". |
| 3485 | * |
| 3486 | * The number of columns is not changed, which means that the last |
| 3487 | * dimensions of "bmap" are being reintepreted as the new divs. |
| 3488 | * The space of "bmap" is not adjusted, however, which means |
| 3489 | * that "bmap" is left in an inconsistent state. Removing "n" dimensions |
| 3490 | * from the space of "bmap" is the responsibility of the caller. |
| 3491 | */ |
| 3492 | static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap, |
| 3493 | int n) |
| 3494 | { |
| 3495 | int i; |
| 3496 | size_t row_size; |
| 3497 | isl_int **new_div; |
| 3498 | isl_int *old; |
| 3499 | |
| 3500 | bmap = isl_basic_map_cow(bmap); |
| 3501 | if (!bmap) |
| 3502 | return NULL; |
| 3503 | |
| 3504 | row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra; |
| 3505 | old = bmap->block2.data; |
| 3506 | bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2, |
| 3507 | (bmap->extra + n) * (1 + row_size)); |
| 3508 | if (!bmap->block2.data) |
| 3509 | return isl_basic_map_free(bmap); |
| 3510 | new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n); |
| 3511 | if (!new_div) |
| 3512 | return isl_basic_map_free(bmap); |
| 3513 | for (i = 0; i < n; ++i) { |
| 3514 | new_div[i] = bmap->block2.data + |
| 3515 | (bmap->extra + i) * (1 + row_size); |
| 3516 | isl_seq_clr(new_div[i], 1 + row_size); |
| 3517 | } |
| 3518 | for (i = 0; i < bmap->extra; ++i) |
| 3519 | new_div[n + i] = bmap->block2.data + (bmap->div[i] - old); |
| 3520 | free(bmap->div); |
| 3521 | bmap->div = new_div; |
| 3522 | bmap->n_div += n; |
| 3523 | bmap->extra += n; |
| 3524 | |
| 3525 | return bmap; |
| 3526 | } |
| 3527 | |
Tobias Grosser | a67ac97 | 2016-02-26 11:35:12 +0000 | [diff] [blame] | 3528 | /* Drop constraints from "bmap" that only involve the variables |
| 3529 | * of "type" in the range [first, first + n] that are not related |
| 3530 | * to any of the variables outside that interval. |
| 3531 | * These constraints cannot influence the values for the variables |
| 3532 | * outside the interval, except in case they cause "bmap" to be empty. |
| 3533 | * Only drop the constraints if "bmap" is known to be non-empty. |
| 3534 | */ |
| 3535 | static __isl_give isl_basic_map *drop_irrelevant_constraints( |
| 3536 | __isl_take isl_basic_map *bmap, enum isl_dim_type type, |
| 3537 | unsigned first, unsigned n) |
| 3538 | { |
| 3539 | int i; |
| 3540 | int *groups; |
| 3541 | unsigned dim, n_div; |
| 3542 | isl_bool non_empty; |
| 3543 | |
| 3544 | non_empty = isl_basic_map_plain_is_non_empty(bmap); |
| 3545 | if (non_empty < 0) |
| 3546 | return isl_basic_map_free(bmap); |
| 3547 | if (!non_empty) |
| 3548 | return bmap; |
| 3549 | |
| 3550 | dim = isl_basic_map_dim(bmap, isl_dim_all); |
| 3551 | n_div = isl_basic_map_dim(bmap, isl_dim_div); |
| 3552 | groups = isl_calloc_array(isl_basic_map_get_ctx(bmap), int, dim); |
| 3553 | if (!groups) |
| 3554 | return isl_basic_map_free(bmap); |
| 3555 | first += isl_basic_map_offset(bmap, type) - 1; |
| 3556 | for (i = 0; i < first; ++i) |
| 3557 | groups[i] = -1; |
| 3558 | for (i = first + n; i < dim - n_div; ++i) |
| 3559 | groups[i] = -1; |
| 3560 | |
| 3561 | bmap = isl_basic_map_drop_unrelated_constraints(bmap, groups); |
| 3562 | |
| 3563 | return bmap; |
| 3564 | } |
| 3565 | |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 3566 | /* Turn the n dimensions of type type, starting at first |
| 3567 | * into existentially quantified variables. |
Tobias Grosser | a67ac97 | 2016-02-26 11:35:12 +0000 | [diff] [blame] | 3568 | * |
| 3569 | * If a subset of the projected out variables are unrelated |
| 3570 | * to any of the variables that remain, then the constraints |
| 3571 | * involving this subset are simply dropped first. |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 3572 | */ |
| 3573 | __isl_give isl_basic_map *isl_basic_map_project_out( |
| 3574 | __isl_take isl_basic_map *bmap, |
| 3575 | enum isl_dim_type type, unsigned first, unsigned n) |
| 3576 | { |
| 3577 | if (n == 0) |
| 3578 | return basic_map_space_reset(bmap, type); |
Tobias Grosser | a67ac97 | 2016-02-26 11:35:12 +0000 | [diff] [blame] | 3579 | if (type == isl_dim_div) |
| 3580 | isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid, |
| 3581 | "cannot project out existentially quantified variables", |
| 3582 | return isl_basic_map_free(bmap)); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 3583 | |
Tobias Grosser | a67ac97 | 2016-02-26 11:35:12 +0000 | [diff] [blame] | 3584 | bmap = drop_irrelevant_constraints(bmap, type, first, n); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 3585 | if (!bmap) |
| 3586 | return NULL; |
| 3587 | |
| 3588 | if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) |
| 3589 | return isl_basic_map_remove_dims(bmap, type, first, n); |
| 3590 | |
| 3591 | isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type), |
| 3592 | goto error); |
| 3593 | |
| 3594 | bmap = move_last(bmap, type, first, n); |
| 3595 | bmap = isl_basic_map_cow(bmap); |
| 3596 | bmap = insert_div_rows(bmap, n); |
| 3597 | if (!bmap) |
| 3598 | return NULL; |
| 3599 | |
| 3600 | bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n); |
| 3601 | if (!bmap->dim) |
| 3602 | goto error; |
| 3603 | bmap = isl_basic_map_simplify(bmap); |
| 3604 | bmap = isl_basic_map_drop_redundant_divs(bmap); |
| 3605 | return isl_basic_map_finalize(bmap); |
| 3606 | error: |
| 3607 | isl_basic_map_free(bmap); |
| 3608 | return NULL; |
| 3609 | } |
| 3610 | |
| 3611 | /* Turn the n dimensions of type type, starting at first |
| 3612 | * into existentially quantified variables. |
| 3613 | */ |
| 3614 | struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset, |
| 3615 | enum isl_dim_type type, unsigned first, unsigned n) |
| 3616 | { |
| 3617 | return (isl_basic_set *)isl_basic_map_project_out( |
| 3618 | (isl_basic_map *)bset, type, first, n); |
| 3619 | } |
| 3620 | |
| 3621 | /* Turn the n dimensions of type type, starting at first |
| 3622 | * into existentially quantified variables. |
| 3623 | */ |
| 3624 | __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map, |
| 3625 | enum isl_dim_type type, unsigned first, unsigned n) |
| 3626 | { |
| 3627 | int i; |
| 3628 | |
| 3629 | if (!map) |
| 3630 | return NULL; |
| 3631 | |
| 3632 | if (n == 0) |
| 3633 | return map_space_reset(map, type); |
| 3634 | |
| 3635 | isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error); |
| 3636 | |
| 3637 | map = isl_map_cow(map); |
| 3638 | if (!map) |
| 3639 | return NULL; |
| 3640 | |
| 3641 | map->dim = isl_space_drop_dims(map->dim, type, first, n); |
| 3642 | if (!map->dim) |
| 3643 | goto error; |
| 3644 | |
| 3645 | for (i = 0; i < map->n; ++i) { |
| 3646 | map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n); |
| 3647 | if (!map->p[i]) |
| 3648 | goto error; |
| 3649 | } |
| 3650 | |
| 3651 | return map; |
| 3652 | error: |
| 3653 | isl_map_free(map); |
| 3654 | return NULL; |
| 3655 | } |
| 3656 | |
| 3657 | /* Turn the n dimensions of type type, starting at first |
| 3658 | * into existentially quantified variables. |
| 3659 | */ |
| 3660 | __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set, |
| 3661 | enum isl_dim_type type, unsigned first, unsigned n) |
| 3662 | { |
| 3663 | return (isl_set *)isl_map_project_out((isl_map *)set, type, first, n); |
| 3664 | } |
| 3665 | |
Michael Kruse | e0b34f3 | 2016-05-04 14:41:36 +0000 | [diff] [blame] | 3666 | /* Return a map that projects the elements in "set" onto their |
| 3667 | * "n" set dimensions starting at "first". |
| 3668 | * "type" should be equal to isl_dim_set. |
| 3669 | */ |
| 3670 | __isl_give isl_map *isl_set_project_onto_map(__isl_take isl_set *set, |
| 3671 | enum isl_dim_type type, unsigned first, unsigned n) |
| 3672 | { |
| 3673 | int i; |
| 3674 | int dim; |
| 3675 | isl_map *map; |
| 3676 | |
| 3677 | if (!set) |
| 3678 | return NULL; |
| 3679 | if (type != isl_dim_set) |
| 3680 | isl_die(isl_set_get_ctx(set), isl_error_invalid, |
| 3681 | "only set dimensions can be projected out", goto error); |
| 3682 | dim = isl_set_dim(set, isl_dim_set); |
| 3683 | if (first + n > dim || first + n < first) |
| 3684 | isl_die(isl_set_get_ctx(set), isl_error_invalid, |
| 3685 | "index out of bounds", goto error); |
| 3686 | |
| 3687 | map = isl_map_from_domain(set); |
| 3688 | map = isl_map_add_dims(map, isl_dim_out, n); |
| 3689 | for (i = 0; i < n; ++i) |
| 3690 | map = isl_map_equate(map, isl_dim_in, first + i, |
| 3691 | isl_dim_out, i); |
| 3692 | return map; |
| 3693 | error: |
| 3694 | isl_set_free(set); |
| 3695 | return NULL; |
| 3696 | } |
| 3697 | |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 3698 | static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n) |
| 3699 | { |
| 3700 | int i, j; |
| 3701 | |
| 3702 | for (i = 0; i < n; ++i) { |
| 3703 | j = isl_basic_map_alloc_div(bmap); |
| 3704 | if (j < 0) |
| 3705 | goto error; |
| 3706 | isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap)); |
| 3707 | } |
| 3708 | return bmap; |
| 3709 | error: |
| 3710 | isl_basic_map_free(bmap); |
| 3711 | return NULL; |
| 3712 | } |
| 3713 | |
| 3714 | struct isl_basic_map *isl_basic_map_apply_range( |
| 3715 | struct isl_basic_map *bmap1, struct isl_basic_map *bmap2) |
| 3716 | { |
| 3717 | isl_space *dim_result = NULL; |
| 3718 | struct isl_basic_map *bmap; |
| 3719 | unsigned n_in, n_out, n, nparam, total, pos; |
| 3720 | struct isl_dim_map *dim_map1, *dim_map2; |
| 3721 | |
| 3722 | if (!bmap1 || !bmap2) |
| 3723 | goto error; |
| 3724 | if (!isl_space_match(bmap1->dim, isl_dim_param, |
| 3725 | bmap2->dim, isl_dim_param)) |
| 3726 | isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid, |
| 3727 | "parameters don't match", goto error); |
| 3728 | if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_out, |
| 3729 | bmap2->dim, isl_dim_in)) |
| 3730 | isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid, |
| 3731 | "spaces don't match", goto error); |
| 3732 | |
| 3733 | dim_result = isl_space_join(isl_space_copy(bmap1->dim), |
| 3734 | isl_space_copy(bmap2->dim)); |
| 3735 | |
| 3736 | n_in = isl_basic_map_n_in(bmap1); |
| 3737 | n_out = isl_basic_map_n_out(bmap2); |
| 3738 | n = isl_basic_map_n_out(bmap1); |
| 3739 | nparam = isl_basic_map_n_param(bmap1); |
| 3740 | |
| 3741 | total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n; |
| 3742 | dim_map1 = isl_dim_map_alloc(bmap1->ctx, total); |
| 3743 | dim_map2 = isl_dim_map_alloc(bmap1->ctx, total); |
| 3744 | isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0); |
| 3745 | isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0); |
| 3746 | isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam); |
| 3747 | isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in); |
| 3748 | isl_dim_map_div(dim_map1, bmap1, pos += n_out); |
| 3749 | isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div); |
| 3750 | isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div); |
| 3751 | isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos); |
| 3752 | |
| 3753 | bmap = isl_basic_map_alloc_space(dim_result, |
| 3754 | bmap1->n_div + bmap2->n_div + n, |
| 3755 | bmap1->n_eq + bmap2->n_eq, |
| 3756 | bmap1->n_ineq + bmap2->n_ineq); |
| 3757 | bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1); |
| 3758 | bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2); |
| 3759 | bmap = add_divs(bmap, n); |
| 3760 | bmap = isl_basic_map_simplify(bmap); |
| 3761 | bmap = isl_basic_map_drop_redundant_divs(bmap); |
| 3762 | return isl_basic_map_finalize(bmap); |
| 3763 | error: |
| 3764 | isl_basic_map_free(bmap1); |
| 3765 | isl_basic_map_free(bmap2); |
| 3766 | return NULL; |
| 3767 | } |
| 3768 | |
| 3769 | struct isl_basic_set *isl_basic_set_apply( |
| 3770 | struct isl_basic_set *bset, struct isl_basic_map *bmap) |
| 3771 | { |
| 3772 | if (!bset || !bmap) |
| 3773 | goto error; |
| 3774 | |
| 3775 | isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset), |
| 3776 | goto error); |
| 3777 | |
| 3778 | return (struct isl_basic_set *) |
| 3779 | isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap); |
| 3780 | error: |
| 3781 | isl_basic_set_free(bset); |
| 3782 | isl_basic_map_free(bmap); |
| 3783 | return NULL; |
| 3784 | } |
| 3785 | |
| 3786 | struct isl_basic_map *isl_basic_map_apply_domain( |
| 3787 | struct isl_basic_map *bmap1, struct isl_basic_map *bmap2) |
| 3788 | { |
| 3789 | if (!bmap1 || !bmap2) |
| 3790 | goto error; |
| 3791 | |
| 3792 | isl_assert(bmap1->ctx, |
| 3793 | isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error); |
| 3794 | isl_assert(bmap1->ctx, |
| 3795 | isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2), |
| 3796 | goto error); |
| 3797 | |
| 3798 | bmap1 = isl_basic_map_reverse(bmap1); |
| 3799 | bmap1 = isl_basic_map_apply_range(bmap1, bmap2); |
| 3800 | return isl_basic_map_reverse(bmap1); |
| 3801 | error: |
| 3802 | isl_basic_map_free(bmap1); |
| 3803 | isl_basic_map_free(bmap2); |
| 3804 | return NULL; |
| 3805 | } |
| 3806 | |
| 3807 | /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map |
| 3808 | * A \cap B -> f(A) + f(B) |
| 3809 | */ |
| 3810 | struct isl_basic_map *isl_basic_map_sum( |
| 3811 | struct isl_basic_map *bmap1, struct isl_basic_map *bmap2) |
| 3812 | { |
| 3813 | unsigned n_in, n_out, nparam, total, pos; |
| 3814 | struct isl_basic_map *bmap = NULL; |
| 3815 | struct isl_dim_map *dim_map1, *dim_map2; |
| 3816 | int i; |
| 3817 | |
| 3818 | if (!bmap1 || !bmap2) |
| 3819 | goto error; |
| 3820 | |
| 3821 | isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), |
| 3822 | goto error); |
| 3823 | |
| 3824 | nparam = isl_basic_map_n_param(bmap1); |
| 3825 | n_in = isl_basic_map_n_in(bmap1); |
| 3826 | n_out = isl_basic_map_n_out(bmap1); |
| 3827 | |
| 3828 | total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out; |
| 3829 | dim_map1 = isl_dim_map_alloc(bmap1->ctx, total); |
| 3830 | dim_map2 = isl_dim_map_alloc(bmap2->ctx, total); |
| 3831 | isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0); |
| 3832 | isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos); |
| 3833 | isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam); |
| 3834 | isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos); |
| 3835 | isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out); |
| 3836 | isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div); |
| 3837 | isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div); |
| 3838 | isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out); |
| 3839 | |
| 3840 | bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim), |
| 3841 | bmap1->n_div + bmap2->n_div + 2 * n_out, |
| 3842 | bmap1->n_eq + bmap2->n_eq + n_out, |
| 3843 | bmap1->n_ineq + bmap2->n_ineq); |
| 3844 | for (i = 0; i < n_out; ++i) { |
| 3845 | int j = isl_basic_map_alloc_equality(bmap); |
| 3846 | if (j < 0) |
| 3847 | goto error; |
| 3848 | isl_seq_clr(bmap->eq[j], 1+total); |
| 3849 | isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1); |
| 3850 | isl_int_set_si(bmap->eq[j][1+pos+i], 1); |
| 3851 | isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1); |
| 3852 | } |
| 3853 | bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1); |
| 3854 | bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2); |
| 3855 | bmap = add_divs(bmap, 2 * n_out); |
| 3856 | |
| 3857 | bmap = isl_basic_map_simplify(bmap); |
| 3858 | return isl_basic_map_finalize(bmap); |
| 3859 | error: |
| 3860 | isl_basic_map_free(bmap); |
| 3861 | isl_basic_map_free(bmap1); |
| 3862 | isl_basic_map_free(bmap2); |
| 3863 | return NULL; |
| 3864 | } |
| 3865 | |
| 3866 | /* Given two maps A -> f(A) and B -> g(B), construct a map |
| 3867 | * A \cap B -> f(A) + f(B) |
| 3868 | */ |
| 3869 | struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2) |
| 3870 | { |
| 3871 | struct isl_map *result; |
| 3872 | int i, j; |
| 3873 | |
| 3874 | if (!map1 || !map2) |
| 3875 | goto error; |
| 3876 | |
| 3877 | isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error); |
| 3878 | |
| 3879 | result = isl_map_alloc_space(isl_space_copy(map1->dim), |
| 3880 | map1->n * map2->n, 0); |
| 3881 | if (!result) |
| 3882 | goto error; |
| 3883 | for (i = 0; i < map1->n; ++i) |
| 3884 | for (j = 0; j < map2->n; ++j) { |
| 3885 | struct isl_basic_map *part; |
| 3886 | part = isl_basic_map_sum( |
| 3887 | isl_basic_map_copy(map1->p[i]), |
| 3888 | isl_basic_map_copy(map2->p[j])); |
| 3889 | if (isl_basic_map_is_empty(part)) |
| 3890 | isl_basic_map_free(part); |
| 3891 | else |
| 3892 | result = isl_map_add_basic_map(result, part); |
| 3893 | if (!result) |
| 3894 | goto error; |
| 3895 | } |
| 3896 | isl_map_free(map1); |
| 3897 | isl_map_free(map2); |
| 3898 | return result; |
| 3899 | error: |
| 3900 | isl_map_free(map1); |
| 3901 | isl_map_free(map2); |
| 3902 | return NULL; |
| 3903 | } |
| 3904 | |
| 3905 | __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1, |
| 3906 | __isl_take isl_set *set2) |
| 3907 | { |
| 3908 | return (isl_set *)isl_map_sum((isl_map *)set1, (isl_map *)set2); |
| 3909 | } |
| 3910 | |
| 3911 | /* Given a basic map A -> f(A), construct A -> -f(A). |
| 3912 | */ |
| 3913 | struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap) |
| 3914 | { |
| 3915 | int i, j; |
| 3916 | unsigned off, n; |
| 3917 | |
| 3918 | bmap = isl_basic_map_cow(bmap); |
| 3919 | if (!bmap) |
| 3920 | return NULL; |
| 3921 | |
| 3922 | n = isl_basic_map_dim(bmap, isl_dim_out); |
| 3923 | off = isl_basic_map_offset(bmap, isl_dim_out); |
| 3924 | for (i = 0; i < bmap->n_eq; ++i) |
| 3925 | for (j = 0; j < n; ++j) |
| 3926 | isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]); |
| 3927 | for (i = 0; i < bmap->n_ineq; ++i) |
| 3928 | for (j = 0; j < n; ++j) |
| 3929 | isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]); |
| 3930 | for (i = 0; i < bmap->n_div; ++i) |
| 3931 | for (j = 0; j < n; ++j) |
| 3932 | isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]); |
| 3933 | bmap = isl_basic_map_gauss(bmap, NULL); |
| 3934 | return isl_basic_map_finalize(bmap); |
| 3935 | } |
| 3936 | |
| 3937 | __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset) |
| 3938 | { |
| 3939 | return isl_basic_map_neg(bset); |
| 3940 | } |
| 3941 | |
| 3942 | /* Given a map A -> f(A), construct A -> -f(A). |
| 3943 | */ |
| 3944 | struct isl_map *isl_map_neg(struct isl_map *map) |
| 3945 | { |
| 3946 | int i; |
| 3947 | |
| 3948 | map = isl_map_cow(map); |
| 3949 | if (!map) |
| 3950 | return NULL; |
| 3951 | |
| 3952 | for (i = 0; i < map->n; ++i) { |
| 3953 | map->p[i] = isl_basic_map_neg(map->p[i]); |
| 3954 | if (!map->p[i]) |
| 3955 | goto error; |
| 3956 | } |
| 3957 | |
| 3958 | return map; |
| 3959 | error: |
| 3960 | isl_map_free(map); |
| 3961 | return NULL; |
| 3962 | } |
| 3963 | |
| 3964 | __isl_give isl_set *isl_set_neg(__isl_take isl_set *set) |
| 3965 | { |
| 3966 | return (isl_set *)isl_map_neg((isl_map *)set); |
| 3967 | } |
| 3968 | |
| 3969 | /* Given a basic map A -> f(A) and an integer d, construct a basic map |
| 3970 | * A -> floor(f(A)/d). |
| 3971 | */ |
| 3972 | struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap, |
| 3973 | isl_int d) |
| 3974 | { |
| 3975 | unsigned n_in, n_out, nparam, total, pos; |
| 3976 | struct isl_basic_map *result = NULL; |
| 3977 | struct isl_dim_map *dim_map; |
| 3978 | int i; |
| 3979 | |
| 3980 | if (!bmap) |
| 3981 | return NULL; |
| 3982 | |
| 3983 | nparam = isl_basic_map_n_param(bmap); |
| 3984 | n_in = isl_basic_map_n_in(bmap); |
| 3985 | n_out = isl_basic_map_n_out(bmap); |
| 3986 | |
| 3987 | total = nparam + n_in + n_out + bmap->n_div + n_out; |
| 3988 | dim_map = isl_dim_map_alloc(bmap->ctx, total); |
| 3989 | isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0); |
| 3990 | isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam); |
| 3991 | isl_dim_map_div(dim_map, bmap, pos += n_in + n_out); |
| 3992 | isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div); |
| 3993 | |
| 3994 | result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim), |
| 3995 | bmap->n_div + n_out, |
| 3996 | bmap->n_eq, bmap->n_ineq + 2 * n_out); |
| 3997 | result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map); |
| 3998 | result = add_divs(result, n_out); |
| 3999 | for (i = 0; i < n_out; ++i) { |
| 4000 | int j; |
| 4001 | j = isl_basic_map_alloc_inequality(result); |
| 4002 | if (j < 0) |
| 4003 | goto error; |
| 4004 | isl_seq_clr(result->ineq[j], 1+total); |
| 4005 | isl_int_neg(result->ineq[j][1+nparam+n_in+i], d); |
| 4006 | isl_int_set_si(result->ineq[j][1+pos+i], 1); |
| 4007 | j = isl_basic_map_alloc_inequality(result); |
| 4008 | if (j < 0) |
| 4009 | goto error; |
| 4010 | isl_seq_clr(result->ineq[j], 1+total); |
| 4011 | isl_int_set(result->ineq[j][1+nparam+n_in+i], d); |
| 4012 | isl_int_set_si(result->ineq[j][1+pos+i], -1); |
| 4013 | isl_int_sub_ui(result->ineq[j][0], d, 1); |
| 4014 | } |
| 4015 | |
| 4016 | result = isl_basic_map_simplify(result); |
| 4017 | return isl_basic_map_finalize(result); |
| 4018 | error: |
| 4019 | isl_basic_map_free(result); |
| 4020 | return NULL; |
| 4021 | } |
| 4022 | |
| 4023 | /* Given a map A -> f(A) and an integer d, construct a map |
| 4024 | * A -> floor(f(A)/d). |
| 4025 | */ |
| 4026 | struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d) |
| 4027 | { |
| 4028 | int i; |
| 4029 | |
| 4030 | map = isl_map_cow(map); |
| 4031 | if (!map) |
| 4032 | return NULL; |
| 4033 | |
| 4034 | ISL_F_CLR(map, ISL_MAP_DISJOINT); |
| 4035 | ISL_F_CLR(map, ISL_MAP_NORMALIZED); |
| 4036 | for (i = 0; i < map->n; ++i) { |
| 4037 | map->p[i] = isl_basic_map_floordiv(map->p[i], d); |
| 4038 | if (!map->p[i]) |
| 4039 | goto error; |
| 4040 | } |
| 4041 | |
| 4042 | return map; |
| 4043 | error: |
| 4044 | isl_map_free(map); |
| 4045 | return NULL; |
| 4046 | } |
| 4047 | |
| 4048 | /* Given a map A -> f(A) and an integer d, construct a map |
| 4049 | * A -> floor(f(A)/d). |
| 4050 | */ |
| 4051 | __isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map, |
| 4052 | __isl_take isl_val *d) |
| 4053 | { |
| 4054 | if (!map || !d) |
| 4055 | goto error; |
| 4056 | if (!isl_val_is_int(d)) |
| 4057 | isl_die(isl_val_get_ctx(d), isl_error_invalid, |
| 4058 | "expecting integer denominator", goto error); |
| 4059 | map = isl_map_floordiv(map, d->n); |
| 4060 | isl_val_free(d); |
| 4061 | return map; |
| 4062 | error: |
| 4063 | isl_map_free(map); |
| 4064 | isl_val_free(d); |
| 4065 | return NULL; |
| 4066 | } |
| 4067 | |
| 4068 | static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos) |
| 4069 | { |
| 4070 | int i; |
| 4071 | unsigned nparam; |
| 4072 | unsigned n_in; |
| 4073 | |
| 4074 | i = isl_basic_map_alloc_equality(bmap); |
| 4075 | if (i < 0) |
| 4076 | goto error; |
| 4077 | nparam = isl_basic_map_n_param(bmap); |
| 4078 | n_in = isl_basic_map_n_in(bmap); |
| 4079 | isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap)); |
| 4080 | isl_int_set_si(bmap->eq[i][1+nparam+pos], -1); |
| 4081 | isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1); |
| 4082 | return isl_basic_map_finalize(bmap); |
| 4083 | error: |
| 4084 | isl_basic_map_free(bmap); |
| 4085 | return NULL; |
| 4086 | } |
| 4087 | |
| 4088 | /* Add a constraints to "bmap" expressing i_pos < o_pos |
| 4089 | */ |
| 4090 | static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos) |
| 4091 | { |
| 4092 | int i; |
| 4093 | unsigned nparam; |
| 4094 | unsigned n_in; |
| 4095 | |
| 4096 | i = isl_basic_map_alloc_inequality(bmap); |
| 4097 | if (i < 0) |
| 4098 | goto error; |
| 4099 | nparam = isl_basic_map_n_param(bmap); |
| 4100 | n_in = isl_basic_map_n_in(bmap); |
| 4101 | isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap)); |
| 4102 | isl_int_set_si(bmap->ineq[i][0], -1); |
| 4103 | isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1); |
| 4104 | isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1); |
| 4105 | return isl_basic_map_finalize(bmap); |
| 4106 | error: |
| 4107 | isl_basic_map_free(bmap); |
| 4108 | return NULL; |
| 4109 | } |
| 4110 | |
| 4111 | /* Add a constraint to "bmap" expressing i_pos <= o_pos |
| 4112 | */ |
| 4113 | static __isl_give isl_basic_map *var_less_or_equal( |
| 4114 | __isl_take isl_basic_map *bmap, unsigned pos) |
| 4115 | { |
| 4116 | int i; |
| 4117 | unsigned nparam; |
| 4118 | unsigned n_in; |
| 4119 | |
| 4120 | i = isl_basic_map_alloc_inequality(bmap); |
| 4121 | if (i < 0) |
| 4122 | goto error; |
| 4123 | nparam = isl_basic_map_n_param(bmap); |
| 4124 | n_in = isl_basic_map_n_in(bmap); |
| 4125 | isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap)); |
| 4126 | isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1); |
| 4127 | isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1); |
| 4128 | return isl_basic_map_finalize(bmap); |
| 4129 | error: |
| 4130 | isl_basic_map_free(bmap); |
| 4131 | return NULL; |
| 4132 | } |
| 4133 | |
| 4134 | /* Add a constraints to "bmap" expressing i_pos > o_pos |
| 4135 | */ |
| 4136 | static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos) |
| 4137 | { |
| 4138 | int i; |
| 4139 | unsigned nparam; |
| 4140 | unsigned n_in; |
| 4141 | |
| 4142 | i = isl_basic_map_alloc_inequality(bmap); |
| 4143 | if (i < 0) |
| 4144 | goto error; |
| 4145 | nparam = isl_basic_map_n_param(bmap); |
| 4146 | n_in = isl_basic_map_n_in(bmap); |
| 4147 | isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap)); |
| 4148 | isl_int_set_si(bmap->ineq[i][0], -1); |
| 4149 | isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1); |
| 4150 | isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1); |
| 4151 | return isl_basic_map_finalize(bmap); |
| 4152 | error: |
| 4153 | isl_basic_map_free(bmap); |
| 4154 | return NULL; |
| 4155 | } |
| 4156 | |
| 4157 | /* Add a constraint to "bmap" expressing i_pos >= o_pos |
| 4158 | */ |
| 4159 | static __isl_give isl_basic_map *var_more_or_equal( |
| 4160 | __isl_take isl_basic_map *bmap, unsigned pos) |
| 4161 | { |
| 4162 | int i; |
| 4163 | unsigned nparam; |
| 4164 | unsigned n_in; |
| 4165 | |
| 4166 | i = isl_basic_map_alloc_inequality(bmap); |
| 4167 | if (i < 0) |
| 4168 | goto error; |
| 4169 | nparam = isl_basic_map_n_param(bmap); |
| 4170 | n_in = isl_basic_map_n_in(bmap); |
| 4171 | isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap)); |
| 4172 | isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1); |
| 4173 | isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1); |
| 4174 | return isl_basic_map_finalize(bmap); |
| 4175 | error: |
| 4176 | isl_basic_map_free(bmap); |
| 4177 | return NULL; |
| 4178 | } |
| 4179 | |
| 4180 | __isl_give isl_basic_map *isl_basic_map_equal( |
| 4181 | __isl_take isl_space *dim, unsigned n_equal) |
| 4182 | { |
| 4183 | int i; |
| 4184 | struct isl_basic_map *bmap; |
| 4185 | bmap = isl_basic_map_alloc_space(dim, 0, n_equal, 0); |
| 4186 | if (!bmap) |
| 4187 | return NULL; |
| 4188 | for (i = 0; i < n_equal && bmap; ++i) |
| 4189 | bmap = var_equal(bmap, i); |
| 4190 | return isl_basic_map_finalize(bmap); |
| 4191 | } |
| 4192 | |
| 4193 | /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos] |
| 4194 | */ |
| 4195 | __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *dim, |
| 4196 | unsigned pos) |
| 4197 | { |
| 4198 | int i; |
| 4199 | struct isl_basic_map *bmap; |
| 4200 | bmap = isl_basic_map_alloc_space(dim, 0, pos, 1); |
| 4201 | if (!bmap) |
| 4202 | return NULL; |
| 4203 | for (i = 0; i < pos && bmap; ++i) |
| 4204 | bmap = var_equal(bmap, i); |
| 4205 | if (bmap) |
| 4206 | bmap = var_less(bmap, pos); |
| 4207 | return isl_basic_map_finalize(bmap); |
| 4208 | } |
| 4209 | |
| 4210 | /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos] |
| 4211 | */ |
| 4212 | __isl_give isl_basic_map *isl_basic_map_less_or_equal_at( |
| 4213 | __isl_take isl_space *dim, unsigned pos) |
| 4214 | { |
| 4215 | int i; |
| 4216 | isl_basic_map *bmap; |
| 4217 | |
| 4218 | bmap = isl_basic_map_alloc_space(dim, 0, pos, 1); |
| 4219 | for (i = 0; i < pos; ++i) |
| 4220 | bmap = var_equal(bmap, i); |
| 4221 | bmap = var_less_or_equal(bmap, pos); |
| 4222 | return isl_basic_map_finalize(bmap); |
| 4223 | } |
| 4224 | |
| 4225 | /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos |
| 4226 | */ |
| 4227 | __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *dim, |
| 4228 | unsigned pos) |
| 4229 | { |
| 4230 | int i; |
| 4231 | struct isl_basic_map *bmap; |
| 4232 | bmap = isl_basic_map_alloc_space(dim, 0, pos, 1); |
| 4233 | if (!bmap) |
| 4234 | return NULL; |
| 4235 | for (i = 0; i < pos && bmap; ++i) |
| 4236 | bmap = var_equal(bmap, i); |
| 4237 | if (bmap) |
| 4238 | bmap = var_more(bmap, pos); |
| 4239 | return isl_basic_map_finalize(bmap); |
| 4240 | } |
| 4241 | |
| 4242 | /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos] |
| 4243 | */ |
| 4244 | __isl_give isl_basic_map *isl_basic_map_more_or_equal_at( |
| 4245 | __isl_take isl_space *dim, unsigned pos) |
| 4246 | { |
| 4247 | int i; |
| 4248 | isl_basic_map *bmap; |
| 4249 | |
| 4250 | bmap = isl_basic_map_alloc_space(dim, 0, pos, 1); |
| 4251 | for (i = 0; i < pos; ++i) |
| 4252 | bmap = var_equal(bmap, i); |
| 4253 | bmap = var_more_or_equal(bmap, pos); |
| 4254 | return isl_basic_map_finalize(bmap); |
| 4255 | } |
| 4256 | |
| 4257 | static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *dims, |
| 4258 | unsigned n, int equal) |
| 4259 | { |
| 4260 | struct isl_map *map; |
| 4261 | int i; |
| 4262 | |
| 4263 | if (n == 0 && equal) |
| 4264 | return isl_map_universe(dims); |
| 4265 | |
| 4266 | map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT); |
| 4267 | |
| 4268 | for (i = 0; i + 1 < n; ++i) |
| 4269 | map = isl_map_add_basic_map(map, |
| 4270 | isl_basic_map_less_at(isl_space_copy(dims), i)); |
| 4271 | if (n > 0) { |
| 4272 | if (equal) |
| 4273 | map = isl_map_add_basic_map(map, |
| 4274 | isl_basic_map_less_or_equal_at(dims, n - 1)); |
| 4275 | else |
| 4276 | map = isl_map_add_basic_map(map, |
| 4277 | isl_basic_map_less_at(dims, n - 1)); |
| 4278 | } else |
| 4279 | isl_space_free(dims); |
| 4280 | |
| 4281 | return map; |
| 4282 | } |
| 4283 | |
| 4284 | static __isl_give isl_map *map_lex_lte(__isl_take isl_space *dims, int equal) |
| 4285 | { |
| 4286 | if (!dims) |
| 4287 | return NULL; |
| 4288 | return map_lex_lte_first(dims, dims->n_out, equal); |
| 4289 | } |
| 4290 | |
| 4291 | __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n) |
| 4292 | { |
| 4293 | return map_lex_lte_first(dim, n, 0); |
| 4294 | } |
| 4295 | |
| 4296 | __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n) |
| 4297 | { |
| 4298 | return map_lex_lte_first(dim, n, 1); |
| 4299 | } |
| 4300 | |
| 4301 | __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim) |
| 4302 | { |
| 4303 | return map_lex_lte(isl_space_map_from_set(set_dim), 0); |
| 4304 | } |
| 4305 | |
| 4306 | __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim) |
| 4307 | { |
| 4308 | return map_lex_lte(isl_space_map_from_set(set_dim), 1); |
| 4309 | } |
| 4310 | |
| 4311 | static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *dims, |
| 4312 | unsigned n, int equal) |
| 4313 | { |
| 4314 | struct isl_map *map; |
| 4315 | int i; |
| 4316 | |
| 4317 | if (n == 0 && equal) |
| 4318 | return isl_map_universe(dims); |
| 4319 | |
| 4320 | map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT); |
| 4321 | |
| 4322 | for (i = 0; i + 1 < n; ++i) |
| 4323 | map = isl_map_add_basic_map(map, |
| 4324 | isl_basic_map_more_at(isl_space_copy(dims), i)); |
| 4325 | if (n > 0) { |
| 4326 | if (equal) |
| 4327 | map = isl_map_add_basic_map(map, |
| 4328 | isl_basic_map_more_or_equal_at(dims, n - 1)); |
| 4329 | else |
| 4330 | map = isl_map_add_basic_map(map, |
| 4331 | isl_basic_map_more_at(dims, n - 1)); |
| 4332 | } else |
| 4333 | isl_space_free(dims); |
| 4334 | |
| 4335 | return map; |
| 4336 | } |
| 4337 | |
| 4338 | static __isl_give isl_map *map_lex_gte(__isl_take isl_space *dims, int equal) |
| 4339 | { |
| 4340 | if (!dims) |
| 4341 | return NULL; |
| 4342 | return map_lex_gte_first(dims, dims->n_out, equal); |
| 4343 | } |
| 4344 | |
| 4345 | __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n) |
| 4346 | { |
| 4347 | return map_lex_gte_first(dim, n, 0); |
| 4348 | } |
| 4349 | |
| 4350 | __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n) |
| 4351 | { |
| 4352 | return map_lex_gte_first(dim, n, 1); |
| 4353 | } |
| 4354 | |
| 4355 | __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim) |
| 4356 | { |
| 4357 | return map_lex_gte(isl_space_map_from_set(set_dim), 0); |
| 4358 | } |
| 4359 | |
| 4360 | __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim) |
| 4361 | { |
| 4362 | return map_lex_gte(isl_space_map_from_set(set_dim), 1); |
| 4363 | } |
| 4364 | |
| 4365 | __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1, |
| 4366 | __isl_take isl_set *set2) |
| 4367 | { |
| 4368 | isl_map *map; |
| 4369 | map = isl_map_lex_le(isl_set_get_space(set1)); |
| 4370 | map = isl_map_intersect_domain(map, set1); |
| 4371 | map = isl_map_intersect_range(map, set2); |
| 4372 | return map; |
| 4373 | } |
| 4374 | |
| 4375 | __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1, |
| 4376 | __isl_take isl_set *set2) |
| 4377 | { |
| 4378 | isl_map *map; |
| 4379 | map = isl_map_lex_lt(isl_set_get_space(set1)); |
| 4380 | map = isl_map_intersect_domain(map, set1); |
| 4381 | map = isl_map_intersect_range(map, set2); |
| 4382 | return map; |
| 4383 | } |
| 4384 | |
| 4385 | __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1, |
| 4386 | __isl_take isl_set *set2) |
| 4387 | { |
| 4388 | isl_map *map; |
| 4389 | map = isl_map_lex_ge(isl_set_get_space(set1)); |
| 4390 | map = isl_map_intersect_domain(map, set1); |
| 4391 | map = isl_map_intersect_range(map, set2); |
| 4392 | return map; |
| 4393 | } |
| 4394 | |
| 4395 | __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1, |
| 4396 | __isl_take isl_set *set2) |
| 4397 | { |
| 4398 | isl_map *map; |
| 4399 | map = isl_map_lex_gt(isl_set_get_space(set1)); |
| 4400 | map = isl_map_intersect_domain(map, set1); |
| 4401 | map = isl_map_intersect_range(map, set2); |
| 4402 | return map; |
| 4403 | } |
| 4404 | |
| 4405 | __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1, |
| 4406 | __isl_take isl_map *map2) |
| 4407 | { |
| 4408 | isl_map *map; |
| 4409 | map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1))); |
| 4410 | map = isl_map_apply_domain(map, isl_map_reverse(map1)); |
| 4411 | map = isl_map_apply_range(map, isl_map_reverse(map2)); |
| 4412 | return map; |
| 4413 | } |
| 4414 | |
| 4415 | __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1, |
| 4416 | __isl_take isl_map *map2) |
| 4417 | { |
| 4418 | isl_map *map; |
| 4419 | map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1))); |
| 4420 | map = isl_map_apply_domain(map, isl_map_reverse(map1)); |
| 4421 | map = isl_map_apply_range(map, isl_map_reverse(map2)); |
| 4422 | return map; |
| 4423 | } |
| 4424 | |
| 4425 | __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1, |
| 4426 | __isl_take isl_map *map2) |
| 4427 | { |
| 4428 | isl_map *map; |
| 4429 | map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1))); |
| 4430 | map = isl_map_apply_domain(map, isl_map_reverse(map1)); |
| 4431 | map = isl_map_apply_range(map, isl_map_reverse(map2)); |
| 4432 | return map; |
| 4433 | } |
| 4434 | |
| 4435 | __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1, |
| 4436 | __isl_take isl_map *map2) |
| 4437 | { |
| 4438 | isl_map *map; |
| 4439 | map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1))); |
| 4440 | map = isl_map_apply_domain(map, isl_map_reverse(map1)); |
| 4441 | map = isl_map_apply_range(map, isl_map_reverse(map2)); |
| 4442 | return map; |
| 4443 | } |
| 4444 | |
| 4445 | __isl_give isl_basic_map *isl_basic_map_from_basic_set( |
| 4446 | __isl_take isl_basic_set *bset, __isl_take isl_space *dim) |
| 4447 | { |
| 4448 | struct isl_basic_map *bmap; |
| 4449 | |
| 4450 | bset = isl_basic_set_cow(bset); |
| 4451 | if (!bset || !dim) |
| 4452 | goto error; |
| 4453 | |
| 4454 | isl_assert(bset->ctx, isl_space_compatible(bset->dim, dim), goto error); |
| 4455 | isl_space_free(bset->dim); |
| 4456 | bmap = (struct isl_basic_map *) bset; |
| 4457 | bmap->dim = dim; |
| 4458 | return isl_basic_map_finalize(bmap); |
| 4459 | error: |
| 4460 | isl_basic_set_free(bset); |
| 4461 | isl_space_free(dim); |
| 4462 | return NULL; |
| 4463 | } |
| 4464 | |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 4465 | /* For a div d = floor(f/m), add the constraint |
| 4466 | * |
| 4467 | * f - m d >= 0 |
| 4468 | */ |
| 4469 | static int add_upper_div_constraint(__isl_keep isl_basic_map *bmap, |
| 4470 | unsigned pos, isl_int *div) |
| 4471 | { |
| 4472 | int i; |
| 4473 | unsigned total = isl_basic_map_total_dim(bmap); |
| 4474 | |
| 4475 | i = isl_basic_map_alloc_inequality(bmap); |
| 4476 | if (i < 0) |
| 4477 | return -1; |
| 4478 | isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total); |
| 4479 | isl_int_neg(bmap->ineq[i][1 + pos], div[0]); |
| 4480 | |
| 4481 | return 0; |
| 4482 | } |
| 4483 | |
| 4484 | /* For a div d = floor(f/m), add the constraint |
| 4485 | * |
Tobias Grosser | 07b2095 | 2016-06-12 04:30:40 +0000 | [diff] [blame^] | 4486 | * -(f-(m-1)) + m d >= 0 |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 4487 | */ |
| 4488 | static int add_lower_div_constraint(__isl_keep isl_basic_map *bmap, |
| 4489 | unsigned pos, isl_int *div) |
| 4490 | { |
| 4491 | int i; |
| 4492 | unsigned total = isl_basic_map_total_dim(bmap); |
| 4493 | |
| 4494 | i = isl_basic_map_alloc_inequality(bmap); |
| 4495 | if (i < 0) |
| 4496 | return -1; |
| 4497 | isl_seq_neg(bmap->ineq[i], div + 1, 1 + total); |
| 4498 | isl_int_set(bmap->ineq[i][1 + pos], div[0]); |
| 4499 | isl_int_add(bmap->ineq[i][0], bmap->ineq[i][0], bmap->ineq[i][1 + pos]); |
| 4500 | isl_int_sub_ui(bmap->ineq[i][0], bmap->ineq[i][0], 1); |
| 4501 | |
| 4502 | return 0; |
| 4503 | } |
| 4504 | |
| 4505 | /* For a div d = floor(f/m), add the constraints |
| 4506 | * |
| 4507 | * f - m d >= 0 |
Tobias Grosser | 07b2095 | 2016-06-12 04:30:40 +0000 | [diff] [blame^] | 4508 | * -(f-(m-1)) + m d >= 0 |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 4509 | * |
| 4510 | * Note that the second constraint is the negation of |
| 4511 | * |
Tobias Grosser | 07b2095 | 2016-06-12 04:30:40 +0000 | [diff] [blame^] | 4512 | * f - m d >= m |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 4513 | */ |
| 4514 | int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap, |
| 4515 | unsigned pos, isl_int *div) |
| 4516 | { |
| 4517 | if (add_upper_div_constraint(bmap, pos, div) < 0) |
| 4518 | return -1; |
| 4519 | if (add_lower_div_constraint(bmap, pos, div) < 0) |
| 4520 | return -1; |
| 4521 | return 0; |
| 4522 | } |
| 4523 | |
| 4524 | int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset, |
| 4525 | unsigned pos, isl_int *div) |
| 4526 | { |
| 4527 | return isl_basic_map_add_div_constraints_var((isl_basic_map *)bset, |
| 4528 | pos, div); |
| 4529 | } |
| 4530 | |
| 4531 | int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div) |
| 4532 | { |
| 4533 | unsigned total = isl_basic_map_total_dim(bmap); |
| 4534 | unsigned div_pos = total - bmap->n_div + div; |
| 4535 | |
| 4536 | return isl_basic_map_add_div_constraints_var(bmap, div_pos, |
| 4537 | bmap->div[div]); |
| 4538 | } |
| 4539 | |
| 4540 | /* For each known div d = floor(f/m), add the constraints |
| 4541 | * |
| 4542 | * f - m d >= 0 |
Tobias Grosser | 07b2095 | 2016-06-12 04:30:40 +0000 | [diff] [blame^] | 4543 | * -(f-(m-1)) + m d >= 0 |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 4544 | * |
| 4545 | * Remove duplicate constraints in case of some these div constraints |
| 4546 | * already appear in "bmap". |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 4547 | */ |
| 4548 | __isl_give isl_basic_map *isl_basic_map_add_known_div_constraints( |
| 4549 | __isl_take isl_basic_map *bmap) |
| 4550 | { |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 4551 | unsigned n_div; |
| 4552 | |
| 4553 | if (!bmap) |
| 4554 | return NULL; |
| 4555 | n_div = isl_basic_map_dim(bmap, isl_dim_div); |
| 4556 | if (n_div == 0) |
| 4557 | return bmap; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 4558 | |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 4559 | bmap = add_known_div_constraints(bmap); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 4560 | bmap = isl_basic_map_remove_duplicate_constraints(bmap, NULL, 0); |
| 4561 | bmap = isl_basic_map_finalize(bmap); |
| 4562 | return bmap; |
| 4563 | } |
| 4564 | |
| 4565 | /* Add the div constraint of sign "sign" for div "div" of "bmap". |
| 4566 | * |
| 4567 | * In particular, if this div is of the form d = floor(f/m), |
| 4568 | * then add the constraint |
| 4569 | * |
| 4570 | * f - m d >= 0 |
| 4571 | * |
| 4572 | * if sign < 0 or the constraint |
| 4573 | * |
Tobias Grosser | 07b2095 | 2016-06-12 04:30:40 +0000 | [diff] [blame^] | 4574 | * -(f-(m-1)) + m d >= 0 |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 4575 | * |
| 4576 | * if sign > 0. |
| 4577 | */ |
| 4578 | int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map *bmap, |
| 4579 | unsigned div, int sign) |
| 4580 | { |
| 4581 | unsigned total; |
| 4582 | unsigned div_pos; |
| 4583 | |
| 4584 | if (!bmap) |
| 4585 | return -1; |
| 4586 | |
| 4587 | total = isl_basic_map_total_dim(bmap); |
| 4588 | div_pos = total - bmap->n_div + div; |
| 4589 | |
| 4590 | if (sign < 0) |
| 4591 | return add_upper_div_constraint(bmap, div_pos, bmap->div[div]); |
| 4592 | else |
| 4593 | return add_lower_div_constraint(bmap, div_pos, bmap->div[div]); |
| 4594 | } |
| 4595 | |
| 4596 | int isl_basic_set_add_div_constraints(struct isl_basic_set *bset, unsigned div) |
| 4597 | { |
| 4598 | return isl_basic_map_add_div_constraints(bset, div); |
| 4599 | } |
| 4600 | |
| 4601 | struct isl_basic_set *isl_basic_map_underlying_set( |
| 4602 | struct isl_basic_map *bmap) |
| 4603 | { |
| 4604 | if (!bmap) |
| 4605 | goto error; |
| 4606 | if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 && |
| 4607 | bmap->n_div == 0 && |
| 4608 | !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) && |
| 4609 | !isl_space_is_named_or_nested(bmap->dim, isl_dim_out)) |
| 4610 | return (struct isl_basic_set *)bmap; |
| 4611 | bmap = isl_basic_map_cow(bmap); |
| 4612 | if (!bmap) |
| 4613 | goto error; |
| 4614 | bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div); |
| 4615 | if (!bmap->dim) |
| 4616 | goto error; |
| 4617 | bmap->extra -= bmap->n_div; |
| 4618 | bmap->n_div = 0; |
| 4619 | bmap = isl_basic_map_finalize(bmap); |
| 4620 | return (struct isl_basic_set *)bmap; |
| 4621 | error: |
| 4622 | isl_basic_map_free(bmap); |
| 4623 | return NULL; |
| 4624 | } |
| 4625 | |
| 4626 | __isl_give isl_basic_set *isl_basic_set_underlying_set( |
| 4627 | __isl_take isl_basic_set *bset) |
| 4628 | { |
| 4629 | return isl_basic_map_underlying_set((isl_basic_map *)bset); |
| 4630 | } |
| 4631 | |
| 4632 | /* Replace each element in "list" by the result of applying |
Tobias Grosser | 1fa7b97 | 2015-02-16 19:33:40 +0000 | [diff] [blame] | 4633 | * isl_basic_map_underlying_set to the element. |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 4634 | */ |
Tobias Grosser | 1fa7b97 | 2015-02-16 19:33:40 +0000 | [diff] [blame] | 4635 | __isl_give isl_basic_set_list *isl_basic_map_list_underlying_set( |
| 4636 | __isl_take isl_basic_map_list *list) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 4637 | { |
| 4638 | int i, n; |
| 4639 | |
| 4640 | if (!list) |
| 4641 | return NULL; |
| 4642 | |
Tobias Grosser | 1fa7b97 | 2015-02-16 19:33:40 +0000 | [diff] [blame] | 4643 | n = isl_basic_map_list_n_basic_map(list); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 4644 | for (i = 0; i < n; ++i) { |
Tobias Grosser | 1fa7b97 | 2015-02-16 19:33:40 +0000 | [diff] [blame] | 4645 | isl_basic_map *bmap; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 4646 | isl_basic_set *bset; |
| 4647 | |
Tobias Grosser | 1fa7b97 | 2015-02-16 19:33:40 +0000 | [diff] [blame] | 4648 | bmap = isl_basic_map_list_get_basic_map(list, i); |
| 4649 | bset = isl_basic_set_underlying_set(bmap); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 4650 | list = isl_basic_set_list_set_basic_set(list, i, bset); |
| 4651 | } |
| 4652 | |
| 4653 | return list; |
| 4654 | } |
| 4655 | |
| 4656 | struct isl_basic_map *isl_basic_map_overlying_set( |
| 4657 | struct isl_basic_set *bset, struct isl_basic_map *like) |
| 4658 | { |
| 4659 | struct isl_basic_map *bmap; |
| 4660 | struct isl_ctx *ctx; |
| 4661 | unsigned total; |
| 4662 | int i; |
| 4663 | |
| 4664 | if (!bset || !like) |
| 4665 | goto error; |
| 4666 | ctx = bset->ctx; |
| 4667 | isl_assert(ctx, bset->n_div == 0, goto error); |
| 4668 | isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error); |
| 4669 | isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like), |
| 4670 | goto error); |
Tobias Grosser | 6e3ba33 | 2015-08-11 11:31:18 +0000 | [diff] [blame] | 4671 | if (like->n_div == 0) { |
| 4672 | isl_space *space = isl_basic_map_get_space(like); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 4673 | isl_basic_map_free(like); |
Tobias Grosser | 6e3ba33 | 2015-08-11 11:31:18 +0000 | [diff] [blame] | 4674 | return isl_basic_map_reset_space(bset, space); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 4675 | } |
| 4676 | bset = isl_basic_set_cow(bset); |
| 4677 | if (!bset) |
| 4678 | goto error; |
| 4679 | total = bset->dim->n_out + bset->extra; |
| 4680 | bmap = (struct isl_basic_map *)bset; |
| 4681 | isl_space_free(bmap->dim); |
| 4682 | bmap->dim = isl_space_copy(like->dim); |
| 4683 | if (!bmap->dim) |
| 4684 | goto error; |
| 4685 | bmap->n_div = like->n_div; |
| 4686 | bmap->extra += like->n_div; |
| 4687 | if (bmap->extra) { |
| 4688 | unsigned ltotal; |
| 4689 | isl_int **div; |
| 4690 | ltotal = total - bmap->extra + like->extra; |
| 4691 | if (ltotal > total) |
| 4692 | ltotal = total; |
| 4693 | bmap->block2 = isl_blk_extend(ctx, bmap->block2, |
| 4694 | bmap->extra * (1 + 1 + total)); |
| 4695 | if (isl_blk_is_error(bmap->block2)) |
| 4696 | goto error; |
| 4697 | div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra); |
| 4698 | if (!div) |
| 4699 | goto error; |
| 4700 | bmap->div = div; |
| 4701 | for (i = 0; i < bmap->extra; ++i) |
| 4702 | bmap->div[i] = bmap->block2.data + i * (1 + 1 + total); |
| 4703 | for (i = 0; i < like->n_div; ++i) { |
| 4704 | isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal); |
| 4705 | isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal); |
| 4706 | } |
| 4707 | bmap = isl_basic_map_add_known_div_constraints(bmap); |
| 4708 | } |
| 4709 | isl_basic_map_free(like); |
| 4710 | bmap = isl_basic_map_simplify(bmap); |
| 4711 | bmap = isl_basic_map_finalize(bmap); |
| 4712 | return bmap; |
| 4713 | error: |
| 4714 | isl_basic_map_free(like); |
| 4715 | isl_basic_set_free(bset); |
| 4716 | return NULL; |
| 4717 | } |
| 4718 | |
| 4719 | struct isl_basic_set *isl_basic_set_from_underlying_set( |
| 4720 | struct isl_basic_set *bset, struct isl_basic_set *like) |
| 4721 | { |
| 4722 | return (struct isl_basic_set *) |
| 4723 | isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like); |
| 4724 | } |
| 4725 | |
| 4726 | struct isl_set *isl_set_from_underlying_set( |
| 4727 | struct isl_set *set, struct isl_basic_set *like) |
| 4728 | { |
| 4729 | int i; |
| 4730 | |
| 4731 | if (!set || !like) |
| 4732 | goto error; |
| 4733 | isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like), |
| 4734 | goto error); |
| 4735 | if (isl_space_is_equal(set->dim, like->dim) && like->n_div == 0) { |
| 4736 | isl_basic_set_free(like); |
| 4737 | return set; |
| 4738 | } |
| 4739 | set = isl_set_cow(set); |
| 4740 | if (!set) |
| 4741 | goto error; |
| 4742 | for (i = 0; i < set->n; ++i) { |
| 4743 | set->p[i] = isl_basic_set_from_underlying_set(set->p[i], |
| 4744 | isl_basic_set_copy(like)); |
| 4745 | if (!set->p[i]) |
| 4746 | goto error; |
| 4747 | } |
| 4748 | isl_space_free(set->dim); |
| 4749 | set->dim = isl_space_copy(like->dim); |
| 4750 | if (!set->dim) |
| 4751 | goto error; |
| 4752 | isl_basic_set_free(like); |
| 4753 | return set; |
| 4754 | error: |
| 4755 | isl_basic_set_free(like); |
| 4756 | isl_set_free(set); |
| 4757 | return NULL; |
| 4758 | } |
| 4759 | |
| 4760 | struct isl_set *isl_map_underlying_set(struct isl_map *map) |
| 4761 | { |
| 4762 | int i; |
| 4763 | |
| 4764 | map = isl_map_cow(map); |
| 4765 | if (!map) |
| 4766 | return NULL; |
| 4767 | map->dim = isl_space_cow(map->dim); |
| 4768 | if (!map->dim) |
| 4769 | goto error; |
| 4770 | |
| 4771 | for (i = 1; i < map->n; ++i) |
| 4772 | isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div, |
| 4773 | goto error); |
| 4774 | for (i = 0; i < map->n; ++i) { |
| 4775 | map->p[i] = (struct isl_basic_map *) |
| 4776 | isl_basic_map_underlying_set(map->p[i]); |
| 4777 | if (!map->p[i]) |
| 4778 | goto error; |
| 4779 | } |
| 4780 | if (map->n == 0) |
| 4781 | map->dim = isl_space_underlying(map->dim, 0); |
| 4782 | else { |
| 4783 | isl_space_free(map->dim); |
| 4784 | map->dim = isl_space_copy(map->p[0]->dim); |
| 4785 | } |
| 4786 | if (!map->dim) |
| 4787 | goto error; |
| 4788 | return (struct isl_set *)map; |
| 4789 | error: |
| 4790 | isl_map_free(map); |
| 4791 | return NULL; |
| 4792 | } |
| 4793 | |
| 4794 | struct isl_set *isl_set_to_underlying_set(struct isl_set *set) |
| 4795 | { |
| 4796 | return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set); |
| 4797 | } |
| 4798 | |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 4799 | /* Replace the space of "bmap" by "space". |
| 4800 | * |
| 4801 | * If the space of "bmap" is identical to "space" (including the identifiers |
| 4802 | * of the input and output dimensions), then simply return the original input. |
| 4803 | */ |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 4804 | __isl_give isl_basic_map *isl_basic_map_reset_space( |
Tobias Grosser | 6e3ba33 | 2015-08-11 11:31:18 +0000 | [diff] [blame] | 4805 | __isl_take isl_basic_map *bmap, __isl_take isl_space *space) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 4806 | { |
Tobias Grosser | 6e3ba33 | 2015-08-11 11:31:18 +0000 | [diff] [blame] | 4807 | isl_bool equal; |
| 4808 | |
| 4809 | if (!bmap) |
| 4810 | goto error; |
| 4811 | equal = isl_space_is_equal(bmap->dim, space); |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 4812 | if (equal >= 0 && equal) |
| 4813 | equal = isl_space_match(bmap->dim, isl_dim_in, |
| 4814 | space, isl_dim_in); |
| 4815 | if (equal >= 0 && equal) |
| 4816 | equal = isl_space_match(bmap->dim, isl_dim_out, |
| 4817 | space, isl_dim_out); |
Tobias Grosser | 6e3ba33 | 2015-08-11 11:31:18 +0000 | [diff] [blame] | 4818 | if (equal < 0) |
| 4819 | goto error; |
| 4820 | if (equal) { |
| 4821 | isl_space_free(space); |
| 4822 | return bmap; |
| 4823 | } |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 4824 | bmap = isl_basic_map_cow(bmap); |
Tobias Grosser | 6e3ba33 | 2015-08-11 11:31:18 +0000 | [diff] [blame] | 4825 | if (!bmap || !space) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 4826 | goto error; |
| 4827 | |
| 4828 | isl_space_free(bmap->dim); |
Tobias Grosser | 6e3ba33 | 2015-08-11 11:31:18 +0000 | [diff] [blame] | 4829 | bmap->dim = space; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 4830 | |
| 4831 | bmap = isl_basic_map_finalize(bmap); |
| 4832 | |
| 4833 | return bmap; |
| 4834 | error: |
| 4835 | isl_basic_map_free(bmap); |
Tobias Grosser | 6e3ba33 | 2015-08-11 11:31:18 +0000 | [diff] [blame] | 4836 | isl_space_free(space); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 4837 | return NULL; |
| 4838 | } |
| 4839 | |
| 4840 | __isl_give isl_basic_set *isl_basic_set_reset_space( |
| 4841 | __isl_take isl_basic_set *bset, __isl_take isl_space *dim) |
| 4842 | { |
| 4843 | return (isl_basic_set *)isl_basic_map_reset_space((isl_basic_map *)bset, |
| 4844 | dim); |
| 4845 | } |
| 4846 | |
| 4847 | __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map, |
| 4848 | __isl_take isl_space *dim) |
| 4849 | { |
| 4850 | int i; |
| 4851 | |
| 4852 | map = isl_map_cow(map); |
| 4853 | if (!map || !dim) |
| 4854 | goto error; |
| 4855 | |
| 4856 | for (i = 0; i < map->n; ++i) { |
| 4857 | map->p[i] = isl_basic_map_reset_space(map->p[i], |
| 4858 | isl_space_copy(dim)); |
| 4859 | if (!map->p[i]) |
| 4860 | goto error; |
| 4861 | } |
| 4862 | isl_space_free(map->dim); |
| 4863 | map->dim = dim; |
| 4864 | |
| 4865 | return map; |
| 4866 | error: |
| 4867 | isl_map_free(map); |
| 4868 | isl_space_free(dim); |
| 4869 | return NULL; |
| 4870 | } |
| 4871 | |
| 4872 | __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set, |
| 4873 | __isl_take isl_space *dim) |
| 4874 | { |
| 4875 | return (struct isl_set *) isl_map_reset_space((struct isl_map *)set, dim); |
| 4876 | } |
| 4877 | |
| 4878 | /* Compute the parameter domain of the given basic set. |
| 4879 | */ |
| 4880 | __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset) |
| 4881 | { |
| 4882 | isl_space *space; |
| 4883 | unsigned n; |
| 4884 | |
| 4885 | if (isl_basic_set_is_params(bset)) |
| 4886 | return bset; |
| 4887 | |
| 4888 | n = isl_basic_set_dim(bset, isl_dim_set); |
| 4889 | bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n); |
| 4890 | space = isl_basic_set_get_space(bset); |
| 4891 | space = isl_space_params(space); |
| 4892 | bset = isl_basic_set_reset_space(bset, space); |
| 4893 | return bset; |
| 4894 | } |
| 4895 | |
| 4896 | /* Construct a zero-dimensional basic set with the given parameter domain. |
| 4897 | */ |
| 4898 | __isl_give isl_basic_set *isl_basic_set_from_params( |
| 4899 | __isl_take isl_basic_set *bset) |
| 4900 | { |
| 4901 | isl_space *space; |
| 4902 | space = isl_basic_set_get_space(bset); |
| 4903 | space = isl_space_set_from_params(space); |
| 4904 | bset = isl_basic_set_reset_space(bset, space); |
| 4905 | return bset; |
| 4906 | } |
| 4907 | |
| 4908 | /* Compute the parameter domain of the given set. |
| 4909 | */ |
| 4910 | __isl_give isl_set *isl_set_params(__isl_take isl_set *set) |
| 4911 | { |
| 4912 | isl_space *space; |
| 4913 | unsigned n; |
| 4914 | |
| 4915 | if (isl_set_is_params(set)) |
| 4916 | return set; |
| 4917 | |
| 4918 | n = isl_set_dim(set, isl_dim_set); |
| 4919 | set = isl_set_project_out(set, isl_dim_set, 0, n); |
| 4920 | space = isl_set_get_space(set); |
| 4921 | space = isl_space_params(space); |
| 4922 | set = isl_set_reset_space(set, space); |
| 4923 | return set; |
| 4924 | } |
| 4925 | |
| 4926 | /* Construct a zero-dimensional set with the given parameter domain. |
| 4927 | */ |
| 4928 | __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set) |
| 4929 | { |
| 4930 | isl_space *space; |
| 4931 | space = isl_set_get_space(set); |
| 4932 | space = isl_space_set_from_params(space); |
| 4933 | set = isl_set_reset_space(set, space); |
| 4934 | return set; |
| 4935 | } |
| 4936 | |
| 4937 | /* Compute the parameter domain of the given map. |
| 4938 | */ |
| 4939 | __isl_give isl_set *isl_map_params(__isl_take isl_map *map) |
| 4940 | { |
| 4941 | isl_space *space; |
| 4942 | unsigned n; |
| 4943 | |
| 4944 | n = isl_map_dim(map, isl_dim_in); |
| 4945 | map = isl_map_project_out(map, isl_dim_in, 0, n); |
| 4946 | n = isl_map_dim(map, isl_dim_out); |
| 4947 | map = isl_map_project_out(map, isl_dim_out, 0, n); |
| 4948 | space = isl_map_get_space(map); |
| 4949 | space = isl_space_params(space); |
| 4950 | map = isl_map_reset_space(map, space); |
| 4951 | return map; |
| 4952 | } |
| 4953 | |
| 4954 | struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap) |
| 4955 | { |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 4956 | isl_space *space; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 4957 | unsigned n_out; |
| 4958 | |
| 4959 | if (!bmap) |
| 4960 | return NULL; |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 4961 | space = isl_space_domain(isl_basic_map_get_space(bmap)); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 4962 | |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 4963 | n_out = isl_basic_map_n_out(bmap); |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 4964 | bmap = isl_basic_map_project_out(bmap, isl_dim_out, 0, n_out); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 4965 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 4966 | return isl_basic_map_reset_space(bmap, space); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 4967 | } |
| 4968 | |
| 4969 | int isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap) |
| 4970 | { |
| 4971 | if (!bmap) |
| 4972 | return -1; |
| 4973 | return isl_space_may_be_set(bmap->dim); |
| 4974 | } |
| 4975 | |
| 4976 | /* Is this basic map actually a set? |
| 4977 | * Users should never call this function. Outside of isl, |
| 4978 | * the type should indicate whether something is a set or a map. |
| 4979 | */ |
| 4980 | int isl_basic_map_is_set(__isl_keep isl_basic_map *bmap) |
| 4981 | { |
| 4982 | if (!bmap) |
| 4983 | return -1; |
| 4984 | return isl_space_is_set(bmap->dim); |
| 4985 | } |
| 4986 | |
| 4987 | struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap) |
| 4988 | { |
| 4989 | if (!bmap) |
| 4990 | return NULL; |
| 4991 | if (isl_basic_map_is_set(bmap)) |
| 4992 | return bmap; |
| 4993 | return isl_basic_map_domain(isl_basic_map_reverse(bmap)); |
| 4994 | } |
| 4995 | |
| 4996 | __isl_give isl_basic_map *isl_basic_map_domain_map( |
| 4997 | __isl_take isl_basic_map *bmap) |
| 4998 | { |
| 4999 | int i, k; |
| 5000 | isl_space *dim; |
| 5001 | isl_basic_map *domain; |
| 5002 | int nparam, n_in, n_out; |
| 5003 | unsigned total; |
| 5004 | |
| 5005 | nparam = isl_basic_map_dim(bmap, isl_dim_param); |
| 5006 | n_in = isl_basic_map_dim(bmap, isl_dim_in); |
| 5007 | n_out = isl_basic_map_dim(bmap, isl_dim_out); |
| 5008 | |
| 5009 | dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap))); |
| 5010 | domain = isl_basic_map_universe(dim); |
| 5011 | |
| 5012 | bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap)); |
| 5013 | bmap = isl_basic_map_apply_range(bmap, domain); |
| 5014 | bmap = isl_basic_map_extend_constraints(bmap, n_in, 0); |
| 5015 | |
| 5016 | total = isl_basic_map_total_dim(bmap); |
| 5017 | |
| 5018 | for (i = 0; i < n_in; ++i) { |
| 5019 | k = isl_basic_map_alloc_equality(bmap); |
| 5020 | if (k < 0) |
| 5021 | goto error; |
| 5022 | isl_seq_clr(bmap->eq[k], 1 + total); |
| 5023 | isl_int_set_si(bmap->eq[k][1 + nparam + i], -1); |
| 5024 | isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1); |
| 5025 | } |
| 5026 | |
| 5027 | bmap = isl_basic_map_gauss(bmap, NULL); |
| 5028 | return isl_basic_map_finalize(bmap); |
| 5029 | error: |
| 5030 | isl_basic_map_free(bmap); |
| 5031 | return NULL; |
| 5032 | } |
| 5033 | |
| 5034 | __isl_give isl_basic_map *isl_basic_map_range_map( |
| 5035 | __isl_take isl_basic_map *bmap) |
| 5036 | { |
| 5037 | int i, k; |
| 5038 | isl_space *dim; |
| 5039 | isl_basic_map *range; |
| 5040 | int nparam, n_in, n_out; |
| 5041 | unsigned total; |
| 5042 | |
| 5043 | nparam = isl_basic_map_dim(bmap, isl_dim_param); |
| 5044 | n_in = isl_basic_map_dim(bmap, isl_dim_in); |
| 5045 | n_out = isl_basic_map_dim(bmap, isl_dim_out); |
| 5046 | |
| 5047 | dim = isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap))); |
| 5048 | range = isl_basic_map_universe(dim); |
| 5049 | |
| 5050 | bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap)); |
| 5051 | bmap = isl_basic_map_apply_range(bmap, range); |
| 5052 | bmap = isl_basic_map_extend_constraints(bmap, n_out, 0); |
| 5053 | |
| 5054 | total = isl_basic_map_total_dim(bmap); |
| 5055 | |
| 5056 | for (i = 0; i < n_out; ++i) { |
| 5057 | k = isl_basic_map_alloc_equality(bmap); |
| 5058 | if (k < 0) |
| 5059 | goto error; |
| 5060 | isl_seq_clr(bmap->eq[k], 1 + total); |
| 5061 | isl_int_set_si(bmap->eq[k][1 + nparam + n_in + i], -1); |
| 5062 | isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1); |
| 5063 | } |
| 5064 | |
| 5065 | bmap = isl_basic_map_gauss(bmap, NULL); |
| 5066 | return isl_basic_map_finalize(bmap); |
| 5067 | error: |
| 5068 | isl_basic_map_free(bmap); |
| 5069 | return NULL; |
| 5070 | } |
| 5071 | |
| 5072 | int isl_map_may_be_set(__isl_keep isl_map *map) |
| 5073 | { |
| 5074 | if (!map) |
| 5075 | return -1; |
| 5076 | return isl_space_may_be_set(map->dim); |
| 5077 | } |
| 5078 | |
| 5079 | /* Is this map actually a set? |
| 5080 | * Users should never call this function. Outside of isl, |
| 5081 | * the type should indicate whether something is a set or a map. |
| 5082 | */ |
| 5083 | int isl_map_is_set(__isl_keep isl_map *map) |
| 5084 | { |
| 5085 | if (!map) |
| 5086 | return -1; |
| 5087 | return isl_space_is_set(map->dim); |
| 5088 | } |
| 5089 | |
| 5090 | struct isl_set *isl_map_range(struct isl_map *map) |
| 5091 | { |
| 5092 | int i; |
| 5093 | struct isl_set *set; |
| 5094 | |
| 5095 | if (!map) |
| 5096 | goto error; |
| 5097 | if (isl_map_is_set(map)) |
| 5098 | return (isl_set *)map; |
| 5099 | |
| 5100 | map = isl_map_cow(map); |
| 5101 | if (!map) |
| 5102 | goto error; |
| 5103 | |
| 5104 | set = (struct isl_set *) map; |
| 5105 | set->dim = isl_space_range(set->dim); |
| 5106 | if (!set->dim) |
| 5107 | goto error; |
| 5108 | for (i = 0; i < map->n; ++i) { |
| 5109 | set->p[i] = isl_basic_map_range(map->p[i]); |
| 5110 | if (!set->p[i]) |
| 5111 | goto error; |
| 5112 | } |
| 5113 | ISL_F_CLR(set, ISL_MAP_DISJOINT); |
| 5114 | ISL_F_CLR(set, ISL_SET_NORMALIZED); |
| 5115 | return set; |
| 5116 | error: |
| 5117 | isl_map_free(map); |
| 5118 | return NULL; |
| 5119 | } |
| 5120 | |
| 5121 | __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map) |
| 5122 | { |
| 5123 | int i; |
| 5124 | |
| 5125 | map = isl_map_cow(map); |
| 5126 | if (!map) |
| 5127 | return NULL; |
| 5128 | |
| 5129 | map->dim = isl_space_domain_map(map->dim); |
| 5130 | if (!map->dim) |
| 5131 | goto error; |
| 5132 | for (i = 0; i < map->n; ++i) { |
| 5133 | map->p[i] = isl_basic_map_domain_map(map->p[i]); |
| 5134 | if (!map->p[i]) |
| 5135 | goto error; |
| 5136 | } |
| 5137 | ISL_F_CLR(map, ISL_MAP_DISJOINT); |
| 5138 | ISL_F_CLR(map, ISL_MAP_NORMALIZED); |
| 5139 | return map; |
| 5140 | error: |
| 5141 | isl_map_free(map); |
| 5142 | return NULL; |
| 5143 | } |
| 5144 | |
| 5145 | __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map) |
| 5146 | { |
| 5147 | int i; |
| 5148 | isl_space *range_dim; |
| 5149 | |
| 5150 | map = isl_map_cow(map); |
| 5151 | if (!map) |
| 5152 | return NULL; |
| 5153 | |
| 5154 | range_dim = isl_space_range(isl_map_get_space(map)); |
| 5155 | range_dim = isl_space_from_range(range_dim); |
| 5156 | map->dim = isl_space_from_domain(isl_space_wrap(map->dim)); |
| 5157 | map->dim = isl_space_join(map->dim, range_dim); |
| 5158 | if (!map->dim) |
| 5159 | goto error; |
| 5160 | for (i = 0; i < map->n; ++i) { |
| 5161 | map->p[i] = isl_basic_map_range_map(map->p[i]); |
| 5162 | if (!map->p[i]) |
| 5163 | goto error; |
| 5164 | } |
| 5165 | ISL_F_CLR(map, ISL_MAP_DISJOINT); |
| 5166 | ISL_F_CLR(map, ISL_MAP_NORMALIZED); |
| 5167 | return map; |
| 5168 | error: |
| 5169 | isl_map_free(map); |
| 5170 | return NULL; |
| 5171 | } |
| 5172 | |
| 5173 | /* Given a wrapped map of the form A[B -> C], |
| 5174 | * return the map A[B -> C] -> B. |
| 5175 | */ |
| 5176 | __isl_give isl_map *isl_set_wrapped_domain_map(__isl_take isl_set *set) |
| 5177 | { |
| 5178 | isl_id *id; |
| 5179 | isl_map *map; |
| 5180 | |
| 5181 | if (!set) |
| 5182 | return NULL; |
| 5183 | if (!isl_set_has_tuple_id(set)) |
| 5184 | return isl_map_domain_map(isl_set_unwrap(set)); |
| 5185 | |
| 5186 | id = isl_set_get_tuple_id(set); |
| 5187 | map = isl_map_domain_map(isl_set_unwrap(set)); |
| 5188 | map = isl_map_set_tuple_id(map, isl_dim_in, id); |
| 5189 | |
| 5190 | return map; |
| 5191 | } |
| 5192 | |
| 5193 | __isl_give isl_map *isl_map_from_set(__isl_take isl_set *set, |
| 5194 | __isl_take isl_space *dim) |
| 5195 | { |
| 5196 | int i; |
| 5197 | struct isl_map *map = NULL; |
| 5198 | |
| 5199 | set = isl_set_cow(set); |
| 5200 | if (!set || !dim) |
| 5201 | goto error; |
| 5202 | isl_assert(set->ctx, isl_space_compatible(set->dim, dim), goto error); |
| 5203 | map = (struct isl_map *)set; |
| 5204 | for (i = 0; i < set->n; ++i) { |
| 5205 | map->p[i] = isl_basic_map_from_basic_set( |
| 5206 | set->p[i], isl_space_copy(dim)); |
| 5207 | if (!map->p[i]) |
| 5208 | goto error; |
| 5209 | } |
| 5210 | isl_space_free(map->dim); |
| 5211 | map->dim = dim; |
| 5212 | return map; |
| 5213 | error: |
| 5214 | isl_space_free(dim); |
| 5215 | isl_set_free(set); |
| 5216 | return NULL; |
| 5217 | } |
| 5218 | |
| 5219 | __isl_give isl_basic_map *isl_basic_map_from_domain( |
| 5220 | __isl_take isl_basic_set *bset) |
| 5221 | { |
| 5222 | return isl_basic_map_reverse(isl_basic_map_from_range(bset)); |
| 5223 | } |
| 5224 | |
| 5225 | __isl_give isl_basic_map *isl_basic_map_from_range( |
| 5226 | __isl_take isl_basic_set *bset) |
| 5227 | { |
| 5228 | isl_space *space; |
| 5229 | space = isl_basic_set_get_space(bset); |
| 5230 | space = isl_space_from_range(space); |
| 5231 | bset = isl_basic_set_reset_space(bset, space); |
| 5232 | return (isl_basic_map *)bset; |
| 5233 | } |
| 5234 | |
| 5235 | /* Create a relation with the given set as range. |
| 5236 | * The domain of the created relation is a zero-dimensional |
| 5237 | * flat anonymous space. |
| 5238 | */ |
| 5239 | __isl_give isl_map *isl_map_from_range(__isl_take isl_set *set) |
| 5240 | { |
| 5241 | isl_space *space; |
| 5242 | space = isl_set_get_space(set); |
| 5243 | space = isl_space_from_range(space); |
| 5244 | set = isl_set_reset_space(set, space); |
| 5245 | return (struct isl_map *)set; |
| 5246 | } |
| 5247 | |
| 5248 | /* Create a relation with the given set as domain. |
| 5249 | * The range of the created relation is a zero-dimensional |
| 5250 | * flat anonymous space. |
| 5251 | */ |
| 5252 | __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set) |
| 5253 | { |
| 5254 | return isl_map_reverse(isl_map_from_range(set)); |
| 5255 | } |
| 5256 | |
| 5257 | __isl_give isl_basic_map *isl_basic_map_from_domain_and_range( |
| 5258 | __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range) |
| 5259 | { |
| 5260 | return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range); |
| 5261 | } |
| 5262 | |
| 5263 | __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain, |
| 5264 | __isl_take isl_set *range) |
| 5265 | { |
| 5266 | return isl_map_apply_range(isl_map_reverse(domain), range); |
| 5267 | } |
| 5268 | |
Tobias Grosser | 07b2095 | 2016-06-12 04:30:40 +0000 | [diff] [blame^] | 5269 | /* Return a newly allocated isl_map with given space and flags and |
| 5270 | * room for "n" basic maps. |
| 5271 | * Make sure that all cached information is cleared. |
| 5272 | */ |
| 5273 | __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *space, int n, |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 5274 | unsigned flags) |
| 5275 | { |
| 5276 | struct isl_map *map; |
| 5277 | |
Tobias Grosser | 07b2095 | 2016-06-12 04:30:40 +0000 | [diff] [blame^] | 5278 | if (!space) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 5279 | return NULL; |
| 5280 | if (n < 0) |
Tobias Grosser | 07b2095 | 2016-06-12 04:30:40 +0000 | [diff] [blame^] | 5281 | isl_die(space->ctx, isl_error_internal, |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 5282 | "negative number of basic maps", goto error); |
Tobias Grosser | 07b2095 | 2016-06-12 04:30:40 +0000 | [diff] [blame^] | 5283 | map = isl_calloc(space->ctx, struct isl_map, |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 5284 | sizeof(struct isl_map) + |
| 5285 | (n - 1) * sizeof(struct isl_basic_map *)); |
| 5286 | if (!map) |
| 5287 | goto error; |
| 5288 | |
Tobias Grosser | 07b2095 | 2016-06-12 04:30:40 +0000 | [diff] [blame^] | 5289 | map->ctx = space->ctx; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 5290 | isl_ctx_ref(map->ctx); |
| 5291 | map->ref = 1; |
| 5292 | map->size = n; |
| 5293 | map->n = 0; |
Tobias Grosser | 07b2095 | 2016-06-12 04:30:40 +0000 | [diff] [blame^] | 5294 | map->dim = space; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 5295 | map->flags = flags; |
| 5296 | return map; |
| 5297 | error: |
Tobias Grosser | 07b2095 | 2016-06-12 04:30:40 +0000 | [diff] [blame^] | 5298 | isl_space_free(space); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 5299 | return NULL; |
| 5300 | } |
| 5301 | |
| 5302 | struct isl_map *isl_map_alloc(struct isl_ctx *ctx, |
| 5303 | unsigned nparam, unsigned in, unsigned out, int n, |
| 5304 | unsigned flags) |
| 5305 | { |
| 5306 | struct isl_map *map; |
| 5307 | isl_space *dims; |
| 5308 | |
| 5309 | dims = isl_space_alloc(ctx, nparam, in, out); |
| 5310 | if (!dims) |
| 5311 | return NULL; |
| 5312 | |
| 5313 | map = isl_map_alloc_space(dims, n, flags); |
| 5314 | return map; |
| 5315 | } |
| 5316 | |
| 5317 | __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *dim) |
| 5318 | { |
| 5319 | struct isl_basic_map *bmap; |
| 5320 | bmap = isl_basic_map_alloc_space(dim, 0, 1, 0); |
| 5321 | bmap = isl_basic_map_set_to_empty(bmap); |
| 5322 | return bmap; |
| 5323 | } |
| 5324 | |
| 5325 | __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *dim) |
| 5326 | { |
| 5327 | struct isl_basic_set *bset; |
| 5328 | bset = isl_basic_set_alloc_space(dim, 0, 1, 0); |
| 5329 | bset = isl_basic_set_set_to_empty(bset); |
| 5330 | return bset; |
| 5331 | } |
| 5332 | |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 5333 | __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *dim) |
| 5334 | { |
| 5335 | struct isl_basic_map *bmap; |
| 5336 | bmap = isl_basic_map_alloc_space(dim, 0, 0, 0); |
| 5337 | bmap = isl_basic_map_finalize(bmap); |
| 5338 | return bmap; |
| 5339 | } |
| 5340 | |
| 5341 | __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *dim) |
| 5342 | { |
| 5343 | struct isl_basic_set *bset; |
| 5344 | bset = isl_basic_set_alloc_space(dim, 0, 0, 0); |
| 5345 | bset = isl_basic_set_finalize(bset); |
| 5346 | return bset; |
| 5347 | } |
| 5348 | |
| 5349 | __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_space *dim) |
| 5350 | { |
| 5351 | int i; |
| 5352 | unsigned total = isl_space_dim(dim, isl_dim_all); |
| 5353 | isl_basic_map *bmap; |
| 5354 | |
| 5355 | bmap= isl_basic_map_alloc_space(dim, 0, 0, total); |
| 5356 | for (i = 0; i < total; ++i) { |
| 5357 | int k = isl_basic_map_alloc_inequality(bmap); |
| 5358 | if (k < 0) |
| 5359 | goto error; |
| 5360 | isl_seq_clr(bmap->ineq[k], 1 + total); |
| 5361 | isl_int_set_si(bmap->ineq[k][1 + i], 1); |
| 5362 | } |
| 5363 | return bmap; |
| 5364 | error: |
| 5365 | isl_basic_map_free(bmap); |
| 5366 | return NULL; |
| 5367 | } |
| 5368 | |
| 5369 | __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_space *dim) |
| 5370 | { |
| 5371 | return isl_basic_map_nat_universe(dim); |
| 5372 | } |
| 5373 | |
| 5374 | __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim) |
| 5375 | { |
| 5376 | return isl_map_from_basic_map(isl_basic_map_nat_universe(dim)); |
| 5377 | } |
| 5378 | |
| 5379 | __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim) |
| 5380 | { |
| 5381 | return isl_map_nat_universe(dim); |
| 5382 | } |
| 5383 | |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 5384 | __isl_give isl_map *isl_map_empty(__isl_take isl_space *dim) |
| 5385 | { |
| 5386 | return isl_map_alloc_space(dim, 0, ISL_MAP_DISJOINT); |
| 5387 | } |
| 5388 | |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 5389 | __isl_give isl_set *isl_set_empty(__isl_take isl_space *dim) |
| 5390 | { |
| 5391 | return isl_set_alloc_space(dim, 0, ISL_MAP_DISJOINT); |
| 5392 | } |
| 5393 | |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 5394 | __isl_give isl_map *isl_map_universe(__isl_take isl_space *dim) |
| 5395 | { |
| 5396 | struct isl_map *map; |
| 5397 | if (!dim) |
| 5398 | return NULL; |
| 5399 | map = isl_map_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT); |
| 5400 | map = isl_map_add_basic_map(map, isl_basic_map_universe(dim)); |
| 5401 | return map; |
| 5402 | } |
| 5403 | |
| 5404 | __isl_give isl_set *isl_set_universe(__isl_take isl_space *dim) |
| 5405 | { |
| 5406 | struct isl_set *set; |
| 5407 | if (!dim) |
| 5408 | return NULL; |
| 5409 | set = isl_set_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT); |
| 5410 | set = isl_set_add_basic_set(set, isl_basic_set_universe(dim)); |
| 5411 | return set; |
| 5412 | } |
| 5413 | |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 5414 | struct isl_map *isl_map_dup(struct isl_map *map) |
| 5415 | { |
| 5416 | int i; |
| 5417 | struct isl_map *dup; |
| 5418 | |
| 5419 | if (!map) |
| 5420 | return NULL; |
| 5421 | dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags); |
| 5422 | for (i = 0; i < map->n; ++i) |
| 5423 | dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i])); |
| 5424 | return dup; |
| 5425 | } |
| 5426 | |
| 5427 | __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map, |
| 5428 | __isl_take isl_basic_map *bmap) |
| 5429 | { |
| 5430 | if (!bmap || !map) |
| 5431 | goto error; |
| 5432 | if (isl_basic_map_plain_is_empty(bmap)) { |
| 5433 | isl_basic_map_free(bmap); |
| 5434 | return map; |
| 5435 | } |
| 5436 | isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error); |
| 5437 | isl_assert(map->ctx, map->n < map->size, goto error); |
| 5438 | map->p[map->n] = bmap; |
| 5439 | map->n++; |
| 5440 | ISL_F_CLR(map, ISL_MAP_NORMALIZED); |
| 5441 | return map; |
| 5442 | error: |
| 5443 | if (map) |
| 5444 | isl_map_free(map); |
| 5445 | if (bmap) |
| 5446 | isl_basic_map_free(bmap); |
| 5447 | return NULL; |
| 5448 | } |
| 5449 | |
| 5450 | __isl_null isl_map *isl_map_free(__isl_take isl_map *map) |
| 5451 | { |
| 5452 | int i; |
| 5453 | |
| 5454 | if (!map) |
| 5455 | return NULL; |
| 5456 | |
| 5457 | if (--map->ref > 0) |
| 5458 | return NULL; |
| 5459 | |
Tobias Grosser | 07b2095 | 2016-06-12 04:30:40 +0000 | [diff] [blame^] | 5460 | clear_caches(map); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 5461 | isl_ctx_deref(map->ctx); |
| 5462 | for (i = 0; i < map->n; ++i) |
| 5463 | isl_basic_map_free(map->p[i]); |
| 5464 | isl_space_free(map->dim); |
| 5465 | free(map); |
| 5466 | |
| 5467 | return NULL; |
| 5468 | } |
| 5469 | |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 5470 | static struct isl_basic_map *isl_basic_map_fix_pos_si( |
| 5471 | struct isl_basic_map *bmap, unsigned pos, int value) |
| 5472 | { |
| 5473 | int j; |
| 5474 | |
| 5475 | bmap = isl_basic_map_cow(bmap); |
| 5476 | bmap = isl_basic_map_extend_constraints(bmap, 1, 0); |
| 5477 | j = isl_basic_map_alloc_equality(bmap); |
| 5478 | if (j < 0) |
| 5479 | goto error; |
| 5480 | isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap)); |
| 5481 | isl_int_set_si(bmap->eq[j][pos], -1); |
| 5482 | isl_int_set_si(bmap->eq[j][0], value); |
| 5483 | bmap = isl_basic_map_simplify(bmap); |
| 5484 | return isl_basic_map_finalize(bmap); |
| 5485 | error: |
| 5486 | isl_basic_map_free(bmap); |
| 5487 | return NULL; |
| 5488 | } |
| 5489 | |
| 5490 | static __isl_give isl_basic_map *isl_basic_map_fix_pos( |
| 5491 | __isl_take isl_basic_map *bmap, unsigned pos, isl_int value) |
| 5492 | { |
| 5493 | int j; |
| 5494 | |
| 5495 | bmap = isl_basic_map_cow(bmap); |
| 5496 | bmap = isl_basic_map_extend_constraints(bmap, 1, 0); |
| 5497 | j = isl_basic_map_alloc_equality(bmap); |
| 5498 | if (j < 0) |
| 5499 | goto error; |
| 5500 | isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap)); |
| 5501 | isl_int_set_si(bmap->eq[j][pos], -1); |
| 5502 | isl_int_set(bmap->eq[j][0], value); |
| 5503 | bmap = isl_basic_map_simplify(bmap); |
| 5504 | return isl_basic_map_finalize(bmap); |
| 5505 | error: |
| 5506 | isl_basic_map_free(bmap); |
| 5507 | return NULL; |
| 5508 | } |
| 5509 | |
| 5510 | struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap, |
| 5511 | enum isl_dim_type type, unsigned pos, int value) |
| 5512 | { |
| 5513 | if (!bmap) |
| 5514 | return NULL; |
| 5515 | isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error); |
| 5516 | return isl_basic_map_fix_pos_si(bmap, |
| 5517 | isl_basic_map_offset(bmap, type) + pos, value); |
| 5518 | error: |
| 5519 | isl_basic_map_free(bmap); |
| 5520 | return NULL; |
| 5521 | } |
| 5522 | |
| 5523 | __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap, |
| 5524 | enum isl_dim_type type, unsigned pos, isl_int value) |
| 5525 | { |
| 5526 | if (!bmap) |
| 5527 | return NULL; |
| 5528 | isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error); |
| 5529 | return isl_basic_map_fix_pos(bmap, |
| 5530 | isl_basic_map_offset(bmap, type) + pos, value); |
| 5531 | error: |
| 5532 | isl_basic_map_free(bmap); |
| 5533 | return NULL; |
| 5534 | } |
| 5535 | |
| 5536 | /* Fix the value of the variable at position "pos" of type "type" of "bmap" |
| 5537 | * to be equal to "v". |
| 5538 | */ |
| 5539 | __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap, |
| 5540 | enum isl_dim_type type, unsigned pos, __isl_take isl_val *v) |
| 5541 | { |
| 5542 | if (!bmap || !v) |
| 5543 | goto error; |
| 5544 | if (!isl_val_is_int(v)) |
| 5545 | isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid, |
| 5546 | "expecting integer value", goto error); |
| 5547 | if (pos >= isl_basic_map_dim(bmap, type)) |
| 5548 | isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid, |
| 5549 | "index out of bounds", goto error); |
| 5550 | pos += isl_basic_map_offset(bmap, type); |
| 5551 | bmap = isl_basic_map_fix_pos(bmap, pos, v->n); |
| 5552 | isl_val_free(v); |
| 5553 | return bmap; |
| 5554 | error: |
| 5555 | isl_basic_map_free(bmap); |
| 5556 | isl_val_free(v); |
| 5557 | return NULL; |
| 5558 | } |
| 5559 | |
| 5560 | /* Fix the value of the variable at position "pos" of type "type" of "bset" |
| 5561 | * to be equal to "v". |
| 5562 | */ |
| 5563 | __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset, |
| 5564 | enum isl_dim_type type, unsigned pos, __isl_take isl_val *v) |
| 5565 | { |
| 5566 | return isl_basic_map_fix_val(bset, type, pos, v); |
| 5567 | } |
| 5568 | |
| 5569 | struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset, |
| 5570 | enum isl_dim_type type, unsigned pos, int value) |
| 5571 | { |
| 5572 | return (struct isl_basic_set *) |
| 5573 | isl_basic_map_fix_si((struct isl_basic_map *)bset, |
| 5574 | type, pos, value); |
| 5575 | } |
| 5576 | |
| 5577 | __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset, |
| 5578 | enum isl_dim_type type, unsigned pos, isl_int value) |
| 5579 | { |
| 5580 | return (struct isl_basic_set *) |
| 5581 | isl_basic_map_fix((struct isl_basic_map *)bset, |
| 5582 | type, pos, value); |
| 5583 | } |
| 5584 | |
| 5585 | struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap, |
| 5586 | unsigned input, int value) |
| 5587 | { |
| 5588 | return isl_basic_map_fix_si(bmap, isl_dim_in, input, value); |
| 5589 | } |
| 5590 | |
| 5591 | struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset, |
| 5592 | unsigned dim, int value) |
| 5593 | { |
| 5594 | return (struct isl_basic_set *) |
| 5595 | isl_basic_map_fix_si((struct isl_basic_map *)bset, |
| 5596 | isl_dim_set, dim, value); |
| 5597 | } |
| 5598 | |
| 5599 | static int remove_if_empty(__isl_keep isl_map *map, int i) |
| 5600 | { |
| 5601 | int empty = isl_basic_map_plain_is_empty(map->p[i]); |
| 5602 | |
| 5603 | if (empty < 0) |
| 5604 | return -1; |
| 5605 | if (!empty) |
| 5606 | return 0; |
| 5607 | |
| 5608 | isl_basic_map_free(map->p[i]); |
| 5609 | if (i != map->n - 1) { |
| 5610 | ISL_F_CLR(map, ISL_MAP_NORMALIZED); |
| 5611 | map->p[i] = map->p[map->n - 1]; |
| 5612 | } |
| 5613 | map->n--; |
| 5614 | |
| 5615 | return 0; |
| 5616 | } |
| 5617 | |
| 5618 | /* Perform "fn" on each basic map of "map", where we may not be holding |
| 5619 | * the only reference to "map". |
| 5620 | * In particular, "fn" should be a semantics preserving operation |
| 5621 | * that we want to apply to all copies of "map". We therefore need |
| 5622 | * to be careful not to modify "map" in a way that breaks "map" |
| 5623 | * in case anything goes wrong. |
| 5624 | */ |
| 5625 | __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map, |
| 5626 | __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap)) |
| 5627 | { |
| 5628 | struct isl_basic_map *bmap; |
| 5629 | int i; |
| 5630 | |
| 5631 | if (!map) |
| 5632 | return NULL; |
| 5633 | |
| 5634 | for (i = map->n - 1; i >= 0; --i) { |
| 5635 | bmap = isl_basic_map_copy(map->p[i]); |
| 5636 | bmap = fn(bmap); |
| 5637 | if (!bmap) |
| 5638 | goto error; |
| 5639 | isl_basic_map_free(map->p[i]); |
| 5640 | map->p[i] = bmap; |
| 5641 | if (remove_if_empty(map, i) < 0) |
| 5642 | goto error; |
| 5643 | } |
| 5644 | |
| 5645 | return map; |
| 5646 | error: |
| 5647 | isl_map_free(map); |
| 5648 | return NULL; |
| 5649 | } |
| 5650 | |
| 5651 | struct isl_map *isl_map_fix_si(struct isl_map *map, |
| 5652 | enum isl_dim_type type, unsigned pos, int value) |
| 5653 | { |
| 5654 | int i; |
| 5655 | |
| 5656 | map = isl_map_cow(map); |
| 5657 | if (!map) |
| 5658 | return NULL; |
| 5659 | |
| 5660 | isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error); |
| 5661 | for (i = map->n - 1; i >= 0; --i) { |
| 5662 | map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value); |
| 5663 | if (remove_if_empty(map, i) < 0) |
| 5664 | goto error; |
| 5665 | } |
| 5666 | ISL_F_CLR(map, ISL_MAP_NORMALIZED); |
| 5667 | return map; |
| 5668 | error: |
| 5669 | isl_map_free(map); |
| 5670 | return NULL; |
| 5671 | } |
| 5672 | |
| 5673 | __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set, |
| 5674 | enum isl_dim_type type, unsigned pos, int value) |
| 5675 | { |
| 5676 | return (struct isl_set *) |
| 5677 | isl_map_fix_si((struct isl_map *)set, type, pos, value); |
| 5678 | } |
| 5679 | |
| 5680 | __isl_give isl_map *isl_map_fix(__isl_take isl_map *map, |
| 5681 | enum isl_dim_type type, unsigned pos, isl_int value) |
| 5682 | { |
| 5683 | int i; |
| 5684 | |
| 5685 | map = isl_map_cow(map); |
| 5686 | if (!map) |
| 5687 | return NULL; |
| 5688 | |
| 5689 | isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error); |
| 5690 | for (i = 0; i < map->n; ++i) { |
| 5691 | map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value); |
| 5692 | if (!map->p[i]) |
| 5693 | goto error; |
| 5694 | } |
| 5695 | ISL_F_CLR(map, ISL_MAP_NORMALIZED); |
| 5696 | return map; |
| 5697 | error: |
| 5698 | isl_map_free(map); |
| 5699 | return NULL; |
| 5700 | } |
| 5701 | |
| 5702 | __isl_give isl_set *isl_set_fix(__isl_take isl_set *set, |
| 5703 | enum isl_dim_type type, unsigned pos, isl_int value) |
| 5704 | { |
| 5705 | return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value); |
| 5706 | } |
| 5707 | |
| 5708 | /* Fix the value of the variable at position "pos" of type "type" of "map" |
| 5709 | * to be equal to "v". |
| 5710 | */ |
| 5711 | __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map, |
| 5712 | enum isl_dim_type type, unsigned pos, __isl_take isl_val *v) |
| 5713 | { |
| 5714 | int i; |
| 5715 | |
| 5716 | map = isl_map_cow(map); |
| 5717 | if (!map || !v) |
| 5718 | goto error; |
| 5719 | |
| 5720 | if (!isl_val_is_int(v)) |
| 5721 | isl_die(isl_map_get_ctx(map), isl_error_invalid, |
| 5722 | "expecting integer value", goto error); |
| 5723 | if (pos >= isl_map_dim(map, type)) |
| 5724 | isl_die(isl_map_get_ctx(map), isl_error_invalid, |
| 5725 | "index out of bounds", goto error); |
| 5726 | for (i = map->n - 1; i >= 0; --i) { |
| 5727 | map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos, |
| 5728 | isl_val_copy(v)); |
| 5729 | if (remove_if_empty(map, i) < 0) |
| 5730 | goto error; |
| 5731 | } |
| 5732 | ISL_F_CLR(map, ISL_MAP_NORMALIZED); |
| 5733 | isl_val_free(v); |
| 5734 | return map; |
| 5735 | error: |
| 5736 | isl_map_free(map); |
| 5737 | isl_val_free(v); |
| 5738 | return NULL; |
| 5739 | } |
| 5740 | |
| 5741 | /* Fix the value of the variable at position "pos" of type "type" of "set" |
| 5742 | * to be equal to "v". |
| 5743 | */ |
| 5744 | __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set, |
| 5745 | enum isl_dim_type type, unsigned pos, __isl_take isl_val *v) |
| 5746 | { |
| 5747 | return isl_map_fix_val(set, type, pos, v); |
| 5748 | } |
| 5749 | |
| 5750 | struct isl_map *isl_map_fix_input_si(struct isl_map *map, |
| 5751 | unsigned input, int value) |
| 5752 | { |
| 5753 | return isl_map_fix_si(map, isl_dim_in, input, value); |
| 5754 | } |
| 5755 | |
| 5756 | struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value) |
| 5757 | { |
| 5758 | return (struct isl_set *) |
| 5759 | isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value); |
| 5760 | } |
| 5761 | |
| 5762 | static __isl_give isl_basic_map *basic_map_bound_si( |
| 5763 | __isl_take isl_basic_map *bmap, |
| 5764 | enum isl_dim_type type, unsigned pos, int value, int upper) |
| 5765 | { |
| 5766 | int j; |
| 5767 | |
| 5768 | if (!bmap) |
| 5769 | return NULL; |
| 5770 | isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error); |
| 5771 | pos += isl_basic_map_offset(bmap, type); |
| 5772 | bmap = isl_basic_map_cow(bmap); |
| 5773 | bmap = isl_basic_map_extend_constraints(bmap, 0, 1); |
| 5774 | j = isl_basic_map_alloc_inequality(bmap); |
| 5775 | if (j < 0) |
| 5776 | goto error; |
| 5777 | isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap)); |
| 5778 | if (upper) { |
| 5779 | isl_int_set_si(bmap->ineq[j][pos], -1); |
| 5780 | isl_int_set_si(bmap->ineq[j][0], value); |
| 5781 | } else { |
| 5782 | isl_int_set_si(bmap->ineq[j][pos], 1); |
| 5783 | isl_int_set_si(bmap->ineq[j][0], -value); |
| 5784 | } |
| 5785 | bmap = isl_basic_map_simplify(bmap); |
| 5786 | return isl_basic_map_finalize(bmap); |
| 5787 | error: |
| 5788 | isl_basic_map_free(bmap); |
| 5789 | return NULL; |
| 5790 | } |
| 5791 | |
| 5792 | __isl_give isl_basic_map *isl_basic_map_lower_bound_si( |
| 5793 | __isl_take isl_basic_map *bmap, |
| 5794 | enum isl_dim_type type, unsigned pos, int value) |
| 5795 | { |
| 5796 | return basic_map_bound_si(bmap, type, pos, value, 0); |
| 5797 | } |
| 5798 | |
| 5799 | /* Constrain the values of the given dimension to be no greater than "value". |
| 5800 | */ |
| 5801 | __isl_give isl_basic_map *isl_basic_map_upper_bound_si( |
| 5802 | __isl_take isl_basic_map *bmap, |
| 5803 | enum isl_dim_type type, unsigned pos, int value) |
| 5804 | { |
| 5805 | return basic_map_bound_si(bmap, type, pos, value, 1); |
| 5806 | } |
| 5807 | |
| 5808 | struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset, |
| 5809 | unsigned dim, isl_int value) |
| 5810 | { |
| 5811 | int j; |
| 5812 | |
| 5813 | bset = isl_basic_set_cow(bset); |
| 5814 | bset = isl_basic_set_extend_constraints(bset, 0, 1); |
| 5815 | j = isl_basic_set_alloc_inequality(bset); |
| 5816 | if (j < 0) |
| 5817 | goto error; |
| 5818 | isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset)); |
| 5819 | isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1); |
| 5820 | isl_int_neg(bset->ineq[j][0], value); |
| 5821 | bset = isl_basic_set_simplify(bset); |
| 5822 | return isl_basic_set_finalize(bset); |
| 5823 | error: |
| 5824 | isl_basic_set_free(bset); |
| 5825 | return NULL; |
| 5826 | } |
| 5827 | |
| 5828 | static __isl_give isl_map *map_bound_si(__isl_take isl_map *map, |
| 5829 | enum isl_dim_type type, unsigned pos, int value, int upper) |
| 5830 | { |
| 5831 | int i; |
| 5832 | |
| 5833 | map = isl_map_cow(map); |
| 5834 | if (!map) |
| 5835 | return NULL; |
| 5836 | |
| 5837 | isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error); |
| 5838 | for (i = 0; i < map->n; ++i) { |
| 5839 | map->p[i] = basic_map_bound_si(map->p[i], |
| 5840 | type, pos, value, upper); |
| 5841 | if (!map->p[i]) |
| 5842 | goto error; |
| 5843 | } |
| 5844 | ISL_F_CLR(map, ISL_MAP_NORMALIZED); |
| 5845 | return map; |
| 5846 | error: |
| 5847 | isl_map_free(map); |
| 5848 | return NULL; |
| 5849 | } |
| 5850 | |
| 5851 | __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map, |
| 5852 | enum isl_dim_type type, unsigned pos, int value) |
| 5853 | { |
| 5854 | return map_bound_si(map, type, pos, value, 0); |
| 5855 | } |
| 5856 | |
| 5857 | __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map, |
| 5858 | enum isl_dim_type type, unsigned pos, int value) |
| 5859 | { |
| 5860 | return map_bound_si(map, type, pos, value, 1); |
| 5861 | } |
| 5862 | |
| 5863 | __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set, |
| 5864 | enum isl_dim_type type, unsigned pos, int value) |
| 5865 | { |
| 5866 | return (struct isl_set *) |
| 5867 | isl_map_lower_bound_si((struct isl_map *)set, type, pos, value); |
| 5868 | } |
| 5869 | |
| 5870 | __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set, |
| 5871 | enum isl_dim_type type, unsigned pos, int value) |
| 5872 | { |
| 5873 | return isl_map_upper_bound_si(set, type, pos, value); |
| 5874 | } |
| 5875 | |
| 5876 | /* Bound the given variable of "bmap" from below (or above is "upper" |
| 5877 | * is set) to "value". |
| 5878 | */ |
| 5879 | static __isl_give isl_basic_map *basic_map_bound( |
| 5880 | __isl_take isl_basic_map *bmap, |
| 5881 | enum isl_dim_type type, unsigned pos, isl_int value, int upper) |
| 5882 | { |
| 5883 | int j; |
| 5884 | |
| 5885 | if (!bmap) |
| 5886 | return NULL; |
| 5887 | if (pos >= isl_basic_map_dim(bmap, type)) |
| 5888 | isl_die(bmap->ctx, isl_error_invalid, |
| 5889 | "index out of bounds", goto error); |
| 5890 | pos += isl_basic_map_offset(bmap, type); |
| 5891 | bmap = isl_basic_map_cow(bmap); |
| 5892 | bmap = isl_basic_map_extend_constraints(bmap, 0, 1); |
| 5893 | j = isl_basic_map_alloc_inequality(bmap); |
| 5894 | if (j < 0) |
| 5895 | goto error; |
| 5896 | isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap)); |
| 5897 | if (upper) { |
| 5898 | isl_int_set_si(bmap->ineq[j][pos], -1); |
| 5899 | isl_int_set(bmap->ineq[j][0], value); |
| 5900 | } else { |
| 5901 | isl_int_set_si(bmap->ineq[j][pos], 1); |
| 5902 | isl_int_neg(bmap->ineq[j][0], value); |
| 5903 | } |
| 5904 | bmap = isl_basic_map_simplify(bmap); |
| 5905 | return isl_basic_map_finalize(bmap); |
| 5906 | error: |
| 5907 | isl_basic_map_free(bmap); |
| 5908 | return NULL; |
| 5909 | } |
| 5910 | |
| 5911 | /* Bound the given variable of "map" from below (or above is "upper" |
| 5912 | * is set) to "value". |
| 5913 | */ |
| 5914 | static __isl_give isl_map *map_bound(__isl_take isl_map *map, |
| 5915 | enum isl_dim_type type, unsigned pos, isl_int value, int upper) |
| 5916 | { |
| 5917 | int i; |
| 5918 | |
| 5919 | map = isl_map_cow(map); |
| 5920 | if (!map) |
| 5921 | return NULL; |
| 5922 | |
| 5923 | if (pos >= isl_map_dim(map, type)) |
| 5924 | isl_die(map->ctx, isl_error_invalid, |
| 5925 | "index out of bounds", goto error); |
| 5926 | for (i = map->n - 1; i >= 0; --i) { |
| 5927 | map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper); |
| 5928 | if (remove_if_empty(map, i) < 0) |
| 5929 | goto error; |
| 5930 | } |
| 5931 | ISL_F_CLR(map, ISL_MAP_NORMALIZED); |
| 5932 | return map; |
| 5933 | error: |
| 5934 | isl_map_free(map); |
| 5935 | return NULL; |
| 5936 | } |
| 5937 | |
| 5938 | __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map, |
| 5939 | enum isl_dim_type type, unsigned pos, isl_int value) |
| 5940 | { |
| 5941 | return map_bound(map, type, pos, value, 0); |
| 5942 | } |
| 5943 | |
| 5944 | __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map, |
| 5945 | enum isl_dim_type type, unsigned pos, isl_int value) |
| 5946 | { |
| 5947 | return map_bound(map, type, pos, value, 1); |
| 5948 | } |
| 5949 | |
| 5950 | __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set, |
| 5951 | enum isl_dim_type type, unsigned pos, isl_int value) |
| 5952 | { |
| 5953 | return isl_map_lower_bound(set, type, pos, value); |
| 5954 | } |
| 5955 | |
| 5956 | __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set, |
| 5957 | enum isl_dim_type type, unsigned pos, isl_int value) |
| 5958 | { |
| 5959 | return isl_map_upper_bound(set, type, pos, value); |
| 5960 | } |
| 5961 | |
| 5962 | /* Force the values of the variable at position "pos" of type "type" of "set" |
| 5963 | * to be no smaller than "value". |
| 5964 | */ |
| 5965 | __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set, |
| 5966 | enum isl_dim_type type, unsigned pos, __isl_take isl_val *value) |
| 5967 | { |
| 5968 | if (!value) |
| 5969 | goto error; |
| 5970 | if (!isl_val_is_int(value)) |
| 5971 | isl_die(isl_set_get_ctx(set), isl_error_invalid, |
| 5972 | "expecting integer value", goto error); |
| 5973 | set = isl_set_lower_bound(set, type, pos, value->n); |
| 5974 | isl_val_free(value); |
| 5975 | return set; |
| 5976 | error: |
| 5977 | isl_val_free(value); |
| 5978 | isl_set_free(set); |
| 5979 | return NULL; |
| 5980 | } |
| 5981 | |
| 5982 | /* Force the values of the variable at position "pos" of type "type" of "set" |
| 5983 | * to be no greater than "value". |
| 5984 | */ |
| 5985 | __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set, |
| 5986 | enum isl_dim_type type, unsigned pos, __isl_take isl_val *value) |
| 5987 | { |
| 5988 | if (!value) |
| 5989 | goto error; |
| 5990 | if (!isl_val_is_int(value)) |
| 5991 | isl_die(isl_set_get_ctx(set), isl_error_invalid, |
| 5992 | "expecting integer value", goto error); |
| 5993 | set = isl_set_upper_bound(set, type, pos, value->n); |
| 5994 | isl_val_free(value); |
| 5995 | return set; |
| 5996 | error: |
| 5997 | isl_val_free(value); |
| 5998 | isl_set_free(set); |
| 5999 | return NULL; |
| 6000 | } |
| 6001 | |
| 6002 | struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim, |
| 6003 | isl_int value) |
| 6004 | { |
| 6005 | int i; |
| 6006 | |
| 6007 | set = isl_set_cow(set); |
| 6008 | if (!set) |
| 6009 | return NULL; |
| 6010 | |
| 6011 | isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error); |
| 6012 | for (i = 0; i < set->n; ++i) { |
| 6013 | set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value); |
| 6014 | if (!set->p[i]) |
| 6015 | goto error; |
| 6016 | } |
| 6017 | return set; |
| 6018 | error: |
| 6019 | isl_set_free(set); |
| 6020 | return NULL; |
| 6021 | } |
| 6022 | |
| 6023 | struct isl_map *isl_map_reverse(struct isl_map *map) |
| 6024 | { |
| 6025 | int i; |
| 6026 | |
| 6027 | map = isl_map_cow(map); |
| 6028 | if (!map) |
| 6029 | return NULL; |
| 6030 | |
| 6031 | map->dim = isl_space_reverse(map->dim); |
| 6032 | if (!map->dim) |
| 6033 | goto error; |
| 6034 | for (i = 0; i < map->n; ++i) { |
| 6035 | map->p[i] = isl_basic_map_reverse(map->p[i]); |
| 6036 | if (!map->p[i]) |
| 6037 | goto error; |
| 6038 | } |
| 6039 | ISL_F_CLR(map, ISL_MAP_NORMALIZED); |
| 6040 | return map; |
| 6041 | error: |
| 6042 | isl_map_free(map); |
| 6043 | return NULL; |
| 6044 | } |
| 6045 | |
| 6046 | static struct isl_map *isl_basic_map_partial_lexopt( |
| 6047 | struct isl_basic_map *bmap, struct isl_basic_set *dom, |
| 6048 | struct isl_set **empty, int max) |
| 6049 | { |
| 6050 | return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max); |
| 6051 | } |
| 6052 | |
| 6053 | struct isl_map *isl_basic_map_partial_lexmax( |
| 6054 | struct isl_basic_map *bmap, struct isl_basic_set *dom, |
| 6055 | struct isl_set **empty) |
| 6056 | { |
| 6057 | return isl_basic_map_partial_lexopt(bmap, dom, empty, 1); |
| 6058 | } |
| 6059 | |
| 6060 | struct isl_map *isl_basic_map_partial_lexmin( |
| 6061 | struct isl_basic_map *bmap, struct isl_basic_set *dom, |
| 6062 | struct isl_set **empty) |
| 6063 | { |
| 6064 | return isl_basic_map_partial_lexopt(bmap, dom, empty, 0); |
| 6065 | } |
| 6066 | |
| 6067 | struct isl_set *isl_basic_set_partial_lexmin( |
| 6068 | struct isl_basic_set *bset, struct isl_basic_set *dom, |
| 6069 | struct isl_set **empty) |
| 6070 | { |
| 6071 | return (struct isl_set *) |
| 6072 | isl_basic_map_partial_lexmin((struct isl_basic_map *)bset, |
| 6073 | dom, empty); |
| 6074 | } |
| 6075 | |
| 6076 | struct isl_set *isl_basic_set_partial_lexmax( |
| 6077 | struct isl_basic_set *bset, struct isl_basic_set *dom, |
| 6078 | struct isl_set **empty) |
| 6079 | { |
| 6080 | return (struct isl_set *) |
| 6081 | isl_basic_map_partial_lexmax((struct isl_basic_map *)bset, |
| 6082 | dom, empty); |
| 6083 | } |
| 6084 | |
| 6085 | __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmin_pw_multi_aff( |
| 6086 | __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom, |
| 6087 | __isl_give isl_set **empty) |
| 6088 | { |
| 6089 | return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 0); |
| 6090 | } |
| 6091 | |
| 6092 | __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmax_pw_multi_aff( |
| 6093 | __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom, |
| 6094 | __isl_give isl_set **empty) |
| 6095 | { |
| 6096 | return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 1); |
| 6097 | } |
| 6098 | |
| 6099 | __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmin_pw_multi_aff( |
| 6100 | __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom, |
| 6101 | __isl_give isl_set **empty) |
| 6102 | { |
| 6103 | return isl_basic_map_partial_lexmin_pw_multi_aff(bset, dom, empty); |
| 6104 | } |
| 6105 | |
| 6106 | __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmax_pw_multi_aff( |
| 6107 | __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom, |
| 6108 | __isl_give isl_set **empty) |
| 6109 | { |
| 6110 | return isl_basic_map_partial_lexmax_pw_multi_aff(bset, dom, empty); |
| 6111 | } |
| 6112 | |
| 6113 | __isl_give isl_pw_multi_aff *isl_basic_map_lexopt_pw_multi_aff( |
| 6114 | __isl_take isl_basic_map *bmap, int max) |
| 6115 | { |
| 6116 | isl_basic_set *dom = NULL; |
| 6117 | isl_space *dom_space; |
| 6118 | |
| 6119 | if (!bmap) |
| 6120 | goto error; |
| 6121 | dom_space = isl_space_domain(isl_space_copy(bmap->dim)); |
| 6122 | dom = isl_basic_set_universe(dom_space); |
| 6123 | return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, NULL, max); |
| 6124 | error: |
| 6125 | isl_basic_map_free(bmap); |
| 6126 | return NULL; |
| 6127 | } |
| 6128 | |
| 6129 | __isl_give isl_pw_multi_aff *isl_basic_map_lexmin_pw_multi_aff( |
| 6130 | __isl_take isl_basic_map *bmap) |
| 6131 | { |
| 6132 | return isl_basic_map_lexopt_pw_multi_aff(bmap, 0); |
| 6133 | } |
| 6134 | |
| 6135 | #undef TYPE |
| 6136 | #define TYPE isl_pw_multi_aff |
| 6137 | #undef SUFFIX |
| 6138 | #define SUFFIX _pw_multi_aff |
| 6139 | #undef EMPTY |
| 6140 | #define EMPTY isl_pw_multi_aff_empty |
| 6141 | #undef ADD |
| 6142 | #define ADD isl_pw_multi_aff_union_add |
| 6143 | #include "isl_map_lexopt_templ.c" |
| 6144 | |
| 6145 | /* Given a map "map", compute the lexicographically minimal |
| 6146 | * (or maximal) image element for each domain element in dom, |
| 6147 | * in the form of an isl_pw_multi_aff. |
Tobias Grosser | 37034db | 2016-03-25 19:38:18 +0000 | [diff] [blame] | 6148 | * If "empty" is not NULL, then set *empty to those elements in dom that |
| 6149 | * do not have an image element. |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 6150 | * |
| 6151 | * We first compute the lexicographically minimal or maximal element |
| 6152 | * in the first basic map. This results in a partial solution "res" |
| 6153 | * and a subset "todo" of dom that still need to be handled. |
| 6154 | * We then consider each of the remaining maps in "map" and successively |
| 6155 | * update both "res" and "todo". |
Tobias Grosser | 37034db | 2016-03-25 19:38:18 +0000 | [diff] [blame] | 6156 | * If "empty" is NULL, then the todo sets are not needed and therefore |
| 6157 | * also not computed. |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 6158 | */ |
| 6159 | static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff( |
| 6160 | __isl_take isl_map *map, __isl_take isl_set *dom, |
| 6161 | __isl_give isl_set **empty, int max) |
| 6162 | { |
| 6163 | int i; |
| 6164 | isl_pw_multi_aff *res; |
| 6165 | isl_set *todo; |
| 6166 | |
| 6167 | if (!map || !dom) |
| 6168 | goto error; |
| 6169 | |
| 6170 | if (isl_map_plain_is_empty(map)) { |
| 6171 | if (empty) |
| 6172 | *empty = dom; |
| 6173 | else |
| 6174 | isl_set_free(dom); |
| 6175 | return isl_pw_multi_aff_from_map(map); |
| 6176 | } |
| 6177 | |
| 6178 | res = basic_map_partial_lexopt_pw_multi_aff( |
| 6179 | isl_basic_map_copy(map->p[0]), |
Tobias Grosser | 37034db | 2016-03-25 19:38:18 +0000 | [diff] [blame] | 6180 | isl_set_copy(dom), empty, max); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 6181 | |
Tobias Grosser | 37034db | 2016-03-25 19:38:18 +0000 | [diff] [blame] | 6182 | if (empty) |
| 6183 | todo = *empty; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 6184 | for (i = 1; i < map->n; ++i) { |
| 6185 | isl_pw_multi_aff *res_i; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 6186 | |
| 6187 | res_i = basic_map_partial_lexopt_pw_multi_aff( |
| 6188 | isl_basic_map_copy(map->p[i]), |
Tobias Grosser | 37034db | 2016-03-25 19:38:18 +0000 | [diff] [blame] | 6189 | isl_set_copy(dom), empty, max); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 6190 | |
| 6191 | if (max) |
| 6192 | res = isl_pw_multi_aff_union_lexmax(res, res_i); |
| 6193 | else |
| 6194 | res = isl_pw_multi_aff_union_lexmin(res, res_i); |
| 6195 | |
Tobias Grosser | 37034db | 2016-03-25 19:38:18 +0000 | [diff] [blame] | 6196 | if (empty) |
| 6197 | todo = isl_set_intersect(todo, *empty); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 6198 | } |
| 6199 | |
| 6200 | isl_set_free(dom); |
| 6201 | isl_map_free(map); |
| 6202 | |
| 6203 | if (empty) |
| 6204 | *empty = todo; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 6205 | |
| 6206 | return res; |
| 6207 | error: |
| 6208 | if (empty) |
| 6209 | *empty = NULL; |
| 6210 | isl_set_free(dom); |
| 6211 | isl_map_free(map); |
| 6212 | return NULL; |
| 6213 | } |
| 6214 | |
| 6215 | #undef TYPE |
| 6216 | #define TYPE isl_map |
| 6217 | #undef SUFFIX |
| 6218 | #define SUFFIX |
| 6219 | #undef EMPTY |
| 6220 | #define EMPTY isl_map_empty |
| 6221 | #undef ADD |
| 6222 | #define ADD isl_map_union_disjoint |
| 6223 | #include "isl_map_lexopt_templ.c" |
| 6224 | |
| 6225 | /* Given a map "map", compute the lexicographically minimal |
Michael Kruse | e0b34f3 | 2016-05-04 14:41:36 +0000 | [diff] [blame] | 6226 | * (or maximal) image element for each domain element in "dom", |
| 6227 | * in the form of an isl_map. |
| 6228 | * If "empty" is not NULL, then set *empty to those elements in "dom" that |
| 6229 | * do not have an image element. |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 6230 | * |
Michael Kruse | e0b34f3 | 2016-05-04 14:41:36 +0000 | [diff] [blame] | 6231 | * If the input consists of more than one disjunct, then first |
| 6232 | * compute the desired result in the form of an isl_pw_multi_aff and |
| 6233 | * then convert that into an isl_map. |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 6234 | * |
Michael Kruse | e0b34f3 | 2016-05-04 14:41:36 +0000 | [diff] [blame] | 6235 | * This function used to have an explicit implementation in terms |
| 6236 | * of isl_maps, but it would continually intersect the domains of |
| 6237 | * partial results with the complement of the domain of the next |
| 6238 | * partial solution, potentially leading to an explosion in the number |
| 6239 | * of disjuncts if there are several disjuncts in the input. |
| 6240 | * An even earlier implementation of this function would look for |
| 6241 | * better results in the domain of the partial result and for extra |
| 6242 | * results in the complement of this domain, which would lead to |
| 6243 | * even more splintering. |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 6244 | */ |
| 6245 | static __isl_give isl_map *isl_map_partial_lexopt_aligned( |
| 6246 | __isl_take isl_map *map, __isl_take isl_set *dom, |
| 6247 | __isl_give isl_set **empty, int max) |
| 6248 | { |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 6249 | struct isl_map *res; |
Michael Kruse | e0b34f3 | 2016-05-04 14:41:36 +0000 | [diff] [blame] | 6250 | isl_pw_multi_aff *pma; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 6251 | |
| 6252 | if (!map || !dom) |
| 6253 | goto error; |
| 6254 | |
| 6255 | if (isl_map_plain_is_empty(map)) { |
| 6256 | if (empty) |
| 6257 | *empty = dom; |
| 6258 | else |
| 6259 | isl_set_free(dom); |
| 6260 | return map; |
| 6261 | } |
| 6262 | |
Michael Kruse | e0b34f3 | 2016-05-04 14:41:36 +0000 | [diff] [blame] | 6263 | if (map->n == 1) { |
| 6264 | res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]), |
| 6265 | dom, empty, max); |
| 6266 | isl_map_free(map); |
| 6267 | return res; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 6268 | } |
| 6269 | |
Michael Kruse | e0b34f3 | 2016-05-04 14:41:36 +0000 | [diff] [blame] | 6270 | pma = isl_map_partial_lexopt_aligned_pw_multi_aff(map, dom, empty, max); |
| 6271 | return isl_map_from_pw_multi_aff(pma); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 6272 | error: |
| 6273 | if (empty) |
| 6274 | *empty = NULL; |
| 6275 | isl_set_free(dom); |
| 6276 | isl_map_free(map); |
| 6277 | return NULL; |
| 6278 | } |
| 6279 | |
| 6280 | __isl_give isl_map *isl_map_partial_lexmax( |
| 6281 | __isl_take isl_map *map, __isl_take isl_set *dom, |
| 6282 | __isl_give isl_set **empty) |
| 6283 | { |
| 6284 | return isl_map_partial_lexopt(map, dom, empty, 1); |
| 6285 | } |
| 6286 | |
| 6287 | __isl_give isl_map *isl_map_partial_lexmin( |
| 6288 | __isl_take isl_map *map, __isl_take isl_set *dom, |
| 6289 | __isl_give isl_set **empty) |
| 6290 | { |
| 6291 | return isl_map_partial_lexopt(map, dom, empty, 0); |
| 6292 | } |
| 6293 | |
| 6294 | __isl_give isl_set *isl_set_partial_lexmin( |
| 6295 | __isl_take isl_set *set, __isl_take isl_set *dom, |
| 6296 | __isl_give isl_set **empty) |
| 6297 | { |
| 6298 | return (struct isl_set *) |
| 6299 | isl_map_partial_lexmin((struct isl_map *)set, |
| 6300 | dom, empty); |
| 6301 | } |
| 6302 | |
| 6303 | __isl_give isl_set *isl_set_partial_lexmax( |
| 6304 | __isl_take isl_set *set, __isl_take isl_set *dom, |
| 6305 | __isl_give isl_set **empty) |
| 6306 | { |
| 6307 | return (struct isl_set *) |
| 6308 | isl_map_partial_lexmax((struct isl_map *)set, |
| 6309 | dom, empty); |
| 6310 | } |
| 6311 | |
| 6312 | /* Compute the lexicographic minimum (or maximum if "max" is set) |
| 6313 | * of "bmap" over its domain. |
| 6314 | * |
| 6315 | * Since we are not interested in the part of the domain space where |
| 6316 | * there is no solution, we initialize the domain to those constraints |
| 6317 | * of "bmap" that only involve the parameters and the input dimensions. |
| 6318 | * This relieves the parametric programming engine from detecting those |
| 6319 | * inequalities and transferring them to the context. More importantly, |
| 6320 | * it ensures that those inequalities are transferred first and not |
| 6321 | * intermixed with inequalities that actually split the domain. |
| 6322 | */ |
| 6323 | __isl_give isl_map *isl_basic_map_lexopt(__isl_take isl_basic_map *bmap, int max) |
| 6324 | { |
| 6325 | int n_div; |
| 6326 | int n_out; |
| 6327 | isl_basic_map *copy; |
| 6328 | isl_basic_set *dom; |
| 6329 | |
| 6330 | n_div = isl_basic_map_dim(bmap, isl_dim_div); |
| 6331 | n_out = isl_basic_map_dim(bmap, isl_dim_out); |
| 6332 | copy = isl_basic_map_copy(bmap); |
| 6333 | copy = isl_basic_map_drop_constraints_involving_dims(copy, |
| 6334 | isl_dim_div, 0, n_div); |
| 6335 | copy = isl_basic_map_drop_constraints_involving_dims(copy, |
| 6336 | isl_dim_out, 0, n_out); |
| 6337 | dom = isl_basic_map_domain(copy); |
| 6338 | return isl_basic_map_partial_lexopt(bmap, dom, NULL, max); |
| 6339 | } |
| 6340 | |
| 6341 | __isl_give isl_map *isl_basic_map_lexmin(__isl_take isl_basic_map *bmap) |
| 6342 | { |
| 6343 | return isl_basic_map_lexopt(bmap, 0); |
| 6344 | } |
| 6345 | |
| 6346 | __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap) |
| 6347 | { |
| 6348 | return isl_basic_map_lexopt(bmap, 1); |
| 6349 | } |
| 6350 | |
| 6351 | __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset) |
| 6352 | { |
| 6353 | return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset); |
| 6354 | } |
| 6355 | |
| 6356 | __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset) |
| 6357 | { |
| 6358 | return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset); |
| 6359 | } |
| 6360 | |
| 6361 | /* Extract the first and only affine expression from list |
| 6362 | * and then add it to *pwaff with the given dom. |
| 6363 | * This domain is known to be disjoint from other domains |
| 6364 | * because of the way isl_basic_map_foreach_lexmax works. |
| 6365 | */ |
| 6366 | static int update_dim_opt(__isl_take isl_basic_set *dom, |
| 6367 | __isl_take isl_aff_list *list, void *user) |
| 6368 | { |
| 6369 | isl_ctx *ctx = isl_basic_set_get_ctx(dom); |
| 6370 | isl_aff *aff; |
| 6371 | isl_pw_aff **pwaff = user; |
| 6372 | isl_pw_aff *pwaff_i; |
| 6373 | |
| 6374 | if (!list) |
| 6375 | goto error; |
| 6376 | if (isl_aff_list_n_aff(list) != 1) |
| 6377 | isl_die(ctx, isl_error_internal, |
| 6378 | "expecting single element list", goto error); |
| 6379 | |
| 6380 | aff = isl_aff_list_get_aff(list, 0); |
| 6381 | pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff); |
| 6382 | |
| 6383 | *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i); |
| 6384 | |
| 6385 | isl_aff_list_free(list); |
| 6386 | |
| 6387 | return 0; |
| 6388 | error: |
| 6389 | isl_basic_set_free(dom); |
| 6390 | isl_aff_list_free(list); |
| 6391 | return -1; |
| 6392 | } |
| 6393 | |
| 6394 | /* Given a basic map with one output dimension, compute the minimum or |
| 6395 | * maximum of that dimension as an isl_pw_aff. |
| 6396 | * |
| 6397 | * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt |
| 6398 | * call update_dim_opt on each leaf of the result. |
| 6399 | */ |
| 6400 | static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap, |
| 6401 | int max) |
| 6402 | { |
| 6403 | isl_space *dim = isl_basic_map_get_space(bmap); |
| 6404 | isl_pw_aff *pwaff; |
| 6405 | int r; |
| 6406 | |
| 6407 | dim = isl_space_from_domain(isl_space_domain(dim)); |
| 6408 | dim = isl_space_add_dims(dim, isl_dim_out, 1); |
| 6409 | pwaff = isl_pw_aff_empty(dim); |
| 6410 | |
| 6411 | r = isl_basic_map_foreach_lexopt(bmap, max, &update_dim_opt, &pwaff); |
| 6412 | if (r < 0) |
| 6413 | return isl_pw_aff_free(pwaff); |
| 6414 | |
| 6415 | return pwaff; |
| 6416 | } |
| 6417 | |
| 6418 | /* Compute the minimum or maximum of the given output dimension |
| 6419 | * as a function of the parameters and the input dimensions, |
| 6420 | * but independently of the other output dimensions. |
| 6421 | * |
| 6422 | * We first project out the other output dimension and then compute |
| 6423 | * the "lexicographic" maximum in each basic map, combining the results |
| 6424 | * using isl_pw_aff_union_max. |
| 6425 | */ |
| 6426 | static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos, |
| 6427 | int max) |
| 6428 | { |
| 6429 | int i; |
| 6430 | isl_pw_aff *pwaff; |
| 6431 | unsigned n_out; |
| 6432 | |
| 6433 | n_out = isl_map_dim(map, isl_dim_out); |
| 6434 | map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1)); |
| 6435 | map = isl_map_project_out(map, isl_dim_out, 0, pos); |
| 6436 | if (!map) |
| 6437 | return NULL; |
| 6438 | |
| 6439 | if (map->n == 0) { |
| 6440 | isl_space *dim = isl_map_get_space(map); |
| 6441 | isl_map_free(map); |
| 6442 | return isl_pw_aff_empty(dim); |
| 6443 | } |
| 6444 | |
| 6445 | pwaff = basic_map_dim_opt(map->p[0], max); |
| 6446 | for (i = 1; i < map->n; ++i) { |
| 6447 | isl_pw_aff *pwaff_i; |
| 6448 | |
| 6449 | pwaff_i = basic_map_dim_opt(map->p[i], max); |
| 6450 | pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max); |
| 6451 | } |
| 6452 | |
| 6453 | isl_map_free(map); |
| 6454 | |
| 6455 | return pwaff; |
| 6456 | } |
| 6457 | |
| 6458 | /* Compute the maximum of the given output dimension as a function of the |
| 6459 | * parameters and input dimensions, but independently of |
| 6460 | * the other output dimensions. |
| 6461 | */ |
| 6462 | __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos) |
| 6463 | { |
| 6464 | return map_dim_opt(map, pos, 1); |
| 6465 | } |
| 6466 | |
| 6467 | /* Compute the minimum or maximum of the given set dimension |
| 6468 | * as a function of the parameters, |
| 6469 | * but independently of the other set dimensions. |
| 6470 | */ |
| 6471 | static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos, |
| 6472 | int max) |
| 6473 | { |
| 6474 | return map_dim_opt(set, pos, max); |
| 6475 | } |
| 6476 | |
| 6477 | /* Compute the maximum of the given set dimension as a function of the |
| 6478 | * parameters, but independently of the other set dimensions. |
| 6479 | */ |
| 6480 | __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos) |
| 6481 | { |
| 6482 | return set_dim_opt(set, pos, 1); |
| 6483 | } |
| 6484 | |
| 6485 | /* Compute the minimum of the given set dimension as a function of the |
| 6486 | * parameters, but independently of the other set dimensions. |
| 6487 | */ |
| 6488 | __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos) |
| 6489 | { |
| 6490 | return set_dim_opt(set, pos, 0); |
| 6491 | } |
| 6492 | |
| 6493 | /* Apply a preimage specified by "mat" on the parameters of "bset". |
| 6494 | * bset is assumed to have only parameters and divs. |
| 6495 | */ |
| 6496 | static struct isl_basic_set *basic_set_parameter_preimage( |
| 6497 | struct isl_basic_set *bset, struct isl_mat *mat) |
| 6498 | { |
| 6499 | unsigned nparam; |
| 6500 | |
| 6501 | if (!bset || !mat) |
| 6502 | goto error; |
| 6503 | |
| 6504 | bset->dim = isl_space_cow(bset->dim); |
| 6505 | if (!bset->dim) |
| 6506 | goto error; |
| 6507 | |
| 6508 | nparam = isl_basic_set_dim(bset, isl_dim_param); |
| 6509 | |
| 6510 | isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error); |
| 6511 | |
| 6512 | bset->dim->nparam = 0; |
| 6513 | bset->dim->n_out = nparam; |
| 6514 | bset = isl_basic_set_preimage(bset, mat); |
| 6515 | if (bset) { |
| 6516 | bset->dim->nparam = bset->dim->n_out; |
| 6517 | bset->dim->n_out = 0; |
| 6518 | } |
| 6519 | return bset; |
| 6520 | error: |
| 6521 | isl_mat_free(mat); |
| 6522 | isl_basic_set_free(bset); |
| 6523 | return NULL; |
| 6524 | } |
| 6525 | |
| 6526 | /* Apply a preimage specified by "mat" on the parameters of "set". |
| 6527 | * set is assumed to have only parameters and divs. |
| 6528 | */ |
Tobias Grosser | 37034db | 2016-03-25 19:38:18 +0000 | [diff] [blame] | 6529 | static __isl_give isl_set *set_parameter_preimage(__isl_take isl_set *set, |
| 6530 | __isl_take isl_mat *mat) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 6531 | { |
Tobias Grosser | 37034db | 2016-03-25 19:38:18 +0000 | [diff] [blame] | 6532 | isl_space *space; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 6533 | unsigned nparam; |
| 6534 | |
| 6535 | if (!set || !mat) |
| 6536 | goto error; |
| 6537 | |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 6538 | nparam = isl_set_dim(set, isl_dim_param); |
| 6539 | |
Tobias Grosser | 37034db | 2016-03-25 19:38:18 +0000 | [diff] [blame] | 6540 | if (mat->n_row != 1 + nparam) |
| 6541 | isl_die(isl_set_get_ctx(set), isl_error_internal, |
| 6542 | "unexpected number of rows", goto error); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 6543 | |
Tobias Grosser | 37034db | 2016-03-25 19:38:18 +0000 | [diff] [blame] | 6544 | space = isl_set_get_space(set); |
| 6545 | space = isl_space_move_dims(space, isl_dim_set, 0, |
| 6546 | isl_dim_param, 0, nparam); |
| 6547 | set = isl_set_reset_space(set, space); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 6548 | set = isl_set_preimage(set, mat); |
Tobias Grosser | 37034db | 2016-03-25 19:38:18 +0000 | [diff] [blame] | 6549 | nparam = isl_set_dim(set, isl_dim_out); |
| 6550 | space = isl_set_get_space(set); |
| 6551 | space = isl_space_move_dims(space, isl_dim_param, 0, |
| 6552 | isl_dim_out, 0, nparam); |
| 6553 | set = isl_set_reset_space(set, space); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 6554 | return set; |
| 6555 | error: |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 6556 | isl_mat_free(mat); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 6557 | isl_set_free(set); |
| 6558 | return NULL; |
| 6559 | } |
| 6560 | |
| 6561 | /* Intersect the basic set "bset" with the affine space specified by the |
| 6562 | * equalities in "eq". |
| 6563 | */ |
| 6564 | static struct isl_basic_set *basic_set_append_equalities( |
| 6565 | struct isl_basic_set *bset, struct isl_mat *eq) |
| 6566 | { |
| 6567 | int i, k; |
| 6568 | unsigned len; |
| 6569 | |
| 6570 | if (!bset || !eq) |
| 6571 | goto error; |
| 6572 | |
| 6573 | bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0, |
| 6574 | eq->n_row, 0); |
| 6575 | if (!bset) |
| 6576 | goto error; |
| 6577 | |
| 6578 | len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra; |
| 6579 | for (i = 0; i < eq->n_row; ++i) { |
| 6580 | k = isl_basic_set_alloc_equality(bset); |
| 6581 | if (k < 0) |
| 6582 | goto error; |
| 6583 | isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col); |
| 6584 | isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col); |
| 6585 | } |
| 6586 | isl_mat_free(eq); |
| 6587 | |
| 6588 | bset = isl_basic_set_gauss(bset, NULL); |
| 6589 | bset = isl_basic_set_finalize(bset); |
| 6590 | |
| 6591 | return bset; |
| 6592 | error: |
| 6593 | isl_mat_free(eq); |
| 6594 | isl_basic_set_free(bset); |
| 6595 | return NULL; |
| 6596 | } |
| 6597 | |
| 6598 | /* Intersect the set "set" with the affine space specified by the |
| 6599 | * equalities in "eq". |
| 6600 | */ |
| 6601 | static struct isl_set *set_append_equalities(struct isl_set *set, |
| 6602 | struct isl_mat *eq) |
| 6603 | { |
| 6604 | int i; |
| 6605 | |
| 6606 | if (!set || !eq) |
| 6607 | goto error; |
| 6608 | |
| 6609 | for (i = 0; i < set->n; ++i) { |
| 6610 | set->p[i] = basic_set_append_equalities(set->p[i], |
| 6611 | isl_mat_copy(eq)); |
| 6612 | if (!set->p[i]) |
| 6613 | goto error; |
| 6614 | } |
| 6615 | isl_mat_free(eq); |
| 6616 | return set; |
| 6617 | error: |
| 6618 | isl_mat_free(eq); |
| 6619 | isl_set_free(set); |
| 6620 | return NULL; |
| 6621 | } |
| 6622 | |
| 6623 | /* Given a basic set "bset" that only involves parameters and existentially |
| 6624 | * quantified variables, return the index of the first equality |
| 6625 | * that only involves parameters. If there is no such equality then |
| 6626 | * return bset->n_eq. |
| 6627 | * |
| 6628 | * This function assumes that isl_basic_set_gauss has been called on "bset". |
| 6629 | */ |
| 6630 | static int first_parameter_equality(__isl_keep isl_basic_set *bset) |
| 6631 | { |
| 6632 | int i, j; |
| 6633 | unsigned nparam, n_div; |
| 6634 | |
| 6635 | if (!bset) |
| 6636 | return -1; |
| 6637 | |
| 6638 | nparam = isl_basic_set_dim(bset, isl_dim_param); |
| 6639 | n_div = isl_basic_set_dim(bset, isl_dim_div); |
| 6640 | |
| 6641 | for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) { |
| 6642 | if (!isl_int_is_zero(bset->eq[i][1 + nparam + j])) |
| 6643 | ++i; |
| 6644 | } |
| 6645 | |
| 6646 | return i; |
| 6647 | } |
| 6648 | |
| 6649 | /* Compute an explicit representation for the existentially quantified |
| 6650 | * variables in "bset" by computing the "minimal value" of the set |
| 6651 | * variables. Since there are no set variables, the computation of |
| 6652 | * the minimal value essentially computes an explicit representation |
| 6653 | * of the non-empty part(s) of "bset". |
| 6654 | * |
| 6655 | * The input only involves parameters and existentially quantified variables. |
| 6656 | * All equalities among parameters have been removed. |
| 6657 | * |
| 6658 | * Since the existentially quantified variables in the result are in general |
| 6659 | * going to be different from those in the input, we first replace |
| 6660 | * them by the minimal number of variables based on their equalities. |
| 6661 | * This should simplify the parametric integer programming. |
| 6662 | */ |
| 6663 | static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset) |
| 6664 | { |
| 6665 | isl_morph *morph1, *morph2; |
| 6666 | isl_set *set; |
| 6667 | unsigned n; |
| 6668 | |
| 6669 | if (!bset) |
| 6670 | return NULL; |
| 6671 | if (bset->n_eq == 0) |
| 6672 | return isl_basic_set_lexmin(bset); |
| 6673 | |
| 6674 | morph1 = isl_basic_set_parameter_compression(bset); |
| 6675 | bset = isl_morph_basic_set(isl_morph_copy(morph1), bset); |
| 6676 | bset = isl_basic_set_lift(bset); |
| 6677 | morph2 = isl_basic_set_variable_compression(bset, isl_dim_set); |
| 6678 | bset = isl_morph_basic_set(morph2, bset); |
| 6679 | n = isl_basic_set_dim(bset, isl_dim_set); |
| 6680 | bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n); |
| 6681 | |
| 6682 | set = isl_basic_set_lexmin(bset); |
| 6683 | |
| 6684 | set = isl_morph_set(isl_morph_inverse(morph1), set); |
| 6685 | |
| 6686 | return set; |
| 6687 | } |
| 6688 | |
| 6689 | /* Project the given basic set onto its parameter domain, possibly introducing |
| 6690 | * new, explicit, existential variables in the constraints. |
| 6691 | * The input has parameters and (possibly implicit) existential variables. |
| 6692 | * The output has the same parameters, but only |
| 6693 | * explicit existentially quantified variables. |
| 6694 | * |
| 6695 | * The actual projection is performed by pip, but pip doesn't seem |
| 6696 | * to like equalities very much, so we first remove the equalities |
| 6697 | * among the parameters by performing a variable compression on |
| 6698 | * the parameters. Afterward, an inverse transformation is performed |
| 6699 | * and the equalities among the parameters are inserted back in. |
| 6700 | * |
| 6701 | * The variable compression on the parameters may uncover additional |
| 6702 | * equalities that were only implicit before. We therefore check |
| 6703 | * if there are any new parameter equalities in the result and |
| 6704 | * if so recurse. The removal of parameter equalities is required |
| 6705 | * for the parameter compression performed by base_compute_divs. |
| 6706 | */ |
| 6707 | static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset) |
| 6708 | { |
| 6709 | int i; |
| 6710 | struct isl_mat *eq; |
| 6711 | struct isl_mat *T, *T2; |
| 6712 | struct isl_set *set; |
| 6713 | unsigned nparam; |
| 6714 | |
| 6715 | bset = isl_basic_set_cow(bset); |
| 6716 | if (!bset) |
| 6717 | return NULL; |
| 6718 | |
| 6719 | if (bset->n_eq == 0) |
| 6720 | return base_compute_divs(bset); |
| 6721 | |
| 6722 | bset = isl_basic_set_gauss(bset, NULL); |
| 6723 | if (!bset) |
| 6724 | return NULL; |
| 6725 | if (isl_basic_set_plain_is_empty(bset)) |
| 6726 | return isl_set_from_basic_set(bset); |
| 6727 | |
| 6728 | i = first_parameter_equality(bset); |
| 6729 | if (i == bset->n_eq) |
| 6730 | return base_compute_divs(bset); |
| 6731 | |
| 6732 | nparam = isl_basic_set_dim(bset, isl_dim_param); |
| 6733 | eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i, |
| 6734 | 0, 1 + nparam); |
| 6735 | eq = isl_mat_cow(eq); |
| 6736 | T = isl_mat_variable_compression(isl_mat_copy(eq), &T2); |
| 6737 | if (T && T->n_col == 0) { |
| 6738 | isl_mat_free(T); |
| 6739 | isl_mat_free(T2); |
| 6740 | isl_mat_free(eq); |
| 6741 | bset = isl_basic_set_set_to_empty(bset); |
| 6742 | return isl_set_from_basic_set(bset); |
| 6743 | } |
| 6744 | bset = basic_set_parameter_preimage(bset, T); |
| 6745 | |
| 6746 | i = first_parameter_equality(bset); |
| 6747 | if (!bset) |
| 6748 | set = NULL; |
| 6749 | else if (i == bset->n_eq) |
| 6750 | set = base_compute_divs(bset); |
| 6751 | else |
| 6752 | set = parameter_compute_divs(bset); |
| 6753 | set = set_parameter_preimage(set, T2); |
| 6754 | set = set_append_equalities(set, eq); |
| 6755 | return set; |
| 6756 | } |
| 6757 | |
| 6758 | /* Insert the divs from "ls" before those of "bmap". |
| 6759 | * |
| 6760 | * The number of columns is not changed, which means that the last |
| 6761 | * dimensions of "bmap" are being reintepreted as the divs from "ls". |
| 6762 | * The caller is responsible for removing the same number of dimensions |
| 6763 | * from the space of "bmap". |
| 6764 | */ |
| 6765 | static __isl_give isl_basic_map *insert_divs_from_local_space( |
| 6766 | __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls) |
| 6767 | { |
| 6768 | int i; |
| 6769 | int n_div; |
| 6770 | int old_n_div; |
| 6771 | |
| 6772 | n_div = isl_local_space_dim(ls, isl_dim_div); |
| 6773 | if (n_div == 0) |
| 6774 | return bmap; |
| 6775 | |
| 6776 | old_n_div = bmap->n_div; |
| 6777 | bmap = insert_div_rows(bmap, n_div); |
| 6778 | if (!bmap) |
| 6779 | return NULL; |
| 6780 | |
| 6781 | for (i = 0; i < n_div; ++i) { |
| 6782 | isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col); |
| 6783 | isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div); |
| 6784 | } |
| 6785 | |
| 6786 | return bmap; |
| 6787 | } |
| 6788 | |
| 6789 | /* Replace the space of "bmap" by the space and divs of "ls". |
| 6790 | * |
| 6791 | * If "ls" has any divs, then we simplify the result since we may |
| 6792 | * have discovered some additional equalities that could simplify |
| 6793 | * the div expressions. |
| 6794 | */ |
| 6795 | static __isl_give isl_basic_map *basic_replace_space_by_local_space( |
| 6796 | __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls) |
| 6797 | { |
| 6798 | int n_div; |
| 6799 | |
| 6800 | bmap = isl_basic_map_cow(bmap); |
| 6801 | if (!bmap || !ls) |
| 6802 | goto error; |
| 6803 | |
| 6804 | n_div = isl_local_space_dim(ls, isl_dim_div); |
| 6805 | bmap = insert_divs_from_local_space(bmap, ls); |
| 6806 | if (!bmap) |
| 6807 | goto error; |
| 6808 | |
| 6809 | isl_space_free(bmap->dim); |
| 6810 | bmap->dim = isl_local_space_get_space(ls); |
| 6811 | if (!bmap->dim) |
| 6812 | goto error; |
| 6813 | |
| 6814 | isl_local_space_free(ls); |
| 6815 | if (n_div > 0) |
| 6816 | bmap = isl_basic_map_simplify(bmap); |
| 6817 | bmap = isl_basic_map_finalize(bmap); |
| 6818 | return bmap; |
| 6819 | error: |
| 6820 | isl_basic_map_free(bmap); |
| 6821 | isl_local_space_free(ls); |
| 6822 | return NULL; |
| 6823 | } |
| 6824 | |
| 6825 | /* Replace the space of "map" by the space and divs of "ls". |
| 6826 | */ |
| 6827 | static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map, |
| 6828 | __isl_take isl_local_space *ls) |
| 6829 | { |
| 6830 | int i; |
| 6831 | |
| 6832 | map = isl_map_cow(map); |
| 6833 | if (!map || !ls) |
| 6834 | goto error; |
| 6835 | |
| 6836 | for (i = 0; i < map->n; ++i) { |
| 6837 | map->p[i] = basic_replace_space_by_local_space(map->p[i], |
| 6838 | isl_local_space_copy(ls)); |
| 6839 | if (!map->p[i]) |
| 6840 | goto error; |
| 6841 | } |
| 6842 | isl_space_free(map->dim); |
| 6843 | map->dim = isl_local_space_get_space(ls); |
| 6844 | if (!map->dim) |
| 6845 | goto error; |
| 6846 | |
| 6847 | isl_local_space_free(ls); |
| 6848 | return map; |
| 6849 | error: |
| 6850 | isl_local_space_free(ls); |
| 6851 | isl_map_free(map); |
| 6852 | return NULL; |
| 6853 | } |
| 6854 | |
| 6855 | /* Compute an explicit representation for the existentially |
| 6856 | * quantified variables for which do not know any explicit representation yet. |
| 6857 | * |
| 6858 | * We first sort the existentially quantified variables so that the |
| 6859 | * existentially quantified variables for which we already have an explicit |
| 6860 | * representation are placed before those for which we do not. |
| 6861 | * The input dimensions, the output dimensions and the existentially |
| 6862 | * quantified variables for which we already have an explicit |
| 6863 | * representation are then turned into parameters. |
| 6864 | * compute_divs returns a map with the same parameters and |
| 6865 | * no input or output dimensions and the dimension specification |
| 6866 | * is reset to that of the input, including the existentially quantified |
| 6867 | * variables for which we already had an explicit representation. |
| 6868 | */ |
| 6869 | static struct isl_map *compute_divs(struct isl_basic_map *bmap) |
| 6870 | { |
| 6871 | struct isl_basic_set *bset; |
| 6872 | struct isl_set *set; |
| 6873 | struct isl_map *map; |
| 6874 | isl_space *dim; |
| 6875 | isl_local_space *ls; |
| 6876 | unsigned nparam; |
| 6877 | unsigned n_in; |
| 6878 | unsigned n_out; |
Tobias Grosser | 37034db | 2016-03-25 19:38:18 +0000 | [diff] [blame] | 6879 | int n_known; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 6880 | int i; |
| 6881 | |
| 6882 | bmap = isl_basic_map_sort_divs(bmap); |
| 6883 | bmap = isl_basic_map_cow(bmap); |
| 6884 | if (!bmap) |
| 6885 | return NULL; |
| 6886 | |
Tobias Grosser | 37034db | 2016-03-25 19:38:18 +0000 | [diff] [blame] | 6887 | n_known = isl_basic_map_first_unknown_div(bmap); |
| 6888 | if (n_known < 0) |
| 6889 | return isl_map_from_basic_map(isl_basic_map_free(bmap)); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 6890 | |
| 6891 | nparam = isl_basic_map_dim(bmap, isl_dim_param); |
| 6892 | n_in = isl_basic_map_dim(bmap, isl_dim_in); |
| 6893 | n_out = isl_basic_map_dim(bmap, isl_dim_out); |
| 6894 | dim = isl_space_set_alloc(bmap->ctx, |
| 6895 | nparam + n_in + n_out + n_known, 0); |
| 6896 | if (!dim) |
| 6897 | goto error; |
| 6898 | |
| 6899 | ls = isl_basic_map_get_local_space(bmap); |
| 6900 | ls = isl_local_space_drop_dims(ls, isl_dim_div, |
| 6901 | n_known, bmap->n_div - n_known); |
| 6902 | if (n_known > 0) { |
| 6903 | for (i = n_known; i < bmap->n_div; ++i) |
| 6904 | swap_div(bmap, i - n_known, i); |
| 6905 | bmap->n_div -= n_known; |
| 6906 | bmap->extra -= n_known; |
| 6907 | } |
| 6908 | bmap = isl_basic_map_reset_space(bmap, dim); |
| 6909 | bset = (struct isl_basic_set *)bmap; |
| 6910 | |
| 6911 | set = parameter_compute_divs(bset); |
| 6912 | map = (struct isl_map *)set; |
| 6913 | map = replace_space_by_local_space(map, ls); |
| 6914 | |
| 6915 | return map; |
| 6916 | error: |
| 6917 | isl_basic_map_free(bmap); |
| 6918 | return NULL; |
| 6919 | } |
| 6920 | |
Tobias Grosser | 37034db | 2016-03-25 19:38:18 +0000 | [diff] [blame] | 6921 | /* Remove the explicit representation of local variable "div", |
| 6922 | * if there is any. |
| 6923 | */ |
| 6924 | __isl_give isl_basic_map *isl_basic_map_mark_div_unknown( |
| 6925 | __isl_take isl_basic_map *bmap, int div) |
| 6926 | { |
| 6927 | isl_bool known; |
| 6928 | |
| 6929 | known = isl_basic_map_div_is_known(bmap, div); |
| 6930 | if (known < 0) |
| 6931 | return isl_basic_map_free(bmap); |
| 6932 | if (!known) |
| 6933 | return bmap; |
| 6934 | |
| 6935 | bmap = isl_basic_map_cow(bmap); |
| 6936 | if (!bmap) |
| 6937 | return NULL; |
| 6938 | isl_int_set_si(bmap->div[div][0], 0); |
| 6939 | return bmap; |
| 6940 | } |
| 6941 | |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 6942 | /* Does local variable "div" of "bmap" have an explicit representation? |
| 6943 | */ |
| 6944 | isl_bool isl_basic_map_div_is_known(__isl_keep isl_basic_map *bmap, int div) |
| 6945 | { |
| 6946 | if (!bmap) |
| 6947 | return isl_bool_error; |
| 6948 | if (div < 0 || div >= isl_basic_map_dim(bmap, isl_dim_div)) |
| 6949 | isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid, |
| 6950 | "position out of bounds", return isl_bool_error); |
| 6951 | return !isl_int_is_zero(bmap->div[div][0]); |
| 6952 | } |
| 6953 | |
Tobias Grosser | 37034db | 2016-03-25 19:38:18 +0000 | [diff] [blame] | 6954 | /* Return the position of the first local variable that does not |
| 6955 | * have an explicit representation. |
| 6956 | * Return the total number of local variables if they all have |
| 6957 | * an explicit representation. |
| 6958 | * Return -1 on error. |
| 6959 | */ |
| 6960 | int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map *bmap) |
| 6961 | { |
| 6962 | int i; |
| 6963 | |
| 6964 | if (!bmap) |
| 6965 | return -1; |
| 6966 | |
| 6967 | for (i = 0; i < bmap->n_div; ++i) { |
| 6968 | if (!isl_basic_map_div_is_known(bmap, i)) |
| 6969 | return i; |
| 6970 | } |
| 6971 | return bmap->n_div; |
| 6972 | } |
| 6973 | |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 6974 | /* Does "bmap" have an explicit representation for all local variables? |
| 6975 | */ |
| 6976 | isl_bool isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 6977 | { |
Tobias Grosser | 37034db | 2016-03-25 19:38:18 +0000 | [diff] [blame] | 6978 | int first, n; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 6979 | |
Tobias Grosser | 37034db | 2016-03-25 19:38:18 +0000 | [diff] [blame] | 6980 | n = isl_basic_map_dim(bmap, isl_dim_div); |
| 6981 | first = isl_basic_map_first_unknown_div(bmap); |
| 6982 | if (first < 0) |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 6983 | return isl_bool_error; |
Tobias Grosser | 37034db | 2016-03-25 19:38:18 +0000 | [diff] [blame] | 6984 | return first == n; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 6985 | } |
| 6986 | |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 6987 | /* Do all basic maps in "map" have an explicit representation |
| 6988 | * for all local variables? |
| 6989 | */ |
| 6990 | isl_bool isl_map_divs_known(__isl_keep isl_map *map) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 6991 | { |
| 6992 | int i; |
| 6993 | |
| 6994 | if (!map) |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 6995 | return isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 6996 | |
| 6997 | for (i = 0; i < map->n; ++i) { |
| 6998 | int known = isl_basic_map_divs_known(map->p[i]); |
| 6999 | if (known <= 0) |
| 7000 | return known; |
| 7001 | } |
| 7002 | |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 7003 | return isl_bool_true; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7004 | } |
| 7005 | |
| 7006 | /* If bmap contains any unknown divs, then compute explicit |
| 7007 | * expressions for them. However, this computation may be |
| 7008 | * quite expensive, so first try to remove divs that aren't |
| 7009 | * strictly needed. |
| 7010 | */ |
| 7011 | struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap) |
| 7012 | { |
| 7013 | int known; |
| 7014 | struct isl_map *map; |
| 7015 | |
| 7016 | known = isl_basic_map_divs_known(bmap); |
| 7017 | if (known < 0) |
| 7018 | goto error; |
| 7019 | if (known) |
| 7020 | return isl_map_from_basic_map(bmap); |
| 7021 | |
| 7022 | bmap = isl_basic_map_drop_redundant_divs(bmap); |
| 7023 | |
| 7024 | known = isl_basic_map_divs_known(bmap); |
| 7025 | if (known < 0) |
| 7026 | goto error; |
| 7027 | if (known) |
| 7028 | return isl_map_from_basic_map(bmap); |
| 7029 | |
| 7030 | map = compute_divs(bmap); |
| 7031 | return map; |
| 7032 | error: |
| 7033 | isl_basic_map_free(bmap); |
| 7034 | return NULL; |
| 7035 | } |
| 7036 | |
| 7037 | struct isl_map *isl_map_compute_divs(struct isl_map *map) |
| 7038 | { |
| 7039 | int i; |
| 7040 | int known; |
| 7041 | struct isl_map *res; |
| 7042 | |
| 7043 | if (!map) |
| 7044 | return NULL; |
| 7045 | if (map->n == 0) |
| 7046 | return map; |
| 7047 | |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 7048 | known = isl_map_divs_known(map); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7049 | if (known < 0) { |
| 7050 | isl_map_free(map); |
| 7051 | return NULL; |
| 7052 | } |
| 7053 | if (known) |
| 7054 | return map; |
| 7055 | |
| 7056 | res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0])); |
| 7057 | for (i = 1 ; i < map->n; ++i) { |
| 7058 | struct isl_map *r2; |
| 7059 | r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i])); |
| 7060 | if (ISL_F_ISSET(map, ISL_MAP_DISJOINT)) |
| 7061 | res = isl_map_union_disjoint(res, r2); |
| 7062 | else |
| 7063 | res = isl_map_union(res, r2); |
| 7064 | } |
| 7065 | isl_map_free(map); |
| 7066 | |
| 7067 | return res; |
| 7068 | } |
| 7069 | |
| 7070 | struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset) |
| 7071 | { |
| 7072 | return (struct isl_set *) |
| 7073 | isl_basic_map_compute_divs((struct isl_basic_map *)bset); |
| 7074 | } |
| 7075 | |
| 7076 | struct isl_set *isl_set_compute_divs(struct isl_set *set) |
| 7077 | { |
| 7078 | return (struct isl_set *) |
| 7079 | isl_map_compute_divs((struct isl_map *)set); |
| 7080 | } |
| 7081 | |
| 7082 | struct isl_set *isl_map_domain(struct isl_map *map) |
| 7083 | { |
| 7084 | int i; |
| 7085 | struct isl_set *set; |
| 7086 | |
| 7087 | if (!map) |
| 7088 | goto error; |
| 7089 | |
| 7090 | map = isl_map_cow(map); |
| 7091 | if (!map) |
| 7092 | return NULL; |
| 7093 | |
| 7094 | set = (struct isl_set *)map; |
| 7095 | set->dim = isl_space_domain(set->dim); |
| 7096 | if (!set->dim) |
| 7097 | goto error; |
| 7098 | for (i = 0; i < map->n; ++i) { |
| 7099 | set->p[i] = isl_basic_map_domain(map->p[i]); |
| 7100 | if (!set->p[i]) |
| 7101 | goto error; |
| 7102 | } |
| 7103 | ISL_F_CLR(set, ISL_MAP_DISJOINT); |
| 7104 | ISL_F_CLR(set, ISL_SET_NORMALIZED); |
| 7105 | return set; |
| 7106 | error: |
| 7107 | isl_map_free(map); |
| 7108 | return NULL; |
| 7109 | } |
| 7110 | |
| 7111 | /* Return the union of "map1" and "map2", where we assume for now that |
| 7112 | * "map1" and "map2" are disjoint. Note that the basic maps inside |
| 7113 | * "map1" or "map2" may not be disjoint from each other. |
| 7114 | * Also note that this function is also called from isl_map_union, |
| 7115 | * which takes care of handling the situation where "map1" and "map2" |
| 7116 | * may not be disjoint. |
| 7117 | * |
| 7118 | * If one of the inputs is empty, we can simply return the other input. |
| 7119 | * Similarly, if one of the inputs is universal, then it is equal to the union. |
| 7120 | */ |
| 7121 | static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1, |
| 7122 | __isl_take isl_map *map2) |
| 7123 | { |
| 7124 | int i; |
| 7125 | unsigned flags = 0; |
| 7126 | struct isl_map *map = NULL; |
| 7127 | int is_universe; |
| 7128 | |
| 7129 | if (!map1 || !map2) |
| 7130 | goto error; |
| 7131 | |
| 7132 | if (!isl_space_is_equal(map1->dim, map2->dim)) |
| 7133 | isl_die(isl_map_get_ctx(map1), isl_error_invalid, |
| 7134 | "spaces don't match", goto error); |
| 7135 | |
| 7136 | if (map1->n == 0) { |
| 7137 | isl_map_free(map1); |
| 7138 | return map2; |
| 7139 | } |
| 7140 | if (map2->n == 0) { |
| 7141 | isl_map_free(map2); |
| 7142 | return map1; |
| 7143 | } |
| 7144 | |
| 7145 | is_universe = isl_map_plain_is_universe(map1); |
| 7146 | if (is_universe < 0) |
| 7147 | goto error; |
| 7148 | if (is_universe) { |
| 7149 | isl_map_free(map2); |
| 7150 | return map1; |
| 7151 | } |
| 7152 | |
| 7153 | is_universe = isl_map_plain_is_universe(map2); |
| 7154 | if (is_universe < 0) |
| 7155 | goto error; |
| 7156 | if (is_universe) { |
| 7157 | isl_map_free(map1); |
| 7158 | return map2; |
| 7159 | } |
| 7160 | |
| 7161 | if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) && |
| 7162 | ISL_F_ISSET(map2, ISL_MAP_DISJOINT)) |
| 7163 | ISL_FL_SET(flags, ISL_MAP_DISJOINT); |
| 7164 | |
| 7165 | map = isl_map_alloc_space(isl_space_copy(map1->dim), |
| 7166 | map1->n + map2->n, flags); |
| 7167 | if (!map) |
| 7168 | goto error; |
| 7169 | for (i = 0; i < map1->n; ++i) { |
| 7170 | map = isl_map_add_basic_map(map, |
| 7171 | isl_basic_map_copy(map1->p[i])); |
| 7172 | if (!map) |
| 7173 | goto error; |
| 7174 | } |
| 7175 | for (i = 0; i < map2->n; ++i) { |
| 7176 | map = isl_map_add_basic_map(map, |
| 7177 | isl_basic_map_copy(map2->p[i])); |
| 7178 | if (!map) |
| 7179 | goto error; |
| 7180 | } |
| 7181 | isl_map_free(map1); |
| 7182 | isl_map_free(map2); |
| 7183 | return map; |
| 7184 | error: |
| 7185 | isl_map_free(map); |
| 7186 | isl_map_free(map1); |
| 7187 | isl_map_free(map2); |
| 7188 | return NULL; |
| 7189 | } |
| 7190 | |
| 7191 | /* Return the union of "map1" and "map2", where "map1" and "map2" are |
| 7192 | * guaranteed to be disjoint by the caller. |
| 7193 | * |
| 7194 | * Note that this functions is called from within isl_map_make_disjoint, |
| 7195 | * so we have to be careful not to touch the constraints of the inputs |
| 7196 | * in any way. |
| 7197 | */ |
| 7198 | __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1, |
| 7199 | __isl_take isl_map *map2) |
| 7200 | { |
| 7201 | return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint); |
| 7202 | } |
| 7203 | |
| 7204 | /* Return the union of "map1" and "map2", where "map1" and "map2" may |
| 7205 | * not be disjoint. The parameters are assumed to have been aligned. |
| 7206 | * |
| 7207 | * We currently simply call map_union_disjoint, the internal operation |
| 7208 | * of which does not really depend on the inputs being disjoint. |
| 7209 | * If the result contains more than one basic map, then we clear |
| 7210 | * the disjoint flag since the result may contain basic maps from |
| 7211 | * both inputs and these are not guaranteed to be disjoint. |
| 7212 | * |
| 7213 | * As a special case, if "map1" and "map2" are obviously equal, |
| 7214 | * then we simply return "map1". |
| 7215 | */ |
| 7216 | static __isl_give isl_map *map_union_aligned(__isl_take isl_map *map1, |
| 7217 | __isl_take isl_map *map2) |
| 7218 | { |
| 7219 | int equal; |
| 7220 | |
| 7221 | if (!map1 || !map2) |
| 7222 | goto error; |
| 7223 | |
| 7224 | equal = isl_map_plain_is_equal(map1, map2); |
| 7225 | if (equal < 0) |
| 7226 | goto error; |
| 7227 | if (equal) { |
| 7228 | isl_map_free(map2); |
| 7229 | return map1; |
| 7230 | } |
| 7231 | |
| 7232 | map1 = map_union_disjoint(map1, map2); |
| 7233 | if (!map1) |
| 7234 | return NULL; |
| 7235 | if (map1->n > 1) |
| 7236 | ISL_F_CLR(map1, ISL_MAP_DISJOINT); |
| 7237 | return map1; |
| 7238 | error: |
| 7239 | isl_map_free(map1); |
| 7240 | isl_map_free(map2); |
| 7241 | return NULL; |
| 7242 | } |
| 7243 | |
| 7244 | /* Return the union of "map1" and "map2", where "map1" and "map2" may |
| 7245 | * not be disjoint. |
| 7246 | */ |
| 7247 | __isl_give isl_map *isl_map_union(__isl_take isl_map *map1, |
| 7248 | __isl_take isl_map *map2) |
| 7249 | { |
| 7250 | return isl_map_align_params_map_map_and(map1, map2, &map_union_aligned); |
| 7251 | } |
| 7252 | |
| 7253 | struct isl_set *isl_set_union_disjoint( |
| 7254 | struct isl_set *set1, struct isl_set *set2) |
| 7255 | { |
| 7256 | return (struct isl_set *) |
| 7257 | isl_map_union_disjoint( |
| 7258 | (struct isl_map *)set1, (struct isl_map *)set2); |
| 7259 | } |
| 7260 | |
| 7261 | struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2) |
| 7262 | { |
| 7263 | return (struct isl_set *) |
| 7264 | isl_map_union((struct isl_map *)set1, (struct isl_map *)set2); |
| 7265 | } |
| 7266 | |
| 7267 | /* Apply "fn" to pairs of elements from "map" and "set" and collect |
| 7268 | * the results. |
| 7269 | * |
| 7270 | * "map" and "set" are assumed to be compatible and non-NULL. |
| 7271 | */ |
| 7272 | static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map, |
| 7273 | __isl_take isl_set *set, |
| 7274 | __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap, |
| 7275 | __isl_take isl_basic_set *bset)) |
| 7276 | { |
| 7277 | unsigned flags = 0; |
| 7278 | struct isl_map *result; |
| 7279 | int i, j; |
| 7280 | |
| 7281 | if (isl_set_plain_is_universe(set)) { |
| 7282 | isl_set_free(set); |
| 7283 | return map; |
| 7284 | } |
| 7285 | |
| 7286 | if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) && |
| 7287 | ISL_F_ISSET(set, ISL_MAP_DISJOINT)) |
| 7288 | ISL_FL_SET(flags, ISL_MAP_DISJOINT); |
| 7289 | |
| 7290 | result = isl_map_alloc_space(isl_space_copy(map->dim), |
| 7291 | map->n * set->n, flags); |
| 7292 | for (i = 0; result && i < map->n; ++i) |
| 7293 | for (j = 0; j < set->n; ++j) { |
| 7294 | result = isl_map_add_basic_map(result, |
| 7295 | fn(isl_basic_map_copy(map->p[i]), |
| 7296 | isl_basic_set_copy(set->p[j]))); |
| 7297 | if (!result) |
| 7298 | break; |
| 7299 | } |
| 7300 | |
| 7301 | isl_map_free(map); |
| 7302 | isl_set_free(set); |
| 7303 | return result; |
| 7304 | } |
| 7305 | |
| 7306 | static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map, |
| 7307 | __isl_take isl_set *set) |
| 7308 | { |
| 7309 | if (!map || !set) |
| 7310 | goto error; |
| 7311 | |
| 7312 | if (!isl_map_compatible_range(map, set)) |
| 7313 | isl_die(set->ctx, isl_error_invalid, |
| 7314 | "incompatible spaces", goto error); |
| 7315 | |
| 7316 | return map_intersect_set(map, set, &isl_basic_map_intersect_range); |
| 7317 | error: |
| 7318 | isl_map_free(map); |
| 7319 | isl_set_free(set); |
| 7320 | return NULL; |
| 7321 | } |
| 7322 | |
| 7323 | __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map, |
| 7324 | __isl_take isl_set *set) |
| 7325 | { |
| 7326 | return isl_map_align_params_map_map_and(map, set, &map_intersect_range); |
| 7327 | } |
| 7328 | |
| 7329 | static __isl_give isl_map *map_intersect_domain(__isl_take isl_map *map, |
| 7330 | __isl_take isl_set *set) |
| 7331 | { |
| 7332 | if (!map || !set) |
| 7333 | goto error; |
| 7334 | |
| 7335 | if (!isl_map_compatible_domain(map, set)) |
| 7336 | isl_die(set->ctx, isl_error_invalid, |
| 7337 | "incompatible spaces", goto error); |
| 7338 | |
| 7339 | return map_intersect_set(map, set, &isl_basic_map_intersect_domain); |
| 7340 | error: |
| 7341 | isl_map_free(map); |
| 7342 | isl_set_free(set); |
| 7343 | return NULL; |
| 7344 | } |
| 7345 | |
| 7346 | __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map, |
| 7347 | __isl_take isl_set *set) |
| 7348 | { |
| 7349 | return isl_map_align_params_map_map_and(map, set, |
| 7350 | &map_intersect_domain); |
| 7351 | } |
| 7352 | |
| 7353 | static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1, |
| 7354 | __isl_take isl_map *map2) |
| 7355 | { |
| 7356 | if (!map1 || !map2) |
| 7357 | goto error; |
| 7358 | map1 = isl_map_reverse(map1); |
| 7359 | map1 = isl_map_apply_range(map1, map2); |
| 7360 | return isl_map_reverse(map1); |
| 7361 | error: |
| 7362 | isl_map_free(map1); |
| 7363 | isl_map_free(map2); |
| 7364 | return NULL; |
| 7365 | } |
| 7366 | |
| 7367 | __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1, |
| 7368 | __isl_take isl_map *map2) |
| 7369 | { |
| 7370 | return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain); |
| 7371 | } |
| 7372 | |
| 7373 | static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1, |
| 7374 | __isl_take isl_map *map2) |
| 7375 | { |
| 7376 | isl_space *dim_result; |
| 7377 | struct isl_map *result; |
| 7378 | int i, j; |
| 7379 | |
| 7380 | if (!map1 || !map2) |
| 7381 | goto error; |
| 7382 | |
| 7383 | dim_result = isl_space_join(isl_space_copy(map1->dim), |
| 7384 | isl_space_copy(map2->dim)); |
| 7385 | |
| 7386 | result = isl_map_alloc_space(dim_result, map1->n * map2->n, 0); |
| 7387 | if (!result) |
| 7388 | goto error; |
| 7389 | for (i = 0; i < map1->n; ++i) |
| 7390 | for (j = 0; j < map2->n; ++j) { |
| 7391 | result = isl_map_add_basic_map(result, |
| 7392 | isl_basic_map_apply_range( |
| 7393 | isl_basic_map_copy(map1->p[i]), |
| 7394 | isl_basic_map_copy(map2->p[j]))); |
| 7395 | if (!result) |
| 7396 | goto error; |
| 7397 | } |
| 7398 | isl_map_free(map1); |
| 7399 | isl_map_free(map2); |
| 7400 | if (result && result->n <= 1) |
| 7401 | ISL_F_SET(result, ISL_MAP_DISJOINT); |
| 7402 | return result; |
| 7403 | error: |
| 7404 | isl_map_free(map1); |
| 7405 | isl_map_free(map2); |
| 7406 | return NULL; |
| 7407 | } |
| 7408 | |
| 7409 | __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1, |
| 7410 | __isl_take isl_map *map2) |
| 7411 | { |
| 7412 | return isl_map_align_params_map_map_and(map1, map2, &map_apply_range); |
| 7413 | } |
| 7414 | |
| 7415 | /* |
| 7416 | * returns range - domain |
| 7417 | */ |
| 7418 | struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap) |
| 7419 | { |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 7420 | isl_space *target_space; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7421 | struct isl_basic_set *bset; |
| 7422 | unsigned dim; |
| 7423 | unsigned nparam; |
| 7424 | int i; |
| 7425 | |
| 7426 | if (!bmap) |
| 7427 | goto error; |
| 7428 | isl_assert(bmap->ctx, isl_space_tuple_is_equal(bmap->dim, isl_dim_in, |
| 7429 | bmap->dim, isl_dim_out), |
| 7430 | goto error); |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 7431 | target_space = isl_space_domain(isl_basic_map_get_space(bmap)); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7432 | dim = isl_basic_map_n_in(bmap); |
| 7433 | nparam = isl_basic_map_n_param(bmap); |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 7434 | bmap = isl_basic_map_from_range(isl_basic_map_wrap(bmap)); |
| 7435 | bmap = isl_basic_map_add_dims(bmap, isl_dim_in, dim); |
| 7436 | bmap = isl_basic_map_extend_constraints(bmap, dim, 0); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7437 | for (i = 0; i < dim; ++i) { |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 7438 | int j = isl_basic_map_alloc_equality(bmap); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7439 | if (j < 0) { |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 7440 | bmap = isl_basic_map_free(bmap); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7441 | break; |
| 7442 | } |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 7443 | isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap)); |
| 7444 | isl_int_set_si(bmap->eq[j][1+nparam+i], 1); |
| 7445 | isl_int_set_si(bmap->eq[j][1+nparam+dim+i], 1); |
| 7446 | isl_int_set_si(bmap->eq[j][1+nparam+2*dim+i], -1); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7447 | } |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 7448 | bset = isl_basic_map_domain(bmap); |
| 7449 | bset = isl_basic_set_reset_space(bset, target_space); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7450 | return bset; |
| 7451 | error: |
| 7452 | isl_basic_map_free(bmap); |
| 7453 | return NULL; |
| 7454 | } |
| 7455 | |
| 7456 | /* |
| 7457 | * returns range - domain |
| 7458 | */ |
| 7459 | __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map) |
| 7460 | { |
| 7461 | int i; |
| 7462 | isl_space *dim; |
| 7463 | struct isl_set *result; |
| 7464 | |
| 7465 | if (!map) |
| 7466 | return NULL; |
| 7467 | |
| 7468 | isl_assert(map->ctx, isl_space_tuple_is_equal(map->dim, isl_dim_in, |
| 7469 | map->dim, isl_dim_out), |
| 7470 | goto error); |
| 7471 | dim = isl_map_get_space(map); |
| 7472 | dim = isl_space_domain(dim); |
| 7473 | result = isl_set_alloc_space(dim, map->n, 0); |
| 7474 | if (!result) |
| 7475 | goto error; |
| 7476 | for (i = 0; i < map->n; ++i) |
| 7477 | result = isl_set_add_basic_set(result, |
| 7478 | isl_basic_map_deltas(isl_basic_map_copy(map->p[i]))); |
| 7479 | isl_map_free(map); |
| 7480 | return result; |
| 7481 | error: |
| 7482 | isl_map_free(map); |
| 7483 | return NULL; |
| 7484 | } |
| 7485 | |
| 7486 | /* |
| 7487 | * returns [domain -> range] -> range - domain |
| 7488 | */ |
| 7489 | __isl_give isl_basic_map *isl_basic_map_deltas_map( |
| 7490 | __isl_take isl_basic_map *bmap) |
| 7491 | { |
| 7492 | int i, k; |
| 7493 | isl_space *dim; |
| 7494 | isl_basic_map *domain; |
| 7495 | int nparam, n; |
| 7496 | unsigned total; |
| 7497 | |
| 7498 | if (!isl_space_tuple_is_equal(bmap->dim, isl_dim_in, |
| 7499 | bmap->dim, isl_dim_out)) |
| 7500 | isl_die(bmap->ctx, isl_error_invalid, |
| 7501 | "domain and range don't match", goto error); |
| 7502 | |
| 7503 | nparam = isl_basic_map_dim(bmap, isl_dim_param); |
| 7504 | n = isl_basic_map_dim(bmap, isl_dim_in); |
| 7505 | |
| 7506 | dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap))); |
| 7507 | domain = isl_basic_map_universe(dim); |
| 7508 | |
| 7509 | bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap)); |
| 7510 | bmap = isl_basic_map_apply_range(bmap, domain); |
| 7511 | bmap = isl_basic_map_extend_constraints(bmap, n, 0); |
| 7512 | |
| 7513 | total = isl_basic_map_total_dim(bmap); |
| 7514 | |
| 7515 | for (i = 0; i < n; ++i) { |
| 7516 | k = isl_basic_map_alloc_equality(bmap); |
| 7517 | if (k < 0) |
| 7518 | goto error; |
| 7519 | isl_seq_clr(bmap->eq[k], 1 + total); |
| 7520 | isl_int_set_si(bmap->eq[k][1 + nparam + i], 1); |
| 7521 | isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1); |
| 7522 | isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1); |
| 7523 | } |
| 7524 | |
| 7525 | bmap = isl_basic_map_gauss(bmap, NULL); |
| 7526 | return isl_basic_map_finalize(bmap); |
| 7527 | error: |
| 7528 | isl_basic_map_free(bmap); |
| 7529 | return NULL; |
| 7530 | } |
| 7531 | |
| 7532 | /* |
| 7533 | * returns [domain -> range] -> range - domain |
| 7534 | */ |
| 7535 | __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map) |
| 7536 | { |
| 7537 | int i; |
| 7538 | isl_space *domain_dim; |
| 7539 | |
| 7540 | if (!map) |
| 7541 | return NULL; |
| 7542 | |
| 7543 | if (!isl_space_tuple_is_equal(map->dim, isl_dim_in, |
| 7544 | map->dim, isl_dim_out)) |
| 7545 | isl_die(map->ctx, isl_error_invalid, |
| 7546 | "domain and range don't match", goto error); |
| 7547 | |
| 7548 | map = isl_map_cow(map); |
| 7549 | if (!map) |
| 7550 | return NULL; |
| 7551 | |
| 7552 | domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map))); |
| 7553 | map->dim = isl_space_from_domain(isl_space_wrap(map->dim)); |
| 7554 | map->dim = isl_space_join(map->dim, domain_dim); |
| 7555 | if (!map->dim) |
| 7556 | goto error; |
| 7557 | for (i = 0; i < map->n; ++i) { |
| 7558 | map->p[i] = isl_basic_map_deltas_map(map->p[i]); |
| 7559 | if (!map->p[i]) |
| 7560 | goto error; |
| 7561 | } |
| 7562 | ISL_F_CLR(map, ISL_MAP_NORMALIZED); |
| 7563 | return map; |
| 7564 | error: |
| 7565 | isl_map_free(map); |
| 7566 | return NULL; |
| 7567 | } |
| 7568 | |
| 7569 | static __isl_give isl_basic_map *basic_map_identity(__isl_take isl_space *dims) |
| 7570 | { |
| 7571 | struct isl_basic_map *bmap; |
| 7572 | unsigned nparam; |
| 7573 | unsigned dim; |
| 7574 | int i; |
| 7575 | |
| 7576 | if (!dims) |
| 7577 | return NULL; |
| 7578 | |
| 7579 | nparam = dims->nparam; |
| 7580 | dim = dims->n_out; |
| 7581 | bmap = isl_basic_map_alloc_space(dims, 0, dim, 0); |
| 7582 | if (!bmap) |
| 7583 | goto error; |
| 7584 | |
| 7585 | for (i = 0; i < dim; ++i) { |
| 7586 | int j = isl_basic_map_alloc_equality(bmap); |
| 7587 | if (j < 0) |
| 7588 | goto error; |
| 7589 | isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap)); |
| 7590 | isl_int_set_si(bmap->eq[j][1+nparam+i], 1); |
| 7591 | isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1); |
| 7592 | } |
| 7593 | return isl_basic_map_finalize(bmap); |
| 7594 | error: |
| 7595 | isl_basic_map_free(bmap); |
| 7596 | return NULL; |
| 7597 | } |
| 7598 | |
| 7599 | __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *dim) |
| 7600 | { |
| 7601 | if (!dim) |
| 7602 | return NULL; |
| 7603 | if (dim->n_in != dim->n_out) |
| 7604 | isl_die(dim->ctx, isl_error_invalid, |
| 7605 | "number of input and output dimensions needs to be " |
| 7606 | "the same", goto error); |
| 7607 | return basic_map_identity(dim); |
| 7608 | error: |
| 7609 | isl_space_free(dim); |
| 7610 | return NULL; |
| 7611 | } |
| 7612 | |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7613 | __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim) |
| 7614 | { |
| 7615 | return isl_map_from_basic_map(isl_basic_map_identity(dim)); |
| 7616 | } |
| 7617 | |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7618 | __isl_give isl_map *isl_set_identity(__isl_take isl_set *set) |
| 7619 | { |
| 7620 | isl_space *dim = isl_set_get_space(set); |
| 7621 | isl_map *id; |
| 7622 | id = isl_map_identity(isl_space_map_from_set(dim)); |
| 7623 | return isl_map_intersect_range(id, set); |
| 7624 | } |
| 7625 | |
| 7626 | /* Construct a basic set with all set dimensions having only non-negative |
| 7627 | * values. |
| 7628 | */ |
| 7629 | __isl_give isl_basic_set *isl_basic_set_positive_orthant( |
| 7630 | __isl_take isl_space *space) |
| 7631 | { |
| 7632 | int i; |
| 7633 | unsigned nparam; |
| 7634 | unsigned dim; |
| 7635 | struct isl_basic_set *bset; |
| 7636 | |
| 7637 | if (!space) |
| 7638 | return NULL; |
| 7639 | nparam = space->nparam; |
| 7640 | dim = space->n_out; |
| 7641 | bset = isl_basic_set_alloc_space(space, 0, 0, dim); |
| 7642 | if (!bset) |
| 7643 | return NULL; |
| 7644 | for (i = 0; i < dim; ++i) { |
| 7645 | int k = isl_basic_set_alloc_inequality(bset); |
| 7646 | if (k < 0) |
| 7647 | goto error; |
| 7648 | isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset)); |
| 7649 | isl_int_set_si(bset->ineq[k][1 + nparam + i], 1); |
| 7650 | } |
| 7651 | return bset; |
| 7652 | error: |
| 7653 | isl_basic_set_free(bset); |
| 7654 | return NULL; |
| 7655 | } |
| 7656 | |
| 7657 | /* Construct the half-space x_pos >= 0. |
| 7658 | */ |
| 7659 | static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *dim, |
| 7660 | int pos) |
| 7661 | { |
| 7662 | int k; |
| 7663 | isl_basic_set *nonneg; |
| 7664 | |
| 7665 | nonneg = isl_basic_set_alloc_space(dim, 0, 0, 1); |
| 7666 | k = isl_basic_set_alloc_inequality(nonneg); |
| 7667 | if (k < 0) |
| 7668 | goto error; |
| 7669 | isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg)); |
| 7670 | isl_int_set_si(nonneg->ineq[k][pos], 1); |
| 7671 | |
| 7672 | return isl_basic_set_finalize(nonneg); |
| 7673 | error: |
| 7674 | isl_basic_set_free(nonneg); |
| 7675 | return NULL; |
| 7676 | } |
| 7677 | |
| 7678 | /* Construct the half-space x_pos <= -1. |
| 7679 | */ |
| 7680 | static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *dim, int pos) |
| 7681 | { |
| 7682 | int k; |
| 7683 | isl_basic_set *neg; |
| 7684 | |
| 7685 | neg = isl_basic_set_alloc_space(dim, 0, 0, 1); |
| 7686 | k = isl_basic_set_alloc_inequality(neg); |
| 7687 | if (k < 0) |
| 7688 | goto error; |
| 7689 | isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg)); |
| 7690 | isl_int_set_si(neg->ineq[k][0], -1); |
| 7691 | isl_int_set_si(neg->ineq[k][pos], -1); |
| 7692 | |
| 7693 | return isl_basic_set_finalize(neg); |
| 7694 | error: |
| 7695 | isl_basic_set_free(neg); |
| 7696 | return NULL; |
| 7697 | } |
| 7698 | |
| 7699 | __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set, |
| 7700 | enum isl_dim_type type, unsigned first, unsigned n) |
| 7701 | { |
| 7702 | int i; |
Tobias Grosser | e0f8d59 | 2015-05-13 13:10:13 +0000 | [diff] [blame] | 7703 | unsigned offset; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7704 | isl_basic_set *nonneg; |
| 7705 | isl_basic_set *neg; |
| 7706 | |
| 7707 | if (!set) |
| 7708 | return NULL; |
| 7709 | if (n == 0) |
| 7710 | return set; |
| 7711 | |
| 7712 | isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error); |
| 7713 | |
Tobias Grosser | e0f8d59 | 2015-05-13 13:10:13 +0000 | [diff] [blame] | 7714 | offset = pos(set->dim, type); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7715 | for (i = 0; i < n; ++i) { |
| 7716 | nonneg = nonneg_halfspace(isl_set_get_space(set), |
Tobias Grosser | e0f8d59 | 2015-05-13 13:10:13 +0000 | [diff] [blame] | 7717 | offset + first + i); |
| 7718 | neg = neg_halfspace(isl_set_get_space(set), offset + first + i); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7719 | |
| 7720 | set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg)); |
| 7721 | } |
| 7722 | |
| 7723 | return set; |
| 7724 | error: |
| 7725 | isl_set_free(set); |
| 7726 | return NULL; |
| 7727 | } |
| 7728 | |
| 7729 | static int foreach_orthant(__isl_take isl_set *set, int *signs, int first, |
| 7730 | int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user), |
| 7731 | void *user) |
| 7732 | { |
| 7733 | isl_set *half; |
| 7734 | |
| 7735 | if (!set) |
| 7736 | return -1; |
| 7737 | if (isl_set_plain_is_empty(set)) { |
| 7738 | isl_set_free(set); |
| 7739 | return 0; |
| 7740 | } |
| 7741 | if (first == len) |
| 7742 | return fn(set, signs, user); |
| 7743 | |
| 7744 | signs[first] = 1; |
| 7745 | half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set), |
| 7746 | 1 + first)); |
| 7747 | half = isl_set_intersect(half, isl_set_copy(set)); |
| 7748 | if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0) |
| 7749 | goto error; |
| 7750 | |
| 7751 | signs[first] = -1; |
| 7752 | half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set), |
| 7753 | 1 + first)); |
| 7754 | half = isl_set_intersect(half, set); |
| 7755 | return foreach_orthant(half, signs, first + 1, len, fn, user); |
| 7756 | error: |
| 7757 | isl_set_free(set); |
| 7758 | return -1; |
| 7759 | } |
| 7760 | |
| 7761 | /* Call "fn" on the intersections of "set" with each of the orthants |
| 7762 | * (except for obviously empty intersections). The orthant is identified |
| 7763 | * by the signs array, with each entry having value 1 or -1 according |
| 7764 | * to the sign of the corresponding variable. |
| 7765 | */ |
| 7766 | int isl_set_foreach_orthant(__isl_keep isl_set *set, |
| 7767 | int (*fn)(__isl_take isl_set *orthant, int *signs, void *user), |
| 7768 | void *user) |
| 7769 | { |
| 7770 | unsigned nparam; |
| 7771 | unsigned nvar; |
| 7772 | int *signs; |
| 7773 | int r; |
| 7774 | |
| 7775 | if (!set) |
| 7776 | return -1; |
| 7777 | if (isl_set_plain_is_empty(set)) |
| 7778 | return 0; |
| 7779 | |
| 7780 | nparam = isl_set_dim(set, isl_dim_param); |
| 7781 | nvar = isl_set_dim(set, isl_dim_set); |
| 7782 | |
| 7783 | signs = isl_alloc_array(set->ctx, int, nparam + nvar); |
| 7784 | |
| 7785 | r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar, |
| 7786 | fn, user); |
| 7787 | |
| 7788 | free(signs); |
| 7789 | |
| 7790 | return r; |
| 7791 | } |
| 7792 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 7793 | isl_bool isl_set_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7794 | { |
| 7795 | return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2); |
| 7796 | } |
| 7797 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 7798 | isl_bool isl_basic_map_is_subset(__isl_keep isl_basic_map *bmap1, |
| 7799 | __isl_keep isl_basic_map *bmap2) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7800 | { |
| 7801 | int is_subset; |
| 7802 | struct isl_map *map1; |
| 7803 | struct isl_map *map2; |
| 7804 | |
| 7805 | if (!bmap1 || !bmap2) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 7806 | return isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7807 | |
| 7808 | map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1)); |
| 7809 | map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2)); |
| 7810 | |
| 7811 | is_subset = isl_map_is_subset(map1, map2); |
| 7812 | |
| 7813 | isl_map_free(map1); |
| 7814 | isl_map_free(map2); |
| 7815 | |
| 7816 | return is_subset; |
| 7817 | } |
| 7818 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 7819 | isl_bool isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1, |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7820 | __isl_keep isl_basic_set *bset2) |
| 7821 | { |
| 7822 | return isl_basic_map_is_subset(bset1, bset2); |
| 7823 | } |
| 7824 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 7825 | isl_bool isl_basic_map_is_equal(__isl_keep isl_basic_map *bmap1, |
| 7826 | __isl_keep isl_basic_map *bmap2) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7827 | { |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 7828 | isl_bool is_subset; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7829 | |
| 7830 | if (!bmap1 || !bmap2) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 7831 | return isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7832 | is_subset = isl_basic_map_is_subset(bmap1, bmap2); |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 7833 | if (is_subset != isl_bool_true) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7834 | return is_subset; |
| 7835 | is_subset = isl_basic_map_is_subset(bmap2, bmap1); |
| 7836 | return is_subset; |
| 7837 | } |
| 7838 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 7839 | isl_bool isl_basic_set_is_equal(__isl_keep isl_basic_set *bset1, |
| 7840 | __isl_keep isl_basic_set *bset2) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7841 | { |
| 7842 | return isl_basic_map_is_equal( |
| 7843 | (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2); |
| 7844 | } |
| 7845 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 7846 | isl_bool isl_map_is_empty(__isl_keep isl_map *map) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7847 | { |
| 7848 | int i; |
| 7849 | int is_empty; |
| 7850 | |
| 7851 | if (!map) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 7852 | return isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7853 | for (i = 0; i < map->n; ++i) { |
| 7854 | is_empty = isl_basic_map_is_empty(map->p[i]); |
| 7855 | if (is_empty < 0) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 7856 | return isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7857 | if (!is_empty) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 7858 | return isl_bool_false; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7859 | } |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 7860 | return isl_bool_true; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7861 | } |
| 7862 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 7863 | isl_bool isl_map_plain_is_empty(__isl_keep isl_map *map) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7864 | { |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 7865 | return map ? map->n == 0 : isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7866 | } |
| 7867 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 7868 | isl_bool isl_set_plain_is_empty(__isl_keep isl_set *set) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7869 | { |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 7870 | return set ? set->n == 0 : isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7871 | } |
| 7872 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 7873 | isl_bool isl_set_is_empty(__isl_keep isl_set *set) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7874 | { |
| 7875 | return isl_map_is_empty((struct isl_map *)set); |
| 7876 | } |
| 7877 | |
| 7878 | int isl_map_has_equal_space(__isl_keep isl_map *map1, __isl_keep isl_map *map2) |
| 7879 | { |
| 7880 | if (!map1 || !map2) |
| 7881 | return -1; |
| 7882 | |
| 7883 | return isl_space_is_equal(map1->dim, map2->dim); |
| 7884 | } |
| 7885 | |
| 7886 | int isl_set_has_equal_space(__isl_keep isl_set *set1, __isl_keep isl_set *set2) |
| 7887 | { |
| 7888 | if (!set1 || !set2) |
| 7889 | return -1; |
| 7890 | |
| 7891 | return isl_space_is_equal(set1->dim, set2->dim); |
| 7892 | } |
| 7893 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 7894 | static isl_bool map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7895 | { |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 7896 | isl_bool is_subset; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7897 | |
| 7898 | if (!map1 || !map2) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 7899 | return isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7900 | is_subset = isl_map_is_subset(map1, map2); |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 7901 | if (is_subset != isl_bool_true) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7902 | return is_subset; |
| 7903 | is_subset = isl_map_is_subset(map2, map1); |
| 7904 | return is_subset; |
| 7905 | } |
| 7906 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 7907 | isl_bool isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7908 | { |
| 7909 | return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal); |
| 7910 | } |
| 7911 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 7912 | isl_bool isl_basic_map_is_strict_subset( |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7913 | struct isl_basic_map *bmap1, struct isl_basic_map *bmap2) |
| 7914 | { |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 7915 | isl_bool is_subset; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7916 | |
| 7917 | if (!bmap1 || !bmap2) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 7918 | return isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7919 | is_subset = isl_basic_map_is_subset(bmap1, bmap2); |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 7920 | if (is_subset != isl_bool_true) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7921 | return is_subset; |
| 7922 | is_subset = isl_basic_map_is_subset(bmap2, bmap1); |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 7923 | if (is_subset == isl_bool_error) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7924 | return is_subset; |
| 7925 | return !is_subset; |
| 7926 | } |
| 7927 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 7928 | isl_bool isl_map_is_strict_subset(__isl_keep isl_map *map1, |
| 7929 | __isl_keep isl_map *map2) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7930 | { |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 7931 | isl_bool is_subset; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7932 | |
| 7933 | if (!map1 || !map2) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 7934 | return isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7935 | is_subset = isl_map_is_subset(map1, map2); |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 7936 | if (is_subset != isl_bool_true) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7937 | return is_subset; |
| 7938 | is_subset = isl_map_is_subset(map2, map1); |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 7939 | if (is_subset == isl_bool_error) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7940 | return is_subset; |
| 7941 | return !is_subset; |
| 7942 | } |
| 7943 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 7944 | isl_bool isl_set_is_strict_subset(__isl_keep isl_set *set1, |
| 7945 | __isl_keep isl_set *set2) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7946 | { |
| 7947 | return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2); |
| 7948 | } |
| 7949 | |
Tobias Grosser | a67ac97 | 2016-02-26 11:35:12 +0000 | [diff] [blame] | 7950 | /* Is "bmap" obviously equal to the universe with the same space? |
| 7951 | * |
| 7952 | * That is, does it not have any constraints? |
| 7953 | */ |
| 7954 | isl_bool isl_basic_map_plain_is_universe(__isl_keep isl_basic_map *bmap) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7955 | { |
| 7956 | if (!bmap) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 7957 | return isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7958 | return bmap->n_eq == 0 && bmap->n_ineq == 0; |
| 7959 | } |
| 7960 | |
Tobias Grosser | a67ac97 | 2016-02-26 11:35:12 +0000 | [diff] [blame] | 7961 | /* Is "bset" obviously equal to the universe with the same space? |
| 7962 | */ |
| 7963 | isl_bool isl_basic_set_plain_is_universe(__isl_keep isl_basic_set *bset) |
| 7964 | { |
| 7965 | return isl_basic_map_plain_is_universe(bset); |
| 7966 | } |
| 7967 | |
| 7968 | /* If "c" does not involve any existentially quantified variables, |
| 7969 | * then set *univ to false and abort |
| 7970 | */ |
| 7971 | static isl_stat involves_divs(__isl_take isl_constraint *c, void *user) |
| 7972 | { |
| 7973 | isl_bool *univ = user; |
| 7974 | unsigned n; |
| 7975 | |
| 7976 | n = isl_constraint_dim(c, isl_dim_div); |
| 7977 | *univ = isl_constraint_involves_dims(c, isl_dim_div, 0, n); |
| 7978 | isl_constraint_free(c); |
| 7979 | if (*univ < 0 || !*univ) |
| 7980 | return isl_stat_error; |
| 7981 | return isl_stat_ok; |
| 7982 | } |
| 7983 | |
| 7984 | /* Is "bmap" equal to the universe with the same space? |
| 7985 | * |
| 7986 | * First check if it is obviously equal to the universe. |
| 7987 | * If not and if there are any constraints not involving |
| 7988 | * existentially quantified variables, then it is certainly |
| 7989 | * not equal to the universe. |
| 7990 | * Otherwise, check if the universe is a subset of "bmap". |
| 7991 | */ |
| 7992 | isl_bool isl_basic_map_is_universe(__isl_keep isl_basic_map *bmap) |
| 7993 | { |
| 7994 | isl_bool univ; |
| 7995 | isl_basic_map *test; |
| 7996 | |
| 7997 | univ = isl_basic_map_plain_is_universe(bmap); |
| 7998 | if (univ < 0 || univ) |
| 7999 | return univ; |
| 8000 | if (isl_basic_map_dim(bmap, isl_dim_div) == 0) |
| 8001 | return isl_bool_false; |
| 8002 | univ = isl_bool_true; |
| 8003 | if (isl_basic_map_foreach_constraint(bmap, &involves_divs, &univ) < 0 && |
| 8004 | univ) |
| 8005 | return isl_bool_error; |
| 8006 | if (univ < 0 || !univ) |
| 8007 | return univ; |
| 8008 | test = isl_basic_map_universe(isl_basic_map_get_space(bmap)); |
| 8009 | univ = isl_basic_map_is_subset(test, bmap); |
| 8010 | isl_basic_map_free(test); |
| 8011 | return univ; |
| 8012 | } |
| 8013 | |
| 8014 | /* Is "bset" equal to the universe with the same space? |
| 8015 | */ |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 8016 | isl_bool isl_basic_set_is_universe(__isl_keep isl_basic_set *bset) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8017 | { |
Tobias Grosser | a67ac97 | 2016-02-26 11:35:12 +0000 | [diff] [blame] | 8018 | return isl_basic_map_is_universe(bset); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8019 | } |
| 8020 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 8021 | isl_bool isl_map_plain_is_universe(__isl_keep isl_map *map) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8022 | { |
| 8023 | int i; |
| 8024 | |
| 8025 | if (!map) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 8026 | return isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8027 | |
| 8028 | for (i = 0; i < map->n; ++i) { |
Tobias Grosser | a67ac97 | 2016-02-26 11:35:12 +0000 | [diff] [blame] | 8029 | isl_bool r = isl_basic_map_plain_is_universe(map->p[i]); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8030 | if (r < 0 || r) |
| 8031 | return r; |
| 8032 | } |
| 8033 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 8034 | return isl_bool_false; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8035 | } |
| 8036 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 8037 | isl_bool isl_set_plain_is_universe(__isl_keep isl_set *set) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8038 | { |
| 8039 | return isl_map_plain_is_universe((isl_map *) set); |
| 8040 | } |
| 8041 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 8042 | isl_bool isl_basic_map_is_empty(__isl_keep isl_basic_map *bmap) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8043 | { |
| 8044 | struct isl_basic_set *bset = NULL; |
| 8045 | struct isl_vec *sample = NULL; |
Tobias Grosser | a67ac97 | 2016-02-26 11:35:12 +0000 | [diff] [blame] | 8046 | isl_bool empty, non_empty; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8047 | |
| 8048 | if (!bmap) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 8049 | return isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8050 | |
| 8051 | if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY)) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 8052 | return isl_bool_true; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8053 | |
Tobias Grosser | a67ac97 | 2016-02-26 11:35:12 +0000 | [diff] [blame] | 8054 | if (isl_basic_map_plain_is_universe(bmap)) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 8055 | return isl_bool_false; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8056 | |
| 8057 | if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) { |
| 8058 | struct isl_basic_map *copy = isl_basic_map_copy(bmap); |
| 8059 | copy = isl_basic_map_remove_redundancies(copy); |
| 8060 | empty = isl_basic_map_plain_is_empty(copy); |
| 8061 | isl_basic_map_free(copy); |
| 8062 | return empty; |
| 8063 | } |
| 8064 | |
Tobias Grosser | a67ac97 | 2016-02-26 11:35:12 +0000 | [diff] [blame] | 8065 | non_empty = isl_basic_map_plain_is_non_empty(bmap); |
| 8066 | if (non_empty < 0) |
| 8067 | return isl_bool_error; |
| 8068 | if (non_empty) |
| 8069 | return isl_bool_false; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8070 | isl_vec_free(bmap->sample); |
| 8071 | bmap->sample = NULL; |
| 8072 | bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap)); |
| 8073 | if (!bset) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 8074 | return isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8075 | sample = isl_basic_set_sample_vec(bset); |
| 8076 | if (!sample) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 8077 | return isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8078 | empty = sample->size == 0; |
| 8079 | isl_vec_free(bmap->sample); |
| 8080 | bmap->sample = sample; |
| 8081 | if (empty) |
| 8082 | ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY); |
| 8083 | |
| 8084 | return empty; |
| 8085 | } |
| 8086 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 8087 | isl_bool isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8088 | { |
| 8089 | if (!bmap) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 8090 | return isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8091 | return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY); |
| 8092 | } |
| 8093 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 8094 | isl_bool isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8095 | { |
| 8096 | if (!bset) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 8097 | return isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8098 | return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY); |
| 8099 | } |
| 8100 | |
Tobias Grosser | a67ac97 | 2016-02-26 11:35:12 +0000 | [diff] [blame] | 8101 | /* Is "bmap" known to be non-empty? |
| 8102 | * |
| 8103 | * That is, is the cached sample still valid? |
| 8104 | */ |
| 8105 | isl_bool isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map *bmap) |
| 8106 | { |
| 8107 | unsigned total; |
| 8108 | |
| 8109 | if (!bmap) |
| 8110 | return isl_bool_error; |
| 8111 | if (!bmap->sample) |
| 8112 | return isl_bool_false; |
| 8113 | total = 1 + isl_basic_map_total_dim(bmap); |
| 8114 | if (bmap->sample->size != total) |
| 8115 | return isl_bool_false; |
| 8116 | return isl_basic_map_contains(bmap, bmap->sample); |
| 8117 | } |
| 8118 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 8119 | isl_bool isl_basic_set_is_empty(__isl_keep isl_basic_set *bset) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8120 | { |
| 8121 | return isl_basic_map_is_empty((struct isl_basic_map *)bset); |
| 8122 | } |
| 8123 | |
| 8124 | struct isl_map *isl_basic_map_union( |
| 8125 | struct isl_basic_map *bmap1, struct isl_basic_map *bmap2) |
| 8126 | { |
| 8127 | struct isl_map *map; |
| 8128 | if (!bmap1 || !bmap2) |
| 8129 | goto error; |
| 8130 | |
| 8131 | isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error); |
| 8132 | |
| 8133 | map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0); |
| 8134 | if (!map) |
| 8135 | goto error; |
| 8136 | map = isl_map_add_basic_map(map, bmap1); |
| 8137 | map = isl_map_add_basic_map(map, bmap2); |
| 8138 | return map; |
| 8139 | error: |
| 8140 | isl_basic_map_free(bmap1); |
| 8141 | isl_basic_map_free(bmap2); |
| 8142 | return NULL; |
| 8143 | } |
| 8144 | |
| 8145 | struct isl_set *isl_basic_set_union( |
| 8146 | struct isl_basic_set *bset1, struct isl_basic_set *bset2) |
| 8147 | { |
| 8148 | return (struct isl_set *)isl_basic_map_union( |
| 8149 | (struct isl_basic_map *)bset1, |
| 8150 | (struct isl_basic_map *)bset2); |
| 8151 | } |
| 8152 | |
| 8153 | /* Order divs such that any div only depends on previous divs */ |
| 8154 | struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap) |
| 8155 | { |
| 8156 | int i; |
| 8157 | unsigned off; |
| 8158 | |
| 8159 | if (!bmap) |
| 8160 | return NULL; |
| 8161 | |
| 8162 | off = isl_space_dim(bmap->dim, isl_dim_all); |
| 8163 | |
| 8164 | for (i = 0; i < bmap->n_div; ++i) { |
| 8165 | int pos; |
| 8166 | if (isl_int_is_zero(bmap->div[i][0])) |
| 8167 | continue; |
| 8168 | pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i, |
| 8169 | bmap->n_div-i); |
| 8170 | if (pos == -1) |
| 8171 | continue; |
Tobias Grosser | 37034db | 2016-03-25 19:38:18 +0000 | [diff] [blame] | 8172 | if (pos == 0) |
| 8173 | isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal, |
| 8174 | "integer division depends on itself", |
| 8175 | return isl_basic_map_free(bmap)); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8176 | isl_basic_map_swap_div(bmap, i, i + pos); |
| 8177 | --i; |
| 8178 | } |
| 8179 | return bmap; |
| 8180 | } |
| 8181 | |
| 8182 | struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset) |
| 8183 | { |
| 8184 | return (struct isl_basic_set *) |
| 8185 | isl_basic_map_order_divs((struct isl_basic_map *)bset); |
| 8186 | } |
| 8187 | |
| 8188 | __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map) |
| 8189 | { |
| 8190 | int i; |
| 8191 | |
| 8192 | if (!map) |
| 8193 | return 0; |
| 8194 | |
| 8195 | for (i = 0; i < map->n; ++i) { |
| 8196 | map->p[i] = isl_basic_map_order_divs(map->p[i]); |
| 8197 | if (!map->p[i]) |
| 8198 | goto error; |
| 8199 | } |
| 8200 | |
| 8201 | return map; |
| 8202 | error: |
| 8203 | isl_map_free(map); |
| 8204 | return NULL; |
| 8205 | } |
| 8206 | |
| 8207 | /* Apply the expansion computed by isl_merge_divs. |
| 8208 | * The expansion itself is given by "exp" while the resulting |
| 8209 | * list of divs is given by "div". |
Tobias Grosser | a67ac97 | 2016-02-26 11:35:12 +0000 | [diff] [blame] | 8210 | * |
Tobias Grosser | 07b2095 | 2016-06-12 04:30:40 +0000 | [diff] [blame^] | 8211 | * Move the integer divisions of "bmap" into the right position |
Tobias Grosser | a67ac97 | 2016-02-26 11:35:12 +0000 | [diff] [blame] | 8212 | * according to "exp" and then introduce the additional integer |
| 8213 | * divisions, adding div constraints. |
| 8214 | * The moving should be done first to avoid moving coefficients |
| 8215 | * in the definitions of the extra integer divisions. |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8216 | */ |
Tobias Grosser | 07b2095 | 2016-06-12 04:30:40 +0000 | [diff] [blame^] | 8217 | __isl_give isl_basic_map *isl_basic_map_expand_divs( |
| 8218 | __isl_take isl_basic_set *bmap, __isl_take isl_mat *div, int *exp) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8219 | { |
| 8220 | int i, j; |
| 8221 | int n_div; |
| 8222 | |
Tobias Grosser | 07b2095 | 2016-06-12 04:30:40 +0000 | [diff] [blame^] | 8223 | bmap = isl_basic_map_cow(bmap); |
| 8224 | if (!bmap || !div) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8225 | goto error; |
| 8226 | |
Tobias Grosser | 07b2095 | 2016-06-12 04:30:40 +0000 | [diff] [blame^] | 8227 | if (div->n_row < bmap->n_div) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8228 | isl_die(isl_mat_get_ctx(div), isl_error_invalid, |
| 8229 | "not an expansion", goto error); |
| 8230 | |
Tobias Grosser | 07b2095 | 2016-06-12 04:30:40 +0000 | [diff] [blame^] | 8231 | n_div = bmap->n_div; |
| 8232 | bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim), |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8233 | div->n_row - n_div, 0, |
| 8234 | 2 * (div->n_row - n_div)); |
| 8235 | |
| 8236 | for (i = n_div; i < div->n_row; ++i) |
Tobias Grosser | 07b2095 | 2016-06-12 04:30:40 +0000 | [diff] [blame^] | 8237 | if (isl_basic_map_alloc_div(bmap) < 0) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8238 | goto error; |
| 8239 | |
Tobias Grosser | a67ac97 | 2016-02-26 11:35:12 +0000 | [diff] [blame] | 8240 | for (j = n_div - 1; j >= 0; --j) { |
| 8241 | if (exp[j] == j) |
| 8242 | break; |
Tobias Grosser | 07b2095 | 2016-06-12 04:30:40 +0000 | [diff] [blame^] | 8243 | isl_basic_map_swap_div(bmap, j, exp[j]); |
Tobias Grosser | a67ac97 | 2016-02-26 11:35:12 +0000 | [diff] [blame] | 8244 | } |
| 8245 | j = 0; |
| 8246 | for (i = 0; i < div->n_row; ++i) { |
| 8247 | if (j < n_div && exp[j] == i) { |
| 8248 | j++; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8249 | } else { |
Tobias Grosser | 07b2095 | 2016-06-12 04:30:40 +0000 | [diff] [blame^] | 8250 | isl_seq_cpy(bmap->div[i], div->row[i], div->n_col); |
| 8251 | if (!isl_basic_map_div_is_known(bmap, i)) |
| 8252 | continue; |
| 8253 | if (isl_basic_map_add_div_constraints(bmap, i) < 0) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8254 | goto error; |
| 8255 | } |
| 8256 | } |
| 8257 | |
| 8258 | isl_mat_free(div); |
Tobias Grosser | 07b2095 | 2016-06-12 04:30:40 +0000 | [diff] [blame^] | 8259 | return bmap; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8260 | error: |
Tobias Grosser | 07b2095 | 2016-06-12 04:30:40 +0000 | [diff] [blame^] | 8261 | isl_basic_map_free(bmap); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8262 | isl_mat_free(div); |
| 8263 | return NULL; |
| 8264 | } |
| 8265 | |
Tobias Grosser | 07b2095 | 2016-06-12 04:30:40 +0000 | [diff] [blame^] | 8266 | /* Apply the expansion computed by isl_merge_divs. |
| 8267 | * The expansion itself is given by "exp" while the resulting |
| 8268 | * list of divs is given by "div". |
| 8269 | */ |
| 8270 | __isl_give isl_basic_set *isl_basic_set_expand_divs( |
| 8271 | __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp) |
| 8272 | { |
| 8273 | return isl_basic_map_expand_divs(bset, div, exp); |
| 8274 | } |
| 8275 | |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8276 | /* Look for a div in dst that corresponds to the div "div" in src. |
| 8277 | * The divs before "div" in src and dst are assumed to be the same. |
| 8278 | * |
| 8279 | * Returns -1 if no corresponding div was found and the position |
| 8280 | * of the corresponding div in dst otherwise. |
| 8281 | */ |
| 8282 | static int find_div(struct isl_basic_map *dst, |
| 8283 | struct isl_basic_map *src, unsigned div) |
| 8284 | { |
| 8285 | int i; |
| 8286 | |
| 8287 | unsigned total = isl_space_dim(src->dim, isl_dim_all); |
| 8288 | |
| 8289 | isl_assert(dst->ctx, div <= dst->n_div, return -1); |
| 8290 | for (i = div; i < dst->n_div; ++i) |
| 8291 | if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) && |
| 8292 | isl_seq_first_non_zero(dst->div[i]+1+1+total+div, |
| 8293 | dst->n_div - div) == -1) |
| 8294 | return i; |
| 8295 | return -1; |
| 8296 | } |
| 8297 | |
| 8298 | /* Align the divs of "dst" to those of "src", adding divs from "src" |
| 8299 | * if needed. That is, make sure that the first src->n_div divs |
| 8300 | * of the result are equal to those of src. |
| 8301 | * |
| 8302 | * The result is not finalized as by design it will have redundant |
| 8303 | * divs if any divs from "src" were copied. |
| 8304 | */ |
| 8305 | __isl_give isl_basic_map *isl_basic_map_align_divs( |
| 8306 | __isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src) |
| 8307 | { |
| 8308 | int i; |
| 8309 | int known, extended; |
| 8310 | unsigned total; |
| 8311 | |
| 8312 | if (!dst || !src) |
| 8313 | return isl_basic_map_free(dst); |
| 8314 | |
| 8315 | if (src->n_div == 0) |
| 8316 | return dst; |
| 8317 | |
| 8318 | known = isl_basic_map_divs_known(src); |
| 8319 | if (known < 0) |
| 8320 | return isl_basic_map_free(dst); |
| 8321 | if (!known) |
| 8322 | isl_die(isl_basic_map_get_ctx(src), isl_error_invalid, |
| 8323 | "some src divs are unknown", |
| 8324 | return isl_basic_map_free(dst)); |
| 8325 | |
| 8326 | src = isl_basic_map_order_divs(src); |
| 8327 | |
| 8328 | extended = 0; |
| 8329 | total = isl_space_dim(src->dim, isl_dim_all); |
| 8330 | for (i = 0; i < src->n_div; ++i) { |
| 8331 | int j = find_div(dst, src, i); |
| 8332 | if (j < 0) { |
| 8333 | if (!extended) { |
| 8334 | int extra = src->n_div - i; |
| 8335 | dst = isl_basic_map_cow(dst); |
| 8336 | if (!dst) |
| 8337 | return NULL; |
| 8338 | dst = isl_basic_map_extend_space(dst, |
| 8339 | isl_space_copy(dst->dim), |
| 8340 | extra, 0, 2 * extra); |
| 8341 | extended = 1; |
| 8342 | } |
| 8343 | j = isl_basic_map_alloc_div(dst); |
| 8344 | if (j < 0) |
| 8345 | return isl_basic_map_free(dst); |
| 8346 | isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i); |
| 8347 | isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i); |
| 8348 | if (isl_basic_map_add_div_constraints(dst, j) < 0) |
| 8349 | return isl_basic_map_free(dst); |
| 8350 | } |
| 8351 | if (j != i) |
| 8352 | isl_basic_map_swap_div(dst, i, j); |
| 8353 | } |
| 8354 | return dst; |
| 8355 | } |
| 8356 | |
| 8357 | struct isl_basic_set *isl_basic_set_align_divs( |
| 8358 | struct isl_basic_set *dst, struct isl_basic_set *src) |
| 8359 | { |
| 8360 | return (struct isl_basic_set *)isl_basic_map_align_divs( |
| 8361 | (struct isl_basic_map *)dst, (struct isl_basic_map *)src); |
| 8362 | } |
| 8363 | |
| 8364 | struct isl_map *isl_map_align_divs(struct isl_map *map) |
| 8365 | { |
| 8366 | int i; |
| 8367 | |
| 8368 | if (!map) |
| 8369 | return NULL; |
| 8370 | if (map->n == 0) |
| 8371 | return map; |
| 8372 | map = isl_map_compute_divs(map); |
| 8373 | map = isl_map_cow(map); |
| 8374 | if (!map) |
| 8375 | return NULL; |
| 8376 | |
| 8377 | for (i = 1; i < map->n; ++i) |
| 8378 | map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]); |
| 8379 | for (i = 1; i < map->n; ++i) { |
| 8380 | map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]); |
| 8381 | if (!map->p[i]) |
| 8382 | return isl_map_free(map); |
| 8383 | } |
| 8384 | |
| 8385 | ISL_F_CLR(map, ISL_MAP_NORMALIZED); |
| 8386 | return map; |
| 8387 | } |
| 8388 | |
| 8389 | struct isl_set *isl_set_align_divs(struct isl_set *set) |
| 8390 | { |
| 8391 | return (struct isl_set *)isl_map_align_divs((struct isl_map *)set); |
| 8392 | } |
| 8393 | |
Tobias Grosser | 1fa7b97 | 2015-02-16 19:33:40 +0000 | [diff] [blame] | 8394 | /* Align the divs of the basic maps in "map" to those |
| 8395 | * of the basic maps in "list", as well as to the other basic maps in "map". |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8396 | * The elements in "list" are assumed to have known divs. |
| 8397 | */ |
Tobias Grosser | 1fa7b97 | 2015-02-16 19:33:40 +0000 | [diff] [blame] | 8398 | __isl_give isl_map *isl_map_align_divs_to_basic_map_list( |
| 8399 | __isl_take isl_map *map, __isl_keep isl_basic_map_list *list) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8400 | { |
| 8401 | int i, n; |
| 8402 | |
Tobias Grosser | 1fa7b97 | 2015-02-16 19:33:40 +0000 | [diff] [blame] | 8403 | map = isl_map_compute_divs(map); |
| 8404 | map = isl_map_cow(map); |
| 8405 | if (!map || !list) |
| 8406 | return isl_map_free(map); |
| 8407 | if (map->n == 0) |
| 8408 | return map; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8409 | |
Tobias Grosser | 1fa7b97 | 2015-02-16 19:33:40 +0000 | [diff] [blame] | 8410 | n = isl_basic_map_list_n_basic_map(list); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8411 | for (i = 0; i < n; ++i) { |
Tobias Grosser | 1fa7b97 | 2015-02-16 19:33:40 +0000 | [diff] [blame] | 8412 | isl_basic_map *bmap; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8413 | |
Tobias Grosser | 1fa7b97 | 2015-02-16 19:33:40 +0000 | [diff] [blame] | 8414 | bmap = isl_basic_map_list_get_basic_map(list, i); |
| 8415 | map->p[0] = isl_basic_map_align_divs(map->p[0], bmap); |
| 8416 | isl_basic_map_free(bmap); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8417 | } |
Tobias Grosser | 1fa7b97 | 2015-02-16 19:33:40 +0000 | [diff] [blame] | 8418 | if (!map->p[0]) |
| 8419 | return isl_map_free(map); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8420 | |
Tobias Grosser | 1fa7b97 | 2015-02-16 19:33:40 +0000 | [diff] [blame] | 8421 | return isl_map_align_divs(map); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8422 | } |
| 8423 | |
Tobias Grosser | 1fa7b97 | 2015-02-16 19:33:40 +0000 | [diff] [blame] | 8424 | /* Align the divs of each element of "list" to those of "bmap". |
| 8425 | * Both "bmap" and the elements of "list" are assumed to have known divs. |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8426 | */ |
Tobias Grosser | 1fa7b97 | 2015-02-16 19:33:40 +0000 | [diff] [blame] | 8427 | __isl_give isl_basic_map_list *isl_basic_map_list_align_divs_to_basic_map( |
| 8428 | __isl_take isl_basic_map_list *list, __isl_keep isl_basic_map *bmap) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8429 | { |
| 8430 | int i, n; |
| 8431 | |
Tobias Grosser | 1fa7b97 | 2015-02-16 19:33:40 +0000 | [diff] [blame] | 8432 | if (!list || !bmap) |
| 8433 | return isl_basic_map_list_free(list); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8434 | |
Tobias Grosser | 1fa7b97 | 2015-02-16 19:33:40 +0000 | [diff] [blame] | 8435 | n = isl_basic_map_list_n_basic_map(list); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8436 | for (i = 0; i < n; ++i) { |
Tobias Grosser | 1fa7b97 | 2015-02-16 19:33:40 +0000 | [diff] [blame] | 8437 | isl_basic_map *bmap_i; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8438 | |
Tobias Grosser | 1fa7b97 | 2015-02-16 19:33:40 +0000 | [diff] [blame] | 8439 | bmap_i = isl_basic_map_list_get_basic_map(list, i); |
| 8440 | bmap_i = isl_basic_map_align_divs(bmap_i, bmap); |
| 8441 | list = isl_basic_map_list_set_basic_map(list, i, bmap_i); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8442 | } |
| 8443 | |
| 8444 | return list; |
| 8445 | } |
| 8446 | |
| 8447 | static __isl_give isl_set *set_apply( __isl_take isl_set *set, |
| 8448 | __isl_take isl_map *map) |
| 8449 | { |
| 8450 | if (!set || !map) |
| 8451 | goto error; |
| 8452 | isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error); |
| 8453 | map = isl_map_intersect_domain(map, set); |
| 8454 | set = isl_map_range(map); |
| 8455 | return set; |
| 8456 | error: |
| 8457 | isl_set_free(set); |
| 8458 | isl_map_free(map); |
| 8459 | return NULL; |
| 8460 | } |
| 8461 | |
| 8462 | __isl_give isl_set *isl_set_apply( __isl_take isl_set *set, |
| 8463 | __isl_take isl_map *map) |
| 8464 | { |
| 8465 | return isl_map_align_params_map_map_and(set, map, &set_apply); |
| 8466 | } |
| 8467 | |
| 8468 | /* There is no need to cow as removing empty parts doesn't change |
| 8469 | * the meaning of the set. |
| 8470 | */ |
| 8471 | struct isl_map *isl_map_remove_empty_parts(struct isl_map *map) |
| 8472 | { |
| 8473 | int i; |
| 8474 | |
| 8475 | if (!map) |
| 8476 | return NULL; |
| 8477 | |
| 8478 | for (i = map->n - 1; i >= 0; --i) |
| 8479 | remove_if_empty(map, i); |
| 8480 | |
| 8481 | return map; |
| 8482 | } |
| 8483 | |
| 8484 | struct isl_set *isl_set_remove_empty_parts(struct isl_set *set) |
| 8485 | { |
| 8486 | return (struct isl_set *) |
| 8487 | isl_map_remove_empty_parts((struct isl_map *)set); |
| 8488 | } |
| 8489 | |
| 8490 | struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map) |
| 8491 | { |
| 8492 | struct isl_basic_map *bmap; |
| 8493 | if (!map || map->n == 0) |
| 8494 | return NULL; |
| 8495 | bmap = map->p[map->n-1]; |
| 8496 | isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL); |
| 8497 | return isl_basic_map_copy(bmap); |
| 8498 | } |
| 8499 | |
| 8500 | struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set) |
| 8501 | { |
| 8502 | return (struct isl_basic_set *) |
| 8503 | isl_map_copy_basic_map((struct isl_map *)set); |
| 8504 | } |
| 8505 | |
| 8506 | __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map, |
| 8507 | __isl_keep isl_basic_map *bmap) |
| 8508 | { |
| 8509 | int i; |
| 8510 | |
| 8511 | if (!map || !bmap) |
| 8512 | goto error; |
| 8513 | for (i = map->n-1; i >= 0; --i) { |
| 8514 | if (map->p[i] != bmap) |
| 8515 | continue; |
| 8516 | map = isl_map_cow(map); |
| 8517 | if (!map) |
| 8518 | goto error; |
| 8519 | isl_basic_map_free(map->p[i]); |
| 8520 | if (i != map->n-1) { |
| 8521 | ISL_F_CLR(map, ISL_SET_NORMALIZED); |
| 8522 | map->p[i] = map->p[map->n-1]; |
| 8523 | } |
| 8524 | map->n--; |
| 8525 | return map; |
| 8526 | } |
| 8527 | return map; |
| 8528 | error: |
| 8529 | isl_map_free(map); |
| 8530 | return NULL; |
| 8531 | } |
| 8532 | |
| 8533 | struct isl_set *isl_set_drop_basic_set(struct isl_set *set, |
| 8534 | struct isl_basic_set *bset) |
| 8535 | { |
| 8536 | return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set, |
| 8537 | (struct isl_basic_map *)bset); |
| 8538 | } |
| 8539 | |
| 8540 | /* Given two basic sets bset1 and bset2, compute the maximal difference |
| 8541 | * between the values of dimension pos in bset1 and those in bset2 |
| 8542 | * for any common value of the parameters and dimensions preceding pos. |
| 8543 | */ |
| 8544 | static enum isl_lp_result basic_set_maximal_difference_at( |
| 8545 | __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2, |
| 8546 | int pos, isl_int *opt) |
| 8547 | { |
| 8548 | isl_space *dims; |
| 8549 | struct isl_basic_map *bmap1 = NULL; |
| 8550 | struct isl_basic_map *bmap2 = NULL; |
| 8551 | struct isl_ctx *ctx; |
| 8552 | struct isl_vec *obj; |
| 8553 | unsigned total; |
| 8554 | unsigned nparam; |
| 8555 | unsigned dim1, dim2; |
| 8556 | enum isl_lp_result res; |
| 8557 | |
| 8558 | if (!bset1 || !bset2) |
| 8559 | return isl_lp_error; |
| 8560 | |
| 8561 | nparam = isl_basic_set_n_param(bset1); |
| 8562 | dim1 = isl_basic_set_n_dim(bset1); |
| 8563 | dim2 = isl_basic_set_n_dim(bset2); |
| 8564 | dims = isl_space_alloc(bset1->ctx, nparam, pos, dim1 - pos); |
| 8565 | bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims); |
| 8566 | dims = isl_space_alloc(bset2->ctx, nparam, pos, dim2 - pos); |
| 8567 | bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims); |
| 8568 | if (!bmap1 || !bmap2) |
| 8569 | goto error; |
| 8570 | bmap1 = isl_basic_map_cow(bmap1); |
| 8571 | bmap1 = isl_basic_map_extend(bmap1, nparam, |
| 8572 | pos, (dim1 - pos) + (dim2 - pos), |
| 8573 | bmap2->n_div, bmap2->n_eq, bmap2->n_ineq); |
| 8574 | bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos); |
| 8575 | if (!bmap1) |
| 8576 | goto error2; |
| 8577 | total = isl_basic_map_total_dim(bmap1); |
| 8578 | ctx = bmap1->ctx; |
| 8579 | obj = isl_vec_alloc(ctx, 1 + total); |
| 8580 | if (!obj) |
| 8581 | goto error2; |
| 8582 | isl_seq_clr(obj->block.data, 1 + total); |
| 8583 | isl_int_set_si(obj->block.data[1+nparam+pos], 1); |
| 8584 | isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1); |
| 8585 | res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one, |
| 8586 | opt, NULL, NULL); |
| 8587 | isl_basic_map_free(bmap1); |
| 8588 | isl_vec_free(obj); |
| 8589 | return res; |
| 8590 | error: |
| 8591 | isl_basic_map_free(bmap2); |
| 8592 | error2: |
| 8593 | isl_basic_map_free(bmap1); |
| 8594 | return isl_lp_error; |
| 8595 | } |
| 8596 | |
| 8597 | /* Given two _disjoint_ basic sets bset1 and bset2, check whether |
| 8598 | * for any common value of the parameters and dimensions preceding pos |
| 8599 | * in both basic sets, the values of dimension pos in bset1 are |
| 8600 | * smaller or larger than those in bset2. |
| 8601 | * |
| 8602 | * Returns |
| 8603 | * 1 if bset1 follows bset2 |
| 8604 | * -1 if bset1 precedes bset2 |
| 8605 | * 0 if bset1 and bset2 are incomparable |
| 8606 | * -2 if some error occurred. |
| 8607 | */ |
| 8608 | int isl_basic_set_compare_at(struct isl_basic_set *bset1, |
| 8609 | struct isl_basic_set *bset2, int pos) |
| 8610 | { |
| 8611 | isl_int opt; |
| 8612 | enum isl_lp_result res; |
| 8613 | int cmp; |
| 8614 | |
| 8615 | isl_int_init(opt); |
| 8616 | |
| 8617 | res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt); |
| 8618 | |
| 8619 | if (res == isl_lp_empty) |
| 8620 | cmp = 0; |
| 8621 | else if ((res == isl_lp_ok && isl_int_is_pos(opt)) || |
| 8622 | res == isl_lp_unbounded) |
| 8623 | cmp = 1; |
| 8624 | else if (res == isl_lp_ok && isl_int_is_neg(opt)) |
| 8625 | cmp = -1; |
| 8626 | else |
| 8627 | cmp = -2; |
| 8628 | |
| 8629 | isl_int_clear(opt); |
| 8630 | return cmp; |
| 8631 | } |
| 8632 | |
| 8633 | /* Given two basic sets bset1 and bset2, check whether |
| 8634 | * for any common value of the parameters and dimensions preceding pos |
| 8635 | * there is a value of dimension pos in bset1 that is larger |
| 8636 | * than a value of the same dimension in bset2. |
| 8637 | * |
| 8638 | * Return |
| 8639 | * 1 if there exists such a pair |
| 8640 | * 0 if there is no such pair, but there is a pair of equal values |
| 8641 | * -1 otherwise |
| 8642 | * -2 if some error occurred. |
| 8643 | */ |
| 8644 | int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1, |
| 8645 | __isl_keep isl_basic_set *bset2, int pos) |
| 8646 | { |
| 8647 | isl_int opt; |
| 8648 | enum isl_lp_result res; |
| 8649 | int cmp; |
| 8650 | |
| 8651 | isl_int_init(opt); |
| 8652 | |
| 8653 | res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt); |
| 8654 | |
| 8655 | if (res == isl_lp_empty) |
| 8656 | cmp = -1; |
| 8657 | else if ((res == isl_lp_ok && isl_int_is_pos(opt)) || |
| 8658 | res == isl_lp_unbounded) |
| 8659 | cmp = 1; |
| 8660 | else if (res == isl_lp_ok && isl_int_is_neg(opt)) |
| 8661 | cmp = -1; |
| 8662 | else if (res == isl_lp_ok) |
| 8663 | cmp = 0; |
| 8664 | else |
| 8665 | cmp = -2; |
| 8666 | |
| 8667 | isl_int_clear(opt); |
| 8668 | return cmp; |
| 8669 | } |
| 8670 | |
| 8671 | /* Given two sets set1 and set2, check whether |
| 8672 | * for any common value of the parameters and dimensions preceding pos |
| 8673 | * there is a value of dimension pos in set1 that is larger |
| 8674 | * than a value of the same dimension in set2. |
| 8675 | * |
| 8676 | * Return |
| 8677 | * 1 if there exists such a pair |
| 8678 | * 0 if there is no such pair, but there is a pair of equal values |
| 8679 | * -1 otherwise |
| 8680 | * -2 if some error occurred. |
| 8681 | */ |
| 8682 | int isl_set_follows_at(__isl_keep isl_set *set1, |
| 8683 | __isl_keep isl_set *set2, int pos) |
| 8684 | { |
| 8685 | int i, j; |
| 8686 | int follows = -1; |
| 8687 | |
| 8688 | if (!set1 || !set2) |
| 8689 | return -2; |
| 8690 | |
| 8691 | for (i = 0; i < set1->n; ++i) |
| 8692 | for (j = 0; j < set2->n; ++j) { |
| 8693 | int f; |
| 8694 | f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos); |
| 8695 | if (f == 1 || f == -2) |
| 8696 | return f; |
| 8697 | if (f > follows) |
| 8698 | follows = f; |
| 8699 | } |
| 8700 | |
| 8701 | return follows; |
| 8702 | } |
| 8703 | |
| 8704 | static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map *bmap, |
| 8705 | unsigned pos, isl_int *val) |
| 8706 | { |
| 8707 | int i; |
| 8708 | int d; |
| 8709 | unsigned total; |
| 8710 | |
| 8711 | if (!bmap) |
| 8712 | return -1; |
| 8713 | total = isl_basic_map_total_dim(bmap); |
| 8714 | for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) { |
| 8715 | for (; d+1 > pos; --d) |
| 8716 | if (!isl_int_is_zero(bmap->eq[i][1+d])) |
| 8717 | break; |
| 8718 | if (d != pos) |
| 8719 | continue; |
| 8720 | if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1) |
| 8721 | return 0; |
| 8722 | if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1) |
| 8723 | return 0; |
| 8724 | if (!isl_int_is_one(bmap->eq[i][1+d])) |
| 8725 | return 0; |
| 8726 | if (val) |
| 8727 | isl_int_neg(*val, bmap->eq[i][0]); |
| 8728 | return 1; |
| 8729 | } |
| 8730 | return 0; |
| 8731 | } |
| 8732 | |
| 8733 | static int isl_map_plain_has_fixed_var(__isl_keep isl_map *map, |
| 8734 | unsigned pos, isl_int *val) |
| 8735 | { |
| 8736 | int i; |
| 8737 | isl_int v; |
| 8738 | isl_int tmp; |
| 8739 | int fixed; |
| 8740 | |
| 8741 | if (!map) |
| 8742 | return -1; |
| 8743 | if (map->n == 0) |
| 8744 | return 0; |
| 8745 | if (map->n == 1) |
| 8746 | return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val); |
| 8747 | isl_int_init(v); |
| 8748 | isl_int_init(tmp); |
| 8749 | fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v); |
| 8750 | for (i = 1; fixed == 1 && i < map->n; ++i) { |
| 8751 | fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp); |
| 8752 | if (fixed == 1 && isl_int_ne(tmp, v)) |
| 8753 | fixed = 0; |
| 8754 | } |
| 8755 | if (val) |
| 8756 | isl_int_set(*val, v); |
| 8757 | isl_int_clear(tmp); |
| 8758 | isl_int_clear(v); |
| 8759 | return fixed; |
| 8760 | } |
| 8761 | |
| 8762 | static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set *bset, |
| 8763 | unsigned pos, isl_int *val) |
| 8764 | { |
| 8765 | return isl_basic_map_plain_has_fixed_var((struct isl_basic_map *)bset, |
| 8766 | pos, val); |
| 8767 | } |
| 8768 | |
| 8769 | static int isl_set_plain_has_fixed_var(__isl_keep isl_set *set, unsigned pos, |
| 8770 | isl_int *val) |
| 8771 | { |
| 8772 | return isl_map_plain_has_fixed_var((struct isl_map *)set, pos, val); |
| 8773 | } |
| 8774 | |
| 8775 | int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap, |
| 8776 | enum isl_dim_type type, unsigned pos, isl_int *val) |
| 8777 | { |
| 8778 | if (pos >= isl_basic_map_dim(bmap, type)) |
| 8779 | return -1; |
| 8780 | return isl_basic_map_plain_has_fixed_var(bmap, |
| 8781 | isl_basic_map_offset(bmap, type) - 1 + pos, val); |
| 8782 | } |
| 8783 | |
| 8784 | /* If "bmap" obviously lies on a hyperplane where the given dimension |
| 8785 | * has a fixed value, then return that value. |
| 8786 | * Otherwise return NaN. |
| 8787 | */ |
| 8788 | __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed( |
| 8789 | __isl_keep isl_basic_map *bmap, |
| 8790 | enum isl_dim_type type, unsigned pos) |
| 8791 | { |
| 8792 | isl_ctx *ctx; |
| 8793 | isl_val *v; |
| 8794 | int fixed; |
| 8795 | |
| 8796 | if (!bmap) |
| 8797 | return NULL; |
| 8798 | ctx = isl_basic_map_get_ctx(bmap); |
| 8799 | v = isl_val_alloc(ctx); |
| 8800 | if (!v) |
| 8801 | return NULL; |
| 8802 | fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n); |
| 8803 | if (fixed < 0) |
| 8804 | return isl_val_free(v); |
| 8805 | if (fixed) { |
| 8806 | isl_int_set_si(v->d, 1); |
| 8807 | return v; |
| 8808 | } |
| 8809 | isl_val_free(v); |
| 8810 | return isl_val_nan(ctx); |
| 8811 | } |
| 8812 | |
| 8813 | int isl_map_plain_is_fixed(__isl_keep isl_map *map, |
| 8814 | enum isl_dim_type type, unsigned pos, isl_int *val) |
| 8815 | { |
| 8816 | if (pos >= isl_map_dim(map, type)) |
| 8817 | return -1; |
| 8818 | return isl_map_plain_has_fixed_var(map, |
| 8819 | map_offset(map, type) - 1 + pos, val); |
| 8820 | } |
| 8821 | |
| 8822 | /* If "map" obviously lies on a hyperplane where the given dimension |
| 8823 | * has a fixed value, then return that value. |
| 8824 | * Otherwise return NaN. |
| 8825 | */ |
| 8826 | __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map, |
| 8827 | enum isl_dim_type type, unsigned pos) |
| 8828 | { |
| 8829 | isl_ctx *ctx; |
| 8830 | isl_val *v; |
| 8831 | int fixed; |
| 8832 | |
| 8833 | if (!map) |
| 8834 | return NULL; |
| 8835 | ctx = isl_map_get_ctx(map); |
| 8836 | v = isl_val_alloc(ctx); |
| 8837 | if (!v) |
| 8838 | return NULL; |
| 8839 | fixed = isl_map_plain_is_fixed(map, type, pos, &v->n); |
| 8840 | if (fixed < 0) |
| 8841 | return isl_val_free(v); |
| 8842 | if (fixed) { |
| 8843 | isl_int_set_si(v->d, 1); |
| 8844 | return v; |
| 8845 | } |
| 8846 | isl_val_free(v); |
| 8847 | return isl_val_nan(ctx); |
| 8848 | } |
| 8849 | |
| 8850 | /* If "set" obviously lies on a hyperplane where the given dimension |
| 8851 | * has a fixed value, then return that value. |
| 8852 | * Otherwise return NaN. |
| 8853 | */ |
| 8854 | __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set, |
| 8855 | enum isl_dim_type type, unsigned pos) |
| 8856 | { |
| 8857 | return isl_map_plain_get_val_if_fixed(set, type, pos); |
| 8858 | } |
| 8859 | |
| 8860 | int isl_set_plain_is_fixed(__isl_keep isl_set *set, |
| 8861 | enum isl_dim_type type, unsigned pos, isl_int *val) |
| 8862 | { |
| 8863 | return isl_map_plain_is_fixed(set, type, pos, val); |
| 8864 | } |
| 8865 | |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8866 | /* Check if dimension dim has fixed value and if so and if val is not NULL, |
| 8867 | * then return this fixed value in *val. |
| 8868 | */ |
| 8869 | int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset, |
| 8870 | unsigned dim, isl_int *val) |
| 8871 | { |
| 8872 | return isl_basic_set_plain_has_fixed_var(bset, |
| 8873 | isl_basic_set_n_param(bset) + dim, val); |
| 8874 | } |
| 8875 | |
| 8876 | /* Check if dimension dim has fixed value and if so and if val is not NULL, |
| 8877 | * then return this fixed value in *val. |
| 8878 | */ |
| 8879 | int isl_set_plain_dim_is_fixed(__isl_keep isl_set *set, |
| 8880 | unsigned dim, isl_int *val) |
| 8881 | { |
| 8882 | return isl_set_plain_has_fixed_var(set, isl_set_n_param(set) + dim, val); |
| 8883 | } |
| 8884 | |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8885 | /* Check if input variable in has fixed value and if so and if val is not NULL, |
| 8886 | * then return this fixed value in *val. |
| 8887 | */ |
| 8888 | int isl_map_plain_input_is_fixed(__isl_keep isl_map *map, |
| 8889 | unsigned in, isl_int *val) |
| 8890 | { |
| 8891 | return isl_map_plain_has_fixed_var(map, isl_map_n_param(map) + in, val); |
| 8892 | } |
| 8893 | |
| 8894 | /* Check if dimension dim has an (obvious) fixed lower bound and if so |
| 8895 | * and if val is not NULL, then return this lower bound in *val. |
| 8896 | */ |
| 8897 | int isl_basic_set_plain_dim_has_fixed_lower_bound( |
| 8898 | __isl_keep isl_basic_set *bset, unsigned dim, isl_int *val) |
| 8899 | { |
| 8900 | int i, i_eq = -1, i_ineq = -1; |
| 8901 | isl_int *c; |
| 8902 | unsigned total; |
| 8903 | unsigned nparam; |
| 8904 | |
| 8905 | if (!bset) |
| 8906 | return -1; |
| 8907 | total = isl_basic_set_total_dim(bset); |
| 8908 | nparam = isl_basic_set_n_param(bset); |
| 8909 | for (i = 0; i < bset->n_eq; ++i) { |
| 8910 | if (isl_int_is_zero(bset->eq[i][1+nparam+dim])) |
| 8911 | continue; |
| 8912 | if (i_eq != -1) |
| 8913 | return 0; |
| 8914 | i_eq = i; |
| 8915 | } |
| 8916 | for (i = 0; i < bset->n_ineq; ++i) { |
| 8917 | if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim])) |
| 8918 | continue; |
| 8919 | if (i_eq != -1 || i_ineq != -1) |
| 8920 | return 0; |
| 8921 | i_ineq = i; |
| 8922 | } |
| 8923 | if (i_eq == -1 && i_ineq == -1) |
| 8924 | return 0; |
| 8925 | c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq]; |
| 8926 | /* The coefficient should always be one due to normalization. */ |
| 8927 | if (!isl_int_is_one(c[1+nparam+dim])) |
| 8928 | return 0; |
| 8929 | if (isl_seq_first_non_zero(c+1, nparam+dim) != -1) |
| 8930 | return 0; |
| 8931 | if (isl_seq_first_non_zero(c+1+nparam+dim+1, |
| 8932 | total - nparam - dim - 1) != -1) |
| 8933 | return 0; |
| 8934 | if (val) |
| 8935 | isl_int_neg(*val, c[0]); |
| 8936 | return 1; |
| 8937 | } |
| 8938 | |
| 8939 | int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set *set, |
| 8940 | unsigned dim, isl_int *val) |
| 8941 | { |
| 8942 | int i; |
| 8943 | isl_int v; |
| 8944 | isl_int tmp; |
| 8945 | int fixed; |
| 8946 | |
| 8947 | if (!set) |
| 8948 | return -1; |
| 8949 | if (set->n == 0) |
| 8950 | return 0; |
| 8951 | if (set->n == 1) |
| 8952 | return isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0], |
| 8953 | dim, val); |
| 8954 | isl_int_init(v); |
| 8955 | isl_int_init(tmp); |
| 8956 | fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0], |
| 8957 | dim, &v); |
| 8958 | for (i = 1; fixed == 1 && i < set->n; ++i) { |
| 8959 | fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[i], |
| 8960 | dim, &tmp); |
| 8961 | if (fixed == 1 && isl_int_ne(tmp, v)) |
| 8962 | fixed = 0; |
| 8963 | } |
| 8964 | if (val) |
| 8965 | isl_int_set(*val, v); |
| 8966 | isl_int_clear(tmp); |
| 8967 | isl_int_clear(v); |
| 8968 | return fixed; |
| 8969 | } |
| 8970 | |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 8971 | /* Return -1 if the constraint "c1" should be sorted before "c2" |
| 8972 | * and 1 if it should be sorted after "c2". |
| 8973 | * Return 0 if the two constraints are the same (up to the constant term). |
| 8974 | * |
| 8975 | * In particular, if a constraint involves later variables than another |
| 8976 | * then it is sorted after this other constraint. |
| 8977 | * uset_gist depends on constraints without existentially quantified |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8978 | * variables sorting first. |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 8979 | * |
| 8980 | * For constraints that have the same latest variable, those |
| 8981 | * with the same coefficient for this latest variable (first in absolute value |
| 8982 | * and then in actual value) are grouped together. |
| 8983 | * This is useful for detecting pairs of constraints that can |
| 8984 | * be chained in their printed representation. |
| 8985 | * |
| 8986 | * Finally, within a group, constraints are sorted according to |
| 8987 | * their coefficients (excluding the constant term). |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8988 | */ |
| 8989 | static int sort_constraint_cmp(const void *p1, const void *p2, void *arg) |
| 8990 | { |
| 8991 | isl_int **c1 = (isl_int **) p1; |
| 8992 | isl_int **c2 = (isl_int **) p2; |
| 8993 | int l1, l2; |
| 8994 | unsigned size = *(unsigned *) arg; |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 8995 | int cmp; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8996 | |
| 8997 | l1 = isl_seq_last_non_zero(*c1 + 1, size); |
| 8998 | l2 = isl_seq_last_non_zero(*c2 + 1, size); |
| 8999 | |
| 9000 | if (l1 != l2) |
| 9001 | return l1 - l2; |
| 9002 | |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 9003 | cmp = isl_int_abs_cmp((*c1)[1 + l1], (*c2)[1 + l1]); |
| 9004 | if (cmp != 0) |
| 9005 | return cmp; |
| 9006 | cmp = isl_int_cmp((*c1)[1 + l1], (*c2)[1 + l1]); |
| 9007 | if (cmp != 0) |
| 9008 | return -cmp; |
| 9009 | |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 9010 | return isl_seq_cmp(*c1 + 1, *c2 + 1, size); |
| 9011 | } |
| 9012 | |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 9013 | /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2" |
| 9014 | * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2" |
| 9015 | * and 0 if the two constraints are the same (up to the constant term). |
| 9016 | */ |
| 9017 | int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map *bmap, |
| 9018 | isl_int *c1, isl_int *c2) |
| 9019 | { |
| 9020 | unsigned total; |
| 9021 | |
| 9022 | if (!bmap) |
| 9023 | return -2; |
| 9024 | total = isl_basic_map_total_dim(bmap); |
| 9025 | return sort_constraint_cmp(&c1, &c2, &total); |
| 9026 | } |
| 9027 | |
| 9028 | __isl_give isl_basic_map *isl_basic_map_sort_constraints( |
| 9029 | __isl_take isl_basic_map *bmap) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 9030 | { |
| 9031 | unsigned total; |
| 9032 | |
| 9033 | if (!bmap) |
| 9034 | return NULL; |
| 9035 | if (bmap->n_ineq == 0) |
| 9036 | return bmap; |
| 9037 | if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED)) |
| 9038 | return bmap; |
| 9039 | total = isl_basic_map_total_dim(bmap); |
| 9040 | if (isl_sort(bmap->ineq, bmap->n_ineq, sizeof(isl_int *), |
| 9041 | &sort_constraint_cmp, &total) < 0) |
| 9042 | return isl_basic_map_free(bmap); |
| 9043 | return bmap; |
| 9044 | } |
| 9045 | |
| 9046 | __isl_give isl_basic_set *isl_basic_set_sort_constraints( |
| 9047 | __isl_take isl_basic_set *bset) |
| 9048 | { |
| 9049 | return (struct isl_basic_set *)isl_basic_map_sort_constraints( |
| 9050 | (struct isl_basic_map *)bset); |
| 9051 | } |
| 9052 | |
| 9053 | struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap) |
| 9054 | { |
| 9055 | if (!bmap) |
| 9056 | return NULL; |
| 9057 | if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED)) |
| 9058 | return bmap; |
| 9059 | bmap = isl_basic_map_remove_redundancies(bmap); |
| 9060 | bmap = isl_basic_map_sort_constraints(bmap); |
| 9061 | if (bmap) |
| 9062 | ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED); |
| 9063 | return bmap; |
| 9064 | } |
| 9065 | |
| 9066 | struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset) |
| 9067 | { |
| 9068 | return (struct isl_basic_set *)isl_basic_map_normalize( |
| 9069 | (struct isl_basic_map *)bset); |
| 9070 | } |
| 9071 | |
| 9072 | int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map *bmap1, |
| 9073 | const __isl_keep isl_basic_map *bmap2) |
| 9074 | { |
| 9075 | int i, cmp; |
| 9076 | unsigned total; |
| 9077 | |
Tobias Grosser | 3e6070e | 2015-05-09 09:37:30 +0000 | [diff] [blame] | 9078 | if (!bmap1 || !bmap2) |
| 9079 | return -1; |
| 9080 | |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 9081 | if (bmap1 == bmap2) |
| 9082 | return 0; |
| 9083 | if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) != |
| 9084 | ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL)) |
| 9085 | return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1; |
| 9086 | if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2)) |
| 9087 | return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2); |
| 9088 | if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2)) |
| 9089 | return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2); |
| 9090 | if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2)) |
| 9091 | return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2); |
| 9092 | if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) && |
| 9093 | ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY)) |
| 9094 | return 0; |
| 9095 | if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY)) |
| 9096 | return 1; |
| 9097 | if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY)) |
| 9098 | return -1; |
| 9099 | if (bmap1->n_eq != bmap2->n_eq) |
| 9100 | return bmap1->n_eq - bmap2->n_eq; |
| 9101 | if (bmap1->n_ineq != bmap2->n_ineq) |
| 9102 | return bmap1->n_ineq - bmap2->n_ineq; |
| 9103 | if (bmap1->n_div != bmap2->n_div) |
| 9104 | return bmap1->n_div - bmap2->n_div; |
| 9105 | total = isl_basic_map_total_dim(bmap1); |
| 9106 | for (i = 0; i < bmap1->n_eq; ++i) { |
| 9107 | cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total); |
| 9108 | if (cmp) |
| 9109 | return cmp; |
| 9110 | } |
| 9111 | for (i = 0; i < bmap1->n_ineq; ++i) { |
| 9112 | cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total); |
| 9113 | if (cmp) |
| 9114 | return cmp; |
| 9115 | } |
| 9116 | for (i = 0; i < bmap1->n_div; ++i) { |
| 9117 | cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total); |
| 9118 | if (cmp) |
| 9119 | return cmp; |
| 9120 | } |
| 9121 | return 0; |
| 9122 | } |
| 9123 | |
| 9124 | int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set *bset1, |
| 9125 | const __isl_keep isl_basic_set *bset2) |
| 9126 | { |
| 9127 | return isl_basic_map_plain_cmp(bset1, bset2); |
| 9128 | } |
| 9129 | |
| 9130 | int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2) |
| 9131 | { |
| 9132 | int i, cmp; |
| 9133 | |
| 9134 | if (set1 == set2) |
| 9135 | return 0; |
| 9136 | if (set1->n != set2->n) |
| 9137 | return set1->n - set2->n; |
| 9138 | |
| 9139 | for (i = 0; i < set1->n; ++i) { |
| 9140 | cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]); |
| 9141 | if (cmp) |
| 9142 | return cmp; |
| 9143 | } |
| 9144 | |
| 9145 | return 0; |
| 9146 | } |
| 9147 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 9148 | isl_bool isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1, |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 9149 | __isl_keep isl_basic_map *bmap2) |
| 9150 | { |
Tobias Grosser | e0f8d59 | 2015-05-13 13:10:13 +0000 | [diff] [blame] | 9151 | if (!bmap1 || !bmap2) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 9152 | return isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 9153 | return isl_basic_map_plain_cmp(bmap1, bmap2) == 0; |
| 9154 | } |
| 9155 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 9156 | isl_bool isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1, |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 9157 | __isl_keep isl_basic_set *bset2) |
| 9158 | { |
| 9159 | return isl_basic_map_plain_is_equal((isl_basic_map *)bset1, |
| 9160 | (isl_basic_map *)bset2); |
| 9161 | } |
| 9162 | |
| 9163 | static int qsort_bmap_cmp(const void *p1, const void *p2) |
| 9164 | { |
| 9165 | const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1; |
| 9166 | const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2; |
| 9167 | |
| 9168 | return isl_basic_map_plain_cmp(bmap1, bmap2); |
| 9169 | } |
| 9170 | |
| 9171 | /* Sort the basic maps of "map" and remove duplicate basic maps. |
| 9172 | * |
| 9173 | * While removing basic maps, we make sure that the basic maps remain |
| 9174 | * sorted because isl_map_normalize expects the basic maps of the result |
| 9175 | * to be sorted. |
| 9176 | */ |
| 9177 | static __isl_give isl_map *sort_and_remove_duplicates(__isl_take isl_map *map) |
| 9178 | { |
| 9179 | int i, j; |
| 9180 | |
| 9181 | map = isl_map_remove_empty_parts(map); |
| 9182 | if (!map) |
| 9183 | return NULL; |
| 9184 | qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp); |
| 9185 | for (i = map->n - 1; i >= 1; --i) { |
| 9186 | if (!isl_basic_map_plain_is_equal(map->p[i - 1], map->p[i])) |
| 9187 | continue; |
| 9188 | isl_basic_map_free(map->p[i-1]); |
| 9189 | for (j = i; j < map->n; ++j) |
| 9190 | map->p[j - 1] = map->p[j]; |
| 9191 | map->n--; |
| 9192 | } |
| 9193 | |
| 9194 | return map; |
| 9195 | } |
| 9196 | |
| 9197 | /* Remove obvious duplicates among the basic maps of "map". |
| 9198 | * |
| 9199 | * Unlike isl_map_normalize, this function does not remove redundant |
| 9200 | * constraints and only removes duplicates that have exactly the same |
| 9201 | * constraints in the input. It does sort the constraints and |
| 9202 | * the basic maps to ease the detection of duplicates. |
| 9203 | * |
| 9204 | * If "map" has already been normalized or if the basic maps are |
| 9205 | * disjoint, then there can be no duplicates. |
| 9206 | */ |
| 9207 | __isl_give isl_map *isl_map_remove_obvious_duplicates(__isl_take isl_map *map) |
| 9208 | { |
| 9209 | int i; |
| 9210 | isl_basic_map *bmap; |
| 9211 | |
| 9212 | if (!map) |
| 9213 | return NULL; |
| 9214 | if (map->n <= 1) |
| 9215 | return map; |
| 9216 | if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED | ISL_MAP_DISJOINT)) |
| 9217 | return map; |
| 9218 | for (i = 0; i < map->n; ++i) { |
| 9219 | bmap = isl_basic_map_copy(map->p[i]); |
| 9220 | bmap = isl_basic_map_sort_constraints(bmap); |
| 9221 | if (!bmap) |
| 9222 | return isl_map_free(map); |
| 9223 | isl_basic_map_free(map->p[i]); |
| 9224 | map->p[i] = bmap; |
| 9225 | } |
| 9226 | |
| 9227 | map = sort_and_remove_duplicates(map); |
| 9228 | return map; |
| 9229 | } |
| 9230 | |
| 9231 | /* We normalize in place, but if anything goes wrong we need |
| 9232 | * to return NULL, so we need to make sure we don't change the |
| 9233 | * meaning of any possible other copies of map. |
| 9234 | */ |
| 9235 | __isl_give isl_map *isl_map_normalize(__isl_take isl_map *map) |
| 9236 | { |
| 9237 | int i; |
| 9238 | struct isl_basic_map *bmap; |
| 9239 | |
| 9240 | if (!map) |
| 9241 | return NULL; |
| 9242 | if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED)) |
| 9243 | return map; |
| 9244 | for (i = 0; i < map->n; ++i) { |
| 9245 | bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i])); |
| 9246 | if (!bmap) |
| 9247 | goto error; |
| 9248 | isl_basic_map_free(map->p[i]); |
| 9249 | map->p[i] = bmap; |
| 9250 | } |
| 9251 | |
| 9252 | map = sort_and_remove_duplicates(map); |
| 9253 | if (map) |
| 9254 | ISL_F_SET(map, ISL_MAP_NORMALIZED); |
| 9255 | return map; |
| 9256 | error: |
| 9257 | isl_map_free(map); |
| 9258 | return NULL; |
| 9259 | } |
| 9260 | |
| 9261 | struct isl_set *isl_set_normalize(struct isl_set *set) |
| 9262 | { |
| 9263 | return (struct isl_set *)isl_map_normalize((struct isl_map *)set); |
| 9264 | } |
| 9265 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 9266 | isl_bool isl_map_plain_is_equal(__isl_keep isl_map *map1, |
| 9267 | __isl_keep isl_map *map2) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 9268 | { |
| 9269 | int i; |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 9270 | isl_bool equal; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 9271 | |
| 9272 | if (!map1 || !map2) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 9273 | return isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 9274 | |
| 9275 | if (map1 == map2) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 9276 | return isl_bool_true; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 9277 | if (!isl_space_is_equal(map1->dim, map2->dim)) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 9278 | return isl_bool_false; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 9279 | |
| 9280 | map1 = isl_map_copy(map1); |
| 9281 | map2 = isl_map_copy(map2); |
| 9282 | map1 = isl_map_normalize(map1); |
| 9283 | map2 = isl_map_normalize(map2); |
| 9284 | if (!map1 || !map2) |
| 9285 | goto error; |
| 9286 | equal = map1->n == map2->n; |
| 9287 | for (i = 0; equal && i < map1->n; ++i) { |
| 9288 | equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]); |
| 9289 | if (equal < 0) |
| 9290 | goto error; |
| 9291 | } |
| 9292 | isl_map_free(map1); |
| 9293 | isl_map_free(map2); |
| 9294 | return equal; |
| 9295 | error: |
| 9296 | isl_map_free(map1); |
| 9297 | isl_map_free(map2); |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 9298 | return isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 9299 | } |
| 9300 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 9301 | isl_bool isl_set_plain_is_equal(__isl_keep isl_set *set1, |
| 9302 | __isl_keep isl_set *set2) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 9303 | { |
| 9304 | return isl_map_plain_is_equal((struct isl_map *)set1, |
| 9305 | (struct isl_map *)set2); |
| 9306 | } |
| 9307 | |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 9308 | /* Return an interval that ranges from min to max (inclusive) |
| 9309 | */ |
| 9310 | struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx, |
| 9311 | isl_int min, isl_int max) |
| 9312 | { |
| 9313 | int k; |
| 9314 | struct isl_basic_set *bset = NULL; |
| 9315 | |
| 9316 | bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2); |
| 9317 | if (!bset) |
| 9318 | goto error; |
| 9319 | |
| 9320 | k = isl_basic_set_alloc_inequality(bset); |
| 9321 | if (k < 0) |
| 9322 | goto error; |
| 9323 | isl_int_set_si(bset->ineq[k][1], 1); |
| 9324 | isl_int_neg(bset->ineq[k][0], min); |
| 9325 | |
| 9326 | k = isl_basic_set_alloc_inequality(bset); |
| 9327 | if (k < 0) |
| 9328 | goto error; |
| 9329 | isl_int_set_si(bset->ineq[k][1], -1); |
| 9330 | isl_int_set(bset->ineq[k][0], max); |
| 9331 | |
| 9332 | return bset; |
| 9333 | error: |
| 9334 | isl_basic_set_free(bset); |
| 9335 | return NULL; |
| 9336 | } |
| 9337 | |
Tobias Grosser | 1fa7b97 | 2015-02-16 19:33:40 +0000 | [diff] [blame] | 9338 | /* Return the basic maps in "map" as a list. |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 9339 | */ |
Tobias Grosser | 1fa7b97 | 2015-02-16 19:33:40 +0000 | [diff] [blame] | 9340 | __isl_give isl_basic_map_list *isl_map_get_basic_map_list( |
| 9341 | __isl_keep isl_map *map) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 9342 | { |
| 9343 | int i; |
| 9344 | isl_ctx *ctx; |
Tobias Grosser | 1fa7b97 | 2015-02-16 19:33:40 +0000 | [diff] [blame] | 9345 | isl_basic_map_list *list; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 9346 | |
Tobias Grosser | 1fa7b97 | 2015-02-16 19:33:40 +0000 | [diff] [blame] | 9347 | if (!map) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 9348 | return NULL; |
Tobias Grosser | 1fa7b97 | 2015-02-16 19:33:40 +0000 | [diff] [blame] | 9349 | ctx = isl_map_get_ctx(map); |
| 9350 | list = isl_basic_map_list_alloc(ctx, map->n); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 9351 | |
Tobias Grosser | 1fa7b97 | 2015-02-16 19:33:40 +0000 | [diff] [blame] | 9352 | for (i = 0; i < map->n; ++i) { |
| 9353 | isl_basic_map *bmap; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 9354 | |
Tobias Grosser | 1fa7b97 | 2015-02-16 19:33:40 +0000 | [diff] [blame] | 9355 | bmap = isl_basic_map_copy(map->p[i]); |
| 9356 | list = isl_basic_map_list_add(list, bmap); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 9357 | } |
| 9358 | |
| 9359 | return list; |
| 9360 | } |
| 9361 | |
| 9362 | /* Return the intersection of the elements in the non-empty list "list". |
| 9363 | * All elements are assumed to live in the same space. |
| 9364 | */ |
Tobias Grosser | 1fa7b97 | 2015-02-16 19:33:40 +0000 | [diff] [blame] | 9365 | __isl_give isl_basic_map *isl_basic_map_list_intersect( |
| 9366 | __isl_take isl_basic_map_list *list) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 9367 | { |
| 9368 | int i, n; |
Tobias Grosser | 1fa7b97 | 2015-02-16 19:33:40 +0000 | [diff] [blame] | 9369 | isl_basic_map *bmap; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 9370 | |
| 9371 | if (!list) |
| 9372 | return NULL; |
Tobias Grosser | 1fa7b97 | 2015-02-16 19:33:40 +0000 | [diff] [blame] | 9373 | n = isl_basic_map_list_n_basic_map(list); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 9374 | if (n < 1) |
Tobias Grosser | 1fa7b97 | 2015-02-16 19:33:40 +0000 | [diff] [blame] | 9375 | isl_die(isl_basic_map_list_get_ctx(list), isl_error_invalid, |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 9376 | "expecting non-empty list", goto error); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 9377 | |
Tobias Grosser | 1fa7b97 | 2015-02-16 19:33:40 +0000 | [diff] [blame] | 9378 | bmap = isl_basic_map_list_get_basic_map(list, 0); |
| 9379 | for (i = 1; i < n; ++i) { |
| 9380 | isl_basic_map *bmap_i; |
| 9381 | |
| 9382 | bmap_i = isl_basic_map_list_get_basic_map(list, i); |
| 9383 | bmap = isl_basic_map_intersect(bmap, bmap_i); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 9384 | } |
| 9385 | |
Tobias Grosser | 1fa7b97 | 2015-02-16 19:33:40 +0000 | [diff] [blame] | 9386 | isl_basic_map_list_free(list); |
| 9387 | return bmap; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 9388 | error: |
Tobias Grosser | 1fa7b97 | 2015-02-16 19:33:40 +0000 | [diff] [blame] | 9389 | isl_basic_map_list_free(list); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 9390 | return NULL; |
| 9391 | } |
| 9392 | |
Tobias Grosser | 1fa7b97 | 2015-02-16 19:33:40 +0000 | [diff] [blame] | 9393 | /* Return the intersection of the elements in the non-empty list "list". |
| 9394 | * All elements are assumed to live in the same space. |
| 9395 | */ |
| 9396 | __isl_give isl_basic_set *isl_basic_set_list_intersect( |
| 9397 | __isl_take isl_basic_set_list *list) |
| 9398 | { |
| 9399 | return isl_basic_map_list_intersect(list); |
| 9400 | } |
| 9401 | |
Tobias Grosser | a67ac97 | 2016-02-26 11:35:12 +0000 | [diff] [blame] | 9402 | /* Return the union of the elements in the non-empty list "list". |
| 9403 | * All elements are assumed to live in the same space. |
| 9404 | */ |
| 9405 | __isl_give isl_set *isl_set_list_union(__isl_take isl_set_list *list) |
| 9406 | { |
| 9407 | int i, n; |
| 9408 | isl_set *set; |
| 9409 | |
| 9410 | if (!list) |
| 9411 | return NULL; |
| 9412 | n = isl_set_list_n_set(list); |
| 9413 | if (n < 1) |
| 9414 | isl_die(isl_set_list_get_ctx(list), isl_error_invalid, |
| 9415 | "expecting non-empty list", goto error); |
| 9416 | |
| 9417 | set = isl_set_list_get_set(list, 0); |
| 9418 | for (i = 1; i < n; ++i) { |
| 9419 | isl_set *set_i; |
| 9420 | |
| 9421 | set_i = isl_set_list_get_set(list, i); |
| 9422 | set = isl_set_union(set, set_i); |
| 9423 | } |
| 9424 | |
| 9425 | isl_set_list_free(list); |
| 9426 | return set; |
| 9427 | error: |
| 9428 | isl_set_list_free(list); |
| 9429 | return NULL; |
| 9430 | } |
| 9431 | |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 9432 | /* Return the Cartesian product of the basic sets in list (in the given order). |
| 9433 | */ |
| 9434 | __isl_give isl_basic_set *isl_basic_set_list_product( |
| 9435 | __isl_take struct isl_basic_set_list *list) |
| 9436 | { |
| 9437 | int i; |
| 9438 | unsigned dim; |
| 9439 | unsigned nparam; |
| 9440 | unsigned extra; |
| 9441 | unsigned n_eq; |
| 9442 | unsigned n_ineq; |
| 9443 | struct isl_basic_set *product = NULL; |
| 9444 | |
| 9445 | if (!list) |
| 9446 | goto error; |
| 9447 | isl_assert(list->ctx, list->n > 0, goto error); |
| 9448 | isl_assert(list->ctx, list->p[0], goto error); |
| 9449 | nparam = isl_basic_set_n_param(list->p[0]); |
| 9450 | dim = isl_basic_set_n_dim(list->p[0]); |
| 9451 | extra = list->p[0]->n_div; |
| 9452 | n_eq = list->p[0]->n_eq; |
| 9453 | n_ineq = list->p[0]->n_ineq; |
| 9454 | for (i = 1; i < list->n; ++i) { |
| 9455 | isl_assert(list->ctx, list->p[i], goto error); |
| 9456 | isl_assert(list->ctx, |
| 9457 | nparam == isl_basic_set_n_param(list->p[i]), goto error); |
| 9458 | dim += isl_basic_set_n_dim(list->p[i]); |
| 9459 | extra += list->p[i]->n_div; |
| 9460 | n_eq += list->p[i]->n_eq; |
| 9461 | n_ineq += list->p[i]->n_ineq; |
| 9462 | } |
| 9463 | product = isl_basic_set_alloc(list->ctx, nparam, dim, extra, |
| 9464 | n_eq, n_ineq); |
| 9465 | if (!product) |
| 9466 | goto error; |
| 9467 | dim = 0; |
| 9468 | for (i = 0; i < list->n; ++i) { |
| 9469 | isl_basic_set_add_constraints(product, |
| 9470 | isl_basic_set_copy(list->p[i]), dim); |
| 9471 | dim += isl_basic_set_n_dim(list->p[i]); |
| 9472 | } |
| 9473 | isl_basic_set_list_free(list); |
| 9474 | return product; |
| 9475 | error: |
| 9476 | isl_basic_set_free(product); |
| 9477 | isl_basic_set_list_free(list); |
| 9478 | return NULL; |
| 9479 | } |
| 9480 | |
| 9481 | struct isl_basic_map *isl_basic_map_product( |
| 9482 | struct isl_basic_map *bmap1, struct isl_basic_map *bmap2) |
| 9483 | { |
| 9484 | isl_space *dim_result = NULL; |
| 9485 | struct isl_basic_map *bmap; |
| 9486 | unsigned in1, in2, out1, out2, nparam, total, pos; |
| 9487 | struct isl_dim_map *dim_map1, *dim_map2; |
| 9488 | |
| 9489 | if (!bmap1 || !bmap2) |
| 9490 | goto error; |
| 9491 | |
| 9492 | isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param, |
| 9493 | bmap2->dim, isl_dim_param), goto error); |
| 9494 | dim_result = isl_space_product(isl_space_copy(bmap1->dim), |
| 9495 | isl_space_copy(bmap2->dim)); |
| 9496 | |
| 9497 | in1 = isl_basic_map_n_in(bmap1); |
| 9498 | in2 = isl_basic_map_n_in(bmap2); |
| 9499 | out1 = isl_basic_map_n_out(bmap1); |
| 9500 | out2 = isl_basic_map_n_out(bmap2); |
| 9501 | nparam = isl_basic_map_n_param(bmap1); |
| 9502 | |
| 9503 | total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div; |
| 9504 | dim_map1 = isl_dim_map_alloc(bmap1->ctx, total); |
| 9505 | dim_map2 = isl_dim_map_alloc(bmap1->ctx, total); |
| 9506 | isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0); |
| 9507 | isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0); |
| 9508 | isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam); |
| 9509 | isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1); |
| 9510 | isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2); |
| 9511 | isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1); |
| 9512 | isl_dim_map_div(dim_map1, bmap1, pos += out2); |
| 9513 | isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div); |
| 9514 | |
| 9515 | bmap = isl_basic_map_alloc_space(dim_result, |
| 9516 | bmap1->n_div + bmap2->n_div, |
| 9517 | bmap1->n_eq + bmap2->n_eq, |
| 9518 | bmap1->n_ineq + bmap2->n_ineq); |
| 9519 | bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1); |
| 9520 | bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2); |
| 9521 | bmap = isl_basic_map_simplify(bmap); |
| 9522 | return isl_basic_map_finalize(bmap); |
| 9523 | error: |
| 9524 | isl_basic_map_free(bmap1); |
| 9525 | isl_basic_map_free(bmap2); |
| 9526 | return NULL; |
| 9527 | } |
| 9528 | |
| 9529 | __isl_give isl_basic_map *isl_basic_map_flat_product( |
| 9530 | __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2) |
| 9531 | { |
| 9532 | isl_basic_map *prod; |
| 9533 | |
| 9534 | prod = isl_basic_map_product(bmap1, bmap2); |
| 9535 | prod = isl_basic_map_flatten(prod); |
| 9536 | return prod; |
| 9537 | } |
| 9538 | |
| 9539 | __isl_give isl_basic_set *isl_basic_set_flat_product( |
| 9540 | __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2) |
| 9541 | { |
| 9542 | return isl_basic_map_flat_range_product(bset1, bset2); |
| 9543 | } |
| 9544 | |
| 9545 | __isl_give isl_basic_map *isl_basic_map_domain_product( |
| 9546 | __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2) |
| 9547 | { |
| 9548 | isl_space *space_result = NULL; |
| 9549 | isl_basic_map *bmap; |
| 9550 | unsigned in1, in2, out, nparam, total, pos; |
| 9551 | struct isl_dim_map *dim_map1, *dim_map2; |
| 9552 | |
| 9553 | if (!bmap1 || !bmap2) |
| 9554 | goto error; |
| 9555 | |
| 9556 | space_result = isl_space_domain_product(isl_space_copy(bmap1->dim), |
| 9557 | isl_space_copy(bmap2->dim)); |
| 9558 | |
| 9559 | in1 = isl_basic_map_dim(bmap1, isl_dim_in); |
| 9560 | in2 = isl_basic_map_dim(bmap2, isl_dim_in); |
| 9561 | out = isl_basic_map_dim(bmap1, isl_dim_out); |
| 9562 | nparam = isl_basic_map_dim(bmap1, isl_dim_param); |
| 9563 | |
| 9564 | total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div; |
| 9565 | dim_map1 = isl_dim_map_alloc(bmap1->ctx, total); |
| 9566 | dim_map2 = isl_dim_map_alloc(bmap1->ctx, total); |
| 9567 | isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0); |
| 9568 | isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0); |
| 9569 | isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam); |
| 9570 | isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1); |
| 9571 | isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2); |
| 9572 | isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos); |
| 9573 | isl_dim_map_div(dim_map1, bmap1, pos += out); |
| 9574 | isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div); |
| 9575 | |
| 9576 | bmap = isl_basic_map_alloc_space(space_result, |
| 9577 | bmap1->n_div + bmap2->n_div, |
| 9578 | bmap1->n_eq + bmap2->n_eq, |
| 9579 | bmap1->n_ineq + bmap2->n_ineq); |
| 9580 | bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1); |
| 9581 | bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2); |
| 9582 | bmap = isl_basic_map_simplify(bmap); |
| 9583 | return isl_basic_map_finalize(bmap); |
| 9584 | error: |
| 9585 | isl_basic_map_free(bmap1); |
| 9586 | isl_basic_map_free(bmap2); |
| 9587 | return NULL; |
| 9588 | } |
| 9589 | |
| 9590 | __isl_give isl_basic_map *isl_basic_map_range_product( |
| 9591 | __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2) |
| 9592 | { |
| 9593 | isl_space *dim_result = NULL; |
| 9594 | isl_basic_map *bmap; |
| 9595 | unsigned in, out1, out2, nparam, total, pos; |
| 9596 | struct isl_dim_map *dim_map1, *dim_map2; |
| 9597 | |
| 9598 | if (!bmap1 || !bmap2) |
| 9599 | goto error; |
| 9600 | |
| 9601 | if (!isl_space_match(bmap1->dim, isl_dim_param, |
| 9602 | bmap2->dim, isl_dim_param)) |
| 9603 | isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid, |
| 9604 | "parameters don't match", goto error); |
| 9605 | |
| 9606 | dim_result = isl_space_range_product(isl_space_copy(bmap1->dim), |
| 9607 | isl_space_copy(bmap2->dim)); |
| 9608 | |
| 9609 | in = isl_basic_map_dim(bmap1, isl_dim_in); |
| 9610 | out1 = isl_basic_map_n_out(bmap1); |
| 9611 | out2 = isl_basic_map_n_out(bmap2); |
| 9612 | nparam = isl_basic_map_n_param(bmap1); |
| 9613 | |
| 9614 | total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div; |
| 9615 | dim_map1 = isl_dim_map_alloc(bmap1->ctx, total); |
| 9616 | dim_map2 = isl_dim_map_alloc(bmap1->ctx, total); |
| 9617 | isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0); |
| 9618 | isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0); |
| 9619 | isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam); |
| 9620 | isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos); |
| 9621 | isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in); |
| 9622 | isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1); |
| 9623 | isl_dim_map_div(dim_map1, bmap1, pos += out2); |
| 9624 | isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div); |
| 9625 | |
| 9626 | bmap = isl_basic_map_alloc_space(dim_result, |
| 9627 | bmap1->n_div + bmap2->n_div, |
| 9628 | bmap1->n_eq + bmap2->n_eq, |
| 9629 | bmap1->n_ineq + bmap2->n_ineq); |
| 9630 | bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1); |
| 9631 | bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2); |
| 9632 | bmap = isl_basic_map_simplify(bmap); |
| 9633 | return isl_basic_map_finalize(bmap); |
| 9634 | error: |
| 9635 | isl_basic_map_free(bmap1); |
| 9636 | isl_basic_map_free(bmap2); |
| 9637 | return NULL; |
| 9638 | } |
| 9639 | |
| 9640 | __isl_give isl_basic_map *isl_basic_map_flat_range_product( |
| 9641 | __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2) |
| 9642 | { |
| 9643 | isl_basic_map *prod; |
| 9644 | |
| 9645 | prod = isl_basic_map_range_product(bmap1, bmap2); |
| 9646 | prod = isl_basic_map_flatten_range(prod); |
| 9647 | return prod; |
| 9648 | } |
| 9649 | |
| 9650 | /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2" |
| 9651 | * and collect the results. |
| 9652 | * The result live in the space obtained by calling "space_product" |
| 9653 | * on the spaces of "map1" and "map2". |
| 9654 | * If "remove_duplicates" is set then the result may contain duplicates |
| 9655 | * (even if the inputs do not) and so we try and remove the obvious |
| 9656 | * duplicates. |
| 9657 | */ |
| 9658 | static __isl_give isl_map *map_product(__isl_take isl_map *map1, |
| 9659 | __isl_take isl_map *map2, |
| 9660 | __isl_give isl_space *(*space_product)(__isl_take isl_space *left, |
| 9661 | __isl_take isl_space *right), |
| 9662 | __isl_give isl_basic_map *(*basic_map_product)( |
| 9663 | __isl_take isl_basic_map *left, |
| 9664 | __isl_take isl_basic_map *right), |
| 9665 | int remove_duplicates) |
| 9666 | { |
| 9667 | unsigned flags = 0; |
| 9668 | struct isl_map *result; |
| 9669 | int i, j; |
| 9670 | |
| 9671 | if (!map1 || !map2) |
| 9672 | goto error; |
| 9673 | |
| 9674 | isl_assert(map1->ctx, isl_space_match(map1->dim, isl_dim_param, |
| 9675 | map2->dim, isl_dim_param), goto error); |
| 9676 | |
| 9677 | if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) && |
| 9678 | ISL_F_ISSET(map2, ISL_MAP_DISJOINT)) |
| 9679 | ISL_FL_SET(flags, ISL_MAP_DISJOINT); |
| 9680 | |
| 9681 | result = isl_map_alloc_space(space_product(isl_space_copy(map1->dim), |
| 9682 | isl_space_copy(map2->dim)), |
| 9683 | map1->n * map2->n, flags); |
| 9684 | if (!result) |
| 9685 | goto error; |
| 9686 | for (i = 0; i < map1->n; ++i) |
| 9687 | for (j = 0; j < map2->n; ++j) { |
| 9688 | struct isl_basic_map *part; |
| 9689 | part = basic_map_product(isl_basic_map_copy(map1->p[i]), |
| 9690 | isl_basic_map_copy(map2->p[j])); |
| 9691 | if (isl_basic_map_is_empty(part)) |
| 9692 | isl_basic_map_free(part); |
| 9693 | else |
| 9694 | result = isl_map_add_basic_map(result, part); |
| 9695 | if (!result) |
| 9696 | goto error; |
| 9697 | } |
| 9698 | if (remove_duplicates) |
| 9699 | result = isl_map_remove_obvious_duplicates(result); |
| 9700 | isl_map_free(map1); |
| 9701 | isl_map_free(map2); |
| 9702 | return result; |
| 9703 | error: |
| 9704 | isl_map_free(map1); |
| 9705 | isl_map_free(map2); |
| 9706 | return NULL; |
| 9707 | } |
| 9708 | |
| 9709 | /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D] |
| 9710 | */ |
| 9711 | static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1, |
| 9712 | __isl_take isl_map *map2) |
| 9713 | { |
| 9714 | return map_product(map1, map2, &isl_space_product, |
| 9715 | &isl_basic_map_product, 0); |
| 9716 | } |
| 9717 | |
| 9718 | __isl_give isl_map *isl_map_product(__isl_take isl_map *map1, |
| 9719 | __isl_take isl_map *map2) |
| 9720 | { |
| 9721 | return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned); |
| 9722 | } |
| 9723 | |
| 9724 | /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D) |
| 9725 | */ |
| 9726 | __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1, |
| 9727 | __isl_take isl_map *map2) |
| 9728 | { |
| 9729 | isl_map *prod; |
| 9730 | |
| 9731 | prod = isl_map_product(map1, map2); |
| 9732 | prod = isl_map_flatten(prod); |
| 9733 | return prod; |
| 9734 | } |
| 9735 | |
| 9736 | /* Given two set A and B, construct its Cartesian product A x B. |
| 9737 | */ |
| 9738 | struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2) |
| 9739 | { |
| 9740 | return isl_map_range_product(set1, set2); |
| 9741 | } |
| 9742 | |
| 9743 | __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1, |
| 9744 | __isl_take isl_set *set2) |
| 9745 | { |
| 9746 | return isl_map_flat_range_product(set1, set2); |
| 9747 | } |
| 9748 | |
| 9749 | /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D) |
| 9750 | */ |
| 9751 | static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1, |
| 9752 | __isl_take isl_map *map2) |
| 9753 | { |
| 9754 | return map_product(map1, map2, &isl_space_domain_product, |
| 9755 | &isl_basic_map_domain_product, 1); |
| 9756 | } |
| 9757 | |
| 9758 | /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D] |
| 9759 | */ |
| 9760 | static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1, |
| 9761 | __isl_take isl_map *map2) |
| 9762 | { |
| 9763 | return map_product(map1, map2, &isl_space_range_product, |
| 9764 | &isl_basic_map_range_product, 1); |
| 9765 | } |
| 9766 | |
| 9767 | __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1, |
| 9768 | __isl_take isl_map *map2) |
| 9769 | { |
| 9770 | return isl_map_align_params_map_map_and(map1, map2, |
| 9771 | &map_domain_product_aligned); |
| 9772 | } |
| 9773 | |
| 9774 | __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1, |
| 9775 | __isl_take isl_map *map2) |
| 9776 | { |
| 9777 | return isl_map_align_params_map_map_and(map1, map2, |
| 9778 | &map_range_product_aligned); |
| 9779 | } |
| 9780 | |
| 9781 | /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C. |
| 9782 | */ |
| 9783 | __isl_give isl_map *isl_map_factor_domain(__isl_take isl_map *map) |
| 9784 | { |
| 9785 | isl_space *space; |
| 9786 | int total1, keep1, total2, keep2; |
| 9787 | |
| 9788 | if (!map) |
| 9789 | return NULL; |
| 9790 | if (!isl_space_domain_is_wrapping(map->dim) || |
| 9791 | !isl_space_range_is_wrapping(map->dim)) |
| 9792 | isl_die(isl_map_get_ctx(map), isl_error_invalid, |
| 9793 | "not a product", return isl_map_free(map)); |
| 9794 | |
| 9795 | space = isl_map_get_space(map); |
| 9796 | total1 = isl_space_dim(space, isl_dim_in); |
| 9797 | total2 = isl_space_dim(space, isl_dim_out); |
| 9798 | space = isl_space_factor_domain(space); |
| 9799 | keep1 = isl_space_dim(space, isl_dim_in); |
| 9800 | keep2 = isl_space_dim(space, isl_dim_out); |
| 9801 | map = isl_map_project_out(map, isl_dim_in, keep1, total1 - keep1); |
| 9802 | map = isl_map_project_out(map, isl_dim_out, keep2, total2 - keep2); |
| 9803 | map = isl_map_reset_space(map, space); |
| 9804 | |
| 9805 | return map; |
| 9806 | } |
| 9807 | |
| 9808 | /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D. |
| 9809 | */ |
| 9810 | __isl_give isl_map *isl_map_factor_range(__isl_take isl_map *map) |
| 9811 | { |
| 9812 | isl_space *space; |
| 9813 | int total1, keep1, total2, keep2; |
| 9814 | |
| 9815 | if (!map) |
| 9816 | return NULL; |
| 9817 | if (!isl_space_domain_is_wrapping(map->dim) || |
| 9818 | !isl_space_range_is_wrapping(map->dim)) |
| 9819 | isl_die(isl_map_get_ctx(map), isl_error_invalid, |
| 9820 | "not a product", return isl_map_free(map)); |
| 9821 | |
| 9822 | space = isl_map_get_space(map); |
| 9823 | total1 = isl_space_dim(space, isl_dim_in); |
| 9824 | total2 = isl_space_dim(space, isl_dim_out); |
| 9825 | space = isl_space_factor_range(space); |
| 9826 | keep1 = isl_space_dim(space, isl_dim_in); |
| 9827 | keep2 = isl_space_dim(space, isl_dim_out); |
| 9828 | map = isl_map_project_out(map, isl_dim_in, 0, total1 - keep1); |
| 9829 | map = isl_map_project_out(map, isl_dim_out, 0, total2 - keep2); |
| 9830 | map = isl_map_reset_space(map, space); |
| 9831 | |
| 9832 | return map; |
| 9833 | } |
| 9834 | |
| 9835 | /* Given a map of the form [A -> B] -> C, return the map A -> C. |
| 9836 | */ |
| 9837 | __isl_give isl_map *isl_map_domain_factor_domain(__isl_take isl_map *map) |
| 9838 | { |
| 9839 | isl_space *space; |
| 9840 | int total, keep; |
| 9841 | |
| 9842 | if (!map) |
| 9843 | return NULL; |
| 9844 | if (!isl_space_domain_is_wrapping(map->dim)) |
| 9845 | isl_die(isl_map_get_ctx(map), isl_error_invalid, |
| 9846 | "domain is not a product", return isl_map_free(map)); |
| 9847 | |
| 9848 | space = isl_map_get_space(map); |
| 9849 | total = isl_space_dim(space, isl_dim_in); |
| 9850 | space = isl_space_domain_factor_domain(space); |
| 9851 | keep = isl_space_dim(space, isl_dim_in); |
| 9852 | map = isl_map_project_out(map, isl_dim_in, keep, total - keep); |
| 9853 | map = isl_map_reset_space(map, space); |
| 9854 | |
| 9855 | return map; |
| 9856 | } |
| 9857 | |
| 9858 | /* Given a map of the form [A -> B] -> C, return the map B -> C. |
| 9859 | */ |
| 9860 | __isl_give isl_map *isl_map_domain_factor_range(__isl_take isl_map *map) |
| 9861 | { |
| 9862 | isl_space *space; |
| 9863 | int total, keep; |
| 9864 | |
| 9865 | if (!map) |
| 9866 | return NULL; |
| 9867 | if (!isl_space_domain_is_wrapping(map->dim)) |
| 9868 | isl_die(isl_map_get_ctx(map), isl_error_invalid, |
| 9869 | "domain is not a product", return isl_map_free(map)); |
| 9870 | |
| 9871 | space = isl_map_get_space(map); |
| 9872 | total = isl_space_dim(space, isl_dim_in); |
| 9873 | space = isl_space_domain_factor_range(space); |
| 9874 | keep = isl_space_dim(space, isl_dim_in); |
| 9875 | map = isl_map_project_out(map, isl_dim_in, 0, total - keep); |
| 9876 | map = isl_map_reset_space(map, space); |
| 9877 | |
| 9878 | return map; |
| 9879 | } |
| 9880 | |
| 9881 | /* Given a map A -> [B -> C], extract the map A -> B. |
| 9882 | */ |
| 9883 | __isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map) |
| 9884 | { |
| 9885 | isl_space *space; |
| 9886 | int total, keep; |
| 9887 | |
| 9888 | if (!map) |
| 9889 | return NULL; |
| 9890 | if (!isl_space_range_is_wrapping(map->dim)) |
| 9891 | isl_die(isl_map_get_ctx(map), isl_error_invalid, |
| 9892 | "range is not a product", return isl_map_free(map)); |
| 9893 | |
| 9894 | space = isl_map_get_space(map); |
| 9895 | total = isl_space_dim(space, isl_dim_out); |
| 9896 | space = isl_space_range_factor_domain(space); |
| 9897 | keep = isl_space_dim(space, isl_dim_out); |
| 9898 | map = isl_map_project_out(map, isl_dim_out, keep, total - keep); |
| 9899 | map = isl_map_reset_space(map, space); |
| 9900 | |
| 9901 | return map; |
| 9902 | } |
| 9903 | |
| 9904 | /* Given a map A -> [B -> C], extract the map A -> C. |
| 9905 | */ |
| 9906 | __isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map) |
| 9907 | { |
| 9908 | isl_space *space; |
| 9909 | int total, keep; |
| 9910 | |
| 9911 | if (!map) |
| 9912 | return NULL; |
| 9913 | if (!isl_space_range_is_wrapping(map->dim)) |
| 9914 | isl_die(isl_map_get_ctx(map), isl_error_invalid, |
| 9915 | "range is not a product", return isl_map_free(map)); |
| 9916 | |
| 9917 | space = isl_map_get_space(map); |
| 9918 | total = isl_space_dim(space, isl_dim_out); |
| 9919 | space = isl_space_range_factor_range(space); |
| 9920 | keep = isl_space_dim(space, isl_dim_out); |
| 9921 | map = isl_map_project_out(map, isl_dim_out, 0, total - keep); |
| 9922 | map = isl_map_reset_space(map, space); |
| 9923 | |
| 9924 | return map; |
| 9925 | } |
| 9926 | |
| 9927 | /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D) |
| 9928 | */ |
| 9929 | __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1, |
| 9930 | __isl_take isl_map *map2) |
| 9931 | { |
| 9932 | isl_map *prod; |
| 9933 | |
| 9934 | prod = isl_map_domain_product(map1, map2); |
| 9935 | prod = isl_map_flatten_domain(prod); |
| 9936 | return prod; |
| 9937 | } |
| 9938 | |
| 9939 | /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D) |
| 9940 | */ |
| 9941 | __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1, |
| 9942 | __isl_take isl_map *map2) |
| 9943 | { |
| 9944 | isl_map *prod; |
| 9945 | |
| 9946 | prod = isl_map_range_product(map1, map2); |
| 9947 | prod = isl_map_flatten_range(prod); |
| 9948 | return prod; |
| 9949 | } |
| 9950 | |
| 9951 | uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap) |
| 9952 | { |
| 9953 | int i; |
| 9954 | uint32_t hash = isl_hash_init(); |
| 9955 | unsigned total; |
| 9956 | |
| 9957 | if (!bmap) |
| 9958 | return 0; |
| 9959 | bmap = isl_basic_map_copy(bmap); |
| 9960 | bmap = isl_basic_map_normalize(bmap); |
| 9961 | if (!bmap) |
| 9962 | return 0; |
| 9963 | total = isl_basic_map_total_dim(bmap); |
| 9964 | isl_hash_byte(hash, bmap->n_eq & 0xFF); |
| 9965 | for (i = 0; i < bmap->n_eq; ++i) { |
| 9966 | uint32_t c_hash; |
| 9967 | c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total); |
| 9968 | isl_hash_hash(hash, c_hash); |
| 9969 | } |
| 9970 | isl_hash_byte(hash, bmap->n_ineq & 0xFF); |
| 9971 | for (i = 0; i < bmap->n_ineq; ++i) { |
| 9972 | uint32_t c_hash; |
| 9973 | c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total); |
| 9974 | isl_hash_hash(hash, c_hash); |
| 9975 | } |
| 9976 | isl_hash_byte(hash, bmap->n_div & 0xFF); |
| 9977 | for (i = 0; i < bmap->n_div; ++i) { |
| 9978 | uint32_t c_hash; |
| 9979 | if (isl_int_is_zero(bmap->div[i][0])) |
| 9980 | continue; |
| 9981 | isl_hash_byte(hash, i & 0xFF); |
| 9982 | c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total); |
| 9983 | isl_hash_hash(hash, c_hash); |
| 9984 | } |
| 9985 | isl_basic_map_free(bmap); |
| 9986 | return hash; |
| 9987 | } |
| 9988 | |
| 9989 | uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset) |
| 9990 | { |
| 9991 | return isl_basic_map_get_hash((isl_basic_map *)bset); |
| 9992 | } |
| 9993 | |
| 9994 | uint32_t isl_map_get_hash(__isl_keep isl_map *map) |
| 9995 | { |
| 9996 | int i; |
| 9997 | uint32_t hash; |
| 9998 | |
| 9999 | if (!map) |
| 10000 | return 0; |
| 10001 | map = isl_map_copy(map); |
| 10002 | map = isl_map_normalize(map); |
| 10003 | if (!map) |
| 10004 | return 0; |
| 10005 | |
| 10006 | hash = isl_hash_init(); |
| 10007 | for (i = 0; i < map->n; ++i) { |
| 10008 | uint32_t bmap_hash; |
| 10009 | bmap_hash = isl_basic_map_get_hash(map->p[i]); |
| 10010 | isl_hash_hash(hash, bmap_hash); |
| 10011 | } |
| 10012 | |
| 10013 | isl_map_free(map); |
| 10014 | |
| 10015 | return hash; |
| 10016 | } |
| 10017 | |
| 10018 | uint32_t isl_set_get_hash(__isl_keep isl_set *set) |
| 10019 | { |
| 10020 | return isl_map_get_hash((isl_map *)set); |
| 10021 | } |
| 10022 | |
| 10023 | /* Check if the value for dimension dim is completely determined |
| 10024 | * by the values of the other parameters and variables. |
| 10025 | * That is, check if dimension dim is involved in an equality. |
| 10026 | */ |
| 10027 | int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim) |
| 10028 | { |
| 10029 | int i; |
| 10030 | unsigned nparam; |
| 10031 | |
| 10032 | if (!bset) |
| 10033 | return -1; |
| 10034 | nparam = isl_basic_set_n_param(bset); |
| 10035 | for (i = 0; i < bset->n_eq; ++i) |
| 10036 | if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim])) |
| 10037 | return 1; |
| 10038 | return 0; |
| 10039 | } |
| 10040 | |
| 10041 | /* Check if the value for dimension dim is completely determined |
| 10042 | * by the values of the other parameters and variables. |
| 10043 | * That is, check if dimension dim is involved in an equality |
| 10044 | * for each of the subsets. |
| 10045 | */ |
| 10046 | int isl_set_dim_is_unique(struct isl_set *set, unsigned dim) |
| 10047 | { |
| 10048 | int i; |
| 10049 | |
| 10050 | if (!set) |
| 10051 | return -1; |
| 10052 | for (i = 0; i < set->n; ++i) { |
| 10053 | int unique; |
| 10054 | unique = isl_basic_set_dim_is_unique(set->p[i], dim); |
| 10055 | if (unique != 1) |
| 10056 | return unique; |
| 10057 | } |
| 10058 | return 1; |
| 10059 | } |
| 10060 | |
| 10061 | /* Return the number of basic maps in the (current) representation of "map". |
| 10062 | */ |
| 10063 | int isl_map_n_basic_map(__isl_keep isl_map *map) |
| 10064 | { |
| 10065 | return map ? map->n : 0; |
| 10066 | } |
| 10067 | |
| 10068 | int isl_set_n_basic_set(__isl_keep isl_set *set) |
| 10069 | { |
| 10070 | return set ? set->n : 0; |
| 10071 | } |
| 10072 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10073 | isl_stat isl_map_foreach_basic_map(__isl_keep isl_map *map, |
| 10074 | isl_stat (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10075 | { |
| 10076 | int i; |
| 10077 | |
| 10078 | if (!map) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10079 | return isl_stat_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10080 | |
| 10081 | for (i = 0; i < map->n; ++i) |
| 10082 | if (fn(isl_basic_map_copy(map->p[i]), user) < 0) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10083 | return isl_stat_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10084 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10085 | return isl_stat_ok; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10086 | } |
| 10087 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10088 | isl_stat isl_set_foreach_basic_set(__isl_keep isl_set *set, |
| 10089 | isl_stat (*fn)(__isl_take isl_basic_set *bset, void *user), void *user) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10090 | { |
| 10091 | int i; |
| 10092 | |
| 10093 | if (!set) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10094 | return isl_stat_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10095 | |
| 10096 | for (i = 0; i < set->n; ++i) |
| 10097 | if (fn(isl_basic_set_copy(set->p[i]), user) < 0) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10098 | return isl_stat_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10099 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10100 | return isl_stat_ok; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10101 | } |
| 10102 | |
Tobias Grosser | 4db5531 | 2015-06-30 08:22:14 +0000 | [diff] [blame] | 10103 | /* Return a list of basic sets, the union of which is equal to "set". |
| 10104 | */ |
| 10105 | __isl_give isl_basic_set_list *isl_set_get_basic_set_list( |
| 10106 | __isl_keep isl_set *set) |
| 10107 | { |
| 10108 | int i; |
| 10109 | isl_basic_set_list *list; |
| 10110 | |
| 10111 | if (!set) |
| 10112 | return NULL; |
| 10113 | |
| 10114 | list = isl_basic_set_list_alloc(isl_set_get_ctx(set), set->n); |
| 10115 | for (i = 0; i < set->n; ++i) { |
| 10116 | isl_basic_set *bset; |
| 10117 | |
| 10118 | bset = isl_basic_set_copy(set->p[i]); |
| 10119 | list = isl_basic_set_list_add(list, bset); |
| 10120 | } |
| 10121 | |
| 10122 | return list; |
| 10123 | } |
| 10124 | |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10125 | __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset) |
| 10126 | { |
| 10127 | isl_space *dim; |
| 10128 | |
| 10129 | if (!bset) |
| 10130 | return NULL; |
| 10131 | |
| 10132 | bset = isl_basic_set_cow(bset); |
| 10133 | if (!bset) |
| 10134 | return NULL; |
| 10135 | |
| 10136 | dim = isl_basic_set_get_space(bset); |
| 10137 | dim = isl_space_lift(dim, bset->n_div); |
| 10138 | if (!dim) |
| 10139 | goto error; |
| 10140 | isl_space_free(bset->dim); |
| 10141 | bset->dim = dim; |
| 10142 | bset->extra -= bset->n_div; |
| 10143 | bset->n_div = 0; |
| 10144 | |
| 10145 | bset = isl_basic_set_finalize(bset); |
| 10146 | |
| 10147 | return bset; |
| 10148 | error: |
| 10149 | isl_basic_set_free(bset); |
| 10150 | return NULL; |
| 10151 | } |
| 10152 | |
| 10153 | __isl_give isl_set *isl_set_lift(__isl_take isl_set *set) |
| 10154 | { |
| 10155 | int i; |
| 10156 | isl_space *dim; |
| 10157 | unsigned n_div; |
| 10158 | |
| 10159 | set = isl_set_align_divs(set); |
| 10160 | |
| 10161 | if (!set) |
| 10162 | return NULL; |
| 10163 | |
| 10164 | set = isl_set_cow(set); |
| 10165 | if (!set) |
| 10166 | return NULL; |
| 10167 | |
| 10168 | n_div = set->p[0]->n_div; |
| 10169 | dim = isl_set_get_space(set); |
| 10170 | dim = isl_space_lift(dim, n_div); |
| 10171 | if (!dim) |
| 10172 | goto error; |
| 10173 | isl_space_free(set->dim); |
| 10174 | set->dim = dim; |
| 10175 | |
| 10176 | for (i = 0; i < set->n; ++i) { |
| 10177 | set->p[i] = isl_basic_set_lift(set->p[i]); |
| 10178 | if (!set->p[i]) |
| 10179 | goto error; |
| 10180 | } |
| 10181 | |
| 10182 | return set; |
| 10183 | error: |
| 10184 | isl_set_free(set); |
| 10185 | return NULL; |
| 10186 | } |
| 10187 | |
| 10188 | __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set) |
| 10189 | { |
| 10190 | isl_space *dim; |
| 10191 | struct isl_basic_map *bmap; |
| 10192 | unsigned n_set; |
| 10193 | unsigned n_div; |
| 10194 | unsigned n_param; |
| 10195 | unsigned total; |
| 10196 | int i, k, l; |
| 10197 | |
| 10198 | set = isl_set_align_divs(set); |
| 10199 | |
| 10200 | if (!set) |
| 10201 | return NULL; |
| 10202 | |
| 10203 | dim = isl_set_get_space(set); |
| 10204 | if (set->n == 0 || set->p[0]->n_div == 0) { |
| 10205 | isl_set_free(set); |
| 10206 | return isl_map_identity(isl_space_map_from_set(dim)); |
| 10207 | } |
| 10208 | |
| 10209 | n_div = set->p[0]->n_div; |
| 10210 | dim = isl_space_map_from_set(dim); |
| 10211 | n_param = isl_space_dim(dim, isl_dim_param); |
| 10212 | n_set = isl_space_dim(dim, isl_dim_in); |
| 10213 | dim = isl_space_extend(dim, n_param, n_set, n_set + n_div); |
| 10214 | bmap = isl_basic_map_alloc_space(dim, 0, n_set, 2 * n_div); |
| 10215 | for (i = 0; i < n_set; ++i) |
| 10216 | bmap = var_equal(bmap, i); |
| 10217 | |
| 10218 | total = n_param + n_set + n_set + n_div; |
| 10219 | for (i = 0; i < n_div; ++i) { |
| 10220 | k = isl_basic_map_alloc_inequality(bmap); |
| 10221 | if (k < 0) |
| 10222 | goto error; |
| 10223 | isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param); |
| 10224 | isl_seq_clr(bmap->ineq[k]+1+n_param, n_set); |
| 10225 | isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set, |
| 10226 | set->p[0]->div[i]+1+1+n_param, n_set + n_div); |
| 10227 | isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i], |
| 10228 | set->p[0]->div[i][0]); |
| 10229 | |
| 10230 | l = isl_basic_map_alloc_inequality(bmap); |
| 10231 | if (l < 0) |
| 10232 | goto error; |
| 10233 | isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total); |
| 10234 | isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0], |
| 10235 | set->p[0]->div[i][0]); |
| 10236 | isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1); |
| 10237 | } |
| 10238 | |
| 10239 | isl_set_free(set); |
| 10240 | bmap = isl_basic_map_simplify(bmap); |
| 10241 | bmap = isl_basic_map_finalize(bmap); |
| 10242 | return isl_map_from_basic_map(bmap); |
| 10243 | error: |
| 10244 | isl_set_free(set); |
| 10245 | isl_basic_map_free(bmap); |
| 10246 | return NULL; |
| 10247 | } |
| 10248 | |
| 10249 | int isl_basic_set_size(__isl_keep isl_basic_set *bset) |
| 10250 | { |
| 10251 | unsigned dim; |
| 10252 | int size = 0; |
| 10253 | |
| 10254 | if (!bset) |
| 10255 | return -1; |
| 10256 | |
| 10257 | dim = isl_basic_set_total_dim(bset); |
| 10258 | size += bset->n_eq * (1 + dim); |
| 10259 | size += bset->n_ineq * (1 + dim); |
| 10260 | size += bset->n_div * (2 + dim); |
| 10261 | |
| 10262 | return size; |
| 10263 | } |
| 10264 | |
| 10265 | int isl_set_size(__isl_keep isl_set *set) |
| 10266 | { |
| 10267 | int i; |
| 10268 | int size = 0; |
| 10269 | |
| 10270 | if (!set) |
| 10271 | return -1; |
| 10272 | |
| 10273 | for (i = 0; i < set->n; ++i) |
| 10274 | size += isl_basic_set_size(set->p[i]); |
| 10275 | |
| 10276 | return size; |
| 10277 | } |
| 10278 | |
| 10279 | /* Check if there is any lower bound (if lower == 0) and/or upper |
| 10280 | * bound (if upper == 0) on the specified dim. |
| 10281 | */ |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10282 | static isl_bool basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap, |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10283 | enum isl_dim_type type, unsigned pos, int lower, int upper) |
| 10284 | { |
| 10285 | int i; |
| 10286 | |
| 10287 | if (!bmap) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10288 | return isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10289 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10290 | isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), |
| 10291 | return isl_bool_error); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10292 | |
| 10293 | pos += isl_basic_map_offset(bmap, type); |
| 10294 | |
| 10295 | for (i = 0; i < bmap->n_div; ++i) { |
| 10296 | if (isl_int_is_zero(bmap->div[i][0])) |
| 10297 | continue; |
| 10298 | if (!isl_int_is_zero(bmap->div[i][1 + pos])) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10299 | return isl_bool_true; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10300 | } |
| 10301 | |
| 10302 | for (i = 0; i < bmap->n_eq; ++i) |
| 10303 | if (!isl_int_is_zero(bmap->eq[i][pos])) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10304 | return isl_bool_true; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10305 | |
| 10306 | for (i = 0; i < bmap->n_ineq; ++i) { |
| 10307 | int sgn = isl_int_sgn(bmap->ineq[i][pos]); |
| 10308 | if (sgn > 0) |
| 10309 | lower = 1; |
| 10310 | if (sgn < 0) |
| 10311 | upper = 1; |
| 10312 | } |
| 10313 | |
| 10314 | return lower && upper; |
| 10315 | } |
| 10316 | |
| 10317 | int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap, |
| 10318 | enum isl_dim_type type, unsigned pos) |
| 10319 | { |
| 10320 | return basic_map_dim_is_bounded(bmap, type, pos, 0, 0); |
| 10321 | } |
| 10322 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10323 | isl_bool isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap, |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10324 | enum isl_dim_type type, unsigned pos) |
| 10325 | { |
| 10326 | return basic_map_dim_is_bounded(bmap, type, pos, 0, 1); |
| 10327 | } |
| 10328 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10329 | isl_bool isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap, |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10330 | enum isl_dim_type type, unsigned pos) |
| 10331 | { |
| 10332 | return basic_map_dim_is_bounded(bmap, type, pos, 1, 0); |
| 10333 | } |
| 10334 | |
| 10335 | int isl_map_dim_is_bounded(__isl_keep isl_map *map, |
| 10336 | enum isl_dim_type type, unsigned pos) |
| 10337 | { |
| 10338 | int i; |
| 10339 | |
| 10340 | if (!map) |
| 10341 | return -1; |
| 10342 | |
| 10343 | for (i = 0; i < map->n; ++i) { |
| 10344 | int bounded; |
| 10345 | bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos); |
| 10346 | if (bounded < 0 || !bounded) |
| 10347 | return bounded; |
| 10348 | } |
| 10349 | |
| 10350 | return 1; |
| 10351 | } |
| 10352 | |
| 10353 | /* Return 1 if the specified dim is involved in both an upper bound |
| 10354 | * and a lower bound. |
| 10355 | */ |
| 10356 | int isl_set_dim_is_bounded(__isl_keep isl_set *set, |
| 10357 | enum isl_dim_type type, unsigned pos) |
| 10358 | { |
| 10359 | return isl_map_dim_is_bounded((isl_map *)set, type, pos); |
| 10360 | } |
| 10361 | |
| 10362 | /* Does "map" have a bound (according to "fn") for any of its basic maps? |
| 10363 | */ |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10364 | static isl_bool has_any_bound(__isl_keep isl_map *map, |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10365 | enum isl_dim_type type, unsigned pos, |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10366 | isl_bool (*fn)(__isl_keep isl_basic_map *bmap, |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10367 | enum isl_dim_type type, unsigned pos)) |
| 10368 | { |
| 10369 | int i; |
| 10370 | |
| 10371 | if (!map) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10372 | return isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10373 | |
| 10374 | for (i = 0; i < map->n; ++i) { |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10375 | isl_bool bounded; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10376 | bounded = fn(map->p[i], type, pos); |
| 10377 | if (bounded < 0 || bounded) |
| 10378 | return bounded; |
| 10379 | } |
| 10380 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10381 | return isl_bool_false; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10382 | } |
| 10383 | |
| 10384 | /* Return 1 if the specified dim is involved in any lower bound. |
| 10385 | */ |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10386 | isl_bool isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set, |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10387 | enum isl_dim_type type, unsigned pos) |
| 10388 | { |
| 10389 | return has_any_bound(set, type, pos, |
| 10390 | &isl_basic_map_dim_has_lower_bound); |
| 10391 | } |
| 10392 | |
| 10393 | /* Return 1 if the specified dim is involved in any upper bound. |
| 10394 | */ |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10395 | isl_bool isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set, |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10396 | enum isl_dim_type type, unsigned pos) |
| 10397 | { |
| 10398 | return has_any_bound(set, type, pos, |
| 10399 | &isl_basic_map_dim_has_upper_bound); |
| 10400 | } |
| 10401 | |
| 10402 | /* Does "map" have a bound (according to "fn") for all of its basic maps? |
| 10403 | */ |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10404 | static isl_bool has_bound(__isl_keep isl_map *map, |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10405 | enum isl_dim_type type, unsigned pos, |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10406 | isl_bool (*fn)(__isl_keep isl_basic_map *bmap, |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10407 | enum isl_dim_type type, unsigned pos)) |
| 10408 | { |
| 10409 | int i; |
| 10410 | |
| 10411 | if (!map) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10412 | return isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10413 | |
| 10414 | for (i = 0; i < map->n; ++i) { |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10415 | isl_bool bounded; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10416 | bounded = fn(map->p[i], type, pos); |
| 10417 | if (bounded < 0 || !bounded) |
| 10418 | return bounded; |
| 10419 | } |
| 10420 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10421 | return isl_bool_true; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10422 | } |
| 10423 | |
| 10424 | /* Return 1 if the specified dim has a lower bound (in each of its basic sets). |
| 10425 | */ |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10426 | isl_bool isl_set_dim_has_lower_bound(__isl_keep isl_set *set, |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10427 | enum isl_dim_type type, unsigned pos) |
| 10428 | { |
| 10429 | return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound); |
| 10430 | } |
| 10431 | |
| 10432 | /* Return 1 if the specified dim has an upper bound (in each of its basic sets). |
| 10433 | */ |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10434 | isl_bool isl_set_dim_has_upper_bound(__isl_keep isl_set *set, |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10435 | enum isl_dim_type type, unsigned pos) |
| 10436 | { |
| 10437 | return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound); |
| 10438 | } |
| 10439 | |
| 10440 | /* For each of the "n" variables starting at "first", determine |
| 10441 | * the sign of the variable and put the results in the first "n" |
| 10442 | * elements of the array "signs". |
| 10443 | * Sign |
| 10444 | * 1 means that the variable is non-negative |
| 10445 | * -1 means that the variable is non-positive |
| 10446 | * 0 means the variable attains both positive and negative values. |
| 10447 | */ |
| 10448 | int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset, |
| 10449 | unsigned first, unsigned n, int *signs) |
| 10450 | { |
| 10451 | isl_vec *bound = NULL; |
| 10452 | struct isl_tab *tab = NULL; |
| 10453 | struct isl_tab_undo *snap; |
| 10454 | int i; |
| 10455 | |
| 10456 | if (!bset || !signs) |
| 10457 | return -1; |
| 10458 | |
| 10459 | bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset)); |
| 10460 | tab = isl_tab_from_basic_set(bset, 0); |
| 10461 | if (!bound || !tab) |
| 10462 | goto error; |
| 10463 | |
| 10464 | isl_seq_clr(bound->el, bound->size); |
| 10465 | isl_int_set_si(bound->el[0], -1); |
| 10466 | |
| 10467 | snap = isl_tab_snap(tab); |
| 10468 | for (i = 0; i < n; ++i) { |
| 10469 | int empty; |
| 10470 | |
| 10471 | isl_int_set_si(bound->el[1 + first + i], -1); |
| 10472 | if (isl_tab_add_ineq(tab, bound->el) < 0) |
| 10473 | goto error; |
| 10474 | empty = tab->empty; |
| 10475 | isl_int_set_si(bound->el[1 + first + i], 0); |
| 10476 | if (isl_tab_rollback(tab, snap) < 0) |
| 10477 | goto error; |
| 10478 | |
| 10479 | if (empty) { |
| 10480 | signs[i] = 1; |
| 10481 | continue; |
| 10482 | } |
| 10483 | |
| 10484 | isl_int_set_si(bound->el[1 + first + i], 1); |
| 10485 | if (isl_tab_add_ineq(tab, bound->el) < 0) |
| 10486 | goto error; |
| 10487 | empty = tab->empty; |
| 10488 | isl_int_set_si(bound->el[1 + first + i], 0); |
| 10489 | if (isl_tab_rollback(tab, snap) < 0) |
| 10490 | goto error; |
| 10491 | |
| 10492 | signs[i] = empty ? -1 : 0; |
| 10493 | } |
| 10494 | |
| 10495 | isl_tab_free(tab); |
| 10496 | isl_vec_free(bound); |
| 10497 | return 0; |
| 10498 | error: |
| 10499 | isl_tab_free(tab); |
| 10500 | isl_vec_free(bound); |
| 10501 | return -1; |
| 10502 | } |
| 10503 | |
| 10504 | int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset, |
| 10505 | enum isl_dim_type type, unsigned first, unsigned n, int *signs) |
| 10506 | { |
| 10507 | if (!bset || !signs) |
| 10508 | return -1; |
| 10509 | isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type), |
| 10510 | return -1); |
| 10511 | |
| 10512 | first += pos(bset->dim, type) - 1; |
| 10513 | return isl_basic_set_vars_get_sign(bset, first, n, signs); |
| 10514 | } |
| 10515 | |
| 10516 | /* Is it possible for the integer division "div" to depend (possibly |
| 10517 | * indirectly) on any output dimensions? |
| 10518 | * |
| 10519 | * If the div is undefined, then we conservatively assume that it |
| 10520 | * may depend on them. |
| 10521 | * Otherwise, we check if it actually depends on them or on any integer |
| 10522 | * divisions that may depend on them. |
| 10523 | */ |
| 10524 | static int div_may_involve_output(__isl_keep isl_basic_map *bmap, int div) |
| 10525 | { |
| 10526 | int i; |
| 10527 | unsigned n_out, o_out; |
| 10528 | unsigned n_div, o_div; |
| 10529 | |
| 10530 | if (isl_int_is_zero(bmap->div[div][0])) |
| 10531 | return 1; |
| 10532 | |
| 10533 | n_out = isl_basic_map_dim(bmap, isl_dim_out); |
| 10534 | o_out = isl_basic_map_offset(bmap, isl_dim_out); |
| 10535 | |
| 10536 | if (isl_seq_first_non_zero(bmap->div[div] + 1 + o_out, n_out) != -1) |
| 10537 | return 1; |
| 10538 | |
| 10539 | n_div = isl_basic_map_dim(bmap, isl_dim_div); |
| 10540 | o_div = isl_basic_map_offset(bmap, isl_dim_div); |
| 10541 | |
| 10542 | for (i = 0; i < n_div; ++i) { |
| 10543 | if (isl_int_is_zero(bmap->div[div][1 + o_div + i])) |
| 10544 | continue; |
| 10545 | if (div_may_involve_output(bmap, i)) |
| 10546 | return 1; |
| 10547 | } |
| 10548 | |
| 10549 | return 0; |
| 10550 | } |
| 10551 | |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 10552 | /* Return the first integer division of "bmap" in the range |
| 10553 | * [first, first + n[ that may depend on any output dimensions and |
| 10554 | * that has a non-zero coefficient in "c" (where the first coefficient |
| 10555 | * in "c" corresponds to integer division "first"). |
| 10556 | */ |
| 10557 | static int first_div_may_involve_output(__isl_keep isl_basic_map *bmap, |
| 10558 | isl_int *c, int first, int n) |
| 10559 | { |
| 10560 | int k; |
| 10561 | |
| 10562 | if (!bmap) |
| 10563 | return -1; |
| 10564 | |
| 10565 | for (k = first; k < first + n; ++k) { |
| 10566 | if (isl_int_is_zero(c[k])) |
| 10567 | continue; |
| 10568 | if (div_may_involve_output(bmap, k)) |
| 10569 | return k; |
| 10570 | } |
| 10571 | |
| 10572 | return first + n; |
| 10573 | } |
| 10574 | |
| 10575 | /* Look for a pair of inequality constraints in "bmap" of the form |
| 10576 | * |
| 10577 | * -l + i >= 0 or i >= l |
| 10578 | * and |
| 10579 | * n + l - i >= 0 or i <= l + n |
| 10580 | * |
| 10581 | * with n < "m" and i the output dimension at position "pos". |
| 10582 | * (Note that n >= 0 as otherwise the two constraints would conflict.) |
| 10583 | * Furthermore, "l" is only allowed to involve parameters, input dimensions |
| 10584 | * and earlier output dimensions, as well as integer divisions that do |
| 10585 | * not involve any of the output dimensions. |
| 10586 | * |
| 10587 | * Return the index of the first inequality constraint or bmap->n_ineq |
| 10588 | * if no such pair can be found. |
| 10589 | */ |
| 10590 | static int find_modulo_constraint_pair(__isl_keep isl_basic_map *bmap, |
| 10591 | int pos, isl_int m) |
| 10592 | { |
| 10593 | int i, j; |
| 10594 | isl_ctx *ctx; |
| 10595 | unsigned total; |
| 10596 | unsigned n_div, o_div; |
| 10597 | unsigned n_out, o_out; |
| 10598 | int less; |
| 10599 | |
| 10600 | if (!bmap) |
| 10601 | return -1; |
| 10602 | |
| 10603 | ctx = isl_basic_map_get_ctx(bmap); |
| 10604 | total = isl_basic_map_total_dim(bmap); |
| 10605 | n_out = isl_basic_map_dim(bmap, isl_dim_out); |
| 10606 | o_out = isl_basic_map_offset(bmap, isl_dim_out); |
| 10607 | n_div = isl_basic_map_dim(bmap, isl_dim_div); |
| 10608 | o_div = isl_basic_map_offset(bmap, isl_dim_div); |
| 10609 | for (i = 0; i < bmap->n_ineq; ++i) { |
| 10610 | if (!isl_int_abs_eq(bmap->ineq[i][o_out + pos], ctx->one)) |
| 10611 | continue; |
| 10612 | if (isl_seq_first_non_zero(bmap->ineq[i] + o_out + pos + 1, |
| 10613 | n_out - (pos + 1)) != -1) |
| 10614 | continue; |
| 10615 | if (first_div_may_involve_output(bmap, bmap->ineq[i] + o_div, |
| 10616 | 0, n_div) < n_div) |
| 10617 | continue; |
| 10618 | for (j = i + 1; j < bmap->n_ineq; ++j) { |
| 10619 | if (!isl_int_abs_eq(bmap->ineq[j][o_out + pos], |
| 10620 | ctx->one)) |
| 10621 | continue; |
| 10622 | if (!isl_seq_is_neg(bmap->ineq[i] + 1, |
| 10623 | bmap->ineq[j] + 1, total)) |
| 10624 | continue; |
| 10625 | break; |
| 10626 | } |
| 10627 | if (j >= bmap->n_ineq) |
| 10628 | continue; |
| 10629 | isl_int_add(bmap->ineq[i][0], |
| 10630 | bmap->ineq[i][0], bmap->ineq[j][0]); |
| 10631 | less = isl_int_abs_lt(bmap->ineq[i][0], m); |
| 10632 | isl_int_sub(bmap->ineq[i][0], |
| 10633 | bmap->ineq[i][0], bmap->ineq[j][0]); |
| 10634 | if (!less) |
| 10635 | continue; |
| 10636 | if (isl_int_is_one(bmap->ineq[i][o_out + pos])) |
| 10637 | return i; |
| 10638 | else |
| 10639 | return j; |
| 10640 | } |
| 10641 | |
| 10642 | return bmap->n_ineq; |
| 10643 | } |
| 10644 | |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10645 | /* Return the index of the equality of "bmap" that defines |
| 10646 | * the output dimension "pos" in terms of earlier dimensions. |
| 10647 | * The equality may also involve integer divisions, as long |
| 10648 | * as those integer divisions are defined in terms of |
| 10649 | * parameters or input dimensions. |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 10650 | * In this case, *div is set to the number of integer divisions and |
| 10651 | * *ineq is set to the number of inequality constraints (provided |
| 10652 | * div and ineq are not NULL). |
| 10653 | * |
| 10654 | * The equality may also involve a single integer division involving |
| 10655 | * the output dimensions (typically only output dimension "pos") as |
| 10656 | * long as the coefficient of output dimension "pos" is 1 or -1 and |
| 10657 | * there is a pair of constraints i >= l and i <= l + n, with i referring |
| 10658 | * to output dimension "pos", l an expression involving only earlier |
| 10659 | * dimensions and n smaller than the coefficient of the integer division |
| 10660 | * in the equality. In this case, the output dimension can be defined |
| 10661 | * in terms of a modulo expression that does not involve the integer division. |
| 10662 | * *div is then set to this single integer division and |
| 10663 | * *ineq is set to the index of constraint i >= l. |
| 10664 | * |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10665 | * Return bmap->n_eq if there is no such equality. |
| 10666 | * Return -1 on error. |
| 10667 | */ |
| 10668 | int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map *bmap, |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 10669 | int pos, int *div, int *ineq) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10670 | { |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 10671 | int j, k, l; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10672 | unsigned n_out, o_out; |
| 10673 | unsigned n_div, o_div; |
| 10674 | |
| 10675 | if (!bmap) |
| 10676 | return -1; |
| 10677 | |
| 10678 | n_out = isl_basic_map_dim(bmap, isl_dim_out); |
| 10679 | o_out = isl_basic_map_offset(bmap, isl_dim_out); |
| 10680 | n_div = isl_basic_map_dim(bmap, isl_dim_div); |
| 10681 | o_div = isl_basic_map_offset(bmap, isl_dim_div); |
| 10682 | |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 10683 | if (ineq) |
| 10684 | *ineq = bmap->n_ineq; |
| 10685 | if (div) |
| 10686 | *div = n_div; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10687 | for (j = 0; j < bmap->n_eq; ++j) { |
| 10688 | if (isl_int_is_zero(bmap->eq[j][o_out + pos])) |
| 10689 | continue; |
| 10690 | if (isl_seq_first_non_zero(bmap->eq[j] + o_out + pos + 1, |
| 10691 | n_out - (pos + 1)) != -1) |
| 10692 | continue; |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 10693 | k = first_div_may_involve_output(bmap, bmap->eq[j] + o_div, |
| 10694 | 0, n_div); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10695 | if (k >= n_div) |
| 10696 | return j; |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 10697 | if (!isl_int_is_one(bmap->eq[j][o_out + pos]) && |
| 10698 | !isl_int_is_negone(bmap->eq[j][o_out + pos])) |
| 10699 | continue; |
| 10700 | if (first_div_may_involve_output(bmap, bmap->eq[j] + o_div, |
| 10701 | k + 1, n_div - (k+1)) < n_div) |
| 10702 | continue; |
| 10703 | l = find_modulo_constraint_pair(bmap, pos, |
| 10704 | bmap->eq[j][o_div + k]); |
| 10705 | if (l < 0) |
| 10706 | return -1; |
| 10707 | if (l >= bmap->n_ineq) |
| 10708 | continue; |
| 10709 | if (div) |
| 10710 | *div = k; |
| 10711 | if (ineq) |
| 10712 | *ineq = l; |
| 10713 | return j; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10714 | } |
| 10715 | |
| 10716 | return bmap->n_eq; |
| 10717 | } |
| 10718 | |
| 10719 | /* Check if the given basic map is obviously single-valued. |
| 10720 | * In particular, for each output dimension, check that there is |
| 10721 | * an equality that defines the output dimension in terms of |
| 10722 | * earlier dimensions. |
| 10723 | */ |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10724 | isl_bool isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10725 | { |
| 10726 | int i; |
| 10727 | unsigned n_out; |
| 10728 | |
| 10729 | if (!bmap) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10730 | return isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10731 | |
| 10732 | n_out = isl_basic_map_dim(bmap, isl_dim_out); |
| 10733 | |
| 10734 | for (i = 0; i < n_out; ++i) { |
| 10735 | int eq; |
| 10736 | |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 10737 | eq = isl_basic_map_output_defining_equality(bmap, i, |
| 10738 | NULL, NULL); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10739 | if (eq < 0) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10740 | return isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10741 | if (eq >= bmap->n_eq) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10742 | return isl_bool_false; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10743 | } |
| 10744 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10745 | return isl_bool_true; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10746 | } |
| 10747 | |
| 10748 | /* Check if the given basic map is single-valued. |
| 10749 | * We simply compute |
| 10750 | * |
| 10751 | * M \circ M^-1 |
| 10752 | * |
| 10753 | * and check if the result is a subset of the identity mapping. |
| 10754 | */ |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10755 | isl_bool isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10756 | { |
| 10757 | isl_space *space; |
| 10758 | isl_basic_map *test; |
| 10759 | isl_basic_map *id; |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10760 | isl_bool sv; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10761 | |
| 10762 | sv = isl_basic_map_plain_is_single_valued(bmap); |
| 10763 | if (sv < 0 || sv) |
| 10764 | return sv; |
| 10765 | |
| 10766 | test = isl_basic_map_reverse(isl_basic_map_copy(bmap)); |
| 10767 | test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap)); |
| 10768 | |
| 10769 | space = isl_basic_map_get_space(bmap); |
| 10770 | space = isl_space_map_from_set(isl_space_range(space)); |
| 10771 | id = isl_basic_map_identity(space); |
| 10772 | |
| 10773 | sv = isl_basic_map_is_subset(test, id); |
| 10774 | |
| 10775 | isl_basic_map_free(test); |
| 10776 | isl_basic_map_free(id); |
| 10777 | |
| 10778 | return sv; |
| 10779 | } |
| 10780 | |
| 10781 | /* Check if the given map is obviously single-valued. |
| 10782 | */ |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10783 | isl_bool isl_map_plain_is_single_valued(__isl_keep isl_map *map) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10784 | { |
| 10785 | if (!map) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10786 | return isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10787 | if (map->n == 0) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10788 | return isl_bool_true; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10789 | if (map->n >= 2) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10790 | return isl_bool_false; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10791 | |
| 10792 | return isl_basic_map_plain_is_single_valued(map->p[0]); |
| 10793 | } |
| 10794 | |
| 10795 | /* Check if the given map is single-valued. |
| 10796 | * We simply compute |
| 10797 | * |
| 10798 | * M \circ M^-1 |
| 10799 | * |
| 10800 | * and check if the result is a subset of the identity mapping. |
| 10801 | */ |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10802 | isl_bool isl_map_is_single_valued(__isl_keep isl_map *map) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10803 | { |
| 10804 | isl_space *dim; |
| 10805 | isl_map *test; |
| 10806 | isl_map *id; |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10807 | isl_bool sv; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10808 | |
| 10809 | sv = isl_map_plain_is_single_valued(map); |
| 10810 | if (sv < 0 || sv) |
| 10811 | return sv; |
| 10812 | |
| 10813 | test = isl_map_reverse(isl_map_copy(map)); |
| 10814 | test = isl_map_apply_range(test, isl_map_copy(map)); |
| 10815 | |
| 10816 | dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map))); |
| 10817 | id = isl_map_identity(dim); |
| 10818 | |
| 10819 | sv = isl_map_is_subset(test, id); |
| 10820 | |
| 10821 | isl_map_free(test); |
| 10822 | isl_map_free(id); |
| 10823 | |
| 10824 | return sv; |
| 10825 | } |
| 10826 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10827 | isl_bool isl_map_is_injective(__isl_keep isl_map *map) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10828 | { |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10829 | isl_bool in; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10830 | |
| 10831 | map = isl_map_copy(map); |
| 10832 | map = isl_map_reverse(map); |
| 10833 | in = isl_map_is_single_valued(map); |
| 10834 | isl_map_free(map); |
| 10835 | |
| 10836 | return in; |
| 10837 | } |
| 10838 | |
| 10839 | /* Check if the given map is obviously injective. |
| 10840 | */ |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10841 | isl_bool isl_map_plain_is_injective(__isl_keep isl_map *map) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10842 | { |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10843 | isl_bool in; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10844 | |
| 10845 | map = isl_map_copy(map); |
| 10846 | map = isl_map_reverse(map); |
| 10847 | in = isl_map_plain_is_single_valued(map); |
| 10848 | isl_map_free(map); |
| 10849 | |
| 10850 | return in; |
| 10851 | } |
| 10852 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10853 | isl_bool isl_map_is_bijective(__isl_keep isl_map *map) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10854 | { |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10855 | isl_bool sv; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10856 | |
| 10857 | sv = isl_map_is_single_valued(map); |
| 10858 | if (sv < 0 || !sv) |
| 10859 | return sv; |
| 10860 | |
| 10861 | return isl_map_is_injective(map); |
| 10862 | } |
| 10863 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10864 | isl_bool isl_set_is_singleton(__isl_keep isl_set *set) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10865 | { |
| 10866 | return isl_map_is_single_valued((isl_map *)set); |
| 10867 | } |
| 10868 | |
Michael Kruse | e0b34f3 | 2016-05-04 14:41:36 +0000 | [diff] [blame] | 10869 | /* Does "map" only map elements to themselves? |
| 10870 | * |
| 10871 | * If the domain and range spaces are different, then "map" |
| 10872 | * is considered not to be an identity relation, even if it is empty. |
| 10873 | * Otherwise, construct the maximal identity relation and |
| 10874 | * check whether "map" is a subset of this relation. |
| 10875 | */ |
| 10876 | isl_bool isl_map_is_identity(__isl_keep isl_map *map) |
| 10877 | { |
| 10878 | isl_space *space; |
| 10879 | isl_map *id; |
| 10880 | isl_bool equal, is_identity; |
| 10881 | |
| 10882 | space = isl_map_get_space(map); |
| 10883 | equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out); |
| 10884 | isl_space_free(space); |
| 10885 | if (equal < 0 || !equal) |
| 10886 | return equal; |
| 10887 | |
| 10888 | id = isl_map_identity(isl_map_get_space(map)); |
| 10889 | is_identity = isl_map_is_subset(map, id); |
| 10890 | isl_map_free(id); |
| 10891 | |
| 10892 | return is_identity; |
| 10893 | } |
| 10894 | |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10895 | int isl_map_is_translation(__isl_keep isl_map *map) |
| 10896 | { |
| 10897 | int ok; |
| 10898 | isl_set *delta; |
| 10899 | |
| 10900 | delta = isl_map_deltas(isl_map_copy(map)); |
| 10901 | ok = isl_set_is_singleton(delta); |
| 10902 | isl_set_free(delta); |
| 10903 | |
| 10904 | return ok; |
| 10905 | } |
| 10906 | |
| 10907 | static int unique(isl_int *p, unsigned pos, unsigned len) |
| 10908 | { |
| 10909 | if (isl_seq_first_non_zero(p, pos) != -1) |
| 10910 | return 0; |
| 10911 | if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1) |
| 10912 | return 0; |
| 10913 | return 1; |
| 10914 | } |
| 10915 | |
| 10916 | int isl_basic_set_is_box(__isl_keep isl_basic_set *bset) |
| 10917 | { |
| 10918 | int i, j; |
| 10919 | unsigned nvar; |
| 10920 | unsigned ovar; |
| 10921 | |
| 10922 | if (!bset) |
| 10923 | return -1; |
| 10924 | |
| 10925 | if (isl_basic_set_dim(bset, isl_dim_div) != 0) |
| 10926 | return 0; |
| 10927 | |
| 10928 | nvar = isl_basic_set_dim(bset, isl_dim_set); |
| 10929 | ovar = isl_space_offset(bset->dim, isl_dim_set); |
| 10930 | for (j = 0; j < nvar; ++j) { |
| 10931 | int lower = 0, upper = 0; |
| 10932 | for (i = 0; i < bset->n_eq; ++i) { |
| 10933 | if (isl_int_is_zero(bset->eq[i][1 + ovar + j])) |
| 10934 | continue; |
| 10935 | if (!unique(bset->eq[i] + 1 + ovar, j, nvar)) |
| 10936 | return 0; |
| 10937 | break; |
| 10938 | } |
| 10939 | if (i < bset->n_eq) |
| 10940 | continue; |
| 10941 | for (i = 0; i < bset->n_ineq; ++i) { |
| 10942 | if (isl_int_is_zero(bset->ineq[i][1 + ovar + j])) |
| 10943 | continue; |
| 10944 | if (!unique(bset->ineq[i] + 1 + ovar, j, nvar)) |
| 10945 | return 0; |
| 10946 | if (isl_int_is_pos(bset->ineq[i][1 + ovar + j])) |
| 10947 | lower = 1; |
| 10948 | else |
| 10949 | upper = 1; |
| 10950 | } |
| 10951 | if (!lower || !upper) |
| 10952 | return 0; |
| 10953 | } |
| 10954 | |
| 10955 | return 1; |
| 10956 | } |
| 10957 | |
| 10958 | int isl_set_is_box(__isl_keep isl_set *set) |
| 10959 | { |
| 10960 | if (!set) |
| 10961 | return -1; |
| 10962 | if (set->n != 1) |
| 10963 | return 0; |
| 10964 | |
| 10965 | return isl_basic_set_is_box(set->p[0]); |
| 10966 | } |
| 10967 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10968 | isl_bool isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10969 | { |
| 10970 | if (!bset) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10971 | return isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10972 | |
| 10973 | return isl_space_is_wrapping(bset->dim); |
| 10974 | } |
| 10975 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10976 | isl_bool isl_set_is_wrapping(__isl_keep isl_set *set) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10977 | { |
| 10978 | if (!set) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10979 | return isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10980 | |
| 10981 | return isl_space_is_wrapping(set->dim); |
| 10982 | } |
| 10983 | |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 10984 | /* Modify the space of "map" through a call to "change". |
| 10985 | * If "can_change" is set (not NULL), then first call it to check |
| 10986 | * if the modification is allowed, printing the error message "cannot_change" |
| 10987 | * if it is not. |
| 10988 | */ |
| 10989 | static __isl_give isl_map *isl_map_change_space(__isl_take isl_map *map, |
| 10990 | isl_bool (*can_change)(__isl_keep isl_map *map), |
| 10991 | const char *cannot_change, |
| 10992 | __isl_give isl_space *(*change)(__isl_take isl_space *space)) |
| 10993 | { |
| 10994 | isl_bool ok; |
| 10995 | isl_space *space; |
| 10996 | |
| 10997 | if (!map) |
| 10998 | return NULL; |
| 10999 | |
| 11000 | ok = can_change ? can_change(map) : isl_bool_true; |
| 11001 | if (ok < 0) |
| 11002 | return isl_map_free(map); |
| 11003 | if (!ok) |
| 11004 | isl_die(isl_map_get_ctx(map), isl_error_invalid, cannot_change, |
| 11005 | return isl_map_free(map)); |
| 11006 | |
| 11007 | space = change(isl_map_get_space(map)); |
| 11008 | map = isl_map_reset_space(map, space); |
| 11009 | |
| 11010 | return map; |
| 11011 | } |
| 11012 | |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11013 | /* Is the domain of "map" a wrapped relation? |
| 11014 | */ |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 11015 | isl_bool isl_map_domain_is_wrapping(__isl_keep isl_map *map) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11016 | { |
| 11017 | if (!map) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 11018 | return isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11019 | |
| 11020 | return isl_space_domain_is_wrapping(map->dim); |
| 11021 | } |
| 11022 | |
| 11023 | /* Is the range of "map" a wrapped relation? |
| 11024 | */ |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 11025 | isl_bool isl_map_range_is_wrapping(__isl_keep isl_map *map) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11026 | { |
| 11027 | if (!map) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 11028 | return isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11029 | |
| 11030 | return isl_space_range_is_wrapping(map->dim); |
| 11031 | } |
| 11032 | |
| 11033 | __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap) |
| 11034 | { |
| 11035 | bmap = isl_basic_map_cow(bmap); |
| 11036 | if (!bmap) |
| 11037 | return NULL; |
| 11038 | |
| 11039 | bmap->dim = isl_space_wrap(bmap->dim); |
| 11040 | if (!bmap->dim) |
| 11041 | goto error; |
| 11042 | |
| 11043 | bmap = isl_basic_map_finalize(bmap); |
| 11044 | |
| 11045 | return (isl_basic_set *)bmap; |
| 11046 | error: |
| 11047 | isl_basic_map_free(bmap); |
| 11048 | return NULL; |
| 11049 | } |
| 11050 | |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 11051 | /* Given a map A -> B, return the set (A -> B). |
| 11052 | */ |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11053 | __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map) |
| 11054 | { |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 11055 | return isl_map_change_space(map, NULL, NULL, &isl_space_wrap); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11056 | } |
| 11057 | |
| 11058 | __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset) |
| 11059 | { |
| 11060 | bset = isl_basic_set_cow(bset); |
| 11061 | if (!bset) |
| 11062 | return NULL; |
| 11063 | |
| 11064 | bset->dim = isl_space_unwrap(bset->dim); |
| 11065 | if (!bset->dim) |
| 11066 | goto error; |
| 11067 | |
| 11068 | bset = isl_basic_set_finalize(bset); |
| 11069 | |
| 11070 | return (isl_basic_map *)bset; |
| 11071 | error: |
| 11072 | isl_basic_set_free(bset); |
| 11073 | return NULL; |
| 11074 | } |
| 11075 | |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 11076 | /* Given a set (A -> B), return the map A -> B. |
| 11077 | * Error out if "set" is not of the form (A -> B). |
| 11078 | */ |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11079 | __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set) |
| 11080 | { |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 11081 | return isl_map_change_space(set, &isl_set_is_wrapping, |
| 11082 | "not a wrapping set", &isl_space_unwrap); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11083 | } |
| 11084 | |
| 11085 | __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap, |
| 11086 | enum isl_dim_type type) |
| 11087 | { |
| 11088 | if (!bmap) |
| 11089 | return NULL; |
| 11090 | |
| 11091 | if (!isl_space_is_named_or_nested(bmap->dim, type)) |
| 11092 | return bmap; |
| 11093 | |
| 11094 | bmap = isl_basic_map_cow(bmap); |
| 11095 | if (!bmap) |
| 11096 | return NULL; |
| 11097 | |
| 11098 | bmap->dim = isl_space_reset(bmap->dim, type); |
| 11099 | if (!bmap->dim) |
| 11100 | goto error; |
| 11101 | |
| 11102 | bmap = isl_basic_map_finalize(bmap); |
| 11103 | |
| 11104 | return bmap; |
| 11105 | error: |
| 11106 | isl_basic_map_free(bmap); |
| 11107 | return NULL; |
| 11108 | } |
| 11109 | |
| 11110 | __isl_give isl_map *isl_map_reset(__isl_take isl_map *map, |
| 11111 | enum isl_dim_type type) |
| 11112 | { |
| 11113 | int i; |
| 11114 | |
| 11115 | if (!map) |
| 11116 | return NULL; |
| 11117 | |
| 11118 | if (!isl_space_is_named_or_nested(map->dim, type)) |
| 11119 | return map; |
| 11120 | |
| 11121 | map = isl_map_cow(map); |
| 11122 | if (!map) |
| 11123 | return NULL; |
| 11124 | |
| 11125 | for (i = 0; i < map->n; ++i) { |
| 11126 | map->p[i] = isl_basic_map_reset(map->p[i], type); |
| 11127 | if (!map->p[i]) |
| 11128 | goto error; |
| 11129 | } |
| 11130 | map->dim = isl_space_reset(map->dim, type); |
| 11131 | if (!map->dim) |
| 11132 | goto error; |
| 11133 | |
| 11134 | return map; |
| 11135 | error: |
| 11136 | isl_map_free(map); |
| 11137 | return NULL; |
| 11138 | } |
| 11139 | |
| 11140 | __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap) |
| 11141 | { |
| 11142 | if (!bmap) |
| 11143 | return NULL; |
| 11144 | |
| 11145 | if (!bmap->dim->nested[0] && !bmap->dim->nested[1]) |
| 11146 | return bmap; |
| 11147 | |
| 11148 | bmap = isl_basic_map_cow(bmap); |
| 11149 | if (!bmap) |
| 11150 | return NULL; |
| 11151 | |
| 11152 | bmap->dim = isl_space_flatten(bmap->dim); |
| 11153 | if (!bmap->dim) |
| 11154 | goto error; |
| 11155 | |
| 11156 | bmap = isl_basic_map_finalize(bmap); |
| 11157 | |
| 11158 | return bmap; |
| 11159 | error: |
| 11160 | isl_basic_map_free(bmap); |
| 11161 | return NULL; |
| 11162 | } |
| 11163 | |
| 11164 | __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset) |
| 11165 | { |
| 11166 | return (isl_basic_set *)isl_basic_map_flatten((isl_basic_map *)bset); |
| 11167 | } |
| 11168 | |
| 11169 | __isl_give isl_basic_map *isl_basic_map_flatten_domain( |
| 11170 | __isl_take isl_basic_map *bmap) |
| 11171 | { |
| 11172 | if (!bmap) |
| 11173 | return NULL; |
| 11174 | |
| 11175 | if (!bmap->dim->nested[0]) |
| 11176 | return bmap; |
| 11177 | |
| 11178 | bmap = isl_basic_map_cow(bmap); |
| 11179 | if (!bmap) |
| 11180 | return NULL; |
| 11181 | |
| 11182 | bmap->dim = isl_space_flatten_domain(bmap->dim); |
| 11183 | if (!bmap->dim) |
| 11184 | goto error; |
| 11185 | |
| 11186 | bmap = isl_basic_map_finalize(bmap); |
| 11187 | |
| 11188 | return bmap; |
| 11189 | error: |
| 11190 | isl_basic_map_free(bmap); |
| 11191 | return NULL; |
| 11192 | } |
| 11193 | |
| 11194 | __isl_give isl_basic_map *isl_basic_map_flatten_range( |
| 11195 | __isl_take isl_basic_map *bmap) |
| 11196 | { |
| 11197 | if (!bmap) |
| 11198 | return NULL; |
| 11199 | |
| 11200 | if (!bmap->dim->nested[1]) |
| 11201 | return bmap; |
| 11202 | |
| 11203 | bmap = isl_basic_map_cow(bmap); |
| 11204 | if (!bmap) |
| 11205 | return NULL; |
| 11206 | |
| 11207 | bmap->dim = isl_space_flatten_range(bmap->dim); |
| 11208 | if (!bmap->dim) |
| 11209 | goto error; |
| 11210 | |
| 11211 | bmap = isl_basic_map_finalize(bmap); |
| 11212 | |
| 11213 | return bmap; |
| 11214 | error: |
| 11215 | isl_basic_map_free(bmap); |
| 11216 | return NULL; |
| 11217 | } |
| 11218 | |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 11219 | /* Remove any internal structure from the spaces of domain and range of "map". |
| 11220 | */ |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11221 | __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map) |
| 11222 | { |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11223 | if (!map) |
| 11224 | return NULL; |
| 11225 | |
| 11226 | if (!map->dim->nested[0] && !map->dim->nested[1]) |
| 11227 | return map; |
| 11228 | |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 11229 | return isl_map_change_space(map, NULL, NULL, &isl_space_flatten); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11230 | } |
| 11231 | |
| 11232 | __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set) |
| 11233 | { |
| 11234 | return (isl_set *)isl_map_flatten((isl_map *)set); |
| 11235 | } |
| 11236 | |
| 11237 | __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set) |
| 11238 | { |
| 11239 | isl_space *dim, *flat_dim; |
| 11240 | isl_map *map; |
| 11241 | |
| 11242 | dim = isl_set_get_space(set); |
| 11243 | flat_dim = isl_space_flatten(isl_space_copy(dim)); |
| 11244 | map = isl_map_identity(isl_space_join(isl_space_reverse(dim), flat_dim)); |
| 11245 | map = isl_map_intersect_domain(map, set); |
| 11246 | |
| 11247 | return map; |
| 11248 | } |
| 11249 | |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 11250 | /* Remove any internal structure from the space of the domain of "map". |
| 11251 | */ |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11252 | __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map) |
| 11253 | { |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11254 | if (!map) |
| 11255 | return NULL; |
| 11256 | |
| 11257 | if (!map->dim->nested[0]) |
| 11258 | return map; |
| 11259 | |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 11260 | return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_domain); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11261 | } |
| 11262 | |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 11263 | /* Remove any internal structure from the space of the range of "map". |
| 11264 | */ |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11265 | __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map) |
| 11266 | { |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11267 | if (!map) |
| 11268 | return NULL; |
| 11269 | |
| 11270 | if (!map->dim->nested[1]) |
| 11271 | return map; |
| 11272 | |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 11273 | return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_range); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11274 | } |
| 11275 | |
| 11276 | /* Reorder the dimensions of "bmap" according to the given dim_map |
Tobias Grosser | a7f1e04 | 2016-02-04 06:19:12 +0000 | [diff] [blame] | 11277 | * and set the dimension specification to "dim" and |
| 11278 | * perform Gaussian elimination on the result. |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11279 | */ |
| 11280 | __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap, |
| 11281 | __isl_take isl_space *dim, __isl_take struct isl_dim_map *dim_map) |
| 11282 | { |
| 11283 | isl_basic_map *res; |
| 11284 | unsigned flags; |
| 11285 | |
| 11286 | bmap = isl_basic_map_cow(bmap); |
| 11287 | if (!bmap || !dim || !dim_map) |
| 11288 | goto error; |
| 11289 | |
| 11290 | flags = bmap->flags; |
| 11291 | ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL); |
| 11292 | ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED); |
| 11293 | ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS); |
| 11294 | res = isl_basic_map_alloc_space(dim, |
| 11295 | bmap->n_div, bmap->n_eq, bmap->n_ineq); |
| 11296 | res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map); |
| 11297 | if (res) |
| 11298 | res->flags = flags; |
Tobias Grosser | a7f1e04 | 2016-02-04 06:19:12 +0000 | [diff] [blame] | 11299 | res = isl_basic_map_gauss(res, NULL); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11300 | res = isl_basic_map_finalize(res); |
| 11301 | return res; |
| 11302 | error: |
| 11303 | free(dim_map); |
| 11304 | isl_basic_map_free(bmap); |
| 11305 | isl_space_free(dim); |
| 11306 | return NULL; |
| 11307 | } |
| 11308 | |
| 11309 | /* Reorder the dimensions of "map" according to given reordering. |
| 11310 | */ |
| 11311 | __isl_give isl_map *isl_map_realign(__isl_take isl_map *map, |
| 11312 | __isl_take isl_reordering *r) |
| 11313 | { |
| 11314 | int i; |
| 11315 | struct isl_dim_map *dim_map; |
| 11316 | |
| 11317 | map = isl_map_cow(map); |
| 11318 | dim_map = isl_dim_map_from_reordering(r); |
| 11319 | if (!map || !r || !dim_map) |
| 11320 | goto error; |
| 11321 | |
| 11322 | for (i = 0; i < map->n; ++i) { |
| 11323 | struct isl_dim_map *dim_map_i; |
| 11324 | |
| 11325 | dim_map_i = isl_dim_map_extend(dim_map, map->p[i]); |
| 11326 | |
| 11327 | map->p[i] = isl_basic_map_realign(map->p[i], |
| 11328 | isl_space_copy(r->dim), dim_map_i); |
| 11329 | |
| 11330 | if (!map->p[i]) |
| 11331 | goto error; |
| 11332 | } |
| 11333 | |
| 11334 | map = isl_map_reset_space(map, isl_space_copy(r->dim)); |
| 11335 | |
| 11336 | isl_reordering_free(r); |
| 11337 | free(dim_map); |
| 11338 | return map; |
| 11339 | error: |
| 11340 | free(dim_map); |
| 11341 | isl_map_free(map); |
| 11342 | isl_reordering_free(r); |
| 11343 | return NULL; |
| 11344 | } |
| 11345 | |
| 11346 | __isl_give isl_set *isl_set_realign(__isl_take isl_set *set, |
| 11347 | __isl_take isl_reordering *r) |
| 11348 | { |
| 11349 | return (isl_set *)isl_map_realign((isl_map *)set, r); |
| 11350 | } |
| 11351 | |
| 11352 | __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map, |
| 11353 | __isl_take isl_space *model) |
| 11354 | { |
| 11355 | isl_ctx *ctx; |
| 11356 | |
| 11357 | if (!map || !model) |
| 11358 | goto error; |
| 11359 | |
| 11360 | ctx = isl_space_get_ctx(model); |
| 11361 | if (!isl_space_has_named_params(model)) |
| 11362 | isl_die(ctx, isl_error_invalid, |
| 11363 | "model has unnamed parameters", goto error); |
| 11364 | if (!isl_space_has_named_params(map->dim)) |
| 11365 | isl_die(ctx, isl_error_invalid, |
| 11366 | "relation has unnamed parameters", goto error); |
| 11367 | if (!isl_space_match(map->dim, isl_dim_param, model, isl_dim_param)) { |
| 11368 | isl_reordering *exp; |
| 11369 | |
| 11370 | model = isl_space_drop_dims(model, isl_dim_in, |
| 11371 | 0, isl_space_dim(model, isl_dim_in)); |
| 11372 | model = isl_space_drop_dims(model, isl_dim_out, |
| 11373 | 0, isl_space_dim(model, isl_dim_out)); |
| 11374 | exp = isl_parameter_alignment_reordering(map->dim, model); |
| 11375 | exp = isl_reordering_extend_space(exp, isl_map_get_space(map)); |
| 11376 | map = isl_map_realign(map, exp); |
| 11377 | } |
| 11378 | |
| 11379 | isl_space_free(model); |
| 11380 | return map; |
| 11381 | error: |
| 11382 | isl_space_free(model); |
| 11383 | isl_map_free(map); |
| 11384 | return NULL; |
| 11385 | } |
| 11386 | |
| 11387 | __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set, |
| 11388 | __isl_take isl_space *model) |
| 11389 | { |
| 11390 | return isl_map_align_params(set, model); |
| 11391 | } |
| 11392 | |
| 11393 | /* Align the parameters of "bmap" to those of "model", introducing |
| 11394 | * additional parameters if needed. |
| 11395 | */ |
| 11396 | __isl_give isl_basic_map *isl_basic_map_align_params( |
| 11397 | __isl_take isl_basic_map *bmap, __isl_take isl_space *model) |
| 11398 | { |
| 11399 | isl_ctx *ctx; |
| 11400 | |
| 11401 | if (!bmap || !model) |
| 11402 | goto error; |
| 11403 | |
| 11404 | ctx = isl_space_get_ctx(model); |
| 11405 | if (!isl_space_has_named_params(model)) |
| 11406 | isl_die(ctx, isl_error_invalid, |
| 11407 | "model has unnamed parameters", goto error); |
| 11408 | if (!isl_space_has_named_params(bmap->dim)) |
| 11409 | isl_die(ctx, isl_error_invalid, |
| 11410 | "relation has unnamed parameters", goto error); |
| 11411 | if (!isl_space_match(bmap->dim, isl_dim_param, model, isl_dim_param)) { |
| 11412 | isl_reordering *exp; |
| 11413 | struct isl_dim_map *dim_map; |
| 11414 | |
| 11415 | model = isl_space_drop_dims(model, isl_dim_in, |
| 11416 | 0, isl_space_dim(model, isl_dim_in)); |
| 11417 | model = isl_space_drop_dims(model, isl_dim_out, |
| 11418 | 0, isl_space_dim(model, isl_dim_out)); |
| 11419 | exp = isl_parameter_alignment_reordering(bmap->dim, model); |
| 11420 | exp = isl_reordering_extend_space(exp, |
| 11421 | isl_basic_map_get_space(bmap)); |
| 11422 | dim_map = isl_dim_map_from_reordering(exp); |
| 11423 | bmap = isl_basic_map_realign(bmap, |
| 11424 | exp ? isl_space_copy(exp->dim) : NULL, |
| 11425 | isl_dim_map_extend(dim_map, bmap)); |
| 11426 | isl_reordering_free(exp); |
| 11427 | free(dim_map); |
| 11428 | } |
| 11429 | |
| 11430 | isl_space_free(model); |
| 11431 | return bmap; |
| 11432 | error: |
| 11433 | isl_space_free(model); |
| 11434 | isl_basic_map_free(bmap); |
| 11435 | return NULL; |
| 11436 | } |
| 11437 | |
| 11438 | /* Align the parameters of "bset" to those of "model", introducing |
| 11439 | * additional parameters if needed. |
| 11440 | */ |
| 11441 | __isl_give isl_basic_set *isl_basic_set_align_params( |
| 11442 | __isl_take isl_basic_set *bset, __isl_take isl_space *model) |
| 11443 | { |
| 11444 | return isl_basic_map_align_params(bset, model); |
| 11445 | } |
| 11446 | |
| 11447 | __isl_give isl_mat *isl_basic_map_equalities_matrix( |
| 11448 | __isl_keep isl_basic_map *bmap, enum isl_dim_type c1, |
| 11449 | enum isl_dim_type c2, enum isl_dim_type c3, |
| 11450 | enum isl_dim_type c4, enum isl_dim_type c5) |
| 11451 | { |
| 11452 | enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 }; |
| 11453 | struct isl_mat *mat; |
| 11454 | int i, j, k; |
| 11455 | int pos; |
| 11456 | |
| 11457 | if (!bmap) |
| 11458 | return NULL; |
| 11459 | mat = isl_mat_alloc(bmap->ctx, bmap->n_eq, |
| 11460 | isl_basic_map_total_dim(bmap) + 1); |
| 11461 | if (!mat) |
| 11462 | return NULL; |
| 11463 | for (i = 0; i < bmap->n_eq; ++i) |
| 11464 | for (j = 0, pos = 0; j < 5; ++j) { |
| 11465 | int off = isl_basic_map_offset(bmap, c[j]); |
| 11466 | for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) { |
| 11467 | isl_int_set(mat->row[i][pos], |
| 11468 | bmap->eq[i][off + k]); |
| 11469 | ++pos; |
| 11470 | } |
| 11471 | } |
| 11472 | |
| 11473 | return mat; |
| 11474 | } |
| 11475 | |
| 11476 | __isl_give isl_mat *isl_basic_map_inequalities_matrix( |
| 11477 | __isl_keep isl_basic_map *bmap, enum isl_dim_type c1, |
| 11478 | enum isl_dim_type c2, enum isl_dim_type c3, |
| 11479 | enum isl_dim_type c4, enum isl_dim_type c5) |
| 11480 | { |
| 11481 | enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 }; |
| 11482 | struct isl_mat *mat; |
| 11483 | int i, j, k; |
| 11484 | int pos; |
| 11485 | |
| 11486 | if (!bmap) |
| 11487 | return NULL; |
| 11488 | mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq, |
| 11489 | isl_basic_map_total_dim(bmap) + 1); |
| 11490 | if (!mat) |
| 11491 | return NULL; |
| 11492 | for (i = 0; i < bmap->n_ineq; ++i) |
| 11493 | for (j = 0, pos = 0; j < 5; ++j) { |
| 11494 | int off = isl_basic_map_offset(bmap, c[j]); |
| 11495 | for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) { |
| 11496 | isl_int_set(mat->row[i][pos], |
| 11497 | bmap->ineq[i][off + k]); |
| 11498 | ++pos; |
| 11499 | } |
| 11500 | } |
| 11501 | |
| 11502 | return mat; |
| 11503 | } |
| 11504 | |
| 11505 | __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices( |
| 11506 | __isl_take isl_space *dim, |
| 11507 | __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1, |
| 11508 | enum isl_dim_type c2, enum isl_dim_type c3, |
| 11509 | enum isl_dim_type c4, enum isl_dim_type c5) |
| 11510 | { |
| 11511 | enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 }; |
| 11512 | isl_basic_map *bmap; |
| 11513 | unsigned total; |
| 11514 | unsigned extra; |
| 11515 | int i, j, k, l; |
| 11516 | int pos; |
| 11517 | |
| 11518 | if (!dim || !eq || !ineq) |
| 11519 | goto error; |
| 11520 | |
| 11521 | if (eq->n_col != ineq->n_col) |
| 11522 | isl_die(dim->ctx, isl_error_invalid, |
| 11523 | "equalities and inequalities matrices should have " |
| 11524 | "same number of columns", goto error); |
| 11525 | |
| 11526 | total = 1 + isl_space_dim(dim, isl_dim_all); |
| 11527 | |
| 11528 | if (eq->n_col < total) |
| 11529 | isl_die(dim->ctx, isl_error_invalid, |
| 11530 | "number of columns too small", goto error); |
| 11531 | |
| 11532 | extra = eq->n_col - total; |
| 11533 | |
| 11534 | bmap = isl_basic_map_alloc_space(isl_space_copy(dim), extra, |
| 11535 | eq->n_row, ineq->n_row); |
| 11536 | if (!bmap) |
| 11537 | goto error; |
| 11538 | for (i = 0; i < extra; ++i) { |
| 11539 | k = isl_basic_map_alloc_div(bmap); |
| 11540 | if (k < 0) |
| 11541 | goto error; |
| 11542 | isl_int_set_si(bmap->div[k][0], 0); |
| 11543 | } |
| 11544 | for (i = 0; i < eq->n_row; ++i) { |
| 11545 | l = isl_basic_map_alloc_equality(bmap); |
| 11546 | if (l < 0) |
| 11547 | goto error; |
| 11548 | for (j = 0, pos = 0; j < 5; ++j) { |
| 11549 | int off = isl_basic_map_offset(bmap, c[j]); |
| 11550 | for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) { |
| 11551 | isl_int_set(bmap->eq[l][off + k], |
| 11552 | eq->row[i][pos]); |
| 11553 | ++pos; |
| 11554 | } |
| 11555 | } |
| 11556 | } |
| 11557 | for (i = 0; i < ineq->n_row; ++i) { |
| 11558 | l = isl_basic_map_alloc_inequality(bmap); |
| 11559 | if (l < 0) |
| 11560 | goto error; |
| 11561 | for (j = 0, pos = 0; j < 5; ++j) { |
| 11562 | int off = isl_basic_map_offset(bmap, c[j]); |
| 11563 | for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) { |
| 11564 | isl_int_set(bmap->ineq[l][off + k], |
| 11565 | ineq->row[i][pos]); |
| 11566 | ++pos; |
| 11567 | } |
| 11568 | } |
| 11569 | } |
| 11570 | |
| 11571 | isl_space_free(dim); |
| 11572 | isl_mat_free(eq); |
| 11573 | isl_mat_free(ineq); |
| 11574 | |
| 11575 | bmap = isl_basic_map_simplify(bmap); |
| 11576 | return isl_basic_map_finalize(bmap); |
| 11577 | error: |
| 11578 | isl_space_free(dim); |
| 11579 | isl_mat_free(eq); |
| 11580 | isl_mat_free(ineq); |
| 11581 | return NULL; |
| 11582 | } |
| 11583 | |
| 11584 | __isl_give isl_mat *isl_basic_set_equalities_matrix( |
| 11585 | __isl_keep isl_basic_set *bset, enum isl_dim_type c1, |
| 11586 | enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4) |
| 11587 | { |
| 11588 | return isl_basic_map_equalities_matrix((isl_basic_map *)bset, |
| 11589 | c1, c2, c3, c4, isl_dim_in); |
| 11590 | } |
| 11591 | |
| 11592 | __isl_give isl_mat *isl_basic_set_inequalities_matrix( |
| 11593 | __isl_keep isl_basic_set *bset, enum isl_dim_type c1, |
| 11594 | enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4) |
| 11595 | { |
| 11596 | return isl_basic_map_inequalities_matrix((isl_basic_map *)bset, |
| 11597 | c1, c2, c3, c4, isl_dim_in); |
| 11598 | } |
| 11599 | |
| 11600 | __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices( |
| 11601 | __isl_take isl_space *dim, |
| 11602 | __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1, |
| 11603 | enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4) |
| 11604 | { |
| 11605 | return (isl_basic_set*) |
| 11606 | isl_basic_map_from_constraint_matrices(dim, eq, ineq, |
| 11607 | c1, c2, c3, c4, isl_dim_in); |
| 11608 | } |
| 11609 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 11610 | isl_bool isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11611 | { |
| 11612 | if (!bmap) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 11613 | return isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11614 | |
| 11615 | return isl_space_can_zip(bmap->dim); |
| 11616 | } |
| 11617 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 11618 | isl_bool isl_map_can_zip(__isl_keep isl_map *map) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11619 | { |
| 11620 | if (!map) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 11621 | return isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11622 | |
| 11623 | return isl_space_can_zip(map->dim); |
| 11624 | } |
| 11625 | |
| 11626 | /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map |
| 11627 | * (A -> C) -> (B -> D). |
| 11628 | */ |
| 11629 | __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap) |
| 11630 | { |
| 11631 | unsigned pos; |
| 11632 | unsigned n1; |
| 11633 | unsigned n2; |
| 11634 | |
| 11635 | if (!bmap) |
| 11636 | return NULL; |
| 11637 | |
| 11638 | if (!isl_basic_map_can_zip(bmap)) |
| 11639 | isl_die(bmap->ctx, isl_error_invalid, |
| 11640 | "basic map cannot be zipped", goto error); |
| 11641 | pos = isl_basic_map_offset(bmap, isl_dim_in) + |
| 11642 | isl_space_dim(bmap->dim->nested[0], isl_dim_in); |
| 11643 | n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out); |
| 11644 | n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in); |
| 11645 | bmap = isl_basic_map_cow(bmap); |
| 11646 | bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2); |
| 11647 | if (!bmap) |
| 11648 | return NULL; |
| 11649 | bmap->dim = isl_space_zip(bmap->dim); |
| 11650 | if (!bmap->dim) |
| 11651 | goto error; |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 11652 | bmap = isl_basic_map_mark_final(bmap); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11653 | return bmap; |
| 11654 | error: |
| 11655 | isl_basic_map_free(bmap); |
| 11656 | return NULL; |
| 11657 | } |
| 11658 | |
| 11659 | /* Given a map (A -> B) -> (C -> D), return the corresponding map |
| 11660 | * (A -> C) -> (B -> D). |
| 11661 | */ |
| 11662 | __isl_give isl_map *isl_map_zip(__isl_take isl_map *map) |
| 11663 | { |
| 11664 | int i; |
| 11665 | |
| 11666 | if (!map) |
| 11667 | return NULL; |
| 11668 | |
| 11669 | if (!isl_map_can_zip(map)) |
| 11670 | isl_die(map->ctx, isl_error_invalid, "map cannot be zipped", |
| 11671 | goto error); |
| 11672 | |
| 11673 | map = isl_map_cow(map); |
| 11674 | if (!map) |
| 11675 | return NULL; |
| 11676 | |
| 11677 | for (i = 0; i < map->n; ++i) { |
| 11678 | map->p[i] = isl_basic_map_zip(map->p[i]); |
| 11679 | if (!map->p[i]) |
| 11680 | goto error; |
| 11681 | } |
| 11682 | |
| 11683 | map->dim = isl_space_zip(map->dim); |
| 11684 | if (!map->dim) |
| 11685 | goto error; |
| 11686 | |
| 11687 | return map; |
| 11688 | error: |
| 11689 | isl_map_free(map); |
| 11690 | return NULL; |
| 11691 | } |
| 11692 | |
| 11693 | /* Can we apply isl_basic_map_curry to "bmap"? |
| 11694 | * That is, does it have a nested relation in its domain? |
| 11695 | */ |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 11696 | isl_bool isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11697 | { |
| 11698 | if (!bmap) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 11699 | return isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11700 | |
| 11701 | return isl_space_can_curry(bmap->dim); |
| 11702 | } |
| 11703 | |
| 11704 | /* Can we apply isl_map_curry to "map"? |
| 11705 | * That is, does it have a nested relation in its domain? |
| 11706 | */ |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 11707 | isl_bool isl_map_can_curry(__isl_keep isl_map *map) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11708 | { |
| 11709 | if (!map) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 11710 | return isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11711 | |
| 11712 | return isl_space_can_curry(map->dim); |
| 11713 | } |
| 11714 | |
| 11715 | /* Given a basic map (A -> B) -> C, return the corresponding basic map |
| 11716 | * A -> (B -> C). |
| 11717 | */ |
| 11718 | __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap) |
| 11719 | { |
| 11720 | |
| 11721 | if (!bmap) |
| 11722 | return NULL; |
| 11723 | |
| 11724 | if (!isl_basic_map_can_curry(bmap)) |
| 11725 | isl_die(bmap->ctx, isl_error_invalid, |
| 11726 | "basic map cannot be curried", goto error); |
| 11727 | bmap = isl_basic_map_cow(bmap); |
| 11728 | if (!bmap) |
| 11729 | return NULL; |
| 11730 | bmap->dim = isl_space_curry(bmap->dim); |
| 11731 | if (!bmap->dim) |
| 11732 | goto error; |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 11733 | bmap = isl_basic_map_mark_final(bmap); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11734 | return bmap; |
| 11735 | error: |
| 11736 | isl_basic_map_free(bmap); |
| 11737 | return NULL; |
| 11738 | } |
| 11739 | |
| 11740 | /* Given a map (A -> B) -> C, return the corresponding map |
| 11741 | * A -> (B -> C). |
| 11742 | */ |
| 11743 | __isl_give isl_map *isl_map_curry(__isl_take isl_map *map) |
| 11744 | { |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 11745 | return isl_map_change_space(map, &isl_map_can_curry, |
| 11746 | "map cannot be curried", &isl_space_curry); |
| 11747 | } |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11748 | |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 11749 | /* Can isl_map_range_curry be applied to "map"? |
| 11750 | * That is, does it have a nested relation in its range, |
| 11751 | * the domain of which is itself a nested relation? |
| 11752 | */ |
| 11753 | isl_bool isl_map_can_range_curry(__isl_keep isl_map *map) |
| 11754 | { |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11755 | if (!map) |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 11756 | return isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11757 | |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 11758 | return isl_space_can_range_curry(map->dim); |
| 11759 | } |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11760 | |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 11761 | /* Given a map A -> ((B -> C) -> D), return the corresponding map |
| 11762 | * A -> (B -> (C -> D)). |
| 11763 | */ |
| 11764 | __isl_give isl_map *isl_map_range_curry(__isl_take isl_map *map) |
| 11765 | { |
| 11766 | return isl_map_change_space(map, &isl_map_can_range_curry, |
| 11767 | "map range cannot be curried", |
| 11768 | &isl_space_range_curry); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11769 | } |
| 11770 | |
| 11771 | /* Can we apply isl_basic_map_uncurry to "bmap"? |
| 11772 | * That is, does it have a nested relation in its domain? |
| 11773 | */ |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 11774 | isl_bool isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11775 | { |
| 11776 | if (!bmap) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 11777 | return isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11778 | |
| 11779 | return isl_space_can_uncurry(bmap->dim); |
| 11780 | } |
| 11781 | |
| 11782 | /* Can we apply isl_map_uncurry to "map"? |
| 11783 | * That is, does it have a nested relation in its domain? |
| 11784 | */ |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 11785 | isl_bool isl_map_can_uncurry(__isl_keep isl_map *map) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11786 | { |
| 11787 | if (!map) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 11788 | return isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11789 | |
| 11790 | return isl_space_can_uncurry(map->dim); |
| 11791 | } |
| 11792 | |
| 11793 | /* Given a basic map A -> (B -> C), return the corresponding basic map |
| 11794 | * (A -> B) -> C. |
| 11795 | */ |
| 11796 | __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap) |
| 11797 | { |
| 11798 | |
| 11799 | if (!bmap) |
| 11800 | return NULL; |
| 11801 | |
| 11802 | if (!isl_basic_map_can_uncurry(bmap)) |
| 11803 | isl_die(bmap->ctx, isl_error_invalid, |
| 11804 | "basic map cannot be uncurried", |
| 11805 | return isl_basic_map_free(bmap)); |
| 11806 | bmap = isl_basic_map_cow(bmap); |
| 11807 | if (!bmap) |
| 11808 | return NULL; |
| 11809 | bmap->dim = isl_space_uncurry(bmap->dim); |
| 11810 | if (!bmap->dim) |
| 11811 | return isl_basic_map_free(bmap); |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 11812 | bmap = isl_basic_map_mark_final(bmap); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11813 | return bmap; |
| 11814 | } |
| 11815 | |
| 11816 | /* Given a map A -> (B -> C), return the corresponding map |
| 11817 | * (A -> B) -> C. |
| 11818 | */ |
| 11819 | __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map) |
| 11820 | { |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 11821 | return isl_map_change_space(map, &isl_map_can_uncurry, |
| 11822 | "map cannot be uncurried", &isl_space_uncurry); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11823 | } |
| 11824 | |
| 11825 | /* Construct a basic map mapping the domain of the affine expression |
| 11826 | * to a one-dimensional range prescribed by the affine expression. |
Tobias Grosser | 37034db | 2016-03-25 19:38:18 +0000 | [diff] [blame] | 11827 | * |
| 11828 | * A NaN affine expression cannot be converted to a basic map. |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11829 | */ |
| 11830 | __isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff) |
| 11831 | { |
| 11832 | int k; |
| 11833 | int pos; |
Tobias Grosser | 37034db | 2016-03-25 19:38:18 +0000 | [diff] [blame] | 11834 | isl_bool is_nan; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11835 | isl_local_space *ls; |
Tobias Grosser | 37034db | 2016-03-25 19:38:18 +0000 | [diff] [blame] | 11836 | isl_basic_map *bmap = NULL; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11837 | |
| 11838 | if (!aff) |
| 11839 | return NULL; |
Tobias Grosser | 37034db | 2016-03-25 19:38:18 +0000 | [diff] [blame] | 11840 | is_nan = isl_aff_is_nan(aff); |
| 11841 | if (is_nan < 0) |
| 11842 | goto error; |
| 11843 | if (is_nan) |
| 11844 | isl_die(isl_aff_get_ctx(aff), isl_error_invalid, |
| 11845 | "cannot convert NaN", goto error); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11846 | |
| 11847 | ls = isl_aff_get_local_space(aff); |
| 11848 | bmap = isl_basic_map_from_local_space(ls); |
| 11849 | bmap = isl_basic_map_extend_constraints(bmap, 1, 0); |
| 11850 | k = isl_basic_map_alloc_equality(bmap); |
| 11851 | if (k < 0) |
| 11852 | goto error; |
| 11853 | |
| 11854 | pos = isl_basic_map_offset(bmap, isl_dim_out); |
| 11855 | isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos); |
| 11856 | isl_int_neg(bmap->eq[k][pos], aff->v->el[0]); |
| 11857 | isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos, |
| 11858 | aff->v->size - (pos + 1)); |
| 11859 | |
| 11860 | isl_aff_free(aff); |
| 11861 | bmap = isl_basic_map_finalize(bmap); |
| 11862 | return bmap; |
| 11863 | error: |
| 11864 | isl_aff_free(aff); |
| 11865 | isl_basic_map_free(bmap); |
| 11866 | return NULL; |
| 11867 | } |
| 11868 | |
| 11869 | /* Construct a map mapping the domain of the affine expression |
| 11870 | * to a one-dimensional range prescribed by the affine expression. |
| 11871 | */ |
| 11872 | __isl_give isl_map *isl_map_from_aff(__isl_take isl_aff *aff) |
| 11873 | { |
| 11874 | isl_basic_map *bmap; |
| 11875 | |
| 11876 | bmap = isl_basic_map_from_aff(aff); |
| 11877 | return isl_map_from_basic_map(bmap); |
| 11878 | } |
| 11879 | |
| 11880 | /* Construct a basic map mapping the domain the multi-affine expression |
| 11881 | * to its range, with each dimension in the range equated to the |
| 11882 | * corresponding affine expression. |
| 11883 | */ |
| 11884 | __isl_give isl_basic_map *isl_basic_map_from_multi_aff( |
| 11885 | __isl_take isl_multi_aff *maff) |
| 11886 | { |
| 11887 | int i; |
| 11888 | isl_space *space; |
| 11889 | isl_basic_map *bmap; |
| 11890 | |
| 11891 | if (!maff) |
| 11892 | return NULL; |
| 11893 | |
| 11894 | if (isl_space_dim(maff->space, isl_dim_out) != maff->n) |
| 11895 | isl_die(isl_multi_aff_get_ctx(maff), isl_error_internal, |
| 11896 | "invalid space", goto error); |
| 11897 | |
| 11898 | space = isl_space_domain(isl_multi_aff_get_space(maff)); |
| 11899 | bmap = isl_basic_map_universe(isl_space_from_domain(space)); |
| 11900 | |
| 11901 | for (i = 0; i < maff->n; ++i) { |
| 11902 | isl_aff *aff; |
| 11903 | isl_basic_map *bmap_i; |
| 11904 | |
| 11905 | aff = isl_aff_copy(maff->p[i]); |
| 11906 | bmap_i = isl_basic_map_from_aff(aff); |
| 11907 | |
| 11908 | bmap = isl_basic_map_flat_range_product(bmap, bmap_i); |
| 11909 | } |
| 11910 | |
| 11911 | bmap = isl_basic_map_reset_space(bmap, isl_multi_aff_get_space(maff)); |
| 11912 | |
| 11913 | isl_multi_aff_free(maff); |
| 11914 | return bmap; |
| 11915 | error: |
| 11916 | isl_multi_aff_free(maff); |
| 11917 | return NULL; |
| 11918 | } |
| 11919 | |
| 11920 | /* Construct a map mapping the domain the multi-affine expression |
| 11921 | * to its range, with each dimension in the range equated to the |
| 11922 | * corresponding affine expression. |
| 11923 | */ |
| 11924 | __isl_give isl_map *isl_map_from_multi_aff(__isl_take isl_multi_aff *maff) |
| 11925 | { |
| 11926 | isl_basic_map *bmap; |
| 11927 | |
| 11928 | bmap = isl_basic_map_from_multi_aff(maff); |
| 11929 | return isl_map_from_basic_map(bmap); |
| 11930 | } |
| 11931 | |
| 11932 | /* Construct a basic map mapping a domain in the given space to |
| 11933 | * to an n-dimensional range, with n the number of elements in the list, |
| 11934 | * where each coordinate in the range is prescribed by the |
| 11935 | * corresponding affine expression. |
| 11936 | * The domains of all affine expressions in the list are assumed to match |
| 11937 | * domain_dim. |
| 11938 | */ |
| 11939 | __isl_give isl_basic_map *isl_basic_map_from_aff_list( |
| 11940 | __isl_take isl_space *domain_dim, __isl_take isl_aff_list *list) |
| 11941 | { |
| 11942 | int i; |
| 11943 | isl_space *dim; |
| 11944 | isl_basic_map *bmap; |
| 11945 | |
| 11946 | if (!list) |
| 11947 | return NULL; |
| 11948 | |
| 11949 | dim = isl_space_from_domain(domain_dim); |
| 11950 | bmap = isl_basic_map_universe(dim); |
| 11951 | |
| 11952 | for (i = 0; i < list->n; ++i) { |
| 11953 | isl_aff *aff; |
| 11954 | isl_basic_map *bmap_i; |
| 11955 | |
| 11956 | aff = isl_aff_copy(list->p[i]); |
| 11957 | bmap_i = isl_basic_map_from_aff(aff); |
| 11958 | |
| 11959 | bmap = isl_basic_map_flat_range_product(bmap, bmap_i); |
| 11960 | } |
| 11961 | |
| 11962 | isl_aff_list_free(list); |
| 11963 | return bmap; |
| 11964 | } |
| 11965 | |
| 11966 | __isl_give isl_set *isl_set_equate(__isl_take isl_set *set, |
| 11967 | enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2) |
| 11968 | { |
| 11969 | return isl_map_equate(set, type1, pos1, type2, pos2); |
| 11970 | } |
| 11971 | |
| 11972 | /* Construct a basic map where the given dimensions are equal to each other. |
| 11973 | */ |
| 11974 | static __isl_give isl_basic_map *equator(__isl_take isl_space *space, |
| 11975 | enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2) |
| 11976 | { |
| 11977 | isl_basic_map *bmap = NULL; |
| 11978 | int i; |
| 11979 | |
| 11980 | if (!space) |
| 11981 | return NULL; |
| 11982 | |
| 11983 | if (pos1 >= isl_space_dim(space, type1)) |
| 11984 | isl_die(isl_space_get_ctx(space), isl_error_invalid, |
| 11985 | "index out of bounds", goto error); |
| 11986 | if (pos2 >= isl_space_dim(space, type2)) |
| 11987 | isl_die(isl_space_get_ctx(space), isl_error_invalid, |
| 11988 | "index out of bounds", goto error); |
| 11989 | |
| 11990 | if (type1 == type2 && pos1 == pos2) |
| 11991 | return isl_basic_map_universe(space); |
| 11992 | |
| 11993 | bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0); |
| 11994 | i = isl_basic_map_alloc_equality(bmap); |
| 11995 | if (i < 0) |
| 11996 | goto error; |
| 11997 | isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap)); |
| 11998 | pos1 += isl_basic_map_offset(bmap, type1); |
| 11999 | pos2 += isl_basic_map_offset(bmap, type2); |
| 12000 | isl_int_set_si(bmap->eq[i][pos1], -1); |
| 12001 | isl_int_set_si(bmap->eq[i][pos2], 1); |
| 12002 | bmap = isl_basic_map_finalize(bmap); |
| 12003 | isl_space_free(space); |
| 12004 | return bmap; |
| 12005 | error: |
| 12006 | isl_space_free(space); |
| 12007 | isl_basic_map_free(bmap); |
| 12008 | return NULL; |
| 12009 | } |
| 12010 | |
| 12011 | /* Add a constraint imposing that the given two dimensions are equal. |
| 12012 | */ |
| 12013 | __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap, |
| 12014 | enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2) |
| 12015 | { |
| 12016 | isl_basic_map *eq; |
| 12017 | |
| 12018 | eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2); |
| 12019 | |
| 12020 | bmap = isl_basic_map_intersect(bmap, eq); |
| 12021 | |
| 12022 | return bmap; |
| 12023 | } |
| 12024 | |
| 12025 | /* Add a constraint imposing that the given two dimensions are equal. |
| 12026 | */ |
| 12027 | __isl_give isl_map *isl_map_equate(__isl_take isl_map *map, |
| 12028 | enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2) |
| 12029 | { |
| 12030 | isl_basic_map *bmap; |
| 12031 | |
| 12032 | bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2); |
| 12033 | |
| 12034 | map = isl_map_intersect(map, isl_map_from_basic_map(bmap)); |
| 12035 | |
| 12036 | return map; |
| 12037 | } |
| 12038 | |
| 12039 | /* Add a constraint imposing that the given two dimensions have opposite values. |
| 12040 | */ |
| 12041 | __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map, |
| 12042 | enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2) |
| 12043 | { |
| 12044 | isl_basic_map *bmap = NULL; |
| 12045 | int i; |
| 12046 | |
| 12047 | if (!map) |
| 12048 | return NULL; |
| 12049 | |
| 12050 | if (pos1 >= isl_map_dim(map, type1)) |
| 12051 | isl_die(map->ctx, isl_error_invalid, |
| 12052 | "index out of bounds", goto error); |
| 12053 | if (pos2 >= isl_map_dim(map, type2)) |
| 12054 | isl_die(map->ctx, isl_error_invalid, |
| 12055 | "index out of bounds", goto error); |
| 12056 | |
| 12057 | bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0); |
| 12058 | i = isl_basic_map_alloc_equality(bmap); |
| 12059 | if (i < 0) |
| 12060 | goto error; |
| 12061 | isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap)); |
| 12062 | pos1 += isl_basic_map_offset(bmap, type1); |
| 12063 | pos2 += isl_basic_map_offset(bmap, type2); |
| 12064 | isl_int_set_si(bmap->eq[i][pos1], 1); |
| 12065 | isl_int_set_si(bmap->eq[i][pos2], 1); |
| 12066 | bmap = isl_basic_map_finalize(bmap); |
| 12067 | |
| 12068 | map = isl_map_intersect(map, isl_map_from_basic_map(bmap)); |
| 12069 | |
| 12070 | return map; |
| 12071 | error: |
| 12072 | isl_basic_map_free(bmap); |
| 12073 | isl_map_free(map); |
| 12074 | return NULL; |
| 12075 | } |
| 12076 | |
| 12077 | /* Construct a constraint imposing that the value of the first dimension is |
| 12078 | * greater than or equal to that of the second. |
| 12079 | */ |
| 12080 | static __isl_give isl_constraint *constraint_order_ge( |
| 12081 | __isl_take isl_space *space, enum isl_dim_type type1, int pos1, |
| 12082 | enum isl_dim_type type2, int pos2) |
| 12083 | { |
| 12084 | isl_constraint *c; |
| 12085 | |
| 12086 | if (!space) |
| 12087 | return NULL; |
| 12088 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 12089 | c = isl_constraint_alloc_inequality(isl_local_space_from_space(space)); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 12090 | |
| 12091 | if (pos1 >= isl_constraint_dim(c, type1)) |
| 12092 | isl_die(isl_constraint_get_ctx(c), isl_error_invalid, |
| 12093 | "index out of bounds", return isl_constraint_free(c)); |
| 12094 | if (pos2 >= isl_constraint_dim(c, type2)) |
| 12095 | isl_die(isl_constraint_get_ctx(c), isl_error_invalid, |
| 12096 | "index out of bounds", return isl_constraint_free(c)); |
| 12097 | |
| 12098 | if (type1 == type2 && pos1 == pos2) |
| 12099 | return c; |
| 12100 | |
| 12101 | c = isl_constraint_set_coefficient_si(c, type1, pos1, 1); |
| 12102 | c = isl_constraint_set_coefficient_si(c, type2, pos2, -1); |
| 12103 | |
| 12104 | return c; |
| 12105 | } |
| 12106 | |
| 12107 | /* Add a constraint imposing that the value of the first dimension is |
| 12108 | * greater than or equal to that of the second. |
| 12109 | */ |
| 12110 | __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap, |
| 12111 | enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2) |
| 12112 | { |
| 12113 | isl_constraint *c; |
| 12114 | isl_space *space; |
| 12115 | |
| 12116 | if (type1 == type2 && pos1 == pos2) |
| 12117 | return bmap; |
| 12118 | space = isl_basic_map_get_space(bmap); |
| 12119 | c = constraint_order_ge(space, type1, pos1, type2, pos2); |
| 12120 | bmap = isl_basic_map_add_constraint(bmap, c); |
| 12121 | |
| 12122 | return bmap; |
| 12123 | } |
| 12124 | |
| 12125 | /* Add a constraint imposing that the value of the first dimension is |
| 12126 | * greater than or equal to that of the second. |
| 12127 | */ |
| 12128 | __isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map, |
| 12129 | enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2) |
| 12130 | { |
| 12131 | isl_constraint *c; |
| 12132 | isl_space *space; |
| 12133 | |
| 12134 | if (type1 == type2 && pos1 == pos2) |
| 12135 | return map; |
| 12136 | space = isl_map_get_space(map); |
| 12137 | c = constraint_order_ge(space, type1, pos1, type2, pos2); |
| 12138 | map = isl_map_add_constraint(map, c); |
| 12139 | |
| 12140 | return map; |
| 12141 | } |
| 12142 | |
| 12143 | /* Add a constraint imposing that the value of the first dimension is |
| 12144 | * less than or equal to that of the second. |
| 12145 | */ |
| 12146 | __isl_give isl_map *isl_map_order_le(__isl_take isl_map *map, |
| 12147 | enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2) |
| 12148 | { |
| 12149 | return isl_map_order_ge(map, type2, pos2, type1, pos1); |
| 12150 | } |
| 12151 | |
| 12152 | /* Construct a basic map where the value of the first dimension is |
| 12153 | * greater than that of the second. |
| 12154 | */ |
| 12155 | static __isl_give isl_basic_map *greator(__isl_take isl_space *space, |
| 12156 | enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2) |
| 12157 | { |
| 12158 | isl_basic_map *bmap = NULL; |
| 12159 | int i; |
| 12160 | |
| 12161 | if (!space) |
| 12162 | return NULL; |
| 12163 | |
| 12164 | if (pos1 >= isl_space_dim(space, type1)) |
| 12165 | isl_die(isl_space_get_ctx(space), isl_error_invalid, |
| 12166 | "index out of bounds", goto error); |
| 12167 | if (pos2 >= isl_space_dim(space, type2)) |
| 12168 | isl_die(isl_space_get_ctx(space), isl_error_invalid, |
| 12169 | "index out of bounds", goto error); |
| 12170 | |
| 12171 | if (type1 == type2 && pos1 == pos2) |
| 12172 | return isl_basic_map_empty(space); |
| 12173 | |
| 12174 | bmap = isl_basic_map_alloc_space(space, 0, 0, 1); |
| 12175 | i = isl_basic_map_alloc_inequality(bmap); |
| 12176 | if (i < 0) |
| 12177 | return isl_basic_map_free(bmap); |
| 12178 | isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap)); |
| 12179 | pos1 += isl_basic_map_offset(bmap, type1); |
| 12180 | pos2 += isl_basic_map_offset(bmap, type2); |
| 12181 | isl_int_set_si(bmap->ineq[i][pos1], 1); |
| 12182 | isl_int_set_si(bmap->ineq[i][pos2], -1); |
| 12183 | isl_int_set_si(bmap->ineq[i][0], -1); |
| 12184 | bmap = isl_basic_map_finalize(bmap); |
| 12185 | |
| 12186 | return bmap; |
| 12187 | error: |
| 12188 | isl_space_free(space); |
| 12189 | isl_basic_map_free(bmap); |
| 12190 | return NULL; |
| 12191 | } |
| 12192 | |
| 12193 | /* Add a constraint imposing that the value of the first dimension is |
| 12194 | * greater than that of the second. |
| 12195 | */ |
| 12196 | __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap, |
| 12197 | enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2) |
| 12198 | { |
| 12199 | isl_basic_map *gt; |
| 12200 | |
| 12201 | gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2); |
| 12202 | |
| 12203 | bmap = isl_basic_map_intersect(bmap, gt); |
| 12204 | |
| 12205 | return bmap; |
| 12206 | } |
| 12207 | |
| 12208 | /* Add a constraint imposing that the value of the first dimension is |
| 12209 | * greater than that of the second. |
| 12210 | */ |
| 12211 | __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map, |
| 12212 | enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2) |
| 12213 | { |
| 12214 | isl_basic_map *bmap; |
| 12215 | |
| 12216 | bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2); |
| 12217 | |
| 12218 | map = isl_map_intersect(map, isl_map_from_basic_map(bmap)); |
| 12219 | |
| 12220 | return map; |
| 12221 | } |
| 12222 | |
| 12223 | /* Add a constraint imposing that the value of the first dimension is |
| 12224 | * smaller than that of the second. |
| 12225 | */ |
| 12226 | __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map, |
| 12227 | enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2) |
| 12228 | { |
| 12229 | return isl_map_order_gt(map, type2, pos2, type1, pos1); |
| 12230 | } |
| 12231 | |
| 12232 | __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap, |
| 12233 | int pos) |
| 12234 | { |
| 12235 | isl_aff *div; |
| 12236 | isl_local_space *ls; |
| 12237 | |
| 12238 | if (!bmap) |
| 12239 | return NULL; |
| 12240 | |
| 12241 | if (!isl_basic_map_divs_known(bmap)) |
| 12242 | isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid, |
| 12243 | "some divs are unknown", return NULL); |
| 12244 | |
| 12245 | ls = isl_basic_map_get_local_space(bmap); |
| 12246 | div = isl_local_space_get_div(ls, pos); |
| 12247 | isl_local_space_free(ls); |
| 12248 | |
| 12249 | return div; |
| 12250 | } |
| 12251 | |
| 12252 | __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset, |
| 12253 | int pos) |
| 12254 | { |
| 12255 | return isl_basic_map_get_div(bset, pos); |
| 12256 | } |
| 12257 | |
| 12258 | /* Plug in "subs" for dimension "type", "pos" of "bset". |
| 12259 | * |
| 12260 | * Let i be the dimension to replace and let "subs" be of the form |
| 12261 | * |
| 12262 | * f/d |
| 12263 | * |
| 12264 | * Any integer division with a non-zero coefficient for i, |
| 12265 | * |
| 12266 | * floor((a i + g)/m) |
| 12267 | * |
| 12268 | * is replaced by |
| 12269 | * |
| 12270 | * floor((a f + d g)/(m d)) |
| 12271 | * |
| 12272 | * Constraints of the form |
| 12273 | * |
| 12274 | * a i + g |
| 12275 | * |
| 12276 | * are replaced by |
| 12277 | * |
| 12278 | * a f + d g |
| 12279 | * |
| 12280 | * We currently require that "subs" is an integral expression. |
| 12281 | * Handling rational expressions may require us to add stride constraints |
| 12282 | * as we do in isl_basic_set_preimage_multi_aff. |
| 12283 | */ |
| 12284 | __isl_give isl_basic_set *isl_basic_set_substitute( |
| 12285 | __isl_take isl_basic_set *bset, |
| 12286 | enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs) |
| 12287 | { |
| 12288 | int i; |
| 12289 | isl_int v; |
| 12290 | isl_ctx *ctx; |
| 12291 | |
| 12292 | if (bset && isl_basic_set_plain_is_empty(bset)) |
| 12293 | return bset; |
| 12294 | |
| 12295 | bset = isl_basic_set_cow(bset); |
| 12296 | if (!bset || !subs) |
| 12297 | goto error; |
| 12298 | |
| 12299 | ctx = isl_basic_set_get_ctx(bset); |
| 12300 | if (!isl_space_is_equal(bset->dim, subs->ls->dim)) |
| 12301 | isl_die(ctx, isl_error_invalid, |
| 12302 | "spaces don't match", goto error); |
| 12303 | if (isl_local_space_dim(subs->ls, isl_dim_div) != 0) |
| 12304 | isl_die(ctx, isl_error_unsupported, |
| 12305 | "cannot handle divs yet", goto error); |
| 12306 | if (!isl_int_is_one(subs->v->el[0])) |
| 12307 | isl_die(ctx, isl_error_invalid, |
| 12308 | "can only substitute integer expressions", goto error); |
| 12309 | |
| 12310 | pos += isl_basic_set_offset(bset, type); |
| 12311 | |
| 12312 | isl_int_init(v); |
| 12313 | |
| 12314 | for (i = 0; i < bset->n_eq; ++i) { |
| 12315 | if (isl_int_is_zero(bset->eq[i][pos])) |
| 12316 | continue; |
| 12317 | isl_int_set(v, bset->eq[i][pos]); |
| 12318 | isl_int_set_si(bset->eq[i][pos], 0); |
| 12319 | isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i], |
| 12320 | v, subs->v->el + 1, subs->v->size - 1); |
| 12321 | } |
| 12322 | |
| 12323 | for (i = 0; i < bset->n_ineq; ++i) { |
| 12324 | if (isl_int_is_zero(bset->ineq[i][pos])) |
| 12325 | continue; |
| 12326 | isl_int_set(v, bset->ineq[i][pos]); |
| 12327 | isl_int_set_si(bset->ineq[i][pos], 0); |
| 12328 | isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i], |
| 12329 | v, subs->v->el + 1, subs->v->size - 1); |
| 12330 | } |
| 12331 | |
| 12332 | for (i = 0; i < bset->n_div; ++i) { |
| 12333 | if (isl_int_is_zero(bset->div[i][1 + pos])) |
| 12334 | continue; |
| 12335 | isl_int_set(v, bset->div[i][1 + pos]); |
| 12336 | isl_int_set_si(bset->div[i][1 + pos], 0); |
| 12337 | isl_seq_combine(bset->div[i] + 1, |
| 12338 | subs->v->el[0], bset->div[i] + 1, |
| 12339 | v, subs->v->el + 1, subs->v->size - 1); |
| 12340 | isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]); |
| 12341 | } |
| 12342 | |
| 12343 | isl_int_clear(v); |
| 12344 | |
| 12345 | bset = isl_basic_set_simplify(bset); |
| 12346 | return isl_basic_set_finalize(bset); |
| 12347 | error: |
| 12348 | isl_basic_set_free(bset); |
| 12349 | return NULL; |
| 12350 | } |
| 12351 | |
| 12352 | /* Plug in "subs" for dimension "type", "pos" of "set". |
| 12353 | */ |
| 12354 | __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set, |
| 12355 | enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs) |
| 12356 | { |
| 12357 | int i; |
| 12358 | |
| 12359 | if (set && isl_set_plain_is_empty(set)) |
| 12360 | return set; |
| 12361 | |
| 12362 | set = isl_set_cow(set); |
| 12363 | if (!set || !subs) |
| 12364 | goto error; |
| 12365 | |
| 12366 | for (i = set->n - 1; i >= 0; --i) { |
| 12367 | set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs); |
| 12368 | if (remove_if_empty(set, i) < 0) |
| 12369 | goto error; |
| 12370 | } |
| 12371 | |
| 12372 | return set; |
| 12373 | error: |
| 12374 | isl_set_free(set); |
| 12375 | return NULL; |
| 12376 | } |
| 12377 | |
| 12378 | /* Check if the range of "ma" is compatible with the domain or range |
| 12379 | * (depending on "type") of "bmap". |
| 12380 | * Return -1 if anything is wrong. |
| 12381 | */ |
| 12382 | static int check_basic_map_compatible_range_multi_aff( |
| 12383 | __isl_keep isl_basic_map *bmap, enum isl_dim_type type, |
| 12384 | __isl_keep isl_multi_aff *ma) |
| 12385 | { |
| 12386 | int m; |
| 12387 | isl_space *ma_space; |
| 12388 | |
| 12389 | ma_space = isl_multi_aff_get_space(ma); |
| 12390 | |
| 12391 | m = isl_space_match(bmap->dim, isl_dim_param, ma_space, isl_dim_param); |
| 12392 | if (m < 0) |
| 12393 | goto error; |
| 12394 | if (!m) |
| 12395 | isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid, |
| 12396 | "parameters don't match", goto error); |
| 12397 | m = isl_space_tuple_is_equal(bmap->dim, type, ma_space, isl_dim_out); |
| 12398 | if (m < 0) |
| 12399 | goto error; |
| 12400 | if (!m) |
| 12401 | isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid, |
| 12402 | "spaces don't match", goto error); |
| 12403 | |
| 12404 | isl_space_free(ma_space); |
| 12405 | return m; |
| 12406 | error: |
| 12407 | isl_space_free(ma_space); |
| 12408 | return -1; |
| 12409 | } |
| 12410 | |
| 12411 | /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before" |
| 12412 | * coefficients before the transformed range of dimensions, |
| 12413 | * the "n_after" coefficients after the transformed range of dimensions |
| 12414 | * and the coefficients of the other divs in "bmap". |
| 12415 | */ |
| 12416 | static int set_ma_divs(__isl_keep isl_basic_map *bmap, |
| 12417 | __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div) |
| 12418 | { |
| 12419 | int i; |
| 12420 | int n_param; |
| 12421 | int n_set; |
| 12422 | isl_local_space *ls; |
| 12423 | |
| 12424 | if (n_div == 0) |
| 12425 | return 0; |
| 12426 | |
| 12427 | ls = isl_aff_get_domain_local_space(ma->p[0]); |
| 12428 | if (!ls) |
| 12429 | return -1; |
| 12430 | |
| 12431 | n_param = isl_local_space_dim(ls, isl_dim_param); |
| 12432 | n_set = isl_local_space_dim(ls, isl_dim_set); |
| 12433 | for (i = 0; i < n_div; ++i) { |
| 12434 | int o_bmap = 0, o_ls = 0; |
| 12435 | |
| 12436 | isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param); |
| 12437 | o_bmap += 1 + 1 + n_param; |
| 12438 | o_ls += 1 + 1 + n_param; |
| 12439 | isl_seq_clr(bmap->div[i] + o_bmap, n_before); |
| 12440 | o_bmap += n_before; |
| 12441 | isl_seq_cpy(bmap->div[i] + o_bmap, |
| 12442 | ls->div->row[i] + o_ls, n_set); |
| 12443 | o_bmap += n_set; |
| 12444 | o_ls += n_set; |
| 12445 | isl_seq_clr(bmap->div[i] + o_bmap, n_after); |
| 12446 | o_bmap += n_after; |
| 12447 | isl_seq_cpy(bmap->div[i] + o_bmap, |
| 12448 | ls->div->row[i] + o_ls, n_div); |
| 12449 | o_bmap += n_div; |
| 12450 | o_ls += n_div; |
| 12451 | isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div); |
| 12452 | if (isl_basic_set_add_div_constraints(bmap, i) < 0) |
| 12453 | goto error; |
| 12454 | } |
| 12455 | |
| 12456 | isl_local_space_free(ls); |
| 12457 | return 0; |
| 12458 | error: |
| 12459 | isl_local_space_free(ls); |
| 12460 | return -1; |
| 12461 | } |
| 12462 | |
| 12463 | /* How many stride constraints does "ma" enforce? |
| 12464 | * That is, how many of the affine expressions have a denominator |
| 12465 | * different from one? |
| 12466 | */ |
| 12467 | static int multi_aff_strides(__isl_keep isl_multi_aff *ma) |
| 12468 | { |
| 12469 | int i; |
| 12470 | int strides = 0; |
| 12471 | |
| 12472 | for (i = 0; i < ma->n; ++i) |
| 12473 | if (!isl_int_is_one(ma->p[i]->v->el[0])) |
| 12474 | strides++; |
| 12475 | |
| 12476 | return strides; |
| 12477 | } |
| 12478 | |
| 12479 | /* For each affine expression in ma of the form |
| 12480 | * |
| 12481 | * x_i = (f_i y + h_i)/m_i |
| 12482 | * |
| 12483 | * with m_i different from one, add a constraint to "bmap" |
| 12484 | * of the form |
| 12485 | * |
| 12486 | * f_i y + h_i = m_i alpha_i |
| 12487 | * |
| 12488 | * with alpha_i an additional existentially quantified variable. |
Tobias Grosser | a67ac97 | 2016-02-26 11:35:12 +0000 | [diff] [blame] | 12489 | * |
| 12490 | * The input variables of "ma" correspond to a subset of the variables |
| 12491 | * of "bmap". There are "n_before" variables in "bmap" before this |
| 12492 | * subset and "n_after" variables after this subset. |
| 12493 | * The integer divisions of the affine expressions in "ma" are assumed |
| 12494 | * to have been aligned. There are "n_div_ma" of them and |
| 12495 | * they appear first in "bmap", straight after the "n_after" variables. |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 12496 | */ |
| 12497 | static __isl_give isl_basic_map *add_ma_strides( |
| 12498 | __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma, |
Tobias Grosser | a67ac97 | 2016-02-26 11:35:12 +0000 | [diff] [blame] | 12499 | int n_before, int n_after, int n_div_ma) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 12500 | { |
| 12501 | int i, k; |
| 12502 | int div; |
| 12503 | int total; |
| 12504 | int n_param; |
| 12505 | int n_in; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 12506 | |
| 12507 | total = isl_basic_map_total_dim(bmap); |
| 12508 | n_param = isl_multi_aff_dim(ma, isl_dim_param); |
| 12509 | n_in = isl_multi_aff_dim(ma, isl_dim_in); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 12510 | for (i = 0; i < ma->n; ++i) { |
| 12511 | int o_bmap = 0, o_ma = 1; |
| 12512 | |
| 12513 | if (isl_int_is_one(ma->p[i]->v->el[0])) |
| 12514 | continue; |
| 12515 | div = isl_basic_map_alloc_div(bmap); |
| 12516 | k = isl_basic_map_alloc_equality(bmap); |
| 12517 | if (div < 0 || k < 0) |
| 12518 | goto error; |
| 12519 | isl_int_set_si(bmap->div[div][0], 0); |
| 12520 | isl_seq_cpy(bmap->eq[k] + o_bmap, |
| 12521 | ma->p[i]->v->el + o_ma, 1 + n_param); |
| 12522 | o_bmap += 1 + n_param; |
| 12523 | o_ma += 1 + n_param; |
| 12524 | isl_seq_clr(bmap->eq[k] + o_bmap, n_before); |
| 12525 | o_bmap += n_before; |
| 12526 | isl_seq_cpy(bmap->eq[k] + o_bmap, |
| 12527 | ma->p[i]->v->el + o_ma, n_in); |
| 12528 | o_bmap += n_in; |
| 12529 | o_ma += n_in; |
| 12530 | isl_seq_clr(bmap->eq[k] + o_bmap, n_after); |
| 12531 | o_bmap += n_after; |
| 12532 | isl_seq_cpy(bmap->eq[k] + o_bmap, |
Tobias Grosser | a67ac97 | 2016-02-26 11:35:12 +0000 | [diff] [blame] | 12533 | ma->p[i]->v->el + o_ma, n_div_ma); |
| 12534 | o_bmap += n_div_ma; |
| 12535 | o_ma += n_div_ma; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 12536 | isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap); |
| 12537 | isl_int_neg(bmap->eq[k][1 + total], ma->p[i]->v->el[0]); |
| 12538 | total++; |
| 12539 | } |
| 12540 | |
| 12541 | return bmap; |
| 12542 | error: |
| 12543 | isl_basic_map_free(bmap); |
| 12544 | return NULL; |
| 12545 | } |
| 12546 | |
| 12547 | /* Replace the domain or range space (depending on "type) of "space" by "set". |
| 12548 | */ |
| 12549 | static __isl_give isl_space *isl_space_set(__isl_take isl_space *space, |
| 12550 | enum isl_dim_type type, __isl_take isl_space *set) |
| 12551 | { |
| 12552 | if (type == isl_dim_in) { |
| 12553 | space = isl_space_range(space); |
| 12554 | space = isl_space_map_from_domain_and_range(set, space); |
| 12555 | } else { |
| 12556 | space = isl_space_domain(space); |
| 12557 | space = isl_space_map_from_domain_and_range(space, set); |
| 12558 | } |
| 12559 | |
| 12560 | return space; |
| 12561 | } |
| 12562 | |
| 12563 | /* Compute the preimage of the domain or range (depending on "type") |
| 12564 | * of "bmap" under the function represented by "ma". |
| 12565 | * In other words, plug in "ma" in the domain or range of "bmap". |
| 12566 | * The result is a basic map that lives in the same space as "bmap" |
| 12567 | * except that the domain or range has been replaced by |
| 12568 | * the domain space of "ma". |
| 12569 | * |
| 12570 | * If bmap is represented by |
| 12571 | * |
| 12572 | * A(p) + S u + B x + T v + C(divs) >= 0, |
| 12573 | * |
| 12574 | * where u and x are input and output dimensions if type == isl_dim_out |
| 12575 | * while x and v are input and output dimensions if type == isl_dim_in, |
| 12576 | * and ma is represented by |
| 12577 | * |
| 12578 | * x = D(p) + F(y) + G(divs') |
| 12579 | * |
| 12580 | * then the result is |
| 12581 | * |
| 12582 | * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0 |
| 12583 | * |
| 12584 | * The divs in the input set are similarly adjusted. |
| 12585 | * In particular |
| 12586 | * |
| 12587 | * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i) |
| 12588 | * |
| 12589 | * becomes |
| 12590 | * |
| 12591 | * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v + |
| 12592 | * B_i G(divs') + c_i(divs))/n_i) |
| 12593 | * |
| 12594 | * If bmap is not a rational map and if F(y) involves any denominators |
| 12595 | * |
| 12596 | * x_i = (f_i y + h_i)/m_i |
| 12597 | * |
| 12598 | * then additional constraints are added to ensure that we only |
| 12599 | * map back integer points. That is we enforce |
| 12600 | * |
| 12601 | * f_i y + h_i = m_i alpha_i |
| 12602 | * |
| 12603 | * with alpha_i an additional existentially quantified variable. |
| 12604 | * |
| 12605 | * We first copy over the divs from "ma". |
| 12606 | * Then we add the modified constraints and divs from "bmap". |
| 12607 | * Finally, we add the stride constraints, if needed. |
| 12608 | */ |
| 12609 | __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff( |
| 12610 | __isl_take isl_basic_map *bmap, enum isl_dim_type type, |
| 12611 | __isl_take isl_multi_aff *ma) |
| 12612 | { |
| 12613 | int i, k; |
| 12614 | isl_space *space; |
| 12615 | isl_basic_map *res = NULL; |
| 12616 | int n_before, n_after, n_div_bmap, n_div_ma; |
| 12617 | isl_int f, c1, c2, g; |
| 12618 | int rational, strides; |
| 12619 | |
| 12620 | isl_int_init(f); |
| 12621 | isl_int_init(c1); |
| 12622 | isl_int_init(c2); |
| 12623 | isl_int_init(g); |
| 12624 | |
| 12625 | ma = isl_multi_aff_align_divs(ma); |
| 12626 | if (!bmap || !ma) |
| 12627 | goto error; |
| 12628 | if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0) |
| 12629 | goto error; |
| 12630 | |
| 12631 | if (type == isl_dim_in) { |
| 12632 | n_before = 0; |
| 12633 | n_after = isl_basic_map_dim(bmap, isl_dim_out); |
| 12634 | } else { |
| 12635 | n_before = isl_basic_map_dim(bmap, isl_dim_in); |
| 12636 | n_after = 0; |
| 12637 | } |
| 12638 | n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div); |
| 12639 | n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0; |
| 12640 | |
| 12641 | space = isl_multi_aff_get_domain_space(ma); |
| 12642 | space = isl_space_set(isl_basic_map_get_space(bmap), type, space); |
| 12643 | rational = isl_basic_map_is_rational(bmap); |
| 12644 | strides = rational ? 0 : multi_aff_strides(ma); |
| 12645 | res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides, |
| 12646 | bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma); |
| 12647 | if (rational) |
| 12648 | res = isl_basic_map_set_rational(res); |
| 12649 | |
| 12650 | for (i = 0; i < n_div_ma + n_div_bmap; ++i) |
| 12651 | if (isl_basic_map_alloc_div(res) < 0) |
| 12652 | goto error; |
| 12653 | |
| 12654 | if (set_ma_divs(res, ma, n_before, n_after, n_div_ma) < 0) |
| 12655 | goto error; |
| 12656 | |
| 12657 | for (i = 0; i < bmap->n_eq; ++i) { |
| 12658 | k = isl_basic_map_alloc_equality(res); |
| 12659 | if (k < 0) |
| 12660 | goto error; |
| 12661 | isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before, |
| 12662 | n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0); |
| 12663 | } |
| 12664 | |
| 12665 | for (i = 0; i < bmap->n_ineq; ++i) { |
| 12666 | k = isl_basic_map_alloc_inequality(res); |
| 12667 | if (k < 0) |
| 12668 | goto error; |
| 12669 | isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before, |
| 12670 | n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0); |
| 12671 | } |
| 12672 | |
| 12673 | for (i = 0; i < bmap->n_div; ++i) { |
| 12674 | if (isl_int_is_zero(bmap->div[i][0])) { |
| 12675 | isl_int_set_si(res->div[n_div_ma + i][0], 0); |
| 12676 | continue; |
| 12677 | } |
| 12678 | isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma, |
| 12679 | n_before, n_after, n_div_ma, n_div_bmap, |
| 12680 | f, c1, c2, g, 1); |
| 12681 | } |
| 12682 | |
| 12683 | if (strides) |
Tobias Grosser | a67ac97 | 2016-02-26 11:35:12 +0000 | [diff] [blame] | 12684 | res = add_ma_strides(res, ma, n_before, n_after, n_div_ma); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 12685 | |
| 12686 | isl_int_clear(f); |
| 12687 | isl_int_clear(c1); |
| 12688 | isl_int_clear(c2); |
| 12689 | isl_int_clear(g); |
| 12690 | isl_basic_map_free(bmap); |
| 12691 | isl_multi_aff_free(ma); |
| 12692 | res = isl_basic_set_simplify(res); |
| 12693 | return isl_basic_map_finalize(res); |
| 12694 | error: |
| 12695 | isl_int_clear(f); |
| 12696 | isl_int_clear(c1); |
| 12697 | isl_int_clear(c2); |
| 12698 | isl_int_clear(g); |
| 12699 | isl_basic_map_free(bmap); |
| 12700 | isl_multi_aff_free(ma); |
| 12701 | isl_basic_map_free(res); |
| 12702 | return NULL; |
| 12703 | } |
| 12704 | |
| 12705 | /* Compute the preimage of "bset" under the function represented by "ma". |
| 12706 | * In other words, plug in "ma" in "bset". The result is a basic set |
| 12707 | * that lives in the domain space of "ma". |
| 12708 | */ |
| 12709 | __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff( |
| 12710 | __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma) |
| 12711 | { |
| 12712 | return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma); |
| 12713 | } |
| 12714 | |
| 12715 | /* Compute the preimage of the domain of "bmap" under the function |
| 12716 | * represented by "ma". |
| 12717 | * In other words, plug in "ma" in the domain of "bmap". |
| 12718 | * The result is a basic map that lives in the same space as "bmap" |
| 12719 | * except that the domain has been replaced by the domain space of "ma". |
| 12720 | */ |
| 12721 | __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff( |
| 12722 | __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma) |
| 12723 | { |
| 12724 | return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma); |
| 12725 | } |
| 12726 | |
| 12727 | /* Compute the preimage of the range of "bmap" under the function |
| 12728 | * represented by "ma". |
| 12729 | * In other words, plug in "ma" in the range of "bmap". |
| 12730 | * The result is a basic map that lives in the same space as "bmap" |
| 12731 | * except that the range has been replaced by the domain space of "ma". |
| 12732 | */ |
| 12733 | __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff( |
| 12734 | __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma) |
| 12735 | { |
| 12736 | return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma); |
| 12737 | } |
| 12738 | |
| 12739 | /* Check if the range of "ma" is compatible with the domain or range |
| 12740 | * (depending on "type") of "map". |
| 12741 | * Return -1 if anything is wrong. |
| 12742 | */ |
| 12743 | static int check_map_compatible_range_multi_aff( |
| 12744 | __isl_keep isl_map *map, enum isl_dim_type type, |
| 12745 | __isl_keep isl_multi_aff *ma) |
| 12746 | { |
| 12747 | int m; |
| 12748 | isl_space *ma_space; |
| 12749 | |
| 12750 | ma_space = isl_multi_aff_get_space(ma); |
| 12751 | m = isl_space_tuple_is_equal(map->dim, type, ma_space, isl_dim_out); |
| 12752 | isl_space_free(ma_space); |
| 12753 | if (m >= 0 && !m) |
| 12754 | isl_die(isl_map_get_ctx(map), isl_error_invalid, |
| 12755 | "spaces don't match", return -1); |
| 12756 | return m; |
| 12757 | } |
| 12758 | |
| 12759 | /* Compute the preimage of the domain or range (depending on "type") |
| 12760 | * of "map" under the function represented by "ma". |
| 12761 | * In other words, plug in "ma" in the domain or range of "map". |
| 12762 | * The result is a map that lives in the same space as "map" |
| 12763 | * except that the domain or range has been replaced by |
| 12764 | * the domain space of "ma". |
| 12765 | * |
| 12766 | * The parameters are assumed to have been aligned. |
| 12767 | */ |
| 12768 | static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map, |
| 12769 | enum isl_dim_type type, __isl_take isl_multi_aff *ma) |
| 12770 | { |
| 12771 | int i; |
| 12772 | isl_space *space; |
| 12773 | |
| 12774 | map = isl_map_cow(map); |
| 12775 | ma = isl_multi_aff_align_divs(ma); |
| 12776 | if (!map || !ma) |
| 12777 | goto error; |
| 12778 | if (check_map_compatible_range_multi_aff(map, type, ma) < 0) |
| 12779 | goto error; |
| 12780 | |
| 12781 | for (i = 0; i < map->n; ++i) { |
| 12782 | map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type, |
| 12783 | isl_multi_aff_copy(ma)); |
| 12784 | if (!map->p[i]) |
| 12785 | goto error; |
| 12786 | } |
| 12787 | |
| 12788 | space = isl_multi_aff_get_domain_space(ma); |
| 12789 | space = isl_space_set(isl_map_get_space(map), type, space); |
| 12790 | |
| 12791 | isl_space_free(map->dim); |
| 12792 | map->dim = space; |
| 12793 | if (!map->dim) |
| 12794 | goto error; |
| 12795 | |
| 12796 | isl_multi_aff_free(ma); |
| 12797 | if (map->n > 1) |
| 12798 | ISL_F_CLR(map, ISL_MAP_DISJOINT); |
| 12799 | ISL_F_CLR(map, ISL_SET_NORMALIZED); |
| 12800 | return map; |
| 12801 | error: |
| 12802 | isl_multi_aff_free(ma); |
| 12803 | isl_map_free(map); |
| 12804 | return NULL; |
| 12805 | } |
| 12806 | |
| 12807 | /* Compute the preimage of the domain or range (depending on "type") |
| 12808 | * of "map" under the function represented by "ma". |
| 12809 | * In other words, plug in "ma" in the domain or range of "map". |
| 12810 | * The result is a map that lives in the same space as "map" |
| 12811 | * except that the domain or range has been replaced by |
| 12812 | * the domain space of "ma". |
| 12813 | */ |
| 12814 | __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map, |
| 12815 | enum isl_dim_type type, __isl_take isl_multi_aff *ma) |
| 12816 | { |
| 12817 | if (!map || !ma) |
| 12818 | goto error; |
| 12819 | |
| 12820 | if (isl_space_match(map->dim, isl_dim_param, ma->space, isl_dim_param)) |
| 12821 | return map_preimage_multi_aff(map, type, ma); |
| 12822 | |
| 12823 | if (!isl_space_has_named_params(map->dim) || |
| 12824 | !isl_space_has_named_params(ma->space)) |
| 12825 | isl_die(map->ctx, isl_error_invalid, |
| 12826 | "unaligned unnamed parameters", goto error); |
| 12827 | map = isl_map_align_params(map, isl_multi_aff_get_space(ma)); |
| 12828 | ma = isl_multi_aff_align_params(ma, isl_map_get_space(map)); |
| 12829 | |
| 12830 | return map_preimage_multi_aff(map, type, ma); |
| 12831 | error: |
| 12832 | isl_multi_aff_free(ma); |
| 12833 | return isl_map_free(map); |
| 12834 | } |
| 12835 | |
| 12836 | /* Compute the preimage of "set" under the function represented by "ma". |
| 12837 | * In other words, plug in "ma" in "set". The result is a set |
| 12838 | * that lives in the domain space of "ma". |
| 12839 | */ |
| 12840 | __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set, |
| 12841 | __isl_take isl_multi_aff *ma) |
| 12842 | { |
| 12843 | return isl_map_preimage_multi_aff(set, isl_dim_set, ma); |
| 12844 | } |
| 12845 | |
| 12846 | /* Compute the preimage of the domain of "map" under the function |
| 12847 | * represented by "ma". |
| 12848 | * In other words, plug in "ma" in the domain of "map". |
| 12849 | * The result is a map that lives in the same space as "map" |
| 12850 | * except that the domain has been replaced by the domain space of "ma". |
| 12851 | */ |
| 12852 | __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map, |
| 12853 | __isl_take isl_multi_aff *ma) |
| 12854 | { |
| 12855 | return isl_map_preimage_multi_aff(map, isl_dim_in, ma); |
| 12856 | } |
| 12857 | |
| 12858 | /* Compute the preimage of the range of "map" under the function |
| 12859 | * represented by "ma". |
| 12860 | * In other words, plug in "ma" in the range of "map". |
| 12861 | * The result is a map that lives in the same space as "map" |
| 12862 | * except that the range has been replaced by the domain space of "ma". |
| 12863 | */ |
| 12864 | __isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map, |
| 12865 | __isl_take isl_multi_aff *ma) |
| 12866 | { |
| 12867 | return isl_map_preimage_multi_aff(map, isl_dim_out, ma); |
| 12868 | } |
| 12869 | |
| 12870 | /* Compute the preimage of "map" under the function represented by "pma". |
| 12871 | * In other words, plug in "pma" in the domain or range of "map". |
| 12872 | * The result is a map that lives in the same space as "map", |
| 12873 | * except that the space of type "type" has been replaced by |
| 12874 | * the domain space of "pma". |
| 12875 | * |
| 12876 | * The parameters of "map" and "pma" are assumed to have been aligned. |
| 12877 | */ |
| 12878 | static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned( |
| 12879 | __isl_take isl_map *map, enum isl_dim_type type, |
| 12880 | __isl_take isl_pw_multi_aff *pma) |
| 12881 | { |
| 12882 | int i; |
| 12883 | isl_map *res; |
| 12884 | |
| 12885 | if (!pma) |
| 12886 | goto error; |
| 12887 | |
| 12888 | if (pma->n == 0) { |
| 12889 | isl_pw_multi_aff_free(pma); |
| 12890 | res = isl_map_empty(isl_map_get_space(map)); |
| 12891 | isl_map_free(map); |
| 12892 | return res; |
| 12893 | } |
| 12894 | |
| 12895 | res = isl_map_preimage_multi_aff(isl_map_copy(map), type, |
| 12896 | isl_multi_aff_copy(pma->p[0].maff)); |
| 12897 | if (type == isl_dim_in) |
| 12898 | res = isl_map_intersect_domain(res, |
| 12899 | isl_map_copy(pma->p[0].set)); |
| 12900 | else |
| 12901 | res = isl_map_intersect_range(res, |
| 12902 | isl_map_copy(pma->p[0].set)); |
| 12903 | |
| 12904 | for (i = 1; i < pma->n; ++i) { |
| 12905 | isl_map *res_i; |
| 12906 | |
| 12907 | res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type, |
| 12908 | isl_multi_aff_copy(pma->p[i].maff)); |
| 12909 | if (type == isl_dim_in) |
| 12910 | res_i = isl_map_intersect_domain(res_i, |
| 12911 | isl_map_copy(pma->p[i].set)); |
| 12912 | else |
| 12913 | res_i = isl_map_intersect_range(res_i, |
| 12914 | isl_map_copy(pma->p[i].set)); |
| 12915 | res = isl_map_union(res, res_i); |
| 12916 | } |
| 12917 | |
| 12918 | isl_pw_multi_aff_free(pma); |
| 12919 | isl_map_free(map); |
| 12920 | return res; |
| 12921 | error: |
| 12922 | isl_pw_multi_aff_free(pma); |
| 12923 | isl_map_free(map); |
| 12924 | return NULL; |
| 12925 | } |
| 12926 | |
| 12927 | /* Compute the preimage of "map" under the function represented by "pma". |
| 12928 | * In other words, plug in "pma" in the domain or range of "map". |
| 12929 | * The result is a map that lives in the same space as "map", |
| 12930 | * except that the space of type "type" has been replaced by |
| 12931 | * the domain space of "pma". |
| 12932 | */ |
| 12933 | __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map, |
| 12934 | enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma) |
| 12935 | { |
| 12936 | if (!map || !pma) |
| 12937 | goto error; |
| 12938 | |
| 12939 | if (isl_space_match(map->dim, isl_dim_param, pma->dim, isl_dim_param)) |
| 12940 | return isl_map_preimage_pw_multi_aff_aligned(map, type, pma); |
| 12941 | |
| 12942 | if (!isl_space_has_named_params(map->dim) || |
| 12943 | !isl_space_has_named_params(pma->dim)) |
| 12944 | isl_die(map->ctx, isl_error_invalid, |
| 12945 | "unaligned unnamed parameters", goto error); |
| 12946 | map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma)); |
| 12947 | pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map)); |
| 12948 | |
| 12949 | return isl_map_preimage_pw_multi_aff_aligned(map, type, pma); |
| 12950 | error: |
| 12951 | isl_pw_multi_aff_free(pma); |
| 12952 | return isl_map_free(map); |
| 12953 | } |
| 12954 | |
| 12955 | /* Compute the preimage of "set" under the function represented by "pma". |
| 12956 | * In other words, plug in "pma" in "set". The result is a set |
| 12957 | * that lives in the domain space of "pma". |
| 12958 | */ |
| 12959 | __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set, |
| 12960 | __isl_take isl_pw_multi_aff *pma) |
| 12961 | { |
| 12962 | return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma); |
| 12963 | } |
| 12964 | |
| 12965 | /* Compute the preimage of the domain of "map" under the function |
| 12966 | * represented by "pma". |
| 12967 | * In other words, plug in "pma" in the domain of "map". |
| 12968 | * The result is a map that lives in the same space as "map", |
| 12969 | * except that domain space has been replaced by the domain space of "pma". |
| 12970 | */ |
| 12971 | __isl_give isl_map *isl_map_preimage_domain_pw_multi_aff( |
| 12972 | __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma) |
| 12973 | { |
| 12974 | return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma); |
| 12975 | } |
| 12976 | |
| 12977 | /* Compute the preimage of the range of "map" under the function |
| 12978 | * represented by "pma". |
| 12979 | * In other words, plug in "pma" in the range of "map". |
| 12980 | * The result is a map that lives in the same space as "map", |
| 12981 | * except that range space has been replaced by the domain space of "pma". |
| 12982 | */ |
| 12983 | __isl_give isl_map *isl_map_preimage_range_pw_multi_aff( |
| 12984 | __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma) |
| 12985 | { |
| 12986 | return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma); |
| 12987 | } |
| 12988 | |
| 12989 | /* Compute the preimage of "map" under the function represented by "mpa". |
| 12990 | * In other words, plug in "mpa" in the domain or range of "map". |
| 12991 | * The result is a map that lives in the same space as "map", |
| 12992 | * except that the space of type "type" has been replaced by |
| 12993 | * the domain space of "mpa". |
| 12994 | * |
| 12995 | * If the map does not involve any constraints that refer to the |
| 12996 | * dimensions of the substituted space, then the only possible |
| 12997 | * effect of "mpa" on the map is to map the space to a different space. |
| 12998 | * We create a separate isl_multi_aff to effectuate this change |
| 12999 | * in order to avoid spurious splitting of the map along the pieces |
| 13000 | * of "mpa". |
| 13001 | */ |
| 13002 | __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map, |
| 13003 | enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa) |
| 13004 | { |
| 13005 | int n; |
| 13006 | isl_pw_multi_aff *pma; |
| 13007 | |
| 13008 | if (!map || !mpa) |
| 13009 | goto error; |
| 13010 | |
| 13011 | n = isl_map_dim(map, type); |
| 13012 | if (!isl_map_involves_dims(map, type, 0, n)) { |
| 13013 | isl_space *space; |
| 13014 | isl_multi_aff *ma; |
| 13015 | |
| 13016 | space = isl_multi_pw_aff_get_space(mpa); |
| 13017 | isl_multi_pw_aff_free(mpa); |
| 13018 | ma = isl_multi_aff_zero(space); |
| 13019 | return isl_map_preimage_multi_aff(map, type, ma); |
| 13020 | } |
| 13021 | |
| 13022 | pma = isl_pw_multi_aff_from_multi_pw_aff(mpa); |
| 13023 | return isl_map_preimage_pw_multi_aff(map, type, pma); |
| 13024 | error: |
| 13025 | isl_map_free(map); |
| 13026 | isl_multi_pw_aff_free(mpa); |
| 13027 | return NULL; |
| 13028 | } |
| 13029 | |
| 13030 | /* Compute the preimage of "map" under the function represented by "mpa". |
| 13031 | * In other words, plug in "mpa" in the domain "map". |
| 13032 | * The result is a map that lives in the same space as "map", |
| 13033 | * except that domain space has been replaced by the domain space of "mpa". |
| 13034 | */ |
| 13035 | __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff( |
| 13036 | __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa) |
| 13037 | { |
| 13038 | return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa); |
| 13039 | } |
| 13040 | |
| 13041 | /* Compute the preimage of "set" by the function represented by "mpa". |
| 13042 | * In other words, plug in "mpa" in "set". |
| 13043 | */ |
| 13044 | __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set, |
| 13045 | __isl_take isl_multi_pw_aff *mpa) |
| 13046 | { |
| 13047 | return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa); |
| 13048 | } |