1 /*
2  * Copyright 2010      INRIA Saclay
3  * Copyright 2013      Ecole Normale Superieure
4  *
5  * Use of this software is governed by the MIT license
6  *
7  * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
8  * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
9  * 91893 Orsay, France
10  * and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
11  */
12 
13 #undef TYPE
14 #define TYPE	UNION
15 static
16 #include "has_single_reference_templ.c"
17 
18 __isl_give UNION *FN(UNION,cow)(__isl_take UNION *u);
19 
FN(UNION,get_ctx)20 isl_ctx *FN(UNION,get_ctx)(__isl_keep UNION *u)
21 {
22 	return u ? u->space->ctx : NULL;
23 }
24 
25 /* Return the space of "u".
26  */
FN(UNION,peek_space)27 static __isl_keep isl_space *FN(UNION,peek_space)(__isl_keep UNION *u)
28 {
29 	if (!u)
30 		return NULL;
31 	return u->space;
32 }
33 
34 /* Return a copy of the space of "u".
35  */
FN(UNION,get_space)36 __isl_give isl_space *FN(UNION,get_space)(__isl_keep UNION *u)
37 {
38 	return isl_space_copy(FN(UNION,peek_space)(u));
39 }
40 
41 /* Return the number of parameters of "u", where "type"
42  * is required to be set to isl_dim_param.
43  */
FN(UNION,dim)44 isl_size FN(UNION,dim)(__isl_keep UNION *u, enum isl_dim_type type)
45 {
46 	if (!u)
47 		return isl_size_error;
48 
49 	if (type != isl_dim_param)
50 		isl_die(FN(UNION,get_ctx)(u), isl_error_invalid,
51 			"can only reference parameters", return isl_size_error);
52 
53 	return isl_space_dim(u->space, type);
54 }
55 
56 /* Return the position of the parameter with the given name
57  * in "u".
58  * Return -1 if no such dimension can be found.
59  */
FN(UNION,find_dim_by_name)60 int FN(UNION,find_dim_by_name)(__isl_keep UNION *u, enum isl_dim_type type,
61 	const char *name)
62 {
63 	if (!u)
64 		return -1;
65 	return isl_space_find_dim_by_name(u->space, type, name);
66 }
67 
68 #include "opt_type.h"
69 
FN(UNION,alloc)70 static __isl_give UNION *FN(UNION,alloc)(__isl_take isl_space *space
71 	OPT_TYPE_PARAM, int size)
72 {
73 	UNION *u;
74 
75 	space = isl_space_params(space);
76 	if (!space)
77 		return NULL;
78 
79 	u = isl_calloc_type(space->ctx, UNION);
80 	if (!u)
81 		goto error;
82 
83 	u->ref = 1;
84 	OPT_SET_TYPE(u->, type);
85 	u->space = space;
86 	if (isl_hash_table_init(space->ctx, &u->table, size) < 0)
87 		return FN(UNION,free)(u);
88 
89 	return u;
90 error:
91 	isl_space_free(space);
92 	return NULL;
93 }
94 
95 /* Create an empty/zero union without specifying any parameters.
96  */
FN(FN (UNION,ZERO),ctx)97 __isl_give UNION *FN(FN(UNION,ZERO),ctx)(isl_ctx *ctx OPT_TYPE_PARAM)
98 {
99 	isl_space *space;
100 
101 	space = isl_space_unit(ctx);
102 	return FN(FN(UNION,ZERO),space)(space OPT_TYPE_ARG(NO_LOC));
103 }
104 
FN(FN (UNION,ZERO),space)105 __isl_give UNION *FN(FN(UNION,ZERO),space)(__isl_take isl_space *space
106 	OPT_TYPE_PARAM)
107 {
108 	return FN(UNION,alloc)(space OPT_TYPE_ARG(NO_LOC), 16);
109 }
110 
111 /* This is an alternative name for the function above.
112  */
FN(UNION,ZERO)113 __isl_give UNION *FN(UNION,ZERO)(__isl_take isl_space *space OPT_TYPE_PARAM)
114 {
115 	return FN(FN(UNION,ZERO),space)(space OPT_TYPE_ARG(NO_LOC));
116 }
117 
FN(UNION,copy)118 __isl_give UNION *FN(UNION,copy)(__isl_keep UNION *u)
119 {
120 	if (!u)
121 		return NULL;
122 
123 	u->ref++;
124 	return u;
125 }
126 
127 /* Do the tuples of "space" correspond to those of the domain of "part"?
128  * That is, is the domain space of "part" equal to "space", ignoring parameters?
129  */
FN(PART,has_domain_space_tuples)130 static isl_bool FN(PART,has_domain_space_tuples)(__isl_keep PART *part,
131 	__isl_keep isl_space *space)
132 {
133 	return isl_space_has_domain_tuples(space, FN(PART,peek_space)(part));
134 }
135 
136 /* Extract the element of "u" living in "space" (ignoring parameters).
137  *
138  * Return the ZERO element if "u" does not contain any element
139  * living in "space".
140  */
FN(FN (UNION,extract),BASE)141 __isl_give PART *FN(FN(UNION,extract),BASE)(__isl_keep UNION *u,
142 	__isl_take isl_space *space)
143 {
144 	struct isl_hash_table_entry *entry;
145 
146 	space = isl_space_replace_params(space, FN(UNION,peek_space)(u));
147 
148 	entry = FN(UNION,find_part_entry)(u, space, 0);
149 	if (!entry)
150 		goto error;
151 	if (entry == isl_hash_table_entry_none)
152 		return FN(PART,ZERO)(space OPT_TYPE_ARG(u->));
153 	isl_space_free(space);
154 	return FN(PART,copy)(entry->data);
155 error:
156 	isl_space_free(space);
157 	return NULL;
158 }
159 
160 /* Add "part" to "u".
161  * If "disjoint" is set, then "u" is not allowed to already have
162  * a part that is defined over a domain that overlaps with the domain
163  * of "part".
164  * Otherwise, compute the union sum of "part" and the part in "u"
165  * defined on the same space.
166  */
FN(UNION,add_part_generic)167 static __isl_give UNION *FN(UNION,add_part_generic)(__isl_take UNION *u,
168 	__isl_take PART *part, int disjoint)
169 {
170 	int empty;
171 	struct isl_hash_table_entry *entry;
172 
173 	if (!part)
174 		goto error;
175 
176 	empty = FN(PART,IS_ZERO)(part);
177 	if (empty < 0)
178 		goto error;
179 	if (empty) {
180 		FN(PART,free)(part);
181 		return u;
182 	}
183 
184 	u = FN(UNION,align_params)(u, FN(PART,get_space)(part));
185 	part = FN(PART,align_params)(part, FN(UNION,get_space)(u));
186 
187 	u = FN(UNION,cow)(u);
188 
189 	if (!u)
190 		goto error;
191 
192 	if (FN(UNION,check_disjoint_domain_other)(u, part) < 0)
193 		goto error;
194 	entry = FN(UNION,find_part_entry)(u, part->dim, 1);
195 	if (!entry)
196 		goto error;
197 
198 	if (!entry->data)
199 		entry->data = part;
200 	else {
201 		if (disjoint &&
202 		    FN(UNION,check_disjoint_domain)(entry->data, part) < 0)
203 			goto error;
204 		entry->data = FN(PART,union_add_)(entry->data,
205 						FN(PART,copy)(part));
206 		if (!entry->data)
207 			goto error;
208 		empty = FN(PART,IS_ZERO)(part);
209 		if (empty < 0)
210 			goto error;
211 		if (empty)
212 			u = FN(UNION,remove_part_entry)(u, entry);
213 		FN(PART,free)(part);
214 	}
215 
216 	return u;
217 error:
218 	FN(PART,free)(part);
219 	FN(UNION,free)(u);
220 	return NULL;
221 }
222 
223 /* Add "part" to "u", where "u" is assumed not to already have
224  * a part that is defined on the same space as "part".
225  */
FN(FN (UNION,add),BASE)226 __isl_give UNION *FN(FN(UNION,add),BASE)(__isl_take UNION *u,
227 	__isl_take PART *part)
228 {
229 	return FN(UNION,add_part_generic)(u, part, 1);
230 }
231 
232 /* Allocate a UNION with the same type (if any) and the same size as "u" and
233  * with space "space".
234  */
FN(UNION,alloc_same_size_on_space)235 static __isl_give UNION *FN(UNION,alloc_same_size_on_space)(__isl_keep UNION *u,
236 	__isl_take isl_space *space)
237 {
238 	if (!u)
239 		goto error;
240 	return FN(UNION,alloc)(space OPT_TYPE_ARG(u->), u->table.n);
241 error:
242 	isl_space_free(space);
243 	return NULL;
244 }
245 
246 /* Allocate a UNION with the same space, the same type (if any) and
247  * the same size as "u".
248  */
FN(UNION,alloc_same_size)249 static __isl_give UNION *FN(UNION,alloc_same_size)(__isl_keep UNION *u)
250 {
251 	return FN(UNION,alloc_same_size_on_space)(u, FN(UNION,get_space)(u));
252 }
253 
254 /* Data structure that specifies how isl_union_*_transform
255  * should modify the base expressions in the union expression.
256  *
257  * If "inplace" is set, then the base expression in the input union
258  * are modified in place.  This means that "fn" should not
259  * change the meaning of the union or that the union only
260  * has a single reference.
261  * If "space" is not NULL, then a new union is created in this space.
262  * If "filter" is not NULL, then only the base expressions that satisfy "filter"
263  * are taken into account.
264  * "filter_user" is passed as the second argument to "filter".
265  * If "fn" it not NULL, then it is applied to each entry in the input.
266  * "fn_user" is passed as the second argument to "fn".
267  */
S(UNION,transform_control)268 S(UNION,transform_control) {
269 	int inplace;
270 	isl_space *space;
271 	isl_bool (*filter)(__isl_keep PART *part, void *user);
272 	void *filter_user;
273 	__isl_give PART *(*fn)(__isl_take PART *part, void *user);
274 	void *fn_user;
275 };
276 
277 /* Internal data structure for isl_union_*_transform_space.
278  * "control" specifies how the base expressions should be modified.
279  * "res" collects the results (if control->inplace is not set).
280  */
S(UNION,transform_data)281 S(UNION,transform_data)
282 {
283 	S(UNION,transform_control) *control;
284 	UNION *res;
285 };
286 
287 /* Apply control->fn to "part" and add the result to data->res or
288  * place it back into the input union if control->inplace is set.
289  */
FN(UNION,transform_entry)290 static isl_stat FN(UNION,transform_entry)(void **entry, void *user)
291 {
292 	S(UNION,transform_data) *data = (S(UNION,transform_data) *)user;
293 	S(UNION,transform_control) *control = data->control;
294 	PART *part = *entry;
295 
296 	if (control->filter) {
297 		isl_bool handle;
298 
299 		handle = control->filter(part, control->filter_user);
300 		if (handle < 0)
301 			return isl_stat_error;
302 		if (!handle)
303 			return isl_stat_ok;
304 	}
305 
306 	if (!control->inplace)
307 		part = FN(PART,copy)(part);
308 	if (control->fn)
309 		part = control->fn(part, control->fn_user);
310 	if (control->inplace)
311 		*entry = part;
312 	else
313 		data->res = FN(FN(UNION,add),BASE)(data->res, part);
314 	if (!part || !data->res)
315 		return isl_stat_error;
316 
317 	return isl_stat_ok;
318 }
319 
320 /* Return a UNION that is obtained by modifying "u" according to "control".
321  */
FN(UNION,transform)322 static __isl_give UNION *FN(UNION,transform)(__isl_take UNION *u,
323 	S(UNION,transform_control) *control)
324 {
325 	S(UNION,transform_data) data = { control };
326 	isl_space *space;
327 
328 	if (control->inplace) {
329 		data.res = u;
330 	} else {
331 		if (control->space)
332 			space = isl_space_copy(control->space);
333 		else
334 			space = FN(UNION,get_space)(u);
335 		data.res = FN(UNION,alloc_same_size_on_space)(u, space);
336 	}
337 	if (FN(UNION,foreach_inplace)(u, &FN(UNION,transform_entry), &data) < 0)
338 		data.res = FN(UNION,free)(data.res);
339 	if (!control->inplace)
340 		FN(UNION,free)(u);
341 	return data.res;
342 }
343 
344 /* Return a UNION living in "space" that is otherwise obtained by modifying "u"
345  * according to "control".
346  */
FN(UNION,transform_space)347 static __isl_give UNION *FN(UNION,transform_space)(__isl_take UNION *u,
348 	__isl_take isl_space *space, S(UNION,transform_control) *control)
349 {
350 	if (!space)
351 		return FN(UNION,free)(u);
352 	control->space = space;
353 	u = FN(UNION,transform)(u, control);
354 	isl_space_free(space);
355 	return u;
356 }
357 
358 /* Update "u" by applying "fn" to each entry.
359  * This operation is assumed not to change the number of entries nor
360  * the spaces of the entries.
361  *
362  * If there is only one reference to "u", then change "u" inplace.
363  * Otherwise, create a new UNION from "u" and discard the original.
364  */
FN(UNION,transform_inplace)365 static __isl_give UNION *FN(UNION,transform_inplace)(__isl_take UNION *u,
366 	__isl_give PART *(*fn)(__isl_take PART *part, void *user), void *user)
367 {
368 	S(UNION,transform_control) control = { .fn = fn, .fn_user = user };
369 	isl_bool single_ref;
370 
371 	single_ref = FN(UNION,has_single_reference)(u);
372 	if (single_ref < 0)
373 		return FN(UNION,free)(u);
374 	if (single_ref)
375 		control.inplace = 1;
376 	return FN(UNION,transform)(u, &control);
377 }
378 
379 /* An isl_union_*_transform callback for use in isl_union_*_dup
380  * that simply returns "part".
381  */
FN(UNION,copy_part)382 static __isl_give PART *FN(UNION,copy_part)(__isl_take PART *part, void *user)
383 {
384 	return part;
385 }
386 
FN(UNION,dup)387 __isl_give UNION *FN(UNION,dup)(__isl_keep UNION *u)
388 {
389 	S(UNION,transform_control) control = { .fn = &FN(UNION,copy_part) };
390 
391 	u = FN(UNION,copy)(u);
392 	return FN(UNION,transform)(u, &control);
393 }
394 
FN(UNION,cow)395 __isl_give UNION *FN(UNION,cow)(__isl_take UNION *u)
396 {
397 	if (!u)
398 		return NULL;
399 
400 	if (u->ref == 1)
401 		return u;
402 	u->ref--;
403 	return FN(UNION,dup)(u);
404 }
405 
FN(UNION,free)406 __isl_null UNION *FN(UNION,free)(__isl_take UNION *u)
407 {
408 	if (!u)
409 		return NULL;
410 
411 	if (--u->ref > 0)
412 		return NULL;
413 
414 	isl_hash_table_foreach(u->space->ctx, &u->table,
415 				&FN(UNION,free_u_entry), NULL);
416 	isl_hash_table_clear(&u->table);
417 	isl_space_free(u->space);
418 	free(u);
419 	return NULL;
420 }
421 
FN(UNION,align_entry)422 static __isl_give PART *FN(UNION,align_entry)(__isl_take PART *part, void *user)
423 {
424 	isl_reordering *exp = user;
425 
426 	exp = isl_reordering_extend_space(isl_reordering_copy(exp),
427 				    FN(PART,get_domain_space)(part));
428 	return FN(PART,realign_domain)(part, exp);
429 }
430 
431 /* Reorder the parameters of "u" according to the given reordering.
432  */
FN(UNION,realign_domain)433 static __isl_give UNION *FN(UNION,realign_domain)(__isl_take UNION *u,
434 	__isl_take isl_reordering *r)
435 {
436 	S(UNION,transform_control) control = {
437 		.fn = &FN(UNION,align_entry),
438 		.fn_user = r,
439 	};
440 	isl_space *space;
441 
442 	if (!u || !r)
443 		goto error;
444 
445 	space = isl_reordering_get_space(r);
446 	u = FN(UNION,transform_space)(u, space, &control);
447 	isl_reordering_free(r);
448 	return u;
449 error:
450 	FN(UNION,free)(u);
451 	isl_reordering_free(r);
452 	return NULL;
453 }
454 
455 /* Align the parameters of "u" to those of "model".
456  */
FN(UNION,align_params)457 __isl_give UNION *FN(UNION,align_params)(__isl_take UNION *u,
458 	__isl_take isl_space *model)
459 {
460 	isl_bool equal_params;
461 	isl_reordering *r;
462 
463 	if (!u || !model)
464 		goto error;
465 
466 	equal_params = isl_space_has_equal_params(u->space, model);
467 	if (equal_params < 0)
468 		goto error;
469 	if (equal_params) {
470 		isl_space_free(model);
471 		return u;
472 	}
473 
474 	r = isl_parameter_alignment_reordering(u->space, model);
475 	isl_space_free(model);
476 
477 	return FN(UNION,realign_domain)(u, r);
478 error:
479 	isl_space_free(model);
480 	FN(UNION,free)(u);
481 	return NULL;
482 }
483 
484 /* Add "part" to *u, taking the union sum if "u" already has
485  * a part defined on the same space as "part".
486  */
FN(UNION,union_add_part)487 static isl_stat FN(UNION,union_add_part)(__isl_take PART *part, void *user)
488 {
489 	UNION **u = (UNION **)user;
490 
491 	*u = FN(UNION,add_part_generic)(*u, part, 0);
492 
493 	return isl_stat_ok;
494 }
495 
496 /* Compute the sum of "u1" and "u2" on the union of their domains,
497  * with the actual sum on the shared domain and
498  * the defined expression on the symmetric difference of the domains.
499  *
500  * This is an internal function that is exposed under different
501  * names depending on whether the base expressions have a zero default
502  * value.
503  * If they do, then this function is called "add".
504  * Otherwise, it is called "union_add".
505  */
FN(UNION,union_add_)506 static __isl_give UNION *FN(UNION,union_add_)(__isl_take UNION *u1,
507 	__isl_take UNION *u2)
508 {
509 	u1 = FN(UNION,align_params)(u1, FN(UNION,get_space)(u2));
510 	u2 = FN(UNION,align_params)(u2, FN(UNION,get_space)(u1));
511 
512 	u1 = FN(UNION,cow)(u1);
513 
514 	if (!u1 || !u2)
515 		goto error;
516 
517 	if (FN(FN(UNION,foreach),BASE)(u2, &FN(UNION,union_add_part), &u1) < 0)
518 		goto error;
519 
520 	FN(UNION,free)(u2);
521 
522 	return u1;
523 error:
524 	FN(UNION,free)(u1);
525 	FN(UNION,free)(u2);
526 	return NULL;
527 }
528 
FN(FN (UNION,from),BASE)529 __isl_give UNION *FN(FN(UNION,from),BASE)(__isl_take PART *part)
530 {
531 	isl_space *space;
532 	UNION *u;
533 
534 	if (!part)
535 		return NULL;
536 
537 	space = FN(PART,get_space)(part);
538 	space = isl_space_drop_dims(space, isl_dim_in, 0,
539 					isl_space_dim(space, isl_dim_in));
540 	space = isl_space_drop_dims(space, isl_dim_out, 0,
541 					isl_space_dim(space, isl_dim_out));
542 	u = FN(UNION,ZERO)(space OPT_TYPE_ARG(part->));
543 	u = FN(FN(UNION,add),BASE)(u, part);
544 
545 	return u;
546 }
547 
S(UNION,match_bin_data)548 S(UNION,match_bin_data) {
549 	UNION *u2;
550 	UNION *res;
551 	__isl_give PART *(*fn)(__isl_take PART *, __isl_take PART *);
552 };
553 
554 /* Check if data->u2 has an element living in the same space as "part".
555  * If so, call data->fn on the two elements and add the result to
556  * data->res.
557  */
FN(UNION,match_bin_entry)558 static isl_stat FN(UNION,match_bin_entry)(__isl_take PART *part, void *user)
559 {
560 	S(UNION,match_bin_data) *data = user;
561 	struct isl_hash_table_entry *entry2;
562 	isl_space *space;
563 	PART *part2;
564 
565 	space = FN(PART,get_space)(part);
566 	entry2 = FN(UNION,find_part_entry)(data->u2, space, 0);
567 	isl_space_free(space);
568 	if (!entry2)
569 		goto error;
570 	if (entry2 == isl_hash_table_entry_none) {
571 		FN(PART,free)(part);
572 		return isl_stat_ok;
573 	}
574 
575 	part2 = entry2->data;
576 	if (!isl_space_tuple_is_equal(part->dim, isl_dim_out,
577 					part2->dim, isl_dim_out))
578 		isl_die(FN(UNION,get_ctx)(data->u2), isl_error_invalid,
579 			"entries should have the same range space",
580 			goto error);
581 
582 	part = data->fn(part, FN(PART, copy)(entry2->data));
583 
584 	data->res = FN(FN(UNION,add),BASE)(data->res, part);
585 	if (!data->res)
586 		return isl_stat_error;
587 
588 	return isl_stat_ok;
589 error:
590 	FN(PART,free)(part);
591 	return isl_stat_error;
592 }
593 
594 /* This function is currently only used from isl_polynomial.c
595  * and not from isl_fold.c.
596  */
597 static __isl_give UNION *FN(UNION,match_bin_op)(__isl_take UNION *u1,
598 	__isl_take UNION *u2,
599 	__isl_give PART *(*fn)(__isl_take PART *, __isl_take PART *))
600 	__attribute__ ((unused));
601 /* For each pair of elements in "u1" and "u2" living in the same space,
602  * call "fn" and collect the results.
603  */
FN(UNION,match_bin_op)604 static __isl_give UNION *FN(UNION,match_bin_op)(__isl_take UNION *u1,
605 	__isl_take UNION *u2,
606 	__isl_give PART *(*fn)(__isl_take PART *, __isl_take PART *))
607 {
608 	S(UNION,match_bin_data) data = { NULL, NULL, fn };
609 
610 	u1 = FN(UNION,align_params)(u1, FN(UNION,get_space)(u2));
611 	u2 = FN(UNION,align_params)(u2, FN(UNION,get_space)(u1));
612 
613 	if (!u1 || !u2)
614 		goto error;
615 
616 	data.u2 = u2;
617 	data.res = FN(UNION,alloc_same_size)(u1);
618 	if (FN(FN(UNION,foreach),BASE)(u1,
619 				    &FN(UNION,match_bin_entry), &data) < 0)
620 		goto error;
621 
622 	FN(UNION,free)(u1);
623 	FN(UNION,free)(u2);
624 	return data.res;
625 error:
626 	FN(UNION,free)(u1);
627 	FN(UNION,free)(u2);
628 	FN(UNION,free)(data.res);
629 	return NULL;
630 }
631 
632 /* Compute the sum of "u1" and "u2".
633  *
634  * If the base expressions have a default zero value, then the sum
635  * is computed on the union of the domains of "u1" and "u2".
636  * Otherwise, it is computed on their shared domains.
637  */
FN(UNION,add)638 __isl_give UNION *FN(UNION,add)(__isl_take UNION *u1, __isl_take UNION *u2)
639 {
640 #if DEFAULT_IS_ZERO
641 	return FN(UNION,union_add_)(u1, u2);
642 #else
643 	return FN(UNION,match_bin_op)(u1, u2, &FN(PART,add));
644 #endif
645 }
646 
647 #ifndef NO_SUB
648 /* Subtract "u2" from "u1" and return the result.
649  */
FN(UNION,sub)650 __isl_give UNION *FN(UNION,sub)(__isl_take UNION *u1, __isl_take UNION *u2)
651 {
652 	return FN(UNION,match_bin_op)(u1, u2, &FN(PART,sub));
653 }
654 #endif
655 
S(UNION,any_set_data)656 S(UNION,any_set_data) {
657 	isl_set *set;
658 	__isl_give PW *(*fn)(__isl_take PW*, __isl_take isl_set*);
659 };
660 
FN(UNION,any_set_entry)661 static __isl_give PART *FN(UNION,any_set_entry)(__isl_take PART *part,
662 	void *user)
663 {
664 	S(UNION,any_set_data) *data = user;
665 
666 	return data->fn(part, isl_set_copy(data->set));
667 }
668 
669 /* Update each element of "u" by calling "fn" on the element and "set".
670  */
FN(UNION,any_set_op)671 static __isl_give UNION *FN(UNION,any_set_op)(__isl_take UNION *u,
672 	__isl_take isl_set *set,
673 	__isl_give PW *(*fn)(__isl_take PW*, __isl_take isl_set*))
674 {
675 	S(UNION,any_set_data) data = { NULL, fn };
676 	S(UNION,transform_control) control = {
677 		.fn = &FN(UNION,any_set_entry),
678 		.fn_user = &data,
679 	};
680 
681 	u = FN(UNION,align_params)(u, isl_set_get_space(set));
682 	set = isl_set_align_params(set, FN(UNION,get_space)(u));
683 
684 	if (!u || !set)
685 		goto error;
686 
687 	data.set = set;
688 	u = FN(UNION,transform)(u, &control);
689 	isl_set_free(set);
690 	return u;
691 error:
692 	FN(UNION,free)(u);
693 	isl_set_free(set);
694 	return NULL;
695 }
696 
697 /* Intersect the domain of "u" with the parameter domain "context".
698  */
FN(UNION,intersect_params)699 __isl_give UNION *FN(UNION,intersect_params)(__isl_take UNION *u,
700 	__isl_take isl_set *set)
701 {
702 	return FN(UNION,any_set_op)(u, set, &FN(PW,intersect_params));
703 }
704 
705 /* Compute the gist of the domain of "u" with respect to
706  * the parameter domain "context".
707  */
FN(UNION,gist_params)708 __isl_give UNION *FN(UNION,gist_params)(__isl_take UNION *u,
709 	__isl_take isl_set *set)
710 {
711 	return FN(UNION,any_set_op)(u, set, &FN(PW,gist_params));
712 }
713 
714 /* Data structure that specifies how isl_union_*_match_domain_op
715  * should combine its arguments.
716  *
717  * If "filter" is not NULL, then only parts that pass the given
718  * filter are considered for matching.
719  * "fn" is applied to each part in the union and each corresponding
720  * set in the union set, i.e., such that the set lives in the same space
721  * as the domain of the part.
722  * If "match_space" is not NULL, then the set extracted from the union set
723  * does not live in the same space as the domain of the part,
724  * but rather in the space that results from calling "match_space"
725  * on this domain space.
726  */
S(UNION,match_domain_control)727 S(UNION,match_domain_control) {
728 	isl_bool (*filter)(__isl_keep PART *part);
729 	__isl_give isl_space *(*match_space)(__isl_take isl_space *space);
730 	__isl_give PW *(*fn)(__isl_take PW*, __isl_take isl_set*);
731 };
732 
S(UNION,match_domain_data)733 S(UNION,match_domain_data) {
734 	isl_union_set *uset;
735 	UNION *res;
736 	S(UNION,match_domain_control) *control;
737 };
738 
FN(UNION,set_has_space)739 static isl_bool FN(UNION,set_has_space)(const void *entry, const void *val)
740 {
741 	isl_set *set = (isl_set *)entry;
742 	isl_space *space = (isl_space *)val;
743 
744 	return isl_space_is_equal(set->dim, space);
745 }
746 
747 /* Find the set in data->uset that lives in the same space as the domain
748  * of "part", apply data->fn to *entry and this set (if any), and add
749  * the result to data->res.
750  */
FN(UNION,match_domain_entry)751 static isl_stat FN(UNION,match_domain_entry)(__isl_take PART *part, void *user)
752 {
753 	S(UNION,match_domain_data) *data = user;
754 	uint32_t hash;
755 	struct isl_hash_table_entry *entry2;
756 	isl_space *space, *uset_space;
757 
758 	if (data->control->filter) {
759 		isl_bool pass = data->control->filter(part);
760 		if (pass < 0 || !pass) {
761 			FN(PART,free)(part);
762 			return pass < 0 ? isl_stat_error : isl_stat_ok;
763 		}
764 	}
765 
766 	uset_space = isl_union_set_peek_space(data->uset);
767 	space = FN(PART,get_domain_space)(part);
768 	if (data->control->match_space)
769 		space = data->control->match_space(space);
770 	space = isl_space_replace_params(space, uset_space);
771 	hash = isl_space_get_hash(space);
772 	entry2 = isl_hash_table_find(data->uset->dim->ctx, &data->uset->table,
773 				     hash, &FN(UNION,set_has_space), space, 0);
774 	isl_space_free(space);
775 	if (!entry2 || entry2 == isl_hash_table_entry_none) {
776 		FN(PART,free)(part);
777 		return isl_stat_non_null(entry2);
778 	}
779 
780 	part = data->control->fn(part, isl_set_copy(entry2->data));
781 
782 	data->res = FN(FN(UNION,add),BASE)(data->res, part);
783 	if (!data->res)
784 		return isl_stat_error;
785 
786 	return isl_stat_ok;
787 }
788 
789 /* Combine "u" and "uset" according to "control"
790  * and collect the results.
791  */
FN(UNION,match_domain_op)792 static __isl_give UNION *FN(UNION,match_domain_op)(__isl_take UNION *u,
793 	__isl_take isl_union_set *uset, S(UNION,match_domain_control) *control)
794 {
795 	S(UNION,match_domain_data) data = { NULL, NULL, control };
796 
797 	if (!u || !uset)
798 		goto error;
799 
800 	data.uset = uset;
801 	data.res = FN(UNION,alloc_same_size)(u);
802 	if (FN(FN(UNION,foreach),BASE)(u,
803 				   &FN(UNION,match_domain_entry), &data) < 0)
804 		goto error;
805 
806 	FN(UNION,free)(u);
807 	isl_union_set_free(uset);
808 	return data.res;
809 error:
810 	FN(UNION,free)(u);
811 	isl_union_set_free(uset);
812 	FN(UNION,free)(data.res);
813 	return NULL;
814 }
815 
816 /* Intersect the domain of "u" with "uset".
817  * If "uset" is a parameters domain, then intersect the parameter
818  * domain of "u" with this set.
819  */
FN(UNION,intersect_domain_union_set)820 __isl_give UNION *FN(UNION,intersect_domain_union_set)(__isl_take UNION *u,
821 	__isl_take isl_union_set *uset)
822 {
823 	S(UNION,match_domain_control) control = {
824 		.fn = &FN(PW,intersect_domain),
825 	};
826 
827 	if (isl_union_set_is_params(uset))
828 		return FN(UNION,intersect_params)(u,
829 						isl_set_from_union_set(uset));
830 	return FN(UNION,match_domain_op)(u, uset, &control);
831 }
832 
833 /* This is an alternative name for the function above.
834  */
FN(UNION,intersect_domain)835 __isl_give UNION *FN(UNION,intersect_domain)(__isl_take UNION *u,
836 	__isl_take isl_union_set *uset)
837 {
838 	return FN(UNION,intersect_domain_union_set)(u, uset);
839 }
840 
841 /* Return true if this part should be kept.
842  *
843  * In particular, it should be kept if its domain space
844  * corresponds to "space".
845  */
FN(UNION,select_entry)846 static isl_bool FN(UNION,select_entry)(__isl_keep PART *part, void *user)
847 {
848 	isl_space *space = user;
849 
850 	return FN(PW,has_domain_space_tuples)(part, space);
851 }
852 
853 /* Remove any not element in "space" from the domain of "u".
854  *
855  * In particular, select any part of the function defined
856  * on this domain space.
857  */
FN(UNION,intersect_domain_space)858 __isl_give UNION *FN(UNION,intersect_domain_space)(__isl_take UNION *u,
859 	__isl_take isl_space *space)
860 {
861 	S(UNION,transform_control) control = {
862 		.filter = &FN(UNION,select_entry),
863 		.filter_user = space,
864 	};
865 
866 	u = FN(UNION,transform)(u, &control);
867 	isl_space_free(space);
868 	return u;
869 }
870 
871 /* Is the domain of "pw" a wrapped relation?
872  */
FN(PW,domain_is_wrapping)873 static isl_bool FN(PW,domain_is_wrapping)(__isl_keep PW *pw)
874 {
875 	return isl_space_domain_is_wrapping(FN(PW,peek_space)(pw));
876 }
877 
878 /* Intersect the domain of the wrapped relation inside the domain of "u"
879  * with "uset".
880  */
FN(UNION,intersect_domain_wrapped_domain)881 __isl_give UNION *FN(UNION,intersect_domain_wrapped_domain)(__isl_take UNION *u,
882 	__isl_take isl_union_set *uset)
883 {
884 	S(UNION,match_domain_control) control = {
885 		.filter = &FN(PART,domain_is_wrapping),
886 		.match_space = &isl_space_factor_domain,
887 		.fn = &FN(PW,intersect_domain_wrapped_domain),
888 	};
889 
890 	return FN(UNION,match_domain_op)(u, uset, &control);
891 }
892 
893 /* Intersect the range of the wrapped relation inside the domain of "u"
894  * with "uset".
895  */
FN(UNION,intersect_domain_wrapped_range)896 __isl_give UNION *FN(UNION,intersect_domain_wrapped_range)(__isl_take UNION *u,
897 	__isl_take isl_union_set *uset)
898 {
899 	S(UNION,match_domain_control) control = {
900 		.filter = &FN(PART,domain_is_wrapping),
901 		.match_space = &isl_space_factor_range,
902 		.fn = &FN(PW,intersect_domain_wrapped_range),
903 	};
904 
905 	return FN(UNION,match_domain_op)(u, uset, &control);
906 }
907 
908 /* Take the set (which may be empty) in data->uset that lives
909  * in the same space as the domain of "pw", subtract it from the domain
910  * of "part" and return the result.
911  */
FN(UNION,subtract_domain_entry)912 static __isl_give PART *FN(UNION,subtract_domain_entry)(__isl_take PART *part,
913 	void *user)
914 {
915 	isl_union_set *uset = user;
916 	isl_space *space;
917 	isl_set *set;
918 
919 	space = FN(PART,get_domain_space)(part);
920 	set = isl_union_set_extract_set(uset, space);
921 	return FN(PART,subtract_domain)(part, set);
922 }
923 
924 /* Subtract "uset" from the domain of "u".
925  */
FN(UNION,subtract_domain_union_set)926 __isl_give UNION *FN(UNION,subtract_domain_union_set)(__isl_take UNION *u,
927 	__isl_take isl_union_set *uset)
928 {
929 	S(UNION,transform_control) control = {
930 		.fn = &FN(UNION,subtract_domain_entry),
931 		.fn_user = uset,
932 	};
933 
934 	u = FN(UNION,transform)(u, &control);
935 	isl_union_set_free(uset);
936 	return u;
937 }
938 
939 /* This is an alternative name for the function above.
940  */
FN(UNION,subtract_domain)941 __isl_give UNION *FN(UNION,subtract_domain)(__isl_take UNION *u,
942 	__isl_take isl_union_set *uset)
943 {
944 	return FN(UNION,subtract_domain_union_set)(u, uset);
945 }
946 
947 /* Return true if this part should be kept.
948  *
949  * In particular, it should be kept if its domain space
950  * does not correspond to "space".
951  */
FN(UNION,filter_out_entry)952 static isl_bool FN(UNION,filter_out_entry)(__isl_keep PART *part, void *user)
953 {
954 	isl_space *space = user;
955 
956 	return isl_bool_not(FN(PW,has_domain_space_tuples)(part, space));
957 }
958 
959 /* Remove any element in "space" from the domain of "u".
960  *
961  * In particular, filter out any part of the function defined
962  * on this domain space.
963  */
FN(UNION,subtract_domain_space)964 __isl_give UNION *FN(UNION,subtract_domain_space)(__isl_take UNION *u,
965 	__isl_take isl_space *space)
966 {
967 	S(UNION,transform_control) control = {
968 		.filter = &FN(UNION,filter_out_entry),
969 		.filter_user = space,
970 	};
971 
972 	u = FN(UNION,transform)(u, &control);
973 	isl_space_free(space);
974 	return u;
975 }
976 
FN(UNION,gist)977 __isl_give UNION *FN(UNION,gist)(__isl_take UNION *u,
978 	__isl_take isl_union_set *uset)
979 {
980 	S(UNION,match_domain_control) control = {
981 		.fn = &FN(PW,gist),
982 	};
983 
984 	if (isl_union_set_is_params(uset))
985 		return FN(UNION,gist_params)(u, isl_set_from_union_set(uset));
986 	return FN(UNION,match_domain_op)(u, uset, &control);
987 }
988 
989 /* Coalesce an entry in a UNION.  Coalescing is performed in-place.
990  * Since the UNION may have several references, the entry is only
991  * replaced if the coalescing is successful.
992  */
FN(UNION,coalesce_entry)993 static isl_stat FN(UNION,coalesce_entry)(void **entry, void *user)
994 {
995 	PART **part_p = (PART **) entry;
996 	PART *part;
997 
998 	part = FN(PART,copy)(*part_p);
999 	part = FN(PW,coalesce)(part);
1000 	if (!part)
1001 		return isl_stat_error;
1002 	FN(PART,free)(*part_p);
1003 	*part_p = part;
1004 
1005 	return isl_stat_ok;
1006 }
1007 
FN(UNION,coalesce)1008 __isl_give UNION *FN(UNION,coalesce)(__isl_take UNION *u)
1009 {
1010 	if (FN(UNION,foreach_inplace)(u, &FN(UNION,coalesce_entry), NULL) < 0)
1011 		goto error;
1012 
1013 	return u;
1014 error:
1015 	FN(UNION,free)(u);
1016 	return NULL;
1017 }
1018 
FN(UNION,domain_entry)1019 static isl_stat FN(UNION,domain_entry)(__isl_take PART *part, void *user)
1020 {
1021 	isl_union_set **uset = (isl_union_set **)user;
1022 
1023 	*uset = isl_union_set_add_set(*uset, FN(PART,domain)(part));
1024 
1025 	return isl_stat_ok;
1026 }
1027 
FN(UNION,domain)1028 __isl_give isl_union_set *FN(UNION,domain)(__isl_take UNION *u)
1029 {
1030 	isl_union_set *uset;
1031 
1032 	uset = isl_union_set_empty(FN(UNION,get_space)(u));
1033 	if (FN(FN(UNION,foreach),BASE)(u, &FN(UNION,domain_entry), &uset) < 0)
1034 		goto error;
1035 
1036 	FN(UNION,free)(u);
1037 
1038 	return uset;
1039 error:
1040 	isl_union_set_free(uset);
1041 	FN(UNION,free)(u);
1042 	return NULL;
1043 }
1044 
1045 #ifdef HAS_TYPE
1046 /* Negate the type of "u".
1047  */
FN(UNION,negate_type)1048 static __isl_give UNION *FN(UNION,negate_type)(__isl_take UNION *u)
1049 {
1050 	u = FN(UNION,cow)(u);
1051 	if (!u)
1052 		return NULL;
1053 	u->type = isl_fold_type_negate(u->type);
1054 	return u;
1055 }
1056 #else
1057 /* Negate the type of "u".
1058  * Since "u" does not have a type, do nothing.
1059  */
FN(UNION,negate_type)1060 static __isl_give UNION *FN(UNION,negate_type)(__isl_take UNION *u)
1061 {
1062 	return u;
1063 }
1064 #endif
1065 
1066 /* Multiply "part" by the isl_val "user" and return the result.
1067  */
FN(UNION,scale_val_entry)1068 static __isl_give PART *FN(UNION,scale_val_entry)(__isl_take PART *part,
1069 	void *user)
1070 {
1071 	isl_val *v = user;
1072 
1073 	return FN(PART,scale_val)(part, isl_val_copy(v));
1074 }
1075 
1076 /* Multiply "u" by "v" and return the result.
1077  */
FN(UNION,scale_val)1078 __isl_give UNION *FN(UNION,scale_val)(__isl_take UNION *u,
1079 	__isl_take isl_val *v)
1080 {
1081 	if (!u || !v)
1082 		goto error;
1083 	if (isl_val_is_one(v)) {
1084 		isl_val_free(v);
1085 		return u;
1086 	}
1087 
1088 	if (DEFAULT_IS_ZERO && u && isl_val_is_zero(v)) {
1089 		UNION *zero;
1090 		isl_space *space = FN(UNION,get_space)(u);
1091 		zero = FN(UNION,ZERO)(space OPT_TYPE_ARG(u->));
1092 		FN(UNION,free)(u);
1093 		isl_val_free(v);
1094 		return zero;
1095 	}
1096 
1097 	if (!isl_val_is_rat(v))
1098 		isl_die(isl_val_get_ctx(v), isl_error_invalid,
1099 			"expecting rational factor", goto error);
1100 
1101 	u = FN(UNION,transform_inplace)(u, &FN(UNION,scale_val_entry), v);
1102 	if (isl_val_is_neg(v))
1103 		u = FN(UNION,negate_type)(u);
1104 
1105 	isl_val_free(v);
1106 	return u;
1107 error:
1108 	isl_val_free(v);
1109 	FN(UNION,free)(u);
1110 	return NULL;
1111 }
1112 
1113 /* Divide "part" by the isl_val "user" and return the result.
1114  */
FN(UNION,scale_down_val_entry)1115 static __isl_give PART *FN(UNION,scale_down_val_entry)(__isl_take PART *part,
1116 	void *user)
1117 {
1118 	isl_val *v = user;
1119 
1120 	return FN(PART,scale_down_val)(part, isl_val_copy(v));
1121 }
1122 
1123 /* Divide "u" by "v" and return the result.
1124  */
FN(UNION,scale_down_val)1125 __isl_give UNION *FN(UNION,scale_down_val)(__isl_take UNION *u,
1126 	__isl_take isl_val *v)
1127 {
1128 	if (!u || !v)
1129 		goto error;
1130 	if (isl_val_is_one(v)) {
1131 		isl_val_free(v);
1132 		return u;
1133 	}
1134 
1135 	if (!isl_val_is_rat(v))
1136 		isl_die(isl_val_get_ctx(v), isl_error_invalid,
1137 			"expecting rational factor", goto error);
1138 	if (isl_val_is_zero(v))
1139 		isl_die(isl_val_get_ctx(v), isl_error_invalid,
1140 			"cannot scale down by zero", goto error);
1141 
1142 	u = FN(UNION,transform_inplace)(u, &FN(UNION,scale_down_val_entry), v);
1143 	if (isl_val_is_neg(v))
1144 		u = FN(UNION,negate_type)(u);
1145 
1146 	isl_val_free(v);
1147 	return u;
1148 error:
1149 	isl_val_free(v);
1150 	FN(UNION,free)(u);
1151 	return NULL;
1152 }
1153 
1154 /* Internal data structure for isl_union_*_every_*.
1155  *
1156  * "test" is the user-specified callback function.
1157  * "user" is the user-specified callback function argument.
1158  *
1159  * "res" is the final result, initialized to isl_bool_true.
1160  */
S(UNION,every_data)1161 S(UNION,every_data) {
1162 	isl_bool (*test)(__isl_keep PW *pw, void *user);
1163 	void *user;
1164 
1165 	isl_bool res;
1166 };
1167 
1168 /* Call data->test on the piecewise expression at *entry,
1169  * updating the result in data->res.
1170  * Abort if this result is no longer isl_bool_true.
1171  */
FN(UNION,every_entry)1172 static isl_stat FN(UNION,every_entry)(void **entry, void *user)
1173 {
1174 	S(UNION,every_data) *data = user;
1175 	PW *pw = *entry;
1176 
1177 	data->res = data->test(pw, data->user);
1178 	if (data->res < 0 || !data->res)
1179 		return isl_stat_error;
1180 
1181 	return isl_stat_ok;
1182 }
1183 
1184 /* Does "test" succeed on every piecewise expression in "u"?
1185  */
FN(FN (UNION,every),BASE)1186 isl_bool FN(FN(UNION,every),BASE)(__isl_keep UNION *u,
1187 	isl_bool (*test)(__isl_keep PW *pw, void *user), void *user)
1188 {
1189 	S(UNION,every_data) data = { test, user };
1190 
1191 	data.res = isl_bool_true;
1192 	if (FN(UNION,foreach_inplace)(u, &FN(UNION,every_entry), &data) < 0 &&
1193 	    data.res == isl_bool_true)
1194 		return isl_bool_error;
1195 
1196 	return data.res;
1197 }
1198 
S(UNION,plain_is_equal_data)1199 S(UNION,plain_is_equal_data)
1200 {
1201 	UNION *u2;
1202 };
1203 
FN(UNION,plain_is_equal_el)1204 static isl_bool FN(UNION,plain_is_equal_el)(__isl_keep PW *pw, void *user)
1205 {
1206 	S(UNION,plain_is_equal_data) *data = user;
1207 	struct isl_hash_table_entry *entry;
1208 
1209 	entry = FN(UNION,find_part_entry)(data->u2, pw->dim, 0);
1210 	if (!entry)
1211 		return isl_bool_error;
1212 	if (entry == isl_hash_table_entry_none)
1213 		return isl_bool_false;
1214 
1215 	return FN(PW,plain_is_equal)(pw, entry->data);
1216 }
1217 
FN(UNION,plain_is_equal)1218 isl_bool FN(UNION,plain_is_equal)(__isl_keep UNION *u1, __isl_keep UNION *u2)
1219 {
1220 	S(UNION,plain_is_equal_data) data;
1221 	isl_size n1, n2;
1222 	isl_bool is_equal;
1223 
1224 	if (!u1 || !u2)
1225 		return isl_bool_error;
1226 	if (u1 == u2)
1227 		return isl_bool_true;
1228 	if (u1->table.n != u2->table.n)
1229 		return isl_bool_false;
1230 	n1 = FN(FN(UNION,n),BASE)(u1);
1231 	n2 = FN(FN(UNION,n),BASE)(u2);
1232 	if (n1 < 0 || n2 < 0)
1233 		return isl_bool_error;
1234 	if (n1 != n2)
1235 		return isl_bool_false;
1236 
1237 	u1 = FN(UNION,copy)(u1);
1238 	u2 = FN(UNION,copy)(u2);
1239 	u1 = FN(UNION,align_params)(u1, FN(UNION,get_space)(u2));
1240 	u2 = FN(UNION,align_params)(u2, FN(UNION,get_space)(u1));
1241 	if (!u1 || !u2)
1242 		goto error;
1243 
1244 	data.u2 = u2;
1245 	is_equal = FN(FN(UNION,every),BASE)(u1,
1246 					  &FN(UNION,plain_is_equal_el), &data);
1247 
1248 	FN(UNION,free)(u1);
1249 	FN(UNION,free)(u2);
1250 
1251 	return is_equal;
1252 error:
1253 	FN(UNION,free)(u1);
1254 	FN(UNION,free)(u2);
1255 	return isl_bool_error;
1256 }
1257 
1258 /* An isl_union_*_every_* callback that checks whether "pw"
1259  * does not involve any NaNs.
1260  */
FN(UNION,no_nan_el)1261 static isl_bool FN(UNION,no_nan_el)(__isl_keep PW *pw, void *user)
1262 {
1263 	return isl_bool_not(FN(PW,involves_nan)(pw));
1264 }
1265 
1266 /* Does "u" involve any NaNs?
1267  */
FN(UNION,involves_nan)1268 isl_bool FN(UNION,involves_nan)(__isl_keep UNION *u)
1269 {
1270 	isl_bool no_nan;
1271 
1272 	no_nan = FN(FN(UNION,every),BASE)(u, &FN(UNION,no_nan_el), NULL);
1273 
1274 	return isl_bool_not(no_nan);
1275 }
1276 
1277 /* Internal data structure for isl_union_*_drop_dims.
1278  * type, first and n are passed to isl_*_drop_dims.
1279  */
S(UNION,drop_dims_data)1280 S(UNION,drop_dims_data) {
1281 	enum isl_dim_type type;
1282 	unsigned first;
1283 	unsigned n;
1284 };
1285 
1286 /* Drop the parameters specified by "data" from "part" and return the result.
1287  */
FN(UNION,drop_dims_entry)1288 static __isl_give PART *FN(UNION,drop_dims_entry)(__isl_take PART *part,
1289 	void *user)
1290 {
1291 	S(UNION,drop_dims_data) *data = user;
1292 
1293 	return FN(PART,drop_dims)(part, data->type, data->first, data->n);
1294 }
1295 
1296 /* Drop the specified parameters from "u".
1297  * That is, type is required to be isl_dim_param.
1298  */
FN(UNION,drop_dims)1299 __isl_give UNION *FN(UNION,drop_dims)( __isl_take UNION *u,
1300 	enum isl_dim_type type, unsigned first, unsigned n)
1301 {
1302 	isl_space *space;
1303 	S(UNION,drop_dims_data) data = { type, first, n };
1304 	S(UNION,transform_control) control = {
1305 		.fn = &FN(UNION,drop_dims_entry),
1306 		.fn_user = &data,
1307 	};
1308 
1309 	if (!u)
1310 		return NULL;
1311 
1312 	if (type != isl_dim_param)
1313 		isl_die(FN(UNION,get_ctx)(u), isl_error_invalid,
1314 			"can only project out parameters",
1315 			return FN(UNION,free)(u));
1316 
1317 	space = FN(UNION,get_space)(u);
1318 	space = isl_space_drop_dims(space, type, first, n);
1319 	return FN(UNION,transform_space)(u, space, &control);
1320 }
1321 
1322 /* Internal data structure for isl_union_*_set_dim_name.
1323  * pos is the position of the parameter that needs to be renamed.
1324  * s is the new name.
1325  */
S(UNION,set_dim_name_data)1326 S(UNION,set_dim_name_data) {
1327 	unsigned pos;
1328 	const char *s;
1329 };
1330 
1331 /* Change the name of the parameter at position data->pos of "part" to data->s
1332  * and return the result.
1333  */
FN(UNION,set_dim_name_entry)1334 static __isl_give PART *FN(UNION,set_dim_name_entry)(__isl_take PART *part,
1335 	void *user)
1336 {
1337 	S(UNION,set_dim_name_data) *data = user;
1338 
1339 	return FN(PART,set_dim_name)(part, isl_dim_param, data->pos, data->s);
1340 }
1341 
1342 /* Change the name of the parameter at position "pos" to "s".
1343  * That is, type is required to be isl_dim_param.
1344  */
FN(UNION,set_dim_name)1345 __isl_give UNION *FN(UNION,set_dim_name)(__isl_take UNION *u,
1346 	enum isl_dim_type type, unsigned pos, const char *s)
1347 {
1348 	S(UNION,set_dim_name_data) data = { pos, s };
1349 	S(UNION,transform_control) control = {
1350 		.fn = &FN(UNION,set_dim_name_entry),
1351 		.fn_user = &data,
1352 	};
1353 	isl_space *space;
1354 
1355 	if (!u)
1356 		return NULL;
1357 
1358 	if (type != isl_dim_param)
1359 		isl_die(FN(UNION,get_ctx)(u), isl_error_invalid,
1360 			"can only set parameter names",
1361 			return FN(UNION,free)(u));
1362 
1363 	space = FN(UNION,get_space)(u);
1364 	space = isl_space_set_dim_name(space, type, pos, s);
1365 	return FN(UNION,transform_space)(u, space, &control);
1366 }
1367 
1368 /* Reset the user pointer on all identifiers of parameters and tuples
1369  * of the space of "part" and return the result.
1370  */
FN(UNION,reset_user_entry)1371 static __isl_give PART *FN(UNION,reset_user_entry)(__isl_take PART *part,
1372 	void *user)
1373 {
1374 	return FN(PART,reset_user)(part);
1375 }
1376 
1377 /* Reset the user pointer on all identifiers of parameters and tuples
1378  * of the spaces of "u".
1379  */
FN(UNION,reset_user)1380 __isl_give UNION *FN(UNION,reset_user)(__isl_take UNION *u)
1381 {
1382 	S(UNION,transform_control) control = {
1383 		.fn = &FN(UNION,reset_user_entry),
1384 	};
1385 	isl_space *space;
1386 
1387 	space = FN(UNION,get_space)(u);
1388 	space = isl_space_reset_user(space);
1389 	return FN(UNION,transform_space)(u, space, &control);
1390 }
1391 
1392 /* Add the base expression held by "entry" to "list".
1393  */
FN(UNION,add_to_list)1394 static isl_stat FN(UNION,add_to_list)(void **entry, void *user)
1395 {
1396 	PW *pw = *entry;
1397 	LIST(PART) **list = user;
1398 
1399 	*list = FN(LIST(PART),add)(*list, FN(PART,copy)(pw));
1400 	if (!*list)
1401 		return isl_stat_error;
1402 
1403 	return isl_stat_ok;
1404 }
1405 
1406 /* Return a list containing all the base expressions in "u".
1407  *
1408  * First construct a list of the appropriate size and
1409  * then add all the elements.
1410  */
LIST(PART)1411 __isl_give LIST(PART) *FN(FN(UNION,get),LIST(BASE))(__isl_keep UNION *u)
1412 {
1413 	isl_size n;
1414 	LIST(PART) *list;
1415 
1416 	if (!u)
1417 		return NULL;
1418 	n = FN(FN(UNION,n),BASE)(u);
1419 	if (n < 0)
1420 		return NULL;
1421 	list = FN(LIST(PART),alloc)(FN(UNION,get_ctx(u)), n);
1422 	if (FN(UNION,foreach_inplace)(u, &FN(UNION,add_to_list), &list) < 0)
1423 		return FN(LIST(PART),free)(list);
1424 
1425 	return list;
1426 }
1427