Coverage Report

Created: 2017-11-21 16:49

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/polly/lib/External/isl/isl_ast_build_expr.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2012-2014 Ecole Normale Superieure
3
 * Copyright 2014      INRIA Rocquencourt
4
 *
5
 * Use of this software is governed by the MIT license
6
 *
7
 * Written by Sven Verdoolaege,
8
 * Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
9
 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
10
 * B.P. 105 - 78153 Le Chesnay, France
11
 */
12
13
#include <isl/constraint.h>
14
#include <isl/ilp.h>
15
#include <isl_ast_build_expr.h>
16
#include <isl_ast_private.h>
17
#include <isl_ast_build_private.h>
18
#include <isl_sort.h>
19
20
/* Compute the "opposite" of the (numerator of the) argument of a div
21
 * with denominator "d".
22
 *
23
 * In particular, compute
24
 *
25
 *  -aff + (d - 1)
26
 */
27
static __isl_give isl_aff *oppose_div_arg(__isl_take isl_aff *aff,
28
  __isl_take isl_val *d)
29
48
{
30
48
  aff = isl_aff_neg(aff);
31
48
  aff = isl_aff_add_constant_val(aff, d);
32
48
  aff = isl_aff_add_constant_si(aff, -1);
33
48
34
48
  return aff;
35
48
}
36
37
/* Internal data structure used inside isl_ast_expr_add_term.
38
 * The domain of "build" is used to simplify the expressions.
39
 * "build" needs to be set by the caller of isl_ast_expr_add_term.
40
 * "cst" is the constant term of the expression in which the added term
41
 * appears.  It may be modified by isl_ast_expr_add_term.
42
 *
43
 * "v" is the coefficient of the term that is being constructed and
44
 * is set internally by isl_ast_expr_add_term.
45
 */
46
struct isl_ast_add_term_data {
47
  isl_ast_build *build;
48
  isl_val *cst;
49
  isl_val *v;
50
};
51
52
/* Given the numerator "aff" of the argument of an integer division
53
 * with denominator "d", check if it can be made non-negative over
54
 * data->build->domain by stealing part of the constant term of
55
 * the expression in which the integer division appears.
56
 *
57
 * In particular, the outer expression is of the form
58
 *
59
 *  v * floor(aff/d) + cst
60
 *
61
 * We already know that "aff" itself may attain negative values.
62
 * Here we check if aff + d*floor(cst/v) is non-negative, such
63
 * that we could rewrite the expression to
64
 *
65
 *  v * floor((aff + d*floor(cst/v))/d) + cst - v*floor(cst/v)
66
 *
67
 * Note that aff + d*floor(cst/v) can only possibly be non-negative
68
 * if data->cst and data->v have the same sign.
69
 * Similarly, if floor(cst/v) is zero, then there is no point in
70
 * checking again.
71
 */
72
static int is_non_neg_after_stealing(__isl_keep isl_aff *aff,
73
  __isl_keep isl_val *d, struct isl_ast_add_term_data *data)
74
6
{
75
6
  isl_aff *shifted;
76
6
  isl_val *shift;
77
6
  int is_zero;
78
6
  int non_neg;
79
6
80
6
  if (isl_val_sgn(data->cst) != isl_val_sgn(data->v))
81
5
    return 0;
82
1
83
1
  shift = isl_val_div(isl_val_copy(data->cst), isl_val_copy(data->v));
84
1
  shift = isl_val_floor(shift);
85
1
  is_zero = isl_val_is_zero(shift);
86
1
  if (is_zero < 0 || is_zero) {
87
1
    isl_val_free(shift);
88
1
    return is_zero < 0 ? 
-10
: 0;
89
1
  }
90
0
  shift = isl_val_mul(shift, isl_val_copy(d));
91
0
  shifted = isl_aff_copy(aff);
92
0
  shifted = isl_aff_add_constant_val(shifted, shift);
93
0
  non_neg = isl_ast_build_aff_is_nonneg(data->build, shifted);
94
0
  isl_aff_free(shifted);
95
0
96
0
  return non_neg;
97
0
}
98
99
/* Given the numerator "aff' of the argument of an integer division
100
 * with denominator "d", steal part of the constant term of
101
 * the expression in which the integer division appears to make it
102
 * non-negative over data->build->domain.
103
 *
104
 * In particular, the outer expression is of the form
105
 *
106
 *  v * floor(aff/d) + cst
107
 *
108
 * We know that "aff" itself may attain negative values,
109
 * but that aff + d*floor(cst/v) is non-negative.
110
 * Find the minimal positive value that we need to add to "aff"
111
 * to make it positive and adjust data->cst accordingly.
112
 * That is, compute the minimal value "m" of "aff" over
113
 * data->build->domain and take
114
 *
115
 *  s = ceil(m/d)
116
 *
117
 * such that
118
 *
119
 *  aff + d * s >= 0
120
 *
121
 * and rewrite the expression to
122
 *
123
 *  v * floor((aff + s*d)/d) + (cst - v*s)
124
 */
125
static __isl_give isl_aff *steal_from_cst(__isl_take isl_aff *aff,
126
  __isl_keep isl_val *d, struct isl_ast_add_term_data *data)
127
0
{
128
0
  isl_set *domain;
129
0
  isl_val *shift, *t;
130
0
131
0
  domain = isl_ast_build_get_domain(data->build);
132
0
  shift = isl_set_min_val(domain, aff);
133
0
  isl_set_free(domain);
134
0
135
0
  shift = isl_val_neg(shift);
136
0
  shift = isl_val_div(shift, isl_val_copy(d));
137
0
  shift = isl_val_ceil(shift);
138
0
139
0
  t = isl_val_copy(shift);
140
0
  t = isl_val_mul(t, isl_val_copy(data->v));
141
0
  data->cst = isl_val_sub(data->cst, t);
142
0
143
0
  shift = isl_val_mul(shift, isl_val_copy(d));
144
0
  return isl_aff_add_constant_val(aff, shift);
145
0
}
146
147
/* Create an isl_ast_expr evaluating the div at position "pos" in "ls".
148
 * The result is simplified in terms of data->build->domain.
149
 * This function may change (the sign of) data->v.
150
 *
151
 * "ls" is known to be non-NULL.
152
 *
153
 * Let the div be of the form floor(e/d).
154
 * If the ast_build_prefer_pdiv option is set then we check if "e"
155
 * is non-negative, so that we can generate
156
 *
157
 *  (pdiv_q, expr(e), expr(d))
158
 *
159
 * instead of
160
 *
161
 *  (fdiv_q, expr(e), expr(d))
162
 *
163
 * If the ast_build_prefer_pdiv option is set and
164
 * if "e" is not non-negative, then we check if "-e + d - 1" is non-negative.
165
 * If so, we can rewrite
166
 *
167
 *  floor(e/d) = -ceil(-e/d) = -floor((-e + d - 1)/d)
168
 *
169
 * and still use pdiv_q, while changing the sign of data->v.
170
 *
171
 * Otherwise, we check if
172
 *
173
 *  e + d*floor(cst/v)
174
 *
175
 * is non-negative and if so, replace floor(e/d) by
176
 *
177
 *  floor((e + s*d)/d) - s
178
 *
179
 * with s the minimal shift that makes the argument non-negative.
180
 */
181
static __isl_give isl_ast_expr *var_div(struct isl_ast_add_term_data *data,
182
  __isl_keep isl_local_space *ls, int pos)
183
14
{
184
14
  isl_ctx *ctx = isl_local_space_get_ctx(ls);
185
14
  isl_aff *aff;
186
14
  isl_ast_expr *num, *den;
187
14
  isl_val *d;
188
14
  enum isl_ast_op_type type;
189
14
190
14
  aff = isl_local_space_get_div(ls, pos);
191
14
  d = isl_aff_get_denominator_val(aff);
192
14
  aff = isl_aff_scale_val(aff, isl_val_copy(d));
193
14
  den = isl_ast_expr_from_val(isl_val_copy(d));
194
14
195
14
  type = isl_ast_op_fdiv_q;
196
14
  if (isl_options_get_ast_build_prefer_pdiv(ctx)) {
197
14
    int non_neg = isl_ast_build_aff_is_nonneg(data->build, aff);
198
14
    if (non_neg >= 0 && !non_neg) {
199
6
      isl_aff *opp = oppose_div_arg(isl_aff_copy(aff),
200
6
              isl_val_copy(d));
201
6
      non_neg = isl_ast_build_aff_is_nonneg(data->build, opp);
202
6
      if (non_neg >= 0 && non_neg) {
203
0
        data->v = isl_val_neg(data->v);
204
0
        isl_aff_free(aff);
205
0
        aff = opp;
206
0
      } else
207
6
        isl_aff_free(opp);
208
6
    }
209
14
    if (non_neg >= 0 && !non_neg) {
210
6
      non_neg = is_non_neg_after_stealing(aff, d, data);
211
6
      if (non_neg >= 0 && non_neg)
212
0
        aff = steal_from_cst(aff, d, data);
213
6
    }
214
14
    if (non_neg < 0)
215
0
      aff = isl_aff_free(aff);
216
14
    else if (non_neg)
217
8
      type = isl_ast_op_pdiv_q;
218
14
  }
219
14
220
14
  isl_val_free(d);
221
14
  num = isl_ast_expr_from_aff(aff, data->build);
222
14
  return isl_ast_expr_alloc_binary(type, num, den);
223
14
}
224
225
/* Create an isl_ast_expr evaluating the specified dimension of "ls".
226
 * The result is simplified in terms of data->build->domain.
227
 * This function may change (the sign of) data->v.
228
 *
229
 * The isl_ast_expr is constructed based on the type of the dimension.
230
 * - divs are constructed by var_div
231
 * - set variables are constructed from the iterator isl_ids in data->build
232
 * - parameters are constructed from the isl_ids in "ls"
233
 */
234
static __isl_give isl_ast_expr *var(struct isl_ast_add_term_data *data,
235
  __isl_keep isl_local_space *ls, enum isl_dim_type type, int pos)
236
4.97k
{
237
4.97k
  isl_ctx *ctx = isl_local_space_get_ctx(ls);
238
4.97k
  isl_id *id;
239
4.97k
240
4.97k
  if (type == isl_dim_div)
241
14
    return var_div(data, ls, pos);
242
4.96k
243
4.96k
  if (type == isl_dim_set) {
244
4.55k
    id = isl_ast_build_get_iterator_id(data->build, pos);
245
4.55k
    return isl_ast_expr_from_id(id);
246
4.55k
  }
247
411
248
411
  if (!isl_local_space_has_dim_id(ls, type, pos))
249
411
    
isl_die0
(ctx, isl_error_internal, "unnamed dimension",
250
411
      return NULL);
251
411
  id = isl_local_space_get_dim_id(ls, type, pos);
252
411
  return isl_ast_expr_from_id(id);
253
4.97k
}
254
255
/* Does "expr" represent the zero integer?
256
 */
257
static int ast_expr_is_zero(__isl_keep isl_ast_expr *expr)
258
15.1k
{
259
15.1k
  if (!expr)
260
0
    return -1;
261
15.1k
  if (expr->type != isl_ast_expr_int)
262
5.08k
    return 0;
263
10.0k
  return isl_val_is_zero(expr->u.v);
264
10.0k
}
265
266
/* Create an expression representing the sum of "expr1" and "expr2",
267
 * provided neither of the two expressions is identically zero.
268
 */
269
static __isl_give isl_ast_expr *ast_expr_add(__isl_take isl_ast_expr *expr1,
270
  __isl_take isl_ast_expr *expr2)
271
7.24k
{
272
7.24k
  if (!expr1 || !expr2)
273
0
    goto error;
274
7.24k
275
7.24k
  if (ast_expr_is_zero(expr1)) {
276
4.18k
    isl_ast_expr_free(expr1);
277
4.18k
    return expr2;
278
4.18k
  }
279
3.06k
280
3.06k
  if (ast_expr_is_zero(expr2)) {
281
0
    isl_ast_expr_free(expr2);
282
0
    return expr1;
283
0
  }
284
3.06k
285
3.06k
  return isl_ast_expr_add(expr1, expr2);
286
0
error:
287
0
  isl_ast_expr_free(expr1);
288
0
  isl_ast_expr_free(expr2);
289
0
  return NULL;
290
7.24k
}
291
292
/* Subtract expr2 from expr1.
293
 *
294
 * If expr2 is zero, we simply return expr1.
295
 * If expr1 is zero, we return
296
 *
297
 *  (isl_ast_op_minus, expr2)
298
 *
299
 * Otherwise, we return
300
 *
301
 *  (isl_ast_op_sub, expr1, expr2)
302
 */
303
static __isl_give isl_ast_expr *ast_expr_sub(__isl_take isl_ast_expr *expr1,
304
  __isl_take isl_ast_expr *expr2)
305
4.31k
{
306
4.31k
  if (!expr1 || !expr2)
307
0
    goto error;
308
4.31k
309
4.31k
  if (ast_expr_is_zero(expr2)) {
310
4.21k
    isl_ast_expr_free(expr2);
311
4.21k
    return expr1;
312
4.21k
  }
313
101
314
101
  if (ast_expr_is_zero(expr1)) {
315
41
    isl_ast_expr_free(expr1);
316
41
    return isl_ast_expr_neg(expr2);
317
41
  }
318
60
319
60
  return isl_ast_expr_sub(expr1, expr2);
320
0
error:
321
0
  isl_ast_expr_free(expr1);
322
0
  isl_ast_expr_free(expr2);
323
0
  return NULL;
324
4.31k
}
325
326
/* Return an isl_ast_expr that represents
327
 *
328
 *  v * (aff mod d)
329
 *
330
 * v is assumed to be non-negative.
331
 * The result is simplified in terms of build->domain.
332
 */
333
static __isl_give isl_ast_expr *isl_ast_expr_mod(__isl_keep isl_val *v,
334
  __isl_keep isl_aff *aff, __isl_keep isl_val *d,
335
  __isl_keep isl_ast_build *build)
