Coverage Report

Created: 2019-04-21 11:35

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/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
681k
{
23
681k
  return dim ? dim->ctx : NULL;
24
681k
}
25
26
__isl_give isl_space *isl_space_alloc(isl_ctx *ctx,
27
      unsigned nparam, unsigned n_in, unsigned n_out)
28
1.90M
{
29
1.90M
  isl_space *dim;
30
1.90M
31
1.90M
  dim = isl_alloc_type(ctx, struct isl_space);
32
1.90M
  if (!dim)
33
4
    return NULL;
34
1.90M
35
1.90M
  dim->ctx = ctx;
36
1.90M
  isl_ctx_ref(ctx);
37
1.90M
  dim->ref = 1;
38
1.90M
  dim->nparam = nparam;
39
1.90M
  dim->n_in = n_in;
40
1.90M
  dim->n_out = n_out;
41
1.90M
42
1.90M
  dim->tuple_id[0] = NULL;
43
1.90M
  dim->tuple_id[1] = NULL;
44
1.90M
45
1.90M
  dim->nested[0] = NULL;
46
1.90M
  dim->nested[1] = NULL;
47
1.90M
48
1.90M
  dim->n_id = 0;
49
1.90M
  dim->ids = NULL;
50
1.90M
51
1.90M
  return dim;
52
1.90M
}
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
547k
{
59
547k
  space = isl_space_cow(space);
60
547k
  if (!space)
61
0
    return NULL;
62
547k
  space = isl_space_set_tuple_id(space, isl_dim_in, &isl_id_none);
63
547k
  return space;
64
547k
}
65
66
/* Is the space that of a set?
67
 */
68
isl_bool isl_space_is_set(__isl_keep isl_space *space)
69
713k
{
70
713k
  if (!space)
71
0
    return isl_bool_error;
72
713k
  if (space->n_in != 0 || 
space->nested[0]623k
)
73
92.6k
    return isl_bool_false;
74
621k
  if (space->tuple_id[0] != &isl_id_none)
75
6.24k
    return isl_bool_false;
76
615k
  return isl_bool_true;
77
615k
}
78
79
/* Is the given space that of a map?
80
 */
81
isl_bool isl_space_is_map(__isl_keep isl_space *space)
82
9.48k
{
83
9.48k
  if (!space)
84
0
    return isl_bool_error;
85
9.48k
  return space->tuple_id[0] != &isl_id_none &&
86
9.48k
    
space->tuple_id[1] != &isl_id_none220
;
87
9.48k
}
88
89
__isl_give isl_space *isl_space_set_alloc(isl_ctx *ctx,
90
      unsigned nparam, unsigned dim)
91
182k
{
92
182k
  isl_space *space;
93
182k
  space = isl_space_alloc(ctx, nparam, 0, dim);
94
182k
  space = mark_as_set(space);
95
182k
  return space;
96
182k
}
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
77.2k
{
103
77.2k
  if (!space)
104
0
    return NULL;
105
77.2k
  space = isl_space_set_tuple_id(space, isl_dim_in, &isl_id_none);
106
77.2k
  space = isl_space_set_tuple_id(space, isl_dim_out, &isl_id_none);
107
77.2k
  return space;
108
77.2k
}
109
110
/* Is the space that of a parameter domain?
111
 */
112
isl_bool isl_space_is_params(__isl_keep isl_space *space)
113
262k
{
114
262k
  if (!space)
115
9
    return isl_bool_error;
116
262k
  if (space->n_in != 0 || 
space->nested[0]214k
||
117
262k
      
space->n_out != 0213k
||
space->nested[1]161k
)
118
101k
    return isl_bool_false;
119
161k
  if (space->tuple_id[0] != &isl_id_none)
120
10.2k
    return isl_bool_false;
121
151k
  if (space->tuple_id[1] != &isl_id_none)
122
12.9k
    return isl_bool_false;
123
138k
  return isl_bool_true;
124
138k
}
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
13.7k
{
130
13.7k
  isl_space *space;
131
13.7k
  space = isl_space_alloc(ctx, nparam, 0, 0);
132
13.7k
  space = mark_as_params(space);
133
13.7k
  return space;
134
13.7k
}
135
136
static unsigned global_pos(__isl_keep isl_space *dim,
137
         enum isl_dim_type type, unsigned pos)
