|
1 | 1 | /*
|
2 |
| - * Copyright (c) 2013, 2019, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2013, 2024, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
4 | 4 | *
|
5 | 5 | * This code is free software; you can redistribute it and/or modify it
|
|
24 | 24 | */
|
25 | 25 | package jdk.graal.compiler.nodes.test;
|
26 | 26 |
|
| 27 | +import org.junit.Assert; |
| 28 | +import org.junit.BeforeClass; |
| 29 | +import org.junit.Test; |
| 30 | + |
27 | 31 | import jdk.graal.compiler.api.directives.GraalDirectives;
|
28 | 32 | import jdk.graal.compiler.core.test.GraalCompilerTest;
|
| 33 | +import jdk.graal.compiler.graph.Node; |
| 34 | +import jdk.graal.compiler.graph.iterators.NodeIterable; |
29 | 35 | import jdk.graal.compiler.graph.iterators.NodePredicate;
|
| 36 | +import jdk.graal.compiler.nodes.GuardPhiNode; |
30 | 37 | import jdk.graal.compiler.nodes.LoopBeginNode;
|
31 | 38 | import jdk.graal.compiler.nodes.PhiNode;
|
32 | 39 | import jdk.graal.compiler.nodes.StructuredGraph;
|
33 | 40 | import jdk.graal.compiler.nodes.StructuredGraph.AllowAssumptions;
|
| 41 | +import jdk.graal.compiler.nodes.ValuePhiNode; |
| 42 | +import jdk.graal.compiler.nodes.debug.BlackholeNode; |
34 | 43 | import jdk.graal.compiler.nodes.spi.CoreProviders;
|
35 | 44 | import jdk.graal.compiler.phases.common.CanonicalizerPhase;
|
36 | 45 | import jdk.graal.compiler.phases.util.GraphOrder;
|
37 |
| -import org.junit.Assert; |
38 |
| -import org.junit.BeforeClass; |
39 |
| -import org.junit.Test; |
40 | 46 |
|
41 | 47 | public class LoopPhiCanonicalizerTest extends GraalCompilerTest {
|
42 | 48 |
|
@@ -261,4 +267,52 @@ private void testSchedulable(String snippet, int loopPhisAfter) {
|
261 | 267 | GraphOrder.assertSchedulableGraph(g);
|
262 | 268 | Assert.assertEquals(loopPhisAfter, g.getNodes().filter(PhiNode.class).filter(x -> ((PhiNode) x).isLoopPhi()).count());
|
263 | 269 | }
|
| 270 | + |
| 271 | + public static int loopSnippet06() { |
| 272 | + int sum = 0; |
| 273 | + for (int x : array) { |
| 274 | + sum += x; |
| 275 | + } |
| 276 | + return sum; |
| 277 | + } |
| 278 | + |
| 279 | + @Test |
| 280 | + public void test06() { |
| 281 | + StructuredGraph g = parseEager(getResolvedJavaMethod("loopSnippet06"), AllowAssumptions.NO); |
| 282 | + CanonicalizerPhase canonicalizer = CanonicalizerPhase.create(); |
| 283 | + canonicalizer.apply(g, getDefaultHighTierContext()); |
| 284 | + |
| 285 | + /* |
| 286 | + * JDK-8335915 regression test: Guard phi that has a loop begin node both as its merge and |
| 287 | + * as one of its guard inputs. |
| 288 | + */ |
| 289 | + LoopBeginNode loopBegin = g.getNodes(LoopBeginNode.TYPE).first(); |
| 290 | + GuardPhiNode guardPhi = g.addWithoutUnique(new GuardPhiNode(loopBegin, g.start(), loopBegin)); |
| 291 | + BlackholeNode blackhole = g.add(new BlackholeNode(guardPhi)); |
| 292 | + g.addAfterFixed(loopBegin, blackhole); |
| 293 | + |
| 294 | + /* The guard phi is counted twice in the loop begin's usages, but only once in its phis. */ |
| 295 | + assertValueAndGuardPhiCounts(loopBegin.usages(), 2, 2); |
| 296 | + assertValueAndGuardPhiCounts(loopBegin.phis(), 2, 1); |
| 297 | + |
| 298 | + canonicalizer.apply(g, getDefaultHighTierContext()); |
| 299 | + |
| 300 | + assertValueAndGuardPhiCounts(loopBegin.usages(), 2, 2); |
| 301 | + assertValueAndGuardPhiCounts(loopBegin.phis(), 2, 1); |
| 302 | + } |
| 303 | + |
| 304 | + private static void assertValueAndGuardPhiCounts(NodeIterable<? extends Node> nodes, int expectedValuePhis, int expectedGuardPhis) { |
| 305 | + int actualValuePhis = 0; |
| 306 | + int actualGuardPhis = 0; |
| 307 | + for (Node n : nodes) { |
| 308 | + if (n instanceof ValuePhiNode) { |
| 309 | + actualValuePhis++; |
| 310 | + } |
| 311 | + if (n instanceof GuardPhiNode) { |
| 312 | + actualGuardPhis++; |
| 313 | + } |
| 314 | + } |
| 315 | + Assert.assertEquals("value phis", expectedValuePhis, actualValuePhis); |
| 316 | + Assert.assertEquals("guard phis", expectedGuardPhis, actualGuardPhis); |
| 317 | + } |
264 | 318 | }
|
0 commit comments