Coverage Report

Created: 2018-02-20 23:11

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