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

Nuno Lopes via llvm-dev llvm-dev at lists.llvm.org
Mon Nov 30 15:34:21 PST 2020


Both transformations are correct, yes. See here:
https://alive2.llvm.org/ce/z/EpqCUT
https://alive2.llvm.org/ce/z/yyj9TQ

For a fixed input, if the source triggers UB for *at least one* set of 
chosen non-deterministic values (e.g., undef, freeze), then the source is 
declared UB for that input. So you can optimize it away to UB.

Nuno

-----Original Message----- 
From: Anna Thomas
Sent: Monday, November 30, 2020 10:45 PM
Subject: [llvm-dev] Hoisting instructions in presence of Undefined Behaviour

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