Hi! The following patch does the same check the tree-ssa-strlen.c pass does to punt on broken builtin redeclarations. I agree the ultimate fix is change the C FE, but don't feel I know that part of the FE good enough to know all the consequences and so it might be too risky for stage4.
Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2020-02-26 Jakub Jelinek <ja...@redhat.com> PR tree-optimization/93927 * gimple-fold.c (gimple_fold_builtin): Punt if callee has incompatible prototype. * gcc.c-torture/compile/pr93927-1.c: New test. * gcc.c-torture/compile/pr93927-2.c: New test. --- gcc/gimple-fold.c.jj 2020-02-06 09:13:21.732085293 +0100 +++ gcc/gimple-fold.c 2020-02-25 11:46:14.863796864 +0100 @@ -3906,6 +3906,12 @@ gimple_fold_builtin (gimple_stmt_iterato if (avoid_folding_inline_builtin (callee)) return false; + tree decl = builtin_decl_explicit (DECL_FUNCTION_CODE (callee)); + if (decl + && decl != callee + && !gimple_builtin_call_types_compatible_p (stmt, decl)) + return false; + unsigned n = gimple_call_num_args (stmt); enum built_in_function fcode = DECL_FUNCTION_CODE (callee); switch (fcode) --- gcc/testsuite/gcc.c-torture/compile/pr93927-1.c.jj 2020-02-25 11:47:10.983971425 +0100 +++ gcc/testsuite/gcc.c-torture/compile/pr93927-1.c 2020-02-25 11:46:43.004382966 +0100 @@ -0,0 +1,9 @@ +/* PR tree-optimization/93927 */ + +__SIZE_TYPE__ strstr (const char *, const char *); + +char * +foo (char *x) +{ + return !!strstr (x, "0") ? "0" : "1"; +} --- gcc/testsuite/gcc.c-torture/compile/pr93927-2.c.jj 2020-02-25 11:47:13.930928081 +0100 +++ gcc/testsuite/gcc.c-torture/compile/pr93927-2.c 2020-02-25 11:46:57.304172632 +0100 @@ -0,0 +1,9 @@ +/* PR tree-optimization/93927 */ + +__SIZE_TYPE__ strchr (const char *, int); + +char * +foo (char *x) +{ + return !!strchr (x, 0) ? "0" : "1"; +} Jakub