This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug tree-optimization/83517] New: Missed optimization in math expression: (x+x)/x == 2


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

            Bug ID: 83517
           Summary: Missed optimization in math expression: (x+x)/x == 2
           Product: gcc
           Version: tree-ssa
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: zamazan4ik at tut dot by
  Target Milestone: ---

gcc (trunk) with '-O3 -std=c++17 -ffast-math -funsafe-math-optimizations' for
this code:

int test(int x)
{
    return (x+x)/x;
}


generates:

test(int):
  lea eax, [rdi+rdi]
  cdq
  idiv edi
  ret


Why? In this case we can return simply 2. Because there are only two corner
cases: when x is 0 and we have division by zero (it's UB), and when x+x is
integer overflow (it's also UB).

So we can simply optimize it. There are a lot of similar cases.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]