Coverage Report

Created: 2017-06-28 17:40

/Users/buildslave/jenkins/sharedspace/clang-stage2-coverage-R@2/llvm/tools/polly/lib/External/isl/isl_space.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2008-2009 Katholieke Universiteit Leuven
3
 * Copyright 2010      INRIA Saclay
4
 * Copyright 2013-2014 Ecole Normale Superieure
5
 *
6
 * Use of this software is governed by the MIT license
7
 *
8
 * Written by Sven Verdoolaege, K.U.Leuven, Departement
9
 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10
 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
11
 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France 
12
 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
13
 */
14
15
#include <stdlib.h>
16
#include <string.h>
17
#include <isl_space_private.h>
18
#include <isl_id_private.h>
19
#include <isl_reordering.h>
20
21
isl_ctx *isl_space_get_ctx(__isl_keep isl_space *dim)
22
1.01M
{
23
1.01M
  return dim ? dim->ctx : NULL;
24
1.01M
}
25
26
__isl_give isl_space *isl_space_alloc(isl_ctx *ctx,
27
      unsigned nparam, unsigned n_in, unsigned n_out)
28
3.57M
{
29
3.57M
  isl_space *dim;
30
3.57M
31
3.57M
  dim = isl_alloc_type(ctx, struct isl_space);
32
3.57M
  if (!dim)
33
8
    return NULL;
34
3.57M
35
3.57M
  dim->ctx = ctx;
36
3.57M
  isl_ctx_ref(ctx);
37
3.57M
  dim->ref = 1;
38
3.57M
  dim->nparam = nparam;
39
3.57M
  dim->n_in = n_in;
40
3.57M
  dim->n_out = n_out;
41
3.57M
42
3.57M
  dim->tuple_id[0] = NULL;
43
3.57M
  dim->tuple_id[1] = NULL;
44
3.57M
45
3.57M
  dim->nested[0] = NULL;
46
3.57M
  dim->nested[1] = NULL;
47
3.57M
48
3.57M
  dim->n_id = 0;
49
3.57M
  dim->ids = NULL;
50
3.57M
51
3.57M
  return dim;
52
3.57M
}
53
54
/* Mark the space as being that of a set, by setting the domain tuple
55
 * to isl_id_none.
56
 */
57
static __isl_give isl_space *mark_as_set(__isl_take isl_space *space)
58
1.08M
{
59
1.08M
  space = isl_space_cow(space);
60
1.08M
  if (!space)
61
0
    return NULL;
62
1.08M
  space = isl_space_set_tuple_id(space, isl_dim_in, &isl_id_none);
63
1.08M
  return space;
64
1.08M
}
65
66
/* Is the space that of a set?
67
 */
68
isl_bool isl_space_is_set(__isl_keep isl_space *space)
69
1.13M
{
70
1.13M
  if (!space)
71
0
    return isl_bool_error;
72
1.13M
  
if (1.13M
space->n_in != 0 || 1.13M
space->nested[0]913k
)
73
223k
    return isl_bool_false;
74
906k
  
if (906k
space->tuple_id[0] != &isl_id_none906k
)
75
14.2k
    return isl_bool_false;
76
892k
  return isl_bool_true;
77
906k
}
78
79
/* Is the given space that of a map?
80
 */
81
isl_bool isl_space_is_map(__isl_keep isl_space *space)
82
12.7k
{
83
12.7k
  if (!space)
84
0
    return isl_bool_error;
85
12.7k
  return space->tuple_id[0] != &isl_id_none &&
86
405
    space->tuple_id[1] != &isl_id_none;
87
12.7k
}
88
89
__isl_give isl_space *isl_space_set_alloc(isl_ctx *ctx,
90
      unsigned nparam, unsigned dim)
91
374k
{
92
374k
  isl_space *space;
93
374k
  space = isl_space_alloc(ctx, nparam, 0, dim);
94
374k
  space = mark_as_set(space);
95
374k
  return space;
96
374k
}
97
98
/* Mark the space as being that of a parameter domain, by setting
99
 * both tuples to isl_id_none.
100
 */
101
static __isl_give isl_space *mark_as_params(isl_space *space)
102
106k
{
103
106k
  if (!space)
104
2
    return NULL;
105
106k
  space = isl_space_set_tuple_id(space, isl_dim_in, &isl_id_none);
106
106k
  space = isl_space_set_tuple_id(space, isl_dim_out, &isl_id_none);
107
106k
  return space;
108
106k
}
109
110
/* Is the space that of a parameter domain?
111
 */
112
isl_bool isl_space_is_params(__isl_keep isl_space *space)
113
483k
{
114
483k
  if (!space)
115
16
    return isl_bool_error;
116
483k
  
if (483k
space->n_in != 0 || 483k
space->nested[0]406k
||
117
405k
      
space->n_out != 0405k
||
space->nested[1]317k
)
118
166k
    return isl_bool_false;
119
316k
  
if (316k
space->tuple_id[0] != &isl_id_none316k
)
120
18.6k
    return isl_bool_false;
121
298k
  
if (298k
space->tuple_id[1] != &isl_id_none298k
)
122
22.8k
    return isl_bool_false;
123
275k
  return isl_bool_true;
124
298k
}
125
126
/* Create a space for a parameter domain.
127
 */
128
__isl_give isl_space *isl_space_params_alloc(isl_ctx *ctx, unsigned nparam)
129
22.2k
{
130
22.2k
  isl_space *space;
131
22.2k
  space = isl_space_alloc(ctx, nparam, 0, 0);
132
22.2k
  space = mark_as_params(space);
133
22.2k
  return space;
134
22.2k
}
135
136
static unsigned global_pos(__isl_keep isl_space *dim,
137
         enum isl_dim_type type, unsigned pos)
138
51.4M
{
139
51.4M
  struct isl_ctx *ctx = dim->ctx;
140
51.4M
141
51.4M
  switch (type) {
142
28.6M
  case isl_dim_param:
143
28.6M
    isl_assert(ctx, pos < dim->nparam,
144
28.6M
          return isl_space_dim(dim, isl_dim_all));
145
28.6M
    return pos;
146
8.79M
  case isl_dim_in:
147
8.79M
    isl_assert(ctx, pos < dim->n_in,
148
8.79M
          return isl_space_dim(dim, isl_dim_all));
149
8.79M
    return pos + dim->nparam;
150
13.9M
  case isl_dim_out:
151
13.9M
    isl_assert(ctx, pos < dim->n_out,
152
13.9M
          return isl_space_dim(dim, isl_dim_all));
153
13.9M
    return pos + dim->nparam + dim->n_in;
154
0
  default:
155
0
    isl_assert(ctx, 0, return isl_space_dim(dim, isl_dim_all));
156
51.4M
  }
157
0
  return isl_space_dim(dim, isl_dim_all);
158
51.4M
}
159
160
/* Extend length of ids array to the total number of dimensions.
161
 */
