Coverage Report

Created: 2017-11-23 03:11

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/polly/lib/External/isl/isl_range.c
Line
Count
Source (jump to first uncovered line)
1
#include <isl_ctx_private.h>
2
#include <isl_constraint_private.h>
3
#include <isl/set.h>
4
#include <isl_polynomial_private.h>
5
#include <isl_morph.h>
6
#include <isl_range.h>
7
8
struct range_data {
9
  struct isl_bound  *bound;
10
  int           *signs;
11
  int     sign;
12
  int     test_monotonicity;
13
  int         monotonicity;
14
  int     tight;
15
  isl_qpolynomial       *poly;
16
  isl_pw_qpolynomial_fold *pwf;
17
  isl_pw_qpolynomial_fold *pwf_tight;
18
};
19
20
static isl_stat propagate_on_domain(__isl_take isl_basic_set *bset,
21
  __isl_take isl_qpolynomial *poly, struct range_data *data);
22
23
/* Check whether the polynomial "poly" has sign "sign" over "bset",
24
 * i.e., if sign == 1, check that the lower bound on the polynomial
25
 * is non-negative and if sign == -1, check that the upper bound on
26
 * the polynomial is non-positive.
27
 */
28
static int has_sign(__isl_keep isl_basic_set *bset,
29
  __isl_keep isl_qpolynomial *poly, int sign, int *signs)
30
7
{
31
7
  struct range_data data_m;
32
7
  unsigned nparam;
33
7
  isl_space *dim;
34
7
  isl_val *opt;
35
7
  int r;
36
7
  enum isl_fold type;
37
7
38
7
  nparam = isl_basic_set_dim(bset, isl_dim_param);
39
7
40
7
  bset = isl_basic_set_copy(bset);
41
7
  poly = isl_qpolynomial_copy(poly);
42
7
43
7
  bset = isl_basic_set_move_dims(bset, isl_dim_set, 0,
44
7
          isl_dim_param, 0, nparam);
45
7
  poly = isl_qpolynomial_move_dims(poly, isl_dim_in, 0,
46
7
          isl_dim_param, 0, nparam);
47
7
48
7
  dim = isl_qpolynomial_get_space(poly);
49
7
  dim = isl_space_params(dim);
50
7
  dim = isl_space_from_domain(dim);
51
7
  dim = isl_space_add_dims(dim, isl_dim_out, 1);
52
7
53
7
  data_m.test_monotonicity = 0;
54
7
  data_m.signs = signs;
55
7
  data_m.sign = -sign;
56
7
  type = data_m.sign < 0 ? 
isl_fold_min5
:
isl_fold_max2
;
57
7
  data_m.pwf = isl_pw_qpolynomial_fold_zero(dim, type);
58
7
  data_m.tight = 0;
59
7
  data_m.pwf_tight = NULL;
60
7
61
7
  if (propagate_on_domain(bset, poly, &data_m) < 0)
62
0
    goto error;
63
7
64
7
  if (sign > 0)
65
5
    opt = isl_pw_qpolynomial_fold_min(data_m.pwf);
66
2
  else
67
2
    opt = isl_pw_qpolynomial_fold_max(data_m.pwf);
68
7
69
7
  if (!opt)
70
0
    r = -1;
71
7
  else if (isl_val_is_nan(opt) ||
72
7
     isl_val_is_infty(opt) ||
73
7
     isl_val_is_neginfty(opt))
74
2
    r = 0;
75
5
  else
76
5
    r = sign * isl_val_sgn(opt) >= 0;
77
7
78
7
  isl_val_free(opt);
79
7
80
7
  return r;
81
0
error:
82
0
  isl_pw_qpolynomial_fold_free(data_m.pwf);
83
0
  return -1;
84
7
}
85
86
/* Return  1 if poly is monotonically increasing in the last set variable,
87
 *        -1 if poly is monotonically decreasing in the last set variable,
88
 *     0 if no conclusion,
89
 *    -2 on error.
90
 *
91
 * We simply check the sign of p(x+1)-p(x)
92
 */
93
static int monotonicity(__isl_keep isl_basic_set *bset,
94
  __isl_keep isl_qpolynomial *poly, struct range_data *data)
95
5
{
96
5
  isl_ctx *ctx;
97
5
  isl_space *dim;
98
5
  isl_qpolynomial *sub = NULL;
99
5
  isl_qpolynomial *diff = NULL;
100
5
  int result = 0;
101
5
  int s;
102
5
  unsigned nvar;
103
5
104
5
  ctx = isl_qpolynomial_get_ctx(poly);
105
5
  dim = isl_qpolynomial_get_domain_space(poly);
106
5
107
5
  nvar = isl_basic_set_dim(bset, isl_dim_set);
108
5
109
5
  sub = isl_qpolynomial_var_on_domain(isl_space_copy(dim), isl_dim_set, nvar - 1);
110
5
  sub = isl_qpolynomial_add(sub,
111
5
    isl_qpolynomial_rat_cst_on_domain(dim, ctx->one, ctx->one));
112
5
113
5
  diff = isl_qpolynomial_substitute(isl_qpolynomial_copy(poly),
114
5
      isl_dim_in, nvar - 1, 1, &sub);
115
5
  diff = isl_qpolynomial_sub(diff, isl_qpolynomial_copy(poly));
116
5
117
5
  s = has_sign(bset, diff, 1, data->signs);
118
5
  if (s < 0)
119
0
    goto error;
120
5
  if (s)
121
3
    result = 1;
122
2
  else {
123
2
    s = has_sign(bset, diff, -1, data->signs);
124
2
    if (s < 0)
125
0
      goto error;
126
2
    if (s)
127
2
      result = -1;
128
2
  }
129
5
130
5
  isl_qpolynomial_free(diff);
131
5
  isl_qpolynomial_free(sub);
132
5
133
5
  return result;
134
0
error:
135
0
  isl_qpolynomial_free(diff);
136
0
  isl_qpolynomial_free(sub);
137
0
  return -2;
138
5
}
139
140
/* Return a positive ("sign" > 0) or negative ("sign" < 0) infinite polynomial
141
 * with domain space "space".
142
 */
143
static __isl_give isl_qpolynomial *signed_infty(__isl_take isl_space *space,
144
  int sign)
145
18
{
146
18
  if (sign > 0)
147
10
    return isl_qpolynomial_infty_on_domain(space);
148
8
  else
149
8
    return isl_qpolynomial_neginfty_on_domain(space);
150
18
}
151
152
static __isl_give isl_qpolynomial *bound2poly(__isl_take isl_constraint *bound,
153
  __isl_take isl_space *space, unsigned pos, int sign)
154
29
{
155
29
  if (!bound)
156
14
    return signed_infty(space, sign);
157
15
  isl_space_free(space);
158
15
  return isl_qpolynomial_from_constraint(bound, isl_dim_set, pos);
159
15
}
160
161
static int bound_is_integer(__isl_take isl_constraint *bound, unsigned pos)
162
0
{
163
0
  isl_int c;
164
0
  int is_int;
165
0
166
0
  if (!bound)
167
0
    return 1;
168
0
169
0
  isl_int_init(c);
170
0
  isl_constraint_get_coefficient(bound, isl_dim_set, pos, &c);
171
0
  is_int = isl_int_is_one(c) || isl_int_is_negone(c);
172
0
  isl_int_clear(c);
173
0
174
0
  return is_int;
175
0
}
176
177
struct isl_fixed_sign_data {
178
  int   *signs;
179
  int   sign;
180
  isl_qpolynomial *poly;
181
};
182
183
/* Add term "term" to data->poly if it has sign data->sign.
184
 * The sign is determined based on the signs of the parameters
185
 * and variables in data->signs.  The integer divisions, if
186
 * any, are assumed to be non-negative.
187
 */