336
68
{
337
68
  isl_ast_expr *expr;
338
68
  isl_ast_expr *c;
339
68
340
68
  if (!aff)
341
0
    return NULL;
342
68
343
68
  expr = isl_ast_expr_from_aff(isl_aff_copy(aff), build);
344
68
345
68
  c = isl_ast_expr_from_val(isl_val_copy(d));
346
68
  expr = isl_ast_expr_alloc_binary(isl_ast_op_pdiv_r, expr, c);
347
68
348
68
  if (!isl_val_is_one(v)) {
349
0
    c = isl_ast_expr_from_val(isl_val_copy(v));
350
0
    expr = isl_ast_expr_mul(c, expr);
351
0
  }
352
68
353
68
  return expr;
354
68
}
355
356
/* Create an isl_ast_expr that scales "expr" by "v".
357
 *
358
 * If v is 1, we simply return expr.
359
 * If v is -1, we return
360
 *
361
 *  (isl_ast_op_minus, expr)
362
 *
363
 * Otherwise, we return
364
 *
365
 *  (isl_ast_op_mul, expr(v), expr)
366
 */
367
static __isl_give isl_ast_expr *scale(__isl_take isl_ast_expr *expr,
368
  __isl_take isl_val *v)
369
4.97k
{
370
4.97k
  isl_ast_expr *c;
371
4.97k
372
4.97k
  if (!expr || !v)
373
0
    goto error;
374
4.97k
  if (isl_val_is_one(v)) {
375
1.83k
    isl_val_free(v);
376
1.83k
    return expr;
377
1.83k
  }
378
3.13k
379
3.13k
  if (isl_val_is_negone(v)) {
380
6
    isl_val_free(v);
381
6
    expr = isl_ast_expr_neg(expr);
382
3.13k
  } else {
383
3.13k
    c = isl_ast_expr_from_val(v);
384
3.13k
    expr = isl_ast_expr_mul(c, expr);
385
3.13k
  }
386
3.13k
387
3.13k
  return expr;
388
0
error:
389
0
  isl_val_free(v);
390
0
  isl_ast_expr_free(expr);
391
0
  return NULL;
392
4.97k
}
393
394
/* Add an expression for "*v" times the specified dimension of "ls"
395
 * to expr.
396
 * If the dimension is an integer division, then this function
397
 * may modify data->cst in order to make the numerator non-negative.
398
 * The result is simplified in terms of data->build->domain.
399
 *
400
 * Let e be the expression for the specified dimension,
401
 * multiplied by the absolute value of "*v".
402
 * If "*v" is negative, we create
403
 *
404
 *  (isl_ast_op_sub, expr, e)
405
 *
406
 * except when expr is trivially zero, in which case we create
407
 *
408
 *  (isl_ast_op_minus, e)
409
 *
410
 * instead.
411
 *
412
 * If "*v" is positive, we simply create
413
 *
414
 *  (isl_ast_op_add, expr, e)
415
 *
416
 */
417
static __isl_give isl_ast_expr *isl_ast_expr_add_term(
418
  __isl_take isl_ast_expr *expr,
419
  __isl_keep isl_local_space *ls, enum isl_dim_type type, int pos,
420
  __isl_take isl_val *v, struct isl_ast_add_term_data *data)
421
4.97k
{
422
4.97k
  isl_ast_expr *term;
423
4.97k
424
4.97k
  if (!expr)
425
0
    return NULL;
426
4.97k
427
4.97k
  data->v = v;
428
4.97k
  term = var(data, ls, type, pos);
429
4.97k
  v = data->v;
430
4.97k
431
4.97k
  if (isl_val_is_neg(v) && 
!ast_expr_is_zero(expr)29
) {
432
7
    v = isl_val_neg(v);
433
7
    term = scale(term, v);
434
7
    return ast_expr_sub(expr, term);
435
4.96k
  } else {
436
4.96k
    term = scale(term, v);
437
4.96k
    return ast_expr_add(expr, term);
438
4.96k
  }
439
4.97k
}
440
441
/* Add an expression for "v" to expr.
442
 */
443
static __isl_give isl_ast_expr *isl_ast_expr_add_int(
444
  __isl_take isl_ast_expr *expr, __isl_take isl_val *v)
445
4.44k
{
446
4.44k
  isl_ast_expr *expr_int;
447
4.44k
448
4.44k
  if (!expr || !v)
449
0
    goto error;
450
4.44k
451
4.44k
  if (isl_val_is_zero(v)) {
452
2.18k
    isl_val_free(v);
453
2.18k
    return expr;
454
2.18k
  }
455
2.26k
456
2.26k
  if (isl_val_is_neg(v) && 
!ast_expr_is_zero(expr)77
) {
457
53
    v = isl_val_neg(v);
458
53
    expr_int = isl_ast_expr_from_val(v);
459
53
    return ast_expr_sub(expr, expr_int);
460
2.20k
  } else {
461
2.20k
    expr_int = isl_ast_expr_from_val(v);
462
2.20k
    return ast_expr_add(expr, expr_int);
463
2.20k
  }
464
0
error:
465
0
  isl_ast_expr_free(expr);
466
0
  isl_val_free(v);
467
0
  return NULL;
468
4.44k
}
469
470
/* Internal data structure used inside extract_modulos.
471
 *
472
 * If any modulo expressions are detected in "aff", then the
473
 * expression is removed from "aff" and added to either "pos" or "neg"
474
 * depending on the sign of the coefficient of the modulo expression
475
 * inside "aff".
476
 *
477
 * "add" is an expression that needs to be added to "aff" at the end of
478
 * the computation.  It is NULL as long as no modulos have been extracted.
479
 *
480
 * "i" is the position in "aff" of the div under investigation
481
 * "v" is the coefficient in "aff" of the div
482
 * "div" is the argument of the div, with the denominator removed
483
 * "d" is the original denominator of the argument of the div
484
 *
485
 * "nonneg" is an affine expression that is non-negative over "build"
486
 * and that can be used to extract a modulo expression from "div".
487
 * In particular, if "sign" is 1, then the coefficients of "nonneg"
488
 * are equal to those of "div" modulo "d".  If "sign" is -1, then
489
 * the coefficients of "nonneg" are opposite to those of "div" modulo "d".
490
 * If "sign" is 0, then no such affine expression has been found (yet).
491
 */
492
struct isl_extract_mod_data {
493
  isl_ast_build *build;
494
  isl_aff *aff;
495
496
  isl_ast_expr *pos;
497
  isl_ast_expr *neg;
498
499
  isl_aff *add;
500
501
  int i;
502
  isl_val *v;
503
  isl_val *d;
504
  isl_aff *div;
505
506
  isl_aff *nonneg;
507
  int sign;
508
};
509
510
/* Given that data->v * div_i in data->aff is equal to
511
 *
512
 *  f * (term - (arg mod d))
513
 *
514
 * with data->d * f = data->v, add
515
 *
516
 *  f * term
517
 *
518
 * to data->add and
519
 *
520
 *  abs(f) * (arg mod d)
521
 *
522
 * to data->neg or data->pos depending on the sign of -f.
523
 */
524
static int extract_term_and_mod(struct isl_extract_mod_data *data,
525
  __isl_take isl_aff *term, __isl_take isl_aff *arg)
526
68
{
527
68
  isl_ast_expr *expr;
528
68
  int s;
529
68
530
68
  data->v = isl_val_div(data->v, isl_val_copy(data->d));
531
68
  s = isl_val_sgn(data->v);
532
68
  data->v = isl_val_abs(data->v);
533
68
  expr = isl_ast_expr_mod(data->v, arg, data->d, data->build);
534
68
  isl_aff_free(arg);
535
68
  if (s > 0)
536
41
    data->neg = ast_expr_add(data->neg, expr);
537
27
  else
538
27
    data->pos = ast_expr_add(data->pos, expr);
539
68
  data->aff = isl_aff_set_coefficient_si(data->aff,
540
68
            isl_dim_div, data->i, 0);
541
68
  if (s < 0)
542
27
    data->v = isl_val_neg(data->v);
543
68
  term = isl_aff_scale_val(term, isl_val_copy(data->v));
544
68
545
68
  if (!data->add)
546
68
    data->add = term;
547
0
  else
548
0
    data->add = isl_aff_add(data->add, term);
549
68
  if (!data->add)
550
0
    return -1;
551
68
552
68
  return 0;
553
68
}
554
555
/* Given that data->v * div_i in data->aff is of the form
556
 *
557
 *  f * d * floor(div/d)
558
 *
559
 * with div nonnegative on data->build, rewrite it as
560
 *
561
 *  f * (div - (div mod d)) = f * div - f * (div mod d)
562
 *
563
 * and add
564
 *
565
 *  f * div
566
 *
567
 * to data->add and
568
 *
569
 *  abs(f) * (div mod d)
570
 *
571
 * to data->neg or data->pos depending on the sign of -f.
572
 */
573
static int extract_mod(struct isl_extract_mod_data *data)
574
39
{
575
39
  return extract_term_and_mod(data, isl_aff_copy(data->div),
576
39
      isl_aff_copy(data->div));
577
39
}
578
579
/* Given that data->v * div_i in data->aff is of the form
580
 *
581
 *  f * d * floor(div/d)          (1)
582
 *
583
 * check if div is non-negative on data->build and, if so,
584
 * extract the corresponding modulo from data->aff.
585
 * If not, then check if
586
 *
587
 *  -div + d - 1
588
 *
589
 * is non-negative on data->build.  If so, replace (1) by
590
 *
591
 *  -f * d * floor((-div + d - 1)/d)
592
 *
593
 * and extract the corresponding modulo from data->aff.
594
 *
595
 * This function may modify data->div.
596
 */
597
static int extract_nonneg_mod(struct isl_extract_mod_data *data)
598
40
{
599
40
  int mod;
600
40
601
40
  mod = isl_ast_build_aff_is_nonneg(data->build, data->div);
602
40
  if (mod < 0)
603
0
    goto error;
604
40
  if (mod)
605
23
    return extract_mod(data);
606
17
607
17
  data->div = oppose_div_arg(data->div, isl_val_copy(data->d));
608
17
  mod = isl_ast_build_aff_is_nonneg(data->build, data->div);
609
17
  if (mod < 0)
610
0
    goto error;
611
17
  if (mod) {
612
16
    data->v = isl_val_neg(data->v);
613
16
    return extract_mod(data);
614
16
  }
615
1
616
1
  return 0;
617
0
error:
618
0
  data->aff = isl_aff_free(data->aff);
619
0
  return -1;
620
40
}
621
622
/* Is the affine expression of constraint "c" "simpler" than data->nonneg
623
 * for use in extracting a modulo expression?
624
 *
625
 * We currently only consider the constant term of the affine expression.
626
 * In particular, we prefer the affine expression with the smallest constant
627
 * term.
628
 * This means that if there are two constraints, say x >= 0 and -x + 10 >= 0,
629
 * then we would pick x >= 0
630
 *
631
 * More detailed heuristics could be used if it turns out that there is a need.
632
 */
633
static int mod_constraint_is_simpler(struct isl_extract_mod_data *data,
634
  __isl_keep isl_constraint *c)
635
30
{
636
30
  isl_val *v1, *v2;
637
30
  int simpler;
638
30
639
30
  if (!data->nonneg)
640
29
    return 1;
641
1
642
1
  v1 = isl_val_abs(isl_constraint_get_constant_val(c));
643
1
  v2 = isl_val_abs(isl_aff_get_constant_val(data->nonneg));
644
1
  simpler = isl_val_lt(v1, v2);
645
1
  isl_val_free(v1);
646
1
  isl_val_free(v2);
647
1
648
1
  return simpler;
649
1
}
650
651
/* Check if the coefficients of "c" are either equal or opposite to those
652
 * of data->div modulo data->d.  If so, and if "c" is "simpler" than
653
 * data->nonneg, then replace data->nonneg by the affine expression of "c"
654
 * and set data->sign accordingly.
655
 *
656
 * Both "c" and data->div are assumed not to involve any integer divisions.
657
 *
658
 * Before we start the actual comparison, we first quickly check if
659
 * "c" and data->div have the same non-zero coefficients.
660
 * If not, then we assume that "c" is not of the desired form.
661
 * Note that while the coefficients of data->div can be reasonably expected
662
 * not to involve any coefficients that are multiples of d, "c" may
663
 * very well involve such coefficients.  This means that we may actually
664
 * miss some cases.
665
 *
666
 * If the constant term is "too large", then the constraint is rejected,
667
 * where "too large" is fairly arbitrarily set to 1 << 15.
668
 * We do this to avoid picking up constraints that bound a variable
669
 * by a very large number, say the largest or smallest possible
670
 * variable in the representation of some integer type.
671
 */
672
static isl_stat check_parallel_or_opposite(__isl_take isl_constraint *c,
673
  void *user)
