--- old/src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.loop.phases/src/org/graalvm/compiler/loop/phases/LoopFullUnrollPhase.java	2020-06-17 23:33:12.000000000 +0200
+++ new/src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.loop.phases/src/org/graalvm/compiler/loop/phases/LoopFullUnrollPhase.java	2020-06-17 23:33:12.000000000 +0200
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2012, 2020, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -24,6 +24,11 @@
 
 package org.graalvm.compiler.loop.phases;
 
+import java.util.Comparator;
+import java.util.List;
+import java.util.function.ToDoubleFunction;
+import java.util.function.ToIntFunction;
+
 import org.graalvm.compiler.core.common.GraalOptions;
 import org.graalvm.compiler.debug.CounterKey;
 import org.graalvm.compiler.debug.DebugContext;
@@ -32,11 +37,24 @@
 import org.graalvm.compiler.loop.LoopsData;
 import org.graalvm.compiler.nodes.StructuredGraph;
 import org.graalvm.compiler.nodes.spi.CoreProviders;
+import org.graalvm.compiler.options.Option;
+import org.graalvm.compiler.options.OptionKey;
+import org.graalvm.compiler.options.OptionType;
 import org.graalvm.compiler.phases.common.CanonicalizerPhase;
 
 public class LoopFullUnrollPhase extends LoopPhase<LoopPolicies> {
+    public static class Options {
+        @Option(help = "", type = OptionType.Expert) public static final OptionKey<Integer> FullUnrollMaxApplication = new OptionKey<>(60);
+    }
 
     private static final CounterKey FULLY_UNROLLED_LOOPS = DebugContext.counter("FullUnrolls");
+    public static final Comparator<LoopEx> LOOP_COMPARATOR;
+    static {
+        ToDoubleFunction<LoopEx> loopFreq = e -> e.loop().getHeader().getFirstPredecessor().getRelativeFrequency();
+        ToIntFunction<LoopEx> loopDepth = e -> e.loop().getDepth();
+        LOOP_COMPARATOR = Comparator.comparingDouble(loopFreq).thenComparingInt(loopDepth).reversed();
+    }
+
     private final CanonicalizerPhase canonicalizer;
 
     public LoopFullUnrollPhase(CanonicalizerPhase canonicalizer, LoopPolicies policies) {
@@ -50,11 +68,14 @@
             DebugContext debug = graph.getDebug();
             if (graph.hasLoops()) {
                 boolean peeled;
+                int applications = 0;
                 do {
                     peeled = false;
                     final LoopsData dataCounted = new LoopsData(graph);
                     dataCounted.detectedCountedLoops();
-                    for (LoopEx loop : dataCounted.countedLoops()) {
+                    List<LoopEx> countedLoops = dataCounted.countedLoops();
+                    countedLoops.sort(LOOP_COMPARATOR);
+                    for (LoopEx loop : countedLoops) {
                         if (getPolicies().shouldFullUnroll(loop)) {
                             debug.log("FullUnroll %s", loop);
                             LoopTransformations.fullUnroll(loop, context, canonicalizer);
@@ -65,7 +86,8 @@
                         }
                     }
                     dataCounted.deleteUnusedNodes();
-                } while (peeled);
+                    applications++;
+                } while (peeled && applications < Options.FullUnrollMaxApplication.getValue(graph.getOptions()));
             }
         }
     }
--- old/src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.loop/src/org/graalvm/compiler/loop/LoopsData.java	2020-06-17 23:33:13.000000000 +0200
+++ new/src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.loop/src/org/graalvm/compiler/loop/LoopsData.java	2020-06-17 23:33:13.000000000 +0200
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2012, 2020, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -25,8 +25,6 @@
 package org.graalvm.compiler.loop;
 
 import java.util.ArrayList;
-import java.util.Collection;
-import java.util.LinkedList;
 import java.util.List;
 
 import jdk.internal.vm.compiler.collections.EconomicMap;
@@ -92,8 +90,8 @@
         return loops;
     }
 
-    public Collection<LoopEx> countedLoops() {
-        List<LoopEx> counted = new LinkedList<>();
+    public List<LoopEx> countedLoops() {
+        List<LoopEx> counted = new ArrayList<>();
         for (LoopEx loop : loops()) {
             if (loop.isCounted()) {
                 counted.add(loop);
--- /dev/null	2020-06-17 23:33:14.000000000 +0200
+++ new/src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.core.test/src/org/graalvm/compiler/core/test/DeepUnrollingTest.java	2020-06-17 23:33:14.000000000 +0200
@@ -0,0 +1,86 @@
+/*
+ * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package org.graalvm.compiler.core.test;
+
+import java.io.IOException;
+import java.util.concurrent.TimeUnit;
+
+import org.graalvm.compiler.core.common.GraalOptions;
+import org.graalvm.compiler.options.OptionValues;
+import org.junit.Test;
+
+import jdk.vm.ci.meta.ResolvedJavaMethod;
+
+public class DeepUnrollingTest extends SubprocessTest {
+
+    public static void loops() {
+        for (int i = 0; i < 6; i++) {
+            for (int n = 2; n < 30; n++) {
+                for (int j = 1; j <= n; j++) {
+                    for (int k = 1; k <= j; k++) {
+                        // nop
+                    }
+                }
+            }
+        }
+    }
+
+    public static int reference(int a, int n) {
+        int v = a;
+        for (int i = 0; i < n; i++) {
+            if (v % 2 == 0) {
+                v = v / 2;
+            } else {
+                v = 3 * v + 1;
+            }
+        }
+        return v;
+    }
+
+    public void loopTest() {
+        // warmup
+        time("reference");
+        time("loops");
+        long reference = time("reference");
+        long loops = time("loops");
+        // observed ratio is ~20-30x. Pathological case before fix was ~300x
+        assertTrue("Compilation of the loop nest is too slow", loops < reference * 45);
+    }
+
+    public long time(String methodName) {
+        ResolvedJavaMethod method = getResolvedJavaMethod(methodName);
+        OptionValues options = new OptionValues(getInitialOptions(),
+                        GraalOptions.FullUnroll, true);
+        long start = System.nanoTime();
+        getCode(method, null, true, false, options);
+        long end = System.nanoTime();
+        return TimeUnit.NANOSECONDS.toMillis(end - start);
+    }
+
+    @Test
+    public void test() throws IOException, InterruptedException {
+        launchSubprocess(this::loopTest);
+    }
+}
