diff options
author | Marek Safar <marek.safar@gmail.com> | 2019-10-26 09:20:15 +0300 |
---|---|---|
committer | Marek Safar <marek.safar@gmail.com> | 2019-12-11 02:05:36 +0300 |
commit | 132d8afe7ac5eae64b7597f120319fd2ccca5d5a (patch) | |
tree | 195bf01f9ee9602bb0833a941b436576bafbe6c9 /test/Mono.Linker.Tests.Cases/UnreachableBlock/ComplexConditions.cs | |
parent | b4770592e35a54823ef5705cc339bf6327229570 (diff) |
Add new optimization steps to make Mark step more effective
Two new steps have been introduced
BodySubstituterStep
This step removes any conditional blocks when the condition or conditions
are evaluated as constants. This step does not do any inlining. The conditional
logic is kept but based on the known values only one branch is always taken.
A simple example which can be detected by linker.
```c#
class C
{
static void Main ()
{
if (Result)
Console.WriteLine (); // <- this call will be marked and removed
}
static bool Result () => false;
}
```
RemoveUnreachableBlocksStep
A new command-line option called `--substitutions` allow external customization of any
methoda for assemblies which are linked. The syntax is same as for existing linker descriptor
XML files but it add way to control body modifications.
An example of XML file
```xml
<linker>
<assembly fullname="test, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null">
<type fullname="Mono.Linker.Tests.Cases.Substitutions.StubBodyWithValue">
<method signature="System.String TestMethod_1()" body="stub" value="abcd">
</method>
</type>
</assembly>
</linker>
```
The `value` attribute is optional and only required when the method stub should not
fallback to default behaviour.
Additional to `stub` value also `remove` value is supported to forcibly remove body
of the method when the method is marked. This is useful when the conditional logic
cannot be evaluated by linker and the method will be marked but never actually reached.
Applicability
Both steps can be combined to achieve the effect of externally customizing which
conditional branches can be removed during the linking.
I can illustrate that on IntPtr.Size property. With following substitution any code
that has compiled in conditional logic for 64 bits handling by checking IntPtr.Size will
be removed during linking.
```xml
<linker>
<assembly fullname="mscorlib">
<type fullname="System.IntPtr">
<method signature="System.Int32 get_Size()" body="stub" value="4">
</method>
</type>
</assembly>
</linker>
```
Implements #752
Diffstat (limited to 'test/Mono.Linker.Tests.Cases/UnreachableBlock/ComplexConditions.cs')
-rw-r--r-- | test/Mono.Linker.Tests.Cases/UnreachableBlock/ComplexConditions.cs | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/test/Mono.Linker.Tests.Cases/UnreachableBlock/ComplexConditions.cs b/test/Mono.Linker.Tests.Cases/UnreachableBlock/ComplexConditions.cs new file mode 100644 index 000000000..9a92ab742 --- /dev/null +++ b/test/Mono.Linker.Tests.Cases/UnreachableBlock/ComplexConditions.cs @@ -0,0 +1,35 @@ +using System; +using System.Reflection.Emit; +using Mono.Linker.Tests.Cases.Expectations.Assertions; + +namespace Mono.Linker.Tests.Cases.UnreachableBlock +{ + public class ComplexConditions + { + public static void Main() + { + Test_1 (null); + } + + [Kept] + [ExpectBodyModified] + static void Test_1 (object type) + { + if (type is Type || (IsDynamicCodeSupported && type is TypeBuilder)) + Reached_1 (); + } + + [Kept] + static bool IsDynamicCodeSupported { + [Kept] + get { + return true; + } + } + + [Kept] + static void Reached_1 () + { + } + } +}
\ No newline at end of file |