Coverage Report

Created: 2017-10-03 07:32

/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.26M
{
23
1.26M
  return dim ? dim->ctx : NULL;
24
1.26M
}
25
26
__isl_give isl_space *isl_space_alloc(isl_ctx *ctx,
27
      unsigned nparam, unsigned n_in, unsigned n_out)
28
4.75M
{
29
4.75M
  isl_space *dim;
30
4.75M
31
4.75M
  dim = isl_alloc_type(ctx, struct isl_space);
32
4.75M
  if (!dim)
33
4
    return NULL;
34
4.75M
35
4.75M
  dim->ctx = ctx;
36
4.75M
  isl_ctx_ref(ctx);
37
4.75M
  dim->ref = 1;
38
4.75M
  dim->nparam = nparam;
39
4.75M
  dim->n_in = n_in;
40
4.75M
  dim->n_out = n_out;
41
4.75M
42
4.75M
  dim->tuple_id[0] = NULL;
43
4.75M
  dim->tuple_id[1] = NULL;
44
4.75M
45
4.75M
  dim->nested[0] = NULL;
46
4.75M
  dim->nested[1] = NULL;
47
4.75M
48
4.75M
  dim->n_id = 0;
49
4.75M
  dim->ids = NULL;
50
4.75M
51
4.75M
  return dim;
52
4.75M
}
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.53M
{
59
1.53M
  space = isl_space_cow(space);
60
1.53M
  if (!space)
61
0
    return NULL;
62
1.53M
  space = isl_space_set_tuple_id(space, isl_dim_in, &isl_id_none);
63
1.53M
  return space;
64
1.53M
}
65
66
/* Is the space that of a set?
67
 */
68
isl_bool isl_space_is_set(__isl_keep isl_space *space)
69
1.46M
{
70
1.46M
  if (!space)
71
0
    return isl_bool_error;
72
1.46M
  
if (1.46M
space->n_in != 0 || 1.46M
space->nested[0]1.18M
)
73
291k
    return isl_bool_false;
74
1.17M
  
if (1.17M
space->tuple_id[0] != &isl_id_none1.17M
)
75
15.9k
    return isl_bool_false;
76
1.15M
  return isl_bool_true;
77
1.15M
}
78
79
/* Is the given space that of a map?
80
 */
81
isl_bool isl_space_is_map(__isl_keep isl_space *space)
82
29.0k
{
83
29.0k
  if (!space)
84
0
    return isl_bool_error;
85
29.0k
  return space->tuple_id[0] != &isl_id_none &&
86
774
    space->tuple_id[1] != &isl_id_none;
87
29.0k
}
88
89
__isl_give isl_space *isl_space_set_alloc(isl_ctx *ctx,
90
      unsigned nparam, unsigned dim)
91
532k
{
92
532k
  isl_space *space;
93
532k
  space = isl_space_alloc(ctx, nparam, 0, dim);
94
532k
  space = mark_as_set(space);
95
532k
  return space;
96
532k
}
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
126k
{
103
126k
  if (!space)
104
0
    return NULL;
105
126k
  space = isl_space_set_tuple_id(space, isl_dim_in, &isl_id_none);
106
126k
  space = isl_space_set_tuple_id(space, isl_dim_out, &isl_id_none);
107
126k
  return space;
108
126k
}
109
110
/* Is the space that of a parameter domain?
111
 */
112
isl_bool isl_space_is_params(__isl_keep isl_space *space)
113
618k
{
114
618k
  if (!space)
115
14
    return isl_bool_error;
116
618k
  
if (618k
space->n_in != 0 || 618k
space->nested[0]501k
||
117
618k
      
space->n_out != 0500k
||
space->nested[1]386k
)
118
232k
    return isl_bool_false;
119
385k
  
if (385k
space->tuple_id[0] != &isl_id_none385k
)
120
21.1k
    return isl_bool_false;
121
364k
  
if (364k
space->tuple_id[1] != &isl_id_none364k
)
122
24.5k
    return isl_bool_false;
123
339k
  return isl_bool_true;
124
339k
}
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
24.9k
{
130
24.9k
  isl_space *space;
131
24.9k
  space = isl_space_alloc(ctx, nparam, 0, 0);
132
24.9k
  space = mark_as_params(space);
133
24.9k
  return space;
134
24.9k
}
135
136
static unsigned global_pos(__isl_keep isl_space *dim,
137
         enum isl_dim_type type, unsigned pos)
138
73.7M
{
139
73.7M
  struct isl_ctx *ctx = dim->ctx;
140
73.7M
141
73.7M
  switch (type) {
142
32.6M
  case isl_dim_param:
143
32.6M
    isl_assert(ctx, pos < dim->nparam,
144
32.6M
          return isl_space_dim(dim, isl_dim_all));
145
32.6M
    return pos;
146
16.2M
  case isl_dim_in:
147
16.2M
    isl_assert(ctx, pos < dim->n_in,
148
16.2M
          return isl_space_dim(dim, isl_dim_all));
149
16.2M
    return pos + dim->nparam;
150
24.8M
  case isl_dim_out:
151
24.8M
    isl_assert(ctx, pos < dim->n_out,
152
24.8M
          return isl_space_dim(dim, isl_dim_all));
153
24.8M
    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
73.7M
  }
157
0
  return isl_space_dim(dim, isl_dim_all);
158
73.7M
}
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
3.78M
{
164
3.78M
  isl_id **ids;
165
3.78M
  int i;
166
3.78M
167
3.78M
  if (isl_space_dim(dim, isl_dim_all) <= dim->n_id)
168
1.08M
    return dim;
169
2.69M
170
2.69M
  
if (2.69M
!dim->ids2.69M
) {
171
2.69M
    dim->ids = isl_calloc_array(dim->ctx,
172
2.69M
        isl_id *, isl_space_dim(dim, isl_dim_all));
173
2.69M
    if (!dim->ids)
174
0
      goto error;
175
619
  } else {
176
619
    ids = isl_realloc_array(dim->ctx, dim->ids,
177
619
        isl_id *, isl_space_dim(dim, isl_dim_all));
178
619
    if (!ids)
179
0
      goto error;
180
619
    dim->ids = ids;
181
3.32k
    for (i = dim->n_id; 
i < isl_space_dim(dim, isl_dim_all)3.32k
;
++i2.70k
)
182
2.70k
      dim->ids[i] = NULL;
183
619
  }
184
2.69M
185
2.69M
  dim->n_id = isl_space_dim(dim, isl_dim_all);
186
2.69M
187
2.69M
  return dim;
188
0
error:
189
0
  isl_space_free(dim);
190
0
  return NULL;
191
3.78M
}
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
17.4M
{
196
17.4M
  dim = isl_space_cow(dim);
197
17.4M
198
17.4M
  if (!dim)
199
0
    goto error;
200
17.4M
201
17.4M
  pos = global_pos(dim, type, pos);
202
17.4M
  if (pos == isl_space_dim(dim, isl_dim_all))
203
0
    goto error;
204
17.4M
205
17.4M
  
if (17.4M
pos >= dim->n_id17.4M
) {
206
2.69M
    if (!id)
207
0
      return dim;
208
2.69M
    dim = extend_ids(dim);
209
2.69M
    if (!dim)
210
0
      goto error;
211
17.4M
  }
212
17.4M
213
17.4M
  dim->ids[pos] = id;
214
17.4M
215
17.4M
  return dim;
216
0
error:
217
0
  isl_id_free(id);
218
0
  isl_space_free(dim);
219
0
  return NULL;
220
17.4M
}
221
222
static __isl_keep isl_id *get_id(__isl_keep isl_space *dim,
223
         enum isl_dim_type type, unsigned pos)
224
56.3M
{
225
56.3M
  if (!dim)
226
0
    return NULL;
227
56.3M
228
56.3M
  pos = global_pos(dim, type, pos);
229
56.3M
  if (pos == isl_space_dim(dim, isl_dim_all))
230
0
    return NULL;
231
56.3M
  
if (56.3M
pos >= dim->n_id56.3M
)
232
2.26M
    return NULL;
233
54.0M
  return dim->ids[pos];
234
54.0M
}
235
236
static unsigned offset(__isl_keep isl_space *dim, enum isl_dim_type type)
237
2.34M
{
238
2.34M
  switch (type) {
239
668k
  case isl_dim_param: return 0;
240
525k
  case isl_dim_in:  return dim->nparam;
241
1.14M
  case isl_dim_out: return dim->nparam + dim->n_in;
242
0
  default:    return 0;
243
0
  }
244
0
}
245
246
static unsigned n(__isl_keep isl_space *dim, enum isl_dim_type type)
247
318M
{
248
318M
  switch (type) {
249
57.7M
  case isl_dim_param: return dim->nparam;
250
38.5M
  case isl_dim_in:  return dim->n_in;
251
45.8M
  case isl_dim_out: return dim->n_out;
252
176M
  case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
253
0
  default:    return 0;
254
0
  }
255
0
}
256
257
unsigned isl_space_dim(__isl_keep isl_space *dim, enum isl_dim_type type)
258
228M
{
259
228M
  if (!dim)
260
3
    return 0;
261
228M
  return n(dim, type);
262
228M
}
263
264
unsigned isl_space_offset(__isl_keep isl_space *dim, enum isl_dim_type type)
265
1.89M
{
266
1.89M
  if (!dim)
267
0
    return 0;
268
1.89M
  return offset(dim, type);
269
1.89M
}
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
9.57M
{
275
9.57M
  int i;
276
9.57M
  isl_id *id;
277
9.57M
278
9.57M
  if (!dst)
279
0
    return NULL;
280
9.57M
281
27.8M
  
for (i = 0; 9.57M
i < n(src, src_type)27.8M
;
++i18.3M
) {
282
18.3M
    id = get_id(src, src_type, i);
283
18.3M
    if (!id)
284
5.61M
      continue;
285
12.7M
    dst = set_id(dst, dst_type, offset + i, isl_id_copy(id));
286
12.7M
    if (!dst)
287
0
      return NULL;
288
18.3M
  }
289
9.57M
  return dst;
290
9.57M
}
291
292
__isl_take isl_space *isl_space_dup(__isl_keep isl_space *dim)
293
3.52M
{
294
3.52M
  isl_space *dup;
295
3.52M
  if (!dim)
296
0
    return NULL;
297
3.52M
  dup = isl_space_alloc(dim->ctx, dim->nparam, dim->n_in, dim->n_out);
298
3.52M
  if (!dup)
299
4
    return NULL;
300
3.52M
  
if (3.52M
dim->tuple_id[0] &&
301
1.95M
      !(dup->tuple_id[0] = isl_id_copy(dim->tuple_id[0])))
302
0
    goto error;
303
3.52M
  
if (3.52M
dim->tuple_id[1] &&
304
831k
      !(dup->tuple_id[1] = isl_id_copy(dim->tuple_id[1])))
305
0
    goto error;
306
3.52M
  
if (3.52M
dim->nested[0] && 3.52M
!(dup->nested[0] = isl_space_copy(dim->nested[0]))684k
)
307
0
    goto error;
308
3.52M
  
if (3.52M
dim->nested[1] && 3.52M
!(dup->nested[1] = isl_space_copy(dim->nested[1]))859k
)
309
0
    goto error;
310
3.52M
  
if (3.52M
!dim->ids3.52M
)
311
1.09M
    return dup;
312
2.43M
  dup = copy_ids(dup, isl_dim_param, 0, dim, isl_dim_param);
313
2.43M
  dup = copy_ids(dup, isl_dim_in, 0, dim, isl_dim_in);
314
2.43M
  dup = copy_ids(dup, isl_dim_out, 0, dim, isl_dim_out);
315
2.43M
  return dup;
316
0
error:
317
0
  isl_space_free(dup);
318
0
  return NULL;
319
3.52M
}
320
321
__isl_give isl_space *isl_space_cow(__isl_take isl_space *dim)
322
28.5M
{
323
28.5M
  if (!dim)
324
5
    return NULL;
325
28.5M
326
28.5M
  
if (28.5M
dim->ref == 128.5M
)
327
25.0M
    return dim;
328
3.50M
  dim->ref--;
329
3.50M
  return isl_space_dup(dim);
330
3.50M
}
331
332
__isl_give isl_space *isl_space_copy(__isl_keep isl_space *dim)
333
15.8M
{
334
15.8M
  if (!dim)
335
8.45k
    return NULL;
336
15.8M
337
15.8M
  dim->ref++;
338
15.8M
  return dim;
339
15.8M
}
340
341
__isl_null isl_space *isl_space_free(__isl_take isl_space *space)
342
27.9M
{
343
27.9M
  int i;
344
27.9M
345
27.9M
  if (!space)
346
10.8M
    return NULL;
347
17.0M
348
17.0M
  
if (17.0M
--space->ref > 017.0M
)
349
12.3M
    return NULL;
350
4.75M
351
4.75M
  isl_id_free(space->tuple_id[0]);
352
4.75M
  isl_id_free(space->tuple_id[1]);
353
4.75M
354
4.75M
  isl_space_free(space->nested[0]);
355
4.75M
  isl_space_free(space->nested[1]);
356
4.75M
357
16.0M
  for (i = 0; 
i < space->n_id16.0M
;
++i11.2M
)
358
11.2M
    isl_id_free(space->ids[i]);
359
27.9M
  free(space->ids);
360
27.9M
  isl_ctx_deref(space->ctx);
361
27.9M
  
362
27.9M
  free(space);
363
27.9M
364
27.9M
  return NULL;
365
27.9M
}
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
53.0k
{
374
53.0k
  char *p;
375
53.0k
  long dummy;
376
53.0k
377
53.0k
  dummy = strtol(s, &p, 0);
378
53.0k
  if (p != s)
379
0
    isl_die(ctx, isl_error_invalid, "name looks like a number",
380
53.0k
      return 0);
381
53.0k
382
53.0k
  return 1;
383
53.0k
}
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
110k
{
390
110k
  if (!space)
391
0
    return 0;
392
110k
  
if (110k
isl_space_is_params(space)110k
)
393
0
    isl_die(space->ctx, isl_error_invalid,
394
110k
      "parameter spaces don't have tuple ids", return 0);
395
110k
  
if (110k
isl_space_is_set(space) && 110k
type != isl_dim_set25.2k
)
396
0
    isl_die(space->ctx, isl_error_invalid,
397
110k
      "set spaces can only have a set id", return 0);
398
110k
  
if (110k
type != isl_dim_in && 110k
type != isl_dim_out109k
)
399
0
    isl_die(space->ctx, isl_error_invalid,
400
110k
      "only input, output and set tuples can have ids",
401
110k
      return 0);
402
110k
403
110k
  return 1;
404
110k
}
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
109k
{
411
109k
  if (!space_can_have_id(dim, type))
412
0
    return isl_bool_error;
413
109k
  return dim->tuple_id[type - isl_dim_in] != NULL;
414
109k
}
415
416
__isl_give isl_id *isl_space_get_tuple_id(__isl_keep isl_space *dim,
417
  enum isl_dim_type type)
