2 * Copyright 2011 Leiden University. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following
13 * disclaimer in the documentation and/or other materials provided
14 * with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY LEIDEN UNIVERSITY ''AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL LEIDEN UNIVERSITY OR
20 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
23 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * The views and conclusions contained in the software and documentation
29 * are those of the authors and should not be interpreted as
30 * representing official policies, either expressed or implied, of
34 #include <isl/constraint.h>
35 #include <isl/union_set.h>
39 #define ARRAY_SIZE(array) (sizeof(array)/sizeof(*array))
41 static char *type_str
[] = {
42 [pet_expr_access
] = "access",
43 [pet_expr_call
] = "call",
44 [pet_expr_double
] = "double",
45 [pet_expr_unary
] = "unary",
46 [pet_expr_binary
] = "binary",
47 [pet_expr_ternary
] = "ternary"
50 static char *op_str
[] = {
51 [pet_op_add_assign
] = "+=",
52 [pet_op_sub_assign
] = "-=",
53 [pet_op_mul_assign
] = "*=",
54 [pet_op_div_assign
] = "/=",
55 [pet_op_assign
] = "=",
67 const char *pet_op_str(enum pet_op_type op
)
72 const char *pet_type_str(enum pet_expr_type type
)
74 return type_str
[type
];
77 enum pet_op_type
pet_str_op(const char *str
)
81 for (i
= 0; i
< ARRAY_SIZE(op_str
); ++i
)
82 if (!strcmp(op_str
[i
], str
))
88 enum pet_expr_type
pet_str_type(const char *str
)
92 for (i
= 0; i
< ARRAY_SIZE(type_str
); ++i
)
93 if (!strcmp(type_str
[i
], str
))
99 /* Construct a pet_expr from an access relation.
100 * By default, it is considered to be a read access.
102 struct pet_expr
*pet_expr_from_access(__isl_take isl_map
*access
)
104 isl_ctx
*ctx
= isl_map_get_ctx(access
);
105 struct pet_expr
*expr
;
109 expr
= isl_calloc_type(ctx
, struct pet_expr
);
113 expr
->type
= pet_expr_access
;
114 expr
->acc
.access
= access
;
120 isl_map_free(access
);
124 /* Construct a unary pet_expr that performs "op" on "arg".
126 struct pet_expr
*pet_expr_new_unary(isl_ctx
*ctx
, enum pet_op_type op
,
127 struct pet_expr
*arg
)
129 struct pet_expr
*expr
;
133 expr
= isl_alloc_type(ctx
, struct pet_expr
);
137 expr
->type
= pet_expr_unary
;
140 expr
->args
= isl_calloc_array(ctx
, struct pet_expr
*, 1);
143 expr
->args
[pet_un_arg
] = arg
;
151 /* Construct a binary pet_expr that performs "op" on "lhs" and "rhs".
153 struct pet_expr
*pet_expr_new_binary(isl_ctx
*ctx
, enum pet_op_type op
,
154 struct pet_expr
*lhs
, struct pet_expr
*rhs
)
156 struct pet_expr
*expr
;
160 expr
= isl_alloc_type(ctx
, struct pet_expr
);
164 expr
->type
= pet_expr_binary
;
167 expr
->args
= isl_calloc_array(ctx
, struct pet_expr
*, 2);
170 expr
->args
[pet_bin_lhs
] = lhs
;
171 expr
->args
[pet_bin_rhs
] = rhs
;
180 /* Construct a ternary pet_expr that performs "cond" ? "lhs" : "rhs".
182 struct pet_expr
*pet_expr_new_ternary(isl_ctx
*ctx
, struct pet_expr
*cond
,
183 struct pet_expr
*lhs
, struct pet_expr
*rhs
)
185 struct pet_expr
*expr
;
187 if (!cond
|| !lhs
|| !rhs
)
189 expr
= isl_alloc_type(ctx
, struct pet_expr
);
193 expr
->type
= pet_expr_ternary
;
195 expr
->args
= isl_calloc_array(ctx
, struct pet_expr
*, 3);
198 expr
->args
[pet_ter_cond
] = cond
;
199 expr
->args
[pet_ter_true
] = lhs
;
200 expr
->args
[pet_ter_false
] = rhs
;
210 /* Construct a call pet_expr that calls function "name" with "n_arg"
211 * arguments. The caller is responsible for filling in the arguments.
213 struct pet_expr
*pet_expr_new_call(isl_ctx
*ctx
, const char *name
,
216 struct pet_expr
*expr
;
218 expr
= isl_alloc_type(ctx
, struct pet_expr
);
222 expr
->type
= pet_expr_call
;
224 expr
->name
= strdup(name
);
225 expr
->args
= isl_calloc_array(ctx
, struct pet_expr
*, n_arg
);
226 if (!expr
->name
|| !expr
->args
)
227 return pet_expr_free(expr
);
232 /* Construct a pet_expr that represents the double "d".
234 struct pet_expr
*pet_expr_new_double(isl_ctx
*ctx
, double d
)
236 struct pet_expr
*expr
;
238 expr
= isl_calloc_type(ctx
, struct pet_expr
);
242 expr
->type
= pet_expr_double
;
248 void *pet_expr_free(struct pet_expr
*expr
)
255 for (i
= 0; i
< expr
->n_arg
; ++i
)
256 pet_expr_free(expr
->args
[i
]);
259 switch (expr
->type
) {
260 case pet_expr_access
:
261 isl_map_free(expr
->acc
.access
);
266 case pet_expr_double
:
268 case pet_expr_binary
:
269 case pet_expr_ternary
:
277 static void expr_dump(struct pet_expr
*expr
, int indent
)
284 fprintf(stderr
, "%*s", indent
, "");
286 switch (expr
->type
) {
287 case pet_expr_double
:
288 fprintf(stderr
, "%g\n", expr
->d
);
290 case pet_expr_access
:
291 isl_map_dump(expr
->acc
.access
);
292 fprintf(stderr
, "%*sread: %d\n", indent
+ 2,
294 fprintf(stderr
, "%*swrite: %d\n", indent
+ 2,
295 "", expr
->acc
.write
);
296 for (i
= 0; i
< expr
->n_arg
; ++i
)
297 expr_dump(expr
->args
[i
], indent
+ 2);
300 fprintf(stderr
, "%s\n", op_str
[expr
->op
]);
301 expr_dump(expr
->args
[pet_un_arg
], indent
+ 2);
303 case pet_expr_binary
:
304 fprintf(stderr
, "%s\n", op_str
[expr
->op
]);
305 expr_dump(expr
->args
[pet_bin_lhs
], indent
+ 2);
306 expr_dump(expr
->args
[pet_bin_rhs
], indent
+ 2);
308 case pet_expr_ternary
:
309 fprintf(stderr
, "?:\n");
310 expr_dump(expr
->args
[pet_ter_cond
], indent
+ 2);
311 expr_dump(expr
->args
[pet_ter_true
], indent
+ 2);
312 expr_dump(expr
->args
[pet_ter_false
], indent
+ 2);
315 fprintf(stderr
, "%s/%d\n", expr
->name
, expr
->n_arg
);
316 for (i
= 0; i
< expr
->n_arg
; ++i
)
317 expr_dump(expr
->args
[i
], indent
+ 2);
322 void pet_expr_dump(struct pet_expr
*expr
)
327 /* Return 1 if the two pet_exprs are equivalent.
329 int pet_expr_is_equal(struct pet_expr
*expr1
, struct pet_expr
*expr2
)
333 if (!expr1
|| !expr2
)
336 if (expr1
->type
!= expr2
->type
)
338 if (expr1
->n_arg
!= expr2
->n_arg
)
340 for (i
= 0; i
< expr1
->n_arg
; ++i
)
341 if (!pet_expr_is_equal(expr1
->args
[i
], expr2
->args
[i
]))
343 switch (expr1
->type
) {
344 case pet_expr_double
:
345 if (expr1
->d
!= expr2
->d
)
348 case pet_expr_access
:
349 if (expr1
->acc
.read
!= expr2
->acc
.read
)
351 if (expr1
->acc
.write
!= expr2
->acc
.write
)
353 if (!expr1
->acc
.access
|| !expr2
->acc
.access
)
355 if (!isl_map_is_equal(expr1
->acc
.access
, expr2
->acc
.access
))
359 case pet_expr_binary
:
360 case pet_expr_ternary
:
361 if (expr1
->op
!= expr2
->op
)
365 if (strcmp(expr1
->name
, expr2
->name
))
373 /* Add extra conditions on the parameters to all access relations in "expr".
375 struct pet_expr
*pet_expr_restrict(struct pet_expr
*expr
,
376 __isl_take isl_set
*cond
)
383 for (i
= 0; i
< expr
->n_arg
; ++i
) {
384 expr
->args
[i
] = pet_expr_restrict(expr
->args
[i
],
390 if (expr
->type
== pet_expr_access
) {
391 expr
->acc
.access
= isl_map_intersect_params(expr
->acc
.access
,
393 if (!expr
->acc
.access
)
401 return pet_expr_free(expr
);
404 /* Modify all access relations in "expr" by calling "fn" on them.
406 static struct pet_expr
*expr_foreach_access(struct pet_expr
*expr
,
407 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*access
, void *user
),
415 for (i
= 0; i
< expr
->n_arg
; ++i
) {
416 expr
->args
[i
] = expr_foreach_access(expr
->args
[i
], fn
, user
);
418 return pet_expr_free(expr
);
421 if (expr
->type
== pet_expr_access
) {
422 expr
->acc
.access
= fn(expr
->acc
.access
, user
);
423 if (!expr
->acc
.access
)
424 return pet_expr_free(expr
);
430 /* Modify the given access relation based on the given iteration space
432 * If the access has any arguments then the domain of the access relation
433 * is a wrapped mapping from the iteration space to the space of
434 * argument values. We only need to change the domain of this wrapped
435 * mapping, so we extend the input transformation with an identity mapping
436 * on the space of argument values.
438 static __isl_give isl_map
*update_domain(__isl_take isl_map
*access
,
441 isl_map
*update
= user
;
444 update
= isl_map_copy(update
);
446 dim
= isl_map_get_space(access
);
447 dim
= isl_space_domain(dim
);
448 if (!isl_space_is_wrapping(dim
))
452 dim
= isl_space_unwrap(dim
);
453 dim
= isl_space_range(dim
);
454 dim
= isl_space_map_from_set(dim
);
455 id
= isl_map_identity(dim
);
456 update
= isl_map_product(update
, id
);
459 return isl_map_apply_domain(access
, update
);
462 /* Modify all access relations in "expr" based on the given iteration space
465 static struct pet_expr
*expr_update_domain(struct pet_expr
*expr
,
466 __isl_take isl_map
*update
)
468 expr
= expr_foreach_access(expr
, &update_domain
, update
);
469 isl_map_free(update
);
473 /* Construct a pet_stmt with given line number and statement
474 * number from a pet_expr.
475 * The initial iteration domain is the zero-dimensional universe.
476 * The domains of all access relations are modified to refer
477 * to the statement iteration domain.
479 struct pet_stmt
*pet_stmt_from_pet_expr(isl_ctx
*ctx
, int line
, int id
,
480 struct pet_expr
*expr
)
482 struct pet_stmt
*stmt
;
492 stmt
= isl_alloc_type(ctx
, struct pet_stmt
);
494 return pet_expr_free(expr
);
496 dim
= isl_space_set_alloc(ctx
, 0, 0);
497 snprintf(name
, sizeof(name
), "S_%d", id
);
498 dim
= isl_space_set_tuple_name(dim
, isl_dim_set
, name
);
499 dom
= isl_set_universe(isl_space_copy(dim
));
500 sched
= isl_map_from_domain(isl_set_copy(dom
));
502 dim
= isl_space_from_range(dim
);
503 add_name
= isl_map_universe(dim
);
504 expr
= expr_update_domain(expr
, add_name
);
508 stmt
->schedule
= sched
;
511 if (!stmt
->domain
|| !stmt
->schedule
|| !stmt
->body
)
512 return pet_stmt_free(stmt
);
517 void *pet_stmt_free(struct pet_stmt
*stmt
)
522 isl_set_free(stmt
->domain
);
523 isl_map_free(stmt
->schedule
);
524 pet_expr_free(stmt
->body
);
530 static void stmt_dump(struct pet_stmt
*stmt
, int indent
)
535 fprintf(stderr
, "%*s%d\n", indent
, "", stmt
->line
);
536 fprintf(stderr
, "%*s", indent
, "");
537 isl_set_dump(stmt
->domain
);
538 fprintf(stderr
, "%*s", indent
, "");
539 isl_map_dump(stmt
->schedule
);
540 expr_dump(stmt
->body
, indent
);
543 void pet_stmt_dump(struct pet_stmt
*stmt
)
548 void *pet_array_free(struct pet_array
*array
)
553 isl_set_free(array
->context
);
554 isl_set_free(array
->extent
);
555 isl_set_free(array
->value_bounds
);
556 free(array
->element_type
);
562 void pet_array_dump(struct pet_array
*array
)
567 isl_set_dump(array
->context
);
568 isl_set_dump(array
->extent
);
569 isl_set_dump(array
->value_bounds
);
570 fprintf(stderr
, "%s %s\n", array
->element_type
,
571 array
->live_out
? "live-out" : "");
574 /* Construct a pet_scop with room for n statements.
576 static struct pet_scop
*scop_alloc(isl_ctx
*ctx
, int n
)
578 struct pet_scop
*scop
;
580 scop
= isl_calloc_type(ctx
, struct pet_scop
);
584 scop
->context
= isl_set_universe(isl_space_params_alloc(ctx
, 0));
585 scop
->stmts
= isl_calloc_array(ctx
, struct pet_stmt
*, n
);
586 if (!scop
->context
|| !scop
->stmts
)
587 return pet_scop_free(scop
);
594 struct pet_scop
*pet_scop_empty(isl_ctx
*ctx
)
596 return scop_alloc(ctx
, 0);
599 /* Construct a pet_scop that contains the given pet_stmt.
601 struct pet_scop
*pet_scop_from_pet_stmt(isl_ctx
*ctx
, struct pet_stmt
*stmt
)
603 struct pet_scop
*scop
;
608 scop
= scop_alloc(ctx
, 1);
610 scop
->stmts
[0] = stmt
;
619 /* Construct a pet_scop that contains the statements in "scop1" and "scop2".
621 struct pet_scop
*pet_scop_add(isl_ctx
*ctx
, struct pet_scop
*scop1
,
622 struct pet_scop
*scop2
)
625 struct pet_scop
*scop
;
627 if (!scop1
|| !scop2
)
630 if (scop1
->n_stmt
== 0) {
631 pet_scop_free(scop1
);
635 if (scop2
->n_stmt
== 0) {
636 pet_scop_free(scop2
);
640 scop
= scop_alloc(ctx
, scop1
->n_stmt
+ scop2
->n_stmt
);
644 for (i
= 0; i
< scop1
->n_stmt
; ++i
) {
645 scop
->stmts
[i
] = scop1
->stmts
[i
];
646 scop1
->stmts
[i
] = NULL
;
649 for (i
= 0; i
< scop2
->n_stmt
; ++i
) {
650 scop
->stmts
[scop1
->n_stmt
+ i
] = scop2
->stmts
[i
];
651 scop2
->stmts
[i
] = NULL
;
654 pet_scop_free(scop1
);
655 pet_scop_free(scop2
);
658 pet_scop_free(scop1
);
659 pet_scop_free(scop2
);
663 void *pet_scop_free(struct pet_scop
*scop
)
669 isl_set_free(scop
->context
);
671 for (i
= 0; i
< scop
->n_array
; ++i
)
672 pet_array_free(scop
->arrays
[i
]);
675 for (i
= 0; i
< scop
->n_stmt
; ++i
)
676 pet_stmt_free(scop
->stmts
[i
]);
682 void pet_scop_dump(struct pet_scop
*scop
)
689 isl_set_dump(scop
->context
);
690 for (i
= 0; i
< scop
->n_array
; ++i
)
691 pet_array_dump(scop
->arrays
[i
]);
692 for (i
= 0; i
< scop
->n_stmt
; ++i
)
693 pet_stmt_dump(scop
->stmts
[i
]);
696 /* Return 1 if the two pet_arrays are equivalent.
698 int pet_array_is_equal(struct pet_array
*array1
, struct pet_array
*array2
)
700 if (!array1
|| !array2
)
703 if (!isl_set_is_equal(array1
->context
, array2
->context
))
705 if (!isl_set_is_equal(array1
->extent
, array2
->extent
))
707 if (!!array1
->value_bounds
!= !!array2
->value_bounds
)
709 if (array1
->value_bounds
&&
710 !isl_set_is_equal(array1
->value_bounds
, array2
->value_bounds
))
712 if (strcmp(array1
->element_type
, array2
->element_type
))
714 if (array1
->live_out
!= array2
->live_out
)
720 /* Return 1 if the two pet_stmts are equivalent.
722 int pet_stmt_is_equal(struct pet_stmt
*stmt1
, struct pet_stmt
*stmt2
)
724 if (!stmt1
|| !stmt2
)
727 if (stmt1
->line
!= stmt2
->line
)
729 if (!isl_set_is_equal(stmt1
->domain
, stmt2
->domain
))
731 if (!isl_map_is_equal(stmt1
->schedule
, stmt2
->schedule
))
733 if (!pet_expr_is_equal(stmt1
->body
, stmt2
->body
))
739 /* Return 1 if the two pet_scops are equivalent.
741 int pet_scop_is_equal(struct pet_scop
*scop1
, struct pet_scop
*scop2
)
745 if (!scop1
|| !scop2
)
748 if (!isl_set_is_equal(scop1
->context
, scop2
->context
))
751 if (scop1
->n_array
!= scop2
->n_array
)
753 for (i
= 0; i
< scop1
->n_array
; ++i
)
754 if (!pet_array_is_equal(scop1
->arrays
[i
], scop2
->arrays
[i
]))
757 if (scop1
->n_stmt
!= scop2
->n_stmt
)
759 for (i
= 0; i
< scop1
->n_stmt
; ++i
)
760 if (!pet_stmt_is_equal(scop1
->stmts
[i
], scop2
->stmts
[i
]))
766 /* Prefix the schedule of "stmt" with an extra dimension with constant
769 struct pet_stmt
*pet_stmt_prefix(struct pet_stmt
*stmt
, int pos
)
774 stmt
->schedule
= isl_map_insert_dims(stmt
->schedule
, isl_dim_out
, 0, 1);
775 stmt
->schedule
= isl_map_fix_si(stmt
->schedule
, isl_dim_out
, 0, pos
);
777 return pet_stmt_free(stmt
);
782 /* Prefix the schedules of all statements in "scop" with an extra
783 * dimension with constant value "pos".
785 struct pet_scop
*pet_scop_prefix(struct pet_scop
*scop
, int pos
)
792 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
793 scop
->stmts
[i
] = pet_stmt_prefix(scop
->stmts
[i
], pos
);
795 return pet_scop_free(scop
);
801 /* Data used in embed_access.
802 * extend adds an iterator to the iteration domain
803 * var_id represents the induction variable of the corresponding loop
805 struct pet_embed_access
{
810 /* Embed the access relation in an extra outer loop.
812 * We first update the iteration domain to insert the extra dimension.
814 * If the access refers to the induction variable, then it is
815 * turned into an access to the set of integers with index (and value)
816 * equal to the induction variable.
818 * If the induction variable appears in the constraints (as a parameter),
819 * then the parameter is equated to the newly introduced iteration
820 * domain dimension and subsequently projected out.
822 static __isl_give isl_map
*embed_access(__isl_take isl_map
*access
,
825 struct pet_embed_access
*data
= user
;
826 isl_id
*array_id
= NULL
;
829 access
= update_domain(access
, data
->extend
);
831 if (isl_map_has_tuple_id(access
, isl_dim_out
))
832 array_id
= isl_map_get_tuple_id(access
, isl_dim_out
);
833 if (array_id
== data
->var_id
) {
834 access
= isl_map_insert_dims(access
, isl_dim_out
, 0, 1);
835 access
= isl_map_equate(access
,
836 isl_dim_in
, 0, isl_dim_out
, 0);
838 isl_id_free(array_id
);
840 pos
= isl_map_find_dim_by_id(access
, isl_dim_param
, data
->var_id
);
842 access
= isl_map_equate(access
,
843 isl_dim_param
, pos
, isl_dim_in
, 0);
844 access
= isl_map_project_out(access
, isl_dim_param
, pos
, 1);
846 access
= isl_map_set_dim_id(access
, isl_dim_in
, 0,
847 isl_id_copy(data
->var_id
));
852 /* Embed all access relations in "expr" in an extra loop.
853 * "extend" inserts an outer loop iterator in the iteration domains.
854 * "var_id" represents the induction variable.
856 static struct pet_expr
*expr_embed(struct pet_expr
*expr
,
857 __isl_take isl_map
*extend
, __isl_keep isl_id
*var_id
)
859 struct pet_embed_access data
= { .extend
= extend
, .var_id
= var_id
};
861 expr
= expr_foreach_access(expr
, &embed_access
, &data
);
862 isl_map_free(extend
);
866 /* Embed the given pet_stmt in an extra outer loop with iteration domain
867 * "dom" and schedule "sched". "var_id" represents the induction variable
870 * The iteration domain and schedule of the statement are updated
871 * according to the iteration domain and schedule of the new loop.
873 * If the induction variable appears in the constraints (as a parameter)
874 * of the current iteration domain or the schedule of the statement,
875 * then the parameter is equated to the newly introduced iteration
876 * domain dimension and subsequently projected out.
878 * Finally, all access relations are updated based on the extra loop.
880 struct pet_stmt
*pet_stmt_embed(struct pet_stmt
*stmt
, __isl_take isl_set
*dom
,
881 __isl_take isl_map
*sched
, __isl_take isl_id
*var_id
)
891 stmt_id
= isl_set_get_tuple_id(stmt
->domain
);
892 stmt
->domain
= isl_set_flat_product(isl_set_copy(dom
), stmt
->domain
);
893 stmt
->domain
= isl_set_set_tuple_id(stmt
->domain
, isl_id_copy(stmt_id
));
895 pos
= isl_set_find_dim_by_id(stmt
->domain
, isl_dim_param
, var_id
);
897 stmt
->domain
= isl_set_equate(stmt
->domain
,
898 isl_dim_param
, pos
, isl_dim_set
, 0);
899 stmt
->domain
= isl_set_project_out(stmt
->domain
,
900 isl_dim_param
, pos
, 1);
903 stmt
->schedule
= isl_map_flat_product(sched
, stmt
->schedule
);
904 stmt
->schedule
= isl_map_set_tuple_id(stmt
->schedule
,
905 isl_dim_in
, stmt_id
);
907 pos
= isl_map_find_dim_by_id(stmt
->schedule
, isl_dim_param
, var_id
);
909 stmt
->schedule
= isl_map_equate(stmt
->schedule
,
910 isl_dim_param
, pos
, isl_dim_in
, 0);
911 stmt
->schedule
= isl_map_project_out(stmt
->schedule
,
912 isl_dim_param
, pos
, 1);
915 dim
= isl_space_map_from_set(isl_set_get_space(stmt
->domain
));
916 extend
= isl_map_identity(dim
);
917 extend
= isl_map_remove_dims(extend
, isl_dim_in
, 0, 1);
918 extend
= isl_map_set_tuple_id(extend
, isl_dim_in
,
919 isl_map_get_tuple_id(extend
, isl_dim_out
));
920 stmt
->body
= expr_embed(stmt
->body
, extend
, var_id
);
925 if (!stmt
->domain
|| !stmt
->schedule
|| !stmt
->body
)
926 return pet_stmt_free(stmt
);
935 /* Embed all statements in "scop" in an extra outer loop with iteration domain
936 * "dom" and schedule "sched". "var_id" represents the induction variable
939 struct pet_scop
*pet_scop_embed(struct pet_scop
*scop
, __isl_take isl_set
*dom
,
940 __isl_take isl_map
*sched
, __isl_take isl_id
*id
)
947 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
948 scop
->stmts
[i
] = pet_stmt_embed(scop
->stmts
[i
],
950 isl_map_copy(sched
), isl_id_copy(id
));
963 return pet_scop_free(scop
);
966 /* Add extra conditions on the parameters to iteration domain of "stmt".
968 static struct pet_stmt
*stmt_restrict(struct pet_stmt
*stmt
,
969 __isl_take isl_set
*cond
)
974 stmt
->domain
= isl_set_intersect_params(stmt
->domain
, cond
);
979 return pet_stmt_free(stmt
);
982 /* Add extra conditions on the parameters to all iteration domains.
984 struct pet_scop
*pet_scop_restrict(struct pet_scop
*scop
,
985 __isl_take isl_set
*cond
)
992 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
993 scop
->stmts
[i
] = stmt_restrict(scop
->stmts
[i
],
1003 return pet_scop_free(scop
);
1006 /* Add all parameters in "expr" to "dim" and return the result.
1008 static __isl_give isl_space
*expr_collect_params(struct pet_expr
*expr
,
1009 __isl_take isl_space
*dim
)
1015 for (i
= 0; i
< expr
->n_arg
; ++i
)
1017 dim
= expr_collect_params(expr
->args
[i
], dim
);
1019 if (expr
->type
== pet_expr_access
)
1020 dim
= isl_space_align_params(dim
,
1021 isl_map_get_space(expr
->acc
.access
));
1025 isl_space_free(dim
);
1026 return pet_expr_free(expr
);
1029 /* Add all parameters in "stmt" to "dim" and return the result.
1031 static __isl_give isl_space
*stmt_collect_params(struct pet_stmt
*stmt
,
1032 __isl_take isl_space
*dim
)
1037 dim
= isl_space_align_params(dim
, isl_set_get_space(stmt
->domain
));
1038 dim
= isl_space_align_params(dim
, isl_map_get_space(stmt
->schedule
));
1039 dim
= expr_collect_params(stmt
->body
, dim
);
1043 isl_space_free(dim
);
1044 return pet_stmt_free(stmt
);
1047 /* Add all parameters in "array" to "dim" and return the result.
1049 static __isl_give isl_space
*array_collect_params(struct pet_array
*array
,
1050 __isl_take isl_space
*dim
)
1055 dim
= isl_space_align_params(dim
, isl_set_get_space(array
->context
));
1056 dim
= isl_space_align_params(dim
, isl_set_get_space(array
->extent
));
1060 isl_space_free(dim
);
1061 return pet_array_free(array
);
1064 /* Add all parameters in "scop" to "dim" and return the result.
1066 static __isl_give isl_space
*scop_collect_params(struct pet_scop
*scop
,
1067 __isl_take isl_space
*dim
)
1074 for (i
= 0; i
< scop
->n_array
; ++i
)
1075 dim
= array_collect_params(scop
->arrays
[i
], dim
);
1077 for (i
= 0; i
< scop
->n_stmt
; ++i
)
1078 dim
= stmt_collect_params(scop
->stmts
[i
], dim
);
1082 isl_space_free(dim
);
1083 return pet_scop_free(scop
);
1086 /* Add all parameters in "dim" to all access relations in "expr".
1088 static struct pet_expr
*expr_propagate_params(struct pet_expr
*expr
,
1089 __isl_take isl_space
*dim
)
1096 for (i
= 0; i
< expr
->n_arg
; ++i
) {
1098 expr_propagate_params(expr
->args
[i
],
1099 isl_space_copy(dim
));
1104 if (expr
->type
== pet_expr_access
) {
1105 expr
->acc
.access
= isl_map_align_params(expr
->acc
.access
,
1106 isl_space_copy(dim
));
1107 if (!expr
->acc
.access
)
1111 isl_space_free(dim
);
1114 isl_space_free(dim
);
1115 return pet_expr_free(expr
);
1118 /* Add all parameters in "dim" to the domain, schedule and
1119 * all access relations in "stmt".
1121 static struct pet_stmt
*stmt_propagate_params(struct pet_stmt
*stmt
,
1122 __isl_take isl_space
*dim
)
1127 stmt
->domain
= isl_set_align_params(stmt
->domain
, isl_space_copy(dim
));
1128 stmt
->schedule
= isl_map_align_params(stmt
->schedule
,
1129 isl_space_copy(dim
));
1130 stmt
->body
= expr_propagate_params(stmt
->body
, isl_space_copy(dim
));
1132 if (!stmt
->domain
|| !stmt
->schedule
|| !stmt
->body
)
1135 isl_space_free(dim
);
1138 isl_space_free(dim
);
1139 return pet_stmt_free(stmt
);
1142 /* Add all parameters in "dim" to "array".
1144 static struct pet_array
*array_propagate_params(struct pet_array
*array
,
1145 __isl_take isl_space
*dim
)
1150 array
->context
= isl_set_align_params(array
->context
,
1151 isl_space_copy(dim
));
1152 array
->extent
= isl_set_align_params(array
->extent
,
1153 isl_space_copy(dim
));
1154 if (array
->value_bounds
) {
1155 array
->value_bounds
= isl_set_align_params(array
->value_bounds
,
1156 isl_space_copy(dim
));
1157 if (!array
->value_bounds
)
1161 if (!array
->context
|| !array
->extent
)
1164 isl_space_free(dim
);
1167 isl_space_free(dim
);
1168 return pet_array_free(array
);
1171 /* Add all parameters in "dim" to "scop".
1173 static struct pet_scop
*scop_propagate_params(struct pet_scop
*scop
,
1174 __isl_take isl_space
*dim
)
1181 for (i
= 0; i
< scop
->n_array
; ++i
) {
1182 scop
->arrays
[i
] = array_propagate_params(scop
->arrays
[i
],
1183 isl_space_copy(dim
));
1184 if (!scop
->arrays
[i
])
1188 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
1189 scop
->stmts
[i
] = stmt_propagate_params(scop
->stmts
[i
],
1190 isl_space_copy(dim
));
1191 if (!scop
->stmts
[i
])
1195 isl_space_free(dim
);
1198 isl_space_free(dim
);
1199 return pet_scop_free(scop
);
1202 /* Update all isl_sets and isl_maps in "scop" such that they all
1203 * have the same parameters.
1205 struct pet_scop
*pet_scop_align_params(struct pet_scop
*scop
)
1212 dim
= isl_set_get_space(scop
->context
);
1213 dim
= scop_collect_params(scop
, dim
);
1215 scop
->context
= isl_set_align_params(scop
->context
, isl_space_copy(dim
));
1216 scop
= scop_propagate_params(scop
, dim
);
1221 /* Check if the given access relation accesses a (0D) array that corresponds
1222 * to one of the parameters in "dim". If so, replace the array access
1223 * by an access to the set of integers with as index (and value)
1226 static __isl_give isl_map
*access_detect_parameter(__isl_take isl_map
*access
,
1227 __isl_take isl_space
*dim
)
1229 isl_id
*array_id
= NULL
;
1232 if (isl_map_has_tuple_id(access
, isl_dim_out
)) {
1233 array_id
= isl_map_get_tuple_id(access
, isl_dim_out
);
1234 pos
= isl_space_find_dim_by_id(dim
, isl_dim_param
, array_id
);
1236 isl_space_free(dim
);
1239 isl_id_free(array_id
);
1243 pos
= isl_map_find_dim_by_id(access
, isl_dim_param
, array_id
);
1245 access
= isl_map_insert_dims(access
, isl_dim_param
, 0, 1);
1246 access
= isl_map_set_dim_id(access
, isl_dim_param
, 0, array_id
);
1249 isl_id_free(array_id
);
1251 access
= isl_map_insert_dims(access
, isl_dim_out
, 0, 1);
1252 access
= isl_map_equate(access
, isl_dim_param
, pos
, isl_dim_out
, 0);
1257 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
1258 * in "dim" by a value equal to the corresponding parameter.
1260 static struct pet_expr
*expr_detect_parameter_accesses(struct pet_expr
*expr
,
1261 __isl_take isl_space
*dim
)
1268 for (i
= 0; i
< expr
->n_arg
; ++i
) {
1270 expr_detect_parameter_accesses(expr
->args
[i
],
1271 isl_space_copy(dim
));
1276 if (expr
->type
== pet_expr_access
) {
1277 expr
->acc
.access
= access_detect_parameter(expr
->acc
.access
,
1278 isl_space_copy(dim
));
1279 if (!expr
->acc
.access
)
1283 isl_space_free(dim
);
1286 isl_space_free(dim
);
1287 return pet_expr_free(expr
);
1290 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
1291 * in "dim" by a value equal to the corresponding parameter.
1293 static struct pet_stmt
*stmt_detect_parameter_accesses(struct pet_stmt
*stmt
,
1294 __isl_take isl_space
*dim
)
1299 stmt
->body
= expr_detect_parameter_accesses(stmt
->body
,
1300 isl_space_copy(dim
));
1302 if (!stmt
->domain
|| !stmt
->schedule
|| !stmt
->body
)
1305 isl_space_free(dim
);
1308 isl_space_free(dim
);
1309 return pet_stmt_free(stmt
);
1312 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
1313 * in "dim" by a value equal to the corresponding parameter.
1315 static struct pet_scop
*scop_detect_parameter_accesses(struct pet_scop
*scop
,
1316 __isl_take isl_space
*dim
)
1323 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
1324 scop
->stmts
[i
] = stmt_detect_parameter_accesses(scop
->stmts
[i
],
1325 isl_space_copy(dim
));
1326 if (!scop
->stmts
[i
])
1330 isl_space_free(dim
);
1333 isl_space_free(dim
);
1334 return pet_scop_free(scop
);
1337 /* Replace all accesses to (0D) arrays that correspond to any of
1338 * the parameters used in "scop" by a value equal
1339 * to the corresponding parameter.
1341 struct pet_scop
*pet_scop_detect_parameter_accesses(struct pet_scop
*scop
)
1348 dim
= isl_set_get_space(scop
->context
);
1349 dim
= scop_collect_params(scop
, dim
);
1351 scop
= scop_detect_parameter_accesses(scop
, dim
);
1356 /* Add all read access relations (if "read" is set) and/or all write
1357 * access relations (if "write" is set) to "accesses" and return the result.
1359 static __isl_give isl_union_map
*expr_collect_accesses(struct pet_expr
*expr
,
1360 int read
, int write
, __isl_take isl_union_map
*accesses
)
1369 for (i
= 0; i
< expr
->n_arg
; ++i
)
1370 accesses
= expr_collect_accesses(expr
->args
[i
],
1371 read
, write
, accesses
);
1373 if (expr
->type
== pet_expr_access
&&
1374 isl_map_has_tuple_id(expr
->acc
.access
, isl_dim_out
) &&
1375 ((read
&& expr
->acc
.read
) || (write
&& expr
->acc
.write
)))
1376 accesses
= isl_union_map_add_map(accesses
,
1377 isl_map_copy(expr
->acc
.access
));
1382 /* Collect and return all read access relations (if "read" is set)
1383 * and/or all write * access relations (if "write" is set) in "stmt".
1385 static __isl_give isl_union_map
*stmt_collect_accesses(struct pet_stmt
*stmt
,
1386 int read
, int write
, __isl_take isl_space
*dim
)
1388 isl_union_map
*accesses
;
1393 accesses
= isl_union_map_empty(dim
);
1394 accesses
= expr_collect_accesses(stmt
->body
, read
, write
, accesses
);
1395 accesses
= isl_union_map_intersect_domain(accesses
,
1396 isl_union_set_from_set(isl_set_copy(stmt
->domain
)));
1401 /* Collect and return all read access relations (if "read" is set)
1402 * and/or all write * access relations (if "write" is set) in "scop".
1404 static __isl_give isl_union_map
*scop_collect_accesses(struct pet_scop
*scop
,
1405 int read
, int write
)
1408 isl_union_map
*accesses
;
1413 accesses
= isl_union_map_empty(isl_set_get_space(scop
->context
));
1415 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
1416 isl_union_map
*accesses_i
;
1417 isl_space
*dim
= isl_set_get_space(scop
->context
);
1418 accesses_i
= stmt_collect_accesses(scop
->stmts
[i
],
1420 accesses
= isl_union_map_union(accesses
, accesses_i
);
1426 __isl_give isl_union_map
*pet_scop_collect_reads(struct pet_scop
*scop
)
1428 return scop_collect_accesses(scop
, 1, 0);
1431 __isl_give isl_union_map
*pet_scop_collect_writes(struct pet_scop
*scop
)
1433 return scop_collect_accesses(scop
, 0, 1);
1436 /* Collect and return the union of iteration domains in "scop".
1438 __isl_give isl_union_set
*pet_scop_collect_domains(struct pet_scop
*scop
)
1442 isl_union_set
*domain
;
1447 domain
= isl_union_set_empty(isl_set_get_space(scop
->context
));
1449 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
1450 domain_i
= isl_set_copy(scop
->stmts
[i
]->domain
);
1451 domain
= isl_union_set_add_set(domain
, domain_i
);
1457 /* Collect and return the schedules of the statements in "scop".
1458 * The range is normalized to the maximal number of scheduling
1461 __isl_give isl_union_map
*pet_scop_collect_schedule(struct pet_scop
*scop
)
1464 isl_map
*schedule_i
;
1465 isl_union_map
*schedule
;
1466 int depth
, max_depth
= 0;
1471 schedule
= isl_union_map_empty(isl_set_get_space(scop
->context
));
1473 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
1474 depth
= isl_map_dim(scop
->stmts
[i
]->schedule
, isl_dim_out
);
1475 if (depth
> max_depth
)
1479 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
1480 schedule_i
= isl_map_copy(scop
->stmts
[i
]->schedule
);
1481 depth
= isl_map_dim(schedule_i
, isl_dim_out
);
1482 schedule_i
= isl_map_add_dims(schedule_i
, isl_dim_out
,
1484 for (j
= depth
; j
< max_depth
; ++j
)
1485 schedule_i
= isl_map_fix_si(schedule_i
,
1487 schedule
= isl_union_map_add_map(schedule
, schedule_i
);