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
41 #define ARRAY_SIZE(array) (sizeof(array)/sizeof(*array))
43 static const char *type_str
[] = {
44 [pet_tree_block
] = "block",
45 [pet_tree_break
] = "break",
46 [pet_tree_continue
] = "continue",
47 [pet_tree_decl
] = "declaration",
48 [pet_tree_decl_init
] = "declaration-init",
49 [pet_tree_expr
] = "expression",
50 [pet_tree_for
] = "for",
51 [pet_tree_infinite_loop
] = "infinite-loop",
53 [pet_tree_if_else
] = "if-else",
54 [pet_tree_while
] = "while",
57 /* Return a textual representation of the type "type".
59 const char *pet_tree_type_str(enum pet_tree_type type
)
63 return type_str
[type
];
66 /* Extract a type from its textual representation "str".
68 enum pet_tree_type
pet_tree_str_type(const char *str
)
72 for (i
= 0; i
< ARRAY_SIZE(type_str
); ++i
)
73 if (!strcmp(type_str
[i
], str
))
76 return pet_tree_error
;
79 /* Return a new pet_tree of the given type.
81 * The location is initializaed to pet_loc_dummy.
83 __isl_give pet_tree
*pet_tree_alloc(isl_ctx
*ctx
, enum pet_tree_type type
)
87 tree
= isl_calloc_type(ctx
, struct pet_tree
);
95 tree
->loc
= &pet_loc_dummy
;
100 /* Return a new pet_tree representing the declaration (without initialization)
101 * of the variable "var".
103 __isl_give pet_tree
*pet_tree_new_decl(__isl_take pet_expr
*var
)
110 ctx
= pet_expr_get_ctx(var
);
111 tree
= pet_tree_alloc(ctx
, pet_tree_decl
);
123 /* Return a new pet_tree representing the declaration of the variable "var"
124 * with initial value "init".
126 __isl_give pet_tree
*pet_tree_new_decl_init(__isl_take pet_expr
*var
,
127 __isl_take pet_expr
*init
)
134 ctx
= pet_expr_get_ctx(var
);
135 tree
= pet_tree_alloc(ctx
, pet_tree_decl_init
);
140 tree
->u
.d
.init
= init
;
149 /* Return a new pet_tree representing the expression "expr".
151 __isl_give pet_tree
*pet_tree_new_expr(__isl_take pet_expr
*expr
)
158 ctx
= pet_expr_get_ctx(expr
);
159 tree
= pet_tree_alloc(ctx
, pet_tree_expr
);
163 tree
->u
.e
.expr
= expr
;
171 /* Return a new pet_tree representing an intially empty sequence
172 * of trees with room for "n" trees.
173 * "block" indicates whether the sequence has its own scope.
175 __isl_give pet_tree
*pet_tree_new_block(isl_ctx
*ctx
, int block
, int n
)
179 tree
= pet_tree_alloc(ctx
, pet_tree_block
);
182 tree
->u
.b
.block
= block
;
185 tree
->u
.b
.child
= isl_calloc_array(ctx
, pet_tree
*, n
);
186 if (n
&& !tree
->u
.b
.child
)
187 return pet_tree_free(tree
);
192 /* Return a new pet_tree representing a break statement.
194 __isl_give pet_tree
*pet_tree_new_break(isl_ctx
*ctx
)
196 return pet_tree_alloc(ctx
, pet_tree_break
);
199 /* Return a new pet_tree representing a continue statement.
201 __isl_give pet_tree
*pet_tree_new_continue(isl_ctx
*ctx
)
203 return pet_tree_alloc(ctx
, pet_tree_continue
);
206 /* Return a new pet_tree representing a for loop
207 * with induction variable "iv", initial value for the induction
208 * variable "init", loop condition "cond", induction variable increment "inc"
209 * and loop body "body". "declared" indicates whether the induction variable
210 * is declared by the loop. "independent" is set if the for loop is marked
213 * The location of the loop is initialized to that of the body.
215 __isl_give pet_tree
*pet_tree_new_for(int independent
, int declared
,
216 __isl_take pet_expr
*iv
, __isl_take pet_expr
*init
,
217 __isl_take pet_expr
*cond
, __isl_take pet_expr
*inc
,
218 __isl_take pet_tree
*body
)
223 if (!iv
|| !init
|| !cond
|| !inc
|| !body
)
225 ctx
= pet_tree_get_ctx(body
);
226 tree
= pet_tree_alloc(ctx
, pet_tree_for
);
230 tree
->u
.l
.independent
= independent
;
231 tree
->u
.l
.declared
= declared
;
233 tree
->u
.l
.init
= init
;
234 tree
->u
.l
.cond
= cond
;
236 tree
->u
.l
.body
= body
;
237 tree
->loc
= pet_tree_get_loc(body
);
239 return pet_tree_free(tree
);
251 /* Return a new pet_tree representing a while loop
252 * with loop condition "cond" and loop body "body".
254 * The location of the loop is initialized to that of the body.
256 __isl_give pet_tree
*pet_tree_new_while(__isl_take pet_expr
*cond
,
257 __isl_take pet_tree
*body
)
264 ctx
= pet_tree_get_ctx(body
);
265 tree
= pet_tree_alloc(ctx
, pet_tree_while
);
269 tree
->u
.l
.cond
= cond
;
270 tree
->u
.l
.body
= body
;
271 tree
->loc
= pet_tree_get_loc(body
);
273 return pet_tree_free(tree
);
282 /* Return a new pet_tree representing an infinite loop
283 * with loop body "body".
285 * The location of the loop is initialized to that of the body.
287 __isl_give pet_tree
*pet_tree_new_infinite_loop(__isl_take pet_tree
*body
)
294 ctx
= pet_tree_get_ctx(body
);
295 tree
= pet_tree_alloc(ctx
, pet_tree_infinite_loop
);
297 return pet_tree_free(body
);
299 tree
->u
.l
.body
= body
;
300 tree
->loc
= pet_tree_get_loc(body
);
302 return pet_tree_free(tree
);
307 /* Return a new pet_tree representing an if statement
308 * with condition "cond" and then branch "then_body".
310 * The location of the if statement is initialized to that of the body.
312 __isl_give pet_tree
*pet_tree_new_if(__isl_take pet_expr
*cond
,
313 __isl_take pet_tree
*then_body
)
318 if (!cond
|| !then_body
)
320 ctx
= pet_tree_get_ctx(then_body
);
321 tree
= pet_tree_alloc(ctx
, pet_tree_if
);
325 tree
->u
.i
.cond
= cond
;
326 tree
->u
.i
.then_body
= then_body
;
327 tree
->loc
= pet_tree_get_loc(then_body
);
329 return pet_tree_free(tree
);
334 pet_tree_free(then_body
);
338 /* Return a new pet_tree representing an if statement
339 * with condition "cond", then branch "then_body" and else branch "else_body".
341 * The location of the if statement is initialized to cover
342 * those of the bodies.
344 __isl_give pet_tree
*pet_tree_new_if_else(__isl_take pet_expr
*cond
,
345 __isl_take pet_tree
*then_body
, __isl_take pet_tree
*else_body
)
350 if (!cond
|| !then_body
|| !else_body
)
352 ctx
= pet_tree_get_ctx(then_body
);
353 tree
= pet_tree_alloc(ctx
, pet_tree_if_else
);
357 tree
->u
.i
.cond
= cond
;
358 tree
->u
.i
.then_body
= then_body
;
359 tree
->u
.i
.else_body
= else_body
;
360 tree
->loc
= pet_tree_get_loc(then_body
);
361 tree
->loc
= pet_loc_update_start_end_from_loc(tree
->loc
,
364 return pet_tree_free(tree
);
369 pet_tree_free(then_body
);
370 pet_tree_free(else_body
);
374 /* Return an independent duplicate of "tree".
376 static __isl_give pet_tree
*pet_tree_dup(__isl_keep pet_tree
*tree
)
384 switch (tree
->type
) {
388 dup
= pet_tree_new_block(tree
->ctx
, tree
->u
.b
.block
,
390 for (i
= 0; i
< tree
->u
.b
.n
; ++i
)
391 dup
= pet_tree_block_add_child(dup
,
392 pet_tree_copy(tree
->u
.b
.child
[i
]));
395 dup
= pet_tree_new_break(tree
->ctx
);
397 case pet_tree_continue
:
398 dup
= pet_tree_new_continue(tree
->ctx
);
401 dup
= pet_tree_new_decl(pet_expr_copy(tree
->u
.d
.var
));
403 case pet_tree_decl_init
:
404 dup
= pet_tree_new_decl_init(pet_expr_copy(tree
->u
.d
.var
),
405 pet_expr_copy(tree
->u
.d
.init
));
408 dup
= pet_tree_new_expr(pet_expr_copy(tree
->u
.e
.expr
));
411 dup
= pet_tree_new_for(tree
->u
.l
.independent
,
413 pet_expr_copy(tree
->u
.l
.iv
), pet_expr_copy(tree
->u
.l
.init
),
414 pet_expr_copy(tree
->u
.l
.cond
), pet_expr_copy(tree
->u
.l
.inc
),
415 pet_tree_copy(tree
->u
.l
.body
));
418 dup
= pet_tree_new_while(pet_expr_copy(tree
->u
.l
.cond
),
419 pet_tree_copy(tree
->u
.l
.body
));
421 case pet_tree_infinite_loop
:
422 dup
= pet_tree_new_infinite_loop(pet_tree_copy(tree
->u
.l
.body
));
425 dup
= pet_tree_new_if(pet_expr_copy(tree
->u
.i
.cond
),
426 pet_tree_copy(tree
->u
.i
.then_body
));
428 case pet_tree_if_else
:
429 dup
= pet_tree_new_if_else(pet_expr_copy(tree
->u
.i
.cond
),
430 pet_tree_copy(tree
->u
.i
.then_body
),
431 pet_tree_copy(tree
->u
.i
.else_body
));
437 pet_loc_free(dup
->loc
);
438 dup
->loc
= pet_loc_copy(tree
->loc
);
440 return pet_tree_free(dup
);
442 dup
->label
= isl_id_copy(tree
->label
);
444 return pet_tree_free(dup
);
450 /* Return a pet_tree that is equal to "tree" and that has only one reference.
452 __isl_give pet_tree
*pet_tree_cow(__isl_take pet_tree
*tree
)
460 return pet_tree_dup(tree
);
463 /* Return an extra reference to "tree".
465 __isl_give pet_tree
*pet_tree_copy(__isl_keep pet_tree
*tree
)
474 /* Free a reference to "tree".
476 __isl_null pet_tree
*pet_tree_free(__isl_take pet_tree
*tree
)
485 pet_loc_free(tree
->loc
);
486 isl_id_free(tree
->label
);
488 switch (tree
->type
) {
492 for (i
= 0; i
< tree
->u
.b
.n
; ++i
)
493 pet_tree_free(tree
->u
.b
.child
[i
]);
494 free(tree
->u
.b
.child
);
497 case pet_tree_continue
:
499 case pet_tree_decl_init
:
500 pet_expr_free(tree
->u
.d
.init
);
502 pet_expr_free(tree
->u
.d
.var
);
505 pet_expr_free(tree
->u
.e
.expr
);
508 pet_expr_free(tree
->u
.l
.iv
);
509 pet_expr_free(tree
->u
.l
.init
);
510 pet_expr_free(tree
->u
.l
.inc
);
512 pet_expr_free(tree
->u
.l
.cond
);
513 case pet_tree_infinite_loop
:
514 pet_tree_free(tree
->u
.l
.body
);
516 case pet_tree_if_else
:
517 pet_tree_free(tree
->u
.i
.else_body
);
519 pet_expr_free(tree
->u
.i
.cond
);
520 pet_tree_free(tree
->u
.i
.then_body
);
524 isl_ctx_deref(tree
->ctx
);
529 /* Return the isl_ctx in which "tree" was created.
531 isl_ctx
*pet_tree_get_ctx(__isl_keep pet_tree
*tree
)
533 return tree
? tree
->ctx
: NULL
;
536 /* Return the location of "tree".
538 __isl_give pet_loc
*pet_tree_get_loc(__isl_keep pet_tree
*tree
)
540 return tree
? pet_loc_copy(tree
->loc
) : NULL
;
543 /* Return the type of "tree".
545 enum pet_tree_type
pet_tree_get_type(__isl_keep pet_tree
*tree
)
548 return pet_tree_error
;
553 /* Replace the location of "tree" by "loc".
555 __isl_give pet_tree
*pet_tree_set_loc(__isl_take pet_tree
*tree
,
556 __isl_take pet_loc
*loc
)
558 tree
= pet_tree_cow(tree
);
562 pet_loc_free(tree
->loc
);
572 /* Replace the label of "tree" by "label".
574 __isl_give pet_tree
*pet_tree_set_label(__isl_take pet_tree
*tree
,
575 __isl_take isl_id
*label
)
577 tree
= pet_tree_cow(tree
);
581 isl_id_free(tree
->label
);
587 return pet_tree_free(tree
);
590 /* Given an expression tree "tree", return the associated expression.
592 __isl_give pet_expr
*pet_tree_expr_get_expr(__isl_keep pet_tree
*tree
)
596 if (pet_tree_get_type(tree
) != pet_tree_expr
)
597 isl_die(pet_tree_get_ctx(tree
), isl_error_invalid
,
598 "not an expression tree", return NULL
);
600 return pet_expr_copy(tree
->u
.e
.expr
);
603 /* Given a block tree "tree", return the number of children in the sequence.
605 int pet_tree_block_n_child(__isl_keep pet_tree
*tree
)
609 if (pet_tree_get_type(tree
) != pet_tree_block
)
610 isl_die(pet_tree_get_ctx(tree
), isl_error_invalid
,
611 "not a block tree", return -1);
616 /* Add "child" to the sequence of trees represented by "block".
618 * Update the location of "block" to include that of "child".
620 __isl_give pet_tree
*pet_tree_block_add_child(__isl_take pet_tree
*block
,
621 __isl_take pet_tree
*child
)
623 block
= pet_tree_cow(block
);
624 if (!block
|| !child
)
626 if (block
->type
!= pet_tree_block
)
627 isl_die(pet_tree_get_ctx(block
), isl_error_invalid
,
628 "not a block tree", goto error
);
629 if (block
->u
.b
.n
>= block
->u
.b
.max
)
630 isl_die(pet_tree_get_ctx(block
), isl_error_invalid
,
631 "out of space in block", goto error
);
633 block
->loc
= pet_loc_update_start_end_from_loc(block
->loc
, child
->loc
);
634 block
->u
.b
.child
[block
->u
.b
.n
++] = child
;
637 return pet_tree_free(block
);
641 pet_tree_free(block
);
642 pet_tree_free(child
);
646 /* Given a block tree "tree", return the child at position "pos".
648 __isl_give pet_tree
*pet_tree_block_get_child(__isl_keep pet_tree
*tree
,
653 if (pet_tree_get_type(tree
) != pet_tree_block
)
654 isl_die(pet_tree_get_ctx(tree
), isl_error_invalid
,
655 "not a block tree", return NULL
);
656 if (pos
< 0 || pos
>= tree
->u
.b
.n
)
657 isl_die(pet_tree_get_ctx(tree
), isl_error_invalid
,
658 "position out of bounds", return NULL
);
660 return pet_tree_copy(tree
->u
.b
.child
[pos
]);
663 /* Does "tree" represent a declaration (with or without initialization)?
665 int pet_tree_is_decl(__isl_keep pet_tree
*tree
)
670 switch (pet_tree_get_type(tree
)) {
672 case pet_tree_decl_init
:
679 /* Given a declaration tree "tree", return the variable that is being
682 __isl_give pet_expr
*pet_tree_decl_get_var(__isl_keep pet_tree
*tree
)
686 if (!pet_tree_is_decl(tree
))
687 isl_die(pet_tree_get_ctx(tree
), isl_error_invalid
,
688 "not a decl tree", return NULL
);
690 return pet_expr_copy(tree
->u
.d
.var
);
693 /* Given a declaration tree with initialization "tree",
694 * return the initial value of the declared variable.
696 __isl_give pet_expr
*pet_tree_decl_get_init(__isl_keep pet_tree
*tree
)
700 if (pet_tree_get_type(tree
) != pet_tree_decl_init
)
701 isl_die(pet_tree_get_ctx(tree
), isl_error_invalid
,
702 "not a decl_init tree", return NULL
);
704 return pet_expr_copy(tree
->u
.d
.init
);
707 /* Given an if tree "tree", return the if condition.
709 __isl_give pet_expr
*pet_tree_if_get_cond(__isl_keep pet_tree
*tree
)
711 enum pet_tree_type type
;
715 type
= pet_tree_get_type(tree
);
716 if (type
!= pet_tree_if
&& type
!= pet_tree_if_else
)
717 isl_die(pet_tree_get_ctx(tree
), isl_error_invalid
,
718 "not an if tree", return NULL
);
720 return pet_expr_copy(tree
->u
.i
.cond
);
723 /* Given an if tree "tree", return the body of the then branch.
725 __isl_give pet_tree
*pet_tree_if_get_then(__isl_keep pet_tree
*tree
)
727 enum pet_tree_type type
;
731 type
= pet_tree_get_type(tree
);
732 if (type
!= pet_tree_if
&& type
!= pet_tree_if_else
)
733 isl_die(pet_tree_get_ctx(tree
), isl_error_invalid
,
734 "not an if tree", return NULL
);
736 return pet_tree_copy(tree
->u
.i
.then_body
);
739 /* Given an if tree with an else branch "tree",
740 * return the body of that else branch.
742 __isl_give pet_tree
*pet_tree_if_get_else(__isl_keep pet_tree
*tree
)
746 if (pet_tree_get_type(tree
) != pet_tree_if_else
)
747 isl_die(pet_tree_get_ctx(tree
), isl_error_invalid
,
748 "not an if tree with an else branch", return NULL
);
750 return pet_tree_copy(tree
->u
.i
.else_body
);
753 /* Does "tree" represent some type of loop?
755 int pet_tree_is_loop(__isl_keep pet_tree
*tree
)
760 switch (pet_tree_get_type(tree
)) {
762 case pet_tree_infinite_loop
:
770 /* Given a for loop "tree", return the induction variable.
772 __isl_give pet_expr
*pet_tree_loop_get_var(__isl_keep pet_tree
*tree
)
776 if (pet_tree_get_type(tree
) != pet_tree_for
)
777 isl_die(pet_tree_get_ctx(tree
), isl_error_invalid
,
778 "not a for tree", return NULL
);
780 return pet_expr_copy(tree
->u
.l
.iv
);
783 /* Given a for loop "tree", return the initial value of the induction variable.
785 __isl_give pet_expr
*pet_tree_loop_get_init(__isl_keep pet_tree
*tree
)
789 if (pet_tree_get_type(tree
) != pet_tree_for
)
790 isl_die(pet_tree_get_ctx(tree
), isl_error_invalid
,
791 "not a for tree", return NULL
);
793 return pet_expr_copy(tree
->u
.l
.init
);
796 /* Given a loop "tree", return the loop condition.
798 * For an infinite loop, the loop condition always holds,
799 * so we simply return "1".
801 __isl_give pet_expr
*pet_tree_loop_get_cond(__isl_keep pet_tree
*tree
)
803 enum pet_tree_type type
;
807 type
= pet_tree_get_type(tree
);
808 if (type
== pet_tree_infinite_loop
)
809 return pet_expr_new_int(isl_val_one(pet_tree_get_ctx(tree
)));
810 if (type
!= pet_tree_for
&& type
!= pet_tree_while
)
811 isl_die(pet_tree_get_ctx(tree
), isl_error_invalid
,
812 "not a for or while tree", return NULL
);
814 return pet_expr_copy(tree
->u
.l
.cond
);
817 /* Given a for loop "tree", return the increment of the induction variable.
819 __isl_give pet_expr
*pet_tree_loop_get_inc(__isl_keep pet_tree
*tree
)
823 if (pet_tree_get_type(tree
) != pet_tree_for
)
824 isl_die(pet_tree_get_ctx(tree
), isl_error_invalid
,
825 "not a for tree", return NULL
);
827 return pet_expr_copy(tree
->u
.l
.inc
);
830 /* Given a loop tree "tree", return the body.
832 __isl_give pet_tree
*pet_tree_loop_get_body(__isl_keep pet_tree
*tree
)
837 if (!pet_tree_is_loop(tree
))
838 isl_die(pet_tree_get_ctx(tree
), isl_error_invalid
,
839 "not a loop tree", return NULL
);
841 return pet_tree_copy(tree
->u
.l
.body
);
844 /* Call "fn" on each node of "tree", including "tree" itself.
846 * Return 0 on success and -1 on error, where "fn" returning a negative
847 * value is treated as an error.
849 int pet_tree_foreach_sub_tree(__isl_keep pet_tree
*tree
,
850 int (*fn
)(__isl_keep pet_tree
*tree
, void *user
), void *user
)
857 if (fn(tree
, user
) < 0)
860 switch (tree
->type
) {
864 for (i
= 0; i
< tree
->u
.b
.n
; ++i
)
865 if (pet_tree_foreach_sub_tree(tree
->u
.b
.child
[i
],
870 case pet_tree_continue
:
872 case pet_tree_decl_init
:
876 if (pet_tree_foreach_sub_tree(tree
->u
.i
.then_body
,
880 case pet_tree_if_else
:
881 if (pet_tree_foreach_sub_tree(tree
->u
.i
.then_body
,
884 if (pet_tree_foreach_sub_tree(tree
->u
.i
.else_body
,
890 case pet_tree_infinite_loop
:
891 if (pet_tree_foreach_sub_tree(tree
->u
.l
.body
, fn
, user
) < 0)
899 /* Intermediate data structure for foreach_expr.
901 * "fn" is the function that needs to be called on each expression.
902 * "user" is the user argument to be passed to "fn".
904 struct pet_tree_foreach_expr_data
{
905 int (*fn
)(__isl_keep pet_expr
*expr
, void *user
);
909 /* Call data->fn on each expression in the "tree" object.
910 * This function is used as a callback to pet_tree_foreach_sub_tree
911 * to implement pet_tree_foreach_expr.
913 * Return 0 on success and -1 on error, where data->fn returning a negative
914 * value is treated as an error.
916 static int foreach_expr(__isl_keep pet_tree
*tree
, void *user
)
918 struct pet_tree_foreach_expr_data
*data
= user
;
923 switch (tree
->type
) {
928 case pet_tree_continue
:
929 case pet_tree_infinite_loop
:
932 if (data
->fn(tree
->u
.d
.var
, data
->user
) < 0)
935 case pet_tree_decl_init
:
936 if (data
->fn(tree
->u
.d
.var
, data
->user
) < 0)
938 if (data
->fn(tree
->u
.d
.init
, data
->user
) < 0)
942 if (data
->fn(tree
->u
.e
.expr
, data
->user
) < 0)
946 if (data
->fn(tree
->u
.i
.cond
, data
->user
) < 0)
949 case pet_tree_if_else
:
950 if (data
->fn(tree
->u
.i
.cond
, data
->user
) < 0)
954 if (data
->fn(tree
->u
.l
.cond
, data
->user
) < 0)
958 if (data
->fn(tree
->u
.l
.iv
, data
->user
) < 0)
960 if (data
->fn(tree
->u
.l
.init
, data
->user
) < 0)
962 if (data
->fn(tree
->u
.l
.cond
, data
->user
) < 0)
964 if (data
->fn(tree
->u
.l
.inc
, data
->user
) < 0)
972 /* Call "fn" on each top-level expression in the nodes of "tree"
974 * Return 0 on success and -1 on error, where "fn" returning a negative
975 * value is treated as an error.
977 * We run over all nodes in "tree" and call "fn"
978 * on each expression in those nodes.
980 int pet_tree_foreach_expr(__isl_keep pet_tree
*tree
,
981 int (*fn
)(__isl_keep pet_expr
*expr
, void *user
), void *user
)
983 struct pet_tree_foreach_expr_data data
= { fn
, user
};
985 return pet_tree_foreach_sub_tree(tree
, &foreach_expr
, &data
);
988 /* Intermediate data structure for foreach_access_expr.
990 * "fn" is the function that needs to be called on each access subexpression.
991 * "user" is the user argument to be passed to "fn".
993 struct pet_tree_foreach_access_expr_data
{
994 int (*fn
)(__isl_keep pet_expr
*expr
, void *user
);
998 /* Call data->fn on each access subexpression of "expr".
999 * This function is used as a callback to pet_tree_foreach_expr
1000 * to implement pet_tree_foreach_access_expr.
1002 * Return 0 on success and -1 on error, where data->fn returning a negative
1003 * value is treated as an error.
1005 static int foreach_access_expr(__isl_keep pet_expr
*expr
, void *user
)
1007 struct pet_tree_foreach_access_expr_data
*data
= user
;
1009 return pet_expr_foreach_access_expr(expr
, data
->fn
, data
->user
);
1012 /* Call "fn" on each access subexpression in the nodes of "tree"
1014 * Return 0 on success and -1 on error, where "fn" returning a negative
1015 * value is treated as an error.
1017 * We run over all expressions in the nodes of "tree" and call "fn"
1018 * on each access subexpression of those expressions.
1020 int pet_tree_foreach_access_expr(__isl_keep pet_tree
*tree
,
1021 int (*fn
)(__isl_keep pet_expr
*expr
, void *user
), void *user
)
1023 struct pet_tree_foreach_access_expr_data data
= { fn
, user
};
1025 return pet_tree_foreach_expr(tree
, &foreach_access_expr
, &data
);
1028 /* Modify all top-level expressions in the nodes of "tree"
1029 * by calling "fn" on them.
1031 __isl_give pet_tree
*pet_tree_map_expr(__isl_take pet_tree
*tree
,
1032 __isl_give pet_expr
*(*fn
)(__isl_take pet_expr
*expr
, void *user
),
1037 tree
= pet_tree_cow(tree
);
1041 switch (tree
->type
) {
1042 case pet_tree_error
:
1043 return pet_tree_free(tree
);
1044 case pet_tree_block
:
1045 for (i
= 0; i
< tree
->u
.b
.n
; ++i
) {
1046 tree
->u
.b
.child
[i
] =
1047 pet_tree_map_expr(tree
->u
.b
.child
[i
], fn
, user
);
1048 if (!tree
->u
.b
.child
[i
])
1049 return pet_tree_free(tree
);
1052 case pet_tree_break
:
1053 case pet_tree_continue
:
1056 tree
->u
.d
.var
= fn(tree
->u
.d
.var
, user
);
1058 return pet_tree_free(tree
);
1060 case pet_tree_decl_init
:
1061 tree
->u
.d
.var
= fn(tree
->u
.d
.var
, user
);
1062 tree
->u
.d
.init
= fn(tree
->u
.d
.init
, user
);
1063 if (!tree
->u
.d
.var
|| !tree
->u
.d
.init
)
1064 return pet_tree_free(tree
);
1067 tree
->u
.e
.expr
= fn(tree
->u
.e
.expr
, user
);
1068 if (!tree
->u
.e
.expr
)
1069 return pet_tree_free(tree
);
1072 tree
->u
.i
.cond
= fn(tree
->u
.i
.cond
, user
);
1073 tree
->u
.i
.then_body
=
1074 pet_tree_map_expr(tree
->u
.i
.then_body
, fn
, user
);
1075 if (!tree
->u
.i
.cond
|| !tree
->u
.i
.then_body
)
1076 return pet_tree_free(tree
);
1078 case pet_tree_if_else
:
1079 tree
->u
.i
.cond
= fn(tree
->u
.i
.cond
, user
);
1080 tree
->u
.i
.then_body
=
1081 pet_tree_map_expr(tree
->u
.i
.then_body
, fn
, user
);
1082 tree
->u
.i
.else_body
=
1083 pet_tree_map_expr(tree
->u
.i
.else_body
, fn
, user
);
1084 if (!tree
->u
.i
.cond
||
1085 !tree
->u
.i
.then_body
|| !tree
->u
.i
.else_body
)
1086 return pet_tree_free(tree
);
1088 case pet_tree_while
:
1089 tree
->u
.l
.cond
= fn(tree
->u
.l
.cond
, user
);
1090 tree
->u
.l
.body
= pet_tree_map_expr(tree
->u
.l
.body
, fn
, user
);
1091 if (!tree
->u
.l
.cond
|| !tree
->u
.l
.body
)
1092 return pet_tree_free(tree
);
1095 tree
->u
.l
.iv
= fn(tree
->u
.l
.iv
, user
);
1096 tree
->u
.l
.init
= fn(tree
->u
.l
.init
, user
);
1097 tree
->u
.l
.cond
= fn(tree
->u
.l
.cond
, user
);
1098 tree
->u
.l
.inc
= fn(tree
->u
.l
.inc
, user
);
1099 tree
->u
.l
.body
= pet_tree_map_expr(tree
->u
.l
.body
, fn
, user
);
1100 if (!tree
->u
.l
.iv
|| !tree
->u
.l
.init
|| !tree
->u
.l
.cond
||
1101 !tree
->u
.l
.inc
|| !tree
->u
.l
.body
)
1102 return pet_tree_free(tree
);
1104 case pet_tree_infinite_loop
:
1105 tree
->u
.l
.body
= pet_tree_map_expr(tree
->u
.l
.body
, fn
, user
);
1106 if (!tree
->u
.l
.body
)
1107 return pet_tree_free(tree
);
1114 /* Intermediate data structure for map_expr.
1116 * "map" is a function that modifies subexpressions of a given type.
1117 * "fn" is the function that needs to be called on each of those subexpressions.
1118 * "user" is the user argument to be passed to "fn".
1120 struct pet_tree_map_expr_data
{
1121 __isl_give pet_expr
*(*map
)(__isl_take pet_expr
*expr
,
1122 __isl_give pet_expr
*(*fn
)(__isl_take pet_expr
*expr
,
1123 void *user
), void *user
);
1124 __isl_give pet_expr
*(*fn
)(__isl_take pet_expr
*expr
, void *user
);
1128 /* This function is called on each top-level expressions in the nodes
1129 * of a tree. Call data->map to modify subexpressions of the top-level
1130 * expression by calling data->fn on them.
1132 * This is a wrapper around data->map for use as a callback
1133 * to pet_tree_map_expr.
1135 static __isl_give pet_expr
*map_expr(__isl_take pet_expr
*expr
,
1138 struct pet_tree_map_expr_data
*data
= user
;
1140 return data
->map(expr
, data
->fn
, data
->user
);
1143 /* Modify all access subexpressions in the nodes of "tree"
1144 * by calling "fn" on them.
1146 * We run over all expressions in the nodes of "tree" and call "fn"
1147 * on each access subexpression of those expressions.
1149 __isl_give pet_tree
*pet_tree_map_access_expr(__isl_take pet_tree
*tree
,
1150 __isl_give pet_expr
*(*fn
)(__isl_take pet_expr
*expr
, void *user
),
1153 struct pet_tree_map_expr_data data
= { &pet_expr_map_access
, fn
, user
};
1155 return pet_tree_map_expr(tree
, &map_expr
, &data
);
1158 /* Modify all call subexpressions in the nodes of "tree"
1159 * by calling "fn" on them.
1161 * We run over all expressions in the nodes of "tree" and call "fn"
1162 * on each call subexpression of those expressions.
1164 __isl_give pet_tree
*pet_tree_map_call_expr(__isl_take pet_tree
*tree
,
1165 __isl_give pet_expr
*(*fn
)(__isl_take pet_expr
*expr
, void *user
),
1168 struct pet_tree_map_expr_data data
= { &pet_expr_map_call
, fn
, user
};
1170 return pet_tree_map_expr(tree
, &map_expr
, &data
);
1173 /* Wrapper around pet_expr_align_params
1174 * for use as a pet_tree_map_expr callback.
1176 static __isl_give pet_expr
*align_params(__isl_take pet_expr
*expr
,
1179 isl_space
*space
= user
;
1181 return pet_expr_align_params(expr
, isl_space_copy(space
));
1184 /* Add all parameters in "space" to all access relations and index expressions
1187 __isl_give pet_tree
*pet_tree_align_params(__isl_take pet_tree
*tree
,
1188 __isl_take isl_space
*space
)
1190 tree
= pet_tree_map_expr(tree
, &align_params
, space
);
1191 isl_space_free(space
);
1195 /* Wrapper around pet_expr_add_ref_ids
1196 * for use as a pet_tree_map_expr callback.
1198 static __isl_give pet_expr
*add_ref_ids(__isl_take pet_expr
*expr
, void *user
)
1202 return pet_expr_add_ref_ids(expr
, n_ref
);
1205 /* Add reference identifiers to all access expressions in "tree".
1206 * "n_ref" points to an integer that contains the sequence number
1207 * of the next reference.
1209 __isl_give pet_tree
*pet_tree_add_ref_ids(__isl_take pet_tree
*tree
,
1212 return pet_tree_map_expr(tree
, &add_ref_ids
, n_ref
);
1215 /* Wrapper around pet_expr_anonymize
1216 * for use as a pet_tree_map_expr callback.
1218 static __isl_give pet_expr
*anonymize(__isl_take pet_expr
*expr
, void *user
)
1220 return pet_expr_anonymize(expr
);
1223 /* Reset the user pointer on all parameter and tuple ids in "tree".
1225 __isl_give pet_tree
*pet_tree_anonymize(__isl_take pet_tree
*tree
)
1227 return pet_tree_map_expr(tree
, &anonymize
, NULL
);
1230 /* Arguments to be passed to pet_expr_gist from the gist wrapper.
1232 struct pet_tree_gist_data
{
1234 isl_union_map
*value_bounds
;
1237 /* Wrapper around pet_expr_gist for use as a pet_tree_map_expr callback.
1239 static __isl_give pet_expr
*gist(__isl_take pet_expr
*expr
, void *user
)
1241 struct pet_tree_gist_data
*data
= user
;
1243 return pet_expr_gist(expr
, data
->domain
, data
->value_bounds
);
1246 /* Compute the gist of all access relations and index expressions inside
1247 * "tree" based on the constraints on the parameters specified by "context"
1248 * and the constraints on the values of nested accesses specified
1249 * by "value_bounds".
1251 __isl_give pet_tree
*pet_tree_gist(__isl_take pet_tree
*tree
,
1252 __isl_keep isl_set
*context
, __isl_keep isl_union_map
*value_bounds
)
1254 struct pet_tree_gist_data data
= { context
, value_bounds
};
1256 return pet_tree_map_expr(tree
, &gist
, &data
);
1259 /* Return 1 if the two pet_tree objects are equivalent.
1261 * We ignore the locations of the trees.
1263 int pet_tree_is_equal(__isl_keep pet_tree
*tree1
, __isl_keep pet_tree
*tree2
)
1268 if (!tree1
|| !tree2
)
1274 if (tree1
->type
!= tree2
->type
)
1276 if (tree1
->label
!= tree2
->label
)
1279 switch (tree1
->type
) {
1280 case pet_tree_error
:
1282 case pet_tree_block
:
1283 if (tree1
->u
.b
.block
!= tree2
->u
.b
.block
)
1285 if (tree1
->u
.b
.n
!= tree2
->u
.b
.n
)
1287 for (i
= 0; i
< tree1
->u
.b
.n
; ++i
) {
1288 equal
= pet_tree_is_equal(tree1
->u
.b
.child
[i
],
1289 tree2
->u
.b
.child
[i
]);
1290 if (equal
< 0 || !equal
)
1294 case pet_tree_break
:
1295 case pet_tree_continue
:
1298 return pet_expr_is_equal(tree1
->u
.d
.var
, tree2
->u
.d
.var
);
1299 case pet_tree_decl_init
:
1300 equal
= pet_expr_is_equal(tree1
->u
.d
.var
, tree2
->u
.d
.var
);
1301 if (equal
< 0 || !equal
)
1303 return pet_expr_is_equal(tree1
->u
.d
.init
, tree2
->u
.d
.init
);
1305 return pet_expr_is_equal(tree1
->u
.e
.expr
, tree2
->u
.e
.expr
);
1307 if (tree1
->u
.l
.declared
!= tree2
->u
.l
.declared
)
1309 equal
= pet_expr_is_equal(tree1
->u
.l
.iv
, tree2
->u
.l
.iv
);
1310 if (equal
< 0 || !equal
)
1312 equal
= pet_expr_is_equal(tree1
->u
.l
.init
, tree2
->u
.l
.init
);
1313 if (equal
< 0 || !equal
)
1315 equal
= pet_expr_is_equal(tree1
->u
.l
.cond
, tree2
->u
.l
.cond
);
1316 if (equal
< 0 || !equal
)
1318 equal
= pet_expr_is_equal(tree1
->u
.l
.inc
, tree2
->u
.l
.inc
);
1319 if (equal
< 0 || !equal
)
1321 return pet_tree_is_equal(tree1
->u
.l
.body
, tree2
->u
.l
.body
);
1322 case pet_tree_while
:
1323 equal
= pet_expr_is_equal(tree1
->u
.l
.cond
, tree2
->u
.l
.cond
);
1324 if (equal
< 0 || !equal
)
1326 return pet_tree_is_equal(tree1
->u
.l
.body
, tree2
->u
.l
.body
);
1327 case pet_tree_infinite_loop
:
1328 return pet_tree_is_equal(tree1
->u
.l
.body
, tree2
->u
.l
.body
);
1330 equal
= pet_expr_is_equal(tree1
->u
.i
.cond
, tree2
->u
.i
.cond
);
1331 if (equal
< 0 || !equal
)
1333 return pet_tree_is_equal(tree1
->u
.i
.then_body
,
1334 tree2
->u
.i
.then_body
);
1335 case pet_tree_if_else
:
1336 equal
= pet_expr_is_equal(tree1
->u
.i
.cond
, tree2
->u
.i
.cond
);
1337 if (equal
< 0 || !equal
)
1339 equal
= pet_tree_is_equal(tree1
->u
.i
.then_body
,
1340 tree2
->u
.i
.then_body
);
1341 if (equal
< 0 || !equal
)
1343 return pet_tree_is_equal(tree1
->u
.i
.else_body
,
1344 tree2
->u
.i
.else_body
);
1350 /* Is "tree" an expression tree that performs the operation "type"?
1352 static int pet_tree_is_op_of_type(__isl_keep pet_tree
*tree
,
1353 enum pet_op_type type
)
1357 if (tree
->type
!= pet_tree_expr
)
1359 if (pet_expr_get_type(tree
->u
.e
.expr
) != pet_expr_op
)
1361 return pet_expr_op_get_type(tree
->u
.e
.expr
) == type
;
1364 /* Is "tree" an expression tree that performs a kill operation?
1366 int pet_tree_is_kill(__isl_keep pet_tree
*tree
)
1368 return pet_tree_is_op_of_type(tree
, pet_op_kill
);
1371 /* Is "tree" an expression tree that performs an assignment operation?
1373 int pet_tree_is_assign(__isl_keep pet_tree
*tree
)
1375 return pet_tree_is_op_of_type(tree
, pet_op_assign
);
1378 /* Is "tree" an expression tree that performs an assume operation?
1380 int pet_tree_is_assume(__isl_keep pet_tree
*tree
)
1382 return pet_tree_is_op_of_type(tree
, pet_op_assume
);
1385 /* Is "tree" an expression tree that performs an assume operation
1386 * such that the assumed expression is affine?
1388 int pet_tree_is_affine_assume(__isl_keep pet_tree
*tree
)
1390 if (!pet_tree_is_assume(tree
))
1392 return pet_expr_is_affine(tree
->u
.e
.expr
->args
[0]);
1395 /* Given a tree that represent an assume operation expression
1396 * with an access as argument (possibly an affine expression),
1397 * return the index expression of that access.
1399 __isl_give isl_multi_pw_aff
*pet_tree_assume_get_index(
1400 __isl_keep pet_tree
*tree
)
1404 if (!pet_tree_is_assume(tree
))
1405 isl_die(pet_tree_get_ctx(tree
), isl_error_invalid
,
1406 "not an assume tree", return NULL
);
1407 return pet_expr_access_get_index(tree
->u
.e
.expr
->args
[0]);
1410 /* Internal data structure for pet_tree_writes.
1411 * "id" is the identifier that we are looking for.
1412 * "writes" is set if we have found the identifier being written to.
1414 struct pet_tree_writes_data
{
1419 /* Check if expr writes to data->id.
1420 * If so, set data->writes and abort the search.
1422 static int check_write(__isl_keep pet_expr
*expr
, void *user
)
1424 struct pet_tree_writes_data
*data
= user
;
1426 data
->writes
= pet_expr_writes(expr
, data
->id
);
1427 if (data
->writes
< 0 || data
->writes
)
1433 /* Is there any write access in "tree" that accesses "id"?
1435 int pet_tree_writes(__isl_keep pet_tree
*tree
, __isl_keep isl_id
*id
)
1437 struct pet_tree_writes_data data
;
1441 if (pet_tree_foreach_expr(tree
, &check_write
, &data
) < 0 &&
1448 /* Wrapper around pet_expr_update_domain
1449 * for use as a pet_tree_map_expr callback.
1451 static __isl_give pet_expr
*update_domain(__isl_take pet_expr
*expr
, void *user
)
1453 isl_multi_pw_aff
*update
= user
;
1455 return pet_expr_update_domain(expr
, isl_multi_pw_aff_copy(update
));
1458 /* Modify all access relations in "tree" by precomposing them with
1459 * the given iteration space transformation.
1461 __isl_give pet_tree
*pet_tree_update_domain(__isl_take pet_tree
*tree
,
1462 __isl_take isl_multi_pw_aff
*update
)
1464 tree
= pet_tree_map_expr(tree
, &update_domain
, update
);
1465 isl_multi_pw_aff_free(update
);
1469 /* Does "tree" contain a continue or break node (not contained in any loop
1470 * subtree of "tree")?
1472 int pet_tree_has_continue_or_break(__isl_keep pet_tree
*tree
)
1480 switch (tree
->type
) {
1481 case pet_tree_error
:
1483 case pet_tree_continue
:
1484 case pet_tree_break
:
1487 case pet_tree_decl_init
:
1490 case pet_tree_while
:
1491 case pet_tree_infinite_loop
:
1493 case pet_tree_block
:
1494 for (i
= 0; i
< tree
->u
.b
.n
; ++i
) {
1496 pet_tree_has_continue_or_break(tree
->u
.b
.child
[i
]);
1497 if (found
< 0 || found
)
1502 return pet_tree_has_continue_or_break(tree
->u
.i
.then_body
);
1503 case pet_tree_if_else
:
1504 found
= pet_tree_has_continue_or_break(tree
->u
.i
.then_body
);
1505 if (found
< 0 || found
)
1507 return pet_tree_has_continue_or_break(tree
->u
.i
.else_body
);
1511 static void print_indent(int indent
)
1513 fprintf(stderr
, "%*s", indent
, "");
1516 void pet_tree_dump_with_indent(__isl_keep pet_tree
*tree
, int indent
)
1523 print_indent(indent
);
1524 fprintf(stderr
, "%s\n", pet_tree_type_str(tree
->type
));
1525 print_indent(indent
);
1526 fprintf(stderr
, "line: %d\n", pet_loc_get_line(tree
->loc
));
1527 print_indent(indent
);
1528 fprintf(stderr
, "start: %d\n", pet_loc_get_start(tree
->loc
));
1529 print_indent(indent
);
1530 fprintf(stderr
, "end: %d\n", pet_loc_get_end(tree
->loc
));
1532 print_indent(indent
);
1533 fprintf(stderr
, "label: ");
1534 isl_id_dump(tree
->label
);
1536 switch (tree
->type
) {
1537 case pet_tree_block
:
1538 print_indent(indent
);
1539 fprintf(stderr
, "block: %d\n", tree
->u
.b
.block
);
1540 for (i
= 0; i
< tree
->u
.b
.n
; ++i
)
1541 pet_tree_dump_with_indent(tree
->u
.b
.child
[i
],
1545 pet_expr_dump_with_indent(tree
->u
.e
.expr
, indent
);
1547 case pet_tree_break
:
1548 case pet_tree_continue
:
1551 case pet_tree_decl_init
:
1552 print_indent(indent
);
1553 fprintf(stderr
, "var:\n");
1554 pet_expr_dump_with_indent(tree
->u
.d
.var
, indent
+ 2);
1555 if (tree
->type
!= pet_tree_decl_init
)
1557 print_indent(indent
);
1558 fprintf(stderr
, "init:\n");
1559 pet_expr_dump_with_indent(tree
->u
.d
.init
, indent
+ 2);
1562 case pet_tree_if_else
:
1563 print_indent(indent
);
1564 fprintf(stderr
, "condition:\n");
1565 pet_expr_dump_with_indent(tree
->u
.i
.cond
, indent
+ 2);
1566 print_indent(indent
);
1567 fprintf(stderr
, "then:\n");
1568 pet_tree_dump_with_indent(tree
->u
.i
.then_body
, indent
+ 2);
1569 if (tree
->type
!= pet_tree_if_else
)
1571 print_indent(indent
);
1572 fprintf(stderr
, "else:\n");
1573 pet_tree_dump_with_indent(tree
->u
.i
.else_body
, indent
+ 2);
1576 print_indent(indent
);
1577 fprintf(stderr
, "declared: %d\n", tree
->u
.l
.declared
);
1578 print_indent(indent
);
1579 fprintf(stderr
, "var:\n");
1580 pet_expr_dump_with_indent(tree
->u
.l
.iv
, indent
+ 2);
1581 print_indent(indent
);
1582 fprintf(stderr
, "init:\n");
1583 pet_expr_dump_with_indent(tree
->u
.l
.init
, indent
+ 2);
1584 print_indent(indent
);
1585 fprintf(stderr
, "inc:\n");
1586 pet_expr_dump_with_indent(tree
->u
.l
.inc
, indent
+ 2);
1587 case pet_tree_while
:
1588 print_indent(indent
);
1589 fprintf(stderr
, "condition:\n");
1590 pet_expr_dump_with_indent(tree
->u
.l
.cond
, indent
+ 2);
1591 case pet_tree_infinite_loop
:
1592 print_indent(indent
);
1593 fprintf(stderr
, "body:\n");
1594 pet_tree_dump_with_indent(tree
->u
.l
.body
, indent
+ 2);
1596 case pet_tree_error
:
1597 print_indent(indent
);
1598 fprintf(stderr
, "ERROR\n");
1603 void pet_tree_dump(__isl_keep pet_tree
*tree
)
1605 pet_tree_dump_with_indent(tree
, 0);