188
static isl_stat collect_fixed_sign_terms(__isl_take isl_term *term, void *user)
189
24
{
190
24
  struct isl_fixed_sign_data *data = (struct isl_fixed_sign_data *)user;
191
24
  isl_int n;
192
24
  int i;
193
24
  int sign;
194
24
  unsigned nparam;
195
24
  unsigned nvar;
196
24
197
24
  if (!term)
198
0
    return isl_stat_error;
199
24
200
24
  nparam = isl_term_dim(term, isl_dim_param);
201
24
  nvar = isl_term_dim(term, isl_dim_set);
202
24
203
24
  isl_int_init(n);
204
24
205
24
  isl_term_get_num(term, &n);
206
24
207
24
  sign = isl_int_sgn(n);
208
24
  for (i = 0; i < nparam; 
++i0
) {
209
0
    if (data->signs[i] > 0)
210
0
      continue;
211
0
    if (isl_term_get_exp(term, isl_dim_param, i) % 2)
212
0
      sign = -sign;
213
0
  }
214
60
  for (i = 0; i < nvar; 
++i36
) {
215
36
    if (data->signs[nparam + i] > 0)
216
22
      continue;
217
14
    if (isl_term_get_exp(term, isl_dim_set, i) % 2)
218
8
      sign = -sign;
219
36
  }
220
24
221
24
  if (sign == data->sign) {
222
12
    isl_qpolynomial *t = isl_qpolynomial_from_term(term);
223
12
224
12
    data->poly = isl_qpolynomial_add(data->poly, t);
225
12
  } else
226
12
    isl_term_free(term);
227
24
228
24
  isl_int_clear(n);
229
24
230
24
  return isl_stat_ok;
231
24
}
232
233
/* Construct and return a polynomial that consists of the terms
234
 * in "poly" that have sign "sign".  The integer divisions, if
235
 * any, are assumed to be non-negative.
236
 */
237
__isl_give isl_qpolynomial *isl_qpolynomial_terms_of_sign(
238
  __isl_keep isl_qpolynomial *poly, int *signs, int sign)
239
24
{
240
24
  isl_space *space;
241
24
  struct isl_fixed_sign_data data = { signs, sign };
242
24
243
24
  space = isl_qpolynomial_get_domain_space(poly);
244
24
  data.poly = isl_qpolynomial_zero_on_domain(space);
245
24
246
24
  if (isl_qpolynomial_foreach_term(poly, collect_fixed_sign_terms, &data) < 0)
247
0
    goto error;
248
24
249
24
  return data.poly;
250
0
error:
251
0
  isl_qpolynomial_free(data.poly);
252
0
  return NULL;
253
24
}
254
255
/* Helper function to add a guarded polynomial to either pwf_tight or pwf,
256
 * depending on whether the result has been determined to be tight.
257
 */
258
static isl_stat add_guarded_poly(__isl_take isl_basic_set *bset,
259
  __isl_take isl_qpolynomial *poly, struct range_data *data)
260
11
{
261
11
  enum isl_fold type = data->sign < 0 ? 
isl_fold_min5
:
isl_fold_max6
;
262
11
  isl_set *set;
263
11
  isl_qpolynomial_fold *fold;
264
11
  isl_pw_qpolynomial_fold *pwf;
265
11
266
11
  bset = isl_basic_set_params(bset);
267
11
  poly = isl_qpolynomial_project_domain_on_params(poly);
268
11
269
11
  fold = isl_qpolynomial_fold_alloc(type, poly);
270
11
  set = isl_set_from_basic_set(bset);
271
11
  pwf = isl_pw_qpolynomial_fold_alloc(type, set, fold);
272
11
  if (data->tight)
273
0
    data->pwf_tight = isl_pw_qpolynomial_fold_fold(
274
0
            data->pwf_tight, pwf);
275
11
  else
276
11
    data->pwf = isl_pw_qpolynomial_fold_fold(data->pwf, pwf);
277
11
278
11
  return isl_stat_ok;
279
11
}
280
281
/* Plug in "sub" for the variable at position "pos" in "poly".
282
 *
283
 * If "sub" is an infinite polynomial and if the variable actually
284
 * appears in "poly", then calling isl_qpolynomial_substitute
285
 * to perform the substitution may result in a NaN result.
286
 * In such cases, return positive or negative infinity instead,
287
 * depending on whether an upper bound or a lower bound is being computed,
288
 * and mark the result as not being tight.
289
 */
