2 * Copyright 2011 INRIA Saclay
3 * Copyright 2014 Ecole Normale Superieure
5 * Use of this software is governed by the MIT license
7 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
8 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
10 * and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
13 #include <isl/space.h>
14 #include <isl_vec_private.h>
15 #include <isl_mat_private.h>
16 #include <isl_reordering.h>
18 #include <isl_local_private.h>
20 /* Return the isl_ctx to which "local" belongs.
22 isl_ctx
*isl_local_get_ctx(__isl_keep isl_local
*local
)
27 return isl_mat_get_ctx(local
);
30 /* Create an isl_local object from a matrix describing
33 * An isl_local object is current defined as exactly such a matrix,
34 * so simply return the input.
36 __isl_give isl_local
*isl_local_alloc_from_mat(__isl_take isl_mat
*mat
)
41 /* Free "local" and return NULL.
43 __isl_null isl_local
*isl_local_free(__isl_take isl_local
*local
)
49 /* Return the number of local variables (isl_dim_div),
50 * the number of other variables (isl_dim_set) or
51 * the total number of variables (isl_dim_all) in "local".
53 * Other types do not have any meaning for an isl_local object.
55 int isl_local_dim(__isl_keep isl_local
*local
, enum isl_dim_type type
)
61 if (type
== isl_dim_div
)
62 return isl_mat_rows(mat
);
63 if (type
== isl_dim_all
)
64 return isl_mat_cols(mat
) - 2;
65 if (type
== isl_dim_set
)
66 return isl_local_dim(local
, isl_dim_all
) -
67 isl_local_dim(local
, isl_dim_div
);
68 isl_die(isl_local_get_ctx(local
), isl_error_unsupported
,
69 "unsupported dimension type", return 0);
73 #define TYPE isl_local
75 #include "check_type_range_templ.c"
77 /* Check that "pos" is a valid position for a variable in "local".
79 static isl_stat
isl_local_check_pos(__isl_keep isl_local
*local
, int pos
)
81 return isl_local_check_range(local
, isl_dim_div
, pos
, 1);
84 /* Given local variables "local",
85 * is the variable at position "pos" marked as not having
86 * an explicit representation?
87 * Note that even if this variable is not marked in this way and therefore
88 * does have an explicit representation, this representation may still
89 * depend (indirectly) on other local variables that do not
90 * have an explicit representation.
92 isl_bool
isl_local_div_is_marked_unknown(__isl_keep isl_local
*local
, int pos
)
96 if (isl_local_check_pos(local
, pos
) < 0)
97 return isl_bool_error
;
98 return isl_int_is_zero(mat
->row
[pos
][0]);
101 /* Given local variables "local",
102 * does the variable at position "pos" have a complete explicit representation?
103 * Having a complete explicit representation requires not only
104 * an explicit representation, but also that all local variables
105 * that appear in this explicit representation in turn have
106 * a complete explicit representation.
108 isl_bool
isl_local_div_is_known(__isl_keep isl_local
*local
, int pos
)
112 isl_mat
*mat
= local
;
114 if (isl_local_check_pos(local
, pos
) < 0)
115 return isl_bool_error
;
117 marked
= isl_local_div_is_marked_unknown(local
, pos
);
118 if (marked
< 0 || marked
)
119 return isl_bool_not(marked
);
121 n
= isl_local_dim(local
, isl_dim_div
);
122 off
= isl_mat_cols(mat
) - n
;
124 for (i
= n
- 1; i
>= 0; --i
) {
127 if (isl_int_is_zero(mat
->row
[pos
][off
+ i
]))
129 known
= isl_local_div_is_known(local
, i
);
130 if (known
< 0 || !known
)
134 return isl_bool_true
;
137 /* Does "local" have an explicit representation for all local variables?
139 isl_bool
isl_local_divs_known(__isl_keep isl_local
*local
)
144 return isl_bool_error
;
146 n
= isl_local_dim(local
, isl_dim_div
);
147 for (i
= 0; i
< n
; ++i
) {
148 isl_bool unknown
= isl_local_div_is_marked_unknown(local
, i
);
149 if (unknown
< 0 || unknown
)
150 return isl_bool_not(unknown
);
153 return isl_bool_true
;
156 /* Compare two sets of local variables, defined over
159 * Return -1 if "local1" is "smaller" than "local2", 1 if "local1" is "greater"
160 * than "local2" and 0 if they are equal.
162 * The order is fairly arbitrary. We do "prefer" divs that only involve
163 * earlier dimensions in the sense that we consider matrices where
164 * the first differing div involves earlier dimensions to be smaller.
166 int isl_local_cmp(__isl_keep isl_local
*local1
, __isl_keep isl_local
*local2
)
170 isl_bool unknown1
, unknown2
;
173 isl_mat
*mat1
= local1
;
174 isl_mat
*mat2
= local2
;
176 if (local1
== local2
)
183 if (mat1
->n_row
!= mat2
->n_row
)
184 return mat1
->n_row
- mat2
->n_row
;
186 n_col
= isl_mat_cols(mat1
);
187 for (i
= 0; i
< mat1
->n_row
; ++i
) {
188 unknown1
= isl_local_div_is_marked_unknown(local1
, i
);
189 unknown2
= isl_local_div_is_marked_unknown(local2
, i
);
190 if (unknown1
&& unknown2
)
196 last1
= isl_seq_last_non_zero(mat1
->row
[i
] + 1, n_col
- 1);
197 last2
= isl_seq_last_non_zero(mat2
->row
[i
] + 1, n_col
- 1);
199 return last1
- last2
;
200 cmp
= isl_seq_cmp(mat1
->row
[i
], mat2
->row
[i
], n_col
);
208 /* Reorder the columns of the given local variables according to the
210 * The order of the local variables themselves is assumed not to change.
212 __isl_give isl_local
*isl_local_reorder(__isl_take isl_local
*local
,
213 __isl_take isl_reordering
*r
)
215 isl_mat
*div
= local
;
224 space
= isl_reordering_peek_space(r
);
225 extra
= isl_space_dim(space
, isl_dim_all
) + div
->n_row
- r
->len
;
226 mat
= isl_mat_alloc(div
->ctx
, div
->n_row
, div
->n_col
+ extra
);
230 for (i
= 0; i
< div
->n_row
; ++i
) {
231 isl_seq_cpy(mat
->row
[i
], div
->row
[i
], 2);
232 isl_seq_clr(mat
->row
[i
] + 2, mat
->n_col
- 2);
233 for (j
= 0; j
< r
->len
; ++j
)
234 isl_int_set(mat
->row
[i
][2 + r
->pos
[j
]],
238 isl_reordering_free(r
);
239 isl_local_free(local
);
240 return isl_local_alloc_from_mat(mat
);
242 isl_reordering_free(r
);
243 isl_local_free(local
);
247 /* Extend a vector "v" representing an integer point
248 * in the domain space of "local"
249 * to one that also includes values for the local variables.
250 * All local variables are required to have an explicit representation.
252 __isl_give isl_vec
*isl_local_extend_point_vec(__isl_keep isl_local
*local
,
253 __isl_take isl_vec
*v
)
257 isl_mat
*mat
= local
;
260 return isl_vec_free(v
);
261 known
= isl_local_divs_known(local
);
263 return isl_vec_free(v
);
265 isl_die(isl_local_get_ctx(local
), isl_error_invalid
,
266 "unknown local variables", return isl_vec_free(v
));
267 if (isl_vec_size(v
) != 1 + isl_local_dim(local
, isl_dim_set
))
268 isl_die(isl_local_get_ctx(local
), isl_error_invalid
,
269 "incorrect size", return isl_vec_free(v
));
270 if (!isl_int_is_one(v
->el
[0]))
271 isl_die(isl_local_get_ctx(local
), isl_error_invalid
,
272 "expecting integer point", return isl_vec_free(v
));
273 n_div
= isl_local_dim(local
, isl_dim_div
);
276 unsigned dim
= isl_local_dim(local
, isl_dim_set
);
277 v
= isl_vec_add_els(v
, n_div
);
281 for (i
= 0; i
< n_div
; ++i
) {
282 isl_seq_inner_product(mat
->row
[i
] + 1, v
->el
,
283 1 + dim
+ i
, &v
->el
[1+dim
+i
]);
284 isl_int_fdiv_q(v
->el
[1+dim
+i
], v
->el
[1+dim
+i
],