Fix overflow checking for operations with mixed sign #9403
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is a rebuilt of arithmetic operations with overflow checking (
+
,-
,*
) to fix issues on cases with arguments of different sign.I implemented the same approach used by clang (https://github.com/llvm/llvm-project/blob/796898172c48a475f27f038e587c35dbba9ab7a6/clang/lib/CodeGen/CGBuiltin.cpp#L3378-L3465) for the builtin operations (
__builtin_add_overflow
, etc.) and using the same workaround for multiplication of integers mixed sign. This workaround is needed to avoid internal i128 multiplication for i64 values and to avoid sigfaults for i128 multiplications.This also adds specs to test operations between all integer types, using some extreme values from each type and contrasting results using BigInt.
Before implementing this approach I've been testing a port of SafeInt. It's a more exhaustive case by case approach that produces shorter binary code, but it requires more work and detailed testing. I assume shorter also means faster although I didn't perform any benchmark. I decided to go with this easier but correct code first.
Fixes #9277