diff --git a/gcc/graphite-isl-ast-to-gimple.c b/gcc/graphite-isl-ast-to-gimple.c index d3f0153..f2811cd 100644 --- a/gcc/graphite-isl-ast-to-gimple.c +++ b/gcc/graphite-isl-ast-to-gimple.c @@ -20,6 +20,7 @@ along with GCC; see the file COPYING3. If not see #include "config.h" +#include #include #include #include @@ -49,6 +50,241 @@ along with GCC; see the file COPYING3. If not see static bool graphite_regenerate_error; + +/* Converts a GMP constant VAL to a tree and returns it. */ + +static tree +gmp_cst_to_tree (tree type, mpz_t val) +{ + tree t = type ? type : integer_type_node; + mpz_t tmp; + + mpz_init (tmp); + mpz_set (tmp, val); + wide_int wi = wi::from_mpz (t, tmp, true); + mpz_clear (tmp); + + return wide_int_to_tree (t, wi); +} + +static tree +gcc_expression_from_isl_expression (tree type, __isl_keep isl_ast_expr *); + +/* Converts a isl_ast_expr_int expression E to a GCC expression tree of + type TYPE. */ + +static tree +gcc_expression_from_isl_expr_int (tree type, __isl_keep isl_ast_expr *expr) +{ + gcc_assert (isl_ast_expr_get_type (expr) == isl_ast_expr_int); + isl_int val; + isl_int_init (val); + if (isl_ast_expr_get_int (expr, &val) == -1) + { + isl_int_clear (val); + return NULL_TREE; + } + else + return gmp_cst_to_tree (type, val); +} + +/* Converts a binary isl_ast_expr_op expression E to a GCC expression tree of + type TYPE. */ + +static tree +binary_op_to_tree (tree type, __isl_keep isl_ast_expr *expr) +{ + isl_ast_expr *arg_expr = isl_ast_expr_get_op_arg (expr, 0); + tree tree_lhs_expr = gcc_expression_from_isl_expression (type, arg_expr); + isl_ast_expr_free (arg_expr); + arg_expr = isl_ast_expr_get_op_arg (expr, 1); + tree tree_rhs_expr = gcc_expression_from_isl_expression (type, arg_expr); + isl_ast_expr_free (arg_expr); + switch (isl_ast_expr_get_op_type (expr)) + { + case isl_ast_op_add: + return fold_build2 (PLUS_EXPR, type, tree_lhs_expr, tree_rhs_expr); + + case isl_ast_op_sub: + return fold_build2 (MINUS_EXPR, type, tree_lhs_expr, tree_rhs_expr); + + case isl_ast_op_mul: + return fold_build2 (MULT_EXPR, type, tree_lhs_expr, tree_rhs_expr); + + case isl_ast_op_div: + return fold_build2 (EXACT_DIV_EXPR, type, tree_lhs_expr, tree_rhs_expr); + + case isl_ast_op_fdiv_q: + return fold_build2 (FLOOR_DIV_EXPR, type, tree_lhs_expr, tree_rhs_expr); + + case isl_ast_op_and: + return fold_build2 (TRUTH_ANDIF_EXPR, type, + tree_lhs_expr, tree_rhs_expr); + + case isl_ast_op_or: + return fold_build2 (TRUTH_ORIF_EXPR, type, tree_lhs_expr, tree_rhs_expr); + + case isl_ast_op_eq: + return fold_build2 (EQ_EXPR, type, tree_lhs_expr, tree_rhs_expr); + + case isl_ast_op_le: + return fold_build2 (LE_EXPR, type, tree_lhs_expr, tree_rhs_expr); + + case isl_ast_op_lt: + return fold_build2 (LT_EXPR, type, tree_lhs_expr, tree_rhs_expr); + + case isl_ast_op_ge: + return fold_build2 (GE_EXPR, type, tree_lhs_expr, tree_rhs_expr); + + case isl_ast_op_gt: + return fold_build2 (GT_EXPR, type, tree_lhs_expr, tree_rhs_expr); + + default: + gcc_unreachable (); + } +} + +/* Converts a unary isl_ast_expr_op expression E to a GCC expression tree of + type TYPE. */ + +static tree +unary_op_to_tree (tree type, __isl_keep isl_ast_expr *expr) +{ + gcc_assert (isl_ast_expr_get_op_type (expr) == isl_ast_op_minus); + isl_ast_expr *arg_expr = isl_ast_expr_get_op_arg (expr, 0); + tree tree_first_expr = gcc_expression_from_isl_expression (type, arg_expr); + isl_ast_expr_free (arg_expr); + arg_expr = isl_ast_expr_get_op_arg (expr, 1); + tree tree_second_expr = gcc_expression_from_isl_expression (type, arg_expr); + isl_ast_expr_free (arg_expr); + arg_expr = isl_ast_expr_get_op_arg (expr, 2); + tree tree_third_expr = gcc_expression_from_isl_expression (type, arg_expr); + isl_ast_expr_free (arg_expr); + return fold_build3 (COND_EXPR, type, tree_first_expr, + tree_second_expr, tree_third_expr); +} + +/* Converts a ternary isl_ast_expr_op expression E to a GCC expression tree of + type TYPE. */ + +static tree +ternary_op_to_tree (tree type, __isl_keep isl_ast_expr *expr) +{ + gcc_assert (isl_ast_expr_get_op_type (expr) == isl_ast_op_cond); + isl_ast_expr *arg_expr = isl_ast_expr_get_op_arg (expr, 0); + tree tree_expr = gcc_expression_from_isl_expression (type, arg_expr); + isl_ast_expr_free (arg_expr); + return fold_build1 (NEGATE_EXPR, type, tree_expr); +} + +/* Converts a isl_ast_expr_op expression E with unknown number of arguments + to a GCC expression tree of type TYPE. */ + +static tree +nary_op_to_tree (tree type, __isl_keep isl_ast_expr *expr) +{ + enum tree_code op_code; + switch (isl_ast_expr_get_op_type (expr)) + { + case isl_ast_op_max: + op_code = MAX_EXPR; + break; + + case isl_ast_op_min: + op_code = MIN_EXPR; + break; + + default: + gcc_unreachable (); + } + isl_ast_expr *arg_expr = isl_ast_expr_get_op_arg (expr, 0); + tree res = gcc_expression_from_isl_expression (type, arg_expr); + isl_ast_expr_free (arg_expr); + int i; + for (i = 1; i < isl_ast_expr_get_op_n_arg(expr); i++) + { + arg_expr = isl_ast_expr_get_op_arg (expr, i); + tree t = gcc_expression_from_isl_expression (type, arg_expr); + res = fold_build2 (op_code, type, res, t); + isl_ast_expr_free (arg_expr); + } + return res; +} + + +/* Converts a isl_ast_expr_op expression E to a GCC expression tree of + type TYPE. */ + +static tree +gcc_expression_from_isl_expr_op (tree type, __isl_keep isl_ast_expr *expr) +{ + gcc_assert (isl_ast_expr_get_type (expr) == isl_ast_expr_op); + switch (isl_ast_expr_get_op_type (expr)) + { + case isl_ast_op_error: + case isl_ast_op_call: + case isl_ast_op_and_then: + case isl_ast_op_or_else: + case isl_ast_op_pdiv_q: + case isl_ast_op_pdiv_r: + case isl_ast_op_select: + gcc_unreachable (); + + case isl_ast_op_max: + case isl_ast_op_min: + return nary_op_to_tree (type, expr); + + case isl_ast_op_add: + case isl_ast_op_sub: + case isl_ast_op_mul: + case isl_ast_op_div: + case isl_ast_op_fdiv_q: + case isl_ast_op_and: + case isl_ast_op_or: + case isl_ast_op_eq: + case isl_ast_op_le: + case isl_ast_op_lt: + case isl_ast_op_ge: + case isl_ast_op_gt: + return binary_op_to_tree (type, expr); + + case isl_ast_op_minus: + return unary_op_to_tree (type, expr); + + case isl_ast_op_cond: + return ternary_op_to_tree (type, expr); + + default: + gcc_unreachable (); + } + + return NULL_TREE; +} + +/* Converts a ISL AST expression E back to a GCC expression tree of + type TYPE. */ + +static tree +gcc_expression_from_isl_expression (tree type, __isl_keep isl_ast_expr *expr) +{ + switch (isl_ast_expr_get_type (expr)) + { + case isl_ast_expr_id: + gcc_unreachable (); + + case isl_ast_expr_int: + return gcc_expression_from_isl_expr_int (type, expr); + + case isl_ast_expr_op: + return gcc_expression_from_isl_expr_op (type, expr); + + default: + gcc_unreachable (); + } + + return NULL_TREE; +} + /* Prints NODE to FILE. */ void