[prev in list] [next in list] [prev in thread] [next in thread] 

List:       gcc-bugs
Subject:    [Bug tree-optimization/94892] New: (x >> 31) + 1 not getting narrowed to compare
From:       "gabravier at gmail dot com" <gcc-bugzilla () gcc ! gnu ! org>
Date:       2020-04-30 17:25:03
Message-ID: bug-94892-4 () http ! gcc ! gnu ! org/bugzilla/
[Download RAW message or body]

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

            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=
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic