Skip to content

Commit 106afdc

Browse files
authored
Use correct VN relation kind for redundant relop opts (#61912)
We could overwrite the candidate VN relation kind with one from an earlier tree that did not necessarily end up as a candidate. Fix #61908
1 parent 259b561 commit 106afdc

File tree

3 files changed

+45
-11
lines changed

3 files changed

+45
-11
lines changed

src/coreclr/jit/redundantbranchopts.cpp

+13-11
Original file line numberDiff line numberDiff line change
@@ -847,11 +847,11 @@ bool Compiler::optRedundantRelop(BasicBlock* const block)
847847
// * makes the current relop redundant;
848848
// * can safely and profitably forward substituted to the jump.
849849
//
850-
Statement* prevStmt = stmt;
851-
GenTree* candidateTree = nullptr;
852-
Statement* candidateStmt = nullptr;
853-
ValueNumStore::VN_RELATION_KIND vnRelationMatch = ValueNumStore::VN_RELATION_KIND::VRK_Same;
854-
bool sideEffect = false;
850+
Statement* prevStmt = stmt;
851+
GenTree* candidateTree = nullptr;
852+
Statement* candidateStmt = nullptr;
853+
ValueNumStore::VN_RELATION_KIND candidateVnRelation = ValueNumStore::VN_RELATION_KIND::VRK_Same;
854+
bool sideEffect = false;
855855

856856
const ValueNumStore::VN_RELATION_KIND vnRelations[] = {ValueNumStore::VN_RELATION_KIND::VRK_Same,
857857
ValueNumStore::VN_RELATION_KIND::VRK_Reverse,
@@ -984,8 +984,9 @@ bool Compiler::optRedundantRelop(BasicBlock* const block)
984984
// If the normal liberal VN of RHS is the normal liberal VN of the current tree, or is "related",
985985
// consider forward sub.
986986
//
987-
const ValueNum domCmpVN = vnStore->VNNormalValue(prevTreeRHS->GetVN(VNK_Liberal));
988-
bool matched = false;
987+
const ValueNum domCmpVN = vnStore->VNNormalValue(prevTreeRHS->GetVN(VNK_Liberal));
988+
bool matched = false;
989+
ValueNumStore::VN_RELATION_KIND vnRelationMatch = ValueNumStore::VN_RELATION_KIND::VRK_Same;
989990

990991
for (auto vnRelation : vnRelations)
991992
{
@@ -1058,8 +1059,9 @@ bool Compiler::optRedundantRelop(BasicBlock* const block)
10581059
}
10591060

10601061
JITDUMP(" -- prev tree is viable candidate for relop fwd sub!\n");
1061-
candidateTree = prevTreeRHS;
1062-
candidateStmt = prevStmt;
1062+
candidateTree = prevTreeRHS;
1063+
candidateStmt = prevStmt;
1064+
candidateVnRelation = vnRelationMatch;
10631065
}
10641066

10651067
if (candidateTree == nullptr)
@@ -1088,8 +1090,8 @@ bool Compiler::optRedundantRelop(BasicBlock* const block)
10881090
// If we need the reverse compare, make it so.
10891091
// We also need to set a proper VN.
10901092
//
1091-
if ((vnRelationMatch == ValueNumStore::VN_RELATION_KIND::VRK_Reverse) ||
1092-
(vnRelationMatch == ValueNumStore::VN_RELATION_KIND::VRK_SwapReverse))
1093+
if ((candidateVnRelation == ValueNumStore::VN_RELATION_KIND::VRK_Reverse) ||
1094+
(candidateVnRelation == ValueNumStore::VN_RELATION_KIND::VRK_SwapReverse))
10931095
{
10941096
// Copy the vn info as it will be trashed when we change the oper.
10951097
//
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
public class Runtime_61908
5+
{
6+
public static bool s_3;
7+
public static int Main()
8+
{
9+
var vr6 = M3(s_3);
10+
if (M3(vr6))
11+
{
12+
return -1;
13+
}
14+
15+
return 100;
16+
}
17+
18+
public static bool M3(bool arg0)
19+
{
20+
arg0 = !arg0;
21+
return arg0;
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<Optimize>True</Optimize>
5+
</PropertyGroup>
6+
<ItemGroup>
7+
<Compile Include="$(MSBuildProjectName).cs" />
8+
</ItemGroup>
9+
</Project>

0 commit comments

Comments
 (0)