[LLVMdev] Integer divide by zero

Cameron McInally cameron.mcinally at nyu.edu
Fri Apr 5 11:23:39 PDT 2013


Hey guys,

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.

A little testcase:

#include <stdio.h>

int foo(int j, int d) {
  return j / d ;
}

int bar (int k, int d) {
  return foo(k + 1, d);
}

int main( void ) {
  int r = bar(5, 0);
  printf( " r = %d\n", r);
}

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.

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.

Thanks in advance,
Cameron
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20130405/2a299492/attachment.html>


More information about the llvm-dev mailing list