674
935
{
675
935
  struct isl_extract_mod_data *data = user;
676
935
  enum isl_dim_type c_type[2] = { isl_dim_param, isl_dim_set };
677
935
  enum isl_dim_type a_type[2] = { isl_dim_param, isl_dim_in };
678
935
  int i, t;
679
935
  int n[2];
680
935
  int parallel = 1, opposite = 1;
681
935
682
2.80k
  for (t = 0; t < 2; 
++t1.87k
) {
683
1.87k
    n[t] = isl_constraint_dim(c, c_type[t]);
684
12.7k
    for (i = 0; i < n[t]; 
++i10.8k
) {
685
10.8k
      int a, b;
686
10.8k
687
10.8k
      a = isl_constraint_involves_dims(c, c_type[t], i, 1);
688
10.8k
      b = isl_aff_involves_dims(data->div, a_type[t], i, 1);
689
10.8k
      if (a != b)
690
1.83k
        parallel = opposite = 0;
691
10.8k
    }
692
1.87k
  }
693
935
694
935
  if (parallel || 
opposite839
) {
695
96
    isl_val *v;
696
96
697
96
    v = isl_val_abs(isl_constraint_get_constant_val(c));
698
96
    if (isl_val_cmp_si(v, 1 << 15) > 0)
699
66
      parallel = opposite = 0;
700
96
    isl_val_free(v);
701
96
  }
702
935
703
2.80k
  for (t = 0; t < 2; 
++t1.87k
) {
704
2.18k
    for (i = 0; i < n[t]; 
++i310
) {
705
2.10k
      isl_val *v1, *v2;
706
2.10k
707
2.10k
      if (!parallel && 
!opposite2.03k
)
708
1.79k
        break;
709
310
      v1 = isl_constraint_get_coefficient_val(c,
710
310
                c_type[t], i);
711
310
      v2 = isl_aff_get_coefficient_val(data->div,
712
310
                a_type[t], i);
713
310
      if (parallel) {
714
67
        v1 = isl_val_sub(v1, isl_val_copy(v2));
715
67
        parallel = isl_val_is_divisible_by(v1, data->d);
716
67
        v1 = isl_val_add(v1, isl_val_copy(v2));
717
67
      }
718
310
      if (opposite) {
719
309
        v1 = isl_val_add(v1, isl_val_copy(v2));
720
309
        opposite = isl_val_is_divisible_by(v1, data->d);
721
309
      }
722
2.10k
      isl_val_free(v1);
723
2.10k
      isl_val_free(v2);
724
2.10k
    }
725
1.87k
  }
726
935
727
935
  if ((parallel || 
opposite931
) &&
mod_constraint_is_simpler(data, c)30
) {
728
29
    isl_aff_free(data->nonneg);
729
29
    data->nonneg = isl_constraint_get_aff(c);
730
29
    data->sign = parallel ? 
14
:
-125
;
731
29
  }
732
935
733
935
  isl_constraint_free(c);
734
935
735
935
  if (data->sign != 0 && 
data->nonneg == NULL344
)
736
935
    
return isl_stat_error0
;
737
935
738
935
  return isl_stat_ok;
739
935
}
740
741
/* Given that data->v * div_i in data->aff is of the form
742
 *
743
 *  f * d * floor(div/d)          (1)
744
 *
745
 * see if we can find an expression div' that is non-negative over data->build
746
 * and that is related to div through
747
 *
748
 *  div' = div + d * e
749
 *
750
 * or
751
 *
752
 *  div' = -div + d - 1 + d * e
753
 *
754
 * with e some affine expression.
755
 * If so, we write (1) as
756
 *
757
 *  f * div + f * (div' mod d)
758
 *
759
 * or
760
 *
761
 *  -f * (-div + d - 1) - f * (div' mod d)
762
 *
763
 * exploiting (in the second case) the fact that
764
 *
765
 *  f * d * floor(div/d) =  -f * d * floor((-div + d - 1)/d)
766
 *
767
 *
768
 * We first try to find an appropriate expression for div'
769
 * from the constraints of data->build->domain (which is therefore
770
 * guaranteed to be non-negative on data->build), where we remove
771
 * any integer divisions from the constraints and skip this step
772
 * if "div" itself involves any integer divisions.
773
 * If we cannot find an appropriate expression this way, then
774
 * we pass control to extract_nonneg_mod where check
775
 * if div or "-div + d -1" themselves happen to be
776
 * non-negative on data->build.
777
 *
778
 * While looking for an appropriate constraint in data->build->domain,
779
 * we ignore the constant term, so after finding such a constraint,
780
 * we still need to fix up the constant term.
781
 * In particular, if a is the constant term of "div"
782
 * (or d - 1 - the constant term of "div" if data->sign < 0)
783
 * and b is the constant term of the constraint, then we need to find
784
 * a non-negative constant c such that
785
 *
786
 *  b + c \equiv a  mod d
787
 *
788
 * We therefore take
789
 *
790
 *  c = (a - b) mod d
791
 *
792
 * and add it to b to obtain the constant term of div'.
793
 * If this constant term is "too negative", then we add an appropriate
794
 * multiple of d to make it positive.
795
 *
796
 *
797
 * Note that the above is a only a very simple heuristic for finding an
798
 * appropriate expression.  We could try a bit harder by also considering
799
 * sums of constraints that involve disjoint sets of variables or
800
 * we could consider arbitrary linear combinations of constraints,
801
 * although that could potentially be much more expensive as it involves
802
 * the solution of an LP problem.
803
 *
804
 * In particular, if v_i is a column vector representing constraint i,
805
 * w represents div and e_i is the i-th unit vector, then we are looking
806
 * for a solution of the constraints
807
 *
808
 *  \sum_i lambda_i v_i = w + \sum_i alpha_i d e_i
809
 *
810
 * with \lambda_i >= 0 and alpha_i of unrestricted sign.
811
 * If we are not just interested in a non-negative expression, but
812
 * also in one with a minimal range, then we don't just want
813
 * c = \sum_i lambda_i v_i to be non-negative over the domain,
814
 * but also beta - c = \sum_i mu_i v_i, where beta is a scalar
815
 * that we want to minimize and we now also have to take into account
816
 * the constant terms of the constraints.
817
 * Alternatively, we could first compute the dual of the domain
818
 * and plug in the constraints on the coefficients.
819
 */
820
static int try_extract_mod(struct isl_extract_mod_data *data)
821
69
{
822
69
  isl_basic_set *hull;
823
69
  isl_val *v1, *v2;
824
69
  int r, n;
825
69
826
69
  if (!data->build)
827
0
    goto error;
828
69
829
69
  n = isl_aff_dim(data->div, isl_dim_div);
830
69
831
69
  if (isl_aff_involves_dims(data->div, isl_dim_div, 0, n))
832
0
    return extract_nonneg_mod(data);
833
69
834
69
  hull = isl_set_simple_hull(isl_set_copy(data->build->domain));
835
69
  hull = isl_basic_set_remove_divs(hull);
836
69
  data->sign = 0;
837
69
  data->nonneg = NULL;
838
69
  r = isl_basic_set_foreach_constraint(hull, &check_parallel_or_opposite,
839
69
          data);
840
69
  isl_basic_set_free(hull);
841
69
842
69
  if (!data->sign || 
r < 029
) {
843
40
    isl_aff_free(data->nonneg);
844
40
    if (r < 0)
845
0
      goto error;
846
40
    return extract_nonneg_mod(data);
847
40
  }
848
29
849
29
  v1 = isl_aff_get_constant_val(data->div);
850
29
  v2 = isl_aff_get_constant_val(data->nonneg);
851
29
  if (data->sign < 0) {
852
25
    v1 = isl_val_neg(v1);
853
25
    v1 = isl_val_add(v1, isl_val_copy(data->d));
854
25
    v1 = isl_val_sub_ui(v1, 1);
855
25
  }
856
29
  v1 = isl_val_sub(v1, isl_val_copy(v2));
857
29
  v1 = isl_val_mod(v1, isl_val_copy(data->d));
858
29
  v1 = isl_val_add(v1, v2);
859
29
  v2 = isl_val_div(isl_val_copy(v1), isl_val_copy(data->d));
860
29
  v2 = isl_val_ceil(v2);
861
29
  if (isl_val_is_neg(v2)) {
862
1
    v2 = isl_val_mul(v2, isl_val_copy(data->d));
863
1
    v1 = isl_val_sub(v1, isl_val_copy(v2));
864
1
  }
865
29
  data->nonneg = isl_aff_set_constant_val(data->nonneg, v1);
866
29
  isl_val_free(v2);
867
29
868
29
  if (data->sign < 0) {
869
25
    data->div = oppose_div_arg(data->div, isl_val_copy(data->d));
870
25
    data->v = isl_val_neg(data->v);
871
25
  }
872
29
873
29
  return extract_term_and_mod(data,
874
29
            isl_aff_copy(data->div), data->nonneg);
875
0
error:
876
0
  data->aff = isl_aff_free(data->aff);
877
0
  return -1;
878
69
}
879
880
/* Check if "data->aff" involves any (implicit) modulo computations based
881
 * on div "data->i".
882
 * If so, remove them from aff and add expressions corresponding
883
 * to those modulo computations to data->pos and/or data->neg.
884
 *
885
 * "aff" is assumed to be an integer affine expression.
886
 *
887
 * In particular, check if (v * div_j) is of the form
888
 *
889
 *  f * m * floor(a / m)
890
 *
891
 * and, if so, rewrite it as
892
 *
893
 *  f * (a - (a mod m)) = f * a - f * (a mod m)
894
 *
895
 * and extract out -f * (a mod m).
896
 * In particular, if f > 0, we add (f * (a mod m)) to *neg.
897
 * If f < 0, we add ((-f) * (a mod m)) to *pos.
898
 *
899
 * Note that in order to represent "a mod m" as
900
 *
901
 *  (isl_ast_op_pdiv_r, a, m)
902
 *
903
 * we need to make sure that a is non-negative.
904
 * If not, we check if "-a + m - 1" is non-negative.
905
 * If so, we can rewrite
906
 *
907
 *  floor(a/m) = -ceil(-a/m) = -floor((-a + m - 1)/m)
908
 *
909
 * and still extract a modulo.
910
 */
911
static int extract_modulo(struct isl_extract_mod_data *data)
912
69
{
913
69
  data->div = isl_aff_get_div(data->aff, data->i);
914
69
  data->d = isl_aff_get_denominator_val(data->div);
915
69
  if (isl_val_is_divisible_by(data->v, data->d)) {
916
69
    data->div = isl_aff_scale_val(data->div, isl_val_copy(data->d));
917
69
    if (try_extract_mod(data) < 0)
918
0
      data->aff = isl_aff_free(data->aff);
919
69
  }
920
69
  isl_aff_free(data->div);
921
69
  isl_val_free(data->d);
922
69
  return 0;
923
69
}
924
925
/* Check if "aff" involves any (implicit) modulo computations.
926
 * If so, remove them from aff and add expressions corresponding
927
 * to those modulo computations to *pos and/or *neg.
928
 * We only do this if the option ast_build_prefer_pdiv is set.
929
 *
930
 * "aff" is assumed to be an integer affine expression.
931
 *
932
 * A modulo expression is of the form
933
 *
934
 *  a mod m = a - m * floor(a / m)
935
 *
936
 * To detect them in aff, we look for terms of the form
937
 *
938
 *  f * m * floor(a / m)
939
 *
940
 * rewrite them as
941
 *
942
 *  f * (a - (a mod m)) = f * a - f * (a mod m)
943
 *
944
 * and extract out -f * (a mod m).
945
 * In particular, if f > 0, we add (f * (a mod m)) to *neg.
946
 * If f < 0, we add ((-f) * (a mod m)) to *pos.
947
 */
948
static __isl_give isl_aff *extract_modulos(__isl_take isl_aff *aff,
949
  __isl_keep isl_ast_expr **pos, __isl_keep isl_ast_expr **neg,
950
  __isl_keep isl_ast_build *build)
951
4.44k
{
952
4.44k
  struct isl_extract_mod_data data = { build, aff, *pos, *neg };
953
4.44k
  isl_ctx *ctx;
954
4.44k
  int n;
955
4.44k
956
4.44k
  if (!aff)
957
0
    return NULL;
958
4.44k
959
4.44k
  ctx = isl_aff_get_ctx(aff);
960
4.44k
  if (!isl_options_get_ast_build_prefer_pdiv(ctx))
961
0
    return aff;
962
4.44k
963
4.44k
  n = isl_aff_dim(data.aff, isl_dim_div);
964
4.64k
  for (data.i = 0; data.i < n; 
++data.i202
) {
965
202
    data.v = isl_aff_get_coefficient_val(data.aff,
966
202
              isl_dim_div, data.i);
967
202
    if (!data.v)
968
0
      return isl_aff_free(aff);
969
202
    if (isl_val_is_zero(data.v) ||
970
202
        
isl_val_is_one(data.v)82
||
isl_val_is_negone(data.v)69
) {
971
133
      isl_val_free(data.v);
972
133
      continue;
973
133
    }
974
69
    if (extract_modulo(&data) < 0)
975
0
      data.aff = isl_aff_free(data.aff);
976
69
    isl_val_free(data.v);
977
69
    if (!data.aff)
978
0
      break;
979
202
  }
980
4.44k
981
4.44k
  if (data.add)
982
68
    data.aff = isl_aff_add(data.aff, data.add);
983
4.44k
984
4.44k
  *pos = data.pos;
985
4.44k
  *neg = data.neg;
986
4.44k
  return data.aff;
987
4.44k
}
988
989
/* Check if aff involves any non-integer coefficients.
990
 * If so, split aff into
991
 *
992
 *  aff = aff1 + (aff2 / d)
993
 *
994
 * with both aff1 and aff2 having only integer coefficients.
995
 * Return aff1 and add (aff2 / d) to *expr.
996
 */
997
static __isl_give isl_aff *extract_rational(__isl_take isl_aff *aff,
998
  __isl_keep isl_ast_expr **expr, __isl_keep isl_ast_build *build)
