[llvm-dev] Is trapping allowed when an add with nsw flag overflows?

Mehdi Amini via llvm-dev llvm-dev at lists.llvm.org
Fri Apr 15 10:42:18 PDT 2016


> On Apr 15, 2016, at 10:28 AM, Manuel Jacob via llvm-dev <llvm-dev at lists.llvm.org> wrote:
> 
> Hi,
> 
> In our backend, we currently emit add operations that trap on overflow if the IR operation has the nsw flag set.  Is this allowed?

Isn't it what something like UBSAN would do?


> According to the documentation about poison values, overflowing a nsw add is undefined behavior.

I don't read it the same way: "Poison value behavior is defined in terms of value dependence:  [....]  Poison values have the same behavior as undef values, with the additional effect that any instruction that has a dependence on a poison value has undefined behavior."
So merely overflowing a nsw operation is not UB, until you do a side effect operation that depends on the poison value.

-- 
Mehdi




>  However I didn't find a formal definition of undefined behavior in LLVM.  Judging from previous discussions on the mailing list, there seems to be a vague line of what LLVM is allowed to do in case of undefined behavior.  Is trapping allowed?
> 
> -Manuel
> _______________________________________________
> LLVM Developers mailing list
> llvm-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev



More information about the llvm-dev mailing list