how LLVM deals with 'undef'

Hi,

I’m using LLVM-3.8 (current SVN version) to optimize my code. After loop unrolling, I find there is ‘undef’ in the IR. For example, “%.lcssa21.unr = phi double [ undef, %.lr.ph9 ], [ %13, %7 ]”. I have two questions:

(1) Why this ‘undef’ is generated? Because before ‘loop unrolling’, there is no such ‘undef’.
(2) How LLVM backend deals with this ‘undef’?

Any suggestion is appreciable.

Regards,

Xiangyang

I'm using LLVM-3.8 (current SVN version) to optimize my code. After loop
unrolling, I find there is 'undef' in the IR. For example,
"/%.lcssa21.unr = phi double [ undef, %.lr.ph9 ], [ %13, %7 ]/". I have
two questions:

(1) Why this 'undef' is generated? Because before 'loop unrolling',
there is no such 'undef'.

Most likely to satisfy the syntax of a phi node. If everything is correct, that value will never be used. If things weren't exactly correct, this may be a result of a load from an uninitialized memory, or something like that.

(2) How LLVM backend deals with this 'undef'?

In general, undef will propagate itself through instructions. How it will deal with this specific one, it's impossible to say without the context.

-Krzysztof

Thanks for reply.

First, I use Clang to compile my source code without any optimization so that I get one .bc file, in which there is no ‘undef’ showing up. Then, I use opt to optimize the IR. So I assume things are correct and the ‘undef’ is introduced to satisfy the syntax of phi node, as you mentioned. But I really don’t want this ‘undef’ in IR. Do you have any idea to get rid of ‘undef’? Thanks a lot.

Regards,

Xiangyang

" But I really don't want this 'undef' in IR"

You can't avoid it in IR, it is in fact, part of the IR, and may get
introduced in certain situations.
It's like saying "i don't want add's"
:slight_smile:

Just because you don’t see an ‘undef’ doesn’t mean there isn’t any undefined behaviour already present in your code.
eg, bit shifting too far, loading from uninitialised memory, divide by zero, calling a function with the wrong ABI…

There are plenty of optimizations that will detect this undefined behaviour and replace it with ‘undef’.

Without seeing your code, we can’t tell you exactly why this is happening.