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