290
static __isl_give isl_qpolynomial *plug_in_at_pos(
291
  __isl_take isl_qpolynomial *poly, int pos,
292
  __isl_take isl_qpolynomial *sub, struct range_data *data)
293
29
{
294
29
  isl_bool involves, infty;
295
29
296
29
  involves = isl_qpolynomial_involves_dims(poly, isl_dim_in, pos, 1);
297
29
  if (involves < 0)
298
0
    goto error;
299
29
  if (!involves) {
300
18
    isl_qpolynomial_free(sub);
301
18
    return poly;
302
18
  }
303
11
304
11
  infty = isl_qpolynomial_is_infty(sub);
305
11
  if (infty >= 0 && !infty)
306
8
    infty = isl_qpolynomial_is_neginfty(sub);
307
11
  if (infty < 0)
308
0
    goto error;
309
11
  if (infty) {
310
4
    isl_space *space = isl_qpolynomial_get_domain_space(poly);
311
4
    data->tight = 0;
312
4
    isl_qpolynomial_free(poly);
313
4
    isl_qpolynomial_free(sub);
314
4
    return signed_infty(space, data->sign);
315
4
  }
316
7
317
7
  poly = isl_qpolynomial_substitute(poly, isl_dim_in, pos, 1, &sub);
318
7
  isl_qpolynomial_free(sub);
319
7
320
7
  return poly;
321
0
error:
322
0
  isl_qpolynomial_free(poly);
323
0
  isl_qpolynomial_free(sub);
324
0
  return NULL;
325
29
}
326
327
/* Given a lower and upper bound on the final variable and constraints
328
 * on the remaining variables where these bounds are active,
329
 * eliminate the variable from data->poly based on these bounds.
330
 * If the polynomial has been determined to be monotonic
331
 * in the variable, then simply plug in the appropriate bound.
332
 * If the current polynomial is tight and if this bound is integer,
333
 * then the result is still tight.  In all other cases, the results
334
 * may not be tight.
335
 * Otherwise, plug in the largest bound (in absolute value) in
336
 * the positive terms (if an upper bound is wanted) or the negative terms
337
 * (if a lower bounded is wanted) and the other bound in the other terms.
338
 *
339
 * If all variables have been eliminated, then record the result.
340
 * Ohterwise, recurse on the next variable.
341
 */
342
static isl_stat propagate_on_bound_pair(__isl_take isl_constraint *lower,
343
  __isl_take isl_constraint *upper, __isl_take isl_basic_set *bset,
344
  void *user)
