From: Sven Verdoolaege Date: Wed, 3 Jul 2013 11:21:45 +0000 (+0200) Subject: add isl_ast_expr_substitute_ids X-Git-Tag: isl-0.13~145 X-Git-Url: https://repo.or.cz/w/isl.git/commitdiff_plain/fcddea268e1cadd50f4dabdbe649a039657e5288 add isl_ast_expr_substitute_ids Signed-off-by: Sven Verdoolaege --- diff --git a/doc/user.pod b/doc/user.pod index fe9ad511..a4d55eb7 100644 --- a/doc/user.pod +++ b/doc/user.pod @@ -6112,6 +6112,13 @@ the context of an C. __isl_give isl_ast_expr *isl_ast_expr_access( __isl_take isl_ast_expr *array, __isl_take isl_ast_expr_list *indices); + __isl_give isl_ast_expr *isl_ast_expr_substitute_ids( + __isl_take isl_ast_expr *expr, + __isl_take isl_id_to_ast_expr *id2expr); + +The function C replaces the +subexpressions of C of type C +by the corresponding expression in C, if there is any. #include __isl_give isl_ast_expr *isl_ast_build_expr_from_pw_aff( diff --git a/include/isl/ast.h b/include/isl/ast.h index bebb2ebd..2a40582d 100644 --- a/include/isl/ast.h +++ b/include/isl/ast.h @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -49,6 +50,9 @@ __isl_give isl_ast_expr *isl_ast_expr_get_op_arg(__isl_keep isl_ast_expr *expr, int isl_ast_expr_is_equal(__isl_keep isl_ast_expr *expr1, __isl_keep isl_ast_expr *expr2); +__isl_give isl_ast_expr *isl_ast_expr_substitute_ids( + __isl_take isl_ast_expr *expr, __isl_take isl_id_to_ast_expr *id2expr); + __isl_give isl_printer *isl_printer_print_ast_expr(__isl_take isl_printer *p, __isl_keep isl_ast_expr *expr); void isl_ast_expr_dump(__isl_keep isl_ast_expr *expr); diff --git a/include/isl/id_to_ast_expr.h b/include/isl/id_to_ast_expr.h index 0ced87af..958360c3 100644 --- a/include/isl/id_to_ast_expr.h +++ b/include/isl/id_to_ast_expr.h @@ -2,7 +2,7 @@ #define ISL_ID_TO_AST_EXPR_H #include -#include +#include #define ISL_KEY_BASE id #define ISL_VAL_BASE ast_expr diff --git a/isl_ast.c b/isl_ast.c index 93eff2cf..63fc2431 100644 --- a/isl_ast.c +++ b/isl_ast.c @@ -591,6 +591,63 @@ error: return NULL; } +/* For each subexpression of "expr" of type isl_ast_expr_id, + * if it appears in "id2expr", then replace it by the corresponding + * expression. + */ +__isl_give isl_ast_expr *isl_ast_expr_substitute_ids( + __isl_take isl_ast_expr *expr, __isl_take isl_id_to_ast_expr *id2expr) +{ + int i; + isl_id *id; + + if (!expr || !id2expr) + goto error; + + switch (expr->type) { + case isl_ast_expr_int: + break; + case isl_ast_expr_id: + if (!isl_id_to_ast_expr_has(id2expr, expr->u.id)) + break; + id = isl_id_copy(expr->u.id); + isl_ast_expr_free(expr); + expr = isl_id_to_ast_expr_get(id2expr, id); + break; + case isl_ast_expr_op: + for (i = 0; i < expr->u.op.n_arg; ++i) { + isl_ast_expr *arg; + arg = isl_ast_expr_copy(expr->u.op.args[i]); + arg = isl_ast_expr_substitute_ids(arg, + isl_id_to_ast_expr_copy(id2expr)); + if (arg == expr->u.op.args[i]) { + isl_ast_expr_free(arg); + continue; + } + if (!arg) + expr = isl_ast_expr_free(expr); + expr = isl_ast_expr_cow(expr); + if (!expr) { + isl_ast_expr_free(arg); + break; + } + isl_ast_expr_free(expr->u.op.args[i]); + expr->u.op.args[i] = arg; + } + break; + case isl_ast_expr_error: + expr = isl_ast_expr_free(expr); + break; + } + + isl_id_to_ast_expr_free(id2expr); + return expr; +error: + isl_ast_expr_free(expr); + isl_id_to_ast_expr_free(id2expr); + return NULL; +} + isl_ctx *isl_ast_node_get_ctx(__isl_keep isl_ast_node *node) { return node ? node->ctx : NULL; diff --git a/isl_id_to_ast_expr.c b/isl_id_to_ast_expr.c index a0f6080e..f8ac7f7c 100644 --- a/isl_id_to_ast_expr.c +++ b/isl_id_to_ast_expr.c @@ -1,4 +1,5 @@ #include +#include #define isl_id_is_equal(id1,id2) id1 == id2