1 /* 2 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 25 package org.graalvm.compiler.core.test; 26 27 import org.graalvm.compiler.api.directives.GraalDirectives; 28 import org.graalvm.compiler.graph.Node; 29 import org.graalvm.compiler.graph.iterators.FilteredNodeIterable; 30 import org.graalvm.compiler.loop.LoopEx; 31 import org.graalvm.compiler.loop.LoopsData; 32 import org.graalvm.compiler.nodes.DeoptimizingNode; 33 import org.graalvm.compiler.nodes.StructuredGraph; 34 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions; 35 import org.junit.Assert; 36 import org.junit.Test; 37 38 public class CountedLoopTest2 extends GraalCompilerTest { 39 public static float countedDeoptLoop0(int n) { 40 float v = 0; 41 for (int i = 0; i < n; i++) { 42 v += 2.1f * i; 43 GraalDirectives.controlFlowAnchor(); 44 } 45 GraalDirectives.deoptimizeAndInvalidate(); 46 return v; 47 } 48 49 @Test 50 public void test0() { 51 test("countedDeoptLoop0"); 52 } 53 54 public static float countedDeoptLoop1(int n) { 55 float v = 0; 56 for (int i = 0; i < n; i++) { 57 v += 2.1f * i; 58 GraalDirectives.controlFlowAnchor(); 59 } 60 if (v > 0) { 61 if (v / 55 < 3) { 62 v -= 2; 63 GraalDirectives.controlFlowAnchor(); 64 } else { 65 v += 6; 66 GraalDirectives.controlFlowAnchor(); 67 } 68 } else { 69 v += 1; 70 GraalDirectives.controlFlowAnchor(); 71 } 72 GraalDirectives.deoptimizeAndInvalidate(); 73 return v; 74 } 75 76 @Test 77 public void test1() { 78 test("countedDeoptLoop1"); 79 } 80 81 public static float countedDeoptLoop2(int n, float init) { 82 float v = init; 83 if (v > 0) { 84 if (v / 55 < 3) { 85 for (int i = 0; i < n; i++) { 86 v += 2.1f * i; 87 GraalDirectives.controlFlowAnchor(); 88 } 89 } else { 90 for (int i = 0; i < n; i++) { 91 v += 1.1f * i; 92 GraalDirectives.controlFlowAnchor(); 93 } 94 } 95 } else { 96 for (int i = 0; i < n; i++) { 97 v += -0.1f * i; 98 GraalDirectives.controlFlowAnchor(); 99 } 100 } 101 GraalDirectives.deoptimizeAndInvalidate(); 102 return v; 103 } 104 105 @Test 106 public void test2() { 107 test("countedDeoptLoop2", 3); 108 } 109 110 private void test(String methodName) { 111 test(methodName, 1); 112 } 113 114 private void test(String methodName, int nLoops) { 115 StructuredGraph graph = parseEager(methodName, AllowAssumptions.YES); 116 LoopsData loops = new LoopsData(graph); 117 Assert.assertEquals(nLoops, loops.loops().size()); 118 for (LoopEx loop : loops.loops()) { 119 Assert.assertTrue(loop.detectCounted()); 120 } 121 122 StructuredGraph finalGraph = getFinalGraph(methodName); 123 loops = new LoopsData(finalGraph); 124 Assert.assertEquals(nLoops, loops.loops().size()); 125 FilteredNodeIterable<Node> nonStartDeopts = finalGraph.getNodes().filter(n -> { 126 return n instanceof DeoptimizingNode.DeoptBefore && ((DeoptimizingNode.DeoptBefore) n).stateBefore().bci > 0; 127 }); 128 Assert.assertTrue(nonStartDeopts.isNotEmpty()); 129 } 130 }