-
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.
Better codegen for
(T)x | (T)y
(#58727)
* Better codegen for `(T)x | (T)y` I added a morph pass to fold expressions like `(T)x | (T)y` into `(T)(x | y)`. This results in fewer `movzx` instructions in the asm. Fixes #13816 * Code review updates * Rename function to fgMorphCastedBitwiseOp * Don't fold checked arithmetic * Reuse op1 node for return value * Don't run outside global morphing * Various code style and comment tweaks * Don't call gtGetOp2 if tree was folded. If it was folded, it was folded to a unary (cast) operation and gtGetOp2() will crash. I also tweaked fgMorphCastedBitwiseOp to return nullptr if it didn't do anything (to match behaviour of fgMorphCommutative) * Code review changes for tests * Removed all but one csproj * Added tests for scenarios with overflow, compound exprs, side effects * Add in some asserts and remove a redundant call * fix typo * Code review changes: * Formatting * Use getters instead of fields * set flags on op1 * Fix formatting
- Loading branch information
Benjamin Hodgson
authored
Oct 12, 2021
1 parent
e734fde
commit 68042e1
Showing
4 changed files
with
174 additions
and
0 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// | ||
using System; | ||
using System.Runtime.CompilerServices; | ||
|
||
// Test for https://github.com/dotnet/runtime/issues/13816 | ||
public class Test | ||
{ | ||
[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)] | ||
static int DowncastOr(int a, int b) | ||
{ | ||
return (byte)a | (byte)b; | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)] | ||
static long UpcastAnd(int a, int b) | ||
{ | ||
return (long)a & (long)b; | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)] | ||
static long UpcastAnd_ComplexExpression(int a, int b) | ||
{ | ||
return (long)(a - 2) & (long)(b + 1); | ||
} | ||
[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)] | ||
static long UpcastAnd_SideEffect(int a, int b, out int a1, out int b1) | ||
{ | ||
return (long)(a1 = a) & (long)(b1 = b); | ||
} | ||
[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)] | ||
static int DowncastAnd_Overflow(int a, int b) | ||
{ | ||
checked | ||
{ | ||
return (byte)a & (byte)b; | ||
} | ||
} | ||
|
||
public static int Main() | ||
{ | ||
const int Pass = 100; | ||
const int Fail = -1; | ||
|
||
if (DowncastOr(0x0F, 0xF0) != 0xFF) | ||
{ | ||
return Fail; | ||
} | ||
if (UpcastAnd(0x0FF, 0xFF0) != 0xF0) | ||
{ | ||
return Fail; | ||
} | ||
|
||
try | ||
{ | ||
DowncastAnd_Overflow(0x100, 0xFF); | ||
// should throw | ||
return Fail; | ||
} | ||
catch (OverflowException) | ||
{ | ||
// expected | ||
} | ||
|
||
{ | ||
var result = UpcastAnd_ComplexExpression(0x0FF, 0xFF0); | ||
if (result != 0xF1) | ||
{ | ||
return Fail; | ||
} | ||
} | ||
{ | ||
var result = UpcastAnd_SideEffect(0x0FF, 0xFF0, out var out1, out var out2); | ||
if (result != 0xF0 || out1 != 0x0FF || out2 != 0xFF0) | ||
{ | ||
return Fail; | ||
} | ||
} | ||
|
||
return Pass; | ||
} | ||
} |
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,10 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<DebugType>None</DebugType> | ||
<Optimize>True</Optimize> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Compile Include="CastThenBinop.cs" /> | ||
</ItemGroup> | ||
</Project> |