Coverage Report

Created: 2018-10-23 15:26

/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.71M
{
23
1.71M
  return dim ? dim->ctx : NULL;
24
1.71M
}
25
26
__isl_give isl_space *isl_space_alloc(isl_ctx *ctx,
27
      unsigned nparam, unsigned n_in, unsigned n_out)
28
5.06M
{
29
5.06M
  isl_space *dim;
30
5.06M
31
5.06M
  dim = isl_alloc_type(ctx, struct isl_space);
32
5.06M
  if (!dim)
33
12
    return NULL;
34
5.06M
35
5.06M
  dim->ctx = ctx;
36
5.06M
  isl_ctx_ref(ctx);
37
5.06M
  dim->ref = 1;
38
5.06M
  dim->nparam = nparam;
39
5.06M
  dim->n_in = n_in;
40
5.06M
  dim->n_out = n_out;
41
5.06M
42
5.06M
  dim->tuple_id[0] = NULL;
43
5.06M
  dim->tuple_id[1] = NULL;
44
5.06M
45
5.06M
  dim->nested[0] = NULL;
46
5.06M
  dim->nested[1] = NULL;
47
5.06M
48
5.06M
  dim->n_id = 0;
49
5.06M
  dim->ids = NULL;
50
5.06M
51
5.06M
  return dim;
52
5.06M
}
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.61M
{
59
1.61M
  space = isl_space_cow(space);
60
1.61M
  if (!space)
61
2
    return NULL;
62
1.61M
  space = isl_space_set_tuple_id(space, isl_dim_in, &isl_id_none);
63
1.61M
  return space;
64
1.61M
}
65
66
/* Is the space that of a set?
67
 */
68
isl_bool isl_space_is_set(__isl_keep isl_space *space)
69
1.78M
{
70
1.78M
  if (!space)
71
0
    return isl_bool_error;
72
1.78M
  if (space->n_in != 0 || 
space->nested[0]1.48M
)
73
308k
    return isl_bool_false;
74
1.47M
  if (space->tuple_id[0] != &isl_id_none)
75
16.7k
    return isl_bool_false;
76
1.45M
  return isl_bool_true;
77
1.45M
}
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.5k
{
83
30.5k
  if (!space)
84
0
    return isl_bool_error;
85
30.5k
  return space->tuple_id[0] != &isl_id_none &&
86
30.5k
    
space->tuple_id[1] != &isl_id_none805
;
87
30.5k
}
88
89
__isl_give isl_space *isl_space_set_alloc(isl_ctx *ctx,
90
      unsigned nparam, unsigned dim)
91
543k
{
92
543k
  isl_space *space;
93
543k
  space = isl_space_alloc(ctx, nparam, 0, dim);
94
543k
  space = mark_as_set(space);
95
543k
  return space;
96
543k
}
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
199k
{
103
199k
  if (!space)
104
0
    return NULL;
105
199k
  space = isl_space_set_tuple_id(space, isl_dim_in, &isl_id_none);
106
199k
  space = isl_space_set_tuple_id(space, isl_dim_out, &isl_id_none);
107
199k
  return space;
108
199k
}
109
110
/* Is the space that of a parameter domain?
111
 */
112
isl_bool isl_space_is_params(__isl_keep isl_space *space)
113
770k
{
114
770k
  if (!space)
115
27
    return isl_bool_error;
116
770k
  if (space->n_in != 0 || 
space->nested[0]624k
||
117
770k
      
space->n_out != 0624k
||
space->nested[1]470k
)
118
301k
    return isl_bool_false;
119
469k
  if (space->tuple_id[0] != &isl_id_none)
120
25.9k
    return isl_bool_false;
121
443k
  if (space->tuple_id[1] != &isl_id_none)
122
39.0k
    return isl_bool_false;
123
404k
  return isl_bool_true;
124
404k
}
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
28.6k
{
130
28.6k
  isl_space *space;
131
28.6k
  space = isl_space_alloc(ctx, nparam, 0, 0);
132
28.6k
  space = mark_as_params(space);
133
28.6k
  return space;
134
28.6k
}
135
136
static unsigned global_pos(__isl_keep isl_space *dim,
137
         enum isl_dim_type type, unsigned pos)
138
52.6M
{
139
52.6M
  struct isl_ctx *ctx = dim->ctx;
140
52.6M
141
52.6M
  switch (type) {
142
52.6M
  case isl_dim_param:
143
34.8M
    isl_assert(ctx, pos < dim->nparam,
144
34.8M
          return isl_space_dim(dim, isl_dim_all));
145
34.8M
    return pos;
146
34.8M
  case isl_dim_in:
147
6.85M
    isl_assert(ctx, pos < dim->n_in,
148
6.85M
          return isl_space_dim(dim, isl_dim_all));
149
6.85M
    return pos + dim->nparam;
150
10.9M
  case isl_dim_out:
151
10.9M
    isl_assert(ctx, pos < dim->n_out,
152
10.9M
          return isl_space_dim(dim, isl_dim_all));
153
10.9M
    return pos + dim->nparam + dim->n_in;
154
10.9M
  default:
155
0
    isl_assert(ctx, 0, return isl_space_dim(dim, isl_dim_all));
156
52.6M
  }
157
52.6M
  
return isl_space_dim(dim, isl_dim_all)0
;
158
52.6M
}
159
160
/* Extend length of ids array to the total number of dimensions.
161
 */
162
static __isl_give isl_space *extend_ids(__isl_take isl_space *dim)
163
2.23M
{
164
2.23M
  isl_id **ids;
165
2.23M
  int i;
166
2.23M
167
2.23M
  if (isl_space_dim(dim, isl_dim_all) <= dim->n_id)
168
533k
    return dim;
169
1.70M
170
1.70M
  if (!dim->ids) {
171
1.70M
    dim->ids = isl_calloc_array(dim->ctx,
172
1.70M
        isl_id *, isl_space_dim(dim, isl_dim_all));
173
1.70M
    if (!dim->ids)
174
0
      goto error;
175
738
  } else {
176
738
    ids = isl_realloc_array(dim->ctx, dim->ids,
177
738
        isl_id *, isl_space_dim(dim, isl_dim_all));
178
738
    if (!ids)
179
0
      goto error;
180
738
    dim->ids = ids;
181
4.55k
    for (i = dim->n_id; i < isl_space_dim(dim, isl_dim_all); 
++i3.82k
)
182
3.82k
      dim->ids[i] = NULL;
183
738
  }
184
1.70M
185
1.70M
  dim->n_id = isl_space_dim(dim, isl_dim_all);
186
1.70M
187
1.70M
  return dim;
188
0
error:
189
0
  isl_space_free(dim);
190
0
  return NULL;
191
1.70M
}
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
7.01M
{
196
7.01M
  dim = isl_space_cow(dim);
197
7.01M
198
7.01M
  if (!dim)
199
0
    goto error;
200
7.01M
201
7.01M
  pos = global_pos(dim, type, pos);
202
7.01M
  if (pos == isl_space_dim(dim, isl_dim_all))
203
0
    goto error;
204
7.01M
205
7.01M
  if (pos >= dim->n_id) {
206
1.70M
    if (!id)
207
0
      return dim;
208
1.70M
    dim = extend_ids(dim);
209
1.70M
    if (!dim)
210
0
      goto error;
211
7.01M
  }
212
7.01M
213
7.01M
  dim->ids[pos] = id;
214
7.01M
215
7.01M
  return dim;
216
0
error:
217
0
  isl_id_free(id);
218
0
  isl_space_free(dim);
219
0
  return NULL;
220
7.01M
}
221
222
static __isl_keep isl_id *get_id(__isl_keep isl_space *dim,
223
         enum isl_dim_type type, unsigned pos)
