Coverage Report

Created: 2018-12-09 11:54

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/polly/lib/External/isl/isl_factorization.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2005-2007 Universiteit Leiden
3
 * Copyright 2008-2009 Katholieke Universiteit Leuven
4
 * Copyright 2010      INRIA Saclay
5
 *
6
 * Use of this software is governed by the MIT license
7
 *
8
 * Written by Sven Verdoolaege, Leiden Institute of Advanced Computer Science,
9
 * Universiteit Leiden, Niels Bohrweg 1, 2333 CA Leiden, The Netherlands
10
 * and K.U.Leuven, Departement Computerwetenschappen, Celestijnenlaan 200A,
11
 * B-3001 Leuven, Belgium
12
 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
13
 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France 
14
 */
15
16
#include <isl_map_private.h>
17
#include <isl_factorization.h>
18
#include <isl_space_private.h>
19
#include <isl_mat_private.h>
20
21
static __isl_give isl_factorizer *isl_factorizer_alloc(
22
  __isl_take isl_morph *morph, int n_group)
23
109k
{
24
109k
  isl_factorizer *f = NULL;
25
109k
  int *len = NULL;
26
109k
27
109k
  if (!morph)
28
0
    return NULL;
29
109k
30
109k
  if (n_group > 0) {
31
45.6k
    len = isl_alloc_array(morph->dom->ctx, int, n_group);
32
45.6k
    if (!len)
33
0
      goto error;
34
109k
  }
35
109k
36
109k
  f = isl_alloc_type(morph->dom->ctx, struct isl_factorizer);
37
109k
  if (!f)
38
0
    goto error;
39
109k
40
109k
  f->morph = morph;
41
109k
  f->n_group = n_group;
42
109k
  f->len = len;
43
109k
44
109k
  return f;
45
0
error:
46
0
  free(len);
47
0
  isl_morph_free(morph);
48
0
  return NULL;
49
109k
}
50
51
void isl_factorizer_free(__isl_take isl_factorizer *f)
52
109k
{
53
109k
  if (!f)
54
0
    return;
55
109k
56
109k
  isl_morph_free(f->morph);
57
109k
  free(f->len);
58
109k
  free(f);
59
109k
}
60
61
void isl_factorizer_dump(__isl_take isl_factorizer *f)
62
0
{
63
0
  int i;
64
0
65
0
  if (!f)
66
0
    return;
67
0
68
0
  isl_morph_print_internal(f->morph, stderr);
69
0
  fprintf(stderr, "[");
70
0
  for (i = 0; i < f->n_group; ++i) {
71
0
    if (i)
72
0
      fprintf(stderr, ", ");
73
0
    fprintf(stderr, "%d", f->len[i]);
74
0
  }
75
0
  fprintf(stderr, "]\n");
76
0
}
77
78
__isl_give isl_factorizer *isl_factorizer_identity(__isl_keep isl_basic_set *bset)
79
63.7k
{
80
63.7k
  return isl_factorizer_alloc(isl_morph_identity(bset), 0);
81
63.7k
}
82
83
__isl_give isl_factorizer *isl_factorizer_groups(__isl_keep isl_basic_set *bset,
84
  __isl_take isl_mat *Q, __isl_take isl_mat *U, int n, int *len)
85
45.6k
{
86
45.6k
  int i;
87
45.6k
  unsigned nvar;
88
45.6k
  unsigned ovar;
89
45.6k
  isl_space *dim;
90
45.6k
  isl_basic_set *dom;
91
45.6k
  isl_basic_set *ran;
92
45.6k
  isl_morph *morph;
93
45.6k
  isl_factorizer *f;
94
45.6k
  isl_mat *id;
95
45.6k
96
45.6k
  if (!bset || !Q || !U)
97
0
    goto error;
98
45.6k
99
45.6k
  ovar = 1 + isl_space_offset(bset->dim, isl_dim_set);
100
45.6k
  id = isl_mat_identity(bset->ctx, ovar);
101
45.6k
  Q = isl_mat_diagonal(isl_mat_copy(id), Q);
102
45.6k
  U = isl_mat_diagonal(id, U);
103
45.6k
104
45.6k
  nvar = isl_basic_set_dim(bset, isl_dim_set);
105
45.6k
  dim = isl_basic_set_get_space(bset);
106
45.6k
  dom = isl_basic_set_universe(isl_space_copy(dim));
107
45.6k
  dim = isl_space_drop_dims(dim, isl_dim_set, 0, nvar);
108
45.6k
  dim = isl_space_add_dims(dim, isl_dim_set, nvar);
109
45.6k
  ran = isl_basic_set_universe(dim);
110
45.6k
  morph = isl_morph_alloc(dom, ran, Q, U);
111
45.6k
  f = isl_factorizer_alloc(morph, n);
112
45.6k
  if (!f)
113
0
    return NULL;
114
231k
  
for (i = 0; 45.6k
i < n;
++i185k
)
115
185k
    f->len[i] = len[i];
116
45.6k
  return f;
117
0
error:
118
0
  isl_mat_free(Q);
119
0
  isl_mat_free(U);
120
0
  return NULL;
121
45.6k
}
122
123
struct isl_factor_groups {
124
  int *pos;   /* for each column: row position of pivot */
125
  int *group;   /* group to which a column belongs */
126
  int *cnt;   /* number of columns in the group */
127
  int *rowgroup;    /* group to which a constraint belongs */
128
};
129
130
/* Initialize isl_factor_groups structure: find pivot row positions,
131
 * each column initially belongs to its own group and the groups
132
 * of the constraints are still unknown.
133
 */
