Use case of undef

Hello all,

I am wondering the use case of undef. Is there any C code that I could get undef if it is compiled by clang.

Thanks

Here is an example:
https://godbolt.org/g/wKoXWW

– Sean Silva

Hello Sean,

I read several LLVM undef materials[1][2][3] but still have some questions of it. I can understand “add %X, undef → undef”. But why the equation “and %X, undef → 0” is valid? In LLVM Language Reference Manual, the explanation of such equation is that it is safe to assume that all bits of the ‘undef‘ could be 0. If so, why can’t we assume all bits of the ‘undef’ is 0 in the former equation. Therefore we can get “add %X, 0 → %X” instead?

By the way, is there any document that describe how ‘undef’ should be handled with other LLVM value? Is the ‘undef’ in LLVM the same as the UNDEF[4] (the top element in lattice) in constant propagation?

[1] http://www.nondot.org/sabre/LLVMNotes/UndefinedValue.txt
[2] https://www.cs.utah.edu/~regehr/llvm-ub.pdf
[3] http://sunfishcode.github.io/blog/2014/07/14/undef-introduction.html
[4] http://infolab.stanford.edu/~ullman/dragon/w06/lectures/cp.pdf

Thanks for your reply

For integer I believe you’re allowed to fold add %X, 0 → %X

AFAIK you can use any valid bit pattern for the type when replacing an undef.

Hello Sean,

I read several LLVM undef materials[1][2][3] but still have some questions
of it. I can understand "add %X, undef -> undef". But why the equation "and
%X, undef -> 0" is valid? In LLVM Language Reference Manual, the
explanation of such equation is that it is safe to assume that all bits of
the ‘undef‘ could be 0. If so, why can't we assume all bits of the 'undef'
is 0 in the former equation. Therefore we can get "add %X, 0 -> %X" instead?

This is a good question and shows that you have a fairly good understanding
already. "add %X, undef -> %X" is legal as you predict. The reason it isn't
done is that "add %X, undef -> undef" is a stronger simplification since
it propagates the undef.

To go further, I can recommend the thread "[llvm-dev] RFC: Killing undef
and spreading poison". Nuno also did a presentation at the latest LLVM
developer meeting:
http://llvm.org/devmtg/2016-11/Slides/Lopes-LongLivePoison.pdf

By the way, is there any document that describe how 'undef' should be
handled with other LLVM value? Is the 'undef' in LLVM the same as the
UNDEF[4] (the top element in lattice) in constant propagation?

No, the UNDEF lattice value in constant propagation (and any other lattice
framework) basically just means "I don't know anything about the set of
dynamic values that this value can take".

-- Sean Silva