138
26.7M
{
139
26.7M
  struct isl_ctx *ctx = dim->ctx;
140
26.7M
141
26.7M
  switch (type) {
142
26.7M
  case isl_dim_param:
143
19.6M
    isl_assert(ctx, pos < dim->nparam,
144
19.6M
          return isl_space_dim(dim, isl_dim_all));
145
19.6M
    return pos;
146
19.6M
  case isl_dim_in:
147
2.65M
    isl_assert(ctx, pos < dim->n_in,
148
2.65M
          return isl_space_dim(dim, isl_dim_all));
149
2.65M
    return pos + dim->nparam;
150
4.43M
  case isl_dim_out:
151
4.43M
    isl_assert(ctx, pos < dim->n_out,
152
4.43M
          return isl_space_dim(dim, isl_dim_all));
153
4.43M
    return pos + dim->nparam + dim->n_in;
154
4.43M
  default:
155
0
    isl_assert(ctx, 0, return isl_space_dim(dim, isl_dim_all));
156
26.7M
  }
157
26.7M
  
return isl_space_dim(dim, isl_dim_all)0
;
158
26.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
884k
{
164
884k
  isl_id **ids;
165
884k
  int i;
166
884k
167
884k
  if (isl_space_dim(dim, isl_dim_all) <= dim->n_id)
168
193k
    return dim;
169
691k
170
691k
  if (!dim->ids) {
171
691k
    dim->ids = isl_calloc_array(dim->ctx,
172
691k
        isl_id *, isl_space_dim(dim, isl_dim_all));
173
691k
    if (!dim->ids)
174
0
      goto error;
175
161
  } else {
176
161
    ids = isl_realloc_array(dim->ctx, dim->ids,
177
161
        isl_id *, isl_space_dim(dim, isl_dim_all));
178
161
    if (!ids)
179
0
      goto error;
180
161
    dim->ids = ids;
181
828
    for (i = dim->n_id; i < isl_space_dim(dim, isl_dim_all); 
++i667
)
182
667
      dim->ids[i] = NULL;
183
161
  }
184
691k
185
691k
  dim->n_id = isl_space_dim(dim, isl_dim_all);
186
691k
187
691k
  return dim;
188
0
error:
189
0
  isl_space_free(dim);
190
0
  return NULL;
191
691k
}
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
3.21M
{
196
3.21M
  dim = isl_space_cow(dim);
197
3.21M
198
3.21M
  if (!dim)
199
0
    goto error;
200
3.21M
201
3.21M
  pos = global_pos(dim, type, pos);
202
3.21M
  if (pos == isl_space_dim(dim, isl_dim_all))
203
0
    goto error;
204
3.21M
205
3.21M
  if (pos >= dim->n_id) {
206
691k
    if (!id)
207
0
      return dim;
208
691k
    dim = extend_ids(dim);
209
691k
    if (!dim)
210
0
      goto error;
211
3.21M
  }
212
3.21M
213
3.21M
  dim->ids[pos] = id;
214
3.21M
215
3.21M
  return dim;
216
0
error:
217
0
  isl_id_free(id);
218
0
  isl_space_free(dim);
219
0
  return NULL;
220
3.21M
}
221
222
static __isl_keep isl_id *get_id(__isl_keep isl_space *dim,
223
         enum isl_dim_type type, unsigned pos)
224
23.4M
{
225
23.4M
  if (!dim)
226
0
    return NULL;
227
23.4M
228
23.4M
  pos = global_pos(dim, type, pos);
229
23.4M
  if (pos == isl_space_dim(dim, isl_dim_all))
230
0
    return NULL;
231
23.4M
  if (pos >= dim->n_id)
232
968k
    return NULL;
233
22.5M
  return dim->ids[pos];
234
22.5M
}
235
236
static unsigned offset(__isl_keep isl_space *dim, enum isl_dim_type type)
237
683k
{
238
683k
  switch (type) {
239
683k
  
case isl_dim_param: return 0229k
;
240
683k
  
case isl_dim_in: return dim->nparam155k
;
241
683k
  
case isl_dim_out: return dim->nparam + dim->n_in298k
;
242
683k
  
default: return 00
;
243
683k
  }
244
683k
}
245
246
static unsigned n(__isl_keep isl_space *dim, enum isl_dim_type type)
247
116M
{
248
116M
  switch (type) {
249
116M
  
case isl_dim_param: return dim->nparam23.9M
;
250
116M
  
case isl_dim_in: return dim->n_in13.3M
;
251
116M
  
case isl_dim_out: return dim->n_out15.3M
;
252
116M
  
case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out63.8M
;
253
116M
  
default: return 00
;
254
116M
  }
255
116M
}
256
257
unsigned isl_space_dim(__isl_keep isl_space *dim, enum isl_dim_type type)
258
84.1M
{
259
84.1M
  if (!dim)
260
1
    return 0;
261
84.1M
  return n(dim, type);
262
84.1M
}
263
264
unsigned isl_space_offset(__isl_keep isl_space *dim, enum isl_dim_type type)
265
612k
{
266
612k
  if (!dim)
267
0
    return 0;
268
612k
  return offset(dim, type);
269
612k
}
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
2.59M
{
275
2.59M
  int i;
276
2.59M
  isl_id *id;
277
2.59M
278
2.59M
  if (!dst)
279
0
    return NULL;
280
2.59M
281
7.07M
  
for (i = 0; 2.59M
i < n(src, src_type);
++i4.48M
) {
282
4.48M
    id = get_id(src, src_type, i);
283
4.48M
    if (!id)
284
2.22M
      continue;
285
2.26M
    dst = set_id(dst, dst_type, offset + i, isl_id_copy(id));
286
2.26M
    if (!dst)
287
0
      return NULL;
288
2.26M
  }
289
2.59M
  return dst;
290
2.59M
}
291
292
__isl_take isl_space *isl_space_dup(__isl_keep isl_space *dim)
293
1.44M
{
294
1.44M
  isl_space *dup;
295
1.44M
  if (!dim)
296
0
    return NULL;
297
1.44M
  dup = isl_space_alloc(dim->ctx, dim->nparam, dim->n_in, dim->n_out);
298
1.44M
  if (!dup)
299
4
    return NULL;
300
1.44M
  if (dim->tuple_id[0] &&
301
1.44M
      
!(dup->tuple_id[0] = isl_id_copy(dim->tuple_id[0]))880k
)
302
0
    goto error;
303
1.44M
  if (dim->tuple_id[1] &&
304
1.44M
      
!(dup->tuple_id[1] = isl_id_copy(dim->tuple_id[1]))310k
)
305
0
    goto error;
306
1.44M
  if (dim->nested[0] && 
!(dup->nested[0] = isl_space_copy(dim->nested[0]))214k
)
307
0
    goto error;
308
1.44M
  if (dim->nested[1] && 
!(dup->nested[1] = isl_space_copy(dim->nested[1]))375k
)
309
0
    goto error;
310
1.44M
  if (!dim->ids)
311
831k
    return dup;
312
617k
  dup = copy_ids(dup, isl_dim_param, 0, dim, isl_dim_param);
313
617k
  dup = copy_ids(dup, isl_dim_in, 0, dim, isl_dim_in);
314
617k
  dup = copy_ids(dup, isl_dim_out, 0, dim, isl_dim_out);
315
617k
  return dup;
316
0
error:
317
0
  isl_space_free(dup);
318
0
  return NULL;
319
617k
}
320
321
__isl_give isl_space *isl_space_cow(__isl_take isl_space *dim)
322
7.64M
{
323
7.64M
  if (!dim)
324
3
    return NULL;
325
7.64M
326
7.64M
  if (dim->ref == 1)
327
6.20M
    return dim;
328
1.44M
  dim->ref--;
329
1.44M
  return isl_space_dup(dim);
330
1.44M
}
331
332
__isl_give isl_space *isl_space_copy(__isl_keep isl_space *dim)
333
6.01M
{
334
6.01M
  if (!dim)
335
2.99k
    return NULL;
336
6.01M
337
6.01M
  dim->ref++;
338
6.01M
  return dim;
339
6.01M
}
340
341
__isl_null isl_space *isl_space_free(__isl_take isl_space *space)
342
10.7M
{
343
10.7M
  int i;
344
10.7M
345
10.7M
  if (!space)
346
4.29M
    return NULL;
347
6.46M
348
6.46M
  if (--space->ref > 0)
349
4.56M
    return NULL;
350
1.90M
351
1.90M
  isl_id_free(space->tuple_id[0]);
352
1.90M
  isl_id_free(space->tuple_id[1]);
353
1.90M
354
1.90M
  isl_space_free(space->nested[0]);
355
1.90M
  isl_space_free(space->nested[1]);
356
1.90M
357
4.75M
  for (i = 0; i < space->n_id; 
++i2.85M
)
358
2.85M
    isl_id_free(space->ids[i]);
359
1.90M
  free(space->ids);
360
1.90M
  isl_ctx_deref(space->ctx);
361
1.90M
  
362
1.90M
  free(space);
363
1.90M
364
1.90M
  return NULL;
365
1.90M
}
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
24.5k
{
374
24.5k
  char *p;
375
24.5k
  long dummy;
376
24.5k
377
24.5k
  dummy = strtol(s, &p, 0);
378
24.5k
  if (p != s)
379
24.5k
    
isl_die0
(ctx, isl_error_invalid, "name looks like a number",
380
24.5k
      return 0);
381
24.5k
382
24.5k
  return 1;
383
24.5k
}
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
37.0k
{
390
37.0k
  if (!space)
391
0
    return 0;
392
37.0k
  if (isl_space_is_params(space))
393
37.0k
    
isl_die0
(space->ctx, isl_error_invalid,
394
37.0k
      "parameter spaces don't have tuple ids", return 0);
395
37.0k
  if (isl_space_is_set(space) && 
type != isl_dim_set10.9k
)
396
37.0k
    
isl_die0
(space->ctx, isl_error_invalid,
397
37.0k
      "set spaces can only have a set id", return 0);
398
37.0k
  if (type != isl_dim_in && 
type != isl_dim_out36.9k
)
399
37.0k
    
isl_die0
(space->ctx, isl_error_invalid,
400
37.0k
      "only input, output and set tuples can have ids",
401
37.0k
      return 0);
402
37.0k
403
37.0k
  return 1;
404
37.0k
}
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
36.3k
{
411
36.3k
  if (!space_can_have_id(dim, type))
412
0
    return isl_bool_error;
413
36.3k
  return dim->tuple_id[type - isl_dim_in] != NULL;
414
36.3k
}
415
416
__isl_give isl_id *isl_space_get_tuple_id(__isl_keep isl_space *dim,
417
  enum isl_dim_type type)
418
12.4k
{
419
12.4k
  int has_id;
420
12.4k
421
12.4k
  if (!dim)
422
0
    return NULL;
423
12.4k
  has_id = isl_space_has_tuple_id(dim, type);
424
12.4k
  if (has_id < 0)
425
0
    return NULL;
426
12.4k
  if (!has_id)
427
12.4k
    
isl_die0
(dim->ctx, isl_error_invalid,
428
12.4k
      "tuple has no id", return NULL);
429
12.4k
  return isl_id_copy(dim->tuple_id[type - isl_dim_in]);
430
12.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
728k
{
435
728k
  dim = isl_space_cow(dim);
436
728k
  if (!dim || !id)
437
0
    goto error;
438
728k
  if (type != isl_dim_in && 
type != isl_dim_out101k
)
439
728k
    
isl_die0
(dim->ctx, isl_error_invalid,
440
728k
      "only input, output and set tuples can have names",
441
728k
      goto error);
442
728k
443
728k
  isl_id_free(dim->tuple_id[type - isl_dim_in]);
444
728k
  dim->tuple_id[type - isl_dim_in] = id;
445
728k
446
728k
  return dim;
447
0
error:
448
0
  isl_id_free(id);
449
0
  isl_space_free(dim);
450
0
  return NULL;
451
728k
}
452
453
__isl_give isl_space *isl_space_reset_tuple_id(__isl_take isl_space *dim,
454
  enum isl_dim_type type)
455
2.01k
{
456
2.01k
  dim = isl_space_cow(dim);
457
2.01k
  if (!dim)
458
0
    return NULL;
459
2.01k
  if (type != isl_dim_in && type != isl_dim_out)
460
2.01k
    
isl_die0
(dim->ctx, isl_error_invalid,
461
2.01k
      "only input, output and set tuples can have names",
462
2.01k
      goto error);
463
2.01k
464
2.01k
  isl_id_free(dim->tuple_id[type - isl_dim_in]);
465
2.01k
  dim->tuple_id[type - isl_dim_in] = NULL;
466
2.01k
467
2.01k
  return dim;
468
0
error:
469
0
  isl_space_free(dim);
470
0
  return NULL;
471
2.01k
}
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
39.3k
{
481
39.3k
  space = isl_space_cow(space);
482
39.3k
  if (!space || !id)
483
0
    goto error;
484
39.3k
485
39.3k
  if (type == isl_dim_param) {
486
30.3k
    int i;
487
30.3k
488
91.1k
    for (i = 0; i < 2; 
++i60.7k
) {
489
60.7k
      if (!space->nested[i])
490
60.7k
        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
30.3k
  }
498
39.3k
499
39.3k
  isl_id_free(get_id(space, type, pos));
500
39.3k
  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
39.3k
}
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 (type == isl_dim_param) {
520
0
    int i;
521
0
522
0
    for (i = 0; i < 2; ++i) {
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
249
{
543
249
  if (!dim)
544
0
    return isl_bool_error;
545
249
  return get_id(dim, type, pos) != NULL;
546
249
}
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
561k
{
551
561k
  if (!dim)
552
0
    return NULL;
553
561k
  if (!get_id(dim, type, pos))
554
561k
    
isl_die0
(dim->ctx, isl_error_invalid,
555
561k
      "dim has no id", return NULL);
556
561k
  return isl_id_copy(get_id(dim, type, pos));
557
561k
}
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
18.9k
{
562
18.9k
  isl_id *id;
563
18.9k
564
18.9k
  if (!dim)
565
0
    return NULL;
566
18.9k
567
18.9k
  if (!s)
568
0
    return isl_space_reset_tuple_id(dim, type);
569
18.9k
570
18.9k
  if (!name_ok(dim->ctx, s))
571
0
    goto error;
572
18.9k
573
18.9k
  id = isl_id_alloc(dim->ctx, s, NULL);
574
18.9k
  return isl_space_set_tuple_id(dim, type, id);
575
0
error:
576
0
  isl_space_free(dim);
577
0
  return NULL;
578
18.9k
}
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
710
{
585
710
  isl_id *id;
586
710
587
710
  if (!space_can_have_id(space, type))
588
0
    return isl_bool_error;
589
710
  id = space->tuple_id[type - isl_dim_in];
590
710
  return id && 
id->name80
;
591
710
}
592
593
__isl_keep const char *isl_space_get_tuple_name(__isl_keep isl_space *dim,
594
   enum isl_dim_type type)
595
3.57k
{
596
3.57k
  isl_id *id;
597
3.57k
  if (!dim)
598
0
    return NULL;
599
3.57k
  if (type != isl_dim_in && 
type != isl_dim_out2.13k
)
600
0
    return NULL;
601
3.57k
  id = dim->tuple_id[type - isl_dim_in];
602
3.57k
  return id ? 
id->name3.05k
: NULL;
603
3.57k
}
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.59k
{
609
5.59k
  isl_id *id;
610
5.59k
611
5.59k
  if (!dim)
612
0
    return NULL;
613
5.59k
  if (!s)
614
0
    return isl_space_reset_dim_id(dim, type, pos);
615
5.59k
  if (!name_ok(dim->ctx, s))
616
0
    goto error;
617
5.59k
  id = isl_id_alloc(dim->ctx, s, NULL);
618
5.59k
  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.59k
}
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
2.57k
{
629
2.57k
  isl_id *id;
630
2.57k
631
2.57k
  if (!space)
632
0
    return isl_bool_error;
633
2.57k
  id = get_id(space, type, pos);
634
2.57k
  return id && 
id->name772
;
635
2.57k
}
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
164k
{
640
164k
  isl_id *id = get_id(dim, type, pos);
641
164k
  return id ? 
id->name146k
: NULL;
642
164k
}
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
148
{
647
148
  int i;
648
148
  int offset;
649
148
  int n;
650
148
651
148
  if (!dim || !id)
652
0
    return -1;
653
148
654
148
  offset = isl_space_offset(dim, type);
655
148
  n = isl_space_dim(dim, type);
656
176
  for (i = 0; i < n && 
offset + i < dim->n_id175
;
++i28
)
657
175
    if (dim->ids[offset + i] == id)
658
147
      return i;
659
148
660
148
  
return -11
;
661
148
}
662
663
int isl_space_find_dim_by_name(__isl_keep isl_space *space,
664
  enum isl_dim_type type, const char *name)
665
272
{
666
272
  int i;
667
272
  int offset;
668
272
  int n;
669
272
670
272
  if (!space || !name)
671
0
    return -1;
672
272
673
272
  offset = isl_space_offset(space, type);
674
272
  n = isl_space_dim(space, type);
675
390
  for (i = 0; i < n && 
offset + i < space->n_id118
;
++i118
) {
676
118
    isl_id *id = get_id(space, type, i);
677
118
    if (id && id->name && !strcmp(id->name, name))
678
0
      return i;
679
118
  }
680
272
681
272
  return -1;
682
272
}
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 && i < space->n_id; ++i) {
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; i < 2; ++i) {
714
0
    if (!space->tuple_id[i])
715
0
      continue;
716
0
    if (!isl_id_get_user(space->tuple_id[i]))
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; i < 2; ++i) {
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
16.5M
{
746
16.5M
  if (!dim)
747
0
    return NULL;
748
16.5M
  if (type == isl_dim_in)
749
5.18M
    return dim->tuple_id[0];
750
11.3M
  if (type == isl_dim_out)
751
4.67M
    return dim->tuple_id[1];
752
6.65M
  return NULL;
753
6.65M
}
754
755
static __isl_keep isl_space *nested(__isl_keep isl_space *dim,
756
  enum isl_dim_type type)
757
15.4M
{
758
15.4M
  if (!dim)
759
0
    return NULL;
760
15.4M
  if (type == isl_dim_in)
761
4.48M
    return dim->nested[0];
762
10.9M
  if (type == isl_dim_out)
763
4.29M
    return dim->nested[1];
764
6.65M
  return NULL;
765
6.65M
}
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
2.38M
{
772
2.38M
  if (!space1 || !space2)
773
0
    return isl_bool_error;
774
2.38M
  if (space1 == space2)
775
643k
    return isl_bool_true;
776
1.74M
  return isl_space_tuple_is_equal(space1, isl_dim_in,
777
1.74M
          space2, isl_dim_in) &&
778
1.74M
         isl_space_tuple_is_equal(space1, isl_dim_out,
779
1.50M
          space2, isl_dim_out);
780
1.74M
}
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
7.81M
{
798
7.81M
  isl_id *id1, *id2;
799
7.81M
  isl_space *nested1, *nested2;
800
7.81M
801
7.81M
  if (!space1 || !space2)
802
0
    return isl_bool_error;
803
7.81M
804
7.81M
  if (space1 == space2 && 
type1 == type2749k
)
805
2.23k
    return isl_bool_true;
806
7.81M
807
7.81M
  if (n(space1, type1) != n(space2, type2))
808
860k
    return isl_bool_false;
809
6.94M
  id1 = tuple_id(space1, type1);
810
6.94M
  id2 = tuple_id(space2, type2);
811
6.94M
  if (!id1 ^ !id2)
812
232k
    return isl_bool_false;
813
6.71M
  if (id1 && 
id1 != id21.60M
)
814
52.7k
    return isl_bool_false;
815
6.66M
  nested1 = nested(space1, type1);
816
6.66M
  nested2 = nested(space2, type2);
817
6.66M
  if (!nested1 ^ !nested2)
818
22.7k
    return isl_bool_false;
819
6.64M
  if (nested1 && 
!isl_space_has_equal_tuples(nested1, nested2)983k
)
820
6.43k
    return isl_bool_false;
821
6.63M
  return isl_bool_true;
822
6.63M
}
823
824
static isl_bool match(__isl_keep isl_space *space1, enum isl_dim_type type1,
825
  __isl_keep isl_space *space2, enum isl_dim_type type2)
826
4.24M
{
827
4.24M
  int i;
828
4.24M
829
4.24M
  if (space1 == space2 && 
type1 == type21.11M
)
830
375k
    return isl_bool_true;
831
3.86M
832
3.86M
  if (!isl_space_tuple_is_equal(space1, type1, space2, type2))
833
803k
    return isl_bool_false;
834
3.06M
835
3.06M
  if (!space1->ids && 
!space2->ids1.84M
)
836
1.82M
    return isl_bool_true;
837
1.23M
838
5.66M
  
for (i = 0; 1.23M
i < n(space1, type1);
++i4.42M
) {
839
4.44M
    if (get_id(space1, type1, i) != get_id(space2, type2, i))
840
15.5k
      return isl_bool_false;
841
4.44M
  }
842
1.23M
  
return isl_bool_true1.22M
;
843
1.23M
}
844
845
/* Do "space1" and "space2" have the same parameters?
846
 */
847
isl_bool isl_space_has_equal_params(__isl_keep isl_space *space1,
848
  __isl_keep isl_space *space2)
849
3.35M
{
850
3.35M
  if (!space1 || !space2)
851
0
    return isl_bool_error;
852
3.35M
853
3.35M
  return match(space1, isl_dim_param, space2, isl_dim_param);
854
3.35M
}
855
856
/* Do "space1" and "space2" have the same identifiers for all
857
 * the tuple variables?
858
 */
859
isl_bool isl_space_has_equal_ids(__isl_keep isl_space *space1,
860
  __isl_keep isl_space *space2)
861
69.8k
{
862
69.8k
  isl_bool equal;
863
69.8k
864
69.8k
  if (!space1 || !space2)
865
0
    return isl_bool_error;
866
69.8k
867
69.8k
  equal = match(space1, isl_dim_in, space2, isl_dim_in);
868
69.8k
  if (equal < 0 || !equal)
869
132
    return equal;
870
69.7k
  return match(space1, isl_dim_out, space2, isl_dim_out);
871
69.7k
}
872
873
isl_bool isl_space_match(__isl_keep isl_space *space1, enum isl_dim_type type1,
874
  __isl_keep isl_space *space2, enum isl_dim_type type2)
875
0
{
876
0
  if (!space1 || !space2)
877
0
    return isl_bool_error;
878
0
879
0
  return match(space1, type1, space2, type2);
880
0
}
881
882
static void get_ids(__isl_keep isl_space *dim, enum isl_dim_type type,
883
  unsigned first, unsigned n, __isl_keep isl_id **ids)
884
1.30M
{
885
1.30M
  int i;
886
1.30M
887
3.31M
  for (i = 0; i < n ; 
++i2.01M
)
888
2.01M
    ids[i] = get_id(dim, type, first + i);
889
1.30M
}
890
891
static __isl_give isl_space *space_extend(__isl_take isl_space *space,
892
      unsigned nparam, unsigned n_in, unsigned n_out)
893
341k
{
894
341k
  isl_id **ids = NULL;
895
341k
896
341k
  if (!space)
897
0
    return NULL;
898
341k
  if (space->nparam == nparam &&
899
341k
      
space->n_in == n_in291k
&&
space->n_out == n_out286k
)
900
623
    return space;
901
341k
902
341k
  isl_assert(space->ctx, space->nparam <= nparam, goto error);
903
341k
  isl_assert(space->ctx, space->n_in <= n_in, goto error);
904
341k
  isl_assert(space->ctx, space->n_out <= n_out, goto error);
905
341k
906
341k
  space = isl_space_cow(space);
907
341k
  if (!space)
908
0
    goto error;
909
341k
910
341k
  if (space->ids) {
911
169k
    unsigned n;
912
169k
    n = nparam + n_in + n_out;
913
169k
    if (n < nparam || n < n_in || n < n_out)
914
169k
      
isl_die0
(isl_space_get_ctx(space), isl_error_invalid,
915
169k
        "overflow in total number of dimensions",
916
169k
        goto error);
917
169k
    ids = isl_calloc_array(space->ctx, isl_id *, n);
918
169k
    if (!ids)
919
0
      goto error;
920
169k
    get_ids(space, isl_dim_param, 0, space->nparam, ids);
921
169k
    get_ids(space, isl_dim_in, 0, space->n_in, ids + nparam);
922
169k
    get_ids(space, isl_dim_out, 0, space->n_out,
923
169k
      ids + nparam + n_in);
924
169k
    free(space->ids);
925
169k
    space->ids = ids;
926
169k
    space->n_id = nparam + n_in + n_out;
927
169k
  }
928
341k
  space->nparam = nparam;
929
341k
  space->n_in = n_in;
930
341k
  space->n_out = n_out;
931
341k
932
341k
  return space;
933
0
error:
934
0
  free(ids);
935
0
  isl_space_free(space);
936
0
  return NULL;
937
341k
}
938
939
__isl_give isl_space *isl_space_extend(__isl_take isl_space *space,
940
  unsigned nparam, unsigned n_in, unsigned n_out)
941
0
{
942
0
  return space_extend(space, nparam, n_in, n_out);
943
0
}
944
945
__isl_give isl_space *isl_space_add_dims(__isl_take isl_space *space,
946
  enum isl_dim_type type, unsigned n)
947
341k
{
948
341k
  space = isl_space_reset(space, type);
949
341k
  if (!space)
950
3
    return NULL;
951
341k
  switch (type) {
952
341k
  case isl_dim_param:
953
50.5k
    space = space_extend(space,
954
50.5k
        space->nparam + n, space->n_in, space->n_out);
955
50.5k
    if (space && space->nested[0] &&
956
50.5k
        !(space->nested[0] = isl_space_add_dims(space->nested[0],
957
6
                isl_dim_param, n)))
958
0
      goto error;
959
50.5k
    if (space && space->nested[1] &&
960
50.5k
        !(space->nested[1] = isl_space_add_dims(space->nested[1],
961
169
                isl_dim_param, n)))
962
0
      goto error;
963
50.5k
    return space;
964
50.5k
  case isl_dim_in:
965
5.44k
    return space_extend(space,
966
5.44k
        space->nparam, space->n_in + n, space->n_out);
967
286k
  case isl_dim_out:
968
286k
    return space_extend(space,
969
286k
        space->nparam, space->n_in, space->n_out + n);
970
50.5k
  default:
971
0
    isl_die(space->ctx, isl_error_invalid,
972
341k
      "cannot add dimensions of specified type", goto error);
973
341k
  }
974
341k
error:
975
0
  isl_space_free(space);
976
0
  return NULL;
977
341k
}
978
979
/* Add a parameter with identifier "id" to "space", provided
980
 * it does not already appear in "space".
981
 */
982
__isl_give isl_space *isl_space_add_param_id(__isl_take isl_space *space,
983
  __isl_take isl_id *id)
984
1
{
985
1
  int pos;
986
1
987
1
  if (!space || !id)
988
0
    goto error;
989
1
990
1
  if (isl_space_find_dim_by_id(space, isl_dim_param, id) >= 0) {
991
0
    isl_id_free(id);
992
0
    return space;
993
0
  }
994
1
995
1
  pos = isl_space_dim(space, isl_dim_param);
996
1
  space = isl_space_add_dims(space, isl_dim_param, 1);
997
1
  space = isl_space_set_dim_id(space, isl_dim_param, pos, id);
998
1
999
1
  return space;
1000
0
error:
1001
0
  isl_space_free(space);
1002
0
  isl_id_free(id);
1003
0
  return NULL;
1004
1
}
1005
1006
static int valid_dim_type(enum isl_dim_type type)
1007
702k
{
1008
702k
  switch (type) {
1009
702k
  case isl_dim_param:
1010
702k
  case isl_dim_in:
1011
702k
  case isl_dim_out:
1012
702k
    return 1;
1013
702k
  default:
1014
0
    return 0;
1015
702k
  }
1016
702k
}
1017
1018
/* Insert "n" dimensions of type "type" at position "pos".
1019
 * If we are inserting parameters, then they are also inserted in
1020
 * any nested spaces.
1021
 */
1022
__isl_give isl_space *isl_space_insert_dims(__isl_take isl_space *space,
1023
  enum isl_dim_type type, unsigned pos, unsigned n)
1024
66.4k
{
1025
66.4k
  isl_ctx *ctx;
1026
66.4k
  isl_id **ids = NULL;
1027
66.4k
  unsigned total;
1028
66.4k
1029
66.4k
  if (!space)
1030
0
    return NULL;
1031
66.4k
  if (n == 0)
1032
6
    return isl_space_reset(space, type);
1033
66.4k
1034
66.4k
  ctx = isl_space_get_ctx(space);
1035
66.4k
  if (!valid_dim_type(type))
1036
66.4k
    
isl_die0
(ctx, isl_error_invalid,
1037
66.4k
      "cannot insert dimensions of specified type",
1038
66.4k
      goto error);
1039
66.4k
1040
66.4k
  total = isl_space_dim(space, isl_dim_all);
1041
66.4k
  if (total + n < total)
1042
66.4k
    
isl_die0
(ctx, isl_error_invalid,
1043
66.4k
      "overflow in total number of dimensions",
1044
66.4k
      return isl_space_free(space));
1045
66.4k
  isl_assert(ctx, pos <= isl_space_dim(space, type), goto error);
1046
66.4k
1047
66.4k
  space = isl_space_cow(space);
1048
66.4k
  if (!space)
1049
0
    return NULL;
1050
66.4k
1051
66.4k
  if (space->ids) {
1052
20.5k
    enum isl_dim_type t, o = isl_dim_param;
1053
20.5k
    int off;
1054
20.5k
    int s[3];
1055
20.5k
    ids = isl_calloc_array(ctx, isl_id *,
1056
20.5k
           space->nparam + space->n_in + space->n_out + n);
1057
20.5k
    if (!ids)
1058
0
      goto error;
1059
20.5k
    off = 0;
1060
20.5k
    s[isl_dim_param - o] = space->nparam;
1061
20.5k
    s[isl_dim_in - o] = space->n_in;
1062
20.5k
    s[isl_dim_out - o] = space->n_out;
1063
82.2k
    for (t = isl_dim_param; t <= isl_dim_out; 
++t61.6k
) {
1064
61.6k
      if (t != type) {
1065
41.1k
        get_ids(space, t, 0, s[t - o], ids + off);
1066
41.1k
        off += s[t - o];
1067
41.1k
      } else {
1068
20.5k
        get_ids(space, t, 0, pos, ids + off);
1069
20.5k
        off += pos + n;
1070
20.5k
        get_ids(space, t, pos, s[t - o] - pos,
1071
20.5k
          ids + off);
1072
20.5k
        off += s[t - o] - pos;
1073
20.5k
      }
1074
61.6k
    }
1075
20.5k
    free(space->ids);
1076
20.5k
    space->ids = ids;
1077
20.5k
    space->n_id = space->nparam + space->n_in + space->n_out + n;
1078
20.5k
  }
1079
66.4k
  switch (type) {
1080
66.4k
  
case isl_dim_param: space->nparam += n; break1.63k
;
1081
66.4k
  
case isl_dim_in: space->n_in += n; break5.10k
;
1082
66.4k
  
case isl_dim_out: space->n_out += n; break59.7k
;
1083
66.4k
  
default: ;0
1084
66.4k
  }
1085
66.4k
  space = isl_space_reset(space, type);
1086
66.4k
1087
66.4k
  if (type == isl_dim_param) {
1088
1.63k
    if (space && space->nested[0] &&
1089
1.63k
        !(space->nested[0] = isl_space_insert_dims(space->nested[0],
1090
0
                isl_dim_param, pos, n)))
1091
0
      goto error;
1092
1.63k
    if (space && space->nested[1] &&
1093
1.63k
        !(space->nested[1] = isl_space_insert_dims(space->nested[1],
1094
1
                isl_dim_param, pos, n)))
1095
0
      goto error;
1096
66.4k
  }
1097
66.4k
1098
66.4k
  return space;
1099
0
error:
1100
0
  isl_space_free(space);
1101
0
  return NULL;
1102
66.4k
}
1103
1104
__isl_give isl_space *isl_space_move_dims(__isl_take isl_space *space,
1105
  enum isl_dim_type dst_type, unsigned dst_pos,
1106
  enum isl_dim_type src_type, unsigned src_pos, unsigned n)
1107
596
{
1108
596
  int i;
1109
596
1110
596
  space = isl_space_reset(space, src_type);
1111
596
  space = isl_space_reset(space, dst_type);
1112
596
  if (!space)
1113
0
    return NULL;
1114
596
  if (n == 0)
1115
250
    return space;
1116
346
1117
346
  isl_assert(space->ctx, src_pos + n <= isl_space_dim(space, src_type),
1118
346
    goto error);
1119
346
1120
346
  if (dst_type == src_type && 
dst_pos == src_pos0
)
1121
0
    return space;
1122
346
1123
346
  isl_assert(space->ctx, dst_type != src_type, goto error);
1124
346
1125
346
  space = isl_space_cow(space);
1126
346
  if (!space)
1127
0
    return NULL;
1128
346
1129
346
  if (space->ids) {
1130
208
    isl_id **ids;
1131
208
    enum isl_dim_type t, o = isl_dim_param;
1132
208
    int off;
1133
208
    int s[3];
1134
208
    ids = isl_calloc_array(space->ctx, isl_id *,
1135
208
         space->nparam + space->n_in + space->n_out);
1136
208
    if (!ids)
1137
0
      goto error;
1138
208
    off = 0;
1139
208
    s[isl_dim_param - o] = space->nparam;
1140
208
    s[isl_dim_in - o] = space->n_in;
1141
208
    s[isl_dim_out - o] = space->n_out;
1142
832
    for (t = isl_dim_param; t <= isl_dim_out; 
++t624
) {
1143
624
      if (t == dst_type) {
1144
208
        get_ids(space, t, 0, dst_pos, ids + off);
1145
208
        off += dst_pos;
1146
208
        get_ids(space, src_type, src_pos, n, ids + off);
1147
208
        off += n;
1148
208
        get_ids(space, t, dst_pos, s[t - o] - dst_pos,
1149
208
            ids + off);
1150
208
        off += s[t - o] - dst_pos;
1151
416
      } else if (t == src_type) {
1152
208
        get_ids(space, t, 0, src_pos, ids + off);
1153
208
        off += src_pos;
1154
208
        get_ids(space, t, src_pos + n,
1155
208
              s[t - o] - src_pos - n, ids + off);
1156
208
        off += s[t - o] - src_pos - n;
1157
208
      } else {
1158
208
        get_ids(space, t, 0, s[t - o], ids + off);
1159
208
        off += s[t - o];
1160
208
      }
1161
624
    }
1162
208
    free(space->ids);
1163
208
    space->ids = ids;
1164
208
    space->n_id = space->nparam + space->n_in + space->n_out;
1165
208
  }
1166
346
1167
346
  switch (dst_type) {
1168
346
  
case isl_dim_param: space->nparam += n; break23
;
1169
346
  
case isl_dim_in: space->n_in += n; break119
;
1170
346
  
case isl_dim_out: space->n_out += n; break204
;
1171
346
  
default: ;0
1172
346
  }
1173
346
1174
346
  switch (src_type) {
1175
346
  
case isl_dim_param: space->nparam -= n; break169
;
1176
346
  
case isl_dim_in: space->n_in -= n; break53
;
1177
346
  
case isl_dim_out: space->n_out -= n; break124
;
1178
346
  
default: ;0
1179
346
  }
1180
346
1181
346
  if (dst_type != isl_dim_param && 
src_type != isl_dim_param323
)
1182
154
    return space;
1183
192
1184
576
  
for (i = 0; 192
i < 2;
++i384
) {
1185
384
    if (!space->nested[i])
1186
367
      continue;
1187
17
    space->nested[i] = isl_space_replace_params(space->nested[i],
1188
17
                   space);
1189
17
    if (!space->nested[i])
1190
0
      goto error;
1191
17
  }
1192
192
1193
192
  return space;
1194
0
error:
1195
0
  isl_space_free(space);
1196
0
  return NULL;
1197
192
}
1198
1199
/* Check that "space1" and "space2" have the same parameters,
1200
 * reporting an error if they do not.
1201
 */
1202
isl_stat isl_space_check_equal_params(__isl_keep isl_space *space1,
1203
  __isl_keep isl_space *space2)
1204
205k
{
1205
205k
  isl_bool equal;
1206
205k
1207
205k
  equal = isl_space_has_equal_params(space1, space2);
1208
205k
  if (equal < 0)
1209
0
    return isl_stat_error;
1210
205k
  if (!equal)
1211
205k
    
isl_die0
(isl_space_get_ctx(space1), isl_error_invalid,
1212
205k
      "parameters need to match", return isl_stat_error);
1213
205k
  return isl_stat_ok;
1214
205k
}
1215
1216
__isl_give isl_space *isl_space_join(__isl_take isl_space *left,
1217
  __isl_take isl_space *right)
1218
169k
{
1219
169k
  isl_space *dim;
1220
169k
1221
169k
  if (isl_space_check_equal_params(left, right) < 0)
1222
0
    goto error;
1223
169k
1224
169k
  isl_assert(left->ctx,
1225
169k
    isl_space_tuple_is_equal(left, isl_dim_out, right, isl_dim_in),
1226
169k
    goto error);
1227
169k
1228
169k
  dim = isl_space_alloc(left->ctx, left->nparam, left->n_in, right->n_out);
1229
169k
  if (!dim)
1230
0
    goto error;
1231
169k
1232
169k
  dim = copy_ids(dim, isl_dim_param, 0, left, isl_dim_param);
1233
169k
  dim = copy_ids(dim, isl_dim_in, 0, left, isl_dim_in);
1234
169k
  dim = copy_ids(dim, isl_dim_out, 0, right, isl_dim_out);
1235
169k
1236
169k
  if (dim && left->tuple_id[0] &&
1237
169k
      
!(dim->tuple_id[0] = isl_id_copy(left->tuple_id[0]))61.2k
)
1238
0
    goto error;
1239
169k
  if (dim && right->tuple_id[1] &&
1240
169k
      
!(dim->tuple_id[1] = isl_id_copy(right->tuple_id[1]))46.9k
)
1241
0
    goto error;
1242
169k
  if (dim && left->nested[0] &&
1243
169k
      
!(dim->nested[0] = isl_space_copy(left->nested[0]))56.6k
)
1244
0
    goto error;
1245
169k
  if (dim && right->nested[1] &&
1246
169k
      
!(dim->nested[1] = isl_space_copy(right->nested[1]))75.2k
)
1247
0
    goto error;
1248
169k
1249
169k
  isl_space_free(left);
1250
169k
  isl_space_free(right);
1251
169k
1252
169k
  return dim;
1253
0
error:
1254
0
  isl_space_free(left);
1255
0
  isl_space_free(right);
1256
0
  return NULL;
1257
169k
}
1258
1259
/* Given two map spaces { A -> C } and { B -> D }, construct the space
1260
 * { [A -> B] -> [C -> D] }.
1261
 * Given two set spaces { A } and { B }, construct the space { [A -> B] }.
1262
 */
1263
__isl_give isl_space *isl_space_product(__isl_take isl_space *left,
1264
  __isl_take isl_space *right)
1265
1.03k
{
1266
1.03k
  isl_space *dom1, *dom2, *nest1, *nest2;
1267
1.03k
  int is_set;
1268
1.03k
1269
1.03k
  if (!left || !right)
1270
0
    goto error;
1271
1.03k
1272
1.03k
  is_set = isl_space_is_set(left);
1273
1.03k
  if (is_set != isl_space_is_set(right))
1274
1.03k
    
isl_die0
(isl_space_get_ctx(left), isl_error_invalid,
1275
1.03k
      "expecting either two set spaces or two map spaces",
1276
1.03k
      goto error);
1277
1.03k
  if (is_set)
1278
238
    return isl_space_range_product(left, right);
1279
797
1280
797
  if (isl_space_check_equal_params(left, right) < 0)
1281
0
    goto error;
1282
797
1283
797
  dom1 = isl_space_domain(isl_space_copy(left));
1284
797
  dom2 = isl_space_domain(isl_space_copy(right));
1285
797
  nest1 = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1286
797
1287
797
  dom1 = isl_space_range(left);
1288
797
  dom2 = isl_space_range(right);
1289
797
  nest2 = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1290
797
1291
797
  return isl_space_join(isl_space_reverse(nest1), nest2);
1292
0
error:
1293
0
  isl_space_free(left);
1294
0
  isl_space_free(right);
1295
0
  return NULL;
1296
797
}
1297
1298
/* Given two spaces { A -> C } and { B -> C }, construct the space
1299
 * { [A -> B] -> C }
1300
 */
1301
__isl_give isl_space *isl_space_domain_product(__isl_take isl_space *left,
1302
  __isl_take isl_space *right)
1303
606
{
1304
606
  isl_space *ran, *dom1, *dom2, *nest;
1305
606
1306
606
  if (isl_space_check_equal_params(left, right) < 0)
1307
0
    goto error;
1308
606
1309
606
  if (!isl_space_tuple_is_equal(left, isl_dim_out, right, isl_dim_out))
1310
606
    
isl_die0
(left->ctx, isl_error_invalid,
1311
606
      "ranges need to match", goto error);
1312
606
1313
606
  ran = isl_space_range(isl_space_copy(left));
1314
606
1315
606
  dom1 = isl_space_domain(left);
1316
606
  dom2 = isl_space_domain(right);
1317
606
  nest = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1318
606
1319
606
  return isl_space_join(isl_space_reverse(nest), ran);
1320
0
error:
1321
0
  isl_space_free(left);
1322
0
  isl_space_free(right);
1323
0
  return NULL;
1324
606
}
1325
1326
__isl_give isl_space *isl_space_range_product(__isl_take isl_space *left,
1327
  __isl_take isl_space *right)
1328
34.5k
{
1329
34.5k
  isl_space *dom, *ran1, *ran2, *nest;
1330
34.5k
1331
34.5k
  if (isl_space_check_equal_params(left, right) < 0)
1332
0
    goto error;
1333
34.5k
1334
34.5k
  if (!isl_space_tuple_is_equal(left, isl_dim_in, right, isl_dim_in))
1335
34.5k
    
isl_die0
(left->ctx, isl_error_invalid,
1336
34.5k
      "domains need to match", goto error);
1337
34.5k
1338
34.5k
  dom = isl_space_domain(isl_space_copy(left));
1339
34.5k
1340
34.5k
  ran1 = isl_space_range(left);
1341
34.5k
  ran2 = isl_space_range(right);
1342
34.5k
  nest = isl_space_wrap(isl_space_join(isl_space_reverse(ran1), ran2));
1343
34.5k
1344
34.5k
  return isl_space_join(isl_space_reverse(dom), nest);
1345
0
error:
1346
0
  isl_space_free(left);
1347
0
  isl_space_free(right);
1348
0
  return NULL;
1349
34.5k
}
1350
1351
/* Given a space of the form [A -> B] -> C, return the space A -> C.
1352
 */
1353
__isl_give isl_space *isl_space_domain_factor_domain(
1354
  __isl_take isl_space *space)
1355
223
{
1356
223
  isl_space *nested;
1357
223
  isl_space *domain;
1358
223
1359
223
  if (!space)
1360
0
    return NULL;
1361
223
  if (!isl_space_domain_is_wrapping(space))
1362
223
    
isl_die0
(isl_space_get_ctx(space), isl_error_invalid,
1363
223
      "domain not a product", return isl_space_free(space));
1364
223
1365
223
  nested = space->nested[0];
1366
223
  domain = isl_space_copy(space);
1367
223
  domain = isl_space_drop_dims(domain, isl_dim_in,
1368
223
          nested->n_in, nested->n_out);
1369
223
  if (!domain)
1370
0
    return isl_space_free(space);
1371
223
  if (nested->tuple_id[0]) {
1372
155
    domain->tuple_id[0] = isl_id_copy(nested->tuple_id[0]);
1373
155
    if (!domain->tuple_id[0])
1374
0
      goto error;
1375
223
  }
1376
223
  if (nested->nested[0]) {
1377
68
    domain->nested[0] = isl_space_copy(nested->nested[0]);
1378
68
    if (!domain->nested[0])
1379
0
      goto error;
1380
223
  }
1381
223
1382
223
  isl_space_free(space);
1383
223
  return domain;
1384
0
error:
1385
0
  isl_space_free(space);
1386
0
  isl_space_free(domain);
1387
0
  return NULL;
1388
223
}
1389
1390
/* Given a space of the form [A -> B] -> C, return the space B -> C.
1391
 */
1392
__isl_give isl_space *isl_space_domain_factor_range(
1393
  __isl_take isl_space *space)
1394
5.72k
{
1395
5.72k
  isl_space *nested;
1396
5.72k
  isl_space *range;
1397
5.72k
1398
5.72k
  if (!space)
1399
0
    return NULL;
1400
5.72k
  if (!isl_space_domain_is_wrapping(space))
1401
5.72k
    
isl_die0
(isl_space_get_ctx(space), isl_error_invalid,
1402
5.72k
      "domain not a product", return isl_space_free(space));
1403
5.72k
1404
5.72k
  nested = space->nested[0];
1405
5.72k
  range = isl_space_copy(space);
1406
5.72k
  range = isl_space_drop_dims(range, isl_dim_in, 0, nested->n_in);
1407
5.72k
  if (!range)
1408
0
    return isl_space_free(space);
1409
5.72k
  if (nested->tuple_id[1]) {
1410
4.13k
    range->tuple_id[0] = isl_id_copy(nested->tuple_id[1]);
1411
4.13k
    if (!range->tuple_id[0])
1412
0
      goto error;
1413
5.72k
  }
1414
5.72k
  if (nested->nested[1]) {
1415
1.57k
    range->nested[0] = isl_space_copy(nested->nested[1]);
1416
1.57k
    if (!range->nested[0])
1417
0
      goto error;
1418
5.72k
  }
1419
5.72k
1420
5.72k
  isl_space_free(space);
1421
5.72k
  return range;
1422
0
error:
1423
0
  isl_space_free(space);
1424
0
  isl_space_free(range);
1425
0
  return NULL;
1426
5.72k
}
1427
1428
/* Internal function that selects the domain of the map that is
1429
 * embedded in either a set space or the range of a map space.
1430
 * In particular, given a space of the form [A -> B], return the space A.
1431
 * Given a space of the form A -> [B -> C], return the space A -> B.
1432
 */
1433
static __isl_give isl_space *range_factor_domain(__isl_take isl_space *space)
1434
645
{
1435
645
  isl_space *nested;
1436
645
  isl_space *domain;
1437
645
1438
645
  if (!space)
1439
0
    return NULL;
1440
645
1441
645
  nested = space->nested[1];
1442
645
  domain = isl_space_copy(space);
1443
645
  domain = isl_space_drop_dims(domain, isl_dim_out,
1444
645
          nested->n_in, nested->n_out);
1445
645
  if (!domain)
1446
0
    return isl_space_free(space);
1447
645
  if (nested->tuple_id[0]) {
1448
457
    domain->tuple_id[1] = isl_id_copy(nested->tuple_id[0]);
1449
457
    if (!domain->tuple_id[1])
1450
0
      goto error;
1451
645
  }
1452
645
  if (nested->nested[0]) {
1453
173
    domain->nested[1] = isl_space_copy(nested->nested[0]);
1454
173
    if (!domain->nested[1])
1455
0
      goto error;
1456
645
  }
1457
645
1458
645
  isl_space_free(space);
1459
645
  return domain;
1460
0
error:
1461
0
  isl_space_free(space);
1462
0
  isl_space_free(domain);
1463
0
  return NULL;
1464
645
}
1465
1466
/* Given a space of the form A -> [B -> C], return the space A -> B.
1467
 */
1468
__isl_give isl_space *isl_space_range_factor_domain(
1469
  __isl_take isl_space *space)
1470
645
{
1471
645
  if (!space)
1472
0
    return NULL;
1473
645
  if (!isl_space_range_is_wrapping(space))
1474
645
    
isl_die0
(isl_space_get_ctx(space), isl_error_invalid,
1475
645
      "range not a product", return isl_space_free(space));
1476
645
1477
645
  return range_factor_domain(space);
1478
645
}
1479
1480
/* Given a space of the form [A -> B], return the space A.
1481
 */
1482
static __isl_give isl_space *set_factor_domain(__isl_take isl_space *space)
1483
0
{
1484
0
  if (!space)
1485
0
    return NULL;
1486
0
  if (!isl_space_is_wrapping(space))
1487
0
    isl_die(isl_space_get_ctx(space), isl_error_invalid,
1488
0
      "not a product", return isl_space_free(space));
1489
0
1490
0
  return range_factor_domain(space);
1491
0
}
1492
1493
/* Given a space of the form [A -> B] -> [C -> D], return the space A -> C.
1494
 * Given a space of the form [A -> B], return the space A.
1495
 */
1496
__isl_give isl_space *isl_space_factor_domain(__isl_take isl_space *space)
1497
36
{
1498
36
  if (!space)
1499
0
    return NULL;
1500
36
  if (isl_space_is_set(space))
1501
0
    return set_factor_domain(space);
1502
36
  space = isl_space_domain_factor_domain(space);
1503
36
  space = isl_space_range_factor_domain(space);
1504
36
  return space;
1505
36
}
1506
1507
/* Internal function that selects the range of the map that is
1508
 * embedded in either a set space or the range of a map space.
1509
 * In particular, given a space of the form [A -> B], return the space B.
1510
 * Given a space of the form A -> [B -> C], return the space A -> C.
1511
 */
1512
static __isl_give isl_space *range_factor_range(__isl_take isl_space *space)
1513
3.55k
{
1514
3.55k
  isl_space *nested;
1515
3.55k
  isl_space *range;
1516
3.55k
1517
3.55k
  if (!space)
1518
0
    return NULL;
1519
3.55k
1520
3.55k
  nested = space->nested[1];
1521
3.55k
  range = isl_space_copy(space);
1522
3.55k
  range = isl_space_drop_dims(range, isl_dim_out, 0, nested->n_in);
1523
3.55k
  if (!range)
1524
0
    return isl_space_free(space);
1525
3.55k
  if (nested->tuple_id[1]) {
1526
210
    range->tuple_id[1] = isl_id_copy(nested->tuple_id[1]);
1527
210
    if (!range->tuple_id[1])
1528
0
      goto error;
1529
3.55k
  }
1530
3.55k
  if (nested->nested[1]) {
1531
3.34k
    range->nested[1] = isl_space_copy(nested->nested[1]);
1532
3.34k
    if (!range->nested[1])
1533
0
      goto error;
1534
3.55k
  }
1535
3.55k
1536
3.55k
  isl_space_free(space);
1537
3.55k
  return range;
1538
0
error:
1539
0
  isl_space_free(space);
1540
0
  isl_space_free(range);
1541
0
  return NULL;
1542
3.55k
}
1543
1544
/* Given a space of the form A -> [B -> C], return the space A -> C.
1545
 */
1546
__isl_give isl_space *isl_space_range_factor_range(
1547
  __isl_take isl_space *space)
1548
3.54k
{
1549
3.54k
  if (!space)
1550
0
    return NULL;
1551
3.54k
  if (!isl_space_range_is_wrapping(space))
1552
3.54k
    
isl_die0
(isl_space_get_ctx(space), isl_error_invalid,
1553
3.54k
      "range not a product", return isl_space_free(space));
1554
3.54k
1555
3.54k
  return range_factor_range(space);
1556
3.54k
}
1557
1558
/* Given a space of the form [A -> B], return the space B.
1559
 */
1560
static __isl_give isl_space *set_factor_range(__isl_take isl_space *space)
1561
9
{
1562
9
  if (!space)
1563
0
    return NULL;
1564
9
  if (!isl_space_is_wrapping(space))
1565
9
    
isl_die0
(isl_space_get_ctx(space), isl_error_invalid,
1566
9
      "not a product", return isl_space_free(space));
1567
9
1568
9
  return range_factor_range(space);
1569
9
}
1570
1571
/* Given a space of the form [A -> B] -> [C -> D], return the space B -> D.
1572
 * Given a space of the form [A -> B], return the space B.
1573
 */
1574
__isl_give isl_space *isl_space_factor_range(__isl_take isl_space *space)
1575
3.34k
{
1576
3.34k
  if (!space)
1577
0
    return NULL;
1578
3.34k
  if (isl_space_is_set(space))
1579
9
    return set_factor_range(space);
1580
3.34k
  space = isl_space_domain_factor_range(space);
1581
3.34k
  space = isl_space_range_factor_range(space);
1582
3.34k
  return space;
1583
3.34k
}
1584
1585
__isl_give isl_space *isl_space_map_from_set(__isl_take isl_space *space)
1586
3.97k
{
1587
3.97k
  isl_ctx *ctx;
1588
3.97k
  isl_id **ids = NULL;
1589
3.97k
  int n_id;
1590
3.97k
1591
3.97k
  if (!space)
1592
3
    return NULL;
1593
3.97k
  ctx = isl_space_get_ctx(space);
1594
3.97k
  if (!isl_space_is_set(space))
1595
3.97k
    
isl_die0
(ctx, isl_error_invalid, "not a set space", goto error);
1596
3.97k
  space = isl_space_cow(space);
1597
3.97k
  if (!space)
1598
0
    return NULL;
1599
3.97k
  n_id = space->nparam + space->n_out + space->n_out;
1600
3.97k
  if (n_id > 0 && 
space->ids3.78k
) {
1601
1.08k
    ids = isl_calloc_array(space->ctx, isl_id *, n_id);
1602
1.08k
    if (!ids)
1603
0
      goto error;
1604
1.08k
    get_ids(space, isl_dim_param, 0, space->nparam, ids);
1605
1.08k
    get_ids(space, isl_dim_out, 0, space->n_out,
1606
1.08k
      ids + space->nparam);
1607
1.08k
  }
1608
3.97k
  space->n_in = space->n_out;
1609
3.97k
  if (ids) {
1610
1.08k
    free(space->ids);
1611
1.08k
    space->ids = ids;
1612
1.08k
    space->n_id = n_id;
1613
1.08k
    space = copy_ids(space, isl_dim_out, 0, space, isl_dim_in);
1614
1.08k
  }
1615
3.97k
  isl_id_free(space->tuple_id[0]);
1616
3.97k
  space->tuple_id[0] = isl_id_copy(space->tuple_id[1]);
1617
3.97k
  isl_space_free(space->nested[0]);
1618
3.97k
  space->nested[0] = isl_space_copy(space->nested[1]);
1619
3.97k
  return space;
1620
0
error:
1621
0
  isl_space_free(space);
1622
0
  return NULL;
1623
3.97k
}
1624
1625
__isl_give isl_space *isl_space_map_from_domain_and_range(
1626
  __isl_take isl_space *domain, __isl_take isl_space *range)
1627
23.6k
{
1628
23.6k
  if (!domain || !range)
1629
0
    goto error;
1630
23.6k
  if (!isl_space_is_set(domain))
1631
23.6k
    
isl_die0
(isl_space_get_ctx(domain), isl_error_invalid,
1632
23.6k
      "domain is not a set space", goto error);
1633
23.6k
  if (!isl_space_is_set(range))
1634
23.6k
    
isl_die0
(isl_space_get_ctx(range), isl_error_invalid,
1635
23.6k
      "range is not a set space", goto error);
1636
23.6k
  return isl_space_join(isl_space_reverse(domain), range);
1637
0
error:
1638
0
  isl_space_free(domain);
1639
0
  isl_space_free(range);
1640
0
  return NULL;
1641
23.6k
}
1642
1643
static __isl_give isl_space *set_ids(__isl_take isl_space *dim,
1644
  enum isl_dim_type type,
1645
  unsigned first, unsigned n, __isl_take isl_id **ids)
1646
635k
{
1647
635k
  int i;
1648
635k
1649
1.53M
  for (i = 0; i < n ; 
++i898k
)
1650
898k
    dim = set_id(dim, type, first + i, ids[i]);
1651
635k
1652
635k
  return dim;
1653
635k
}
1654
1655
__isl_give isl_space *isl_space_reverse(__isl_take isl_space *dim)
1656
744k
{
1657
744k
  unsigned t;
1658
744k
  isl_space *nested;
1659
744k
  isl_id **ids = NULL;
1660
744k
  isl_id *id;
1661
744k
1662
744k
  if (!dim)
1663
0
    return NULL;
1664
744k
  if (match(dim, isl_dim_in, dim, isl_dim_out))
1665
36.4k
    return dim;
1666
708k
1667
708k
  dim = isl_space_cow(dim);
1668
708k
  if (!dim)
1669
0
    return NULL;
1670
708k
1671
708k
  id = dim->tuple_id[0];
1672
708k
  dim->tuple_id[0] = dim->tuple_id[1];
1673
708k
  dim->tuple_id[1] = id;
1674
708k
1675
708k
  nested = dim->nested[0];
1676
708k
  dim->nested[0] = dim->nested[1];
1677
708k
  dim->nested[1] = nested;
1678
708k
1679
708k
  if (dim->ids) {
1680
317k
    int n_id = dim->n_in + dim->n_out;
1681
317k
    ids = isl_alloc_array(dim->ctx, isl_id *, n_id);
1682
317k
    if (n_id && 
!ids277k
)
1683
0
      goto error;
1684
317k
    get_ids(dim, isl_dim_in, 0, dim->n_in, ids);
1685
317k
    get_ids(dim, isl_dim_out, 0, dim->n_out, ids + dim->n_in);
1686
317k
  }
1687
708k
1688
708k
  t = dim->n_in;
1689
708k
  dim->n_in = dim->n_out;
1690
708k
  dim->n_out = t;
1691
708k
1692
708k
  if (dim->ids) {
1693
317k
    dim = set_ids(dim, isl_dim_out, 0, dim->n_out, ids);
1694
317k
    dim = set_ids(dim, isl_dim_in, 0, dim->n_in, ids + dim->n_out);
1695
317k
    free(ids);
1696
317k
  }
1697
708k
1698
708k
  return dim;
1699
0
error:
1700
0
  free(ids);
1701
0
  isl_space_free(dim);
1702
0
  return NULL;
1703
708k
}
1704
1705
__isl_give isl_space *isl_space_drop_dims(__isl_take isl_space *dim,
1706
  enum isl_dim_type type, unsigned first, unsigned num)
1707
776k
{
1708
776k
  int i;
1709
776k
1710
776k
  if (!dim)
1711
1
    return NULL;
1712
776k
1713
776k
  if (num == 0)
1714
140k
    return isl_space_reset(dim, type);
1715
635k
1716
635k
  if (!valid_dim_type(type))
1717
635k
    
isl_die0
(dim->ctx, isl_error_invalid,
1718
635k
      "cannot drop dimensions of specified type", goto error);
1719
635k
1720
635k
  if (first + num > n(dim, type) || first + num < first)
1721
635k
    
isl_die0
(isl_space_get_ctx(dim), isl_error_invalid,
1722
635k
      "index out of bounds", return isl_space_free(dim));
1723
635k
  dim = isl_space_cow(dim);
1724
635k
  if (!dim)
1725
0
    goto error;
1726
635k
  if (dim->ids) {
1727
193k
    dim = extend_ids(dim);
1728
193k
    if (!dim)
1729
0
      goto error;
1730
570k
    
for (i = 0; 193k
i < num;
++i377k
)
1731
377k
      isl_id_free(get_id(dim, type, first + i));
1732
210k
    for (i = first+num; i < n(dim, type); 
++i16.8k
)
1733
16.8k
      set_id(dim, type, i - num, get_id(dim, type, i));
1734
193k
    switch (type) {
1735
193k
    case isl_dim_param:
1736
9.23k
      get_ids(dim, isl_dim_in, 0, dim->n_in,
1737
9.23k
        dim->ids + offset(dim, isl_dim_in) - num);
1738
62.5k
    case isl_dim_in:
1739
62.5k
      get_ids(dim, isl_dim_out, 0, dim->n_out,
1740
62.5k
        dim->ids + offset(dim, isl_dim_out) - num);
1741
193k
    default:
1742
193k
      ;
1743
193k
    }
1744
193k
    dim->n_id -= num;
1745
193k
  }
1746
635k
  switch (type) {
1747
635k
  
case isl_dim_param: dim->nparam -= num; break9.48k
;
1748
635k
  
case isl_dim_in: dim->n_in -= num; break135k
;
1749
635k
  
case isl_dim_out: dim->n_out -= num; break490k
;
1750
635k
  
default: ;0
1751
635k
  }
1752
635k
  dim = isl_space_reset(dim, type);
1753
635k
  if (type == isl_dim_param) {
1754
9.48k
    if (dim && dim->nested[0] &&
1755
9.48k
        !(dim->nested[0] = isl_space_drop_dims(dim->nested[0],
1756
3
                isl_dim_param, first, num)))
1757
0
      goto error;
1758
9.48k
    if (dim && dim->nested[1] &&
1759
9.48k
        !(dim->nested[1] = isl_space_drop_dims(dim->nested[1],
1760
164
                isl_dim_param, first, num)))
1761
0
      goto error;
1762
635k
  }
1763
635k
  return dim;
1764
0
error:
1765
0
  isl_space_free(dim);
1766
0
  return NULL;
1767
635k
}
1768
1769
__isl_give isl_space *isl_space_drop_inputs(__isl_take isl_space *dim,
1770
    unsigned first, unsigned n)
1771
0
{
1772
0
  if (!dim)
1773
0
    return NULL;
1774
0
  return isl_space_drop_dims(dim, isl_dim_in, first, n);
1775
0
}
1776
1777
__isl_give isl_space *isl_space_drop_outputs(__isl_take isl_space *dim,
1778
    unsigned first, unsigned n)
1779
0
{
1780
0
  if (!dim)
1781
0
    return NULL;
1782
0
  return isl_space_drop_dims(dim, isl_dim_out, first, n);
1783
0
}
1784
1785
__isl_give isl_space *isl_space_domain(__isl_take isl_space *space)
1786
247k
{
1787
247k
  if (!space)
1788
0
    return NULL;
1789
247k
  space = isl_space_drop_dims(space, isl_dim_out, 0, space->n_out);
1790
247k
  space = isl_space_reverse(space);
1791
247k
  space = mark_as_set(space);
1792
247k
  return space;
1793
247k
}
1794
1795
__isl_give isl_space *isl_space_from_domain(__isl_take isl_space *dim)
1796
309k
{
1797
309k
  if (!dim)
1798
0
    return NULL;
1799
309k
  if (!isl_space_is_set(dim))
1800
309k
    
isl_die0
(isl_space_get_ctx(dim), isl_error_invalid,
1801
309k
      "not a set space", goto error);
1802
309k
  dim = isl_space_reverse(dim);
1803
309k
  dim = isl_space_reset(dim, isl_dim_out);
1804
309k
  return dim;
1805
0
error:
1806
0
  isl_space_free(dim);
1807
0
  return NULL;
1808
309k
}
1809
1810
__isl_give isl_space *isl_space_range(__isl_take isl_space *space)
1811
118k
{
1812
118k
  if (!space)
1813
1
    return NULL;
1814
118k
  space = isl_space_drop_dims(space, isl_dim_in, 0, space->n_in);
1815
118k
  space = mark_as_set(space);
1816
118k
  return space;
1817
118k
}
1818
1819
__isl_give isl_space *isl_space_from_range(__isl_take isl_space *dim)
1820
69.5k
{
1821
69.5k
  if (!dim)
1822
0
    return NULL;
1823
69.5k
  if (!isl_space_is_set(dim))
1824
69.5k
    
isl_die0
(isl_space_get_ctx(dim), isl_error_invalid,
1825
69.5k
      "not a set space", goto error);
1826
69.5k
  return isl_space_reset(dim, isl_dim_in);
1827
0
error:
1828
0
  isl_space_free(dim);
1829
0
  return NULL;
1830
69.5k
}
1831
1832
/* Given a map space A -> B, return the map space [A -> B] -> A.
1833
 */
1834
__isl_give isl_space *isl_space_domain_map(__isl_take isl_space *space)
1835
506
{
1836
506
  isl_space *domain;
1837
506
1838
506
  domain = isl_space_from_range(isl_space_domain(isl_space_copy(space)));
1839
506
  space = isl_space_from_domain(isl_space_wrap(space));
1840
506
  space = isl_space_join(space, domain);
1841
506
1842
506
  return space;
1843
506
}
1844
1845
/* Given a map space A -> B, return the map space [A -> B] -> B.
1846
 */
1847
__isl_give isl_space *isl_space_range_map(__isl_take isl_space *space)
1848
16
{
1849
16
  isl_space *range;
1850
16
1851
16
  range = isl_space_from_range(isl_space_range(isl_space_copy(space)));
1852
16
  space = isl_space_from_domain(isl_space_wrap(space));
1853
16
  space = isl_space_join(space, range);
1854
16
1855
16
  return space;
1856
16
}
1857
1858
__isl_give isl_space *isl_space_params(__isl_take isl_space *space)
1859
190k
{
1860
190k
  if (isl_space_is_params(space))
1861
127k
    return space;
1862
63.4k
  space = isl_space_drop_dims(space,
1863
63.4k
          isl_dim_in, 0, isl_space_dim(space, isl_dim_in));
1864
63.4k
  space = isl_space_drop_dims(space,
1865
63.4k
          isl_dim_out, 0, isl_space_dim(space, isl_dim_out));
1866
63.4k
  space = mark_as_params(space);
1867
63.4k
  return space;
1868
63.4k
}
1869
1870
__isl_give isl_space *isl_space_set_from_params(__isl_take isl_space *space)
1871
8.59k
{
1872
8.59k
  if (!space)
1873
0
    return NULL;
1874
8.59k
  if (!isl_space_is_params(space))
1875
8.59k
    
isl_die0
(isl_space_get_ctx(space), isl_error_invalid,
1876
8.59k
      "not a parameter space", goto error);
1877
8.59k
  return isl_space_reset(space, isl_dim_set);
1878
0
error:
1879
0
  isl_space_free(space);
1880
0
  return NULL;
1881
8.59k
}
1882
1883
__isl_give isl_space *isl_space_underlying(__isl_take isl_space *dim,
1884
  unsigned n_div)
1885
170k
{
1886
170k
  int i;
1887
170k
1888
170k
  if (!dim)
1889
0
    return NULL;
1890
170k
  if (n_div == 0 &&
1891
170k
      
dim->nparam == 0167k
&&
dim->n_in == 090.6k
&&
dim->n_id == 076.0k
)
1892
59.3k
    return isl_space_reset(isl_space_reset(dim, isl_dim_in), isl_dim_out);
1893
111k
  dim = isl_space_cow(dim);
1894
111k
  if (!dim)
1895
0
    return NULL;
1896
111k
  dim->n_out += dim->nparam + dim->n_in + n_div;
1897
111k
  dim->nparam = 0;
1898
111k
  dim->n_in = 0;
1899
111k
1900
627k
  for (i = 0; i < dim->n_id; 
++i516k
)
1901
516k
    isl_id_free(get_id(dim, isl_dim_out, i));
1902
111k
  dim->n_id = 0;
1903
111k
  dim = isl_space_reset(dim, isl_dim_in);
1904
111k
  dim = isl_space_reset(dim, isl_dim_out);
1905
111k
1906
111k
  return dim;
1907
111k
}
1908
1909
/* Are the two spaces the same, including positions and names of parameters?
1910
 */
1911
isl_bool isl_space_is_equal(__isl_keep isl_space *space1,
1912
  __isl_keep isl_space *space2)
1913
3.09M
{
1914
3.09M
  isl_bool equal;
1915
3.09M
1916
3.09M
  if (!space1 || !space2)
1917
0
    return isl_bool_error;
1918
3.09M
  if (space1 == space2)
1919
1.66M
    return isl_bool_true;
1920
1.43M
  equal = isl_space_has_equal_params(space1, space2);
1921
1.43M
  if (equal < 0 || !equal)
1922
31.8k
    return equal;
1923
1.40M
  return isl_space_has_equal_tuples(space1, space2);
1924
1.40M
}
1925
1926
/* Do the tuples of "space1" correspond to those of the domain of "space2"?
1927
 * That is, is "space1" eqaul to the domain of "space2", ignoring parameters.
1928
 *
1929
 * "space2" is allowed to be a set space, in which case "space1"
1930
 * should be a parameter space.
1931
 */
1932
isl_bool isl_space_has_domain_tuples(__isl_keep isl_space *space1,
1933
  __isl_keep isl_space *space2)
1934
58.2k
{
1935
58.2k
  isl_bool is_set;
1936
58.2k
1937
58.2k
  is_set = isl_space_is_set(space1);
1938
58.2k
  if (is_set < 0 || !is_set)
1939
0
    return is_set;
1940
58.2k
  return isl_space_tuple_is_equal(space1, isl_dim_set,
1941
58.2k
          space2, isl_dim_in);
1942
58.2k
}
1943
1944
/* Is space1 equal to the domain of space2?
1945
 *
1946
 * In the internal version we also allow space2 to be the space of a set,
1947
 * provided space1 is a parameter space.
1948
 */
1949
isl_bool isl_space_is_domain_internal(__isl_keep isl_space *space1,
1950
  __isl_keep isl_space *space2)
1951
57.9k
{
1952
57.9k
  isl_bool equal_params;
1953
57.9k
1954
57.9k
  if (!space1 || !space2)
1955
0
    return isl_bool_error;
1956
57.9k
  equal_params = isl_space_has_equal_params(space1, space2);
1957
57.9k
  if (equal_params < 0 || !equal_params)
1958
0
    return equal_params;
1959
57.9k
  return isl_space_has_domain_tuples(space1, space2);
1960
57.9k
}
1961
1962
/* Is space1 equal to the domain of space2?
1963
 */
1964
isl_bool isl_space_is_domain(__isl_keep isl_space *space1,
1965
  __isl_keep isl_space *space2)
1966
9
{
1967
9
  if (!space2)
1968
0
    return isl_bool_error;
1969
9
  if (!isl_space_is_map(space2))
1970
0
    return isl_bool_false;
1971
9
  return isl_space_is_domain_internal(space1, space2);
1972
9
}
1973
1974
/* Is space1 equal to the range of space2?
1975
 *
1976
 * In the internal version, space2 is allowed to be the space of a set,
1977
 * in which case it should be equal to space1.
1978
 */
1979
isl_bool isl_space_is_range_internal(__isl_keep isl_space *space1,
1980
  __isl_keep isl_space *space2)
1981
4.50k
{
1982
4.50k
  isl_bool equal_params;
1983
4.50k
1984
4.50k
  if (!space1 || !space2)
1985
0
    return isl_bool_error;
1986
4.50k
  if (!isl_space_is_set(space1))
1987
0
    return isl_bool_false;
1988
4.50k
  equal_params = isl_space_has_equal_params(space1, space2);
1989
4.50k
  if (equal_params < 0 || !equal_params)
1990
0
    return equal_params;
1991
4.50k
  return isl_space_tuple_is_equal(space1, isl_dim_set,
1992
4.50k
          space2, isl_dim_out);
1993
4.50k
}
1994
1995
/* Is space1 equal to the range of space2?
1996
 */
1997
isl_bool isl_space_is_range(__isl_keep isl_space *space1,
1998
  __isl_keep isl_space *space2)
1999
0
{
2000
0
  if (!space2)
2001
0
    return isl_bool_error;
2002
0
  if (!isl_space_is_map(space2))
2003
0
    return isl_bool_false;
2004
0
  return isl_space_is_range_internal(space1, space2);
2005
0
}
2006
2007
/* Update "hash" by hashing in the parameters of "space".
2008
 */
2009
static uint32_t isl_hash_params(uint32_t hash, __isl_keep isl_space *space)
2010
182k
{
2011
182k
  int i;
2012
182k
  isl_id *id;
2013
182k
2014
182k
  if (!space)
2015
0
    return hash;
2016
182k
2017
182k
  isl_hash_byte(hash, space->nparam % 256);
2018
182k
2019
472k
  for (i = 0; i < space->nparam; 
++i290k
) {
2020
290k
    id = get_id(space, isl_dim_param, i);
2021
290k
    hash = isl_hash_id(hash, id);
2022
290k
  }
2023
182k
2024
182k
  return hash;
2025
182k
}
2026
2027
/* Update "hash" by hashing in the tuples of "space".
2028
 * Changes in this function should be reflected in isl_hash_tuples_domain.
2029
 */
2030
static uint32_t isl_hash_tuples(uint32_t hash, __isl_keep isl_space *space)
2031
680k
{
2032
680k
  isl_id *id;
2033
680k
2034
680k
  if (!space)
2035
432k
    return hash;
2036
247k
2037
247k
  isl_hash_byte(hash, space->n_in % 256);
2038
247k
  isl_hash_byte(hash, space->n_out % 256);
2039
247k
2040
247k
  id = tuple_id(space, isl_dim_in);
2041
247k
  hash = isl_hash_id(hash, id);
2042
247k
  id = tuple_id(space, isl_dim_out);
2043
247k
  hash = isl_hash_id(hash, id);
2044
247k
2045
247k
  hash = isl_hash_tuples(hash, space->nested[0]);
2046
247k
  hash = isl_hash_tuples(hash, space->nested[1]);
2047
247k
2048
247k
  return hash;
2049
247k
}
2050
2051
/* Update "hash" by hashing in the domain tuple of "space".
2052
 * The result of this function is equal to the result of applying
2053
 * isl_hash_tuples to the domain of "space".
2054
 */
2055
static uint32_t isl_hash_tuples_domain(uint32_t hash,
2056
  __isl_keep isl_space *space)
2057
16.0k
{
2058
16.0k
  isl_id *id;
2059
16.0k
2060
16.0k
  if (!space)
2061
0
    return hash;
2062
16.0k
2063
16.0k
  isl_hash_byte(hash, 0);
2064
16.0k
  isl_hash_byte(hash, space->n_in % 256);
2065
16.0k
2066
16.0k
  hash = isl_hash_id(hash, &isl_id_none);
2067
16.0k
  id = tuple_id(space, isl_dim_in);
2068
16.0k
  hash = isl_hash_id(hash, id);
2069
16.0k
2070
16.0k
  hash = isl_hash_tuples(hash, space->nested[0]);
2071
16.0k
2072
16.0k
  return hash;
2073
16.0k
}
2074
2075
/* Return a hash value that digests the tuples of "space",
2076
 * i.e., that ignores the parameters.
2077
 */
2078
uint32_t isl_space_get_tuple_hash(__isl_keep isl_space *space)
2079
1.95k
{
2080
1.95k
  uint32_t hash;
2081
1.95k
2082
1.95k
  if (!space)
2083
0
    return 0;
2084
1.95k
2085
1.95k
  hash = isl_hash_init();
2086
1.95k
  hash = isl_hash_tuples(hash, space);
2087
1.95k
2088
1.95k
  return hash;
2089
1.95k
}
2090
2091
uint32_t isl_space_get_hash(__isl_keep isl_space *space)
2092
166k
{
2093
166k
  uint32_t hash;
2094
166k
2095
166k
  if (!space)
2096
0
    return 0;
2097
166k
2098
166k
  hash = isl_hash_init();
2099
166k
  hash = isl_hash_params(hash, space);
2100
166k
  hash = isl_hash_tuples(hash, space);
2101
166k
2102
166k
  return hash;
2103
166k
}
2104
2105
/* Return the hash value of the domain of "space".
2106
 * That is, isl_space_get_domain_hash(space) is equal to
2107
 * isl_space_get_hash(isl_space_domain(space)).
2108
 */
2109
uint32_t isl_space_get_domain_hash(__isl_keep isl_space *space)
2110
16.0k
{
2111
16.0k
  uint32_t hash;
2112
16.0k
2113
16.0k
  if (!space)
2114
0
    return 0;
2115
16.0k
2116
16.0k
  hash = isl_hash_init();
2117
16.0k
  hash = isl_hash_params(hash, space);
2118
16.0k
  hash = isl_hash_tuples_domain(hash, space);
2119
16.0k
2120
16.0k
  return hash;
2121
16.0k
}
2122
2123
isl_bool isl_space_is_wrapping(__isl_keep isl_space *dim)
2124
24.5k
{
2125
24.5k
  if (!dim)
2126
0
    return isl_bool_error;
2127
24.5k
2128
24.5k
  if (!isl_space_is_set(dim))
2129
0
    return isl_bool_false;
2130
24.5k
2131
24.5k
  return dim->nested[1] != NULL;
2132
24.5k
}
2133
2134
/* Is "space" the space of a map where the domain is a wrapped map space?
2135
 */
2136
isl_bool isl_space_domain_is_wrapping(__isl_keep isl_space *space)
2137
14.3k
{
2138
14.3k
  if (!space)
2139
0
    return isl_bool_error;
2140
14.3k
2141
14.3k
  if (isl_space_is_set(space))
2142
0
    return isl_bool_false;
2143
14.3k
2144
14.3k
  return space->nested[0] != NULL;
2145
14.3k
}
2146
2147
/* Is "space" the space of a map where the range is a wrapped map space?
2148
 */
2149
isl_bool isl_space_range_is_wrapping(__isl_keep isl_space *space)
2150
18.2k
{
2151
18.2k
  if (!space)
2152
0
    return isl_bool_error;
2153
18.2k
2154
18.2k
  if (isl_space_is_set(space))
2155
183
    return isl_bool_false;
2156
18.1k
2157
18.1k
  return space->nested[1] != NULL;
2158
18.1k
}
2159
2160
/* Is "space" a product of two spaces?
2161
 * That is, is it a wrapping set space or a map space
2162
 * with wrapping domain and range?
2163
 */
2164
isl_bool isl_space_is_product(__isl_keep isl_space *space)
2165
2.16k
{
2166
2.16k
  isl_bool is_set;
2167
2.16k
  isl_bool is_product;
2168
2.16k
2169
2.16k
  is_set = isl_space_is_set(space);
2170
2.16k
  if (is_set < 0)
2171
0
    return isl_bool_error;
2172
2.16k
  if (is_set)
2173
0
    return isl_space_is_wrapping(space);
2174
2.16k
  is_product = isl_space_domain_is_wrapping(space);
2175
2.16k
  if (is_product < 0 || !is_product)
2176
20
    return is_product;
2177
2.14k
  return isl_space_range_is_wrapping(space);
2178
2.14k
}
2179
2180
__isl_give isl_space *isl_space_wrap(__isl_take isl_space *dim)
2181
71.0k
{
2182
71.0k
  isl_space *wrap;
2183
71.0k
2184
71.0k
  if (!dim)
2185
0
    return NULL;
2186
71.0k
2187
71.0k
  wrap = isl_space_set_alloc(dim->ctx,
2188
71.0k
            dim->nparam, dim->n_in + dim->n_out);
2189
71.0k
2190
71.0k
  wrap = copy_ids(wrap, isl_dim_param, 0, dim, isl_dim_param);
2191
71.0k
  wrap = copy_ids(wrap, isl_dim_set, 0, dim, isl_dim_in);
2192
71.0k
  wrap = copy_ids(wrap, isl_dim_set, dim->n_in, dim, isl_dim_out);
2193
71.0k
2194
71.0k
  if (!wrap)
2195
0
    goto error;
2196
71.0k
2197
71.0k
  wrap->nested[1] = dim;
2198
71.0k
2199
71.0k
  return wrap;
2200
0
error:
2201
0
  isl_space_free(dim);
2202
0
  return NULL;
2203
71.0k
}
2204
2205
__isl_give isl_space *isl_space_unwrap(__isl_take isl_space *dim)
2206
15.6k
{
2207
15.6k
  isl_space *unwrap;
2208
15.6k
2209
15.6k
  if (!dim)
2210
0
    return NULL;
2211
15.6k
2212
15.6k
  if (!isl_space_is_wrapping(dim))
2213
15.6k
    
isl_die0
(dim->ctx, isl_error_invalid, "not a wrapping space",
2214
15.6k
      goto error);
2215
15.6k
2216
15.6k
  unwrap = isl_space_copy(dim->nested[1]);
2217
15.6k
  isl_space_free(dim);
2218
15.6k
2219
15.6k
  return unwrap;
2220
0
error:
2221
0
  isl_space_free(dim);
2222
0
  return NULL;
2223
15.6k
}
2224
2225
isl_bool isl_space_is_named_or_nested(__isl_keep isl_space *space,
2226
  enum isl_dim_type type)
2227
2.14M
{
2228
2.14M
  if (type != isl_dim_in && 
type != isl_dim_out1.58M
)
2229
79.5k
    return isl_bool_false;
2230
2.07M
  if (!space)
2231
3
    return isl_bool_error;
2232
2.07M
  if (space->tuple_id[type - isl_dim_in])
2233
889k
    return isl_bool_true;
2234
1.18M
  if (space->nested[type - isl_dim_in])
2235
241k
    return isl_bool_true;
2236
938k
  return isl_bool_false;
2237
938k
}
2238
2239
isl_bool isl_space_may_be_set(__isl_keep isl_space *space)
2240
276
{
2241
276
  isl_bool nested;
2242
276
2243
276
  if (!space)
2244
0
    return isl_bool_error;
2245
276
  if (isl_space_is_set(space))
2246
171
    return isl_bool_true;
2247
105
  if (isl_space_dim(space, isl_dim_in) != 0)
2248
0
    return isl_bool_false;
2249
105
  nested = isl_space_is_named_or_nested(space, isl_dim_in);
2250
105
  if (nested < 0 || nested)
2251
0
    return isl_bool_not(nested);
2252
105
  return isl_bool_true;
2253
105
}
2254
2255
__isl_give isl_space *isl_space_reset(__isl_take isl_space *dim,
2256
  enum isl_dim_type type)
2257
1.96M
{
2258
1.96M
  if (!isl_space_is_named_or_nested(dim, type))
2259
924k
    return dim;
2260
1.03M
2261
1.03M
  dim = isl_space_cow(dim);
2262
1.03M
  if (!dim)
2263
7
    return NULL;
2264
1.03M
2265
1.03M
  isl_id_free(dim->tuple_id[type - isl_dim_in]);
2266
1.03M
  dim->tuple_id[type - isl_dim_in] = NULL;
2267
1.03M
  isl_space_free(dim->nested[type - isl_dim_in]);
2268
1.03M
  dim->nested[type - isl_dim_in] = NULL;
2269
1.03M
2270
1.03M
  return dim;
2271
1.03M
}
2272
2273
__isl_give isl_space *isl_space_flatten(__isl_take isl_space *dim)
2274
7.86k
{
2275
7.86k
  if (!dim)
2276
0
    return NULL;
2277
7.86k
  if (!dim->nested[0] && !dim->nested[1])
2278
0
    return dim;
2279
7.86k
2280
7.86k
  if (dim->nested[0])
2281
0
    dim = isl_space_reset(dim, isl_dim_in);
2282
7.86k
  if (dim && dim->nested[1])
2283
7.86k
    dim = isl_space_reset(dim, isl_dim_out);
2284
7.86k
2285
7.86k
  return dim;
2286
7.86k
}
2287
2288
__isl_give isl_space *isl_space_flatten_domain(__isl_take isl_space *space)
2289
8
{
2290
8
  if (!space)
2291
0
    return NULL;
2292
8
  if (!space->nested[0])
2293
0
    return space;
2294
8
2295
8
  return isl_space_reset(space, isl_dim_in);
2296
8
}
2297
2298
__isl_give isl_space *isl_space_flatten_range(__isl_take isl_space *space)
2299
24.1k
{
2300
24.1k
  if (!space)
2301
0
    return NULL;
2302
24.1k
  if (!space->nested[1])
2303
0
    return space;
2304
24.1k
2305
24.1k
  return isl_space_reset(space, isl_dim_out);
2306
24.1k
}
2307
2308
/* Replace the parameters of dst by those of src.
2309
 */
2310
__isl_give isl_space *isl_space_replace_params(__isl_take isl_space *dst,
2311
  __isl_keep isl_space *src)
2312
46.8k
{
2313
46.8k
  isl_bool equal_params;
2314
46.8k
  enum isl_dim_type type = isl_dim_param;
2315
46.8k
2316
46.8k
  equal_params = isl_space_has_equal_params(dst, src);
2317
46.8k
  if (equal_params < 0)
2318
0
    return isl_space_free(dst);
2319
46.8k
  if (equal_params)
2320
24.9k
    return dst;
2321
21.8k
2322
21.8k
  dst = isl_space_cow(dst);
2323
21.8k
2324
21.8k
  if (!dst || !src)
2325
0
    goto error;
2326
21.8k
2327
21.8k
  dst = isl_space_drop_dims(dst, type, 0, isl_space_dim(dst, type));
2328
21.8k
  dst = isl_space_add_dims(dst, type, isl_space_dim(src, type));
2329
21.8k
  dst = copy_ids(dst, type, 0, src, type);
2330
21.8k
2331
21.8k
  if (dst) {
2332
21.8k
    int i;
2333
65.6k
    for (i = 0; i <= 1; 
++i43.7k
) {
2334
43.7k
      if (!dst->nested[i])
2335
43.6k
        continue;
2336
172
      dst->nested[i] = isl_space_replace_params(
2337
172
              dst->nested[i], src);
2338
172
      if (!dst->nested[i])
2339
0
        goto error;
2340
172
    }
2341
21.8k
  }
2342
21.8k
2343
21.8k
  return dst;
2344
0
error:
2345
0
  isl_space_free(dst);
2346
0
  return NULL;
2347
21.8k
}
2348
2349
/* Given a dimension specification "dim" of a set, create a dimension
2350
 * specification for the lift of the set.  In particular, the result
2351
 * is of the form [dim -> local[..]], with n_local variables in the
2352
 * range of the wrapped map.
2353
 */
2354
__isl_give isl_space *isl_space_lift(__isl_take isl_space *dim, unsigned n_local)
2355
7.28k
{
2356
7.28k
  isl_space *local_dim;
2357
7.28k
2358
7.28k
  if (!dim)
2359
0
    return NULL;
2360
7.28k
2361
7.28k
  local_dim = isl_space_dup(dim);
2362
7.28k
  local_dim = isl_space_drop_dims(local_dim, isl_dim_set, 0, dim->n_out);
2363
7.28k
  local_dim = isl_space_add_dims(local_dim, isl_dim_set, n_local);
2364
7.28k
  local_dim = isl_space_set_tuple_name(local_dim, isl_dim_set, "local");
2365
7.28k
  dim = isl_space_join(isl_space_from_domain(dim),
2366
7.28k
          isl_space_from_range(local_dim));
2367
7.28k
  dim = isl_space_wrap(dim);
2368
7.28k
  dim = isl_space_set_tuple_name(dim, isl_dim_set, "lifted");
2369
7.28k
2370
7.28k
  return dim;
2371
7.28k
}
2372
2373
isl_bool isl_space_can_zip(__isl_keep isl_space *space)
2374
2.06k
{
2375
2.06k
  isl_bool is_set;
2376
2.06k
2377
2.06k
  is_set = isl_space_is_set(space);
2378
2.06k
  if (is_set < 0)
2379
0
    return isl_bool_error;
2380
2.06k
  if (is_set)
2381
0
    return isl_bool_false;
2382
2.06k
  return isl_space_is_product(space);
2383
2.06k
}
2384
2385
__isl_give isl_space *isl_space_zip(__isl_take isl_space *dim)
2386
864
{
2387
864
  isl_space *dom, *ran;
2388
864
  isl_space *dom_dom, *dom_ran, *ran_dom, *ran_ran;
2389
864
2390
864
  if (!isl_space_can_zip(dim))
2391
864
    
isl_die0
(dim->ctx, isl_error_invalid, "dim cannot be zipped",
2392
864
      goto error);
2393
864
2394
864
  if (!dim)
2395
0
    return NULL;
2396
864
  dom = isl_space_unwrap(isl_space_domain(isl_space_copy(dim)));
2397
864
  ran = isl_space_unwrap(isl_space_range(dim));
2398
864
  dom_dom = isl_space_domain(isl_space_copy(dom));
2399
864
  dom_ran = isl_space_range(dom);
2400
864
  ran_dom = isl_space_domain(isl_space_copy(ran));
2401
864
  ran_ran = isl_space_range(ran);
2402
864
  dom = isl_space_join(isl_space_from_domain(dom_dom),
2403
864
         isl_space_from_range(ran_dom));
2404
864
  ran = isl_space_join(isl_space_from_domain(dom_ran),
2405
864
         isl_space_from_range(ran_ran));
2406
864
  return isl_space_join(isl_space_from_domain(isl_space_wrap(dom)),
2407
864
          isl_space_from_range(isl_space_wrap(ran)));
2408
0
error:
2409
0
  isl_space_free(dim);
2410
0
  return NULL;
2411
864
}
2412
2413
/* Can we apply isl_space_curry to "space"?
2414
 * That is, does it have a nested relation in its domain?
2415
 */
2416
isl_bool isl_space_can_curry(__isl_keep isl_space *space)
2417
10.9k
{
2418
10.9k
  if (!space)
2419
0
    return isl_bool_error;
2420
10.9k
2421
10.9k
  return !!space->nested[0];
2422
10.9k
}
2423
2424
/* Given a space (A -> B) -> C, return the corresponding space
2425
 * A -> (B -> C).
2426
 */
2427
__isl_give isl_space *isl_space_curry(__isl_take isl_space *space)
2428
3.52k
{
2429
3.52k
  isl_space *dom, *ran;
2430
3.52k
  isl_space *dom_dom, *dom_ran;
2431
3.52k
2432
3.52k
  if (!space)
2433
0
    return NULL;
2434
3.52k
2435
3.52k
  if (!isl_space_can_curry(space))
2436
3.52k
    
isl_die0
(space->ctx, isl_error_invalid,
2437
3.52k
      "space cannot be curried", goto error);
2438
3.52k
2439
3.52k
  dom = isl_space_unwrap(isl_space_domain(isl_space_copy(space)));
2440
3.52k
  ran = isl_space_range(space);
2441
3.52k
  dom_dom = isl_space_domain(isl_space_copy(dom));
2442
3.52k
  dom_ran = isl_space_range(dom);
2443
3.52k
  ran = isl_space_join(isl_space_from_domain(dom_ran),
2444
3.52k
         isl_space_from_range(ran));
2445
3.52k
  return isl_space_join(isl_space_from_domain(dom_dom),
2446
3.52k
          isl_space_from_range(isl_space_wrap(ran)));
2447
0
error:
2448
0
  isl_space_free(space);
2449
0
  return NULL;
2450
3.52k
}
2451
2452
/* Can isl_space_range_curry be applied to "space"?
2453
 * That is, does it have a nested relation in its range,
2454
 * the domain of which is itself a nested relation?
2455
 */
2456
isl_bool isl_space_can_range_curry(__isl_keep isl_space *space)
2457
6.77k
{
2458
6.77k
  isl_bool can;
2459
6.77k
2460
6.77k
  if (!space)
2461
0
    return isl_bool_error;
2462
6.77k
  can = isl_space_range_is_wrapping(space);
2463
6.77k
  if (can < 0 || !can)
2464
0
    return can;
2465
6.77k
  return isl_space_can_curry(space->nested[1]);
2466
6.77k
}
2467
2468
/* Given a space A -> ((B -> C) -> D), return the corresponding space
2469
 * A -> (B -> (C -> D)).
2470
 */
2471
__isl_give isl_space *isl_space_range_curry(__isl_take isl_space *space)
2472
3.34k
{
2473
3.34k
  if (!space)
2474
0
    return NULL;
2475
3.34k
2476
3.34k
  if (!isl_space_can_range_curry(space))
2477
3.34k
    
isl_die0
(isl_space_get_ctx(space), isl_error_invalid,
2478
3.34k
      "space range cannot be curried",
2479
3.34k
      return isl_space_free(space));
2480
3.34k
2481
3.34k
  space = isl_space_cow(space);
2482
3.34k
  if (!space)
2483
0
    return NULL;
2484
3.34k
  space->nested[1] = isl_space_curry(space->nested[1]);
2485
3.34k
  if (!space->nested[1])
2486
0
    return isl_space_free(space);
2487
3.34k
2488
3.34k
  return space;
2489
3.34k
}
2490
2491
/* Can we apply isl_space_uncurry to "space"?
2492
 * That is, does it have a nested relation in its range?
2493
 */
2494
isl_bool isl_space_can_uncurry(__isl_keep isl_space *space)
2495
547
{
2496
547
  if (!space)
2497
0
    return isl_bool_error;
2498
547
2499
547
  return !!space->nested[1];
2500
547
}
2501
2502
/* Given a space A -> (B -> C), return the corresponding space
2503
 * (A -> B) -> C.
2504
 */
2505
__isl_give isl_space *isl_space_uncurry(__isl_take isl_space *space)
2506
191
{
2507
191
  isl_space *dom, *ran;
2508
191
  isl_space *ran_dom, *ran_ran;
2509
191
2510
191
  if (!space)
2511
0
    return NULL;
2512
191
2513
191
  if (!isl_space_can_uncurry(space))
2514
191
    
isl_die0
(space->ctx, isl_error_invalid,
2515
191
      "space cannot be uncurried",
2516
191
      return isl_space_free(space));
2517
191
2518
191
  dom = isl_space_domain(isl_space_copy(space));
2519
191
  ran = isl_space_unwrap(isl_space_range(space));
2520
191
  ran_dom = isl_space_domain(isl_space_copy(ran));
2521
191
  ran_ran = isl_space_range(ran);
2522
191
  dom = isl_space_join(isl_space_from_domain(dom),
2523
191
         isl_space_from_range(ran_dom));
2524
191
  return isl_space_join(isl_space_from_domain(isl_space_wrap(dom)),
2525
191
          isl_space_from_range(ran_ran));
2526
191
}
2527
2528
isl_bool isl_space_has_named_params(__isl_keep isl_space *space)
2529
192k
{
2530
192k
  int i;
2531
192k
  unsigned off;
2532
192k
2533
192k
  if (!space)
2534
1
    return isl_bool_error;
2535
192k
  if (space->nparam == 0)
2536
100k
    return isl_bool_true;
2537
92.2k
  off = isl_space_offset(space, isl_dim_param);
2538
92.2k
  if (off + space->nparam > space->n_id)
2539
0
    return isl_bool_false;
2540
414k
  
for (i = 0; 92.2k
i < space->nparam;
++i322k
)
2541
322k
    if (!space->ids[off + i])
2542
0
      return isl_bool_false;
2543
92.2k
  return isl_bool_true;
2544
92.2k
}
2545
2546
/* Check that "space" has only named parameters, reporting an error
2547
 * if it does not.
2548
 */
2549
isl_stat isl_space_check_named_params(__isl_keep isl_space *space)
2550
126k
{
2551
126k
  isl_bool named;
2552
126k
2553
126k
  named = isl_space_has_named_params(space);
2554
126k
  if (named < 0)
2555
1
    return isl_stat_error;
2556
126k
  if (!named)
2557
126k
    
isl_die0
(isl_space_get_ctx(space), isl_error_invalid,
2558
126k
      "unexpected unnamed parameters", return isl_stat_error);
2559
126k
2560
126k
  return isl_stat_ok;
2561
126k
}
2562
2563
/* Align the initial parameters of space1 to match the order in space2.
2564
 */
2565
__isl_give isl_space *isl_space_align_params(__isl_take isl_space *space1,
2566
  __isl_take isl_space *space2)
2567
16.6k
{
2568
16.6k
  isl_reordering *exp;
2569
16.6k
2570
16.6k
  if (isl_space_check_named_params(space1) < 0 ||
2571
16.6k
      
isl_space_check_named_params(space2) < 016.6k
)
2572
1
    goto error;
2573
16.6k
2574
16.6k
  exp = isl_parameter_alignment_reordering(space1, space2);
2575
16.6k
  exp = isl_reordering_extend_space(exp, space1);
2576
16.6k
  isl_space_free(space2);
2577
16.6k
  space1 = isl_reordering_get_space(exp);
2578
16.6k
  isl_reordering_free(exp);
2579
16.6k
  return space1;
2580
1
error:
2581
1
  isl_space_free(space1);
2582
1
  isl_space_free(space2);
2583
1
  return NULL;
2584
16.6k
}
2585
2586
/* Given the space of set (domain), construct a space for a map
2587
 * with as domain the given space and as range the range of "model".
2588
 */
2589
__isl_give isl_space *isl_space_extend_domain_with_range(
2590
  __isl_take isl_space *space, __isl_take isl_space *model)
2591
22.0k
{
2592
22.0k
  if (!model)
2593
0
    goto error;
2594
22.0k
2595
22.0k
  space = isl_space_from_domain(space);
2596
22.0k
  space = isl_space_add_dims(space, isl_dim_out,
2597
22.0k
            isl_space_dim(model, isl_dim_out));
2598
22.0k
  if (isl_space_has_tuple_id(model, isl_dim_out))
2599
125
    space = isl_space_set_tuple_id(space, isl_dim_out,
2600
125
        isl_space_get_tuple_id(model, isl_dim_out));
2601
22.0k
  if (!space)
2602
0
    goto error;
2603
22.0k
  if (model->nested[1]) {
2604
1
    isl_space *nested = isl_space_copy(model->nested[1]);
2605
1
    int n_nested, n_space;
2606
1
    nested = isl_space_align_params(nested, isl_space_copy(space));
2607
1
    n_nested = isl_space_dim(nested, isl_dim_param);
2608
1
    n_space = isl_space_dim(space, isl_dim_param);
2609
1
    if (n_nested > n_space)
2610
0
      nested = isl_space_drop_dims(nested, isl_dim_param,
2611
0
            n_space, n_nested - n_space);
2612
1
    if (!nested)
2613
0
      goto error;
2614
1
    space->nested[1] = nested;
2615
1
  }
2616
22.0k
  isl_space_free(model);
2617
22.0k
  return space;
2618
0
error:
2619
0
  isl_space_free(model);
2620
0
  isl_space_free(space);
2621
0
  return NULL;
2622
22.0k
}
2623
2624
/* Compare the "type" dimensions of two isl_spaces.
2625
 *
2626
 * The order is fairly arbitrary.
2627
 */
2628
static int isl_space_cmp_type(__isl_keep isl_space *space1,
2629
  __isl_keep isl_space *space2, enum isl_dim_type type)
2630
1.05M
{
2631
1.05M
  int cmp;
2632
1.05M
  isl_space *nested1, *nested2;
2633
1.05M
2634
1.05M
  if (isl_space_dim(space1, type) != isl_space_dim(space2, type))
2635
0
    return isl_space_dim(space1, type) -
2636
0
      isl_space_dim(space2, type);
2637
1.05M
2638
1.05M
  cmp = isl_id_cmp(tuple_id(space1, type), tuple_id(space2, type));
2639
1.05M
  if (cmp != 0)
2640
0
    return cmp;
2641
1.05M
2642
1.05M
  nested1 = nested(space1, type);
2643
1.05M
  nested2 = nested(space2, type);
2644
1.05M
  if (!nested1 != !nested2)
2645
0
    return !nested1 - !nested2;
2646
1.05M
2647
1.05M
  if (nested1)
2648
50.5k
    return isl_space_cmp(nested1, nested2);
2649
1.00M
2650
1.00M
  return 0;
2651
1.00M
}
2652
2653
/* Compare two isl_spaces.
2654
 *
2655
 * The order is fairly arbitrary.
2656
 */
2657
int isl_space_cmp(__isl_keep isl_space *space1, __isl_keep isl_space *space2)
2658
1.40M
{
2659
1.40M
  int i;
2660
1.40M
  int cmp;
2661
1.40M
2662
1.40M
  if (space1 == space2)
2663
1.05M
    return 0;
2664
351k
  if (!space1)
2665
0
    return -1;
2666
351k
  if (!space2)
2667
0
    return 1;
2668
351k
2669
351k
  cmp = isl_space_cmp_type(space1, space2, isl_dim_param);
2670
351k
  if (cmp != 0)
2671
0
    return cmp;
2672
351k
  cmp = isl_space_cmp_type(space1, space2, isl_dim_in);
2673
351k
  if (cmp != 0)
2674
0
    return cmp;
2675
351k
  cmp = isl_space_cmp_type(space1, space2, isl_dim_out);
2676
351k
  if (cmp != 0)
2677
0
    return cmp;
2678
351k
2679
351k
  if (!space1->ids && 
!space2->ids53.6k
)
2680
53.5k
    return 0;
2681
298k
2682
3.09M
  
for (i = 0; 298k
i < n(space1, isl_dim_param);
++i2.79M
) {
2683
2.79M
    cmp = isl_id_cmp(get_id(space1, isl_dim_param, i),
2684
2.79M
         get_id(space2, isl_dim_param, i));
2685
2.79M
    if (cmp != 0)
2686
0
      return cmp;
2687
2.79M
  }
2688
298k
2689
298k
  return 0;
2690
298k
}