1 /*
   2  * Copyright (c) 2018, 2019, 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.core.test;
  26 
  27 import org.graalvm.compiler.debug.DebugContext;
  28 import org.graalvm.compiler.graph.Node;
  29 import org.graalvm.compiler.nodes.StructuredGraph;
  30 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
  31 import org.graalvm.compiler.nodes.calc.IntegerLessThanNode;
  32 import org.graalvm.compiler.nodes.spi.CoreProviders;
  33 import org.graalvm.compiler.nodes.spi.LoweringTool;
  34 import org.graalvm.compiler.phases.common.CanonicalizerPhase;
  35 import org.graalvm.compiler.phases.common.IterativeConditionalEliminationPhase;
  36 import org.graalvm.compiler.phases.common.LoweringPhase;
  37 import org.graalvm.compiler.virtual.phases.ea.EarlyReadEliminationPhase;
  38 import org.junit.Assert;
  39 import org.junit.Test;
  40 
  41 /**
  42  * Collection of tests for {@link org.graalvm.compiler.phases.common.ConditionalEliminationPhase}
  43  * including those that triggered bugs in this phase.
  44  */
  45 public class ConditionalEliminationTest15 extends ConditionalEliminationTestBase {
  46 
  47     private void checkNodeCount(String methodName, Class<? extends Node> nodeClass, int count) {
  48         StructuredGraph graph = parseEager(methodName, AllowAssumptions.YES);
  49 
  50         CanonicalizerPhase canonicalizer = new CanonicalizerPhase();
  51         CoreProviders context = getProviders();
  52 
  53         new LoweringPhase(new CanonicalizerPhase(), LoweringTool.StandardLoweringStage.HIGH_TIER).apply(graph, context);
  54         canonicalizer.apply(graph, context);
  55 
  56         // Merge arr.length reads.
  57         new EarlyReadEliminationPhase(canonicalizer).apply(graph, context);
  58         new IterativeConditionalEliminationPhase(canonicalizer, true).apply(graph, context);
  59 
  60         getDebugContext().dump(DebugContext.BASIC_LEVEL, graph, "After ConditionalEliminationPhase");
  61 
  62         Assert.assertEquals(count, graph.getNodes().filter(nodeClass).count());
  63     }
  64 
  65     public static int testRedundantIntegerLessThanNode(int index, int[] arr) {
  66         while (arr[index] != 42) {
  67             if (index >= 0) { // redundant
  68                 return 1;
  69             }
  70         }
  71         return 2;
  72     }
  73 
  74     public static int testRedundantIntegerLessThanNode2(int index, int[] arr) {
  75         while (arr[index] != 42) {
  76             if (index < arr.length) { // redundant
  77                 return 1;
  78             }
  79         }
  80         return 2;
  81     }
  82 
  83     @Test
  84     public void testRedundantSignedLessThanNode() {
  85         checkNodeCount("testRedundantIntegerLessThanNode", IntegerLessThanNode.class, 0);
  86         checkNodeCount("testRedundantIntegerLessThanNode2", IntegerLessThanNode.class, 0);
  87     }
  88 }