345
17
{
346
17
  struct range_data *data = (struct range_data *)user;
347
17
  int save_tight = data->tight;
348
17
  isl_qpolynomial *poly;
349
17
  isl_stat r;
350
17
  unsigned nvar;
351
17
352
17
  nvar = isl_basic_set_dim(bset, isl_dim_set);
353
17
354
17
  if (data->monotonicity) {
355
5
    isl_qpolynomial *sub;
356
5
    isl_space *dim = isl_qpolynomial_get_domain_space(data->poly);
357
5
    if (data->monotonicity * data->sign > 0) {
358
3
      if (data->tight)
359
0
        data->tight = bound_is_integer(upper, nvar);
360
3
      sub = bound2poly(upper, dim, nvar, 1);
361
3
      isl_constraint_free(lower);
362
3
    } else {
363
2
      if (data->tight)
364
0
        data->tight = bound_is_integer(lower, nvar);
365
2
      sub = bound2poly(lower, dim, nvar, -1);
366
2
      isl_constraint_free(upper);
367
2
    }
368
5
    poly = isl_qpolynomial_copy(data->poly);
369
5
    poly = plug_in_at_pos(poly, nvar, sub, data);
370
5
    poly = isl_qpolynomial_drop_dims(poly, isl_dim_in, nvar, 1);
371
12
  } else {
372
12
    isl_qpolynomial *l, *u;
373
12
    isl_qpolynomial *pos, *neg;
374
12
    isl_space *dim = isl_qpolynomial_get_domain_space(data->poly);
375
12
    unsigned nparam = isl_basic_set_dim(bset, isl_dim_param);
376
12
    int sign = data->sign * data->signs[nparam + nvar];
377
12
378
12
    data->tight = 0;
379
12
380
12
    u = bound2poly(upper, isl_space_copy(dim), nvar, 1);
381
12
    l = bound2poly(lower, dim, nvar, -1);
382
12
383
12
    pos = isl_qpolynomial_terms_of_sign(data->poly, data->signs, sign);
384
12
    neg = isl_qpolynomial_terms_of_sign(data->poly, data->signs, -sign);
385
12
386
12
    pos = plug_in_at_pos(pos, nvar, u, data);
387
12
    neg = plug_in_at_pos(neg, nvar, l, data);
388
12
389
12
    poly = isl_qpolynomial_add(pos, neg);
390
12
    poly = isl_qpolynomial_drop_dims(poly, isl_dim_in, nvar, 1);
391
12
  }
392
17
393
17
  if (isl_basic_set_dim(bset, isl_dim_set) == 0)
394
7
    r = add_guarded_poly(bset, poly, data);
395
10
  else
396
10
    r = propagate_on_domain(bset, poly, data);
397
17
398
17
  data->tight = save_tight;
399
17
400
17
  return r;
401
17
}
402
403
/* Recursively perform range propagation on the polynomial "poly"
404
 * defined over the basic set "bset" and collect the results in "data".
405
 */
406
static isl_stat propagate_on_domain(__isl_take isl_basic_set *bset,
407
  __isl_take isl_qpolynomial *poly, struct range_data *data)
408
21
{
409
21
  isl_ctx *ctx;
410
21
  isl_qpolynomial *save_poly = data->poly;
411
21
  int save_monotonicity = data->monotonicity;
412
21
  unsigned d;
413
21
414
21
  if (!bset || !poly)
415
0
    goto error;
416
21
417
21
  ctx = isl_basic_set_get_ctx(bset);
418
21
  d = isl_basic_set_dim(bset, isl_dim_set);
419
21
  isl_assert(ctx, d >= 1, goto error);
420
21
421
21
  if (isl_qpolynomial_is_cst(poly, NULL, NULL)) {
422
4
    bset = isl_basic_set_project_out(bset, isl_dim_set, 0, d);
423
4
    poly = isl_qpolynomial_drop_dims(poly, isl_dim_in, 0, d);
424
4
    return add_guarded_poly(bset, poly, data);
425
4
  }
426
17
427
17
  if (data->test_monotonicity)
428
5
    data->monotonicity = monotonicity(bset, poly, data);
429
12
  else
430
12
    data->monotonicity = 0;
431
17
  if (data->monotonicity < -1)
432
0
    goto error;
433
17
434
17
  data->poly = poly;
435
17
  if (isl_basic_set_foreach_bound_pair(bset, isl_dim_set, d - 1,
436
17
              &propagate_on_bound_pair, data) < 0)
437
0
    goto error;
438
17
439
17
  isl_basic_set_free(bset);
440
17
  isl_qpolynomial_free(poly);
441
17
  data->monotonicity = save_monotonicity;
442
17
  data->poly = save_poly;
443
17
444
17
  return isl_stat_ok;
445
0
error:
446
0
  isl_basic_set_free(bset);
447
0
  isl_qpolynomial_free(poly);
448
0
  data->monotonicity = save_monotonicity;
449
0
  data->poly = save_poly;
450
0
  return isl_stat_error;
451
21
}
452
453
static isl_stat basic_guarded_poly_bound(__isl_take isl_basic_set *bset,
454
  void *user)
