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