1 /*
   2  * Copyright (c) 2013, 2014, 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.phases.common.inlining.info;
  24 
  25 import java.util.Collection;
  26 
  27 import org.graalvm.compiler.graph.Node;
  28 import org.graalvm.compiler.nodes.Invoke;
  29 import org.graalvm.compiler.phases.common.inlining.info.elem.Inlineable;
  30 import org.graalvm.compiler.phases.util.Providers;
  31 
  32 import jdk.vm.ci.meta.ResolvedJavaMethod;
  33 
  34 /**
  35  * Represents an inlining opportunity where the compiler can statically determine a monomorphic
  36  * target method and therefore is able to determine the called method exactly.
  37  */
  38 public class ExactInlineInfo extends AbstractInlineInfo {
  39 
  40     protected final ResolvedJavaMethod concrete;
  41     private Inlineable inlineableElement;
  42     private boolean suppressNullCheck;
  43 
  44     public ExactInlineInfo(Invoke invoke, ResolvedJavaMethod concrete) {
  45         super(invoke);
  46         this.concrete = concrete;
  47         assert concrete != null;
  48     }
  49 
  50     public void suppressNullCheck() {
  51         suppressNullCheck = true;
  52     }
  53 
  54     @Override
  55     public Collection<Node> inline(Providers providers) {
  56         return inline(invoke, concrete, inlineableElement, !suppressNullCheck);
  57     }
  58 
  59     @Override
  60     public void tryToDevirtualizeInvoke(Providers providers) {
  61         // nothing todo, can already be bound statically
  62     }
  63 
  64     @Override
  65     public int numberOfMethods() {
  66         return 1;
  67     }
  68 
  69     @Override
  70     public ResolvedJavaMethod methodAt(int index) {
  71         assert index == 0;
  72         return concrete;
  73     }
  74 
  75     @Override
  76     public double probabilityAt(int index) {
  77         assert index == 0;
  78         return 1.0;
  79     }
  80 
  81     @Override
  82     public double relevanceAt(int index) {
  83         assert index == 0;
  84         return 1.0;
  85     }
  86 
  87     @Override
  88     public String toString() {
  89         return "exact " + concrete.format("%H.%n(%p):%r");
  90     }
  91 
  92     @Override
  93     public Inlineable inlineableElementAt(int index) {
  94         assert index == 0;
  95         return inlineableElement;
  96     }
  97 
  98     @Override
  99     public void setInlinableElement(int index, Inlineable inlineableElement) {
 100         assert index == 0;
 101         this.inlineableElement = inlineableElement;
 102     }
 103 
 104     @Override
 105     public boolean shouldInline() {
 106         return concrete.shouldBeInlined();
 107     }
 108 }