]> git.ipfire.org Git - thirdparty/gcc.git/commit
Match: Support form 7 for unsigned integer SAT_ADD
authorPan Li <pan2.li@intel.com>
Mon, 28 Apr 2025 12:35:08 +0000 (20:35 +0800)
committerPan Li <pan2.li@intel.com>
Mon, 12 May 2025 23:07:22 +0000 (07:07 +0800)
commit656db31e4448e7b51a919dc1acfb3080c82f43de
tree8a1d189b876a383526d8baa50e85f3d6d3460c1e
parentfbd7c49bba4d3afccdfd77eb2ff590428420baf7
Match: Support form 7 for unsigned integer SAT_ADD

This patch would like to support the form 7 of the unsigned
integer SAT_ADD, aka below example.

  #define DEF_SAT_U_ADD_FMT_7(WT, T)     \
  T __attribute__((noinline))            \
  sat_u_add_##WT##_##T##_fmt_7(T x, T y) \
  {                                      \
    T max = -1;                          \
    WT val = (WT)x + (WT)y;              \
    return val > max ? max : (T)val;     \
  }

  DEF_SAT_U_ADD_FMT_7(uint64_t, uint32_t)

If we take -O3 build with -fdump-tree-optimized, we will have

Before this patch:
   5   │ __attribute__((noinline))
   6   │ uint32_t sat_u_add_uint64_t_uint32_t_fmt_7 (uint32_t x, uint32_t y)
   7   │ {
   8   │   uint64_t val;
   9   │   long unsigned int _1;
  10   │   long unsigned int _2;
  11   │   uint32_t _3;
  12   │   uint32_t _7;
  13   │
  14   │   <bb 2> [local count: 1073741824]:
  15   │   _1 = (long unsigned int) x_4(D);
  16   │   _2 = (long unsigned int) y_5(D);
  17   │   val_6 = _1 + _2;
  18   │   if (val_6 <= 4294967295)
  19   │     goto <bb 3>; [65.00%]
  20   │   else
  21   │     goto <bb 4>; [35.00%]
  22   │
  23   │   <bb 3> [local count: 697932184]:
  24   │   _7 = x_4(D) + y_5(D);
  25   │
  26   │   <bb 4> [local count: 1073741824]:
  27   │   # _3 = PHI <4294967295(2), _7(3)>
  28   │   return _3;
  29   │
  30   │ }

After this patch:
   4   │ __attribute__((noinline))
   5   │ uint32_t sat_u_add_uint64_t_uint32_t_fmt_7 (uint32_t x, uint32_t y)
   6   │ {
   7   │   uint32_t _3;
   8   │
   9   │   <bb 2> [local count: 1073741824]:
  10   │   _3 = .SAT_ADD (x_4(D), y_5(D)); [tail call]
  11   │   return _3;
  12   │
  13   │ }

This change also effects on vector mode too.

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: Add form 7 matching pattern for unsigned integer
SAT_ADD.

Signed-off-by: Pan Li <pan2.li@intel.com>
gcc/match.pd