Coverage Report

Created: 2017-04-28 04:32

/Users/buildslave/jenkins/sharedspace/clang-stage2-coverage-R@2/llvm/tools/polly/lib/External/isl/isl_lp.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2008-2009 Katholieke Universiteit Leuven
3
 *
4
 * Use of this software is governed by the MIT license
5
 *
6
 * Written by Sven Verdoolaege, K.U.Leuven, Departement
7
 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
8
 */
9
10
#include <isl_ctx_private.h>
11
#include <isl_map_private.h>
12
#include <isl/lp.h>
13
#include <isl_seq.h>
14
#include "isl_tab.h"
15
#include <isl_options_private.h>
16
#include <isl_local_space_private.h>
17
#include <isl_aff_private.h>
18
#include <isl_mat_private.h>
19
#include <isl_val_private.h>
20
#include <isl_vec_private.h>
21
22
#include <bset_to_bmap.c>
23
#include <set_to_map.c>
24
25
enum isl_lp_result isl_tab_solve_lp(struct isl_basic_map *bmap, int maximize,
26
              isl_int *f, isl_int denom, isl_int *opt,
27
              isl_int *opt_denom,
28
              struct isl_vec **sol)
29
23.9k
{
30
23.9k
  struct isl_tab *tab;
31
23.9k
  enum isl_lp_result res;
32
23.9k
  unsigned dim = isl_basic_map_total_dim(bmap);
33
23.9k
34
23.9k
  if (maximize)
35
295
    isl_seq_neg(f, f, 1 + dim);
36
23.9k
37
23.9k
  bmap = isl_basic_map_gauss(bmap, NULL);
38
23.9k
  tab = isl_tab_from_basic_map(bmap, 0);
39
23.9k
  res = isl_tab_min(tab, f, denom, opt, opt_denom, 0);
40
23.9k
  if (
res == isl_lp_ok && 23.9k
sol23.0k
)
{913
41
913
    *sol = isl_tab_get_sample_value(tab);
42
913
    if (!*sol)
43
0
      res = isl_lp_error;
44
913
  }
45
23.9k
  isl_tab_free(tab);
46
23.9k
47
23.9k
  if (maximize)
48
295
    isl_seq_neg(f, f, 1 + dim);
49
23.9k
  if (
maximize && 23.9k
opt295
)
50
295
    isl_int_neg(*opt, *opt);
51
23.9k
52
23.9k
  return res;
53
23.9k
}
54
55
/* Given a basic map "bmap" and an affine combination of the variables "f"
56
 * with denominator "denom", set *opt / *opt_denom to the minimal
57
 * (or maximal if "maximize" is true) value attained by f/d over "bmap",
58
 * assuming the basic map is not empty and the expression cannot attain
59
 * arbitrarily small (or large) values.
60
 * If opt_denom is NULL, then *opt is rounded up (or down)
61
 * to the nearest integer.
62
 * The return value reflects the nature of the result (empty, unbounded,
63
 * minimal or maximal value returned in *opt).
64
 */
65
enum isl_lp_result isl_basic_map_solve_lp(struct isl_basic_map *bmap, int max,
66
              isl_int *f, isl_int d, isl_int *opt,
67
              isl_int *opt_denom,
68
              struct isl_vec **sol)
69
23.9k
{
70
23.9k
  if (sol)
71
1.30k
    *sol = NULL;
72
23.9k
73
23.9k
  if (!bmap)
74
0
    return isl_lp_error;
75
23.9k
76
23.9k
  return isl_tab_solve_lp(bmap, max, f, d, opt, opt_denom, sol);
77
23.9k
}
78
79
enum isl_lp_result isl_basic_set_solve_lp(struct isl_basic_set *bset, int max,
80
              isl_int *f, isl_int d, isl_int *opt,
81
              isl_int *opt_denom,
82
              struct isl_vec **sol)
83
23.6k
{
84
23.6k
  return isl_basic_map_solve_lp(bset_to_bmap(bset), max,
85
23.6k
          f, d, opt, opt_denom, sol);
86
23.6k
}
87
88
enum isl_lp_result isl_map_solve_lp(__isl_keep isl_map *map, int max,
89
              isl_int *f, isl_int d, isl_int *opt,
90
              isl_int *opt_denom,
91
              struct isl_vec **sol)
