Coverage Report

Created: 2018-06-24 14:39

/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.53M
{
23
1.53M
  return dim ? dim->ctx : NULL;
24
1.53M
}
25
26
__isl_give isl_space *isl_space_alloc(isl_ctx *ctx,
27
      unsigned nparam, unsigned n_in, unsigned n_out)
28
5.10M
{
29
5.10M
  isl_space *dim;
30
5.10M
31
5.10M
  dim = isl_alloc_type(ctx, struct isl_space);
32
5.10M
  if (!dim)
33
25
    return NULL;
34
5.10M
35
5.10M
  dim->ctx = ctx;
36
5.10M
  isl_ctx_ref(ctx);
37
5.10M
  dim->ref = 1;
38
5.10M
  dim->nparam = nparam;
39
5.10M
  dim->n_in = n_in;
40
5.10M
  dim->n_out = n_out;
41
5.10M
42
5.10M
  dim->tuple_id[0] = NULL;
43
5.10M
  dim->tuple_id[1] = NULL;
44
5.10M
45
5.10M
  dim->nested[0] = NULL;
46
5.10M
  dim->nested[1] = NULL;
47
5.10M
48
5.10M
  dim->n_id = 0;
49
5.10M
  dim->ids = NULL;
50
5.10M
51
5.10M
  return dim;
52
5.10M
}
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.62M
{
59
1.62M
  space = isl_space_cow(space);
60
1.62M
  if (!space)
61
16
    return NULL;
62
1.62M
  space = isl_space_set_tuple_id(space, isl_dim_in, &isl_id_none);
63
1.62M
  return space;
64
1.62M
}
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
303k
    return isl_bool_false;
74
1.47M
  if (space->tuple_id[0] != &isl_id_none)
75
16.7k
    return isl_bool_false;
76
1.46M
  return isl_bool_true;
77
1.46M
}
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.3k
{
83
30.3k
  if (!space)
84
0
    return isl_bool_error;
85
30.3k
  return space->tuple_id[0] != &isl_id_none &&
86
30.3k
    
space->tuple_id[1] != &isl_id_none805
;
87
30.3k
}
88
89
__isl_give isl_space *isl_space_set_alloc(isl_ctx *ctx,
90
      unsigned nparam, unsigned dim)
91
559k
{
92
559k
  isl_space *space;
93
559k
  space = isl_space_alloc(ctx, nparam, 0, dim);
94
559k
  space = mark_as_set(space);
95
559k
  return space;
96
559k
}
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
767k
{
114
767k
  if (!space)
115
27
    return isl_bool_error;
116
767k
  if (space->n_in != 0 || 
space->nested[0]622k
||
117
767k
      
space->n_out != 0621k
||
space->nested[1]468k
)
118
299k
    return isl_bool_false;
119
467k
  if (space->tuple_id[0] != &isl_id_none)
120
25.9k
    return isl_bool_false;
121
441k
  if (space->tuple_id[1] != &isl_id_none)
122
39.1k
    return isl_bool_false;
123
402k
  return isl_bool_true;
124
402k
}
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
51.3M
{
139
51.3M
  struct isl_ctx *ctx = dim->ctx;
140
51.3M
141
51.3M
  switch (type) {
142
51.3M
  case isl_dim_param:
143
33.7M
    isl_assert(ctx, pos < dim->nparam,
144
33.7M
          return isl_space_dim(dim, isl_dim_all));
145
33.7M
    return pos;
146
33.7M
  case isl_dim_in:
147
6.76M
    isl_assert(ctx, pos < dim->n_in,
148
6.76M
          return isl_space_dim(dim, isl_dim_all));
149
6.76M
    return pos + dim->nparam;
150
10.7M
  case isl_dim_out:
151
10.7M
    isl_assert(ctx, pos < dim->n_out,
152
10.7M
          return isl_space_dim(dim, isl_dim_all));
153
10.7M
    return pos + dim->nparam + dim->n_in;
154
10.7M
  default:
155
0
    isl_assert(ctx, 0, return isl_space_dim(dim, isl_dim_all));
156
51.3M
  }
157
51.3M
  
return isl_space_dim(dim, isl_dim_all)0
;
158
51.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
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
529k
    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
681
  } else {
176
681
    ids = isl_realloc_array(dim->ctx, dim->ids,
177
681
        isl_id *, isl_space_dim(dim, isl_dim_all));
178
681
    if (!ids)
179
0
      goto error;
180
681
    dim->ids = ids;
181
3.81k
    for (i = dim->n_id; i < isl_space_dim(dim, isl_dim_all); 
++i3.13k
)
182
3.13k
      dim->ids[i] = NULL;
183
681
  }
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
6.90M
{
196
6.90M
  dim = isl_space_cow(dim);
197
6.90M
198
6.90M
  if (!dim)
199
0
    goto error;
200
6.90M
201
6.90M
  pos = global_pos(dim, type, pos);
202
6.90M
  if (pos == isl_space_dim(dim, isl_dim_all))
203
0
    goto error;
204
6.90M
205
6.90M
  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
6.90M
  }
212
6.90M
213
6.90M
  dim->ids[pos] = id;
214
6.90M
215
6.90M
  return dim;
216
0
error:
217
0
  isl_id_free(id);
218
0
  isl_space_free(dim);
219
0
  return NULL;
220
6.90M
}
221
222
static __isl_keep isl_id *get_id(__isl_keep isl_space *dim,
223
         enum isl_dim_type type, unsigned pos)
224
44.3M
{
225
44.3M
  if (!dim)
226
0
    return NULL;
227
44.3M
228
44.3M
  pos = global_pos(dim, type, pos);
229
44.3M
  if (pos == isl_space_dim(dim, isl_dim_all))
230
0
    return NULL;
231
44.3M
  if (pos >= dim->n_id)
232
3.17M
    return NULL;
233
41.2M
  return dim->ids[pos];
234
41.2M
}
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 0719k
;
240
2.21M
  
case isl_dim_in: return dim->nparam512k
;
241
2.21M
  