999
4.25k
{
1000
4.25k
  int i, j, n;
1001
4.25k
  isl_aff *rat = NULL;
1002
4.25k
  isl_local_space *ls = NULL;
1003
4.25k
  isl_ast_expr *rat_expr;
1004
4.25k
  isl_val *v, *d;
1005
4.25k
  enum isl_dim_type t[] = { isl_dim_param, isl_dim_in, isl_dim_div };
1006
4.25k
  enum isl_dim_type l[] = { isl_dim_param, isl_dim_set, isl_dim_div };
1007
4.25k
1008
4.25k
  if (!aff)
1009
0
    return NULL;
1010
4.25k
  d = isl_aff_get_denominator_val(aff);
1011
4.25k
  if (!d)
1012
0
    goto error;
1013
4.25k
  if (isl_val_is_one(d)) {
1014
4.25k
    isl_val_free(d);
1015
4.25k
    return aff;
1016
4.25k
  }
1017
3
1018
3
  aff = isl_aff_scale_val(aff, isl_val_copy(d));
1019
3
1020
3
  ls = isl_aff_get_domain_local_space(aff);
1021
3
  rat = isl_aff_zero_on_domain(isl_local_space_copy(ls));
1022
3
1023
12
  for (i = 0; i < 3; 
++i9
) {
1024
9
    n = isl_aff_dim(aff, t[i]);
1025
20
    for (j = 0; j < n; 
++j11
) {
1026
11
      isl_aff *rat_j;
1027
11
1028
11
      v = isl_aff_get_coefficient_val(aff, t[i], j);
1029
11
      if (!v)
1030
0
        goto error;
1031
11
      if (isl_val_is_divisible_by(v, d)) {
1032
8
        isl_val_free(v);
1033
8
        continue;
1034
8
      }
1035
3
      rat_j = isl_aff_var_on_domain(isl_local_space_copy(ls),
1036
3
              l[i], j);
1037
3
      rat_j = isl_aff_scale_val(rat_j, v);
1038
3
      rat = isl_aff_add(rat, rat_j);
1039
3
    }
1040
9
  }
1041
3
1042
3
  v = isl_aff_get_constant_val(aff);
1043
3
  if (isl_val_is_divisible_by(v, d)) {
1044
3
    isl_val_free(v);
1045
3
  } else {
1046
0
    isl_aff *rat_0;
1047
0
1048
0
    rat_0 = isl_aff_val_on_domain(isl_local_space_copy(ls), v);
1049
0
    rat = isl_aff_add(rat, rat_0);
1050
0
  }
1051
3
1052
3
  isl_local_space_free(ls);
1053
3
1054
3
  aff = isl_aff_sub(aff, isl_aff_copy(rat));
1055
3
  aff = isl_aff_scale_down_val(aff, isl_val_copy(d));
1056
3
1057
3
  rat_expr = isl_ast_expr_from_aff(rat, build);
1058
3
  rat_expr = isl_ast_expr_div(rat_expr, isl_ast_expr_from_val(d));
1059
3
  *expr = ast_expr_add(*expr, rat_expr);
1060
3
1061
3
  return aff;
1062
0
error:
1063
0
  isl_aff_free(rat);
1064
0
  isl_local_space_free(ls);
1065
0
  isl_aff_free(aff);
1066
0
  isl_val_free(d);
1067
0
  return NULL;
1068
4.25k
}
1069
1070
/* Construct an isl_ast_expr that evaluates the affine expression "aff",
1071
 * The result is simplified in terms of build->domain.
1072
 *
1073
 * We first extract hidden modulo computations from the affine expression
1074
 * and then add terms for each variable with a non-zero coefficient.
1075
 * Finally, if the affine expression has a non-trivial denominator,
1076
 * we divide the resulting isl_ast_expr by this denominator.
1077
 */
1078
__isl_give isl_ast_expr *isl_ast_expr_from_aff(__isl_take isl_aff *aff,
1079
  __isl_keep isl_ast_build *build)
1080
4.25k
{
1081
4.25k
  int i, j;
1082
4.25k
  int n;
1083
4.25k
  isl_val *v;
1084
4.25k
  isl_ctx *ctx = isl_aff_get_ctx(aff);
1085
4.25k
  isl_ast_expr *expr, *expr_neg;
1086
4.25k
  enum isl_dim_type t[] = { isl_dim_param, isl_dim_in, isl_dim_div };
1087
4.25k
  enum isl_dim_type l[] = { isl_dim_param, isl_dim_set, isl_dim_div };
1088
4.25k
  isl_local_space *ls;
1089
4.25k
  struct isl_ast_add_term_data data;
1090
4.25k
1091
4.25k
  if (!aff)
1092
0
    return NULL;
1093
4.25k
1094
4.25k
  expr = isl_ast_expr_alloc_int_si(ctx, 0);
1095
4.25k
  expr_neg = isl_ast_expr_alloc_int_si(ctx, 0);
1096
4.25k
1097
4.25k
  aff = extract_rational(aff, &expr, build);
1098
4.25k
1099
4.25k
  aff = extract_modulos(aff, &expr, &expr_neg, build);
1100
4.25k
  expr = ast_expr_sub(expr, expr_neg);
1101
4.25k
1102
4.25k
  ls = isl_aff_get_domain_local_space(aff);
1103
4.25k
1104
4.25k
  data.build = build;
1105
4.25k
  data.cst = isl_aff_get_constant_val(aff);
1106
17.0k
  for (i = 0; i < 3; 
++i12.7k
) {
1107
12.7k
    n = isl_aff_dim(aff, t[i]);
1108
44.4k
    for (j = 0; j < n; 
++j31.7k
) {
1109
31.7k
      v = isl_aff_get_coefficient_val(aff, t[i], j);
1110
31.7k
      if (!v)
1111
0
        expr = isl_ast_expr_free(expr);
1112
31.7k
      if (isl_val_is_zero(v)) {
1113
26.9k
        isl_val_free(v);
1114
26.9k
        continue;
1115
26.9k
      }
1116
4.74k
      expr = isl_ast_expr_add_term(expr,
1117
4.74k
              ls, l[i], j, v, &data);
1118
4.74k
    }
1119
12.7k
  }
1120
4.25k
1121
4.25k
  expr = isl_ast_expr_add_int(expr, data.cst);
1122
4.25k
1123
4.25k
  isl_local_space_free(ls);
1124
4.25k
  isl_aff_free(aff);
1125
4.25k
  return expr;
1126
4.25k
}
1127
1128
/* Add terms to "expr" for each variable in "aff" with a coefficient
1129
 * with sign equal to "sign".
1130
 * The result is simplified in terms of data->build->domain.
1131
 */
1132
static __isl_give isl_ast_expr *add_signed_terms(__isl_take isl_ast_expr *expr,
1133
  __isl_keep isl_aff *aff, int sign, struct isl_ast_add_term_data *data)
1134
374
{
1135
374
  int i, j;
1136
374
  isl_val *v;
1137
374
  enum isl_dim_type t[] = { isl_dim_param, isl_dim_in, isl_dim_div };
1138
374
  enum isl_dim_type l[] = { isl_dim_param, isl_dim_set, isl_dim_div };
1139
374
  isl_local_space *ls;
1140
374
1141
374
  ls = isl_aff_get_domain_local_space(aff);
1142
374
1143
1.49k
  for (i = 0; i < 3; 
++i1.12k
) {
1144
1.12k
    int n = isl_aff_dim(aff, t[i]);
1145
2.85k
    for (j = 0; j < n; 
++j1.73k
) {
1146
1.73k
      v = isl_aff_get_coefficient_val(aff, t[i], j);
1147
1.73k
      if (sign * isl_val_sgn(v) <= 0) {
1148
1.50k
        isl_val_free(v);
1149
1.50k
        continue;
1150
1.50k
      }
1151
235
      v = isl_val_abs(v);
1152
235
      expr = isl_ast_expr_add_term(expr,
1153
235
            ls, l[i], j, v, data);
1154
235
    }
1155
1.12k
  }
1156
374
1157
374
  isl_local_space_free(ls);
1158
374
1159
374
  return expr;
1160
374
}
1161
1162
/* Should the constant term "v" be considered positive?
1163
 *
1164
 * A positive constant will be added to "pos" by the caller,
1165
 * while a negative constant will be added to "neg".
1166
 * If either "pos" or "neg" is exactly zero, then we prefer
1167
 * to add the constant "v" to that side, irrespective of the sign of "v".
1168
 * This results in slightly shorter expressions and may reduce the risk
1169
 * of overflows.
1170
 */
1171
static int constant_is_considered_positive(__isl_keep isl_val *v,
1172
  __isl_keep isl_ast_expr *pos, __isl_keep isl_ast_expr *neg)
1173
187
{
1174
187
  if (ast_expr_is_zero(pos))
1175
50
    return 1;
1176
137
  if (ast_expr_is_zero(neg))
1177
95
    return 0;
1178
42
  return isl_val_is_pos(v);
1179
42
}
1180
1181
/* Check if the equality
1182
 *
1183
 *  aff = 0
1184
 *
1185
 * represents a stride constraint on the integer division "pos".
1186
 *
1187
 * In particular, if the integer division "pos" is equal to
1188
 *
1189
 *  floor(e/d)
1190
 *
1191
 * then check if aff is equal to
1192
 *
1193
 *  e - d floor(e/d)
1194
 *
1195
 * or its opposite.
1196
 *
1197
 * If so, the equality is exactly
1198
 *
1199
 *  e mod d = 0
1200
 *
1201
 * Note that in principle we could also accept
1202
 *
1203
 *  e - d floor(e'/d)
1204
 *
1205
 * where e and e' differ by a constant.
1206
 */
1207
static int is_stride_constraint(__isl_keep isl_aff *aff, int pos)
1208
8
{
1209
8
  isl_aff *div;
1210
8
  isl_val *c, *d;
1211
8
  int eq;
1212
8
1213
8
  div = isl_aff_get_div(aff, pos);
1214
8
  c = isl_aff_get_coefficient_val(aff, isl_dim_div, pos);
1215
8
  d = isl_aff_get_denominator_val(div);
1216
8
  eq = isl_val_abs_eq(c, d);
1217
8
  if (eq >= 0 && eq) {
1218
8
    aff = isl_aff_copy(aff);
1219
8
    aff = isl_aff_set_coefficient_si(aff, isl_dim_div, pos, 0);
1220
8
    div = isl_aff_scale_val(div, d);
1221
8
    if (isl_val_is_pos(c))
1222
8
      div = isl_aff_neg(div);
1223
8
    eq = isl_aff_plain_is_equal(div, aff);
1224
8
    isl_aff_free(aff);
1225
8
  } else
1226
0
    isl_val_free(d);
1227
8
  isl_val_free(c);
1228
8
  isl_aff_free(div);
1229
8
1230
8
  return eq;
1231
8
}
1232
1233
/* Are all coefficients of "aff" (zero or) negative?
1234
 */
1235
static int all_negative_coefficients(__isl_keep isl_aff *aff)
1236
8
{
1237
8
  int i, n;
1238
8
1239
8
  if (!aff)
1240
0
    return 0;
1241
8
1242
8
  n = isl_aff_dim(aff, isl_dim_param);
1243
26
  for (i = 0; i < n; 
++i18
)
1244
18
    if (isl_aff_coefficient_sgn(aff, isl_dim_param, i) > 0)
1245
0
      return 0;
1246
8
1247
8
  n = isl_aff_dim(aff, isl_dim_in);
1248
52
  for (i = 0; i < n; 
++i44
)
1249
44
    if (isl_aff_coefficient_sgn(aff, isl_dim_in, i) > 0)
1250
0
      return 0;
1251
8
1252
8
  return 1;
1253
8
}
1254
1255
/* Give an equality of the form
1256
 *
1257
 *  aff = e - d floor(e/d) = 0
1258
 *
1259
 * or
1260
 *
1261
 *  aff = -e + d floor(e/d) = 0
1262
 *
1263
 * with the integer division "pos" equal to floor(e/d),
1264
 * construct the AST expression
1265
 *
1266
 *  (isl_ast_op_eq, (isl_ast_op_zdiv_r, expr(e), expr(d)), expr(0))
1267
 *
1268
 * If e only has negative coefficients, then construct
1269
 *
1270
 *  (isl_ast_op_eq, (isl_ast_op_zdiv_r, expr(-e), expr(d)), expr(0))
1271
 *
1272
 * instead.
1273
 */
1274
static __isl_give isl_ast_expr *extract_stride_constraint(
1275
  __isl_take isl_aff *aff, int pos, __isl_keep isl_ast_build *build)
1276
8
{
1277
8
  isl_ctx *ctx;
1278
8
  isl_val *c;
1279
8
  isl_ast_expr *expr, *cst;
1280
8
1281
8
  if (!aff)
1282
0
    return NULL;
1283
8
1284
8
  ctx = isl_aff_get_ctx(aff);
1285
8
1286
8
  c = isl_aff_get_coefficient_val(aff, isl_dim_div, pos);
1287
8
  aff = isl_aff_set_coefficient_si(aff, isl_dim_div, pos, 0);
1288
8
1289
8
  if (all_negative_coefficients(aff))
1290
8
    aff = isl_aff_neg(aff);
1291
8
1292
8
  cst = isl_ast_expr_from_val(isl_val_abs(c));
1293
8
  expr = isl_ast_expr_from_aff(aff, build);
1294
8
1295
8
  expr = isl_ast_expr_alloc_binary(isl_ast_op_zdiv_r, expr, cst);
1296
8
  cst = isl_ast_expr_alloc_int_si(ctx, 0);
1297
8
  expr = isl_ast_expr_alloc_binary(isl_ast_op_eq, expr, cst);
1298
8
1299
8
  return expr;
1300
8
}
1301
1302
/* Construct an isl_ast_expr that evaluates the condition "constraint",
1303
 * The result is simplified in terms of build->domain.
1304
 *
1305
 * We first check if the constraint is an equality of the form
1306
 *
1307
 *  e - d floor(e/d) = 0
1308
 *
1309
 * i.e.,
1310
 *
1311
 *  e mod d = 0
1312
 *
1313
 * If so, we convert it to
1314
 *
1315
 *  (isl_ast_op_eq, (isl_ast_op_zdiv_r, expr(e), expr(d)), expr(0))
1316
 *
1317
 * Otherwise, let the constraint by either "a >= 0" or "a == 0".
1318
 * We first extract hidden modulo computations from "a"
1319
 * and then collect all the terms with a positive coefficient in cons_pos
1320
 * and the terms with a negative coefficient in cons_neg.
1321
 *
1322
 * The result is then of the form
1323
 *
1324
 *  (isl_ast_op_ge, expr(pos), expr(-neg)))
1325
 *
1326
 * or
1327
 *
1328
 *  (isl_ast_op_eq, expr(pos), expr(-neg)))
1329
 *
1330
 * However, if the first expression is an integer constant (and the second
1331
 * is not), then we swap the two expressions.  This ensures that we construct,
1332
 * e.g., "i <= 5" rather than "5 >= i".
1333
 *
1334
 * Furthermore, is there are no terms with positive coefficients (or no terms
1335
 * with negative coefficients), then the constant term is added to "pos"
1336
 * (or "neg"), ignoring the sign of the constant term.
1337
 */
1338
static __isl_give isl_ast_expr *isl_ast_expr_from_constraint(
1339
  __isl_take isl_constraint *constraint, __isl_keep isl_ast_build *build)
