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

John Regehr via llvm-dev llvm-dev at lists.llvm.org
Fri Apr 15 10:44:17 PDT 2016

No, trapping is not allowed, since an overflowing add nsw is defined to 
produce a poison value, which sort of explodes into undefined behavior 
if it reaches a side-effect.  This is to support speculative execution.

If you emit trapping adds for nsw, you'll see spurious traps every now 
and then.

There's a stronger form of undefined behavior, exhibited by things like 
divide by zero, that permits traps.


On 4/15/16 7:28 PM, Manuel Jacob via llvm-dev 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?
> According to the documentation about poison values, overflowing a nsw
> add is undefined behavior.  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