<div dir="ltr">Hey guys,<div><br></div><div>I'm learning that LLVM does not preserve faults during constant folding. I realize that this is an architecture dependent problem, but I'm not sure if it's safe to constant fold away a fault on x86-64.</div>
<div><br></div><div style>A little testcase:</div><div style><br></div><div style><div>#include <stdio.h></div><div><br></div><div>int foo(int j, int d) {</div><div>  return j / d ;</div><div>}</div><div><br></div><div>
int bar (int k, int d) {</div><div>  return foo(k + 1, d);</div><div>}</div><div><br></div><div>int main( void ) {</div><div>  int r = bar(5, 0);</div><div>  printf( " r = %d\n", r);</div><div>}</div><div><br></div>
<div style>At execution, on an x86-64 processor, this optimized code should fault with a divide-by-zero. At -O2, LLVM 3.1 folds the divide by zero into a constant load. This is, of course, undefined by the C standard, so the folding makes sense on that front. But on x86-64, I don't think this is okay. On x86-64, an integer divide by zero is non-maskable, meaning it will always trap and has no result of any type.</div>
<div style><br></div><div style>My first question is, what's the rationale behind not preserving faults on constant integer folding? Second, is this a bug? With my current understanding, this is most definitely a bug for me on x86-64. But, I'm wondering if there is a hole in my understanding.</div>
<div style><br></div><div style>Thanks in advance,</div><div style>Cameron</div><div style><br></div><div style><br></div></div></div>