134
static int init_groups(struct isl_factor_groups *g, __isl_keep isl_mat *H)
135
109k
{
136
109k
  int i, j;
137
109k
138
109k
  if (!H)
139
0
    return -1;
140
109k
141
109k
  g->pos = isl_alloc_array(H->ctx, int, H->n_col);
142
109k
  g->group = isl_alloc_array(H->ctx, int, H->n_col);
143
109k
  g->cnt = isl_alloc_array(H->ctx, int, H->n_col);
144
109k
  g->rowgroup = isl_alloc_array(H->ctx, int, H->n_row);
145
109k
146
109k
  if (!g->pos || !g->group || !g->cnt || !g->rowgroup)
147
0
    return -1;
148
109k
149
1.00M
  
for (i = 0; 109k
i < H->n_row;
++i891k
)
150
891k
    g->rowgroup[i] = -1;
151
516k
  for (i = 0, j = 0; i < H->n_col; 
++i406k
) {
152
942k
    for ( ; j < H->n_row; 
++j535k
)
153
942k
      if (!isl_int_is_zero(H->row[j][i]))
154
942k
        
break406k
;
155
406k
    g->pos[i] = j;
156
406k
  }
157
516k
  for (i = 0; i < H->n_col; 
++i406k
) {
158
406k
    g->group[i] = i;
159
406k
    g->cnt[i] = 1;
160
406k
  }
161
109k
162
109k
  return 0;
163
109k
}
164
165
/* Update group[k] to the group column k belongs to.
166
 * When merging two groups, only the group of the current
167
 * group leader is changed.  Here we change the group of
168
 * the other members to also point to the group that the
169
 * old group leader now points to.
170
 */
171
static void update_group(struct isl_factor_groups *g, int k)
172
1.79M
{
173
1.79M
  int p = g->group[k];
174
1.79M
  while (g->cnt[p] == 0)
175
58
    p = g->group[p];
176
1.79M
  g->group[k] = p;
177
1.79M
}
178
179
/* Merge group i with all groups of the subsequent columns
180
 * with non-zero coefficients in row j of H.
181
 * (The previous columns are all zero; otherwise we would have handled
182
 * the row before.)
183
 */
184
static int update_group_i_with_row_j(struct isl_factor_groups *g, int i, int j,
185
  __isl_keep isl_mat *H)
186
492k
{
187
492k
  int k;
188
492k
189
492k
  g->rowgroup[j] = g->group[i];
190
1.23M
  for (k = i + 1; k < H->n_col && 
j >= g->pos[k]1.00M
;
++k746k
) {
191
746k
    update_group(g, k);
192
746k
    update_group(g, i);
193
746k
    if (g->group[k] != g->group[i] &&
194
746k
        
!517k
isl_int_is_zero517k
(H->row[j][k])) {
195
156k
      isl_assert(H->ctx, g->cnt[g->group[k]] != 0, return -1);
196
156k
      isl_assert(H->ctx, g->cnt[g->group[i]] != 0, return -1);
197
156k
      if (g->group[i] < g->group[k]) {
198
156k
        g->cnt[g->group[i]] += g->cnt[g->group[k]];
199
156k
        g->cnt[g->group[k]] = 0;
200
156k
        g->group[g->group[k]] = g->group[i];
201
156k
      } else {
202
742
        g->cnt[g->group[k]] += g->cnt[g->group[i]];
203
742
        g->cnt[g->group[i]] = 0;
204
742
        g->group[g->group[i]] = g->group[k];
205
742
      }
206
156k
    }
207
746k
  }
208
492k
209
492k
  return 0;
210
492k
}
211
212
/* Update the group information based on the constraint matrix.
213
 */
