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 package org.graalvm.compiler.loop.test; 24 25 import org.graalvm.compiler.core.test.GraalCompilerTest; 26 import org.graalvm.compiler.graph.iterators.NodeIterable; 27 import org.graalvm.compiler.nodes.LoopBeginNode; 28 import org.graalvm.compiler.nodes.StructuredGraph; 29 import org.junit.Ignore; 30 import org.junit.Test; 31 32 public class LoopPartialUnrollTest extends GraalCompilerTest { 33 34 @Override 35 protected boolean checkLowTierGraph(StructuredGraph graph) { 36 NodeIterable<LoopBeginNode> loops = graph.getNodes().filter(LoopBeginNode.class); 37 for (LoopBeginNode loop : loops) { 38 if (loop.isMainLoop()) { 39 return true; 40 } 41 } 42 return false; 43 } 44 45 public static long testMultiplySnippet(int arg) { 46 long r = 1; 47 for (int i = 0; branchProbability(0.99, i < arg); i++) { 48 r *= i; 49 } 50 return r; 51 } 52 53 @Test 54 public void testMultiply() { 55 test("testMultiplySnippet", 9); 56 } 57 58 public static int testNestedSumSnippet(int d) { 59 int c = 0; 60 for (int i = 0; i < d; i++) { 61 for (int j = 0; branchProbability(0.99, j < i); j++) { 62 c += j & 0x3; 63 } 64 } 65 return c; 66 } 67 68 @Test 69 public void testNestedSum() { 70 for (int i = 0; i < 1000; i++) { 71 test("testNestedSumSnippet", i); 72 } 73 } 74 75 public static int testSumDownSnippet(int d) { 76 int c = 0; 77 for (int j = d; branchProbability(0.99, j > -4); j--) { 78 c += j & 0x3; 79 } 80 return c; 81 } 82 83 @Test 84 public void testSumDown() { 85 test("testSumDownSnippet", 1); 86 for (int i = 0; i < 8; i++) { 87 test("testSumDownSnippet", i); 88 } 89 } 90 91 @Ignore("Phis which reference the backedge value of other Phis aren't handled properly") 92 @Test 93 public void testLoopCarried() { 94 test("testLoopCarriedSnippet", 1, 2); 95 } 96 97 public static int testLoopCarriedSnippet(int a, int b) { 98 int c = a; 99 int d = b; 100 for (int j = 0; j < a; j++) { 101 d = c; 102 c += 1; 103 } 104 return c + d; 105 } 106 107 public static long init = Runtime.getRuntime().totalMemory(); 108 private int x; 109 private int z; 110 111 public int[] testComplexSnippet(int d) { 112 x = 3; 113 int y = 5; 114 z = 7; 115 for (int i = 0; i < d; i++) { 116 for (int j = 0; branchProbability(0.99, j < i); j++) { 117 z += x; 118 } 119 y = x ^ z; 120 if ((i & 4) == 0) { 121 z--; 122 } else if ((i & 8) == 0) { 123 Runtime.getRuntime().totalMemory(); 124 } 125 } 126 return new int[]{x, y, z}; 127 } 128 129 @Test 130 public void testComplex() { 131 for (int i = 0; i < 10; i++) { 132 test("testComplexSnippet", i); 133 } 134 test("testComplexSnippet", 10); 135 test("testComplexSnippet", 100); 136 test("testComplexSnippet", 1000); 137 } 138 139 } | 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 package org.graalvm.compiler.loop.test; 24 25 import org.graalvm.compiler.core.test.GraalCompilerTest; 26 import org.graalvm.compiler.graph.iterators.NodeIterable; 27 import org.graalvm.compiler.nodes.LoopBeginNode; 28 import org.graalvm.compiler.nodes.StructuredGraph; 29 import org.junit.Test; 30 31 public class LoopPartialUnrollTest extends GraalCompilerTest { 32 33 @Override 34 protected boolean checkMidTierGraph(StructuredGraph graph) { 35 NodeIterable<LoopBeginNode> loops = graph.getNodes().filter(LoopBeginNode.class); 36 for (LoopBeginNode loop : loops) { 37 if (loop.isMainLoop()) { 38 return true; 39 } 40 } 41 return false; 42 } 43 44 public static long testMultiplySnippet(int arg) { 45 long r = 1; 46 for (int i = 0; branchProbability(0.99, i < arg); i++) { 47 r += r * i; 48 } 49 return r; 50 } 51 52 @Test 53 public void testMultiply() { 54 test("testMultiplySnippet", 9); 55 } 56 57 public static int testNestedSumSnippet(int d) { 58 int c = 0; 59 for (int i = 0; i < d; i++) { 60 for (int j = 0; branchProbability(0.99, j < i); j++) { 61 c += c + j & 0x3; 62 } 63 } 64 return c; 65 } 66 67 @Test 68 public void testNestedSumBy2() { 69 for (int i = 0; i < 1000; i++) { 70 test("testNestedSumBy2Snippet", i); 71 } 72 } 73 74 public static int testNestedSumBy2Snippet(int d) { 75 int c = 0; 76 for (int i = 0; i < d; i++) { 77 for (int j = 0; branchProbability(0.99, j < i); j += 2) { 78 c += c + j & 0x3; 79 } 80 } 81 return c; 82 } 83 84 @Test 85 public void testNestedSum() { 86 for (int i = 0; i < 1000; i++) { 87 test("testNestedSumSnippet", i); 88 } 89 } 90 91 public static int testSumDownSnippet(int d) { 92 int c = 0; 93 for (int j = d; branchProbability(0.99, j > -4); j--) { 94 c += c + j & 0x3; 95 } 96 return c; 97 } 98 99 @Test 100 public void testSumDown() { 101 test("testSumDownSnippet", 1); 102 for (int i = 0; i < 160; i++) { 103 test("testSumDownSnippet", i); 104 } 105 } 106 107 public static int testSumDownBy2Snippet(int d) { 108 int c = 0; 109 for (int j = d; branchProbability(0.99, j > -4); j -= 2) { 110 c += c + j & 0x3; 111 } 112 return c; 113 } 114 115 @Test 116 public void testSumDownBy2() { 117 test("testSumDownBy2Snippet", 1); 118 for (int i = 0; i < 160; i++) { 119 test("testSumDownBy2Snippet", i); 120 } 121 } 122 123 @Test 124 public void testLoopCarried() { 125 test("testLoopCarriedSnippet", 1, 2); 126 test("testLoopCarriedSnippet", 0, 4); 127 test("testLoopCarriedSnippet", 4, 0); 128 } 129 130 public static int testLoopCarriedSnippet(int a, int b) { 131 int c = a; 132 int d = b; 133 for (int j = 0; branchProbability(0.99, j < a); j++) { 134 d = c; 135 c += 1; 136 } 137 return c + d; 138 } 139 140 public static long init = Runtime.getRuntime().totalMemory(); 141 private int x; 142 private int z; 143 144 public int[] testComplexSnippet(int d) { 145 x = 3; 146 int y = 5; 147 z = 7; 148 for (int i = 0; i < d; i++) { 149 for (int j = 0; branchProbability(0.99, j < i); j++) { 150 z += x; 151 } 152 y = x ^ z; 153 if ((i & 4) == 0) { 154 z--; 155 } else if ((i & 8) == 0) { 156 Runtime.getRuntime().totalMemory(); 157 } 158 } 159 return new int[]{x, y, z}; 160 } 161 162 @Test 163 public void testComplex() { 164 for (int i = 0; i < 10; i++) { 165 test("testComplexSnippet", i); 166 } 167 test("testComplexSnippet", 10); 168 test("testComplexSnippet", 100); 169 test("testComplexSnippet", 1000); 170 } 171 172 public static long testSignExtensionSnippet(long arg) { 173 long r = 1; 174 for (int i = 0; branchProbability(0.99, i < arg); i++) { 175 r *= i; 176 } 177 return r; 178 } 179 180 @Test 181 public void testSignExtension() { 182 test("testSignExtensionSnippet", 9L); 183 } 184 } |