1 /*
   2  * Copyright (c) 2017, 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 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 }