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 isNullConstant(Value value) {
  75         assert value != null;
  76         return isJavaConstant(value) && asJavaConstant(value).isNull();
  77     }
  78 
  79     public static boolean isIntConstant(Value value, long expected) {
  80         if (isJavaConstant(value)) {
  81             JavaConstant javaConstant = asJavaConstant(value);
  82             if (javaConstant != null && javaConstant.getJavaKind().isNumericInteger()) {
  83                 return javaConstant.asLong() == expected;
  84             }
  85         }
  86         return false;
  87     }
  88 
  89     public static boolean isStackSlotValue(Value value) {
  90         assert value != null;
  91         return value instanceof StackSlot || value instanceof VirtualStackSlot;
  92     }
  93 
  94     public static boolean isVirtualStackSlot(Value value) {
  95         assert value != null;
  96         return value instanceof VirtualStackSlot;
  97     }
  98 
  99     public static VirtualStackSlot asVirtualStackSlot(Value value) {
 100         assert value != null;
 101         return (VirtualStackSlot) value;
 102     }
 103 
 104     public static boolean sameRegister(Value v1, Value v2) {
 105         return isRegister(v1) && isRegister(v2) && asRegister(v1).equals(asRegister(v2));
 106     }
 107 
 108     public static boolean sameRegister(Value v1, Value v2, Value v3) {
 109         return sameRegister(v1, v2) && sameRegister(v1, v3);
 110     }
 111 
 112     /**
 113      * Checks if all the provided values are different physical registers. The parameters can be
 114      * either {@link Register registers}, {@link Value values} or arrays of them. All values that
 115      * are not {@link RegisterValue registers} are ignored.
 116      */
 117     public static boolean differentRegisters(Object... values) {
 118         List<Register> registers = collectRegisters(values, new ArrayList<Register>());
 119         for (int i = 1; i < registers.size(); i++) {
 120             Register r1 = registers.get(i);
 121             for (int j = 0; j < i; j++) {
 122                 Register r2 = registers.get(j);
 123                 if (r1.equals(r2)) {
 124                     return false;
 125                 }
 126             }
 127         }
 128         return true;
 129     }
 130 
 131     private static List<Register> collectRegisters(Object[] values, List<Register> registers) {
 132         for (Object o : values) {
 133             if (o instanceof Register) {
 134                 registers.add((Register) o);
 135             } else if (o instanceof Value) {
 136                 if (isRegister((Value) o)) {
 137                     registers.add(asRegister((Value) o));
 138                 }
 139             } else if (o instanceof Object[]) {
 140                 collectRegisters((Object[]) o, registers);
 141             } else {
 142                 throw new IllegalArgumentException("Not a Register or Value: " + o);
 143             }
 144         }
 145         return registers;
 146     }
 147 
 148     /**
 149      * Subtract sets of registers (x - y).
 150      *
 151      * @param x a set of register to subtract from.
 152      * @param y a set of registers to subtract.
 153      * @return resulting set of registers (x - y).
 154      */
 155     public static Value[] subtractRegisters(Value[] x, Value[] y) {
 156         ArrayList<Value> result = new ArrayList<>(x.length);
 157         for (Value i : x) {
 158             boolean append = true;
 159             for (Value j : y) {
 160                 if (sameRegister(i, j)) {
 161                     append = false;
 162                     break;
 163                 }
 164             }
 165             if (append) {
 166                 result.add(i);
 167             }
 168         }
 169         Value[] resultArray = new Value[result.size()];
 170         return result.toArray(resultArray);
 171     }
 172 }