}; // class pass_fold_builtins
-/* Optimize memcmp STMT into memcmp_eq if it is only used with
- `== 0` or `!= 0`. */
-
-static void
-optimize_memcmp_eq (gcall *stmt)
-{
- /* Make sure memcmp arguments are the correct type. */
- if (gimple_call_num_args (stmt) != 3)
- return;
- tree arg1 = gimple_call_arg (stmt, 0);
- tree arg2 = gimple_call_arg (stmt, 1);
- tree len = gimple_call_arg (stmt, 2);
-
- if (!POINTER_TYPE_P (TREE_TYPE (arg1)))
- return;
- if (!POINTER_TYPE_P (TREE_TYPE (arg2)))
- return;
- if (!INTEGRAL_TYPE_P (TREE_TYPE (len)))
- return;
- /* The return value of the memcmp has to be used
- equality comparison to zero. */
- tree res = gimple_call_lhs (stmt);
-
- if (!res || !use_in_zero_equality (res))
- return;
-
- gimple_call_set_fndecl (stmt, builtin_decl_explicit (BUILT_IN_MEMCMP_EQ));
- update_stmt (stmt);
-}
-
unsigned int
pass_fold_builtins::execute (function *fun)
{
gsi_next (&i);
continue;
- case BUILT_IN_MEMCMP:
- optimize_memcmp_eq (as_a<gcall*>(stmt));
- break;
-
case BUILT_IN_UNREACHABLE:
if (optimize_unreachable (i))
cfg_changed = true;
return true;
}
}
- return false;
+
+ /* Replace memcmp with memcmp_eq if the above fails. */
+ if (DECL_FUNCTION_CODE (gimple_call_fndecl (stmt)) == BUILT_IN_MEMCMP_EQ)
+ return false;
+ if (!(cfun->curr_properties & (PROP_last_full_fold)))
+ return false;
+ gimple_call_set_fndecl (stmt, builtin_decl_explicit (BUILT_IN_MEMCMP_EQ));
+ update_stmt (stmt);
+ return true;
}
/* Optimizes builtin memchrs for small constant sizes with a const string.