From: Sven Verdoolaege <
sv...@cerebras.net>
This reduces the dependence on the internal representation.
In practice, the already available isl_multi_*_restore
can simply be reused as a isl_multi_*_restore_at
to pair off with isl_multi_*_take_at.
Note that the calls to isl_multi_*_cow can be removed
because it is called by isl_multi_*_restore_at when needed.
Conversely, isl_multi_*_coalesce is not converted
to used isl_multi_*_{take,restore}_at because
it should perform its modifications inplace.
isl_aff.c | 66 ++++++++++++++----------------
isl_multi_apply_templ.c | 17 +++-----
isl_multi_bin_val_templ.c | 31 ++++----------
isl_multi_dims.c | 13 +++---
isl_multi_move_dims_templ.c | 13 +++---
isl_multi_pw_aff_pullback_templ.c | 9 ++--
isl_multi_templ.c | 68 +++++++++++++++++++++----------
isl_multi_un_op_templ.c | 9 ++--
8 files changed, 113 insertions(+), 113 deletions(-)
diff --git a/isl_aff.c b/isl_aff.c
index 0aa5004c2d..d2ac33ec37 100644
--- a/isl_aff.c
+++ b/isl_aff.c
@@ -4280,16 +4280,17 @@ static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
isl_size n;
int i;
- maff = isl_multi_aff_cow(maff);
n = isl_multi_aff_size(maff);
if (n < 0 || !eq)
goto error;
for (i = 0; i < n; ++i) {
- maff->u.p[i] = isl_aff_substitute_equalities(maff->u.p[i],
+ isl_aff *aff;
+
+ aff = isl_multi_aff_take_at(maff, i);
+ aff = isl_aff_substitute_equalities(aff,
isl_basic_set_copy(eq));
- if (!maff->u.p[i])
- goto error;
+ maff = isl_multi_aff_restore_at(maff, i, aff);
}
isl_basic_set_free(eq);
@@ -4306,15 +4307,16 @@ __isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
isl_size n;
int i;
- maff = isl_multi_aff_cow(maff);
n = isl_multi_aff_size(maff);
if (n < 0)
return isl_multi_aff_free(maff);
for (i = 0; i < n; ++i) {
- maff->u.p[i] = isl_aff_scale(maff->u.p[i], f);
- if (!maff->u.p[i])
- return isl_multi_aff_free(maff);
+ isl_aff *aff;
+
+ aff = isl_multi_aff_take_at(maff, i);
+ aff = isl_aff_scale(aff, f);
+ maff = isl_multi_aff_restore_at(maff, i, aff);
}
return maff;
@@ -5693,7 +5695,6 @@ __isl_give isl_multi_aff *isl_multi_aff_substitute(
isl_size n;
int i;
- maff = isl_multi_aff_cow(maff);
n = isl_multi_aff_size(maff);
if (n < 0 || !subs)
return isl_multi_aff_free(maff);
@@ -5702,10 +5703,11 @@ __isl_give isl_multi_aff *isl_multi_aff_substitute(
type = isl_dim_set;
for (i = 0; i < n; ++i) {
- maff->u.p[i] = isl_aff_substitute(maff->u.p[i],
- type, pos, subs);
- if (!maff->u.p[i])
- return isl_multi_aff_free(maff);
+ isl_aff *aff;
+
+ aff = isl_multi_aff_take_at(maff, i);
+ aff = isl_aff_substitute(aff, type, pos, subs);
+ maff = isl_multi_aff_restore_at(maff, i, aff);
}
return maff;
@@ -5975,7 +5977,6 @@ __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff(
isl_multi_aff_align_params_bin(&ma1, &ma2);
ma2 = isl_multi_aff_align_divs(ma2);
- ma1 = isl_multi_aff_cow(ma1);
n = isl_multi_aff_size(ma1);
if (n < 0 || !ma2)
goto error;
@@ -5984,10 +5985,11 @@ __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff(
isl_multi_aff_get_space(ma1));
for (i = 0; i < n; ++i) {
- ma1->u.p[i] = isl_aff_pullback_multi_aff(ma1->u.p[i],
- isl_multi_aff_copy(ma2));
- if (!ma1->u.p[i])
- goto error;
+ isl_aff *aff;
+
+ aff = isl_multi_aff_take_at(ma1, i);
+ aff = isl_aff_pullback_multi_aff(aff, isl_multi_aff_copy(ma2));
+ ma1 = isl_multi_aff_restore_at(ma1, i, aff);
}
ma1 = isl_multi_aff_reset_space(ma1, space);
@@ -6069,22 +6071,23 @@ __isl_give isl_multi_aff *isl_multi_aff_align_divs(
return isl_multi_aff_free(maff);
if (n <= 1)
return maff;
- maff = isl_multi_aff_cow(maff);
- if (!maff)
- return NULL;
+ aff_0 = isl_multi_aff_take_at(maff, 0);
for (i = 1; i < n; ++i) {
isl_aff *aff_i;
aff_i = isl_multi_aff_peek_at(maff, i);
- maff->u.p[0] = isl_aff_align_divs(maff->u.p[0], aff_i);
+ aff_0 = isl_aff_align_divs(aff_0, aff_i);
}
+ maff = isl_multi_aff_restore_at(maff, 0, aff_0);
aff_0 = isl_multi_aff_peek_at(maff, 0);
for (i = 1; i < n; ++i) {
- maff->u.p[i] = isl_aff_align_divs(maff->u.p[i], aff_0);
- if (!maff->u.p[i])
- return isl_multi_aff_free(maff);
+ isl_aff *aff_i;
+
+ aff_i = isl_multi_aff_take_at(maff, i);
+ aff_i = isl_aff_align_divs(aff_i, aff_0);
+ maff = isl_multi_aff_restore_at(maff, i, aff_i);
}
return maff;
@@ -6133,7 +6136,6 @@ __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
return maff;
}
- maff = isl_multi_aff_cow(maff);
maff = isl_multi_aff_align_divs(maff);
aff = isl_multi_aff_peek_at(maff, 0);
@@ -6145,8 +6147,6 @@ __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
space = isl_space_extend_domain_with_range(space,
isl_multi_aff_get_space(maff));
maff = isl_multi_aff_restore_space(maff, space);
- if (!maff)
- return NULL;
if (ls) {
aff = isl_multi_aff_peek_at(maff, 0);
@@ -6156,16 +6156,12 @@ __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
}
for (i = 0; i < n; ++i) {
- maff->u.p[i] = isl_aff_lift(maff->u.p[i]);
- if (!maff->u.p[i])
- goto error;
+ aff = isl_multi_aff_take_at(maff, i);
+ aff = isl_aff_lift(aff);
+ maff = isl_multi_aff_restore_at(maff, i, aff);
}
return maff;
-error:
- if (ls)
- isl_local_space_free(*ls);
- return isl_multi_aff_free(maff);
}
#undef TYPE
diff --git a/isl_multi_apply_templ.c b/isl_multi_apply_templ.c
index d8e3b03893..31e27f26fb 100644
--- a/isl_multi_apply_templ.c
+++ b/isl_multi_apply_templ.c
@@ -26,19 +26,12 @@ __isl_give MULTI(BASE) *FN(FN(MULTI(BASE),apply_aligned),APPLY_DOMBASE)(
if (n < 0 || !set)
goto error;
- if (n == 0) {
- FN(APPLY_DOM,free)(set);
- return multi;
- }
-
- multi = FN(MULTI(BASE),cow)(multi);
- if (!multi)
- goto error;
-
for (i = 0; i < n; ++i) {
- multi->u.p[i] = fn(multi->u.p[i], FN(APPLY_DOM,copy)(set));
- if (!multi->u.p[i])
- goto error;
+ EL *el;
+
+ el = FN(MULTI(BASE),take_at)(multi, i);
+ el = fn(el, FN(APPLY_DOM,copy)(set));
+ multi = FN(MULTI(BASE),restore_at)(multi, i, el);
}
FN(APPLY_DOM,free)(set);
diff --git a/isl_multi_bin_val_templ.c b/isl_multi_bin_val_templ.c
index 96023439cd..a7a3913da9 100644
--- a/isl_multi_bin_val_templ.c
+++ b/isl_multi_bin_val_templ.c
@@ -20,19 +20,13 @@ static __isl_give MULTI(BASE) *FN(MULTI(BASE),fn_val)(
n = FN(MULTI(BASE),size)(multi);
if (n < 0 || !v)
goto error;
- if (n == 0) {
- isl_val_free(v);
- return multi;
- }
-
- multi = FN(MULTI(BASE),cow)(multi);
- if (!multi)
- goto error;
for (i = 0; i < n; ++i) {
- multi->u.p[i] = fn(multi->u.p[i], isl_val_copy(v));
- if (!multi->u.p[i])
- goto error;
+ EL *el;
+
+ el = FN(MULTI(BASE),take_at)(multi, i);
+ el = fn(el, isl_val_copy(v));
+ multi = FN(MULTI(BASE),restore_at)(multi, i, el);
}
isl_val_free(v);
@@ -60,22 +54,15 @@ static __isl_give MULTI(BASE) *FN(MULTI(BASE),fn_multi_val)(
n = FN(MULTI(BASE),size)(multi);
if (n < 0 || FN(MULTI(BASE),check_match_range_multi_val)(multi, mv) < 0)
goto error;
- if (n == 0) {
- isl_multi_val_free(mv);
- return multi;
- }
-
- multi = FN(MULTI(BASE),cow)(multi);
- if (!multi)
- goto error;
for (i = 0; i < n; ++i) {
isl_val *v;
+ EL *el;
v = isl_multi_val_get_val(mv, i);
- multi->u.p[i] = fn(multi->u.p[i], v);
- if (!multi->u.p[i])
- goto error;
+ el = FN(MULTI(BASE),take_at)(multi, i);
+ el = fn(el, v);
+ multi = FN(MULTI(BASE),restore_at)(multi, i, el);
}
isl_multi_val_free(mv);
diff --git a/isl_multi_dims.c b/isl_multi_dims.c
index 07bb16123f..fb5cb1147b 100644
--- a/isl_multi_dims.c
+++ b/isl_multi_dims.c
@@ -63,19 +63,16 @@ __isl_give MULTI(BASE) *FN(MULTI(BASE),insert_dims)(
space = isl_space_insert_dims(space, type, first, n);
multi = FN(MULTI(BASE),restore_space)(multi, space);
- multi = FN(MULTI(BASE),cow)(multi);
-
if (FN(MULTI(BASE),has_explicit_domain)(multi))
multi = FN(MULTI(BASE),insert_explicit_domain_dims)(multi,
type, first, n);
- if (!multi)
- return NULL;
for (i = 0; i < size; ++i) {
- multi->u.p[i] = FN(EL,insert_dims)(multi->u.p[i],
- type, first, n);
- if (!multi->u.p[i])
- return FN(MULTI(BASE),free)(multi);
+ EL *el;
+
+ el = FN(MULTI(BASE),take_at)(multi, i);
+ el = FN(EL,insert_dims)(el, type, first, n);
+ multi = FN(MULTI(BASE),restore_at)(multi, i, el);
}
return multi;
diff --git a/isl_multi_move_dims_templ.c b/isl_multi_move_dims_templ.c
index 832541804f..ba9830e2b6 100644
--- a/isl_multi_move_dims_templ.c
+++ b/isl_multi_move_dims_templ.c
@@ -53,20 +53,17 @@ __isl_give MULTI(BASE) *FN(MULTI(BASE),move_dims)(__isl_take MULTI(BASE) *multi,
src_type, src_pos, n);
multi = FN(MULTI(BASE),restore_space)(multi, space);
- multi = FN(MULTI(BASE),cow)(multi);
-
if (FN(MULTI(BASE),has_explicit_domain)(multi))
multi = FN(MULTI(BASE),move_explicit_domain_dims)(multi,
dst_type, dst_pos, src_type, src_pos, n);
- if (!multi)
- return NULL;
for (i = 0; i < size; ++i) {
- multi->u.p[i] = FN(EL,move_dims)(multi->u.p[i],
- dst_type, dst_pos,
+ EL *el;
+
+ el = FN(MULTI(BASE),take_at)(multi, i);
+ el = FN(EL,move_dims)(el, dst_type, dst_pos,
src_type, src_pos, n);
- if (!multi->u.p[i])
- return FN(MULTI(BASE),free)(multi);
+ multi = FN(MULTI(BASE),restore_at)(multi, i, el);
}
return multi;
diff --git a/isl_multi_pw_aff_pullback_templ.c b/isl_multi_pw_aff_pullback_templ.c
index 89c4bfa47b..fc5aed2b3f 100644
--- a/isl_multi_pw_aff_pullback_templ.c
+++ b/isl_multi_pw_aff_pullback_templ.c
@@ -47,9 +47,12 @@ __isl_give isl_multi_pw_aff *FN(isl_multi_pw_aff_pullback,BASE)(
isl_multi_pw_aff_get_space(mpa));
for (i = 0; i < n; ++i) {
- mpa->u.p[i] = FN(isl_pw_aff_pullback,BASE)(mpa->u.p[i],
- FN(TYPE,copy)(fn));
- if (!mpa->u.p[i])
+ isl_pw_aff *pa;
+
+ pa = isl_multi_pw_aff_take_at(mpa, i);
+ pa = FN(isl_pw_aff_pullback,BASE)(pa, FN(TYPE,copy)(fn));
+ mpa = isl_multi_pw_aff_restore_at(mpa, i, pa);
+ if (!mpa)
goto error;
}
if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
diff --git a/isl_multi_templ.c b/isl_multi_templ.c
index 5fec937f16..ac4d480d3f 100644
--- a/isl_multi_templ.c
+++ b/isl_multi_templ.c
@@ -231,10 +231,36 @@ __isl_give EL *FN(FN(MULTI(BASE),get),BASE)(__isl_keep MULTI(BASE) *multi,
return FN(MULTI(BASE),get_at)(multi, pos);
}
+/* Return the base expression at position "pos" in "multi".
+ * This may be either a copy or the base expression itself
+ * if there is only one reference to "multi".
+ * This allows the base expression to be modified inplace
+ * if both the multi expression and this base expression
+ * have only a single reference.
+ * The caller is not allowed to modify "multi" between this call and
+ * the subsequent call to isl_multi_*_restore_at_*.
+ * The only exception is that isl_multi_*_free can be called instead.
+ */
+static __isl_give EL *FN(MULTI(BASE),take_at)(__isl_keep MULTI(BASE) *multi,
+ int pos)
+{
+ EL *el;
+
+ if (!multi)
+ return NULL;
+ if (multi->ref != 1)
+ return FN(MULTI(BASE),get_at)(multi, pos);
+ if (FN(MULTI(BASE),check_range)(multi, isl_dim_out, pos, 1) < 0)
+ return NULL;
+ el = multi->u.p[pos];
+ multi->u.p[pos] = NULL;
+ return el;
+}
+
/* Set the element at position "pos" of "multi" to "el",
* where the position may be empty if "multi" has only a single reference.
*/
-static __isl_give MULTI(BASE) *FN(MULTI(BASE),restore)(
+static __isl_give MULTI(BASE) *FN(MULTI(BASE),restore_at)(
__isl_take MULTI(BASE) *multi, int pos, __isl_take EL *el)
{
if (FN(MULTI(BASE),check_range)(multi, isl_dim_out, pos, 1) < 0 || !el)
@@ -272,7 +298,7 @@ static __isl_give MULTI(BASE) *FN(MULTI(BASE),restore_check_space)(
space = FN(MULTI(BASE),peek_space)(multi);
if (FN(EL,check_match_domain_space)(el, space) < 0)
multi = FN(MULTI(BASE),free)(multi);
- return FN(MULTI(BASE),restore)(multi, pos, el);
+ return FN(MULTI(BASE),restore_at)(multi, pos, el);
}
/* Replace the base expression at position "pos" in "multi" with "el".
@@ -356,16 +382,16 @@ __isl_give MULTI(BASE) *FN(MULTI(BASE),reset_space_and_domain)(
isl_size n;
int i;
- multi = FN(MULTI(BASE),cow)(multi);
n = FN(MULTI(BASE),size)(multi);
if (n < 0 || !space || !domain)
goto error;
for (i = 0; i < n; ++i) {
- multi->u.p[i] = FN(EL,reset_domain_space)(multi->u.p[i],
- isl_space_copy(domain));
- if (!multi->u.p[i])
- goto error;
+ EL *el;
+
+ el = FN(MULTI(BASE),take_at)(multi, i);
+ el = FN(EL,reset_domain_space)(el, isl_space_copy(domain));
+ multi = FN(MULTI(BASE),restore_at)(multi, i, el);
}
if (FN(MULTI(BASE),has_explicit_domain)(multi))
multi = FN(MULTI(BASE),reset_explicit_domain_space)(multi,
@@ -422,16 +448,16 @@ __isl_give MULTI(BASE) *FN(MULTI(BASE),realign_domain)(
isl_size n;
isl_space *space;
- multi = FN(MULTI(BASE),cow)(multi);
n = FN(MULTI(BASE),size)(multi);
if (n < 0 || !exp)
goto error;
for (i = 0; i < n; ++i) {
- multi->u.p[i] = FN(EL,realign_domain)(multi->u.p[i],
- isl_reordering_copy(exp));
- if (!multi->u.p[i])
- goto error;
+ EL *el;
+
+ el = FN(MULTI(BASE),take_at)(multi, i);
+ el = FN(EL,realign_domain)(el, isl_reordering_copy(exp));
+ multi = FN(MULTI(BASE),restore_at)(multi, i, el);
}
space = isl_reordering_get_space(exp);
@@ -580,7 +606,6 @@ __isl_give MULTI(BASE) *FN(MULTI(BASE),drop_dims)(
isl_size size;
int i;
- multi = FN(MULTI(BASE),cow)(multi);
if (FN(MULTI(BASE),check_range)(multi, type, first, n) < 0)
return FN(MULTI(BASE),free)(multi);
@@ -599,9 +624,11 @@ __isl_give MULTI(BASE) *FN(MULTI(BASE),drop_dims)(
if (size < 0)
return FN(MULTI(BASE),free)(multi);
for (i = 0; i < size; ++i) {
- multi->u.p[i] = FN(EL,drop_dims)(multi->u.p[i], type, first, n);
- if (!multi->u.p[i])
- return FN(MULTI(BASE),free)(multi);
+ EL *el;
+
+ el = FN(MULTI(BASE),take_at)(multi, i);
+ el = FN(EL,drop_dims)(el, type, first, n);
+ multi = FN(MULTI(BASE),restore_at)(multi, i, el);
}
return multi;
@@ -847,18 +874,17 @@ static __isl_give MULTI(BASE) *FN(MULTI(BASE),bin_op)(
int i;
FN(MULTI(BASE),align_params_bin)(&multi1, &multi2);
- multi1 = FN(MULTI(BASE),cow)(multi1);
n = FN(MULTI(BASE),size)(multi1);
if (n < 0 || FN(MULTI(BASE),check_equal_space)(multi1, multi2) < 0)
goto error;
for (i = 0; i < n; ++i) {
- EL *el2;
+ EL *el1, *el2;
el2 = FN(MULTI(BASE),get_at)(multi2, i);
- multi1->u.p[i] = fn(multi1->u.p[i], el2);
- if (!multi1->u.p[i])
- goto error;
+ el1 = FN(MULTI(BASE),take_at)(multi1, i);
+ el1 = fn(el1, el2);
+ multi1 = FN(MULTI(BASE),restore_at)(multi1, i, el1);
}
if (FN(MULTI(BASE),has_explicit_domain)(multi2))
diff --git a/isl_multi_un_op_templ.c b/isl_multi_un_op_templ.c
index 1273cd780a..bbf105b592 100644
--- a/isl_multi_un_op_templ.c
+++ b/isl_multi_un_op_templ.c
@@ -17,15 +17,16 @@ static __isl_give MULTI(BASE) *FN(MULTI(BASE),un_op)(
int i;
isl_size n;
- multi = FN(MULTI(BASE),cow)(multi);
n = FN(MULTI(BASE),size)(multi);
if (n < 0)
return FN(MULTI(BASE),free)(multi);
for (i = 0; i < n; ++i) {
- multi->u.p[i] = fn(multi->u.p[i]);
- if (!multi->u.p[i])
- return FN(MULTI(BASE),free)(multi);
+ EL *el;
+
+ el = FN(MULTI(BASE),take_at)(multi, i);
+ el = fn(el);
+ multi = FN(MULTI(BASE),restore_at)(multi, i, el);
}
return multi;
--
2.25.1