]> git.ipfire.org Git - thirdparty/gcc.git/commit
Match: Simplify branch form 3 of unsigned SAT_ADD into branchless
authorPan Li <pan2.li@intel.com>
Thu, 24 Oct 2024 13:57:04 +0000 (21:57 +0800)
committerPan Li <pan2.li@intel.com>
Fri, 25 Oct 2024 13:52:43 +0000 (21:52 +0800)
commitdf4af89bc3eabbeaccb16539aa1082cb9863e187
tree09ec974a2f102735fc3cb6f5c13b84b1f6c8fa3e
parente2a8772c9328960c625f5b95091d4312efa0e284
Match: Simplify branch form 3 of unsigned SAT_ADD into branchless

There are sorts of forms for the unsigned SAT_ADD.  Some of them are
complicated while others are cheap.  This patch would like to simplify
the complicated form into the cheap ones.  For example as below:

From the form 3 (branch):
  SAT_U_ADD = (X + Y) >= x ? (X + Y) : -1.

To (branchless):
  SAT_U_ADD = (X + Y) | - ((X + Y) < X).

  #define T uint8_t

  T sat_add_u_1 (T x, T y)
  {
    return (T)(x + y) >= x ? (x + y) : -1;
  }

Before this patch:
   1   │ uint8_t sat_add_u_1 (uint8_t x, uint8_t y)
   2   │ {
   3   │   uint8_t D.2809;
   4   │
   5   │   _1 = x + y;
   6   │   if (x <= _1) goto <D.2810>; else goto <D.2811>;
   7   │   <D.2810>:
   8   │   D.2809 = x + y;
   9   │   goto <D.2812>;
  10   │   <D.2811>:
  11   │   D.2809 = 255;
  12   │   <D.2812>:
  13   │   return D.2809;
  14   │ }

After this patch:
   1   │ uint8_t sat_add_u_1 (uint8_t x, uint8_t y)
   2   │ {
   3   │   uint8_t D.2809;
   4   │
   5   │   _1 = x + y;
   6   │   _2 = x + y;
   7   │   _3 = x > _2;
   8   │   _4 = (unsigned char) _3;
   9   │   _5 = -_4;
  10   │   D.2809 = _1 | _5;
  11   │   return D.2809;
  12   │ }

The simplify doesn't need to check if target support the SAT_ADD, it
is somehow the optimization in gimple level.

The below test suites are passed for this patch.
* The rv64gcv fully regression test.
* The x86 bootstrap test.
* The x86 fully regression test.

gcc/ChangeLog:

* match.pd: Remove unsigned branch form 3 for SAT_ADD, and
add simplify to branchless instead.

gcc/testsuite/ChangeLog:

* gcc.dg/tree-ssa/sat_u_add-simplify-1-u16.c: New test.
* gcc.dg/tree-ssa/sat_u_add-simplify-1-u32.c: New test.
* gcc.dg/tree-ssa/sat_u_add-simplify-1-u64.c: New test.
* gcc.dg/tree-ssa/sat_u_add-simplify-1-u8.c: New test.

Signed-off-by: Pan Li <pan2.li@intel.com>
gcc/match.pd
gcc/testsuite/gcc.dg/tree-ssa/sat_u_add-simplify-1-u16.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/tree-ssa/sat_u_add-simplify-1-u32.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/tree-ssa/sat_u_add-simplify-1-u64.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/tree-ssa/sat_u_add-simplify-1-u8.c [new file with mode: 0644]