Question: I'm writing a compiler for LLVM for a language whose semantics *explicity* define that a division by zero *should always* raise a floating point exception. The problem is, after running -mem2reg and -constprop on my raw IR, my code gets converted to:

```
define i32 @main() {
entry:
 %t3 = sdiv i32 2, 0
 ret i32 7
}

```

which then gets turned by `llc -O0` into:

```
.text
.globl  _c0_main
.align  16, 0x90
.type   _c0_main,@function
main:                               
.cfi_startproc
# BB#0:                                 # %entry
  movl  $7, %eax
  ret
.Ltmp0:
  .size main, .Ltmp0-main
  .cfi_endproc

```

Is there a way to force llc not to remove effectful operations?

Answer: 
The sdiv instruction has divide by zero semantics that are undefined. If your front end language has some defined semantics for this you'll need to use instructions other than sdiv.

Perhaps you'll have to detect a divide by zero and branch at runtime to a sequence of instructions that gives the semantics you want.
The [language reference](http://llvm.org/docs/LangRef.html#sdiv-instruction) states that `[d]ivision by zero leads to undefined behavior` as stated by Colin.

Thus, you have to check if the dividend is zero and explicitly generate a floating point exception. In C, this could look as follows:

```
extern void raiseFloatingException();

int someFunc() {
    int a = 2;
    int b = 0;
    if (!b) {
        raiseFloatingException();
    }
    int result = a / b;
    return result;
}

```

If you compile this to bit code and optimize it with -mem2reg you get the pattern that you could generate with your back end:

```
define i32 @someFunc() #0 {
  %1 = icmp ne i32 0, 0
  br i1 %1, label %3, label %2

; <label>:2                                       ; preds = %0
  call void (...)* @raiseFloatingException()
  br label %3

; <label>:3                                       ; preds = %2, %0
  %4 = sdiv i32 2, 0
  ret i32 %4
}

```

Note, that you will have to provide the `raiseFloatingException` function and link it with your code.