From 070ab283d16d8e8e8bb70f9801aca347f008cbd0 Mon Sep 17 00:00:00 2001 From: Jakub Jelinek Date: Thu, 11 Mar 2021 10:59:18 +0100 Subject: [PATCH] icf: Check return type of internal fn calls [PR99517] The following testcase is miscompiled, because IPA-ICF considers the two functions identical. They aren't, the types of the .VEC_CONVERT call lhs is different. But for calls to internal functions, there is no fntype nor callee with a function type to compare, so all we compare is just the ifn, arguments and some call flags. The following patch fixes it by checking the internal fn calls like e.g. gimple assignments where the type of the lhs is checked too. 2021-03-11 Jakub Jelinek PR ipa/99517 * ipa-icf-gimple.c (func_checker::compare_gimple_call): For internal function calls with lhs fail if the lhs don't have compatible types. * gcc.target/i386/avx2-pr99517-1.c: New test. * gcc.target/i386/avx2-pr99517-2.c: New test. --- gcc/ipa-icf-gimple.c | 10 +++++++- .../gcc.target/i386/avx2-pr99517-1.c | 25 +++++++++++++++++++ .../gcc.target/i386/avx2-pr99517-2.c | 20 +++++++++++++++ 3 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/gcc.target/i386/avx2-pr99517-1.c create mode 100644 gcc/testsuite/gcc.target/i386/avx2-pr99517-2.c diff --git a/gcc/ipa-icf-gimple.c b/gcc/ipa-icf-gimple.c index 3bc168d8a38..edf5f025627 100644 --- a/gcc/ipa-icf-gimple.c +++ b/gcc/ipa-icf-gimple.c @@ -667,7 +667,7 @@ func_checker::compare_gimple_call (gcall *s1, gcall *s2) tree fntype1 = gimple_call_fntype (s1); tree fntype2 = gimple_call_fntype (s2); - /* For direct calls we verify that types are comopatible so if we matced + /* For direct calls we verify that types are compatible so if we matched callees, callers must match, too. For indirect calls however verify function type. */ if (!gimple_call_fndecl (s1)) @@ -703,6 +703,14 @@ func_checker::compare_gimple_call (gcall *s1, gcall *s2) t1 = gimple_get_lhs (s1); t2 = gimple_get_lhs (s2); + /* For internal calls, lhs types need to be verified, as neither fntype nor + callee comparisons can catch that. */ + if (gimple_call_internal_p (s1) + && t1 + && t2 + && !compatible_types_p (TREE_TYPE (t1), TREE_TYPE (t2))) + return return_false_with_msg ("GIMPLE internal call LHS type mismatch"); + return compare_operand (t1, t2, get_operand_access_type (&map, t1)); } diff --git a/gcc/testsuite/gcc.target/i386/avx2-pr99517-1.c b/gcc/testsuite/gcc.target/i386/avx2-pr99517-1.c new file mode 100644 index 00000000000..f1d7f82cde8 --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/avx2-pr99517-1.c @@ -0,0 +1,25 @@ +/* PR ipa/99517 */ +/* { dg-do run { target avx2 } } */ +/* { dg-additional-sources "avx2-pr99517-2.c" } */ +/* { dg-options "-O2 -mavx2" } */ + +#include "avx2-check.h" + +typedef signed char v32qi __attribute__((vector_size(32))); +typedef int v4si __attribute__((vector_size(16))); +typedef long long int v4di __attribute__((vector_size(32))); +typedef double v4df __attribute__((vector_size(32))); +extern v32qi foo (v4si); +extern v32qi bar (v4si); + +static void +avx2_test (void) +{ + v4si a = { 1, -2, 3, -4 }; + __asm ("" : "+x" (a)); + v4di b = (v4di) bar (a); + v4df c = (v4df) foo (a); + if (b[0] != 1 || c[0] != 1.0 || b[1] != -2 || c[1] != -2.0 + || b[2] != 3 || c[2] != 3.0 || b[3] != -4 || c[3] != -4.0) + __builtin_abort (); +} diff --git a/gcc/testsuite/gcc.target/i386/avx2-pr99517-2.c b/gcc/testsuite/gcc.target/i386/avx2-pr99517-2.c new file mode 100644 index 00000000000..80bd39585cb --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/avx2-pr99517-2.c @@ -0,0 +1,20 @@ +/* PR ipa/99517 */ +/* { dg-do compile } */ +/* { dg-options "-O2 -mavx2" } */ + +typedef signed char v32qi __attribute__((vector_size(32))); +typedef int v4si __attribute__((vector_size(16))); +typedef long long int v4di __attribute__((vector_size(32))); +typedef double v4df __attribute__((vector_size(32))); + +v32qi +foo (v4si x) +{ + return (v32qi) __builtin_convertvector (x, v4df); +} + +v32qi +bar (v4si x) +{ + return (v32qi) __builtin_convertvector (x, v4di); +}