214
static int update_groups(struct isl_factor_groups *g, __isl_keep isl_mat *H)
215
109k
{
216
109k
  int i, j;
217
109k
218
429k
  for (i = 0; i < H->n_col && 
g->cnt[0] < H->n_col384k
;
++i320k
) {
219
320k
    if (g->pos[i] == H->n_row)
220
0
      continue; /* A line direction */
221
320k
    if (g->rowgroup[g->pos[i]] == -1)
222
295k
      g->rowgroup[g->pos[i]] = i;
223
4.78M
    for (j = g->pos[i] + 1; j < H->n_row; 
++j4.46M
) {
224
4.46M
      if (isl_int_is_zero(H->row[j][i]))
225
4.46M
        
continue3.87M
;
226
593k
      if (g->rowgroup[j] != -1)
227
100k
        continue;
228
492k
      if (update_group_i_with_row_j(g, i, j, H) < 0)
229
0
        return -1;
230
492k
    }
231
320k
  }
232
406k
  
for (i = 1; 109k
i < H->n_col;
++i297k
)
233
297k
    update_group(g, i);
234
109k
235
109k
  return 0;
236
109k
}
237
238
static void clear_groups(struct isl_factor_groups *g)
239
109k
{
240
109k
  if (!g)
241
0
    return;
242
109k
  free(g->pos);
243
109k
  free(g->group);
244
109k
  free(g->cnt);
245
109k
  free(g->rowgroup);
246
109k
}
247
248
/* Determine if the set variables of the basic set can be factorized and
249
 * return the results in an isl_factorizer.
250
 *
251
 * The algorithm works by first computing the Hermite normal form
252
 * and then grouping columns linked by one or more constraints together,
253
 * where a constraints "links" two or more columns if the constraint
254
 * has nonzero coefficients in the columns.
255
 */
256
__isl_give isl_factorizer *isl_basic_set_factorizer(
257
  __isl_keep isl_basic_set *bset)
258
109k
{
259
109k
  int i, j, n, done;
260
109k
  isl_mat *H, *U, *Q;
261
109k
  unsigned nvar;
262
109k
  struct isl_factor_groups g = { 0 };
263
109k
  isl_factorizer *f;
264
109k
265
109k
  if (!bset)
266
0
    return NULL;
267
109k
268
109k
  isl_assert(bset->ctx, isl_basic_set_dim(bset, isl_dim_div) == 0,
269
109k
    return NULL);
270
109k
271
109k
  nvar = isl_basic_set_dim(bset, isl_dim_set);
272
109k
  if (nvar <= 1)
273
1
    return isl_factorizer_identity(bset);
274
109k
275
109k
  H = isl_mat_alloc(bset->ctx, bset->n_eq + bset->n_ineq, nvar);
276
109k
  if (!H)
277
0
    return NULL;
278
109k
  isl_mat_sub_copy(bset->ctx, H->row, bset->eq, bset->n_eq,
279
109k
    0, 1 + isl_space_offset(bset->dim, isl_dim_set), nvar);
280
109k
  isl_mat_sub_copy(bset->ctx, H->row + bset->n_eq, bset->ineq, bset->n_ineq,
281
109k
    0, 1 + isl_space_offset(bset->dim, isl_dim_set), nvar);
282
109k
  H = isl_mat_left_hermite(H, 0, &U, &Q);
283
109k
284
109k
  if (init_groups(&g, H) < 0)
285
0
    goto error;
286
109k
  if (update_groups(&g, H) < 0)
287
0
    goto error;
288
109k
289
109k
  if (g.cnt[0] == nvar) {
290
63.7k
    isl_mat_free(H);
291
63.7k
    isl_mat_free(U);
292
63.7k
    isl_mat_free(Q);
293
63.7k
    clear_groups(&g);
294
63.7k
295
63.7k
    return isl_factorizer_identity(bset);
296
63.7k
  }
297
45.6k
298
45.6k
  done = 0;
299
45.6k
  n = 0;
300
231k
  while (done != nvar) {
301
185k
    int group = g.group[done];
302
253k
    for (i = 1; i < g.cnt[group]; 
++i67.5k
) {
303
67.5k
      if (g.group[done + i] == group)
304
35.8k
        continue;
305
88.7k
      
for (j = done + g.cnt[group]; 31.6k
j < nvar;
++j57.0k
)
306
88.7k
        if (g.group[j] == group)
307
31.6k
          break;
308
31.6k
      if (j == nvar)
309
31.6k
        
isl_die0
(bset->ctx, isl_error_internal,
310
31.6k
          "internal error", goto error);
311
31.6k
      g.group[j] = g.group[done + i];
312
31.6k
      Q = isl_mat_swap_rows(Q, done + i, j);
313
31.6k
      U = isl_mat_swap_cols(U, done + i, j);
314
31.6k
    }
315
185k
    done += g.cnt[group];
316
185k
    g.pos[n++] = g.cnt[group];
317
185k
  }
318
45.6k
319
45.6k
  f = isl_factorizer_groups(bset, Q, U, n, g.pos);
320
45.6k
321
45.6k
  isl_mat_free(H);
322
45.6k
  clear_groups(&g);
323
45.6k
324
45.6k
  return f;
325
0
error:
326
0
  isl_mat_free(H);
327
0
  isl_mat_free(U);
328
0
  isl_mat_free(Q);
329
0
  clear_groups(&g);
330
0
  return NULL;
331
45.6k
}