162
static __isl_give isl_space *extend_ids(__isl_take isl_space *dim)
163
2.75M
{
164
2.75M
  isl_id **ids;
165
2.75M
  int i;
166
2.75M
167
2.75M
  if (isl_space_dim(dim, isl_dim_all) <= dim->n_id)
168
776k
    return dim;
169
2.75M
170
1.97M
  
if (1.97M
!dim->ids1.97M
)
{1.97M
171
1.97M
    dim->ids = isl_calloc_array(dim->ctx,
172
1.97M
        isl_id *, isl_space_dim(dim, isl_dim_all));
173
1.97M
    if (!dim->ids)
174
0
      goto error;
175
617
  } else {
176
617
    ids = isl_realloc_array(dim->ctx, dim->ids,
177
617
        isl_id *, isl_space_dim(dim, isl_dim_all));
178
617
    if (!ids)
179
0
      goto error;
180
617
    dim->ids = ids;
181
3.29k
    for (i = dim->n_id; 
i < isl_space_dim(dim, isl_dim_all)3.29k
;
++i2.68k
)
182
2.68k
      dim->ids[i] = NULL;
183
617
  }
184
1.97M
185
1.97M
  dim->n_id = isl_space_dim(dim, isl_dim_all);
186
1.97M
187
1.97M
  return dim;
188
0
error:
189
0
  isl_space_free(dim);
190
0
  return NULL;
191
1.97M
}
192
193
static __isl_give isl_space *set_id(__isl_take isl_space *dim,
194
  enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
195
10.2M
{
196
10.2M
  dim = isl_space_cow(dim);
197
10.2M
198
10.2M
  if (!dim)
199
0
    goto error;
200
10.2M
201
10.2M
  pos = global_pos(dim, type, pos);
202
10.2M
  if (pos == isl_space_dim(dim, isl_dim_all))
203
0
    goto error;
204
10.2M
205
10.2M
  
if (10.2M
pos >= dim->n_id10.2M
)
{1.97M
206
1.97M
    if (!id)
207
0
      return dim;
208
1.97M
    dim = extend_ids(dim);
209
1.97M
    if (!dim)
210
0
      goto error;
211
1.97M
  }
212
10.2M
213
10.2M
  dim->ids[pos] = id;
214
10.2M
215
10.2M
  return dim;
216
0
error:
217
0
  isl_id_free(id);
218
0
  isl_space_free(dim);
219
0
  return NULL;
220
10.2M
}
221
222
static __isl_keep isl_id *get_id(__isl_keep isl_space *dim,
223
         enum isl_dim_type type, unsigned pos)
224
41.1M
{
225
41.1M
  if (!dim)
226
0
    return NULL;
227
41.1M
228
41.1M
  pos = global_pos(dim, type, pos);
229
41.1M
  if (pos == isl_space_dim(dim, isl_dim_all))
230
0
    return NULL;
231
41.1M
  
if (41.1M
pos >= dim->n_id41.1M
)
232
1.46M
    return NULL;
233
39.7M
  return dim->ids[pos];
234
41.1M
}
235
236
static unsigned offset(__isl_keep isl_space *dim, enum isl_dim_type type)
237
1.69M
{
238
1.69M
  switch (type) {
239
516k
  case isl_dim_param: return 0;
240
392k
  case isl_dim_in:  return dim->nparam;
241
781k
  case isl_dim_out: return dim->nparam + dim->n_in;
242
0
  default:    return 0;
243
1.69M
  }
244
1.69M
}
245
246
static unsigned n(__isl_keep isl_space *dim, enum isl_dim_type type)
247
216M
{
248
216M
  switch (type) {
249
41.5M
  case isl_dim_param: return dim->nparam;
250
25.0M
  case isl_dim_in:  return dim->n_in;
251
29.2M
  case isl_dim_out: return dim->n_out;
252
120M
  case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
253
0
  default:    return 0;
254
216M
  }
255
216M
}
256
257
unsigned isl_space_dim(__isl_keep isl_space *dim, enum isl_dim_type type)
258
152M
{
259
152M
  if (!dim)
260
4
    return 0;
261
152M
  return n(dim, type);
262
152M
}
263
264
unsigned isl_space_offset(__isl_keep isl_space *dim, enum isl_dim_type type)
265
1.32M
{
266
1.32M
  if (!dim)
267
0
    return 0;
268
1.32M
  return offset(dim, type);
269
1.32M
}
270
271
static __isl_give isl_space *copy_ids(__isl_take isl_space *dst,
272
  enum isl_dim_type dst_type, unsigned offset, __isl_keep isl_space *src,
273
  enum isl_dim_type src_type)
274
7.02M
{
275
7.02M
  int i;
276
7.02M
  isl_id *id;
277
7.02M
278
7.02M
  if (!dst)
279
0
    return NULL;
280
7.02M
281
18.6M
  
for (i = 0; 7.02M
i < n(src, src_type)18.6M
;
++i11.6M
)
{11.6M
282
11.6M
    id = get_id(src, src_type, i);
283
11.6M
    if (!id)
284
4.00M
      continue;
285
7.66M
    dst = set_id(dst, dst_type, offset + i, isl_id_copy(id));
286
7.66M
    if (!dst)
287
0
      return NULL;
288
7.66M
  }
289
7.02M
  return dst;
290
7.02M
}
291
292
__isl_take isl_space *isl_space_dup(__isl_keep isl_space *dim)
293
2.68M
{
294
2.68M
  isl_space *dup;
295
2.68M
  if (!dim)
296
0
    return NULL;
297
2.68M
  dup = isl_space_alloc(dim->ctx, dim->nparam, dim->n_in, dim->n_out);
298
2.68M
  if (!dup)
299
8
    return NULL;
300
2.68M
  
if (2.68M
dim->tuple_id[0] &&2.68M
301
1.59M
      !(dup->tuple_id[0] = isl_id_copy(dim->tuple_id[0])))
302
0
    goto error;
303
2.68M
  
if (2.68M
dim->tuple_id[1] &&2.68M
304
663k
      !(dup->tuple_id[1] = isl_id_copy(dim->tuple_id[1])))
305
0
    goto error;
306
2.68M
  
if (2.68M
dim->nested[0] && 2.68M
!(dup->nested[0] = isl_space_copy(dim->nested[0]))439k
)
307
0
    goto error;
308
2.68M
  
if (2.68M
dim->nested[1] && 2.68M
!(dup->nested[1] = isl_space_copy(dim->nested[1]))573k
)
309
0
    goto error;
310
2.68M
  
if (2.68M
!dim->ids2.68M
)
311
906k
    return dup;
312
1.78M
  dup = copy_ids(dup, isl_dim_param, 0, dim, isl_dim_param);
313
1.78M
  dup = copy_ids(dup, isl_dim_in, 0, dim, isl_dim_in);
314
1.78M
  dup = copy_ids(dup, isl_dim_out, 0, dim, isl_dim_out);
315
1.78M
  return dup;
316
0
error:
317
0
  isl_space_free(dup);
318
0
  return NULL;
319
2.68M
}
320
321
__isl_give isl_space *isl_space_cow(__isl_take isl_space *dim)
322
18.5M
{
323
18.5M
  if (!dim)
324
7
    return NULL;
325
18.5M
326
18.5M
  
if (18.5M
dim->ref == 118.5M
)
327
15.8M
    return dim;
328
2.66M
  dim->ref--;
329
2.66M
  return isl_space_dup(dim);
330
18.5M
}
331
332
__isl_give isl_space *isl_space_copy(__isl_keep isl_space *dim)
333
11.2M
{
334
11.2M
  if (!dim)
335
7.49k
    return NULL;
336
11.2M
337
11.1M
  dim->ref++;
338
11.1M
  return dim;
339
11.2M
}
340
341
__isl_null isl_space *isl_space_free(__isl_take isl_space *space)
342
20.1M
{
343
20.1M
  int i;
344
20.1M
345
20.1M
  if (!space)
346
8.07M
    return NULL;
347
20.1M
348
12.1M
  
if (12.1M
--space->ref > 012.1M
)
349
8.52M
    return NULL;
350
12.1M
351
3.57M
  isl_id_free(space->tuple_id[0]);
352
3.57M
  isl_id_free(space->tuple_id[1]);
353
3.57M
354
3.57M
  isl_space_free(space->nested[0]);
355
3.57M
  isl_space_free(space->nested[1]);
356
3.57M
357
10.9M
  for (i = 0; 
i < space->n_id10.9M
;
++i7.36M
)
358
7.36M
    isl_id_free(space->ids[i]);
359
3.57M
  free(space->ids);
360
3.57M
  isl_ctx_deref(space->ctx);
361
3.57M
  
362
3.57M
  free(space);
363
3.57M
364
3.57M
  return NULL;
365
12.1M
}
366
367
/* Check if "s" is a valid dimension or tuple name.
368
 * We currently only forbid names that look like a number.
369
 *
370
 * s is assumed to be non-NULL.
371
 */
372
static int name_ok(isl_ctx *ctx, const char *s)
373
49.8k
{
374
49.8k
  char *p;
375
49.8k
  long dummy;
376
49.8k
377
49.8k
  dummy = strtol(s, &p, 0);
378
49.8k
  if (p != s)
379
0
    isl_die(ctx, isl_error_invalid, "name looks like a number",
380
49.8k
      return 0);
381
49.8k
382
49.8k
  return 1;
383
49.8k
}
384
385
/* Is it possible for the given dimension type to have a tuple id?
386
 */
387
static int space_can_have_id(__isl_keep isl_space *space,
388
  enum isl_dim_type type)
389
70.3k
{
390
70.3k
  if (!space)
391
0
    return 0;
392
70.3k
  
if (70.3k
isl_space_is_params(space)70.3k
)
393
0
    isl_die(space->ctx, isl_error_invalid,
394
70.3k
      "parameter spaces don't have tuple ids", return 0);
395
70.3k
  
if (70.3k
isl_space_is_set(space) && 70.3k
type != isl_dim_set19.5k
)
396
0
    isl_die(space->ctx, isl_error_invalid,
397
70.3k
      "set spaces can only have a set id", return 0);
398
70.3k
  
if (70.3k
type != isl_dim_in && 70.3k
type != isl_dim_out70.1k
)
399
0
    isl_die(space->ctx, isl_error_invalid,
400
70.3k
      "only input, output and set tuples can have ids",
401
70.3k
      return 0);
402
70.3k
403
70.3k
  return 1;
404
70.3k
}
405
406
/* Does the tuple have an id?
407
 */
408
isl_bool isl_space_has_tuple_id(__isl_keep isl_space *dim,
409
  enum isl_dim_type type)
410
69.6k
{
411
69.6k
  if (!space_can_have_id(dim, type))
412
0
    return isl_bool_error;
413
69.6k
  return dim->tuple_id[type - isl_dim_in] != NULL;
414
69.6k
}
415
416
__isl_give isl_id *isl_space_get_tuple_id(__isl_keep isl_space *dim,
417
  enum isl_dim_type type)
418
25.4k
{
419
25.4k
  int has_id;
420
25.4k
421
25.4k
  if (!dim)
422
0
    return NULL;
423
25.4k
  has_id = isl_space_has_tuple_id(dim, type);
424
25.4k
  if (has_id < 0)
425
0
    return NULL;
426
25.4k
  
if (25.4k
!has_id25.4k
)
427
0
    isl_die(dim->ctx, isl_error_invalid,
428
25.4k
      "tuple has no id", return NULL);
429
25.4k
  return isl_id_copy(dim->tuple_id[type - isl_dim_in]);
430
25.4k
}
431
432
__isl_give isl_space *isl_space_set_tuple_id(__isl_take isl_space *dim,
433
  enum isl_dim_type type, __isl_take isl_id *id)
434
1.36M
{
435
1.36M
  dim = isl_space_cow(dim);
436
1.36M
  if (
!dim || 1.36M
!id1.36M
)
437
1
    goto error;
438
1.36M
  
if (1.36M
type != isl_dim_in && 1.36M
type != isl_dim_out162k
)
439
0
    isl_die(dim->ctx, isl_error_invalid,
440
1.36M
      "only input, output and set tuples can have names",
441
1.36M
      goto error);
442
1.36M
443
1.36M
  isl_id_free(dim->tuple_id[type - isl_dim_in]);
444
1.36M
  dim->tuple_id[type - isl_dim_in] = id;
445
1.36M
446
1.36M
  return dim;
447
1
error:
448
1
  isl_id_free(id);
449
1
  isl_space_free(dim);
450
1
  return NULL;
451
1.36M
}
452
453
__isl_give isl_space *isl_space_reset_tuple_id(__isl_take isl_space *dim,
454
  enum isl_dim_type type)
455
5.18k
{
456
5.18k
  dim = isl_space_cow(dim);
457
5.18k
  if (!dim)
458
0
    return NULL;
459
5.18k
  
if (5.18k
type != isl_dim_in && 5.18k
type != isl_dim_out5.18k
)
460
0
    isl_die(dim->ctx, isl_error_invalid,
461
5.18k
      "only input, output and set tuples can have names",
462
5.18k
      goto error);
463
5.18k
464
5.18k
  isl_id_free(dim->tuple_id[type - isl_dim_in]);
465
5.18k
  dim->tuple_id[type - isl_dim_in] = NULL;
466
5.18k
467
5.18k
  return dim;
468
0
error:
469
0
  isl_space_free(dim);
470
0
  return NULL;
471
5.18k
}
472
473
/* Set the id of the given dimension of "space" to "id".
474
 * If the dimension already has an id, then it is replaced.
475
 * If the dimension is a parameter, then we need to change it
476
 * in the nested spaces (if any) as well.
477
 */
478
__isl_give isl_space *isl_space_set_dim_id(__isl_take isl_space *space,
479
  enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
480
60.6k
{
481
60.6k
  space = isl_space_cow(space);
482
60.6k
  if (
!space || 60.6k
!id60.6k
)
483
0
    goto error;
484
60.6k
485
60.6k
  
if (60.6k
type == isl_dim_param60.6k
)
{44.1k
486
44.1k
    int i;
487
44.1k
488
132k
    for (i = 0; 
i < 2132k
;
++i88.3k
)
{88.3k
489
88.3k
      if (!space->nested[i])
490
88.3k
        continue;
491
0
      space->nested[i] =
492
0
        isl_space_set_dim_id(space->nested[i],
493
0
            type, pos, isl_id_copy(id));
494
0
      if (!space->nested[i])
495
0
        goto error;
496
0
    }
497
44.1k
  }
498
60.6k
499
60.6k
  isl_id_free(get_id(space, type, pos));
500
60.6k
  return set_id(space, type, pos, id);
501
0
error:
502
0
  isl_id_free(id);
503
0
  isl_space_free(space);
504
0
  return NULL;
505
60.6k
}
506
507
/* Reset the id of the given dimension of "space".
508
 * If the dimension already has an id, then it is removed.
509
 * If the dimension is a parameter, then we need to reset it
510
 * in the nested spaces (if any) as well.
511
 */
512
__isl_give isl_space *isl_space_reset_dim_id(__isl_take isl_space *space,
513
  enum isl_dim_type type, unsigned pos)
514
0
{
515
0
  space = isl_space_cow(space);
516
0
  if (!space)
517
0
    goto error;
518
0
519
0
  
if (0
type == isl_dim_param0
)
{0
520
0
    int i;
521
0
522
0
    for (i = 0; 
i < 20
;
++i0
)
{0
523
0
      if (!space->nested[i])
524
0
        continue;
525
0
      space->nested[i] =
526
0
        isl_space_reset_dim_id(space->nested[i],
527
0
              type, pos);
528
0
      if (!space->nested[i])
529
0
        goto error;
530
0
    }
531
0
  }
532
0
533
0
  isl_id_free(get_id(space, type, pos));
534
0
  return set_id(space, type, pos, NULL);
535
0
error:
536
0
  isl_space_free(space);
537
0
  return NULL;
538
0
}
539
540
isl_bool isl_space_has_dim_id(__isl_keep isl_space *dim,
541
  enum isl_dim_type type, unsigned pos)
542
1.03k
{
543
1.03k
  if (!dim)
544
0
    return isl_bool_error;
545
1.03k
  return get_id(dim, type, pos) != NULL;
546
1.03k
}
547
548
__isl_give isl_id *isl_space_get_dim_id(__isl_keep isl_space *dim,
549
  enum isl_dim_type type, unsigned pos)
550
827k
{
551
827k
  if (!dim)
552
0
    return NULL;
553
827k
  
if (827k
!get_id(dim, type, pos)827k
)
554
0
    isl_die(dim->ctx, isl_error_invalid,
555
827k
      "dim has no id", return NULL);
556
827k
  return isl_id_copy(get_id(dim, type, pos));
557
827k
}
558
559
__isl_give isl_space *isl_space_set_tuple_name(__isl_take isl_space *dim,
560
  enum isl_dim_type type, const char *s)
561
45.0k
{
562
45.0k
  isl_id *id;
563
45.0k
564
45.0k
  if (!dim)
565
0
    return NULL;
566
45.0k
567
45.0k
  
if (45.0k
!s45.0k
)
568
0
    return isl_space_reset_tuple_id(dim, type);
569
45.0k
570
45.0k
  
if (45.0k
!name_ok(dim->ctx, s)45.0k
)
571
0
    goto error;
572
45.0k
573
45.0k
  id = isl_id_alloc(dim->ctx, s, NULL);
574
45.0k
  return isl_space_set_tuple_id(dim, type, id);
575
0
error:
576
0
  isl_space_free(dim);
577
0
  return NULL;
578
45.0k
}
579
580
/* Does the tuple have a name?
581
 */
582
isl_bool isl_space_has_tuple_name(__isl_keep isl_space *space,
583
  enum isl_dim_type type)
584
708
{
585
708
  isl_id *id;
586
708
587
708
  if (!space_can_have_id(space, type))
588
0
    return isl_bool_error;
589
708
  id = space->tuple_id[type - isl_dim_in];
590
154
  return id && id->name;
591
708
}
592
593
__isl_keep const char *isl_space_get_tuple_name(__isl_keep isl_space *dim,
594
   enum isl_dim_type type)
595
8.46k
{
596
8.46k
  isl_id *id;
597
8.46k
  if (!dim)
598
0
    return NULL;
599
8.46k
  
if (8.46k
type != isl_dim_in && 8.46k
type != isl_dim_out5.02k
)
600
0
    return NULL;
601
8.46k
  id = dim->tuple_id[type - isl_dim_in];
602
7.25k
  return id ? id->name : NULL;
603
8.46k
}
604
605
__isl_give isl_space *isl_space_set_dim_name(__isl_take isl_space *dim,
606
         enum isl_dim_type type, unsigned pos,
607
         const char *s)
608
4.87k
{
609
4.87k
  isl_id *id;
610
4.87k
611
4.87k
  if (!dim)
612
0
    return NULL;
613
4.87k
  
if (4.87k
!s4.87k
)
614
0
    return isl_space_reset_dim_id(dim, type, pos);
615
4.87k
  
if (4.87k
!name_ok(dim->ctx, s)4.87k
)
616
0
    goto error;
617
4.87k
  id = isl_id_alloc(dim->ctx, s, NULL);
618
4.87k
  return isl_space_set_dim_id(dim, type, pos, id);
619
0
error:
620
0
  isl_space_free(dim);
621
0
  return NULL;
622
4.87k
}
623
624
/* Does the given dimension have a name?
625
 */
626
isl_bool isl_space_has_dim_name(__isl_keep isl_space *space,
627
  enum isl_dim_type type, unsigned pos)
628
5.18k
{
629
5.18k
  isl_id *id;
630
5.18k
631
5.18k
  if (!space)
632
0
    return isl_bool_error;
633
5.18k
  id = get_id(space, type, pos);
634
975
  return id && id->name;
635
5.18k
}
636
637
__isl_keep const char *isl_space_get_dim_name(__isl_keep isl_space *dim,
638
         enum isl_dim_type type, unsigned pos)
639
305k
{
640
305k
  isl_id *id = get_id(dim, type, pos);
641
275k
  return id ? id->name : NULL;
642
305k
}
643
644
int isl_space_find_dim_by_id(__isl_keep isl_space *dim, enum isl_dim_type type,
645
  __isl_keep isl_id *id)
646
366
{
647
366
  int i;
648
366
  int offset;
649
366
  int n;
650
366
651
366
  if (
!dim || 366
!id366
)
652
0
    return -1;
653
366
654
366
  offset = isl_space_offset(dim, type);
655
366
  n = isl_space_dim(dim, type);
656
470
  for (i = 0; 
i < n && 470
offset + i < dim->n_id469
;
++i104
)
657
469
    
if (469
dim->ids[offset + i] == id469
)
658
365
      return i;
659
366
660
1
  return -1;
661
366
}
662
663
int isl_space_find_dim_by_name(__isl_keep isl_space *space,
664
  enum isl_dim_type type, const char *name)
665
826
{
666
826
  int i;
667
826
  int offset;
668
826
  int n;
669
826
670
826
  if (
!space || 826
!name826
)
671
0
    return -1;
672
826
673
826
  offset = isl_space_offset(space, type);
674
826
  n = isl_space_dim(space, type);
675
1.34k
  for (i = 0; 
i < n && 1.34k
offset + i < space->n_id516
;
++i516
)
{516
676
516
    isl_id *id = get_id(space, type, i);
677
516
    if (
id && 516
id->name516
&&
!strcmp(id->name, name)516
)
678
0
      return i;
679
516
  }
680
826
681
826
  return -1;
682
826
}
683
684
/* Reset the user pointer on all identifiers of parameters and tuples
685
 * of "space".
686
 */
687
__isl_give isl_space *isl_space_reset_user(__isl_take isl_space *space)
688
0
{
689
0
  int i;
690
0
  isl_ctx *ctx;
691
0
  isl_id *id;
692
0
  const char *name;
693
0
694
0
  if (!space)
695
0
    return NULL;
696
0
697
0
  ctx = isl_space_get_ctx(space);
698
0
699
0
  for (i = 0; 
i < space->nparam && 0
i < space->n_id0
;
++i0
)
{0
700
0
    if (!isl_id_get_user(space->ids[i]))
701
0
      continue;
702
0
    space = isl_space_cow(space);
703
0
    if (!space)
704
0
      return NULL;
705
0
    name = isl_id_get_name(space->ids[i]);
706
0
    id = isl_id_alloc(ctx, name, NULL);
707
0
    isl_id_free(space->ids[i]);
708
0
    space->ids[i] = id;
709
0
    if (!id)
710
0
      return isl_space_free(space);
711
0
  }
712
0
713
0
  
for (i = 0; 0
i < 20
;
++i0
)
{0
714
0
    if (!space->tuple_id[i])
715
0
      continue;
716
0
    
if (0
!isl_id_get_user(space->tuple_id[i])0
)
717
0
      continue;
718
0
    space = isl_space_cow(space);
719
0
    if (!space)
720
0
      return NULL;
721
0
    name = isl_id_get_name(space->tuple_id[i]);
722
0
    id = isl_id_alloc(ctx, name, NULL);
723
0
    isl_id_free(space->tuple_id[i]);
724
0
    space->tuple_id[i] = id;
725
0
    if (!id)
726
0
      return isl_space_free(space);
727
0
  }
728
0
729
0
  
for (i = 0; 0
i < 20
;
++i0
)
{0
730
0
    if (!space->nested[i])
731
0
      continue;
732
0
    space = isl_space_cow(space);
733
0
    if (!space)
734
0
      return NULL;
735
0
    space->nested[i] = isl_space_reset_user(space->nested[i]);
736
0
    if (!space->nested[i])
737
0
      return isl_space_free(space);
738
0
  }
739
0
740
0
  return space;
741
0
}
742
743
static __isl_keep isl_id *tuple_id(__isl_keep isl_space *dim,
744
  enum isl_dim_type type)
745
29.0M
{
746
29.0M
  if (!dim)
747
0
    return NULL;
748
29.0M
  
if (29.0M
type == isl_dim_in29.0M
)
749
9.13M
    return dim->tuple_id[0];
750
19.8M
  
if (19.8M
type == isl_dim_out19.8M
)
751
8.22M
    return dim->tuple_id[1];
752
11.6M
  return NULL;
753
19.8M
}
754
755
static __isl_keep isl_space *nested(__isl_keep isl_space *dim,
756
  enum isl_dim_type type)
757
26.7M
{
758
26.7M
  if (!dim)
759
0
    return NULL;
760
26.7M
  
if (26.7M
type == isl_dim_in26.7M
)
761
7.68M
    return dim->nested[0];
762
19.0M
  
if (19.0M
type == isl_dim_out19.0M
)
763
7.41M
    return dim->nested[1];
764
11.6M
  return NULL;
765
19.0M
}
766
767
/* Are the two spaces the same, apart from positions and names of parameters?
768
 */
769
isl_bool isl_space_has_equal_tuples(__isl_keep isl_space *space1,
770
  __isl_keep isl_space *space2)
771
4.27M
{
772
4.27M
  if (
!space1 || 4.27M
!space24.27M
)
773
0
    return isl_bool_error;
774
4.27M
  
if (4.27M
space1 == space24.27M
)
775
1.10M
    return isl_bool_true;
776
3.16M
  return isl_space_tuple_is_equal(space1, isl_dim_in,
777
3.16M
          space2, isl_dim_in) &&
778
2.69M
         isl_space_tuple_is_equal(space1, isl_dim_out,
779
2.69M
          space2, isl_dim_out);
780
4.27M
}
781
782
/* Check if the tuple of type "type1" of "space1" is the same as
783
 * the tuple of type "type2" of "space2".
784
 *
785
 * That is, check if the tuples have the same identifier, the same dimension
786
 * and the same internal structure.
787
 * The identifiers of the dimensions inside the tuples do not affect the result.
788
 *
789
 * Note that this function only checks the tuples themselves.
790
 * If nested tuples are involved, then we need to be careful not
791
 * to have result affected by possibly differing parameters
792
 * in those nested tuples.
793
 */
794
isl_bool isl_space_tuple_is_equal(__isl_keep isl_space *space1,
795
  enum isl_dim_type type1, __isl_keep isl_space *space2,
796
  enum isl_dim_type type2)
797
14.1M
{
798
14.1M
  isl_id *id1, *id2;
799
14.1M
  isl_space *nested1, *nested2;
800
14.1M
801
14.1M
  if (
!space1 || 14.1M
!space214.1M
)
802
0
    return isl_bool_error;
803
14.1M
804
14.1M
  
if (14.1M
space1 == space2 && 14.1M
type1 == type21.29M
)
805
6.24k
    return isl_bool_true;
806
14.1M
807
14.1M
  
if (14.1M
n(space1, type1) != n(space2, type2)14.1M
)
808
1.41M
    return isl_bool_false;
809
12.7M
  id1 = tuple_id(space1, type1);
810
12.7M
  id2 = tuple_id(space2, type2);
811
12.7M
  if (!id1 ^ !id2)
812
464k
    return isl_bool_false;
813
12.2M
  
if (12.2M
id1 && 12.2M
id1 != id22.92M
)
814
132k
    return isl_bool_false;
815
12.1M
  nested1 = nested(space1, type1);
816
12.1M
  nested2 = nested(space2, type2);
817
12.1M
  if (!nested1 ^ !nested2)
818
44.3k
    return isl_bool_false;
819
12.0M
  
if (12.0M
nested1 && 12.0M
!isl_space_has_equal_tuples(nested1, nested2)1.85M
)
820
16.7k
    return isl_bool_false;
821
12.0M
  return isl_bool_true;
822
12.0M
}
823
824
/* This is the old, undocumented, name for isl_space_tuple_is_equal.
825
 * It will be removed at some point.
826
 */
827
int isl_space_tuple_match(__isl_keep isl_space *space1, enum isl_dim_type type1,
828
  __isl_keep isl_space *space2, enum isl_dim_type type2)
829
0
{
830
0
  return isl_space_tuple_is_equal(space1, type1, space2, type2);
831
0
}
832
833
static isl_bool match(__isl_keep isl_space *space1, enum isl_dim_type type1,
834
  __isl_keep isl_space *space2, enum isl_dim_type type2)
835
7.74M
{
836
7.74M
  int i;
837
7.74M
838
7.74M
  if (
space1 == space2 && 7.74M
type1 == type22.06M
)
839
782k
    return isl_bool_true;
840
7.74M
841
6.96M
  
if (6.96M
!isl_space_tuple_is_equal(space1, type1, space2, type2)6.96M
)
842
1.36M
    return isl_bool_false;
843
6.96M
844
5.59M
  
if (5.59M
!space1->ids && 5.59M
!space2->ids1.41M
)
845
1.29M
    return isl_bool_true;
846
5.59M
847
10.9M
  
for (i = 0; 4.30M
i < n(space1, type1)10.9M
;
++i6.64M
)
{6.67M
848
6.67M
    if (get_id(space1, type1, i) != get_id(space2, type2, i))
849
33.4k
      return isl_bool_false;
850
6.67M
  }
851
4.27M
  return isl_bool_true;
852
4.30M
}
853
854
/* Do "space1" and "space2" have the same parameters?
855
 */
856
isl_bool isl_space_has_equal_params(__isl_keep isl_space *space1,
857
  __isl_keep isl_space *space2)
858
6.12M
{
859
6.12M
  if (
!space1 || 6.12M
!space26.12M
)
860
1
    return isl_bool_error;
861
6.12M
862
6.12M
  return match(space1, isl_dim_param, space2, isl_dim_param);
863
6.12M
}
864
865
/* Do "space1" and "space2" have the same identifiers for all
866
 * the tuple variables?
867
 */
868
isl_bool isl_space_has_equal_ids(__isl_keep isl_space *space1,
869
  __isl_keep isl_space *space2)
870
168k
{
871
168k
  isl_bool equal;
872
168k
873
168k
  if (
!space1 || 168k
!space2168k
)
874
0
    return isl_bool_error;
875
168k
876
168k
  equal = match(space1, isl_dim_in, space2, isl_dim_in);
877
168k
  if (
equal < 0 || 168k
!equal168k
)
878
137
    return equal;
879
168k
  return match(space1, isl_dim_out, space2, isl_dim_out);
880
168k
}
881
882
isl_bool isl_space_match(__isl_keep isl_space *space1, enum isl_dim_type type1,
883
  __isl_keep isl_space *space2, enum isl_dim_type type2)
884
0
{
885
0
  if (
!space1 || 0
!space20
)
886
0
    return isl_bool_error;
887
0
888
0
  return match(space1, type1, space2, type2);
889
0
}
890
891
static void get_ids(__isl_keep isl_space *dim, enum isl_dim_type type,
892
  unsigned first, unsigned n, __isl_keep isl_id **ids)
893
3.15M
{
894
3.15M
  int i;
895
3.15M
896
7.44M
  for (i = 0; 
i < n7.44M
;
++i4.29M
)
897
4.29M
    ids[i] = get_id(dim, type, first + i);
898
3.15M
}
899
900
static __isl_give isl_space *space_extend(__isl_take isl_space *space,
901
      unsigned nparam, unsigned n_in, unsigned n_out)
902
539k
{
903
539k
  isl_id **ids = NULL;
904
539k
905
539k
  if (!space)
906
0
    return NULL;
907
539k
  
if (539k
space->nparam == nparam &&539k
908
395k
      
space->n_in == n_in395k
&&
space->n_out == n_out390k
)
909
23.7k
    return space;
910
539k
911
515k
  
isl_assert515k
(space->ctx, space->nparam <= nparam, goto error);515k
912
515k
  
isl_assert515k
(space->ctx, space->n_in <= n_in, goto error);515k
913
515k
  
isl_assert515k
(space->ctx, space->n_out <= n_out, goto error);515k
914
515k
915
515k
  space = isl_space_cow(space);
916
515k
  if (!space)
917
0
    goto error;
918
515k
919
515k
  
if (515k
space->ids515k
)
{276k
920
276k
    unsigned n;
921
276k
    n = nparam + n_in + n_out;
922
276k
    if (
n < nparam || 276k
n < n_in276k
||
n < n_out276k
)
923
0
      isl_die(isl_space_get_ctx(space), isl_error_invalid,
924
276k
        "overflow in total number of dimensions",
925
276k
        goto error);
926
276k
    
ids = 276k
isl_calloc_array276k
(space->ctx, isl_id *, n);
927
276k
    if (!ids)
928
0
      goto error;
929
276k
    get_ids(space, isl_dim_param, 0, space->nparam, ids);
930
276k
    get_ids(space, isl_dim_in, 0, space->n_in, ids + nparam);
931
276k
    get_ids(space, isl_dim_out, 0, space->n_out,
932
276k
      ids + nparam + n_in);
933
276k
    free(space->ids);
934
276k
    space->ids = ids;
935
276k
    space->n_id = nparam + n_in + n_out;
936
276k
  }
937
515k
  space->nparam = nparam;
938
515k
  space->n_in = n_in;
939
515k
  space->n_out = n_out;
940
515k
941
515k
  return space;
942
0
error:
943
0
  free(ids);
944
0
  isl_space_free(space);
945
0
  return NULL;
946
515k
}
947
948
__isl_give isl_space *isl_space_extend(__isl_take isl_space *space,
949
  unsigned nparam, unsigned n_in, unsigned n_out)
950
0
{
951
0
  return space_extend(space, nparam, n_in, n_out);
952
0
}
953
954
__isl_give isl_space *isl_space_add_dims(__isl_take isl_space *space,
955
  enum isl_dim_type type, unsigned n)
956
539k
{
957
539k
  space = isl_space_reset(space, type);
958
539k
  if (!space)
959
6
    return NULL;
960
539k
  switch (type) {
961
166k
  case isl_dim_param:
962
166k
    space = space_extend(space,
963
166k
        space->nparam + n, space->n_in, space->n_out);
964
166k
    if (
space && 166k
space->nested[0]166k
&&
965
6.53k
        !(space->nested[0] = isl_space_add_dims(space->nested[0],
966
6.53k
                isl_dim_param, n)))
967
0
      goto error;
968
166k
    
if (166k
space && 166k
space->nested[1]166k
&&
969
4.84k
        !(space->nested[1] = isl_space_add_dims(space->nested[1],
970
4.84k
                isl_dim_param, n)))
971
0
      goto error;
972
166k
    return space;
973
6.25k
  case isl_dim_in:
974
6.25k
    return space_extend(space,
975
6.25k
        space->nparam, space->n_in + n, space->n_out);
976
366k
  case isl_dim_out:
977
366k
    return space_extend(space,
978
366k
        space->nparam, space->n_in, space->n_out + n);
979
0
  default:
980
0
    isl_die(space->ctx, isl_error_invalid,
981
539k
      "cannot add dimensions of specified type", goto error);
982
539k
  }
983
0
error:
984
0
  isl_space_free(space);
985
0
  return NULL;
986
539k
}
987
988
static int valid_dim_type(enum isl_dim_type type)
989
1.40M
{
990
1.40M
  switch (type) {
991
1.40M
  case isl_dim_param:
992
1.40M
  case isl_dim_in:
993
1.40M
  case isl_dim_out:
994
1.40M
    return 1;
995
0
  default:
996
0
    return 0;
997
1.40M
  }
998
1.40M
}
999
1000
/* Insert "n" dimensions of type "type" at position "pos".
1001
 * If we are inserting parameters, then they are also inserted in
1002
 * any nested spaces.
1003
 */
1004
__isl_give isl_space *isl_space_insert_dims(__isl_take isl_space *dim,
1005
  enum isl_dim_type type, unsigned pos, unsigned n)
1006
127k
{
1007
127k
  isl_id **ids = NULL;
1008
127k
1009
127k
  if (!dim)
1010
0
    return NULL;
1011
127k
  
if (127k
n == 0127k
)
1012
2
    return isl_space_reset(dim, type);
1013
127k
1014
127k
  
if (127k
!valid_dim_type(type)127k
)
1015
0
    isl_die(dim->ctx, isl_error_invalid,
1016
127k
      "cannot insert dimensions of specified type",
1017
127k
      goto error);
1018
127k
1019
127k
  
isl_assert127k
(dim->ctx, pos <= isl_space_dim(dim, type), goto error);127k
1020
127k
1021
127k
  dim = isl_space_cow(dim);
1022
127k
  if (!dim)
1023
0
    return NULL;
1024
127k
1025
127k
  
if (127k
dim->ids127k
)
{72.5k
1026
72.5k
    enum isl_dim_type t, o = isl_dim_param;
1027
72.5k
    int off;
1028
72.5k
    int s[3];
1029
72.5k
    ids = isl_calloc_array(dim->ctx, isl_id *,
1030
72.5k
             dim->nparam + dim->n_in + dim->n_out + n);
1031
72.5k
    if (!ids)
1032
0
      goto error;
1033
72.5k
    off = 0;
1034
72.5k
    s[isl_dim_param - o] = dim->nparam;
1035
72.5k
    s[isl_dim_in - o] = dim->n_in;
1036
72.5k
    s[isl_dim_out - o] = dim->n_out;
1037
290k
    for (t = isl_dim_param; 
t <= isl_dim_out290k
;
++t217k
)
{217k
1038
217k
      if (
t != type217k
)
{145k
1039
145k
        get_ids(dim, t, 0, s[t - o], ids + off);
1040
145k
        off += s[t - o];
1041
72.5k
      } else {
1042
72.5k
        get_ids(dim, t, 0, pos, ids + off);
1043
72.5k
        off += pos + n;
1044
72.5k
        get_ids(dim, t, pos, s[t - o] - pos, ids + off);
1045
72.5k
        off += s[t - o] - pos;
1046
72.5k
      }
1047
217k
    }
1048
72.5k
    free(dim->ids);
1049
72.5k
    dim->ids = ids;
1050
72.5k
    dim->n_id = dim->nparam + dim->n_in + dim->n_out + n;
1051
72.5k
  }
1052
127k
  switch (type) {
1053
2.05k
  case isl_dim_param: dim->nparam += n; break;
1054
7.53k
  case isl_dim_in:  dim->n_in += n; break;
1055
118k
  case isl_dim_out: dim->n_out += n; break;
1056
0
  default:    ;
1057
127k
  }
1058
127k
  dim = isl_space_reset(dim, type);
1059
127k
1060
127k
  if (
type == isl_dim_param127k
)
{2.05k
1061
2.05k
    if (
dim && 2.05k
dim->nested[0]2.05k
&&
1062
0
        !(dim->nested[0] = isl_space_insert_dims(dim->nested[0],
1063
0
                isl_dim_param, pos, n)))
1064
0
      goto error;
1065
2.05k
    
if (2.05k
dim && 2.05k
dim->nested[1]2.05k
&&
1066
3
        !(dim->nested[1] = isl_space_insert_dims(dim->nested[1],
1067
3
                isl_dim_param, pos, n)))
1068
0
      goto error;
1069
2.05k
  }
1070
127k
1071
127k
  return dim;
1072
0
error:
1073
0
  isl_space_free(dim);
1074
0
  return NULL;
1075
127k
}
1076
1077
__isl_give isl_space *isl_space_move_dims(__isl_take isl_space *space,
1078
  enum isl_dim_type dst_type, unsigned dst_pos,
1079
  enum isl_dim_type src_type, unsigned src_pos, unsigned n)
1080
917
{
1081
917
  int i;
1082
917
1083
917
  space = isl_space_reset(space, src_type);
1084
917
  space = isl_space_reset(space, dst_type);
1085
917
  if (!space)
1086
0
    return NULL;
1087
917
  
if (917
n == 0917
)
1088
129
    return space;
1089
917
1090
788
  
isl_assert788
(space->ctx, src_pos + n <= isl_space_dim(space, src_type),788
1091
788
    goto error);
1092
788
1093
788
  
if (788
dst_type == src_type && 788
dst_pos == src_pos0
)
1094
0
    return space;
1095
788
1096
788
  
isl_assert788
(space->ctx, dst_type != src_type, goto error);788
1097
788
1098
788
  space = isl_space_cow(space);
1099
788
  if (!space)
1100
0
    return NULL;
1101
788
1102
788
  
if (788
space->ids788
)
{480
1103
480
    isl_id **ids;
1104
480
    enum isl_dim_type t, o = isl_dim_param;
1105
480
    int off;
1106
480
    int s[3];
1107
480
    ids = isl_calloc_array(space->ctx, isl_id *,
1108
480
         space->nparam + space->n_in + space->n_out);
1109
480
    if (!ids)
1110
0
      goto error;
1111
480
    off = 0;
1112
480
    s[isl_dim_param - o] = space->nparam;
1113
480
    s[isl_dim_in - o] = space->n_in;
1114
480
    s[isl_dim_out - o] = space->n_out;
1115
1.92k
    for (t = isl_dim_param; 
t <= isl_dim_out1.92k
;
++t1.44k
)
{1.44k
1116
1.44k
      if (
t == dst_type1.44k
)
{480
1117
480
        get_ids(space, t, 0, dst_pos, ids + off);
1118
480
        off += dst_pos;
1119
480
        get_ids(space, src_type, src_pos, n, ids + off);
1120
480
        off += n;
1121
480
        get_ids(space, t, dst_pos, s[t - o] - dst_pos,
1122
480
            ids + off);
1123
480
        off += s[t - o] - dst_pos;
1124
960
      } else 
if (960
t == src_type960
)
{480
1125
480
        get_ids(space, t, 0, src_pos, ids + off);
1126
480
        off += src_pos;
1127
480
        get_ids(space, t, src_pos + n,
1128
480
              s[t - o] - src_pos - n, ids + off);
1129
480
        off += s[t - o] - src_pos - n;
1130
480
      } else {
1131
480
        get_ids(space, t, 0, s[t - o], ids + off);
1132
480
        off += s[t - o];
1133
480
      }
1134
1.44k
    }
1135
480
    free(space->ids);
1136
480
    space->ids = ids;
1137
480
    space->n_id = space->nparam + space->n_in + space->n_out;
1138
480
  }
1139
788
1140
788
  switch (dst_type) {
1141
76
  case isl_dim_param: space->nparam += n; break;
1142
297
  case isl_dim_in:  space->n_in += n; break;
1143
415
  case isl_dim_out: space->n_out += n; break;
1144
0
  default:    ;
1145
788
  }
1146
788
1147
788
  switch (src_type) {
1148
278
  case isl_dim_param: space->nparam -= n; break;
1149
155
  case isl_dim_in:  space->n_in -= n; break;
1150
355
  case isl_dim_out: space->n_out -= n; break;
1151
0
  default:    ;
1152
788
  }
1153
788
1154
788
  
if (788
dst_type != isl_dim_param && 788
src_type != isl_dim_param712
)
1155
434
    return space;
1156
788
1157
1.06k
  
for (i = 0; 354
i < 21.06k
;
++i708
)
{708
1158
708
    if (!space->nested[i])
1159
691
      continue;
1160
17
    space->nested[i] = isl_space_replace(space->nested[i],
1161
17
             isl_dim_param, space);
1162
17
    if (!space->nested[i])
1163
0
      goto error;
1164
17
  }
1165
354
1166
354
  return space;
1167
0
error:
1168
0
  isl_space_free(space);
1169
0
  return NULL;
1170
354
}
1171
1172
/* Check that "space1" and "space2" have the same parameters,
1173
 * reporting an error if they do not.
1174
 */
1175
isl_stat isl_space_check_equal_params(__isl_keep isl_space *space1,
1176
  __isl_keep isl_space *space2)
1177
436k
{
1178
436k
  isl_bool equal;
1179
436k
1180
436k
  equal = isl_space_has_equal_params(space1, space2);
1181
436k
  if (equal < 0)
1182
1
    return isl_stat_error;
1183
436k
  
if (436k
!equal436k
)
1184
0
    isl_die(isl_space_get_ctx(space1), isl_error_invalid,
1185
436k
      "parameters need to match", return isl_stat_error);
1186
436k
  return isl_stat_ok;
1187
436k
}
1188
1189
__isl_give isl_space *isl_space_join(__isl_take isl_space *left,
1190
  __isl_take isl_space *right)
1191
366k
{
1192
366k
  isl_space *dim;
1193
366k
1194
366k
  if (isl_space_check_equal_params(left, right) < 0)
1195
1
    goto error;
1196
366k
1197
366k
  
isl_assert366k
(left->ctx,366k
1198
366k
    isl_space_tuple_is_equal(left, isl_dim_out, right, isl_dim_in),
1199
366k
    goto error);
1200
366k
1201
366k
  dim = isl_space_alloc(left->ctx, left->nparam, left->n_in, right->n_out);
1202
366k
  if (!dim)
1203
0
    goto error;
1204
366k
1205
366k
  dim = copy_ids(dim, isl_dim_param, 0, left, isl_dim_param);
1206
366k
  dim = copy_ids(dim, isl_dim_in, 0, left, isl_dim_in);
1207
366k
  dim = copy_ids(dim, isl_dim_out, 0, right, isl_dim_out);
1208
366k
1209
366k
  if (
dim && 366k
left->tuple_id[0]366k
&&
1210
122k
      !(dim->tuple_id[0] = isl_id_copy(left->tuple_id[0])))
1211
0
    goto error;
1212
366k
  
if (366k
dim && 366k
right->tuple_id[1]366k
&&
1213
110k
      !(dim->tuple_id[1] = isl_id_copy(right->tuple_id[1])))
1214
0
    goto error;
1215
366k
  
if (366k
dim && 366k
left->nested[0]366k
&&
1216
131k
      !(dim->nested[0] = isl_space_copy(left->nested[0])))
1217
0
    goto error;
1218
366k
  
if (366k
dim && 366k
right->nested[1]366k
&&
1219
162k
      !(dim->nested[1] = isl_space_copy(right->nested[1])))
1220
0
    goto error;
1221
366k
1222
366k
  isl_space_free(left);
1223
366k
  isl_space_free(right);
1224
366k
1225
366k
  return dim;
1226
1
error:
1227
1
  isl_space_free(left);
1228
1
  isl_space_free(right);
1229
1
  return NULL;
1230
366k
}
1231
1232
/* Given two map spaces { A -> C } and { B -> D }, construct the space
1233
 * { [A -> B] -> [C -> D] }.
1234
 * Given two set spaces { A } and { B }, construct the space { [A -> B] }.
1235
 */
1236
__isl_give isl_space *isl_space_product(__isl_take isl_space *left,
1237
  __isl_take isl_space *right)
1238
2.39k
{
1239
2.39k
  isl_space *dom1, *dom2, *nest1, *nest2;
1240
2.39k
  int is_set;
1241
2.39k
1242
2.39k
  if (
!left || 2.39k
!right2.39k
)
1243
0
    goto error;
1244
2.39k
1245
2.39k
  is_set = isl_space_is_set(left);
1246
2.39k
  if (is_set != isl_space_is_set(right))
1247
0
    isl_die(isl_space_get_ctx(left), isl_error_invalid,
1248
2.39k
      "expecting either two set spaces or two map spaces",
1249
2.39k
      goto error);
1250
2.39k
  
if (2.39k
is_set2.39k
)
1251
220
    return isl_space_range_product(left, right);
1252
2.39k
1253
2.17k
  
if (2.17k
isl_space_check_equal_params(left, right) < 02.17k
)
1254
0
    goto error;
1255
2.17k
1256
2.17k
  dom1 = isl_space_domain(isl_space_copy(left));
1257
2.17k
  dom2 = isl_space_domain(isl_space_copy(right));
1258
2.17k
  nest1 = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1259
2.17k
1260
2.17k
  dom1 = isl_space_range(left);
1261
2.17k
  dom2 = isl_space_range(right);
1262
2.17k
  nest2 = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1263
2.17k
1264
2.17k
  return isl_space_join(isl_space_reverse(nest1), nest2);
1265
0
error:
1266
0
  isl_space_free(left);
1267
0
  isl_space_free(right);
1268
0
  return NULL;
1269
2.17k
}
1270
1271
/* Given two spaces { A -> C } and { B -> C }, construct the space
1272
 * { [A -> B] -> C }
1273
 */
1274
__isl_give isl_space *isl_space_domain_product(__isl_take isl_space *left,
1275
  __isl_take isl_space *right)
1276
1.46k
{
1277
1.46k
  isl_space *ran, *dom1, *dom2, *nest;
1278
1.46k
1279
1.46k
  if (isl_space_check_equal_params(left, right) < 0)
1280
0
    goto error;
1281
1.46k
1282
1.46k
  
if (1.46k
!isl_space_tuple_is_equal(left, isl_dim_out, right, isl_dim_out)1.46k
)
1283
0
    isl_die(left->ctx, isl_error_invalid,
1284
1.46k
      "ranges need to match", goto error);
1285
1.46k
1286
1.46k
  ran = isl_space_range(isl_space_copy(left));
1287
1.46k
1288
1.46k
  dom1 = isl_space_domain(left);
1289
1.46k
  dom2 = isl_space_domain(right);
1290
1.46k
  nest = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1291
1.46k
1292
1.46k
  return isl_space_join(isl_space_reverse(nest), ran);
1293
0
error:
1294
0
  isl_space_free(left);
1295
0
  isl_space_free(right);
1296
0
  return NULL;
1297
1.46k
}
1298
1299
__isl_give isl_space *isl_space_range_product(__isl_take isl_space *left,
1300
  __isl_take isl_space *right)
1301
65.6k
{
1302
65.6k
  isl_space *dom, *ran1, *ran2, *nest;
1303
65.6k
1304
65.6k
  if (isl_space_check_equal_params(left, right) < 0)
1305
0
    goto error;
1306
65.6k
1307
65.6k
  
if (65.6k
!isl_space_tuple_is_equal(left, isl_dim_in, right, isl_dim_in)65.6k
)
1308
0
    isl_die(left->ctx, isl_error_invalid,
1309
65.6k
      "domains need to match", goto error);
1310
65.6k
1311
65.6k
  dom = isl_space_domain(isl_space_copy(left));
1312
65.6k
1313
65.6k
  ran1 = isl_space_range(left);
1314
65.6k
  ran2 = isl_space_range(right);
1315
65.6k
  nest = isl_space_wrap(isl_space_join(isl_space_reverse(ran1), ran2));
1316
65.6k
1317
65.6k
  return isl_space_join(isl_space_reverse(dom), nest);
1318
0
error:
1319
0
  isl_space_free(left);
1320
0
  isl_space_free(right);
1321
0
  return NULL;
1322
65.6k
}
1323
1324
/* Given a space of the form [A -> B] -> [C -> D], return the space A -> C.
1325
 */
1326
__isl_give isl_space *isl_space_factor_domain(__isl_take isl_space *space)
1327
0
{
1328
0
  space = isl_space_domain_factor_domain(space);
1329
0
  space = isl_space_range_factor_domain(space);
1330
0
  return space;
1331
0
}
1332
1333
/* Given a space of the form [A -> B] -> C, return the space A -> C.
1334
 */
1335
__isl_give isl_space *isl_space_domain_factor_domain(
1336
  __isl_take isl_space *space)
1337
418
{
1338
418
  isl_space *nested;
1339
418
  isl_space *domain;
1340
418
1341
418
  if (!space)
1342
0
    return NULL;
1343
418
  
if (418
!isl_space_domain_is_wrapping(space)418
)
1344
0
    isl_die(isl_space_get_ctx(space), isl_error_invalid,
1345
418
      "domain not a product", return isl_space_free(space));
1346
418
1347
418
  nested = space->nested[0];
1348
418
  domain = isl_space_copy(space);
1349
418
  domain = isl_space_drop_dims(domain, isl_dim_in,
1350
418
          nested->n_in, nested->n_out);
1351
418
  if (!domain)
1352
0
    return isl_space_free(space);
1353
418
  
if (418
nested->tuple_id[0]418
)
{259
1354
259
    domain->tuple_id[0] = isl_id_copy(nested->tuple_id[0]);
1355
259
    if (!domain->tuple_id[0])
1356
0
      goto error;
1357
259
  }
1358
418
  
if (418
nested->nested[0]418
)
{159
1359
159
    domain->nested[0] = isl_space_copy(nested->nested[0]);
1360
159
    if (!domain->nested[0])
1361
0
      goto error;
1362
159
  }
1363
418
1364
418
  isl_space_free(space);
1365
418
  return domain;
1366
0
error:
1367
0
  isl_space_free(space);
1368
0
  isl_space_free(domain);
1369
0
  return NULL;
1370
418
}
1371
1372
/* Given a space of the form [A -> B] -> C, return the space B -> C.
1373
 */
1374
__isl_give isl_space *isl_space_domain_factor_range(
1375
  __isl_take isl_space *space)
1376
17.4k
{
1377
17.4k
  isl_space *nested;
1378
17.4k
  isl_space *range;
1379
17.4k
1380
17.4k
  if (!space)
1381
0
    return NULL;
1382
17.4k
  
if (17.4k
!isl_space_domain_is_wrapping(space)17.4k
)
1383
0
    isl_die(isl_space_get_ctx(space), isl_error_invalid,
1384
17.4k
      "domain not a product", return isl_space_free(space));
1385
17.4k
1386
17.4k
  nested = space->nested[0];
1387
17.4k
  range = isl_space_copy(space);
1388
17.4k
  range = isl_space_drop_dims(range, isl_dim_in, 0, nested->n_in);
1389
17.4k
  if (!range)
1390
0
    return isl_space_free(space);
1391
17.4k
  
if (17.4k
nested->tuple_id[1]17.4k
)
{10.8k
1392
10.8k
    range->tuple_id[0] = isl_id_copy(nested->tuple_id[1]);
1393
10.8k
    if (!range->tuple_id[0])
1394
0
      goto error;
1395
10.8k
  }
1396
17.4k
  
if (17.4k
nested->nested[1]17.4k
)
{6.58k
1397
6.58k
    range->nested[0] = isl_space_copy(nested->nested[1]);
1398
6.58k
    if (!range->nested[0])
1399
0
      goto error;
1400
6.58k
  }
1401
17.4k
1402
17.4k
  isl_space_free(space);
1403
17.4k
  return range;
1404
0
error:
1405
0
  isl_space_free(space);
1406
0
  isl_space_free(range);
1407
0
  return NULL;
1408
17.4k
}
1409
1410
/* Given a space of the form A -> [B -> C], return the space A -> B.
1411
 */
1412
__isl_give isl_space *isl_space_range_factor_domain(
1413
  __isl_take isl_space *space)
1414
1.64k
{
1415
1.64k
  isl_space *nested;
1416
1.64k
  isl_space *domain;
1417
1.64k
1418
1.64k
  if (!space)
1419
0
    return NULL;
1420
1.64k
  
if (1.64k
!isl_space_range_is_wrapping(space)1.64k
)
1421
0
    isl_die(isl_space_get_ctx(space), isl_error_invalid,
1422
1.64k
      "range not a product", return isl_space_free(space));
1423
1.64k
1424
1.64k
  nested = space->nested[1];
1425
1.64k
  domain = isl_space_copy(space);
1426
1.64k
  domain = isl_space_drop_dims(domain, isl_dim_out,
1427
1.64k
          nested->n_in, nested->n_out);
1428
1.64k
  if (!domain)
1429
0
    return isl_space_free(space);
1430
1.64k
  
if (1.64k
nested->tuple_id[0]1.64k
)
{951
1431
951
    domain->tuple_id[1] = isl_id_copy(nested->tuple_id[0]);
1432
951
    if (!domain->tuple_id[1])
1433
0
      goto error;
1434
951
  }
1435
1.64k
  
if (1.64k
nested->nested[0]1.64k
)
{674
1436
674
    domain->nested[1] = isl_space_copy(nested->nested[0]);
1437
674
    if (!domain->nested[1])
1438
0
      goto error;
1439
674
  }
1440
1.64k
1441
1.64k
  isl_space_free(space);
1442
1.64k
  return domain;
1443
0
error:
1444
0
  isl_space_free(space);
1445
0
  isl_space_free(domain);
1446
0
  return NULL;
1447
1.64k
}
1448
1449
/* Internal function that selects the range of the map that is
1450
 * embedded in either a set space or the range of a map space.
1451
 * In particular, given a space of the form [A -> B], return the space B.
1452
 * Given a space of the form A -> [B -> C], return the space A -> C.
1453
 */
1454
static __isl_give isl_space *range_factor_range(__isl_take isl_space *space)
1455
11.3k
{
1456
11.3k
  isl_space *nested;
1457
11.3k
  isl_space *range;
1458
11.3k
1459
11.3k
  if (!space)
1460
0
    return NULL;
1461
11.3k
1462
11.3k
  nested = space->nested[1];
1463
11.3k
  range = isl_space_copy(space);
1464
11.3k
  range = isl_space_drop_dims(range, isl_dim_out, 0, nested->n_in);
1465
11.3k
  if (!range)
1466
0
    return isl_space_free(space);
1467
11.3k
  
if (11.3k
nested->tuple_id[1]11.3k
)
{524
1468
524
    range->tuple_id[1] = isl_id_copy(nested->tuple_id[1]);
1469
524
    if (!range->tuple_id[1])
1470
0
      goto error;
1471
524
  }
1472
11.3k
  
if (11.3k
nested->nested[1]11.3k
)
{10.7k
1473
10.7k
    range->nested[1] = isl_space_copy(nested->nested[1]);
1474
10.7k
    if (!range->nested[1])
1475
0
      goto error;
1476
10.7k
  }
1477
11.3k
1478
11.3k
  isl_space_free(space);
1479
11.3k
  return range;
1480
0
error:
1481
0
  isl_space_free(space);
1482
0
  isl_space_free(range);
1483
0
  return NULL;
1484
11.3k
}
1485
1486
/* Given a space of the form A -> [B -> C], return the space A -> C.
1487
 */
1488
__isl_give isl_space *isl_space_range_factor_range(
1489
  __isl_take isl_space *space)
1490
11.3k
{
1491
11.3k
  if (!space)
1492
0
    return NULL;
1493
11.3k
  
if (11.3k
!isl_space_range_is_wrapping(space)11.3k
)
1494
0
    isl_die(isl_space_get_ctx(space), isl_error_invalid,
1495
11.3k
      "range not a product", return isl_space_free(space));
1496
11.3k
1497
11.3k
  return range_factor_range(space);
1498
11.3k
}
1499
1500
/* Given a space of the form [A -> B], return the space B.
1501
 */
1502
static __isl_give isl_space *set_factor_range(__isl_take isl_space *space)
1503
0
{
1504
0
  if (!space)
1505
0
    return NULL;
1506
0
  
if (0
!isl_space_is_wrapping(space)0
)
1507
0
    isl_die(isl_space_get_ctx(space), isl_error_invalid,
1508
0
      "not a product", return isl_space_free(space));
1509
0
1510
0
  return range_factor_range(space);
1511
0
}
1512
1513
/* Given a space of the form [A -> B] -> [C -> D], return the space B -> D.
1514
 * Given a space of the form [A -> B], return the space B.
1515
 */
1516
__isl_give isl_space *isl_space_factor_range(__isl_take isl_space *space)
1517
10.7k
{
1518
10.7k
  if (!space)
1519
0
    return NULL;
1520
10.7k
  
if (10.7k
isl_space_is_set(space)10.7k
)
1521
0
    return set_factor_range(space);
1522
10.7k
  space = isl_space_domain_factor_range(space);
1523
10.7k
  space = isl_space_range_factor_range(space);
1524
10.7k
  return space;
1525
10.7k
}
1526
1527
__isl_give isl_space *isl_space_map_from_set(__isl_take isl_space *space)
1528
9.01k
{
1529
9.01k
  isl_ctx *ctx;
1530
9.01k
  isl_id **ids = NULL;
1531
9.01k
  int n_id;
1532
9.01k
1533
9.01k
  if (!space)
1534
3
    return NULL;
1535
9.00k
  ctx = isl_space_get_ctx(space);
1536
9.00k
  if (!isl_space_is_set(space))
1537
0
    isl_die(ctx, isl_error_invalid, "not a set space", goto error);
1538
9.00k
  space = isl_space_cow(space);
1539
9.00k
  if (!space)
1540
0
    return NULL;
1541
9.00k
  n_id = space->nparam + space->n_out + space->n_out;
1542
9.00k
  if (
n_id > 0 && 9.00k
space->ids8.49k
)
{6.85k
1543
6.85k
    ids = isl_calloc_array(space->ctx, isl_id *, n_id);
1544
6.85k
    if (!ids)
1545
0
      goto error;
1546
6.85k
    get_ids(space, isl_dim_param, 0, space->nparam, ids);
1547
6.85k
    get_ids(space, isl_dim_out, 0, space->n_out,
1548
6.85k
      ids + space->nparam);
1549
6.85k
  }
1550
9.00k
  space->n_in = space->n_out;
1551
9.00k
  if (
ids9.00k
)
{6.85k
1552
6.85k
    free(space->ids);
1553
6.85k
    space->ids = ids;
1554
6.85k
    space->n_id = n_id;
1555
6.85k
    space = copy_ids(space, isl_dim_out, 0, space, isl_dim_in);
1556
6.85k
  }
1557
9.00k
  isl_id_free(space->tuple_id[0]);
1558
9.00k
  space->tuple_id[0] = isl_id_copy(space->tuple_id[1]);
1559
9.00k
  isl_space_free(space->nested[0]);
1560
9.00k
  space->nested[0] = isl_space_copy(space->nested[1]);
1561
9.00k
  return space;
1562
0
error:
1563
0
  isl_space_free(space);
1564
0
  return NULL;
1565
9.00k
}
1566
1567
__isl_give isl_space *isl_space_map_from_domain_and_range(
1568
  __isl_take isl_space *domain, __isl_take isl_space *range)
1569
46.4k
{
1570
46.4k
  if (
!domain || 46.4k
!range46.4k
)
1571
1
    goto error;
1572
46.4k
  
if (46.4k
!isl_space_is_set(domain)46.4k
)
1573
0
    isl_die(isl_space_get_ctx(domain), isl_error_invalid,
1574
46.4k
      "domain is not a set space", goto error);
1575
46.4k
  
if (46.4k
!isl_space_is_set(range)46.4k
)
1576
0
    isl_die(isl_space_get_ctx(range), isl_error_invalid,
1577
46.4k
      "range is not a set space", goto error);
1578
46.4k
  return isl_space_join(isl_space_reverse(domain), range);
1579
1
error:
1580
1
  isl_space_free(domain);
1581
1
  isl_space_free(range);
1582
1
  return NULL;
1583
46.4k
}
1584
1585
static __isl_give isl_space *set_ids(__isl_take isl_space *dim,
1586
  enum isl_dim_type type,
1587
  unsigned first, unsigned n, __isl_take isl_id **ids)
1588
1.65M
{
1589
1.65M
  int i;
1590
1.65M
1591
4.13M
  for (i = 0; 
i < n4.13M
;
++i2.48M
)
1592
2.48M
    dim = set_id(dim, type, first + i, ids[i]);
1593
1.65M
1594
1.65M
  return dim;
1595
1.65M
}
1596
1597
__isl_give isl_space *isl_space_reverse(__isl_take isl_space *dim)
1598
1.28M
{
1599
1.28M
  unsigned t;
1600
1.28M
  isl_space *nested;
1601
1.28M
  isl_id **ids = NULL;
1602
1.28M
  isl_id *id;
1603
1.28M
1604
1.28M
  if (!dim)
1605
0
    return NULL;
1606
1.28M
  
if (1.28M
match(dim, isl_dim_in, dim, isl_dim_out)1.28M
)
1607
75.5k
    return dim;
1608
1.28M
1609
1.20M
  dim = isl_space_cow(dim);
1610
1.20M
  if (!dim)
1611
1
    return NULL;
1612
1.20M
1613
1.20M
  id = dim->tuple_id[0];
1614
1.20M
  dim->tuple_id[0] = dim->tuple_id[1];
1615
1.20M
  dim->tuple_id[1] = id;
1616
1.20M
1617
1.20M
  nested = dim->nested[0];
1618
1.20M
  dim->nested[0] = dim->nested[1];
1619
1.20M
  dim->nested[1] = nested;
1620
1.20M
1621
1.20M
  if (
dim->ids1.20M
)
{828k
1622
828k
    int n_id = dim->n_in + dim->n_out;
1623
828k
    ids = isl_alloc_array(dim->ctx, isl_id *, n_id);
1624
828k
    if (
n_id && 828k
!ids703k
)
1625
0
      goto error;
1626
828k
    get_ids(dim, isl_dim_in, 0, dim->n_in, ids);
1627
828k
    get_ids(dim, isl_dim_out, 0, dim->n_out, ids + dim->n_in);
1628
828k
  }
1629
1.20M
1630
1.20M
  t = dim->n_in;
1631
1.20M
  dim->n_in = dim->n_out;
1632
1.20M
  dim->n_out = t;
1633
1.20M
1634
1.20M
  if (
dim->ids1.20M
)
{828k
1635
828k
    dim = set_ids(dim, isl_dim_out, 0, dim->n_out, ids);
1636
828k
    dim = set_ids(dim, isl_dim_in, 0, dim->n_in, ids + dim->n_out);
1637
828k
    free(ids);
1638
828k
  }
1639
1.20M
1640
1.20M
  return dim;
1641
0
error:
1642
0
  free(ids);
1643
0
  isl_space_free(dim);
1644
0
  return NULL;
1645
1.20M
}
1646
1647
__isl_give isl_space *isl_space_drop_dims(__isl_take isl_space *dim,
1648
  enum isl_dim_type type, unsigned first, unsigned num)
1649
1.61M
{
1650
1.61M
  int i;
1651
1.61M
1652
1.61M
  if (!dim)
1653
2
    return NULL;
1654
1.61M
1655
1.61M
  
if (1.61M
num == 01.61M
)
1656
336k
    return isl_space_reset(dim, type);
1657
1.61M
1658
1.27M
  
if (1.27M
!valid_dim_type(type)1.27M
)
1659
0
    isl_die(dim->ctx, isl_error_invalid,
1660
1.27M
      "cannot drop dimensions of specified type", goto error);
1661
1.27M
1662
1.27M
  
if (1.27M
first + num > n(dim, type) || 1.27M
first + num < first1.27M
)
1663
0
    isl_die(isl_space_get_ctx(dim), isl_error_invalid,
1664
1.27M
      "index out of bounds", return isl_space_free(dim));
1665
1.27M
  dim = isl_space_cow(dim);
1666
1.27M
  if (!dim)
1667
0
    goto error;
1668
1.27M
  
if (1.27M
dim->ids1.27M
)
{777k
1669
777k
    dim = extend_ids(dim);
1670
777k
    if (!dim)
1671
0
      goto error;
1672
2.77M
    
for (i = 0; 777k
i < num2.77M
;
++i2.00M
)
1673
2.00M
      isl_id_free(get_id(dim, type, first + i));
1674
871k
    for (i = first+num; 
i < n(dim, type)871k
;
++i94.4k
)
1675
94.4k
      set_id(dim, type, i - num, get_id(dim, type, i));
1676
777k
    switch (type) {
1677
75.5k
    case isl_dim_param:
1678
75.5k
      get_ids(dim, isl_dim_in, 0, dim->n_in,
1679
75.5k
        dim->ids + offset(dim, isl_dim_in) - num);
1680
284k
    case isl_dim_in:
1681
284k
      get_ids(dim, isl_dim_out, 0, dim->n_out,
1682
284k
        dim->ids + offset(dim, isl_dim_out) - num);
1683
777k
    default:
1684
777k
      ;
1685
777k
    }
1686
777k
    dim->n_id -= num;
1687
777k
  }
1688
1.27M
  switch (type) {
1689
75.6k
  case isl_dim_param: dim->nparam -= num; break;
1690
273k
  case isl_dim_in:  dim->n_in -= num; break;
1691
928k
  case isl_dim_out: dim->n_out -= num; break;
1692
0
  default:    ;
1693
1.27M
  }
1694
1.27M
  dim = isl_space_reset(dim, type);
1695
1.27M
  if (
type == isl_dim_param1.27M
)
{75.6k
1696
75.6k
    if (
dim && 75.6k
dim->nested[0]75.6k
&&
1697
2.71k
        !(dim->nested[0] = isl_space_drop_dims(dim->nested[0],
1698
2.71k
                isl_dim_param, first, num)))
1699
0
      goto error;
1700
75.6k
    
if (75.6k
dim && 75.6k
dim->nested[1]75.6k
&&
1701
2.04k
        !(dim->nested[1] = isl_space_drop_dims(dim->nested[1],
1702
2.04k
                isl_dim_param, first, num)))
1703
0
      goto error;
1704
75.6k
  }
1705
1.27M
  return dim;
1706
0
error:
1707
0
  isl_space_free(dim);
1708
0
  return NULL;
1709
1.27M
}
1710
1711
__isl_give isl_space *isl_space_drop_inputs(__isl_take isl_space *dim,
1712
    unsigned first, unsigned n)
1713
0
{
1714
0
  if (!dim)
1715
0
    return NULL;
1716
0
  return isl_space_drop_dims(dim, isl_dim_in, first, n);
1717
0
}
1718
1719
__isl_give isl_space *isl_space_drop_outputs(__isl_take isl_space *dim,
1720
    unsigned first, unsigned n)
1721
0
{
1722
0
  if (!dim)
1723
0
    return NULL;
1724
0
  return isl_space_drop_dims(dim, isl_dim_out, first, n);
1725
0
}
1726
1727
__isl_give isl_space *isl_space_domain(__isl_take isl_space *space)
1728
469k
{
1729
469k
  if (!space)
1730
0
    return NULL;
1731
469k
  space = isl_space_drop_dims(space, isl_dim_out, 0, space->n_out);
1732
469k
  space = isl_space_reverse(space);
1733
469k
  space = mark_as_set(space);
1734
469k
  return space;
1735
469k
}
1736
1737
__isl_give isl_space *isl_space_from_domain(__isl_take isl_space *dim)
1738
425k
{
1739
425k
  if (!dim)
1740
0
    return NULL;
1741
425k
  
if (425k
!isl_space_is_set(dim)425k
)
1742
0
    isl_die(isl_space_get_ctx(dim), isl_error_invalid,
1743
425k
      "not a set space", goto error);
1744
425k
  dim = isl_space_reverse(dim);
1745
425k
  dim = isl_space_reset(dim, isl_dim_out);
1746
425k
  return dim;
1747
0
error:
1748
0
  isl_space_free(dim);
1749
0
  return NULL;
1750
425k
}
1751
1752
__isl_give isl_space *isl_space_range(__isl_take isl_space *space)
1753
242k
{
1754
242k
  if (!space)
1755
0
    return NULL;
1756
242k
  space = isl_space_drop_dims(space, isl_dim_in, 0, space->n_in);
1757
242k
  space = mark_as_set(space);
1758
242k
  return space;
1759
242k
}
1760
1761
__isl_give isl_space *isl_space_from_range(__isl_take isl_space *dim)
1762
155k
{
1763
155k
  if (!dim)
1764
4
    return NULL;
1765
155k
  
if (155k
!isl_space_is_set(dim)155k
)
1766
0
    isl_die(isl_space_get_ctx(dim), isl_error_invalid,
1767
155k
      "not a set space", goto error);
1768
155k
  return isl_space_reset(dim, isl_dim_in);
1769
0
error:
1770
0
  isl_space_free(dim);
1771
0
  return NULL;
1772
155k
}
1773
1774
/* Given a map space A -> B, return the map space [A -> B] -> A.
1775
 */
1776
__isl_give isl_space *isl_space_domain_map(__isl_take isl_space *space)
1777
931
{
1778
931
  isl_space *domain;
1779
931
1780
931
  domain = isl_space_from_range(isl_space_domain(isl_space_copy(space)));
1781
931
  space = isl_space_from_domain(isl_space_wrap(space));
1782
931
  space = isl_space_join(space, domain);
1783
931
1784
931
  return space;
1785
931
}
1786
1787
/* Given a map space A -> B, return the map space [A -> B] -> B.
1788
 */
1789
__isl_give isl_space *isl_space_range_map(__isl_take isl_space *space)
1790
0
{
1791
0
  isl_space *range;
1792
0
1793
0
  range = isl_space_from_range(isl_space_range(isl_space_copy(space)));
1794
0
  space = isl_space_from_domain(isl_space_wrap(space));
1795
0
  space = isl_space_join(space, range);
1796
0
1797
0
  return space;
1798
0
}
1799
1800
__isl_give isl_space *isl_space_params(__isl_take isl_space *space)
1801
341k
{
1802
341k
  if (isl_space_is_params(space))
1803
257k
    return space;
1804
84.4k
  space = isl_space_drop_dims(space,
1805
84.4k
          isl_dim_in, 0, isl_space_dim(space, isl_dim_in));
1806
84.4k
  space = isl_space_drop_dims(space,
1807
84.4k
          isl_dim_out, 0, isl_space_dim(space, isl_dim_out));
1808
84.4k
  space = mark_as_params(space);
1809
84.4k
  return space;
1810
341k
}
1811
1812
__isl_give isl_space *isl_space_set_from_params(__isl_take isl_space *space)
1813
12.2k
{
1814
12.2k
  if (!space)
1815
0
    return NULL;
1816
12.2k
  
if (12.2k
!isl_space_is_params(space)12.2k
)
1817
0
    isl_die(isl_space_get_ctx(space), isl_error_invalid,
1818
12.2k
      "not a parameter space", goto error);
1819
12.2k
  return isl_space_reset(space, isl_dim_set);
1820
0
error:
1821
0
  isl_space_free(space);
1822
0
  return NULL;
1823
12.2k
}
1824
1825
__isl_give isl_space *isl_space_underlying(__isl_take isl_space *dim,
1826
  unsigned n_div)
1827
333k
{
1828
333k
  int i;
1829
333k
1830
333k
  if (!dim)
1831
0
    return NULL;
1832
333k
  
if (333k
n_div == 0 &&333k
1833
321k
      
dim->nparam == 0321k
&&
dim->n_in == 0161k
&&
dim->n_id == 0130k
)
1834
77.1k
    return isl_space_reset(isl_space_reset(dim, isl_dim_in), isl_dim_out);
1835
256k
  dim = isl_space_cow(dim);
1836
256k
  if (!dim)
1837
0
    return NULL;
1838
256k
  dim->n_out += dim->nparam + dim->n_in + n_div;
1839
256k
  dim->nparam = 0;
1840
256k
  dim->n_in = 0;
1841
256k
1842
1.46M
  for (i = 0; 
i < dim->n_id1.46M
;
++i1.21M
)
1843
1.21M
    isl_id_free(get_id(dim, isl_dim_out, i));
1844
256k
  dim->n_id = 0;
1845
256k
  dim = isl_space_reset(dim, isl_dim_in);
1846
256k
  dim = isl_space_reset(dim, isl_dim_out);
1847
256k
1848
256k
  return dim;
1849
256k
}
1850
1851
/* Are the two spaces the same, including positions and names of parameters?
1852
 */
1853
isl_bool isl_space_is_equal(__isl_keep isl_space *space1,
1854
  __isl_keep isl_space *space2)
1855
5.52M
{
1856
5.52M
  isl_bool equal;
1857
5.52M
1858
5.52M
  if (
!space1 || 5.52M
!space25.52M
)
1859
0
    return isl_bool_error;
1860
5.52M
  
if (5.52M
space1 == space25.52M
)
1861
3.04M
    return isl_bool_true;
1862
2.48M
  equal = isl_space_has_equal_params(space1, space2);
1863
2.48M
  if (
equal < 0 || 2.48M
!equal2.48M
)
1864
58.0k
    return equal;
1865
2.42M
  return isl_space_has_equal_tuples(space1, space2);
1866
2.48M
}
1867
1868
/* Is space1 equal to the domain of space2?
1869
 *
1870
 * In the internal version we also allow space2 to be the space of a set,
1871
 * provided space1 is a parameter space.
1872
 */
1873
isl_bool isl_space_is_domain_internal(__isl_keep isl_space *space1,
1874
  __isl_keep isl_space *space2)
1875
47
{
1876
47
  isl_bool equal_params;
1877
47
1878
47
  if (
!space1 || 47
!space247
)
1879
0
    return isl_bool_error;
1880
47
  
if (47
!isl_space_is_set(space1)47
)
1881
0
    return isl_bool_false;
1882
47
  equal_params = isl_space_has_equal_params(space1, space2);
1883
47
  if (
equal_params < 0 || 47
!equal_params47
)
1884
0
    return equal_params;
1885
47
  return isl_space_tuple_is_equal(space1, isl_dim_set,
1886
47
          space2, isl_dim_in);
1887
47
}
1888
1889
/* Is space1 equal to the domain of space2?
1890
 */
1891
isl_bool isl_space_is_domain(__isl_keep isl_space *space1,
1892
  __isl_keep isl_space *space2)
1893
19
{
1894
19
  if (!space2)
1895
0
    return isl_bool_error;
1896
19
  
if (19
!isl_space_is_map(space2)19
)
1897
0
    return isl_bool_false;
1898
19
  return isl_space_is_domain_internal(space1, space2);
1899
19
}
1900
1901
/* Is space1 equal to the range of space2?
1902
 *
1903
 * In the internal version, space2 is allowed to be the space of a set,
1904
 * in which case it should be equal to space1.
1905
 */
1906
isl_bool isl_space_is_range_internal(__isl_keep isl_space *space1,
1907
  __isl_keep isl_space *space2)
1908
9.97k
{
1909
9.97k
  isl_bool equal_params;
1910
9.97k
1911
9.97k
  if (
!space1 || 9.97k
!space29.97k
)
1912
0
    return isl_bool_error;
1913
9.97k
  
if (9.97k
!isl_space_is_set(space1)9.97k
)
1914
0
    return isl_bool_false;
1915
9.97k
  equal_params = isl_space_has_equal_params(space1, space2);
1916
9.97k
  if (
equal_params < 0 || 9.97k
!equal_params9.97k
)
1917
0
    return equal_params;
1918
9.97k
  return isl_space_tuple_is_equal(space1, isl_dim_set,
1919
9.97k
          space2, isl_dim_out);
1920
9.97k
}
1921
1922
/* Is space1 equal to the range of space2?
1923
 */
1924
isl_bool isl_space_is_range(__isl_keep isl_space *space1,
1925
  __isl_keep isl_space *space2)
1926
0
{
1927
0
  if (!space2)
1928
0
    return isl_bool_error;
1929
0
  
if (0
!isl_space_is_map(space2)0
)
1930
0
    return isl_bool_false;
1931
0
  return isl_space_is_range_internal(space1, space2);
1932
0
}
1933
1934
/* Update "hash" by hashing in the parameters of "space".
1935
 */
1936
static uint32_t isl_hash_params(uint32_t hash, __isl_keep isl_space *space)
1937
388k
{
1938
388k
  int i;
1939
388k
  isl_id *id;
1940
388k
1941
388k
  if (!space)
1942
0
    return hash;
1943
388k
1944
388k
  
isl_hash_byte388k
(hash, space->nparam % 256);388k
1945
388k
1946
1.07M
  for (i = 0; 
i < space->nparam1.07M
;
++i686k
)
{686k
1947
686k
    id = get_id(space, isl_dim_param, i);
1948
686k
    hash = isl_hash_id(hash, id);
1949
686k
  }
1950
388k
1951
388k
  return hash;
1952
388k
}
1953
1954
/* Update "hash" by hashing in the tuples of "space".
1955
 * Changes in this function should be reflected in isl_hash_tuples_domain.
1956
 */
1957
static uint32_t isl_hash_tuples(uint32_t hash, __isl_keep isl_space *space)
1958
1.43M
{
1959
1.43M
  isl_id *id;
1960
1.43M
1961
1.43M
  if (!space)
1962
912k
    return hash;
1963
1.43M
1964
521k
  
isl_hash_byte521k
(hash, space->n_in % 256);521k
1965
521k
  isl_hash_byte(hash, space->n_out % 256);
1966
521k
1967
521k
  id = tuple_id(space, isl_dim_in);
1968
521k
  hash = isl_hash_id(hash, id);
1969
521k
  id = tuple_id(space, isl_dim_out);
1970
521k
  hash = isl_hash_id(hash, id);
1971
521k
1972
521k
  hash = isl_hash_tuples(hash, space->nested[0]);
1973
521k
  hash = isl_hash_tuples(hash, space->nested[1]);
1974
521k
1975
521k
  return hash;
1976
1.43M
}
1977
1978
/* Update "hash" by hashing in the domain tuple of "space".
1979
 * The result of this function is equal to the result of applying
1980
 * isl_hash_tuples to the domain of "space".
1981
 */
1982
static uint32_t isl_hash_tuples_domain(uint32_t hash,
1983
  __isl_keep isl_space *space)
1984
26.8k
{
1985
26.8k
  isl_id *id;
1986
26.8k
1987
26.8k
  if (!space)
1988
0
    return hash;
1989
26.8k
1990
26.8k
  
isl_hash_byte26.8k
(hash, 0);26.8k
1991
26.8k
  isl_hash_byte(hash, space->n_in % 256);
1992
26.8k
1993
26.8k
  hash = isl_hash_id(hash, &isl_id_none);
1994
26.8k
  id = tuple_id(space, isl_dim_in);
1995
26.8k
  hash = isl_hash_id(hash, id);
1996
26.8k
1997
26.8k
  hash = isl_hash_tuples(hash, space->nested[0]);
1998
26.8k
1999
26.8k
  return hash;
2000
26.8k
}
2001
2002
/* Return a hash value that digests the tuples of "space",
2003
 * i.e., that ignores the parameters.
2004
 */
2005
uint32_t isl_space_get_tuple_hash(__isl_keep isl_space *space)
2006
1.94k
{
2007
1.94k
  uint32_t hash;
2008
1.94k
2009
1.94k
  if (!space)
2010
0
    return 0;
2011
1.94k
2012
1.94k
  
hash = 1.94k
isl_hash_init1.94k
();
2013
1.94k
  hash = isl_hash_tuples(hash, space);
2014
1.94k
2015
1.94k
  return hash;
2016
1.94k
}
2017
2018
uint32_t isl_space_get_hash(__isl_keep isl_space *space)
2019
362k
{
2020
362k
  uint32_t hash;
2021
362k
2022
362k
  if (!space)
2023
0
    return 0;
2024
362k
2025
362k
  
hash = 362k
isl_hash_init362k
();
2026
362k
  hash = isl_hash_params(hash, space);
2027
362k
  hash = isl_hash_tuples(hash, space);
2028
362k
2029
362k
  return hash;
2030
362k
}
2031
2032
/* Return the hash value of the domain of "space".
2033
 * That is, isl_space_get_domain_hash(space) is equal to
2034
 * isl_space_get_hash(isl_space_domain(space)).
2035
 */
2036
uint32_t isl_space_get_domain_hash(__isl_keep isl_space *space)
2037
26.8k
{
2038
26.8k
  uint32_t hash;
2039
26.8k
2040
26.8k
  if (!space)
2041
0
    return 0;
2042
26.8k
2043
26.8k
  
hash = 26.8k
isl_hash_init26.8k
();
2044
26.8k
  hash = isl_hash_params(hash, space);
2045
26.8k
  hash = isl_hash_tuples_domain(hash, space);
2046
26.8k
2047
26.8k
  return hash;
2048
26.8k
}
2049
2050
isl_bool isl_space_is_wrapping(__isl_keep isl_space *dim)
2051
45.5k
{
2052
45.5k
  if (!dim)
2053
0
    return isl_bool_error;
2054
45.5k
2055
45.5k
  
if (45.5k
!isl_space_is_set(dim)45.5k
)
2056
0
    return isl_bool_false;
2057
45.5k
2058
45.5k
  return dim->nested[1] != NULL;
2059
45.5k
}
2060
2061
/* Is "space" the space of a map where the domain is a wrapped map space?
2062
 */
2063
isl_bool isl_space_domain_is_wrapping(__isl_keep isl_space *space)
2064
41.3k
{
2065
41.3k
  if (!space)
2066
0
    return isl_bool_error;
2067
41.3k
2068
41.3k
  
if (41.3k
isl_space_is_set(space)41.3k
)
2069
0
    return isl_bool_false;
2070
41.3k
2071
41.3k
  return space->nested[0] != NULL;
2072
41.3k
}
2073
2074
/* Is "space" the space of a map where the range is a wrapped map space?
2075
 */
2076
isl_bool isl_space_range_is_wrapping(__isl_keep isl_space *space)
2077
55.5k
{
2078
55.5k
  if (!space)
2079
0
    return isl_bool_error;
2080
55.5k
2081
55.5k
  
if (55.5k
isl_space_is_set(space)55.5k
)
2082
730
    return isl_bool_false;
2083
55.5k
2084
54.7k
  return space->nested[1] != NULL;
2085
55.5k
}
2086
2087
/* Is "space" a product of two spaces?
2088
 * That is, is it a wrapping set space or a map space
2089
 * with wrapping domain and range?
2090
 */
2091
isl_bool isl_space_is_product(__isl_keep isl_space *space)
2092
5.02k
{
2093
5.02k
  isl_bool is_set;
2094
5.02k
  isl_bool is_product;
2095
5.02k
2096
5.02k
  is_set = isl_space_is_set(space);
2097
5.02k
  if (is_set < 0)
2098
0
    return isl_bool_error;
2099
5.02k
  
if (5.02k
is_set5.02k
)
2100
0
    return isl_space_is_wrapping(space);
2101
5.02k
  is_product = isl_space_domain_is_wrapping(space);
2102
5.02k
  if (
is_product < 0 || 5.02k
!is_product5.02k
)
2103
34
    return is_product;
2104
4.99k
  return isl_space_range_is_wrapping(space);
2105
5.02k
}
2106
2107
__isl_give isl_space *isl_space_wrap(__isl_take isl_space *dim)
2108
149k
{
2109
149k
  isl_space *wrap;
2110
149k
2111
149k
  if (!dim)
2112
0
    return NULL;
2113
149k
2114
149k
  wrap = isl_space_set_alloc(dim->ctx,
2115
149k
            dim->nparam, dim->n_in + dim->n_out);
2116
149k
2117
149k
  wrap = copy_ids(wrap, isl_dim_param, 0, dim, isl_dim_param);
2118
149k
  wrap = copy_ids(wrap, isl_dim_set, 0, dim, isl_dim_in);
2119
149k
  wrap = copy_ids(wrap, isl_dim_set, dim->n_in, dim, isl_dim_out);
2120
149k
2121
149k
  if (!wrap)
2122
0
    goto error;
2123
149k
2124
149k
  wrap->nested[1] = dim;
2125
149k
2126
149k
  return wrap;
2127
0
error:
2128
0
  isl_space_free(dim);
2129
0
  return NULL;
2130
149k
}
2131
2132
__isl_give isl_space *isl_space_unwrap(__isl_take isl_space *dim)
2133
31.5k
{
2134
31.5k
  isl_space *unwrap;
2135
31.5k
2136
31.5k
  if (!dim)
2137
0
    return NULL;
2138
31.5k
2139
31.5k
  
if (31.5k
!isl_space_is_wrapping(dim)31.5k
)
2140
0
    isl_die(dim->ctx, isl_error_invalid, "not a wrapping space",
2141
31.5k
      goto error);
2142
31.5k
2143
31.5k
  unwrap = isl_space_copy(dim->nested[1]);
2144
31.5k
  isl_space_free(dim);
2145
31.5k
2146
31.5k
  return unwrap;
2147
0
error:
2148
0
  isl_space_free(dim);
2149
0
  return NULL;
2150
31.5k
}
2151
2152
isl_bool isl_space_is_named_or_nested(__isl_keep isl_space *space,
2153
  enum isl_dim_type type)
2154
3.98M
{
2155
3.98M
  if (
type != isl_dim_in && 3.98M
type != isl_dim_out2.84M
)
2156
299k
    return isl_bool_false;
2157
3.68M
  
if (3.68M
!space3.68M
)
2158
6
    return isl_bool_error;
2159
3.68M
  
if (3.68M
space->tuple_id[type - isl_dim_in]3.68M
)
2160
1.62M
    return isl_bool_true;
2161
2.06M
  
if (2.06M
space->nested[type - isl_dim_in]2.06M
)
2162
478k
    return isl_bool_true;
2163
1.59M
  return isl_bool_false;
2164
2.06M
}
2165
2166
isl_bool isl_space_may_be_set(__isl_keep isl_space *space)
2167
271
{
2168
271
  isl_bool nested;
2169
271
2170
271
  if (!space)
2171
0
    return isl_bool_error;
2172
271
  
if (271
isl_space_is_set(space)271
)
2173
166
    return isl_bool_true;
2174
105
  
if (105
isl_space_dim(space, isl_dim_in) != 0105
)
2175
0
    return isl_bool_false;
2176
105
  nested = isl_space_is_named_or_nested(space, isl_dim_in);
2177
105
  if (
nested < 0 || 105
nested105
)
2178
0
    return isl_bool_not(nested);
2179
105
  return isl_bool_true;
2180
105
}
2181
2182
__isl_give isl_space *isl_space_reset(__isl_take isl_space *dim,
2183
  enum isl_dim_type type)
2184
3.63M
{
2185
3.63M
  if (!isl_space_is_named_or_nested(dim, type))
2186
1.69M
    return dim;
2187
3.63M
2188
1.93M
  dim = isl_space_cow(dim);
2189
1.93M
  if (!dim)
2190
13
    return NULL;
2191
1.93M
2192
1.93M
  isl_id_free(dim->tuple_id[type - isl_dim_in]);
2193
1.93M
  dim->tuple_id[type - isl_dim_in] = NULL;
2194
1.93M
  isl_space_free(dim->nested[type - isl_dim_in]);
2195
1.93M
  dim->nested[type - isl_dim_in] = NULL;
2196
1.93M
2197
1.93M
  return dim;
2198
1.93M
}
2199
2200
__isl_give isl_space *isl_space_flatten(__isl_take isl_space *dim)
2201
21.3k
{
2202
21.3k
  if (!dim)
2203
0
    return NULL;
2204
21.3k
  
if (21.3k
!dim->nested[0] && 21.3k
!dim->nested[1]21.3k
)
2205
0
    return dim;
2206
21.3k
2207
21.3k
  
if (21.3k
dim->nested[0]21.3k
)
2208
0
    dim = isl_space_reset(dim, isl_dim_in);
2209
21.3k
  if (
dim && 21.3k
dim->nested[1]21.3k
)
2210
21.3k
    dim = isl_space_reset(dim, isl_dim_out);
2211
21.3k
2212
21.3k
  return dim;
2213
21.3k
}
2214
2215
__isl_give isl_space *isl_space_flatten_domain(__isl_take isl_space *dim)
2216
30
{
2217
30
  if (!dim)
2218
0
    return NULL;
2219
30
  
if (30
!dim->nested[0]30
)
2220
0
    return dim;
2221
30
2222
30
  return isl_space_reset(dim, isl_dim_in);
2223
30
}
2224
2225
__isl_give isl_space *isl_space_flatten_range(__isl_take isl_space *dim)
2226
43.1k
{
2227
43.1k
  if (!dim)
2228
0
    return NULL;
2229
43.1k
  
if (43.1k
!dim->nested[1]43.1k
)
2230
0
    return dim;
2231
43.1k
2232
43.1k
  return isl_space_reset(dim, isl_dim_out);
2233
43.1k
}
2234
2235
/* Replace the dimensions of the given type of dst by those of src.
2236
 */
2237
__isl_give isl_space *isl_space_replace(__isl_take isl_space *dst,
2238
  enum isl_dim_type type, __isl_keep isl_space *src)
2239
114k
{
2240
114k
  dst = isl_space_cow(dst);
2241
114k
2242
114k
  if (
!dst || 114k
!src114k
)
2243
0
    goto error;
2244
114k
2245
114k
  dst = isl_space_drop_dims(dst, type, 0, isl_space_dim(dst, type));
2246
114k
  dst = isl_space_add_dims(dst, type, isl_space_dim(src, type));
2247
114k
  dst = copy_ids(dst, type, 0, src, type);
2248
114k
2249
114k
  if (
dst && 114k
type == isl_dim_param114k
)
{114k
2250
114k
    int i;
2251
343k
    for (i = 0; 
i <= 1343k
;
++i228k
)
{228k
2252
228k
      if (!dst->nested[i])
2253
219k
        continue;
2254
9.47k
      dst->nested[i] = isl_space_replace(dst->nested[i],
2255
9.47k
               type, src);
2256
9.47k
      if (!dst->nested[i])
2257
0
        goto error;
2258
9.47k
    }
2259
114k
  }
2260
114k
2261
114k
  return dst;
2262
0
error:
2263
0
  isl_space_free(dst);
2264
0
  return NULL;
2265
114k
}
2266
2267
/* Given a dimension specification "dim" of a set, create a dimension
2268
 * specification for the lift of the set.  In particular, the result
2269
 * is of the form [dim -> local[..]], with n_local variables in the
2270
 * range of the wrapped map.
2271
 */
2272
__isl_give isl_space *isl_space_lift(__isl_take isl_space *dim, unsigned n_local)
2273
19.8k
{
2274
19.8k
  isl_space *local_dim;
2275
19.8k
2276
19.8k
  if (!dim)
2277
0
    return NULL;
2278
19.8k
2279
19.8k
  local_dim = isl_space_dup(dim);
2280
19.8k
  local_dim = isl_space_drop_dims(local_dim, isl_dim_set, 0, dim->n_out);
2281
19.8k
  local_dim = isl_space_add_dims(local_dim, isl_dim_set, n_local);
2282
19.8k
  local_dim = isl_space_set_tuple_name(local_dim, isl_dim_set, "local");
2283
19.8k
  dim = isl_space_join(isl_space_from_domain(dim),
2284
19.8k
          isl_space_from_range(local_dim));
2285
19.8k
  dim = isl_space_wrap(dim);
2286
19.8k
  dim = isl_space_set_tuple_name(dim, isl_dim_set, "lifted");
2287
19.8k
2288
19.8k
  return dim;
2289
19.8k
}
2290
2291
isl_bool isl_space_can_zip(__isl_keep isl_space *space)
2292
4.93k
{
2293
4.93k
  isl_bool is_set;
2294
4.93k
2295
4.93k
  is_set = isl_space_is_set(space);
2296
4.93k
  if (is_set < 0)
2297
0
    return isl_bool_error;
2298
4.93k
  
if (4.93k
is_set4.93k
)
2299
0
    return isl_bool_false;
2300
4.93k
  return isl_space_is_product(space);
2301
4.93k
}
2302
2303
__isl_give isl_space *isl_space_zip(__isl_take isl_space *dim)
2304
2.14k
{
2305
2.14k
  isl_space *dom, *ran;
2306
2.14k
  isl_space *dom_dom, *dom_ran, *ran_dom, *ran_ran;
2307
2.14k
2308
2.14k
  if (!isl_space_can_zip(dim))
2309
0
    isl_die(dim->ctx, isl_error_invalid, "dim cannot be zipped",
2310
2.14k
      goto error);
2311
2.14k
2312
2.14k
  
if (2.14k
!dim2.14k
)
2313
0
    return NULL;
2314
2.14k
  dom = isl_space_unwrap(isl_space_domain(isl_space_copy(dim)));
2315
2.14k
  ran = isl_space_unwrap(isl_space_range(dim));
2316
2.14k
  dom_dom = isl_space_domain(isl_space_copy(dom));
2317
2.14k
  dom_ran = isl_space_range(dom);
2318
2.14k
  ran_dom = isl_space_domain(isl_space_copy(ran));
2319
2.14k
  ran_ran = isl_space_range(ran);
2320
2.14k
  dom = isl_space_join(isl_space_from_domain(dom_dom),
2321
2.14k
         isl_space_from_range(ran_dom));
2322
2.14k
  ran = isl_space_join(isl_space_from_domain(dom_ran),
2323
2.14k
         isl_space_from_range(ran_ran));
2324
2.14k
  return isl_space_join(isl_space_from_domain(isl_space_wrap(dom)),
2325
2.14k
          isl_space_from_range(isl_space_wrap(ran)));
2326
0
error:
2327
0
  isl_space_free(dim);
2328
0
  return NULL;
2329
2.14k
}
2330
2331
/* Can we apply isl_space_curry to "space"?
2332
 * That is, does it have a nested relation in its domain?
2333
 */
2334
isl_bool isl_space_can_curry(__isl_keep isl_space *space)
2335
32.7k
{
2336
32.7k
  if (!space)
2337
0
    return isl_bool_error;
2338
32.7k
2339
32.7k
  return !!space->nested[0];
2340
32.7k
}
2341
2342
/* Given a space (A -> B) -> C, return the corresponding space
2343
 * A -> (B -> C).
2344
 */
2345
__isl_give isl_space *isl_space_curry(__isl_take isl_space *space)
2346
10.8k
{
2347
10.8k
  isl_space *dom, *ran;
2348
10.8k
  isl_space *dom_dom, *dom_ran;
2349
10.8k
2350
10.8k
  if (!space)
2351
0
    return NULL;
2352
10.8k
2353
10.8k
  
if (10.8k
!isl_space_can_curry(space)10.8k
)
2354
0
    isl_die(space->ctx, isl_error_invalid,
2355
10.8k
      "space cannot be curried", goto error);
2356
10.8k
2357
10.8k
  dom = isl_space_unwrap(isl_space_domain(isl_space_copy(space)));
2358
10.8k
  ran = isl_space_range(space);
2359
10.8k
  dom_dom = isl_space_domain(isl_space_copy(dom));
2360
10.8k
  dom_ran = isl_space_range(dom);
2361
10.8k
  ran = isl_space_join(isl_space_from_domain(dom_ran),
2362
10.8k
         isl_space_from_range(ran));
2363
10.8k
  return isl_space_join(isl_space_from_domain(dom_dom),
2364
10.8k
          isl_space_from_range(isl_space_wrap(ran)));
2365
0
error:
2366
0
  isl_space_free(space);
2367
0
  return NULL;
2368
10.8k
}
2369
2370
/* Can isl_space_range_curry be applied to "space"?
2371
 * That is, does it have a nested relation in its range,
2372
 * the domain of which is itself a nested relation?
2373
 */
2374
isl_bool isl_space_can_range_curry(__isl_keep isl_space *space)
2375
21.6k
{
2376
21.6k
  isl_bool can;
2377
21.6k
2378
21.6k
  if (!space)
2379
0
    return isl_bool_error;
2380
21.6k
  can = isl_space_range_is_wrapping(space);
2381
21.6k
  if (
can < 0 || 21.6k
!can21.6k
)
2382
0
    return can;
2383
21.6k
  return isl_space_can_curry(space->nested[1]);
2384
21.6k
}
2385
2386
/* Given a space A -> ((B -> C) -> D), return the corresponding space
2387
 * A -> (B -> (C -> D)).
2388
 */
2389
__isl_give isl_space *isl_space_range_curry(__isl_take isl_space *space)
2390
10.7k
{
2391
10.7k
  if (!space)
2392
0
    return NULL;
2393
10.7k
2394
10.7k
  
if (10.7k
!isl_space_can_range_curry(space)10.7k
)
2395
0
    isl_die(isl_space_get_ctx(space), isl_error_invalid,
2396
10.7k
      "space range cannot be curried",
2397
10.7k
      return isl_space_free(space));
2398
10.7k
2399
10.7k
  space = isl_space_cow(space);
2400
10.7k
  if (!space)
2401
0
    return NULL;
2402
10.7k
  space->nested[1] = isl_space_curry(space->nested[1]);
2403
10.7k
  if (!space->nested[1])
2404
0
    return isl_space_free(space);
2405
10.7k
2406
10.7k
  return space;
2407
10.7k
}
2408
2409
/* Can we apply isl_space_uncurry to "space"?
2410
 * That is, does it have a nested relation in its range?
2411
 */
2412
isl_bool isl_space_can_uncurry(__isl_keep isl_space *space)
2413
492
{
2414
492
  if (!space)
2415
0
    return isl_bool_error;
2416
492
2417
492
  return !!space->nested[1];
2418
492
}
2419
2420
/* Given a space A -> (B -> C), return the corresponding space
2421
 * (A -> B) -> C.
2422
 */
2423
__isl_give isl_space *isl_space_uncurry(__isl_take isl_space *space)
2424
174
{
2425
174
  isl_space *dom, *ran;
2426
174
  isl_space *ran_dom, *ran_ran;
2427
174
2428
174
  if (!space)
2429
0
    return NULL;
2430
174
2431
174
  
if (174
!isl_space_can_uncurry(space)174
)
2432
0
    isl_die(space->ctx, isl_error_invalid,
2433
174
      "space cannot be uncurried",
2434
174
      return isl_space_free(space));
2435
174
2436
174
  dom = isl_space_domain(isl_space_copy(space));
2437
174
  ran = isl_space_unwrap(isl_space_range(space));
2438
174
  ran_dom = isl_space_domain(isl_space_copy(ran));
2439
174
  ran_ran = isl_space_range(ran);
2440
174
  dom = isl_space_join(isl_space_from_domain(dom),
2441
174
         isl_space_from_range(ran_dom));
2442
174
  return isl_space_join(isl_space_from_domain(isl_space_wrap(dom)),
2443
174
          isl_space_from_range(ran_ran));
2444
174
}
2445
2446
isl_bool isl_space_has_named_params(__isl_keep isl_space *space)
2447
371k
{
2448
371k
  int i;
2449
371k
  unsigned off;
2450
371k
2451
371k
  if (!space)
2452
0
    return isl_bool_error;
2453
371k
  
if (371k
space->nparam == 0371k
)
2454
148k
    return isl_bool_true;
2455
223k
  off = isl_space_offset(space, isl_dim_param);
2456
223k
  if (off + space->nparam > space->n_id)
2457
0
    return isl_bool_false;
2458
853k
  
for (i = 0; 223k
i < space->nparam853k
;
++i630k
)
2459
630k
    
if (630k
!space->ids[off + i]630k
)
2460
0
      return isl_bool_false;
2461
223k
  return isl_bool_true;
2462
223k
}
2463
2464
/* Check that "space" has only named parameters, reporting an error
2465
 * if it does not.
2466
 */
2467
isl_stat isl_space_check_named_params(__isl_keep isl_space *space)
2468
152k
{
2469
152k
  isl_bool named;
2470
152k
2471
152k
  named = isl_space_has_named_params(space);
2472
152k
  if (named < 0)
2473
0
    return isl_stat_error;
2474
152k
  
if (152k
!named152k
)
2475
0
    isl_die(isl_space_get_ctx(space), isl_error_invalid,
2476
152k
      "unaligned unnamed parameters", return isl_stat_error);
2477
152k
2478
152k
  return isl_stat_ok;
2479
152k
}
2480
2481
/* Align the initial parameters of dim1 to match the order in dim2.
2482
 */
2483
__isl_give isl_space *isl_space_align_params(__isl_take isl_space *dim1,
2484
  __isl_take isl_space *dim2)
2485
20.3k
{
2486
20.3k
  isl_reordering *exp;
2487
20.3k
2488
20.3k
  if (
!isl_space_has_named_params(dim1) || 20.3k
!isl_space_has_named_params(dim2)20.3k
)
2489
0
    isl_die(isl_space_get_ctx(dim1), isl_error_invalid,
2490
20.3k
      "parameter alignment requires named parameters",
2491
20.3k
      goto error);
2492
20.3k
2493
20.3k
  dim2 = isl_space_params(dim2);
2494
20.3k
  exp = isl_parameter_alignment_reordering(dim1, dim2);
2495
20.3k
  exp = isl_reordering_extend_space(exp, dim1);
2496
20.3k
  isl_space_free(dim2);
2497
20.3k
  if (!exp)
2498
0
    return NULL;
2499
20.3k
  dim1 = isl_space_copy(exp->dim);
2500
20.3k
  isl_reordering_free(exp);
2501
20.3k
  return dim1;
2502
0
error:
2503
0
  isl_space_free(dim1);
2504
0
  isl_space_free(dim2);
2505
0
  return NULL;
2506
20.3k
}
2507
2508
/* Given the space of set (domain), construct a space for a map
2509
 * with as domain the given space and as range the range of "model".
2510
 */
2511
__isl_give isl_space *isl_space_extend_domain_with_range(
2512
  __isl_take isl_space *space, __isl_take isl_space *model)
2513
39.5k
{
2514
39.5k
  if (!model)
2515
0
    goto error;
2516
39.5k
2517
39.5k
  space = isl_space_from_domain(space);
2518
39.5k
  space = isl_space_add_dims(space, isl_dim_out,
2519
39.5k
            isl_space_dim(model, isl_dim_out));
2520
39.5k
  if (isl_space_has_tuple_id(model, isl_dim_out))
2521
8
    space = isl_space_set_tuple_id(space, isl_dim_out,
2522
8
        isl_space_get_tuple_id(model, isl_dim_out));
2523
39.5k
  if (!space)
2524
0
    goto error;
2525
39.5k
  
if (39.5k
model->nested[1]39.5k
)
{1
2526
1
    isl_space *nested = isl_space_copy(model->nested[1]);
2527
1
    int n_nested, n_space;
2528
1
    nested = isl_space_align_params(nested, isl_space_copy(space));
2529
1
    n_nested = isl_space_dim(nested, isl_dim_param);
2530
1
    n_space = isl_space_dim(space, isl_dim_param);
2531
1
    if (n_nested > n_space)
2532
0
      nested = isl_space_drop_dims(nested, isl_dim_param,
2533
0
            n_space, n_nested - n_space);
2534
1
    if (!nested)
2535
0
      goto error;
2536
1
    space->nested[1] = nested;
2537
1
  }
2538
39.5k
  isl_space_free(model);
2539
39.5k
  return space;
2540
0
error:
2541
0
  isl_space_free(model);
2542
0
  isl_space_free(space);
2543
0
  return NULL;
2544
39.5k
}
2545
2546
/* Compare the "type" dimensions of two isl_spaces.
2547
 *
2548
 * The order is fairly arbitrary.
2549
 */
2550
static int isl_space_cmp_type(__isl_keep isl_space *space1,
2551
  __isl_keep isl_space *space2, enum isl_dim_type type)
2552
1.26M
{
2553
1.26M
  int cmp;
2554
1.26M
  isl_space *nested1, *nested2;
2555
1.26M
2556
1.26M
  if (isl_space_dim(space1, type) != isl_space_dim(space2, type))
2557
0
    return isl_space_dim(space1, type) -
2558
0
      isl_space_dim(space2, type);
2559
1.26M
2560
1.26M
  cmp = isl_id_cmp(tuple_id(space1, type), tuple_id(space2, type));
2561
1.26M
  if (cmp != 0)
2562
0
    return cmp;
2563
1.26M
2564
1.26M
  nested1 = nested(space1, type);
2565
1.26M
  nested2 = nested(space2, type);
2566
1.26M
  if (!nested1 != !nested2)
2567
0
    return !nested1 - !nested2;
2568
1.26M
2569
1.26M
  
if (1.26M
nested11.26M
)
2570
63.1k
    return isl_space_cmp(nested1, nested2);
2571
1.26M
2572
1.19M
  return 0;
2573
1.26M
}
2574
2575
/* Compare two isl_spaces.
2576
 *
2577
 * The order is fairly arbitrary.
2578
 */
2579
int isl_space_cmp(__isl_keep isl_space *space1, __isl_keep isl_space *space2)
2580
1.50M
{
2581
1.50M
  int i;
2582
1.50M
  int cmp;
2583
1.50M
2584
1.50M
  if (space1 == space2)
2585
1.08M
    return 0;
2586
420k
  
if (420k
!space1420k
)
2587
0
    return -1;
2588
420k
  
if (420k
!space2420k
)
2589
0
    return 1;
2590
420k
2591
420k
  cmp = isl_space_cmp_type(space1, space2, isl_dim_param);
2592
420k
  if (cmp != 0)
2593
0
    return cmp;
2594
420k
  cmp = isl_space_cmp_type(space1, space2, isl_dim_in);
2595
420k
  if (cmp != 0)
2596
0
    return cmp;
2597
420k
  cmp = isl_space_cmp_type(space1, space2, isl_dim_out);
2598
420k
  if (cmp != 0)
2599
0
    return cmp;
2600
420k
2601
420k
  
if (420k
!space1->ids && 420k
!space2->ids35.0k
)
2602
34.8k
    return 0;
2603
420k
2604
3.30M
  
for (i = 0; 385k
i < n(space1, isl_dim_param)3.30M
;
++i2.92M
)
{2.92M
2605
2.92M
    cmp = isl_id_cmp(get_id(space1, isl_dim_param, i),
2606
2.92M
         get_id(space2, isl_dim_param, i));
2607
2.92M
    if (cmp != 0)
2608
0
      return cmp;
2609
2.92M
  }
2610
385k
2611
385k
  return 0;
2612
385k
}