1 /*
   2  * Copyright (c) 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 jdk.vm.ci.meta.JavaConstant;
  26 
  27 import static org.graalvm.compiler.core.common.CompilationIdentifier.INVALID_COMPILATION_ID;
  28 
  29 import org.junit.Assert;
  30 import org.junit.Test;
  31 
  32 import org.graalvm.compiler.nodes.ConstantNode;
  33 import org.graalvm.compiler.nodes.ReturnNode;
  34 import org.graalvm.compiler.nodes.StructuredGraph;
  35 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
  36 import org.graalvm.compiler.nodes.ValueNode;
  37 import org.graalvm.compiler.nodes.calc.AddNode;
  38 import org.graalvm.compiler.nodes.debug.OpaqueNode;
  39 import org.graalvm.compiler.phases.common.CanonicalizerPhase;
  40 import org.graalvm.compiler.phases.schedule.SchedulePhase;
  41 import org.graalvm.compiler.phases.schedule.SchedulePhase.SchedulingStrategy;
  42 import org.graalvm.compiler.phases.tiers.HighTierContext;
  43 
  44 public class LongNodeChainTest extends GraalCompilerTest {
  45 
  46     public static final int N = 10000;
  47 
  48     private static final SchedulingStrategy[] Strategies = new SchedulingStrategy[]{SchedulingStrategy.EARLIEST};
  49 
  50     @Test
  51     public void testLongAddChain() {
  52         longAddChain(true);
  53         longAddChain(false);
  54     }
  55 
  56     private void longAddChain(boolean reverse) {
  57         HighTierContext context = getDefaultHighTierContext();
  58         StructuredGraph graph = new StructuredGraph(AllowAssumptions.NO, INVALID_COMPILATION_ID);
  59         ValueNode constant = graph.unique(ConstantNode.forPrimitive(JavaConstant.INT_1));
  60         ValueNode value = null;
  61         if (reverse) {
  62             // Make sure the constant's stamp is not used to infer the add node's stamp.
  63             OpaqueNode opaque = graph.unique(new OpaqueNode(constant));
  64             constant = opaque;
  65             AddNode addNode = graph.unique(new AddNode(constant, constant));
  66             value = addNode;
  67             for (int i = 1; i < N; ++i) {
  68                 AddNode newAddNode = graph.addWithoutUnique(new AddNode(constant, constant));
  69                 addNode.setY(newAddNode);
  70                 addNode = newAddNode;
  71             }
  72             opaque.replaceAndDelete(opaque.getValue());
  73         } else {
  74             value = constant;
  75             for (int i = 0; i < N; ++i) {
  76                 value = graph.unique(new AddNode(constant, value));
  77             }
  78         }
  79         ReturnNode returnNode = graph.add(new ReturnNode(value));
  80         graph.start().setNext(returnNode);
  81 
  82         for (SchedulingStrategy s : Strategies) {
  83             new SchedulePhase(s).apply(graph);
  84         }
  85 
  86         new CanonicalizerPhase().apply(graph, context);
  87         JavaConstant asConstant = (JavaConstant) returnNode.result().asConstant();
  88         Assert.assertEquals(N + 1, asConstant.asInt());
  89     }
  90 }