-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
1 parent
c2e4efc
commit c44526d
Showing
8 changed files
with
441 additions
and
201 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
diff --git a/src/coreclr/src/jit/importer.cpp b/src/coreclr/src/jit/importer.cpp | ||
index a71c325ff48..c0569355c89 100644 | ||
--- a/src/coreclr/src/jit/importer.cpp | ||
+++ b/src/coreclr/src/jit/importer.cpp | ||
@@ -15184,41 +15184,41 @@ void Compiler::impImportBlockCode(BasicBlock* block) | ||
info.compCompHnd->compareTypesForEquality(resolvedToken.hClass, clsHnd); | ||
|
||
if (compare == TypeCompareState::Must) | ||
{ | ||
JITDUMP("\nOptimizing %s (%s) -- type test will succeed\n", | ||
opcode == CEE_UNBOX ? "UNBOX" : "UNBOX.ANY", eeGetClassName(clsHnd)); | ||
|
||
// For UNBOX, null check (if necessary), and then leave the box payload byref on the stack. | ||
if (opcode == CEE_UNBOX) | ||
{ | ||
GenTree* cloneOperand; | ||
op1 = impCloneExpr(op1, &cloneOperand, NO_CLASS_HANDLE, (unsigned)CHECK_SPILL_ALL, | ||
nullptr DEBUGARG("optimized unbox clone")); | ||
|
||
GenTree* boxPayloadOffset = gtNewIconNode(TARGET_POINTER_SIZE, TYP_I_IMPL); | ||
GenTree* boxPayloadAddress = | ||
gtNewOperNode(GT_ADD, TYP_BYREF, cloneOperand, boxPayloadOffset); | ||
GenTree* nullcheck = gtNewOperNode(GT_NULLCHECK, TYP_I_IMPL, op1); | ||
block->bbFlags |= BBF_HAS_NULLCHECK; | ||
optMethodFlags |= OMF_HAS_NULLCHECK; | ||
- GenTree* result = gtNewOperNode(GT_COMMA, TYP_BYREF, nullcheck, boxPayloadAddress); | ||
+ GenTree* result = gtNewOperNode(GT_COMMA, TYP_BYREF, nullcheck, boxPayloadAddress); | ||
impPushOnStack(result, tiRetVal); | ||
break; | ||
} | ||
|
||
// For UNBOX.ANY load the struct from the box payload byref (the load will nullcheck) | ||
assert(opcode == CEE_UNBOX_ANY); | ||
GenTree* boxPayloadOffset = gtNewIconNode(TARGET_POINTER_SIZE, TYP_I_IMPL); | ||
GenTree* boxPayloadAddress = gtNewOperNode(GT_ADD, TYP_BYREF, op1, boxPayloadOffset); | ||
impPushOnStack(boxPayloadAddress, tiRetVal); | ||
oper = GT_OBJ; | ||
goto OBJ; | ||
} | ||
else | ||
{ | ||
JITDUMP("\nUnable to optimize %s -- can't resolve type comparison\n", | ||
opcode == CEE_UNBOX ? "UNBOX" : "UNBOX.ANY"); | ||
} | ||
} | ||
else | ||
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.