Issue 128200
Summary Wrong x86 binary generated with -O1 optimization level. Division by zero FPE exception. (Regression since Clang 16)
Labels clang
Assignees
Reporter pavelliavonau
    Following code (with -O1) throws signal SIGFPE due division by zero.
```

#include <cfenv>

double calc(int field)
{

  volatile int dField = 0;

  // make a or c non-zero to pass a without exception
  volatile double a = 0;
 volatile double c = 0;

  double fSource = 2;
  double dSource = 1;
  
 const double AUXD = a;
  const double AUXE = c;
  const double AUX = AUXD + AUXE;
  if (AUX > 1.0)// && AUX != 0.0) // this check forces Clang generate correct binary
  {
    dSource = AUXE / AUX;
    fSource = AUXD / AUX;
 }

  if (field == dField)
    return dSource;
  else 
    return fSource;
}

int main() {

    feenableexcept(FE_ALL_EXCEPT);

    auto d = calc(3);

    return d;
}
```
Affected Clang 16 and later versions. Checked at godbolt service. Wrong machine code is generated. 
Looks like a bug with optimization algorithm.
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to