1340
195
{
1341
195
  int i, n;
1342
195
  isl_ctx *ctx;
1343
195
  isl_ast_expr *expr_pos;
1344
195
  isl_ast_expr *expr_neg;
1345
195
  isl_ast_expr *expr;
1346
195
  isl_aff *aff;
1347
195
  int eq;
1348
195
  enum isl_ast_op_type type;
1349
195
  struct isl_ast_add_term_data data;
1350
195
1351
195
  if (!constraint)
1352
0
    return NULL;
1353
195
1354
195
  aff = isl_constraint_get_aff(constraint);
1355
195
  eq = isl_constraint_is_equality(constraint);
1356
195
  isl_constraint_free(constraint);
1357
195
1358
195
  n = isl_aff_dim(aff, isl_dim_div);
1359
195
  if (eq && 
n > 023
)
1360
8
    for (i = 0; i < n; 
++i0
) {
1361
8
      int is_stride;
1362
8
      is_stride = is_stride_constraint(aff, i);
1363
8
      if (is_stride < 0)
1364
0
        goto error;
1365
8
      if (is_stride)
1366
8
        return extract_stride_constraint(aff, i, build);
1367
8
    }
1368
195
1369
195
  ctx = isl_aff_get_ctx(aff);
1370
187
  expr_pos = isl_ast_expr_alloc_int_si(ctx, 0);
1371
187
  expr_neg = isl_ast_expr_alloc_int_si(ctx, 0);
1372
187
1373
187
  aff = extract_modulos(aff, &expr_pos, &expr_neg, build);
1374
187
1375
187
  data.build = build;
1376
187
  data.cst = isl_aff_get_constant_val(aff);
1377
187
  expr_pos = add_signed_terms(expr_pos, aff, 1, &data);
1378
187
  data.cst = isl_val_neg(data.cst);
1379
187
  expr_neg = add_signed_terms(expr_neg, aff, -1, &data);
1380
187
  data.cst = isl_val_neg(data.cst);
1381
187
1382
187
  if (constant_is_considered_positive(data.cst, expr_pos, expr_neg)) {
1383
56
    expr_pos = isl_ast_expr_add_int(expr_pos, data.cst);
1384
131
  } else {
1385
131
    data.cst = isl_val_neg(data.cst);
1386
131
    expr_neg = isl_ast_expr_add_int(expr_neg, data.cst);
1387
131
  }
1388
187
1389
187
  if (isl_ast_expr_get_type(expr_pos) == isl_ast_expr_int &&
1390
187
      
isl_ast_expr_get_type(expr_neg) != isl_ast_expr_int50
) {
1391
50
    type = eq ? 
isl_ast_op_eq0
: isl_ast_op_le;
1392
50
    expr = isl_ast_expr_alloc_binary(type, expr_neg, expr_pos);
1393
137
  } else {
1394
137
    type = eq ? 
isl_ast_op_eq15
:
isl_ast_op_ge122
;
1395
137
    expr = isl_ast_expr_alloc_binary(type, expr_pos, expr_neg);
1396
137
  }
1397
187
1398
187
  isl_aff_free(aff);
1399
187
  return expr;
1400
0
error:
1401
0
  isl_aff_free(aff);
1402
0
  return NULL;
1403
195
}
1404
1405
/* Wrapper around isl_constraint_cmp_last_non_zero for use
1406
 * as a callback to isl_constraint_list_sort.
1407
 * If isl_constraint_cmp_last_non_zero cannot tell the constraints
1408
 * apart, then use isl_constraint_plain_cmp instead.
1409
 */
1410
static int cmp_constraint(__isl_keep isl_constraint *a,
1411
  __isl_keep isl_constraint *b, void *user)
1412
11
{
1413
11
  int cmp;
1414
11
1415
11
  cmp = isl_constraint_cmp_last_non_zero(a, b);
1416
11
  if (cmp != 0)
1417
10
    return cmp;
1418
1
  return isl_constraint_plain_cmp(a, b);
1419
1
}
1420
1421
/* Construct an isl_ast_expr that evaluates the conditions defining "bset".
1422
 * The result is simplified in terms of build->domain.
1423
 *
1424
 * If "bset" is not bounded by any constraint, then we contruct
1425
 * the expression "1", i.e., "true".
1426
 *
1427
 * Otherwise, we sort the constraints, putting constraints that involve
1428
 * integer divisions after those that do not, and construct an "and"
1429
 * of the ast expressions of the individual constraints.
1430
 *
1431
 * Each constraint is added to the generated constraints of the build
1432
 * after it has been converted to an AST expression so that it can be used
1433
 * to simplify the following constraints.  This may change the truth value
1434
 * of subsequent constraints that do not satisfy the earlier constraints,
1435
 * but this does not affect the outcome of the conjunction as it is
1436
 * only true if all the conjuncts are true (no matter in what order
1437
 * they are evaluated).  In particular, the constraints that do not
1438
 * involve integer divisions may serve to simplify some constraints
1439
 * that do involve integer divisions.
1440
 */
1441
__isl_give isl_ast_expr *isl_ast_build_expr_from_basic_set(
1442
   __isl_keep isl_ast_build *build, __isl_take isl_basic_set *bset)
1443
312
{
1444
312
  int i, n;
1445
312
  isl_constraint *c;
1446
312
  isl_constraint_list *list;
1447
312
  isl_ast_expr *res;
1448
312
  isl_set *set;
1449
312
1450
312
  list = isl_basic_set_get_constraint_list(bset);
1451
312
  isl_basic_set_free(bset);
1452
312
  list = isl_constraint_list_sort(list, &cmp_constraint, NULL);
1453
312
  if (!list)
1454
0
    return NULL;
1455
312
  n = isl_constraint_list_n_constraint(list);
1456
312
  if (n == 0) {
1457
128
    isl_ctx *ctx = isl_constraint_list_get_ctx(list);
1458
128
    isl_constraint_list_free(list);
1459
128
    return isl_ast_expr_alloc_int_si(ctx, 1);
1460
128
  }
1461
184
1462
184
  build = isl_ast_build_copy(build);
1463
184
1464
184
  c = isl_constraint_list_get_constraint(list, 0);
1465
184
  bset = isl_basic_set_from_constraint(isl_constraint_copy(c));
1466
184
  set = isl_set_from_basic_set(bset);
1467
184
  res = isl_ast_expr_from_constraint(c, build);
1468
184
  build = isl_ast_build_restrict_generated(build, set);
1469
184
1470
195
  for (i = 1; i < n; 
++i11
) {
1471
11
    isl_ast_expr *expr;
1472
11
1473
11
    c = isl_constraint_list_get_constraint(list, i);
1474
11
    bset = isl_basic_set_from_constraint(isl_constraint_copy(c));
1475
11
    set = isl_set_from_basic_set(bset);
1476
11
    expr = isl_ast_expr_from_constraint(c, build);
1477
11
    build = isl_ast_build_restrict_generated(build, set);
1478
11
    res = isl_ast_expr_and(res, expr);
1479
11
  }
1480
312
1481
312
  isl_constraint_list_free(list);
1482
312
  isl_ast_build_free(build);
1483
312
  return res;
1484
312
}
1485
1486
/* Construct an isl_ast_expr that evaluates the conditions defining "set".
1487
 * The result is simplified in terms of build->domain.
1488
 *
1489
 * If "set" is an (obviously) empty set, then return the expression "0".
1490
 *
1491
 * If there are multiple disjuncts in the description of the set,
1492
 * then subsequent disjuncts are simplified in a context where
1493
 * the previous disjuncts have been removed from build->domain.
1494
 * In particular, constraints that ensure that there is no overlap
1495
 * with these previous disjuncts, can be removed.
1496
 * This is mostly useful for disjuncts that are only defined by
1497
 * a single constraint (relative to the build domain) as the opposite
1498
 * of that single constraint can then be removed from the other disjuncts.
1499
 * In order not to increase the number of disjuncts in the build domain
1500
 * after subtracting the previous disjuncts of "set", the simple hull
1501
 * is computed after taking the difference with each of these disjuncts.
1502
 * This means that constraints that prevent overlap with a union
1503
 * of multiple previous disjuncts are not removed.
1504
 *
1505
 * "set" lives in the internal schedule space.
1506
 */
1507
__isl_give isl_ast_expr *isl_ast_build_expr_from_set_internal(
1508
  __isl_keep isl_ast_build *build, __isl_take isl_set *set)
1509
293
{
1510
293
  int i, n;
1511
293
  isl_basic_set *bset;
1512
293
  isl_basic_set_list *list;
1513
293
  isl_set *domain;
1514
293
  isl_ast_expr *res;
1515
293
1516
293
  list = isl_set_get_basic_set_list(set);
1517
293
  isl_set_free(set);
1518
293
1519
293
  if (!list)
1520
0
    return NULL;
1521
293
  n = isl_basic_set_list_n_basic_set(list);
1522
293
  if (n == 0) {
1523
2
    isl_ctx *ctx = isl_ast_build_get_ctx(build);
1524
2
    isl_basic_set_list_free(list);
1525
2
    return isl_ast_expr_from_val(isl_val_zero(ctx));
1526
2
  }
1527
291
1528
291
  domain = isl_ast_build_get_domain(build);
1529
291
1530
291
  bset = isl_basic_set_list_get_basic_set(list, 0);
1531
291
  set = isl_set_from_basic_set(isl_basic_set_copy(bset));
1532
291
  res = isl_ast_build_expr_from_basic_set(build, bset);
1533
291
1534
312
  for (i = 1; i < n; 
++i21
) {
1535
21
    isl_ast_expr *expr;
1536
21
    isl_set *rest;
1537
21
1538
21
    rest = isl_set_subtract(isl_set_copy(domain), set);
1539
21
    rest = isl_set_from_basic_set(isl_set_simple_hull(rest));
1540
21
    domain = isl_set_intersect(domain, rest);
1541
21
    bset = isl_basic_set_list_get_basic_set(list, i);
1542
21
    set = isl_set_from_basic_set(isl_basic_set_copy(bset));
1543
21
    bset = isl_basic_set_gist(bset,
1544
21
        isl_set_simple_hull(isl_set_copy(domain)));
1545
21
    expr = isl_ast_build_expr_from_basic_set(build, bset);
1546
21
    res = isl_ast_expr_or(res, expr);
1547
21
  }
1548
293
1549
293
  isl_set_free(domain);
1550
293
  isl_set_free(set);
1551
293
  isl_basic_set_list_free(list);
1552
293
  return res;
1553
293
}
1554
1555
/* Construct an isl_ast_expr that evaluates the conditions defining "set".
1556
 * The result is simplified in terms of build->domain.
1557
 *
1558
 * If "set" is an (obviously) empty set, then return the expression "0".
1559
 *
1560
 * "set" lives in the external schedule space.
1561
 *
1562
 * The internal AST expression generation assumes that there are
1563
 * no unknown divs, so make sure an explicit representation is available.
1564
 * Since the set comes from the outside, it may have constraints that
1565
 * are redundant with respect to the build domain.  Remove them first.
1566
 */
1567
__isl_give isl_ast_expr *isl_ast_build_expr_from_set(
1568
  __isl_keep isl_ast_build *build, __isl_take isl_set *set)
1569
181
{
1570
181
  if (isl_ast_build_need_schedule_map(build)) {
1571
4
    isl_multi_aff *ma;
1572
4
    ma = isl_ast_build_get_schedule_map_multi_aff(build);
1573
4
    set = isl_set_preimage_multi_aff(set, ma);
1574
4
  }
1575
181
1576
181
  set = isl_set_compute_divs(set);
1577
181
  set = isl_ast_build_compute_gist(build, set);
1578
181
  return isl_ast_build_expr_from_set_internal(build, set);
1579
181
}
1580
1581
/* State of data about previous pieces in
1582
 * isl_ast_build_expr_from_pw_aff_internal.
1583
 *
1584
 * isl_state_none: no data about previous pieces
1585
 * isl_state_single: data about a single previous piece
1586
 * isl_state_min: data represents minimum of several pieces
1587
 * isl_state_max: data represents maximum of several pieces
1588
 */
1589
enum isl_from_pw_aff_state {
1590
  isl_state_none,
1591
  isl_state_single,
1592
  isl_state_min,
1593
  isl_state_max
1594
};
1595
1596
/* Internal date structure representing a single piece in the input of
1597
 * isl_ast_build_expr_from_pw_aff_internal.
1598
 *
1599
 * If "state" is isl_state_none, then "set_list" and "aff_list" are not used.
1600
 * If "state" is isl_state_single, then "set_list" and "aff_list" contain the
1601
 * single previous subpiece.
1602
 * If "state" is isl_state_min, then "set_list" and "aff_list" contain
1603
 * a sequence of several previous subpieces that are equal to the minimum
1604
 * of the entries in "aff_list" over the union of "set_list"
1605
 * If "state" is isl_state_max, then "set_list" and "aff_list" contain
1606
 * a sequence of several previous subpieces that are equal to the maximum
1607
 * of the entries in "aff_list" over the union of "set_list"
1608
 *
1609
 * During the construction of the pieces, "set" is NULL.
1610
 * After the construction, "set" is set to the union of the elements
1611
 * in "set_list", at which point "set_list" is set to NULL.
1612
 */
1613
struct isl_from_pw_aff_piece {
1614
  enum isl_from_pw_aff_state state;
1615
  isl_set *set;
1616
  isl_set_list *set_list;
1617
  isl_aff_list *aff_list;
1618
};
1619
1620
/* Internal data structure for isl_ast_build_expr_from_pw_aff_internal.
1621
 *
1622
 * "build" specifies the domain against which the result is simplified.
1623
 * "dom" is the domain of the entire isl_pw_aff.
1624
 *
1625
 * "n" is the number of pieces constructed already.
1626
 * In particular, during the construction of the pieces, "n" points to
1627
 * the piece that is being constructed.  After the construction of the
1628
 * pieces, "n" is set to the total number of pieces.
1629
 * "max" is the total number of allocated entries.
1630
 * "p" contains the individual pieces.
1631
 */
