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>
17 #include <isl_local.h>
19 /* Return the isl_ctx to which "local" belongs.
21 isl_ctx
*isl_local_get_ctx(__isl_keep isl_local
*local
)
26 return isl_mat_get_ctx(local
);
29 /* Return the number of local variables (isl_dim_div),
30 * the number of other variables (isl_dim_set) or
31 * the total number of variables (isl_dim_all) in "local".
33 * Other types do not have any meaning for an isl_local object.
35 int isl_local_dim(__isl_keep isl_local
*local
, enum isl_dim_type type
)
41 if (type
== isl_dim_div
)
42 return isl_mat_rows(mat
);
43 if (type
== isl_dim_all
)
44 return isl_mat_cols(mat
) - 2;
45 if (type
== isl_dim_set
)
46 return isl_local_dim(local
, isl_dim_all
) -
47 isl_local_dim(local
, isl_dim_div
);
48 isl_die(isl_local_get_ctx(local
), isl_error_unsupported
,
49 "unsupported dimension type", return 0);
52 /* Check that "pos" is a valid position for a variable in "local".
54 static isl_stat
isl_local_check_pos(__isl_keep isl_local
*local
, int pos
)
57 return isl_stat_error
;
58 if (pos
< 0 || pos
>= isl_local_dim(local
, isl_dim_div
))
59 isl_die(isl_local_get_ctx(local
), isl_error_invalid
,
60 "position out of bounds", return isl_stat_error
);
64 /* Given local variables "local",
65 * is the variable at position "pos" marked as not having
66 * an explicit representation?
67 * Note that even if this variable is not marked in this way and therefore
68 * does have an explicit representation, this representation may still
69 * depend (indirectly) on other local variables that do not
70 * have an explicit representation.
72 isl_bool
isl_local_div_is_marked_unknown(__isl_keep isl_local
*local
, int pos
)
76 if (isl_local_check_pos(local
, pos
) < 0)
77 return isl_bool_error
;
78 return isl_int_is_zero(mat
->row
[pos
][0]);
81 /* Given local variables "local",
82 * does the variable at position "pos" have a complete explicit representation?
83 * Having a complete explicit representation requires not only
84 * an explicit representation, but also that all local variables
85 * that appear in this explicit representation in turn have
86 * a complete explicit representation.
88 isl_bool
isl_local_div_is_known(__isl_keep isl_local
*local
, int pos
)
94 if (isl_local_check_pos(local
, pos
) < 0)
95 return isl_bool_error
;
97 marked
= isl_local_div_is_marked_unknown(local
, pos
);
98 if (marked
< 0 || marked
)
99 return isl_bool_not(marked
);
101 n
= isl_local_dim(local
, isl_dim_div
);
102 off
= isl_mat_cols(mat
) - n
;
104 for (i
= n
- 1; i
>= 0; --i
) {
107 if (isl_int_is_zero(mat
->row
[pos
][off
+ i
]))
109 known
= isl_local_div_is_known(local
, i
);
110 if (known
< 0 || !known
)
114 return isl_bool_true
;
117 /* Does "local" have an explicit representation for all local variables?
119 isl_bool
isl_local_divs_known(__isl_keep isl_local
*local
)
124 return isl_bool_error
;
126 n
= isl_local_dim(local
, isl_dim_div
);
127 for (i
= 0; i
< n
; ++i
) {
128 isl_bool unknown
= isl_local_div_is_marked_unknown(local
, i
);
129 if (unknown
< 0 || unknown
)
130 return isl_bool_not(unknown
);
133 return isl_bool_true
;
136 /* Compare two sets of local variables, defined over
139 * Return -1 if "local1" is "smaller" than "local2", 1 if "local1" is "greater"
140 * than "local2" and 0 if they are equal.
142 * The order is fairly arbitrary. We do "prefer" divs that only involve
143 * earlier dimensions in the sense that we consider matrices where
144 * the first differing div involves earlier dimensions to be smaller.
146 int isl_local_cmp(__isl_keep isl_local
*local1
, __isl_keep isl_local
*local2
)
150 isl_bool unknown1
, unknown2
;
153 isl_mat
*mat1
= local1
;
154 isl_mat
*mat2
= local2
;
156 if (local1
== local2
)
163 if (mat1
->n_row
!= mat2
->n_row
)
164 return mat1
->n_row
- mat2
->n_row
;
166 n_col
= isl_mat_cols(mat1
);
167 for (i
= 0; i
< mat1
->n_row
; ++i
) {
168 unknown1
= isl_local_div_is_marked_unknown(local1
, i
);
169 unknown2
= isl_local_div_is_marked_unknown(local2
, i
);
170 if (unknown1
&& unknown2
)
176 last1
= isl_seq_last_non_zero(mat1
->row
[i
] + 1, n_col
- 1);
177 last2
= isl_seq_last_non_zero(mat2
->row
[i
] + 1, n_col
- 1);
179 return last1
- last2
;
180 cmp
= isl_seq_cmp(mat1
->row
[i
], mat2
->row
[i
], n_col
);
188 /* Extend a vector "v" representing an integer point
189 * in the domain space of "local"
190 * to one that also includes values for the local variables.
191 * All local variables are required to have an explicit representation.
193 __isl_give isl_vec
*isl_local_extend_point_vec(__isl_keep isl_local
*local
,
194 __isl_take isl_vec
*v
)
198 isl_mat
*mat
= local
;
201 return isl_vec_free(v
);
202 known
= isl_local_divs_known(local
);
204 return isl_vec_free(v
);
206 isl_die(isl_local_get_ctx(local
), isl_error_invalid
,
207 "unknown local variables", return isl_vec_free(v
));
208 if (isl_vec_size(v
) != 1 + isl_local_dim(local
, isl_dim_set
))
209 isl_die(isl_local_get_ctx(local
), isl_error_invalid
,
210 "incorrect size", return isl_vec_free(v
));
211 if (!isl_int_is_one(v
->el
[0]))
212 isl_die(isl_local_get_ctx(local
), isl_error_invalid
,
213 "expecting integer point", return isl_vec_free(v
));
214 n_div
= isl_local_dim(local
, isl_dim_div
);
217 unsigned dim
= isl_local_dim(local
, isl_dim_set
);
218 v
= isl_vec_add_els(v
, n_div
);
222 for (i
= 0; i
< n_div
; ++i
) {
223 isl_seq_inner_product(mat
->row
[i
] + 1, v
->el
,
224 1 + dim
+ i
, &v
->el
[1+dim
+i
]);
225 isl_int_fdiv_q(v
->el
[1+dim
+i
], v
->el
[1+dim
+i
],