1 /*
2  * Copyright 2012      Ecole Normale Superieure
3  * Copyright 2017      Sven Verdoolaege
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 #define xFN(TYPE,NAME) TYPE ## _ ## NAME
12 #define FN(TYPE,NAME) xFN(TYPE,NAME)
13 
14 /* Drop the "n" domain dimensions starting at "first" from "obj",
15  * after checking that they do not appear in the affine expression.
16  */
FN(TYPE,drop_domain)17 static __isl_give TYPE *FN(TYPE,drop_domain)(__isl_take TYPE *obj,
18 	unsigned first, unsigned n)
19 {
20 	isl_bool involves;
21 
22 	involves = FN(TYPE,involves_dims)(obj, isl_dim_in, first, n);
23 	if (involves < 0)
24 		return FN(TYPE,free)(obj);
25 	if (involves)
26 		isl_die(FN(TYPE,get_ctx)(obj), isl_error_invalid,
27 		    "affine expression involves some of the domain dimensions",
28 		    return FN(TYPE,free)(obj));
29 	return FN(TYPE,drop_dims)(obj, isl_dim_in, first, n);
30 }
31 
32 /* Check that the domain of "obj" is a product.
33  */
FN(TYPE,check_domain_product)34 static isl_stat FN(TYPE,check_domain_product)(__isl_keep TYPE *obj)
35 {
36 	isl_bool is_product;
37 
38 	is_product = FN(TYPE,domain_is_product)(obj);
39 	if (is_product < 0)
40 		return isl_stat_error;
41 	if (!is_product)
42 		isl_die(FN(TYPE,get_ctx)(obj), isl_error_invalid,
43 			"domain is not a product", return isl_stat_error);
44 	return isl_stat_ok;
45 }
46 
47 /* Given an affine function with a domain of the form [A -> B] that
48  * does not depend on B, return the same function on domain A.
49  */
FN(TYPE,domain_factor_domain)50 __isl_give TYPE *FN(TYPE,domain_factor_domain)(__isl_take TYPE *obj)
51 {
52 	isl_space *space;
53 	isl_size n, n_in;
54 
55 	if (FN(TYPE,check_domain_product)(obj) < 0)
56 		return FN(TYPE,free)(obj);
57 	space = FN(TYPE,get_domain_space)(obj);
58 	n = isl_space_dim(space, isl_dim_set);
59 	space = isl_space_factor_domain(space);
60 	n_in = isl_space_dim(space, isl_dim_set);
61 	if (n < 0 || n_in < 0)
62 		obj = FN(TYPE,free)(obj);
63 	else
64 		obj = FN(TYPE,drop_domain)(obj, n_in, n - n_in);
65 	obj = FN(TYPE,reset_domain_space)(obj, space);
66 	return obj;
67 }
68