1 /*
2  * Copyright (c) 2017, 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.loop.test;
26 
27 import static org.graalvm.compiler.core.common.util.ReversedList.reversed;
28 
29 import java.util.HashSet;
30 import java.util.Set;
31 
32 import org.graalvm.compiler.core.test.GraalCompilerTest;
33 import org.graalvm.compiler.loop.LoopEx;
34 import org.graalvm.compiler.loop.LoopsData;
35 import org.graalvm.compiler.nodes.StructuredGraph;
36 import org.junit.Assert;
37 import org.junit.Test;
38 
39 public class LoopsDataTest extends GraalCompilerTest {
40 
41     @SuppressWarnings("unused")
loopy(int n)42     private static int loopy(int n) {
43         int t = n;
44         for (int i = 0; i < n; i++) {
45             t += i * n;
46         }
47         while (t != 0) {
48             if (t > 0) {
49                 for (int i = 0; i < 2 * n; i++) {
50                     t -= n + i;
51                 }
52             } else {
53                 for (int i = 0; i < n / 2; i++) {
54                     t += i * i;
55                     for (int j = 0; j < n; j++) {
56                         t += i * j * (((i + j) % 2) * 2 - 1);
57                     }
58                 }
59             }
60             for (int i = 0; i < n; i++) {
61                 for (int j = 0; j < n; j++) {
62                     t += i * j * (((i + j) % 2) * 2 - 1);
63                     for (int k = 0; k < n; k++) {
64                         t += i * k * (((i + k) % 2) * 2 - 1);
65                     }
66                 }
67             }
68             if (t % 17 == 0) {
69                 return t;
70             }
71         }
72         return -1;
73     }
74 
75     @Test
sanityTests()76     public void sanityTests() {
77         LoopsData loops = getLoopsData();
78         Assert.assertEquals(8, loops.outerFirst().size());
79         Assert.assertEquals(1, loops.outerFirst().get(0).loop().getDepth());
80         Assert.assertEquals(1, loops.outerFirst().get(1).loop().getDepth());
81         Assert.assertEquals(2, loops.outerFirst().get(2).loop().getDepth());
82         Assert.assertEquals(3, loops.outerFirst().get(3).loop().getDepth());
83         Assert.assertEquals(2, loops.outerFirst().get(4).loop().getDepth());
84         Assert.assertEquals(2, loops.outerFirst().get(5).loop().getDepth());
85         Assert.assertEquals(3, loops.outerFirst().get(6).loop().getDepth());
86         Assert.assertEquals(4, loops.outerFirst().get(7).loop().getDepth());
87 
88         for (LoopEx loop : loops.loops()) {
89             if (loop.parent() != null) {
90                 Assert.assertEquals(loop.parent().loop().getDepth() + 1, loop.loop().getDepth());
91             }
92         }
93     }
94 
95     @Test
testInnerFirst()96     public void testInnerFirst() {
97         LoopsData loops = getLoopsData();
98 
99         Set<LoopEx> seen = new HashSet<>();
100         for (LoopEx loop : reversed(loops.outerFirst())) {
101             assertFalse(seen.contains(loop), "%s has already been seen", loop);
102             if (loop.parent() != null) {
103                 assertFalse(seen.contains(loop.parent()), "%s's parent (%s) should not have already been seen", loop, loop.parent());
104             }
105             seen.add(loop);
106         }
107     }
108 
109     @Test
testouterFirst()110     public void testouterFirst() {
111         LoopsData loops = getLoopsData();
112 
113         Set<LoopEx> seen = new HashSet<>();
114         for (LoopEx loop : loops.outerFirst()) {
115             assertFalse(seen.contains(loop), "%s has already been seen", loop);
116             if (loop.parent() != null) {
117                 assertTrue(seen.contains(loop.parent()), "%s's parent (%s) should have already been seen", loop, loop.parent());
118             }
119             seen.add(loop);
120         }
121     }
122 
getLoopsData()123     private LoopsData getLoopsData() {
124         StructuredGraph graph = parseEager("loopy", StructuredGraph.AllowAssumptions.NO);
125         return new LoopsData(graph);
126     }
127 }
128