fold strlen (s) eq/ne 0 to *s eq/ne 0 on GIMPLE

Andrew Pinski pinskia@gmail.com
Mon Aug 1 07:35:00 GMT 2016


On Mon, Aug 1, 2016 at 12:22 AM, Andrew Pinski <pinskia@gmail.com> wrote:
> On Mon, Aug 1, 2016 at 12:15 AM, Prathamesh Kulkarni
> <prathamesh.kulkarni@linaro.org> wrote:
>> Hi Richard,
>> The attached patch tries to fold strlen (s) eq/ne 0 to *s eq/ne 0 on GIMPLE.
>> I am not sure where was the ideal place to put this transform in and ended up
>> adding it to strlen_optimize_stmt().
>> Does that look OK ?
>
> I suspect it might be better in match.pd.

The main reason is it is already in fold-const.c:
      /* Optimize comparisons of strlen vs zero to a compare of the
         first character of the string vs zero.  To wit,
                strlen(ptr) == 0   =>  *ptr == 0
                strlen(ptr) != 0   =>  *ptr != 0
         Other cases should reduce to one of these two (or a constant)
         due to the return value of strlen being unsigned.  */
      if (TREE_CODE (arg0) == CALL_EXPR
          && integer_zerop (arg1))
        {
          tree fndecl = get_callee_fndecl (arg0);

          if (fndecl
              && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
              && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN
              && call_expr_nargs (arg0) == 1
              && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0))) ==
POINTER_TYPE)
            {
              tree iref = build_fold_indirect_ref_loc (loc,
                                                   CALL_EXPR_ARG (arg0, 0));
              return fold_build2_loc (loc, code, type, iref,
                                  build_int_cst (TREE_TYPE (iref), 0));
            }
        }

So you are basically moving that to match.pd instead of adding extra code.


Thanks,
Andrew Pinski

>
>>
>> I needed to add TODO_update_ssa to strlen pass, otherwise we hit the
>> following assert in execute_todo():
>> if (flag_checking
>>       && cfun
>>       && need_ssa_update_p (cfun))
>>     gcc_assert (flags & TODO_update_ssa_any);
>
> Also you only need to update the virtual SSAs rather than a full SSA update.
>
> Thanks,
> Andrew
>
>>
>> Bootstrap+test in progress on x86_64-unknown-linux-gnu.
>>
>> Thanks,
>> Prathamesh



More information about the Gcc-patches mailing list