[llvm-dev] Meaning of `sub nsw`

David Chisnall via llvm-dev llvm-dev at lists.llvm.org
Fri Mar 1 07:23:40 PST 2019


Practically, this means (roughly) two things:

1. Optimisers can assume that overflow will not occur.  For example, 
that %22 >= %19 and %22 >= %21.  This can be used, for example, to 
reason about loop termination.

2. If, after later optimisations, we learn enough about the values of 
%19 and %21 to determine that the operation will overflow, then we can 
replace it with undef.

The latter is less useful, because this typically means the code is 
wrong (though it might also be dead, for example in C++ template 
instantiation where the programmer is relying on DCE removing 
unreachable code that would exhibit undefined behaviour).  The former is 
a lot more useful for exposing later optimisation opportunities.

David


On 01/03/2019 14:49, Cranmer, Joshua via llvm-dev wrote:
> In simple terms, nsw (for no signed wrap) means you invoke C-style undefined behavior should the operation have an overflow when interpreted as a signed operation.
> 
> -----Original Message-----
> From: llvm-dev [mailto:llvm-dev-bounces at lists.llvm.org] On Behalf Of Peng Yu via llvm-dev
> Sent: Friday, March 1, 2019 7:35
> To: llvm-dev <llvm-dev at lists.llvm.org>
> Subject: [llvm-dev] Meaning of `sub nsw`
> 
> In the following code.
> 	unsigned char x1 = atoi(argv[1]);
> 	unsigned char x2 = atoi(argv[2]);
> 	printf("%d\n", x1-x2);
> 
> The substraction of the last command is translated to the following IR code.
> 
> %18 = load i8, i8* %6, align 1
> %19 = zext i8 %18 to i32
> %20 = load i8, i8* %7, align 1
> %21 = zext i8 %20 to i32
> %22 = sub nsw i32 %19, %21
> 
> I don't follow the explanation of nsw in langref. Could anybody help explain what nsw means here? Thanks.
> 
> 
> --
> Regards,
> Peng
> _______________________________________________
> LLVM Developers mailing list
> llvm-dev at lists.llvm.org
> https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
> _______________________________________________
> LLVM Developers mailing list
> llvm-dev at lists.llvm.org
> https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
> 


More information about the llvm-dev mailing list