418
36.3k
{
419
36.3k
  int has_id;
420
36.3k
421
36.3k
  if (!dim)
422
0
    return NULL;
423
36.3k
  has_id = isl_space_has_tuple_id(dim, type);
424
36.3k
  if (has_id < 0)
425
0
    return NULL;
426
36.3k
  
if (36.3k
!has_id36.3k
)
427
0
    isl_die(dim->ctx, isl_error_invalid,
428
36.3k
      "tuple has no id", return NULL);
429
36.3k
  return isl_id_copy(dim->tuple_id[type - isl_dim_in]);
430
36.3k
}
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.84M
{
435
1.84M
  dim = isl_space_cow(dim);
436
1.84M
  if (
!dim || 1.84M
!id1.84M
)
437
0
    goto error;
438
1.84M
  
if (1.84M
type != isl_dim_in && 1.84M
type != isl_dim_out187k
)
439
0
    isl_die(dim->ctx, isl_error_invalid,
440
1.84M
      "only input, output and set tuples can have names",
441
1.84M
      goto error);
442
1.84M
443
1.84M
  isl_id_free(dim->tuple_id[type - isl_dim_in]);
444
1.84M
  dim->tuple_id[type - isl_dim_in] = id;
445
1.84M
446
1.84M
  return dim;
447
0
error:
448
0
  isl_id_free(id);
449
0
  isl_space_free(dim);
450
0
  return NULL;
451
1.84M
}
452
453
__isl_give isl_space *isl_space_reset_tuple_id(__isl_take isl_space *dim,
454
  enum isl_dim_type type)
