1/*
2 * Copyright 2013      Ecole Normale Superieure
3 *
4 * Use of this software is governed by the MIT license
5 *
6 * Written by Sven Verdoolaege,
7 * Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
8 */
9
10#define xCAT(A,B) A ## B
11#define CAT(A,B) xCAT(A,B)
12#undef TYPE
13#define TYPE CAT(isl_,BASE)
14#define xFN(TYPE,NAME) TYPE ## _ ## NAME
15#define FN(TYPE,NAME) xFN(TYPE,NAME)
16
17#undef SUFFIX
18#define SUFFIX	BASE
19#undef ARG1
20#define ARG1	isl_multi_pw_aff
21#undef ARG2
22#define ARG2	TYPE
23
24static
25#include "isl_align_params_templ.c"
26
27/* Compute the pullback of "mpa" by the function represented by "fn".
28 * In other words, plug in "fn" in "mpa".
29 *
30 * If "mpa" has an explicit domain, then it is this domain
31 * that needs to undergo a pullback, i.e., a preimage.
32 */
33__isl_give isl_multi_pw_aff *FN(isl_multi_pw_aff_pullback,BASE)(
34	__isl_take isl_multi_pw_aff *mpa, __isl_take TYPE *fn)
35{
36	int i;
37	isl_size n;
38	isl_space *space = NULL;
39
40	FN(isl_multi_pw_aff_align_params,BASE)(&mpa, &fn);
41	mpa = isl_multi_pw_aff_cow(mpa);
42	n = isl_multi_pw_aff_size(mpa);
43	if (n < 0 || !fn)
44		goto error;
45
46	space = isl_space_join(FN(TYPE,get_space)(fn),
47				isl_multi_pw_aff_get_space(mpa));
48
49	for (i = 0; i < n; ++i) {
50		isl_pw_aff *pa;
51
52		pa = isl_multi_pw_aff_take_at(mpa, i);
53		pa = FN(isl_pw_aff_pullback,BASE)(pa, FN(TYPE,copy)(fn));
54		mpa = isl_multi_pw_aff_restore_at(mpa, i, pa);
55		if (!mpa)
56			goto error;
57	}
58	if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
59		mpa->u.dom = FN(isl_set_preimage,BASE)(mpa->u.dom,
60							FN(TYPE,copy)(fn));
61		if (!mpa->u.dom)
62			goto error;
63	}
64
65	FN(TYPE,free)(fn);
66	isl_multi_pw_aff_restore_space(mpa, space);
67	return mpa;
68error:
69	isl_space_free(space);
70	isl_multi_pw_aff_free(mpa);
71	FN(TYPE,free)(fn);
72	return NULL;
73}
74