Removing nuw and nsw

Hello everybody,

currently I'm writing a thesis about compiler optimization techniques and I've choosen LLVM as a framework for implementing and testing my ideas.

While reading the LLVM Language Reference Manual I noticed that the nuw and nsw keywords for the instructions add, sub and mul complicate reasoning about integer arithmetic for the purpose of my thesis somewhat. Things would be easier for me if there would be just the plain variants of these instructions so I could formally treat them modular arithmetic operations. Thus, I'm wondering if it's a valid transformation to simply drop these keywords during an optimisation pass ...

As I see it, the effect of nuw resp. nsw is to create poison values in case of an overflow, which are essentially undefined values with the additional effect that they can cause other operation that depend on them to also produce poison values. So, as I understand it, removing these keywords would basically turn undefined values into defined ones, thus dropping some information that could be used for further optimizations, but it would not strictly speaking be an incorrect transformation - is this correct or do I miss or misunderstand something?

Thanks in advance for any comments and insights about this matter!

Raffael Bild

Hi Raffael,

currently I'm writing a thesis about compiler optimization techniques and I've
choosen LLVM as a framework for implementing and testing my ideas.

While reading the LLVM Language Reference Manual I noticed that the nuw and nsw
keywords for the instructions add, sub and mul complicate reasoning about
integer arithmetic for the purpose of my thesis somewhat. Things would be easier
for me if there would be just the plain variants of these instructions so I
could formally treat them modular arithmetic operations. Thus, I'm wondering if
it's a valid transformation to simply drop these keywords during an optimisation
pass ...

it is valid. Dropping them decreases the effectiveness of the optimizers, but
it doesn't create any correctness issues.

As I see it, the effect of nuw resp. nsw is to create poison values in case of
an overflow, which are essentially undefined values with the additional effect
that they can cause other operation that depend on them to also produce poison
values. So, as I understand it, removing these keywords would basically turn
undefined values into defined ones, thus dropping some information that could be
used for further optimizations, but it would not strictly speaking be an
incorrect transformation - is this correct or do I miss or misunderstand something?

That's right, you got it.

Ciao, Duncan.