1 /*
   2  * Copyright (c) 2009, 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.nodes;
  24 
  25 import java.util.stream.Collectors;
  26 
  27 import org.graalvm.compiler.core.common.type.Stamp;
  28 import org.graalvm.compiler.core.common.type.StampFactory;
  29 import org.graalvm.compiler.graph.NodeClass;
  30 import org.graalvm.compiler.graph.NodeInputList;
  31 import org.graalvm.compiler.nodeinfo.NodeInfo;
  32 import org.graalvm.compiler.nodes.spi.ArrayLengthProvider;
  33 import org.graalvm.compiler.nodes.type.StampTool;
  34 import org.graalvm.compiler.nodes.util.GraphUtil;
  35 
  36 /**
  37  * Value {@link PhiNode}s merge data flow values at control flow merges.
  38  */
  39 @NodeInfo(nameTemplate = "Phi({i#values})")
  40 public class ValuePhiNode extends PhiNode implements ArrayLengthProvider {
  41 
  42     public static final NodeClass<ValuePhiNode> TYPE = NodeClass.create(ValuePhiNode.class);
  43     @Input protected NodeInputList<ValueNode> values;
  44 
  45     public ValuePhiNode(Stamp stamp, AbstractMergeNode merge) {
  46         this(TYPE, stamp, merge);
  47     }
  48 
  49     protected ValuePhiNode(NodeClass<? extends ValuePhiNode> c, Stamp stamp, AbstractMergeNode merge) {
  50         super(c, stamp, merge);
  51         assert stamp != StampFactory.forVoid();
  52         values = new NodeInputList<>(this);
  53     }
  54 
  55     public ValuePhiNode(Stamp stamp, AbstractMergeNode merge, ValueNode[] values) {
  56         super(TYPE, stamp, merge);
  57         assert stamp != StampFactory.forVoid();
  58         this.values = new NodeInputList<>(this, values);
  59     }
  60 
  61     @Override
  62     public NodeInputList<ValueNode> values() {
  63         return values;
  64     }
  65 
  66     @Override
  67     public boolean inferStamp() {
  68         /*
  69          * Meet all the values feeding this Phi but don't use the stamp of this Phi since that's
  70          * what's being computed.
  71          */
  72         Stamp valuesStamp = StampTool.meetOrNull(values(), this);
  73         if (valuesStamp == null) {
  74             valuesStamp = stamp;
  75         } else if (stamp.isCompatible(valuesStamp)) {
  76             valuesStamp = stamp.join(valuesStamp);
  77         }
  78         return updateStamp(valuesStamp);
  79     }
  80 
  81     @Override
  82     public ValueNode length() {
  83         if (merge() instanceof LoopBeginNode) {
  84             return null;
  85         }
  86         ValueNode length = null;
  87         for (ValueNode input : values()) {
  88             ValueNode l = GraphUtil.arrayLength(input);
  89             if (l == null) {
  90                 return null;
  91             }
  92             if (length == null) {
  93                 length = l;
  94             } else if (length != l) {
  95                 return null;
  96             }
  97         }
  98         return length;
  99     }
 100 
 101     @Override
 102     public boolean verify() {
 103         Stamp s = null;
 104         for (ValueNode input : values()) {
 105             if (s == null) {
 106                 s = input.stamp();
 107             } else {
 108                 if (!s.isCompatible(input.stamp())) {
 109                     fail("Phi Input Stamps are not compatible. Phi:%s inputs:%s", this, values().stream().map(x -> x.toString() + ":" + x.stamp()).collect(Collectors.joining(", ")));
 110                 }
 111             }
 112         }
 113         return super.verify();
 114     }
 115 }