1/*
2 * Copyright 2018      Cerebras Systems
3 *
4 * Use of this software is governed by the MIT license
5 *
6 * Written by Sven Verdoolaege,
7 * Cerebras Systems, 175 S San Antonio Rd, Los Altos, CA, USA
8 */
9
10/* Bind the expressions of "multi" to parameters with identifiers
11 * specified by "tuple", living in the same space as
12 * (the target space of) "multi",
13 * returning the elements in the domain where the expressions
14 * are equal to the parameters.
15 */
16__isl_give DOM *FN(MULTI(BASE),bind)(__isl_take MULTI(BASE) *multi,
17	__isl_take isl_multi_id *tuple)
18{
19	int i;
20	isl_id *id;
21	isl_stat r;
22	isl_size n;
23	isl_space *multi_space, *tuple_space;
24	EL *el;
25	DOM *bnd;
26
27	multi_space = isl_space_range(FN(MULTI(BASE),get_space)(multi));
28	tuple_space = isl_multi_id_peek_space(tuple);
29	r = isl_space_check_equal_tuples(multi_space, tuple_space);
30	isl_space_free(multi_space);
31	if (r < 0)
32		goto error;
33	n = FN(MULTI(BASE),dim)(multi, isl_dim_set);
34	if (n < 0)
35		goto error;
36
37	if (n == 0) {
38		isl_multi_id_free(tuple);
39		return FN(MULTI(BASE),domain)(multi);
40	}
41
42	el = FN(MULTI(BASE),get_at)(multi, 0);
43	id = isl_multi_id_get_at(tuple, 0);
44	bnd = FN(EL,bind_id)(el, id);
45
46	for (i = 1; i < n; ++i) {
47		DOM *bnd_i;
48
49		el = FN(MULTI(BASE),get_at)(multi, i);
50		id = isl_multi_id_get_at(tuple, i);
51		bnd_i = FN(EL,bind_id)(el, id);
52
53		bnd_i = FN(DOM,align_params)(bnd_i, FN(DOM,get_space)(bnd));
54		bnd = FN(DOM,align_params)(bnd, FN(DOM,get_space)(bnd_i));
55		bnd = FN(DOM,intersect)(bnd, bnd_i);
56	}
57
58	FN(MULTI(BASE),free)(multi);
59	isl_multi_id_free(tuple);
60	return bnd;
61error:
62	FN(MULTI(BASE),free)(multi);
63	isl_multi_id_free(tuple);
64	return NULL;
65}
66