1 /*
   2  * Copyright (c) 2015, 2015, 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.core.test;
  24 
  25 import org.graalvm.compiler.graph.iterators.NodeIterable;
  26 import org.graalvm.compiler.nodes.ConstantNode;
  27 import org.graalvm.compiler.nodes.FixedGuardNode;
  28 import org.graalvm.compiler.nodes.GuardNode;
  29 import org.graalvm.compiler.nodes.LogicNode;
  30 import org.graalvm.compiler.nodes.StructuredGraph;
  31 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
  32 import org.graalvm.compiler.nodes.calc.IntegerBelowNode;
  33 import org.graalvm.compiler.nodes.java.LoadIndexedNode;
  34 import org.graalvm.compiler.nodes.memory.FloatingReadNode;
  35 import org.graalvm.compiler.nodes.memory.ReadNode;
  36 import org.graalvm.compiler.nodes.spi.LoweringTool;
  37 import org.graalvm.compiler.phases.common.CanonicalizerPhase;
  38 import org.graalvm.compiler.phases.common.FloatingReadPhase;
  39 import org.graalvm.compiler.phases.common.IterativeConditionalEliminationPhase;
  40 import org.graalvm.compiler.phases.common.LoweringPhase;
  41 import org.graalvm.compiler.phases.tiers.PhaseContext;
  42 import org.junit.Assert;
  43 import org.junit.Test;
  44 
  45 import jdk.vm.ci.meta.DeoptimizationReason;
  46 
  47 /**
  48  * Check that multiple bounds checks are correctly grouped together.
  49  */
  50 public class ConditionalEliminationTest14 extends ConditionalEliminationTestBase {
  51 
  52     public static void test1Snippet(Object[] args) {
  53         Object a5 = args[5];
  54         Object a7 = args[7];
  55         Object a6 = args[6];
  56 
  57         /*
  58          * The order of the conditions matters: The scheduler processes the floating reads for the
  59          * array loads in the order of the conditions here, and we want the index 7 access to be
  60          * processed before the index 6 access.
  61          */
  62         if (a5 != null && a7 != null && a6 != null) {
  63             sink1 = 1;
  64         }
  65         sink0 = 0;
  66     }
  67 
  68     @Test
  69     public void test1() {
  70         StructuredGraph graph = parseEager("test1Snippet", AllowAssumptions.YES);
  71         CanonicalizerPhase canonicalizer = new CanonicalizerPhase();
  72         PhaseContext context = new PhaseContext(getProviders());
  73 
  74         /* Convert the LoadIndexNode to ReadNode with floating guards. */
  75         new LoweringPhase(canonicalizer, LoweringTool.StandardLoweringStage.HIGH_TIER).apply(graph, context);
  76         /* Convert the ReadNode to FloatingReadNode. */
  77         new FloatingReadPhase().apply(graph);
  78         /* Apply the phase that we want to test. */
  79         new IterativeConditionalEliminationPhase(canonicalizer, true).apply(graph, context);
  80 
  81         Assert.assertEquals("All guards must be floating", 0, graph.getNodes(FixedGuardNode.TYPE).count());
  82         Assert.assertEquals("All array accesses must have been lowered", 0, graph.getNodes().filter(LoadIndexedNode.class).count());
  83         Assert.assertEquals("All reads must be floating", 0, graph.getNodes().filter(ReadNode.class).count());
  84         Assert.assertEquals("Must have floating reads (3 array accesses, 1 array length)", 4, graph.getNodes().filter(FloatingReadNode.class).count());
  85 
  86         NodeIterable<GuardNode> boundsChecks = graph.getNodes(GuardNode.TYPE).filter(n -> ((GuardNode) n).getReason() == DeoptimizationReason.BoundsCheckException);
  87         Assert.assertEquals("Must have only 1 bounds check remaining", 1, boundsChecks.count());
  88         LogicNode condition = boundsChecks.first().getCondition();
  89         Assert.assertTrue("Bounds check must check for array length 8", condition instanceof IntegerBelowNode && ((IntegerBelowNode) condition).getY().valueEquals(ConstantNode.forInt(8)));
  90     }
  91 }