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 of "pc" in a form that is suitable
183 * for use as a gist context.
184 * In particular, remove all references to nested expression parameters
185 * so that they do not get introduced in the gisted expression.
187 __isl_give isl_set
*pet_context_get_gist_domain(__isl_keep pet_context
*pc
)
191 domain
= pet_context_get_domain(pc
);
192 domain
= pet_nested_remove_from_set(domain
);
196 /* Return the domain space of "pc".
198 * The domain of "pc" may have constraints involving parameters
199 * that encode a pet_expr. These parameters are not relevant
200 * outside this domain. Furthermore, they need to be resolved
201 * from the final result, so we do not want to propagate them needlessly.
203 __isl_give isl_space
*pet_context_get_space(__isl_keep pet_context
*pc
)
210 space
= isl_set_get_space(pc
->domain
);
211 space
= pet_nested_remove_from_space(space
);
215 /* Return the dimension of the domain space of "pc".
217 unsigned pet_context_dim(__isl_keep pet_context
*pc
)
221 return isl_set_dim(pc
->domain
, isl_dim_set
);
224 /* Return the assignments of "pc".
226 __isl_give isl_id_to_pw_aff
*pet_context_get_assignments(
227 __isl_keep pet_context
*pc
)
231 return isl_id_to_pw_aff_copy(pc
->assignments
);
234 /* Is "id" assigned any value in "pc"?
236 int pet_context_is_assigned(__isl_keep pet_context
*pc
, __isl_keep isl_id
*id
)
240 return isl_id_to_pw_aff_has(pc
->assignments
, id
);
243 /* Return the value assigned to "id" in "pc".
245 * Some dimensions may have been added to pc->domain after the value
246 * associated to "id" was added. We therefore need to adjust the domain
247 * of the stored value to match pc->domain by adding the missing
250 __isl_give isl_pw_aff
*pet_context_get_value(__isl_keep pet_context
*pc
,
251 __isl_take isl_id
*id
)
260 pa
= isl_id_to_pw_aff_get(pc
->assignments
, id
);
261 dim
= isl_pw_aff_dim(pa
, isl_dim_in
);
262 if (dim
== isl_set_dim(pc
->domain
, isl_dim_set
))
265 ma
= pet_prefix_projection(pet_context_get_space(pc
), dim
);
266 pa
= isl_pw_aff_pullback_multi_aff(pa
, ma
);
274 /* Assign the value "value" to "id" in "pc", replacing the previously
275 * assigned value, if any.
277 __isl_give pet_context
*pet_context_set_value(__isl_take pet_context
*pc
,
278 __isl_take isl_id
*id
, isl_pw_aff
*value
)
280 pc
= pet_context_cow(pc
);
283 pc
->assignments
= isl_id_to_pw_aff_set(pc
->assignments
, id
, value
);
284 if (!pc
->assignments
)
285 return pet_context_free(pc
);
289 isl_pw_aff_free(value
);
293 /* Remove any assignment to "id" in "pc".
295 __isl_give pet_context
*pet_context_clear_value(__isl_keep pet_context
*pc
,
296 __isl_take isl_id
*id
)
298 pc
= pet_context_cow(pc
);
301 pc
->assignments
= isl_id_to_pw_aff_drop(pc
->assignments
, id
);
302 if (!pc
->assignments
)
303 return pet_context_free(pc
);
310 /* Return a piecewise affine expression defined on the specified domain
311 * that represents NaN.
313 static __isl_give isl_pw_aff
*nan_on_domain(__isl_take isl_space
*space
)
315 return isl_pw_aff_nan_on_domain(isl_local_space_from_space(space
));
318 /* Assign the value NaN to "id" in "pc", marked it as having an unknown
321 __isl_give pet_context
*pet_context_mark_unknown(__isl_take pet_context
*pc
,
322 __isl_take isl_id
*id
)
326 pa
= nan_on_domain(pet_context_get_space(pc
));
327 pc
= pet_context_set_value(pc
, id
, pa
);
332 /* Are affine expressions created in this context allowed to involve
333 * parameters that encode a pet_expr?
335 int pet_context_allow_nesting(__isl_keep pet_context
*pc
)
339 return pc
->allow_nested
;
342 /* Allow affine expressions created in this context to involve
343 * parameters that encode a pet_expr based on the value of "allow_nested".
345 __isl_give pet_context
*pet_context_set_allow_nested(__isl_take pet_context
*pc
,
350 if (pc
->allow_nested
== allow_nested
)
352 pc
= pet_context_cow(pc
);
355 pc
->allow_nested
= allow_nested
;
359 /* If the access expression "expr" writes to a (non-virtual) scalar,
360 * then mark the scalar as having an unknown value in "pc".
362 static int clear_write(__isl_keep pet_expr
*expr
, void *user
)
365 pet_context
**pc
= (pet_context
**) user
;
367 if (!pet_expr_access_is_write(expr
))
369 if (!pet_expr_is_scalar_access(expr
))
372 id
= pet_expr_access_get_id(expr
);
373 if (isl_id_get_user(id
))
374 *pc
= pet_context_mark_unknown(*pc
, id
);
381 /* Look for any writes to scalar variables in "expr" and
382 * mark them as having an unknown value in "pc".
384 __isl_give pet_context
*pet_context_clear_writes_in_expr(
385 __isl_take pet_context
*pc
, __isl_keep pet_expr
*expr
)
387 if (pet_expr_foreach_access_expr(expr
, &clear_write
, &pc
) < 0)
388 pc
= pet_context_free(pc
);
393 /* Look for any writes to scalar variables in "tree" and
394 * mark them as having an unknown value in "pc".
396 __isl_give pet_context
*pet_context_clear_writes_in_tree(
397 __isl_take pet_context
*pc
, __isl_keep pet_tree
*tree
)
399 if (pet_tree_foreach_access_expr(tree
, &clear_write
, &pc
) < 0)
400 pc
= pet_context_free(pc
);
405 /* Internal data structure for pet_context_add_parameters.
407 * "pc" is the context that is being updated.
408 * "get_array_size" is a callback function that can be used to determine
409 * the size of the array that is accessed by a given access expression.
410 * "user" is the user data for this callback.
412 struct pet_context_add_parameter_data
{
414 __isl_give pet_expr
*(*get_array_size
)(__isl_keep pet_expr
*access
,
419 /* Given an access expression "expr", add a parameter assignment to data->pc
420 * to the variable being accessed, provided it is a read from an integer
422 * If an array is being accesed, then recursively call the function
423 * on each of the access expressions in the size expression of the array.
425 static int add_parameter(__isl_keep pet_expr
*expr
, void *user
)
427 struct pet_context_add_parameter_data
*data
= user
;
435 if (!pet_expr_is_scalar_access(expr
)) {
436 pet_expr
*size
= data
->get_array_size(expr
, data
->user
);
437 if (pet_expr_foreach_access_expr(size
,
438 &add_parameter
, data
) < 0)
439 data
->pc
= pet_context_free(data
->pc
);
443 if (!pet_expr_access_is_read(expr
))
445 if (pet_expr_get_type_size(expr
) == 0)
448 id
= pet_expr_access_get_id(expr
);
449 if (pet_context_is_assigned(data
->pc
, id
)) {
454 space
= pet_context_get_space(data
->pc
);
455 pos
= isl_space_find_dim_by_id(space
, isl_dim_param
, id
);
457 pos
= isl_space_dim(space
, isl_dim_param
);
458 space
= isl_space_add_dims(space
, isl_dim_param
, 1);
459 space
= isl_space_set_dim_id(space
, isl_dim_param
, pos
,
463 ls
= isl_local_space_from_space(space
);
464 aff
= isl_aff_var_on_domain(ls
, isl_dim_param
, pos
);
465 pa
= isl_pw_aff_from_aff(aff
);
466 data
->pc
= pet_context_set_value(data
->pc
, id
, pa
);
471 /* Add an assignment to "pc" for each parameter in "tree".
472 * "get_array_size" is a callback function that can be used to determine
473 * the size of the array that is accessed by a given access expression.
475 * We initially treat as parameters any integer variable that is read
476 * anywhere in "tree" or in any of the size expressions for any of
477 * the arrays accessed in "tree".
478 * Then we remove from those variables that are written anywhere
481 __isl_give pet_context
*pet_context_add_parameters(__isl_take pet_context
*pc
,
482 __isl_keep pet_tree
*tree
,
483 __isl_give pet_expr
*(*get_array_size
)(__isl_keep pet_expr
*access
,
484 void *user
), void *user
)
486 struct pet_context_add_parameter_data data
;
489 data
.get_array_size
= get_array_size
;
491 if (pet_tree_foreach_access_expr(tree
, &add_parameter
, &data
) < 0)
492 data
.pc
= pet_context_free(data
.pc
);
494 data
.pc
= pet_context_clear_writes_in_tree(data
.pc
, tree
);
499 /* Given an access expression, check if it reads a scalar variable
500 * that has a known value in "pc".
501 * If so, then replace the access by an access to that value.
503 static __isl_give pet_expr
*access_plug_in_affine_read(
504 __isl_take pet_expr
*expr
, void *user
)
506 pet_context
*pc
= user
;
509 if (pet_expr_access_is_write(expr
))
511 if (!pet_expr_is_scalar_access(expr
))
514 pa
= pet_expr_extract_affine(expr
, pc
);
516 return pet_expr_free(expr
);
517 if (isl_pw_aff_involves_nan(pa
)) {
523 expr
= pet_expr_from_index(isl_multi_pw_aff_from_pw_aff(pa
));
528 /* Replace every read access in "expr" to a scalar variable
529 * that has a known value in "pc" by that known value.
531 static __isl_give pet_expr
*plug_in_affine_read(__isl_take pet_expr
*expr
,
532 __isl_keep pet_context
*pc
)
534 return pet_expr_map_access(expr
, &access_plug_in_affine_read
, pc
);
537 /* Evaluate "expr" in the context of "pc".
539 * In particular, we first make sure that all the access expressions
540 * inside "expr" have the same domain as "pc".
541 * Then, we plug in affine expressions for scalar reads and
542 * plug in the arguments of all access expressions in "expr".
544 __isl_give pet_expr
*pet_context_evaluate_expr(__isl_keep pet_context
*pc
,
545 __isl_take pet_expr
*expr
)
547 expr
= pet_expr_insert_domain(expr
, pet_context_get_space(pc
));
548 expr
= plug_in_affine_read(expr
, pc
);
549 return pet_expr_plug_in_args(expr
, pc
);
552 /* Add an unbounded inner dimension "id" to pc->domain.
554 * The assignments are not adjusted here and therefore keep
555 * their original domain. These domains need to be adjusted before
556 * these assigned values can be used. This is taken care of by
557 * pet_context_get_value.
559 static __isl_give pet_context
*extend_domain(__isl_take pet_context
*pc
,
560 __isl_take isl_id
*id
)
564 pc
= pet_context_cow(pc
);
568 pos
= pet_context_dim(pc
);
569 pc
->domain
= isl_set_add_dims(pc
->domain
, isl_dim_set
, 1);
570 pc
->domain
= isl_set_set_dim_id(pc
->domain
, isl_dim_set
, pos
, id
);
572 return pet_context_free(pc
);
576 pet_context_free(pc
);
581 /* Add an unbounded inner iterator "id" to pc->domain.
582 * Additionally, mark the variable "id" as having the value of this
583 * new inner iterator.
585 __isl_give pet_context
*pet_context_add_inner_iterator(
586 __isl_take pet_context
*pc
, __isl_take isl_id
*id
)
597 pos
= pet_context_dim(pc
);
598 pc
= extend_domain(pc
, isl_id_copy(id
));
602 space
= pet_context_get_space(pc
);
603 ls
= isl_local_space_from_space(space
);
604 aff
= isl_aff_var_on_domain(ls
, isl_dim_set
, pos
);
605 pa
= isl_pw_aff_from_aff(aff
);
607 pc
= pet_context_set_value(pc
, id
, pa
);
611 pet_context_free(pc
);
616 /* Add an inner iterator to pc->domain.
617 * In particular, extend the domain with an inner loop { [t] : t >= 0 }.
619 __isl_give pet_context
*pet_context_add_infinite_loop(
620 __isl_take pet_context
*pc
)
629 dim
= pet_context_dim(pc
);
630 ctx
= pet_context_get_ctx(pc
);
631 id
= isl_id_alloc(ctx
, "t", NULL
);
632 pc
= extend_domain(pc
, id
);
633 pc
= pet_context_cow(pc
);
636 pc
->domain
= isl_set_lower_bound_si(pc
->domain
, isl_dim_set
, dim
, 0);
638 return pet_context_free(pc
);
643 /* Internal data structure for preimage_domain.
645 * "ma" is the function under which the preimage should be computed.
646 * "assignments" collects the results.
648 struct pet_preimage_domain_data
{
650 isl_id_to_pw_aff
*assignments
;
653 /* Add the assignment to "key" of the preimage of "val" under data->ma
654 * to data->assignments.
656 * Some dimensions may have been added to the domain of the enclosing
657 * pet_context after the value "val" was added. We therefore need to
658 * adjust the domain of "val" to match the range of data->ma (which
659 * in turn matches the domain of the pet_context), by adding the missing
662 static int preimage_domain_pair(__isl_take isl_id
*key
,
663 __isl_take isl_pw_aff
*val
, void *user
)
665 struct pet_preimage_domain_data
*data
= user
;
669 ma
= isl_multi_aff_copy(data
->ma
);
671 dim
= isl_pw_aff_dim(val
, isl_dim_in
);
672 if (dim
!= isl_multi_aff_dim(data
->ma
, isl_dim_out
)) {
675 space
= isl_multi_aff_get_space(data
->ma
);
676 space
= isl_space_range(space
);
677 proj
= pet_prefix_projection(space
, dim
);
678 ma
= isl_multi_aff_pullback_multi_aff(proj
, ma
);
681 val
= isl_pw_aff_pullback_multi_aff(val
, ma
);
682 data
->assignments
= isl_id_to_pw_aff_set(data
->assignments
, key
, val
);
687 /* Compute the preimage of "assignments" under the function represented by "ma".
688 * In other words, plug in "ma" in the domains of the assigned values.
690 static __isl_give isl_id_to_pw_aff
*preimage_domain(
691 __isl_take isl_id_to_pw_aff
*assignments
, __isl_keep isl_multi_aff
*ma
)
693 struct pet_preimage_domain_data data
= { ma
};
696 ctx
= isl_id_to_pw_aff_get_ctx(assignments
);
697 data
.assignments
= isl_id_to_pw_aff_alloc(ctx
, 0);
698 if (isl_id_to_pw_aff_foreach(assignments
, &preimage_domain_pair
,
700 data
.assignments
= isl_id_to_pw_aff_free(data
.assignments
);
701 isl_id_to_pw_aff_free(assignments
);
703 return data
.assignments
;
706 /* Compute the preimage of "pc" under the function represented by "ma".
707 * In other words, plug in "ma" in the domain of "pc".
709 __isl_give pet_context
*pet_context_preimage_domain(__isl_take pet_context
*pc
,
710 __isl_keep isl_multi_aff
*ma
)
712 pc
= pet_context_cow(pc
);
715 pc
->domain
= isl_set_preimage_multi_aff(pc
->domain
,
716 isl_multi_aff_copy(ma
));
717 pc
->assignments
= preimage_domain(pc
->assignments
, ma
);
718 if (!pc
->domain
|| !pc
->assignments
)
719 return pet_context_free(pc
);
723 /* Add the constraints of "set" to the domain of "pc".
725 __isl_give pet_context
*pet_context_intersect_domain(__isl_take pet_context
*pc
,
726 __isl_take isl_set
*set
)
728 pc
= pet_context_cow(pc
);
731 pc
->domain
= isl_set_intersect(pc
->domain
, set
);
733 return pet_context_free(pc
);
736 pet_context_free(pc
);
741 void pet_context_dump(__isl_keep pet_context
*pc
)
745 fprintf(stderr
, "domain: ");
746 isl_set_dump(pc
->domain
);
747 fprintf(stderr
, "assignments: ");
748 isl_id_to_pw_aff_dump(pc
->assignments
);
749 fprintf(stderr
, "nesting allowed: %d\n", pc
->allow_nested
);