Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/coreclr/jit/optimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2142,6 +2142,11 @@ bool Compiler::optTryInvertWhileLoop(FlowGraphNaturalLoop* loop)
// block will be the new preheader.
BasicBlock* const newPreheader = fgSplitBlockAtEnd(preheader);

// 'preheader' stays the entry of any EH region it begins, so the new block does not
// need the BBF_DONT_REMOVE it inherited; keeping it would leave an unremovable block
// behind if the loop later becomes unreachable.
newPreheader->RemoveFlags(BBF_DONT_REMOVE);

// Make sure exit stays canonical
BasicBlock* nonEnterBlock = fgSplitBlockAtBeginning(exit);

Expand Down
44 changes: 44 additions & 0 deletions src/tests/JIT/Regression_ro_2/Runtime_136041.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// 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;
using Xunit;

public class Runtime_136041
{
[Fact]
public static void TestEntryPoint()
{
Assert.Equal(0, Test(new int[] { 1 }, 0));
Assert.Equal(1, Test(new int[] { 1 }, 1));
}

// Loop inversion splits the catch handler entry to create the loop preheader,
// then the loop condition folds to false, leaving an unreachable block that
// inherited the handler entry's "don't remove" flag.
[MethodImpl(MethodImplOptions.NoInlining)]
private static int Test(int[] p, int d)
{
int result = 0;
try
{
result = 1 / d;
GC.KeepAlive(p);
}
catch (DivideByZeroException)
{
while (checked(p[0] > p[0]))
{
try
{
GC.Collect();
}
catch (Exception) { }
}
}
catch (Exception) { }

return result;
}
}
Loading