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.lir.gen; 24 25 import org.graalvm.compiler.core.common.LIRKind; 26 import org.graalvm.compiler.lir.Variable; 27 import org.graalvm.compiler.options.OptionValues; 28 29 import jdk.vm.ci.meta.AllocatableValue; 30 import jdk.vm.ci.meta.PlatformKind; 31 import jdk.vm.ci.meta.Value; 32 33 /** 34 * This class traverses the HIR instructions and generates LIR instructions from them. 35 */ 36 public abstract class ArithmeticLIRGenerator implements ArithmeticLIRGeneratorTool { 37 38 LIRGenerator lirGen; 39 40 public LIRGenerator getLIRGen() { 41 return lirGen; 42 } 43 44 public OptionValues getOptions() { 45 return getLIRGen().getResult().getLIR().getOptions(); 46 } 47 // automatic derived reference handling 48 49 protected abstract boolean isNumericInteger(PlatformKind kind); 50 51 protected abstract Variable emitAdd(LIRKind resultKind, Value a, Value b, boolean setFlags); 52 53 @Override 54 public final Variable emitAdd(Value aVal, Value bVal, boolean setFlags) { 55 LIRKind resultKind; 56 Value a = aVal; 57 Value b = bVal; 58 59 if (isNumericInteger(a.getPlatformKind())) { 60 LIRKind aKind = a.getValueKind(LIRKind.class); 61 LIRKind bKind = b.getValueKind(LIRKind.class); 62 assert a.getPlatformKind() == b.getPlatformKind() : a.getPlatformKind() + " vs. " + b.getPlatformKind(); 63 64 if (aKind.isUnknownReference()) { 65 resultKind = aKind; 66 } else if (bKind.isUnknownReference()) { 67 resultKind = bKind; 68 } else if (aKind.isValue() && bKind.isValue()) { 69 resultKind = aKind; 70 } else if (aKind.isValue()) { 71 if (bKind.isDerivedReference()) { 72 resultKind = bKind; 73 } else { 74 AllocatableValue allocatable = getLIRGen().asAllocatable(b); 75 resultKind = bKind.makeDerivedReference(allocatable); 76 b = allocatable; 77 } 78 } else if (bKind.isValue()) { 79 if (aKind.isDerivedReference()) { 80 resultKind = aKind; 81 } else { 82 AllocatableValue allocatable = getLIRGen().asAllocatable(a); 83 resultKind = aKind.makeDerivedReference(allocatable); 84 a = allocatable; 85 } 86 } else { 87 resultKind = aKind.makeUnknownReference(); 88 } 89 } else { 90 resultKind = LIRKind.combine(a, b); 91 } 92 93 return emitAdd(resultKind, a, b, setFlags); 94 } 95 96 protected abstract Variable emitSub(LIRKind resultKind, Value a, Value b, boolean setFlags); 97 98 @Override 99 public final Variable emitSub(Value aVal, Value bVal, boolean setFlags) { 100 LIRKind resultKind; 101 Value a = aVal; 102 Value b = bVal; 103 104 if (isNumericInteger(a.getPlatformKind())) { 105 LIRKind aKind = a.getValueKind(LIRKind.class); 106 LIRKind bKind = b.getValueKind(LIRKind.class); 107 assert a.getPlatformKind() == b.getPlatformKind(); 108 109 if (aKind.isUnknownReference()) { 110 resultKind = aKind; 111 } else if (bKind.isUnknownReference()) { 112 resultKind = bKind; 113 } 114 115 if (aKind.isValue() && bKind.isValue()) { 116 resultKind = aKind; 117 } else if (bKind.isValue()) { 118 if (aKind.isDerivedReference()) { 119 resultKind = aKind; 120 } else { 121 AllocatableValue allocatable = getLIRGen().asAllocatable(a); 122 resultKind = aKind.makeDerivedReference(allocatable); 123 a = allocatable; 124 } 125 } else if (aKind.isDerivedReference() && bKind.isDerivedReference() && aKind.getDerivedReferenceBase().equals(bKind.getDerivedReferenceBase())) { 126 resultKind = LIRKind.value(a.getPlatformKind()); 127 } else { 128 resultKind = aKind.makeUnknownReference(); 129 } 130 } else { 131 resultKind = LIRKind.combine(a, b); 132 } 133 134 return emitSub(resultKind, a, b, setFlags); 135 } 136 }