2 * Copyright 2011 Leiden University. All rights reserved.
3 * Copyright 2014 Ecole Normale Superieure. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials provided
15 * with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY LEIDEN UNIVERSITY ''AS IS'' AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL LEIDEN UNIVERSITY OR
21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * The views and conclusions contained in the software and documentation
30 * are those of the authors and should not be interpreted as
31 * representing official policies, either expressed or implied, of
43 /* A pet_context represents the context in which a pet_expr
44 * in converted to an affine expression.
46 * "domain" prescribes the domain of the affine expressions.
48 * "assignments" maps variable names to their currently known values.
49 * Internally, the domains of the values may be equal to some prefix
50 * of the space of "domain", but the domains are updated to be
51 * equal to the space of "domain" before passing them to the user.
52 * If a variable has been assigned an unknown value (possibly because
53 * it may be assigned a different expression in each iteration) or a value
54 * that is not an affine expression, then the corresponding isl_pw_aff
57 * If "allow_nested" is set, then the affine expression created
58 * in this context may involve new parameters that encode a pet_expr.
64 isl_id_to_pw_aff
*assignments
;
68 /* Create a pet_context with the given domain, assignments,
69 * and value for allow_nested.
71 static __isl_give pet_context
*context_alloc(__isl_take isl_set
*domain
,
72 __isl_take isl_id_to_pw_aff
*assignments
, int allow_nested
)
76 if (!domain
|| !assignments
)
79 pc
= isl_calloc_type(isl_set_get_ctx(domain
), struct pet_context
);
85 pc
->assignments
= assignments
;
86 pc
->allow_nested
= allow_nested
;
91 isl_id_to_pw_aff_free(assignments
);
95 /* Create a pet_context with the given domain.
97 * Initially, there are no assigned values and parameters that
98 * encode a pet_expr are not allowed.
100 __isl_give pet_context
*pet_context_alloc(__isl_take isl_set
*domain
)
102 isl_id_to_pw_aff
*assignments
;
107 assignments
= isl_id_to_pw_aff_alloc(isl_set_get_ctx(domain
), 0);;
108 return context_alloc(domain
, assignments
, 0);
111 /* Return an independent duplicate of "pc".
113 static __isl_give pet_context
*pet_context_dup(__isl_keep pet_context
*pc
)
120 dup
= context_alloc(isl_set_copy(pc
->domain
),
121 isl_id_to_pw_aff_copy(pc
->assignments
),
127 /* Return a pet_context that is equal to "pc" and that has only one reference.
129 static __isl_give pet_context
*pet_context_cow(__isl_take pet_context
*pc
)
137 return pet_context_dup(pc
);
140 /* Return an extra reference to "pc".
142 __isl_give pet_context
*pet_context_copy(__isl_keep pet_context
*pc
)
151 /* Free a reference to "pc" and return NULL.
153 __isl_null pet_context
*pet_context_free(__isl_take pet_context
*pc
)
160 isl_set_free(pc
->domain
);
161 isl_id_to_pw_aff_free(pc
->assignments
);
166 /* Return the isl_ctx in which "pc" was created.
168 isl_ctx
*pet_context_get_ctx(__isl_keep pet_context
*pc
)
170 return pc
? isl_set_get_ctx(pc
->domain
) : NULL
;
173 /* Return the domain of "pc".
175 __isl_give isl_set
*pet_context_get_domain(__isl_keep pet_context
*pc
)
179 return isl_set_copy(pc
->domain
);
182 /* Return the domain space of "pc".
184 * The domain of "pc" may have constraints involving parameters
185 * that encode a pet_expr. These parameters are not relevant
186 * outside this domain. Furthermore, they need to be resolved
187 * from the final result, so we do not want to propagate them needlessly.
189 __isl_give isl_space
*pet_context_get_space(__isl_keep pet_context
*pc
)
196 space
= isl_set_get_space(pc
->domain
);
197 space
= pet_nested_remove_from_space(space
);
201 /* Return the dimension of the domain space of "pc".
203 unsigned pet_context_dim(__isl_keep pet_context
*pc
)
207 return isl_set_dim(pc
->domain
, isl_dim_set
);
210 /* Return the assignments of "pc".
212 __isl_give isl_id_to_pw_aff
*pet_context_get_assignments(
213 __isl_keep pet_context
*pc
)
217 return isl_id_to_pw_aff_copy(pc
->assignments
);
220 /* Is "id" assigned any value in "pc"?
222 int pet_context_is_assigned(__isl_keep pet_context
*pc
, __isl_keep isl_id
*id
)
226 return isl_id_to_pw_aff_has(pc
->assignments
, id
);
229 /* Return the value assigned to "id" in "pc".
231 * Some dimensions may have been added to pc->domain after the value
232 * associated to "id" was added. We therefore need to adjust the domain
233 * of the stored value to match pc->domain by adding the missing
236 __isl_give isl_pw_aff
*pet_context_get_value(__isl_keep pet_context
*pc
,
237 __isl_take isl_id
*id
)
246 pa
= isl_id_to_pw_aff_get(pc
->assignments
, id
);
247 dim
= isl_pw_aff_dim(pa
, isl_dim_in
);
248 if (dim
== isl_set_dim(pc
->domain
, isl_dim_set
))
251 ma
= pet_prefix_projection(pet_context_get_space(pc
), dim
);
252 pa
= isl_pw_aff_pullback_multi_aff(pa
, ma
);
260 /* Assign the value "value" to "id" in "pc", replacing the previously
261 * assigned value, if any.
263 __isl_give pet_context
*pet_context_set_value(__isl_take pet_context
*pc
,
264 __isl_take isl_id
*id
, isl_pw_aff
*value
)
266 pc
= pet_context_cow(pc
);
269 pc
->assignments
= isl_id_to_pw_aff_set(pc
->assignments
, id
, value
);
270 if (!pc
->assignments
)
271 return pet_context_free(pc
);
275 isl_pw_aff_free(value
);
279 /* Remove any assignment to "id" in "pc".
281 __isl_give pet_context
*pet_context_clear_value(__isl_keep pet_context
*pc
,
282 __isl_take isl_id
*id
)
284 pc
= pet_context_cow(pc
);
287 pc
->assignments
= isl_id_to_pw_aff_drop(pc
->assignments
, id
);
288 if (!pc
->assignments
)
289 return pet_context_free(pc
);
296 /* Return a piecewise affine expression defined on the specified domain
297 * that represents NaN.
299 static __isl_give isl_pw_aff
*nan_on_domain(__isl_take isl_space
*space
)
301 return isl_pw_aff_nan_on_domain(isl_local_space_from_space(space
));
304 /* Assign the value NaN to "id" in "pc", marked it as having an unknown
307 __isl_give pet_context
*pet_context_mark_unknown(__isl_take pet_context
*pc
,
308 __isl_take isl_id
*id
)
312 pa
= nan_on_domain(pet_context_get_space(pc
));
313 pc
= pet_context_set_value(pc
, id
, pa
);
318 /* Are affine expressions created in this context allowed to involve
319 * parameters that encode a pet_expr?
321 int pet_context_allow_nesting(__isl_keep pet_context
*pc
)
325 return pc
->allow_nested
;
328 /* Allow affine expressions created in this context to involve
329 * parameters that encode a pet_expr based on the value of "allow_nested".
331 __isl_give pet_context
*pet_context_set_allow_nested(__isl_take pet_context
*pc
,
336 if (pc
->allow_nested
== allow_nested
)
338 pc
= pet_context_cow(pc
);
341 pc
->allow_nested
= allow_nested
;
345 /* If the access expression "expr" writes to a (non-virtual) scalar,
346 * then mark the scalar as having an unknown value in "pc".
348 static int clear_write(__isl_keep pet_expr
*expr
, void *user
)
351 pet_context
**pc
= (pet_context
**) user
;
353 if (!pet_expr_access_is_write(expr
))
355 if (!pet_expr_is_scalar_access(expr
))
358 id
= pet_expr_access_get_id(expr
);
359 if (isl_id_get_user(id
))
360 *pc
= pet_context_mark_unknown(*pc
, id
);
367 /* Look for any writes to scalar variables in "expr" and
368 * mark them as having an unknown value in "pc".
370 __isl_give pet_context
*pet_context_clear_writes_in_expr(
371 __isl_take pet_context
*pc
, __isl_keep pet_expr
*expr
)
373 if (pet_expr_foreach_access_expr(expr
, &clear_write
, &pc
) < 0)
374 pc
= pet_context_free(pc
);
379 /* Look for any writes to scalar variables in "tree" and
380 * mark them as having an unknown value in "pc".
382 __isl_give pet_context
*pet_context_clear_writes_in_tree(
383 __isl_take pet_context
*pc
, __isl_keep pet_tree
*tree
)
385 if (pet_tree_foreach_access_expr(tree
, &clear_write
, &pc
) < 0)
386 pc
= pet_context_free(pc
);
391 /* Given an access expression, check if it reads a scalar variable
392 * that has a known value in "pc".
393 * If so, then replace the access by an access to that value.
395 static __isl_give pet_expr
*access_plug_in_affine_read(
396 __isl_take pet_expr
*expr
, void *user
)
398 pet_context
*pc
= user
;
401 if (pet_expr_access_is_write(expr
))
403 if (!pet_expr_is_scalar_access(expr
))
406 pa
= pet_expr_extract_affine(expr
, pc
);
408 return pet_expr_free(expr
);
409 if (isl_pw_aff_involves_nan(pa
)) {
415 expr
= pet_expr_from_index(isl_multi_pw_aff_from_pw_aff(pa
));
420 /* Replace every read access in "expr" to a scalar variable
421 * that has a known value in "pc" by that known value.
423 static __isl_give pet_expr
*plug_in_affine_read(__isl_take pet_expr
*expr
,
424 __isl_keep pet_context
*pc
)
426 return pet_expr_map_access(expr
, &access_plug_in_affine_read
, pc
);
429 /* Evaluate "expr" in the context of "pc".
431 * In particular, we first make sure that all the access expressions
432 * inside "expr" have the same domain as "pc".
433 * Then, we plug in affine expressions for scalar reads and
434 * plug in the arguments of all access expressions in "expr".
436 __isl_give pet_expr
*pet_context_evaluate_expr(__isl_keep pet_context
*pc
,
437 __isl_take pet_expr
*expr
)
439 expr
= pet_expr_insert_domain(expr
, pet_context_get_space(pc
));
440 expr
= plug_in_affine_read(expr
, pc
);
441 return pet_expr_plug_in_args(expr
, pc
);
444 /* Add an unbounded inner dimension "id" to pc->domain.
446 * The assignments are not adjusted here and therefore keep
447 * their original domain. These domains need to be adjusted before
448 * these assigned values can be used. This is taken care of by
449 * pet_context_get_value.
451 static __isl_give pet_context
*extend_domain(__isl_take pet_context
*pc
,
452 __isl_take isl_id
*id
)
456 pc
= pet_context_cow(pc
);
460 pos
= pet_context_dim(pc
);
461 pc
->domain
= isl_set_add_dims(pc
->domain
, isl_dim_set
, 1);
462 pc
->domain
= isl_set_set_dim_id(pc
->domain
, isl_dim_set
, pos
, id
);
464 return pet_context_free(pc
);
468 pet_context_free(pc
);
473 /* Add an unbounded inner iterator "id" to pc->domain.
474 * Additionally, mark the variable "id" as having the value of this
475 * new inner iterator.
477 __isl_give pet_context
*pet_context_add_inner_iterator(
478 __isl_take pet_context
*pc
, __isl_take isl_id
*id
)
489 pos
= pet_context_dim(pc
);
490 pc
= extend_domain(pc
, isl_id_copy(id
));
494 space
= pet_context_get_space(pc
);
495 ls
= isl_local_space_from_space(space
);
496 aff
= isl_aff_var_on_domain(ls
, isl_dim_set
, pos
);
497 pa
= isl_pw_aff_from_aff(aff
);
499 pc
= pet_context_set_value(pc
, id
, pa
);
503 pet_context_free(pc
);
508 /* Add an inner iterator to pc->domain.
509 * In particular, extend the domain with an inner loop { [t] : t >= 0 }.
511 __isl_give pet_context
*pet_context_add_infinite_loop(
512 __isl_take pet_context
*pc
)
521 dim
= pet_context_dim(pc
);
522 ctx
= pet_context_get_ctx(pc
);
523 id
= isl_id_alloc(ctx
, "t", NULL
);
524 pc
= extend_domain(pc
, id
);
525 pc
= pet_context_cow(pc
);
528 pc
->domain
= isl_set_lower_bound_si(pc
->domain
, isl_dim_set
, dim
, 0);
530 return pet_context_free(pc
);
535 /* Internal data structure for preimage_domain.
537 * "ma" is the function under which the preimage should be computed.
538 * "assignments" collects the results.
540 struct pet_preimage_domain_data
{
542 isl_id_to_pw_aff
*assignments
;
545 /* Add the assignment to "key" of the preimage of "val" under data->ma
546 * to data->assignments.
548 * Some dimensions may have been added to the domain of the enclosing
549 * pet_context after the value "val" was added. We therefore need to
550 * adjust the domain of "val" to match the range of data->ma (which
551 * in turn matches the domain of the pet_context), by adding the missing
554 static int preimage_domain_pair(__isl_take isl_id
*key
,
555 __isl_take isl_pw_aff
*val
, void *user
)
557 struct pet_preimage_domain_data
*data
= user
;
561 ma
= isl_multi_aff_copy(data
->ma
);
563 dim
= isl_pw_aff_dim(val
, isl_dim_in
);
564 if (dim
!= isl_multi_aff_dim(data
->ma
, isl_dim_out
)) {
567 space
= isl_multi_aff_get_space(data
->ma
);
568 space
= isl_space_range(space
);
569 proj
= pet_prefix_projection(space
, dim
);
570 ma
= isl_multi_aff_pullback_multi_aff(proj
, ma
);
573 val
= isl_pw_aff_pullback_multi_aff(val
, ma
);
574 data
->assignments
= isl_id_to_pw_aff_set(data
->assignments
, key
, val
);
579 /* Compute the preimage of "assignments" under the function represented by "ma".
580 * In other words, plug in "ma" in the domains of the assigned values.
582 static __isl_give isl_id_to_pw_aff
*preimage_domain(
583 __isl_take isl_id_to_pw_aff
*assignments
, __isl_keep isl_multi_aff
*ma
)
585 struct pet_preimage_domain_data data
= { ma
};
588 ctx
= isl_id_to_pw_aff_get_ctx(assignments
);
589 data
.assignments
= isl_id_to_pw_aff_alloc(ctx
, 0);
590 if (isl_id_to_pw_aff_foreach(assignments
, &preimage_domain_pair
,
592 data
.assignments
= isl_id_to_pw_aff_free(data
.assignments
);
593 isl_id_to_pw_aff_free(assignments
);
595 return data
.assignments
;
598 /* Compute the preimage of "pc" under the function represented by "ma".
599 * In other words, plug in "ma" in the domain of "pc".
601 __isl_give pet_context
*pet_context_preimage_domain(__isl_take pet_context
*pc
,
602 __isl_keep isl_multi_aff
*ma
)
604 pc
= pet_context_cow(pc
);
607 pc
->domain
= isl_set_preimage_multi_aff(pc
->domain
,
608 isl_multi_aff_copy(ma
));
609 pc
->assignments
= preimage_domain(pc
->assignments
, ma
);
610 if (!pc
->domain
|| !pc
->assignments
)
611 return pet_context_free(pc
);
615 /* Add the constraints of "set" to the domain of "pc".
617 __isl_give pet_context
*pet_context_intersect_domain(__isl_take pet_context
*pc
,
618 __isl_take isl_set
*set
)
620 pc
= pet_context_cow(pc
);
623 pc
->domain
= isl_set_intersect(pc
->domain
, set
);
625 return pet_context_free(pc
);
628 pet_context_free(pc
);
633 void pet_context_dump(__isl_keep pet_context
*pc
)
637 fprintf(stderr
, "domain: ");
638 isl_set_dump(pc
->domain
);
639 fprintf(stderr
, "assignments: ");
640 isl_id_to_pw_aff_dump(pc
->assignments
);
641 fprintf(stderr
, "nesting allowed: %d\n", pc
->allow_nested
);