case isl_dim_out: return dim->nparam + dim->n_in987k
;
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
286M
{
248
286M
  switch (type) {
249
286M
  
case isl_dim_param: return dim->nparam54.9M
;
250
286M
  
case isl_dim_in: return dim->n_in35.8M
;
251
286M
  
case isl_dim_out: return dim->n_out41.4M
;
252
286M
  
case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out154M
;
253
286M
  
default: return 00
;
254
286M
  }
255
286M
}
256
257
unsigned isl_space_dim(__isl_keep isl_space *dim, enum isl_dim_type type)
258
206M
{
259
206M
  if (!dim)
260
4
    return 0;
261
206M
  return n(dim, type);
262
206M
}
263
264
unsigned isl_space_offset(__isl_keep isl_space *dim, enum isl_dim_type type)
265
2.01M
{
266
2.01M
  if (!dim)
267
0
    return 0;
268
2.01M
  return offset(dim, type);
269
2.01M
}
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.83M
{
275
6.83M
  int i;
276
6.83M
  isl_id *id;
277
6.83M
278
6.83M
  if (!dst)
279
0
    return NULL;
280
6.83M
281
18.6M
  
for (i = 0; 6.83M
i < n(src, src_type);
++i11.8M
) {
282
11.8M
    id = get_id(src, src_type, i);
283
11.8M
    if (!id)
284
7.12M
      continue;
285
4.70M
    dst = set_id(dst, dst_type, offset + i, isl_id_copy(id));
286
4.70M
    if (!dst)
287
0
      return NULL;
288
4.70M
  }
289
6.83M
  return dst;
290
6.83M
}
291
292
__isl_take isl_space *isl_space_dup(__isl_keep isl_space *dim)
293
3.80M
{
294
3.80M
  isl_space *dup;
295
3.80M
  if (!dim)
296
0
    return NULL;
297
3.80M
  dup = isl_space_alloc(dim->ctx, dim->nparam, dim->n_in, dim->n_out);
298
3.80M
  if (!dup)
299
25
    return NULL;
300
3.80M
  if (dim->tuple_id[0] &&
301
3.80M
      
!(dup->tuple_id[0] = isl_id_copy(dim->tuple_id[0]))2.17M
)
302
0
    goto error;
303
3.80M
  if (dim->tuple_id[1] &&
304
3.80M
      
!(dup->tuple_id[1] = isl_id_copy(dim->tuple_id[1]))883k
)
305
0
    goto error;
306
3.80M
  if (dim->nested[0] && 
!(dup->nested[0] = isl_space_copy(dim->nested[0]))722k
)
307
0
    goto error;
308
3.80M
  if (dim->nested[1] && 
!(dup->nested[1] = isl_space_copy(dim->nested[1]))939k
)
309
0
    goto error;
310
3.80M
  if (!dim->ids)
311
2.31M
    return dup;
312
1.49M
  dup = copy_ids(dup, isl_dim_param, 0, dim, isl_dim_param);
313
1.49M
  dup = copy_ids(dup, isl_dim_in, 0, dim, isl_dim_in);
314
1.49M
  dup = copy_ids(dup, isl_dim_out, 0, dim, isl_dim_out);
315
1.49M
  return dup;
316
0
error:
317
0
  isl_space_free(dup);
318
0
  return NULL;
319
1.49M
}
320
321
__isl_give isl_space *isl_space_cow(__isl_take isl_space *dim)
322
18.9M
{
323
18.9M
  if (!dim)
324
25
    return NULL;
325
18.9M
326
18.9M
  if (dim->ref == 1)
327
15.1M
    return dim;
328
3.78M
  dim->ref--;
329
3.78M
  return isl_space_dup(dim);
330
3.78M
}
331
332
__isl_give isl_space *isl_space_copy(__isl_keep isl_space *dim)
333
16.9M
{
334
16.9M
  if (!dim)
335
9.17k
    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.9M
{
343
29.9M
  int i;
344
29.9M
345
29.9M
  if (!space)
346
11.6M
    return NULL;
347
18.3M
348
18.3M
  if (--space->ref > 0)
349
13.2M
    return NULL;
350
5.10M
351
5.10M
  isl_id_free(space->tuple_id[0]);
352
5.10M
  isl_id_free(space->tuple_id[1]);
353
5.10M
354
5.10M
  isl_space_free(space->nested[0]);
355
5.10M
  isl_space_free(space->nested[1]);
356
5.10M
357
11.9M
  for (i = 0; i < space->n_id; 
++i6.85M
)
358
6.85M
    isl_id_free(space->ids[i]);
359
5.10M
  free(space->ids);
360
5.10M
  isl_ctx_deref(space->ctx);
361
5.10M
  
362
5.10M
  free(space);
363
5.10M
364
5.10M
  return NULL;
365
5.10M
}
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
57.5k
{
374
57.5k
  char *p;
375
57.5k
  long dummy;
376
57.5k
377
57.5k
  dummy = strtol(s, &p, 0);
378
57.5k
  if (p != s)
379
57.5k
    
isl_die0
(ctx, isl_error_invalid, "name looks like a number",
380
57.5k
      return 0);
381
57.5k
382
57.5k
  return 1;
383
57.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
119k
{
390
119k
  if (!space)
391
7
    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_out118k
)
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
117k
{
411
117k
  if (!space_can_have_id(dim, type))
412
7
    return isl_bool_error;
413
117k
  return dim->tuple_id[type - isl_dim_in] != NULL;
414
117k
}
415
416
__isl_give isl_id *isl_space_get_tuple_id(__isl_keep isl_space *dim,
417
  enum isl_dim_type type)
418
38.3k
{
419
38.3k
  int has_id;
420
38.3k
421
38.3k
  if (!dim)
422
0
    return NULL;
423
38.3k
  has_id = isl_space_has_tuple_id(dim, type);
424
38.3k
  if (has_id < 0)
425
0
    return NULL;
426
38.3k
  if (!has_id)
427
38.3k
    
isl_die0
(dim->ctx, isl_error_invalid,
428
38.3k
      "tuple has no id", return NULL);
429
38.3k
  return isl_id_copy(dim->tuple_id[type - isl_dim_in]);
430
38.3k
}
431
432
__isl_give isl_space *isl_space_set_tuple_id(__isl_take isl_space *dim,
433
  enum isl_dim_type type, __isl_take isl_id *id)
434
2.09M
{
435
2.09M
  dim = isl_space_cow(dim);
436
2.09M
  if (!dim || !id)
437
0
    goto error;
438
2.09M
  if (type != isl_dim_in && 
type != isl_dim_out264k
)
439
2.09M
    
isl_die0
(dim->ctx, isl_error_invalid,
440
2.09M
      "only input, output and set tuples can have names",
441
2.09M
      goto error);
442
2.09M
443
2.09M
  isl_id_free(dim->tuple_id[type - isl_dim_in]);
444
2.09M
  dim->tuple_id[type - isl_dim_in] = id;
445
2.09M
446
2.09M
  return dim;
447
0
error:
448
0
  isl_id_free(id);
449
0
  isl_space_free(dim);
450
0
  return NULL;
451
2.09M
}
452
453
__isl_give isl_space *isl_space_reset_tuple_id(__isl_take isl_space *dim,
454
  enum isl_dim_type type)
455
5.94k
{
456
5.94k
  dim = isl_space_cow(dim);
457
5.94k
  if (!dim)
458
0
    return NULL;
459
5.94k
  if (type != isl_dim_in && type != isl_dim_out)
460
5.94k
    
isl_die0
(dim->ctx, isl_error_invalid,
461
5.94k
      "only input, output and set tuples can have names",
462
5.94k
      goto error);
463
5.94k
464
5.94k
  isl_id_free(dim->tuple_id[type - isl_dim_in]);
465
5.94k
  dim->tuple_id[type - isl_dim_in] = NULL;
466
5.94k
467
5.94k
  return dim;
468
0
error:
469
0
  isl_space_free(dim);
470
0
  return NULL;
471
5.94k
}
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.5k
{
481
82.5k
  space = isl_space_cow(space);
482
82.5k
  if (!space || !id)
483
0
    goto error;
484
82.5k
485
82.5k
  if (type == isl_dim_param) {
486
61.1k
    int i;
487
61.1k
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.1k
  }
498
82.5k
499
82.5k
  isl_id_free(get_id(space, type, pos));
500
82.5k
  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.5k
}
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.11k
{
543
1.11k
  if (!dim)
544
0
    return isl_bool_error;
545
1.11k
  return get_id(dim, type, pos) != NULL;
546
1.11k
}
547
548
__isl_give isl_id *isl_space_get_dim_id(__isl_keep isl_space *dim,
549
  enum isl_dim_type type, unsigned pos)
550
779k
{
551
779k
  if (!dim)
552
0
    return NULL;
553
779k
  if (!get_id(dim, type, pos))
554
779k
    
isl_die0
(dim->ctx, isl_error_invalid,
555
779k
      "dim has no id", return NULL);
556
779k
  return isl_id_copy(get_id(dim, type, pos));
557
779k
}
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
51.0k
{
562
51.0k
  isl_id *id;
563
51.0k
564
51.0k
  if (!dim)
565
0
    return NULL;
566
51.0k
567
51.0k
  if (!s)
568
0
    return isl_space_reset_tuple_id(dim, type);
569
51.0k
570
51.0k
  if (!name_ok(dim->ctx, s))
571
0
    goto error;
572
51.0k
573
51.0k
  id = isl_id_alloc(dim->ctx, s, NULL);
574
51.0k
  return isl_space_set_tuple_id(dim, type, id);
575
0
error:
576
0
  isl_space_free(dim);
577
0
  return NULL;
578
51.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->name180
;
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.38k
)
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
6.54k
{
609
6.54k
  isl_id *id;
610
6.54k
611
6.54k
  if (!dim)
612
0
    return NULL;
613
6.54k
  if (!s)
614
0
    return isl_space_reset_dim_id(dim, type, pos);
615
6.54k
  if (!name_ok(dim->ctx, s))
616
0
    goto error;
617
6.54k
  id = isl_id_alloc(dim->ctx, s, NULL);
618
6.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
6.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.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
312k
{
640
312k
  isl_id *id = get_id(dim, type, pos);
641
312k
  return id ? 
id->name261k
: NULL;
642
312k
}
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
392
{
647
392
  int i;
648
392
  int offset;
649
392
  int n;
650
392
651
392
  if (!dim || !id)
652
0
    return -1;
653
392
654
392
  offset = isl_space_offset(dim, type);
655
392
  n = isl_space_dim(dim, type);
656
516
  for (i = 0; i < n && 
offset + i < dim->n_id514
;
++i124
)
657
514
    if (dim->ids[offset + i] == id)
658
390
      return i;
659
392
660
392
  
return -12
;
661
392
}
662
663
int isl_space_find_dim_by_name(__isl_keep isl_space *space,
664
  enum isl_dim_type type, const char *name)
665
875
{
666
875
  int i;
667
875
  int offset;
668
875
  int n;
669
875
670
875
  if (!space || !name)
671
0
    return -1;
672
875
673
875
  offset = isl_space_offset(space, type);
674
875
  n = isl_space_dim(space, type);
675
1.39k
  for (i = 0; i < n && 
offset + i < space->n_id524
;
++i524
) {
676
524
    isl_id *id = get_id(space, type, i);
677
524
    if (id && id->name && !strcmp(id->name, name))
678
0
      return i;
679
524
  }
680
875
681
875
  return -1;
682
875
}
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
43.9M
{
746
43.9M
  if (!dim)
747
0
    return NULL;
748
43.9M
  if (type == isl_dim_in)
749
13.6M
    return dim->tuple_id[0];
750
30.2M
  if (type == isl_dim_out)
751
12.4M
    return dim->tuple_id[1];
752
17.8M
  return NULL;
753
17.8M
}
754
755
static __isl_keep isl_space *nested(__isl_keep isl_space *dim,
756
  enum isl_dim_type type)
757
40.7M
{
758
40.7M
  if (!dim)
759
0
    return NULL;
760
40.7M
  if (type == isl_dim_in)
761
11.6M
    return dim->nested[0];
762
29.0M
  if (type == isl_dim_out)
763
11.2M
    return dim->nested[1];
764
17.8M
  return NULL;
765
17.8M
}
766
767
/* Are the two spaces the same, apart from positions and names of parameters?
768
 */
769
isl_bool isl_space_has_equal_tuples(__isl_keep isl_space *space1,
770
  __isl_keep isl_space *space2)
771
6.93M
{
772
6.93M
  if (!space1 || !space2)
773
0
    return isl_bool_error;
774
6.93M
  if (space1 == space2)
775
2.26M
    return isl_bool_true;
776
4.67M
  return isl_space_tuple_is_equal(space1, isl_dim_in,
777
4.67M
          space2, isl_dim_in) &&
778
4.67M
         isl_space_tuple_is_equal(space1, isl_dim_out,
779
4.01M
          space2, isl_dim_out);
780
4.67M
}
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
21.8M
{
798
21.8M
  isl_id *id1, *id2;
799
21.8M
  isl_space *nested1, *nested2;
800
21.8M
801
21.8M
  if (!space1 || !space2)
802
0
    return isl_bool_error;
803
21.8M
804
21.8M
  if (space1 == space2 && 
type1 == type21.97M
)
805
6.57k
    return isl_bool_true;
806
21.8M
807
21.8M
  if (n(space1, type1) != n(space2, type2))
808
2.21M
    return isl_bool_false;
809
19.6M
  id1 = tuple_id(space1, type1);
810
19.6M
  id2 = tuple_id(space2, type2);
811
19.6M
  if (!id1 ^ !id2)
812
632k
    return isl_bool_false;
813
18.9M
  if (id1 && 
id1 != id23.94M
)
814
147k
    return isl_bool_false;
815
18.8M
  nested1 = nested(space1, type1);
816
18.8M
  nested2 = nested(space2, type2);
817
18.8M
  if (!nested1 ^ !nested2)
818
64.3k
    return isl_bool_false;
819
18.7M
  if (nested1 && 
!isl_space_has_equal_tuples(nested1, nested2)3.59M
)
820
26.1k
    return isl_bool_false;
821
18.7M
  return isl_bool_true;
822
18.7M
}
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.89M
)
830
933k
    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.71M
835
8.71M
  if (!space1->ids && 
!space2->ids5.69M
)
836
5.65M
    return isl_bool_true;
837
3.05M
838
11.2M
  
for (i = 0; 3.05M
i < n(space1, type1);
++i8.23M
) {
839
8.29M
    if (get_id(space1, type1, i) != get_id(space2, type2, i))
840
62.1k
      return isl_bool_false;
841
8.29M
  }
842
3.05M
  
return isl_bool_true2.99M
;
843
3.05M
}
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.36M
{
850
9.36M
  if (!space1 || 
!space29.36M
)
851
2
    return isl_bool_error;
852
9.36M
853
9.36M
  return match(space1, isl_dim_param, space2, isl_dim_param);
854
9.36M
}
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
204k
{
862
204k
  isl_bool equal;
863
204k
864
204k
  if (!space1 || !space2)
865
0
    return isl_bool_error;
866
204k
867
204k
  equal = match(space1, isl_dim_in, space2, isl_dim_in);
868
204k
  if (equal < 0 || !equal)
869
132
    return equal;
870
204k
  return match(space1, isl_dim_out, space2, isl_dim_out);
871
204k
}
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.07M
  for (i = 0; i < n ; 
++i4.26M
)
888
4.26M
    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
710k
{
894
710k
  isl_id **ids = NULL;
895
710k
896
710k
  if (!space)
897
0
    return NULL;
898
710k
  if (space->nparam == nparam &&
899
710k
      
space->n_in == n_in587k
&&
space->n_out == n_out580k
)
900
1.44k
    return space;
901
709k
902
709k
  isl_assert(space->ctx, space->nparam <= nparam, goto error);
903
709k
  isl_assert(space->ctx, space->n_in <= n_in, goto error);
904
709k
  isl_assert(space->ctx, space->n_out <= n_out, goto error);
905
709k
906
709k
  space = isl_space_cow(space);
907
709k
  if (!space)
908
0
    goto error;
909
709k
910
709k
  if (space->ids) {
911
311k
    unsigned n;
912
311k
    n = nparam + n_in + n_out;
913
311k
    if (n < nparam || n < n_in || n < n_out)
914
311k
      
isl_die0
(isl_space_get_ctx(space), isl_error_invalid,
915
311k
        "overflow in total number of dimensions",
916
311k
        goto error);
917
311k
    ids = isl_calloc_array(space->ctx, isl_id *, n);
918
311k
    if (!ids)
919
0
      goto error;
920
311k
    get_ids(space, isl_dim_param, 0, space->nparam, ids);
921
311k
    get_ids(space, isl_dim_in, 0, space->n_in, ids + nparam);
922
311k
    get_ids(space, isl_dim_out, 0, space->n_out,
923
311k
      ids + nparam + n_in);
924
311k
    free(space->ids);
925
311k
    space->ids = ids;
926
311k
    space->n_id = nparam + n_in + n_out;
927
311k
  }
928
709k
  space->nparam = nparam;
929
709k
  space->n_in = n_in;
930
709k
  space->n_out = n_out;
931
709k
932
709k
  return space;
933
0
error:
934
0
  free(ids);
935
0
  isl_space_free(space);
936
0
  return NULL;
937
709k
}
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
710k
{
948
710k
  space = isl_space_reset(space, type);
949
710k
  if (!space)
950
9
    return NULL;
951
710k
  switch (type) {
952
710k
  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
389
                isl_dim_param, n)))