455
5.86k
{
456
5.86k
  dim = isl_space_cow(dim);
457
5.86k
  if (!dim)
458
0
    return NULL;
459
5.86k
  
if (5.86k
type != isl_dim_in && 5.86k
type != isl_dim_out5.86k
)
460
0
    isl_die(dim->ctx, isl_error_invalid,
461
5.86k
      "only input, output and set tuples can have names",
462
5.86k
      goto error);
463
5.86k
464
5.86k
  isl_id_free(dim->tuple_id[type - isl_dim_in]);
465
5.86k
  dim->tuple_id[type - isl_dim_in] = NULL;
466
5.86k
467
5.86k
  return dim;
468
0
error:
469
0
  isl_space_free(dim);
470
0
  return NULL;
471
5.86k
}
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
85.8k
{
481
85.8k
  space = isl_space_cow(space);
482
85.8k
  if (
!space || 85.8k
!id85.8k
)
483
0
    goto error;
484
85.8k
485
85.8k
  
if (85.8k
type == isl_dim_param85.8k
) {
486
60.1k
    int i;
487
60.1k
488
180k
    for (i = 0; 
i < 2180k
;
++i120k
) {
489
120k
      if (!space->nested[i])
490
120k
        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
120k
    }
497
60.1k
  }
498
85.8k
499
85.8k
  isl_id_free(get_id(space, type, pos));
500
85.8k
  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
85.8k
}
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
) {
520
0
    int i;
521
0
522
0
    for (i = 0; 
i < 20
;
++i0
) {
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.11k
{
543
1.11k
  if (!dim)
544
0
    return isl_bool_error;
545
1.11k
  return get_id(dim, type, pos) != NULL;
546
1.11k
}
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
789k
{
551
789k
  if (!dim)
552
0
    return NULL;
553
789k
  
if (789k
!get_id(dim, type, pos)789k
)
554
0
    isl_die(dim->ctx, isl_error_invalid,
555
789k
      "dim has no id", return NULL);
556
789k
  return isl_id_copy(get_id(dim, type, pos));
557
789k
}
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
47.6k
{
562
47.6k
  isl_id *id;
563
47.6k
564
47.6k
  if (!dim)
565
0
    return NULL;
566
47.6k
567
47.6k
  
if (47.6k
!s47.6k
)
568
0
    return isl_space_reset_tuple_id(dim, type);
569
47.6k
570
47.6k
  
if (47.6k
!name_ok(dim->ctx, s)47.6k
)
571
0
    goto error;
572
47.6k
573
47.6k
  id = isl_id_alloc(dim->ctx, s, NULL);
574
47.6k
  return isl_space_set_tuple_id(dim, type, id);
575
0
error:
576
0
  isl_space_free(dim);
577
0
  return NULL;
578
47.6k
}
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
831
{
585
831
  isl_id *id;
586
831
587
831
  if (!space_can_have_id(space, type))
588
0
    return isl_bool_error;
589
831
  id = space->tuple_id[type - isl_dim_in];
590
178
  return id && id->name;
591
831
}
592
593
__isl_keep const char *isl_space_get_tuple_name(__isl_keep isl_space *dim,
594
   enum isl_dim_type type)
595
10.0k
{
596
10.0k
  isl_id *id;
597
10.0k
  if (!dim)
598
0
    return NULL;
599
10.0k
  
if (10.0k
type != isl_dim_in && 10.0k
type != isl_dim_out6.00k
)
600
0
    return NULL;
601
10.0k
  id = dim->tuple_id[type - isl_dim_in];
602
8.67k
  return id ? id->name : NULL;
603
10.0k
}
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
5.39k
{
609
5.39k
  isl_id *id;
610
5.39k
611
5.39k
  if (!dim)
612
0
    return NULL;
613
5.39k
  
if (5.39k
!s5.39k
)
614
0
    return isl_space_reset_dim_id(dim, type, pos);
615
5.39k
  
if (5.39k
!name_ok(dim->ctx, s)5.39k
)
616
0
    goto error;
617
5.39k
  id = isl_id_alloc(dim->ctx, s, NULL);
618
5.39k
  return isl_space_set_dim_id(dim, type, pos, id);
619
0
error:
620
0
  isl_space_free(dim);
621
0
  return NULL;
622
5.39k
}
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
6.07k
{
629
6.07k
  isl_id *id;
630
6.07k
631
6.07k
  if (!space)
632
0
    return isl_bool_error;
633
6.07k
  id = get_id(space, type, pos);
634
1.16k
  return id && id->name;
635
6.07k
}
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
310k
{
640
310k
  isl_id *id = get_id(dim, type, pos);
641
276k
  return id ? id->name : NULL;
642
310k
}
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
381
{
647
381
  int i;
648
381
  int offset;
649
381
  int n;
650
381
651
381
  if (
!dim || 381
!id381
)
652
0
    return -1;
653
381
654
381
  offset = isl_space_offset(dim, type);
655
381
  n = isl_space_dim(dim, type);
656
502
  for (i = 0; 
i < n && 502
offset + i < dim->n_id501
;
++i121
)
657
501
    
if (501
dim->ids[offset + i] == id501
)
658
380
      return i;
659
381
660
1
  return -1;
661
381
}
662
663
int isl_space_find_dim_by_name(__isl_keep isl_space *space,
664
  enum isl_dim_type type, const char *name)
665
866
{
666
866
  int i;
667
866
  int offset;
668
866
  int n;
669
866
670
866
  if (
!space || 866
!name866
)
671
0
    return -1;
672
866
673
866
  offset = isl_space_offset(space, type);
674
866
  n = isl_space_dim(space, type);
675
1.37k
  for (i = 0; 
i < n && 1.37k
offset + i < space->n_id513
;
++i513
) {
676
513
    isl_id *id = get_id(space, type, i);
677
513
    if (
id && 513
id->name513
&&
!strcmp(id->name, name)513
)
678
0
      return i;
679
513
  }
680
866
681
866
  return -1;
682
866
}
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
) {
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
) {
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
) {
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
42.1M
{
746
42.1M
  if (!dim)
747
0
    return NULL;
748
42.1M
  
if (42.1M
type == isl_dim_in42.1M
)
749
13.1M
    return dim->tuple_id[0];
750
28.9M
  
if (28.9M
type == isl_dim_out28.9M
)
751
12.0M
    return dim->tuple_id[1];
752
16.8M
  return NULL;
753
16.8M
}
754
755
static __isl_keep isl_space *nested(__isl_keep isl_space *dim,
756
  enum isl_dim_type type)
757
39.2M
{
758
39.2M
  if (!dim)
759
0
    return NULL;
760
39.2M
  
if (39.2M
type == isl_dim_in39.2M
)
761
11.3M
    return dim->nested[0];
762
27.8M
  
if (27.8M
type == isl_dim_out27.8M
)
763
11.0M
    return dim->nested[1];
764
16.8M
  return NULL;
765
16.8M
}
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
6.73M
{
772
6.73M
  if (
!space1 || 6.73M
!space26.73M
)
773
0
    return isl_bool_error;
774
6.73M
  
if (6.73M
space1 == space26.73M
)
775
2.19M
    return isl_bool_true;
776
4.53M
  return isl_space_tuple_is_equal(space1, isl_dim_in,
777
4.53M
          space2, isl_dim_in) &&
778
3.92M
         isl_space_tuple_is_equal(space1, isl_dim_out,
779
3.92M
          space2, isl_dim_out);
780
6.73M
}
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
20.5M
{
798
20.5M
  isl_id *id1, *id2;
799
20.5M
  isl_space *nested1, *nested2;
800
20.5M
801
20.5M
  if (
!space1 || 20.5M
!space220.5M
)
802
0
    return isl_bool_error;
803
20.5M
804
20.5M
  
if (20.5M
space1 == space2 && 20.5M
type1 == type21.74M
)
805
7.07k
    return isl_bool_true;
806
20.5M
807
20.5M
  
if (20.5M
n(space1, type1) != n(space2, type2)20.5M
)
808
1.90M
    return isl_bool_false;
809
18.6M
  id1 = tuple_id(space1, type1);
810
18.6M
  id2 = tuple_id(space2, type2);
811
18.6M
  if (!id1 ^ !id2)
812
578k
    return isl_bool_false;
813
18.0M
  
if (18.0M
id1 && 18.0M
id1 != id23.81M
)
814
138k
    return isl_bool_false;
815
17.9M
  nested1 = nested(space1, type1);
816
17.9M
  nested2 = nested(space2, type2);
817
17.9M
  if (!nested1 ^ !nested2)
818
61.7k
    return isl_bool_false;
819
17.8M
  
if (17.8M
nested1 && 17.8M
!isl_space_has_equal_tuples(nested1, nested2)3.53M
)
820
24.4k
    return isl_bool_false;
821
17.8M
  return isl_bool_true;
822
17.8M
}
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
10.8M
{
836
10.8M
  int i;
837
10.8M
838
10.8M
  if (
space1 == space2 && 10.8M
type1 == type22.61M
)
839
884k
    return isl_bool_true;
840
9.95M
841
9.95M
  
if (9.95M
!isl_space_tuple_is_equal(space1, type1, space2, type2)9.95M
)
842
1.80M
    return isl_bool_false;
843
8.15M
844
8.15M
  
if (8.15M
!space1->ids && 8.15M
!space2->ids1.60M
)
845
1.45M
    return isl_bool_true;
846
6.70M
847
14.7M
  
for (i = 0; 6.70M
i < n(space1, type1)14.7M
;
++i8.02M
) {
848
8.08M
    if (get_id(space1, type1, i) != get_id(space2, type2, i))
849
58.0k
      return isl_bool_false;
850
8.08M
  }
851
6.64M
  return isl_bool_true;
852
10.8M
}
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
8.70M
{
859
8.70M
  if (
!space1 || 8.70M
!space28.70M
)
860
0
    return isl_bool_error;
861
8.70M
862
8.70M
  return match(space1, isl_dim_param, space2, isl_dim_param);
863
8.70M
}
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
204k
{
871
204k
  isl_bool equal;
872
204k
873
204k
  if (
!space1 || 204k
!space2204k
)
874
0
    return isl_bool_error;
875
204k
876
204k
  equal = match(space1, isl_dim_in, space2, isl_dim_in);
877
204k
  if (
equal < 0 || 204k
!equal204k
)
878
137
    return equal;
879
204k
  return match(space1, isl_dim_out, space2, isl_dim_out);
880
204k
}
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
4.28M
{
894
4.28M
  int i;
895
4.28M
896
11.6M
  for (i = 0; 
i < n11.6M
;
++i7.40M
)
897
7.40M
    ids[i] = get_id(dim, type, first + i);
898
4.28M
}
899
900
static __isl_give isl_space *space_extend(__isl_take isl_space *space,
901
      unsigned nparam, unsigned n_in, unsigned n_out)
902
639k
{
903
639k
  isl_id **ids = NULL;
904
639k
905
639k
  if (!space)
906
0
    return NULL;
907
639k
  
if (639k
space->nparam == nparam &&
908
639k
      
space->n_in == n_in482k
&&
space->n_out == n_out476k
)
909
34.2k
    return space;
910
605k
911
605k
  
isl_assert605k
(space->ctx, space->nparam <= nparam, goto error);
912
605k
  
isl_assert605k
(space->ctx, space->n_in <= n_in, goto error);
913
605k
  
isl_assert605k
(space->ctx, space->n_out <= n_out, goto error);
914
605k
915
605k
  space = isl_space_cow(space);
916
605k
  if (!space)
917
0
    goto error;
918
605k
919
605k
  
if (605k
space->ids605k
) {
920
325k
    unsigned n;
921
325k
    n = nparam + n_in + n_out;
922
325k
    if (
n < nparam || 325k
n < n_in325k
||
n < n_out325k
)
923
0
      isl_die(isl_space_get_ctx(space), isl_error_invalid,
924
325k
        "overflow in total number of dimensions",
925
325k
        goto error);
926
325k
    
ids = 325k
isl_calloc_array325k
(space->ctx, isl_id *, n);
927
325k
    if (!ids)
928
0
      goto error;
929
325k
    get_ids(space, isl_dim_param, 0, space->nparam, ids);
930
325k
    get_ids(space, isl_dim_in, 0, space->n_in, ids + nparam);
931
325k
    get_ids(space, isl_dim_out, 0, space->n_out,
932
325k
      ids + nparam + n_in);
933
325k
    free(space->ids);
934
325k
    space->ids = ids;
935
325k
    space->n_id = nparam + n_in + n_out;
936
325k
  }
937
605k
  space->nparam = nparam;
938
605k
  space->n_in = n_in;
939
605k
  space->n_out = n_out;
940
605k
941
605k
  return space;
942
0
error:
943
0
  free(ids);
944
0
  isl_space_free(space);
945
0
  return NULL;
946
639k
}
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
639k
{
957
639k
  space = isl_space_reset(space, type);
958
639k
  if (!space)
959
5
    return NULL;
960
639k
  switch (type) {
961
189k
  case isl_dim_param:
962
189k
    space = space_extend(space,
963
189k
        space->nparam + n, space->n_in, space->n_out);
964
189k
    if (
space && 189k
space->nested[0]189k
&&
965
8.23k
        !(space->nested[0] = isl_space_add_dims(space->nested[0],
966
8.23k
                isl_dim_param, n)))
967
0
      goto error;
968
189k
    
if (189k
space && 189k
space->nested[1]189k
&&
969
7.08k
        !(space->nested[1] = isl_space_add_dims(space->nested[1],
970
7.08k
                isl_dim_param, n)))
971
0
      goto error;
972
189k
    return space;
973
7.63k
  case isl_dim_in:
974
7.63k
    return space_extend(space,
975
7.63k
        space->nparam, space->n_in + n, space->n_out);
976
442k
  case isl_dim_out:
977
442k
    return space_extend(space,
978
442k
        space->nparam, space->n_in, space->n_out + n);
979
0
  default:
980
0
    isl_die(space->ctx, isl_error_invalid,
981
639k
      "cannot add dimensions of specified type", goto error);
982
639k
  }
983
0
error:
984
0
  isl_space_free(space);
985
0
  return NULL;
986
639k
}
987
988
static int valid_dim_type(enum isl_dim_type type)
989
1.96M
{
990
1.96M
  switch (type) {
991
1.96M
  case isl_dim_param:
992
1.96M
  case isl_dim_in:
993
1.96M
  case isl_dim_out:
994
1.96M
    return 1;
995
0
  default:
996
0
    return 0;
997
0
  }
998
0
}
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
168k
{
1007
168k
  isl_id **ids = NULL;
1008
168k
1009
168k
  if (!dim)
1010
0
    return NULL;
1011
168k
  
if (168k
n == 0168k
)
1012
2
    return isl_space_reset(dim, type);
1013
168k
1014
168k
  
if (168k
!valid_dim_type(type)168k
)
1015
0
    isl_die(dim->ctx, isl_error_invalid,
1016
168k
      "cannot insert dimensions of specified type",
1017
168k
      goto error);
1018
168k
1019
168k
  
isl_assert168k
(dim->ctx, pos <= isl_space_dim(dim, type), goto error);
1020
168k
1021
168k
  dim = isl_space_cow(dim);
1022
168k
  if (!dim)
1023
0
    return NULL;
1024
168k
1025
168k
  
if (168k
dim->ids168k
) {
1026
109k
    enum isl_dim_type t, o = isl_dim_param;
1027
109k
    int off;
1028
109k
    int s[3];
1029
109k
    ids = isl_calloc_array(dim->ctx, isl_id *,
1030
109k
             dim->nparam + dim->n_in + dim->n_out + n);
1031
109k
    if (!ids)
1032
0
      goto error;
1033
109k
    off = 0;
1034
109k
    s[isl_dim_param - o] = dim->nparam;
1035
109k
    s[isl_dim_in - o] = dim->n_in;
1036
109k
    s[isl_dim_out - o] = dim->n_out;
1037
438k
    for (t = isl_dim_param; 
t <= isl_dim_out438k
;
++t329k
) {
1038
329k
      if (
t != type329k
) {
1039
219k
        get_ids(dim, t, 0, s[t - o], ids + off);
1040
219k
        off += s[t - o];
1041
329k
      } else {
1042
109k
        get_ids(dim, t, 0, pos, ids + off);
1043
109k
        off += pos + n;
1044
109k
        get_ids(dim, t, pos, s[t - o] - pos, ids + off);
1045
109k
        off += s[t - o] - pos;
1046
109k
      }
1047
329k
    }
1048
109k
    free(dim->ids);
1049
109k
    dim->ids = ids;
1050
109k
    dim->n_id = dim->nparam + dim->n_in + dim->n_out + n;
1051
109k
  }
1052
168k
  switch (type) {
1053
2.34k
  case isl_dim_param: dim->nparam += n; break;
1054
9.56k
  case isl_dim_in:  dim->n_in += n; break;
1055
156k
  case isl_dim_out: dim->n_out += n; break;
1056
0
  default:    ;
1057
168k
  }
1058
168k
  dim = isl_space_reset(dim, type);
1059
168k
1060
168k
  if (
type == isl_dim_param168k
) {
1061
2.34k
    if (
dim && 2.34k
dim->nested[0]2.34k
&&
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.34k
    
if (2.34k
dim && 2.34k
dim->nested[1]2.34k
&&
1066
3
        !(dim->nested[1] = isl_space_insert_dims(dim->nested[1],
1067
3
                isl_dim_param, pos, n)))
1068
0
      goto error;
1069
168k
  }
1070
168k
1071
168k
  return dim;
1072
0
error:
1073
0
  isl_space_free(dim);
1074
0
  return NULL;
1075
168k
}
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
1.13k
{
1081
1.13k
  int i;
1082
1.13k
1083
1.13k
  space = isl_space_reset(space, src_type);
1084
1.13k
  space = isl_space_reset(space, dst_type);
1085
1.13k
  if (!space)
1086
0
    return NULL;
1087
1.13k
  
if (1.13k
n == 01.13k
)
1088
302
    return space;
1089
834
1090
834
  
isl_assert834
(space->ctx, src_pos + n <= isl_space_dim(space, src_type),
1091
834
    goto error);
1092
834
1093
834
  
if (834
dst_type == src_type && 834
dst_pos == src_pos0
)
1094
0
    return space;
1095
834
1096
834
  
isl_assert834
(space->ctx, dst_type != src_type, goto error);
1097
834
1098
834
  space = isl_space_cow(space);
1099
834
  if (!space)
1100
0
    return NULL;
1101
834
1102
834
  
if (834
space->ids834
) {
1103
496
    isl_id **ids;
1104
496
    enum isl_dim_type t, o = isl_dim_param;
1105
496
    int off;
1106
496
    int s[3];
1107
496
    ids = isl_calloc_array(space->ctx, isl_id *,
1108
496
         space->nparam + space->n_in + space->n_out);
1109
496
    if (!ids)
1110
0
      goto error;
1111
496
    off = 0;
1112
496
    s[isl_dim_param - o] = space->nparam;
1113
496
    s[isl_dim_in - o] = space->n_in;
1114
496
    s[isl_dim_out - o] = space->n_out;
1115
1.98k
    for (t = isl_dim_param; 
t <= isl_dim_out1.98k
;
++t1.48k
) {
1116
1.48k
      if (
t == dst_type1.48k
) {
1117
496
        get_ids(space, t, 0, dst_pos, ids + off);
1118
496
        off += dst_pos;
1119
496
        get_ids(space, src_type, src_pos, n, ids + off);
1120
496
        off += n;
1121
496
        get_ids(space, t, dst_pos, s[t - o] - dst_pos,
1122
496
            ids + off);
1123
496
        off += s[t - o] - dst_pos;
1124
1.48k
      } else 
if (992
t == src_type992
) {
1125
496
        get_ids(space, t, 0, src_pos, ids + off);
1126
496
        off += src_pos;
1127
496
        get_ids(space, t, src_pos + n,
1128
496
              s[t - o] - src_pos - n, ids + off);
1129
496
        off += s[t - o] - src_pos - n;
1130
992
      } else {
1131
496
        get_ids(space, t, 0, s[t - o], ids + off);
1132
496
        off += s[t - o];
1133
496
      }
1134
1.48k
    }
1135
496
    free(space->ids);
1136
496
    space->ids = ids;
1137
496
    space->n_id = space->nparam + space->n_in + space->n_out;
1138
496
  }
1139
834
1140
834
  switch (dst_type) {
1141
85
  case isl_dim_param: space->nparam += n; break;
1142
341
  case isl_dim_in:  space->n_in += n; break;
1143
408
  case isl_dim_out: space->n_out += n; break;
1144
0
  default:    ;
1145
834
  }
1146
834
1147
834
  switch (src_type) {
1148
235
  case isl_dim_param: space->nparam -= n; break;
1149
191
  case isl_dim_in:  space->n_in -= n; break;
1150
408
  case isl_dim_out: space->n_out -= n; break;
1151
0
  default:    ;
1152
834
  }
1153
834
1154
834
  
if (834
dst_type != isl_dim_param && 834
src_type != isl_dim_param749
)
1155
514
    return space;
1156
320
1157
960
  
for (i = 0; 320
i < 2960
;
++i640
) {
1158
640
    if (!space->nested[i])
1159
623
      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
640
  }
1165
320
1166
320
  return space;
1167
0
error:
1168
0
  isl_space_free(space);
1169
0
  return NULL;
1170
1.13k
}
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
623k
{
1178
623k
  isl_bool equal;
1179
623k
1180
623k
  equal = isl_space_has_equal_params(space1, space2);
1181
623k
  if (equal < 0)
1182
0
    return isl_stat_error;
1183
623k
  
if (623k
!equal623k
)
1184
0
    isl_die(isl_space_get_ctx(space1), isl_error_invalid,
1185
623k
      "parameters need to match", return isl_stat_error);
1186
623k
  return isl_stat_ok;
1187
623k
}
1188
1189
__isl_give isl_space *isl_space_join(__isl_take isl_space *left,
1190
  __isl_take isl_space *right)
1191
521k
{
1192
521k
  isl_space *dim;
1193
521k
1194
521k
  if (isl_space_check_equal_params(left, right) < 0)
1195
0
    goto error;
1196
521k
1197
521k
  
isl_assert521k
(left->ctx,
1198
521k
    isl_space_tuple_is_equal(left, isl_dim_out, right, isl_dim_in),
1199
521k
    goto error);
1200
521k
1201
521k
  dim = isl_space_alloc(left->ctx, left->nparam, left->n_in, right->n_out);
1202
521k
  if (!dim)
1203
0
    goto error;
1204
521k
1205
521k
  dim = copy_ids(dim, isl_dim_param, 0, left, isl_dim_param);
1206
521k
  dim = copy_ids(dim, isl_dim_in, 0, left, isl_dim_in);
1207
521k
  dim = copy_ids(dim, isl_dim_out, 0, right, isl_dim_out);
1208
521k
1209
521k
  if (
dim && 521k
left->tuple_id[0]521k
&&
1210
177k
      !(dim->tuple_id[0] = isl_id_copy(left->tuple_id[0])))
1211
0
    goto error;
1212
521k
  
if (521k
dim && 521k
right->tuple_id[1]521k
&&
1213
143k
      !(dim->tuple_id[1] = isl_id_copy(right->tuple_id[1])))
1214
0
    goto error;
1215
521k
  
if (521k
dim && 521k
left->nested[0]521k
&&
1216
196k
      !(dim->nested[0] = isl_space_copy(left->nested[0])))
1217
0
    goto error;
1218
521k
  
if (521k
dim && 521k
right->nested[1]521k
&&
1219
242k
      !(dim->nested[1] = isl_space_copy(right->nested[1])))
1220
0
    goto error;
1221
521k
1222
521k
  isl_space_free(left);
1223
521k
  isl_space_free(right);
1224
521k
1225
521k
  return dim;
1226
0
error:
1227
0
  isl_space_free(left);
1228
0
  isl_space_free(right);
1229
0
  return NULL;
1230
521k
}
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.73k
{
1239
2.73k
  isl_space *dom1, *dom2, *nest1, *nest2;
1240
2.73k
  int is_set;
1241
2.73k
1242
2.73k
  if (
!left || 2.73k
!right2.73k
)
1243
0
    goto error;
1244
2.73k
1245
2.73k
  is_set = isl_space_is_set(left);
1246
2.73k
  if (is_set != isl_space_is_set(right))
1247
0
    isl_die(isl_space_get_ctx(left), isl_error_invalid,
1248
2.73k
      "expecting either two set spaces or two map spaces",
1249
2.73k
      goto error);
1250
2.73k
  
if (2.73k
is_set2.73k
)
1251
220
    return isl_space_range_product(left, right);
1252
2.51k
1253
2.51k
  
if (2.51k
isl_space_check_equal_params(left, right) < 02.51k
)
1254
0
    goto error;
1255
2.51k
1256
2.51k
  dom1 = isl_space_domain(isl_space_copy(left));
1257
2.51k
  dom2 = isl_space_domain(isl_space_copy(right));
1258
2.51k
  nest1 = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1259
2.51k
1260
2.51k
  dom1 = isl_space_range(left);
1261
2.51k
  dom2 = isl_space_range(right);
1262
2.51k
  nest2 = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1263
2.51k
1264
2.51k
  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.73k
}
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.75k
{
1277
1.75k
  isl_space *ran, *dom1, *dom2, *nest;
1278
1.75k
1279
1.75k
  if (isl_space_check_equal_params(left, right) < 0)
1280
0
    goto error;
1281
1.75k
1282
1.75k
  
if (1.75k
!isl_space_tuple_is_equal(left, isl_dim_out, right, isl_dim_out)1.75k
)
1283
0
    isl_die(left->ctx, isl_error_invalid,
1284
1.75k
      "ranges need to match", goto error);
1285
1.75k
1286
1.75k
  ran = isl_space_range(isl_space_copy(left));
1287
1.75k
1288
1.75k
  dom1 = isl_space_domain(left);
1289
1.75k
  dom2 = isl_space_domain(right);
1290
1.75k
  nest = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1291
1.75k
1292
1.75k
  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.75k
}
1298
1299
__isl_give isl_space *isl_space_range_product(__isl_take isl_space *left,
1300
  __isl_take isl_space *right)
1301
97.5k
{
1302
97.5k
  isl_space *dom, *ran1, *ran2, *nest;
1303
97.5k
1304
97.5k
  if (isl_space_check_equal_params(left, right) < 0)
1305
0
    goto error;
1306
97.5k
1307
97.5k
  
if (97.5k
!isl_space_tuple_is_equal(left, isl_dim_in, right, isl_dim_in)97.5k
)
1308
0
    isl_die(left->ctx, isl_error_invalid,
1309
97.5k
      "domains need to match", goto error);
1310
97.5k
1311
97.5k
  dom = isl_space_domain(isl_space_copy(left));
1312
97.5k
1313
97.5k
  ran1 = isl_space_range(left);
1314
97.5k
  ran2 = isl_space_range(right);
1315
97.5k
  nest = isl_space_wrap(isl_space_join(isl_space_reverse(ran1), ran2));
1316
97.5k
1317
97.5k
  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
97.5k
}
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
132
{
1328
132
  space = isl_space_domain_factor_domain(space);
1329
132
  space = isl_space_range_factor_domain(space);
1330
132
  return space;
1331
132
}
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
796
{
1338
796
  isl_space *nested;
1339
796
  isl_space *domain;
1340
796
1341
796
  if (!space)
1342
0
    return NULL;
1343
796
  
if (796
!isl_space_domain_is_wrapping(space)796
)
1344
0
    isl_die(isl_space_get_ctx(space), isl_error_invalid,
1345
796
      "domain not a product", return isl_space_free(space));
1346
796
1347
796
  nested = space->nested[0];
1348
796
  domain = isl_space_copy(space);
1349
796
  domain = isl_space_drop_dims(domain, isl_dim_in,
1350
796
          nested->n_in, nested->n_out);
1351
796
  if (!domain)
1352
0
    return isl_space_free(space);
1353
796
  
if (796
nested->tuple_id[0]796
) {
1354
484
    domain->tuple_id[0] = isl_id_copy(nested->tuple_id[0]);
1355
484
    if (!domain->tuple_id[0])
1356
0
      goto error;
1357
796
  }
1358
796
  
if (796
nested->nested[0]796
) {
1359
312
    domain->nested[0] = isl_space_copy(nested->nested[0]);
1360
312
    if (!domain->nested[0])
1361
0
      goto error;
1362
796
  }
1363
796
1364
796
  isl_space_free(space);
1365
796
  return domain;
1366
0
error:
1367
0
  isl_space_free(space);
1368
0
  isl_space_free(domain);
1369
0
  return NULL;
1370
796
}
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
19.9k
{
1377
19.9k
  isl_space *nested;
1378
19.9k
  isl_space *range;
1379
19.9k
1380
19.9k
  if (!space)
1381
0
    return NULL;
1382
19.9k
  
if (19.9k
!isl_space_domain_is_wrapping(space)19.9k
)
1383
0
    isl_die(isl_space_get_ctx(space), isl_error_invalid,
1384
19.9k
      "domain not a product", return isl_space_free(space));
1385
19.9k
1386
19.9k
  nested = space->nested[0];
1387
19.9k
  range = isl_space_copy(space);
1388
19.9k
  range = isl_space_drop_dims(range, isl_dim_in, 0, nested->n_in);
1389
19.9k
  if (!range)
1390
0
    return isl_space_free(space);
1391
19.9k
  
if (19.9k
nested->tuple_id[1]19.9k
) {
1392
11.7k
    range->tuple_id[0] = isl_id_copy(nested->tuple_id[1]);
1393
11.7k
    if (!range->tuple_id[0])
1394
0
      goto error;
1395
19.9k
  }
1396
19.9k
  
if (19.9k
nested->nested[1]19.9k
) {
1397
8.12k
    range->nested[0] = isl_space_copy(nested->nested[1]);
1398
8.12k
    if (!range->nested[0])
1399
0
      goto error;
1400
19.9k
  }
1401
19.9k
1402
19.9k
  isl_space_free(space);
1403
19.9k
  return range;
1404
0
error:
1405
0
  isl_space_free(space);
1406
0
  isl_space_free(range);
1407
0
  return NULL;
1408
19.9k
}
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
2.13k
{
1415
2.13k
  isl_space *nested;
1416
2.13k
  isl_space *domain;
1417
2.13k
1418
2.13k
  if (!space)
1419
0
    return NULL;
1420
2.13k
  
if (2.13k
!isl_space_range_is_wrapping(space)2.13k
)
1421
0
    isl_die(isl_space_get_ctx(space), isl_error_invalid,
1422
2.13k
      "range not a product", return isl_space_free(space));
1423
2.13k
1424
2.13k
  nested = space->nested[1];
1425
2.13k
  domain = isl_space_copy(space);
1426
2.13k
  domain = isl_space_drop_dims(domain, isl_dim_out,
1427
2.13k
          nested->n_in, nested->n_out);
1428
2.13k
  if (!domain)
1429
0
    return isl_space_free(space);
1430
2.13k
  
if (2.13k
nested->tuple_id[0]2.13k
) {
1431
1.19k
    domain->tuple_id[1] = isl_id_copy(nested->tuple_id[0]);
1432
1.19k
    if (!domain->tuple_id[1])
1433
0
      goto error;
1434
2.13k
  }
1435
2.13k
  
if (2.13k
nested->nested[0]2.13k
) {
1436
925
    domain->nested[1] = isl_space_copy(nested->nested[0]);
1437
925
    if (!domain->nested[1])
1438
0
      goto error;
1439
2.13k
  }
1440
2.13k
1441
2.13k
  isl_space_free(space);
1442
2.13k
  return domain;
1443
0
error:
1444
0
  isl_space_free(space);
1445
0
  isl_space_free(domain);
1446
0
  return NULL;
1447
2.13k
}
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
12.8k
{
1456
12.8k
  isl_space *nested;
1457
12.8k
  isl_space *range;
1458
12.8k
1459
12.8k
  if (!space)
1460
0
    return NULL;
1461
12.8k
1462
12.8k
  nested = space->nested[1];
1463
12.8k
  range = isl_space_copy(space);
1464
12.8k
  range = isl_space_drop_dims(range, isl_dim_out, 0, nested->n_in);
1465
12.8k
  if (!range)
1466
0
    return isl_space_free(space);
1467
12.8k
  
if (12.8k
nested->tuple_id[1]12.8k
) {
1468
689
    range->tuple_id[1] = isl_id_copy(nested->tuple_id[1]);
1469
689
    if (!range->tuple_id[1])
1470
0
      goto error;
1471
12.8k
  }
1472
12.8k
  
if (12.8k
nested->nested[1]12.8k
) {
1473
12.1k
    range->nested[1] = isl_space_copy(nested->nested[1]);
1474
12.1k
    if (!range->nested[1])
1475
0
      goto error;
1476
12.8k
  }
1477
12.8k
1478
12.8k
  isl_space_free(space);
1479
12.8k
  return range;
1480
0
error:
1481
0
  isl_space_free(space);
1482
0
  isl_space_free(range);
1483
0
  return NULL;
1484
12.8k
}
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
12.8k
{
1491
12.8k
  if (!space)
1492
0
    return NULL;
1493
12.8k
  
if (12.8k
!isl_space_range_is_wrapping(space)12.8k
)
1494
0
    isl_die(isl_space_get_ctx(space), isl_error_invalid,
1495
12.8k
      "range not a product", return isl_space_free(space));
1496
12.8k
1497
12.8k
  return range_factor_range(space);
1498
12.8k
}
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
12.1k
{
1518
12.1k
  if (!space)
1519
0
    return NULL;
1520
12.1k
  
if (12.1k
isl_space_is_set(space)12.1k
)
1521
0
    return set_factor_range(space);
1522
12.1k
  space = isl_space_domain_factor_range(space);
1523
12.1k
  space = isl_space_range_factor_range(space);
1524
12.1k
  return space;
1525
12.1k
}
1526
1527
__isl_give isl_space *isl_space_map_from_set(__isl_take isl_space *space)
1528
11.9k
{
1529
11.9k
  isl_ctx *ctx;
1530
11.9k
  isl_id **ids = NULL;
1531
11.9k
  int n_id;
1532
11.9k
1533
11.9k
  if (!space)
1534
3
    return NULL;
1535
11.9k
  ctx = isl_space_get_ctx(space);
1536
11.9k
  if (!isl_space_is_set(space))
1537
0
    isl_die(ctx, isl_error_invalid, "not a set space", goto error);
1538
11.9k
  space = isl_space_cow(space);
1539
11.9k
  if (!space)
1540
0
    return NULL;
1541
11.9k
  n_id = space->nparam + space->n_out + space->n_out;
1542
11.9k
  if (
n_id > 0 && 11.9k
space->ids11.4k
) {
1543
9.53k
    ids = isl_calloc_array(space->ctx, isl_id *, n_id);
1544
9.53k
    if (!ids)
1545
0
      goto error;
1546
9.53k
    get_ids(space, isl_dim_param, 0, space->nparam, ids);
1547
9.53k
    get_ids(space, isl_dim_out, 0, space->n_out,
1548
9.53k
      ids + space->nparam);
1549
9.53k
  }
1550
11.9k
  space->n_in = space->n_out;
1551
11.9k
  if (
ids11.9k
) {
1552
9.53k
    free(space->ids);
1553
9.53k
    space->ids = ids;
1554
9.53k
    space->n_id = n_id;
1555
9.53k
    space = copy_ids(space, isl_dim_out, 0, space, isl_dim_in);
1556
9.53k
  }
1557
11.9k
  isl_id_free(space->tuple_id[0]);
1558
11.9k
  space->tuple_id[0] = isl_id_copy(space->tuple_id[1]);
1559
11.9k
  isl_space_free(space->nested[0]);
1560
11.9k
  space->nested[0] = isl_space_copy(space->nested[1]);
1561
11.9k
  return space;
1562
0
error:
1563
0
  isl_space_free(space);
1564
0
  return NULL;
1565
11.9k
}
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
81.8k
{
1570
81.8k
  if (
!domain || 81.8k
!range81.8k
)
1571
0
    goto error;
1572
81.8k
  
if (81.8k
!isl_space_is_set(domain)81.8k
)
1573
0
    isl_die(isl_space_get_ctx(domain), isl_error_invalid,
1574
81.8k
      "domain is not a set space", goto error);
1575
81.8k
  
if (81.8k
!isl_space_is_set(range)81.8k
)
1576
0
    isl_die(isl_space_get_ctx(range), isl_error_invalid,
1577
81.8k
      "range is not a set space", goto error);
1578
81.8k
  return isl_space_join(isl_space_reverse(domain), range);
1579
0
error:
1580
0
  isl_space_free(domain);
1581
0
  isl_space_free(range);
1582
0
  return NULL;
1583
81.8k
}
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
2.39M
{
1589
2.39M
  int i;
1590
2.39M
1591
6.90M
  for (i = 0; 
i < n6.90M
;
++i4.50M
)
1592
4.50M
    dim = set_id(dim, type, first + i, ids[i]);
1593
2.39M
1594
2.39M
  return dim;
1595
2.39M
}
1596
1597
__isl_give isl_space *isl_space_reverse(__isl_take isl_space *dim)
1598
1.73M
{
1599
1.73M
  unsigned t;
1600
1.73M
  isl_space *nested;
1601
1.73M
  isl_id **ids = NULL;
1602
1.73M
  isl_id *id;
1603
1.73M
1604
1.73M
  if (!dim)
1605
0
    return NULL;
1606
1.73M
  
if (1.73M
match(dim, isl_dim_in, dim, isl_dim_out)1.73M
)
1607
108k
    return dim;
1608
1.62M
1609
1.62M
  dim = isl_space_cow(dim);
1610
1.62M
  if (!dim)
1611
0
    return NULL;
1612
1.62M
1613
1.62M
  id = dim->tuple_id[0];
1614
1.62M
  dim->tuple_id[0] = dim->tuple_id[1];
1615
1.62M
  dim->tuple_id[1] = id;
1616
1.62M
1617
1.62M
  nested = dim->nested[0];
1618
1.62M
  dim->nested[0] = dim->nested[1];
1619
1.62M
  dim->nested[1] = nested;
1620
1.62M
1621
1.62M
  if (
dim->ids1.62M
) {
1622
1.19M
    int n_id = dim->n_in + dim->n_out;
1623
1.19M
    ids = isl_alloc_array(dim->ctx, isl_id *, n_id);
1624
1.19M
    if (
n_id && 1.19M
!ids1.04M
)
1625
0
      goto error;
1626
1.19M
    get_ids(dim, isl_dim_in, 0, dim->n_in, ids);
1627
1.19M
    get_ids(dim, isl_dim_out, 0, dim->n_out, ids + dim->n_in);
1628
1.19M
  }
1629
1.62M
1630
1.62M
  t = dim->n_in;
1631
1.62M
  dim->n_in = dim->n_out;
1632
1.62M
  dim->n_out = t;
1633
1.62M
1634
1.62M
  if (
dim->ids1.62M
) {
1635
1.19M
    dim = set_ids(dim, isl_dim_out, 0, dim->n_out, ids);
1636
1.19M
    dim = set_ids(dim, isl_dim_in, 0, dim->n_in, ids + dim->n_out);
1637
1.19M
    free(ids);
1638
1.19M
  }
1639
1.62M
1640
1.62M
  return dim;
1641
0
error:
1642
0
  free(ids);
1643
0
  isl_space_free(dim);
1644
0
  return NULL;
1645
1.73M
}
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
2.19M
{
1650
2.19M
  int i;
1651
2.19M
1652
2.19M
  if (!dim)
1653
1
    return NULL;
1654
2.19M
1655
2.19M
  
if (2.19M
num == 02.19M
)
1656
397k
    return isl_space_reset(dim, type);
1657
1.80M
1658
1.80M
  
if (1.80M
!valid_dim_type(type)1.80M
)
1659
0
    isl_die(dim->ctx, isl_error_invalid,
1660
1.80M
      "cannot drop dimensions of specified type", goto error);
1661
1.80M
1662
1.80M
  
if (1.80M
first + num > n(dim, type) || 1.80M
first + num < first1.80M
)
1663
0
    isl_die(isl_space_get_ctx(dim), isl_error_invalid,
1664
1.80M
      "index out of bounds", return isl_space_free(dim));
1665
1.80M
  dim = isl_space_cow(dim);
1666
1.80M
  if (!dim)
1667
0
    goto error;
1668
1.80M
  
if (1.80M
dim->ids1.80M
) {
1669
1.08M
    dim = extend_ids(dim);
1670
1.08M
    if (!dim)
1671
0
      goto error;
1672
4.46M
    
for (i = 0; 1.08M
i < num4.46M
;
++i3.37M
)
1673
3.37M
      isl_id_free(get_id(dim, type, first + i));
1674
1.20M
    for (i = first+num; 
i < n(dim, type)1.20M
;
++i117k
)
1675
117k
      set_id(dim, type, i - num, get_id(dim, type, i));
1676
1.08M
    switch (type) {
1677
70.0k
    case isl_dim_param:
1678
70.0k
      get_ids(dim, isl_dim_in, 0, dim->n_in,
1679
70.0k
        dim->ids + offset(dim, isl_dim_in) - num);
1680
375k
    case isl_dim_in:
1681
375k
      get_ids(dim, isl_dim_out, 0, dim->n_out,
1682
375k
        dim->ids + offset(dim, isl_dim_out) - num);
1683
1.08M
    default:
1684
1.08M
      ;
1685
1.08M
    }
1686
1.08M
    dim->n_id -= num;
1687
1.08M
  }
1688
1.80M
  switch (type) {
1689
70.2k
  case isl_dim_param: dim->nparam -= num; break;
1690
376k
  case isl_dim_in:  dim->n_in -= num; break;
1691
1.35M
  case isl_dim_out: dim->n_out -= num; break;
1692
0
  default:    ;
1693
1.80M
  }
1694
1.80M
  dim = isl_space_reset(dim, type);
1695
1.80M
  if (
type == isl_dim_param1.80M
) {
1696
70.2k
    if (
dim && 70.2k
dim->nested[0]70.2k
&&
1697
3.30k
        !(dim->nested[0] = isl_space_drop_dims(dim->nested[0],
1698
3.30k
                isl_dim_param, first, num)))
1699
0
      goto error;
1700
70.2k
    
if (70.2k
dim && 70.2k
dim->nested[1]70.2k
&&
1701
2.09k
        !(dim->nested[1] = isl_space_drop_dims(dim->nested[1],
1702
2.09k
                isl_dim_param, first, num)))
1703
0
      goto error;
1704
1.80M
  }
1705
1.80M
  return dim;
1706
0
error:
1707
0
  isl_space_free(dim);
1708
0
  return NULL;
1709
2.19M
}
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
666k
{
1729
666k
  if (!space)
1730
0
    return NULL;
1731
666k
  space = isl_space_drop_dims(space, isl_dim_out, 0, space->n_out);
1732
666k
  space = isl_space_reverse(space);
1733
666k
  space = mark_as_set(space);
1734
666k
  return space;
1735
666k
}
1736
1737
__isl_give isl_space *isl_space_from_domain(__isl_take isl_space *dim)
1738
518k
{
1739
518k
  if (!dim)
1740
0
    return NULL;
1741
518k
  
if (518k
!isl_space_is_set(dim)518k
)
1742
0
    isl_die(isl_space_get_ctx(dim), isl_error_invalid,
1743
518k
      "not a set space", goto error);
1744
518k
  dim = isl_space_reverse(dim);
1745
518k
  dim = isl_space_reset(dim, isl_dim_out);
1746
518k
  return dim;
1747
0
error:
1748
0
  isl_space_free(dim);
1749
0
  return NULL;
1750
518k
}
1751
1752
__isl_give isl_space *isl_space_range(__isl_take isl_space *space)
1753
330k
{
1754
330k
  if (!space)
1755
1
    return NULL;
1756
330k
  space = isl_space_drop_dims(space, isl_dim_in, 0, space->n_in);
1757
330k
  space = mark_as_set(space);
1758
330k
  return space;
1759
330k
}
1760
1761
__isl_give isl_space *isl_space_from_range(__isl_take isl_space *dim)
1762
190k
{
1763
190k
  if (!dim)
1764
4
    return NULL;
1765
190k
  
if (190k
!isl_space_is_set(dim)190k
)
1766
0
    isl_die(isl_space_get_ctx(dim), isl_error_invalid,
1767
190k
      "not a set space", goto error);
1768
190k
  return isl_space_reset(dim, isl_dim_in);
1769
0
error:
1770
0
  isl_space_free(dim);
1771
0
  return NULL;
1772
190k
}
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
1.64k
{
1778
1.64k
  isl_space *domain;
1779
1.64k
1780
1.64k
  domain = isl_space_from_range(isl_space_domain(isl_space_copy(space)));
1781
1.64k
  space = isl_space_from_domain(isl_space_wrap(space));
1782
1.64k
  space = isl_space_join(space, domain);
1783
1.64k
1784
1.64k
  return space;
1785
1.64k
}
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
2
{
1791
2
  isl_space *range;
1792
2
1793
2
  range = isl_space_from_range(isl_space_range(isl_space_copy(space)));
1794
2
  space = isl_space_from_domain(isl_space_wrap(space));
1795
2
  space = isl_space_join(space, range);
1796
2
1797
2
  return space;
1798
2
}
1799
1800
__isl_give isl_space *isl_space_params(__isl_take isl_space *space)
1801
420k
{
1802
420k
  if (isl_space_is_params(space))
1803
318k
    return space;
1804
101k
  space = isl_space_drop_dims(space,
1805
101k
          isl_dim_in, 0, isl_space_dim(space, isl_dim_in));
1806
101k
  space = isl_space_drop_dims(space,
1807
101k
          isl_dim_out, 0, isl_space_dim(space, isl_dim_out));
1808
101k
  space = mark_as_params(space);
1809
101k
  return space;
1810
101k
}
1811
1812
__isl_give isl_space *isl_space_set_from_params(__isl_take isl_space *space)
1813
14.8k
{
1814
14.8k
  if (!space)
1815
0
    return NULL;
1816
14.8k
  
if (14.8k
!isl_space_is_params(space)14.8k
)
1817
0
    isl_die(isl_space_get_ctx(space), isl_error_invalid,
1818
14.8k
      "not a parameter space", goto error);
1819
14.8k
  return isl_space_reset(space, isl_dim_set);
1820
0
error:
1821
0
  isl_space_free(space);
1822
0
  return NULL;
1823
14.8k
}
1824
1825
__isl_give isl_space *isl_space_underlying(__isl_take isl_space *dim,
1826
  unsigned n_div)
1827
426k
{
1828
426k
  int i;
1829
426k
1830
426k
  if (!dim)
1831
0
    return NULL;
1832
426k
  
if (426k
n_div == 0 &&
1833
426k
      
dim->nparam == 0409k
&&
dim->n_in == 0230k
&&
dim->n_id == 0186k
)
1834
82.4k
    return isl_space_reset(isl_space_reset(dim, isl_dim_in), isl_dim_out);
1835
344k
  dim = isl_space_cow(dim);
1836
344k
  if (!dim)
1837
0
    return NULL;
1838
344k
  dim->n_out += dim->nparam + dim->n_in + n_div;
1839
344k
  dim->nparam = 0;
1840
344k
  dim->n_in = 0;
1841
344k
1842
2.26M
  for (i = 0; 
i < dim->n_id2.26M
;
++i1.91M
)
1843
1.91M
    isl_id_free(get_id(dim, isl_dim_out, i));
1844
426k
  dim->n_id = 0;
1845
426k
  dim = isl_space_reset(dim, isl_dim_in);
1846
426k
  dim = isl_space_reset(dim, isl_dim_out);
1847
426k
1848
426k
  return dim;
1849
426k
}
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
7.53M
{
1856
7.53M
  isl_bool equal;
1857
7.53M
1858
7.53M
  if (
!space1 || 7.53M
!space27.53M
)
1859
0
    return isl_bool_error;
1860
7.53M
  
if (7.53M
space1 == space27.53M
)
1861
4.27M
    return isl_bool_true;
1862
3.25M
  equal = isl_space_has_equal_params(space1, space2);
1863
3.25M
  if (
equal < 0 || 3.25M
!equal3.25M
)
1864
62.5k
    return equal;
1865
3.19M
  return isl_space_has_equal_tuples(space1, space2);
1866
3.19M
}
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
53
{
1876
53
  isl_bool equal_params;
1877
53
1878
53
  if (
!space1 || 53
!space253
)
1879
0
    return isl_bool_error;
1880
53
  
if (53
!isl_space_is_set(space1)53
)
1881
0
    return isl_bool_false;
1882
53
  equal_params = isl_space_has_equal_params(space1, space2);
1883
53
  if (
equal_params < 0 || 53
!equal_params53
)
1884
0
    return equal_params;
1885
53
  return isl_space_tuple_is_equal(space1, isl_dim_set,
1886
53
          space2, isl_dim_in);
1887
53
}
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
25
{
1894
25
  if (!space2)
1895
0
    return isl_bool_error;
1896
25
  
if (25
!isl_space_is_map(space2)25
)
1897
0
    return isl_bool_false;
1898
25
  return isl_space_is_domain_internal(space1, space2);
1899
25
}
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
17.1k
{
1909
17.1k
  isl_bool equal_params;
1910
17.1k
1911
17.1k
  if (
!space1 || 17.1k
!space217.1k
)
1912
0
    return isl_bool_error;
1913
17.1k
  
if (17.1k
!isl_space_is_set(space1)17.1k
)
1914
0
    return isl_bool_false;
1915
17.1k
  equal_params = isl_space_has_equal_params(space1, space2);
1916
17.1k
  if (
equal_params < 0 || 17.1k
!equal_params17.1k
)
1917
0
    return equal_params;
1918
17.1k
  return isl_space_tuple_is_equal(space1, isl_dim_set,
1919
17.1k
          space2, isl_dim_out);
1920
17.1k
}
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
481k
{
1938
481k
  int i;
1939
481k
  isl_id *id;
1940
481k
1941
481k
  if (!space)
1942
0
    return hash;
1943
481k
1944
481k
  
isl_hash_byte481k
(hash, space->nparam % 256);
1945
481k
1946
1.10M
  for (i = 0; 
i < space->nparam1.10M
;
++i627k
) {
1947
627k
    id = get_id(space, isl_dim_param, i);
1948
627k
    hash = isl_hash_id(hash, id);
1949
627k
  }
1950
481k
1951
481k
  return hash;
1952
481k
}
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.92M
{
1959
1.92M
  isl_id *id;
1960
1.92M
1961
1.92M
  if (!space)
1962
1.20M
    return hash;
1963
719k
1964
719k
  
isl_hash_byte719k
(hash, space->n_in % 256);
1965
719k
  isl_hash_byte(hash, space->n_out % 256);
1966
1.92M
1967
1.92M
  id = tuple_id(space, isl_dim_in);
1968
1.92M
  hash = isl_hash_id(hash, id);
1969
1.92M
  id = tuple_id(space, isl_dim_out);
1970
1.92M
  hash = isl_hash_id(hash, id);
1971
1.92M
1972
1.92M
  hash = isl_hash_tuples(hash, space->nested[0]);
1973
1.92M
  hash = isl_hash_tuples(hash, space->nested[1]);
1974
1.92M
1975
1.92M
  return hash;
1976
1.92M
}
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
32.2k
{
1985
32.2k
  isl_id *id;
1986
32.2k
1987
32.2k
  if (!space)
1988
0
    return hash;
1989
32.2k
1990
32.2k
  
isl_hash_byte32.2k
(hash, 0);
1991
32.2k
  isl_hash_byte(hash, space->n_in % 256);
1992
32.2k
1993
32.2k
  hash = isl_hash_id(hash, &isl_id_none);
1994
32.2k
  id = tuple_id(space, isl_dim_in);
1995
32.2k
  hash = isl_hash_id(hash, id);
1996
32.2k
1997
32.2k
  hash = isl_hash_tuples(hash, space->nested[0]);
1998
32.2k
1999
32.2k
  return hash;
2000
32.2k
}
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
2.03k
{
2007
2.03k
  uint32_t hash;
2008
2.03k
2009
2.03k
  if (!space)
2010
0
    return 0;
2011
2.03k
2012
2.03k
  
hash = 2.03k
isl_hash_init2.03k
();
2013
2.03k
  hash = isl_hash_tuples(hash, space);
2014
2.03k
2015
2.03k
  return hash;
2016
2.03k
}
2017
2018
uint32_t isl_space_get_hash(__isl_keep isl_space *space)
2019
448k
{
2020
448k
  uint32_t hash;
2021
448k
2022
448k
  if (!space)
2023
0
    return 0;
2024
448k
2025
448k
  
hash = 448k
isl_hash_init448k
();
2026
448k
  hash = isl_hash_params(hash, space);
2027
448k
  hash = isl_hash_tuples(hash, space);
2028
448k
2029
448k
  return hash;
2030
448k
}
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
32.2k
{
2038
32.2k
  uint32_t hash;
2039
32.2k
2040
32.2k
  if (!space)
2041
0
    return 0;
2042
32.2k
2043
32.2k
  
hash = 32.2k
isl_hash_init32.2k
();
2044
32.2k
  hash = isl_hash_params(hash, space);
2045
32.2k
  hash = isl_hash_tuples_domain(hash, space);
2046
32.2k
2047
32.2k
  return hash;
2048
32.2k
}
2049
2050
isl_bool isl_space_is_wrapping(__isl_keep isl_space *dim)
2051
53.9k
{
2052
53.9k
  if (!dim)
2053
0
    return isl_bool_error;
2054
53.9k
2055
53.9k
  
if (53.9k
!isl_space_is_set(dim)53.9k
)
2056
0
    return isl_bool_false;
2057
53.9k
2058
53.9k
  return dim->nested[1] != NULL;
2059
53.9k
}
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
48.1k
{
2065
48.1k
  if (!space)
2066
0
    return isl_bool_error;
2067
48.1k
2068
48.1k
  
if (48.1k
isl_space_is_set(space)48.1k
)
2069
0
    return isl_bool_false;
2070
48.1k
2071
48.1k
  return space->nested[0] != NULL;
2072
48.1k
}
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
64.2k
{
2078
64.2k
  if (!space)
2079
0
    return isl_bool_error;
2080
64.2k
2081
64.2k
  
if (64.2k
isl_space_is_set(space)64.2k
)
2082
773
    return isl_bool_false;
2083
63.4k
2084
63.4k
  return space->nested[1] != NULL;
2085
63.4k
}
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
6.04k
{
2093
6.04k
  isl_bool is_set;
2094
6.04k
  isl_bool is_product;
2095
6.04k
2096
6.04k
  is_set = isl_space_is_set(space);
2097
6.04k
  if (is_set < 0)
2098
0
    return isl_bool_error;
2099
6.04k
  
if (6.04k
is_set6.04k
)
2100
0
    return isl_space_is_wrapping(space);
2101
6.04k
  is_product = isl_space_domain_is_wrapping(space);
2102
6.04k
  if (
is_product < 0 || 6.04k
!is_product6.04k
)
2103
34
    return is_product;
2104
6.01k
  return isl_space_range_is_wrapping(space);
2105
6.01k
}
2106
2107
__isl_give isl_space *isl_space_wrap(__isl_take isl_space *dim)
2108
194k
{
2109
194k
  isl_space *wrap;
2110
194k
2111
194k
  if (!dim)
2112
0
    return NULL;
2113
194k
2114
194k
  wrap = isl_space_set_alloc(dim->ctx,
2115
194k
            dim->nparam, dim->n_in + dim->n_out);
2116
194k
2117
194k
  wrap = copy_ids(wrap, isl_dim_param, 0, dim, isl_dim_param);
2118
194k
  wrap = copy_ids(wrap, isl_dim_set, 0, dim, isl_dim_in);
2119
194k
  wrap = copy_ids(wrap, isl_dim_set, dim->n_in, dim, isl_dim_out);
2120
194k
2121
194k
  if (!wrap)
2122
0
    goto error;
2123
194k
2124
194k
  wrap->nested[1] = dim;
2125
194k
2126
194k
  return wrap;
2127
0
error:
2128
0
  isl_space_free(dim);
2129
0
  return NULL;
2130
194k
}
2131
2132
__isl_give isl_space *isl_space_unwrap(__isl_take isl_space *dim)
2133
37.6k
{
2134
37.6k
  isl_space *unwrap;
2135
37.6k
2136
37.6k
  if (!dim)
2137
0
    return NULL;
2138
37.6k
2139
37.6k
  
if (37.6k
!isl_space_is_wrapping(dim)37.6k
)
2140
0
    isl_die(dim->ctx, isl_error_invalid, "not a wrapping space",
2141
37.6k
      goto error);
2142
37.6k
2143
37.6k
  unwrap = isl_space_copy(dim->nested[1]);
2144
37.6k
  isl_space_free(dim);
2145
37.6k
2146
37.6k
  return unwrap;
2147
0
error:
2148
0
  isl_space_free(dim);
2149
0
  return NULL;
2150
37.6k
}
2151
2152
isl_bool isl_space_is_named_or_nested(__isl_keep isl_space *space,
2153
  enum isl_dim_type type)
2154
5.18M
{
2155
5.18M
  if (
type != isl_dim_in && 5.18M
type != isl_dim_out3.72M
)
2156
330k
    return isl_bool_false;
2157
4.85M
  
if (4.85M
!space4.85M
)
2158
5
    return isl_bool_error;
2159
4.85M
  
if (4.85M
space->tuple_id[type - isl_dim_in]4.85M
)
2160
2.03M
    return isl_bool_true;
2161
2.81M
  
if (2.81M
space->nested[type - isl_dim_in]2.81M
)
2162
744k
    return isl_bool_true;
2163
2.07M
  return isl_bool_false;
2164
2.07M
}
2165
2166
isl_bool isl_space_may_be_set(__isl_keep isl_space *space)
2167
272
{
2168
272
  isl_bool nested;
2169
272
2170
272
  if (!space)
2171
0
    return isl_bool_error;
2172
272
  
if (272
isl_space_is_set(space)272
)
2173
167
    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
4.71M
{
2185
4.71M
  if (!isl_space_is_named_or_nested(dim, type))
2186
2.15M
    return dim;
2187
2.55M
2188
2.55M
  dim = isl_space_cow(dim);
2189
2.55M
  if (!dim)
2190
9
    return NULL;
2191
2.55M
2192
2.55M
  isl_id_free(dim->tuple_id[type - isl_dim_in]);
2193
2.55M
  dim->tuple_id[type - isl_dim_in] = NULL;
2194
2.55M
  isl_space_free(dim->nested[type - isl_dim_in]);
2195
2.55M
  dim->nested[type - isl_dim_in] = NULL;
2196
2.55M
2197
2.55M
  return dim;
2198
2.55M
}
2199
2200
__isl_give isl_space *isl_space_flatten(__isl_take isl_space *dim)
2201
22.7k
{
2202
22.7k
  if (!dim)
2203
0
    return NULL;
2204
22.7k
  
if (22.7k
!dim->nested[0] && 22.7k
!dim->nested[1]22.7k
)
2205
0
    return dim;
2206
22.7k
2207
22.7k
  
if (22.7k
dim->nested[0]22.7k
)
2208
0
    dim = isl_space_reset(dim, isl_dim_in);
2209
22.7k
  if (
dim && 22.7k
dim->nested[1]22.7k
)
2210
22.7k
    dim = isl_space_reset(dim, isl_dim_out);
2211
22.7k
2212
22.7k
  return dim;
2213
22.7k
}
2214
2215
__isl_give isl_space *isl_space_flatten_domain(__isl_take isl_space *dim)
2216
33
{
2217
33
  if (!dim)
2218
0
    return NULL;
2219
33
  
if (33
!dim->nested[0]33
)
2220
0
    return dim;
2221
33
2222
33
  return isl_space_reset(dim, isl_dim_in);
2223
33
}
2224
2225
__isl_give isl_space *isl_space_flatten_range(__isl_take isl_space *dim)
2226
69.1k
{
2227
69.1k
  if (!dim)
2228
0
    return NULL;
2229
69.1k
  
if (69.1k
!dim->nested[1]69.1k
)
2230
0
    return dim;
2231
69.1k
2232
69.1k
  return isl_space_reset(dim, isl_dim_out);
2233
69.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
118k
{
2240
118k
  dst = isl_space_cow(dst);
2241
118k
2242
118k
  if (
!dst || 118k
!src118k
)
2243
0
    goto error;
2244
118k
2245
118k
  dst = isl_space_drop_dims(dst, type, 0, isl_space_dim(dst, type));
2246
118k
  dst = isl_space_add_dims(dst, type, isl_space_dim(src, type));
2247
118k
  dst = copy_ids(dst, type, 0, src, type);
2248
118k
2249
118k
  if (
dst && 118k
type == isl_dim_param118k
) {
2250
118k
    int i;
2251
355k
    for (i = 0; 
i <= 1355k
;
++i237k
) {
2252
237k
      if (!dst->nested[i])
2253
224k
        continue;
2254
12.6k
      dst->nested[i] = isl_space_replace(dst->nested[i],
2255
12.6k
               type, src);
2256
12.6k
      if (!dst->nested[i])
2257
0
        goto error;
2258
237k
    }
2259
118k
  }
2260
118k
2261
118k
  return dst;
2262
0
error:
2263
0
  isl_space_free(dst);
2264
0
  return NULL;
2265
118k
}
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
20.4k
{
2274
20.4k
  isl_space *local_dim;
2275
20.4k
2276
20.4k
  if (!dim)
2277
0
    return NULL;
2278
20.4k
2279
20.4k
  local_dim = isl_space_dup(dim);
2280
20.4k
  local_dim = isl_space_drop_dims(local_dim, isl_dim_set, 0, dim->n_out);
2281
20.4k
  local_dim = isl_space_add_dims(local_dim, isl_dim_set, n_local);
2282
20.4k
  local_dim = isl_space_set_tuple_name(local_dim, isl_dim_set, "local");
2283
20.4k
  dim = isl_space_join(isl_space_from_domain(dim),
2284
20.4k
          isl_space_from_range(local_dim));
2285
20.4k
  dim = isl_space_wrap(dim);
2286
20.4k
  dim = isl_space_set_tuple_name(dim, isl_dim_set, "lifted");
2287
20.4k
2288
20.4k
  return dim;
2289
20.4k
}
2290
2291
isl_bool isl_space_can_zip(__isl_keep isl_space *space)
2292
5.95k
{
2293
5.95k
  isl_bool is_set;
2294
5.95k
2295
5.95k
  is_set = isl_space_is_set(space);
2296
5.95k
  if (is_set < 0)
2297
0
    return isl_bool_error;
2298
5.95k
  
if (5.95k
is_set5.95k
)
2299
0
    return isl_bool_false;
2300
5.95k
  return isl_space_is_product(space);
2301
5.95k
}
2302
2303
__isl_give isl_space *isl_space_zip(__isl_take isl_space *dim)
2304
2.56k
{
2305
2.56k
  isl_space *dom, *ran;
2306
2.56k
  isl_space *dom_dom, *dom_ran, *ran_dom, *ran_ran;
2307
2.56k
2308
2.56k
  if (!isl_space_can_zip(dim))
2309
0
    isl_die(dim->ctx, isl_error_invalid, "dim cannot be zipped",
2310
2.56k
      goto error);
2311
2.56k
2312
2.56k
  
if (2.56k
!dim2.56k
)
2313
0
    return NULL;
2314
2.56k
  dom = isl_space_unwrap(isl_space_domain(isl_space_copy(dim)));
2315
2.56k
  ran = isl_space_unwrap(isl_space_range(dim));
2316
2.56k
  dom_dom = isl_space_domain(isl_space_copy(dom));
2317
2.56k
  dom_ran = isl_space_range(dom);
2318
2.56k
  ran_dom = isl_space_domain(isl_space_copy(ran));
2319
2.56k
  ran_ran = isl_space_range(ran);
2320
2.56k
  dom = isl_space_join(isl_space_from_domain(dom_dom),
2321
2.56k
         isl_space_from_range(ran_dom));
2322
2.56k
  ran = isl_space_join(isl_space_from_domain(dom_ran),
2323
2.56k
         isl_space_from_range(ran_ran));
2324
2.56k
  return isl_space_join(isl_space_from_domain(isl_space_wrap(dom)),
2325
2.56k
          isl_space_from_range(isl_space_wrap(ran)));
2326
0
error:
2327
0
  isl_space_free(dim);
2328
0
  return NULL;
2329
2.56k
}
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
39.2k
{
2336
39.2k
  if (!space)
2337
0
    return isl_bool_error;
2338
39.2k
2339
39.2k
  return !!space->nested[0];
2340
39.2k
}
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
12.7k
{
2347
12.7k
  isl_space *dom, *ran;
2348
12.7k
  isl_space *dom_dom, *dom_ran;
2349
12.7k
2350
12.7k
  if (!space)
2351
0
    return NULL;
2352
12.7k
2353
12.7k
  
if (12.7k
!isl_space_can_curry(space)12.7k
)
2354
0
    isl_die(space->ctx, isl_error_invalid,
2355
12.7k
      "space cannot be curried", goto error);
2356
12.7k
2357
12.7k
  dom = isl_space_unwrap(isl_space_domain(isl_space_copy(space)));
2358
12.7k
  ran = isl_space_range(space);
2359
12.7k
  dom_dom = isl_space_domain(isl_space_copy(dom));
2360
12.7k
  dom_ran = isl_space_range(dom);
2361
12.7k
  ran = isl_space_join(isl_space_from_domain(dom_ran),
2362
12.7k
         isl_space_from_range(ran));
2363
12.7k
  return isl_space_join(isl_space_from_domain(dom_dom),
2364
12.7k
          isl_space_from_range(isl_space_wrap(ran)));
2365
0
error:
2366
0
  isl_space_free(space);
2367
0
  return NULL;
2368
12.7k
}
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
24.4k
{
2376
24.4k
  isl_bool can;
2377
24.4k
2378
24.4k
  if (!space)
2379
0
    return isl_bool_error;
2380
24.4k
  can = isl_space_range_is_wrapping(space);
2381
24.4k
  if (
can < 0 || 24.4k
!can24.4k
)
2382
0
    return can;
2383
24.4k
  return isl_space_can_curry(space->nested[1]);
2384
24.4k
}
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
12.1k
{
2391
12.1k
  if (!space)
2392
0
    return NULL;
2393
12.1k
2394
12.1k
  
if (12.1k
!isl_space_can_range_curry(space)12.1k
)
2395
0
    isl_die(isl_space_get_ctx(space), isl_error_invalid,
2396
12.1k
      "space range cannot be curried",
2397
12.1k
      return isl_space_free(space));
2398
12.1k
2399
12.1k
  space = isl_space_cow(space);
2400
12.1k
  if (!space)
2401
0
    return NULL;
2402
12.1k
  space->nested[1] = isl_space_curry(space->nested[1]);
2403
12.1k
  if (!space->nested[1])
2404
0
    return isl_space_free(space);
2405
12.1k
2406
12.1k
  return space;
2407
12.1k
}
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
1.34k
{
2414
1.34k
  if (!space)
2415
0
    return isl_bool_error;
2416
1.34k
2417
1.34k
  return !!space->nested[1];
2418
1.34k
}
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
474
{
2425
474
  isl_space *dom, *ran;
2426
474
  isl_space *ran_dom, *ran_ran;
2427
474
2428
474
  if (!space)
2429
0
    return NULL;
2430
474
2431
474
  
if (474
!isl_space_can_uncurry(space)474
)
2432
0
    isl_die(space->ctx, isl_error_invalid,
2433
474
      "space cannot be uncurried",
2434
474
      return isl_space_free(space));
2435
474
2436
474
  dom = isl_space_domain(isl_space_copy(space));
2437
474
  ran = isl_space_unwrap(isl_space_range(space));
2438
474
  ran_dom = isl_space_domain(isl_space_copy(ran));
2439
474
  ran_ran = isl_space_range(ran);
2440
474
  dom = isl_space_join(isl_space_from_domain(dom),
2441
474
         isl_space_from_range(ran_dom));
2442
474
  return isl_space_join(isl_space_from_domain(isl_space_wrap(dom)),
2443
474
          isl_space_from_range(ran_ran));
2444
474
}
2445
2446
isl_bool isl_space_has_named_params(__isl_keep isl_space *space)
2447
439k
{
2448
439k
  int i;
2449
439k
  unsigned off;
2450
439k
2451
439k
  if (!space)
2452
1
    return isl_bool_error;
2453
439k
  
if (439k
space->nparam == 0439k
)
2454
194k
    return isl_bool_true;
2455
245k
  off = isl_space_offset(space, isl_dim_param);
2456
245k
  if (off + space->nparam > space->n_id)
2457
0
    return isl_bool_false;
2458
1.00M
  
for (i = 0; 245k
i < space->nparam1.00M
;
++i758k
)
2459
758k
    
if (758k
!space->ids[off + i]758k
)
2460
0
      return isl_bool_false;
2461
245k
  return isl_bool_true;
2462
439k
}
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
169k
{
2469
169k
  isl_bool named;
2470
169k
2471
169k
  named = isl_space_has_named_params(space);
2472
169k
  if (named < 0)
2473
0
    return isl_stat_error;
2474
169k
  
if (169k
!named169k
)
2475
0
    isl_die(isl_space_get_ctx(space), isl_error_invalid,
2476
169k
      "unaligned unnamed parameters", return isl_stat_error);
2477
169k
2478
169k
  return isl_stat_ok;
2479
169k
}
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
25.4k
{
2486
25.4k
  isl_reordering *exp;
2487
25.4k
2488
25.4k
  if (
!isl_space_has_named_params(dim1) || 25.4k
!isl_space_has_named_params(dim2)25.4k
)
2489
0
    isl_die(isl_space_get_ctx(dim1), isl_error_invalid,
2490
25.4k
      "parameter alignment requires named parameters",
2491
25.4k
      goto error);
2492
25.4k
2493
25.4k
  dim2 = isl_space_params(dim2);
2494
25.4k
  exp = isl_parameter_alignment_reordering(dim1, dim2);
2495
25.4k
  exp = isl_reordering_extend_space(exp, dim1);
2496
25.4k
  isl_space_free(dim2);
2497
25.4k
  if (!exp)
2498
1
    return NULL;
2499
25.4k
  dim1 = isl_space_copy(exp->dim);
2500
25.4k
  isl_reordering_free(exp);
2501
25.4k
  return dim1;
2502
0
error:
2503
0
  isl_space_free(dim1);
2504
0
  isl_space_free(dim2);
2505
0
  return NULL;
2506
25.4k
}
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
67.1k
{
2514
67.1k
  if (!model)
2515
0
    goto error;
2516
67.1k
2517
67.1k
  space = isl_space_from_domain(space);
2518
67.1k
  space = isl_space_add_dims(space, isl_dim_out,
2519
67.1k
            isl_space_dim(model, isl_dim_out));
2520
67.1k
  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
67.1k
  if (!space)
2524
0
    goto error;
2525
67.1k
  
if (67.1k
model->nested[1]67.1k
) {
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
67.1k
  isl_space_free(model);
2539
67.1k
  return space;
2540
0
error:
2541
0
  isl_space_free(model);
2542
0
  isl_space_free(space);
2543
0
  return NULL;
2544
67.1k
}
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.66M
{
2553
1.66M
  int cmp;
2554
1.66M
  isl_space *nested1, *nested2;
2555
1.66M
2556
1.66M
  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.66M
2560
1.66M
  cmp = isl_id_cmp(tuple_id(space1, type), tuple_id(space2, type));
2561
1.66M
  if (cmp != 0)
2562
0
    return cmp;
2563
1.66M
2564
1.66M
  nested1 = nested(space1, type);
2565
1.66M
  nested2 = nested(space2, type);
2566
1.66M
  if (!nested1 != !nested2)
2567
0
    return !nested1 - !nested2;
2568
1.66M
2569
1.66M
  
if (1.66M
nested11.66M
)
2570
183k
    return isl_space_cmp(nested1, nested2);
2571
1.47M
2572
1.47M
  return 0;
2573
1.47M
}
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.84M
{
2581
1.84M
  int i;
2582
1.84M
  int cmp;
2583
1.84M
2584
1.84M
  if (space1 == space2)
2585
1.28M
    return 0;
2586
553k
  
if (553k
!space1553k
)
2587
0
    return -1;
2588
553k
  
if (553k
!space2553k
)
2589
0
    return 1;
2590
553k
2591
553k
  cmp = isl_space_cmp_type(space1, space2, isl_dim_param);
2592
553k
  if (cmp != 0)
2593
0
    return cmp;
2594
553k
  cmp = isl_space_cmp_type(space1, space2, isl_dim_in);
2595
553k
  if (cmp != 0)
2596
0
    return cmp;
2597
553k
  cmp = isl_space_cmp_type(space1, space2, isl_dim_out);
2598
553k
  if (cmp != 0)
2599
0
    return cmp;
2600
553k
2601
553k
  
if (553k
!space1->ids && 553k
!space2->ids37.8k
)
2602
37.6k
    return 0;
2603
515k
2604
3.72M
  
for (i = 0; 515k
i < n(space1, isl_dim_param)3.72M
;
++i3.21M
) {
2605
3.21M
    cmp = isl_id_cmp(get_id(space1, isl_dim_param, i),
2606
3.21M
         get_id(space2, isl_dim_param, i));
2607
3.21M
    if (cmp != 0)
2608
0
      return cmp;
2609
3.21M
  }
2610
515k
2611
515k
  return 0;
2612
1.84M
}