1632
struct isl_from_pw_aff_data {
1633
  isl_ast_build *build;
1634
  isl_set *dom;
1635
1636
  int n;
1637
  int max;
1638
  struct isl_from_pw_aff_piece *p;
1639
};
1640
1641
/* Initialize "data" based on "build" and "pa".
1642
 */
1643
static isl_stat isl_from_pw_aff_data_init(struct isl_from_pw_aff_data *data,
1644
  __isl_keep isl_ast_build *build, __isl_keep isl_pw_aff *pa)
1645
4.15k
{
1646
4.15k
  int n;
1647
4.15k
  isl_ctx *ctx;
1648
4.15k
1649
4.15k
  ctx = isl_pw_aff_get_ctx(pa);
1650
4.15k
  n = isl_pw_aff_n_piece(pa);
1651
4.15k
  if (n == 0)
1652
4.15k
    
isl_die0
(ctx, isl_error_invalid,
1653
4.15k
      "cannot handle void expression", return isl_stat_error);
1654
4.15k
  data->max = n;
1655
4.15k
  data->p = isl_calloc_array(ctx, struct isl_from_pw_aff_piece, n);
1656
4.15k
  if (!data->p)
1657
0
    return isl_stat_error;
1658
4.15k
  data->build = build;
1659
4.15k
  data->dom = isl_pw_aff_domain(isl_pw_aff_copy(pa));
1660
4.15k
  data->n = 0;
1661
4.15k
1662
4.15k
  return isl_stat_ok;
1663
4.15k
}
1664
1665
/* Free all memory allocated for "data".
1666
 */
1667
static void isl_from_pw_aff_data_clear(struct isl_from_pw_aff_data *data)
1668
4.15k
{
1669
4.15k
  int i;
1670
4.15k
1671
4.15k
  isl_set_free(data->dom);
1672
4.15k
  if (!data->p)
1673
0
    return;
1674
4.15k
1675
8.31k
  
for (i = 0; 4.15k
i < data->max;
++i4.16k
) {
1676
4.16k
    isl_set_free(data->p[i].set);
1677
4.16k
    isl_set_list_free(data->p[i].set_list);
1678
4.16k
    isl_aff_list_free(data->p[i].aff_list);
1679
4.16k
  }
1680
4.15k
  free(data->p);
1681
4.15k
}
1682
1683
/* Initialize the current entry of "data" to an unused piece.
1684
 */
1685
static void set_none(struct isl_from_pw_aff_data *data)
1686
4.15k
{
1687
4.15k
  data->p[data->n].state = isl_state_none;
1688
4.15k
  data->p[data->n].set_list = NULL;
1689
4.15k
  data->p[data->n].aff_list = NULL;
1690
4.15k
}
1691
1692
/* Store "set" and "aff" in the current entry of "data" as a single subpiece.
1693
 */
1694
static void set_single(struct isl_from_pw_aff_data *data,
1695
  __isl_take isl_set *set, __isl_take isl_aff *aff)
1696
4.15k
{
1697
4.15k
  data->p[data->n].state = isl_state_single;
1698
4.15k
  data->p[data->n].set_list = isl_set_list_from_set(set);
1699
4.15k
  data->p[data->n].aff_list = isl_aff_list_from_aff(aff);
1700
4.15k
}
1701
1702
/* Extend the current entry of "data" with "set" and "aff"
1703
 * as a minimum expression.
1704
 */
1705
static isl_stat extend_min(struct isl_from_pw_aff_data *data,
1706
  __isl_take isl_set *set, __isl_take isl_aff *aff)
1707
3
{
1708
3
  int n = data->n;
1709
3
  data->p[n].state = isl_state_min;
1710
3
  data->p[n].set_list = isl_set_list_add(data->p[n].set_list, set);
1711
3
  data->p[n].aff_list = isl_aff_list_add(data->p[n].aff_list, aff);
1712
3
1713
3
  if (!data->p[n].set_list || !data->p[n].aff_list)
1714
0
    return isl_stat_error;
1715
3
  return isl_stat_ok;
1716
3
}
1717
1718
/* Extend the current entry of "data" with "set" and "aff"
1719
 * as a maximum expression.
1720
 */
1721
static isl_stat extend_max(struct isl_from_pw_aff_data *data,
1722
  __isl_take isl_set *set, __isl_take isl_aff *aff)
1723
5
{
1724
5
  int n = data->n;
1725
5
  data->p[n].state = isl_state_max;
1726
5
  data->p[n].set_list = isl_set_list_add(data->p[n].set_list, set);
1727
5
  data->p[n].aff_list = isl_aff_list_add(data->p[n].aff_list, aff);
1728
5
1729
5
  if (!data->p[n].set_list || !data->p[n].aff_list)
1730
0
    return isl_stat_error;
1731
5
  return isl_stat_ok;
1732
5
}
1733
1734
/* Extend the domain of the current entry of "data", which is assumed
1735
 * to contain a single subpiece, with "set".  If "replace" is set,
1736
 * then also replace the affine function by "aff".  Otherwise,
1737
 * simply free "aff".
1738
 */
1739
static isl_stat extend_domain(struct isl_from_pw_aff_data *data,
1740
  __isl_take isl_set *set, __isl_take isl_aff *aff, int replace)
1741
0
{
1742
0
  int n = data->n;
1743
0
  isl_set *set_n;
1744
0
1745
0
  set_n = isl_set_list_get_set(data->p[n].set_list, 0);
1746
0
  set_n = isl_set_union(set_n, set);
1747
0
  data->p[n].set_list =
1748
0
    isl_set_list_set_set(data->p[n].set_list, 0, set_n);
1749
0
1750
0
  if (replace)
1751
0
    data->p[n].aff_list =
1752
0
      isl_aff_list_set_aff(data->p[n].aff_list, 0, aff);
1753
0
  else
1754
0
    isl_aff_free(aff);
1755
0
1756
0
  if (!data->p[n].set_list || !data->p[n].aff_list)
1757
0
    return isl_stat_error;
1758
0
  return isl_stat_ok;
1759
0
}
1760
1761
/* Construct an isl_ast_expr from "list" within "build".
1762
 * If "state" is isl_state_single, then "list" contains a single entry and
1763
 * an isl_ast_expr is constructed for that entry.
1764
 * Otherwise a min or max expression is constructed from "list"
1765
 * depending on "state".
1766
 */
1767
static __isl_give isl_ast_expr *ast_expr_from_aff_list(
1768
  __isl_take isl_aff_list *list, enum isl_from_pw_aff_state state,
1769
  __isl_keep isl_ast_build *build)
1770
4.15k
{
1771
4.15k
  int i, n;
1772
4.15k
  isl_aff *aff;
1773
4.15k
  isl_ast_expr *expr;
1774
4.15k
  enum isl_ast_op_type op_type;
1775
4.15k
1776
4.15k
  if (state == isl_state_single) {
1777
4.14k
    aff = isl_aff_list_get_aff(list, 0);
1778
4.14k
    isl_aff_list_free(list);
1779
4.14k
    return isl_ast_expr_from_aff(aff, build);
1780
4.14k
  }
1781
8
  n = isl_aff_list_n_aff(list);
1782
8
  op_type = state == isl_state_min ? 
isl_ast_op_min3
:
isl_ast_op_max5
;
1783
8
  expr = isl_ast_expr_alloc_op(isl_ast_build_get_ctx(build), op_type, n);
1784
8
  if (!expr)
1785
0
    goto error;
1786
8
1787
24
  
for (i = 0; 8
i < n;
++i16
) {
1788
16
    isl_ast_expr *expr_i;
1789
16
1790
16
    aff = isl_aff_list_get_aff(list, i);
1791
16
    expr_i = isl_ast_expr_from_aff(aff, build);
1792
16
    if (!expr_i)
1793
0
      goto error;
1794
16
    expr->u.op.args[i] = expr_i;
1795
16
  }
1796
8
1797
8
  isl_aff_list_free(list);
1798
8
  return expr;
1799
0
error:
1800
0
  isl_aff_list_free(list);
1801
0
  isl_ast_expr_free(expr);
1802
0
  return NULL;
1803
4.15k
}
1804
1805
/* Extend the expression in "next" to take into account
1806
 * the piece at position "pos" in "data", allowing for a further extension
1807
 * for the next piece(s).
1808
 * In particular, "next" is set to a select operation that selects
1809
 * an isl_ast_expr corresponding to data->aff_list on data->set and
1810
 * to an expression that will be filled in by later calls.
1811
 * Return a pointer to this location.
1812
 * Afterwards, the state of "data" is set to isl_state_none.
1813
 *
1814
 * The constraints of data->set are added to the generated
1815
 * constraints of the build such that they can be exploited to simplify
1816
 * the AST expression constructed from data->aff_list.
1817
 */
1818
static isl_ast_expr **add_intermediate_piece(struct isl_from_pw_aff_data *data,
1819
  int pos, isl_ast_expr **next)
1820
1
{
1821
1
  isl_ctx *ctx;
1822
1
  isl_ast_build *build;
1823
1
  isl_ast_expr *ternary, *arg;
1824
1
  isl_set *set, *gist;
1825
1
1826
1
  set = data->p[pos].set;
1827
1
  data->p[pos].set = NULL;
1828
1
  ctx = isl_ast_build_get_ctx(data->build);
1829
1
  ternary = isl_ast_expr_alloc_op(ctx, isl_ast_op_select, 3);
1830
1
  gist = isl_set_gist(isl_set_copy(set), isl_set_copy(data->dom));
1831
1
  arg = isl_ast_build_expr_from_set_internal(data->build, gist);
1832
1
  ternary = isl_ast_expr_set_op_arg(ternary, 0, arg);
1833
1
  build = isl_ast_build_copy(data->build);
1834
1
  build = isl_ast_build_restrict_generated(build, set);
1835
1
  arg = ast_expr_from_aff_list(data->p[pos].aff_list,
1836
1
          data->p[pos].state, build);
1837
1
  data->p[pos].aff_list = NULL;
1838
1
  isl_ast_build_free(build);
1839
1
  ternary = isl_ast_expr_set_op_arg(ternary, 1, arg);
1840
1
  data->p[pos].state = isl_state_none;
1841
1
  if (!ternary)
1842
0
    return NULL;
1843
1
1844
1
  *next = ternary;
1845
1
  return &ternary->u.op.args[2];
1846
1
}
1847
1848
/* Extend the expression in "next" to take into account
1849
 * the final piece, located at position "pos" in "data".
1850
 * In particular, "next" is set to evaluate data->aff_list
1851
 * and the domain is ignored.
1852
 * Return isl_stat_ok on success and isl_stat_error on failure.
1853
 *
1854
 * The constraints of data->set are however added to the generated
1855
 * constraints of the build such that they can be exploited to simplify
1856
 * the AST expression constructed from data->aff_list.
1857
 */
1858
static isl_stat add_last_piece(struct isl_from_pw_aff_data *data,
1859
  int pos, isl_ast_expr **next)
1860
4.15k
{
1861
4.15k
  isl_ast_build *build;
1862
4.15k
1863
4.15k
  if (data->p[pos].state == isl_state_none)
1864
4.15k
    
isl_die0
(isl_ast_build_get_ctx(data->build), isl_error_invalid,
1865
4.15k
      "cannot handle void expression", return isl_stat_error);
1866
4.15k
1867
4.15k
  build = isl_ast_build_copy(data->build);
1868
4.15k
  build = isl_ast_build_restrict_generated(build, data->p[pos].set);
1869
4.15k
  data->p[pos].set = NULL;
1870
4.15k
  *next = ast_expr_from_aff_list(data->p[pos].aff_list,
1871
4.15k
            data->p[pos].state, build);
1872
4.15k
  data->p[pos].aff_list = NULL;
1873
4.15k
  isl_ast_build_free(build);
1874
4.15k
  data->p[pos].state = isl_state_none;
1875
4.15k
  if (!*next)
1876
0
    return isl_stat_error;
1877
4.15k
1878
4.15k
  return isl_stat_ok;
1879
4.15k
}
1880
1881
/* Return -1 if the piece "p1" should be sorted before "p2"
1882
 * and 1 if it should be sorted after "p2".
1883
 * Return 0 if they do not need to be sorted in a specific order.
1884
 *
1885
 * Pieces are sorted according to the number of disjuncts
1886
 * in their domains.
1887
 */
1888
static int sort_pieces_cmp(const void *p1, const void *p2, void *arg)
1889
1
{
1890
1
  const struct isl_from_pw_aff_piece *piece1 = p1;
1891
1
  const struct isl_from_pw_aff_piece *piece2 = p2;
1892
1
  int n1, n2;
1893
1
1894
1
  n1 = isl_set_n_basic_set(piece1->set);
1895
1
  n2 = isl_set_n_basic_set(piece2->set);
1896
1
1897
1
  return n1 - n2;
1898
1
}
1899
1900
/* Construct an isl_ast_expr from the pieces in "data".
1901
 * Return the result or NULL on failure.
1902
 *
1903
 * When this function is called, data->n points to the current piece.
1904
 * If this is an effective piece, then first increment data->n such
1905
 * that data->n contains the number of pieces.
1906
 * The "set_list" fields are subsequently replaced by the corresponding
1907
 * "set" fields, after which the pieces are sorted according to
1908
 * the number of disjuncts in these "set" fields.
1909
 *
1910
 * Construct intermediate AST expressions for the initial pieces and
1911
 * finish off with the final pieces.
1912
 */
