From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1984) id 7AAA3385843D; Sun, 12 Mar 2023 18:43:55 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7AAA3385843D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1678646635; bh=FZY4/o6SKqEB0soeVDOlwUR5DgjPuW6sV8jgd5hwyrg=; h=From:To:Subject:Date:From; b=YSEDm7o49O5JY2jahWMHEFSDadKqO34EC01ufrOwtWn3pxLJ3C23kCQNw1JB1AhD0 I4h28Sgg8cwIQpes/wmU7MlnaTzetzmc1Yj3Y8ceqMJTLpzR3gEnG9I9llnVUhzWid a+pYwa9rW4GunRdSgLpgY2ywt6Cqy8wM5jX2MD78= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Tamar Christina To: gcc-cvs@gcc.gnu.org Subject: [gcc r13-6618] middle-end: don't form FMAs when multiplication is not single use. [PR108583] X-Act-Checkin: gcc X-Git-Author: Tamar Christina X-Git-Refname: refs/heads/master X-Git-Oldrev: 03c6ba86757f0684c5419c90651106900f5ecb5a X-Git-Newrev: 0b3c630fcc44063a61f6131af48a4171b1de2b37 Message-Id: <20230312184355.7AAA3385843D@sourceware.org> Date: Sun, 12 Mar 2023 18:43:55 +0000 (GMT) List-Id: https://gcc.gnu.org/g:0b3c630fcc44063a61f6131af48a4171b1de2b37 commit r13-6618-g0b3c630fcc44063a61f6131af48a4171b1de2b37 Author: Tamar Christina Date: Sun Mar 12 18:40:50 2023 +0000 middle-end: don't form FMAs when multiplication is not single use. [PR108583] The testcase typedef unsigned int vec __attribute__((vector_size(32))); vec f3 (vec a, vec b, vec c) { vec d = a * b; return d + ((c + d) >> 1); } shows a case where we don't want to form an FMA due to the MUL not being single use. In this case to form an FMA we have to redo the MUL as well as we no longer have it to share. As such making an FMA here would be a de-optimization. gcc/ChangeLog: PR target/108583 * tree-ssa-math-opts.cc (convert_mult_to_fma): Inhibit FMA in case not single use. gcc/testsuite/ChangeLog: PR target/108583 * gcc.dg/mla_1.c: New test. Co-Authored-By: Richard Sandiford Diff: --- gcc/testsuite/gcc.dg/mla_1.c | 40 ++++++++++++++++++++++++++++++++++++++++ gcc/tree-ssa-math-opts.cc | 14 ++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/gcc/testsuite/gcc.dg/mla_1.c b/gcc/testsuite/gcc.dg/mla_1.c new file mode 100644 index 00000000000..98e5808ee70 --- /dev/null +++ b/gcc/testsuite/gcc.dg/mla_1.c @@ -0,0 +1,40 @@ +/* { dg-do compile } */ +/* { dg-require-effective-target vect_int } */ +/* { dg-options "-O2 -msve-vector-bits=256 -march=armv8.2-a+sve -fdump-tree-optimized" { target aarch64*-*-* } } */ + +unsigned int +f1 (unsigned int a, unsigned int b, unsigned int c) { + unsigned int d = a * b; + return d + ((c + d) >> 1); +} + +unsigned int +g1 (unsigned int a, unsigned int b, unsigned int c) { + return a * b + c; +} + +__Uint32x4_t +f2 (__Uint32x4_t a, __Uint32x4_t b, __Uint32x4_t c) { + __Uint32x4_t d = a * b; + return d + ((c + d) >> 1); +} + +__Uint32x4_t +g2 (__Uint32x4_t a, __Uint32x4_t b, __Uint32x4_t c) { + return a * b + c; +} + +typedef unsigned int vec __attribute__((vector_size(32))); vec +f3 (vec a, vec b, vec c) +{ + vec d = a * b; + return d + ((c + d) >> 1); +} + +vec +g3 (vec a, vec b, vec c) +{ + return a * b + c; +} + +/* { dg-final { scan-tree-dump-times {\.FMA } 1 "optimized" { target aarch64*-*-* } } } */ diff --git a/gcc/tree-ssa-math-opts.cc b/gcc/tree-ssa-math-opts.cc index 5ab5b944a57..26ed91d58fa 100644 --- a/gcc/tree-ssa-math-opts.cc +++ b/gcc/tree-ssa-math-opts.cc @@ -3346,6 +3346,20 @@ convert_mult_to_fma (gimple *mul_stmt, tree op1, tree op2, param_avoid_fma_max_bits)); bool defer = check_defer; bool seen_negate_p = false; + + /* There is no numerical difference between fused and unfused integer FMAs, + and the assumption below that FMA is as cheap as addition is unlikely + to be true, especially if the multiplication occurs multiple times on + the same chain. E.g., for something like: + + (((a * b) + c) >> 1) + (a * b) + + we do not want to duplicate the a * b into two additions, not least + because the result is not a natural FMA chain. */ + if (ANY_INTEGRAL_TYPE_P (type) + && !has_single_use (mul_result)) + return false; + /* Make sure that the multiplication statement becomes dead after the transformation, thus that all uses are transformed to FMAs. This means we assume that an FMA operation has the same cost