Bug Summary

File:tools/polly/lib/External/isl/isl_mat.c
Warning:line 1559, column 13
Use of memory after it is freed

Annotated Source Code

1/*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2014 Ecole Normale Superieure
4 *
5 * Use of this software is governed by the MIT license
6 *
7 * Written by Sven Verdoolaege, K.U.Leuven, Departement
8 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
9 * and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
10 */
11
12#include <isl_ctx_private.h>
13#include <isl_map_private.h>
14#include <isl/space.h>
15#include <isl_seq.h>
16#include <isl_mat_private.h>
17#include <isl_vec_private.h>
18#include <isl_space_private.h>
19#include <isl_val_private.h>
20#include <isl/deprecated/mat_int.h>
21
22isl_ctx *isl_mat_get_ctx(__isl_keep isl_mat *mat)
23{
24 return mat ? mat->ctx : NULL((void*)0);
25}
26
27/* Return a hash value that digests "mat".
28 */
29uint32_t isl_mat_get_hash(__isl_keep isl_mat *mat)
30{
31 int i;
32 uint32_t hash;
33
34 if (!mat)
35 return 0;
36
37 hash = isl_hash_init()(2166136261u);
38 isl_hash_byte(hash, mat->n_row & 0xFF)do { hash *= 16777619; hash ^= mat->n_row & 0xFF; } while
(0)
;
39 isl_hash_byte(hash, mat->n_col & 0xFF)do { hash *= 16777619; hash ^= mat->n_col & 0xFF; } while
(0)
;
40 for (i = 0; i < mat->n_row; ++i) {
41 uint32_t row_hash;
42
43 row_hash = isl_seq_get_hash(mat->row[i], mat->n_col);
44 isl_hash_hash(hash, row_hash)do { do { hash *= 16777619; hash ^= (row_hash) & 0xFF; } while
(0); do { hash *= 16777619; hash ^= ((row_hash) >> 8) &
0xFF; } while(0); do { hash *= 16777619; hash ^= ((row_hash)
>> 16) & 0xFF; } while(0); do { hash *= 16777619; hash
^= ((row_hash) >> 24) & 0xFF; } while(0); } while(
0)
;
45 }
46
47 return hash;
48}
49
50struct isl_mat *isl_mat_alloc(struct isl_ctx *ctx,
51 unsigned n_row, unsigned n_col)
52{
53 int i;
54 struct isl_mat *mat;
55
56 mat = isl_alloc_type(ctx, struct isl_mat)((struct isl_mat *)isl_malloc_or_die(ctx, sizeof(struct isl_mat
)))
;
57 if (!mat)
58 return NULL((void*)0);
59
60 mat->row = NULL((void*)0);
61 mat->block = isl_blk_alloc(ctx, n_row * n_col);
62 if (isl_blk_is_error(mat->block))
63 goto error;
64 mat->row = isl_alloc_array(ctx, isl_int *, n_row)((isl_int * *)isl_malloc_or_die(ctx, (n_row)*sizeof(isl_int *
)))
;
65 if (n_row && !mat->row)
66 goto error;
67
68 for (i = 0; i < n_row; ++i)
69 mat->row[i] = mat->block.data + i * n_col;
70
71 mat->ctx = ctx;
72 isl_ctx_ref(ctx);
73 mat->ref = 1;
74 mat->n_row = n_row;
75 mat->n_col = n_col;
76 mat->max_col = n_col;
77 mat->flags = 0;
78
79 return mat;
80error:
81 isl_blk_free(ctx, mat->block);
82 free(mat);
83 return NULL((void*)0);
84}
85
86struct isl_mat *isl_mat_extend(struct isl_mat *mat,
87 unsigned n_row, unsigned n_col)
88{
89 int i;
90 isl_int *old;
91 isl_int **row;
92
93 if (!mat)
94 return NULL((void*)0);
95
96 if (mat->max_col >= n_col && mat->n_row >= n_row) {
97 if (mat->n_col < n_col)
98 mat->n_col = n_col;
99 return mat;
100 }
101
102 if (mat->max_col < n_col) {
103 struct isl_mat *new_mat;
104
105 if (n_row < mat->n_row)
106 n_row = mat->n_row;
107 new_mat = isl_mat_alloc(mat->ctx, n_row, n_col);
108 if (!new_mat)
109 goto error;
110 for (i = 0; i < mat->n_row; ++i)
111 isl_seq_cpy(new_mat->row[i], mat->row[i], mat->n_col);
112 isl_mat_free(mat);
113 return new_mat;
114 }
115
116 mat = isl_mat_cow(mat);
117 if (!mat)
118 goto error;
119
120 old = mat->block.data;
121 mat->block = isl_blk_extend(mat->ctx, mat->block, n_row * mat->max_col);
122 if (isl_blk_is_error(mat->block))
123 goto error;
124 row = isl_realloc_array(mat->ctx, mat->row, isl_int *, n_row)((isl_int * *)isl_realloc_or_die(mat->ctx, mat->row, (n_row
)*sizeof(isl_int *)))
;
125 if (n_row && !row)
126 goto error;
127 mat->row = row;
128
129 for (i = 0; i < mat->n_row; ++i)
130 mat->row[i] = mat->block.data + (mat->row[i] - old);
131 for (i = mat->n_row; i < n_row; ++i)
132 mat->row[i] = mat->block.data + i * mat->max_col;
133 mat->n_row = n_row;
134 if (mat->n_col < n_col)
135 mat->n_col = n_col;
136
137 return mat;
138error:
139 isl_mat_free(mat);
140 return NULL((void*)0);
141}
142
143__isl_give isl_mat *isl_mat_sub_alloc6(isl_ctx *ctx, isl_int **row,
144 unsigned first_row, unsigned n_row, unsigned first_col, unsigned n_col)
145{
146 int i;
147 struct isl_mat *mat;
148
149 mat = isl_alloc_type(ctx, struct isl_mat)((struct isl_mat *)isl_malloc_or_die(ctx, sizeof(struct isl_mat
)))
;
150 if (!mat)
151 return NULL((void*)0);
152 mat->row = isl_alloc_array(ctx, isl_int *, n_row)((isl_int * *)isl_malloc_or_die(ctx, (n_row)*sizeof(isl_int *
)))
;
153 if (n_row && !mat->row)
154 goto error;
155 for (i = 0; i < n_row; ++i)
156 mat->row[i] = row[first_row+i] + first_col;
157 mat->ctx = ctx;
158 isl_ctx_ref(ctx);
159 mat->ref = 1;
160 mat->n_row = n_row;
161 mat->n_col = n_col;
162 mat->block = isl_blk_empty();
163 mat->flags = ISL_MAT_BORROWED(1 << 0);
164 return mat;
165error:
166 free(mat);
167 return NULL((void*)0);
168}
169
170__isl_give isl_mat *isl_mat_sub_alloc(__isl_keep isl_mat *mat,
171 unsigned first_row, unsigned n_row, unsigned first_col, unsigned n_col)
172{
173 if (!mat)
174 return NULL((void*)0);
175 return isl_mat_sub_alloc6(mat->ctx, mat->row, first_row, n_row,
176 first_col, n_col);
177}
178
179void isl_mat_sub_copy(struct isl_ctx *ctx, isl_int **dst, isl_int **src,
180 unsigned n_row, unsigned dst_col, unsigned src_col, unsigned n_col)
181{
182 int i;
183
184 for (i = 0; i < n_row; ++i)
185 isl_seq_cpy(dst[i]+dst_col, src[i]+src_col, n_col);
186}
187
188void isl_mat_sub_neg(struct isl_ctx *ctx, isl_int **dst, isl_int **src,
189 unsigned n_row, unsigned dst_col, unsigned src_col, unsigned n_col)
190{
191 int i;
192
193 for (i = 0; i < n_row; ++i)
194 isl_seq_neg(dst[i]+dst_col, src[i]+src_col, n_col);
195}
196
197__isl_give isl_mat *isl_mat_copy(__isl_keep isl_mat *mat)
198{
199 if (!mat)
200 return NULL((void*)0);
201
202 mat->ref++;
203 return mat;
204}
205
206struct isl_mat *isl_mat_dup(struct isl_mat *mat)
207{
208 int i;
209 struct isl_mat *mat2;
210
211 if (!mat)
212 return NULL((void*)0);
213 mat2 = isl_mat_alloc(mat->ctx, mat->n_row, mat->n_col);
214 if (!mat2)
215 return NULL((void*)0);
216 for (i = 0; i < mat->n_row; ++i)
217 isl_seq_cpy(mat2->row[i], mat->row[i], mat->n_col);
218 return mat2;
219}
220
221struct isl_mat *isl_mat_cow(struct isl_mat *mat)
222{
223 struct isl_mat *mat2;
224 if (!mat)
8
Taking false branch
225 return NULL((void*)0);
226
227 if (mat->ref == 1 && !ISL_F_ISSET(mat, ISL_MAT_BORROWED)(!!(((mat)->flags) & ((1 << 0)))))
9
Assuming the condition is false
228 return mat;
229
230 mat2 = isl_mat_dup(mat);
231 isl_mat_free(mat);
10
Calling 'isl_mat_free'
16
Returning; memory was released via 1st parameter
232 return mat2;
233}
234
235__isl_null isl_mat *isl_mat_free(__isl_take isl_mat *mat)
236{
237 if (!mat)
11
Taking false branch
238 return NULL((void*)0);
239
240 if (--mat->ref > 0)
12
Assuming the condition is false
13
Taking false branch
241 return NULL((void*)0);
242
243 if (!ISL_F_ISSET(mat, ISL_MAT_BORROWED)(!!(((mat)->flags) & ((1 << 0)))))
14
Taking false branch
244 isl_blk_free(mat->ctx, mat->block);
245 isl_ctx_deref(mat->ctx);
246 free(mat->row);
247 free(mat);
15
Memory is released
248
249 return NULL((void*)0);
250}
251
252int isl_mat_rows(__isl_keep isl_mat *mat)
253{
254 return mat ? mat->n_row : -1;
255}
256
257int isl_mat_cols(__isl_keep isl_mat *mat)
258{
259 return mat ? mat->n_col : -1;
260}
261
262/* Check that "col" is a valid column position for "mat".
263 */
264static isl_stat check_col(__isl_keep isl_mat *mat, int col)
265{
266 if (!mat)
267 return isl_stat_error;
268 if (col < 0 || col >= mat->n_col)
269 isl_die(isl_mat_get_ctx(mat), isl_error_invalid,do { isl_handle_error(isl_mat_get_ctx(mat), isl_error_invalid
, "column out of range", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn306458/tools/polly/lib/External/isl/isl_mat.c"
, 270); return isl_stat_error; } while (0)
270 "column out of range", return isl_stat_error)do { isl_handle_error(isl_mat_get_ctx(mat), isl_error_invalid
, "column out of range", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn306458/tools/polly/lib/External/isl/isl_mat.c"
, 270); return isl_stat_error; } while (0)
;
271 return isl_stat_ok;
272}
273
274int isl_mat_get_element(__isl_keep isl_mat *mat, int row, int col, isl_int *v)
275{
276 if (!mat)
277 return -1;
278 if (row < 0 || row >= mat->n_row)
279 isl_die(mat->ctx, isl_error_invalid, "row out of range",do { isl_handle_error(mat->ctx, isl_error_invalid, "row out of range"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn306458/tools/polly/lib/External/isl/isl_mat.c"
, 280); return -1; } while (0)
280 return -1)do { isl_handle_error(mat->ctx, isl_error_invalid, "row out of range"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn306458/tools/polly/lib/External/isl/isl_mat.c"
, 280); return -1; } while (0)
;
281 if (check_col(mat, col) < 0)
282 return -1;
283 isl_int_set(*v, mat->row[row][col])isl_sioimath_set((*v), *(mat->row[row][col]));
284 return 0;
285}
286
287/* Extract the element at row "row", oolumn "col" of "mat".
288 */
289__isl_give isl_val *isl_mat_get_element_val(__isl_keep isl_mat *mat,
290 int row, int col)
291{
292 isl_ctx *ctx;
293
294 if (!mat)
295 return NULL((void*)0);
296 ctx = isl_mat_get_ctx(mat);
297 if (row < 0 || row >= mat->n_row)
298 isl_die(ctx, isl_error_invalid, "row out of range",do { isl_handle_error(ctx, isl_error_invalid, "row out of range"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn306458/tools/polly/lib/External/isl/isl_mat.c"
, 299); return ((void*)0); } while (0)
299 return NULL)do { isl_handle_error(ctx, isl_error_invalid, "row out of range"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn306458/tools/polly/lib/External/isl/isl_mat.c"
, 299); return ((void*)0); } while (0)
;
300 if (check_col(mat, col) < 0)
301 return NULL((void*)0);
302 return isl_val_int_from_isl_int(ctx, mat->row[row][col]);
303}
304
305__isl_give isl_mat *isl_mat_set_element(__isl_take isl_mat *mat,
306 int row, int col, isl_int v)
307{
308 mat = isl_mat_cow(mat);
309 if (!mat)
310 return NULL((void*)0);
311 if (row < 0 || row >= mat->n_row)
312 isl_die(mat->ctx, isl_error_invalid, "row out of range",do { isl_handle_error(mat->ctx, isl_error_invalid, "row out of range"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn306458/tools/polly/lib/External/isl/isl_mat.c"
, 313); goto error; } while (0)
313 goto error)do { isl_handle_error(mat->ctx, isl_error_invalid, "row out of range"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn306458/tools/polly/lib/External/isl/isl_mat.c"
, 313); goto error; } while (0)
;
314 if (check_col(mat, col) < 0)
315 return isl_mat_free(mat);
316 isl_int_set(mat->row[row][col], v)isl_sioimath_set((mat->row[row][col]), *(v));
317 return mat;
318error:
319 isl_mat_free(mat);
320 return NULL((void*)0);
321}
322
323__isl_give isl_mat *isl_mat_set_element_si(__isl_take isl_mat *mat,
324 int row, int col, int v)
325{
326 mat = isl_mat_cow(mat);
327 if (!mat)
328 return NULL((void*)0);
329 if (row < 0 || row >= mat->n_row)
330 isl_die(mat->ctx, isl_error_invalid, "row out of range",do { isl_handle_error(mat->ctx, isl_error_invalid, "row out of range"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn306458/tools/polly/lib/External/isl/isl_mat.c"
, 331); goto error; } while (0)
331 goto error)do { isl_handle_error(mat->ctx, isl_error_invalid, "row out of range"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn306458/tools/polly/lib/External/isl/isl_mat.c"
, 331); goto error; } while (0)
;
332 if (check_col(mat, col) < 0)
333 return isl_mat_free(mat);
334 isl_int_set_si(mat->row[row][col], v)isl_sioimath_set_si((mat->row[row][col]), v);
335 return mat;
336error:
337 isl_mat_free(mat);
338 return NULL((void*)0);
339}
340
341/* Replace the element at row "row", column "col" of "mat" by "v".
342 */
343__isl_give isl_mat *isl_mat_set_element_val(__isl_take isl_mat *mat,
344 int row, int col, __isl_take isl_val *v)
345{
346 if (!v)
347 return isl_mat_free(mat);
348 if (!isl_val_is_int(v))
349 isl_die(isl_val_get_ctx(v), isl_error_invalid,do { isl_handle_error(isl_val_get_ctx(v), isl_error_invalid, "expecting integer value"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn306458/tools/polly/lib/External/isl/isl_mat.c"
, 350); goto error; } while (0)
350 "expecting integer value", goto error)do { isl_handle_error(isl_val_get_ctx(v), isl_error_invalid, "expecting integer value"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn306458/tools/polly/lib/External/isl/isl_mat.c"
, 350); goto error; } while (0)
;
351 mat = isl_mat_set_element(mat, row, col, v->n);
352 isl_val_free(v);
353 return mat;
354error:
355 isl_val_free(v);
356 return isl_mat_free(mat);
357}
358
359__isl_give isl_mat *isl_mat_diag(isl_ctx *ctx, unsigned n_row, isl_int d)
360{
361 int i;
362 struct isl_mat *mat;
363
364 mat = isl_mat_alloc(ctx, n_row, n_row);
365 if (!mat)
366 return NULL((void*)0);
367 for (i = 0; i < n_row; ++i) {
368 isl_seq_clr(mat->row[i], i);
369 isl_int_set(mat->row[i][i], d)isl_sioimath_set((mat->row[i][i]), *(d));
370 isl_seq_clr(mat->row[i]+i+1, n_row-(i+1));
371 }
372
373 return mat;
374}
375
376/* Create an "n_row" by "n_col" matrix with zero elements.
377 */
378__isl_give isl_mat *isl_mat_zero(isl_ctx *ctx, unsigned n_row, unsigned n_col)
379{
380 int i;
381 isl_mat *mat;
382
383 mat = isl_mat_alloc(ctx, n_row, n_col);
384 if (!mat)
385 return NULL((void*)0);
386 for (i = 0; i < n_row; ++i)
387 isl_seq_clr(mat->row[i], n_col);
388
389 return mat;
390}
391
392__isl_give isl_mat *isl_mat_identity(isl_ctx *ctx, unsigned n_row)
393{
394 if (!ctx)
395 return NULL((void*)0);
396 return isl_mat_diag(ctx, n_row, ctx->one);
397}
398
399/* Is "mat" a (possibly scaled) identity matrix?
400 */
401int isl_mat_is_scaled_identity(__isl_keep isl_mat *mat)
402{
403 int i;
404
405 if (!mat)
406 return -1;
407 if (mat->n_row != mat->n_col)
408 return 0;
409
410 for (i = 0; i < mat->n_row; ++i) {
411 if (isl_seq_first_non_zero(mat->row[i], i) != -1)
412 return 0;
413 if (isl_int_ne(mat->row[0][0], mat->row[i][i])(isl_sioimath_cmp(*(mat->row[0][0]), *(mat->row[i][i]))
!= 0)
)
414 return 0;
415 if (isl_seq_first_non_zero(mat->row[i] + i + 1,
416 mat->n_col - (i + 1)) != -1)
417 return 0;
418 }
419
420 return 1;
421}
422
423__isl_give isl_vec *isl_mat_vec_product(__isl_take isl_mat *mat,
424 __isl_take isl_vec *vec)
425{
426 int i;
427 struct isl_vec *prod;
428
429 if (!mat || !vec)
430 goto error;
431
432 isl_assert(mat->ctx, mat->n_col == vec->size, goto error)do { if (mat->n_col == vec->size) break; do { isl_handle_error
(mat->ctx, isl_error_unknown, "Assertion \"" "mat->n_col == vec->size"
"\" failed", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn306458/tools/polly/lib/External/isl/isl_mat.c"
, 432); goto error; } while (0); } while (0)
;
433
434 prod = isl_vec_alloc(mat->ctx, mat->n_row);
435 if (!prod)
436 goto error;
437
438 for (i = 0; i < prod->size; ++i)
439 isl_seq_inner_product(mat->row[i], vec->el, vec->size,
440 &prod->block.data[i]);
441 isl_mat_free(mat);
442 isl_vec_free(vec);
443 return prod;
444error:
445 isl_mat_free(mat);
446 isl_vec_free(vec);
447 return NULL((void*)0);
448}
449
450__isl_give isl_vec *isl_mat_vec_inverse_product(__isl_take isl_mat *mat,
451 __isl_take isl_vec *vec)
452{
453 struct isl_mat *vec_mat;
454 int i;
455
456 if (!mat || !vec)
457 goto error;
458 vec_mat = isl_mat_alloc(vec->ctx, vec->size, 1);
459 if (!vec_mat)
460 goto error;
461 for (i = 0; i < vec->size; ++i)
462 isl_int_set(vec_mat->row[i][0], vec->el[i])isl_sioimath_set((vec_mat->row[i][0]), *(vec->el[i]));
463 vec_mat = isl_mat_inverse_product(mat, vec_mat);
464 isl_vec_free(vec);
465 if (!vec_mat)
466 return NULL((void*)0);
467 vec = isl_vec_alloc(vec_mat->ctx, vec_mat->n_row);
468 if (vec)
469 for (i = 0; i < vec->size; ++i)
470 isl_int_set(vec->el[i], vec_mat->row[i][0])isl_sioimath_set((vec->el[i]), *(vec_mat->row[i][0]));
471 isl_mat_free(vec_mat);
472 return vec;
473error:
474 isl_mat_free(mat);
475 isl_vec_free(vec);
476 return NULL((void*)0);
477}
478
479__isl_give isl_vec *isl_vec_mat_product(__isl_take isl_vec *vec,
480 __isl_take isl_mat *mat)
481{
482 int i, j;
483 struct isl_vec *prod;
484
485 if (!mat || !vec)
486 goto error;
487
488 isl_assert(mat->ctx, mat->n_row == vec->size, goto error)do { if (mat->n_row == vec->size) break; do { isl_handle_error
(mat->ctx, isl_error_unknown, "Assertion \"" "mat->n_row == vec->size"
"\" failed", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn306458/tools/polly/lib/External/isl/isl_mat.c"
, 488); goto error; } while (0); } while (0)
;
489
490 prod = isl_vec_alloc(mat->ctx, mat->n_col);
491 if (!prod)
492 goto error;
493
494 for (i = 0; i < prod->size; ++i) {
495 isl_int_set_si(prod->el[i], 0)isl_sioimath_set_si((prod->el[i]), 0);
496 for (j = 0; j < vec->size; ++j)
497 isl_int_addmul(prod->el[i], vec->el[j], mat->row[j][i])isl_sioimath_addmul((prod->el[i]), *(vec->el[j]), *(mat
->row[j][i]))
;
498 }
499 isl_mat_free(mat);
500 isl_vec_free(vec);
501 return prod;
502error:
503 isl_mat_free(mat);
504 isl_vec_free(vec);
505 return NULL((void*)0);
506}
507
508__isl_give isl_mat *isl_mat_aff_direct_sum(__isl_take isl_mat *left,
509 __isl_take isl_mat *right)
510{
511 int i;
512 struct isl_mat *sum;
513
514 if (!left || !right)
515 goto error;
516
517 isl_assert(left->ctx, left->n_row == right->n_row, goto error)do { if (left->n_row == right->n_row) break; do { isl_handle_error
(left->ctx, isl_error_unknown, "Assertion \"" "left->n_row == right->n_row"
"\" failed", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn306458/tools/polly/lib/External/isl/isl_mat.c"
, 517); goto error; } while (0); } while (0)
;
518 isl_assert(left->ctx, left->n_row >= 1, goto error)do { if (left->n_row >= 1) break; do { isl_handle_error
(left->ctx, isl_error_unknown, "Assertion \"" "left->n_row >= 1"
"\" failed", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn306458/tools/polly/lib/External/isl/isl_mat.c"
, 518); goto error; } while (0); } while (0)
;
519 isl_assert(left->ctx, left->n_col >= 1, goto error)do { if (left->n_col >= 1) break; do { isl_handle_error
(left->ctx, isl_error_unknown, "Assertion \"" "left->n_col >= 1"
"\" failed", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn306458/tools/polly/lib/External/isl/isl_mat.c"
, 519); goto error; } while (0); } while (0)
;
520 isl_assert(left->ctx, right->n_col >= 1, goto error)do { if (right->n_col >= 1) break; do { isl_handle_error
(left->ctx, isl_error_unknown, "Assertion \"" "right->n_col >= 1"
"\" failed", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn306458/tools/polly/lib/External/isl/isl_mat.c"
, 520); goto error; } while (0); } while (0)
;
521 isl_assert(left->ctx,do { if (isl_seq_first_non_zero(left->row[0]+1, left->n_col
-1) == -1) break; do { isl_handle_error(left->ctx, isl_error_unknown
, "Assertion \"" "isl_seq_first_non_zero(left->row[0]+1, left->n_col-1) == -1"
"\" failed", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn306458/tools/polly/lib/External/isl/isl_mat.c"
, 523); goto error; } while (0); } while (0)
522 isl_seq_first_non_zero(left->row[0]+1, left->n_col-1) == -1,do { if (isl_seq_first_non_zero(left->row[0]+1, left->n_col
-1) == -1) break; do { isl_handle_error(left->ctx, isl_error_unknown
, "Assertion \"" "isl_seq_first_non_zero(left->row[0]+1, left->n_col-1) == -1"
"\" failed", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn306458/tools/polly/lib/External/isl/isl_mat.c"
, 523); goto error; } while (0); } while (0)
523 goto error)do { if (isl_seq_first_non_zero(left->row[0]+1, left->n_col
-1) == -1) break; do { isl_handle_error(left->ctx, isl_error_unknown
, "Assertion \"" "isl_seq_first_non_zero(left->row[0]+1, left->n_col-1) == -1"
"\" failed", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn306458/tools/polly/lib/External/isl/isl_mat.c"
, 523); goto error; } while (0); } while (0)
;
524 isl_assert(left->ctx,do { if (isl_seq_first_non_zero(right->row[0]+1, right->
n_col-1) == -1) break; do { isl_handle_error(left->ctx, isl_error_unknown
, "Assertion \"" "isl_seq_first_non_zero(right->row[0]+1, right->n_col-1) == -1"
"\" failed", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn306458/tools/polly/lib/External/isl/isl_mat.c"
, 526); goto error; } while (0); } while (0)
525 isl_seq_first_non_zero(right->row[0]+1, right->n_col-1) == -1,do { if (isl_seq_first_non_zero(right->row[0]+1, right->
n_col-1) == -1) break; do { isl_handle_error(left->ctx, isl_error_unknown
, "Assertion \"" "isl_seq_first_non_zero(right->row[0]+1, right->n_col-1) == -1"
"\" failed", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn306458/tools/polly/lib/External/isl/isl_mat.c"
, 526); goto error; } while (0); } while (0)
526 goto error)do { if (isl_seq_first_non_zero(right->row[0]+1, right->
n_col-1) == -1) break; do { isl_handle_error(left->ctx, isl_error_unknown
, "Assertion \"" "isl_seq_first_non_zero(right->row[0]+1, right->n_col-1) == -1"
"\" failed", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn306458/tools/polly/lib/External/isl/isl_mat.c"
, 526); goto error; } while (0); } while (0)
;
527
528 sum = isl_mat_alloc(left->ctx, left->n_row, left->n_col + right->n_col - 1);
529 if (!sum)
530 goto error;
531 isl_int_lcm(sum->row[0][0], left->row[0][0], right->row[0][0])isl_sioimath_lcm((sum->row[0][0]), *(left->row[0][0]), *
(right->row[0][0]))
;
532 isl_int_divexact(left->row[0][0], sum->row[0][0], left->row[0][0])isl_sioimath_tdiv_q((left->row[0][0]), *(sum->row[0][0]
), *(left->row[0][0]))
;
533 isl_int_divexact(right->row[0][0], sum->row[0][0], right->row[0][0])isl_sioimath_tdiv_q((right->row[0][0]), *(sum->row[0][0
]), *(right->row[0][0]))
;
534
535 isl_seq_clr(sum->row[0]+1, sum->n_col-1);
536 for (i = 1; i < sum->n_row; ++i) {
537 isl_int_mul(sum->row[i][0], left->row[0][0], left->row[i][0])isl_sioimath_mul((sum->row[i][0]), *(left->row[0][0]), *
(left->row[i][0]))
;
538 isl_int_addmul(sum->row[i][0],isl_sioimath_addmul((sum->row[i][0]), *(right->row[0][0
]), *(right->row[i][0]))
539 right->row[0][0], right->row[i][0])isl_sioimath_addmul((sum->row[i][0]), *(right->row[0][0
]), *(right->row[i][0]))
;
540 isl_seq_scale(sum->row[i]+1, left->row[i]+1, left->row[0][0],
541 left->n_col-1);
542 isl_seq_scale(sum->row[i]+left->n_col,
543 right->row[i]+1, right->row[0][0],
544 right->n_col-1);
545 }
546
547 isl_int_divexact(left->row[0][0], sum->row[0][0], left->row[0][0])isl_sioimath_tdiv_q((left->row[0][0]), *(sum->row[0][0]
), *(left->row[0][0]))
;
548 isl_int_divexact(right->row[0][0], sum->row[0][0], right->row[0][0])isl_sioimath_tdiv_q((right->row[0][0]), *(sum->row[0][0
]), *(right->row[0][0]))
;
549 isl_mat_free(left);
550 isl_mat_free(right);
551 return sum;
552error:
553 isl_mat_free(left);
554 isl_mat_free(right);
555 return NULL((void*)0);
556}
557
558static void exchange(struct isl_mat *M, struct isl_mat **U,
559 struct isl_mat **Q, unsigned row, unsigned i, unsigned j)
560{
561 int r;
562 for (r = row; r < M->n_row; ++r)
563 isl_int_swap(M->row[r][i], M->row[r][j])isl_sioimath_swap((M->row[r][i]), (M->row[r][j]));
564 if (U) {
565 for (r = 0; r < (*U)->n_row; ++r)
566 isl_int_swap((*U)->row[r][i], (*U)->row[r][j])isl_sioimath_swap(((*U)->row[r][i]), ((*U)->row[r][j]));
567 }
568 if (Q)
569 isl_mat_swap_rows(*Q, i, j);
570}
571
572static void subtract(struct isl_mat *M, struct isl_mat **U,
573 struct isl_mat **Q, unsigned row, unsigned i, unsigned j, isl_int m)
574{
575 int r;
576 for (r = row; r < M->n_row; ++r)
577 isl_int_submul(M->row[r][j], m, M->row[r][i])isl_sioimath_submul((M->row[r][j]), *(m), *(M->row[r][i
]))
;
578 if (U) {
579 for (r = 0; r < (*U)->n_row; ++r)
580 isl_int_submul((*U)->row[r][j], m, (*U)->row[r][i])isl_sioimath_submul(((*U)->row[r][j]), *(m), *((*U)->row
[r][i]))
;
581 }
582 if (Q) {
583 for (r = 0; r < (*Q)->n_col; ++r)
584 isl_int_addmul((*Q)->row[i][r], m, (*Q)->row[j][r])isl_sioimath_addmul(((*Q)->row[i][r]), *(m), *((*Q)->row
[j][r]))
;
585 }
586}
587
588static void oppose(struct isl_mat *M, struct isl_mat **U,
589 struct isl_mat **Q, unsigned row, unsigned col)
590{
591 int r;
592 for (r = row; r < M->n_row; ++r)
593 isl_int_neg(M->row[r][col], M->row[r][col])isl_sioimath_neg((M->row[r][col]), *(M->row[r][col]));
594 if (U) {
595 for (r = 0; r < (*U)->n_row; ++r)
596 isl_int_neg((*U)->row[r][col], (*U)->row[r][col])isl_sioimath_neg(((*U)->row[r][col]), *((*U)->row[r][col
]))
;
597 }
598 if (Q)
599 isl_seq_neg((*Q)->row[col], (*Q)->row[col], (*Q)->n_col);
600}
601
602/* Given matrix M, compute
603 *
604 * M U = H
605 * M = H Q
606 *
607 * with U and Q unimodular matrices and H a matrix in column echelon form
608 * such that on each echelon row the entries in the non-echelon column
609 * are non-negative (if neg == 0) or non-positive (if neg == 1)
610 * and strictly smaller (in absolute value) than the entries in the echelon
611 * column.
612 * If U or Q are NULL, then these matrices are not computed.
613 */
614__isl_give isl_mat *isl_mat_left_hermite(__isl_take isl_mat *M, int neg,
615 __isl_give isl_mat **U, __isl_give isl_mat **Q)
616{
617 isl_int c;
618 int row, col;
619
620 if (U)
4
Taking false branch
621 *U = NULL((void*)0);
622 if (Q)
5
Taking true branch
623 *Q = NULL((void*)0);
624 if (!M)
6
Taking false branch
625 goto error;
626 M = isl_mat_cow(M);
7
Calling 'isl_mat_cow'
17
Returning; memory was released via 1st parameter
627 if (!M)
18
Assuming 'M' is non-null
19
Taking false branch
628 goto error;
629 if (U) {
20
Taking false branch
630 *U = isl_mat_identity(M->ctx, M->n_col);
631 if (!*U)
632 goto error;
633 }
634 if (Q) {
21
Taking true branch
635 *Q = isl_mat_identity(M->ctx, M->n_col);
636 if (!*Q)
22
Taking false branch
637 goto error;
638 }
639
640 col = 0;
641 isl_int_init(c)isl_sioimath_init((c));
642 for (row = 0; row < M->n_row; ++row) {
23
Assuming the condition is false
24
Loop condition is false. Execution continues on line 676
643 int first, i, off;
644 first = isl_seq_abs_min_non_zero(M->row[row]+col, M->n_col-col);
645 if (first == -1)
646 continue;
647 first += col;
648 if (first != col)
649 exchange(M, U, Q, row, first, col);
650 if (isl_int_is_neg(M->row[row][col])(isl_sioimath_sgn(*(M->row[row][col])) < 0))
651 oppose(M, U, Q, row, col);
652 first = col+1;
653 while ((off = isl_seq_first_non_zero(M->row[row]+first,
654 M->n_col-first)) != -1) {
655 first += off;
656 isl_int_fdiv_q(c, M->row[row][first], M->row[row][col])isl_sioimath_fdiv_q((c), *(M->row[row][first]), *(M->row
[row][col]))
;
657 subtract(M, U, Q, row, col, first, c);
658 if (!isl_int_is_zero(M->row[row][first])(isl_sioimath_sgn(*(M->row[row][first])) == 0))
659 exchange(M, U, Q, row, first, col);
660 else
661 ++first;
662 }
663 for (i = 0; i < col; ++i) {
664 if (isl_int_is_zero(M->row[row][i])(isl_sioimath_sgn(*(M->row[row][i])) == 0))
665 continue;
666 if (neg)
667 isl_int_cdiv_q(c, M->row[row][i], M->row[row][col])isl_sioimath_cdiv_q((c), *(M->row[row][i]), *(M->row[row
][col]))
;
668 else
669 isl_int_fdiv_q(c, M->row[row][i], M->row[row][col])isl_sioimath_fdiv_q((c), *(M->row[row][i]), *(M->row[row
][col]))
;
670 if (isl_int_is_zero(c)(isl_sioimath_sgn(*(c)) == 0))
671 continue;
672 subtract(M, U, Q, row, col, i, c);
673 }
674 ++col;
675 }
676 isl_int_clear(c)isl_sioimath_clear((c));
677
678 return M;
679error:
680 if (Q) {
681 isl_mat_free(*Q);
682 *Q = NULL((void*)0);
683 }
684 if (U) {
685 isl_mat_free(*U);
686 *U = NULL((void*)0);
687 }
688 isl_mat_free(M);
689 return NULL((void*)0);
690}
691
692struct isl_mat *isl_mat_right_kernel(struct isl_mat *mat)
693{
694 int i, rank;
695 struct isl_mat *U = NULL((void*)0);
696 struct isl_mat *K;
697
698 mat = isl_mat_left_hermite(mat, 0, &U, NULL((void*)0));
699 if (!mat || !U)
700 goto error;
701
702 for (i = 0, rank = 0; rank < mat->n_col; ++rank) {
703 while (i < mat->n_row && isl_int_is_zero(mat->row[i][rank])(isl_sioimath_sgn(*(mat->row[i][rank])) == 0))
704 ++i;
705 if (i >= mat->n_row)
706 break;
707 }
708 K = isl_mat_alloc(U->ctx, U->n_row, U->n_col - rank);
709 if (!K)
710 goto error;
711 isl_mat_sub_copy(K->ctx, K->row, U->row, U->n_row, 0, rank, U->n_col-rank);
712 isl_mat_free(mat);
713 isl_mat_free(U);
714 return K;
715error:
716 isl_mat_free(mat);
717 isl_mat_free(U);
718 return NULL((void*)0);
719}
720
721__isl_give isl_mat *isl_mat_lin_to_aff(__isl_take isl_mat *mat)
722{
723 int i;
724 struct isl_mat *mat2;
725
726 if (!mat)
727 return NULL((void*)0);
728 mat2 = isl_mat_alloc(mat->ctx, 1+mat->n_row, 1+mat->n_col);
729 if (!mat2)
730 goto error;
731 isl_int_set_si(mat2->row[0][0], 1)isl_sioimath_set_si((mat2->row[0][0]), 1);
732 isl_seq_clr(mat2->row[0]+1, mat->n_col);
733 for (i = 0; i < mat->n_row; ++i) {
734 isl_int_set_si(mat2->row[1+i][0], 0)isl_sioimath_set_si((mat2->row[1+i][0]), 0);
735 isl_seq_cpy(mat2->row[1+i]+1, mat->row[i], mat->n_col);
736 }
737 isl_mat_free(mat);
738 return mat2;
739error:
740 isl_mat_free(mat);
741 return NULL((void*)0);
742}
743
744/* Given two matrices M1 and M2, return the block matrix
745 *
746 * [ M1 0 ]
747 * [ 0 M2 ]
748 */
749__isl_give isl_mat *isl_mat_diagonal(__isl_take isl_mat *mat1,
750 __isl_take isl_mat *mat2)
751{
752 int i;
753 isl_mat *mat;
754
755 if (!mat1 || !mat2)
756 goto error;
757
758 mat = isl_mat_alloc(mat1->ctx, mat1->n_row + mat2->n_row,
759 mat1->n_col + mat2->n_col);
760 if (!mat)
761 goto error;
762 for (i = 0; i < mat1->n_row; ++i) {
763 isl_seq_cpy(mat->row[i], mat1->row[i], mat1->n_col);
764 isl_seq_clr(mat->row[i] + mat1->n_col, mat2->n_col);
765 }
766 for (i = 0; i < mat2->n_row; ++i) {
767 isl_seq_clr(mat->row[mat1->n_row + i], mat1->n_col);
768 isl_seq_cpy(mat->row[mat1->n_row + i] + mat1->n_col,
769 mat2->row[i], mat2->n_col);
770 }
771 isl_mat_free(mat1);
772 isl_mat_free(mat2);
773 return mat;
774error:
775 isl_mat_free(mat1);
776 isl_mat_free(mat2);
777 return NULL((void*)0);
778}
779
780static int row_first_non_zero(isl_int **row, unsigned n_row, unsigned col)
781{
782 int i;
783
784 for (i = 0; i < n_row; ++i)
785 if (!isl_int_is_zero(row[i][col])(isl_sioimath_sgn(*(row[i][col])) == 0))
786 return i;
787 return -1;
788}
789
790static int row_abs_min_non_zero(isl_int **row, unsigned n_row, unsigned col)
791{
792 int i, min = row_first_non_zero(row, n_row, col);
793 if (min < 0)
794 return -1;
795 for (i = min + 1; i < n_row; ++i) {
796 if (isl_int_is_zero(row[i][col])(isl_sioimath_sgn(*(row[i][col])) == 0))
797 continue;
798 if (isl_int_abs_lt(row[i][col], row[min][col])(isl_sioimath_abs_cmp(*(row[i][col]), *(row[min][col])) < 0
)
)
799 min = i;
800 }
801 return min;
802}
803
804static isl_stat inv_exchange(__isl_keep isl_mat **left,
805 __isl_keep isl_mat **right, unsigned i, unsigned j)
806{
807 *left = isl_mat_swap_rows(*left, i, j);
808 *right = isl_mat_swap_rows(*right, i, j);
809
810 if (!*left || !*right)
811 return isl_stat_error;
812 return isl_stat_ok;
813}
814
815static void inv_oppose(
816 struct isl_mat *left, struct isl_mat *right, unsigned row)
817{
818 isl_seq_neg(left->row[row]+row, left->row[row]+row, left->n_col-row);
819 isl_seq_neg(right->row[row], right->row[row], right->n_col);
820}
821
822static void inv_subtract(struct isl_mat *left, struct isl_mat *right,
823 unsigned row, unsigned i, isl_int m)
824{
825 isl_int_neg(m, m)isl_sioimath_neg((m), *(m));
826 isl_seq_combine(left->row[i]+row,
827 left->ctx->one, left->row[i]+row,
828 m, left->row[row]+row,
829 left->n_col-row);
830 isl_seq_combine(right->row[i], right->ctx->one, right->row[i],
831 m, right->row[row], right->n_col);
832}
833
834/* Compute inv(left)*right
835 */
836__isl_give isl_mat *isl_mat_inverse_product(__isl_take isl_mat *left,
837 __isl_take isl_mat *right)
838{
839 int row;
840 isl_int a, b;
841
842 if (!left || !right)
843 goto error;
844
845 isl_assert(left->ctx, left->n_row == left->n_col, goto error)do { if (left->n_row == left->n_col) break; do { isl_handle_error
(left->ctx, isl_error_unknown, "Assertion \"" "left->n_row == left->n_col"
"\" failed", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn306458/tools/polly/lib/External/isl/isl_mat.c"
, 845); goto error; } while (0); } while (0)
;
846 isl_assert(left->ctx, left->n_row == right->n_row, goto error)do { if (left->n_row == right->n_row) break; do { isl_handle_error
(left->ctx, isl_error_unknown, "Assertion \"" "left->n_row == right->n_row"
"\" failed", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn306458/tools/polly/lib/External/isl/isl_mat.c"
, 846); goto error; } while (0); } while (0)
;
847
848 if (left->n_row == 0) {
849 isl_mat_free(left);
850 return right;
851 }
852
853 left = isl_mat_cow(left);
854 right = isl_mat_cow(right);
855 if (!left || !right)
856 goto error;
857
858 isl_int_init(a)isl_sioimath_init((a));
859 isl_int_init(b)isl_sioimath_init((b));
860 for (row = 0; row < left->n_row; ++row) {
861 int pivot, first, i, off;
862 pivot = row_abs_min_non_zero(left->row+row, left->n_row-row, row);
863 if (pivot < 0) {
864 isl_int_clear(a)isl_sioimath_clear((a));
865 isl_int_clear(b)isl_sioimath_clear((b));
866 isl_assert(left->ctx, pivot >= 0, goto error)do { if (pivot >= 0) break; do { isl_handle_error(left->
ctx, isl_error_unknown, "Assertion \"" "pivot >= 0" "\" failed"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn306458/tools/polly/lib/External/isl/isl_mat.c"
, 866); goto error; } while (0); } while (0)
;
867 }
868 pivot += row;
869 if (pivot != row)
870 if (inv_exchange(&left, &right, pivot, row) < 0)
871 goto error;
872 if (isl_int_is_neg(left->row[row][row])(isl_sioimath_sgn(*(left->row[row][row])) < 0))
873 inv_oppose(left, right, row);
874 first = row+1;
875 while ((off = row_first_non_zero(left->row+first,
876 left->n_row-first, row)) != -1) {
877 first += off;
878 isl_int_fdiv_q(a, left->row[first][row],isl_sioimath_fdiv_q((a), *(left->row[first][row]), *(left->
row[row][row]))
879 left->row[row][row])isl_sioimath_fdiv_q((a), *(left->row[first][row]), *(left->
row[row][row]))
;
880 inv_subtract(left, right, row, first, a);
881 if (!isl_int_is_zero(left->row[first][row])(isl_sioimath_sgn(*(left->row[first][row])) == 0)) {
882 if (inv_exchange(&left, &right, row, first) < 0)
883 goto error;
884 } else {
885 ++first;
886 }
887 }
888 for (i = 0; i < row; ++i) {
889 if (isl_int_is_zero(left->row[i][row])(isl_sioimath_sgn(*(left->row[i][row])) == 0))
890 continue;
891 isl_int_gcd(a, left->row[row][row], left->row[i][row])isl_sioimath_gcd((a), *(left->row[row][row]), *(left->row
[i][row]))
;
892 isl_int_divexact(b, left->row[i][row], a)isl_sioimath_tdiv_q((b), *(left->row[i][row]), *(a));
893 isl_int_divexact(a, left->row[row][row], a)isl_sioimath_tdiv_q((a), *(left->row[row][row]), *(a));
894 isl_int_neg(b, b)isl_sioimath_neg((b), *(b));
895 isl_seq_combine(left->row[i] + i,
896 a, left->row[i] + i,
897 b, left->row[row] + i,
898 left->n_col - i);
899 isl_seq_combine(right->row[i], a, right->row[i],
900 b, right->row[row], right->n_col);
901 }
902 }
903 isl_int_clear(b)isl_sioimath_clear((b));
904
905 isl_int_set(a, left->row[0][0])isl_sioimath_set((a), *(left->row[0][0]));
906 for (row = 1; row < left->n_row; ++row)
907 isl_int_lcm(a, a, left->row[row][row])isl_sioimath_lcm((a), *(a), *(left->row[row][row]));
908 if (isl_int_is_zero(a)(isl_sioimath_sgn(*(a)) == 0)){
909 isl_int_clear(a)isl_sioimath_clear((a));
910 isl_assert(left->ctx, 0, goto error)do { if (0) break; do { isl_handle_error(left->ctx, isl_error_unknown
, "Assertion \"" "0" "\" failed", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn306458/tools/polly/lib/External/isl/isl_mat.c"
, 910); goto error; } while (0); } while (0)
;
911 }
912 for (row = 0; row < left->n_row; ++row) {
913 isl_int_divexact(left->row[row][row], a, left->row[row][row])isl_sioimath_tdiv_q((left->row[row][row]), *(a), *(left->
row[row][row]))
;
914 if (isl_int_is_one(left->row[row][row])(isl_sioimath_cmp_si(*(left->row[row][row]), 1) == 0))
915 continue;
916 isl_seq_scale(right->row[row], right->row[row],
917 left->row[row][row], right->n_col);
918 }
919 isl_int_clear(a)isl_sioimath_clear((a));
920
921 isl_mat_free(left);
922 return right;
923error:
924 isl_mat_free(left);
925 isl_mat_free(right);
926 return NULL((void*)0);
927}
928
929void isl_mat_col_scale(struct isl_mat *mat, unsigned col, isl_int m)
930{
931 int i;
932
933 for (i = 0; i < mat->n_row; ++i)
934 isl_int_mul(mat->row[i][col], mat->row[i][col], m)isl_sioimath_mul((mat->row[i][col]), *(mat->row[i][col]
), *(m))
;
935}
936
937void isl_mat_col_combine(struct isl_mat *mat, unsigned dst,
938 isl_int m1, unsigned src1, isl_int m2, unsigned src2)
939{
940 int i;
941 isl_int tmp;
942
943 isl_int_init(tmp)isl_sioimath_init((tmp));
944 for (i = 0; i < mat->n_row; ++i) {
945 isl_int_mul(tmp, m1, mat->row[i][src1])isl_sioimath_mul((tmp), *(m1), *(mat->row[i][src1]));
946 isl_int_addmul(tmp, m2, mat->row[i][src2])isl_sioimath_addmul((tmp), *(m2), *(mat->row[i][src2]));
947 isl_int_set(mat->row[i][dst], tmp)isl_sioimath_set((mat->row[i][dst]), *(tmp));
948 }
949 isl_int_clear(tmp)isl_sioimath_clear((tmp));
950}
951
952__isl_give isl_mat *isl_mat_right_inverse(__isl_take isl_mat *mat)
953{
954 struct isl_mat *inv;
955 int row;
956 isl_int a, b;
957
958 mat = isl_mat_cow(mat);
959 if (!mat)
960 return NULL((void*)0);
961
962 inv = isl_mat_identity(mat->ctx, mat->n_col);
963 inv = isl_mat_cow(inv);
964 if (!inv)
965 goto error;
966
967 isl_int_init(a)isl_sioimath_init((a));
968 isl_int_init(b)isl_sioimath_init((b));
969 for (row = 0; row < mat->n_row; ++row) {
970 int pivot, first, i, off;
971 pivot = isl_seq_abs_min_non_zero(mat->row[row]+row, mat->n_col-row);
972 if (pivot < 0) {
973 isl_int_clear(a)isl_sioimath_clear((a));
974 isl_int_clear(b)isl_sioimath_clear((b));
975 isl_assert(mat->ctx, pivot >= 0, goto error)do { if (pivot >= 0) break; do { isl_handle_error(mat->
ctx, isl_error_unknown, "Assertion \"" "pivot >= 0" "\" failed"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn306458/tools/polly/lib/External/isl/isl_mat.c"
, 975); goto error; } while (0); } while (0)
;
976 }
977 pivot += row;
978 if (pivot != row)
979 exchange(mat, &inv, NULL((void*)0), row, pivot, row);
980 if (isl_int_is_neg(mat->row[row][row])(isl_sioimath_sgn(*(mat->row[row][row])) < 0))
981 oppose(mat, &inv, NULL((void*)0), row, row);
982 first = row+1;
983 while ((off = isl_seq_first_non_zero(mat->row[row]+first,
984 mat->n_col-first)) != -1) {
985 first += off;
986 isl_int_fdiv_q(a, mat->row[row][first],isl_sioimath_fdiv_q((a), *(mat->row[row][first]), *(mat->
row[row][row]))
987 mat->row[row][row])isl_sioimath_fdiv_q((a), *(mat->row[row][first]), *(mat->
row[row][row]))
;
988 subtract(mat, &inv, NULL((void*)0), row, row, first, a);
989 if (!isl_int_is_zero(mat->row[row][first])(isl_sioimath_sgn(*(mat->row[row][first])) == 0))
990 exchange(mat, &inv, NULL((void*)0), row, row, first);
991 else
992 ++first;
993 }
994 for (i = 0; i < row; ++i) {
995 if (isl_int_is_zero(mat->row[row][i])(isl_sioimath_sgn(*(mat->row[row][i])) == 0))
996 continue;
997 isl_int_gcd(a, mat->row[row][row], mat->row[row][i])isl_sioimath_gcd((a), *(mat->row[row][row]), *(mat->row
[row][i]))
;
998 isl_int_divexact(b, mat->row[row][i], a)isl_sioimath_tdiv_q((b), *(mat->row[row][i]), *(a));
999 isl_int_divexact(a, mat->row[row][row], a)isl_sioimath_tdiv_q((a), *(mat->row[row][row]), *(a));
1000 isl_int_neg(a, a)isl_sioimath_neg((a), *(a));
1001 isl_mat_col_combine(mat, i, a, i, b, row);
1002 isl_mat_col_combine(inv, i, a, i, b, row);
1003 }
1004 }
1005 isl_int_clear(b)isl_sioimath_clear((b));
1006
1007 isl_int_set(a, mat->row[0][0])isl_sioimath_set((a), *(mat->row[0][0]));
1008 for (row = 1; row < mat->n_row; ++row)
1009 isl_int_lcm(a, a, mat->row[row][row])isl_sioimath_lcm((a), *(a), *(mat->row[row][row]));
1010 if (isl_int_is_zero(a)(isl_sioimath_sgn(*(a)) == 0)){
1011 isl_int_clear(a)isl_sioimath_clear((a));
1012 goto error;
1013 }
1014 for (row = 0; row < mat->n_row; ++row) {
1015 isl_int_divexact(mat->row[row][row], a, mat->row[row][row])isl_sioimath_tdiv_q((mat->row[row][row]), *(a), *(mat->
row[row][row]))
;
1016 if (isl_int_is_one(mat->row[row][row])(isl_sioimath_cmp_si(*(mat->row[row][row]), 1) == 0))
1017 continue;
1018 isl_mat_col_scale(inv, row, mat->row[row][row]);
1019 }
1020 isl_int_clear(a)isl_sioimath_clear((a));
1021
1022 isl_mat_free(mat);
1023
1024 return inv;
1025error:
1026 isl_mat_free(mat);
1027 isl_mat_free(inv);
1028 return NULL((void*)0);
1029}
1030
1031__isl_give isl_mat *isl_mat_transpose(__isl_take isl_mat *mat)
1032{
1033 struct isl_mat *transpose = NULL((void*)0);
1034 int i, j;
1035
1036 if (!mat)
1037 return NULL((void*)0);
1038
1039 if (mat->n_col == mat->n_row) {
1040 mat = isl_mat_cow(mat);
1041 if (!mat)
1042 return NULL((void*)0);
1043 for (i = 0; i < mat->n_row; ++i)
1044 for (j = i + 1; j < mat->n_col; ++j)
1045 isl_int_swap(mat->row[i][j], mat->row[j][i])isl_sioimath_swap((mat->row[i][j]), (mat->row[j][i]));
1046 return mat;
1047 }
1048 transpose = isl_mat_alloc(mat->ctx, mat->n_col, mat->n_row);
1049 if (!transpose)
1050 goto error;
1051 for (i = 0; i < mat->n_row; ++i)
1052 for (j = 0; j < mat->n_col; ++j)
1053 isl_int_set(transpose->row[j][i], mat->row[i][j])isl_sioimath_set((transpose->row[j][i]), *(mat->row[i][
j]))
;
1054 isl_mat_free(mat);
1055 return transpose;
1056error:
1057 isl_mat_free(mat);
1058 return NULL((void*)0);
1059}
1060
1061__isl_give isl_mat *isl_mat_swap_cols(__isl_take isl_mat *mat,
1062 unsigned i, unsigned j)
1063{
1064 int r;
1065
1066 mat = isl_mat_cow(mat);
1067 if (!mat)
1068 return NULL((void*)0);
1069 isl_assert(mat->ctx, i < mat->n_col, goto error)do { if (i < mat->n_col) break; do { isl_handle_error(mat
->ctx, isl_error_unknown, "Assertion \"" "i < mat->n_col"
"\" failed", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn306458/tools/polly/lib/External/isl/isl_mat.c"
, 1069); goto error; } while (0); } while (0)
;
1070 isl_assert(mat->ctx, j < mat->n_col, goto error)do { if (j < mat->n_col) break; do { isl_handle_error(mat
->ctx, isl_error_unknown, "Assertion \"" "j < mat->n_col"
"\" failed", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn306458/tools/polly/lib/External/isl/isl_mat.c"
, 1070); goto error; } while (0); } while (0)
;
1071
1072 for (r = 0; r < mat->n_row; ++r)
1073 isl_int_swap(mat->row[r][i], mat->row[r][j])isl_sioimath_swap((mat->row[r][i]), (mat->row[r][j]));
1074 return mat;
1075error:
1076 isl_mat_free(mat);
1077 return NULL((void*)0);
1078}
1079
1080__isl_give isl_mat *isl_mat_swap_rows(__isl_take isl_mat *mat,
1081 unsigned i, unsigned j)
1082{
1083 isl_int *t;
1084
1085 if (!mat)
1086 return NULL((void*)0);
1087 mat = isl_mat_cow(mat);
1088 if (!mat)
1089 return NULL((void*)0);
1090 t = mat->row[i];
1091 mat->row[i] = mat->row[j];
1092 mat->row[j] = t;
1093 return mat;
1094}
1095
1096/* Calculate the product of two matrices.
1097 *
1098 * This function is optimized for operand matrices that contain many zeros and
1099 * skips multiplications where we know one of the operands is zero.
1100 */
1101__isl_give isl_mat *isl_mat_product(__isl_take isl_mat *left,
1102 __isl_take isl_mat *right)
1103{
1104 int i, j, k;
1105 struct isl_mat *prod;
1106
1107 if (!left || !right)
1108 goto error;
1109 isl_assert(left->ctx, left->n_col == right->n_row, goto error)do { if (left->n_col == right->n_row) break; do { isl_handle_error
(left->ctx, isl_error_unknown, "Assertion \"" "left->n_col == right->n_row"
"\" failed", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn306458/tools/polly/lib/External/isl/isl_mat.c"
, 1109); goto error; } while (0); } while (0)
;
1110 prod = isl_mat_alloc(left->ctx, left->n_row, right->n_col);
1111 if (!prod)
1112 goto error;
1113 if (left->n_col == 0) {
1114 for (i = 0; i < prod->n_row; ++i)
1115 isl_seq_clr(prod->row[i], prod->n_col);
1116 isl_mat_free(left);
1117 isl_mat_free(right);
1118 return prod;
1119 }
1120 for (i = 0; i < prod->n_row; ++i) {
1121 for (j = 0; j < prod->n_col; ++j)
1122 isl_int_mul(prod->row[i][j],isl_sioimath_mul((prod->row[i][j]), *(left->row[i][0]),
*(right->row[0][j]))
1123 left->row[i][0], right->row[0][j])isl_sioimath_mul((prod->row[i][j]), *(left->row[i][0]),
*(right->row[0][j]))
;
1124 for (k = 1; k < left->n_col; ++k) {
1125 if (isl_int_is_zero(left->row[i][k])(isl_sioimath_sgn(*(left->row[i][k])) == 0))
1126 continue;
1127 for (j = 0; j < prod->n_col; ++j)
1128 isl_int_addmul(prod->row[i][j],isl_sioimath_addmul((prod->row[i][j]), *(left->row[i][k
]), *(right->row[k][j]))
1129 left->row[i][k], right->row[k][j])isl_sioimath_addmul((prod->row[i][j]), *(left->row[i][k
]), *(right->row[k][j]))
;
1130 }
1131 }
1132 isl_mat_free(left);
1133 isl_mat_free(right);
1134 return prod;
1135error:
1136 isl_mat_free(left);
1137 isl_mat_free(right);
1138 return NULL((void*)0);
1139}
1140
1141/* Replace the variables x in the rows q by x' given by x = M x',
1142 * with M the matrix mat.
1143 *
1144 * If the number of new variables is greater than the original
1145 * number of variables, then the rows q have already been
1146 * preextended. If the new number is smaller, then the coefficients
1147 * of the divs, which are not changed, need to be shifted down.
1148 * The row q may be the equalities, the inequalities or the
1149 * div expressions. In the latter case, has_div is true and
1150 * we need to take into account the extra denominator column.
1151 */
1152static int preimage(struct isl_ctx *ctx, isl_int **q, unsigned n,
1153 unsigned n_div, int has_div, struct isl_mat *mat)
1154{
1155 int i;
1156 struct isl_mat *t;
1157 int e;
1158
1159 if (mat->n_col >= mat->n_row)
1160 e = 0;
1161 else
1162 e = mat->n_row - mat->n_col;
1163 if (has_div)
1164 for (i = 0; i < n; ++i)
1165 isl_int_mul(q[i][0], q[i][0], mat->row[0][0])isl_sioimath_mul((q[i][0]), *(q[i][0]), *(mat->row[0][0]));
1166 t = isl_mat_sub_alloc6(mat->ctx, q, 0, n, has_div, mat->n_row);
1167 t = isl_mat_product(t, mat);
1168 if (!t)
1169 return -1;
1170 for (i = 0; i < n; ++i) {
1171 isl_seq_swp_or_cpy(q[i] + has_div, t->row[i], t->n_col);
1172 isl_seq_cpy(q[i] + has_div + t->n_col,
1173 q[i] + has_div + t->n_col + e, n_div);
1174 isl_seq_clr(q[i] + has_div + t->n_col + n_div, e);
1175 }
1176 isl_mat_free(t);
1177 return 0;
1178}
1179
1180/* Replace the variables x in bset by x' given by x = M x', with
1181 * M the matrix mat.
1182 *
1183 * If there are fewer variables x' then there are x, then we perform
1184 * the transformation in place, which means that, in principle,
1185 * this frees up some extra variables as the number
1186 * of columns remains constant, but we would have to extend
1187 * the div array too as the number of rows in this array is assumed
1188 * to be equal to extra.
1189 */
1190struct isl_basic_setisl_basic_map *isl_basic_set_preimage(struct isl_basic_setisl_basic_map *bset,
1191 struct isl_mat *mat)
1192{
1193 struct isl_ctx *ctx;
1194
1195 if (!bset || !mat)
1196 goto error;
1197
1198 ctx = bset->ctx;
1199 bset = isl_basic_set_cow(bset);
1200 if (!bset)
1201 goto error;
1202
1203 isl_assert(ctx, bset->dim->nparam == 0, goto error)do { if (bset->dim->nparam == 0) break; do { isl_handle_error
(ctx, isl_error_unknown, "Assertion \"" "bset->dim->nparam == 0"
"\" failed", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn306458/tools/polly/lib/External/isl/isl_mat.c"
, 1203); goto error; } while (0); } while (0)
;
1204 isl_assert(ctx, 1+bset->dim->n_out == mat->n_row, goto error)do { if (1+bset->dim->n_out == mat->n_row) break; do
{ isl_handle_error(ctx, isl_error_unknown, "Assertion \"" "1+bset->dim->n_out == mat->n_row"
"\" failed", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn306458/tools/polly/lib/External/isl/isl_mat.c"
, 1204); goto error; } while (0); } while (0)
;
1205 isl_assert(ctx, mat->n_col > 0, goto error)do { if (mat->n_col > 0) break; do { isl_handle_error(ctx
, isl_error_unknown, "Assertion \"" "mat->n_col > 0" "\" failed"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn306458/tools/polly/lib/External/isl/isl_mat.c"
, 1205); goto error; } while (0); } while (0)
;
1206
1207 if (mat->n_col > mat->n_row) {
1208 bset = isl_basic_set_extend(bset, 0, mat->n_col-1, 0, 0, 0);
1209 if (!bset)
1210 goto error;
1211 } else if (mat->n_col < mat->n_row) {
1212 bset->dim = isl_space_cow(bset->dim);
1213 if (!bset->dim)
1214 goto error;
1215 bset->dim->n_out -= mat->n_row - mat->n_col;
1216 }
1217
1218 if (preimage(ctx, bset->eq, bset->n_eq, bset->n_div, 0,
1219 isl_mat_copy(mat)) < 0)
1220 goto error;
1221
1222 if (preimage(ctx, bset->ineq, bset->n_ineq, bset->n_div, 0,
1223 isl_mat_copy(mat)) < 0)
1224 goto error;
1225
1226 if (preimage(ctx, bset->div, bset->n_div, bset->n_div, 1, mat) < 0)
1227 goto error2;
1228
1229 ISL_F_CLR(bset, ISL_BASIC_SET_NO_IMPLICIT)(((bset)->flags) &= ~((1 << 2)));
1230 ISL_F_CLR(bset, ISL_BASIC_SET_NO_REDUNDANT)(((bset)->flags) &= ~((1 << 3)));
1231 ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED)(((bset)->flags) &= ~((1 << 5)));
1232 ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED_DIVS)(((bset)->flags) &= ~((1 << 6)));
1233 ISL_F_CLR(bset, ISL_BASIC_SET_ALL_EQUALITIES)(((bset)->flags) &= ~((1 << 7)));
1234
1235 bset = isl_basic_set_simplify(bset);
1236 bset = isl_basic_set_finalize(bset);
1237
1238 return bset;
1239error:
1240 isl_mat_free(mat);
1241error2:
1242 isl_basic_set_free(bset);
1243 return NULL((void*)0);
1244}
1245
1246struct isl_setisl_map *isl_set_preimage(struct isl_setisl_map *set, struct isl_mat *mat)
1247{
1248 int i;
1249
1250 set = isl_set_cow(set);
1251 if (!set)
1252 goto error;
1253
1254 for (i = 0; i < set->n; ++i) {
1255 set->p[i] = isl_basic_set_preimage(set->p[i],
1256 isl_mat_copy(mat));
1257 if (!set->p[i])
1258 goto error;
1259 }
1260 if (mat->n_col != mat->n_row) {
1261 set->dim = isl_space_cow(set->dim);
1262 if (!set->dim)
1263 goto error;
1264 set->dim->n_out += mat->n_col;
1265 set->dim->n_out -= mat->n_row;
1266 }
1267 isl_mat_free(mat);
1268 ISL_F_CLR(set, ISL_SET_NORMALIZED)(((set)->flags) &= ~((1 << 1)));
1269 return set;
1270error:
1271 isl_set_free(set);
1272 isl_mat_free(mat);
1273 return NULL((void*)0);
1274}
1275
1276/* Replace the variables x starting at "first_col" in the rows "rows"
1277 * of some coefficient matrix by x' with x = M x' with M the matrix mat.
1278 * That is, replace the corresponding coefficients c by c M.
1279 */
1280isl_stat isl_mat_sub_transform(isl_int **row, unsigned n_row,
1281 unsigned first_col, __isl_take isl_mat *mat)
1282{
1283 int i;
1284 isl_ctx *ctx;
1285 isl_mat *t;
1286
1287 if (!mat)
1288 return isl_stat_error;
1289 ctx = isl_mat_get_ctx(mat);
1290 t = isl_mat_sub_alloc6(ctx, row, 0, n_row, first_col, mat->n_row);
1291 t = isl_mat_product(t, mat);
1292 if (!t)
1293 return isl_stat_error;
1294 for (i = 0; i < n_row; ++i)
1295 isl_seq_swp_or_cpy(row[i] + first_col, t->row[i], t->n_col);
1296 isl_mat_free(t);
1297 return isl_stat_ok;
1298}
1299
1300void isl_mat_print_internal(__isl_keep isl_mat *mat, FILE *out, int indent)
1301{
1302 int i, j;
1303
1304 if (!mat) {
1305 fprintf(out, "%*snull mat\n", indent, "");
1306 return;
1307 }
1308
1309 if (mat->n_row == 0)
1310 fprintf(out, "%*s[]\n", indent, "");
1311
1312 for (i = 0; i < mat->n_row; ++i) {
1313 if (!i)
1314 fprintf(out, "%*s[[", indent, "");
1315 else
1316 fprintf(out, "%*s[", indent+1, "");
1317 for (j = 0; j < mat->n_col; ++j) {
1318 if (j)
1319 fprintf(out, ",");
1320 isl_int_print(out, mat->row[i][j], 0)isl_sioimath_print(out, *(mat->row[i][j]), 0);
1321 }
1322 if (i == mat->n_row-1)
1323 fprintf(out, "]]\n");
1324 else
1325 fprintf(out, "]\n");
1326 }
1327}
1328
1329void isl_mat_dump(__isl_keep isl_mat *mat)
1330{
1331 isl_mat_print_internal(mat, stderrstderr, 0);
1332}
1333
1334struct isl_mat *isl_mat_drop_cols(struct isl_mat *mat, unsigned col, unsigned n)
1335{
1336 int r;
1337
1338 if (n == 0)
1339 return mat;
1340
1341 mat = isl_mat_cow(mat);
1342 if (!mat)
1343 return NULL((void*)0);
1344
1345 if (col != mat->n_col-n) {
1346 for (r = 0; r < mat->n_row; ++r)
1347 isl_seq_cpy(mat->row[r]+col, mat->row[r]+col+n,
1348 mat->n_col - col - n);
1349 }
1350 mat->n_col -= n;
1351 return mat;
1352}
1353
1354struct isl_mat *isl_mat_drop_rows(struct isl_mat *mat, unsigned row, unsigned n)
1355{
1356 int r;
1357
1358 mat = isl_mat_cow(mat);
1359 if (!mat)
1360 return NULL((void*)0);
1361
1362 for (r = row; r+n < mat->n_row; ++r)
1363 mat->row[r] = mat->row[r+n];
1364
1365 mat->n_row -= n;
1366 return mat;
1367}
1368
1369__isl_give isl_mat *isl_mat_insert_cols(__isl_take isl_mat *mat,
1370 unsigned col, unsigned n)
1371{
1372 isl_mat *ext;
1373
1374 if (!mat)
1375 return NULL((void*)0);
1376 if (n == 0)
1377 return mat;
1378
1379 ext = isl_mat_alloc(mat->ctx, mat->n_row, mat->n_col + n);
1380 if (!ext)
1381 goto error;
1382
1383 isl_mat_sub_copy(mat->ctx, ext->row, mat->row, mat->n_row, 0, 0, col);
1384 isl_mat_sub_copy(mat->ctx, ext->row, mat->row, mat->n_row,
1385 col + n, col, mat->n_col - col);
1386
1387 isl_mat_free(mat);
1388 return ext;
1389error:
1390 isl_mat_free(mat);
1391 return NULL((void*)0);
1392}
1393
1394__isl_give isl_mat *isl_mat_insert_zero_cols(__isl_take isl_mat *mat,
1395 unsigned first, unsigned n)
1396{
1397 int i;
1398
1399 if (!mat)
1400 return NULL((void*)0);
1401 mat = isl_mat_insert_cols(mat, first, n);
1402 if (!mat)
1403 return NULL((void*)0);
1404
1405 for (i = 0; i < mat->n_row; ++i)
1406 isl_seq_clr(mat->row[i] + first, n);
1407
1408 return mat;
1409}
1410
1411__isl_give isl_mat *isl_mat_add_zero_cols(__isl_take isl_mat *mat, unsigned n)
1412{
1413 if (!mat)
1414 return NULL((void*)0);
1415
1416 return isl_mat_insert_zero_cols(mat, mat->n_col, n);
1417}
1418
1419__isl_give isl_mat *isl_mat_insert_rows(__isl_take isl_mat *mat,
1420 unsigned row, unsigned n)
1421{
1422 isl_mat *ext;
1423
1424 if (!mat)
1425 return NULL((void*)0);
1426 if (n == 0)
1427 return mat;
1428
1429 ext = isl_mat_alloc(mat->ctx, mat->n_row + n, mat->n_col);
1430 if (!ext)
1431 goto error;
1432
1433 isl_mat_sub_copy(mat->ctx, ext->row, mat->row, row, 0, 0, mat->n_col);
1434 isl_mat_sub_copy(mat->ctx, ext->row + row + n, mat->row + row,
1435 mat->n_row - row, 0, 0, mat->n_col);
1436
1437 isl_mat_free(mat);
1438 return ext;
1439error:
1440 isl_mat_free(mat);
1441 return NULL((void*)0);
1442}
1443
1444__isl_give isl_mat *isl_mat_add_rows(__isl_take isl_mat *mat, unsigned n)
1445{
1446 if (!mat)
1447 return NULL((void*)0);
1448
1449 return isl_mat_insert_rows(mat, mat->n_row, n);
1450}
1451
1452__isl_give isl_mat *isl_mat_insert_zero_rows(__isl_take isl_mat *mat,
1453 unsigned row, unsigned n)
1454{
1455 int i;
1456
1457 mat = isl_mat_insert_rows(mat, row, n);
1458 if (!mat)
1459 return NULL((void*)0);
1460
1461 for (i = 0; i < n; ++i)
1462 isl_seq_clr(mat->row[row + i], mat->n_col);
1463
1464 return mat;
1465}
1466
1467__isl_give isl_mat *isl_mat_add_zero_rows(__isl_take isl_mat *mat, unsigned n)
1468{
1469 if (!mat)
1470 return NULL((void*)0);
1471
1472 return isl_mat_insert_zero_rows(mat, mat->n_row, n);
1473}
1474
1475void isl_mat_col_submul(struct isl_mat *mat,
1476 int dst_col, isl_int f, int src_col)
1477{
1478 int i;
1479
1480 for (i = 0; i < mat->n_row; ++i)
1481 isl_int_submul(mat->row[i][dst_col], f, mat->row[i][src_col])isl_sioimath_submul((mat->row[i][dst_col]), *(f), *(mat->
row[i][src_col]))
;
1482}
1483
1484void isl_mat_col_add(__isl_keep isl_mat *mat, int dst_col, int src_col)
1485{
1486 int i;
1487
1488 if (!mat)
1489 return;
1490
1491 for (i = 0; i < mat->n_row; ++i)
1492 isl_int_add(mat->row[i][dst_col],isl_sioimath_add((mat->row[i][dst_col]), *(mat->row[i][
dst_col]), *(mat->row[i][src_col]))
1493 mat->row[i][dst_col], mat->row[i][src_col])isl_sioimath_add((mat->row[i][dst_col]), *(mat->row[i][
dst_col]), *(mat->row[i][src_col]))
;
1494}
1495
1496void isl_mat_col_mul(struct isl_mat *mat, int dst_col, isl_int f, int src_col)
1497{
1498 int i;
1499
1500 for (i = 0; i < mat->n_row; ++i)
1501 isl_int_mul(mat->row[i][dst_col], f, mat->row[i][src_col])isl_sioimath_mul((mat->row[i][dst_col]), *(f), *(mat->row
[i][src_col]))
;
1502}
1503
1504/* Add "f" times column "src_col" to column "dst_col" of "mat" and
1505 * return the result.
1506 */
1507__isl_give isl_mat *isl_mat_col_addmul(__isl_take isl_mat *mat, int dst_col,
1508 isl_int f, int src_col)
1509{
1510 int i;
1511
1512 if (check_col(mat, dst_col) < 0 || check_col(mat, src_col) < 0)
1513 return isl_mat_free(mat);
1514
1515 for (i = 0; i < mat->n_row; ++i) {
1516 if (isl_int_is_zero(mat->row[i][src_col])(isl_sioimath_sgn(*(mat->row[i][src_col])) == 0))
1517 continue;
1518 mat = isl_mat_cow(mat);
1519 if (!mat)
1520 return NULL((void*)0);
1521 isl_int_addmul(mat->row[i][dst_col], f, mat->row[i][src_col])isl_sioimath_addmul((mat->row[i][dst_col]), *(f), *(mat->
row[i][src_col]))
;
1522 }
1523
1524 return mat;
1525}
1526
1527/* Negate column "col" of "mat" and return the result.
1528 */
1529__isl_give isl_mat *isl_mat_col_neg(__isl_take isl_mat *mat, int col)
1530{
1531 int i;
1532
1533 if (check_col(mat, col) < 0)
1534 return isl_mat_free(mat);
1535
1536 for (i = 0; i < mat->n_row; ++i) {
1537 if (isl_int_is_zero(mat->row[i][col])(isl_sioimath_sgn(*(mat->row[i][col])) == 0))
1538 continue;
1539 mat = isl_mat_cow(mat);
1540 if (!mat)
1541 return NULL((void*)0);
1542 isl_int_neg(mat->row[i][col], mat->row[i][col])isl_sioimath_neg((mat->row[i][col]), *(mat->row[i][col]
))
;
1543 }
1544
1545 return mat;
1546}
1547
1548__isl_give isl_mat *isl_mat_unimodular_complete(__isl_take isl_mat *M, int row)
1549{
1550 int r;
1551 struct isl_mat *H = NULL((void*)0), *Q = NULL((void*)0);
1552
1553 if (!M)
1
Assuming 'M' is non-null
2
Taking false branch
1554 return NULL((void*)0);
1555
1556 isl_assert(M->ctx, M->n_row == M->n_col, goto error)do { if (M->n_row == M->n_col) break; do { isl_handle_error
(M->ctx, isl_error_unknown, "Assertion \"" "M->n_row == M->n_col"
"\" failed", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn306458/tools/polly/lib/External/isl/isl_mat.c"
, 1556); goto error; } while (0); } while (0)
;
1557 M->n_row = row;
1558 H = isl_mat_left_hermite(isl_mat_copy(M), 0, NULL((void*)0), &Q);
3
Calling 'isl_mat_left_hermite'
25
Returning; memory was released via 1st parameter
1559 M->n_row = M->n_col;
26
Use of memory after it is freed
1560 if (!H)
1561 goto error;
1562 for (r = 0; r < row; ++r)
1563 isl_assert(M->ctx, isl_int_is_one(H->row[r][r]), goto error)do { if ((isl_sioimath_cmp_si(*(H->row[r][r]), 1) == 0)) break
; do { isl_handle_error(M->ctx, isl_error_unknown, "Assertion \""
"(isl_sioimath_cmp_si(*(H->row[r][r]), 1) == 0)" "\" failed"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn306458/tools/polly/lib/External/isl/isl_mat.c"
, 1563); goto error; } while (0); } while (0)
;
1564 for (r = row; r < M->n_row; ++r)
1565 isl_seq_cpy(M->row[r], Q->row[r], M->n_col);
1566 isl_mat_free(H);
1567 isl_mat_free(Q);
1568 return M;
1569error:
1570 isl_mat_free(H);
1571 isl_mat_free(Q);
1572 isl_mat_free(M);
1573 return NULL((void*)0);
1574}
1575
1576__isl_give isl_mat *isl_mat_concat(__isl_take isl_mat *top,
1577 __isl_take isl_mat *bot)
1578{
1579 struct isl_mat *mat;
1580
1581 if (!top || !bot)
1582 goto error;
1583
1584 isl_assert(top->ctx, top->n_col == bot->n_col, goto error)do { if (top->n_col == bot->n_col) break; do { isl_handle_error
(top->ctx, isl_error_unknown, "Assertion \"" "top->n_col == bot->n_col"
"\" failed", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn306458/tools/polly/lib/External/isl/isl_mat.c"
, 1584); goto error; } while (0); } while (0)
;
1585 if (top->n_row == 0) {
1586 isl_mat_free(top);
1587 return bot;
1588 }
1589 if (bot->n_row == 0) {
1590 isl_mat_free(bot);
1591 return top;
1592 }
1593
1594 mat = isl_mat_alloc(top->ctx, top->n_row + bot->n_row, top->n_col);
1595 if (!mat)
1596 goto error;
1597 isl_mat_sub_copy(mat->ctx, mat->row, top->row, top->n_row,
1598 0, 0, mat->n_col);
1599 isl_mat_sub_copy(mat->ctx, mat->row + top->n_row, bot->row, bot->n_row,
1600 0, 0, mat->n_col);
1601 isl_mat_free(top);
1602 isl_mat_free(bot);
1603 return mat;
1604error:
1605 isl_mat_free(top);
1606 isl_mat_free(bot);
1607 return NULL((void*)0);
1608}
1609
1610isl_bool isl_mat_is_equal(__isl_keep isl_mat *mat1, __isl_keep isl_mat *mat2)
1611{
1612 int i;
1613
1614 if (!mat1 || !mat2)
1615 return isl_bool_error;
1616
1617 if (mat1->n_row != mat2->n_row)
1618 return isl_bool_false;
1619
1620 if (mat1->n_col != mat2->n_col)
1621 return isl_bool_false;
1622
1623 for (i = 0; i < mat1->n_row; ++i)
1624 if (!isl_seq_eq(mat1->row[i], mat2->row[i], mat1->n_col))
1625 return isl_bool_false;
1626
1627 return isl_bool_true;
1628}
1629
1630__isl_give isl_mat *isl_mat_from_row_vec(__isl_take isl_vec *vec)
1631{
1632 struct isl_mat *mat;
1633
1634 if (!vec)
1635 return NULL((void*)0);
1636 mat = isl_mat_alloc(vec->ctx, 1, vec->size);
1637 if (!mat)
1638 goto error;
1639
1640 isl_seq_cpy(mat->row[0], vec->el, vec->size);
1641
1642 isl_vec_free(vec);
1643 return mat;
1644error:
1645 isl_vec_free(vec);
1646 return NULL((void*)0);
1647}
1648
1649/* Return a copy of row "row" of "mat" as an isl_vec.
1650 */
1651__isl_give isl_vec *isl_mat_get_row(__isl_keep isl_mat *mat, unsigned row)
1652{
1653 isl_vec *v;
1654
1655 if (!mat)
1656 return NULL((void*)0);
1657 if (row >= mat->n_row)
1658 isl_die(mat->ctx, isl_error_invalid, "row out of range",do { isl_handle_error(mat->ctx, isl_error_invalid, "row out of range"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn306458/tools/polly/lib/External/isl/isl_mat.c"
, 1659); return ((void*)0); } while (0)
1659 return NULL)do { isl_handle_error(mat->ctx, isl_error_invalid, "row out of range"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn306458/tools/polly/lib/External/isl/isl_mat.c"
, 1659); return ((void*)0); } while (0)
;
1660
1661 v = isl_vec_alloc(isl_mat_get_ctx(mat), mat->n_col);
1662 if (!v)
1663 return NULL((void*)0);
1664 isl_seq_cpy(v->el, mat->row[row], mat->n_col);
1665
1666 return v;
1667}
1668
1669__isl_give isl_mat *isl_mat_vec_concat(__isl_take isl_mat *top,
1670 __isl_take isl_vec *bot)
1671{
1672 return isl_mat_concat(top, isl_mat_from_row_vec(bot));
1673}
1674
1675__isl_give isl_mat *isl_mat_move_cols(__isl_take isl_mat *mat,
1676 unsigned dst_col, unsigned src_col, unsigned n)
1677{
1678 isl_mat *res;
1679
1680 if (!mat)
1681 return NULL((void*)0);
1682 if (n == 0 || dst_col == src_col)
1683 return mat;
1684
1685 res = isl_mat_alloc(mat->ctx, mat->n_row, mat->n_col);
1686 if (!res)
1687 goto error;
1688
1689 if (dst_col < src_col) {
1690 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1691 0, 0, dst_col);
1692 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1693 dst_col, src_col, n);
1694 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1695 dst_col + n, dst_col, src_col - dst_col);
1696 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1697 src_col + n, src_col + n,
1698 res->n_col - src_col - n);
1699 } else {
1700 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1701 0, 0, src_col);
1702 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1703 src_col, src_col + n, dst_col - src_col);
1704 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1705 dst_col, src_col, n);
1706 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1707 dst_col + n, dst_col + n,
1708 res->n_col - dst_col - n);
1709 }
1710 isl_mat_free(mat);
1711
1712 return res;
1713error:
1714 isl_mat_free(mat);
1715 return NULL((void*)0);
1716}
1717
1718/* Return the gcd of the elements in row "row" of "mat" in *gcd.
1719 * Return isl_stat_ok on success and isl_stat_error on failure.
1720 */
1721isl_stat isl_mat_row_gcd(__isl_keep isl_mat *mat, int row, isl_int *gcd)
1722{
1723 if (!mat)
1724 return isl_stat_error;
1725
1726 if (row < 0 || row >= mat->n_row)
1727 isl_die(isl_mat_get_ctx(mat), isl_error_invalid,do { isl_handle_error(isl_mat_get_ctx(mat), isl_error_invalid
, "row out of range", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn306458/tools/polly/lib/External/isl/isl_mat.c"
, 1728); return isl_stat_error; } while (0)
1728 "row out of range", return isl_stat_error)do { isl_handle_error(isl_mat_get_ctx(mat), isl_error_invalid
, "row out of range", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn306458/tools/polly/lib/External/isl/isl_mat.c"
, 1728); return isl_stat_error; } while (0)
;
1729 isl_seq_gcd(mat->row[row], mat->n_col, gcd);
1730
1731 return isl_stat_ok;
1732}
1733
1734void isl_mat_gcd(__isl_keep isl_mat *mat, isl_int *gcd)
1735{
1736 int i;
1737 isl_int g;
1738
1739 isl_int_set_si(*gcd, 0)isl_sioimath_set_si((*gcd), 0);
1740 if (!mat)
1741 return;
1742
1743 isl_int_init(g)isl_sioimath_init((g));
1744 for (i = 0; i < mat->n_row; ++i) {
1745 isl_seq_gcd(mat->row[i], mat->n_col, &g);
1746 isl_int_gcd(*gcd, *gcd, g)isl_sioimath_gcd((*gcd), *(*gcd), *(g));
1747 }
1748 isl_int_clear(g)isl_sioimath_clear((g));
1749}
1750
1751/* Return the result of scaling "mat" by a factor of "m".
1752 */
1753__isl_give isl_mat *isl_mat_scale(__isl_take isl_mat *mat, isl_int m)
1754{
1755 int i;
1756
1757 if (isl_int_is_one(m)(isl_sioimath_cmp_si(*(m), 1) == 0))
1758 return mat;
1759
1760 mat = isl_mat_cow(mat);
1761 if (!mat)
1762 return NULL((void*)0);
1763
1764 for (i = 0; i < mat->n_row; ++i)
1765 isl_seq_scale(mat->row[i], mat->row[i], m, mat->n_col);
1766
1767 return mat;
1768}
1769
1770__isl_give isl_mat *isl_mat_scale_down(__isl_take isl_mat *mat, isl_int m)
1771{
1772 int i;
1773
1774 if (isl_int_is_one(m)(isl_sioimath_cmp_si(*(m), 1) == 0))
1775 return mat;
1776
1777 mat = isl_mat_cow(mat);
1778 if (!mat)
1779 return NULL((void*)0);
1780
1781 for (i = 0; i < mat->n_row; ++i)
1782 isl_seq_scale_down(mat->row[i], mat->row[i], m, mat->n_col);
1783
1784 return mat;
1785}
1786
1787__isl_give isl_mat *isl_mat_scale_down_row(__isl_take isl_mat *mat, int row,
1788 isl_int m)
1789{
1790 if (isl_int_is_one(m)(isl_sioimath_cmp_si(*(m), 1) == 0))
1791 return mat;
1792
1793 mat = isl_mat_cow(mat);
1794 if (!mat)
1795 return NULL((void*)0);
1796
1797 isl_seq_scale_down(mat->row[row], mat->row[row], m, mat->n_col);
1798
1799 return mat;
1800}
1801
1802__isl_give isl_mat *isl_mat_normalize(__isl_take isl_mat *mat)
1803{
1804 isl_int gcd;
1805
1806 if (!mat)
1807 return NULL((void*)0);
1808
1809 isl_int_init(gcd)isl_sioimath_init((gcd));
1810 isl_mat_gcd(mat, &gcd);
1811 mat = isl_mat_scale_down(mat, gcd);
1812 isl_int_clear(gcd)isl_sioimath_clear((gcd));
1813
1814 return mat;
1815}
1816
1817__isl_give isl_mat *isl_mat_normalize_row(__isl_take isl_mat *mat, int row)
1818{
1819 mat = isl_mat_cow(mat);
1820 if (!mat)
1821 return NULL((void*)0);
1822
1823 isl_seq_normalize(mat->ctx, mat->row[row], mat->n_col);
1824
1825 return mat;
1826}
1827
1828/* Number of initial non-zero columns.
1829 */
1830int isl_mat_initial_non_zero_cols(__isl_keep isl_mat *mat)
1831{
1832 int i;
1833
1834 if (!mat)
1835 return -1;
1836
1837 for (i = 0; i < mat->n_col; ++i)
1838 if (row_first_non_zero(mat->row, mat->n_row, i) < 0)
1839 break;
1840
1841 return i;
1842}