1913
static isl_ast_expr *build_pieces(struct isl_from_pw_aff_data *data)
1914
4.15k
{
1915
4.15k
  int i;
1916
4.15k
  isl_ast_expr *res = NULL;
1917
4.15k
  isl_ast_expr **next = &res;
1918
4.15k
1919
4.15k
  if (data->p[data->n].state != isl_state_none)
1920
4.15k
    data->n++;
1921
4.15k
  if (data->n == 0)
1922
4.15k
    
isl_die0
(isl_ast_build_get_ctx(data->build), isl_error_invalid,
1923
4.15k
      "cannot handle void expression", return NULL);
1924
4.15k
1925
8.30k
  
for (i = 0; 4.15k
i < data->n;
++i4.15k
) {
1926
4.15k
    data->p[i].set = isl_set_list_union(data->p[i].set_list);
1927
4.15k
    if (data->p[i].state != isl_state_single)
1928
8
      data->p[i].set = isl_set_coalesce(data->p[i].set);
1929
4.15k
    data->p[i].set_list = NULL;
1930
4.15k
  }
1931
4.15k
1932
4.15k
  if (isl_sort(data->p, data->n, sizeof(data->p[0]),
1933
4.15k
      &sort_pieces_cmp, NULL) < 0)
1934
0
    return isl_ast_expr_free(res);
1935
4.15k
1936
4.15k
  
for (i = 0; 4.15k
i + 1 < data->n;
++i1
) {
1937
1
    next = add_intermediate_piece(data, i, next);
1938
1
    if (!next)
1939
0
      return isl_ast_expr_free(res);
1940
1
  }
1941
4.15k
1942
4.15k
  if (add_last_piece(data, data->n - 1, next) < 0)
1943
0
    return isl_ast_expr_free(res);
1944
4.15k
1945
4.15k
  return res;
1946
4.15k
}
1947
1948
/* Is the domain of the current entry of "data", which is assumed
1949
 * to contain a single subpiece, a subset of "set"?
1950
 */
1951
static isl_bool single_is_subset(struct isl_from_pw_aff_data *data,
1952
  __isl_keep isl_set *set)
1953
9
{
1954
9
  isl_bool subset;
1955
9
  isl_set *set_n;
1956
9
1957
9
  set_n = isl_set_list_get_set(data->p[data->n].set_list, 0);
1958
9
  subset = isl_set_is_subset(set_n, set);
1959
9
  isl_set_free(set_n);
1960
9
1961
9
  return subset;
1962
9
}
1963
1964
/* Is "aff" a rational expression, i.e., does it have a denominator
1965
 * different from one?
1966
 */
1967
static isl_bool aff_is_rational(__isl_keep isl_aff *aff)
1968
30
{
1969
30
  isl_bool rational;
1970
30
  isl_val *den;
1971
30
1972
30
  den = isl_aff_get_denominator_val(aff);
1973
30
  rational = isl_bool_not(isl_val_is_one(den));
1974
30
  isl_val_free(den);
1975
30
1976
30
  return rational;
1977
30
}
1978
1979
/* Does "list" consist of a single rational affine expression?
1980
 */
1981
static isl_bool is_single_rational_aff(__isl_keep isl_aff_list *list)
1982
15
{
1983
15
  isl_bool rational;
1984
15
  isl_aff *aff;
1985
15
1986
15
  if (isl_aff_list_n_aff(list) != 1)
1987
0
    return isl_bool_false;
1988
15
  aff = isl_aff_list_get_aff(list, 0);
1989
15
  rational = aff_is_rational(aff);
1990
15
  isl_aff_free(aff);
1991
15
1992
15
  return rational;
1993
15
}
1994
1995
/* Can the list of subpieces in the last piece of "data" be extended with
1996
 * "set" and "aff" based on "test"?
1997
 * In particular, is it the case for each entry (set_i, aff_i) that
1998
 *
1999
 *  test(aff, aff_i) holds on set_i, and
2000
 *  test(aff_i, aff) holds on set?
2001
 *
2002
 * "test" returns the set of elements where the tests holds, meaning
2003
 * that test(aff_i, aff) holds on set if set is a subset of test(aff_i, aff).
2004
 *
2005
 * This function is used to detect min/max expressions.
2006
 * If the ast_build_detect_min_max option is turned off, then
2007
 * do not even try and perform any detection and return false instead.
2008
 *
2009
 * Rational affine expressions are not considered for min/max expressions
2010
 * since the combined expression will be defined on the union of the domains,
2011
 * while a rational expression may only yield integer values
2012
 * on its own definition domain.
2013
 */
2014
static isl_bool extends(struct isl_from_pw_aff_data *data,
2015
  __isl_keep isl_set *set, __isl_keep isl_aff *aff,
2016
  __isl_give isl_basic_set *(*test)(__isl_take isl_aff *aff1,
2017
    __isl_take isl_aff *aff2))
2018
15
{
2019
15
  int i, n;
2020
15
  isl_bool is_rational;
2021
15
  isl_ctx *ctx;
2022
15
  isl_set *dom;
2023
15
2024
15
  is_rational = aff_is_rational(aff);
2025
15
  if (is_rational >= 0 && !is_rational)
2026
15
    is_rational = is_single_rational_aff(data->p[data->n].aff_list);
2027
15
  if (is_rational < 0 || is_rational)
2028
0
    return isl_bool_not(is_rational);
2029
15
2030
15
  ctx = isl_ast_build_get_ctx(data->build);
2031
15
  if (!isl_options_get_ast_build_detect_min_max(ctx))
2032
0
    return isl_bool_false;
2033
15
2034
15
  dom = isl_ast_build_get_domain(data->build);
2035
15
  set = isl_set_intersect(dom, isl_set_copy(set));
2036
15
2037
15
  n = isl_set_list_n_set(data->p[data->n].set_list);
2038
23
  for (i = 0; i < n ; 
++i8
) {
2039
15
    isl_aff *aff_i;
2040
15
    isl_set *valid;
2041
15
    isl_set *dom, *required;
2042
15
    isl_bool is_valid;
2043
15
2044
15
    aff_i = isl_aff_list_get_aff(data->p[data->n].aff_list, i);
2045
15
    valid = isl_set_from_basic_set(test(isl_aff_copy(aff), aff_i));
2046
15
    required = isl_set_list_get_set(data->p[data->n].set_list, i);
2047
15
    dom = isl_ast_build_get_domain(data->build);
2048
15
    required = isl_set_intersect(dom, required);
2049
15
    is_valid = isl_set_is_subset(required, valid);
2050
15
    isl_set_free(required);
2051
15
    isl_set_free(valid);
2052
15
    if (is_valid < 0 || !is_valid) {
2053
6
      isl_set_free(set);
2054
6
      return is_valid;
2055
6
    }
2056
9
2057
9
    aff_i = isl_aff_list_get_aff(data->p[data->n].aff_list, i);
2058
9
    valid = isl_set_from_basic_set(test(aff_i, isl_aff_copy(aff)));
2059
9
    is_valid = isl_set_is_subset(set, valid);
2060
9
    isl_set_free(valid);
2061
9
    if (is_valid < 0 || !is_valid) {
2062
1
      isl_set_free(set);
2063
1
      return is_valid;
2064
1
    }
2065
15
  }
2066
15
2067
15
  isl_set_free(set);
2068
8
  return isl_bool_true;
2069
15
}
2070
2071
/* Can the list of pieces in "data" be extended with "set" and "aff"
2072
 * to form/preserve a minimum expression?
2073
 * In particular, is it the case for each entry (set_i, aff_i) that
2074
 *
2075
 *  aff >= aff_i on set_i, and
2076
 *  aff_i >= aff on set?
2077
 */
2078
static isl_bool extends_min(struct isl_from_pw_aff_data *data,
2079
  __isl_keep isl_set *set,  __isl_keep isl_aff *aff)
2080
9
{
2081
9
  return extends(data, set, aff, &isl_aff_ge_basic_set);
2082
9
}
2083
2084
/* Can the list of pieces in "data" be extended with "set" and "aff"
2085
 * to form/preserve a maximum expression?
2086
 * In particular, is it the case for each entry (set_i, aff_i) that
2087
 *
2088
 *  aff <= aff_i on set_i, and
2089
 *  aff_i <= aff on set?
2090
 */
2091
static isl_bool extends_max(struct isl_from_pw_aff_data *data,
2092
  __isl_keep isl_set *set,  __isl_keep isl_aff *aff)
2093
6
{
2094
6
  return extends(data, set, aff, &isl_aff_le_basic_set);
2095
6
}
2096
2097
/* This function is called during the construction of an isl_ast_expr
2098
 * that evaluates an isl_pw_aff.
2099
 * If the last piece of "data" contains a single subpiece and
2100
 * if its affine function is equal to "aff" on a part of the domain
2101
 * that includes either "set" or the domain of that single subpiece,
2102
 * then extend the domain of that single subpiece with "set".
2103
 * If it was the original domain of the single subpiece where
2104
 * the two affine functions are equal, then also replace
2105
 * the affine function of the single subpiece by "aff".
2106
 * If the last piece of "data" contains either a single subpiece
2107
 * or a minimum, then check if this minimum expression can be extended
2108
 * with (set, aff).
2109
 * If so, extend the sequence and return.
2110
 * Perform the same operation for maximum expressions.
2111
 * If no such extension can be performed, then move to the next piece
2112
 * in "data" (if the current piece contains any data), and then store
2113
 * the current subpiece in the current piece of "data" for later handling.
2114
 */
2115
static isl_stat ast_expr_from_pw_aff(__isl_take isl_set *set,
2116
  __isl_take isl_aff *aff, void *user)
2117
4.16k
{
2118
4.16k
  struct isl_from_pw_aff_data *data = user;
2119
4.16k
  isl_bool test;
2120
4.16k
  enum isl_from_pw_aff_state state;
2121
4.16k
2122
4.16k
  state = data->p[data->n].state;
2123
4.16k
  if (state == isl_state_single) {
2124
9
    isl_aff *aff0;
2125
9
    isl_set *eq;
2126
9
    isl_bool subset1, subset2 = isl_bool_false;
2127
9
    aff0 = isl_aff_list_get_aff(data->p[data->n].aff_list, 0);
2128
9
    eq = isl_aff_eq_set(isl_aff_copy(aff), aff0);
2129
9
    subset1 = isl_set_is_subset(set, eq);
2130
9
    if (subset1 >= 0 && !subset1)
2131
9
      subset2 = single_is_subset(data, eq);
2132
9
    isl_set_free(eq);
2133
9
    if (subset1 < 0 || subset2 < 0)
2134
0
      goto error;
2135
9
    if (subset1)
2136
0
      return extend_domain(data, set, aff, 0);
2137
9
    if (subset2)
2138
0
      return extend_domain(data, set, aff, 1);
2139
4.16k
  }
2140
4.16k
  if (state == isl_state_single || 
state == isl_state_min4.15k
) {
2141
9
    test = extends_min(data, set, aff);
2142
9
    if (test < 0)
2143
0
      goto error;
2144
9
    if (test)
2145
3
      return extend_min(data, set, aff);
2146
4.16k
  }
2147
4.16k
  if (state == isl_state_single || 
state == isl_state_max4.15k
) {
2148
6
    test = extends_max(data, set, aff);
2149
6
    if (test < 0)
2150
0
      goto error;
2151
6
    if (test)
2152
5
      return extend_max(data, set, aff);
2153
4.15k
  }
2154
4.15k
  if (state != isl_state_none)
2155
1
    data->n++;
2156
4.15k
  set_single(data, set, aff);
2157
4.15k
2158
4.15k
  return isl_stat_ok;
2159
0
error:
2160
0
  isl_set_free(set);
2161
0
  isl_aff_free(aff);
2162
0
  return isl_stat_error;
2163
4.16k
}
2164
2165
/* Construct an isl_ast_expr that evaluates "pa".
2166
 * The result is simplified in terms of build->domain.
2167
 *
2168
 * The domain of "pa" lives in the internal schedule space.
2169
 */
2170
__isl_give isl_ast_expr *isl_ast_build_expr_from_pw_aff_internal(
2171
  __isl_keep isl_ast_build *build, __isl_take isl_pw_aff *pa)
2172
4.15k
{
2173
4.15k
  struct isl_from_pw_aff_data data = { NULL };
2174
4.15k
  isl_ast_expr *res = NULL;
2175
4.15k
2176
4.15k
  pa = isl_ast_build_compute_gist_pw_aff(build, pa);
2177
4.15k
  pa = isl_pw_aff_coalesce(pa);
2178
4.15k
  if (!pa)
2179
0
    return NULL;
2180
4.15k
2181
4.15k
  if (isl_from_pw_aff_data_init(&data, build, pa) < 0)
2182
0
    goto error;
2183
4.15k
  set_none(&data);
2184
4.15k
2185
4.15k
  if (isl_pw_aff_foreach_piece(pa, &ast_expr_from_pw_aff, &data) >= 0)
2186
4.15k
    res = build_pieces(&data);
2187
4.15k
2188
4.15k
  isl_pw_aff_free(pa);
2189
4.15k
  isl_from_pw_aff_data_clear(&data);
2190
4.15k
  return res;
2191
0
error:
2192
0
  isl_pw_aff_free(pa);
2193
0
  isl_from_pw_aff_data_clear(&data);
2194
0
  return NULL;
2195
4.15k
}
2196
2197
/* Construct an isl_ast_expr that evaluates "pa".
2198
 * The result is simplified in terms of build->domain.
2199
 *
2200
 * The domain of "pa" lives in the external schedule space.
2201
 */
2202
__isl_give isl_ast_expr *isl_ast_build_expr_from_pw_aff(
2203
  __isl_keep isl_ast_build *build, __isl_take isl_pw_aff *pa)
2204
6
{
2205
6
  isl_ast_expr *expr;
2206
6
2207
6
  if (isl_ast_build_need_schedule_map(build)) {
2208
0
    isl_multi_aff *ma;
2209
0
    ma = isl_ast_build_get_schedule_map_multi_aff(build);
2210
0
    pa = isl_pw_aff_pullback_multi_aff(pa, ma);
2211
0
  }
2212
6
  expr = isl_ast_build_expr_from_pw_aff_internal(build, pa);
2213
6
  return expr;
2214
6
}
2215
2216
/* Set the ids of the input dimensions of "mpa" to the iterator ids
2217
 * of "build".
2218
 *
2219
 * The domain of "mpa" is assumed to live in the internal schedule domain.
2220
 */
2221
static __isl_give isl_multi_pw_aff *set_iterator_names(
2222
  __isl_keep isl_ast_build *build, __isl_take isl_multi_pw_aff *mpa)
