Coverage Report

Created: 2017-04-27 19:33

/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
55.3k
{
24
55.3k
  isl_factorizer *f = NULL;
25
55.3k
  int *len = NULL;
26
55.3k
27
55.3k
  if (!morph)
28
0
    return NULL;
29
55.3k
30
55.3k
  
if (55.3k
n_group > 055.3k
)
{23.9k
31
23.9k
    len = isl_alloc_array(morph->dom->ctx, int, n_group);
32
23.9k
    if (!len)
33
0
      goto error;
34
23.9k
  }
35
55.3k
36
55.3k
  
f = 55.3k
isl_alloc_type55.3k
(morph->dom->ctx, struct isl_factorizer);
37
55.3k
  if (!f)
38
0
    goto error;
39
55.3k
40
55.3k
  f->morph = morph;
41
55.3k
  f->n_group = n_group;
42
55.3k
  f->len = len;
43
55.3k
44
55.3k
  return f;
45
0
error:
46
0
  free(len);
47
0
  isl_morph_free(morph);
48
0
  return NULL;
49
55.3k
}
50
51
void isl_factorizer_free(__isl_take isl_factorizer *f)
52
55.3k
{
53
55.3k
  if (!f)
54
0
    return;
55
55.3k
56
55.3k
  isl_morph_free(f->morph);
57
55.3k
  free(f->len);
58
55.3k
  free(f);
59
55.3k
}
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
)
{0
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
31.3k
{
80
31.3k
  return isl_factorizer_alloc(isl_morph_identity(bset), 0);
81
31.3k
}
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
23.9k
{
86
23.9k
  int i;
87
23.9k
  unsigned nvar;
88
23.9k
  unsigned ovar;
89
23.9k
  isl_space *dim;
90
23.9k
  isl_basic_set *dom;
91
23.9k
  isl_basic_set *ran;
92
23.9k
  isl_morph *morph;
93
23.9k
  isl_factorizer *f;
94
23.9k
  isl_mat *id;
95
23.9k
96
23.9k
  if (
!bset || 23.9k
!Q23.9k
||
!U23.9k
)
97
0
    goto error;
98
23.9k
99
23.9k
  ovar = 1 + isl_space_offset(bset->dim, isl_dim_set);
100
23.9k
  id = isl_mat_identity(bset->ctx, ovar);
101
23.9k
  Q = isl_mat_diagonal(isl_mat_copy(id), Q);
102
23.9k
  U = isl_mat_diagonal(id, U);
103
23.9k
104
23.9k
  nvar = isl_basic_set_dim(bset, isl_dim_set);
105
23.9k
  dim = isl_basic_set_get_space(bset);
106
23.9k
  dom = isl_basic_set_universe(isl_space_copy(dim));
107
23.9k
  dim = isl_space_drop_dims(dim, isl_dim_set, 0, nvar);
108
23.9k
  dim = isl_space_add_dims(dim, isl_dim_set, nvar);
109
23.9k
  ran = isl_basic_set_universe(dim);
110
23.9k
  morph = isl_morph_alloc(dom, ran, Q, U);
111
23.9k
  f = isl_factorizer_alloc(morph, n);
112
23.9k
  if (!f)
113
0
    return NULL;
114
113k
  
for (i = 0; 23.9k
i < n113k
;
++i89.5k
)
115
89.5k
    f->len[i] = len[i];
116
23.9k
  return f;
117
0
error:
118
0
  isl_mat_free(Q);
119
0
  isl_mat_free(U);
120
0
  return NULL;
121
23.9k
}
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
55.3k
{
136
55.3k
  int i, j;
137
55.3k
138
55.3k
  if (!H)
139
0
    return -1;
140
55.3k
141
55.3k
  
g->pos = 55.3k
isl_alloc_array55.3k
(H->ctx, int, H->n_col);
142
55.3k
  g->group = isl_alloc_array(H->ctx, int, H->n_col);
143
55.3k
  g->cnt = isl_alloc_array(H->ctx, int, H->n_col);
144
55.3k
  g->rowgroup = isl_alloc_array(H->ctx, int, H->n_row);
145
55.3k
146
55.3k
  if (
!g->pos || 55.3k
!g->group55.3k
||
!g->cnt55.3k
||
!g->rowgroup55.3k
)
147
0
    return -1;
148
55.3k
149
496k
  
for (i = 0; 55.3k
i < H->n_row496k
;
++i441k
)
150
441k
    g->rowgroup[i] = -1;
151
250k
  for (i = 0, j = 0; 
i < H->n_col250k
;
++i195k
)
{195k
152
457k
    for ( ; 
j < H->n_row457k
;
++j262k
)
153
457k
      
if (457k
!457k
isl_int_is_zero457k
(H->row[j][i]))
154
195k
        break;
155
195k
    g->pos[i] = j;
156
195k
  }
157
250k
  for (i = 0; 
i < H->n_col250k
;
++i195k
)
{195k
158
195k
    g->group[i] = i;
159
195k
    g->cnt[i] = 1;
160
195k
  }
161
55.3k
162
55.3k
  return 0;
163
55.3k
}
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
983k
{
173
983k
  int p = g->group[k];
174
983k
  while (g->cnt[p] == 0)
175
11
    p = g->group[p];
176
983k
  g->group[k] = p;
177
983k
}
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
243k
{
187
243k
  int k;
188
243k
189
243k
  g->rowgroup[j] = g->group[i];
190
665k
  for (k = i + 1; 
k < H->n_col && 665k
j >= g->pos[k]550k
;
++k421k
)
{421k
191
421k
    update_group(g, k);
192
421k
    update_group(g, i);
193
421k
    if (g->group[k] != g->group[i] &&
194
287k
        
!287k
isl_int_is_zero287k
(H->row[j][k]))
{74.0k
195
74.0k
      isl_assert(H->ctx, g->cnt[g->group[k]] != 0, return -1);
196
74.0k
      
isl_assert74.0k
(H->ctx, g->cnt[g->group[i]] != 0, return -1);74.0k
197
74.0k
      
if (74.0k
g->group[i] < g->group[k]74.0k
)
{73.4k
198
73.4k
        g->cnt[g->group[i]] += g->cnt[g->group[k]];
199
73.4k
        g->cnt[g->group[k]] = 0;
200
73.4k
        g->group[g->group[k]] = g->group[i];
201
600
      } else {
202
600
        g->cnt[g->group[k]] += g->cnt[g->group[i]];
203
600
        g->cnt[g->group[i]] = 0;
204
600
        g->group[g->group[i]] = g->group[k];
205
600
      }
206
74.0k
    }
207
421k
  }
208
243k
209
243k
  return 0;
210
243k
}
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
55.3k
{
216
55.3k
  int i, j;
217
55.3k
218
205k
  for (i = 0; 
i < H->n_col && 205k
g->cnt[0] < H->n_col181k
;
++i149k
)
{149k
219
149k
    if (g->pos[i] == H->n_row)
220
0
      continue; /* A line direction */
221
149k
    
if (149k
g->rowgroup[g->pos[i]] == -1149k
)
222
139k
      g->rowgroup[g->pos[i]] = i;
223
1.93M
    for (j = g->pos[i] + 1; 
j < H->n_row1.93M
;
++j1.78M
)
{1.78M
224
1.78M
      if (isl_int_is_zero(H->row[j][i]))
225
1.49M
        continue;
226
287k
      
if (287k
g->rowgroup[j] != -1287k
)
227
44.0k
        continue;
228
243k
      
if (243k
update_group_i_with_row_j(g, i, j, H) < 0243k
)
229
0
        return -1;
230
243k
    }
231
149k
  }
232
195k
  
for (i = 1; 55.3k
i < H->n_col195k
;
++i139k
)
233
139k
    update_group(g, i);
234
55.3k
235
55.3k
  return 0;
236
55.3k
}
237
238
static void clear_groups(struct isl_factor_groups *g)
239
55.3k
{
240
55.3k
  if (!g)
241
0
    return;
242
55.3k
  free(g->pos);
243
55.3k
  free(g->group);
244
55.3k
  free(g->cnt);
245
55.3k
  free(g->rowgroup);
246
55.3k
}
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
55.3k
{
259
55.3k
  int i, j, n, done;
260
55.3k
  isl_mat *H, *U, *Q;
261
55.3k
  unsigned nvar;
262
55.3k
  struct isl_factor_groups g = { 0 };
263
55.3k
  isl_factorizer *f;
264
55.3k
265
55.3k
  if (!bset)
266
0
    return NULL;
267
55.3k
268
55.3k
  
isl_assert55.3k
(bset->ctx, isl_basic_set_dim(bset, isl_dim_div) == 0,55.3k
269
55.3k
    return NULL);
270
55.3k
271
55.3k
  nvar = isl_basic_set_dim(bset, isl_dim_set);
272
55.3k
  if (nvar <= 1)
273
1
    return isl_factorizer_identity(bset);
274
55.3k
275
55.3k
  H = isl_mat_alloc(bset->ctx, bset->n_eq + bset->n_ineq, nvar);
276
55.3k
  if (!H)
277
0
    return NULL;
278
55.3k
  isl_mat_sub_copy(bset->ctx, H->row, bset->eq, bset->n_eq,
279
55.3k
    0, 1 + isl_space_offset(bset->dim, isl_dim_set), nvar);
280
55.3k
  isl_mat_sub_copy(bset->ctx, H->row + bset->n_eq, bset->ineq, bset->n_ineq,
281
55.3k
    0, 1 + isl_space_offset(bset->dim, isl_dim_set), nvar);
282
55.3k
  H = isl_mat_left_hermite(H, 0, &U, &Q);
283
55.3k
284
55.3k
  if (init_groups(&g, H) < 0)
285
0
    goto error;
286
55.3k
  
if (55.3k
update_groups(&g, H) < 055.3k
)
287
0
    goto error;
288
55.3k
289
55.3k
  
if (55.3k
g.cnt[0] == nvar55.3k
)
{31.3k
290
31.3k
    isl_mat_free(H);
291
31.3k
    isl_mat_free(U);
292
31.3k
    isl_mat_free(Q);
293
31.3k
    clear_groups(&g);
294
31.3k
295
31.3k
    return isl_factorizer_identity(bset);
296
31.3k
  }
297
55.3k
298
23.9k
  done = 0;
299
23.9k
  n = 0;
300
113k
  while (
done != nvar113k
)
{89.5k
301
89.5k
    int group = g.group[done];
302
116k
    for (i = 1; 
i < g.cnt[group]116k
;
++i27.2k
)
{27.2k
303
27.2k
      if (g.group[done + i] == group)
304
14.0k
        continue;
305
44.9k
      
for (j = done + g.cnt[group]; 13.2k
j < nvar44.9k
;
++j31.7k
)
306
44.9k
        
if (44.9k
g.group[j] == group44.9k
)
307
13.2k
          break;
308
13.2k
      if (j == nvar)
309
0
        isl_die(bset->ctx, isl_error_internal,
310
13.2k
          "internal error", goto error);
311
13.2k
      g.group[j] = g.group[done + i];
312
13.2k
      Q = isl_mat_swap_rows(Q, done + i, j);
313
13.2k
      U = isl_mat_swap_cols(U, done + i, j);
314
13.2k
    }
315
89.5k
    done += g.cnt[group];
316
89.5k
    g.pos[n++] = g.cnt[group];
317
89.5k
  }
318
23.9k
319
23.9k
  f = isl_factorizer_groups(bset, Q, U, n, g.pos);
320
23.9k
321
23.9k
  isl_mat_free(H);
322
23.9k
  clear_groups(&g);
323
23.9k
324
23.9k
  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
23.9k
}