455
4
{
456
4
  struct range_data *data = (struct range_data *)user;
457
4
  isl_ctx *ctx;
458
4
  unsigned nparam = isl_basic_set_dim(bset, isl_dim_param);
459
4
  unsigned dim = isl_basic_set_dim(bset, isl_dim_set);
460
4
  isl_stat r;
461
4
462
4
  data->signs = NULL;
463
4
464
4
  ctx = isl_basic_set_get_ctx(bset);
465
4
  data->signs = isl_alloc_array(ctx, int,
466
4
          isl_basic_set_dim(bset, isl_dim_all));
467
4
468
4
  if (isl_basic_set_dims_get_sign(bset, isl_dim_set, 0, dim,
469
4
          data->signs + nparam) < 0)
470
0
    goto error;
471
4
  if (isl_basic_set_dims_get_sign(bset, isl_dim_param, 0, nparam,
472
4
          data->signs) < 0)
473
0
    goto error;
474
4
475
4
  r = propagate_on_domain(bset, isl_qpolynomial_copy(data->poly), data);
476
4
477
4
  free(data->signs);
478
4
479
4
  return r;
480
0
error:
481
0
  free(data->signs);
482
0
  isl_basic_set_free(bset);
483
0
  return isl_stat_error;
484
4
}
485
486
static isl_stat qpolynomial_bound_on_domain_range(
487
  __isl_take isl_basic_set *bset, __isl_take isl_qpolynomial *poly,
488
  struct range_data *data)
489
1
{
490
1
  unsigned nparam = isl_basic_set_dim(bset, isl_dim_param);
491
1
  unsigned nvar = isl_basic_set_dim(bset, isl_dim_set);
492
1
  isl_set *set = NULL;
493
1
494
1
  if (!bset)
495
0
    goto error;
496
1
497
1
  if (nvar == 0)
498
0
    return add_guarded_poly(bset, poly, data);
499
1
500
1
  set = isl_set_from_basic_set(bset);
501
1
  set = isl_set_split_dims(set, isl_dim_param, 0, nparam);
502
1
  set = isl_set_split_dims(set, isl_dim_set, 0, nvar);
503
1
504
1
  data->poly = poly;
505
1
506
1
  data->test_monotonicity = 1;
507
1
  if (isl_set_foreach_basic_set(set, &basic_guarded_poly_bound, data) < 0)
508
0
    goto error;
509
1
510
1
  isl_set_free(set);
511
1
  isl_qpolynomial_free(poly);
512
1
513
1
  return isl_stat_ok;
514
0
error:
515
0
  isl_set_free(set);
516
0
  isl_qpolynomial_free(poly);
517
0
  return isl_stat_error;
518
1
}
519
520
isl_stat isl_qpolynomial_bound_on_domain_range(__isl_take isl_basic_set *bset,
521
  __isl_take isl_qpolynomial *poly, struct isl_bound *bound)
522
1
{
523
1
  struct range_data data;
524
1
  isl_stat r;
525
1
526
1
  data.pwf = bound->pwf;
527
1
  data.pwf_tight = bound->pwf_tight;
528
1
  data.tight = bound->check_tight;
529
1
  if (bound->type == isl_fold_min)
530
0
    data.sign = -1;
531
1
  else
532
1
    data.sign = 1;
533
1
534
1
  r = qpolynomial_bound_on_domain_range(bset, poly, &data);
535
1
536
1
  bound->pwf = data.pwf;
537
1
  bound->pwf_tight = data.pwf_tight;
538
1
539
1
  return r;
540
1
}