This is an archive of the discontinued LLVM Phabricator instance.

[CodeGen] use nsw negation for abs
ClosedPublic

Authored by spatel on May 22 2018, 8:55 AM.

Details

Summary

The clang builtins have the same semantics as the stdlib functions.
The stdlib functions are defined in section 7.20.6.1 of the C standard with:
"If the result cannot be represented, the behavior is undefined."

So I think that lets us mark the negation with 'nsw' because "sub i32 0, INT_MIN" would be UB/poison.

Diff Detail

Event Timeline

spatel created this revision.May 22 2018, 8:55 AM

That is what happens for "hand-rolled" abs, https://godbolt.org/g/6dbgXD
I *think* this makes sense, since IIRC LLVM only supports twos-complement platforms.
But all these attributes are currently beyond me, so i won't click the accept button :)

That is what happens for "hand-rolled" abs, https://godbolt.org/g/6dbgXD
I *think* this makes sense, since IIRC LLVM only supports twos-complement platforms.
But all these attributes are currently beyond me, so i won't click the accept button :)

Thanks for taking a look! I should also note that we don't translate the standard libcall to the builtin in clang (not sure if we should), so this patch doesn't currently affect the common case. We'd probably want to do something similar in LibCallSimplifier if this is right.

craig.topper accepted this revision.May 22 2018, 11:11 AM

This seems right to me. GCC believes believes that __bultin_abs always returns a positive number.

This revision is now accepted and ready to land.May 22 2018, 11:11 AM
This revision was automatically updated to reflect the committed changes.
This revision was automatically updated to reflect the committed changes.