[llvm-dev] Hoisting instructions in presence of Undefined Behaviour

Anna Thomas via llvm-dev llvm-dev at lists.llvm.org
Mon Nov 30 14:45:53 PST 2020


We’d like to clarify whether the following transform is valid. Given the code:
```
if (freeze(undef))
   return
UB
```

Can we hoist the UB above the `if` block:
```
UB
if (freeze(undef))
  return
```

The reasoning is that:
1. We were already having undefined behaviour in the code initially. `if freeze(undef)` evaluates to true or false. So, a valid execution of the program will fall through the `if` block and execute the UB.
2. Given #1, hoisting a UB to above the `if` block is valid.


Taking this one step further, if the program was:
```
if (freeze(undef))
  return
load 
```
Can we hoist the load over the if-block? I think we can.

The `if freeze(undef)` being taken or not is independent of any other program variables and the compiler is free to refine the code into one where the if block is not taken. 
So, although the load is not guaranteedToExecute, we know that the execution of the load is not actually control dependent on the branch. 

Anything incorrect with the above transforms?


Thanks,
Anna






More information about the llvm-dev mailing list