1 /*
2 * Copyright 2011 Sven Verdoolaege
3 * Copyright 2012-2013 Ecole Normale Superieure
4 *
5 * Use of this software is governed by the MIT license
6 *
7 * Written by Sven Verdoolaege,
8 * Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
9 */
10
11 #include <isl_space_private.h>
12
13 #include <isl_multi_macro.h>
14
15 /* Check whether "multi" has non-zero coefficients for any dimension
16 * in the given range or if any of these dimensions appear
17 * with non-zero coefficients in any of the integer divisions involved.
18 */
FN(MULTI (BASE),involves_dims)19 isl_bool FN(MULTI(BASE),involves_dims)(__isl_keep MULTI(BASE) *multi,
20 enum isl_dim_type type, unsigned first, unsigned n)
21 {
22 int i;
23
24 if (!multi)
25 return isl_bool_error;
26 if (n == 0)
27 return isl_bool_false;
28
29 for (i = 0; i < multi->n; ++i) {
30 isl_bool involves;
31
32 involves = FN(EL,involves_dims)(multi->u.p[i], type, first, n);
33 if (involves < 0 || involves)
34 return involves;
35 }
36
37 if (FN(MULTI(BASE),has_explicit_domain)(multi))
38 return FN(MULTI(BASE),involves_explicit_domain_dims)(multi,
39 type, first, n);
40
41 return isl_bool_false;
42 }
43
MULTI(BASE)44 __isl_give MULTI(BASE) *FN(MULTI(BASE),insert_dims)(
45 __isl_take MULTI(BASE) *multi,
46 enum isl_dim_type type, unsigned first, unsigned n)
47 {
48 int i;
49
50 if (!multi)
51 return NULL;
52 if (type == isl_dim_out)
53 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
54 "cannot insert output/set dimensions",
55 return FN(MULTI(BASE),free)(multi));
56 if (n == 0 && !isl_space_is_named_or_nested(multi->space, type))
57 return multi;
58
59 multi = FN(MULTI(BASE),cow)(multi);
60 if (!multi)
61 return NULL;
62
63 multi->space = isl_space_insert_dims(multi->space, type, first, n);
64 if (!multi->space)
65 return FN(MULTI(BASE),free)(multi);
66 if (FN(MULTI(BASE),has_explicit_domain)(multi))
67 multi = FN(MULTI(BASE),insert_explicit_domain_dims)(multi,
68 type, first, n);
69 if (!multi)
70 return NULL;
71
72 for (i = 0; i < multi->n; ++i) {
73 multi->u.p[i] = FN(EL,insert_dims)(multi->u.p[i],
74 type, first, n);
75 if (!multi->u.p[i])
76 return FN(MULTI(BASE),free)(multi);
77 }
78
79 return multi;
80 }
81
MULTI(BASE)82 __isl_give MULTI(BASE) *FN(MULTI(BASE),add_dims)(__isl_take MULTI(BASE) *multi,
83 enum isl_dim_type type, unsigned n)
84 {
85 isl_size pos;
86
87 pos = FN(MULTI(BASE),dim)(multi, type);
88 if (pos < 0)
89 return FN(MULTI(BASE),free)(multi);
90
91 return FN(MULTI(BASE),insert_dims)(multi, type, pos, n);
92 }
93
94 /* Project the domain of "multi" onto its parameter space.
95 * "multi" may not involve any of the domain dimensions.
96 */
MULTI(BASE)97 __isl_give MULTI(BASE) *FN(MULTI(BASE),project_domain_on_params)(
98 __isl_take MULTI(BASE) *multi)
99 {
100 isl_size n;
101 isl_bool involves;
102 isl_space *space;
103
104 n = FN(MULTI(BASE),dim)(multi, isl_dim_in);
105 if (n < 0)
106 return FN(MULTI(BASE),free)(multi);
107 involves = FN(MULTI(BASE),involves_dims)(multi, isl_dim_in, 0, n);
108 if (involves < 0)
109 return FN(MULTI(BASE),free)(multi);
110 if (involves)
111 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
112 "expression involves some of the domain dimensions",
113 return FN(MULTI(BASE),free)(multi));
114 multi = FN(MULTI(BASE),drop_dims)(multi, isl_dim_in, 0, n);
115 space = FN(MULTI(BASE),get_domain_space)(multi);
116 space = isl_space_params(space);
117 multi = FN(MULTI(BASE),reset_domain_space)(multi, space);
118 return multi;
119 }
120