2223
1.53k
{
2224
1.53k
  int i, n;
2225
1.53k
2226
1.53k
  n = isl_multi_pw_aff_dim(mpa, isl_dim_in);
2227
11.1k
  for (i = 0; i < n; 
++i9.62k
) {
2228
9.62k
    isl_id *id;
2229
9.62k
2230
9.62k
    id = isl_ast_build_get_iterator_id(build, i);
2231
9.62k
    mpa = isl_multi_pw_aff_set_dim_id(mpa, isl_dim_in, i, id);
2232
9.62k
  }
2233
1.53k
2234
1.53k
  return mpa;
2235
1.53k
}
2236
2237
/* Construct an isl_ast_expr of type "type" with as first argument "arg0" and
2238
 * the remaining arguments derived from "mpa".
2239
 * That is, construct a call or access expression that calls/accesses "arg0"
2240
 * with arguments/indices specified by "mpa".
2241
 */
2242
static __isl_give isl_ast_expr *isl_ast_build_with_arguments(
2243
  __isl_keep isl_ast_build *build, enum isl_ast_op_type type,
2244
  __isl_take isl_ast_expr *arg0, __isl_take isl_multi_pw_aff *mpa)
2245
1.53k
{
2246
1.53k
  int i, n;
2247
1.53k
  isl_ctx *ctx;
2248
1.53k
  isl_ast_expr *expr;
2249
1.53k
2250
1.53k
  ctx = isl_ast_build_get_ctx(build);
2251
1.53k
2252
1.53k
  n = isl_multi_pw_aff_dim(mpa, isl_dim_out);
2253
1.53k
  expr = isl_ast_expr_alloc_op(ctx, type, 1 + n);
2254
1.53k
  expr = isl_ast_expr_set_op_arg(expr, 0, arg0);
2255
5.19k
  for (i = 0; i < n; 
++i3.66k
) {
2256
3.66k
    isl_pw_aff *pa;
2257
3.66k
    isl_ast_expr *arg;
2258
3.66k
2259
3.66k
    pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
2260
3.66k
    arg = isl_ast_build_expr_from_pw_aff_internal(build, pa);
2261
3.66k
    expr = isl_ast_expr_set_op_arg(expr, 1 + i, arg);
2262
3.66k
  }
2263
1.53k
2264
1.53k
  isl_multi_pw_aff_free(mpa);
2265
1.53k
  return expr;
2266
1.53k
}
2267
2268
static __isl_give isl_ast_expr *isl_ast_build_from_multi_pw_aff_internal(
2269
  __isl_keep isl_ast_build *build, enum isl_ast_op_type type,
2270
  __isl_take isl_multi_pw_aff *mpa);
2271
2272
/* Construct an isl_ast_expr that accesses the member specified by "mpa".
2273
 * The range of "mpa" is assumed to be wrapped relation.
2274
 * The domain of this wrapped relation specifies the structure being
2275
 * accessed, while the range of this wrapped relation spacifies the
2276
 * member of the structure being accessed.
2277
 *
2278
 * The domain of "mpa" is assumed to live in the internal schedule domain.
2279
 */
2280
static __isl_give isl_ast_expr *isl_ast_build_from_multi_pw_aff_member(
2281
  __isl_keep isl_ast_build *build, __isl_take isl_multi_pw_aff *mpa)
2282
0
{
2283
0
  isl_id *id;
2284
0
  isl_multi_pw_aff *domain;
2285
0
  isl_ast_expr *domain_expr, *expr;
2286
0
  enum isl_ast_op_type type = isl_ast_op_access;
2287
0
2288
0
  domain = isl_multi_pw_aff_copy(mpa);
2289
0
  domain = isl_multi_pw_aff_range_factor_domain(domain);
2290
0
  domain_expr = isl_ast_build_from_multi_pw_aff_internal(build,
2291
0
                type, domain);
2292
0
  mpa = isl_multi_pw_aff_range_factor_range(mpa);
2293
0
  if (!isl_multi_pw_aff_has_tuple_id(mpa, isl_dim_out))
2294
0
    isl_die(isl_ast_build_get_ctx(build), isl_error_invalid,
2295
0
      "missing field name", goto error);
2296
0
  id = isl_multi_pw_aff_get_tuple_id(mpa, isl_dim_out);
2297
0
  expr = isl_ast_expr_from_id(id);
2298
0
  expr = isl_ast_expr_alloc_binary(isl_ast_op_member, domain_expr, expr);
2299
0
  return isl_ast_build_with_arguments(build, type, expr, mpa);
2300
0
error:
2301
0
  isl_multi_pw_aff_free(mpa);
2302
0
  return NULL;
2303
0
}
2304
2305
/* Construct an isl_ast_expr of type "type" that calls or accesses
2306
 * the element specified by "mpa".
2307
 * The first argument is obtained from the output tuple name.
2308
 * The remaining arguments are given by the piecewise affine expressions.
2309
 *
2310
 * If the range of "mpa" is a mapped relation, then we assume it
2311
 * represents an access to a member of a structure.
2312
 *
2313
 * The domain of "mpa" is assumed to live in the internal schedule domain.
2314
 */
2315
static __isl_give isl_ast_expr *isl_ast_build_from_multi_pw_aff_internal(
2316
  __isl_keep isl_ast_build *build, enum isl_ast_op_type type,
2317
  __isl_take isl_multi_pw_aff *mpa)
2318
1.53k
{
2319
1.53k
  isl_ctx *ctx;
2320
1.53k
  isl_id *id;
2321
1.53k
  isl_ast_expr *expr;
2322
1.53k
2323
1.53k
  if (!mpa)
2324
0
    goto error;
2325
1.53k
2326
1.53k
  if (type == isl_ast_op_access &&
2327
1.53k
      
isl_multi_pw_aff_range_is_wrapping(mpa)378
)
2328
0
    return isl_ast_build_from_multi_pw_aff_member(build, mpa);
2329
1.53k
2330
1.53k
  mpa = set_iterator_names(build, mpa);
2331
1.53k
  if (!build || !mpa)
2332
0
    goto error;
2333
1.53k
2334
1.53k
  ctx = isl_ast_build_get_ctx(build);
2335
1.53k
2336
1.53k
  if (isl_multi_pw_aff_has_tuple_id(mpa, isl_dim_out))
2337
1.53k
    id = isl_multi_pw_aff_get_tuple_id(mpa, isl_dim_out);
2338
0
  else
2339
0
    id = isl_id_alloc(ctx, "", NULL);
2340
1.53k
2341
1.53k
  expr = isl_ast_expr_from_id(id);
2342
1.53k
  return isl_ast_build_with_arguments(build, type, expr, mpa);
2343
0
error:
2344
0
  isl_multi_pw_aff_free(mpa);
2345
0
  return NULL;
2346
1.53k
}
2347
2348
/* Construct an isl_ast_expr of type "type" that calls or accesses
2349
 * the element specified by "pma".
2350
 * The first argument is obtained from the output tuple name.
2351
 * The remaining arguments are given by the piecewise affine expressions.
2352
 *
2353
 * The domain of "pma" is assumed to live in the internal schedule domain.
2354
 */
2355
static __isl_give isl_ast_expr *isl_ast_build_from_pw_multi_aff_internal(
2356
  __isl_keep isl_ast_build *build, enum isl_ast_op_type type,
2357
  __isl_take isl_pw_multi_aff *pma)
2358
1.15k
{
2359
1.15k
  isl_multi_pw_aff *mpa;
2360
1.15k
2361
1.15k
  mpa = isl_multi_pw_aff_from_pw_multi_aff(pma);
2362
1.15k
  return isl_ast_build_from_multi_pw_aff_internal(build, type, mpa);
2363
1.15k
}
2364
2365
/* Construct an isl_ast_expr of type "type" that calls or accesses
2366
 * the element specified by "mpa".
2367
 * The first argument is obtained from the output tuple name.
2368
 * The remaining arguments are given by the piecewise affine expressions.
2369
 *
2370
 * The domain of "mpa" is assumed to live in the external schedule domain.
2371
 */
2372
static __isl_give isl_ast_expr *isl_ast_build_from_multi_pw_aff(
2373
  __isl_keep isl_ast_build *build, enum isl_ast_op_type type,
2374
  __isl_take isl_multi_pw_aff *mpa)
2375
378
{
2376
378
  int is_domain;
2377
378
  isl_ast_expr *expr;
2378
378
  isl_space *space_build, *space_mpa;
2379
378
2380
378
  space_build = isl_ast_build_get_space(build, 0);
2381
378
  space_mpa = isl_multi_pw_aff_get_space(mpa);
2382
378
  is_domain = isl_space_tuple_is_equal(space_build, isl_dim_set,
2383
378
          space_mpa, isl_dim_in);
2384
378
  isl_space_free(space_build);
2385
378
  isl_space_free(space_mpa);
2386
378
  if (is_domain < 0)
2387
0
    goto error;
2388
378
  if (!is_domain)
2389
378
    
isl_die0
(isl_ast_build_get_ctx(build), isl_error_invalid,
2390
378
      "spaces don't match", goto error);
2391
378
2392
378
  if (isl_ast_build_need_schedule_map(build)) {
2393
143
    isl_multi_aff *ma;
2394
143
    ma = isl_ast_build_get_schedule_map_multi_aff(build);
2395
143
    mpa = isl_multi_pw_aff_pullback_multi_aff(mpa, ma);
2396
143
  }
2397
378
2398
378
  expr = isl_ast_build_from_multi_pw_aff_internal(build, type, mpa);
2399
378
  return expr;
2400
0
error:
2401
0
  isl_multi_pw_aff_free(mpa);
2402
0
  return NULL;
2403
378
}
2404
2405
/* Construct an isl_ast_expr that calls the domain element specified by "mpa".
2406
 * The name of the function is obtained from the output tuple name.
2407
 * The arguments are given by the piecewise affine expressions.
2408
 *
2409
 * The domain of "mpa" is assumed to live in the external schedule domain.
2410
 */
2411
__isl_give isl_ast_expr *isl_ast_build_call_from_multi_pw_aff(
2412
  __isl_keep isl_ast_build *build, __isl_take isl_multi_pw_aff *mpa)
2413
0
{
2414
0
  return isl_ast_build_from_multi_pw_aff(build, isl_ast_op_call, mpa);
2415
0
}
2416
2417
/* Construct an isl_ast_expr that accesses the array element specified by "mpa".
2418
 * The name of the array is obtained from the output tuple name.
2419
 * The index expressions are given by the piecewise affine expressions.
2420
 *
2421
 * The domain of "mpa" is assumed to live in the external schedule domain.
2422
 */
2423
__isl_give isl_ast_expr *isl_ast_build_access_from_multi_pw_aff(
2424
  __isl_keep isl_ast_build *build, __isl_take isl_multi_pw_aff *mpa)
2425
0
{
2426
0
  return isl_ast_build_from_multi_pw_aff(build, isl_ast_op_access, mpa);
2427
0
}
2428
2429
/* Construct an isl_ast_expr of type "type" that calls or accesses
2430
 * the element specified by "pma".
2431
 * The first argument is obtained from the output tuple name.
2432
 * The remaining arguments are given by the piecewise affine expressions.
2433
 *
2434
 * The domain of "pma" is assumed to live in the external schedule domain.
2435
 */
2436
static __isl_give isl_ast_expr *isl_ast_build_from_pw_multi_aff(
2437
  __isl_keep isl_ast_build *build, enum isl_ast_op_type type,
2438
  __isl_take isl_pw_multi_aff *pma)
2439
378
{
2440
378
  isl_multi_pw_aff *mpa;
2441
378
2442
378
  mpa = isl_multi_pw_aff_from_pw_multi_aff(pma);
2443
378
  return isl_ast_build_from_multi_pw_aff(build, type, mpa);
2444
378
}
2445
2446
/* Construct an isl_ast_expr that calls the domain element specified by "pma".
2447
 * The name of the function is obtained from the output tuple name.
2448
 * The arguments are given by the piecewise affine expressions.
2449
 *
2450
 * The domain of "pma" is assumed to live in the external schedule domain.
2451
 */
2452
__isl_give isl_ast_expr *isl_ast_build_call_from_pw_multi_aff(
2453
  __isl_keep isl_ast_build *build, __isl_take isl_pw_multi_aff *pma)
2454
0
{
2455
0
  return isl_ast_build_from_pw_multi_aff(build, isl_ast_op_call, pma);
2456
0
}
2457
2458
/* Construct an isl_ast_expr that accesses the array element specified by "pma".
2459
 * The name of the array is obtained from the output tuple name.
2460
 * The index expressions are given by the piecewise affine expressions.
2461
 *
2462
 * The domain of "pma" is assumed to live in the external schedule domain.
2463
 */
2464
__isl_give isl_ast_expr *isl_ast_build_access_from_pw_multi_aff(
2465
  __isl_keep isl_ast_build *build, __isl_take isl_pw_multi_aff *pma)
2466
378
{
2467
378
  return isl_ast_build_from_pw_multi_aff(build, isl_ast_op_access, pma);
2468
378
}
2469
2470
/* Construct an isl_ast_expr that calls the domain element
2471
 * specified by "executed".
2472
 *
2473
 * "executed" is assumed to be single-valued, with a domain that lives
2474
 * in the internal schedule space.
2475
 */
2476
__isl_give isl_ast_node *isl_ast_build_call_from_executed(
2477
  __isl_keep isl_ast_build *build, __isl_take isl_map *executed)
2478
1.15k
{
2479
1.15k
  isl_pw_multi_aff *iteration;
2480
1.15k
  isl_ast_expr *expr;
2481
1.15k
2482
1.15k
  iteration = isl_pw_multi_aff_from_map(executed);
2483
1.15k
  iteration = isl_ast_build_compute_gist_pw_multi_aff(build, iteration);
2484
1.15k
  iteration = isl_pw_multi_aff_intersect_domain(iteration,
2485
1.15k
          isl_ast_build_get_domain(build));
2486
1.15k
  expr = isl_ast_build_from_pw_multi_aff_internal(build, isl_ast_op_call,
2487
1.15k
              iteration);
2488
1.15k
  return isl_ast_node_alloc_user(expr);
2489
1.15k
}