"Fold overflow operations in value numbering" (second attempt after revert) (#51440)
authorSingleAccretion <62474226+SingleAccretion@users.noreply.github.com>
Wed, 26 May 2021 18:17:23 +0000 (21:17 +0300)
committerGitHub <noreply@github.com>
Wed, 26 May 2021 18:17:23 +0000 (11:17 -0700)
commit96037547d96a884e8e14bc4161145fe04f5d549e
tree688b6aa06a9424077847d265617c4946cb3956ea
parent5c875e48e5db8a1d2f17619c76d45176fa9ba0e0
"Fold overflow operations in value numbering" (second attempt after revert) (#51440)

* Added a missing license header

* Added a test verifying that checked arithmetic is correct

* Added a test verifying that checked casts are correct

* Disabled the test for checked casts on Mono

* Refactored VNEvalShouldFold

* Refactored gtFoldExprConst to use helpers and follow the common code style

* Fixed the comment stating TYP_BYREF has no zero value

* Moved checking of overflow for arithmetic operations from gtFoldExprConst into a separate namespace

* Implemented folding of overflow arithmetic in value numbering

* Fixed some typos in valuenum.cpp/h

* Added identity-based evaluation for overflow arithmetic

* Made the definition of var_types a standalone header so that it can be safely #included'ed in utils.h

* Refactored gtFoldExpr some more, moved the overflow checking logic to CheckedOps, implemented overflow checking for floating point -> integer casts

* Implemented folding of checked casts in value numbering

* Demote the tests to Tier1

They throw and catch quite a few exceptions.

* Fixed a comment

UINT32 -> UINT64

* Made arithmetic CheckedOps functions templated

Reduces code duplication and obviates the need for
some conditions and casts.

They use the implementation from the Long* variants of
the old functions, except for "SubOverflows", where some
instantiations, unreachable at runtime, were using "size_t" as the
type argument and causing warnings. The relevant part of "AddOverflows"
has been inlined into "SubOverflows".

* Move the locals under "if" to avoid shadowing

* Use ClrSafeInt instead of custom code

* Fix a copy and paste mistake

Co-authored-by: Anton Lapounov <anton.lapounov@microsoft.com>
* Update src/coreclr/jit/utils.cpp

* Apply suggestions from code review

Co-authored-by: Anton Lapounov <anton.lapounov@microsoft.com>
* Assert type != TYP_BYREF in VNEvalShouldFold

The method is not prepared to handle them.
Also add a note about that to the header.
Also delete TODO-Review about it.

Right now the only caller of VNEvalShouldFold guards against
TYP_BYREF folding, so this assert is a safety measure against
future callers not taking byrefs into account.

* Drop the MAX_ prefix from MIN

Co-authored-by: Anton Lapounov <anton.lapounov@microsoft.com>
24 files changed:
src/coreclr/jit/CMakeLists.txt
src/coreclr/jit/assertionprop.cpp
src/coreclr/jit/gentree.cpp
src/coreclr/jit/gentree.h
src/coreclr/jit/utils.cpp
src/coreclr/jit/utils.h
src/coreclr/jit/valuenum.cpp
src/coreclr/jit/valuenum.h
src/coreclr/jit/valuenumfuncs.h
src/coreclr/jit/valuenumtype.h
src/coreclr/jit/vartype.h
src/coreclr/jit/vartypesdef.h [new file with mode: 0644]
src/tests/JIT/Directed/ConstantFolding/value_numbering_checked_arithemtic_with_constants_ro.csproj [new file with mode: 0644]
src/tests/JIT/Directed/ConstantFolding/value_numbering_checked_integer_arithemtic_with_constants.cs [new file with mode: 0644]
src/tests/JIT/Directed/ConstantFolding/value_numbering_unordered_comparisons_of_constants.cs
src/tests/JIT/Directed/Convert/value_numbering_checked_casts_of_constants.cs [new file with mode: 0644]
src/tests/JIT/Directed/Convert/value_numbering_checked_casts_of_constants.csproj [new file with mode: 0644]
src/tests/JIT/Directed/Convert/value_numbering_checked_casts_of_constants_byte.cs [new file with mode: 0644]
src/tests/JIT/Directed/Convert/value_numbering_checked_casts_of_constants_double.cs [new file with mode: 0644]
src/tests/JIT/Directed/Convert/value_numbering_checked_casts_of_constants_float.cs [new file with mode: 0644]
src/tests/JIT/Directed/Convert/value_numbering_checked_casts_of_constants_int.cs [new file with mode: 0644]
src/tests/JIT/Directed/Convert/value_numbering_checked_casts_of_constants_long.cs [new file with mode: 0644]
src/tests/JIT/Directed/Convert/value_numbering_checked_casts_of_constants_short.cs [new file with mode: 0644]
src/tests/issues.targets