[Bug tree-optimization/94892] New: (x >> 31) + 1 not getting narrowed to compare

gabravier at gmail dot com gcc-bugzilla@gcc.gnu.org
Thu Apr 30 17:25:03 GMT 2020


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94892

            Bug ID: 94892
           Summary: (x >> 31) + 1 not getting narrowed to compare
           Product: gcc
           Version: 10.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: gabravier at gmail dot com
  Target Milestone: ---

inline int sign(int x)
{
    return (x >> 31) | ((unsigned)-x >> 31);
}

bool f(int x)
{
    return sign(x) > -1;
}

With -O3, LLVM produces this :

f(int):
  test edi, edi
  setns al
  ret

GCC produces this :

f(int):
  sar edi, 31
  lea eax, [rdi+1]
  ret

Changing `f` to `(x >> 31) + 1` results in it being optimized optimally


More information about the Gcc-bugs mailing list