Coverage Report

Created: 2017-10-03 07:32

/Users/buildslave/jenkins/sharedspace/clang-stage2-coverage-R@2/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
112k
{
24
112k
  isl_factorizer *f = NULL;
25
112k
  int *len = NULL;
26
112k
27
112k
  if (!morph)
28
0
    return NULL;
29
112k
30
112k
  
if (112k
n_group > 0112k
) {
31
44.4k
    len = isl_alloc_array(morph->dom->ctx, int, n_group);
32
44.4k
    if (!len)
33
0
      goto error;
34
112k
  }
35
112k
36
112k
  
f = 112k
isl_alloc_type112k
(morph->dom->ctx, struct isl_factorizer);
37
112k
  if (!f)
38
0
    goto error;
39
112k
40
112k
  f->morph = morph;
41
112k
  f->n_group = n_group;
42
112k
  f->len = len;
43
112k
44
112k
  return f;
45
0
error:
46
0
  free(len);
47
0
  isl_morph_free(morph);
48
0
  return NULL;
49
112k
}
50
51
void isl_factorizer_free(__isl_take isl_factorizer *f)
52
112k
{
53
112k
  if (!f)
54
0
    return;
55
112k
56
112k
  isl_morph_free(f->morph);
57
112k
  free(f->len);
58
112k
  free(f);
59
112k
}
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_group0
;
++i0
) {
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
68.4k
{
80
68.4k
  return isl_factorizer_alloc(isl_morph_identity(bset), 0);
81
68.4k
}
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
44.4k
{
86
44.4k
  int i;
87
44.4k
  unsigned nvar;
88
44.4k
  unsigned ovar;
89
44.4k
  isl_space *dim;
90
44.4k
  isl_basic_set *dom;
91
44.4k
  isl_basic_set *ran;
92
44.4k
  isl_morph *morph;
93
44.4k
  isl_factorizer *f;
94
44.4k
  isl_mat *id;
95
44.4k
96
44.4k
  if (
!bset || 44.4k
!Q44.4k
||
!U44.4k
)
97
0
    goto error;
98
44.4k
99
44.4k
  ovar = 1 + isl_space_offset(bset->dim, isl_dim_set);
100
44.4k
  id = isl_mat_identity(bset->ctx, ovar);
101
44.4k
  Q = isl_mat_diagonal(isl_mat_copy(id), Q);
102
44.4k
  U = isl_mat_diagonal(id, U);
103
44.4k
104
44.4k
  nvar = isl_basic_set_dim(bset, isl_dim_set);
105
44.4k
  dim = isl_basic_set_get_space(bset);
106
44.4k
  dom = isl_basic_set_universe(isl_space_copy(dim));
107
44.4k
  dim = isl_space_drop_dims(dim, isl_dim_set, 0, nvar);
108
44.4k
  dim = isl_space_add_dims(dim, isl_dim_set, nvar);
109
44.4k
  ran = isl_basic_set_universe(dim);
110
44.4k
  morph = isl_morph_alloc(dom, ran, Q, U);
111
44.4k
  f = isl_factorizer_alloc(morph, n);
112
44.4k
  if (!f)
113
0
    return NULL;
114
217k
  
for (i = 0; 44.4k
i < n217k
;
++i173k
)
115
173k
    f->len[i] = len[i];
116
44.4k
  return f;
117
0
error:
118
0
  isl_mat_free(Q);
119
0
  isl_mat_free(U);
120
0
  return NULL;
121
44.4k
}
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
112k
{
136
112k
  int i, j;
137
112k
138
112k
  if (!H)
139
0
    return -1;
140
112k
141
112k
  
g->pos = 112k
isl_alloc_array112k
(H->ctx, int, H->n_col);
142
112k
  g->group = isl_alloc_array(H->ctx, int, H->n_col);
143
112k
  g->cnt = isl_alloc_array(H->ctx, int, H->n_col);
144
112k
  g->rowgroup = isl_alloc_array(H->ctx, int, H->n_row);
145
112k
146
112k
  if (
!g->pos || 112k
!g->group112k
||
!g->cnt112k
||
!g->rowgroup112k
)
147
0
    return -1;
148
112k
149
1.07M
  
for (i = 0; 112k
i < H->n_row1.07M
;
++i958k
)
150
958k
    g->rowgroup[i] = -1;
151
514k
  for (i = 0, j = 0; 
i < H->n_col514k
;
++i401k
) {
152
956k
    for ( ; 
j < H->n_row956k
;
++j555k
)
153
956k
      
if (956k
!956k
isl_int_is_zero956k
(H->row[j][i]))
154
401k
        break;
155
401k
    g->pos[i] = j;
156
401k
  }
157
514k
  for (i = 0; 
i < H->n_col514k
;
++i401k
) {
158
401k
    g->group[i] = i;
159
401k
    g->cnt[i] = 1;
160
401k
  }
161
112k
162
112k
  return 0;
163
112k
}
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.89M
{
173
1.89M
  int p = g->group[k];
174
1.89M
  while (g->cnt[p] == 0)
175
13
    p = g->group[p];
176
1.89M
  g->group[k] = p;
177
1.89M
}
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
558k
{
187
558k
  int k;
188
558k
189
558k
  g->rowgroup[j] = g->group[i];
190
1.36M
  for (k = i + 1; 
k < H->n_col && 1.36M
j >= g->pos[k]1.09M
;
++k805k
) {
191
805k
    update_group(g, k);
192
805k
    update_group(g, i);
193
805k
    if (g->group[k] != g->group[i] &&
194
805k
        
!532k
isl_int_is_zero532k
(H->row[j][k])) {
195
159k
      isl_assert(H->ctx, g->cnt[g->group[k]] != 0, return -1);
196
159k
      
isl_assert159k
(H->ctx, g->cnt[g->group[i]] != 0, return -1);
197
159k
      
if (159k
g->group[i] < g->group[k]159k
) {
198
159k
        g->cnt[g->group[i]] += g->cnt[g->group[k]];
199
159k
        g->cnt[g->group[k]] = 0;
200
159k
        g->group[g->group[k]] = g->group[i];
201
159k
      } else {
202
618
        g->cnt[g->group[k]] += g->cnt[g->group[i]];
203
618
        g->cnt[g->group[i]] = 0;
204
618
        g->group[g->group[i]] = g->group[k];
205
618
      }
206
159k
    }
207
805k
  }
208
558k
209
558k
  return 0;
210
558k
}
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
112k
{
216
112k
  int i, j;
217
112k
218
426k
  for (i = 0; 
i < H->n_col && 426k
g->cnt[0] < H->n_col382k
;
++i313k
) {
219
313k
    if (g->pos[i] == H->n_row)
220
0
      continue; /* A line direction */
221
313k
    
if (313k
g->rowgroup[g->pos[i]] == -1313k
)
222
276k
      g->rowgroup[g->pos[i]] = i;
223
4.61M
    for (j = g->pos[i] + 1; 
j < H->n_row4.61M
;
++j4.29M
) {
224
4.29M
      if (isl_int_is_zero(H->row[j][i]))
225
3.61M
        continue;
226
680k
      
if (680k
g->rowgroup[j] != -1680k
)
227
122k
        continue;
228
558k
      
if (558k
update_group_i_with_row_j(g, i, j, H) < 0558k
)
229
0
        return -1;
230
4.29M
    }
231
313k
  }
232
401k
  
for (i = 1; 112k
i < H->n_col401k
;
++i288k
)
233
288k
    update_group(g, i);
234
112k
235
112k
  return 0;
236
112k
}
237
238
static void clear_groups(struct isl_factor_groups *g)
239
112k
{
240
112k
  if (!g)
241
0
    return;
242
112k
  free(g->pos);
243
112k
  free(g->group);
244
112k
  free(g->cnt);
245
112k
  free(g->rowgroup);
246
112k
}
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
112k
{
259
112k
  int i, j, n, done;
260
112k
  isl_mat *H, *U, *Q;
261
112k
  unsigned nvar;
262
112k
  struct isl_factor_groups g = { 0 };
263
112k
  isl_factorizer *f;
264
112k
265
112k
  if (!bset)
266
0
    return NULL;
267
112k
268
112k
  
isl_assert112k
(bset->ctx, isl_basic_set_dim(bset, isl_dim_div) == 0,
269
112k
    return NULL);
270
112k
271
112k
  nvar = isl_basic_set_dim(bset, isl_dim_set);
272
112k
  if (nvar <= 1)
273
1
    return isl_factorizer_identity(bset);
274
112k
275
112k
  H = isl_mat_alloc(bset->ctx, bset->n_eq + bset->n_ineq, nvar);
276
112k
  if (!H)
277
0
    return NULL;
278
112k
  isl_mat_sub_copy(bset->ctx, H->row, bset->eq, bset->n_eq,
279
112k
    0, 1 + isl_space_offset(bset->dim, isl_dim_set), nvar);
280
112k
  isl_mat_sub_copy(bset->ctx, H->row + bset->n_eq, bset->ineq, bset->n_ineq,
281
112k
    0, 1 + isl_space_offset(bset->dim, isl_dim_set), nvar);
282
112k
  H = isl_mat_left_hermite(H, 0, &U, &Q);
283
112k
284
112k
  if (init_groups(&g, H) < 0)
285
0
    goto error;
286
112k
  
if (112k
update_groups(&g, H) < 0112k
)
287
0
    goto error;
288
112k
289
112k
  
if (112k
g.cnt[0] == nvar112k
) {
290
68.4k
    isl_mat_free(H);
291
68.4k
    isl_mat_free(U);
292
68.4k
    isl_mat_free(Q);
293
68.4k
    clear_groups(&g);
294
68.4k
295
68.4k
    return isl_factorizer_identity(bset);
296
68.4k
  }
297
44.4k
298
44.4k
  done = 0;
299
44.4k
  n = 0;
300
217k
  while (
done != nvar217k
) {
301
173k
    int group = g.group[done];
302
243k
    for (i = 1; 
i < g.cnt[group]243k
;
++i69.7k
) {
303
69.7k
      if (g.group[done + i] == group)
304
39.1k
        continue;
305
95.9k
      
for (j = done + g.cnt[group]; 30.6k
j < nvar95.9k
;
++j65.3k
)
306
95.9k
        
if (95.9k
g.group[j] == group95.9k
)
307
30.6k
          break;
308
30.6k
      if (j == nvar)
309
0
        isl_die(bset->ctx, isl_error_internal,
310
30.6k
          "internal error", goto error);
311
30.6k
      g.group[j] = g.group[done + i];
312
30.6k
      Q = isl_mat_swap_rows(Q, done + i, j);
313
30.6k
      U = isl_mat_swap_cols(U, done + i, j);
314
30.6k
    }
315
173k
    done += g.cnt[group];
316
173k
    g.pos[n++] = g.cnt[group];
317
173k
  }
318
44.4k
319
44.4k
  f = isl_factorizer_groups(bset, Q, U, n, g.pos);
320
44.4k
321
44.4k
  isl_mat_free(H);
322
44.4k
  clear_groups(&g);
323
44.4k
324
44.4k
  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
112k
}