https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66552
Li Jia He <helijia at gcc dot gnu.org> changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |helijia at gcc dot gnu.org --- Comment #5 from Li Jia He <helijia at gcc dot gnu.org> --- Could we consider doing this optimization on gimple? I use the following code on gimple to produce optimized results on powerpc64. diff --git a/gcc/fold-const.c b/gcc/fold-const.c index aefa91666e2..a40681b271f 100644 --- a/gcc/fold-const.c +++ b/gcc/fold-const.c @@ -11131,7 +11131,6 @@ fold_binary_loc (location_t loc, enum tree_code code, tree type, WARN_STRICT_OVERFLOW_MISC); return fold_convert_loc (loc, type, tem); } - return NULL_TREE; case CEIL_MOD_EXPR: @@ -11191,6 +11190,22 @@ fold_binary_loc (location_t loc, enum tree_code code, tree type, prec) == 0) return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0)); + if (code == RSHIFT_EXPR + && (TREE_CODE (arg1) == CEIL_MOD_EXPR + || TREE_CODE (arg1) == FLOOR_MOD_EXPR + || TREE_CODE (arg1) == ROUND_MOD_EXPR + || TREE_CODE (arg1) == TRUNC_MOD_EXPR) + && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST + && integer_pow2p (TREE_OPERAND (arg1, 1))) + { + tree arg10 = TREE_OPERAND (arg1, 0); + tree arg11 = TREE_OPERAND (arg1, 1); + return fold_build2_loc (loc, code, type, arg0, + fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE(arg10), arg10, + fold_build2_loc (loc, MINUS_EXPR, TREE_TYPE(arg11), arg11, + build_one_cst(TREE_TYPE(arg11))))); + } + return NULL_TREE; case MIN_EXPR: