Hence treating first one as redundant based on the second is acceptable. But vice versa is not.But then add on same arguments results in well-defined value.Ok. Got it.If add nsw overflows, this results in undefined value.
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.
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.
On 23/07/2014 09:46, Tim Northover wrote:Tim,
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.
if both instructions are right after each other such that we know that either none of them or both will be executed, is there a way to leave the nsw flag taking advantage of the knowledge that any pair of values
that cause nsw in the instruction that originally had now nsw flag is already known to break the nsw assumption of the other instruction
and causes consequently undefined behaviour?
The langref description is a little surprising, as it seems the undefined behaviour only is invoked is the resulting poison value is
actually used:
"Poison Values have the same behavior as undef values, with the additional affect that any instruction which has a dependence on a poison value has undefined behavior."
You can’t do this because the add’s have a different set of uses. This is valid IR modulo any typos I make:
%safeidx = add nsw i32 %a, %b
%idx = add i32 %a, %b
%cmp = cmp <some condition that guarantees no overflow>
br i1 %cmp, label %maynotoverflow, label %mayoverflow
maynotoverflow:
%p = getelementptr inbounds i32* %o, i32 %idx
load i32* %p
mayoverflow:
sext i32 %idx to i64
—
That’s why we need the concept of a “poison” value.
-Andy
>
> Thanks,
> --
> Rekha