Coverage Report

Created: 2017-11-21 16:49

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