-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dead code elimination for if (typeof(T).IsValueType)
#97080
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -591,6 +591,13 @@ private bool TryGetConstantArgument(MethodIL methodIL, byte[] body, OpcodeFlags[ | |
{ | ||
return true; | ||
} | ||
else if (method.IsIntrinsic && method.Name is "get_IsValueType" | ||
&& method.OwningType is MetadataType mdt | ||
&& mdt.Name == "Type" && mdt.Namespace == "System" && mdt.Module == mdt.Context.SystemModule | ||
&& TryExpandTypeIsValueType(methodIL, body, flags, currentOffset, out constant)) | ||
{ | ||
return true; | ||
} | ||
else | ||
{ | ||
constant = 0; | ||
|
@@ -745,6 +752,40 @@ private string GetResourceStringForAccessor(EcmaMethod method) | |
return null; | ||
} | ||
|
||
private static bool TryExpandTypeIsValueType(MethodIL methodIL, byte[] body, OpcodeFlags[] flags, int offset, out int constant) | ||
{ | ||
// We expect to see a sequence: | ||
// ldtoken Foo | ||
// call GetTypeFromHandle | ||
// -> offset points here | ||
constant = 0; | ||
const int SequenceLength = 10; | ||
if (offset < SequenceLength) | ||
return false; | ||
|
||
if ((flags[offset - SequenceLength] & OpcodeFlags.InstructionStart) == 0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need to check that there are no jump targets in the sequence? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ReadLdToken and ReadGetTypeFromHandle already do that check |
||
return false; | ||
|
||
ILReader reader = new ILReader(body, offset - SequenceLength); | ||
|
||
TypeDesc type = ReadLdToken(ref reader, methodIL, flags); | ||
if (type == null) | ||
return false; | ||
|
||
if (!ReadGetTypeFromHandle(ref reader, methodIL, flags)) | ||
return false; | ||
|
||
// Dataflow runs on top of uninstantiated IL and we can't answer some questions there. | ||
// Unfortunately this means dataflow will still see code that the rest of the system | ||
// might have optimized away. It should not be a problem in practice. | ||
if (type.IsSignatureVariable) | ||
return false; | ||
|
||
constant = type.IsValueType ? 1 : 0; | ||
|
||
return true; | ||
} | ||
|
||
private static bool TryExpandTypeEquality(MethodIL methodIL, byte[] body, OpcodeFlags[] flags, int offset, string op, out int constant) | ||
{ | ||
// We expect to see a sequence: | ||
|
@@ -793,37 +834,37 @@ private static bool TryExpandTypeEquality(MethodIL methodIL, byte[] body, Opcode | |
constant ^= 1; | ||
|
||
return true; | ||
} | ||
|
||
static TypeDesc ReadLdToken(ref ILReader reader, MethodIL methodIL, OpcodeFlags[] flags) | ||
{ | ||
ILOpcode opcode = reader.ReadILOpcode(); | ||
if (opcode != ILOpcode.ldtoken) | ||
return null; | ||
private static TypeDesc ReadLdToken(ref ILReader reader, MethodIL methodIL, OpcodeFlags[] flags) | ||
{ | ||
ILOpcode opcode = reader.ReadILOpcode(); | ||
if (opcode != ILOpcode.ldtoken) | ||
return null; | ||
|
||
TypeDesc t = (TypeDesc)methodIL.GetObject(reader.ReadILToken()); | ||
TypeDesc t = (TypeDesc)methodIL.GetObject(reader.ReadILToken()); | ||
|
||
if ((flags[reader.Offset] & OpcodeFlags.BasicBlockStart) != 0) | ||
return null; | ||
if ((flags[reader.Offset] & OpcodeFlags.BasicBlockStart) != 0) | ||
return null; | ||
|
||
return t; | ||
} | ||
return t; | ||
} | ||
|
||
static bool ReadGetTypeFromHandle(ref ILReader reader, MethodIL methodIL, OpcodeFlags[] flags) | ||
{ | ||
ILOpcode opcode = reader.ReadILOpcode(); | ||
if (opcode != ILOpcode.call) | ||
return false; | ||
private static bool ReadGetTypeFromHandle(ref ILReader reader, MethodIL methodIL, OpcodeFlags[] flags) | ||
{ | ||
ILOpcode opcode = reader.ReadILOpcode(); | ||
if (opcode != ILOpcode.call) | ||
return false; | ||
|
||
MethodDesc method = (MethodDesc)methodIL.GetObject(reader.ReadILToken()); | ||
MethodDesc method = (MethodDesc)methodIL.GetObject(reader.ReadILToken()); | ||
|
||
if (!method.IsIntrinsic || method.Name != "GetTypeFromHandle") | ||
return false; | ||
if (!method.IsIntrinsic || method.Name != "GetTypeFromHandle") | ||
return false; | ||
|
||
if ((flags[reader.Offset] & OpcodeFlags.BasicBlockStart) != 0) | ||
return false; | ||
if ((flags[reader.Offset] & OpcodeFlags.BasicBlockStart) != 0) | ||
return false; | ||
|
||
return true; | ||
} | ||
return true; | ||
} | ||
|
||
private sealed class SubstitutedMethodIL : MethodIL | ||
|
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
method.IsIntrinsic
can be de-duplicated with the existing case above.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need this to land in the
else
branch all the way below if it's an intrinsic but not the one we recognize.