962
0
      goto error;
963
123k
    return space;
964
123k
  case isl_dim_in:
965
8.85k
    return space_extend(space,
966
8.85k
        space->nparam, space->n_in + n, space->n_out);
967
578k
  case isl_dim_out:
968
578k
    return space_extend(space,
969
578k
        space->nparam, space->n_in, space->n_out + n);
970
123k
  default:
971
0
    isl_die(space->ctx, isl_error_invalid,
972
710k
      "cannot add dimensions of specified type", goto error);
973
710k
  }
974
710k
error:
975
0
  isl_space_free(space);
976
0
  return NULL;
977
710k
}
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 *dim,
1023
  enum isl_dim_type type, unsigned pos, unsigned n)
1024
182k
{
1025
182k
  isl_id **ids = NULL;
1026
182k
1027
182k
  if (!dim)
1028
0
    return NULL;
1029
182k
  if (n == 0)
1030
6
    return isl_space_reset(dim, type);
1031
182k
1032
182k
  if (!valid_dim_type(type))
1033
182k
    
isl_die0
(dim->ctx, isl_error_invalid,
1034
182k
      "cannot insert dimensions of specified type",
1035
182k
      goto error);
1036
182k
1037
182k
  isl_assert(dim->ctx, pos <= isl_space_dim(dim, type), goto error);
1038
182k
1039
182k
  dim = isl_space_cow(dim);
1040
182k
  if (!dim)
1041
0
    return NULL;
1042
182k
1043
182k
  if (dim->ids) {
1044
54.5k
    enum isl_dim_type t, o = isl_dim_param;
1045
54.5k
    int off;
1046
54.5k
    int s[3];
1047
54.5k
    ids = isl_calloc_array(dim->ctx, isl_id *,
1048
54.5k
             dim->nparam + dim->n_in + dim->n_out + n);
1049
54.5k
    if (!ids)
1050
0
      goto error;
1051
54.5k
    off = 0;
1052
54.5k
    s[isl_dim_param - o] = dim->nparam;
1053
54.5k
    s[isl_dim_in - o] = dim->n_in;
1054
54.5k
    s[isl_dim_out - o] = dim->n_out;
1055
218k
    for (t = isl_dim_param; t <= isl_dim_out; 
++t163k
) {
1056
163k
      if (t != type) {
1057
109k
        get_ids(dim, t, 0, s[t - o], ids + off);
1058
109k
        off += s[t - o];
1059
109k
      } else {
1060
54.5k
        get_ids(dim, t, 0, pos, ids + off);
1061
54.5k
        off += pos + n;
1062
54.5k
        get_ids(dim, t, pos, s[t - o] - pos, ids + off);
1063
54.5k
        off += s[t - o] - pos;
1064
54.5k
      }
1065
163k
    }
1066
54.5k
    free(dim->ids);
1067
54.5k
    dim->ids = ids;
1068
54.5k
    dim->n_id = dim->nparam + dim->n_in + dim->n_out + n;
1069
54.5k
  }
1070
182k
  switch (type) {
1071
182k
  
case isl_dim_param: dim->nparam += n; break2.79k
;
1072
182k
  
case isl_dim_in: dim->n_in += n; break10.2k
;
1073
182k
  
case isl_dim_out: dim->n_out += n; break169k
;
1074
182k
  
default: ;0
1075
182k
  }
1076
182k
  dim = isl_space_reset(dim, type);
1077
182k
1078
182k
  if (type == isl_dim_param) {
1079
2.79k
    if (dim && dim->nested[0] &&
1080
2.79k
        !(dim->nested[0] = isl_space_insert_dims(dim->nested[0],
1081
0
                isl_dim_param, pos, n)))
1082
0
      goto error;
1083
2.79k
    if (dim && dim->nested[1] &&
1084
2.79k
        !(dim->nested[1] = isl_space_insert_dims(dim->nested[1],
1085
1
                isl_dim_param, pos, n)))
1086
0
      goto error;
1087
182k
  }
1088
182k
1089
182k
  return dim;
1090
0
error:
1091
0
  isl_space_free(dim);
1092
0
  return NULL;
1093
182k
}
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.16k
{
1099
1.16k
  int i;
1100
1.16k
1101
1.16k
  space = isl_space_reset(space, src_type);
1102
1.16k
  space = isl_space_reset(space, dst_type);
1103
1.16k
  if (!space)
1104
0
    return NULL;
1105
1.16k
  if (n == 0)
1106
304
    return space;
1107
860
1108
860
  isl_assert(space->ctx, src_pos + n <= isl_space_dim(space, src_type),
1109
860
    goto error);
1110
860
1111
860
  if (dst_type == src_type && 
dst_pos == src_pos0
)
1112
0
    return space;
1113
860
1114
860
  isl_assert(space->ctx, dst_type != src_type, goto error);
1115
860
1116
860
  space = isl_space_cow(space);
1117
860
  if (!space)
1118
0
    return NULL;
1119
860
1120
860
  if (space->ids) {
1121
306
    isl_id **ids;
1122
306
    enum isl_dim_type t, o = isl_dim_param;
1123
306
    int off;
1124
306
    int s[3];
1125
306
    ids = isl_calloc_array(space->ctx, isl_id *,
1126
306
         space->nparam + space->n_in + space->n_out);
1127
306
    if (!ids)
1128
0
      goto error;
1129
306
    off = 0;
1130
306
    s[isl_dim_param - o] = space->nparam;
1131
306
    s[isl_dim_in - o] = space->n_in;
1132
306
    s[isl_dim_out - o] = space->n_out;
1133
1.22k
    for (t = isl_dim_param; t <= isl_dim_out; 
++t918
) {
1134
918
      if (t == dst_type) {
1135
306
        get_ids(space, t, 0, dst_pos, ids + off);
1136
306
        off += dst_pos;
1137
306
        get_ids(space, src_type, src_pos, n, ids + off);
1138
306
        off += n;
1139
306
        get_ids(space, t, dst_pos, s[t - o] - dst_pos,
1140
306
            ids + off);
1141
306
        off += s[t - o] - dst_pos;
1142
612
      } else if (t == src_type) {
1143
306
        get_ids(space, t, 0, src_pos, ids + off);
1144
306
        off += src_pos;
1145
306
        get_ids(space, t, src_pos + n,
1146
306
              s[t - o] - src_pos - n, ids + off);
1147
306
        off += s[t - o] - src_pos - n;
1148
306
      } else {
1149
306
        get_ids(space, t, 0, s[t - o], ids + off);
1150
306
        off += s[t - o];
1151
306
      }
1152
918
    }
1153
306
    free(space->ids);
1154
306
    space->ids = ids;
1155
306
    space->n_id = space->nparam + space->n_in + space->n_out;
1156
306
  }
1157
860
1158
860
  switch (dst_type) {
1159
860
  
case isl_dim_param: space->nparam += n; break86
;
1160
860
  
case isl_dim_in: space->n_in += n; break361
;
1161
860
  
case isl_dim_out: space->n_out += n; break413
;
1162
860
  
default: ;0
1163
860
  }
1164
860
1165
860
  switch (src_type) {
1166
860
  
case isl_dim_param: space->nparam -= n; break238
;
1167
860
  
case isl_dim_in: space->n_in -= n; break193
;
1168
860
  
case isl_dim_out: space->n_out -= n; break429
;
1169
860
  
default: ;0
1170
860
  }
1171
860
1172
860
  if (dst_type != isl_dim_param && 
src_type != isl_dim_param774
)
1173
536
    return space;
1174
324
1175
972
  
for (i = 0; 324
i < 2;
++i648
) {
1176
648
    if (!space->nested[i])
1177
631
      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
324
1184
324
  return space;
1185
0
error:
1186
0
  isl_space_free(space);
1187
0
  return NULL;
1188
324
}
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
664k
{
1196
664k
  isl_bool equal;
1197
664k
1198
664k
  equal = isl_space_has_equal_params(space1, space2);
1199
664k
  if (equal < 0)
1200
2
    return isl_stat_error;
1201
664k
  if (!equal)
1202
664k
    
isl_die0
(isl_space_get_ctx(space1), isl_error_invalid,
1203
664k
      "parameters need to match", return isl_stat_error);
1204
664k
  return isl_stat_ok;
1205
664k
}
1206
1207
__isl_give isl_space *isl_space_join(__isl_take isl_space *left,
1208
  __isl_take isl_space *right)
1209
553k
{
1210
553k
  isl_space *dim;
1211
553k
1212
553k
  if (isl_space_check_equal_params(left, right) < 0)
1213
2
    goto error;
1214
553k
1215
553k
  isl_assert(left->ctx,
1216
553k
    isl_space_tuple_is_equal(left, isl_dim_out, right, isl_dim_in),
1217
553k
    goto error);
1218
553k
1219
553k
  dim = isl_space_alloc(left->ctx, left->nparam, left->n_in, right->n_out);
1220
553k
  if (!dim)
1221
0
    goto error;
1222
553k
1223
553k
  dim = copy_ids(dim, isl_dim_param, 0, left, isl_dim_param);
1224
553k
  dim = copy_ids(dim, isl_dim_in, 0, left, isl_dim_in);
1225
553k
  dim = copy_ids(dim, isl_dim_out, 0, right, isl_dim_out);
1226
553k
1227
553k
  if (dim && left->tuple_id[0] &&
1228
553k
      
!(dim->tuple_id[0] = isl_id_copy(left->tuple_id[0]))184k
)
1229
0
    goto error;
1230
553k
  if (dim && right->tuple_id[1] &&
1231
553k
      
!(dim->tuple_id[1] = isl_id_copy(right->tuple_id[1]))152k
)
1232
0
    goto error;
1233
553k
  if (dim && left->nested[0] &&
1234
553k
      
!(dim->nested[0] = isl_space_copy(left->nested[0]))210k
)
1235
0
    goto error;
1236
553k
  if (dim && right->nested[1] &&
1237
553k
      
!(dim->nested[1] = isl_space_copy(right->nested[1]))255k
)
1238
0
    goto error;
1239
553k
1240
553k
  isl_space_free(left);
1241
553k
  isl_space_free(right);
1242
553k
1243
553k
  return dim;
1244
2
error:
1245
2
  isl_space_free(left);
1246
2
  isl_space_free(right);
1247
2
  return NULL;
1248
553k
}
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.76k
{
1257
2.76k
  isl_space *dom1, *dom2, *nest1, *nest2;
1258
2.76k
  int is_set;
1259
2.76k
1260
2.76k
  if (!left || !right)
1261
0
    goto error;
1262
2.76k
1263
2.76k
  is_set = isl_space_is_set(left);
1264
2.76k
  if (is_set != isl_space_is_set(right))
1265
2.76k
    
isl_die0
(isl_space_get_ctx(left), isl_error_invalid,
1266
2.76k
      "expecting either two set spaces or two map spaces",
1267
2.76k
      goto error);
1268
2.76k
  if (is_set)
1269
274
    return isl_space_range_product(left, right);
1270
2.49k
1271
2.49k
  if (isl_space_check_equal_params(left, right) < 0)
1272
0
    goto error;
1273
2.49k
1274
2.49k
  dom1 = isl_space_domain(isl_space_copy(left));
1275
2.49k
  dom2 = isl_space_domain(isl_space_copy(right));
1276
2.49k
  nest1 = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1277
2.49k
1278
2.49k
  dom1 = isl_space_range(left);
1279
2.49k
  dom2 = isl_space_range(right);
1280
2.49k
  nest2 = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1281
2.49k
1282
2.49k
  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.49k
}
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.88k
{
1295
1.88k
  isl_space *ran, *dom1, *dom2, *nest;
1296
1.88k
1297
1.88k
  if (isl_space_check_equal_params(left, right) < 0)
1298
0
    goto error;
1299
1.88k
1300
1.88k
  if (!isl_space_tuple_is_equal(left, isl_dim_out, right, isl_dim_out))
1301
1.88k
    
isl_die0
(left->ctx, isl_error_invalid,
1302
1.88k
      "ranges need to match", goto error);
1303
1.88k
1304
1.88k
  ran = isl_space_range(isl_space_copy(left));
1305
1.88k
1306
1.88k
  dom1 = isl_space_domain(left);
1307
1.88k
  dom2 = isl_space_domain(right);
1308
1.88k
  nest = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1309
1.88k
1310
1.88k
  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.88k
}
1316
1317
__isl_give isl_space *isl_space_range_product(__isl_take isl_space *left,
1318
  __isl_take isl_space *right)
1319
106k
{
1320
106k
  isl_space *dom, *ran1, *ran2, *nest;
1321
106k
1322
106k
  if (isl_space_check_equal_params(left, right) < 0)
1323
0
    goto error;
1324
106k
1325
106k
  if (!isl_space_tuple_is_equal(left, isl_dim_in, right, isl_dim_in))
1326
106k
    
isl_die0
(left->ctx, isl_error_invalid,
1327
106k
      "domains need to match", goto error);
1328
106k
1329
106k
  dom = isl_space_domain(isl_space_copy(left));
1330
106k
1331
106k
  ran1 = isl_space_range(left);
1332
106k
  ran2 = isl_space_range(right);
1333
106k
  nest = isl_space_wrap(isl_space_join(isl_space_reverse(ran1), ran2));
1334
106k
1335
106k
  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
106k
}
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
857
{
1347
857
  isl_space *nested;
1348
857
  isl_space *domain;
1349
857
1350
857
  if (!space)
1351
0
    return NULL;
1352
857
  if (!isl_space_domain_is_wrapping(space))
1353
857
    
isl_die0
(isl_space_get_ctx(space), isl_error_invalid,
1354
857
      "domain not a product", return isl_space_free(space));
1355
857
1356
857
  nested = space->nested[0];
1357
857
  domain = isl_space_copy(space);
1358
857
  domain = isl_space_drop_dims(domain, isl_dim_in,
1359
857
          nested->n_in, nested->n_out);
1360
857
  if (!domain)
1361
0
    return isl_space_free(space);
1362
857
  if (nested->tuple_id[0]) {
1363
538
    domain->tuple_id[0] = isl_id_copy(nested->tuple_id[0]);
1364
538
    if (!domain->tuple_id[0])
1365
0
      goto error;
1366
857
  }
1367
857
  if (nested->nested[0]) {
1368
319
    domain->nested[0] = isl_space_copy(nested->nested[0]);
1369
319
    if (!domain->nested[0])
1370
0
      goto error;
1371
857
  }
1372
857
1373
857
  isl_space_free(space);
1374
857
  return domain;
1375
0
error:
1376
0
  isl_space_free(space);
1377
0
  isl_space_free(domain);
1378
0
  return NULL;
1379
857
}
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.2k
    range->tuple_id[0] = isl_id_copy(nested->tuple_id[1]);
1402
12.2k
    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.21k
{
1426
2.21k
  isl_space *nested;
1427
2.21k
  isl_space *domain;
1428
2.21k
1429
2.21k
  if (!space)
1430
0
    return NULL;
1431
2.21k
1432
2.21k
  nested = space->nested[1];
1433
2.21k
  domain = isl_space_copy(space);
1434
2.21k
  domain = isl_space_drop_dims(domain, isl_dim_out,
1435
2.21k
          nested->n_in, nested->n_out);
1436
2.21k
  if (!domain)
1437
0
    return isl_space_free(space);
1438
2.21k
  if (nested->tuple_id[0]) {
1439
1.26k
    domain->tuple_id[1] = isl_id_copy(nested->tuple_id[0]);
1440
1.26k
    if (!domain->tuple_id[1])
1441
0
      goto error;
1442
2.21k
  }
1443
2.21k
  if (nested->nested[0]) {
1444
940
    domain->nested[1] = isl_space_copy(nested->nested[0]);
1445
940
    if (!domain->nested[1])
1446
0
      goto error;
1447
2.21k
  }
1448
2.21k
1449
2.21k
  isl_space_free(space);
1450
2.21k
  return domain;
1451
0
error:
1452
0
  isl_space_free(space);
1453
0
  isl_space_free(domain);
1454
0
  return NULL;
1455
2.21k
}
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.21k
{
1462
2.21k
  if (!space)
1463
0
    return NULL;
1464
2.21k
  if (!isl_space_range_is_wrapping(space))
1465
2.21k
    
isl_die0
(isl_space_get_ctx(space), isl_error_invalid,
1466
2.21k
      "range not a product", return isl_space_free(space));
1467
2.21k
1468
2.21k
  return range_factor_domain(space);
1469
2.21k
}
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
742
    range->tuple_id[1] = isl_id_copy(nested->tuple_id[1]);
1518
742
    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
9
{
1553
9
  if (!space)
1554
0
    return NULL;
1555
9
  if (!isl_space_is_wrapping(space))
1556
9
    
isl_die0
(isl_space_get_ctx(space), isl_error_invalid,
1557
9
      "not a product", return isl_space_free(space));
1558
9
1559
9
  return range_factor_range(space);
1560
9
}
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
9
    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.6k
{
1578
12.6k
  isl_ctx *ctx;
1579
12.6k
  isl_id **ids = NULL;
1580
12.6k
  int n_id;
1581
12.6k
1582
12.6k
  if (!space)
1583
14
    return NULL;
1584
12.6k
  ctx = isl_space_get_ctx(space);
1585
12.6k
  if (!isl_space_is_set(space))
1586
12.6k
    
isl_die0
(ctx, isl_error_invalid, "not a set space", goto error);
1587
12.6k
  space = isl_space_cow(space);
1588
12.6k
  if (!space)
1589
0
    return NULL;
1590
12.6k
  n_id = space->nparam + space->n_out + space->n_out;
1591
12.6k
  if (n_id > 0 && 
space->ids12.1k
) {
1592
3.55k
    ids = isl_calloc_array(space->ctx, isl_id *, n_id);
1593
3.55k
    if (!ids)
1594
0
      goto error;
1595
3.55k
    get_ids(space, isl_dim_param, 0, space->nparam, ids);
1596
3.55k
    get_ids(space, isl_dim_out, 0, space->n_out,
1597
3.55k
      ids + space->nparam);
1598
3.55k
  }
1599
12.6k
  space->n_in = space->n_out;
1600
12.6k
  if (ids) {
1601
3.55k
    free(space->ids);
1602
3.55k
    space->ids = ids;
1603
3.55k
    space->n_id = n_id;
1604
3.55k
    space = copy_ids(space, isl_dim_out, 0, space, isl_dim_in);
1605
3.55k
  }
1606
12.6k
  isl_id_free(space->tuple_id[0]);
1607
12.6k
  space->tuple_id[0] = isl_id_copy(space->tuple_id[1]);
1608
12.6k
  isl_space_free(space->nested[0]);
1609
12.6k
  space->nested[0] = isl_space_copy(space->nested[1]);
1610
12.6k
  return space;
1611
0
error:
1612
0
  isl_space_free(space);
1613
0
  return NULL;
1614
12.6k
}
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.8k
{
1619
83.8k
  if (!domain || !range)
1620
0
    goto error;
1621
83.8k
  if (!isl_space_is_set(domain))
1622
83.8k
    
isl_die0
(isl_space_get_ctx(domain), isl_error_invalid,
1623
83.8k
      "domain is not a set space", goto error);
1624
83.8k
  if (!isl_space_is_set(range))
1625
83.8k
    
isl_die0
(isl_space_get_ctx(range), isl_error_invalid,
1626
83.8k
      "range is not a set space", goto error);
1627
83.8k
  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.8k
}
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
1.44M
{
1638
1.44M
  int i;
1639
1.44M
1640
3.50M
  for (i = 0; i < n ; 
++i2.05M
)
1641
2.05M
    dim = set_id(dim, type, first + i, ids[i]);
1642
1.44M
1643
1.44M
  return dim;
1644
1.44M
}
1645
1646
__isl_give isl_space *isl_space_reverse(__isl_take isl_space *dim)
1647
1.95M
{
1648
1.95M
  unsigned t;
1649
1.95M
  isl_space *nested;
1650
1.95M
  isl_id **ids = NULL;
1651
1.95M
  isl_id *id;
1652
1.95M
1653
1.95M
  if (!dim)
1654
1
    return NULL;
1655
1.95M
  if (match(dim, isl_dim_in, dim, isl_dim_out))
1656
115k
    return dim;
1657
1.84M
1658
1.84M
  dim = isl_space_cow(dim);
1659
1.84M
  if (!dim)
1660
0
    return NULL;
1661
1.84M
1662
1.84M
  id = dim->tuple_id[0];
1663
1.84M
  dim->tuple_id[0] = dim->tuple_id[1];
1664
1.84M
  dim->tuple_id[1] = id;
1665
1.84M
1666
1.84M
  nested = dim->nested[0];
1667
1.84M
  dim->nested[0] = dim->nested[1];
1668
1.84M
  dim->nested[1] = nested;
1669
1.84M
1670
1.84M
  if (dim->ids) {
1671
723k
    int n_id = dim->n_in + dim->n_out;
1672
723k
    ids = isl_alloc_array(dim->ctx, isl_id *, n_id);
1673
723k
    if (n_id && 
!ids608k
)
1674
0
      goto error;
1675
723k
    get_ids(dim, isl_dim_in, 0, dim->n_in, ids);
1676
723k
    get_ids(dim, isl_dim_out, 0, dim->n_out, ids + dim->n_in);
1677
723k
  }
1678
1.84M
1679
1.84M
  t = dim->n_in;
1680
1.84M
  dim->n_in = dim->n_out;
1681
1.84M
  dim->n_out = t;
1682
1.84M
1683
1.84M
  if (dim->ids) {
1684
723k
    dim = set_ids(dim, isl_dim_out, 0, dim->n_out, ids);
1685
723k
    dim = set_ids(dim, isl_dim_in, 0, dim->n_in, ids + dim->n_out);
1686
723k
    free(ids);
1687
723k
  }
1688
1.84M
1689
1.84M
  return dim;
1690
0
error:
1691
0
  free(ids);
1692
0
  isl_space_free(dim);
1693
0
  return NULL;
1694
1.84M
}
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.26M
{
1699
2.26M
  int i;
1700
2.26M
1701
2.26M
  if (!dim)
1702
2
    return NULL;
1703
2.26M
1704
2.26M
  if (num == 0)
1705
382k
    return isl_space_reset(dim, type);
1706
1.88M
1707
1.88M
  if (!valid_dim_type(type))
1708
1.88M
    
isl_die0
(dim->ctx, isl_error_invalid,
1709
1.88M
      "cannot drop dimensions of specified type", goto error);
1710
1.88M
1711
1.88M
  if (first + num > n(dim, type) || first + num < first)
1712
1.88M
    
isl_die0
(isl_space_get_ctx(dim), isl_error_invalid,
1713
1.88M
      "index out of bounds", return isl_space_free(dim));
1714
1.88M
  dim = isl_space_cow(dim);
1715
1.88M
  if (!dim)
1716
16
    goto error;
1717
1.88M
  if (dim->ids) {
1718
530k
    dim = extend_ids(dim);
1719
530k
    if (!dim)
1720
0
      goto error;
1721
1.70M
    
for (i = 0; 530k
i < num;
++i1.17M
)
1722
1.17M
      isl_id_free(get_id(dim, type, first + i));
1723
588k
    for (i = first+num; i < n(dim, type); 
++i57.7k
)
1724
57.7k
      set_id(dim, type, i - num, get_id(dim, type, i));
1725
530k
    switch (type) {
1726
530k
    case isl_dim_param:
1727
30.0k
      get_ids(dim, isl_dim_in, 0, dim->n_in,
1728
30.0k
        dim->ids + offset(dim, isl_dim_in) - num);
1729
177k
    case isl_dim_in:
1730
177k
      get_ids(dim, isl_dim_out, 0, dim->n_out,
1731
177k
        dim->ids + offset(dim, isl_dim_out) - num);
1732
530k
    default:
1733
530k
      ;
1734
530k
    }
1735
530k
    dim->n_id -= num;
1736
530k
  }
1737
1.88M
  switch (type) {
1738
1.88M
  
case isl_dim_param: dim->nparam -= num; break30.8k
;
1739
1.88M
  
case isl_dim_in: dim->n_in -= num; break413k
;
1740
1.88M
  
case isl_dim_out: dim->n_out -= num; break1.43M
;
1741
1.88M
  
default: ;0
1742
1.88M
  }
1743
1.88M
  dim = isl_space_reset(dim, type);
1744
1.88M
  if (type == isl_dim_param) {
1745
30.8k
    if (dim && dim->nested[0] &&
1746
30.8k
        !(dim->nested[0] = isl_space_drop_dims(dim->nested[0],
1747
122
                isl_dim_param, first, num)))
1748
0
      goto error;
1749
30.8k
    if (dim && dim->nested[1] &&
1750
30.8k
        !(dim->nested[1] = isl_space_drop_dims(dim->nested[1],
1751
548
                isl_dim_param, first, num)))
1752
0
      goto error;
1753
1.88M
  }
1754
1.88M
  return dim;
1755
16
error:
1756
16
  isl_space_free(dim);
1757
16
  return NULL;
1758
1.88M
}
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
712k
{
1778
712k
  if (!space)
1779
1
    return NULL;
1780
712k
  space = isl_space_drop_dims(space, isl_dim_out, 0, space->n_out);
1781
712k
  space = isl_space_reverse(space);
1782
712k
  space = mark_as_set(space);
1783
712k
  return space;
1784
712k
}
1785
1786
__isl_give isl_space *isl_space_from_domain(__isl_take isl_space *dim)
1787
664k
{
1788
664k
  if (!dim)
1789
2
    return NULL;
1790
664k
  if (!isl_space_is_set(dim))
1791
664k
    
isl_die0
(isl_space_get_ctx(dim), isl_error_invalid,
1792
664k
      "not a set space", goto error);
1793
664k
  dim = isl_space_reverse(dim);
1794
664k
  dim = isl_space_reset(dim, isl_dim_out);
1795
664k
  return dim;
1796
0
error:
1797
0
  isl_space_free(dim);
1798
0
  return NULL;
1799
664k
}
1800
1801
__isl_give isl_space *isl_space_range(__isl_take isl_space *space)
1802
356k
{
1803
356k
  if (!space)
1804
3
    return NULL;
1805
356k
  space = isl_space_drop_dims(space, isl_dim_in, 0, space->n_in);
1806
356k
  space = mark_as_set(space);
1807
356k
  return space;
1808
356k
}
1809
1810
__isl_give isl_space *isl_space_from_range(__isl_take isl_space *dim)
1811
201k
{
1812
201k
  if (!dim)
1813
5
    return NULL;
1814
201k
  if (!isl_space_is_set(dim))
1815
201k
    
isl_die0
(isl_space_get_ctx(dim), isl_error_invalid,
1816
201k
      "not a set space", goto error);
1817
201k
  return isl_space_reset(dim, isl_dim_in);
1818
0
error:
1819
0
  isl_space_free(dim);
1820
0
  return NULL;
1821
201k
}
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.90k
{
1827
1.90k
  isl_space *domain;
1828
1.90k
1829
1.90k
  domain = isl_space_from_range(isl_space_domain(isl_space_copy(space)));
1830
1.90k
  space = isl_space_from_domain(isl_space_wrap(space));
1831
1.90k
  space = isl_space_join(space, domain);
1832
1.90k
1833
1.90k
  return space;
1834
1.90k
}
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
16
{
1840
16
  isl_space *range;
1841
16
1842
16
  range = isl_space_from_range(isl_space_range(isl_space_copy(space)));
1843
16
  space = isl_space_from_domain(isl_space_wrap(space));
1844
16
  space = isl_space_join(space, range);
1845
16
1846
16
  return space;
1847
16
}
1848
1849
__isl_give isl_space *isl_space_params(__isl_take isl_space *space)
1850
548k
{
1851
548k
  if (isl_space_is_params(space))
1852
377k
    return space;
1853
170k
  space = isl_space_drop_dims(space,
1854
170k
          isl_dim_in, 0, isl_space_dim(space, isl_dim_in));
1855
170k
  space = isl_space_drop_dims(space,
1856
170k
          isl_dim_out, 0, isl_space_dim(space, isl_dim_out));
1857
170k
  space = mark_as_params(space);
1858
170k
  return space;
1859
170k
}
1860
1861
__isl_give isl_space *isl_space_set_from_params(__isl_take isl_space *space)
1862
17.0k
{
1863
17.0k
  if (!space)
1864
0
    return NULL;
1865
17.0k
  if (!isl_space_is_params(space))
1866
17.0k
    
isl_die0
(isl_space_get_ctx(space), isl_error_invalid,
1867
17.0k
      "not a parameter space", goto error);
1868
17.0k
  return isl_space_reset(space, isl_dim_set);
1869
0
error:
1870
0
  isl_space_free(space);
1871
0
  return NULL;
1872
17.0k
}
1873
1874
__isl_give isl_space *isl_space_underlying(__isl_take isl_space *dim,
1875
  unsigned n_div)
1876
452k
{
1877
452k
  int i;
1878
452k
1879
452k
  if (!dim)
1880
0
    return NULL;
1881
452k
  if (n_div == 0 &&
1882
452k
      
dim->nparam == 0432k
&&
dim->n_in == 0245k
&&
dim->n_id == 0196k
)
1883
179k
    return isl_space_reset(isl_space_reset(dim, isl_dim_in), isl_dim_out);
1884
272k
  dim = isl_space_cow(dim);
1885
272k
  if (!dim)
1886
0
    return NULL;
1887
272k
  dim->n_out += dim->nparam + dim->n_in + n_div;
1888
272k
  dim->nparam = 0;
1889
272k
  dim->n_in = 0;
1890
272k
1891
1.39M
  for (i = 0; i < dim->n_id; 
++i1.12M
)
1892
1.12M
    isl_id_free(get_id(dim, isl_dim_out, i));
1893
272k
  dim->n_id = 0;
1894
272k
  dim = isl_space_reset(dim, isl_dim_in);
1895
272k
  dim = isl_space_reset(dim, isl_dim_out);
1896
272k
1897
272k
  return dim;
1898
272k
}
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.84M
{
1905
7.84M
  isl_bool equal;
1906
7.84M
1907
7.84M
  if (!space1 || !space2)
1908
0
    return isl_bool_error;
1909
7.84M
  if (space1 == space2)
1910
4.42M
    return isl_bool_true;
1911
3.41M
  equal = isl_space_has_equal_params(space1, space2);
1912
3.41M
  if (equal < 0 || !equal)
1913
69.0k
    return equal;
1914
3.34M
  return isl_space_has_equal_tuples(space1, space2);
1915
3.34M
}
1916
1917
/* Do the tuples of "space1" correspond to those of the domain of "space2"?
1918
 * That is, is "space1" eqaul to the domain of "space2", ignoring parameters.
1919
 *
1920
 * "space2" is allowed to be a set space, in which case "space1"
1921
 * should be a parameter space.
1922
 */
1923
isl_bool isl_space_has_domain_tuples(__isl_keep isl_space *space1,
1924
  __isl_keep isl_space *space2)
1925
107k
{
1926
107k
  isl_bool is_set;
1927
107k
1928
107k
  is_set = isl_space_is_set(space1);
1929
107k
  if (is_set < 0 || !is_set)
1930
0
    return is_set;
1931
107k
  return isl_space_tuple_is_equal(space1, isl_dim_set,
1932
107k
          space2, isl_dim_in);
1933
107k
}
1934
1935
/* Is space1 equal to the domain of space2?
1936
 *
1937
 * In the internal version we also allow space2 to be the space of a set,
1938
 * provided space1 is a parameter space.
1939
 */
1940
isl_bool isl_space_is_domain_internal(__isl_keep isl_space *space1,
1941
  __isl_keep isl_space *space2)
1942
107k
{
1943
107k
  isl_bool equal_params;
1944
107k
1945
107k
  if (!space1 || !space2)
1946
0
    return isl_bool_error;
1947
107k
  equal_params = isl_space_has_equal_params(space1, space2);
1948
107k
  if (equal_params < 0 || !equal_params)
1949
0
    return equal_params;
1950
107k
  return isl_space_has_domain_tuples(space1, space2);
1951
107k
}
1952
1953
/* Is space1 equal to the domain of space2?
1954
 */
1955
isl_bool isl_space_is_domain(__isl_keep isl_space *space1,
1956
  __isl_keep isl_space *space2)
1957
25
{
1958
25
  if (!space2)
1959
0
    return isl_bool_error;
1960
25
  if (!isl_space_is_map(space2))
1961
0
    return isl_bool_false;
1962
25
  return isl_space_is_domain_internal(space1, space2);
1963
25
}
1964
1965
/* Is space1 equal to the range of space2?
1966
 *
1967
 * In the internal version, space2 is allowed to be the space of a set,
1968
 * in which case it should be equal to space1.
1969
 */
1970
isl_bool isl_space_is_range_internal(__isl_keep isl_space *space1,
1971
  __isl_keep isl_space *space2)
1972
17.3k
{
1973
17.3k
  isl_bool equal_params;
1974
17.3k
1975
17.3k
  if (!space1 || !space2)
1976
0
    return isl_bool_error;
1977
17.3k
  if (!isl_space_is_set(space1))
1978
0
    return isl_bool_false;
1979
17.3k
  equal_params = isl_space_has_equal_params(space1, space2);
1980
17.3k
  if (equal_params < 0 || !equal_params)
1981
0
    return equal_params;
1982
17.3k
  return isl_space_tuple_is_equal(space1, isl_dim_set,
1983
17.3k
          space2, isl_dim_out);
1984
17.3k
}
1985
1986
/* Is space1 equal to the range of space2?
1987
 */
1988
isl_bool isl_space_is_range(__isl_keep isl_space *space1,
1989
  __isl_keep isl_space *space2)
1990
0
{
1991
0
  if (!space2)
1992
0
    return isl_bool_error;
1993
0
  if (!isl_space_is_map(space2))
1994
0
    return isl_bool_false;
1995
0
  return isl_space_is_range_internal(space1, space2);
1996
0
}
1997
1998
/* Update "hash" by hashing in the parameters of "space".
1999
 */
2000
static uint32_t isl_hash_params(uint32_t hash, __isl_keep isl_space *space)
2001
548k
{
2002
548k
  int i;
2003
548k
  isl_id *id;
2004
548k
2005
548k
  if (!space)
2006
0
    return hash;
2007
548k
2008
548k
  isl_hash_byte(hash, space->nparam % 256);
2009
548k
2010
1.28M
  for (i = 0; i < space->nparam; 
++i733k
) {
2011
733k
    id = get_id(space, isl_dim_param, i);
2012
733k
    hash = isl_hash_id(hash, id);
2013
733k
  }
2014
548k
2015
548k
  return hash;
2016
548k
}
2017
2018
/* Update "hash" by hashing in the tuples of "space".
2019
 * Changes in this function should be reflected in isl_hash_tuples_domain.
2020
 */
2021
static uint32_t isl_hash_tuples(uint32_t hash, __isl_keep isl_space *space)
2022
2.12M
{
2023
2.12M
  isl_id *id;
2024
2.12M
2025
2.12M
  if (!space)
2026
1.33M
    return hash;
2027
787k
2028
787k
  isl_hash_byte(hash, space->n_in % 256);
2029
787k
  isl_hash_byte(hash, space->n_out % 256);
2030
787k
2031
787k
  id = tuple_id(space, isl_dim_in);
2032
787k
  hash = isl_hash_id(hash, id);
2033
787k
  id = tuple_id(space, isl_dim_out);
2034
787k
  hash = isl_hash_id(hash, id);
2035
787k
2036
787k
  hash = isl_hash_tuples(hash, space->nested[0]);
2037
787k
  hash = isl_hash_tuples(hash, space->nested[1]);
2038
787k
2039
787k
  return hash;
2040
787k
}
2041
2042
/* Update "hash" by hashing in the domain tuple of "space".
2043
 * The result of this function is equal to the result of applying
2044
 * isl_hash_tuples to the domain of "space".
2045
 */
2046
static uint32_t isl_hash_tuples_domain(uint32_t hash,
2047
  __isl_keep isl_space *space)
2048
45.4k
{
2049
45.4k
  isl_id *id;
2050
45.4k
2051
45.4k
  if (!space)
2052
0
    return hash;
2053
45.4k
2054
45.4k
  isl_hash_byte(hash, 0);
2055
45.4k
  isl_hash_byte(hash, space->n_in % 256);
2056
45.4k
2057
45.4k
  hash = isl_hash_id(hash, &isl_id_none);
2058
45.4k
  id = tuple_id(space, isl_dim_in);
2059
45.4k
  hash = isl_hash_id(hash, id);
2060
45.4k
2061
45.4k
  hash = isl_hash_tuples(hash, space->nested[0]);
2062
45.4k
2063
45.4k
  return hash;
2064
45.4k
}
2065
2066
/* Return a hash value that digests the tuples of "space",
2067
 * i.e., that ignores the parameters.
2068
 */
2069
uint32_t isl_space_get_tuple_hash(__isl_keep isl_space *space)
2070
2.26k
{
2071
2.26k
  uint32_t hash;
2072
2.26k
2073
2.26k
  if (!space)
2074
0
    return 0;
2075
2.26k
2076
2.26k
  hash = isl_hash_init();
2077
2.26k
  hash = isl_hash_tuples(hash, space);
2078
2.26k
2079
2.26k
  return hash;
2080
2.26k
}
2081
2082
uint32_t isl_space_get_hash(__isl_keep isl_space *space)
2083
502k
{
2084
502k
  uint32_t hash;
2085
502k
2086
502k
  if (!space)
2087
0
    return 0;
2088
502k
2089
502k
  hash = isl_hash_init();
2090
502k
  hash = isl_hash_params(hash, space);
2091
502k
  hash = isl_hash_tuples(hash, space);
2092
502k
2093
502k
  return hash;
2094
502k
}
2095
2096
/* Return the hash value of the domain of "space".
2097
 * That is, isl_space_get_domain_hash(space) is equal to
2098
 * isl_space_get_hash(isl_space_domain(space)).
2099
 */
2100
uint32_t isl_space_get_domain_hash(__isl_keep isl_space *space)
2101
45.4k
{
2102
45.4k
  uint32_t hash;
2103
45.4k
2104
45.4k
  if (!space)
2105
0
    return 0;
2106
45.4k
2107
45.4k
  hash = isl_hash_init();
2108
45.4k
  hash = isl_hash_params(hash, space);
2109
45.4k
  hash = isl_hash_tuples_domain(hash, space);
2110
45.4k
2111
45.4k
  return hash;
2112
45.4k
}
2113
2114
isl_bool isl_space_is_wrapping(__isl_keep isl_space *dim)
2115
57.7k
{
2116
57.7k
  if (!dim)
2117
0
    return isl_bool_error;
2118
57.7k
2119
57.7k
  if (!isl_space_is_set(dim))
2120
0
    return isl_bool_false;
2121
57.7k
2122
57.7k
  return dim->nested[1] != NULL;
2123
57.7k
}
2124
2125
/* Is "space" the space of a map where the domain is a wrapped map space?
2126
 */
2127
isl_bool isl_space_domain_is_wrapping(__isl_keep isl_space *space)
2128
49.7k
{
2129
49.7k
  if (!space)
2130
0
    return isl_bool_error;
2131
49.7k
2132
49.7k
  if (isl_space_is_set(space))
2133
0
    return isl_bool_false;
2134
49.7k
2135
49.7k
  return space->nested[0] != NULL;
2136
49.7k
}
2137
2138
/* Is "space" the space of a map where the range is a wrapped map space?
2139
 */
2140
isl_bool isl_space_range_is_wrapping(__isl_keep isl_space *space)
2141
66.4k
{
2142
66.4k
  if (!space)
2143
0
    return isl_bool_error;
2144
66.4k
2145
66.4k
  if (isl_space_is_set(space))
2146
781
    return isl_bool_false;
2147
65.6k
2148
65.6k
  return space->nested[1] != NULL;
2149
65.6k
}
2150
2151
/* Is "space" a product of two spaces?
2152
 * That is, is it a wrapping set space or a map space
2153
 * with wrapping domain and range?
2154
 */
2155
isl_bool isl_space_is_product(__isl_keep isl_space *space)
2156
6.23k
{
2157
6.23k
  isl_bool is_set;
2158
6.23k
  isl_bool is_product;
2159
6.23k
2160
6.23k
  is_set = isl_space_is_set(space);
2161
6.23k
  if (is_set < 0)
2162
0
    return isl_bool_error;
2163
6.23k
  if (is_set)
2164
0
    return isl_space_is_wrapping(space);
2165
6.23k
  is_product = isl_space_domain_is_wrapping(space);
2166
6.23k
  if (is_product < 0 || !is_product)
2167
35
    return is_product;
2168
6.20k
  return isl_space_range_is_wrapping(space);
2169
6.20k
}
2170
2171
__isl_give isl_space *isl_space_wrap(__isl_take isl_space *dim)
2172
210k
{
2173
210k
  isl_space *wrap;
2174
210k
2175
210k
  if (!dim)
2176
1
    return NULL;
2177
210k
2178
210k
  wrap = isl_space_set_alloc(dim->ctx,
2179
210k
            dim->nparam, dim->n_in + dim->n_out);
2180
210k
2181
210k
  wrap = copy_ids(wrap, isl_dim_param, 0, dim, isl_dim_param);
2182
210k
  wrap = copy_ids(wrap, isl_dim_set, 0, dim, isl_dim_in);
2183
210k
  wrap = copy_ids(wrap, isl_dim_set, dim->n_in, dim, isl_dim_out);
2184
210k
2185
210k
  if (!wrap)
2186
0
    goto error;
2187
210k
2188
210k
  wrap->nested[1] = dim;
2189
210k
2190
210k
  return wrap;
2191
0
error:
2192
0
  isl_space_free(dim);
2193
0
  return NULL;
2194
210k
}
2195
2196
__isl_give isl_space *isl_space_unwrap(__isl_take isl_space *dim)
2197
39.9k
{
2198
39.9k
  isl_space *unwrap;
2199
39.9k
2200
39.9k
  if (!dim)
2201
1
    return NULL;
2202
39.9k
2203
39.9k
  if (!isl_space_is_wrapping(dim))
2204
39.9k
    
isl_die0
(dim->ctx, isl_error_invalid, "not a wrapping space",
2205
39.9k
      goto error);
2206
39.9k
2207
39.9k
  unwrap = isl_space_copy(dim->nested[1]);
2208
39.9k
  isl_space_free(dim);
2209
39.9k
2210
39.9k
  return unwrap;
2211
0
error:
2212
0
  isl_space_free(dim);
2213
0
  return NULL;
2214
39.9k
}
2215
2216
isl_bool isl_space_is_named_or_nested(__isl_keep isl_space *space,
2217
  enum isl_dim_type type)
2218
5.58M
{
2219
5.58M
  if (type != isl_dim_in && 
type != isl_dim_out4.03M
)
2220
208k
    return isl_bool_false;
2221
5.37M
  if (!space)
2222
9
    return isl_bool_error;
2223
5.37M
  if (space->tuple_id[type - isl_dim_in])
2224
2.25M
    return isl_bool_true;
2225
3.12M
  if (space->nested[type - isl_dim_in])
2226
787k
    return isl_bool_true;
2227
2.33M
  return isl_bool_false;
2228
2.33M
}
2229
2230
isl_bool isl_space_may_be_set(__isl_keep isl_space *space)
2231
274
{
2232
274
  isl_bool nested;
2233
274
2234
274
  if (!space)
2235
0
    return isl_bool_error;
2236
274
  if (isl_space_is_set(space))
2237
169
    return isl_bool_true;
2238
105
  if (isl_space_dim(space, isl_dim_in) != 0)
2239
0
    return isl_bool_false;
2240
105
  nested = isl_space_is_named_or_nested(space, isl_dim_in);
2241
105
  if (nested < 0 || nested)
2242
0
    return isl_bool_not(nested);
2243
105
  return isl_bool_true;
2244
105
}
2245
2246
__isl_give isl_space *isl_space_reset(__isl_take isl_space *dim,
2247
  enum isl_dim_type type)
2248
5.08M
{
2249
5.08M
  if (!isl_space_is_named_or_nested(dim, type))
2250
2.28M
    return dim;
2251
2.80M
2252
2.80M
  dim = isl_space_cow(dim);
2253
2.80M
  if (!dim)
2254
18
    return NULL;
2255
2.80M
2256
2.80M
  isl_id_free(dim->tuple_id[type - isl_dim_in]);
2257
2.80M
  dim->tuple_id[type - isl_dim_in] = NULL;
2258
2.80M
  isl_space_free(dim->nested[type - isl_dim_in]);
2259
2.80M
  dim->nested[type - isl_dim_in] = NULL;
2260
2.80M
2261
2.80M
  return dim;
2262
2.80M
}
2263
2264
__isl_give isl_space *isl_space_flatten(__isl_take isl_space *dim)
2265
23.8k
{
2266
23.8k
  if (!dim)
2267
0
    return NULL;
2268
23.8k
  if (!dim->nested[0] && !dim->nested[1])
2269
0
    return dim;
2270
23.8k
2271
23.8k
  if (dim->nested[0])
2272
0
    dim = isl_space_reset(dim, isl_dim_in);
2273
23.8k
  if (dim && dim->nested[1])
2274
23.8k
    dim = isl_space_reset(dim, isl_dim_out);
2275
23.8k
2276
23.8k
  return dim;
2277
23.8k
}
2278
2279
__isl_give isl_space *isl_space_flatten_domain(__isl_take isl_space *space)
2280
34
{
2281
34
  if (!space)
2282
0
    return NULL;
2283
34
  if (!space->nested[0])
2284
0
    return space;
2285
34
2286
34
  return isl_space_reset(space, isl_dim_in);
2287
34
}
2288
2289
__isl_give isl_space *isl_space_flatten_range(__isl_take isl_space *space)
2290
76.3k
{
2291
76.3k
  if (!space)
2292
0
    return NULL;
2293
76.3k
  if (!space->nested[1])
2294
0
    return space;
2295
76.3k
2296
76.3k
  return isl_space_reset(space, isl_dim_out);
2297
76.3k
}
2298
2299
/* Replace the parameters of dst by those of src.
2300
 */
2301
__isl_give isl_space *isl_space_replace_params(__isl_take isl_space *dst,
2302
  __isl_keep isl_space *src)
2303
123k
{
2304
123k
  isl_bool equal_params;
2305
123k
  enum isl_dim_type type = isl_dim_param;
2306
123k
2307
123k
  equal_params = isl_space_has_equal_params(dst, src);
2308
123k
  if (equal_params < 0)
2309
0
    return isl_space_free(dst);
2310
123k
  if (equal_params)
2311
57.9k
    return dst;
2312
65.9k
2313
65.9k
  dst = isl_space_cow(dst);
2314
65.9k
2315
65.9k
  if (!dst || !src)
2316
0
    goto error;
2317
65.9k
2318
65.9k
  dst = isl_space_drop_dims(dst, type, 0, isl_space_dim(dst, type));
2319
65.9k
  dst = isl_space_add_dims(dst, type, isl_space_dim(src, type));
2320
65.9k
  dst = copy_ids(dst, type, 0, src, type);
2321
65.9k
2322
65.9k
  if (dst) {
2323
65.9k
    int i;
2324
197k
    for (i = 0; i <= 1; 
++i131k
) {
2325
131k
      if (!dst->nested[i])
2326
131k
        continue;
2327
463
      dst->nested[i] = isl_space_replace_params(
2328
463
              dst->nested[i], src);
2329
463
      if (!dst->nested[i])
2330
0
        goto error;
2331
463
    }
2332
65.9k
  }
2333
65.9k
2334
65.9k
  return dst;
2335
0
error:
2336
0
  isl_space_free(dst);
2337
0
  return NULL;
2338
65.9k
}
2339
2340
/* Given a dimension specification "dim" of a set, create a dimension
2341
 * specification for the lift of the set.  In particular, the result
2342
 * is of the form [dim -> local[..]], with n_local variables in the
2343
 * range of the wrapped map.
2344
 */
2345
__isl_give isl_space *isl_space_lift(__isl_take isl_space *dim, unsigned n_local)
2346
21.5k
{
2347
21.5k
  isl_space *local_dim;
2348
21.5k
2349
21.5k
  if (!dim)
2350
0
    return NULL;
2351
21.5k
2352
21.5k
  local_dim = isl_space_dup(dim);
2353
21.5k
  local_dim = isl_space_drop_dims(local_dim, isl_dim_set, 0, dim->n_out);
2354
21.5k
  local_dim = isl_space_add_dims(local_dim, isl_dim_set, n_local);
2355
21.5k
  local_dim = isl_space_set_tuple_name(local_dim, isl_dim_set, "local");
2356
21.5k
  dim = isl_space_join(isl_space_from_domain(dim),
2357
21.5k
          isl_space_from_range(local_dim));
2358
21.5k
  dim = isl_space_wrap(dim);
2359
21.5k
  dim = isl_space_set_tuple_name(dim, isl_dim_set, "lifted");
2360
21.5k
2361
21.5k
  return dim;
2362
21.5k
}
2363
2364
isl_bool isl_space_can_zip(__isl_keep isl_space *space)
2365
6.14k
{
2366
6.14k
  isl_bool is_set;
2367
6.14k
2368
6.14k
  is_set = isl_space_is_set(space);
2369
6.14k
  if (is_set < 0)
2370
0
    return isl_bool_error;
2371
6.14k
  if (is_set)
2372
0
    return isl_bool_false;
2373
6.14k
  return isl_space_is_product(space);
2374
6.14k
}
2375
2376
__isl_give isl_space *isl_space_zip(__isl_take isl_space *dim)
2377
2.63k
{
2378
2.63k
  isl_space *dom, *ran;
2379
2.63k
  isl_space *dom_dom, *dom_ran, *ran_dom, *ran_ran;
2380
2.63k
2381
2.63k
  if (!isl_space_can_zip(dim))
2382
2.63k
    
isl_die0
(dim->ctx, isl_error_invalid, "dim cannot be zipped",
2383
2.63k
      goto error);
2384
2.63k
2385
2.63k
  if (!dim)
2386
0
    return NULL;
2387
2.63k
  dom = isl_space_unwrap(isl_space_domain(isl_space_copy(dim)));
2388
2.63k
  ran = isl_space_unwrap(isl_space_range(dim));
2389
2.63k
  dom_dom = isl_space_domain(isl_space_copy(dom));
2390
2.63k
  dom_ran = isl_space_range(dom);
2391
2.63k
  ran_dom = isl_space_domain(isl_space_copy(ran));
2392
2.63k
  ran_ran = isl_space_range(ran);
2393
2.63k
  dom = isl_space_join(isl_space_from_domain(dom_dom),
2394
2.63k
         isl_space_from_range(ran_dom));
2395
2.63k
  ran = isl_space_join(isl_space_from_domain(dom_ran),
2396
2.63k
         isl_space_from_range(ran_ran));
2397
2.63k
  return isl_space_join(isl_space_from_domain(isl_space_wrap(dom)),
2398
2.63k
          isl_space_from_range(isl_space_wrap(ran)));
2399
0
error:
2400
0
  isl_space_free(dim);
2401
0
  return NULL;
2402
2.63k
}
2403
2404
/* Can we apply isl_space_curry to "space"?
2405
 * That is, does it have a nested relation in its domain?
2406
 */
2407
isl_bool isl_space_can_curry(__isl_keep isl_space *space)
2408
41.1k
{
2409
41.1k
  if (!space)
2410
0
    return isl_bool_error;
2411
41.1k
2412
41.1k
  return !!space->nested[0];
2413
41.1k
}
2414
2415
/* Given a space (A -> B) -> C, return the corresponding space
2416
 * A -> (B -> C).
2417
 */
2418
__isl_give isl_space *isl_space_curry(__isl_take isl_space *space)
2419
13.3k
{
2420
13.3k
  isl_space *dom, *ran;
2421
13.3k
  isl_space *dom_dom, *dom_ran;
2422
13.3k
2423
13.3k
  if (!space)
2424
0
    return NULL;
2425
13.3k
2426
13.3k
  if (!isl_space_can_curry(space))
2427
13.3k
    
isl_die0
(space->ctx, isl_error_invalid,
2428
13.3k
      "space cannot be curried", goto error);
2429
13.3k
2430
13.3k
  dom = isl_space_unwrap(isl_space_domain(isl_space_copy(space)));
2431
13.3k
  ran = isl_space_range(space);
2432
13.3k
  dom_dom = isl_space_domain(isl_space_copy(dom));
2433
13.3k
  dom_ran = isl_space_range(dom);
2434
13.3k
  ran = isl_space_join(isl_space_from_domain(dom_ran),
2435
13.3k
         isl_space_from_range(ran));
2436
13.3k
  return isl_space_join(isl_space_from_domain(dom_dom),
2437
13.3k
          isl_space_from_range(isl_space_wrap(ran)));
2438
0
error:
2439
0
  isl_space_free(space);
2440
0
  return NULL;
2441
13.3k
}
2442
2443
/* Can isl_space_range_curry be applied to "space"?
2444
 * That is, does it have a nested relation in its range,
2445
 * the domain of which is itself a nested relation?
2446
 */
2447
isl_bool isl_space_can_range_curry(__isl_keep isl_space *space)
2448
25.3k
{
2449
25.3k
  isl_bool can;
2450
25.3k
2451
25.3k
  if (!space)
2452
0
    return isl_bool_error;
2453
25.3k
  can = isl_space_range_is_wrapping(space);
2454
25.3k
  if (can < 0 || !can)
2455
0
    return can;
2456
25.3k
  return isl_space_can_curry(space->nested[1]);
2457
25.3k
}
2458
2459
/* Given a space A -> ((B -> C) -> D), return the corresponding space
2460
 * A -> (B -> (C -> D)).
2461
 */
2462
__isl_give isl_space *isl_space_range_curry(__isl_take isl_space *space)
2463
12.6k
{
2464
12.6k
  if (!space)
2465
0
    return NULL;
2466
12.6k
2467
12.6k
  if (!isl_space_can_range_curry(space))
2468
12.6k
    
isl_die0
(isl_space_get_ctx(space), isl_error_invalid,
2469
12.6k
      "space range cannot be curried",
2470
12.6k
      return isl_space_free(space));
2471
12.6k
2472
12.6k
  space = isl_space_cow(space);
2473
12.6k
  if (!space)
2474
0
    return NULL;
2475
12.6k
  space->nested[1] = isl_space_curry(space->nested[1]);
2476
12.6k
  if (!space->nested[1])
2477
0
    return isl_space_free(space);
2478
12.6k
2479
12.6k
  return space;
2480
12.6k
}
2481
2482
/* Can we apply isl_space_uncurry to "space"?
2483
 * That is, does it have a nested relation in its range?
2484
 */
2485
isl_bool isl_space_can_uncurry(__isl_keep isl_space *space)
2486
1.68k
{
2487
1.68k
  if (!space)
2488
0
    return isl_bool_error;
2489
1.68k
2490
1.68k
  return !!space->nested[1];
2491
1.68k
}
2492
2493
/* Given a space A -> (B -> C), return the corresponding space
2494
 * (A -> B) -> C.
2495
 */
2496
__isl_give isl_space *isl_space_uncurry(__isl_take isl_space *space)
2497
591
{
2498
591
  isl_space *dom, *ran;
2499
591
  isl_space *ran_dom, *ran_ran;
2500
591
2501
591
  if (!space)
2502
0
    return NULL;
2503
591
2504
591
  if (!isl_space_can_uncurry(space))
2505
591
    
isl_die0
(space->ctx, isl_error_invalid,
2506
591
      "space cannot be uncurried",
2507
591
      return isl_space_free(space));
2508
591
2509
591
  dom = isl_space_domain(isl_space_copy(space));
2510
591
  ran = isl_space_unwrap(isl_space_range(space));
2511
591
  ran_dom = isl_space_domain(isl_space_copy(ran));
2512
591
  ran_ran = isl_space_range(ran);
2513
591
  dom = isl_space_join(isl_space_from_domain(dom),
2514
591
         isl_space_from_range(ran_dom));
2515
591
  return isl_space_join(isl_space_from_domain(isl_space_wrap(dom)),
2516
591
          isl_space_from_range(ran_ran));
2517
591
}
2518
2519
isl_bool isl_space_has_named_params(__isl_keep isl_space *space)
2520
524k
{
2521
524k
  int i;
2522
524k
  unsigned off;
2523
524k
2524
524k
  if (!space)
2525
2
    return isl_bool_error;
2526
524k
  if (space->nparam == 0)
2527
253k
    return isl_bool_true;
2528
270k
  off = isl_space_offset(space, isl_dim_param);
2529
270k
  if (off + space->nparam > space->n_id)
2530
0
    return isl_bool_false;
2531
1.09M
  
for (i = 0; 270k
i < space->nparam;
++i822k
)
2532
822k
    if (!space->ids[off + i])
2533
0
      return isl_bool_false;
2534
270k
  return isl_bool_true;
2535
270k
}
2536
2537
/* Check that "space" has only named parameters, reporting an error
2538
 * if it does not.
2539
 */
2540
isl_stat isl_space_check_named_params(__isl_keep isl_space *space)
2541
337k
{
2542
337k
  isl_bool named;
2543
337k
2544
337k
  named = isl_space_has_named_params(space);
2545
337k
  if (named < 0)
2546
2
    return isl_stat_error;
2547
337k
  if (!named)
2548
337k
    
isl_die0
(isl_space_get_ctx(space), isl_error_invalid,
2549
337k
      "unexpected unnamed parameters", return isl_stat_error);
2550
337k
2551
337k
  return isl_stat_ok;
2552
337k
}
2553
2554
/* Align the initial parameters of space1 to match the order in space2.
2555
 */
2556
__isl_give isl_space *isl_space_align_params(__isl_take isl_space *space1,
2557
  __isl_take isl_space *space2)
2558
35.4k
{
2559
35.4k
  isl_reordering *exp;
2560
35.4k
2561
35.4k
  if (isl_space_check_named_params(space1) < 0 ||
2562
35.4k
      
isl_space_check_named_params(space2) < 035.3k
)
2563
2
    goto error;
2564
35.3k
2565
35.3k
  exp = isl_parameter_alignment_reordering(space1, space2);
2566
35.3k
  exp = isl_reordering_extend_space(exp, space1);
2567
35.3k
  isl_space_free(space2);
2568
35.3k
  space1 = isl_reordering_get_space(exp);
2569
35.3k
  isl_reordering_free(exp);
2570
35.3k
  return space1;
2571
2
error:
2572
2
  isl_space_free(space1);
2573
2
  isl_space_free(space2);
2574
2
  return NULL;
2575
35.3k
}
2576
2577
/* Given the space of set (domain), construct a space for a map
2578
 * with as domain the given space and as range the range of "model".
2579
 */
2580
__isl_give isl_space *isl_space_extend_domain_with_range(
2581
  __isl_take isl_space *space, __isl_take isl_space *model)
2582
73.4k
{
2583
73.4k
  if (!model)
2584
0
    goto error;
2585
73.4k
2586
73.4k
  space = isl_space_from_domain(space);
2587
73.4k
  space = isl_space_add_dims(space, isl_dim_out,
2588
73.4k
            isl_space_dim(model, isl_dim_out));
2589
73.4k
  if (isl_space_has_tuple_id(model, isl_dim_out))
2590
125
    space = isl_space_set_tuple_id(space, isl_dim_out,
2591
125
        isl_space_get_tuple_id(model, isl_dim_out));
2592
73.4k
  if (!space)
2593
0
    goto error;
2594
73.4k
  if (model->nested[1]) {
2595
1
    isl_space *nested = isl_space_copy(model->nested[1]);
2596
1
    int n_nested, n_space;
2597
1
    nested = isl_space_align_params(nested, isl_space_copy(space));
2598
1
    n_nested = isl_space_dim(nested, isl_dim_param);
2599
1
    n_space = isl_space_dim(space, isl_dim_param);
2600
1
    if (n_nested > n_space)
2601
0
      nested = isl_space_drop_dims(nested, isl_dim_param,
2602
0
            n_space, n_nested - n_space);
2603
1
    if (!nested)
2604
0
      goto error;
2605
1
    space->nested[1] = nested;
2606
1
  }
2607
73.4k
  isl_space_free(model);
2608
73.4k
  return space;
2609
0
error:
2610
0
  isl_space_free(model);
2611
0
  isl_space_free(space);
2612
0
  return NULL;
2613
73.4k
}
2614
2615
/* Compare the "type" dimensions of two isl_spaces.
2616
 *
2617
 * The order is fairly arbitrary.
2618
 */
2619
static int isl_space_cmp_type(__isl_keep isl_space *space1,
2620
  __isl_keep isl_space *space2, enum isl_dim_type type)
2621
1.51M
{
2622
1.51M
  int cmp;
2623
1.51M
  isl_space *nested1, *nested2;
2624
1.51M
2625
1.51M
  if (isl_space_dim(space1, type) != isl_space_dim(space2, type))
2626
0
    return isl_space_dim(space1, type) -
2627
0
      isl_space_dim(space2, type);
2628
1.51M
2629
1.51M
  cmp = isl_id_cmp(tuple_id(space1, type), tuple_id(space2, type));
2630
1.51M
  if (cmp != 0)
2631
0
    return cmp;
2632
1.51M
2633
1.51M
  nested1 = nested(space1, type);
2634
1.51M
  nested2 = nested(space2, type);
2635
1.51M
  if (!nested1 != !nested2)
2636
0
    return !nested1 - !nested2;
2637
1.51M
2638
1.51M
  if (nested1)
2639
130k
    return isl_space_cmp(nested1, nested2);
2640
1.38M
2641
1.38M
  return 0;
2642
1.38M
}
2643
2644
/* Compare two isl_spaces.
2645
 *
2646
 * The order is fairly arbitrary.
2647
 */
2648
int isl_space_cmp(__isl_keep isl_space *space1, __isl_keep isl_space *space2)
2649
1.64M
{
2650
1.64M
  int i;
2651
1.64M
  int cmp;
2652
1.64M
2653
1.64M
  if (space1 == space2)
2654
1.13M
    return 0;
2655
505k
  if (!space1)
2656
0
    return -1;
2657
505k
  if (!space2)
2658
0
    return 1;
2659
505k
2660
505k
  cmp = isl_space_cmp_type(space1, space2, isl_dim_param);
2661
505k
  if (cmp != 0)
2662
0
    return cmp;
2663
505k
  cmp = isl_space_cmp_type(space1, space2, isl_dim_in);
2664
505k
  if (cmp != 0)
2665
0
    return cmp;
2666
505k
  cmp = isl_space_cmp_type(space1, space2, isl_dim_out);
2667
505k
  if (cmp != 0)
2668
0
    return cmp;
2669
505k
2670
505k
  if (!space1->ids && 
!space2->ids128k
)
2671
128k
    return 0;
2672
377k
2673
3.70M
  
for (i = 0; 377k
i < n(space1, isl_dim_param);
++i3.33M
) {
2674
3.33M
    cmp = isl_id_cmp(get_id(space1, isl_dim_param, i),
2675
3.33M
         get_id(space2, isl_dim_param, i));
2676
3.33M
    if (cmp != 0)
2677
0
      return cmp;
2678
3.33M
  }
2679
377k
2680
377k
  return 0;
2681
377k
}