1 /*
   2  * Copyright (c) 2011, 2018, 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.lir;
  26 
  27 import static jdk.vm.ci.code.ValueUtil.asRegister;
  28 import static jdk.vm.ci.code.ValueUtil.isRegister;
  29 
  30 import java.util.ArrayList;
  31 import java.util.List;
  32 
  33 import jdk.vm.ci.code.Register;
  34 import jdk.vm.ci.code.RegisterValue;
  35 import jdk.vm.ci.code.StackSlot;
  36 import jdk.vm.ci.meta.Constant;
  37 import jdk.vm.ci.meta.JavaConstant;
  38 import jdk.vm.ci.meta.Value;
  39 
  40 public final class LIRValueUtil {
  41 
  42     public static boolean isVariable(Value value) {
  43         assert value != null;
  44         return value instanceof Variable;
  45     }
  46 
  47     public static Variable asVariable(Value value) {
  48         assert value != null;
  49         return (Variable) value;
  50     }
  51 
  52     public static boolean isConstantValue(Value value) {
  53         assert value != null;
  54         return value instanceof ConstantValue;
  55     }
  56 
  57     public static ConstantValue asConstantValue(Value value) {
  58         assert value != null;
  59         return (ConstantValue) value;
  60     }
  61 
  62     public static Constant asConstant(Value value) {
  63         return asConstantValue(value).getConstant();
  64     }
  65 
  66     public static boolean isJavaConstant(Value value) {
  67         return isConstantValue(value) && asConstantValue(value).isJavaConstant();
  68     }
  69 
  70     public static JavaConstant asJavaConstant(Value value) {
  71         return asConstantValue(value).getJavaConstant();
  72     }
  73 
  74     public static boolean isIntConstant(Value value, long expected) {
  75         if (isJavaConstant(value)) {
  76             JavaConstant javaConstant = asJavaConstant(value);
  77             if (javaConstant != null && javaConstant.getJavaKind().isNumericInteger()) {
  78                 return javaConstant.asLong() == expected;
  79             }
  80         }
  81         return false;
  82     }
  83 
  84     public static boolean isStackSlotValue(Value value) {
  85         assert value != null;
  86         return value instanceof StackSlot || value instanceof VirtualStackSlot;
  87     }
  88 
  89     public static boolean isVirtualStackSlot(Value value) {
  90         assert value != null;
  91         return value instanceof VirtualStackSlot;
  92     }
  93 
  94     public static VirtualStackSlot asVirtualStackSlot(Value value) {
  95         assert value != null;
  96         return (VirtualStackSlot) value;
  97     }
  98 
  99     public static boolean sameRegister(Value v1, Value v2) {
 100         return isRegister(v1) && isRegister(v2) && asRegister(v1).equals(asRegister(v2));
 101     }
 102 
 103     public static boolean sameRegister(Value v1, Value v2, Value v3) {
 104         return sameRegister(v1, v2) && sameRegister(v1, v3);
 105     }
 106 
 107     /**
 108      * Checks if all the provided values are different physical registers. The parameters can be
 109      * either {@link Register registers}, {@link Value values} or arrays of them. All values that
 110      * are not {@link RegisterValue registers} are ignored.
 111      */
 112     public static boolean differentRegisters(Object... values) {
 113         List<Register> registers = collectRegisters(values, new ArrayList<Register>());
 114         for (int i = 1; i < registers.size(); i++) {
 115             Register r1 = registers.get(i);
 116             for (int j = 0; j < i; j++) {
 117                 Register r2 = registers.get(j);
 118                 if (r1.equals(r2)) {
 119                     return false;
 120                 }
 121             }
 122         }
 123         return true;
 124     }
 125 
 126     private static List<Register> collectRegisters(Object[] values, List<Register> registers) {
 127         for (Object o : values) {
 128             if (o instanceof Register) {
 129                 registers.add((Register) o);
 130             } else if (o instanceof Value) {
 131                 if (isRegister((Value) o)) {
 132                     registers.add(asRegister((Value) o));
 133                 }
 134             } else if (o instanceof Object[]) {
 135                 collectRegisters((Object[]) o, registers);
 136             } else {
 137                 throw new IllegalArgumentException("Not a Register or Value: " + o);
 138             }
 139         }
 140         return registers;
 141     }
 142 
 143     /**
 144      * Subtract sets of registers (x - y).
 145      *
 146      * @param x a set of register to subtract from.
 147      * @param y a set of registers to subtract.
 148      * @return resulting set of registers (x - y).
 149      */
 150     public static Value[] subtractRegisters(Value[] x, Value[] y) {
 151         ArrayList<Value> result = new ArrayList<>(x.length);
 152         for (Value i : x) {
 153             boolean append = true;
 154             for (Value j : y) {
 155                 if (sameRegister(i, j)) {
 156                     append = false;
 157                     break;
 158                 }
 159             }
 160             if (append) {
 161                 result.add(i);
 162             }
 163         }
 164         Value[] resultArray = new Value[result.size()];
 165         return result.toArray(resultArray);
 166     }
 167 }