92
4
{
93
4
  int i;
94
4
  isl_int o;
95
4
  isl_int t;
96
4
  isl_int opt_i;
97
4
  isl_int opt_denom_i;
98
4
  enum isl_lp_result res;
99
4
  int max_div;
100
4
  isl_vec *v = NULL;
101
4
102
4
  if (!map)
103
0
    return isl_lp_error;
104
4
  
if (4
map->n == 04
)
105
0
    return isl_lp_empty;
106
4
107
4
  max_div = 0;
108
8
  for (i = 0; 
i < map->n8
;
++i4
)
109
4
    
if (4
map->p[i]->n_div > max_div4
)
110
0
      max_div = map->p[i]->n_div;
111
4
  if (
max_div > 04
)
{0
112
0
    unsigned total = isl_space_dim(map->dim, isl_dim_all);
113
0
    v = isl_vec_alloc(map->ctx, 1 + total + max_div);
114
0
    if (!v)
115
0
      return isl_lp_error;
116
0
    isl_seq_cpy(v->el, f, 1 + total);
117
0
    isl_seq_clr(v->el + 1 + total, max_div);
118
0
    f = v->el;
119
0
  }
120
4
121
4
  
if (4
!opt && 4
map->n > 10
&&
sol0
)
{0
122
0
    isl_int_init(o);
123
0
    opt = &o;
124
0
  }
125
4
  if (map->n > 0)
126
4
    isl_int_init(opt_i);
127
4
  if (
map->n > 0 && 4
opt_denom4
)
{0
128
0
    isl_int_init(opt_denom_i);
129
0
    isl_int_init(t);
130
0
  }
131
4
132
4
  res = isl_basic_map_solve_lp(map->p[0], max, f, d,
133
4
          opt, opt_denom, sol);
134
4
  if (
res == isl_lp_error || 4
res == isl_lp_unbounded4
)
135
0
    goto done;
136
4
137
4
  
if (4
sol4
)
138
0
    *sol = NULL;
139
4
140
4
  for (i = 1; 
i < map->n4
;
++i0
)
{0
141
0
    isl_vec *sol_i = NULL;
142
0
    enum isl_lp_result res_i;
143
0
    int better;
144
0
145
0
    res_i = isl_basic_map_solve_lp(map->p[i], max, f, d,
146
0
              &opt_i,
147
0
              opt_denom ? &opt_denom_i : NULL,
148
0
              sol ? &sol_i : NULL);
149
0
    if (
res_i == isl_lp_error || 0
res_i == isl_lp_unbounded0
)
{0
150
0
      res = res_i;
151
0
      goto done;
152
0
    }
153
0
    
if (0
res_i == isl_lp_empty0
)
154
0
      continue;
155
0
    
if (0
res == isl_lp_empty0
)
{0
156
0
      better = 1;
157
0
    } else 
if (0
!opt_denom0
)
{0
158
0
      if (max)
159
0
        
better = 0
isl_int_gt0
(opt_i, *opt);
160
0
      else
161
0
        
better = 0
isl_int_lt0
(opt_i, *opt);
162
0
    } else {
163
0
      isl_int_mul(t, opt_i, *opt_denom);
164
0
      isl_int_submul(t, *opt, opt_denom_i);
165
0
      if (max)
166
0
        
better = 0
isl_int_is_pos0
(t);
167
0
      else
168
0
        
better = 0
isl_int_is_neg0
(t);
169
0
    }
170
0
    if (
better0
)
{0
171
0
      res = res_i;
172
0
      if (opt)
173
0
        isl_int_set(*opt, opt_i);
174
0
      if (opt_denom)
175
0
        isl_int_set(*opt_denom, opt_denom_i);
176
0
      if (
sol0
)
{0
177
0
        isl_vec_free(*sol);
178
0
        *sol = sol_i;
179
0
      }
180
0
    } else
181
0
      isl_vec_free(sol_i);
182
0
  }
183
4
184
4
done:
185
4
  isl_vec_free(v);
186
4
  if (
map->n > 0 && 4
opt_denom4
)
{0
187
0
    isl_int_clear(opt_denom_i);
188
0
    isl_int_clear(t);
189
0
  }
190
4
  if (map->n > 0)
191
4
    isl_int_clear(opt_i);
192
4
  if (opt == &o)
193
0
    isl_int_clear(o);
194
4
  return res;
195
4
}
196
197
enum isl_lp_result isl_set_solve_lp(__isl_keep isl_set *set, int max,
198
              isl_int *f, isl_int d, isl_int *opt,
199
              isl_int *opt_denom,
200
              struct isl_vec **sol)
