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