1 /*
2  * Copyright 2008-2009 Katholieke Universiteit Leuven
3  * Copyright 2010      INRIA Saclay
4  * Copyright 2012-2013 Ecole Normale Superieure
5  * Copyright 2019      Cerebras Systems
6  *
7  * Use of this software is governed by the MIT license
8  *
9  * Written by Sven Verdoolaege, K.U.Leuven, Departement
10  * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
11  * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
12  * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
13  * and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
14  * and Cerebras Systems, 175 S San Antonio Rd, Los Altos, CA, USA
15  */
16 
17 #include <stdlib.h>
18 #include <string.h>
19 #include <isl_ctx_private.h>
20 #include <isl_map_private.h>
21 #include <isl/set.h>
22 #include <isl_seq.h>
23 #include <isl_polynomial_private.h>
24 #include <isl_printer_private.h>
25 #include <isl_space_private.h>
26 #include <isl_mat_private.h>
27 #include <isl_vec_private.h>
28 #include <isl/union_set.h>
29 #include <isl/union_map.h>
30 #include <isl/constraint.h>
31 #include <isl_local.h>
32 #include <isl_local_space_private.h>
33 #include <isl_aff_private.h>
34 #include <isl_id_private.h>
35 #include <isl_val_private.h>
36 #include <isl_constraint_private.h>
37 #include <isl/ast_build.h>
38 #include <isl_sort.h>
39 #include <isl_output_private.h>
40 
41 #include <bset_to_bmap.c>
42 #include <set_to_map.c>
43 #include <uset_to_umap.c>
44 
45 static const char *s_to[2] = { " -> ", " \\to " };
46 static const char *s_and[2] = { " and ", " \\wedge " };
47 static const char *s_or[2] = { " or ", " \\vee " };
48 static const char *s_le[2] = { "<=", "\\le" };
49 static const char *s_ge[2] = { ">=", "\\ge" };
50 static const char *s_open_set[2] = { "{ ", "\\{\\, " };
51 static const char *s_close_set[2] = { " }", " \\,\\}" };
52 static const char *s_open_list[2] = { "[", "(" };
53 static const char *s_close_list[2] = { "]", ")" };
54 static const char *s_such_that[2] = { " : ", " \\mid " };
55 static const char *s_open_exists[2] = { "exists (", "\\exists \\, " };
56 static const char *s_close_exists[2] = { ")", "" };
57 static const char *s_div_prefix[2] = { "e", "\\alpha_" };
58 static const char *s_mod[2] = { "mod", "\\bmod" };
59 static const char *s_param_prefix[2] = { "p", "p_" };
60 static const char *s_input_prefix[2] = { "i", "i_" };
61 static const char *s_output_prefix[2] = { "o", "o_" };
62 
print_constraint_polylib(struct isl_basic_map * bmap,int ineq,int n,__isl_take isl_printer * p)63 static __isl_give isl_printer *print_constraint_polylib(
64 	struct isl_basic_map *bmap, int ineq, int n, __isl_take isl_printer *p)
65 {
66 	int i;
67 	isl_size n_in = isl_basic_map_dim(bmap, isl_dim_in);
68 	isl_size n_out = isl_basic_map_dim(bmap, isl_dim_out);
69 	isl_size nparam = isl_basic_map_dim(bmap, isl_dim_param);
70 	isl_int *c = ineq ? bmap->ineq[n] : bmap->eq[n];
71 
72 	if (n_in < 0 || n_out < 0 || nparam < 0)
73 		return isl_printer_free(p);
74 
75 	p = isl_printer_start_line(p);
76 	p = isl_printer_print_int(p, ineq);
77 	for (i = 0; i < n_out; ++i) {
78 		p = isl_printer_print_str(p, " ");
79 		p = isl_printer_print_isl_int(p, c[1+nparam+n_in+i]);
80 	}
81 	for (i = 0; i < n_in; ++i) {
82 		p = isl_printer_print_str(p, " ");
83 		p = isl_printer_print_isl_int(p, c[1+nparam+i]);
84 	}
85 	for (i = 0; i < bmap->n_div; ++i) {
86 		p = isl_printer_print_str(p, " ");
87 		p = isl_printer_print_isl_int(p, c[1+nparam+n_in+n_out+i]);
88 	}
89 	for (i = 0; i < nparam; ++i) {
90 		p = isl_printer_print_str(p, " ");
91 		p = isl_printer_print_isl_int(p, c[1+i]);
92 	}
93 	p = isl_printer_print_str(p, " ");
94 	p = isl_printer_print_isl_int(p, c[0]);
95 	p = isl_printer_end_line(p);
96 	return p;
97 }
98 
print_constraints_polylib(struct isl_basic_map * bmap,__isl_take isl_printer * p)99 static __isl_give isl_printer *print_constraints_polylib(
100 	struct isl_basic_map *bmap, __isl_take isl_printer *p)
101 {
102 	int i;
103 
104 	p = isl_printer_set_isl_int_width(p, 5);
105 
106 	for (i = 0; i < bmap->n_eq; ++i)
107 		p = print_constraint_polylib(bmap, 0, i, p);
108 	for (i = 0; i < bmap->n_ineq; ++i)
109 		p = print_constraint_polylib(bmap, 1, i, p);
110 
111 	return p;
112 }
113 
bset_print_constraints_polylib(struct isl_basic_set * bset,__isl_take isl_printer * p)114 static __isl_give isl_printer *bset_print_constraints_polylib(
115 	struct isl_basic_set *bset, __isl_take isl_printer *p)
116 {
117 	return print_constraints_polylib(bset_to_bmap(bset), p);
118 }
119 
isl_basic_map_print_polylib(__isl_keep isl_basic_map * bmap,__isl_take isl_printer * p,int ext)120 static __isl_give isl_printer *isl_basic_map_print_polylib(
121 	__isl_keep isl_basic_map *bmap, __isl_take isl_printer *p, int ext)
122 {
123 	isl_size total;
124 
125 	total = isl_basic_map_dim(bmap, isl_dim_all);
126 	if (total < 0)
127 		return isl_printer_free(p);
128 	p = isl_printer_start_line(p);
129 	p = isl_printer_print_int(p, bmap->n_eq + bmap->n_ineq);
130 	p = isl_printer_print_str(p, " ");
131 	p = isl_printer_print_int(p, 1 + total + 1);
132 	if (ext) {
133 		isl_size n_in = isl_basic_map_dim(bmap, isl_dim_in);
134 		isl_size n_out = isl_basic_map_dim(bmap, isl_dim_out);
135 		isl_size n_div = isl_basic_map_dim(bmap, isl_dim_div);
136 		isl_size nparam = isl_basic_map_dim(bmap, isl_dim_param);
137 
138 		if (n_in < 0 || n_out < 0 || n_div < 0 || nparam < 0)
139 			return isl_printer_free(p);
140 
141 		p = isl_printer_print_str(p, " ");
142 		p = isl_printer_print_int(p, n_out);
143 		p = isl_printer_print_str(p, " ");
144 		p = isl_printer_print_int(p, n_in);
145 		p = isl_printer_print_str(p, " ");
146 		p = isl_printer_print_int(p, n_div);
147 		p = isl_printer_print_str(p, " ");
148 		p = isl_printer_print_int(p, nparam);
149 	}
150 	p = isl_printer_end_line(p);
151 	return print_constraints_polylib(bmap, p);
152 }
153 
isl_basic_set_print_polylib(__isl_keep isl_basic_set * bset,__isl_take isl_printer * p,int ext)154 static __isl_give isl_printer *isl_basic_set_print_polylib(
155 	__isl_keep isl_basic_set *bset, __isl_take isl_printer *p, int ext)
156 {
157 	return isl_basic_map_print_polylib(bset_to_bmap(bset), p, ext);
158 }
159 
isl_map_print_polylib(__isl_keep isl_map * map,__isl_take isl_printer * p,int ext)160 static __isl_give isl_printer *isl_map_print_polylib(__isl_keep isl_map *map,
161 	__isl_take isl_printer *p, int ext)
162 {
163 	int i;
164 
165 	p = isl_printer_start_line(p);
166 	p = isl_printer_print_int(p, map->n);
167 	p = isl_printer_end_line(p);
168 	for (i = 0; i < map->n; ++i) {
169 		p = isl_printer_start_line(p);
170 		p = isl_printer_end_line(p);
171 		p = isl_basic_map_print_polylib(map->p[i], p, ext);
172 	}
173 	return p;
174 }
175 
isl_set_print_polylib(__isl_keep isl_set * set,__isl_take isl_printer * p,int ext)176 static __isl_give isl_printer *isl_set_print_polylib(__isl_keep isl_set *set,
177 	__isl_take isl_printer *p, int ext)
178 {
179 	return isl_map_print_polylib(set_to_map(set), p, ext);
180 }
181 
count_same_name(__isl_keep isl_space * space,enum isl_dim_type type,unsigned pos,const char * name)182 static isl_size count_same_name(__isl_keep isl_space *space,
183 	enum isl_dim_type type, unsigned pos, const char *name)
184 {
185 	enum isl_dim_type t;
186 	int p;
187 	isl_size s;
188 	int count = 0;
189 
190 	for (t = isl_dim_param; t <= type && t <= isl_dim_out; ++t) {
191 		s = t == type ? pos : isl_space_dim(space, t);
192 		if (s < 0)
193 			return isl_size_error;
194 		for (p = 0; p < s; ++p) {
195 			const char *n = isl_space_get_dim_name(space, t, p);
196 			if (n && !strcmp(n, name))
197 				count++;
198 		}
199 	}
200 	return count;
201 }
202 
203 /* Print the name of the variable of type "type" and position "pos"
204  * in "space" to "p".
205  */
print_name(__isl_keep isl_space * space,__isl_take isl_printer * p,enum isl_dim_type type,unsigned pos,int latex)206 static __isl_give isl_printer *print_name(__isl_keep isl_space *space,
207 	__isl_take isl_printer *p, enum isl_dim_type type, unsigned pos,
208 	int latex)
209 {
210 	const char *name;
211 	char buffer[20];
212 	isl_size primes;
213 
214 	name = type == isl_dim_div ? NULL
215 				   : isl_space_get_dim_name(space, type, pos);
216 
217 	if (!name) {
218 		const char *prefix;
219 		if (type == isl_dim_param)
220 			prefix = s_param_prefix[latex];
221 		else if (type == isl_dim_div)
222 			prefix = s_div_prefix[latex];
223 		else if (isl_space_is_set(space) || type == isl_dim_in)
224 			prefix = s_input_prefix[latex];
225 		else
226 			prefix = s_output_prefix[latex];
227 		snprintf(buffer, sizeof(buffer), "%s%d", prefix, pos);
228 		name = buffer;
229 	}
230 	primes = count_same_name(space, name == buffer ? isl_dim_div : type,
231 				 pos, name);
232 	if (primes < 0)
233 		return isl_printer_free(p);
234 	p = isl_printer_print_str(p, name);
235 	while (primes-- > 0)
236 		p = isl_printer_print_str(p, "'");
237 	return p;
238 }
239 
pos2type(__isl_keep isl_space * space,enum isl_dim_type * type,unsigned * pos)240 static isl_stat pos2type(__isl_keep isl_space *space,
241 	enum isl_dim_type *type, unsigned *pos)
242 {
243 	isl_size n_in = isl_space_dim(space, isl_dim_in);
244 	isl_size n_out = isl_space_dim(space, isl_dim_out);
245 	isl_size nparam = isl_space_dim(space, isl_dim_param);
246 
247 	if (n_in < 0 || n_out < 0 || nparam < 0)
248 		return isl_stat_error;
249 
250 	if (*pos < 1 + nparam) {
251 		*type = isl_dim_param;
252 		*pos -= 1;
253 	} else if (*pos < 1 + nparam + n_in) {
254 		*type = isl_dim_in;
255 		*pos -= 1 + nparam;
256 	} else if (*pos < 1 + nparam + n_in + n_out) {
257 		*type = isl_dim_out;
258 		*pos -= 1 + nparam + n_in;
259 	} else {
260 		*type = isl_dim_div;
261 		*pos -= 1 + nparam + n_in + n_out;
262 	}
263 
264 	return isl_stat_ok;
265 }
266 
267 /* Can the div expression of the integer division at position "row" of "div"
268  * be printed?
269  * In particular, are the div expressions available and does the selected
270  * variable have a known explicit representation?
271  * Furthermore, the Omega format does not allow any div expressions
272  * to be printed.
273  */
can_print_div_expr(__isl_keep isl_printer * p,__isl_keep isl_mat * div,int pos)274 static isl_bool can_print_div_expr(__isl_keep isl_printer *p,
275 	__isl_keep isl_mat *div, int pos)
276 {
277 	if (p->output_format == ISL_FORMAT_OMEGA)
278 		return isl_bool_false;
279 	if (!div)
280 		return isl_bool_false;
281 	return isl_bool_not(isl_local_div_is_marked_unknown(div, pos));
282 }
283 
284 static __isl_give isl_printer *print_div(__isl_keep isl_space *space,
285 	__isl_keep isl_mat *div, int pos, __isl_take isl_printer *p);
286 
print_term(__isl_keep isl_space * space,__isl_keep isl_mat * div,isl_int c,unsigned pos,__isl_take isl_printer * p,int latex)287 static __isl_give isl_printer *print_term(__isl_keep isl_space *space,
288 	__isl_keep isl_mat *div,
289 	isl_int c, unsigned pos, __isl_take isl_printer *p, int latex)
290 {
291 	enum isl_dim_type type;
292 	int print_div_def;
293 
294 	if (!p || !space)
295 		return isl_printer_free(p);
296 
297 	if (pos == 0)
298 		return isl_printer_print_isl_int(p, c);
299 
300 	if (pos2type(space, &type, &pos) < 0)
301 		return isl_printer_free(p);
302 	print_div_def = type == isl_dim_div && can_print_div_expr(p, div, pos);
303 
304 	if (isl_int_is_one(c))
305 		;
306 	else if (isl_int_is_negone(c))
307 		p = isl_printer_print_str(p, "-");
308 	else {
309 		p = isl_printer_print_isl_int(p, c);
310 		if (p->output_format == ISL_FORMAT_C || print_div_def)
311 			p = isl_printer_print_str(p, "*");
312 	}
313 	if (print_div_def)
314 		p = print_div(space, div, pos, p);
315 	else
316 		p = print_name(space, p, type, pos, latex);
317 	return p;
318 }
319 
print_affine_of_len(__isl_keep isl_space * space,__isl_keep isl_mat * div,__isl_take isl_printer * p,isl_int * c,int len)320 static __isl_give isl_printer *print_affine_of_len(__isl_keep isl_space *space,
321 	__isl_keep isl_mat *div,
322 	__isl_take isl_printer *p, isl_int *c, int len)
323 {
324 	int i;
325 	int first;
326 
327 	for (i = 0, first = 1; i < len; ++i) {
328 		int flip = 0;
329 		if (isl_int_is_zero(c[i]))
330 			continue;
331 		if (!first) {
332 			if (isl_int_is_neg(c[i])) {
333 				flip = 1;
334 				isl_int_neg(c[i], c[i]);
335 				p = isl_printer_print_str(p, " - ");
336 			} else
337 				p = isl_printer_print_str(p, " + ");
338 		}
339 		first = 0;
340 		p = print_term(space, div, c[i], i, p, 0);
341 		if (flip)
342 			isl_int_neg(c[i], c[i]);
343 	}
344 	if (first)
345 		p = isl_printer_print_str(p, "0");
346 	return p;
347 }
348 
349 /* Print an affine expression "c"
350  * to "p", with the variable names taken from "space" and
351  * the integer division definitions taken from "div".
352  */
print_affine(__isl_take isl_printer * p,__isl_keep isl_space * space,__isl_keep isl_mat * div,isl_int * c)353 static __isl_give isl_printer *print_affine(__isl_take isl_printer *p,
354 	__isl_keep isl_space *space, __isl_keep isl_mat *div, isl_int *c)
355 {
356 	isl_size n_div, total;
357 	unsigned len;
358 
359 	total = isl_space_dim(space, isl_dim_all);
360 	n_div = isl_mat_rows(div);
361 	if (total < 0 || n_div < 0)
362 		return isl_printer_free(p);
363 	len = 1 + total + n_div;
364 	return print_affine_of_len(space, div, p, c, len);
365 }
366 
367 /* offset is the offset of local_space inside data->type of data->space.
368  */
print_nested_var_list(__isl_take isl_printer * p,__isl_keep isl_space * local_space,enum isl_dim_type local_type,struct isl_print_space_data * data,int offset)369 static __isl_give isl_printer *print_nested_var_list(__isl_take isl_printer *p,
370 	__isl_keep isl_space *local_space, enum isl_dim_type local_type,
371 	struct isl_print_space_data *data, int offset)
372 {
373 	int i;
374 	isl_size dim;
375 
376 	if (data->space != local_space && local_type == isl_dim_out)
377 		offset += local_space->n_in;
378 
379 	dim = isl_space_dim(local_space, local_type);
380 	if (dim < 0)
381 		return isl_printer_free(p);
382 	for (i = 0; i < dim; ++i) {
383 		if (i)
384 			p = isl_printer_print_str(p, ", ");
385 		if (data->print_dim)
386 			p = data->print_dim(p, data, offset + i);
387 		else
388 			p = print_name(data->space, p, data->type, offset + i,
389 					data->latex);
390 	}
391 	return p;
392 }
393 
print_var_list(__isl_take isl_printer * p,__isl_keep isl_space * space,enum isl_dim_type type)394 static __isl_give isl_printer *print_var_list(__isl_take isl_printer *p,
395 	__isl_keep isl_space *space, enum isl_dim_type type)
396 {
397 	struct isl_print_space_data data = { .space = space, .type = type };
398 
399 	return print_nested_var_list(p, space, type, &data, 0);
400 }
401 
402 static __isl_give isl_printer *print_nested_map_dim(__isl_take isl_printer *p,
403 	__isl_keep isl_space *local_dim,
404 	struct isl_print_space_data *data, int offset);
405 
print_nested_tuple(__isl_take isl_printer * p,__isl_keep isl_space * local_space,enum isl_dim_type local_type,struct isl_print_space_data * data,int offset)406 static __isl_give isl_printer *print_nested_tuple(__isl_take isl_printer *p,
407 	__isl_keep isl_space *local_space, enum isl_dim_type local_type,
408 	struct isl_print_space_data *data, int offset)
409 {
410 	const char *name = NULL;
411 	isl_size n = isl_space_dim(local_space, local_type);
412 
413 	if (n < 0)
414 		return isl_printer_free(p);
415 	if ((local_type == isl_dim_in || local_type == isl_dim_out)) {
416 		name = isl_space_get_tuple_name(local_space, local_type);
417 		if (name) {
418 			if (data->latex)
419 				p = isl_printer_print_str(p, "\\mathrm{");
420 			p = isl_printer_print_str(p, name);
421 			if (data->latex)
422 				p = isl_printer_print_str(p, "}");
423 		}
424 	}
425 	if (!data->latex || n != 1 || name)
426 		p = isl_printer_print_str(p, s_open_list[data->latex]);
427 	if ((local_type == isl_dim_in || local_type == isl_dim_out) &&
428 	    local_space->nested[local_type - isl_dim_in]) {
429 		if (data->space != local_space && local_type == isl_dim_out)
430 			offset += local_space->n_in;
431 		p = print_nested_map_dim(p,
432 				local_space->nested[local_type - isl_dim_in],
433 				data, offset);
434 	} else
435 		p = print_nested_var_list(p, local_space, local_type, data,
436 					  offset);
437 	if (!data->latex || n != 1 || name)
438 		p = isl_printer_print_str(p, s_close_list[data->latex]);
439 	return p;
440 }
441 
print_tuple(__isl_keep isl_space * space,__isl_take isl_printer * p,enum isl_dim_type type,struct isl_print_space_data * data)442 static __isl_give isl_printer *print_tuple(__isl_keep isl_space *space,
443 	__isl_take isl_printer *p, enum isl_dim_type type,
444 	struct isl_print_space_data *data)
445 {
446 	data->space = space;
447 	data->type = type;
448 	return print_nested_tuple(p, space, type, data, 0);
449 }
450 
print_nested_map_dim(__isl_take isl_printer * p,__isl_keep isl_space * local_dim,struct isl_print_space_data * data,int offset)451 static __isl_give isl_printer *print_nested_map_dim(__isl_take isl_printer *p,
452 	__isl_keep isl_space *local_dim,
453 	struct isl_print_space_data *data, int offset)
454 {
455 	p = print_nested_tuple(p, local_dim, isl_dim_in, data, offset);
456 	p = isl_printer_print_str(p, s_to[data->latex]);
457 	p = print_nested_tuple(p, local_dim, isl_dim_out, data, offset);
458 
459 	return p;
460 }
461 
isl_print_space(__isl_keep isl_space * space,__isl_take isl_printer * p,int rational,struct isl_print_space_data * data)462 __isl_give isl_printer *isl_print_space(__isl_keep isl_space *space,
463 	__isl_take isl_printer *p, int rational,
464 	struct isl_print_space_data *data)
465 {
466 	if (rational && !data->latex)
467 		p = isl_printer_print_str(p, "rat: ");
468 	if (isl_space_is_params(space))
469 		;
470 	else if (isl_space_is_set(space))
471 		p = print_tuple(space, p, isl_dim_set, data);
472 	else {
473 		p = print_tuple(space, p, isl_dim_in, data);
474 		p = isl_printer_print_str(p, s_to[data->latex]);
475 		p = print_tuple(space, p, isl_dim_out, data);
476 	}
477 
478 	return p;
479 }
480 
print_omega_parameters(__isl_keep isl_space * space,__isl_take isl_printer * p)481 static __isl_give isl_printer *print_omega_parameters(
482 	__isl_keep isl_space *space, __isl_take isl_printer *p)
483 {
484 	isl_size nparam = isl_space_dim(space, isl_dim_param);
485 
486 	if (nparam < 0)
487 		return isl_printer_free(p);
488 	if (nparam == 0)
489 		return p;
490 
491 	p = isl_printer_start_line(p);
492 	p = isl_printer_print_str(p, "symbolic ");
493 	p = print_var_list(p, space, isl_dim_param);
494 	p = isl_printer_print_str(p, ";");
495 	p = isl_printer_end_line(p);
496 	return p;
497 }
498 
499 /* Does the inequality constraint following "i" in "bmap"
500  * have an opposite value for the same last coefficient?
501  * "last" is the position of the last coefficient of inequality "i".
502  * If the next constraint is a div constraint, then it is ignored
503  * since div constraints are not printed.
504  */
next_is_opposite(__isl_keep isl_basic_map * bmap,int i,int last)505 static isl_bool next_is_opposite(__isl_keep isl_basic_map *bmap, int i,
506 	int last)
507 {
508 	int r;
509 	isl_size total = isl_basic_map_dim(bmap, isl_dim_all);
510 	unsigned o_div = isl_basic_map_offset(bmap, isl_dim_div);
511 
512 	if (total < 0)
513 		return isl_bool_error;
514 	if (i + 1 >= bmap->n_ineq)
515 		return isl_bool_false;
516 	if (isl_seq_last_non_zero(bmap->ineq[i + 1], 1 + total) != last)
517 		return isl_bool_false;
518 	if (last >= o_div) {
519 		isl_bool is_div;
520 		is_div = isl_basic_map_is_div_constraint(bmap,
521 					    bmap->ineq[i + 1], last - o_div);
522 		if (is_div < 0)
523 			return isl_bool_error;
524 		if (is_div)
525 			return isl_bool_false;
526 	}
527 	r = isl_int_abs_eq(bmap->ineq[i][last], bmap->ineq[i + 1][last]) &&
528 	    !isl_int_eq(bmap->ineq[i][last], bmap->ineq[i + 1][last]);
529 	return isl_bool_ok(r);
530 }
531 
532 /* Return a string representation of the operator used when
533  * printing a constraint where the LHS is greater than or equal to the LHS
534  * (sign > 0) or smaller than or equal to the LHS (sign < 0).
535  * If "strict" is set, then return the strict version of the comparison
536  * operator.
537  */
constraint_op(int sign,int strict,int latex)538 static const char *constraint_op(int sign, int strict, int latex)
539 {
540 	if (strict)
541 		return sign < 0 ? "<" : ">";
542 	if (sign < 0)
543 		return s_le[latex];
544 	else
545 		return s_ge[latex];
546 }
547 
548 /* Print one side of a constraint "c" to "p", with
549  * the variable names taken from "space" and the integer division definitions
550  * taken from "div".
551  * "last" is the position of the last non-zero coefficient.
552  * Let c' be the result of zeroing out this coefficient, then
553  * the partial constraint
554  *
555  *	c' op
556  *
557  * is printed.
558  */
print_half_constraint(__isl_take isl_printer * p,__isl_keep isl_space * space,__isl_keep isl_mat * div,isl_int * c,int last,const char * op,int latex)559 static __isl_give isl_printer *print_half_constraint(__isl_take isl_printer *p,
560 	__isl_keep isl_space *space, __isl_keep isl_mat *div,
561 	isl_int *c, int last, const char *op, int latex)
562 {
563 	isl_int_set_si(c[last], 0);
564 	p = print_affine(p, space, div, c);
565 
566 	p = isl_printer_print_str(p, " ");
567 	p = isl_printer_print_str(p, op);
568 	p = isl_printer_print_str(p, " ");
569 
570 	return p;
571 }
572 
573 /* Print a constraint "c" to "p", with the variable names
574  * taken from "space" and the integer division definitions taken from "div".
575  * "last" is the position of the last non-zero coefficient, which is
576  * moreover assumed to be negative.
577  * Let c' be the result of zeroing out this coefficient, then
578  * the constraint is printed in the form
579  *
580  *	-c[last] op c'
581  */
print_constraint(__isl_take isl_printer * p,__isl_keep isl_space * space,__isl_keep isl_mat * div,isl_int * c,int last,const char * op,int latex)582 static __isl_give isl_printer *print_constraint(__isl_take isl_printer *p,
583 	__isl_keep isl_space *space, __isl_keep isl_mat *div,
584 	isl_int *c, int last, const char *op, int latex)
585 {
586 	isl_int_abs(c[last], c[last]);
587 
588 	p = print_term(space, div, c[last], last, p, latex);
589 
590 	p = isl_printer_print_str(p, " ");
591 	p = isl_printer_print_str(p, op);
592 	p = isl_printer_print_str(p, " ");
593 
594 	isl_int_set_si(c[last], 0);
595 	p = print_affine(p, space, div, c);
596 
597 	return p;
598 }
599 
600 /* Given an integer division
601  *
602  *	floor(f/m)
603  *
604  * at position "pos" in "div", print the corresponding modulo expression
605  *
606  *	(f) mod m
607  *
608  * to "p".  The variable names are taken from "space", while any
609  * nested integer division definitions are taken from "div".
610  */
print_mod(__isl_take isl_printer * p,__isl_keep isl_space * space,__isl_keep isl_mat * div,int pos,int latex)611 static __isl_give isl_printer *print_mod(__isl_take isl_printer *p,
612 	__isl_keep isl_space *space, __isl_keep isl_mat *div, int pos,
613 	int latex)
614 {
615 	if (!p || !div)
616 		return isl_printer_free(p);
617 
618 	p = isl_printer_print_str(p, "(");
619 	p = print_affine_of_len(space, div, p,
620 				div->row[pos] + 1, div->n_col - 1);
621 	p = isl_printer_print_str(p, ") ");
622 	p = isl_printer_print_str(p, s_mod[latex]);
623 	p = isl_printer_print_str(p, " ");
624 	p = isl_printer_print_isl_int(p, div->row[pos][0]);
625 	return p;
626 }
627 
628 /* Given an equality constraint with a non-zero coefficient "c"
629  * in position "pos", is this term of the form
630  *
631  *	a m floor(g/m),
632  *
633  * with c = a m?
634  * Return the position of the corresponding integer division if so.
635  * Return the number of integer divisions if not.
636  * Return isl_size_error on error.
637  *
638  * Modulo constraints are currently not printed in C format.
639  * Other than that, "pos" needs to correspond to an integer division
640  * with explicit representation and "c" needs to be a multiple
641  * of the denominator of the integer division.
642  */
print_as_modulo_pos(__isl_keep isl_printer * p,__isl_keep isl_space * space,__isl_keep isl_mat * div,unsigned pos,isl_int c)643 static isl_size print_as_modulo_pos(__isl_keep isl_printer *p,
644 	__isl_keep isl_space *space, __isl_keep isl_mat *div, unsigned pos,
645 	isl_int c)
646 {
647 	isl_bool can_print;
648 	isl_size n_div;
649 	enum isl_dim_type type;
650 
651 	n_div = isl_mat_rows(div);
652 	if (!p || !space || n_div < 0)
653 		return isl_size_error;
654 	if (p->output_format == ISL_FORMAT_C)
655 		return n_div;
656 	if (pos2type(space, &type, &pos) < 0)
657 		return isl_size_error;
658 	if (type != isl_dim_div)
659 		return n_div;
660 	can_print = can_print_div_expr(p, div, pos);
661 	if (can_print < 0)
662 		return isl_size_error;
663 	if (!can_print)
664 		return n_div;
665 	if (!isl_int_is_divisible_by(c, div->row[pos][0]))
666 		return n_div;
667 	return pos;
668 }
669 
670 /* Print equality constraint "c" to "p" as a modulo constraint,
671  * with the variable names taken from "space" and
672  * the integer division definitions taken from "div".
673  * "last" is the position of the last non-zero coefficient, which is
674  * moreover assumed to be negative and a multiple of the denominator
675  * of the corresponding integer division.  "div_pos" is the corresponding
676  * position in the sequence of integer divisions.
677  *
678  * The equality is of the form
679  *
680  *	f - a m floor(g/m) = 0.
681  *
682  * Print it as
683  *
684  *	a (g mod m) = -f + a g
685  */
print_eq_mod_constraint(__isl_take isl_printer * p,__isl_keep isl_space * space,__isl_keep isl_mat * div,unsigned div_pos,isl_int * c,int last,int latex)686 static __isl_give isl_printer *print_eq_mod_constraint(
687 	__isl_take isl_printer *p, __isl_keep isl_space *space,
688 	__isl_keep isl_mat *div, unsigned div_pos,
689 	isl_int *c, int last, int latex)
690 {
691 	isl_ctx *ctx;
692 	int multiple;
693 
694 	ctx = isl_printer_get_ctx(p);
695 	isl_int_divexact(c[last], c[last], div->row[div_pos][0]);
696 	isl_int_abs(c[last], c[last]);
697 	multiple = !isl_int_is_one(c[last]);
698 	if (multiple) {
699 		p = isl_printer_print_isl_int(p, c[last]);
700 		p = isl_printer_print_str(p, "*(");
701 	}
702 	p = print_mod(p, space, div, div_pos, latex);
703 	if (multiple)
704 		p = isl_printer_print_str(p, ")");
705 	p = isl_printer_print_str(p, " = ");
706 	isl_seq_combine(c, ctx->negone, c,
707 			    c[last], div->row[div_pos] + 1, last);
708 	isl_int_set_si(c[last], 0);
709 	p = print_affine(p, space, div, c);
710 	return p;
711 }
712 
713 /* Print equality constraint "c" to "p", with the variable names
714  * taken from "space" and the integer division definitions taken from "div".
715  * "last" is the position of the last non-zero coefficient, which is
716  * moreover assumed to be negative.
717  *
718  * If possible, print the equality constraint as a modulo constraint.
719  */
print_eq_constraint(__isl_take isl_printer * p,__isl_keep isl_space * space,__isl_keep isl_mat * div,isl_int * c,int last,int latex)720 static __isl_give isl_printer *print_eq_constraint(__isl_take isl_printer *p,
721 	__isl_keep isl_space *space, __isl_keep isl_mat *div, isl_int *c,
722 	int last, int latex)
723 {
724 	isl_size n_div;
725 	isl_size div_pos;
726 
727 	n_div = isl_mat_rows(div);
728 	div_pos = print_as_modulo_pos(p, space, div, last, c[last]);
729 	if (n_div < 0 || div_pos < 0)
730 		return isl_printer_free(p);
731 	if (div_pos < n_div)
732 		return print_eq_mod_constraint(p, space, div, div_pos,
733 						c, last, latex);
734 	return print_constraint(p, space, div, c, last, "=", latex);
735 }
736 
737 /* Print the constraints of "bmap" to "p".
738  * The names of the variables are taken from "space" and
739  * the integer division definitions are taken from "div".
740  * Div constraints are only printed in "dump" mode.
741  * The constraints are sorted prior to printing (except in "dump" mode).
742  *
743  * If x is the last variable with a non-zero coefficient,
744  * then a lower bound
745  *
746  *	f - a x >= 0
747  *
748  * is printed as
749  *
750  *	a x <= f
751  *
752  * while an upper bound
753  *
754  *	f + a x >= 0
755  *
756  * is printed as
757  *
758  *	a x >= -f
759  *
760  * If the next constraint has an opposite sign for the same last coefficient,
761  * then it is printed as
762  *
763  *	f >= a x
764  *
765  * or
766  *
767  *	-f <= a x
768  *
769  * instead.  In fact, the "a x" part is not printed explicitly, but
770  * reused from the next constraint, which is therefore treated as
771  * a first constraint in the conjunction.
772  *
773  * If the constant term of "f" is -1, then "f" is replaced by "f + 1" and
774  * the comparison operator is replaced by the strict variant.
775  * Essentially, ">= 1" is replaced by "> 0".
776  */
print_constraints(__isl_keep isl_basic_map * bmap,__isl_keep isl_space * space,__isl_keep isl_mat * div,__isl_take isl_printer * p,int latex)777 static __isl_give isl_printer *print_constraints(__isl_keep isl_basic_map *bmap,
778 	__isl_keep isl_space *space, __isl_keep isl_mat *div,
779 	__isl_take isl_printer *p, int latex)
780 {
781 	int i;
782 	isl_vec *c = NULL;
783 	int rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
784 	isl_size total = isl_basic_map_dim(bmap, isl_dim_all);
785 	unsigned o_div = isl_basic_map_offset(bmap, isl_dim_div);
786 	int first = 1;
787 	int dump;
788 
789 	if (total < 0 || !p)
790 		return isl_printer_free(p);
791 	bmap = isl_basic_map_copy(bmap);
792 	dump = p->dump;
793 	if (!dump)
794 		bmap = isl_basic_map_sort_constraints(bmap);
795 	if (!bmap)
796 		goto error;
797 
798 	c = isl_vec_alloc(bmap->ctx, 1 + total);
799 	if (!c)
800 		goto error;
801 
802 	for (i = bmap->n_eq - 1; i >= 0; --i) {
803 		int l = isl_seq_last_non_zero(bmap->eq[i], 1 + total);
804 		if (l < 0) {
805 			if (i != bmap->n_eq - 1)
806 				p = isl_printer_print_str(p, s_and[latex]);
807 			p = isl_printer_print_str(p, "0 = 0");
808 			continue;
809 		}
810 		if (!first)
811 			p = isl_printer_print_str(p, s_and[latex]);
812 		if (isl_int_is_neg(bmap->eq[i][l]))
813 			isl_seq_cpy(c->el, bmap->eq[i], 1 + total);
814 		else
815 			isl_seq_neg(c->el, bmap->eq[i], 1 + total);
816 		p = print_eq_constraint(p, space, div, c->el, l, latex);
817 		first = 0;
818 	}
819 	for (i = 0; i < bmap->n_ineq; ++i) {
820 		isl_bool combine;
821 		int l = isl_seq_last_non_zero(bmap->ineq[i], 1 + total);
822 		int strict;
823 		int s;
824 		const char *op;
825 		if (l < 0)
826 			continue;
827 		if (!dump && l >= o_div &&
828 		    can_print_div_expr(p, div, l - o_div)) {
829 			isl_bool is_div;
830 			is_div = isl_basic_map_is_div_constraint(bmap,
831 						    bmap->ineq[i], l - o_div);
832 			if (is_div < 0)
833 				goto error;
834 			if (is_div)
835 				continue;
836 		}
837 		if (!first)
838 			p = isl_printer_print_str(p, s_and[latex]);
839 		s = isl_int_sgn(bmap->ineq[i][l]);
840 		strict = !rational && isl_int_is_negone(bmap->ineq[i][0]);
841 		if (s < 0)
842 			isl_seq_cpy(c->el, bmap->ineq[i], 1 + total);
843 		else
844 			isl_seq_neg(c->el, bmap->ineq[i], 1 + total);
845 		if (strict)
846 			isl_int_set_si(c->el[0], 0);
847 		combine = dump ? isl_bool_false : next_is_opposite(bmap, i, l);
848 		if (combine < 0)
849 			goto error;
850 		if (combine) {
851 			op = constraint_op(-s, strict, latex);
852 			p = print_half_constraint(p, space, div, c->el, l,
853 						op, latex);
854 			first = 1;
855 		} else {
856 			op = constraint_op(s, strict, latex);
857 			p = print_constraint(p, space, div, c->el, l,
858 						op, latex);
859 			first = 0;
860 		}
861 	}
862 
863 	isl_basic_map_free(bmap);
864 	isl_vec_free(c);
865 
866 	return p;
867 error:
868 	isl_basic_map_free(bmap);
869 	isl_vec_free(c);
870 	isl_printer_free(p);
871 	return NULL;
872 }
873 
print_div(__isl_keep isl_space * space,__isl_keep isl_mat * div,int pos,__isl_take isl_printer * p)874 static __isl_give isl_printer *print_div(__isl_keep isl_space *space,
875 	__isl_keep isl_mat *div, int pos, __isl_take isl_printer *p)
876 {
877 	int c;
878 
879 	if (!p || !div)
880 		return isl_printer_free(p);
881 
882 	c = p->output_format == ISL_FORMAT_C;
883 	p = isl_printer_print_str(p, c ? "floord(" : "floor((");
884 	p = print_affine_of_len(space, div, p,
885 				div->row[pos] + 1, div->n_col - 1);
886 	p = isl_printer_print_str(p, c ? ", " : ")/");
887 	p = isl_printer_print_isl_int(p, div->row[pos][0]);
888 	p = isl_printer_print_str(p, ")");
889 	return p;
890 }
891 
892 /* Print a comma separated list of div names, except those that have
893  * a definition that can be printed.
894  * If "print_defined_divs" is set, then those div names are printed
895  * as well, along with their definitions.
896  */
print_div_list(__isl_take isl_printer * p,__isl_keep isl_space * space,__isl_keep isl_mat * div,int latex,int print_defined_divs)897 static __isl_give isl_printer *print_div_list(__isl_take isl_printer *p,
898 	__isl_keep isl_space *space, __isl_keep isl_mat *div, int latex,
899 	int print_defined_divs)
900 {
901 	int i;
902 	int first = 1;
903 	isl_size n_div;
904 
905 	n_div = isl_mat_rows(div);
906 	if (!p || !space || n_div < 0)
907 		return isl_printer_free(p);
908 
909 	for (i = 0; i < n_div; ++i) {
910 		if (!print_defined_divs && can_print_div_expr(p, div, i))
911 			continue;
912 		if (!first)
913 			p = isl_printer_print_str(p, ", ");
914 		p = print_name(space, p, isl_dim_div, i, latex);
915 		first = 0;
916 		if (!can_print_div_expr(p, div, i))
917 			continue;
918 		p = isl_printer_print_str(p, " = ");
919 		p = print_div(space, div, i, p);
920 	}
921 
922 	return p;
923 }
924 
925 /* Does printing an object with local variables described by "div"
926  * require an "exists" clause?
927  * That is, are there any local variables without an explicit representation?
928  * An exists clause is also needed in "dump" mode because
929  * explicit div representations are not printed inline in that case.
930  */
need_exists(__isl_keep isl_printer * p,__isl_keep isl_mat * div)931 static isl_bool need_exists(__isl_keep isl_printer *p, __isl_keep isl_mat *div)
932 {
933 	int i;
934 	isl_size n;
935 
936 	n = isl_mat_rows(div);
937 	if (!p || n < 0)
938 		return isl_bool_error;
939 	if (n == 0)
940 		return isl_bool_false;
941 	if (p->dump)
942 		return isl_bool_true;
943 	for (i = 0; i < n; ++i)
944 		if (!can_print_div_expr(p, div, i))
945 			return isl_bool_true;
946 	return isl_bool_false;
947 }
948 
949 /* Print the start of an exists clause, i.e.,
950  *
951  *	(exists variables:
952  *
953  * In dump mode, local variables with an explicit definition are printed
954  * as well because they will not be printed inline.
955  */
open_exists(__isl_take isl_printer * p,__isl_keep isl_space * space,__isl_keep isl_mat * div,int latex)956 static __isl_give isl_printer *open_exists(__isl_take isl_printer *p,
957 	__isl_keep isl_space *space, __isl_keep isl_mat *div, int latex)
958 {
959 	int dump;
960 
961 	if (!p)
962 		return NULL;
963 
964 	dump = p->dump;
965 	p = isl_printer_print_str(p, s_open_exists[latex]);
966 	p = print_div_list(p, space, div, latex, dump);
967 	p = isl_printer_print_str(p, ": ");
968 
969 	return p;
970 }
971 
972 /* Remove the explicit representations of all local variables in "div".
973  */
mark_all_unknown(__isl_take isl_mat * div)974 static __isl_give isl_mat *mark_all_unknown(__isl_take isl_mat *div)
975 {
976 	int i;
977 	isl_size n_div;
978 
979 	n_div = isl_mat_rows(div);
980 	if (n_div < 0)
981 		return isl_mat_free(div);
982 
983 	for (i = 0; i < n_div; ++i)
984 		div = isl_mat_set_element_si(div, i, 0, 0);
985 	return div;
986 }
987 
988 /* Print the constraints of "bmap" to "p".
989  * The names of the variables are taken from "space".
990  * "latex" is set if the constraints should be printed in LaTeX format.
991  * Do not print inline explicit div representations in "dump" mode.
992  */
print_disjunct(__isl_keep isl_basic_map * bmap,__isl_keep isl_space * space,__isl_take isl_printer * p,int latex)993 static __isl_give isl_printer *print_disjunct(__isl_keep isl_basic_map *bmap,
994 	__isl_keep isl_space *space, __isl_take isl_printer *p, int latex)
995 {
996 	int dump;
997 	isl_mat *div;
998 	isl_bool exists;
999 
1000 	if (!p)
1001 		return NULL;
1002 	dump = p->dump;
1003 	div = isl_basic_map_get_divs(bmap);
1004 	exists = need_exists(p, div);
1005 	if (exists >= 0 && exists)
1006 		p = open_exists(p, space, div, latex);
1007 
1008 	if (dump)
1009 		div = mark_all_unknown(div);
1010 	p = print_constraints(bmap, space, div, p, latex);
1011 	isl_mat_free(div);
1012 
1013 	if (exists >= 0 && exists)
1014 		p = isl_printer_print_str(p, s_close_exists[latex]);
1015 	return p;
1016 }
1017 
1018 /* Print a colon followed by the constraints of "bmap"
1019  * to "p", provided there are any constraints.
1020  * The names of the variables are taken from "space".
1021  * "latex" is set if the constraints should be printed in LaTeX format.
1022  */
print_optional_disjunct(__isl_keep isl_basic_map * bmap,__isl_keep isl_space * space,__isl_take isl_printer * p,int latex)1023 static __isl_give isl_printer *print_optional_disjunct(
1024 	__isl_keep isl_basic_map *bmap, __isl_keep isl_space *space,
1025 	__isl_take isl_printer *p, int latex)
1026 {
1027 	if (isl_basic_map_plain_is_universe(bmap))
1028 		return p;
1029 
1030 	p = isl_printer_print_str(p, ": ");
1031 	p = print_disjunct(bmap, space, p, latex);
1032 
1033 	return p;
1034 }
1035 
basic_map_print_omega(__isl_keep isl_basic_map * bmap,__isl_take isl_printer * p)1036 static __isl_give isl_printer *basic_map_print_omega(
1037 	__isl_keep isl_basic_map *bmap, __isl_take isl_printer *p)
1038 {
1039 	p = isl_printer_print_str(p, "{ [");
1040 	p = print_var_list(p, bmap->dim, isl_dim_in);
1041 	p = isl_printer_print_str(p, "] -> [");
1042 	p = print_var_list(p, bmap->dim, isl_dim_out);
1043 	p = isl_printer_print_str(p, "] ");
1044 	p = print_optional_disjunct(bmap, bmap->dim, p, 0);
1045 	p = isl_printer_print_str(p, " }");
1046 	return p;
1047 }
1048 
basic_set_print_omega(__isl_keep isl_basic_set * bset,__isl_take isl_printer * p)1049 static __isl_give isl_printer *basic_set_print_omega(
1050 	__isl_keep isl_basic_set *bset, __isl_take isl_printer *p)
1051 {
1052 	p = isl_printer_print_str(p, "{ [");
1053 	p = print_var_list(p, bset->dim, isl_dim_set);
1054 	p = isl_printer_print_str(p, "] ");
1055 	p = print_optional_disjunct(bset, bset->dim, p, 0);
1056 	p = isl_printer_print_str(p, " }");
1057 	return p;
1058 }
1059 
isl_map_print_omega(__isl_keep isl_map * map,__isl_take isl_printer * p)1060 static __isl_give isl_printer *isl_map_print_omega(__isl_keep isl_map *map,
1061 	__isl_take isl_printer *p)
1062 {
1063 	int i;
1064 
1065 	for (i = 0; i < map->n; ++i) {
1066 		if (i)
1067 			p = isl_printer_print_str(p, " union ");
1068 		p = basic_map_print_omega(map->p[i], p);
1069 	}
1070 	return p;
1071 }
1072 
isl_set_print_omega(__isl_keep isl_set * set,__isl_take isl_printer * p)1073 static __isl_give isl_printer *isl_set_print_omega(__isl_keep isl_set *set,
1074 	__isl_take isl_printer *p)
1075 {
1076 	int i;
1077 
1078 	for (i = 0; i < set->n; ++i) {
1079 		if (i)
1080 			p = isl_printer_print_str(p, " union ");
1081 		p = basic_set_print_omega(set->p[i], p);
1082 	}
1083 	return p;
1084 }
1085 
1086 /* Print the list of parameters in "space", followed by an arrow, to "p",
1087  * if there are any parameters.
1088  */
print_param_tuple(__isl_take isl_printer * p,__isl_keep isl_space * space,struct isl_print_space_data * data)1089 static __isl_give isl_printer *print_param_tuple(__isl_take isl_printer *p,
1090 	__isl_keep isl_space *space, struct isl_print_space_data *data)
1091 {
1092 	isl_size nparam;
1093 
1094 	nparam = isl_space_dim(space, isl_dim_param);
1095 	if (!p || nparam < 0)
1096 		return isl_printer_free(p);
1097 	if (nparam == 0)
1098 		return p;
1099 
1100 	p = print_tuple(space, p, isl_dim_param, data);
1101 	p = isl_printer_print_str(p, s_to[data->latex]);
1102 
1103 	return p;
1104 }
1105 
isl_basic_map_print_isl(__isl_keep isl_basic_map * bmap,__isl_take isl_printer * p,int latex)1106 static __isl_give isl_printer *isl_basic_map_print_isl(
1107 	__isl_keep isl_basic_map *bmap, __isl_take isl_printer *p,
1108 	int latex)
1109 {
1110 	struct isl_print_space_data data = { .latex = latex };
1111 	int rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
1112 
1113 	p = print_param_tuple(p, bmap->dim, &data);
1114 	p = isl_printer_print_str(p, "{ ");
1115 	p = isl_print_space(bmap->dim, p, rational, &data);
1116 	p = isl_printer_print_str(p, " : ");
1117 	p = print_disjunct(bmap, bmap->dim, p, latex);
1118 	p = isl_printer_print_str(p, " }");
1119 	return p;
1120 }
1121 
1122 /* Print the disjuncts of a map (or set) "map" to "p".
1123  * The names of the variables are taken from "space".
1124  * "latex" is set if the constraints should be printed in LaTeX format.
1125  */
print_disjuncts_core(__isl_keep isl_map * map,__isl_keep isl_space * space,__isl_take isl_printer * p,int latex)1126 static __isl_give isl_printer *print_disjuncts_core(__isl_keep isl_map *map,
1127 	__isl_keep isl_space *space, __isl_take isl_printer *p, int latex)
1128 {
1129 	int i;
1130 
1131 	if (map->n == 0)
1132 		p = isl_printer_print_str(p, "false");
1133 	for (i = 0; i < map->n; ++i) {
1134 		if (i)
1135 			p = isl_printer_print_str(p, s_or[latex]);
1136 		if (map->n > 1 && map->p[i]->n_eq + map->p[i]->n_ineq > 1)
1137 			p = isl_printer_print_str(p, "(");
1138 		p = print_disjunct(map->p[i], space, p, latex);
1139 		if (map->n > 1 && map->p[i]->n_eq + map->p[i]->n_ineq > 1)
1140 			p = isl_printer_print_str(p, ")");
1141 	}
1142 	return p;
1143 }
1144 
1145 /* Print the disjuncts of a map (or set) "map" to "p".
1146  * The names of the variables are taken from "space".
1147  * "hull" describes constraints shared by all disjuncts of "map".
1148  * "latex" is set if the constraints should be printed in LaTeX format.
1149  *
1150  * Print the disjuncts as a conjunction of "hull" and
1151  * the result of removing the constraints of "hull" from "map".
1152  * If this result turns out to be the universe, then simply print "hull".
1153  */
print_disjuncts_in_hull(__isl_keep isl_map * map,__isl_keep isl_space * space,__isl_take isl_basic_map * hull,__isl_take isl_printer * p,int latex)1154 static __isl_give isl_printer *print_disjuncts_in_hull(__isl_keep isl_map *map,
1155 	__isl_keep isl_space *space, __isl_take isl_basic_map *hull,
1156 	__isl_take isl_printer *p, int latex)
1157 {
1158 	isl_bool is_universe;
1159 
1160 	p = print_disjunct(hull, space, p, latex);
1161 	map = isl_map_plain_gist_basic_map(isl_map_copy(map), hull);
1162 	is_universe = isl_map_plain_is_universe(map);
1163 	if (is_universe < 0)
1164 		goto error;
1165 	if (!is_universe) {
1166 		p = isl_printer_print_str(p, s_and[latex]);
1167 		p = isl_printer_print_str(p, "(");
1168 		p = print_disjuncts_core(map, space, p, latex);
1169 		p = isl_printer_print_str(p, ")");
1170 	}
1171 	isl_map_free(map);
1172 
1173 	return p;
1174 error:
1175 	isl_map_free(map);
1176 	isl_printer_free(p);
1177 	return NULL;
1178 }
1179 
1180 /* Print the disjuncts of a map (or set) "map" to "p".
1181  * The names of the variables are taken from "space".
1182  * "latex" is set if the constraints should be printed in LaTeX format.
1183  *
1184  * If there are at least two disjuncts and "dump" mode is not turned out,
1185  * check for any shared constraints among all disjuncts.
1186  * If there are any, then print them separately in print_disjuncts_in_hull.
1187  */
print_disjuncts(__isl_keep isl_map * map,__isl_keep isl_space * space,__isl_take isl_printer * p,int latex)1188 static __isl_give isl_printer *print_disjuncts(__isl_keep isl_map *map,
1189 	__isl_keep isl_space *space, __isl_take isl_printer *p, int latex)
1190 {
1191 	if (isl_map_plain_is_universe(map))
1192 		return p;
1193 
1194 	p = isl_printer_print_str(p, s_such_that[latex]);
1195 	if (!p)
1196 		return NULL;
1197 
1198 	if (!p->dump && map->n >= 2) {
1199 		isl_basic_map *hull;
1200 		isl_bool is_universe;
1201 
1202 		hull = isl_map_plain_unshifted_simple_hull(isl_map_copy(map));
1203 		is_universe = isl_basic_map_plain_is_universe(hull);
1204 		if (is_universe < 0)
1205 			p = isl_printer_free(p);
1206 		else if (!is_universe)
1207 			return print_disjuncts_in_hull(map, space, hull,
1208 							p, latex);
1209 		isl_basic_map_free(hull);
1210 	}
1211 
1212 	return print_disjuncts_core(map, space, p, latex);
1213 }
1214 
1215 /* Print the disjuncts of a map (or set).
1216  * The names of the variables are taken from "space".
1217  * "latex" is set if the constraints should be printed in LaTeX format.
1218  *
1219  * If the map turns out to be a universal parameter domain, then
1220  * we need to print the colon.  Otherwise, the output looks identical
1221  * to the empty set.
1222  */
print_disjuncts_map(__isl_keep isl_map * map,__isl_keep isl_space * space,__isl_take isl_printer * p,int latex)1223 static __isl_give isl_printer *print_disjuncts_map(__isl_keep isl_map *map,
1224 	__isl_keep isl_space *space, __isl_take isl_printer *p, int latex)
1225 {
1226 	if (isl_map_plain_is_universe(map) && isl_space_is_params(map->dim))
1227 		return isl_printer_print_str(p, s_such_that[latex]);
1228 	else
1229 		return print_disjuncts(map, space, p, latex);
1230 }
1231 
1232 /* Print the disjuncts of a set.
1233  * The names of the variables are taken from "space".
1234  * "latex" is set if the constraints should be printed in LaTeX format.
1235  */
print_disjuncts_set(__isl_keep isl_set * set,__isl_keep isl_space * space,__isl_take isl_printer * p,int latex)1236 static __isl_give isl_printer *print_disjuncts_set(__isl_keep isl_set *set,
1237 	__isl_keep isl_space *space, __isl_take isl_printer *p, int latex)
1238 {
1239 	return print_disjuncts_map(set_to_map(set), space, p, latex);
1240 }
1241 
1242 struct isl_aff_split {
1243 	isl_basic_map *aff;
1244 	isl_map *map;
1245 };
1246 
free_split(__isl_take struct isl_aff_split * split,int n)1247 static void free_split(__isl_take struct isl_aff_split *split, int n)
1248 {
1249 	int i;
1250 
1251 	if (!split)
1252 		return;
1253 
1254 	for (i = 0; i < n; ++i) {
1255 		isl_basic_map_free(split[i].aff);
1256 		isl_map_free(split[i].map);
1257 	}
1258 
1259 	free(split);
1260 }
1261 
get_aff(__isl_take isl_basic_map * bmap)1262 static __isl_give isl_basic_map *get_aff(__isl_take isl_basic_map *bmap)
1263 {
1264 	int i, j;
1265 	isl_size nparam, n_in, n_out, total;
1266 
1267 	bmap = isl_basic_map_cow(bmap);
1268 	if (!bmap)
1269 		return NULL;
1270 	bmap = isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1271 
1272 	nparam = isl_basic_map_dim(bmap, isl_dim_param);
1273 	n_in = isl_basic_map_dim(bmap, isl_dim_in);
1274 	n_out = isl_basic_map_dim(bmap, isl_dim_out);
1275 	total = isl_basic_map_dim(bmap, isl_dim_all);
1276 	if (n_in < 0 || n_out < 0 || nparam < 0 || total < 0)
1277 		return isl_basic_map_free(bmap);
1278 
1279 	for (i = bmap->n_eq - 1; i >= 0; --i) {
1280 		j = isl_seq_last_non_zero(bmap->eq[i] + 1, total);
1281 		if (j >= nparam && j < nparam + n_in + n_out &&
1282 		    (isl_int_is_one(bmap->eq[i][1 + j]) ||
1283 		     isl_int_is_negone(bmap->eq[i][1 + j])))
1284 			continue;
1285 		if (isl_basic_map_drop_equality(bmap, i) < 0)
1286 			goto error;
1287 	}
1288 
1289 	bmap = isl_basic_map_finalize(bmap);
1290 
1291 	return bmap;
1292 error:
1293 	isl_basic_map_free(bmap);
1294 	return NULL;
1295 }
1296 
aff_split_cmp(const void * p1,const void * p2,void * user)1297 static int aff_split_cmp(const void *p1, const void *p2, void *user)
1298 {
1299 	const struct isl_aff_split *s1, *s2;
1300 	s1 = (const struct isl_aff_split *) p1;
1301 	s2 = (const struct isl_aff_split *) p2;
1302 
1303 	return isl_basic_map_plain_cmp(s1->aff, s2->aff);
1304 }
1305 
drop_aff(__isl_take isl_basic_map * bmap,__isl_keep isl_basic_map * aff)1306 static __isl_give isl_basic_map *drop_aff(__isl_take isl_basic_map *bmap,
1307 	__isl_keep isl_basic_map *aff)
1308 {
1309 	int i, j;
1310 	isl_size v_div;
1311 
1312 	v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
1313 	if (v_div < 0 || !aff)
1314 		goto error;
1315 
1316 	for (i = bmap->n_eq - 1; i >= 0; --i) {
1317 		if (isl_seq_first_non_zero(bmap->eq[i] + 1 + v_div,
1318 					    bmap->n_div) != -1)
1319 			continue;
1320 		for (j = 0; j < aff->n_eq; ++j) {
1321 			if (!isl_seq_eq(bmap->eq[i], aff->eq[j], 1 + v_div) &&
1322 			    !isl_seq_is_neg(bmap->eq[i], aff->eq[j], 1 + v_div))
1323 				continue;
1324 			if (isl_basic_map_drop_equality(bmap, i) < 0)
1325 				goto error;
1326 			break;
1327 		}
1328 	}
1329 
1330 	return bmap;
1331 error:
1332 	isl_basic_map_free(bmap);
1333 	return NULL;
1334 }
1335 
split_aff(__isl_keep isl_map * map)1336 static __isl_give struct isl_aff_split *split_aff(__isl_keep isl_map *map)
1337 {
1338 	int i, n;
1339 	struct isl_aff_split *split;
1340 	isl_ctx *ctx;
1341 
1342 	ctx = isl_map_get_ctx(map);
1343 	split = isl_calloc_array(ctx, struct isl_aff_split, map->n);
1344 	if (!split)
1345 		return NULL;
1346 
1347 	for (i = 0; i < map->n; ++i) {
1348 		isl_basic_map *bmap;
1349 		split[i].aff = get_aff(isl_basic_map_copy(map->p[i]));
1350 		bmap = isl_basic_map_copy(map->p[i]);
1351 		bmap = isl_basic_map_cow(bmap);
1352 		bmap = drop_aff(bmap, split[i].aff);
1353 		split[i].map = isl_map_from_basic_map(bmap);
1354 		if (!split[i].aff || !split[i].map)
1355 			goto error;
1356 	}
1357 
1358 	if (isl_sort(split, map->n, sizeof(struct isl_aff_split),
1359 			&aff_split_cmp, NULL) < 0)
1360 		goto error;
1361 
1362 	n = map->n;
1363 	for (i = n - 1; i >= 1; --i) {
1364 		if (!isl_basic_map_plain_is_equal(split[i - 1].aff,
1365 						 split[i].aff))
1366 			continue;
1367 		isl_basic_map_free(split[i].aff);
1368 		split[i - 1].map = isl_map_union(split[i - 1].map,
1369 						 split[i].map);
1370 		if (i != n - 1)
1371 			split[i] = split[n - 1];
1372 		split[n - 1].aff = NULL;
1373 		split[n - 1].map = NULL;
1374 		--n;
1375 	}
1376 
1377 	return split;
1378 error:
1379 	free_split(split, map->n);
1380 	return NULL;
1381 }
1382 
defining_equality(__isl_keep isl_basic_map * eq,__isl_keep isl_space * space,enum isl_dim_type type,int pos)1383 static int defining_equality(__isl_keep isl_basic_map *eq,
1384 	__isl_keep isl_space *space, enum isl_dim_type type, int pos)
1385 {
1386 	int i;
1387 	isl_size total;
1388 
1389 	total = isl_basic_map_dim(eq, isl_dim_all);
1390 	if (total < 0)
1391 		return -1;
1392 
1393 	pos += isl_space_offset(space, type);
1394 
1395 	for (i = 0; i < eq->n_eq; ++i) {
1396 		if (isl_seq_last_non_zero(eq->eq[i] + 1, total) != pos)
1397 			continue;
1398 		if (isl_int_is_one(eq->eq[i][1 + pos]))
1399 			isl_seq_neg(eq->eq[i], eq->eq[i], 1 + total);
1400 		return i;
1401 	}
1402 
1403 	return -1;
1404 }
1405 
1406 /* Print dimension "pos" of data->space to "p".
1407  *
1408  * data->user is assumed to be an isl_basic_map keeping track of equalities.
1409  *
1410  * If the current dimension is defined by these equalities, then print
1411  * the corresponding expression, assigned to the name of the dimension
1412  * if there is any.  Otherwise, print the name of the dimension.
1413  */
print_dim_eq(__isl_take isl_printer * p,struct isl_print_space_data * data,unsigned pos)1414 static __isl_give isl_printer *print_dim_eq(__isl_take isl_printer *p,
1415 	struct isl_print_space_data *data, unsigned pos)
1416 {
1417 	isl_basic_map *eq = data->user;
1418 	int j;
1419 
1420 	j = defining_equality(eq, data->space, data->type, pos);
1421 	if (j >= 0) {
1422 		if (isl_space_has_dim_name(data->space, data->type, pos)) {
1423 			p = print_name(data->space, p, data->type, pos,
1424 					data->latex);
1425 			p = isl_printer_print_str(p, " = ");
1426 		}
1427 		pos += 1 + isl_space_offset(data->space, data->type);
1428 		p = print_affine_of_len(data->space, NULL, p, eq->eq[j], pos);
1429 	} else {
1430 		p = print_name(data->space, p, data->type, pos, data->latex);
1431 	}
1432 
1433 	return p;
1434 }
1435 
print_split_map(__isl_take isl_printer * p,struct isl_aff_split * split,int n,__isl_keep isl_space * space)1436 static __isl_give isl_printer *print_split_map(__isl_take isl_printer *p,
1437 	struct isl_aff_split *split, int n, __isl_keep isl_space *space)
1438 {
1439 	struct isl_print_space_data data = { 0 };
1440 	int i;
1441 	int rational;
1442 
1443 	data.print_dim = &print_dim_eq;
1444 	for (i = 0; i < n; ++i) {
1445 		if (!split[i].map)
1446 			break;
1447 		rational = split[i].map->n > 0 &&
1448 		    ISL_F_ISSET(split[i].map->p[0], ISL_BASIC_MAP_RATIONAL);
1449 		if (i)
1450 			p = isl_printer_print_str(p, "; ");
1451 		data.user = split[i].aff;
1452 		p = isl_print_space(space, p, rational, &data);
1453 		p = print_disjuncts_map(split[i].map, space, p, 0);
1454 	}
1455 
1456 	return p;
1457 }
1458 
isl_map_print_isl_body(__isl_keep isl_map * map,__isl_take isl_printer * p)1459 static __isl_give isl_printer *isl_map_print_isl_body(__isl_keep isl_map *map,
1460 	__isl_take isl_printer *p)
1461 {
1462 	struct isl_print_space_data data = { 0 };
1463 	struct isl_aff_split *split = NULL;
1464 	int rational;
1465 
1466 	if (!p || !map)
1467 		return isl_printer_free(p);
1468 	if (!p->dump && map->n > 0)
1469 		split = split_aff(map);
1470 	if (split) {
1471 		p = print_split_map(p, split, map->n, map->dim);
1472 	} else {
1473 		rational = map->n > 0 &&
1474 		    ISL_F_ISSET(map->p[0], ISL_BASIC_MAP_RATIONAL);
1475 		p = isl_print_space(map->dim, p, rational, &data);
1476 		p = print_disjuncts_map(map, map->dim, p, 0);
1477 	}
1478 	free_split(split, map->n);
1479 	return p;
1480 }
1481 
isl_map_print_isl(__isl_keep isl_map * map,__isl_take isl_printer * p)1482 static __isl_give isl_printer *isl_map_print_isl(__isl_keep isl_map *map,
1483 	__isl_take isl_printer *p)
1484 {
1485 	struct isl_print_space_data data = { 0 };
1486 
1487 	p = print_param_tuple(p, map->dim, &data);
1488 	p = isl_printer_print_str(p, s_open_set[0]);
1489 	p = isl_map_print_isl_body(map, p);
1490 	p = isl_printer_print_str(p, s_close_set[0]);
1491 	return p;
1492 }
1493 
print_latex_map(__isl_keep isl_map * map,__isl_take isl_printer * p,__isl_keep isl_basic_map * aff)1494 static __isl_give isl_printer *print_latex_map(__isl_keep isl_map *map,
1495 	__isl_take isl_printer *p, __isl_keep isl_basic_map *aff)
1496 {
1497 	struct isl_print_space_data data = { 0 };
1498 
1499 	data.latex = 1;
1500 	p = print_param_tuple(p, map->dim, &data);
1501 	p = isl_printer_print_str(p, s_open_set[1]);
1502 	data.print_dim = &print_dim_eq;
1503 	data.user = aff;
1504 	p = isl_print_space(map->dim, p, 0, &data);
1505 	p = print_disjuncts_map(map, map->dim, p, 1);
1506 	p = isl_printer_print_str(p, s_close_set[1]);
1507 
1508 	return p;
1509 }
1510 
isl_map_print_latex(__isl_keep isl_map * map,__isl_take isl_printer * p)1511 static __isl_give isl_printer *isl_map_print_latex(__isl_keep isl_map *map,
1512 	__isl_take isl_printer *p)
1513 {
1514 	int i;
1515 	struct isl_aff_split *split = NULL;
1516 
1517 	if (map->n > 0)
1518 		split = split_aff(map);
1519 
1520 	if (!split)
1521 		return print_latex_map(map, p, NULL);
1522 
1523 	for (i = 0; i < map->n; ++i) {
1524 		if (!split[i].map)
1525 			break;
1526 		if (i)
1527 			p = isl_printer_print_str(p, " \\cup ");
1528 		p = print_latex_map(split[i].map, p, split[i].aff);
1529 	}
1530 
1531 	free_split(split, map->n);
1532 	return p;
1533 }
1534 
isl_printer_print_basic_map(__isl_take isl_printer * p,__isl_keep isl_basic_map * bmap)1535 __isl_give isl_printer *isl_printer_print_basic_map(__isl_take isl_printer *p,
1536 	__isl_keep isl_basic_map *bmap)
1537 {
1538 	if (!p || !bmap)
1539 		goto error;
1540 	if (p->output_format == ISL_FORMAT_ISL)
1541 		return isl_basic_map_print_isl(bmap, p, 0);
1542 	else if (p->output_format == ISL_FORMAT_OMEGA)
1543 		return basic_map_print_omega(bmap, p);
1544 	isl_assert(bmap->ctx, 0, goto error);
1545 error:
1546 	isl_printer_free(p);
1547 	return NULL;
1548 }
1549 
isl_printer_print_basic_set(__isl_take isl_printer * p,__isl_keep isl_basic_set * bset)1550 __isl_give isl_printer *isl_printer_print_basic_set(__isl_take isl_printer *p,
1551 	__isl_keep isl_basic_set *bset)
1552 {
1553 	if (!p || !bset)
1554 		goto error;
1555 
1556 	if (p->output_format == ISL_FORMAT_ISL)
1557 		return isl_basic_map_print_isl(bset, p, 0);
1558 	else if (p->output_format == ISL_FORMAT_POLYLIB)
1559 		return isl_basic_set_print_polylib(bset, p, 0);
1560 	else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
1561 		return isl_basic_set_print_polylib(bset, p, 1);
1562 	else if (p->output_format == ISL_FORMAT_POLYLIB_CONSTRAINTS)
1563 		return bset_print_constraints_polylib(bset, p);
1564 	else if (p->output_format == ISL_FORMAT_OMEGA)
1565 		return basic_set_print_omega(bset, p);
1566 	isl_assert(p->ctx, 0, goto error);
1567 error:
1568 	isl_printer_free(p);
1569 	return NULL;
1570 }
1571 
isl_printer_print_set(__isl_take isl_printer * p,__isl_keep isl_set * set)1572 __isl_give isl_printer *isl_printer_print_set(__isl_take isl_printer *p,
1573 	__isl_keep isl_set *set)
1574 {
1575 	if (!p || !set)
1576 		goto error;
1577 	if (p->output_format == ISL_FORMAT_ISL)
1578 		return isl_map_print_isl(set_to_map(set), p);
1579 	else if (p->output_format == ISL_FORMAT_POLYLIB)
1580 		return isl_set_print_polylib(set, p, 0);
1581 	else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
1582 		return isl_set_print_polylib(set, p, 1);
1583 	else if (p->output_format == ISL_FORMAT_OMEGA)
1584 		return isl_set_print_omega(set, p);
1585 	else if (p->output_format == ISL_FORMAT_LATEX)
1586 		return isl_map_print_latex(set_to_map(set), p);
1587 	isl_assert(set->ctx, 0, goto error);
1588 error:
1589 	isl_printer_free(p);
1590 	return NULL;
1591 }
1592 
isl_printer_print_map(__isl_take isl_printer * p,__isl_keep isl_map * map)1593 __isl_give isl_printer *isl_printer_print_map(__isl_take isl_printer *p,
1594 	__isl_keep isl_map *map)
1595 {
1596 	if (!p || !map)
1597 		goto error;
1598 
1599 	if (p->output_format == ISL_FORMAT_ISL)
1600 		return isl_map_print_isl(map, p);
1601 	else if (p->output_format == ISL_FORMAT_POLYLIB)
1602 		return isl_map_print_polylib(map, p, 0);
1603 	else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
1604 		return isl_map_print_polylib(map, p, 1);
1605 	else if (p->output_format == ISL_FORMAT_OMEGA)
1606 		return isl_map_print_omega(map, p);
1607 	else if (p->output_format == ISL_FORMAT_LATEX)
1608 		return isl_map_print_latex(map, p);
1609 	isl_assert(map->ctx, 0, goto error);
1610 error:
1611 	isl_printer_free(p);
1612 	return NULL;
1613 }
1614 
1615 struct isl_union_print_data {
1616 	isl_printer *p;
1617 	int first;
1618 };
1619 
print_map_body(__isl_take isl_map * map,void * user)1620 static isl_stat print_map_body(__isl_take isl_map *map, void *user)
1621 {
1622 	struct isl_union_print_data *data;
1623 	data = (struct isl_union_print_data *)user;
1624 
1625 	if (!data->first)
1626 		data->p = isl_printer_print_str(data->p, "; ");
1627 	data->first = 0;
1628 
1629 	data->p = isl_map_print_isl_body(map, data->p);
1630 	isl_map_free(map);
1631 
1632 	return isl_stat_ok;
1633 }
1634 
1635 /* Print the body of "umap" (everything except the parameter declarations)
1636  * to "p" in isl format.
1637  */
isl_printer_print_union_map_isl_body(__isl_take isl_printer * p,__isl_keep isl_union_map * umap)1638 static __isl_give isl_printer *isl_printer_print_union_map_isl_body(
1639 	__isl_take isl_printer *p, __isl_keep isl_union_map *umap)
1640 {
1641 	struct isl_union_print_data data;
1642 
1643 	p = isl_printer_print_str(p, s_open_set[0]);
1644 	data.p = p;
1645 	data.first = 1;
1646 	isl_union_map_foreach_map(umap, &print_map_body, &data);
1647 	p = data.p;
1648 	p = isl_printer_print_str(p, s_close_set[0]);
1649 	return p;
1650 }
1651 
1652 /* Print the body of "uset" (everything except the parameter declarations)
1653  * to "p" in isl format.
1654  */
isl_printer_print_union_set_isl_body(__isl_take isl_printer * p,__isl_keep isl_union_set * uset)1655 static __isl_give isl_printer *isl_printer_print_union_set_isl_body(
1656 	__isl_take isl_printer *p, __isl_keep isl_union_set *uset)
1657 {
1658 	return isl_printer_print_union_map_isl_body(p, uset_to_umap(uset));
1659 }
1660 
1661 /* Print the isl_union_map "umap" to "p" in isl format.
1662  */
isl_union_map_print_isl(__isl_keep isl_union_map * umap,__isl_take isl_printer * p)1663 static __isl_give isl_printer *isl_union_map_print_isl(
1664 	__isl_keep isl_union_map *umap, __isl_take isl_printer *p)
1665 {
1666 	struct isl_print_space_data space_data = { 0 };
1667 	isl_space *space;
1668 
1669 	space = isl_union_map_get_space(umap);
1670 	p = print_param_tuple(p, space, &space_data);
1671 	isl_space_free(space);
1672 
1673 	p = isl_printer_print_union_map_isl_body(p, umap);
1674 
1675 	return p;
1676 }
1677 
print_latex_map_body(__isl_take isl_map * map,void * user)1678 static isl_stat print_latex_map_body(__isl_take isl_map *map, void *user)
1679 {
1680 	struct isl_union_print_data *data;
1681 	data = (struct isl_union_print_data *)user;
1682 
1683 	if (!data->first)
1684 		data->p = isl_printer_print_str(data->p, " \\cup ");
1685 	data->first = 0;
1686 
1687 	data->p = isl_map_print_latex(map, data->p);
1688 	isl_map_free(map);
1689 
1690 	return isl_stat_ok;
1691 }
1692 
isl_union_map_print_latex(__isl_keep isl_union_map * umap,__isl_take isl_printer * p)1693 static __isl_give isl_printer *isl_union_map_print_latex(
1694 	__isl_keep isl_union_map *umap, __isl_take isl_printer *p)
1695 {
1696 	struct isl_union_print_data data = { p, 1 };
1697 	isl_union_map_foreach_map(umap, &print_latex_map_body, &data);
1698 	p = data.p;
1699 	return p;
1700 }
1701 
isl_printer_print_union_map(__isl_take isl_printer * p,__isl_keep isl_union_map * umap)1702 __isl_give isl_printer *isl_printer_print_union_map(__isl_take isl_printer *p,
1703 	__isl_keep isl_union_map *umap)
1704 {
1705 	if (!p || !umap)
1706 		goto error;
1707 
1708 	if (p->output_format == ISL_FORMAT_ISL)
1709 		return isl_union_map_print_isl(umap, p);
1710 	if (p->output_format == ISL_FORMAT_LATEX)
1711 		return isl_union_map_print_latex(umap, p);
1712 
1713 	isl_die(p->ctx, isl_error_invalid,
1714 		"invalid output format for isl_union_map", goto error);
1715 error:
1716 	isl_printer_free(p);
1717 	return NULL;
1718 }
1719 
isl_printer_print_union_set(__isl_take isl_printer * p,__isl_keep isl_union_set * uset)1720 __isl_give isl_printer *isl_printer_print_union_set(__isl_take isl_printer *p,
1721 	__isl_keep isl_union_set *uset)
1722 {
1723 	if (!p || !uset)
1724 		goto error;
1725 
1726 	if (p->output_format == ISL_FORMAT_ISL)
1727 		return isl_union_map_print_isl(uset_to_umap(uset), p);
1728 	if (p->output_format == ISL_FORMAT_LATEX)
1729 		return isl_union_map_print_latex(uset_to_umap(uset), p);
1730 
1731 	isl_die(p->ctx, isl_error_invalid,
1732 		"invalid output format for isl_union_set", goto error);
1733 error:
1734 	isl_printer_free(p);
1735 	return NULL;
1736 }
1737 
poly_rec_n_non_zero(__isl_keep isl_poly_rec * rec)1738 static isl_size poly_rec_n_non_zero(__isl_keep isl_poly_rec *rec)
1739 {
1740 	int i;
1741 	int n;
1742 
1743 	if (!rec)
1744 		return isl_size_error;
1745 
1746 	for (i = 0, n = 0; i < rec->n; ++i) {
1747 		isl_bool is_zero = isl_poly_is_zero(rec->p[i]);
1748 
1749 		if (is_zero < 0)
1750 			return isl_size_error;
1751 		if (!is_zero)
1752 			++n;
1753 	}
1754 
1755 	return n;
1756 }
1757 
poly_print_cst(__isl_keep isl_poly * poly,__isl_take isl_printer * p,int first)1758 static __isl_give isl_printer *poly_print_cst(__isl_keep isl_poly *poly,
1759 	__isl_take isl_printer *p, int first)
1760 {
1761 	isl_poly_cst *cst;
1762 	int neg;
1763 
1764 	cst = isl_poly_as_cst(poly);
1765 	if (!cst)
1766 		goto error;
1767 	neg = !first && isl_int_is_neg(cst->n);
1768 	if (!first)
1769 		p = isl_printer_print_str(p, neg ? " - " :  " + ");
1770 	if (neg)
1771 		isl_int_neg(cst->n, cst->n);
1772 	if (isl_int_is_zero(cst->d)) {
1773 		int sgn = isl_int_sgn(cst->n);
1774 		p = isl_printer_print_str(p, sgn < 0 ? "-infty" :
1775 					    sgn == 0 ? "NaN" : "infty");
1776 	} else
1777 		p = isl_printer_print_isl_int(p, cst->n);
1778 	if (neg)
1779 		isl_int_neg(cst->n, cst->n);
1780 	if (!isl_int_is_zero(cst->d) && !isl_int_is_one(cst->d)) {
1781 		p = isl_printer_print_str(p, "/");
1782 		p = isl_printer_print_isl_int(p, cst->d);
1783 	}
1784 	return p;
1785 error:
1786 	isl_printer_free(p);
1787 	return NULL;
1788 }
1789 
print_base(__isl_take isl_printer * p,__isl_keep isl_space * space,__isl_keep isl_mat * div,int var)1790 static __isl_give isl_printer *print_base(__isl_take isl_printer *p,
1791 	__isl_keep isl_space *space, __isl_keep isl_mat *div, int var)
1792 {
1793 	isl_size total;
1794 
1795 	total = isl_space_dim(space, isl_dim_all);
1796 	if (total < 0)
1797 		return isl_printer_free(p);
1798 	if (var < total)
1799 		p = print_term(space, NULL, space->ctx->one, 1 + var, p, 0);
1800 	else
1801 		p = print_div(space, div, var - total, p);
1802 	return p;
1803 }
1804 
print_pow(__isl_take isl_printer * p,__isl_keep isl_space * space,__isl_keep isl_mat * div,int var,int exp)1805 static __isl_give isl_printer *print_pow(__isl_take isl_printer *p,
1806 	__isl_keep isl_space *space, __isl_keep isl_mat *div, int var, int exp)
1807 {
1808 	p = print_base(p, space, div, var);
1809 	if (exp == 1)
1810 		return p;
1811 	if (p->output_format == ISL_FORMAT_C) {
1812 		int i;
1813 		for (i = 1; i < exp; ++i) {
1814 			p = isl_printer_print_str(p, "*");
1815 			p = print_base(p, space, div, var);
1816 		}
1817 	} else {
1818 		p = isl_printer_print_str(p, "^");
1819 		p = isl_printer_print_int(p, exp);
1820 	}
1821 	return p;
1822 }
1823 
1824 /* Print the polynomial "poly" defined over the domain space "space" and
1825  * local variables defined by "div" to "p".
1826  */
poly_print(__isl_keep isl_poly * poly,__isl_keep isl_space * space,__isl_keep isl_mat * div,__isl_take isl_printer * p)1827 static __isl_give isl_printer *poly_print(__isl_keep isl_poly *poly,
1828 	__isl_keep isl_space *space, __isl_keep isl_mat *div,
1829 	__isl_take isl_printer *p)
1830 {
1831 	int i, first, print_parens;
1832 	isl_size n;
1833 	isl_bool is_cst;
1834 	isl_poly_rec *rec;
1835 
1836 	is_cst = isl_poly_is_cst(poly);
1837 	if (!p || is_cst < 0 || !space || !div)
1838 		goto error;
1839 
1840 	if (is_cst)
1841 		return poly_print_cst(poly, p, 1);
1842 
1843 	rec = isl_poly_as_rec(poly);
1844 	n = poly_rec_n_non_zero(rec);
1845 	if (n < 0)
1846 		return isl_printer_free(p);
1847 	print_parens = n > 1;
1848 	if (print_parens)
1849 		p = isl_printer_print_str(p, "(");
1850 	for (i = 0, first = 1; i < rec->n; ++i) {
1851 		isl_bool is_zero = isl_poly_is_zero(rec->p[i]);
1852 		isl_bool is_one = isl_poly_is_one(rec->p[i]);
1853 		isl_bool is_negone = isl_poly_is_negone(rec->p[i]);
1854 		isl_bool is_cst = isl_poly_is_cst(rec->p[i]);
1855 
1856 		if (is_zero < 0 || is_one < 0 || is_negone < 0)
1857 			return isl_printer_free(p);
1858 		if (is_zero)
1859 			continue;
1860 		if (is_negone) {
1861 			if (!i)
1862 				p = isl_printer_print_str(p, "-1");
1863 			else if (first)
1864 				p = isl_printer_print_str(p, "-");
1865 			else
1866 				p = isl_printer_print_str(p, " - ");
1867 		} else if (is_cst && !is_one)
1868 			p = poly_print_cst(rec->p[i], p, first);
1869 		else {
1870 			if (!first)
1871 				p = isl_printer_print_str(p, " + ");
1872 			if (i == 0 || !is_one)
1873 				p = poly_print(rec->p[i], space, div, p);
1874 		}
1875 		first = 0;
1876 		if (i == 0)
1877 			continue;
1878 		if (!is_one && !is_negone)
1879 			p = isl_printer_print_str(p, " * ");
1880 		p = print_pow(p, space, div, rec->poly.var, i);
1881 	}
1882 	if (print_parens)
1883 		p = isl_printer_print_str(p, ")");
1884 	return p;
1885 error:
1886 	isl_printer_free(p);
1887 	return NULL;
1888 }
1889 
print_qpolynomial(__isl_take isl_printer * p,__isl_keep isl_qpolynomial * qp)1890 static __isl_give isl_printer *print_qpolynomial(__isl_take isl_printer *p,
1891 	__isl_keep isl_qpolynomial *qp)
1892 {
1893 	if (!p || !qp)
1894 		goto error;
1895 	p = poly_print(qp->poly, qp->dim, qp->div, p);
1896 	return p;
1897 error:
1898 	isl_printer_free(p);
1899 	return NULL;
1900 }
1901 
print_qpolynomial_isl(__isl_take isl_printer * p,__isl_keep isl_qpolynomial * qp)1902 static __isl_give isl_printer *print_qpolynomial_isl(__isl_take isl_printer *p,
1903 	__isl_keep isl_qpolynomial *qp)
1904 {
1905 	struct isl_print_space_data data = { 0 };
1906 
1907 	if (!p || !qp)
1908 		goto error;
1909 
1910 	p = print_param_tuple(p, qp->dim, &data);
1911 	p = isl_printer_print_str(p, "{ ");
1912 	if (!isl_space_is_params(qp->dim)) {
1913 		p = isl_print_space(qp->dim, p, 0, &data);
1914 		p = isl_printer_print_str(p, " -> ");
1915 	}
1916 	p = print_qpolynomial(p, qp);
1917 	p = isl_printer_print_str(p, " }");
1918 	return p;
1919 error:
1920 	isl_printer_free(p);
1921 	return NULL;
1922 }
1923 
1924 /* Print the quasi-polynomial "qp" to "p" in C format, with the variable names
1925  * taken from the domain space "space".
1926  */
print_qpolynomial_c(__isl_take isl_printer * p,__isl_keep isl_space * space,__isl_keep isl_qpolynomial * qp)1927 static __isl_give isl_printer *print_qpolynomial_c(__isl_take isl_printer *p,
1928 	__isl_keep isl_space *space, __isl_keep isl_qpolynomial *qp)
1929 {
1930 	isl_bool is_one;
1931 	isl_val *den;
1932 
1933 	den = isl_qpolynomial_get_den(qp);
1934 	qp = isl_qpolynomial_copy(qp);
1935 	qp = isl_qpolynomial_scale_val(qp, isl_val_copy(den));
1936 	is_one = isl_val_is_one(den);
1937 	if (is_one < 0)
1938 		p = isl_printer_free(p);
1939 	if (!is_one)
1940 		p = isl_printer_print_str(p, "(");
1941 	if (qp)
1942 		p = poly_print(qp->poly, space, qp->div, p);
1943 	else
1944 		p = isl_printer_free(p);
1945 	if (!is_one) {
1946 		p = isl_printer_print_str(p, ")/");
1947 		p = isl_printer_print_val(p, den);
1948 	}
1949 	isl_qpolynomial_free(qp);
1950 	isl_val_free(den);
1951 	return p;
1952 }
1953 
isl_printer_print_qpolynomial(__isl_take isl_printer * p,__isl_keep isl_qpolynomial * qp)1954 __isl_give isl_printer *isl_printer_print_qpolynomial(
1955 	__isl_take isl_printer *p, __isl_keep isl_qpolynomial *qp)
1956 {
1957 	if (!p || !qp)
1958 		goto error;
1959 
1960 	if (p->output_format == ISL_FORMAT_ISL)
1961 		return print_qpolynomial_isl(p, qp);
1962 	else if (p->output_format == ISL_FORMAT_C)
1963 		return print_qpolynomial_c(p, qp->dim, qp);
1964 	else
1965 		isl_die(qp->dim->ctx, isl_error_unsupported,
1966 			"output format not supported for isl_qpolynomials",
1967 			goto error);
1968 error:
1969 	isl_printer_free(p);
1970 	return NULL;
1971 }
1972 
isl_qpolynomial_print(__isl_keep isl_qpolynomial * qp,FILE * out,unsigned output_format)1973 void isl_qpolynomial_print(__isl_keep isl_qpolynomial *qp, FILE *out,
1974 	unsigned output_format)
1975 {
1976 	isl_printer *p;
1977 
1978 	if  (!qp)
1979 		return;
1980 
1981 	isl_assert(qp->dim->ctx, output_format == ISL_FORMAT_ISL, return);
1982 	p = isl_printer_to_file(qp->dim->ctx, out);
1983 	p = isl_printer_print_qpolynomial(p, qp);
1984 	isl_printer_free(p);
1985 }
1986 
qpolynomial_fold_print(__isl_keep isl_qpolynomial_fold * fold,__isl_take isl_printer * p)1987 static __isl_give isl_printer *qpolynomial_fold_print(
1988 	__isl_keep isl_qpolynomial_fold *fold, __isl_take isl_printer *p)
1989 {
1990 	int i;
1991 
1992 	if (fold->type == isl_fold_min)
1993 		p = isl_printer_print_str(p, "min");
1994 	else if (fold->type == isl_fold_max)
1995 		p = isl_printer_print_str(p, "max");
1996 	p = isl_printer_print_str(p, "(");
1997 	for (i = 0; i < fold->n; ++i) {
1998 		if (i)
1999 			p = isl_printer_print_str(p, ", ");
2000 		p = print_qpolynomial(p, fold->qp[i]);
2001 	}
2002 	p = isl_printer_print_str(p, ")");
2003 	return p;
2004 }
2005 
isl_qpolynomial_fold_print(__isl_keep isl_qpolynomial_fold * fold,FILE * out,unsigned output_format)2006 void isl_qpolynomial_fold_print(__isl_keep isl_qpolynomial_fold *fold,
2007 	FILE *out, unsigned output_format)
2008 {
2009 	isl_printer *p;
2010 
2011 	if (!fold)
2012 		return;
2013 
2014 	isl_assert(fold->dim->ctx, output_format == ISL_FORMAT_ISL, return);
2015 
2016 	p = isl_printer_to_file(fold->dim->ctx, out);
2017 	p = isl_printer_print_qpolynomial_fold(p, fold);
2018 
2019 	isl_printer_free(p);
2020 }
2021 
isl_pwqp_print_isl_body(__isl_take isl_printer * p,__isl_keep isl_pw_qpolynomial * pwqp)2022 static __isl_give isl_printer *isl_pwqp_print_isl_body(
2023 	__isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
2024 {
2025 	struct isl_print_space_data data = { 0 };
2026 	int i = 0;
2027 
2028 	for (i = 0; i < pwqp->n; ++i) {
2029 		isl_space *space;
2030 
2031 		if (i)
2032 			p = isl_printer_print_str(p, "; ");
2033 		space = isl_qpolynomial_get_domain_space(pwqp->p[i].qp);
2034 		if (!isl_space_is_params(space)) {
2035 			p = isl_print_space(space, p, 0, &data);
2036 			p = isl_printer_print_str(p, " -> ");
2037 		}
2038 		p = print_qpolynomial(p, pwqp->p[i].qp);
2039 		p = print_disjuncts(set_to_map(pwqp->p[i].set), space, p, 0);
2040 		isl_space_free(space);
2041 	}
2042 
2043 	return p;
2044 }
2045 
print_pw_qpolynomial_isl(__isl_take isl_printer * p,__isl_keep isl_pw_qpolynomial * pwqp)2046 static __isl_give isl_printer *print_pw_qpolynomial_isl(
2047 	__isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
2048 {
2049 	struct isl_print_space_data data = { 0 };
2050 
2051 	if (!p || !pwqp)
2052 		goto error;
2053 
2054 	p = print_param_tuple(p, pwqp->dim, &data);
2055 	p = isl_printer_print_str(p, "{ ");
2056 	if (pwqp->n == 0) {
2057 		if (!isl_space_is_set(pwqp->dim)) {
2058 			p = print_tuple(pwqp->dim, p, isl_dim_in, &data);
2059 			p = isl_printer_print_str(p, " -> ");
2060 		}
2061 		p = isl_printer_print_str(p, "0");
2062 	}
2063 	p = isl_pwqp_print_isl_body(p, pwqp);
2064 	p = isl_printer_print_str(p, " }");
2065 	return p;
2066 error:
2067 	isl_printer_free(p);
2068 	return NULL;
2069 }
2070 
isl_pw_qpolynomial_print(__isl_keep isl_pw_qpolynomial * pwqp,FILE * out,unsigned output_format)2071 void isl_pw_qpolynomial_print(__isl_keep isl_pw_qpolynomial *pwqp, FILE *out,
2072 	unsigned output_format)
2073 {
2074 	isl_printer *p;
2075 
2076 	if (!pwqp)
2077 		return;
2078 
2079 	p = isl_printer_to_file(pwqp->dim->ctx, out);
2080 	p = isl_printer_set_output_format(p, output_format);
2081 	p = isl_printer_print_pw_qpolynomial(p, pwqp);
2082 
2083 	isl_printer_free(p);
2084 }
2085 
isl_pwf_print_isl_body(__isl_take isl_printer * p,__isl_keep isl_pw_qpolynomial_fold * pwf)2086 static __isl_give isl_printer *isl_pwf_print_isl_body(
2087 	__isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
2088 {
2089 	struct isl_print_space_data data = { 0 };
2090 	int i = 0;
2091 
2092 	for (i = 0; i < pwf->n; ++i) {
2093 		isl_space *space;
2094 
2095 		if (i)
2096 			p = isl_printer_print_str(p, "; ");
2097 		space = isl_qpolynomial_fold_get_domain_space(pwf->p[i].fold);
2098 		if (!isl_space_is_params(space)) {
2099 			p = isl_print_space(space, p, 0, &data);
2100 			p = isl_printer_print_str(p, " -> ");
2101 		}
2102 		p = qpolynomial_fold_print(pwf->p[i].fold, p);
2103 		p = print_disjuncts(set_to_map(pwf->p[i].set), space, p, 0);
2104 		isl_space_free(space);
2105 	}
2106 
2107 	return p;
2108 }
2109 
print_pw_qpolynomial_fold_isl(__isl_take isl_printer * p,__isl_keep isl_pw_qpolynomial_fold * pwf)2110 static __isl_give isl_printer *print_pw_qpolynomial_fold_isl(
2111 	__isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
2112 {
2113 	struct isl_print_space_data data = { 0 };
2114 
2115 	p = print_param_tuple(p, pwf->dim, &data);
2116 	p = isl_printer_print_str(p, "{ ");
2117 	if (pwf->n == 0) {
2118 		if (!isl_space_is_set(pwf->dim)) {
2119 			p = print_tuple(pwf->dim, p, isl_dim_in, &data);
2120 			p = isl_printer_print_str(p, " -> ");
2121 		}
2122 		p = isl_printer_print_str(p, "0");
2123 	}
2124 	p = isl_pwf_print_isl_body(p, pwf);
2125 	p = isl_printer_print_str(p, " }");
2126 	return p;
2127 }
2128 
2129 static __isl_give isl_printer *print_ls_affine_c(__isl_take isl_printer *p,
2130 	__isl_keep isl_local_space *ls, isl_int *c);
2131 
2132 /* We skip the constraint if it is implied by the div expression.
2133  *
2134  * *first indicates whether this is the first constraint in the conjunction and
2135  * is updated if the constraint is actually printed.
2136  */
print_constraint_c(__isl_take isl_printer * p,__isl_keep isl_local_space * ls,isl_int * c,const char * op,int * first)2137 static __isl_give isl_printer *print_constraint_c(__isl_take isl_printer *p,
2138 	__isl_keep isl_local_space *ls, isl_int *c, const char *op, int *first)
2139 {
2140 	unsigned o_div;
2141 	isl_size n_div;
2142 	int div;
2143 
2144 	o_div = isl_local_space_offset(ls, isl_dim_div);
2145 	n_div = isl_local_space_dim(ls, isl_dim_div);
2146 	if (n_div < 0)
2147 		return isl_printer_free(p);
2148 	div = isl_seq_last_non_zero(c + o_div, n_div);
2149 	if (div >= 0) {
2150 		isl_bool is_div = isl_local_space_is_div_constraint(ls, c, div);
2151 		if (is_div < 0)
2152 			return isl_printer_free(p);
2153 		if (is_div)
2154 			return p;
2155 	}
2156 
2157 	if (!*first)
2158 		p = isl_printer_print_str(p, " && ");
2159 
2160 	p = print_ls_affine_c(p, ls, c);
2161 	p = isl_printer_print_str(p, " ");
2162 	p = isl_printer_print_str(p, op);
2163 	p = isl_printer_print_str(p, " 0");
2164 
2165 	*first = 0;
2166 
2167 	return p;
2168 }
2169 
2170 static __isl_give isl_printer *print_ls_partial_affine_c(
2171 	__isl_take isl_printer *p, __isl_keep isl_local_space *ls,
2172 	isl_int *c, unsigned len);
2173 
print_basic_set_c(__isl_take isl_printer * p,__isl_keep isl_space * space,__isl_keep isl_basic_set * bset)2174 static __isl_give isl_printer *print_basic_set_c(__isl_take isl_printer *p,
2175 	__isl_keep isl_space *space, __isl_keep isl_basic_set *bset)
2176 {
2177 	int i, j;
2178 	int first = 1;
2179 	isl_size n_div = isl_basic_set_dim(bset, isl_dim_div);
2180 	isl_size total = isl_basic_set_dim(bset, isl_dim_all);
2181 	isl_mat *div;
2182 	isl_local_space *ls;
2183 
2184 	if (n_div < 0 || total < 0)
2185 		return isl_printer_free(p);
2186 
2187 	total -= n_div;
2188 	div = isl_basic_set_get_divs(bset);
2189 	ls = isl_local_space_alloc_div(isl_space_copy(space), div);
2190 	for (i = 0; i < bset->n_eq; ++i) {
2191 		j = isl_seq_last_non_zero(bset->eq[i] + 1 + total, n_div);
2192 		if (j < 0)
2193 			p = print_constraint_c(p, ls,
2194 						bset->eq[i], "==", &first);
2195 		else {
2196 			if (i)
2197 				p = isl_printer_print_str(p, " && ");
2198 			p = isl_printer_print_str(p, "(");
2199 			p = print_ls_partial_affine_c(p, ls, bset->eq[i],
2200 						   1 + total + j);
2201 			p = isl_printer_print_str(p, ") % ");
2202 			p = isl_printer_print_isl_int(p,
2203 						bset->eq[i][1 + total + j]);
2204 			p = isl_printer_print_str(p, " == 0");
2205 			first = 0;
2206 		}
2207 	}
2208 	for (i = 0; i < bset->n_ineq; ++i)
2209 		p = print_constraint_c(p, ls, bset->ineq[i], ">=", &first);
2210 	isl_local_space_free(ls);
2211 	return p;
2212 }
2213 
print_set_c(__isl_take isl_printer * p,__isl_keep isl_space * space,__isl_keep isl_set * set)2214 static __isl_give isl_printer *print_set_c(__isl_take isl_printer *p,
2215 	__isl_keep isl_space *space, __isl_keep isl_set *set)
2216 {
2217 	int i;
2218 
2219 	if (!set)
2220 		return isl_printer_free(p);
2221 
2222 	if (set->n == 0)
2223 		p = isl_printer_print_str(p, "0");
2224 
2225 	for (i = 0; i < set->n; ++i) {
2226 		if (i)
2227 			p = isl_printer_print_str(p, " || ");
2228 		if (set->n > 1)
2229 			p = isl_printer_print_str(p, "(");
2230 		p = print_basic_set_c(p, space, set->p[i]);
2231 		if (set->n > 1)
2232 			p = isl_printer_print_str(p, ")");
2233 	}
2234 	return p;
2235 }
2236 
2237 /* Print the piecewise quasi-polynomial "pwqp" to "p" in C format.
2238  */
print_pw_qpolynomial_c(__isl_take isl_printer * p,__isl_keep isl_pw_qpolynomial * pwqp)2239 static __isl_give isl_printer *print_pw_qpolynomial_c(
2240 	__isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
2241 {
2242 	int i;
2243 	isl_space *space;
2244 
2245 	space = isl_pw_qpolynomial_get_domain_space(pwqp);
2246 	if (pwqp->n == 1 && isl_set_plain_is_universe(pwqp->p[0].set)) {
2247 		p = print_qpolynomial_c(p, space, pwqp->p[0].qp);
2248 		isl_space_free(space);
2249 		return p;
2250 	}
2251 
2252 	for (i = 0; i < pwqp->n; ++i) {
2253 		p = isl_printer_print_str(p, "(");
2254 		p = print_set_c(p, space, pwqp->p[i].set);
2255 		p = isl_printer_print_str(p, ") ? (");
2256 		p = print_qpolynomial_c(p, space, pwqp->p[i].qp);
2257 		p = isl_printer_print_str(p, ") : ");
2258 	}
2259 
2260 	isl_space_free(space);
2261 	p = isl_printer_print_str(p, "0");
2262 	return p;
2263 }
2264 
isl_printer_print_pw_qpolynomial(__isl_take isl_printer * p,__isl_keep isl_pw_qpolynomial * pwqp)2265 __isl_give isl_printer *isl_printer_print_pw_qpolynomial(
2266 	__isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
2267 {
2268 	if (!p || !pwqp)
2269 		goto error;
2270 
2271 	if (p->output_format == ISL_FORMAT_ISL)
2272 		return print_pw_qpolynomial_isl(p, pwqp);
2273 	else if (p->output_format == ISL_FORMAT_C)
2274 		return print_pw_qpolynomial_c(p, pwqp);
2275 	isl_assert(p->ctx, 0, goto error);
2276 error:
2277 	isl_printer_free(p);
2278 	return NULL;
2279 }
2280 
print_pwqp_body(__isl_take isl_pw_qpolynomial * pwqp,void * user)2281 static isl_stat print_pwqp_body(__isl_take isl_pw_qpolynomial *pwqp, void *user)
2282 {
2283 	struct isl_union_print_data *data;
2284 	data = (struct isl_union_print_data *)user;
2285 
2286 	if (!data->first)
2287 		data->p = isl_printer_print_str(data->p, "; ");
2288 	data->first = 0;
2289 
2290 	data->p = isl_pwqp_print_isl_body(data->p, pwqp);
2291 	isl_pw_qpolynomial_free(pwqp);
2292 
2293 	return isl_stat_ok;
2294 }
2295 
print_union_pw_qpolynomial_isl(__isl_take isl_printer * p,__isl_keep isl_union_pw_qpolynomial * upwqp)2296 static __isl_give isl_printer *print_union_pw_qpolynomial_isl(
2297 	__isl_take isl_printer *p, __isl_keep isl_union_pw_qpolynomial *upwqp)
2298 {
2299 	struct isl_union_print_data data;
2300 	struct isl_print_space_data space_data = { 0 };
2301 	isl_space *space;
2302 
2303 	space = isl_union_pw_qpolynomial_get_space(upwqp);
2304 	p = print_param_tuple(p, space, &space_data);
2305 	isl_space_free(space);
2306 	p = isl_printer_print_str(p, "{ ");
2307 	data.p = p;
2308 	data.first = 1;
2309 	isl_union_pw_qpolynomial_foreach_pw_qpolynomial(upwqp, &print_pwqp_body,
2310 							&data);
2311 	p = data.p;
2312 	p = isl_printer_print_str(p, " }");
2313 	return p;
2314 }
2315 
isl_printer_print_union_pw_qpolynomial(__isl_take isl_printer * p,__isl_keep isl_union_pw_qpolynomial * upwqp)2316 __isl_give isl_printer *isl_printer_print_union_pw_qpolynomial(
2317 	__isl_take isl_printer *p, __isl_keep isl_union_pw_qpolynomial *upwqp)
2318 {
2319 	if (!p || !upwqp)
2320 		goto error;
2321 
2322 	if (p->output_format == ISL_FORMAT_ISL)
2323 		return print_union_pw_qpolynomial_isl(p, upwqp);
2324 	isl_die(p->ctx, isl_error_invalid,
2325 		"invalid output format for isl_union_pw_qpolynomial",
2326 		goto error);
2327 error:
2328 	isl_printer_free(p);
2329 	return NULL;
2330 }
2331 
2332 /* Print the quasi-polynomial reduction "fold" to "p" in C format,
2333  * with the variable names taken from the domain space "space".
2334  */
print_qpolynomial_fold_c(__isl_take isl_printer * p,__isl_keep isl_space * space,__isl_keep isl_qpolynomial_fold * fold)2335 static __isl_give isl_printer *print_qpolynomial_fold_c(
2336 	__isl_take isl_printer *p, __isl_keep isl_space *space,
2337 	__isl_keep isl_qpolynomial_fold *fold)
2338 {
2339 	int i;
2340 
2341 	for (i = 0; i < fold->n - 1; ++i)
2342 		if (fold->type == isl_fold_min)
2343 			p = isl_printer_print_str(p, "min(");
2344 		else if (fold->type == isl_fold_max)
2345 			p = isl_printer_print_str(p, "max(");
2346 
2347 	for (i = 0; i < fold->n; ++i) {
2348 		if (i)
2349 			p = isl_printer_print_str(p, ", ");
2350 		p = print_qpolynomial_c(p, space, fold->qp[i]);
2351 		if (i)
2352 			p = isl_printer_print_str(p, ")");
2353 	}
2354 	return p;
2355 }
2356 
isl_printer_print_qpolynomial_fold(__isl_take isl_printer * p,__isl_keep isl_qpolynomial_fold * fold)2357 __isl_give isl_printer *isl_printer_print_qpolynomial_fold(
2358 	__isl_take isl_printer *p, __isl_keep isl_qpolynomial_fold *fold)
2359 {
2360 	if  (!p || !fold)
2361 		goto error;
2362 	if (p->output_format == ISL_FORMAT_ISL)
2363 		return qpolynomial_fold_print(fold, p);
2364 	else if (p->output_format == ISL_FORMAT_C)
2365 		return print_qpolynomial_fold_c(p, fold->dim, fold);
2366 	isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2367 		goto error);
2368 error:
2369 	isl_printer_free(p);
2370 	return NULL;
2371 }
2372 
2373 /* Print the piecewise quasi-polynomial reduction "pwf" to "p" in C format.
2374  */
print_pw_qpolynomial_fold_c(__isl_take isl_printer * p,__isl_keep isl_pw_qpolynomial_fold * pwf)2375 static __isl_give isl_printer *print_pw_qpolynomial_fold_c(
2376 	__isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
2377 {
2378 	int i;
2379 	isl_space *space;
2380 
2381 	space = isl_pw_qpolynomial_fold_get_domain_space(pwf);
2382 	if (pwf->n == 1 && isl_set_plain_is_universe(pwf->p[0].set)) {
2383 		p = print_qpolynomial_fold_c(p, space, pwf->p[0].fold);
2384 		isl_space_free(space);
2385 		return p;
2386 	}
2387 
2388 	for (i = 0; i < pwf->n; ++i) {
2389 		p = isl_printer_print_str(p, "(");
2390 		p = print_set_c(p, space, pwf->p[i].set);
2391 		p = isl_printer_print_str(p, ") ? (");
2392 		p = print_qpolynomial_fold_c(p, space, pwf->p[i].fold);
2393 		p = isl_printer_print_str(p, ") : ");
2394 	}
2395 
2396 	isl_space_free(space);
2397 	p = isl_printer_print_str(p, "0");
2398 	return p;
2399 }
2400 
isl_printer_print_pw_qpolynomial_fold(__isl_take isl_printer * p,__isl_keep isl_pw_qpolynomial_fold * pwf)2401 __isl_give isl_printer *isl_printer_print_pw_qpolynomial_fold(
2402 	__isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
2403 {
2404 	if (!p || !pwf)
2405 		goto error;
2406 
2407 	if (p->output_format == ISL_FORMAT_ISL)
2408 		return print_pw_qpolynomial_fold_isl(p, pwf);
2409 	else if (p->output_format == ISL_FORMAT_C)
2410 		return print_pw_qpolynomial_fold_c(p, pwf);
2411 	isl_assert(p->ctx, 0, goto error);
2412 error:
2413 	isl_printer_free(p);
2414 	return NULL;
2415 }
2416 
isl_pw_qpolynomial_fold_print(__isl_keep isl_pw_qpolynomial_fold * pwf,FILE * out,unsigned output_format)2417 void isl_pw_qpolynomial_fold_print(__isl_keep isl_pw_qpolynomial_fold *pwf,
2418 	FILE *out, unsigned output_format)
2419 {
2420 	isl_printer *p;
2421 
2422 	if (!pwf)
2423 		return;
2424 
2425 	p = isl_printer_to_file(pwf->dim->ctx, out);
2426 	p = isl_printer_set_output_format(p, output_format);
2427 	p = isl_printer_print_pw_qpolynomial_fold(p, pwf);
2428 
2429 	isl_printer_free(p);
2430 }
2431 
print_pwf_body(__isl_take isl_pw_qpolynomial_fold * pwf,void * user)2432 static isl_stat print_pwf_body(__isl_take isl_pw_qpolynomial_fold *pwf,
2433 	void *user)
2434 {
2435 	struct isl_union_print_data *data;
2436 	data = (struct isl_union_print_data *)user;
2437 
2438 	if (!data->first)
2439 		data->p = isl_printer_print_str(data->p, "; ");
2440 	data->first = 0;
2441 
2442 	data->p = isl_pwf_print_isl_body(data->p, pwf);
2443 	isl_pw_qpolynomial_fold_free(pwf);
2444 
2445 	return isl_stat_ok;
2446 }
2447 
print_union_pw_qpolynomial_fold_isl(__isl_take isl_printer * p,__isl_keep isl_union_pw_qpolynomial_fold * upwf)2448 static __isl_give isl_printer *print_union_pw_qpolynomial_fold_isl(
2449 	__isl_take isl_printer *p,
2450 	__isl_keep isl_union_pw_qpolynomial_fold *upwf)
2451 {
2452 	struct isl_union_print_data data;
2453 	struct isl_print_space_data space_data = { 0 };
2454 	isl_space *space;
2455 
2456 	space = isl_union_pw_qpolynomial_fold_get_space(upwf);
2457 	p = print_param_tuple(p, space, &space_data);
2458 	isl_space_free(space);
2459 	p = isl_printer_print_str(p, "{ ");
2460 	data.p = p;
2461 	data.first = 1;
2462 	isl_union_pw_qpolynomial_fold_foreach_pw_qpolynomial_fold(upwf,
2463 							&print_pwf_body, &data);
2464 	p = data.p;
2465 	p = isl_printer_print_str(p, " }");
2466 	return p;
2467 }
2468 
isl_printer_print_union_pw_qpolynomial_fold(__isl_take isl_printer * p,__isl_keep isl_union_pw_qpolynomial_fold * upwf)2469 __isl_give isl_printer *isl_printer_print_union_pw_qpolynomial_fold(
2470 	__isl_take isl_printer *p,
2471 	__isl_keep isl_union_pw_qpolynomial_fold *upwf)
2472 {
2473 	if (!p || !upwf)
2474 		goto error;
2475 
2476 	if (p->output_format == ISL_FORMAT_ISL)
2477 		return print_union_pw_qpolynomial_fold_isl(p, upwf);
2478 	isl_die(p->ctx, isl_error_invalid,
2479 		"invalid output format for isl_union_pw_qpolynomial_fold",
2480 		goto error);
2481 error:
2482 	isl_printer_free(p);
2483 	return NULL;
2484 }
2485 
2486 /* Print the isl_constraint "c" to "p".
2487  */
isl_printer_print_constraint(__isl_take isl_printer * p,__isl_keep isl_constraint * c)2488 __isl_give isl_printer *isl_printer_print_constraint(__isl_take isl_printer *p,
2489 	__isl_keep isl_constraint *c)
2490 {
2491 	struct isl_print_space_data data = { 0 };
2492 	isl_local_space *ls;
2493 	isl_space *space;
2494 	isl_bool exists;
2495 
2496 	if (!p || !c)
2497 		goto error;
2498 
2499 	ls = isl_constraint_get_local_space(c);
2500 	if (!ls)
2501 		return isl_printer_free(p);
2502 	space = isl_local_space_get_space(ls);
2503 	p = print_param_tuple(p, space, &data);
2504 	p = isl_printer_print_str(p, "{ ");
2505 	p = isl_print_space(space, p, 0, &data);
2506 	p = isl_printer_print_str(p, " : ");
2507 	exists = need_exists(p, ls->div);
2508 	if (exists < 0)
2509 		p = isl_printer_free(p);
2510 	if (exists >= 0 && exists)
2511 		p = open_exists(p, space, ls->div, 0);
2512 	p = print_affine_of_len(space, ls->div, p, c->v->el, c->v->size);
2513 	if (isl_constraint_is_equality(c))
2514 		p = isl_printer_print_str(p, " = 0");
2515 	else
2516 		p = isl_printer_print_str(p, " >= 0");
2517 	if (exists >= 0 && exists)
2518 		p = isl_printer_print_str(p, s_close_exists[0]);
2519 	p = isl_printer_print_str(p, " }");
2520 	isl_space_free(space);
2521 	isl_local_space_free(ls);
2522 
2523 	return p;
2524 error:
2525 	isl_printer_free(p);
2526 	return NULL;
2527 }
2528 
isl_printer_print_space_isl(__isl_take isl_printer * p,__isl_keep isl_space * space)2529 static __isl_give isl_printer *isl_printer_print_space_isl(
2530 	__isl_take isl_printer *p, __isl_keep isl_space *space)
2531 {
2532 	struct isl_print_space_data data = { 0 };
2533 
2534 	if (!space)
2535 		goto error;
2536 
2537 	p = print_param_tuple(p, space, &data);
2538 
2539 	p = isl_printer_print_str(p, "{ ");
2540 	if (isl_space_is_params(space))
2541 		p = isl_printer_print_str(p, s_such_that[0]);
2542 	else
2543 		p = isl_print_space(space, p, 0, &data);
2544 	p = isl_printer_print_str(p, " }");
2545 
2546 	return p;
2547 error:
2548 	isl_printer_free(p);
2549 	return NULL;
2550 }
2551 
isl_printer_print_space(__isl_take isl_printer * p,__isl_keep isl_space * space)2552 __isl_give isl_printer *isl_printer_print_space(__isl_take isl_printer *p,
2553 	__isl_keep isl_space *space)
2554 {
2555 	if (!p || !space)
2556 		return isl_printer_free(p);
2557 	if (p->output_format == ISL_FORMAT_ISL)
2558 		return isl_printer_print_space_isl(p, space);
2559 	else if (p->output_format == ISL_FORMAT_OMEGA)
2560 		return print_omega_parameters(space, p);
2561 
2562 	isl_die(isl_space_get_ctx(space), isl_error_unsupported,
2563 		"output format not supported for space",
2564 		return isl_printer_free(p));
2565 }
2566 
isl_printer_print_local_space(__isl_take isl_printer * p,__isl_keep isl_local_space * ls)2567 __isl_give isl_printer *isl_printer_print_local_space(__isl_take isl_printer *p,
2568 	__isl_keep isl_local_space *ls)
2569 {
2570 	struct isl_print_space_data data = { 0 };
2571 	isl_size n_div;
2572 
2573 	n_div = isl_local_space_dim(ls, isl_dim_div);
2574 	if (n_div < 0)
2575 		goto error;
2576 
2577 	p = print_param_tuple(p, ls->dim, &data);
2578 	p = isl_printer_print_str(p, "{ ");
2579 	p = isl_print_space(ls->dim, p, 0, &data);
2580 	if (n_div > 0) {
2581 		p = isl_printer_print_str(p, " : ");
2582 		p = isl_printer_print_str(p, s_open_exists[0]);
2583 		p = print_div_list(p, ls->dim, ls->div, 0, 1);
2584 		p = isl_printer_print_str(p, s_close_exists[0]);
2585 	} else if (isl_space_is_params(ls->dim))
2586 		p = isl_printer_print_str(p, s_such_that[0]);
2587 	p = isl_printer_print_str(p, " }");
2588 	return p;
2589 error:
2590 	isl_printer_free(p);
2591 	return NULL;
2592 }
2593 
2594 /* Look for the last of the "n" integer divisions that is used in "aff" and
2595  * that can be printed as a modulo and
2596  * return the position of this integer division.
2597  * Return "n" if no such integer division can be found.
2598  * Return isl_size_error on error.
2599  *
2600  * In particular, look for an integer division that appears in "aff"
2601  * with a coefficient that is a multiple of the denominator
2602  * of the integer division.
2603  * That is, check if the numerator of "aff" is of the form
2604  *
2605  *	f(...) + a m floor(g/m)
2606  *
2607  * and return the position of "floor(g/m)".
2608  *
2609  * Note that, unlike print_as_modulo_pos, no check needs to be made
2610  * for whether the integer division can be printed, since it will
2611  * need to be printed as an integer division anyway if it is not printed
2612  * as a modulo.
2613  */
last_modulo(__isl_keep isl_printer * p,__isl_keep isl_aff * aff,unsigned n)2614 static isl_size last_modulo(__isl_keep isl_printer *p, __isl_keep isl_aff *aff,
2615 	unsigned n)
2616 {
2617 	isl_size o_div;
2618 	int i;
2619 
2620 	if (n == 0)
2621 		return n;
2622 	o_div = isl_aff_domain_offset(aff, isl_dim_div);
2623 	if (o_div < 0)
2624 		return isl_size_error;
2625 	for (i = n - 1; i >= 0; --i) {
2626 		if (isl_int_is_zero(aff->v->el[1 + o_div + i]))
2627 			continue;
2628 		if (isl_int_is_divisible_by(aff->v->el[1 + o_div + i],
2629 					    aff->ls->div->row[i][0]))
2630 			return i;
2631 	}
2632 
2633 	return n;
2634 }
2635 
2636 /* Print the numerator of the affine expression "aff" to "p",
2637  * with the variable names taken from "space".
2638  */
print_aff_num_base(__isl_take isl_printer * p,__isl_keep isl_space * space,__isl_keep isl_aff * aff)2639 static __isl_give isl_printer *print_aff_num_base(__isl_take isl_printer *p,
2640 	__isl_keep isl_space *space, __isl_keep isl_aff *aff)
2641 {
2642 	isl_size total;
2643 
2644 	total = isl_aff_domain_dim(aff, isl_dim_all);
2645 	if (total < 0)
2646 		return isl_printer_free(p);
2647 	p = print_affine_of_len(space, aff->ls->div, p,
2648 				aff->v->el + 1, 1 + total);
2649 
2650 	return p;
2651 }
2652 
2653 static __isl_give isl_printer *print_aff_num(__isl_take isl_printer *p,
2654 	__isl_keep isl_space *space, __isl_keep isl_aff *aff);
2655 
2656 /* Print the modulo term "c" * ("aff" mod "mod") to "p",
2657  * with the variable names taken from "space".
2658  * If "first" is set, then this is the first term of an expression.
2659  */
print_mod_term(__isl_take isl_printer * p,__isl_keep isl_space * space,__isl_keep isl_aff * aff,int first,__isl_take isl_val * c,__isl_keep isl_val * mod)2660 static __isl_give isl_printer *print_mod_term(__isl_take isl_printer *p,
2661 	__isl_keep isl_space *space, __isl_keep isl_aff *aff, int first,
2662 	__isl_take isl_val *c, __isl_keep isl_val *mod)
2663 {
2664 	isl_bool is_one, is_neg;
2665 
2666 	is_neg = isl_val_is_neg(c);
2667 	if (is_neg < 0)
2668 		p = isl_printer_free(p);
2669 	if (!first) {
2670 		if (is_neg)
2671 			c = isl_val_neg(c);
2672 		p = isl_printer_print_str(p, is_neg ? " - " : " + ");
2673 	}
2674 	is_one = isl_val_is_one(c);
2675 	if (is_one < 0)
2676 		p = isl_printer_free(p);
2677 	if (!is_one) {
2678 		p = isl_printer_print_val(p, c);
2679 		p = isl_printer_print_str(p, "*(");
2680 	}
2681 	p = isl_printer_print_str(p, "(");
2682 	p = print_aff_num(p, space, aff);
2683 	p = isl_printer_print_str(p, ")");
2684 	p = isl_printer_print_str(p, " mod ");
2685 	p = isl_printer_print_val(p, mod);
2686 	if (!is_one)
2687 		p = isl_printer_print_str(p, ")");
2688 
2689 	isl_val_free(c);
2690 
2691 	return p;
2692 }
2693 
2694 /* Print the numerator of the affine expression "aff" to "p",
2695  * with the variable names taken from "space",
2696  * given that the numerator of "aff" is of the form
2697  *
2698  *	f(...) + a m floor(g/m)
2699  *
2700  * with "floor(g/m)" the integer division at position "last".
2701  *
2702  * First replace "aff" by its numerator and rewrite it as
2703  *
2704  *	f(...) + a g - a (g mod m)
2705  *
2706  * Recursively write out (the numerator of) "f(...) + a g"
2707  * (which may involve other modulo expressions) and
2708  * then write out "- a (g mod m)".
2709  */
print_aff_num_mod(__isl_take isl_printer * p,__isl_keep isl_space * space,__isl_keep isl_aff * aff,unsigned last)2710 static __isl_give isl_printer *print_aff_num_mod(__isl_take isl_printer *p,
2711 	__isl_keep isl_space *space, __isl_keep isl_aff *aff, unsigned last)
2712 {
2713 	isl_bool is_zero;
2714 	isl_val *a, *m;
2715 	isl_aff *div, *term;
2716 
2717 	aff = isl_aff_copy(aff);
2718 	aff = isl_aff_scale_val(aff, isl_aff_get_denominator_val(aff));
2719 	a = isl_aff_get_coefficient_val(aff, isl_dim_div, last);
2720 	aff = isl_aff_set_coefficient_si(aff, isl_dim_div, last, 0);
2721 	div = isl_aff_get_div(aff, last);
2722 	m = isl_aff_get_denominator_val(div);
2723 	a = isl_val_div(a, isl_val_copy(m));
2724 	div = isl_aff_scale_val(div, isl_val_copy(m));
2725 	term = isl_aff_scale_val(isl_aff_copy(div), isl_val_copy(a));
2726 	aff = isl_aff_add(aff, term);
2727 
2728 	is_zero = isl_aff_plain_is_zero(aff);
2729 	if (is_zero < 0) {
2730 		p = isl_printer_free(p);
2731 	} else {
2732 		if (!is_zero)
2733 			p = print_aff_num(p, space, aff);
2734 		a = isl_val_neg(a);
2735 		p = print_mod_term(p, space, div, is_zero, isl_val_copy(a), m);
2736 	}
2737 
2738 	isl_val_free(a);
2739 	isl_val_free(m);
2740 	isl_aff_free(aff);
2741 	isl_aff_free(div);
2742 
2743 	return p;
2744 }
2745 
2746 /* Print the numerator of the affine expression "aff" to "p",
2747  * with the variable names taken from "space",
2748  * separating out any (obvious) modulo expressions.
2749  *
2750  * In particular, look for modulo expressions in "aff",
2751  * separating them out if found and simply printing out "aff" otherwise.
2752  */
print_aff_num(__isl_take isl_printer * p,__isl_keep isl_space * space,__isl_keep isl_aff * aff)2753 static __isl_give isl_printer *print_aff_num(__isl_take isl_printer *p,
2754 	__isl_keep isl_space *space, __isl_keep isl_aff *aff)
2755 {
2756 	isl_size n_div, mod;
2757 
2758 	n_div = isl_aff_dim(aff, isl_dim_div);
2759 	if (n_div < 0)
2760 		return isl_printer_free(p);
2761 	mod = last_modulo(p, aff, n_div);
2762 	if (mod < 0)
2763 		return isl_printer_free(p);
2764 	if (mod < n_div)
2765 		return print_aff_num_mod(p, space, aff, mod);
2766 	else
2767 		return print_aff_num_base(p, space, aff);
2768 }
2769 
2770 /* Print the (potentially rational) affine expression "aff" to "p",
2771  * with the variable names taken from "space".
2772  */
print_aff_body(__isl_take isl_printer * p,__isl_keep isl_space * space,__isl_keep isl_aff * aff)2773 static __isl_give isl_printer *print_aff_body(__isl_take isl_printer *p,
2774 	__isl_keep isl_space *space, __isl_keep isl_aff *aff)
2775 {
2776 	if (isl_aff_is_nan(aff))
2777 		return isl_printer_print_str(p, "NaN");
2778 
2779 	p = isl_printer_print_str(p, "(");
2780 	p = print_aff_num(p, space, aff);
2781 	if (isl_int_is_one(aff->v->el[0]))
2782 		p = isl_printer_print_str(p, ")");
2783 	else {
2784 		p = isl_printer_print_str(p, ")/");
2785 		p = isl_printer_print_isl_int(p, aff->v->el[0]);
2786 	}
2787 
2788 	return p;
2789 }
2790 
print_aff(__isl_take isl_printer * p,__isl_keep isl_aff * aff)2791 static __isl_give isl_printer *print_aff(__isl_take isl_printer *p,
2792 	__isl_keep isl_aff *aff)
2793 {
2794 	struct isl_print_space_data data = { 0 };
2795 
2796 	if (isl_space_is_params(aff->ls->dim))
2797 		;
2798 	else {
2799 		p = print_tuple(aff->ls->dim, p, isl_dim_set, &data);
2800 		p = isl_printer_print_str(p, " -> ");
2801 	}
2802 	p = isl_printer_print_str(p, "[");
2803 	p = print_aff_body(p, aff->ls->dim, aff);
2804 	p = isl_printer_print_str(p, "]");
2805 
2806 	return p;
2807 }
2808 
print_aff_isl(__isl_take isl_printer * p,__isl_keep isl_aff * aff)2809 static __isl_give isl_printer *print_aff_isl(__isl_take isl_printer *p,
2810 	__isl_keep isl_aff *aff)
2811 {
2812 	struct isl_print_space_data data = { 0 };
2813 
2814 	if (!aff)
2815 		goto error;
2816 
2817 	p = print_param_tuple(p, aff->ls->dim, &data);
2818 	p = isl_printer_print_str(p, "{ ");
2819 	p = print_aff(p, aff);
2820 	p = isl_printer_print_str(p, " }");
2821 	return p;
2822 error:
2823 	isl_printer_free(p);
2824 	return NULL;
2825 }
2826 
2827 /* Print the body of an isl_pw_aff, i.e., a semicolon delimited
2828  * sequence of affine expressions, each followed by constraints.
2829  */
print_pw_aff_body(__isl_take isl_printer * p,__isl_keep isl_pw_aff * pa)2830 static __isl_give isl_printer *print_pw_aff_body(
2831 	__isl_take isl_printer *p, __isl_keep isl_pw_aff *pa)
2832 {
2833 	int i;
2834 
2835 	if (!pa)
2836 		return isl_printer_free(p);
2837 
2838 	for (i = 0; i < pa->n; ++i) {
2839 		isl_space *space;
2840 
2841 		if (i)
2842 			p = isl_printer_print_str(p, "; ");
2843 		p = print_aff(p, pa->p[i].aff);
2844 		space = isl_aff_get_domain_space(pa->p[i].aff);
2845 		p = print_disjuncts(set_to_map(pa->p[i].set), space, p, 0);
2846 		isl_space_free(space);
2847 	}
2848 
2849 	return p;
2850 }
2851 
print_pw_aff_isl(__isl_take isl_printer * p,__isl_keep isl_pw_aff * pwaff)2852 static __isl_give isl_printer *print_pw_aff_isl(__isl_take isl_printer *p,
2853 	__isl_keep isl_pw_aff *pwaff)
2854 {
2855 	struct isl_print_space_data data = { 0 };
2856 
2857 	if (!pwaff)
2858 		goto error;
2859 
2860 	p = print_param_tuple(p, pwaff->dim, &data);
2861 	p = isl_printer_print_str(p, "{ ");
2862 	p = print_pw_aff_body(p, pwaff);
2863 	p = isl_printer_print_str(p, " }");
2864 	return p;
2865 error:
2866 	isl_printer_free(p);
2867 	return NULL;
2868 }
2869 
print_ls_name_c(__isl_take isl_printer * p,__isl_keep isl_local_space * ls,enum isl_dim_type type,unsigned pos)2870 static __isl_give isl_printer *print_ls_name_c(__isl_take isl_printer *p,
2871 	__isl_keep isl_local_space *ls, enum isl_dim_type type, unsigned pos)
2872 {
2873 	if (type == isl_dim_div) {
2874 		p = isl_printer_print_str(p, "floord(");
2875 		p = print_ls_affine_c(p, ls, ls->div->row[pos] + 1);
2876 		p = isl_printer_print_str(p, ", ");
2877 		p = isl_printer_print_isl_int(p, ls->div->row[pos][0]);
2878 		p = isl_printer_print_str(p, ")");
2879 	} else {
2880 		const char *name;
2881 
2882 		name = isl_space_get_dim_name(ls->dim, type, pos);
2883 		if (!name)
2884 			name = "UNNAMED";
2885 		p = isl_printer_print_str(p, name);
2886 	}
2887 	return p;
2888 }
2889 
print_ls_term_c(__isl_take isl_printer * p,__isl_keep isl_local_space * ls,isl_int c,unsigned pos)2890 static __isl_give isl_printer *print_ls_term_c(__isl_take isl_printer *p,
2891 	__isl_keep isl_local_space *ls, isl_int c, unsigned pos)
2892 {
2893 	enum isl_dim_type type;
2894 
2895 	if (!p || !ls)
2896 		return isl_printer_free(p);
2897 
2898 	if (pos == 0)
2899 		return isl_printer_print_isl_int(p, c);
2900 
2901 	if (isl_int_is_one(c))
2902 		;
2903 	else if (isl_int_is_negone(c))
2904 		p = isl_printer_print_str(p, "-");
2905 	else {
2906 		p = isl_printer_print_isl_int(p, c);
2907 		p = isl_printer_print_str(p, "*");
2908 	}
2909 	if (pos2type(ls->dim, &type, &pos) < 0)
2910 		return isl_printer_free(p);
2911 	p = print_ls_name_c(p, ls, type, pos);
2912 	return p;
2913 }
2914 
print_ls_partial_affine_c(__isl_take isl_printer * p,__isl_keep isl_local_space * ls,isl_int * c,unsigned len)2915 static __isl_give isl_printer *print_ls_partial_affine_c(
2916 	__isl_take isl_printer *p, __isl_keep isl_local_space *ls,
2917 	isl_int *c, unsigned len)
2918 {
2919 	int i;
2920 	int first;
2921 
2922 	for (i = 0, first = 1; i < len; ++i) {
2923 		int flip = 0;
2924 		if (isl_int_is_zero(c[i]))
2925 			continue;
2926 		if (!first) {
2927 			if (isl_int_is_neg(c[i])) {
2928 				flip = 1;
2929 				isl_int_neg(c[i], c[i]);
2930 				p = isl_printer_print_str(p, " - ");
2931 			} else
2932 				p = isl_printer_print_str(p, " + ");
2933 		}
2934 		first = 0;
2935 		p = print_ls_term_c(p, ls, c[i], i);
2936 		if (flip)
2937 			isl_int_neg(c[i], c[i]);
2938 	}
2939 	if (first)
2940 		p = isl_printer_print_str(p, "0");
2941 	return p;
2942 }
2943 
print_ls_affine_c(__isl_take isl_printer * p,__isl_keep isl_local_space * ls,isl_int * c)2944 static __isl_give isl_printer *print_ls_affine_c(__isl_take isl_printer *p,
2945 	__isl_keep isl_local_space *ls, isl_int *c)
2946 {
2947 	isl_size total = isl_local_space_dim(ls, isl_dim_all);
2948 
2949 	if (total < 0)
2950 		return isl_printer_free(p);
2951 	return print_ls_partial_affine_c(p, ls, c, 1 + total);
2952 }
2953 
print_aff_c(__isl_take isl_printer * p,__isl_keep isl_aff * aff)2954 static __isl_give isl_printer *print_aff_c(__isl_take isl_printer *p,
2955 	__isl_keep isl_aff *aff)
2956 {
2957 	isl_size total;
2958 
2959 	total = isl_aff_domain_dim(aff, isl_dim_all);
2960 	if (total < 0)
2961 		return isl_printer_free(p);
2962 	if (!isl_int_is_one(aff->v->el[0]))
2963 		p = isl_printer_print_str(p, "(");
2964 	p = print_ls_partial_affine_c(p, aff->ls, aff->v->el + 1, 1 + total);
2965 	if (!isl_int_is_one(aff->v->el[0])) {
2966 		p = isl_printer_print_str(p, ")/");
2967 		p = isl_printer_print_isl_int(p, aff->v->el[0]);
2968 	}
2969 	return p;
2970 }
2971 
2972 /* In the C format, we cannot express that "pwaff" may be undefined
2973  * on parts of the domain space.  We therefore assume that the expression
2974  * will only be evaluated on its definition domain and compute the gist
2975  * of each cell with respect to this domain.
2976  */
print_pw_aff_c(__isl_take isl_printer * p,__isl_keep isl_pw_aff * pwaff)2977 static __isl_give isl_printer *print_pw_aff_c(__isl_take isl_printer *p,
2978 	__isl_keep isl_pw_aff *pwaff)
2979 {
2980 	isl_set *domain;
2981 	isl_ast_build *build;
2982 	isl_ast_expr *expr;
2983 
2984 	if (pwaff->n < 1)
2985 		isl_die(p->ctx, isl_error_unsupported,
2986 			"cannot print empty isl_pw_aff in C format",
2987 			return isl_printer_free(p));
2988 
2989 	domain = isl_pw_aff_domain(isl_pw_aff_copy(pwaff));
2990 	build = isl_ast_build_from_context(domain);
2991 	expr = isl_ast_build_expr_from_pw_aff(build, isl_pw_aff_copy(pwaff));
2992 	p = isl_printer_print_ast_expr(p, expr);
2993 	isl_ast_expr_free(expr);
2994 	isl_ast_build_free(build);
2995 
2996 	return p;
2997 }
2998 
isl_printer_print_aff(__isl_take isl_printer * p,__isl_keep isl_aff * aff)2999 __isl_give isl_printer *isl_printer_print_aff(__isl_take isl_printer *p,
3000 	__isl_keep isl_aff *aff)
3001 {
3002 	if (!p || !aff)
3003 		goto error;
3004 
3005 	if (p->output_format == ISL_FORMAT_ISL)
3006 		return print_aff_isl(p, aff);
3007 	else if (p->output_format == ISL_FORMAT_C)
3008 		return print_aff_c(p, aff);
3009 	isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
3010 		goto error);
3011 error:
3012 	isl_printer_free(p);
3013 	return NULL;
3014 }
3015 
isl_printer_print_pw_aff(__isl_take isl_printer * p,__isl_keep isl_pw_aff * pwaff)3016 __isl_give isl_printer *isl_printer_print_pw_aff(__isl_take isl_printer *p,
3017 	__isl_keep isl_pw_aff *pwaff)
3018 {
3019 	if (!p || !pwaff)
3020 		goto error;
3021 
3022 	if (p->output_format == ISL_FORMAT_ISL)
3023 		return print_pw_aff_isl(p, pwaff);
3024 	else if (p->output_format == ISL_FORMAT_C)
3025 		return print_pw_aff_c(p, pwaff);
3026 	isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
3027 		goto error);
3028 error:
3029 	isl_printer_free(p);
3030 	return NULL;
3031 }
3032 
3033 /* Print "pa" in a sequence of isl_pw_affs delimited by semicolons.
3034  * Each isl_pw_aff itself is also printed as semicolon delimited
3035  * sequence of pieces.
3036  * If data->first = 1, then this is the first in the sequence.
3037  * Update data->first to tell the next element that it is not the first.
3038  */
print_pw_aff_body_wrap(__isl_take isl_pw_aff * pa,void * user)3039 static isl_stat print_pw_aff_body_wrap(__isl_take isl_pw_aff *pa,
3040 	void *user)
3041 {
3042 	struct isl_union_print_data *data;
3043 	data = (struct isl_union_print_data *) user;
3044 
3045 	if (!data->first)
3046 		data->p = isl_printer_print_str(data->p, "; ");
3047 	data->first = 0;
3048 
3049 	data->p = print_pw_aff_body(data->p, pa);
3050 	isl_pw_aff_free(pa);
3051 
3052 	return data->p ? isl_stat_ok : isl_stat_error;
3053 }
3054 
3055 /* Print the body of an isl_union_pw_aff, i.e., a semicolon delimited
3056  * sequence of affine expressions, each followed by constraints,
3057  * with the sequence enclosed in braces.
3058  */
print_union_pw_aff_body(__isl_take isl_printer * p,__isl_keep isl_union_pw_aff * upa)3059 static __isl_give isl_printer *print_union_pw_aff_body(
3060 	__isl_take isl_printer *p, __isl_keep isl_union_pw_aff *upa)
3061 {
3062 	struct isl_union_print_data data = { p, 1 };
3063 
3064 	p = isl_printer_print_str(p, s_open_set[0]);
3065 	data.p = p;
3066 	if (isl_union_pw_aff_foreach_pw_aff(upa,
3067 					    &print_pw_aff_body_wrap, &data) < 0)
3068 		data.p = isl_printer_free(data.p);
3069 	p = data.p;
3070 	p = isl_printer_print_str(p, s_close_set[0]);
3071 
3072 	return p;
3073 }
3074 
3075 /* Print the isl_union_pw_aff "upa" to "p" in isl format.
3076  *
3077  * The individual isl_pw_affs are delimited by a semicolon.
3078  */
print_union_pw_aff_isl(__isl_take isl_printer * p,__isl_keep isl_union_pw_aff * upa)3079 static __isl_give isl_printer *print_union_pw_aff_isl(
3080 	__isl_take isl_printer *p, __isl_keep isl_union_pw_aff *upa)
3081 {
3082 	struct isl_print_space_data data = { 0 };
3083 	isl_space *space;
3084 
3085 	space = isl_union_pw_aff_get_space(upa);
3086 	p = print_param_tuple(p, space, &data);
3087 	isl_space_free(space);
3088 	p = print_union_pw_aff_body(p, upa);
3089 	return p;
3090 }
3091 
3092 /* Print the isl_union_pw_aff "upa" to "p".
3093  *
3094  * We currently only support an isl format.
3095  */
isl_printer_print_union_pw_aff(__isl_take isl_printer * p,__isl_keep isl_union_pw_aff * upa)3096 __isl_give isl_printer *isl_printer_print_union_pw_aff(
3097 	__isl_take isl_printer *p, __isl_keep isl_union_pw_aff *upa)
3098 {
3099 	if (!p || !upa)
3100 		return isl_printer_free(p);
3101 
3102 	if (p->output_format == ISL_FORMAT_ISL)
3103 		return print_union_pw_aff_isl(p, upa);
3104 	isl_die(isl_printer_get_ctx(p), isl_error_unsupported,
3105 		"unsupported output format", return isl_printer_free(p));
3106 }
3107 
3108 /* Print dimension "pos" of data->space to "p".
3109  *
3110  * data->user is assumed to be an isl_multi_aff.
3111  *
3112  * If the current dimension is an output dimension, then print
3113  * the corresponding expression.  Otherwise, print the name of the dimension.
3114  * Make sure to use the domain space for printing names as
3115  * that is the space that will be used for printing constraints (if any).
3116  */
print_dim_ma(__isl_take isl_printer * p,struct isl_print_space_data * data,unsigned pos)3117 static __isl_give isl_printer *print_dim_ma(__isl_take isl_printer *p,
3118 	struct isl_print_space_data *data, unsigned pos)
3119 {
3120 	isl_multi_aff *ma = data->user;
3121 	isl_space *space;
3122 
3123 	space = isl_multi_aff_get_domain_space(ma);
3124 	if (data->type == isl_dim_out) {
3125 		p = print_aff_body(p, space, ma->u.p[pos]);
3126 	} else {
3127 		enum isl_dim_type type = data->type;
3128 
3129 		if (type == isl_dim_in)
3130 			type = isl_dim_set;
3131 		p = print_name(space, p, type, pos, data->latex);
3132 	}
3133 	isl_space_free(space);
3134 
3135 	return p;
3136 }
3137 
print_multi_aff(__isl_take isl_printer * p,__isl_keep isl_multi_aff * maff)3138 static __isl_give isl_printer *print_multi_aff(__isl_take isl_printer *p,
3139 	__isl_keep isl_multi_aff *maff)
3140 {
3141 	struct isl_print_space_data data = { 0 };
3142 
3143 	data.print_dim = &print_dim_ma;
3144 	data.user = maff;
3145 	return isl_print_space(maff->space, p, 0, &data);
3146 }
3147 
print_multi_aff_isl(__isl_take isl_printer * p,__isl_keep isl_multi_aff * maff)3148 static __isl_give isl_printer *print_multi_aff_isl(__isl_take isl_printer *p,
3149 	__isl_keep isl_multi_aff *maff)
3150 {
3151 	struct isl_print_space_data data = { 0 };
3152 
3153 	if (!maff)
3154 		goto error;
3155 
3156 	p = print_param_tuple(p, maff->space, &data);
3157 	p = isl_printer_print_str(p, "{ ");
3158 	p = print_multi_aff(p, maff);
3159 	p = isl_printer_print_str(p, " }");
3160 	return p;
3161 error:
3162 	isl_printer_free(p);
3163 	return NULL;
3164 }
3165 
isl_printer_print_multi_aff(__isl_take isl_printer * p,__isl_keep isl_multi_aff * maff)3166 __isl_give isl_printer *isl_printer_print_multi_aff(__isl_take isl_printer *p,
3167 	__isl_keep isl_multi_aff *maff)
3168 {
3169 	if (!p || !maff)
3170 		goto error;
3171 
3172 	if (p->output_format == ISL_FORMAT_ISL)
3173 		return print_multi_aff_isl(p, maff);
3174 	isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
3175 		goto error);
3176 error:
3177 	isl_printer_free(p);
3178 	return NULL;
3179 }
3180 
print_pw_multi_aff_body(__isl_take isl_printer * p,__isl_keep isl_pw_multi_aff * pma)3181 static __isl_give isl_printer *print_pw_multi_aff_body(
3182 	__isl_take isl_printer *p, __isl_keep isl_pw_multi_aff *pma)
3183 {
3184 	int i;
3185 
3186 	if (!pma)
3187 		goto error;
3188 
3189 	for (i = 0; i < pma->n; ++i) {
3190 		isl_space *space;
3191 
3192 		if (i)
3193 			p = isl_printer_print_str(p, "; ");
3194 		p = print_multi_aff(p, pma->p[i].maff);
3195 		space = isl_multi_aff_get_domain_space(pma->p[i].maff);
3196 		p = print_disjuncts(set_to_map(pma->p[i].set), space, p, 0);
3197 		isl_space_free(space);
3198 	}
3199 	return p;
3200 error:
3201 	isl_printer_free(p);
3202 	return NULL;
3203 }
3204 
print_pw_multi_aff_isl(__isl_take isl_printer * p,__isl_keep isl_pw_multi_aff * pma)3205 static __isl_give isl_printer *print_pw_multi_aff_isl(__isl_take isl_printer *p,
3206 	__isl_keep isl_pw_multi_aff *pma)
3207 {
3208 	struct isl_print_space_data data = { 0 };
3209 
3210 	if (!pma)
3211 		goto error;
3212 
3213 	p = print_param_tuple(p, pma->dim, &data);
3214 	p = isl_printer_print_str(p, "{ ");
3215 	p = print_pw_multi_aff_body(p, pma);
3216 	p = isl_printer_print_str(p, " }");
3217 	return p;
3218 error:
3219 	isl_printer_free(p);
3220 	return NULL;
3221 }
3222 
3223 /* Print the unnamed, single-dimensional piecewise multi affine expression "pma"
3224  * to "p".
3225  */
print_unnamed_pw_multi_aff_c(__isl_take isl_printer * p,__isl_keep isl_pw_multi_aff * pma)3226 static __isl_give isl_printer *print_unnamed_pw_multi_aff_c(
3227 	__isl_take isl_printer *p, __isl_keep isl_pw_multi_aff *pma)
3228 {
3229 	int i;
3230 	isl_space *space;
3231 
3232 	space = isl_pw_multi_aff_get_domain_space(pma);
3233 	for (i = 0; i < pma->n - 1; ++i) {
3234 		p = isl_printer_print_str(p, "(");
3235 		p = print_set_c(p, space, pma->p[i].set);
3236 		p = isl_printer_print_str(p, ") ? (");
3237 		p = print_aff_c(p, pma->p[i].maff->u.p[0]);
3238 		p = isl_printer_print_str(p, ") : ");
3239 	}
3240 	isl_space_free(space);
3241 
3242 	return print_aff_c(p, pma->p[pma->n - 1].maff->u.p[0]);
3243 }
3244 
print_pw_multi_aff_c(__isl_take isl_printer * p,__isl_keep isl_pw_multi_aff * pma)3245 static __isl_give isl_printer *print_pw_multi_aff_c(__isl_take isl_printer *p,
3246 	__isl_keep isl_pw_multi_aff *pma)
3247 {
3248 	isl_size n;
3249 	const char *name;
3250 
3251 	if (!pma)
3252 		goto error;
3253 	if (pma->n < 1)
3254 		isl_die(p->ctx, isl_error_unsupported,
3255 			"cannot print empty isl_pw_multi_aff in C format",
3256 			goto error);
3257 	n = isl_pw_multi_aff_dim(pma, isl_dim_out);
3258 	if (n < 0)
3259 		return isl_printer_free(p);
3260 	name = isl_pw_multi_aff_get_tuple_name(pma, isl_dim_out);
3261 	if (!name && n == 1)
3262 		return print_unnamed_pw_multi_aff_c(p, pma);
3263 	if (!name)
3264 		isl_die(p->ctx, isl_error_unsupported,
3265 			"cannot print unnamed isl_pw_multi_aff in C format",
3266 			goto error);
3267 
3268 	p = isl_printer_print_str(p, name);
3269 	if (n != 0)
3270 		isl_die(p->ctx, isl_error_unsupported,
3271 			"not supported yet", goto error);
3272 
3273 	return p;
3274 error:
3275 	isl_printer_free(p);
3276 	return NULL;
3277 }
3278 
isl_printer_print_pw_multi_aff(__isl_take isl_printer * p,__isl_keep isl_pw_multi_aff * pma)3279 __isl_give isl_printer *isl_printer_print_pw_multi_aff(
3280 	__isl_take isl_printer *p, __isl_keep isl_pw_multi_aff *pma)
3281 {
3282 	if (!p || !pma)
3283 		goto error;
3284 
3285 	if (p->output_format == ISL_FORMAT_ISL)
3286 		return print_pw_multi_aff_isl(p, pma);
3287 	if (p->output_format == ISL_FORMAT_C)
3288 		return print_pw_multi_aff_c(p, pma);
3289 	isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
3290 		goto error);
3291 error:
3292 	isl_printer_free(p);
3293 	return NULL;
3294 }
3295 
print_pw_multi_aff_body_wrap(__isl_take isl_pw_multi_aff * pma,void * user)3296 static isl_stat print_pw_multi_aff_body_wrap(__isl_take isl_pw_multi_aff *pma,
3297 	void *user)
3298 {
3299 	struct isl_union_print_data *data;
3300 	data = (struct isl_union_print_data *) user;
3301 
3302 	if (!data->first)
3303 		data->p = isl_printer_print_str(data->p, "; ");
3304 	data->first = 0;
3305 
3306 	data->p = print_pw_multi_aff_body(data->p, pma);
3307 	isl_pw_multi_aff_free(pma);
3308 
3309 	return isl_stat_ok;
3310 }
3311 
print_union_pw_multi_aff_isl(__isl_take isl_printer * p,__isl_keep isl_union_pw_multi_aff * upma)3312 static __isl_give isl_printer *print_union_pw_multi_aff_isl(
3313 	__isl_take isl_printer *p, __isl_keep isl_union_pw_multi_aff *upma)
3314 {
3315 	struct isl_union_print_data data;
3316 	struct isl_print_space_data space_data = { 0 };
3317 	isl_space *space;
3318 
3319 	space = isl_union_pw_multi_aff_get_space(upma);
3320 	p = print_param_tuple(p, space, &space_data);
3321 	isl_space_free(space);
3322 	p = isl_printer_print_str(p, s_open_set[0]);
3323 	data.p = p;
3324 	data.first = 1;
3325 	isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
3326 					&print_pw_multi_aff_body_wrap, &data);
3327 	p = data.p;
3328 	p = isl_printer_print_str(p, s_close_set[0]);
3329 	return p;
3330 }
3331 
isl_printer_print_union_pw_multi_aff(__isl_take isl_printer * p,__isl_keep isl_union_pw_multi_aff * upma)3332 __isl_give isl_printer *isl_printer_print_union_pw_multi_aff(
3333 	__isl_take isl_printer *p, __isl_keep isl_union_pw_multi_aff *upma)
3334 {
3335 	if (!p || !upma)
3336 		goto error;
3337 
3338 	if (p->output_format == ISL_FORMAT_ISL)
3339 		return print_union_pw_multi_aff_isl(p, upma);
3340 	isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
3341 		goto error);
3342 error:
3343 	isl_printer_free(p);
3344 	return NULL;
3345 }
3346 
3347 /* Print dimension "pos" of data->space to "p".
3348  *
3349  * data->user is assumed to be an isl_multi_pw_aff.
3350  *
3351  * If the current dimension is an output dimension, then print
3352  * the corresponding piecewise affine expression.
3353  * Otherwise, print the name of the dimension.
3354  * Make sure to use the same space in both cases.
3355  * In particular, use the domain space for printing names as
3356  * that is the space that is used for printing constraints.
3357  */
print_dim_mpa(__isl_take isl_printer * p,struct isl_print_space_data * data,unsigned pos)3358 static __isl_give isl_printer *print_dim_mpa(__isl_take isl_printer *p,
3359 	struct isl_print_space_data *data, unsigned pos)
3360 {
3361 	int i;
3362 	int need_parens;
3363 	isl_space *space;
3364 	isl_multi_pw_aff *mpa = data->user;
3365 	isl_pw_aff *pa;
3366 
3367 	if (data->type != isl_dim_out) {
3368 		enum isl_dim_type type = data->type;
3369 
3370 		if (type == isl_dim_in)
3371 			type = isl_dim_set;
3372 		space = isl_multi_pw_aff_get_domain_space(mpa);
3373 		p = print_name(space, p, type, pos, data->latex);
3374 		isl_space_free(space);
3375 		return p;
3376 	}
3377 
3378 	pa = mpa->u.p[pos];
3379 	if (pa->n == 0)
3380 		return isl_printer_print_str(p, "(0 : false)");
3381 
3382 	need_parens = pa->n != 1 || !isl_set_plain_is_universe(pa->p[0].set);
3383 	if (need_parens)
3384 		p = isl_printer_print_str(p, "(");
3385 	space = isl_multi_pw_aff_get_domain_space(mpa);
3386 	for (i = 0; i < pa->n; ++i) {
3387 
3388 		if (i)
3389 			p = isl_printer_print_str(p, "; ");
3390 		p = print_aff_body(p, space, pa->p[i].aff);
3391 		p = print_disjuncts(pa->p[i].set, space, p, 0);
3392 	}
3393 	isl_space_free(space);
3394 	if (need_parens)
3395 		p = isl_printer_print_str(p, ")");
3396 
3397 	return p;
3398 }
3399 
3400 /* Print "mpa" to "p" in isl format.
3401  *
3402  * If "mpa" is zero-dimensional and has a non-trivial explicit domain,
3403  * then it is printed after the tuple of affine expressions.
3404  */
print_multi_pw_aff_isl(__isl_take isl_printer * p,__isl_keep isl_multi_pw_aff * mpa)3405 static __isl_give isl_printer *print_multi_pw_aff_isl(__isl_take isl_printer *p,
3406 	__isl_keep isl_multi_pw_aff *mpa)
3407 {
3408 	struct isl_print_space_data data = { 0 };
3409 	isl_bool has_domain;
3410 
3411 	if (!mpa)
3412 		return isl_printer_free(p);
3413 
3414 	p = print_param_tuple(p, mpa->space, &data);
3415 	p = isl_printer_print_str(p, "{ ");
3416 	data.print_dim = &print_dim_mpa;
3417 	data.user = mpa;
3418 	p = isl_print_space(mpa->space, p, 0, &data);
3419 	has_domain = isl_multi_pw_aff_has_non_trivial_domain(mpa);
3420 	if (has_domain < 0)
3421 		return isl_printer_free(p);
3422 	if (has_domain) {
3423 		isl_space *space;
3424 
3425 		space = isl_space_domain(isl_space_copy(mpa->space));
3426 		p = print_disjuncts_set(mpa->u.dom, space, p, 0);
3427 		isl_space_free(space);
3428 	}
3429 	p = isl_printer_print_str(p, " }");
3430 	return p;
3431 }
3432 
isl_printer_print_multi_pw_aff(__isl_take isl_printer * p,__isl_keep isl_multi_pw_aff * mpa)3433 __isl_give isl_printer *isl_printer_print_multi_pw_aff(
3434 	__isl_take isl_printer *p, __isl_keep isl_multi_pw_aff *mpa)
3435 {
3436 	if (!p || !mpa)
3437 		return isl_printer_free(p);
3438 
3439 	if (p->output_format == ISL_FORMAT_ISL)
3440 		return print_multi_pw_aff_isl(p, mpa);
3441 	isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
3442 		return isl_printer_free(p));
3443 }
3444 
3445 /* Print dimension "pos" of data->space to "p".
3446  *
3447  * data->user is assumed to be an isl_multi_val.
3448  *
3449  * If the current dimension is an output dimension, then print
3450  * the corresponding value.  Otherwise, print the name of the dimension.
3451  */
print_dim_mv(__isl_take isl_printer * p,struct isl_print_space_data * data,unsigned pos)3452 static __isl_give isl_printer *print_dim_mv(__isl_take isl_printer *p,
3453 	struct isl_print_space_data *data, unsigned pos)
3454 {
3455 	isl_multi_val *mv = data->user;
3456 
3457 	if (data->type == isl_dim_out)
3458 		return isl_printer_print_val(p, mv->u.p[pos]);
3459 	else
3460 		return print_name(data->space, p, data->type, pos, data->latex);
3461 }
3462 
3463 /* Print the isl_multi_val "mv" to "p" in isl format.
3464  */
print_multi_val_isl(__isl_take isl_printer * p,__isl_keep isl_multi_val * mv)3465 static __isl_give isl_printer *print_multi_val_isl(__isl_take isl_printer *p,
3466 	__isl_keep isl_multi_val *mv)
3467 {
3468 	struct isl_print_space_data data = { 0 };
3469 
3470 	if (!mv)
3471 		return isl_printer_free(p);
3472 
3473 	p = print_param_tuple(p, mv->space, &data);
3474 	p = isl_printer_print_str(p, "{ ");
3475 	data.print_dim = &print_dim_mv;
3476 	data.user = mv;
3477 	p = isl_print_space(mv->space, p, 0, &data);
3478 	p = isl_printer_print_str(p, " }");
3479 	return p;
3480 }
3481 
3482 /* Print the isl_multi_val "mv" to "p".
3483  *
3484  * Currently only supported in isl format.
3485  */
isl_printer_print_multi_val(__isl_take isl_printer * p,__isl_keep isl_multi_val * mv)3486 __isl_give isl_printer *isl_printer_print_multi_val(
3487 	__isl_take isl_printer *p, __isl_keep isl_multi_val *mv)
3488 {
3489 	if (!p || !mv)
3490 		return isl_printer_free(p);
3491 
3492 	if (p->output_format == ISL_FORMAT_ISL)
3493 		return print_multi_val_isl(p, mv);
3494 	isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
3495 		return isl_printer_free(p));
3496 }
3497 
3498 /* Print dimension "pos" of data->space to "p".
3499  *
3500  * data->user is assumed to be an isl_multi_id.
3501  *
3502  * If the current dimension is an output dimension, then print
3503  * the corresponding identifier.  Otherwise, print the name of the dimension.
3504  */
print_dim_mi(__isl_take isl_printer * p,struct isl_print_space_data * data,unsigned pos)3505 static __isl_give isl_printer *print_dim_mi(__isl_take isl_printer *p,
3506 	struct isl_print_space_data *data, unsigned pos)
3507 {
3508 	isl_multi_id *mi = data->user;
3509 
3510 	if (data->type == isl_dim_out)
3511 		return isl_printer_print_id(p, mi->u.p[pos]);
3512 	else
3513 		return print_name(data->space, p, data->type, pos, data->latex);
3514 }
3515 
3516 /* Print the isl_multi_id "mi" to "p" in isl format.
3517  */
print_multi_id_isl(__isl_take isl_printer * p,__isl_keep isl_multi_id * mi)3518 static __isl_give isl_printer *print_multi_id_isl(__isl_take isl_printer *p,
3519 	__isl_keep isl_multi_id *mi)
3520 {
3521 	isl_space *space;
3522 	struct isl_print_space_data data = { 0 };
3523 
3524 	space = isl_multi_id_peek_space(mi);
3525 	p = print_param_tuple(p, space, &data);
3526 	p = isl_printer_print_str(p, "{ ");
3527 	data.print_dim = &print_dim_mi;
3528 	data.user = mi;
3529 	p = isl_print_space(space, p, 0, &data);
3530 	p = isl_printer_print_str(p, " }");
3531 	return p;
3532 }
3533 
3534 /* Print the isl_multi_id "mi" to "p".
3535  *
3536  * Currently only supported in isl format.
3537  */
isl_printer_print_multi_id(__isl_take isl_printer * p,__isl_keep isl_multi_id * mi)3538 __isl_give isl_printer *isl_printer_print_multi_id(
3539 	__isl_take isl_printer *p, __isl_keep isl_multi_id *mi)
3540 {
3541 	if (!p || !mi)
3542 		return isl_printer_free(p);
3543 
3544 	if (p->output_format == ISL_FORMAT_ISL)
3545 		return print_multi_id_isl(p, mi);
3546 	isl_die(isl_printer_get_ctx(p), isl_error_unsupported,
3547 		"unsupported output format", return isl_printer_free(p));
3548 }
3549 
3550 /* Print dimension "pos" of data->space to "p".
3551  *
3552  * data->user is assumed to be an isl_multi_union_pw_aff.
3553  *
3554  * The current dimension is necessarily a set dimension, so
3555  * we print the corresponding isl_union_pw_aff, including
3556  * the braces.
3557  */
print_union_pw_aff_dim(__isl_take isl_printer * p,struct isl_print_space_data * data,unsigned pos)3558 static __isl_give isl_printer *print_union_pw_aff_dim(__isl_take isl_printer *p,
3559 	struct isl_print_space_data *data, unsigned pos)
3560 {
3561 	isl_multi_union_pw_aff *mupa = data->user;
3562 	isl_union_pw_aff *upa;
3563 
3564 	upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, pos);
3565 	p = print_union_pw_aff_body(p, upa);
3566 	isl_union_pw_aff_free(upa);
3567 
3568 	return p;
3569 }
3570 
3571 /* Print the isl_multi_union_pw_aff "mupa" to "p" in isl format.
3572  *
3573  * If "mupa" is zero-dimensional and has a non-trivial explicit domain,
3574  * then it is printed after the tuple of affine expressions.
3575  * In order to clarify that this domain belongs to the expression,
3576  * the tuple along with the domain are placed inside parentheses.
3577  * If "mupa" has any parameters, then the opening parenthesis
3578  * appears after the parameter declarations.
3579  */
print_multi_union_pw_aff_isl(__isl_take isl_printer * p,__isl_keep isl_multi_union_pw_aff * mupa)3580 static __isl_give isl_printer *print_multi_union_pw_aff_isl(
3581 	__isl_take isl_printer *p, __isl_keep isl_multi_union_pw_aff *mupa)
3582 {
3583 	struct isl_print_space_data data = { 0 };
3584 	isl_bool has_domain;
3585 	isl_space *space;
3586 
3587 	if (!mupa)
3588 		return isl_printer_free(p);
3589 	has_domain = isl_multi_union_pw_aff_has_non_trivial_domain(mupa);
3590 	if (has_domain < 0)
3591 		return isl_printer_free(p);
3592 
3593 	space = isl_multi_union_pw_aff_get_space(mupa);
3594 	p = print_param_tuple(p, space, &data);
3595 
3596 	if (has_domain)
3597 		p = isl_printer_print_str(p, "(");
3598 
3599 	data.print_dim = &print_union_pw_aff_dim;
3600 	data.user = mupa;
3601 
3602 	p = isl_print_space(space, p, 0, &data);
3603 	isl_space_free(space);
3604 
3605 	if (has_domain) {
3606 		p = isl_printer_print_str(p, " : ");
3607 		p = isl_printer_print_union_set_isl_body(p, mupa->u.dom);
3608 		p = isl_printer_print_str(p, ")");
3609 	}
3610 
3611 	return p;
3612 }
3613 
3614 /* Print the isl_multi_union_pw_aff "mupa" to "p" in isl format.
3615  *
3616  * We currently only support an isl format.
3617  */
isl_printer_print_multi_union_pw_aff(__isl_take isl_printer * p,__isl_keep isl_multi_union_pw_aff * mupa)3618 __isl_give isl_printer *isl_printer_print_multi_union_pw_aff(
3619 	__isl_take isl_printer *p, __isl_keep isl_multi_union_pw_aff *mupa)
3620 {
3621 	if (!p || !mupa)
3622 		return isl_printer_free(p);
3623 
3624 	if (p->output_format == ISL_FORMAT_ISL)
3625 		return print_multi_union_pw_aff_isl(p, mupa);
3626 	isl_die(isl_printer_get_ctx(p), isl_error_unsupported,
3627 		"unsupported output format", return isl_printer_free(p));
3628 }
3629