201
4
{
202
4
  return isl_map_solve_lp(set_to_map(set), max,
203
4
          f, d, opt, opt_denom, sol);
204
4
}
205
206
/* Return the optimal (rational) value of "obj" over "bset", assuming
207
 * that "obj" and "bset" have aligned parameters and divs.
208
 * If "max" is set, then the maximal value is computed.
209
 * Otherwise, the minimal value is computed.
210
 *
211
 * Return infinity or negative infinity if the optimal value is unbounded and
212
 * NaN if "bset" is empty.
213
 *
214
 * Call isl_basic_set_solve_lp and translate the results.
215
 */
216
static __isl_give isl_val *basic_set_opt_lp(
217
  __isl_keep isl_basic_set *bset, int max, __isl_keep isl_aff *obj)
218
0
{
219
0
  isl_ctx *ctx;
220
0
  isl_val *res;
221
0
  enum isl_lp_result lp_res;
222
0
223
0
  if (
!bset || 0
!obj0
)
224
0
    return NULL;
225
0
226
0
  ctx = isl_aff_get_ctx(obj);
227
0
  res = isl_val_alloc(ctx);
228
0
  if (!res)
229
0
    return NULL;
230
0
  lp_res = isl_basic_set_solve_lp(bset, max, obj->v->el + 1,
231
0
          obj->v->el[0], &res->n, &res->d, NULL);
232
0
  if (lp_res == isl_lp_ok)
233
0
    return isl_val_normalize(res);
234
0
  isl_val_free(res);
235
0
  if (lp_res == isl_lp_error)
236
0
    return NULL;
237
0
  
if (0
lp_res == isl_lp_empty0
)
238
0
    return isl_val_nan(ctx);
239
0
  
if (0
max0
)
240
0
    return isl_val_infty(ctx);
241
0
  else
242
0
    return isl_val_neginfty(ctx);
243
0
}
244
245
/* Return the optimal (rational) value of "obj" over "bset", assuming
246
 * that "obj" and "bset" have aligned parameters.
247
 * If "max" is set, then the maximal value is computed.
248
 * Otherwise, the minimal value is computed.
249
 *
250
 * Return infinity or negative infinity if the optimal value is unbounded and
251
 * NaN if "bset" is empty.
252
 *
253
 * Align the divs of "bset" and "obj" and call basic_set_opt_lp.
254
 */
255
static __isl_give isl_val *isl_basic_set_opt_lp_val_aligned(
256
  __isl_keep isl_basic_set *bset, int max, __isl_keep isl_aff *obj)
