From 494d2314b27bab0ecd37eb2bb2a74ec85d3e2dfd Mon Sep 17 00:00:00 2001 From: Sven Verdoolaege Date: Sat, 4 Jan 2014 20:35:30 +0100 Subject: [PATCH] pet_expr_extract_affine: exploit domain information while extracting divs/mods An integer division in C is represented by a piecewise affine expression because the result is different for negative and positive values of the lhs. If we know that this lhs is positive (or negative), then we can avoid adding extra pieces, so it is worthwhile to try and simplify the extracted affine expression in the context of the outer iterators domain. Signed-off-by: Sven Verdoolaege --- expr.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/expr.c b/expr.c index 3a462f4..351630f 100644 --- a/expr.c +++ b/expr.c @@ -2182,6 +2182,9 @@ static __isl_give isl_pw_aff *extract_affine_add_sub(__isl_keep pet_expr *expr, * * If the second argument (rhs) is not a (positive) integer constant, * then we fail to extract an affine expression. + * + * We simplify the result in the context of the domain of "pc" in case + * this domain implies that lhs >= 0 (or < 0). */ static __isl_give isl_pw_aff *extract_affine_div_mod(__isl_keep pet_expr *expr, __isl_keep pet_context *pc) @@ -2189,6 +2192,7 @@ static __isl_give isl_pw_aff *extract_affine_div_mod(__isl_keep pet_expr *expr, int is_cst; isl_pw_aff *lhs; isl_pw_aff *rhs; + isl_pw_aff *res; if (!expr) return NULL; @@ -2208,9 +2212,11 @@ static __isl_give isl_pw_aff *extract_affine_div_mod(__isl_keep pet_expr *expr, switch (pet_expr_op_get_type(expr)) { case pet_op_div: - return isl_pw_aff_tdiv_q(lhs, rhs); + res = isl_pw_aff_tdiv_q(lhs, rhs); + break; case pet_op_mod: - return isl_pw_aff_tdiv_r(lhs, rhs); + res = isl_pw_aff_tdiv_r(lhs, rhs); + break; default: isl_pw_aff_free(lhs); isl_pw_aff_free(rhs); @@ -2218,6 +2224,7 @@ static __isl_give isl_pw_aff *extract_affine_div_mod(__isl_keep pet_expr *expr, "not a div or mod operator", return NULL); } + return isl_pw_aff_gist(res, pet_context_get_gist_domain(pc)); } /* Extract an affine expression from a multiplication operation. -- 2.11.4.GIT