[LLVMdev] SCCP and undef branches

Nick Lewycky nicholas at mxc.ca
Wed Jun 7 14:50:53 PDT 2006


Domagoj Babic wrote:
> Here's something I don't understand... How come that UNDEF can
> appear as a branch condition at all? I just can't think of any ways.
> 
> If you write something like
> 
> fun() {
>   int x;
>   if (x > 100) {
>      ...
>   } else {
>      ...
>   }
> }
> 
> LLVM generates a boolean temporary that compares (uninitialized)
> value of x with 100.

Firstly, "int x" generates an alloca of one int. The use of "x > 100"
then generates a load of that int. The instruction combiner detects that
it's an alloca+load with no intervening store, and replaces it with undef.

Then, the instruction combiner will try to constant fold the setgt
operation with undef, 100 as arguments. That folds into undef. So before
SCCP is run, your example does produce "if (undef)".

> Second, if it already can appear, isn't that a bug that should be
> reported by the compiler?

Almost. If that undef never executes, then there is no bug, but that
depends on run-time conditions. I suppose you could warn about it
anyways, or perhaps even replace such BBs with "unreachable" though that
might alarm too many users.

The right place to check for these sorts of things might be at link/JIT
time if undef usage can't be optimized away as dead code. I'm not sure
users are ready for their linker to be emitting those sorts of errors
though.

Nick



More information about the llvm-dev mailing list