Improvements for null check folding. (#1735)
authorEugene Rozenfeld <erozen@microsoft.com>
Fri, 17 Jan 2020 23:35:53 +0000 (15:35 -0800)
committerGitHub <noreply@github.com>
Fri, 17 Jan 2020 23:35:53 +0000 (15:35 -0800)
commitc44526da91bc6911019e57f061fc2a4540591846
tree9115005bb99b7ad683680f26ad44155f6c0e8883
parentc2e4efcde8d994a30e076cde3fc9b0f56d08cf54
Improvements for null check folding. (#1735)

optFoldNullChecks attempts to remove GT_NULLCHECK nodes that are
post-dominated by indirections on the same variable. These changes
implement a number of improvements.

1. Recognize more patterns.
Before these changes only the following pattern was recognized:
x = comma(nullcheck(y), add(y, const1))

followed by

indir(add(x, const2))

where const1 + const2 is sufficiently small.

With these changes the following patterns are recognized:

nullcheck(x)
or
x = comma(nullcheck(y), add(y, const1))

followed by

indir(x)
or
indir(add(x, const2))

where const1 + const2 is sufficiently small.

2. Indirections now include GT_ARR_LENGTH nodes.

3. Morph has an optimization
((x+icon1)+icon2) => (x+(icon1+icon2))
These changes generalize it to handle commas:
((comma(y, x+icon1)+icon2) => comma(y, x+(icon1+icon2))

That exposes more trees to null check folding.

4. Fix a bug in flow transformations that could lose BBF_HAS_NULLCHECK flag
on some basic blocks, which led to missing opportunities for null check folding.

5. Make safety checks in optCanMoveNullCheckPastTree
(for trees between the nullcheck and the indirection) both more correct
and less conservative. For example, we were not allowing any assignments
if we were inside try; however, assignments to compiler temps are safe since
they won't be visible in handlers.

5. Increase the maximum number of trees we check between GT_NULLCHECK and
the indirection from 25 to 50.

7. Refactor the code and move pattern recognition and safety checks to
helper methods.

8. Add missing BBF_HAS_NULLCHECK and OMF_HAS_NULLCHECK when we create GT_NULLCHECK nodes.
src/coreclr/format.patch [new file with mode: 0644]
src/coreclr/src/jit/block.cpp
src/coreclr/src/jit/block.h
src/coreclr/src/jit/compiler.h
src/coreclr/src/jit/compmemkind.h
src/coreclr/src/jit/earlyprop.cpp
src/coreclr/src/jit/importer.cpp
src/coreclr/src/jit/morph.cpp