34 import org.graalvm.compiler.nodes.StructuredGraph; |
34 import org.graalvm.compiler.nodes.StructuredGraph; |
35 import org.graalvm.compiler.nodes.ValueNode; |
35 import org.graalvm.compiler.nodes.ValueNode; |
36 import org.graalvm.compiler.nodes.spi.CoreProviders; |
36 import org.graalvm.compiler.nodes.spi.CoreProviders; |
37 import org.graalvm.compiler.phases.BasePhase; |
37 import org.graalvm.compiler.phases.BasePhase; |
38 import org.graalvm.compiler.phases.common.CanonicalizerPhase; |
38 import org.graalvm.compiler.phases.common.CanonicalizerPhase; |
39 import org.graalvm.compiler.phases.common.CanonicalizerPhase.CustomCanonicalizer; |
39 import org.graalvm.compiler.phases.common.CanonicalizerPhase.CustomSimplification; |
40 import org.graalvm.compiler.phases.contract.NodeCostUtil; |
40 import org.graalvm.compiler.phases.contract.NodeCostUtil; |
41 import org.graalvm.compiler.phases.tiers.HighTierContext; |
41 import org.graalvm.compiler.phases.tiers.HighTierContext; |
42 import org.junit.Assert; |
42 import org.junit.Assert; |
43 import org.junit.Test; |
43 import org.junit.Test; |
44 |
44 |
160 } |
160 } |
161 |
161 |
162 @Test |
162 @Test |
163 public void testCanonicalizationExample() { |
163 public void testCanonicalizationExample() { |
164 HighTierContext htc = getDefaultHighTierContext(); |
164 HighTierContext htc = getDefaultHighTierContext(); |
165 ImprovementSavingCanonicalizer c1 = new ImprovementSavingCanonicalizer(); |
165 ImprovementSavingCalculator c1 = new ImprovementSavingCalculator(); |
166 StructuredGraph g1 = parseForCompile(getResolvedJavaMethod("test1Snippet")); |
166 StructuredGraph g1 = parseForCompile(getResolvedJavaMethod("test1Snippet")); |
167 new CanonicalizerPhase(c1).apply(g1, htc); |
167 CanonicalizerPhase canonicalizer1 = this.createCanonicalizerPhase().copyWithCustomSimplification(c1); |
168 ImprovementSavingCanonicalizer c2 = new ImprovementSavingCanonicalizer(); |
168 canonicalizer1.apply(g1, htc); |
|
169 ImprovementSavingCalculator c2 = new ImprovementSavingCalculator(); |
169 StructuredGraph g2 = parseForCompile(getResolvedJavaMethod("test2Snippet")); |
170 StructuredGraph g2 = parseForCompile(getResolvedJavaMethod("test2Snippet")); |
170 new CanonicalizerPhase(c2).apply(g2, htc); |
171 CanonicalizerPhase canonicalizer2 = this.createCanonicalizerPhase().copyWithCustomSimplification(c2); |
|
172 canonicalizer2.apply(g2, htc); |
171 Assert.assertEquals(0, c1.savedCycles); |
173 Assert.assertEquals(0, c1.savedCycles); |
172 Assert.assertEquals(0, c2.savedCycles); |
174 Assert.assertEquals(0, c2.savedCycles); |
173 } |
175 } |
174 |
176 |
175 private static void prepareGraphForLoopFrequencies(StructuredGraph g, HighTierContext htc) { |
177 private void prepareGraphForLoopFrequencies(StructuredGraph g, HighTierContext htc) { |
176 // let canonicalizer work away branch probability nodes |
178 // let canonicalizer work away branch probability nodes |
177 new CanonicalizerPhase().apply(g, htc); |
179 createCanonicalizerPhase().apply(g, htc); |
178 // recompute the loop frequencies |
180 // recompute the loop frequencies |
179 ComputeLoopFrequenciesClosure.compute(g); |
181 ComputeLoopFrequenciesClosure.compute(g); |
180 } |
182 } |
181 |
183 |
182 private static void assertFrequency(StructuredGraph g, int iterations) { |
184 private static void assertFrequency(StructuredGraph g, int iterations) { |
240 @Test |
242 @Test |
241 public void testGraphCost() { |
243 public void testGraphCost() { |
242 StructuredGraph g1 = parseForCompile(getResolvedJavaMethod("test1Snippet")); |
244 StructuredGraph g1 = parseForCompile(getResolvedJavaMethod("test1Snippet")); |
243 StructuredGraph g2 = parseForCompile(getResolvedJavaMethod("test2Snippet")); |
245 StructuredGraph g2 = parseForCompile(getResolvedJavaMethod("test2Snippet")); |
244 HighTierContext htc = getDefaultHighTierContext(); |
246 HighTierContext htc = getDefaultHighTierContext(); |
245 new CanonicalizerPhase().apply(g1, htc); |
247 createCanonicalizerPhase().apply(g1, htc); |
246 new CanonicalizerPhase().apply(g2, htc); |
248 createCanonicalizerPhase().apply(g2, htc); |
247 GraphCostPhase gc1 = new GraphCostPhase(); |
249 GraphCostPhase gc1 = new GraphCostPhase(); |
248 GraphCostPhase gc2 = new GraphCostPhase(); |
250 GraphCostPhase gc2 = new GraphCostPhase(); |
249 gc1.apply(g1, htc); |
251 gc1.apply(g1, htc); |
250 gc2.apply(g2, htc); |
252 gc2.apply(g2, htc); |
251 g1.getDebug().log("Test Graph Cost --> 1.Graph cost:%f vs. 2.Graph cost:%f\n", gc1.finalCycles, gc2.finalCycles); |
253 g1.getDebug().log("Test Graph Cost --> 1.Graph cost:%f vs. 2.Graph cost:%f\n", gc1.finalCycles, gc2.finalCycles); |
255 |
257 |
256 @Test |
258 @Test |
257 public void testExpectUntrusted() { |
259 public void testExpectUntrusted() { |
258 StructuredGraph g1 = parseForCompile(getResolvedJavaMethod("untrused01")); |
260 StructuredGraph g1 = parseForCompile(getResolvedJavaMethod("untrused01")); |
259 HighTierContext htc = getDefaultHighTierContext(); |
261 HighTierContext htc = getDefaultHighTierContext(); |
260 new CanonicalizerPhase().apply(g1, htc); |
262 createCanonicalizerPhase().apply(g1, htc); |
261 GraphCostPhase gc1 = new GraphCostPhase(); |
263 GraphCostPhase gc1 = new GraphCostPhase(); |
262 gc1.apply(g1, htc); |
264 gc1.apply(g1, htc); |
263 } |
265 } |
264 |
266 |
265 @Test |
267 @Test |
266 public void testArrayLoad() { |
268 public void testArrayLoad() { |
267 StructuredGraph g1 = parseForCompile(getResolvedJavaMethod("arrayLoadTest")); |
269 StructuredGraph g1 = parseForCompile(getResolvedJavaMethod("arrayLoadTest")); |
268 HighTierContext htc = getDefaultHighTierContext(); |
270 HighTierContext htc = getDefaultHighTierContext(); |
269 new CanonicalizerPhase().apply(g1, htc); |
271 createCanonicalizerPhase().apply(g1, htc); |
270 GraphCostPhase gc1 = new GraphCostPhase(); |
272 GraphCostPhase gc1 = new GraphCostPhase(); |
271 gc1.apply(g1, htc); |
273 gc1.apply(g1, htc); |
272 Assert.assertEquals(15, gc1.finalCycles, 25); |
274 Assert.assertEquals(15, gc1.finalCycles, 25); |
273 } |
275 } |
274 |
276 |
275 @Test |
277 @Test |
276 public void testArrayStore() { |
278 public void testArrayStore() { |
277 StructuredGraph g1 = parseForCompile(getResolvedJavaMethod("arrayStoreTest")); |
279 StructuredGraph g1 = parseForCompile(getResolvedJavaMethod("arrayStoreTest")); |
278 HighTierContext htc = getDefaultHighTierContext(); |
280 HighTierContext htc = getDefaultHighTierContext(); |
279 new CanonicalizerPhase().apply(g1, htc); |
281 createCanonicalizerPhase().apply(g1, htc); |
280 GraphCostPhase gc1 = new GraphCostPhase(); |
282 GraphCostPhase gc1 = new GraphCostPhase(); |
281 gc1.apply(g1, htc); |
283 gc1.apply(g1, htc); |
282 Assert.assertEquals(15, gc1.finalCycles, 25); |
284 Assert.assertEquals(15, gc1.finalCycles, 25); |
283 } |
285 } |
284 |
286 |
285 @Test |
287 @Test |
286 public void testFieldLoad() { |
288 public void testFieldLoad() { |
287 StructuredGraph g1 = parseForCompile(getResolvedJavaMethod("fieldLoad")); |
289 StructuredGraph g1 = parseForCompile(getResolvedJavaMethod("fieldLoad")); |
288 HighTierContext htc = getDefaultHighTierContext(); |
290 HighTierContext htc = getDefaultHighTierContext(); |
289 new CanonicalizerPhase().apply(g1, htc); |
291 createCanonicalizerPhase().apply(g1, htc); |
290 GraphCostPhase gc1 = new GraphCostPhase(); |
292 GraphCostPhase gc1 = new GraphCostPhase(); |
291 gc1.apply(g1, htc); |
293 gc1.apply(g1, htc); |
292 Assert.assertEquals(15, gc1.finalCycles, 25); |
294 Assert.assertEquals(15, gc1.finalCycles, 25); |
293 } |
295 } |
294 |
296 |
295 @Test |
297 @Test |
296 public void testFieldStore() { |
298 public void testFieldStore() { |
297 StructuredGraph g1 = parseForCompile(getResolvedJavaMethod("fieldStore")); |
299 StructuredGraph g1 = parseForCompile(getResolvedJavaMethod("fieldStore")); |
298 HighTierContext htc = getDefaultHighTierContext(); |
300 HighTierContext htc = getDefaultHighTierContext(); |
299 new CanonicalizerPhase().apply(g1, htc); |
301 createCanonicalizerPhase().apply(g1, htc); |
300 GraphCostPhase gc1 = new GraphCostPhase(); |
302 GraphCostPhase gc1 = new GraphCostPhase(); |
301 gc1.apply(g1, htc); |
303 gc1.apply(g1, htc); |
302 Assert.assertEquals(15, gc1.finalCycles, 25); |
304 Assert.assertEquals(15, gc1.finalCycles, 25); |
303 } |
305 } |
304 |
306 |
305 static class ImprovementSavingCanonicalizer extends CustomCanonicalizer { |
307 static class ImprovementSavingCalculator implements CustomSimplification { |
306 private int savedCycles; |
308 private int savedCycles; |
307 |
309 |
308 @Override |
310 @Override |
309 public void simplify(Node node, SimplifierTool tool) { |
311 public void simplify(Node node, SimplifierTool tool) { |
310 if (node instanceof Canonicalizable.Binary<?>) { |
312 if (node instanceof Canonicalizable.Binary<?>) { |