224
45.6M
{
225
45.6M
  if (!dim)
226
0
    return NULL;
227
45.6M
228
45.6M
  pos = global_pos(dim, type, pos);
229
45.6M
  if (pos == isl_space_dim(dim, isl_dim_all))
230
0
    return NULL;
231
45.6M
  if (pos >= dim->n_id)
232
3.17M
    return NULL;
233
42.4M
  return dim->ids[pos];
234
42.4M
}
235
236
static unsigned offset(__isl_keep isl_space *dim, enum isl_dim_type type)
237
2.21M
{
238
2.21M
  switch (type) {
239
2.21M
  
case isl_dim_param: return 0722k
;
240
2.21M
  
case isl_dim_in: return dim->nparam514k
;
241
2.21M
  
case isl_dim_out: return dim->nparam + dim->n_in973k
;
242
2.21M
  
default: return 00
;
243
2.21M
  }
244
2.21M
}
245
246
static unsigned n(__isl_keep isl_space *dim, enum isl_dim_type type)
247
285M
{
248
285M
  switch (type) {
249
285M
  
case isl_dim_param: return dim->nparam55.1M
;
250
285M
  
case isl_dim_in: return dim->n_in35.5M
;
251
285M
  
case isl_dim_out: return dim->n_out41.1M
;
252
285M
  
case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out153M
;
253
285M
  
default: return 00
;
254
285M
  }
255
285M
}
256
257
unsigned isl_space_dim(__isl_keep isl_space *dim, enum isl_dim_type type)
258
204M
{
259
204M
  if (!dim)
260
4
    return 0;
261
204M
  return n(dim, type);
262
204M
}
263
264
unsigned isl_space_offset(__isl_keep isl_space *dim, enum isl_dim_type type)
265
2.00M
{
266
2.00M
  if (!dim)
267
0
    return 0;
268
2.00M
  return offset(dim, type);
269
2.00M
}
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
6.82M
{
275
6.82M
  int i;
276
6.82M
  isl_id *id;
277
6.82M
278
6.82M
  if (!dst)
279
0
    return NULL;
280
6.82M
281
18.8M
  
for (i = 0; 6.82M
i < n(src, src_type);
++i12.0M
) {
282
12.0M
    id = get_id(src, src_type, i);
283
12.0M
    if (!id)
284
7.25M
      continue;
285
4.78M
    dst = set_id(dst, dst_type, offset + i, isl_id_copy(id));
286
4.78M
    if (!dst)
287
0
      return NULL;
288
4.78M
  }
289
6.82M
  return dst;
290
6.82M
}
291
292
__isl_take isl_space *isl_space_dup(__isl_keep isl_space *dim)
293
3.78M
{
294
3.78M
  isl_space *dup;
295
3.78M
  if (!dim)
296
0
    return NULL;
297
3.78M
  dup = isl_space_alloc(dim->ctx, dim->nparam, dim->n_in, dim->n_out);
298
3.78M
  if (!dup)
299
12
    return NULL;
300
3.78M
  if (dim->tuple_id[0] &&
301
3.78M
      
!(dup->tuple_id[0] = isl_id_copy(dim->tuple_id[0]))2.15M
)
302
0
    goto error;
303
3.78M
  if (dim->tuple_id[1] &&
304
3.78M
      
!(dup->tuple_id[1] = isl_id_copy(dim->tuple_id[1]))888k
)
305
0
    goto error;
306
3.78M
  if (dim->nested[0] && 
!(dup->nested[0] = isl_space_copy(dim->nested[0]))722k
)
307
0
    goto error;
308
3.78M
  if (dim->nested[1] && 
!(dup->nested[1] = isl_space_copy(dim->nested[1]))941k
)
309
0
    goto error;
310
3.78M
  if (!dim->ids)
311
2.29M
    return dup;
312
1.48M
  dup = copy_ids(dup, isl_dim_param, 0, dim, isl_dim_param);
313
1.48M
  dup = copy_ids(dup, isl_dim_in, 0, dim, isl_dim_in);
314
1.48M
  dup = copy_ids(dup, isl_dim_out, 0, dim, isl_dim_out);
315
1.48M
  return dup;
316
0
error:
317
0
  isl_space_free(dup);
318
0
  return NULL;
319
1.48M
}
320
321
__isl_give isl_space *isl_space_cow(__isl_take isl_space *dim)
322
18.9M
{
323
18.9M
  if (!dim)
324
11
    return NULL;
325
18.9M
326
18.9M
  if (dim->ref == 1)
327
15.1M
    return dim;
328
3.76M
  dim->ref--;
329
3.76M
  return isl_space_dup(dim);
330
3.76M
}
331
332
__isl_give isl_space *isl_space_copy(__isl_keep isl_space *dim)
333
16.9M
{
334
16.9M
  if (!dim)
335
9.22k
    return NULL;
336
16.9M
337
16.9M
  dim->ref++;
338
16.9M
  return dim;
339
16.9M
}
340
341
__isl_null isl_space *isl_space_free(__isl_take isl_space *space)
342
29.7M
{
343
29.7M
  int i;
344
29.7M
345
29.7M
  if (!space)
346
11.5M
    return NULL;
347
18.2M
348
18.2M
  if (--space->ref > 0)
349
13.1M
    return NULL;
350
5.06M
351
5.06M
  isl_id_free(space->tuple_id[0]);
352
5.06M
  isl_id_free(space->tuple_id[1]);
353
5.06M
354
5.06M
  isl_space_free(space->nested[0]);
355
5.06M
  isl_space_free(space->nested[1]);
356
5.06M
357
12.0M
  for (i = 0; i < space->n_id; 
++i7.02M
)
358
7.02M
    isl_id_free(space->ids[i]);
359
5.06M
  free(space->ids);
360
5.06M
  isl_ctx_deref(space->ctx);
361
5.06M
  
362
5.06M
  free(space);
363
5.06M
364
5.06M
  return NULL;
365
5.06M
}
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
55.6k
{
374
55.6k
  char *p;
375
55.6k
  long dummy;
376
55.6k
377
55.6k
  dummy = strtol(s, &p, 0);
378
55.6k
  if (p != s)
379
55.6k
    
isl_die0
(ctx, isl_error_invalid, "name looks like a number",
380
55.6k
      return 0);
381
55.6k
382
55.6k
  return 1;
383
55.6k
}
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
119k
{
390
119k
  if (!space)
391
0
    return 0;
392
119k
  if (isl_space_is_params(space))
393
119k
    
isl_die0
(space->ctx, isl_error_invalid,
394
119k
      "parameter spaces don't have tuple ids", return 0);
395
119k
  if (isl_space_is_set(space) && 
type != isl_dim_set31.0k
)
396
119k
    
isl_die0
(space->ctx, isl_error_invalid,
397
119k
      "set spaces can only have a set id", return 0);
398
119k
  if (type != isl_dim_in && 
type != isl_dim_out119k
)
399
119k
    
isl_die0
(space->ctx, isl_error_invalid,
400
119k
      "only input, output and set tuples can have ids",
401
119k
      return 0);
402
119k
403
119k
  return 1;
404
119k
}
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
118k
{
411
118k
  if (!space_can_have_id(dim, type))
412
0
    return isl_bool_error;
413
118k
  return dim->tuple_id[type - isl_dim_in] != NULL;
414
118k
}
415
416
__isl_give isl_id *isl_space_get_tuple_id(__isl_keep isl_space *dim,
417
  enum isl_dim_type type)
418
38.2k
{
419
38.2k
  int has_id;
420
38.2k
421
38.2k
  if (!dim)
422
0
    return NULL;
423
38.2k
  has_id = isl_space_has_tuple_id(dim, type);
424
38.2k
  if (has_id < 0)
425
0
    return NULL;
426
38.2k
  if (!has_id)
427
38.2k
    
isl_die0
(dim->ctx, isl_error_invalid,
428
38.2k
      "tuple has no id", return NULL);
429
38.2k
  return isl_id_copy(dim->tuple_id[type - isl_dim_in]);
430
38.2k
}
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
2.08M
{
435
2.08M
  dim = isl_space_cow(dim);
436
2.08M
  if (!dim || !id)
437
0
    goto error;
438
2.08M
  if (type != isl_dim_in && 
type != isl_dim_out263k
)
439
2.08M
    
isl_die0
(dim->ctx, isl_error_invalid,
440
2.08M
      "only input, output and set tuples can have names",
441
2.08M
      goto error);
442
2.08M
443
2.08M
  isl_id_free(dim->tuple_id[type - isl_dim_in]);
444
2.08M
  dim->tuple_id[type - isl_dim_in] = id;
445
2.08M
446
2.08M
  return dim;
447
0
error:
448
0
  isl_id_free(id);
449
0
  isl_space_free(dim);
450
0
  return NULL;
451
2.08M
}
452
453
__isl_give isl_space *isl_space_reset_tuple_id(__isl_take isl_space *dim,
454
  enum isl_dim_type type)
