Mercurial > hg > graal-compiler
comparison graal/com.oracle.truffle.api.dsl.test/src/com/oracle/truffle/api/dsl/test/SpecializationGroupingTest.java @ 11195:4f52b08bd2f9
Truffle-DSL: Implemented specialization grouping for generic cases.
author | Christian Humer <christian.humer@gmail.com> |
---|---|
date | Thu, 01 Aug 2013 20:53:54 +0200 |
parents | |
children | b010fd3de42d |
comparison
equal
deleted
inserted
replaced
11194:14d5ff4683e0 | 11195:4f52b08bd2f9 |
---|---|
1 /* | |
2 * Copyright (c) 2012, 2012, 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 com.oracle.truffle.api.dsl.test; | |
24 | |
25 import org.junit.*; | |
26 | |
27 import com.oracle.truffle.api.*; | |
28 import com.oracle.truffle.api.dsl.*; | |
29 import com.oracle.truffle.api.dsl.test.SpecializationGroupingTestFactory.TestGroupingFactory; | |
30 import com.oracle.truffle.api.dsl.test.TypeSystemTest.SimpleTypes; | |
31 import com.oracle.truffle.api.dsl.test.TypeSystemTest.TestRootNode; | |
32 import com.oracle.truffle.api.dsl.test.TypeSystemTest.ValueNode; | |
33 import com.oracle.truffle.api.nodes.*; | |
34 | |
35 /** | |
36 * Tests execution counts of guards. While we do not make guarantees for guard invocation except for | |
37 * their execution order our implementation reduces the calls to guards as much as possible. | |
38 */ | |
39 public class SpecializationGroupingTest { | |
40 | |
41 @Test | |
42 public void testGrouping() { | |
43 MockAssumption a1 = new MockAssumption(true); | |
44 MockAssumption a2 = new MockAssumption(false); | |
45 MockAssumption a3 = new MockAssumption(true); | |
46 | |
47 TestRootNode<TestGrouping> root = TestHelper.createRoot(TestGroupingFactory.getInstance(), a1, a2, a3); | |
48 | |
49 SimpleTypes.intCast = 0; | |
50 SimpleTypes.intCheck = 0; | |
51 TestGrouping.true1 = 0; | |
52 TestGrouping.false1 = 0; | |
53 TestGrouping.true2 = 0; | |
54 TestGrouping.false2 = 0; | |
55 TestGrouping.true3 = 0; | |
56 | |
57 Assert.assertEquals(42, TestHelper.executeWith(root, 21, 21)); | |
58 Assert.assertEquals(1, TestGrouping.true1); | |
59 Assert.assertEquals(1, TestGrouping.false1); | |
60 Assert.assertEquals(1, TestGrouping.true2); | |
61 Assert.assertEquals(1, TestGrouping.false2); | |
62 Assert.assertEquals(1, TestGrouping.true3); | |
63 Assert.assertEquals(2, SimpleTypes.intCheck); | |
64 Assert.assertEquals(2, SimpleTypes.intCast); | |
65 Assert.assertEquals(1, a1.checked); | |
66 Assert.assertEquals(1, a2.checked); | |
67 Assert.assertEquals(1, a3.checked); | |
68 | |
69 Assert.assertEquals(42, TestHelper.executeWith(root, 21, 21)); | |
70 Assert.assertEquals(2, TestGrouping.true1); | |
71 Assert.assertEquals(1, TestGrouping.false1); | |
72 Assert.assertEquals(2, TestGrouping.true2); | |
73 Assert.assertEquals(2, TestGrouping.false2); | |
74 Assert.assertEquals(2, TestGrouping.true3); | |
75 | |
76 Assert.assertEquals(2, a1.checked); | |
77 Assert.assertEquals(1, a2.checked); | |
78 Assert.assertEquals(2, a3.checked); | |
79 Assert.assertEquals(2, SimpleTypes.intCheck); | |
80 Assert.assertEquals(2, SimpleTypes.intCast); | |
81 | |
82 } | |
83 | |
84 @SuppressWarnings("unused") | |
85 @NodeChildren({@NodeChild, @NodeChild}) | |
86 @NodeAssumptions({"a1", "a2", "a3"}) | |
87 public abstract static class TestGrouping extends ValueNode { | |
88 | |
89 private static int true1; | |
90 private static int false1; | |
91 private static int true2; | |
92 private static int false2; | |
93 private static int true3; | |
94 | |
95 protected boolean true1(int value) { | |
96 true1++; | |
97 return true; | |
98 } | |
99 | |
100 protected boolean false1(int value, int value2) { | |
101 false1++; | |
102 return false; | |
103 } | |
104 | |
105 protected boolean true2(int value) { | |
106 true2++; | |
107 return true; | |
108 } | |
109 | |
110 protected boolean false2(int value) { | |
111 false2++; | |
112 return false; | |
113 } | |
114 | |
115 protected boolean true3(int value) { | |
116 true3++; | |
117 return true; | |
118 } | |
119 | |
120 @Specialization(order = 1) | |
121 public int fail(int value1, String value2) { | |
122 throw new AssertionError(); | |
123 } | |
124 | |
125 @Specialization(order = 2, guards = {"true1", "false1"}) | |
126 public int fail1(int value1, int value2) { | |
127 throw new AssertionError(); | |
128 } | |
129 | |
130 @Specialization(order = 3, guards = {"true1", "true2"}, assumptions = {"a1", "a2"}) | |
131 public int fail2(int value1, int value2) { | |
132 throw new AssertionError(); | |
133 } | |
134 | |
135 @Specialization(order = 4, guards = {"true1", "true2"}, assumptions = {"a1", "a3"}, rewriteOn = RuntimeException.class) | |
136 public int throwRewrite(int value1, int value2) { | |
137 throw new RuntimeException(); | |
138 } | |
139 | |
140 @Specialization(order = 5, guards = {"true1", "true2", "false2"}, assumptions = {"a1", "a3"}) | |
141 public int fail4(int value1, int value2) { | |
142 throw new AssertionError(); | |
143 } | |
144 | |
145 @Specialization(order = 6, guards = {"true1", "true2", "!false2", "!true3"}, assumptions = {"a1", "a3"}) | |
146 public int fail5(int value1, int value2) { | |
147 throw new AssertionError(); | |
148 } | |
149 | |
150 @Specialization(order = 7, guards = {"true1", "true2", "!false2", "true3"}, assumptions = {"a1", "a3"}) | |
151 public int success(int value1, int value2) { | |
152 return value1 + value2; | |
153 } | |
154 | |
155 } | |
156 | |
157 private static class MockAssumption implements Assumption { | |
158 | |
159 int checked; | |
160 | |
161 private final boolean valid; | |
162 | |
163 public MockAssumption(boolean valid) { | |
164 this.valid = valid; | |
165 } | |
166 | |
167 public void check() throws InvalidAssumptionException { | |
168 checked++; | |
169 if (!valid) { | |
170 throw new InvalidAssumptionException(); | |
171 } | |
172 } | |
173 | |
174 public boolean isValid() { | |
175 checked++; | |
176 return valid; | |
177 } | |
178 | |
179 public void invalidate() { | |
180 throw new UnsupportedOperationException(); | |
181 } | |
182 | |
183 public String getName() { | |
184 throw new UnsupportedOperationException(); | |
185 } | |
186 | |
187 } | |
188 | |
189 } |