Bug Summary

File:tools/polly/lib/External/isl/isl_mat.c
Warning:line 1322, column 9
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
197struct isl_mat *isl_mat_copy(struct 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)
225 return NULL((void*)0);
226
227 if (mat->ref == 1 && !ISL_F_ISSET(mat, ISL_MAT_BORROWED)(!!(((mat)->flags) & ((1 << 0)))))
228 return mat;
229
230 mat2 = isl_mat_dup(mat);
231 isl_mat_free(mat);
232 return mat2;
233}
234
235__isl_null isl_mat *isl_mat_free(__isl_take isl_mat *mat)
236{
237 if (!mat)
17
Taking false branch
238 return NULL((void*)0);
239
240 if (--mat->ref > 0)
18
Assuming the condition is false
19
Taking false branch
241 return NULL((void*)0);
242
243 if (!ISL_F_ISSET(mat, ISL_MAT_BORROWED)(!!(((mat)->flags) & ((1 << 0)))))
20
Taking false branch
244 isl_blk_free(mat->ctx, mat->block);
245 isl_ctx_deref(mat->ctx);
246 free(mat->row);
247 free(mat);
21
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~svn299582/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~svn299582/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~svn299582/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~svn299582/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~svn299582/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~svn299582/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~svn299582/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~svn299582/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~svn299582/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~svn299582/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~svn299582/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~svn299582/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
423struct isl_vec *isl_mat_vec_product(struct isl_mat *mat, struct isl_vec *vec)
424{
425 int i;
426 struct isl_vec *prod;
427
428 if (!mat || !vec)
429 goto error;
430
431 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~svn299582/tools/polly/lib/External/isl/isl_mat.c"
, 431); goto error; } while (0); } while (0)
;
432
433 prod = isl_vec_alloc(mat->ctx, mat->n_row);
434 if (!prod)
435 goto error;
436
437 for (i = 0; i < prod->size; ++i)
438 isl_seq_inner_product(mat->row[i], vec->el, vec->size,
439 &prod->block.data[i]);
440 isl_mat_free(mat);
441 isl_vec_free(vec);
442 return prod;
443error:
444 isl_mat_free(mat);
445 isl_vec_free(vec);
446 return NULL((void*)0);
447}
448
449__isl_give isl_vec *isl_mat_vec_inverse_product(__isl_take isl_mat *mat,
450 __isl_take isl_vec *vec)
451{
452 struct isl_mat *vec_mat;
453 int i;
454
455 if (!mat || !vec)
456 goto error;
457 vec_mat = isl_mat_alloc(vec->ctx, vec->size, 1);
458 if (!vec_mat)
459 goto error;
460 for (i = 0; i < vec->size; ++i)
461 isl_int_set(vec_mat->row[i][0], vec->el[i])isl_sioimath_set((vec_mat->row[i][0]), *(vec->el[i]));
462 vec_mat = isl_mat_inverse_product(mat, vec_mat);
463 isl_vec_free(vec);
464 if (!vec_mat)
465 return NULL((void*)0);
466 vec = isl_vec_alloc(vec_mat->ctx, vec_mat->n_row);
467 if (vec)
468 for (i = 0; i < vec->size; ++i)
469 isl_int_set(vec->el[i], vec_mat->row[i][0])isl_sioimath_set((vec->el[i]), *(vec_mat->row[i][0]));
470 isl_mat_free(vec_mat);
471 return vec;
472error:
473 isl_mat_free(mat);
474 isl_vec_free(vec);
475 return NULL((void*)0);
476}
477
478struct isl_vec *isl_vec_mat_product(struct isl_vec *vec, struct isl_mat *mat)
479{
480 int i, j;
481 struct isl_vec *prod;
482
483 if (!mat || !vec)
484 goto error;
485
486 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~svn299582/tools/polly/lib/External/isl/isl_mat.c"
, 486); goto error; } while (0); } while (0)
;
487
488 prod = isl_vec_alloc(mat->ctx, mat->n_col);
489 if (!prod)
490 goto error;
491
492 for (i = 0; i < prod->size; ++i) {
493 isl_int_set_si(prod->el[i], 0)isl_sioimath_set_si((prod->el[i]), 0);
494 for (j = 0; j < vec->size; ++j)
495 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]))
;
496 }
497 isl_mat_free(mat);
498 isl_vec_free(vec);
499 return prod;
500error:
501 isl_mat_free(mat);
502 isl_vec_free(vec);
503 return NULL((void*)0);
504}
505
506struct isl_mat *isl_mat_aff_direct_sum(struct isl_mat *left,
507 struct isl_mat *right)
508{
509 int i;
510 struct isl_mat *sum;
511
512 if (!left || !right)
513 goto error;
514
515 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~svn299582/tools/polly/lib/External/isl/isl_mat.c"
, 515); goto error; } while (0); } while (0)
;
516 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~svn299582/tools/polly/lib/External/isl/isl_mat.c"
, 516); goto error; } while (0); } while (0)
;
517 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~svn299582/tools/polly/lib/External/isl/isl_mat.c"
, 517); goto error; } while (0); } while (0)
;
518 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~svn299582/tools/polly/lib/External/isl/isl_mat.c"
, 518); goto error; } while (0); } while (0)
;
519 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~svn299582/tools/polly/lib/External/isl/isl_mat.c"
, 521); goto error; } while (0); } while (0)
520 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~svn299582/tools/polly/lib/External/isl/isl_mat.c"
, 521); goto error; } while (0); } while (0)
521 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~svn299582/tools/polly/lib/External/isl/isl_mat.c"
, 521); goto error; } while (0); } while (0)
;
522 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~svn299582/tools/polly/lib/External/isl/isl_mat.c"
, 524); goto error; } while (0); } while (0)
523 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~svn299582/tools/polly/lib/External/isl/isl_mat.c"
, 524); goto error; } while (0); } while (0)
524 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~svn299582/tools/polly/lib/External/isl/isl_mat.c"
, 524); goto error; } while (0); } while (0)
;
525
526 sum = isl_mat_alloc(left->ctx, left->n_row, left->n_col + right->n_col - 1);
527 if (!sum)
528 goto error;
529 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]))
;
530 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]))
;
531 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]))
;
532
533 isl_seq_clr(sum->row[0]+1, sum->n_col-1);
534 for (i = 1; i < sum->n_row; ++i) {
535 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]))
;
536 isl_int_addmul(sum->row[i][0],isl_sioimath_addmul((sum->row[i][0]), *(right->row[0][0
]), *(right->row[i][0]))
537 right->row[0][0], right->row[i][0])isl_sioimath_addmul((sum->row[i][0]), *(right->row[0][0
]), *(right->row[i][0]))
;
538 isl_seq_scale(sum->row[i]+1, left->row[i]+1, left->row[0][0],
539 left->n_col-1);
540 isl_seq_scale(sum->row[i]+left->n_col,
541 right->row[i]+1, right->row[0][0],
542 right->n_col-1);
543 }
544
545 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]))
;
546 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]))
;
547 isl_mat_free(left);
548 isl_mat_free(right);
549 return sum;
550error:
551 isl_mat_free(left);
552 isl_mat_free(right);
553 return NULL((void*)0);
554}
555
556static void exchange(struct isl_mat *M, struct isl_mat **U,
557 struct isl_mat **Q, unsigned row, unsigned i, unsigned j)
558{
559 int r;
560 for (r = row; r < M->n_row; ++r)
561 isl_int_swap(M->row[r][i], M->row[r][j])isl_sioimath_swap((M->row[r][i]), (M->row[r][j]));
562 if (U) {
563 for (r = 0; r < (*U)->n_row; ++r)
564 isl_int_swap((*U)->row[r][i], (*U)->row[r][j])isl_sioimath_swap(((*U)->row[r][i]), ((*U)->row[r][j]));
565 }
566 if (Q)
567 isl_mat_swap_rows(*Q, i, j);
568}
569
570static void subtract(struct isl_mat *M, struct isl_mat **U,
571 struct isl_mat **Q, unsigned row, unsigned i, unsigned j, isl_int m)
572{
573 int r;
574 for (r = row; r < M->n_row; ++r)
575 isl_int_submul(M->row[r][j], m, M->row[r][i])isl_sioimath_submul((M->row[r][j]), *(m), *(M->row[r][i
]))
;
576 if (U) {
577 for (r = 0; r < (*U)->n_row; ++r)
578 isl_int_submul((*U)->row[r][j], m, (*U)->row[r][i])isl_sioimath_submul(((*U)->row[r][j]), *(m), *((*U)->row
[r][i]))
;
579 }
580 if (Q) {
581 for (r = 0; r < (*Q)->n_col; ++r)
582 isl_int_addmul((*Q)->row[i][r], m, (*Q)->row[j][r])isl_sioimath_addmul(((*Q)->row[i][r]), *(m), *((*Q)->row
[j][r]))
;
583 }
584}
585
586static void oppose(struct isl_mat *M, struct isl_mat **U,
587 struct isl_mat **Q, unsigned row, unsigned col)
588{
589 int r;
590 for (r = row; r < M->n_row; ++r)
591 isl_int_neg(M->row[r][col], M->row[r][col])isl_sioimath_neg((M->row[r][col]), *(M->row[r][col]));
592 if (U) {
593 for (r = 0; r < (*U)->n_row; ++r)
594 isl_int_neg((*U)->row[r][col], (*U)->row[r][col])isl_sioimath_neg(((*U)->row[r][col]), *((*U)->row[r][col
]))
;
595 }
596 if (Q)
597 isl_seq_neg((*Q)->row[col], (*Q)->row[col], (*Q)->n_col);
598}
599
600/* Given matrix M, compute
601 *
602 * M U = H
603 * M = H Q
604 *
605 * with U and Q unimodular matrices and H a matrix in column echelon form
606 * such that on each echelon row the entries in the non-echelon column
607 * are non-negative (if neg == 0) or non-positive (if neg == 1)
608 * and strictly smaller (in absolute value) than the entries in the echelon
609 * column.
610 * If U or Q are NULL, then these matrices are not computed.
611 */
612struct isl_mat *isl_mat_left_hermite(struct isl_mat *M, int neg,
613 struct isl_mat **U, struct isl_mat **Q)
614{
615 isl_int c;
616 int row, col;
617
618 if (U)
619 *U = NULL((void*)0);
620 if (Q)
621 *Q = NULL((void*)0);
622 if (!M)
623 goto error;
624 M = isl_mat_cow(M);
625 if (!M)
626 goto error;
627 if (U) {
628 *U = isl_mat_identity(M->ctx, M->n_col);
629 if (!*U)
630 goto error;
631 }
632 if (Q) {
633 *Q = isl_mat_identity(M->ctx, M->n_col);
634 if (!*Q)
635 goto error;
636 }
637
638 col = 0;
639 isl_int_init(c)isl_sioimath_init((c));
640 for (row = 0; row < M->n_row; ++row) {
641 int first, i, off;
642 first = isl_seq_abs_min_non_zero(M->row[row]+col, M->n_col-col);
643 if (first == -1)
644 continue;
645 first += col;
646 if (first != col)
647 exchange(M, U, Q, row, first, col);
648 if (isl_int_is_neg(M->row[row][col])(isl_sioimath_sgn(*(M->row[row][col])) < 0))
649 oppose(M, U, Q, row, col);
650 first = col+1;
651 while ((off = isl_seq_first_non_zero(M->row[row]+first,
652 M->n_col-first)) != -1) {
653 first += off;
654 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]))
;
655 subtract(M, U, Q, row, col, first, c);
656 if (!isl_int_is_zero(M->row[row][first])(isl_sioimath_sgn(*(M->row[row][first])) == 0))
657 exchange(M, U, Q, row, first, col);
658 else
659 ++first;
660 }
661 for (i = 0; i < col; ++i) {
662 if (isl_int_is_zero(M->row[row][i])(isl_sioimath_sgn(*(M->row[row][i])) == 0))
663 continue;
664 if (neg)
665 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]))
;
666 else
667 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]))
;
668 if (isl_int_is_zero(c)(isl_sioimath_sgn(*(c)) == 0))
669 continue;
670 subtract(M, U, Q, row, col, i, c);
671 }
672 ++col;
673 }
674 isl_int_clear(c)isl_sioimath_clear((c));
675
676 return M;
677error:
678 if (Q) {
679 isl_mat_free(*Q);
680 *Q = NULL((void*)0);
681 }
682 if (U) {
683 isl_mat_free(*U);
684 *U = NULL((void*)0);
685 }
686 isl_mat_free(M);
687 return NULL((void*)0);
688}
689
690struct isl_mat *isl_mat_right_kernel(struct isl_mat *mat)
691{
692 int i, rank;
693 struct isl_mat *U = NULL((void*)0);
694 struct isl_mat *K;
695
696 mat = isl_mat_left_hermite(mat, 0, &U, NULL((void*)0));
697 if (!mat || !U)
698 goto error;
699
700 for (i = 0, rank = 0; rank < mat->n_col; ++rank) {
701 while (i < mat->n_row && isl_int_is_zero(mat->row[i][rank])(isl_sioimath_sgn(*(mat->row[i][rank])) == 0))
702 ++i;
703 if (i >= mat->n_row)
704 break;
705 }
706 K = isl_mat_alloc(U->ctx, U->n_row, U->n_col - rank);
707 if (!K)
708 goto error;
709 isl_mat_sub_copy(K->ctx, K->row, U->row, U->n_row, 0, rank, U->n_col-rank);
710 isl_mat_free(mat);
711 isl_mat_free(U);
712 return K;
713error:
714 isl_mat_free(mat);
715 isl_mat_free(U);
716 return NULL((void*)0);
717}
718
719struct isl_mat *isl_mat_lin_to_aff(struct isl_mat *mat)
720{
721 int i;
722 struct isl_mat *mat2;
723
724 if (!mat)
725 return NULL((void*)0);
726 mat2 = isl_mat_alloc(mat->ctx, 1+mat->n_row, 1+mat->n_col);
727 if (!mat2)
728 goto error;
729 isl_int_set_si(mat2->row[0][0], 1)isl_sioimath_set_si((mat2->row[0][0]), 1);
730 isl_seq_clr(mat2->row[0]+1, mat->n_col);
731 for (i = 0; i < mat->n_row; ++i) {
732 isl_int_set_si(mat2->row[1+i][0], 0)isl_sioimath_set_si((mat2->row[1+i][0]), 0);
733 isl_seq_cpy(mat2->row[1+i]+1, mat->row[i], mat->n_col);
734 }
735 isl_mat_free(mat);
736 return mat2;
737error:
738 isl_mat_free(mat);
739 return NULL((void*)0);
740}
741
742/* Given two matrices M1 and M2, return the block matrix
743 *
744 * [ M1 0 ]
745 * [ 0 M2 ]
746 */
747__isl_give isl_mat *isl_mat_diagonal(__isl_take isl_mat *mat1,
748 __isl_take isl_mat *mat2)
749{
750 int i;
751 isl_mat *mat;
752
753 if (!mat1 || !mat2)
754 goto error;
755
756 mat = isl_mat_alloc(mat1->ctx, mat1->n_row + mat2->n_row,
757 mat1->n_col + mat2->n_col);
758 if (!mat)
759 goto error;
760 for (i = 0; i < mat1->n_row; ++i) {
761 isl_seq_cpy(mat->row[i], mat1->row[i], mat1->n_col);
762 isl_seq_clr(mat->row[i] + mat1->n_col, mat2->n_col);
763 }
764 for (i = 0; i < mat2->n_row; ++i) {
765 isl_seq_clr(mat->row[mat1->n_row + i], mat1->n_col);
766 isl_seq_cpy(mat->row[mat1->n_row + i] + mat1->n_col,
767 mat2->row[i], mat2->n_col);
768 }
769 isl_mat_free(mat1);
770 isl_mat_free(mat2);
771 return mat;
772error:
773 isl_mat_free(mat1);
774 isl_mat_free(mat2);
775 return NULL((void*)0);
776}
777
778static int row_first_non_zero(isl_int **row, unsigned n_row, unsigned col)
779{
780 int i;
781
782 for (i = 0; i < n_row; ++i)
783 if (!isl_int_is_zero(row[i][col])(isl_sioimath_sgn(*(row[i][col])) == 0))
784 return i;
785 return -1;
786}
787
788static int row_abs_min_non_zero(isl_int **row, unsigned n_row, unsigned col)
789{
790 int i, min = row_first_non_zero(row, n_row, col);
791 if (min < 0)
792 return -1;
793 for (i = min + 1; i < n_row; ++i) {
794 if (isl_int_is_zero(row[i][col])(isl_sioimath_sgn(*(row[i][col])) == 0))
795 continue;
796 if (isl_int_abs_lt(row[i][col], row[min][col])(isl_sioimath_abs_cmp(*(row[i][col]), *(row[min][col])) < 0
)
)
797 min = i;
798 }
799 return min;
800}
801
802static isl_stat inv_exchange(__isl_keep isl_mat **left,
803 __isl_keep isl_mat **right, unsigned i, unsigned j)
804{
805 *left = isl_mat_swap_rows(*left, i, j);
806 *right = isl_mat_swap_rows(*right, i, j);
807
808 if (!*left || !*right)
809 return isl_stat_error;
810 return isl_stat_ok;
811}
812
813static void inv_oppose(
814 struct isl_mat *left, struct isl_mat *right, unsigned row)
815{
816 isl_seq_neg(left->row[row]+row, left->row[row]+row, left->n_col-row);
817 isl_seq_neg(right->row[row], right->row[row], right->n_col);
818}
819
820static void inv_subtract(struct isl_mat *left, struct isl_mat *right,
821 unsigned row, unsigned i, isl_int m)
822{
823 isl_int_neg(m, m)isl_sioimath_neg((m), *(m));
824 isl_seq_combine(left->row[i]+row,
825 left->ctx->one, left->row[i]+row,
826 m, left->row[row]+row,
827 left->n_col-row);
828 isl_seq_combine(right->row[i], right->ctx->one, right->row[i],
829 m, right->row[row], right->n_col);
830}
831
832/* Compute inv(left)*right
833 */
834struct isl_mat *isl_mat_inverse_product(struct isl_mat *left,
835 struct isl_mat *right)
836{
837 int row;
838 isl_int a, b;
839
840 if (!left || !right)
841 goto error;
842
843 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~svn299582/tools/polly/lib/External/isl/isl_mat.c"
, 843); goto error; } while (0); } while (0)
;
844 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~svn299582/tools/polly/lib/External/isl/isl_mat.c"
, 844); goto error; } while (0); } while (0)
;
845
846 if (left->n_row == 0) {
847 isl_mat_free(left);
848 return right;
849 }
850
851 left = isl_mat_cow(left);
852 right = isl_mat_cow(right);
853 if (!left || !right)
854 goto error;
855
856 isl_int_init(a)isl_sioimath_init((a));
857 isl_int_init(b)isl_sioimath_init((b));
858 for (row = 0; row < left->n_row; ++row) {
859 int pivot, first, i, off;
860 pivot = row_abs_min_non_zero(left->row+row, left->n_row-row, row);
861 if (pivot < 0) {
862 isl_int_clear(a)isl_sioimath_clear((a));
863 isl_int_clear(b)isl_sioimath_clear((b));
864 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~svn299582/tools/polly/lib/External/isl/isl_mat.c"
, 864); goto error; } while (0); } while (0)
;
865 }
866 pivot += row;
867 if (pivot != row)
868 if (inv_exchange(&left, &right, pivot, row) < 0)
869 goto error;
870 if (isl_int_is_neg(left->row[row][row])(isl_sioimath_sgn(*(left->row[row][row])) < 0))
871 inv_oppose(left, right, row);
872 first = row+1;
873 while ((off = row_first_non_zero(left->row+first,
874 left->n_row-first, row)) != -1) {
875 first += off;
876 isl_int_fdiv_q(a, left->row[first][row],isl_sioimath_fdiv_q((a), *(left->row[first][row]), *(left->
row[row][row]))
877 left->row[row][row])isl_sioimath_fdiv_q((a), *(left->row[first][row]), *(left->
row[row][row]))
;
878 inv_subtract(left, right, row, first, a);
879 if (!isl_int_is_zero(left->row[first][row])(isl_sioimath_sgn(*(left->row[first][row])) == 0)) {
880 if (inv_exchange(&left, &right, row, first) < 0)
881 goto error;
882 } else {
883 ++first;
884 }
885 }
886 for (i = 0; i < row; ++i) {
887 if (isl_int_is_zero(left->row[i][row])(isl_sioimath_sgn(*(left->row[i][row])) == 0))
888 continue;
889 isl_int_gcd(a, left->row[row][row], left->row[i][row])isl_sioimath_gcd((a), *(left->row[row][row]), *(left->row
[i][row]))
;
890 isl_int_divexact(b, left->row[i][row], a)isl_sioimath_tdiv_q((b), *(left->row[i][row]), *(a));
891 isl_int_divexact(a, left->row[row][row], a)isl_sioimath_tdiv_q((a), *(left->row[row][row]), *(a));
892 isl_int_neg(b, b)isl_sioimath_neg((b), *(b));
893 isl_seq_combine(left->row[i] + i,
894 a, left->row[i] + i,
895 b, left->row[row] + i,
896 left->n_col - i);
897 isl_seq_combine(right->row[i], a, right->row[i],
898 b, right->row[row], right->n_col);
899 }
900 }
901 isl_int_clear(b)isl_sioimath_clear((b));
902
903 isl_int_set(a, left->row[0][0])isl_sioimath_set((a), *(left->row[0][0]));
904 for (row = 1; row < left->n_row; ++row)
905 isl_int_lcm(a, a, left->row[row][row])isl_sioimath_lcm((a), *(a), *(left->row[row][row]));
906 if (isl_int_is_zero(a)(isl_sioimath_sgn(*(a)) == 0)){
907 isl_int_clear(a)isl_sioimath_clear((a));
908 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~svn299582/tools/polly/lib/External/isl/isl_mat.c"
, 908); goto error; } while (0); } while (0)
;
909 }
910 for (row = 0; row < left->n_row; ++row) {
911 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]))
;
912 if (isl_int_is_one(left->row[row][row])(isl_sioimath_cmp_si(*(left->row[row][row]), 1) == 0))
913 continue;
914 isl_seq_scale(right->row[row], right->row[row],
915 left->row[row][row], right->n_col);
916 }
917 isl_int_clear(a)isl_sioimath_clear((a));
918
919 isl_mat_free(left);
920 return right;
921error:
922 isl_mat_free(left);
923 isl_mat_free(right);
924 return NULL((void*)0);
925}
926
927void isl_mat_col_scale(struct isl_mat *mat, unsigned col, isl_int m)
928{
929 int i;
930
931 for (i = 0; i < mat->n_row; ++i)
932 isl_int_mul(mat->row[i][col], mat->row[i][col], m)isl_sioimath_mul((mat->row[i][col]), *(mat->row[i][col]
), *(m))
;
933}
934
935void isl_mat_col_combine(struct isl_mat *mat, unsigned dst,
936 isl_int m1, unsigned src1, isl_int m2, unsigned src2)
937{
938 int i;
939 isl_int tmp;
940
941 isl_int_init(tmp)isl_sioimath_init((tmp));
942 for (i = 0; i < mat->n_row; ++i) {
943 isl_int_mul(tmp, m1, mat->row[i][src1])isl_sioimath_mul((tmp), *(m1), *(mat->row[i][src1]));
944 isl_int_addmul(tmp, m2, mat->row[i][src2])isl_sioimath_addmul((tmp), *(m2), *(mat->row[i][src2]));
945 isl_int_set(mat->row[i][dst], tmp)isl_sioimath_set((mat->row[i][dst]), *(tmp));
946 }
947 isl_int_clear(tmp)isl_sioimath_clear((tmp));
948}
949
950struct isl_mat *isl_mat_right_inverse(struct isl_mat *mat)
951{
952 struct isl_mat *inv;
953 int row;
954 isl_int a, b;
955
956 mat = isl_mat_cow(mat);
957 if (!mat)
958 return NULL((void*)0);
959
960 inv = isl_mat_identity(mat->ctx, mat->n_col);
961 inv = isl_mat_cow(inv);
962 if (!inv)
963 goto error;
964
965 isl_int_init(a)isl_sioimath_init((a));
966 isl_int_init(b)isl_sioimath_init((b));
967 for (row = 0; row < mat->n_row; ++row) {
968 int pivot, first, i, off;
969 pivot = isl_seq_abs_min_non_zero(mat->row[row]+row, mat->n_col-row);
970 if (pivot < 0) {
971 isl_int_clear(a)isl_sioimath_clear((a));
972 isl_int_clear(b)isl_sioimath_clear((b));
973 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~svn299582/tools/polly/lib/External/isl/isl_mat.c"
, 973); goto error; } while (0); } while (0)
;
974 }
975 pivot += row;
976 if (pivot != row)
977 exchange(mat, &inv, NULL((void*)0), row, pivot, row);
978 if (isl_int_is_neg(mat->row[row][row])(isl_sioimath_sgn(*(mat->row[row][row])) < 0))
979 oppose(mat, &inv, NULL((void*)0), row, row);
980 first = row+1;
981 while ((off = isl_seq_first_non_zero(mat->row[row]+first,
982 mat->n_col-first)) != -1) {
983 first += off;
984 isl_int_fdiv_q(a, mat->row[row][first],isl_sioimath_fdiv_q((a), *(mat->row[row][first]), *(mat->
row[row][row]))
985 mat->row[row][row])isl_sioimath_fdiv_q((a), *(mat->row[row][first]), *(mat->
row[row][row]))
;
986 subtract(mat, &inv, NULL((void*)0), row, row, first, a);
987 if (!isl_int_is_zero(mat->row[row][first])(isl_sioimath_sgn(*(mat->row[row][first])) == 0))
988 exchange(mat, &inv, NULL((void*)0), row, row, first);
989 else
990 ++first;
991 }
992 for (i = 0; i < row; ++i) {
993 if (isl_int_is_zero(mat->row[row][i])(isl_sioimath_sgn(*(mat->row[row][i])) == 0))
994 continue;
995 isl_int_gcd(a, mat->row[row][row], mat->row[row][i])isl_sioimath_gcd((a), *(mat->row[row][row]), *(mat->row
[row][i]))
;
996 isl_int_divexact(b, mat->row[row][i], a)isl_sioimath_tdiv_q((b), *(mat->row[row][i]), *(a));
997 isl_int_divexact(a, mat->row[row][row], a)isl_sioimath_tdiv_q((a), *(mat->row[row][row]), *(a));
998 isl_int_neg(a, a)isl_sioimath_neg((a), *(a));
999 isl_mat_col_combine(mat, i, a, i, b, row);
1000 isl_mat_col_combine(inv, i, a, i, b, row);
1001 }
1002 }
1003 isl_int_clear(b)isl_sioimath_clear((b));
1004
1005 isl_int_set(a, mat->row[0][0])isl_sioimath_set((a), *(mat->row[0][0]));
1006 for (row = 1; row < mat->n_row; ++row)
1007 isl_int_lcm(a, a, mat->row[row][row])isl_sioimath_lcm((a), *(a), *(mat->row[row][row]));
1008 if (isl_int_is_zero(a)(isl_sioimath_sgn(*(a)) == 0)){
1009 isl_int_clear(a)isl_sioimath_clear((a));
1010 goto error;
1011 }
1012 for (row = 0; row < mat->n_row; ++row) {
1013 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]))
;
1014 if (isl_int_is_one(mat->row[row][row])(isl_sioimath_cmp_si(*(mat->row[row][row]), 1) == 0))
1015 continue;
1016 isl_mat_col_scale(inv, row, mat->row[row][row]);
1017 }
1018 isl_int_clear(a)isl_sioimath_clear((a));
1019
1020 isl_mat_free(mat);
1021
1022 return inv;
1023error:
1024 isl_mat_free(mat);
1025 isl_mat_free(inv);
1026 return NULL((void*)0);
1027}
1028
1029struct isl_mat *isl_mat_transpose(struct isl_mat *mat)
1030{
1031 struct isl_mat *transpose = NULL((void*)0);
1032 int i, j;
1033
1034 if (!mat)
1035 return NULL((void*)0);
1036
1037 if (mat->n_col == mat->n_row) {
1038 mat = isl_mat_cow(mat);
1039 if (!mat)
1040 return NULL((void*)0);
1041 for (i = 0; i < mat->n_row; ++i)
1042 for (j = i + 1; j < mat->n_col; ++j)
1043 isl_int_swap(mat->row[i][j], mat->row[j][i])isl_sioimath_swap((mat->row[i][j]), (mat->row[j][i]));
1044 return mat;
1045 }
1046 transpose = isl_mat_alloc(mat->ctx, mat->n_col, mat->n_row);
1047 if (!transpose)
1048 goto error;
1049 for (i = 0; i < mat->n_row; ++i)
1050 for (j = 0; j < mat->n_col; ++j)
1051 isl_int_set(transpose->row[j][i], mat->row[i][j])isl_sioimath_set((transpose->row[j][i]), *(mat->row[i][
j]))
;
1052 isl_mat_free(mat);
1053 return transpose;
1054error:
1055 isl_mat_free(mat);
1056 return NULL((void*)0);
1057}
1058
1059struct isl_mat *isl_mat_swap_cols(struct isl_mat *mat, unsigned i, unsigned j)
1060{
1061 int r;
1062
1063 mat = isl_mat_cow(mat);
1064 if (!mat)
1065 return NULL((void*)0);
1066 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~svn299582/tools/polly/lib/External/isl/isl_mat.c"
, 1066); goto error; } while (0); } while (0)
;
1067 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~svn299582/tools/polly/lib/External/isl/isl_mat.c"
, 1067); goto error; } while (0); } while (0)
;
1068
1069 for (r = 0; r < mat->n_row; ++r)
1070 isl_int_swap(mat->row[r][i], mat->row[r][j])isl_sioimath_swap((mat->row[r][i]), (mat->row[r][j]));
1071 return mat;
1072error:
1073 isl_mat_free(mat);
1074 return NULL((void*)0);
1075}
1076
1077struct isl_mat *isl_mat_swap_rows(struct isl_mat *mat, unsigned i, unsigned j)
1078{
1079 isl_int *t;
1080
1081 if (!mat)
1082 return NULL((void*)0);
1083 mat = isl_mat_cow(mat);
1084 if (!mat)
1085 return NULL((void*)0);
1086 t = mat->row[i];
1087 mat->row[i] = mat->row[j];
1088 mat->row[j] = t;
1089 return mat;
1090}
1091
1092/* Calculate the product of two matrices.
1093 *
1094 * This function is optimized for operand matrices that contain many zeros and
1095 * skips multiplications where we know one of the operands is zero.
1096 */
1097__isl_give isl_mat *isl_mat_product(__isl_take isl_mat *left,
1098 __isl_take isl_mat *right)
1099{
1100 int i, j, k;
1101 struct isl_mat *prod;
1102
1103 if (!left || !right)
9
Taking false branch
1104 goto error;
1105 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~svn299582/tools/polly/lib/External/isl/isl_mat.c"
, 1105); goto error; } while (0); } while (0)
;
1106 prod = isl_mat_alloc(left->ctx, left->n_row, right->n_col);
1107 if (!prod)
10
Assuming 'prod' is non-null
11
Taking false branch
1108 goto error;
1109 if (left->n_col == 0) {
12
Assuming the condition is false
13
Taking false branch
1110 for (i = 0; i < prod->n_row; ++i)
1111 isl_seq_clr(prod->row[i], prod->n_col);
1112 isl_mat_free(left);
1113 isl_mat_free(right);
1114 return prod;
1115 }
1116 for (i = 0; i < prod->n_row; ++i) {
14
Assuming the condition is false
15
Loop condition is false. Execution continues on line 1128
1117 for (j = 0; j < prod->n_col; ++j)
1118 isl_int_mul(prod->row[i][j],isl_sioimath_mul((prod->row[i][j]), *(left->row[i][0]),
*(right->row[0][j]))
1119 left->row[i][0], right->row[0][j])isl_sioimath_mul((prod->row[i][j]), *(left->row[i][0]),
*(right->row[0][j]))
;
1120 for (k = 1; k < left->n_col; ++k) {
1121 if (isl_int_is_zero(left->row[i][k])(isl_sioimath_sgn(*(left->row[i][k])) == 0))
1122 continue;
1123 for (j = 0; j < prod->n_col; ++j)
1124 isl_int_addmul(prod->row[i][j],isl_sioimath_addmul((prod->row[i][j]), *(left->row[i][k
]), *(right->row[k][j]))
1125 left->row[i][k], right->row[k][j])isl_sioimath_addmul((prod->row[i][j]), *(left->row[i][k
]), *(right->row[k][j]))
;
1126 }
1127 }
1128 isl_mat_free(left);
1129 isl_mat_free(right);
16
Calling 'isl_mat_free'
22
Returning; memory was released via 1st parameter
1130 return prod;
1131error:
1132 isl_mat_free(left);
1133 isl_mat_free(right);
1134 return NULL((void*)0);
1135}
1136
1137/* Replace the variables x in the rows q by x' given by x = M x',
1138 * with M the matrix mat.
1139 *
1140 * If the number of new variables is greater than the original
1141 * number of variables, then the rows q have already been
1142 * preextended. If the new number is smaller, then the coefficients
1143 * of the divs, which are not changed, need to be shifted down.
1144 * The row q may be the equalities, the inequalities or the
1145 * div expressions. In the latter case, has_div is true and
1146 * we need to take into account the extra denominator column.
1147 */
1148static int preimage(struct isl_ctx *ctx, isl_int **q, unsigned n,
1149 unsigned n_div, int has_div, struct isl_mat *mat)
1150{
1151 int i;
1152 struct isl_mat *t;
1153 int e;
1154
1155 if (mat->n_col >= mat->n_row)
1156 e = 0;
1157 else
1158 e = mat->n_row - mat->n_col;
1159 if (has_div)
1160 for (i = 0; i < n; ++i)
1161 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]));
1162 t = isl_mat_sub_alloc6(mat->ctx, q, 0, n, has_div, mat->n_row);
1163 t = isl_mat_product(t, mat);
1164 if (!t)
1165 return -1;
1166 for (i = 0; i < n; ++i) {
1167 isl_seq_swp_or_cpy(q[i] + has_div, t->row[i], t->n_col);
1168 isl_seq_cpy(q[i] + has_div + t->n_col,
1169 q[i] + has_div + t->n_col + e, n_div);
1170 isl_seq_clr(q[i] + has_div + t->n_col + n_div, e);
1171 }
1172 isl_mat_free(t);
1173 return 0;
1174}
1175
1176/* Replace the variables x in bset by x' given by x = M x', with
1177 * M the matrix mat.
1178 *
1179 * If there are fewer variables x' then there are x, then we perform
1180 * the transformation in place, which means that, in principle,
1181 * this frees up some extra variables as the number
1182 * of columns remains constant, but we would have to extend
1183 * the div array too as the number of rows in this array is assumed
1184 * to be equal to extra.
1185 */
1186struct isl_basic_setisl_basic_map *isl_basic_set_preimage(struct isl_basic_setisl_basic_map *bset,
1187 struct isl_mat *mat)
1188{
1189 struct isl_ctx *ctx;
1190
1191 if (!bset || !mat)
1192 goto error;
1193
1194 ctx = bset->ctx;
1195 bset = isl_basic_set_cow(bset);
1196 if (!bset)
1197 goto error;
1198
1199 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~svn299582/tools/polly/lib/External/isl/isl_mat.c"
, 1199); goto error; } while (0); } while (0)
;
1200 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~svn299582/tools/polly/lib/External/isl/isl_mat.c"
, 1200); goto error; } while (0); } while (0)
;
1201 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~svn299582/tools/polly/lib/External/isl/isl_mat.c"
, 1201); goto error; } while (0); } while (0)
;
1202
1203 if (mat->n_col > mat->n_row) {
1204 bset = isl_basic_set_extend(bset, 0, mat->n_col-1, 0, 0, 0);
1205 if (!bset)
1206 goto error;
1207 } else if (mat->n_col < mat->n_row) {
1208 bset->dim = isl_space_cow(bset->dim);
1209 if (!bset->dim)
1210 goto error;
1211 bset->dim->n_out -= mat->n_row - mat->n_col;
1212 }
1213
1214 if (preimage(ctx, bset->eq, bset->n_eq, bset->n_div, 0,
1215 isl_mat_copy(mat)) < 0)
1216 goto error;
1217
1218 if (preimage(ctx, bset->ineq, bset->n_ineq, bset->n_div, 0,
1219 isl_mat_copy(mat)) < 0)
1220 goto error;
1221
1222 if (preimage(ctx, bset->div, bset->n_div, bset->n_div, 1, mat) < 0)
1223 goto error2;
1224
1225 ISL_F_CLR(bset, ISL_BASIC_SET_NO_IMPLICIT)(((bset)->flags) &= ~((1 << 2)));
1226 ISL_F_CLR(bset, ISL_BASIC_SET_NO_REDUNDANT)(((bset)->flags) &= ~((1 << 3)));
1227 ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED)(((bset)->flags) &= ~((1 << 5)));
1228 ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED_DIVS)(((bset)->flags) &= ~((1 << 6)));
1229 ISL_F_CLR(bset, ISL_BASIC_SET_ALL_EQUALITIES)(((bset)->flags) &= ~((1 << 7)));
1230
1231 bset = isl_basic_set_simplify(bset);
1232 bset = isl_basic_set_finalize(bset);
1233
1234 return bset;
1235error:
1236 isl_mat_free(mat);
1237error2:
1238 isl_basic_set_free(bset);
1239 return NULL((void*)0);
1240}
1241
1242struct isl_setisl_map *isl_set_preimage(struct isl_setisl_map *set, struct isl_mat *mat)
1243{
1244 int i;
1245
1246 set = isl_set_cow(set);
1247 if (!set)
1248 goto error;
1249
1250 for (i = 0; i < set->n; ++i) {
1251 set->p[i] = isl_basic_set_preimage(set->p[i],
1252 isl_mat_copy(mat));
1253 if (!set->p[i])
1254 goto error;
1255 }
1256 if (mat->n_col != mat->n_row) {
1257 set->dim = isl_space_cow(set->dim);
1258 if (!set->dim)
1259 goto error;
1260 set->dim->n_out += mat->n_col;
1261 set->dim->n_out -= mat->n_row;
1262 }
1263 isl_mat_free(mat);
1264 ISL_F_CLR(set, ISL_SET_NORMALIZED)(((set)->flags) &= ~((1 << 1)));
1265 return set;
1266error:
1267 isl_set_free(set);
1268 isl_mat_free(mat);
1269 return NULL((void*)0);
1270}
1271
1272/* Replace the variables x starting at pos in the rows q
1273 * by x' with x = M x' with M the matrix mat.
1274 * That is, replace the corresponding coefficients c by c M.
1275 */
1276static int transform(isl_ctx *ctx, isl_int **q, unsigned n,
1277 unsigned pos, __isl_take isl_mat *mat)
1278{
1279 int i;
1280 isl_mat *t;
1281
1282 t = isl_mat_sub_alloc6(ctx, q, 0, n, pos, mat->n_row);
1283 t = isl_mat_product(t, mat);
8
Calling 'isl_mat_product'
23
Returning; memory was released via 2nd parameter
1284 if (!t)
24
Taking false branch
1285 return -1;
1286 for (i = 0; i < n; ++i)
25
Loop condition is false. Execution continues on line 1288
1287 isl_seq_swp_or_cpy(q[i] + pos, t->row[i], t->n_col);
1288 isl_mat_free(t);
1289 return 0;
1290}
1291
1292/* Replace the variables x of type "type" starting at "first" in "bmap"
1293 * by x' with x = M x' with M the matrix trans.
1294 * That is, replace the corresponding coefficients c by c M.
1295 *
1296 * The transformation matrix should be a square matrix.
1297 */
1298__isl_give isl_basic_map *isl_basic_map_transform_dims(
1299 __isl_take isl_basic_map *bmap, enum isl_dim_type type, unsigned first,
1300 __isl_take isl_mat *trans)
1301{
1302 isl_ctx *ctx;
1303 unsigned pos;
1304
1305 bmap = isl_basic_map_cow(bmap);
1306 if (!bmap || !trans)
2
Assuming 'bmap' is non-null
3
Assuming 'trans' is non-null
4
Taking false branch
1307 goto error;
1308
1309 ctx = isl_basic_map_get_ctx(bmap);
1310 if (trans->n_row != trans->n_col)
5
Taking false branch
1311 isl_die(trans->ctx, isl_error_invalid,do { isl_handle_error(trans->ctx, isl_error_invalid, "expecting square transformation matrix"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn299582/tools/polly/lib/External/isl/isl_mat.c"
, 1312); goto error; } while (0)
1312 "expecting square transformation matrix", goto error)do { isl_handle_error(trans->ctx, isl_error_invalid, "expecting square transformation matrix"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn299582/tools/polly/lib/External/isl/isl_mat.c"
, 1312); goto error; } while (0)
;
1313 if (first + trans->n_row > isl_basic_map_dim(bmap, type))
6
Taking false branch
1314 isl_die(trans->ctx, isl_error_invalid,do { isl_handle_error(trans->ctx, isl_error_invalid, "oversized transformation matrix"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn299582/tools/polly/lib/External/isl/isl_mat.c"
, 1315); goto error; } while (0)
1315 "oversized transformation matrix", goto error)do { isl_handle_error(trans->ctx, isl_error_invalid, "oversized transformation matrix"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn299582/tools/polly/lib/External/isl/isl_mat.c"
, 1315); goto error; } while (0)
;
1316
1317 pos = isl_basic_map_offset(bmap, type) + first;
1318
1319 if (transform(ctx, bmap->eq, bmap->n_eq, pos, isl_mat_copy(trans)) < 0)
7
Calling 'transform'
26
Returning; memory was released via 5th parameter
27
Taking false branch
1320 goto error;
1321 if (transform(ctx, bmap->ineq, bmap->n_ineq, pos,
1322 isl_mat_copy(trans)) < 0)
28
Use of memory after it is freed
1323 goto error;
1324 if (transform(ctx, bmap->div, bmap->n_div, 1 + pos,
1325 isl_mat_copy(trans)) < 0)
1326 goto error;
1327
1328 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED)(((bmap)->flags) &= ~((1 << 5)));
1329 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS)(((bmap)->flags) &= ~((1 << 6)));
1330
1331 isl_mat_free(trans);
1332 return bmap;
1333error:
1334 isl_mat_free(trans);
1335 isl_basic_map_free(bmap);
1336 return NULL((void*)0);
1337}
1338
1339/* Replace the variables x of type "type" starting at "first" in "bset"
1340 * by x' with x = M x' with M the matrix trans.
1341 * That is, replace the corresponding coefficients c by c M.
1342 *
1343 * The transformation matrix should be a square matrix.
1344 */
1345__isl_give isl_basic_setisl_basic_map *isl_basic_set_transform_dims(
1346 __isl_take isl_basic_setisl_basic_map *bset, enum isl_dim_type type, unsigned first,
1347 __isl_take isl_mat *trans)
1348{
1349 return isl_basic_map_transform_dims(bset, type, first, trans);
1
Calling 'isl_basic_map_transform_dims'
1350}
1351
1352void isl_mat_print_internal(__isl_keep isl_mat *mat, FILE *out, int indent)
1353{
1354 int i, j;
1355
1356 if (!mat) {
1357 fprintf(out, "%*snull mat\n", indent, "");
1358 return;
1359 }
1360
1361 if (mat->n_row == 0)
1362 fprintf(out, "%*s[]\n", indent, "");
1363
1364 for (i = 0; i < mat->n_row; ++i) {
1365 if (!i)
1366 fprintf(out, "%*s[[", indent, "");
1367 else
1368 fprintf(out, "%*s[", indent+1, "");
1369 for (j = 0; j < mat->n_col; ++j) {
1370 if (j)
1371 fprintf(out, ",");
1372 isl_int_print(out, mat->row[i][j], 0)isl_sioimath_print(out, *(mat->row[i][j]), 0);
1373 }
1374 if (i == mat->n_row-1)
1375 fprintf(out, "]]\n");
1376 else
1377 fprintf(out, "]\n");
1378 }
1379}
1380
1381void isl_mat_dump(__isl_keep isl_mat *mat)
1382{
1383 isl_mat_print_internal(mat, stderrstderr, 0);
1384}
1385
1386struct isl_mat *isl_mat_drop_cols(struct isl_mat *mat, unsigned col, unsigned n)
1387{
1388 int r;
1389
1390 if (n == 0)
1391 return mat;
1392
1393 mat = isl_mat_cow(mat);
1394 if (!mat)
1395 return NULL((void*)0);
1396
1397 if (col != mat->n_col-n) {
1398 for (r = 0; r < mat->n_row; ++r)
1399 isl_seq_cpy(mat->row[r]+col, mat->row[r]+col+n,
1400 mat->n_col - col - n);
1401 }
1402 mat->n_col -= n;
1403 return mat;
1404}
1405
1406struct isl_mat *isl_mat_drop_rows(struct isl_mat *mat, unsigned row, unsigned n)
1407{
1408 int r;
1409
1410 mat = isl_mat_cow(mat);
1411 if (!mat)
1412 return NULL((void*)0);
1413
1414 for (r = row; r+n < mat->n_row; ++r)
1415 mat->row[r] = mat->row[r+n];
1416
1417 mat->n_row -= n;
1418 return mat;
1419}
1420
1421__isl_give isl_mat *isl_mat_insert_cols(__isl_take isl_mat *mat,
1422 unsigned col, unsigned n)
1423{
1424 isl_mat *ext;
1425
1426 if (!mat)
1427 return NULL((void*)0);
1428 if (n == 0)
1429 return mat;
1430
1431 ext = isl_mat_alloc(mat->ctx, mat->n_row, mat->n_col + n);
1432 if (!ext)
1433 goto error;
1434
1435 isl_mat_sub_copy(mat->ctx, ext->row, mat->row, mat->n_row, 0, 0, col);
1436 isl_mat_sub_copy(mat->ctx, ext->row, mat->row, mat->n_row,
1437 col + n, col, mat->n_col - col);
1438
1439 isl_mat_free(mat);
1440 return ext;
1441error:
1442 isl_mat_free(mat);
1443 return NULL((void*)0);
1444}
1445
1446__isl_give isl_mat *isl_mat_insert_zero_cols(__isl_take isl_mat *mat,
1447 unsigned first, unsigned n)
1448{
1449 int i;
1450
1451 if (!mat)
1452 return NULL((void*)0);
1453 mat = isl_mat_insert_cols(mat, first, n);
1454 if (!mat)
1455 return NULL((void*)0);
1456
1457 for (i = 0; i < mat->n_row; ++i)
1458 isl_seq_clr(mat->row[i] + first, n);
1459
1460 return mat;
1461}
1462
1463__isl_give isl_mat *isl_mat_add_zero_cols(__isl_take isl_mat *mat, unsigned n)
1464{
1465 if (!mat)
1466 return NULL((void*)0);
1467
1468 return isl_mat_insert_zero_cols(mat, mat->n_col, n);
1469}
1470
1471__isl_give isl_mat *isl_mat_insert_rows(__isl_take isl_mat *mat,
1472 unsigned row, unsigned n)
1473{
1474 isl_mat *ext;
1475
1476 if (!mat)
1477 return NULL((void*)0);
1478 if (n == 0)
1479 return mat;
1480
1481 ext = isl_mat_alloc(mat->ctx, mat->n_row + n, mat->n_col);
1482 if (!ext)
1483 goto error;
1484
1485 isl_mat_sub_copy(mat->ctx, ext->row, mat->row, row, 0, 0, mat->n_col);
1486 isl_mat_sub_copy(mat->ctx, ext->row + row + n, mat->row + row,
1487 mat->n_row - row, 0, 0, mat->n_col);
1488
1489 isl_mat_free(mat);
1490 return ext;
1491error:
1492 isl_mat_free(mat);
1493 return NULL((void*)0);
1494}
1495
1496__isl_give isl_mat *isl_mat_add_rows(__isl_take isl_mat *mat, unsigned n)
1497{
1498 if (!mat)
1499 return NULL((void*)0);
1500
1501 return isl_mat_insert_rows(mat, mat->n_row, n);
1502}
1503
1504__isl_give isl_mat *isl_mat_insert_zero_rows(__isl_take isl_mat *mat,
1505 unsigned row, unsigned n)
1506{
1507 int i;
1508
1509 mat = isl_mat_insert_rows(mat, row, n);
1510 if (!mat)
1511 return NULL((void*)0);
1512
1513 for (i = 0; i < n; ++i)
1514 isl_seq_clr(mat->row[row + i], mat->n_col);
1515
1516 return mat;
1517}
1518
1519__isl_give isl_mat *isl_mat_add_zero_rows(__isl_take isl_mat *mat, unsigned n)
1520{
1521 if (!mat)
1522 return NULL((void*)0);
1523
1524 return isl_mat_insert_zero_rows(mat, mat->n_row, n);
1525}
1526
1527void isl_mat_col_submul(struct isl_mat *mat,
1528 int dst_col, isl_int f, int src_col)
1529{
1530 int i;
1531
1532 for (i = 0; i < mat->n_row; ++i)
1533 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]))
;
1534}
1535
1536void isl_mat_col_add(__isl_keep isl_mat *mat, int dst_col, int src_col)
1537{
1538 int i;
1539
1540 if (!mat)
1541 return;
1542
1543 for (i = 0; i < mat->n_row; ++i)
1544 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]))
1545 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]))
;
1546}
1547
1548void isl_mat_col_mul(struct isl_mat *mat, int dst_col, isl_int f, int src_col)
1549{
1550 int i;
1551
1552 for (i = 0; i < mat->n_row; ++i)
1553 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]))
;
1554}
1555
1556/* Add "f" times column "src_col" to column "dst_col" of "mat" and
1557 * return the result.
1558 */
1559__isl_give isl_mat *isl_mat_col_addmul(__isl_take isl_mat *mat, int dst_col,
1560 isl_int f, int src_col)
1561{
1562 int i;
1563
1564 if (check_col(mat, dst_col) < 0 || check_col(mat, src_col) < 0)
1565 return isl_mat_free(mat);
1566
1567 for (i = 0; i < mat->n_row; ++i) {
1568 if (isl_int_is_zero(mat->row[i][src_col])(isl_sioimath_sgn(*(mat->row[i][src_col])) == 0))
1569 continue;
1570 mat = isl_mat_cow(mat);
1571 if (!mat)
1572 return NULL((void*)0);
1573 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]))
;
1574 }
1575
1576 return mat;
1577}
1578
1579/* Negate column "col" of "mat" and return the result.
1580 */
1581__isl_give isl_mat *isl_mat_col_neg(__isl_take isl_mat *mat, int col)
1582{
1583 int i;
1584
1585 if (check_col(mat, col) < 0)
1586 return isl_mat_free(mat);
1587
1588 for (i = 0; i < mat->n_row; ++i) {
1589 if (isl_int_is_zero(mat->row[i][col])(isl_sioimath_sgn(*(mat->row[i][col])) == 0))
1590 continue;
1591 mat = isl_mat_cow(mat);
1592 if (!mat)
1593 return NULL((void*)0);
1594 isl_int_neg(mat->row[i][col], mat->row[i][col])isl_sioimath_neg((mat->row[i][col]), *(mat->row[i][col]
))
;
1595 }
1596
1597 return mat;
1598}
1599
1600struct isl_mat *isl_mat_unimodular_complete(struct isl_mat *M, int row)
1601{
1602 int r;
1603 struct isl_mat *H = NULL((void*)0), *Q = NULL((void*)0);
1604
1605 if (!M)
1606 return NULL((void*)0);
1607
1608 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~svn299582/tools/polly/lib/External/isl/isl_mat.c"
, 1608); goto error; } while (0); } while (0)
;
1609 M->n_row = row;
1610 H = isl_mat_left_hermite(isl_mat_copy(M), 0, NULL((void*)0), &Q);
1611 M->n_row = M->n_col;
1612 if (!H)
1613 goto error;
1614 for (r = 0; r < row; ++r)
1615 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~svn299582/tools/polly/lib/External/isl/isl_mat.c"
, 1615); goto error; } while (0); } while (0)
;
1616 for (r = row; r < M->n_row; ++r)
1617 isl_seq_cpy(M->row[r], Q->row[r], M->n_col);
1618 isl_mat_free(H);
1619 isl_mat_free(Q);
1620 return M;
1621error:
1622 isl_mat_free(H);
1623 isl_mat_free(Q);
1624 isl_mat_free(M);
1625 return NULL((void*)0);
1626}
1627
1628__isl_give isl_mat *isl_mat_concat(__isl_take isl_mat *top,
1629 __isl_take isl_mat *bot)
1630{
1631 struct isl_mat *mat;
1632
1633 if (!top || !bot)
1634 goto error;
1635
1636 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~svn299582/tools/polly/lib/External/isl/isl_mat.c"
, 1636); goto error; } while (0); } while (0)
;
1637 if (top->n_row == 0) {
1638 isl_mat_free(top);
1639 return bot;
1640 }
1641 if (bot->n_row == 0) {
1642 isl_mat_free(bot);
1643 return top;
1644 }
1645
1646 mat = isl_mat_alloc(top->ctx, top->n_row + bot->n_row, top->n_col);
1647 if (!mat)
1648 goto error;
1649 isl_mat_sub_copy(mat->ctx, mat->row, top->row, top->n_row,
1650 0, 0, mat->n_col);
1651 isl_mat_sub_copy(mat->ctx, mat->row + top->n_row, bot->row, bot->n_row,
1652 0, 0, mat->n_col);
1653 isl_mat_free(top);
1654 isl_mat_free(bot);
1655 return mat;
1656error:
1657 isl_mat_free(top);
1658 isl_mat_free(bot);
1659 return NULL((void*)0);
1660}
1661
1662isl_bool isl_mat_is_equal(__isl_keep isl_mat *mat1, __isl_keep isl_mat *mat2)
1663{
1664 int i;
1665
1666 if (!mat1 || !mat2)
1667 return isl_bool_error;
1668
1669 if (mat1->n_row != mat2->n_row)
1670 return isl_bool_false;
1671
1672 if (mat1->n_col != mat2->n_col)
1673 return isl_bool_false;
1674
1675 for (i = 0; i < mat1->n_row; ++i)
1676 if (!isl_seq_eq(mat1->row[i], mat2->row[i], mat1->n_col))
1677 return isl_bool_false;
1678
1679 return isl_bool_true;
1680}
1681
1682__isl_give isl_mat *isl_mat_from_row_vec(__isl_take isl_vec *vec)
1683{
1684 struct isl_mat *mat;
1685
1686 if (!vec)
1687 return NULL((void*)0);
1688 mat = isl_mat_alloc(vec->ctx, 1, vec->size);
1689 if (!mat)
1690 goto error;
1691
1692 isl_seq_cpy(mat->row[0], vec->el, vec->size);
1693
1694 isl_vec_free(vec);
1695 return mat;
1696error:
1697 isl_vec_free(vec);
1698 return NULL((void*)0);
1699}
1700
1701/* Return a copy of row "row" of "mat" as an isl_vec.
1702 */
1703__isl_give isl_vec *isl_mat_get_row(__isl_keep isl_mat *mat, unsigned row)
1704{
1705 isl_vec *v;
1706
1707 if (!mat)
1708 return NULL((void*)0);
1709 if (row >= mat->n_row)
1710 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~svn299582/tools/polly/lib/External/isl/isl_mat.c"
, 1711); return ((void*)0); } while (0)
1711 return NULL)do { isl_handle_error(mat->ctx, isl_error_invalid, "row out of range"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn299582/tools/polly/lib/External/isl/isl_mat.c"
, 1711); return ((void*)0); } while (0)
;
1712
1713 v = isl_vec_alloc(isl_mat_get_ctx(mat), mat->n_col);
1714 if (!v)
1715 return NULL((void*)0);
1716 isl_seq_cpy(v->el, mat->row[row], mat->n_col);
1717
1718 return v;
1719}
1720
1721__isl_give isl_mat *isl_mat_vec_concat(__isl_take isl_mat *top,
1722 __isl_take isl_vec *bot)
1723{
1724 return isl_mat_concat(top, isl_mat_from_row_vec(bot));
1725}
1726
1727__isl_give isl_mat *isl_mat_move_cols(__isl_take isl_mat *mat,
1728 unsigned dst_col, unsigned src_col, unsigned n)
1729{
1730 isl_mat *res;
1731
1732 if (!mat)
1733 return NULL((void*)0);
1734 if (n == 0 || dst_col == src_col)
1735 return mat;
1736
1737 res = isl_mat_alloc(mat->ctx, mat->n_row, mat->n_col);
1738 if (!res)
1739 goto error;
1740
1741 if (dst_col < src_col) {
1742 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1743 0, 0, dst_col);
1744 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1745 dst_col, src_col, n);
1746 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1747 dst_col + n, dst_col, src_col - dst_col);
1748 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1749 src_col + n, src_col + n,
1750 res->n_col - src_col - n);
1751 } else {
1752 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1753 0, 0, src_col);
1754 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1755 src_col, src_col + n, dst_col - src_col);
1756 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1757 dst_col, src_col, n);
1758 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1759 dst_col + n, dst_col + n,
1760 res->n_col - dst_col - n);
1761 }
1762 isl_mat_free(mat);
1763
1764 return res;
1765error:
1766 isl_mat_free(mat);
1767 return NULL((void*)0);
1768}
1769
1770/* Return the gcd of the elements in row "row" of "mat" in *gcd.
1771 * Return isl_stat_ok on success and isl_stat_error on failure.
1772 */
1773isl_stat isl_mat_row_gcd(__isl_keep isl_mat *mat, int row, isl_int *gcd)
1774{
1775 if (!mat)
1776 return isl_stat_error;
1777
1778 if (row < 0 || row >= mat->n_row)
1779 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~svn299582/tools/polly/lib/External/isl/isl_mat.c"
, 1780); return isl_stat_error; } while (0)
1780 "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~svn299582/tools/polly/lib/External/isl/isl_mat.c"
, 1780); return isl_stat_error; } while (0)
;
1781 isl_seq_gcd(mat->row[row], mat->n_col, gcd);
1782
1783 return isl_stat_ok;
1784}
1785
1786void isl_mat_gcd(__isl_keep isl_mat *mat, isl_int *gcd)
1787{
1788 int i;
1789 isl_int g;
1790
1791 isl_int_set_si(*gcd, 0)isl_sioimath_set_si((*gcd), 0);
1792 if (!mat)
1793 return;
1794
1795 isl_int_init(g)isl_sioimath_init((g));
1796 for (i = 0; i < mat->n_row; ++i) {
1797 isl_seq_gcd(mat->row[i], mat->n_col, &g);
1798 isl_int_gcd(*gcd, *gcd, g)isl_sioimath_gcd((*gcd), *(*gcd), *(g));
1799 }
1800 isl_int_clear(g)isl_sioimath_clear((g));
1801}
1802
1803/* Return the result of scaling "mat" by a factor of "m".
1804 */
1805__isl_give isl_mat *isl_mat_scale(__isl_take isl_mat *mat, isl_int m)
1806{
1807 int i;
1808
1809 if (isl_int_is_one(m)(isl_sioimath_cmp_si(*(m), 1) == 0))
1810 return mat;
1811
1812 mat = isl_mat_cow(mat);
1813 if (!mat)
1814 return NULL((void*)0);
1815
1816 for (i = 0; i < mat->n_row; ++i)
1817 isl_seq_scale(mat->row[i], mat->row[i], m, mat->n_col);
1818
1819 return mat;
1820}
1821
1822__isl_give isl_mat *isl_mat_scale_down(__isl_take isl_mat *mat, isl_int m)
1823{
1824 int i;
1825
1826 if (isl_int_is_one(m)(isl_sioimath_cmp_si(*(m), 1) == 0))
1827 return mat;
1828
1829 mat = isl_mat_cow(mat);
1830 if (!mat)
1831 return NULL((void*)0);
1832
1833 for (i = 0; i < mat->n_row; ++i)
1834 isl_seq_scale_down(mat->row[i], mat->row[i], m, mat->n_col);
1835
1836 return mat;
1837}
1838
1839__isl_give isl_mat *isl_mat_scale_down_row(__isl_take isl_mat *mat, int row,
1840 isl_int m)
1841{
1842 if (isl_int_is_one(m)(isl_sioimath_cmp_si(*(m), 1) == 0))
1843 return mat;
1844
1845 mat = isl_mat_cow(mat);
1846 if (!mat)
1847 return NULL((void*)0);
1848
1849 isl_seq_scale_down(mat->row[row], mat->row[row], m, mat->n_col);
1850
1851 return mat;
1852}
1853
1854__isl_give isl_mat *isl_mat_normalize(__isl_take isl_mat *mat)
1855{
1856 isl_int gcd;
1857
1858 if (!mat)
1859 return NULL((void*)0);
1860
1861 isl_int_init(gcd)isl_sioimath_init((gcd));
1862 isl_mat_gcd(mat, &gcd);
1863 mat = isl_mat_scale_down(mat, gcd);
1864 isl_int_clear(gcd)isl_sioimath_clear((gcd));
1865
1866 return mat;
1867}
1868
1869__isl_give isl_mat *isl_mat_normalize_row(__isl_take isl_mat *mat, int row)
1870{
1871 mat = isl_mat_cow(mat);
1872 if (!mat)
1873 return NULL((void*)0);
1874
1875 isl_seq_normalize(mat->ctx, mat->row[row], mat->n_col);
1876
1877 return mat;
1878}
1879
1880/* Number of initial non-zero columns.
1881 */
1882int isl_mat_initial_non_zero_cols(__isl_keep isl_mat *mat)
1883{
1884 int i;
1885
1886 if (!mat)
1887 return -1;
1888
1889 for (i = 0; i < mat->n_col; ++i)
1890 if (row_first_non_zero(mat->row, mat->n_row, i) < 0)
1891 break;
1892
1893 return i;
1894}