455
5.96k
{
456
5.96k
  dim = isl_space_cow(dim);
457
5.96k
  if (!dim)
458
0
    return NULL;
459
5.96k
  if (type != isl_dim_in && type != isl_dim_out)
460
5.96k
    
isl_die0
(dim->ctx, isl_error_invalid,
461
5.96k
      "only input, output and set tuples can have names",
462
5.96k
      goto error);
463
5.96k
464
5.96k
  isl_id_free(dim->tuple_id[type - isl_dim_in]);
465
5.96k
  dim->tuple_id[type - isl_dim_in] = NULL;
466
5.96k
467
5.96k
  return dim;
468
0
error:
469
0
  isl_space_free(dim);
470
0
  return NULL;
471
5.96k
}
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
82.7k
{
481
82.7k
  space = isl_space_cow(space);
482
82.7k
  if (!space || !id)
483
0
    goto error;
484
82.7k
485
82.7k
  if (type == isl_dim_param) {
486
61.2k
    int i;
487
61.2k
488
183k
    for (i = 0; i < 2; 
++i122k
) {
489
122k
      if (!space->nested[i])
490
122k
        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
61.2k
  }
498
82.7k
499
82.7k
  isl_id_free(get_id(space, type, pos));
500
82.7k
  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
82.7k
}
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.21k
{
543
1.21k
  if (!dim)
544
0
    return isl_bool_error;
545
1.21k
  return get_id(dim, type, pos) != NULL;
546
1.21k
}
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
780k
{
551
780k
  if (!dim)
552
0
    return NULL;
553
780k
  if (!get_id(dim, type, pos))
554
780k
    
isl_die0
(dim->ctx, isl_error_invalid,
555
780k
      "dim has no id", return NULL);
556
780k
  return isl_id_copy(get_id(dim, type, pos));
557
780k
}
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
49.0k
{
562
49.0k
  isl_id *id;
563
49.0k
564
49.0k
  if (!dim)
565
0
    return NULL;
566
49.0k
567
49.0k
  if (!s)
568
0
    return isl_space_reset_tuple_id(dim, type);
569
49.0k
570
49.0k
  if (!name_ok(dim->ctx, s))
571
0
    goto error;
572
49.0k
573
49.0k
  id = isl_id_alloc(dim->ctx, s, NULL);
574
49.0k
  return isl_space_set_tuple_id(dim, type, id);
575
0
error:
576
0
  isl_space_free(dim);
577
0
  return NULL;
578
49.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
1.06k
{
585
1.06k
  isl_id *id;
586
1.06k
587
1.06k
  if (!space_can_have_id(space, type))
588
0
    return isl_bool_error;
589
1.06k
  id = space->tuple_id[type - isl_dim_in];
590
1.06k
  return id && 
id->name183
;
591
1.06k
}
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.40k
)
600
0
    return NULL;
601
10.8k
  id = dim->tuple_id[type - isl_dim_in];
602
10.8k
  return id ? 
id->name9.35k
: 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
6.52k
{
609
6.52k
  isl_id *id;
610
6.52k
611
6.52k
  if (!dim)
612
0
    return NULL;
613
6.52k
  if (!s)
614
0
    return isl_space_reset_dim_id(dim, type, pos);
615
6.52k
  if (!name_ok(dim->ctx, s))
616
0
    goto error;
617
6.52k
  id = isl_id_alloc(dim->ctx, s, NULL);
618
6.52k
  return isl_space_set_dim_id(dim, type, pos, id);
619
0
error:
620
0
  isl_space_free(dim);
621
0
  return NULL;
622
6.52k
}
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.62k
{
629
6.62k
  isl_id *id;
630
6.62k
631
6.62k
  if (!space)
632
0
    return isl_bool_error;
633
6.62k
  id = get_id(space, type, pos);
634
6.62k
  return id && 
id->name1.01k
;
635
6.62k
}
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->name261k
: 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
394
{
647
394
  int i;
648
394
  int offset;
649
394
  int n;
650
394
651
394
  if (!dim || !id)
652
0
    return -1;
653
394
654
394
  offset = isl_space_offset(dim, type);
655
394
  n = isl_space_dim(dim, type);
656
518
  for (i = 0; i < n && 
offset + i < dim->n_id516
;
++i124
)
657
516
    if (dim->ids[offset + i] == id)
658
392
      return i;
659
394
660
394
  
return -12
;
661
394
}
662
663
int isl_space_find_dim_by_name(__isl_keep isl_space *space,
664
  enum isl_dim_type type, const char *name)
665
918
{
666
918
  int i;
667
918
  int offset;
668
918
  int n;
669
918
670
918
  if (!space || !name)
671
0
    return -1;
672
918
673
918
  offset = isl_space_offset(space, type);
674
918
  n = isl_space_dim(space, type);
675
1.61k
  for (i = 0; i < n && 
offset + i < space->n_id696
;
++i696
) {
676
696
    isl_id *id = get_id(space, type, i);
677
696
    if (id && id->name && !strcmp(id->name, name))
678
0
      return i;
679
696
  }
680
918
681
918
  return -1;
682
918
}
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
45.0M
{
746
45.0M
  if (!dim)
747
0
    return NULL;
748
45.0M
  if (type == isl_dim_in)
749
14.0M
    return dim->tuple_id[0];
750
30.9M
  if (type == isl_dim_out)
751
12.7M
    return dim->tuple_id[1];
752
18.2M
  return NULL;
753
18.2M
}
754
755
static __isl_keep isl_space *nested(__isl_keep isl_space *dim,
756
  enum isl_dim_type type)
757
41.8M
{
758
41.8M
  if (!dim)
759
0
    return NULL;
760
41.8M
  if (type == isl_dim_in)
761
12.0M
    return dim->nested[0];
762
29.8M
  if (type == isl_dim_out)
763
11.5M
    return dim->nested[1];
764
18.2M
  return NULL;
765
18.2M
}
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.99M
{
772
6.99M
  if (!space1 || !space2)
773
0
    return isl_bool_error;
774
6.99M
  if (space1 == space2)
775
2.26M
    return isl_bool_true;
776
4.73M
  return isl_space_tuple_is_equal(space1, isl_dim_in,
777
4.73M
          space2, isl_dim_in) &&
778
4.73M
         isl_space_tuple_is_equal(space1, isl_dim_out,
779
4.07M
          space2, isl_dim_out);
780
4.73M
}
781
782
/* Check if the tuple of type "type1" of "space1" is the same as
783
 * the tuple of type "type2" of "space2".
784
 *
785
 * That is, check if the tuples have the same identifier, the same dimension
786
 * and the same internal structure.
787
 * The identifiers of the dimensions inside the tuples do not affect the result.
788
 *
789
 * Note that this function only checks the tuples themselves.
790
 * If nested tuples are involved, then we need to be careful not
791
 * to have result affected by possibly differing parameters
792
 * in those nested tuples.
793
 */
794
isl_bool isl_space_tuple_is_equal(__isl_keep isl_space *space1,
795
  enum isl_dim_type type1, __isl_keep isl_space *space2,
796
  enum isl_dim_type type2)
797
22.0M
{
798
22.0M
  isl_id *id1, *id2;
799
22.0M
  isl_space *nested1, *nested2;
800
22.0M
801
22.0M
  if (!space1 || !space2)
802
0
    return isl_bool_error;
803
22.0M
804
22.0M
  if (space1 == space2 && 
type1 == type21.97M
)
805
6.39k
    return isl_bool_true;
806
22.0M
807
22.0M
  if (n(space1, type1) != n(space2, type2))
808
2.23M
    return isl_bool_false;
809
19.7M
  id1 = tuple_id(space1, type1);
810
19.7M
  id2 = tuple_id(space2, type2);
811
19.7M
  if (!id1 ^ !id2)
812
629k
    return isl_bool_false;
813
19.1M
  if (id1 && 
id1 != id23.99M
)
814
149k
    return isl_bool_false;
815
19.0M
  nested1 = nested(space1, type1);
816
19.0M
  nested2 = nested(space2, type2);
817
19.0M
  if (!nested1 ^ !nested2)
818
64.5k
    return isl_bool_false;
819
18.9M
  if (nested1 && 
!isl_space_has_equal_tuples(nested1, nested2)3.61M
)
820
26.1k
    return isl_bool_false;
821
18.9M
  return isl_bool_true;
822
18.9M
}
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.7M
{
827
11.7M
  int i;
828
11.7M
829
11.7M
  if (space1 == space2 && 
type1 == type22.87M
)
830
911k
    return isl_bool_true;
831
10.8M
832
10.8M
  if (!isl_space_tuple_is_equal(space1, type1, space2, type2))
833
2.09M
    return isl_bool_false;
834
8.76M
835
8.76M
  if (!space1->ids && 
!space2->ids5.68M
)
836
5.64M
    return isl_bool_true;
837
3.12M
838
11.6M
  
for (i = 0; 3.12M
i < n(space1, type1);
++i8.54M
) {
839
8.61M
    if (get_id(space1, type1, i) != get_id(space2, type2, i))
840
62.7k
      return isl_bool_false;
841
8.61M
  }
842
3.12M
  
return isl_bool_true3.05M
;
843
3.12M
}
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
9.39M
{
850
9.39M
  if (!space1 || 
!space29.39M
)
851
2
    return isl_bool_error;
852
9.39M
853
9.39M
  return match(space1, isl_dim_param, space2, isl_dim_param);
854
9.39M
}
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
206k
{
862
206k
  isl_bool equal;
863
206k
864
206k
  if (!space1 || !space2)
865
0
    return isl_bool_error;
866
206k
867
206k
  equal = match(space1, isl_dim_in, space2, isl_dim_in);
868
206k
  if (equal < 0 || !equal)
869
132
    return equal;
870
205k
  return match(space1, isl_dim_out, space2, isl_dim_out);
871
205k
}
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
2.81M
{
885
2.81M
  int i;
886
2.81M
887
7.13M
  for (i = 0; i < n ; 
++i4.31M
)
888
4.31M
    ids[i] = get_id(dim, type, first + i);
889
2.81M
}
890
891
static __isl_give isl_space *space_extend(__isl_take isl_space *space,
892
      unsigned nparam, unsigned n_in, unsigned n_out)
893
706k
{
894
706k
  isl_id **ids = NULL;
895
706k
896
706k
  if (!space)
897
0
    return NULL;
898
706k
  if (space->nparam == nparam &&
899
706k
      
space->n_in == n_in582k
&&
space->n_out == n_out575k
)
900
1.45k
    return space;
901
704k
902
704k
  isl_assert(space->ctx, space->nparam <= nparam, goto error);
903
704k
  isl_assert(space->ctx, space->n_in <= n_in, goto error);
904
704k
  isl_assert(space->ctx, space->n_out <= n_out, goto error);
905
704k
906
704k
  space = isl_space_cow(space);
907
704k
  if (!space)
908
0
    goto error;
909
704k
910
704k
  if (space->ids) {
911
309k
    unsigned n;
912
309k
    n = nparam + n_in + n_out;
913
309k
    if (n < nparam || n < n_in || n < n_out)
914
309k
      
isl_die0
(isl_space_get_ctx(space), isl_error_invalid,
915
309k
        "overflow in total number of dimensions",
916
309k
        goto error);
917
309k
    ids = isl_calloc_array(space->ctx, isl_id *, n);
918
309k
    if (!ids)
919
0
      goto error;
920
309k
    get_ids(space, isl_dim_param, 0, space->nparam, ids);
921
309k
    get_ids(space, isl_dim_in, 0, space->n_in, ids + nparam);
922
309k
    get_ids(space, isl_dim_out, 0, space->n_out,
923
309k
      ids + nparam + n_in);
924
309k
    free(space->ids);
925
309k
    space->ids = ids;
926
309k
    space->n_id = nparam + n_in + n_out;
927
309k
  }
928
704k
  space->nparam = nparam;
929
704k
  space->n_in = n_in;
930
704k
  space->n_out = n_out;
931
704k
932
704k
  return space;
933
0
error:
934
0
  free(ids);
935
0
  isl_space_free(space);
936
0
  return NULL;
937
704k
}
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
706k
{
948
706k
  space = isl_space_reset(space, type);
949
706k
  if (!space)
950
9
    return NULL;
951
706k
  switch (type) {
952
706k
  case isl_dim_param:
953
123k
    space = space_extend(space,
954
123k
        space->nparam + n, space->n_in, space->n_out);
955
123k
    if (space && space->nested[0] &&
956
123k
        !(space->nested[0] = isl_space_add_dims(space->nested[0],
957
125
                isl_dim_param, n)))
958
0
      goto error;
959
123k
    if (space && space->nested[1] &&
960
123k
        !(space->nested[1] = isl_space_add_dims(space->nested[1],
961
397
                isl_dim_param, n)))
962
0
      goto error;
963
123k
    return space;
964
123k
  case isl_dim_in:
965
8.88k
    return space_extend(space,
966
8.88k
        space->nparam, space->n_in + n, space->n_out);
967
574k
  case isl_dim_out:
968
574k
    return space_extend(space,
969
574k
        space->nparam, space->n_in, space->n_out + n);
970
123k
  default:
971
0
    isl_die(space->ctx, isl_error_invalid,
972
706k
      "cannot add dimensions of specified type", goto error);
973
706k
  }
974
706k
error:
975
0
  isl_space_free(space);
976
0
  return NULL;
977
706k
}
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.06M
{
1008
2.06M
  switch (type) {
1009
2.06M
  case isl_dim_param:
1010
2.06M
  case isl_dim_in:
1011
2.06M
  case isl_dim_out:
1012
2.06M
    return 1;
1013
2.06M
  default:
1014
0
    return 0;
1015
2.06M
  }
1016
2.06M
}
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
180k
{
1025
180k
  isl_ctx *ctx;
1026
180k
  isl_id **ids = NULL;
1027
180k
  unsigned total;
1028
180k
1029
180k
  if (!space)
1030
0
    return NULL;
1031
180k
  if (n == 0)
1032
6
    return isl_space_reset(space, type);
1033
180k
1034
180k
  ctx = isl_space_get_ctx(space);
1035
180k
  if (!valid_dim_type(type))
1036
180k
    
isl_die0
(ctx, isl_error_invalid,
1037
180k
      "cannot insert dimensions of specified type",
1038
180k
      goto error);
1039
180k
1040
180k
  total = isl_space_dim(space, isl_dim_all);
1041
180k
  if (total + n < total)
1042
180k
    
isl_die0
(ctx, isl_error_invalid,
1043
180k
      "overflow in total number of dimensions",
1044
180k
      return isl_space_free(space));
1045
180k
  isl_assert(ctx, pos <= isl_space_dim(space, type), goto error);
1046
180k
1047
180k
  space = isl_space_cow(space);
1048
180k
  if (!space)
1049
0
    return NULL;
1050
180k
1051
180k
  if (space->ids) {
1052
54.5k
    enum isl_dim_type t, o = isl_dim_param;
1053
54.5k
    int off;
1054
54.5k
    int s[3];
1055
54.5k
    ids = isl_calloc_array(ctx, isl_id *,
1056
54.5k
           space->nparam + space->n_in + space->n_out + n);
1057
54.5k
    if (!ids)
1058
0
      goto error;
1059
54.5k
    off = 0;
1060
54.5k
    s[isl_dim_param - o] = space->nparam;
1061
54.5k
    s[isl_dim_in - o] = space->n_in;
1062
54.5k
    s[isl_dim_out - o] = space->n_out;
1063
218k
    for (t = isl_dim_param; t <= isl_dim_out; 
++t163k
) {
1064
163k
      if (t != type) {
1065
109k
        get_ids(space, t, 0, s[t - o], ids + off);
1066
109k
        off += s[t - o];
1067
109k
      } else {
1068
54.5k
        get_ids(space, t, 0, pos, ids + off);
1069
54.5k
        off += pos + n;
1070
54.5k
        get_ids(space, t, pos, s[t - o] - pos,
1071
54.5k
          ids + off);
1072
54.5k
        off += s[t - o] - pos;
1073
54.5k
      }
1074
163k
    }
1075
54.5k
    free(space->ids);
1076
54.5k
    space->ids = ids;
1077
54.5k
    space->n_id = space->nparam + space->n_in + space->n_out + n;
1078
54.5k
  }
1079
180k
  switch (type) {
1080
180k
  
case isl_dim_param: space->nparam += n; break2.81k
;
1081
180k
  
case isl_dim_in: space->n_in += n; break10.4k
;
1082
180k
  
case isl_dim_out: space->n_out += n; break167k
;
1083
180k
  
default: ;0
1084
180k
  }
1085
180k
  space = isl_space_reset(space, type);
1086
180k
1087
180k
  if (type == isl_dim_param) {
1088
2.81k
    if (space && space->nested[0] &&
1089
2.81k
        !(space->nested[0] = isl_space_insert_dims(space->nested[0],
1090
0
                isl_dim_param, pos, n)))
1091
0
      goto error;
1092
2.81k
    if (space && space->nested[1] &&
1093
2.81k
        !(space->nested[1] = isl_space_insert_dims(space->nested[1],
1094
1
                isl_dim_param, pos, n)))
1095
0
      goto error;
1096
180k
  }
1097
180k
1098
180k
  return space;
1099
0
error:
1100
0
  isl_space_free(space);
1101
0
  return NULL;
1102
180k
}
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
1.17k
{
1108
1.17k
  int i;
1109
1.17k
1110
1.17k
  space = isl_space_reset(space, src_type);
1111
1.17k
  space = isl_space_reset(space, dst_type);
1112
1.17k
  if (!space)
1113
0
    return NULL;
1114
1.17k
  if (n == 0)
1115
294
    return space;
1116
879
1117
879
  isl_assert(space->ctx, src_pos + n <= isl_space_dim(space, src_type),
1118
879
    goto error);
1119
879
1120
879
  if (dst_type == src_type && 
dst_pos == src_pos0
)
1121
0
    return space;
1122
879
1123
879
  isl_assert(space->ctx, dst_type != src_type, goto error);
1124
879
1125
879
  space = isl_space_cow(space);
1126
879
  if (!space)
1127
0
    return NULL;
1128
879
1129
879
  if (space->ids) {
1130
323
    isl_id **ids;
1131
323
    enum isl_dim_type t, o = isl_dim_param;
1132
323
    int off;
1133
323
    int s[3];
1134
323
    ids = isl_calloc_array(space->ctx, isl_id *,
1135
323
         space->nparam + space->n_in + space->n_out);
1136
323
    if (!ids)
1137
0
      goto error;
1138
323
    off = 0;
1139
323
    s[isl_dim_param - o] = space->nparam;
1140
323
    s[isl_dim_in - o] = space->n_in;
1141
323
    s[isl_dim_out - o] = space->n_out;
1142
1.29k
    for (t = isl_dim_param; t <= isl_dim_out; 
++t969
) {
1143
969
      if (t == dst_type) {
1144
323
        get_ids(space, t, 0, dst_pos, ids + off);
1145
323
        off += dst_pos;
1146
323
        get_ids(space, src_type, src_pos, n, ids + off);
1147
323
        off += n;
1148
323
        get_ids(space, t, dst_pos, s[t - o] - dst_pos,
1149
323
            ids + off);
1150
323
        off += s[t - o] - dst_pos;
1151
646
      } else if (t == src_type) {
1152
323
        get_ids(space, t, 0, src_pos, ids + off);
1153
323
        off += src_pos;
1154
323
        get_ids(space, t, src_pos + n,
1155
323
              s[t - o] - src_pos - n, ids + off);
1156
323
        off += s[t - o] - src_pos - n;
1157
323
      } else {
1158
323
        get_ids(space, t, 0, s[t - o], ids + off);
1159
323
        off += s[t - o];
1160
323
      }
1161
969
    }
1162
323
    free(space->ids);
1163
323
    space->ids = ids;
1164
323
    space->n_id = space->nparam + space->n_in + space->n_out;
1165
323
  }
1166
879
1167
879
  switch (dst_type) {
1168
879
  
case isl_dim_param: space->nparam += n; break87
;
1169
879
  
case isl_dim_in: space->n_in += n; break361
;
1170
879
  
case isl_dim_out: space->n_out += n; break431
;
1171
879
  
default: ;0
1172
879
  }
1173
879
1174
879
  switch (src_type) {
1175
879
  
case isl_dim_param: space->nparam -= n; break243
;
1176
879
  
case isl_dim_in: space->n_in -= n; break206
;
1177
879
  
case isl_dim_out: space->n_out -= n; break430
;
1178
879
  
default: ;0
1179
879
  }
1180
879
1181
879
  if (dst_type != isl_dim_param && 
src_type != isl_dim_param792
)
1182
549
    return space;
1183
330
1184
990
  
for (i = 0; 330
i < 2;
++i660
) {
1185
660
    if (!space->nested[i])
1186
643
      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
330
1193
330
  return space;
1194
0
error:
1195
0
  isl_space_free(space);
1196
0
  return NULL;
1197
330
}
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
665k
{
1205
665k
  isl_bool equal;
1206
665k
1207
665k
  equal = isl_space_has_equal_params(space1, space2);
1208
665k
  if (equal < 0)
1209
2
    return isl_stat_error;
1210
665k
  if (!equal)
1211
665k
    
isl_die0
(isl_space_get_ctx(space1), isl_error_invalid,
1212
665k
      "parameters need to match", return isl_stat_error);
1213
665k
  return isl_stat_ok;
1214
665k
}
1215
1216
__isl_give isl_space *isl_space_join(__isl_take isl_space *left,
1217
  __isl_take isl_space *right)
1218
555k
{
1219
555k
  isl_space *dim;
1220
555k
1221
555k
  if (isl_space_check_equal_params(left, right) < 0)
1222
2
    goto error;
1223
555k
1224
555k
  isl_assert(left->ctx,
1225
555k
    isl_space_tuple_is_equal(left, isl_dim_out, right, isl_dim_in),
1226
555k
    goto error);
1227
555k
1228
555k
  dim = isl_space_alloc(left->ctx, left->nparam, left->n_in, right->n_out);
1229
555k
  if (!dim)
1230
0
    goto error;
1231
555k
1232
555k
  dim = copy_ids(dim, isl_dim_param, 0, left, isl_dim_param);
1233
555k
  dim = copy_ids(dim, isl_dim_in, 0, left, isl_dim_in);
1234
555k
  dim = copy_ids(dim, isl_dim_out, 0, right, isl_dim_out);
1235
555k
1236
555k
  if (dim && left->tuple_id[0] &&
1237
555k
      
!(dim->tuple_id[0] = isl_id_copy(left->tuple_id[0]))186k
)
1238
0
    goto error;
1239
555k
  if (dim && right->tuple_id[1] &&
1240
555k
      
!(dim->tuple_id[1] = isl_id_copy(right->tuple_id[1]))151k
)
1241
0
    goto error;
1242
555k
  if (dim && left->nested[0] &&
1243
555k
      
!(dim->nested[0] = isl_space_copy(left->nested[0]))211k
)
1244
0
    goto error;
1245
555k
  if (dim && right->nested[1] &&
1246
555k
      
!(dim->nested[1] = isl_space_copy(right->nested[1]))257k
)
1247
0
    goto error;
1248
555k
1249
555k
  isl_space_free(left);
1250
555k
  isl_space_free(right);
1251
555k
1252
555k
  return dim;
1253
2
error:
1254
2
  isl_space_free(left);
1255
2
  isl_space_free(right);
1256
2
  return NULL;
1257
555k
}
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
2.89k
{
1266
2.89k
  isl_space *dom1, *dom2, *nest1, *nest2;
1267
2.89k
  int is_set;
1268
2.89k
1269
2.89k
  if (!left || !right)
1270
0
    goto error;
1271
2.89k
1272
2.89k
  is_set = isl_space_is_set(left);
1273
2.89k
  if (is_set != isl_space_is_set(right))
1274
2.89k
    
isl_die0
(isl_space_get_ctx(left), isl_error_invalid,
1275
2.89k
      "expecting either two set spaces or two map spaces",
1276
2.89k
      goto error);
1277
2.89k
  if (is_set)
1278
274
    return isl_space_range_product(left, right);
1279
2.61k
1280
2.61k
  if (isl_space_check_equal_params(left, right) < 0)
1281
0
    goto error;
1282
2.61k
1283
2.61k
  dom1 = isl_space_domain(isl_space_copy(left));
1284
2.61k
  dom2 = isl_space_domain(isl_space_copy(right));
1285
2.61k
  nest1 = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1286
2.61k
1287
2.61k
  dom1 = isl_space_range(left);
1288
2.61k
  dom2 = isl_space_range(right);
1289
2.61k
  nest2 = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1290
2.61k
1291
2.61k
  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
2.61k
}
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
2.05k
{
1304
2.05k
  isl_space *ran, *dom1, *dom2, *nest;
1305
2.05k
1306
2.05k
  if (isl_space_check_equal_params(left, right) < 0)
1307
0
    goto error;
1308
2.05k
1309
2.05k
  if (!isl_space_tuple_is_equal(left, isl_dim_out, right, isl_dim_out))
1310
2.05k
    
isl_die0
(left->ctx, isl_error_invalid,
1311
2.05k
      "ranges need to match", goto error);
1312
2.05k
1313
2.05k
  ran = isl_space_range(isl_space_copy(left));
1314
2.05k
1315
2.05k
  dom1 = isl_space_domain(left);
1316
2.05k
  dom2 = isl_space_domain(right);
1317
2.05k
  nest = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1318
2.05k
1319
2.05k
  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
2.05k
}
1325
1326
__isl_give isl_space *isl_space_range_product(__isl_take isl_space *left,
1327
  __isl_take isl_space *right)
1328
105k
{
1329
105k
  isl_space *dom, *ran1, *ran2, *nest;
1330
105k
1331
105k
  if (isl_space_check_equal_params(left, right) < 0)
1332
0
    goto error;
1333
105k
1334
105k
  if (!isl_space_tuple_is_equal(left, isl_dim_in, right, isl_dim_in))
1335
105k
    
isl_die0
(left->ctx, isl_error_invalid,
1336
105k
      "domains need to match", goto error);
1337
105k
1338
105k
  dom = isl_space_domain(isl_space_copy(left));
1339
105k
1340
105k
  ran1 = isl_space_range(left);
1341
105k
  ran2 = isl_space_range(right);
1342
105k
  nest = isl_space_wrap(isl_space_join(isl_space_reverse(ran1), ran2));
1343
105k
1344
105k
  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
105k
}
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
857
{
1356
857
  isl_space *nested;
1357
857
  isl_space *domain;
1358
857
1359
857
  if (!space)
1360
0
    return NULL;
1361
857
  if (!isl_space_domain_is_wrapping(space))
1362
857
    
isl_die0
(isl_space_get_ctx(space), isl_error_invalid,
1363
857
      "domain not a product", return isl_space_free(space));
1364
857
1365
857
  nested = space->nested[0];
1366
857
  domain = isl_space_copy(space);
1367
857
  domain = isl_space_drop_dims(domain, isl_dim_in,
1368
857
          nested->n_in, nested->n_out);
1369
857
  if (!domain)
1370
0
    return isl_space_free(space);
1371
857
  if (nested->tuple_id[0]) {
1372
538
    domain->tuple_id[0] = isl_id_copy(nested->tuple_id[0]);
1373
538
    if (!domain->tuple_id[0])
1374
0
      goto error;
1375
857
  }
1376
857
  if (nested->nested[0]) {
1377
319
    domain->nested[0] = isl_space_copy(nested->nested[0]);
1378
319
    if (!domain->nested[0])
1379
0
      goto error;
1380
857
  }
1381
857
1382
857
  isl_space_free(space);
1383
857
  return domain;
1384
0
error:
1385
0
  isl_space_free(space);
1386
0
  isl_space_free(domain);
1387
0
  return NULL;
1388
857
}
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
20.5k
{
1395
20.5k
  isl_space *nested;
1396
20.5k
  isl_space *range;
1397
20.5k
1398
20.5k
  if (!space)
1399
0
    return NULL;
1400
20.5k
  if (!isl_space_domain_is_wrapping(space))
1401
20.5k
    
isl_die0
(isl_space_get_ctx(space), isl_error_invalid,
1402
20.5k
      "domain not a product", return isl_space_free(space));
1403
20.5k
1404
20.5k
  nested = space->nested[0];
1405
20.5k
  range = isl_space_copy(space);
1406
20.5k
  range = isl_space_drop_dims(range, isl_dim_in, 0, nested->n_in);
1407
20.5k
  if (!range)
1408
0
    return isl_space_free(space);
1409
20.5k
  if (nested->tuple_id[1]) {
1410
12.3k
    range->tuple_id[0] = isl_id_copy(nested->tuple_id[1]);
1411
12.3k
    if (!range->tuple_id[0])
1412
0
      goto error;
1413
20.5k
  }
1414
20.5k
  if (nested->nested[1]) {
1415
8.21k
    range->nested[0] = isl_space_copy(nested->nested[1]);
1416
8.21k
    if (!range->nested[0])
1417
0
      goto error;
1418
20.5k
  }
1419
20.5k
1420
20.5k
  isl_space_free(space);
1421
20.5k
  return range;
1422
0
error:
1423
0
  isl_space_free(space);
1424
0
  isl_space_free(range);
1425
0
  return NULL;
1426
20.5k
}
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
2.22k
{
1435
2.22k
  isl_space *nested;
1436
2.22k
  isl_space *domain;
1437
2.22k
1438
2.22k
  if (!space)
1439
0
    return NULL;
1440
2.22k
1441
2.22k
  nested = space->nested[1];
1442
2.22k
  domain = isl_space_copy(space);
1443
2.22k
  domain = isl_space_drop_dims(domain, isl_dim_out,
1444
2.22k
          nested->n_in, nested->n_out);
1445
2.22k
  if (!domain)
1446
0
    return isl_space_free(space);
1447
2.22k
  if (nested->tuple_id[0]) {
1448
1.26k
    domain->tuple_id[1] = isl_id_copy(nested->tuple_id[0]);
1449
1.26k
    if (!domain->tuple_id[1])
1450
0
      goto error;
1451
2.22k
  }
1452
2.22k
  if (nested->nested[0]) {
1453
940
    domain->nested[1] = isl_space_copy(nested->nested[0]);
1454
940
    if (!domain->nested[1])
1455
0
      goto error;
1456
2.22k
  }
1457
2.22k
1458
2.22k
  isl_space_free(space);
1459
2.22k
  return domain;
1460
0
error:
1461
0
  isl_space_free(space);
1462
0
  isl_space_free(domain);
1463
0
  return NULL;
1464
2.22k
}
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
2.22k
{
1471
2.22k
  if (!space)
1472
0
    return NULL;
1473
2.22k
  if (!isl_space_range_is_wrapping(space))
1474
2.22k
    
isl_die0
(isl_space_get_ctx(space), isl_error_invalid,
1475
2.22k
      "range not a product", return isl_space_free(space));
1476
2.22k
1477
2.22k
  return range_factor_domain(space);
1478
2.22k
}
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
132
{
1498
132
  if (!space)
1499
0
    return NULL;
1500
132
  if (isl_space_is_set(space))
1501
0
    return set_factor_domain(space);
1502
132
  space = isl_space_domain_factor_domain(space);
1503
132
  space = isl_space_range_factor_domain(space);
1504
132
  return space;
1505
132
}
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
13.3k
{
1514
13.3k
  isl_space *nested;
1515
13.3k
  isl_space *range;
1516
13.3k
1517
13.3k
  if (!space)
1518
0
    return NULL;
1519
13.3k
1520
13.3k
  nested = space->nested[1];
1521
13.3k
  range = isl_space_copy(space);
1522
13.3k
  range = isl_space_drop_dims(range, isl_dim_out, 0, nested->n_in);
1523
13.3k
  if (!range)
1524
0
    return isl_space_free(space);
1525
13.3k
  if (nested->tuple_id[1]) {
1526
743
    range->tuple_id[1] = isl_id_copy(nested->tuple_id[1]);
1527
743
    if (!range->tuple_id[1])
1528
0
      goto error;
1529
13.3k
  }
1530
13.3k
  if (nested->nested[1]) {
1531
12.6k
    range->nested[1] = isl_space_copy(nested->nested[1]);
1532
12.6k
    if (!range->nested[1])
1533
0
      goto error;
1534
13.3k
  }
1535
13.3k
1536
13.3k
  isl_space_free(space);
1537
13.3k
  return range;
1538
0
error:
1539
0
  isl_space_free(space);
1540
0
  isl_space_free(range);
1541
0
  return NULL;
1542
13.3k
}
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
13.3k
{
1549
13.3k
  if (!space)
1550
0
    return NULL;
1551
13.3k
  if (!isl_space_range_is_wrapping(space))
1552
13.3k
    
isl_die0
(isl_space_get_ctx(space), isl_error_invalid,
1553
13.3k
      "range not a product", return isl_space_free(space));
1554
13.3k
1555
13.3k
  return range_factor_range(space);
1556
13.3k
}
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
12.6k
{
1576
12.6k
  if (!space)
1577
0
    return NULL;
1578
12.6k
  if (isl_space_is_set(space))
1579
9
    return set_factor_range(space);
1580
12.6k
  space = isl_space_domain_factor_range(space);
1581
12.6k
  space = isl_space_range_factor_range(space);
1582
12.6k
  return space;
1583
12.6k
}
1584
1585
__isl_give isl_space *isl_space_map_from_set(__isl_take isl_space *space)
1586
12.6k
{
1587
12.6k
  isl_ctx *ctx;
1588
12.6k
  isl_id **ids = NULL;
1589
12.6k
  int n_id;
1590
12.6k
1591
12.6k
  if (!space)
1592
7
    return NULL;
1593
12.6k
  ctx = isl_space_get_ctx(space);
1594
12.6k
  if (!isl_space_is_set(space))
1595
12.6k
    
isl_die0
(ctx, isl_error_invalid, "not a set space", goto error);
1596
12.6k
  space = isl_space_cow(space);
1597
12.6k
  if (!space)
1598
0
    return NULL;
1599
12.6k
  n_id = space->nparam + space->n_out + space->n_out;
1600
12.6k
  if (n_id > 0 && 
space->ids12.1k
) {
1601
3.56k
    ids = isl_calloc_array(space->ctx, isl_id *, n_id);
1602
3.56k
    if (!ids)
1603
0
      goto error;
1604
3.56k
    get_ids(space, isl_dim_param, 0, space->nparam, ids);
1605
3.56k
    get_ids(space, isl_dim_out, 0, space->n_out,
1606
3.56k
      ids + space->nparam);
1607
3.56k
  }
1608
12.6k
  space->n_in = space->n_out;
1609
12.6k
  if (ids) {
1610
3.56k
    free(space->ids);
1611
3.56k
    space->ids = ids;
1612
3.56k
    space->n_id = n_id;
1613
3.56k
    space = copy_ids(space, isl_dim_out, 0, space, isl_dim_in);
1614
3.56k
  }
1615
12.6k
  isl_id_free(space->tuple_id[0]);
1616
12.6k
  space->tuple_id[0] = isl_id_copy(space->tuple_id[1]);
1617
12.6k
  isl_space_free(space->nested[0]);
1618
12.6k
  space->nested[0] = isl_space_copy(space->nested[1]);
1619
12.6k
  return space;
1620
0
error:
1621
0
  isl_space_free(space);
1622
0
  return NULL;
1623
12.6k
}
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
84.8k
{
1628
84.8k
  if (!domain || !range)
1629
0
    goto error;
1630
84.8k
  if (!isl_space_is_set(domain))
1631
84.8k
    
isl_die0
(isl_space_get_ctx(domain), isl_error_invalid,
1632
84.8k
      "domain is not a set space", goto error);
1633
84.8k
  if (!isl_space_is_set(range))
1634
84.8k
    
isl_die0
(isl_space_get_ctx(range), isl_error_invalid,
1635
84.8k
      "range is not a set space", goto error);
1636
84.8k
  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
84.8k
}
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
1.45M
{
1647
1.45M
  int i;
1648
1.45M
1649
3.53M
  for (i = 0; i < n ; 
++i2.08M
)
1650
2.08M
    dim = set_id(dim, type, first + i, ids[i]);
1651
1.45M
1652
1.45M
  return dim;
1653
1.45M
}
1654
1655
__isl_give isl_space *isl_space_reverse(__isl_take isl_space *dim)
1656
1.95M
{
1657
1.95M
  unsigned t;
1658
1.95M
  isl_space *nested;
1659
1.95M
  isl_id **ids = NULL;
1660
1.95M
  isl_id *id;
1661
1.95M
1662
1.95M
  if (!dim)
1663
1
    return NULL;
1664
1.95M
  if (match(dim, isl_dim_in, dim, isl_dim_out))
1665
116k
    return dim;
1666
1.84M
1667
1.84M
  dim = isl_space_cow(dim);
1668
1.84M
  if (!dim)
1669
0
    return NULL;
1670
1.84M
1671
1.84M
  id = dim->tuple_id[0];
1672
1.84M
  dim->tuple_id[0] = dim->tuple_id[1];
1673
1.84M
  dim->tuple_id[1] = id;
1674
1.84M
1675
1.84M
  nested = dim->nested[0];
1676
1.84M
  dim->nested[0] = dim->nested[1];
1677
1.84M
  dim->nested[1] = nested;
1678
1.84M
1679
1.84M
  if (dim->ids) {
1680
725k
    int n_id = dim->n_in + dim->n_out;
1681
725k
    ids = isl_alloc_array(dim->ctx, isl_id *, n_id);
1682
725k
    if (n_id && 
!ids611k
)
1683
0
      goto error;
1684
725k
    get_ids(dim, isl_dim_in, 0, dim->n_in, ids);
1685
725k
    get_ids(dim, isl_dim_out, 0, dim->n_out, ids + dim->n_in);
1686
725k
  }
1687
1.84M
1688
1.84M
  t = dim->n_in;
1689
1.84M
  dim->n_in = dim->n_out;
1690
1.84M
  dim->n_out = t;
1691
1.84M
1692
1.84M
  if (dim->ids) {
1693
725k
    dim = set_ids(dim, isl_dim_out, 0, dim->n_out, ids);
1694
725k
    dim = set_ids(dim, isl_dim_in, 0, dim->n_in, ids + dim->n_out);
1695
725k
    free(ids);
1696
725k
  }
1697
1.84M
1698
1.84M
  return dim;
1699
0
error:
1700
0
  free(ids);
1701
0
  isl_space_free(dim);
1702
0
  return NULL;
1703
1.84M
}
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
2.26M
{
1708
2.26M
  int i;
1709
2.26M
1710
2.26M
  if (!dim)
1711
2
    return NULL;
1712
2.26M
1713
2.26M
  if (num == 0)
1714
383k
    return isl_space_reset(dim, type);
1715
1.88M
1716
1.88M
  if (!valid_dim_type(type))
1717
1.88M
    
isl_die0
(dim->ctx, isl_error_invalid,
1718
1.88M
      "cannot drop dimensions of specified type", goto error);
1719
1.88M
1720
1.88M
  if (first + num > n(dim, type) || first + num < first)
1721
1.88M
    
isl_die0
(isl_space_get_ctx(dim), isl_error_invalid,
1722
1.88M
      "index out of bounds", return isl_space_free(dim));
1723
1.88M
  dim = isl_space_cow(dim);
1724
1.88M
  if (!dim)
1725
2
    goto error;
1726
1.88M
  if (dim->ids) {
1727
534k
    dim = extend_ids(dim);
1728
534k
    if (!dim)
1729
0
      goto error;
1730
1.73M
    
for (i = 0; 534k
i < num;
++i1.20M
)
1731
1.20M
      isl_id_free(get_id(dim, type, first + i));
1732
593k
    for (i = first+num; i < n(dim, type); 
++i58.7k
)
1733
58.7k
      set_id(dim, type, i - num, get_id(dim, type, i));
1734
534k
    switch (type) {
1735
534k
    case isl_dim_param:
1736
30.2k
      get_ids(dim, isl_dim_in, 0, dim->n_in,
1737
30.2k
        dim->ids + offset(dim, isl_dim_in) - num);
1738
177k
    case isl_dim_in:
1739
177k
      get_ids(dim, isl_dim_out, 0, dim->n_out,
1740
177k
        dim->ids + offset(dim, isl_dim_out) - num);
1741
534k
    default:
1742
534k
      ;
1743
534k
    }
1744
534k
    dim->n_id -= num;
1745
534k
  }
1746
1.88M
  switch (type) {
1747
1.88M
  
case isl_dim_param: dim->nparam -= num; break30.9k
;
1748
1.88M
  
case isl_dim_in: dim->n_in -= num; break412k
;
1749
1.88M
  
case isl_dim_out: dim->n_out -= num; break1.44M
;
1750
1.88M
  
default: ;0
1751
1.88M
  }
1752
1.88M
  dim = isl_space_reset(dim, type);
1753
1.88M
  if (type == isl_dim_param) {
1754
30.9k
    if (dim && dim->nested[0] &&
1755
30.9k
        !(dim->nested[0] = isl_space_drop_dims(dim->nested[0],
1756
122
                isl_dim_param, first, num)))
1757
0
      goto error;
1758
30.9k
    if (dim && dim->nested[1] &&
1759
30.9k
        !(dim->nested[1] = isl_space_drop_dims(dim->nested[1],
1760
597
                isl_dim_param, first, num)))
1761
0
      goto error;
1762
1.88M
  }
1763
1.88M
  return dim;
1764
2
error:
1765
2
  isl_space_free(dim);
1766
2
  return NULL;
1767
1.88M
}
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
715k
{
1787
715k
  if (!space)
1788
1
    return NULL;
1789
715k
  space = isl_space_drop_dims(space, isl_dim_out, 0, space->n_out);
1790
715k
  space = isl_space_reverse(space);
1791
715k
  space = mark_as_set(space);
1792
715k
  return space;
1793
715k
}
1794
1795
__isl_give isl_space *isl_space_from_domain(__isl_take isl_space *dim)
1796
659k
{
1797
659k
  if (!dim)
1798
2
    return NULL;
1799
659k
  if (!isl_space_is_set(dim))
1800
659k
    
isl_die0
(isl_space_get_ctx(dim), isl_error_invalid,
1801
659k
      "not a set space", goto error);
1802
659k
  dim = isl_space_reverse(dim);
1803
659k
  dim = isl_space_reset(dim, isl_dim_out);
1804
659k
  return dim;
1805
0
error:
1806
0
  isl_space_free(dim);
1807
0
  return NULL;
1808
659k
}
1809
1810
__isl_give isl_space *isl_space_range(__isl_take isl_space *space)
1811
356k
{
1812
356k
  if (!space)
1813
3
    return NULL;
1814
356k
  space = isl_space_drop_dims(space, isl_dim_in, 0, space->n_in);
1815
356k
  space = mark_as_set(space);
1816
356k
  return space;
1817
356k
}
1818
1819
__isl_give isl_space *isl_space_from_range(__isl_take isl_space *dim)
1820
200k
{
1821
200k
  if (!dim)
1822
5
    return NULL;
1823
200k
  if (!isl_space_is_set(dim))
1824
200k
    
isl_die0
(isl_space_get_ctx(dim), isl_error_invalid,
1825
200k
      "not a set space", goto error);
1826
200k
  return isl_space_reset(dim, isl_dim_in);
1827
0
error:
1828
0
  isl_space_free(dim);
1829
0
  return NULL;
1830
200k
}
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
1.91k
{
1836
1.91k
  isl_space *domain;
1837
1.91k
1838
1.91k
  domain = isl_space_from_range(isl_space_domain(isl_space_copy(space)));
1839
1.91k
  space = isl_space_from_domain(isl_space_wrap(space));
1840
1.91k
  space = isl_space_join(space, domain);
1841
1.91k
1842
1.91k
  return space;
1843
1.91k
}
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
550k
{
1860
550k
  if (isl_space_is_params(space))
1861
379k
    return space;
1862
170k
  space = isl_space_drop_dims(space,
1863
170k
          isl_dim_in, 0, isl_space_dim(space, isl_dim_in));
1864
170k
  space = isl_space_drop_dims(space,
1865
170k
          isl_dim_out, 0, isl_space_dim(space, isl_dim_out));
1866
170k
  space = mark_as_params(space);
1867
170k
  return space;
1868
170k
}
1869
1870
__isl_give isl_space *isl_space_set_from_params(__isl_take isl_space *space)
1871
17.0k
{
1872
17.0k
  if (!space)
1873
0
    return NULL;
1874
17.0k
  if (!isl_space_is_params(space))
1875
17.0k
    
isl_die0
(isl_space_get_ctx(space), isl_error_invalid,
1876
17.0k
      "not a parameter space", goto error);
1877
17.0k
  return isl_space_reset(space, isl_dim_set);
1878
0
error:
1879
0
  isl_space_free(space);
1880
0
  return NULL;
1881
17.0k
}
1882
1883
__isl_give isl_space *isl_space_underlying(__isl_take isl_space *dim,
1884
  unsigned n_div)
1885
441k
{
1886
441k
  int i;
1887
441k
1888
441k
  if (!dim)
1889
0
    return NULL;
1890
441k
  if (n_div == 0 &&
1891
441k
      
dim->nparam == 0421k
&&
dim->n_in == 0238k
&&
dim->n_id == 0190k
)
1892
173k
    return isl_space_reset(isl_space_reset(dim, isl_dim_in), isl_dim_out);
1893
268k
  dim = isl_space_cow(dim);
1894
268k
  if (!dim)
1895
0
    return NULL;
1896
268k
  dim->n_out += dim->nparam + dim->n_in + n_div;
1897
268k
  dim->nparam = 0;
1898
268k
  dim->n_in = 0;
1899
268k
1900
1.40M
  for (i = 0; i < dim->n_id; 
++i1.13M
)
1901
1.13M
    isl_id_free(get_id(dim, isl_dim_out, i));
1902
268k
  dim->n_id = 0;
1903
268k
  dim = isl_space_reset(dim, isl_dim_in);
1904
268k
  dim = isl_space_reset(dim, isl_dim_out);
1905
268k
1906
268k
  return dim;
1907
268k
}
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
7.86M
{
1914
7.86M
  isl_bool equal;
1915
7.86M
1916
7.86M
  if (!space1 || !space2)
1917
1
    return isl_bool_error;
1918
7.86M
  if (space1 == space2)
1919
4.41M
    return isl_bool_true;
1920
3.44M
  equal = isl_space_has_equal_params(space1, space2);
1921
3.44M
  if (equal < 0 || !equal)
1922
68.4k
    return equal;
1923
3.37M
  return isl_space_has_equal_tuples(space1, space2);
1924
3.37M
}
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
106k
{
1935
106k
  isl_bool is_set;
1936
106k
1937
106k
  is_set = isl_space_is_set(space1);
1938
106k
  if (is_set < 0 || !is_set)
1939
0
    return is_set;
1940
106k
  return isl_space_tuple_is_equal(space1, isl_dim_set,
1941
106k
          space2, isl_dim_in);
1942
106k
}
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
106k
{
1952
106k
  isl_bool equal_params;
1953
106k
1954
106k
  if (!space1 || !space2)
1955
0
    return isl_bool_error;
1956
106k
  equal_params = isl_space_has_equal_params(space1, space2);
1957
106k
  if (equal_params < 0 || !equal_params)
1958
0
    return equal_params;
1959
106k
  return isl_space_has_domain_tuples(space1, space2);
1960
106k
}
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
25
{
1967
25
  if (!space2)
1968
0
    return isl_bool_error;
1969
25
  if (!isl_space_is_map(space2))
1970
0
    return isl_bool_false;
1971
25
  return isl_space_is_domain_internal(space1, space2);
1972
25
}
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
17.6k
{
1982
17.6k
  isl_bool equal_params;
1983
17.6k
1984
17.6k
  if (!space1 || !space2)
1985
0
    return isl_bool_error;
1986
17.6k
  if (!isl_space_is_set(space1))
1987
0
    return isl_bool_false;
1988
17.6k
  equal_params = isl_space_has_equal_params(space1, space2);
1989
17.6k
  if (equal_params < 0 || !equal_params)
1990
0
    return equal_params;
1991
17.6k
  return isl_space_tuple_is_equal(space1, isl_dim_set,
1992
17.6k
          space2, isl_dim_out);
1993
17.6k
}
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
550k
{
2011
550k
  int i;
2012
550k
  isl_id *id;
2013
550k
2014
550k
  if (!space)
2015
0
    return hash;
2016
550k
2017
550k
  isl_hash_byte(hash, space->nparam % 256);
2018
550k
2019
1.29M
  for (i = 0; i < space->nparam; 
++i739k
) {
2020
739k
    id = get_id(space, isl_dim_param, i);
2021
739k
    hash = isl_hash_id(hash, id);
2022
739k
  }
2023
550k
2024
550k
  return hash;
2025
550k
}
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
2.13M
{
2032
2.13M
  isl_id *id;
2033
2.13M
2034
2.13M
  if (!space)
2035
1.34M
    return hash;
2036
791k
2037
791k
  isl_hash_byte(hash, space->n_in % 256);
2038
791k
  isl_hash_byte(hash, space->n_out % 256);
2039
791k
2040
791k
  id = tuple_id(space, isl_dim_in);
2041
791k
  hash = isl_hash_id(hash, id);
2042
791k
  id = tuple_id(space, isl_dim_out);
2043
791k
  hash = isl_hash_id(hash, id);
2044
791k
2045
791k
  hash = isl_hash_tuples(hash, space->nested[0]);
2046
791k
  hash = isl_hash_tuples(hash, space->nested[1]);
2047
791k
2048
791k
  return hash;
2049
791k
}
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
46.0k
{
2058
46.0k
  isl_id *id;
2059
46.0k
2060
46.0k
  if (!space)
2061
0
    return hash;
2062
46.0k
2063
46.0k
  isl_hash_byte(hash, 0);
2064
46.0k
  isl_hash_byte(hash, space->n_in % 256);
2065
46.0k
2066
46.0k
  hash = isl_hash_id(hash, &isl_id_none);
2067
46.0k
  id = tuple_id(space, isl_dim_in);
2068
46.0k
  hash = isl_hash_id(hash, id);
2069
46.0k
2070
46.0k
  hash = isl_hash_tuples(hash, space->nested[0]);
2071
46.0k
2072
46.0k
  return hash;
2073
46.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
2.24k
{
2080
2.24k
  uint32_t hash;
2081
2.24k
2082
2.24k
  if (!space)
2083
0
    return 0;
2084
2.24k
2085
2.24k
  hash = isl_hash_init();
2086
2.24k
  hash = isl_hash_tuples(hash, space);
2087
2.24k
2088
2.24k
  return hash;
2089
2.24k
}
2090
2091
uint32_t isl_space_get_hash(__isl_keep isl_space *space)
2092
504k
{
2093
504k
  uint32_t hash;
2094
504k
2095
504k
  if (!space)
2096
0
    return 0;
2097
504k
2098
504k
  hash = isl_hash_init();
2099
504k
  hash = isl_hash_params(hash, space);
2100
504k
  hash = isl_hash_tuples(hash, space);
2101
504k
2102
504k
  return hash;
2103
504k
}
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
46.0k
{
2111
46.0k
  uint32_t hash;
2112
46.0k
2113
46.0k
  if (!space)
2114
0
    return 0;
2115
46.0k
2116
46.0k
  hash = isl_hash_init();
2117
46.0k
  hash = isl_hash_params(hash, space);
2118
46.0k
  hash = isl_hash_tuples_domain(hash, space);
2119
46.0k
2120
46.0k
  return hash;
2121
46.0k
}
2122
2123
isl_bool isl_space_is_wrapping(__isl_keep isl_space *dim)
2124
57.7k
{
2125
57.7k
  if (!dim)
2126
0
    return isl_bool_error;
2127
57.7k
2128
57.7k
  if (!isl_space_is_set(dim))
2129
0
    return isl_bool_false;
2130
57.7k
2131
57.7k
  return dim->nested[1] != NULL;
2132
57.7k
}
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
49.7k
{
2138
49.7k
  if (!space)
2139
0
    return isl_bool_error;
2140
49.7k
2141
49.7k
  if (isl_space_is_set(space))
2142
0
    return isl_bool_false;
2143
49.7k
2144
49.7k
  return space->nested[0] != NULL;
2145
49.7k
}
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
66.4k
{
2151
66.4k
  if (!space)
2152
0
    return isl_bool_error;
2153
66.4k
2154
66.4k
  if (isl_space_is_set(space))
2155
781
    return isl_bool_false;
2156
65.7k
2157
65.7k
  return space->nested[1] != NULL;
2158
65.7k
}
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
6.23k
{
2166
6.23k
  isl_bool is_set;
2167
6.23k
  isl_bool is_product;
2168
6.23k
2169
6.23k
  is_set = isl_space_is_set(space);
2170
6.23k
  if (is_set < 0)
2171
0
    return isl_bool_error;
2172
6.23k
  if (is_set)
2173
0
    return isl_space_is_wrapping(space);
2174
6.23k
  is_product = isl_space_domain_is_wrapping(space);
2175
6.23k
  if (is_product < 0 || !is_product)
2176
35
    return is_product;
2177
6.20k
  return isl_space_range_is_wrapping(space);
2178
6.20k
}
2179
2180
__isl_give isl_space *isl_space_wrap(__isl_take isl_space *dim)
2181
209k
{
2182
209k
  isl_space *wrap;
2183
209k
2184
209k
  if (!dim)
2185
1
    return NULL;
2186
209k
2187
209k
  wrap = isl_space_set_alloc(dim->ctx,
2188
209k
            dim->nparam, dim->n_in + dim->n_out);
2189
209k
2190
209k
  wrap = copy_ids(wrap, isl_dim_param, 0, dim, isl_dim_param);
2191
209k
  wrap = copy_ids(wrap, isl_dim_set, 0, dim, isl_dim_in);
2192
209k
  wrap = copy_ids(wrap, isl_dim_set, dim->n_in, dim, isl_dim_out);
2193
209k
2194
209k
  if (!wrap)
2195
0
    goto error;
2196
209k
2197
209k
  wrap->nested[1] = dim;
2198
209k
2199
209k
  return wrap;
2200
0
error:
2201
0
  isl_space_free(dim);
2202
0
  return NULL;
2203
209k
}
2204
2205
__isl_give isl_space *isl_space_unwrap(__isl_take isl_space *dim)
2206
39.9k
{
2207
39.9k
  isl_space *unwrap;
2208
39.9k
2209
39.9k
  if (!dim)
2210
1
    return NULL;
2211
39.9k
2212
39.9k
  if (!isl_space_is_wrapping(dim))
2213
39.9k
    
isl_die0
(dim->ctx, isl_error_invalid, "not a wrapping space",
2214
39.9k
      goto error);
2215
39.9k
2216
39.9k
  unwrap = isl_space_copy(dim->nested[1]);
2217
39.9k
  isl_space_free(dim);
2218
39.9k
2219
39.9k
  return unwrap;
2220
0
error:
2221
0
  isl_space_free(dim);
2222
0
  return NULL;
2223
39.9k
}
2224
2225
isl_bool isl_space_is_named_or_nested(__isl_keep isl_space *space,
2226
  enum isl_dim_type type)
2227
5.54M
{
2228
5.54M
  if (type != isl_dim_in && 
type != isl_dim_out4.01M
)
2229
208k
    return isl_bool_false;
2230
5.33M
  if (!space)
2231
9
    return isl_bool_error;
2232
5.33M
  if (space->tuple_id[type - isl_dim_in])
2233
2.23M
    return isl_bool_true;
2234
3.10M
  if (space->nested[type - isl_dim_in])
2235
785k
    return isl_bool_true;
2236
2.32M
  return isl_bool_false;
2237
2.32M
}
2238
2239
isl_bool isl_space_may_be_set(__isl_keep isl_space *space)
2240
278
{
2241
278
  isl_bool nested;
2242
278
2243
278
  if (!space)
2244
0
    return isl_bool_error;
2245
278
  if (isl_space_is_set(space))
2246
173
    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
5.05M
{
2258
5.05M
  if (!isl_space_is_named_or_nested(dim, type))
2259
2.26M
    return dim;
2260
2.78M
2261
2.78M
  dim = isl_space_cow(dim);
2262
2.78M
  if (!dim)
2263
19
    return NULL;
2264
2.78M
2265
2.78M
  isl_id_free(dim->tuple_id[type - isl_dim_in]);
2266
2.78M
  dim->tuple_id[type - isl_dim_in] = NULL;
2267
2.78M
  isl_space_free(dim->nested[type - isl_dim_in]);
2268
2.78M
  dim->nested[type - isl_dim_in] = NULL;
2269
2.78M
2270
2.78M
  return dim;
2271
2.78M
}
2272
2273
__isl_give isl_space *isl_space_flatten(__isl_take isl_space *dim)
2274
22.9k
{
2275
22.9k
  if (!dim)
2276
0
    return NULL;
2277
22.9k
  if (!dim->nested[0] && !dim->nested[1])
2278
0
    return dim;
2279
22.9k
2280
22.9k
  if (dim->nested[0])
2281
0
    dim = isl_space_reset(dim, isl_dim_in);
2282
22.9k
  if (dim && dim->nested[1])
2283
22.9k
    dim = isl_space_reset(dim, isl_dim_out);
2284
22.9k
2285
22.9k
  return dim;
2286
22.9k
}
2287
2288
__isl_give isl_space *isl_space_flatten_domain(__isl_take isl_space *space)
2289
36
{
2290
36
  if (!space)
2291
0
    return NULL;
2292
36
  if (!space->nested[0])
2293
0
    return space;
2294
36
2295
36
  return isl_space_reset(space, isl_dim_in);
2296
36
}
2297
2298
__isl_give isl_space *isl_space_flatten_range(__isl_take isl_space *space)
2299
75.9k
{
2300
75.9k
  if (!space)
2301
0
    return NULL;
2302
75.9k
  if (!space->nested[1])
2303
0
    return space;
2304
75.9k
2305
75.9k
  return isl_space_reset(space, isl_dim_out);
2306
75.9k
}
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
124k
{
2313
124k
  isl_bool equal_params;
2314
124k
  enum isl_dim_type type = isl_dim_param;
2315
124k
2316
124k
  equal_params = isl_space_has_equal_params(dst, src);
2317
124k
  if (equal_params < 0)
2318
0
    return isl_space_free(dst);
2319
124k
  if (equal_params)
2320
57.9k
    return dst;
2321
66.0k
2322
66.0k
  dst = isl_space_cow(dst);
2323
66.0k
2324
66.0k
  if (!dst || !src)
2325
0
    goto error;
2326
66.0k
2327
66.0k
  dst = isl_space_drop_dims(dst, type, 0, isl_space_dim(dst, type));
2328
66.0k
  dst = isl_space_add_dims(dst, type, isl_space_dim(src, type));
2329
66.0k
  dst = copy_ids(dst, type, 0, src, type);
2330
66.0k
2331
66.0k
  if (dst) {
2332
66.0k
    int i;
2333
198k
    for (i = 0; i <= 1; 
++i132k
) {
2334
132k
      if (!dst->nested[i])
2335
131k
        continue;
2336
471
      dst->nested[i] = isl_space_replace_params(
2337
471
              dst->nested[i], src);
2338
471
      if (!dst->nested[i])
2339
0
        goto error;
2340
471
    }
2341
66.0k
  }
2342
66.0k
2343
66.0k
  return dst;
2344
0
error:
2345
0
  isl_space_free(dst);
2346
0
  return NULL;
2347
66.0k
}
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
20.5k
{
2356
20.5k
  isl_space *local_dim;
2357
20.5k
2358
20.5k
  if (!dim)
2359
0
    return NULL;
2360
20.5k
2361
20.5k
  local_dim = isl_space_dup(dim);
2362
20.5k
  local_dim = isl_space_drop_dims(local_dim, isl_dim_set, 0, dim->n_out);
2363
20.5k
  local_dim = isl_space_add_dims(local_dim, isl_dim_set, n_local);
2364
20.5k
  local_dim = isl_space_set_tuple_name(local_dim, isl_dim_set, "local");
2365
20.5k
  dim = isl_space_join(isl_space_from_domain(dim),
2366
20.5k
          isl_space_from_range(local_dim));
2367
20.5k
  dim = isl_space_wrap(dim);
2368
20.5k
  dim = isl_space_set_tuple_name(dim, isl_dim_set, "lifted");
2369
20.5k
2370
20.5k
  return dim;
2371
20.5k
}
2372
2373
isl_bool isl_space_can_zip(__isl_keep isl_space *space)
2374
6.14k
{
2375
6.14k
  isl_bool is_set;
2376
6.14k
2377
6.14k
  is_set = isl_space_is_set(space);
2378
6.14k
  if (is_set < 0)
2379
0
    return isl_bool_error;
2380
6.14k
  if (is_set)
2381
0
    return isl_bool_false;
2382
6.14k
  return isl_space_is_product(space);
2383
6.14k
}
2384
2385
__isl_give isl_space *isl_space_zip(__isl_take isl_space *dim)
2386
2.63k
{
2387
2.63k
  isl_space *dom, *ran;
2388
2.63k
  isl_space *dom_dom, *dom_ran, *ran_dom, *ran_ran;
2389
2.63k
2390
2.63k
  if (!isl_space_can_zip(dim))
2391
2.63k
    
isl_die0
(dim->ctx, isl_error_invalid, "dim cannot be zipped",
2392
2.63k
      goto error);
2393
2.63k
2394
2.63k
  if (!dim)
2395
0
    return NULL;
2396
2.63k
  dom = isl_space_unwrap(isl_space_domain(isl_space_copy(dim)));
2397
2.63k
  ran = isl_space_unwrap(isl_space_range(dim));
2398
2.63k
  dom_dom = isl_space_domain(isl_space_copy(dom));
2399
2.63k
  dom_ran = isl_space_range(dom);
2400
2.63k
  ran_dom = isl_space_domain(isl_space_copy(ran));
2401
2.63k
  ran_ran = isl_space_range(ran);
2402
2.63k
  dom = isl_space_join(isl_space_from_domain(dom_dom),
2403
2.63k
         isl_space_from_range(ran_dom));
2404
2.63k
  ran = isl_space_join(isl_space_from_domain(dom_ran),
2405
2.63k
         isl_space_from_range(ran_ran));
2406
2.63k
  return isl_space_join(isl_space_from_domain(isl_space_wrap(dom)),
2407
2.63k
          isl_space_from_range(isl_space_wrap(ran)));
2408
0
error:
2409
0
  isl_space_free(dim);
2410
0
  return NULL;
2411
2.63k
}
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
41.1k
{
2418
41.1k
  if (!space)
2419
0
    return isl_bool_error;
2420
41.1k
2421
41.1k
  return !!space->nested[0];
2422
41.1k
}
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
13.3k
{
2429
13.3k
  isl_space *dom, *ran;
2430
13.3k
  isl_space *dom_dom, *dom_ran;
2431
13.3k
2432
13.3k
  if (!space)
2433
0
    return NULL;
2434
13.3k
2435
13.3k
  if (!isl_space_can_curry(space))
2436
13.3k
    
isl_die0
(space->ctx, isl_error_invalid,
2437
13.3k
      "space cannot be curried", goto error);
2438
13.3k
2439
13.3k
  dom = isl_space_unwrap(isl_space_domain(isl_space_copy(space)));
2440
13.3k
  ran = isl_space_range(space);
2441
13.3k
  dom_dom = isl_space_domain(isl_space_copy(dom));
2442
13.3k
  dom_ran = isl_space_range(dom);
2443
13.3k
  ran = isl_space_join(isl_space_from_domain(dom_ran),
2444
13.3k
         isl_space_from_range(ran));
2445
13.3k
  return isl_space_join(isl_space_from_domain(dom_dom),
2446
13.3k
          isl_space_from_range(isl_space_wrap(ran)));
2447
0
error:
2448
0
  isl_space_free(space);
2449
0
  return NULL;
2450
13.3k
}
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
25.3k
{
2458
25.3k
  isl_bool can;
2459
25.3k
2460
25.3k
  if (!space)
2461
0
    return isl_bool_error;
2462
25.3k
  can = isl_space_range_is_wrapping(space);
2463
25.3k
  if (can < 0 || !can)
2464
0
    return can;
2465
25.3k
  return isl_space_can_curry(space->nested[1]);
2466
25.3k
}
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
12.6k
{
2473
12.6k
  if (!space)
2474
0
    return NULL;
2475
12.6k
2476
12.6k
  if (!isl_space_can_range_curry(space))
2477
12.6k
    
isl_die0
(isl_space_get_ctx(space), isl_error_invalid,
2478
12.6k
      "space range cannot be curried",
2479
12.6k
      return isl_space_free(space));
2480
12.6k
2481
12.6k
  space = isl_space_cow(space);
2482
12.6k
  if (!space)
2483
0
    return NULL;
2484
12.6k
  space->nested[1] = isl_space_curry(space->nested[1]);
2485
12.6k
  if (!space->nested[1])
2486
0
    return isl_space_free(space);
2487
12.6k
2488
12.6k
  return space;
2489
12.6k
}
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
1.60k
{
2496
1.60k
  if (!space)
2497
0
    return isl_bool_error;
2498
1.60k
2499
1.60k
  return !!space->nested[1];
2500
1.60k
}
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
564
{
2507
564
  isl_space *dom, *ran;
2508
564
  isl_space *ran_dom, *ran_ran;
2509
564
2510
564
  if (!space)
2511
0
    return NULL;
2512
564
2513
564
  if (!isl_space_can_uncurry(space))
2514
564
    
isl_die0
(space->ctx, isl_error_invalid,
2515
564
      "space cannot be uncurried",
2516
564
      return isl_space_free(space));
2517
564
2518
564
  dom = isl_space_domain(isl_space_copy(space));
2519
564
  ran = isl_space_unwrap(isl_space_range(space));
2520
564
  ran_dom = isl_space_domain(isl_space_copy(ran));
2521
564
  ran_ran = isl_space_range(ran);
2522
564
  dom = isl_space_join(isl_space_from_domain(dom),
2523
564
         isl_space_from_range(ran_dom));
2524
564
  return isl_space_join(isl_space_from_domain(isl_space_wrap(dom)),
2525
564
          isl_space_from_range(ran_ran));
2526
564
}
2527
2528
isl_bool isl_space_has_named_params(__isl_keep isl_space *space)
2529
526k
{
2530
526k
  int i;
2531
526k
  unsigned off;
2532
526k
2533
526k
  if (!space)
2534
2
    return isl_bool_error;
2535
526k
  if (space->nparam == 0)
2536
253k
    return isl_bool_true;
2537
272k
  off = isl_space_offset(space, isl_dim_param);
2538
272k
  if (off + space->nparam > space->n_id)
2539
0
    return isl_bool_false;
2540
1.09M
  
for (i = 0; 272k
i < space->nparam;
++i827k
)
2541
827k
    if (!space->ids[off + i])
2542
0
      return isl_bool_false;
2543
272k
  return isl_bool_true;
2544
272k
}
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
338k
{
2551
338k
  isl_bool named;
2552
338k
2553
338k
  named = isl_space_has_named_params(space);
2554
338k
  if (named < 0)
2555
2
    return isl_stat_error;
2556
338k
  if (!named)
2557
338k
    
isl_die0
(isl_space_get_ctx(space), isl_error_invalid,
2558
338k
      "unexpected unnamed parameters", return isl_stat_error);
2559
338k
2560
338k
  return isl_stat_ok;
2561
338k
}
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
35.3k
{
2568
35.3k
  isl_reordering *exp;
2569
35.3k
2570
35.3k
  if (isl_space_check_named_params(space1) < 0 ||
2571
35.3k
      
isl_space_check_named_params(space2) < 035.3k
)
2572
2
    goto error;
2573
35.3k
2574
35.3k
  exp = isl_parameter_alignment_reordering(space1, space2);
2575
35.3k
  exp = isl_reordering_extend_space(exp, space1);
2576
35.3k
  isl_space_free(space2);
2577
35.3k
  space1 = isl_reordering_get_space(exp);
2578
35.3k
  isl_reordering_free(exp);
2579
35.3k
  return space1;
2580
2
error:
2581
2
  isl_space_free(space1);
2582
2
  isl_space_free(space2);
2583
2
  return NULL;
2584
35.3k
}
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
73.9k
{
2592
73.9k
  if (!model)
2593
0
    goto error;
2594
73.9k
2595
73.9k
  space = isl_space_from_domain(space);
2596
73.9k
  space = isl_space_add_dims(space, isl_dim_out,
2597
73.9k
            isl_space_dim(model, isl_dim_out));
2598
73.9k
  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
73.9k
  if (!space)
2602
0
    goto error;
2603
73.9k
  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
73.9k
  isl_space_free(model);
2617
73.9k
  return space;
2618
0
error:
2619
0
  isl_space_free(model);
2620
0
  isl_space_free(space);
2621
0
  return NULL;
2622
73.9k
}
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.91M
{
2631
1.91M
  int cmp;
2632
1.91M
  isl_space *nested1, *nested2;
2633
1.91M
2634
1.91M
  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.91M
2638
1.91M
  cmp = isl_id_cmp(tuple_id(space1, type), tuple_id(space2, type));
2639
1.91M
  if (cmp != 0)
2640
0
    return cmp;
2641
1.91M
2642
1.91M
  nested1 = nested(space1, type);
2643
1.91M
  nested2 = nested(space2, type);
2644
1.91M
  if (!nested1 != !nested2)
2645
0
    return !nested1 - !nested2;
2646
1.91M
2647
1.91M
  if (nested1)
2648
245k
    return isl_space_cmp(nested1, nested2);
2649
1.66M
2650
1.66M
  return 0;
2651
1.66M
}
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.84M
{
2659
1.84M
  int i;
2660
1.84M
  int cmp;
2661
1.84M
2662
1.84M
  if (space1 == space2)
2663
1.20M
    return 0;
2664
637k
  if (!space1)
2665
0
    return -1;
2666
637k
  if (!space2)
2667
0
    return 1;
2668
637k
2669
637k
  cmp = isl_space_cmp_type(space1, space2, isl_dim_param);
2670
637k
  if (cmp != 0)
2671
0
    return cmp;
2672
637k
  cmp = isl_space_cmp_type(space1, space2, isl_dim_in);
2673
637k
  if (cmp != 0)
2674
0
    return cmp;
2675
637k
  cmp = isl_space_cmp_type(space1, space2, isl_dim_out);
2676
637k
  if (cmp != 0)
2677
0
    return cmp;
2678
637k
2679
637k
  if (!space1->ids && 
!space2->ids199k
)
2680
198k
    return 0;
2681
438k
2682
3.92M
  
for (i = 0; 438k
i < n(space1, isl_dim_param);
++i3.48M
) {
2683
3.48M
    cmp = isl_id_cmp(get_id(space1, isl_dim_param, i),
2684
3.48M
         get_id(space2, isl_dim_param, i));
2685
3.48M
    if (cmp != 0)
2686
0
      return cmp;
2687
3.48M
  }
2688
438k
2689
438k
  return 0;
2690
438k
}