[LLVMdev] On semantics of add instruction - nsw,nuw flags

Tim Northover t.p.northover at gmail.com
Wed Jul 23 00:46:50 PDT 2014


> Then why does the Release Note say
> " the operation is guaranteed to not overflow".

It means that the person who wrote the IR has guaranteed that there's
no overflow (by some means) so LLVM can assume it during optimisation.

This guarantee might come from doing explicit checks before executing
the add/sub; or perhaps from performing the operation after a sext so
that the type is guaranteed to be big enough; or (as in C) by trusting
the programmer to make sure that doesn't happen.

> What are the redundancies in the following code snip. Assume they appear in
> that order in a basic block.
>
>   Case1; %add2 = add nsw i32 %add, %add1
>              %add3 = add        i32 %add, %add1
>
>   Case2: %add2 = add        i32 %add, %add1
>              %add3 = add nsw i32 %add, %add1

In both cases the add with nsw can be removed in favour of the one
without. Order is completely irrelevant for normal LLVM arithmetic
instructions.

Cheers.

Tim.



More information about the llvm-dev mailing list