257
0
{
258
0
  int *exp1 = NULL;
259
0
  int *exp2 = NULL;
260
0
  isl_ctx *ctx;
261
0
  isl_mat *bset_div = NULL;
262
0
  isl_mat *div = NULL;
263
0
  isl_val *res;
264
0
  int bset_n_div, obj_n_div;
265
0
266
0
  if (
!bset || 0
!obj0
)
267
0
    return NULL;
268
0
269
0
  ctx = isl_aff_get_ctx(obj);
270
0
  if (!isl_space_is_equal(bset->dim, obj->ls->dim))
271
0
    isl_die(ctx, isl_error_invalid,
272
0
      "spaces don't match", return NULL);
273
0
274
0
  bset_n_div = isl_basic_set_dim(bset, isl_dim_div);
275
0
  obj_n_div = isl_aff_dim(obj, isl_dim_div);
276
0
  if (
bset_n_div == 0 && 0
obj_n_div == 00
)
277
0
    return basic_set_opt_lp(bset, max, obj);
278
0
279
0
  bset = isl_basic_set_copy(bset);
280
0
  obj = isl_aff_copy(obj);
281
0
282
0
  bset_div = isl_basic_set_get_divs(bset);
283
0
  exp1 = isl_alloc_array(ctx, int, bset_n_div);
284
0
  exp2 = isl_alloc_array(ctx, int, obj_n_div);
285
0
  if (
!bset_div || 0
(bset_n_div && 0
!exp10
) ||
(obj_n_div && 0
!exp20
))
286
0
    goto error;
287
0
288
0
  div = isl_merge_divs(bset_div, obj->ls->div, exp1, exp2);
289
0
290
0
  bset = isl_basic_set_expand_divs(bset, isl_mat_copy(div), exp1);
291
0
  obj = isl_aff_expand_divs(obj, isl_mat_copy(div), exp2);
292
0
293
0
  res = basic_set_opt_lp(bset, max, obj);
294
0
295
0
  isl_mat_free(bset_div);
296
0
  isl_mat_free(div);
297
0
  free(exp1);
298
0
  free(exp2);
299
0
  isl_basic_set_free(bset);
300
0
  isl_aff_free(obj);
301
0
302
0
  return res;
303
0
error:
304
0
  isl_mat_free(div);
305
0
  isl_mat_free(bset_div);
306
0
  free(exp1);
307
0
  free(exp2);
308
0
  isl_basic_set_free(bset);
309
0
  isl_aff_free(obj);
310
0
  return NULL;
311
0
}
312
313
/* Return the optimal (rational) value of "obj" over "bset".
314
 * If "max" is set, then the maximal value is computed.
315
 * Otherwise, the minimal value is computed.
316
 *
317
 * Return infinity or negative infinity if the optimal value is unbounded and
318
 * NaN if "bset" is empty.
319
 */
320
static __isl_give isl_val *isl_basic_set_opt_lp_val(
321
  __isl_keep isl_basic_set *bset, int max, __isl_keep isl_aff *obj)
322
0
{
323
0
  isl_bool equal;
324
0
  isl_val *res;
325
0
326
0
  if (
!bset || 0
!obj0
)
327
0
    return NULL;
328
0
329
0
  equal = isl_basic_set_space_has_equal_params(bset, obj->ls->dim);
330
0
  if (equal < 0)
331
0
    return NULL;
332
0
  
if (0
equal0
)
333
0
    return isl_basic_set_opt_lp_val_aligned(bset, max, obj);
334
0
335
0
  bset = isl_basic_set_copy(bset);
336
0
  obj = isl_aff_copy(obj);
337
0
  bset = isl_basic_set_align_params(bset, isl_aff_get_domain_space(obj));
338
0
  obj = isl_aff_align_params(obj, isl_basic_set_get_space(bset));
339
0
340
0
  res = isl_basic_set_opt_lp_val_aligned(bset, max, obj);
341
0
342
0
  isl_basic_set_free(bset);
343
0
  isl_aff_free(obj);
344
0
345
0
  return res;
346
0
}
347
348
/* Return the minimal (rational) value of "obj" over "bset".
349
 *
350
 * Return negative infinity if the minimal value is unbounded and
351
 * NaN if "bset" is empty.
352
 */
353
__isl_give isl_val *isl_basic_set_min_lp_val(__isl_keep isl_basic_set *bset,
354
  __isl_keep isl_aff *obj)
355
0
{
356
0
  return isl_basic_set_opt_lp_val(bset, 0, obj);
357
0
}
358
359
/* Return the maximal (rational) value of "obj" over "bset".
360
 *
361
 * Return infinity if the maximal value is unbounded and
362
 * NaN if "bset" is empty.
363
 */
364
__isl_give isl_val *isl_basic_set_max_lp_val(__isl_keep isl_basic_set *bset,
365
  __isl_keep isl_aff *obj)
366
0
{
367
0
  return isl_basic_set_opt_lp_val(bset, 1, obj);
368
0
}