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     protected abstract Variable emitSub(LIRKind resultKind, Value a, Value b, boolean setFlags);
  54 
  55     @Override
  56     public final Variable emitAdd(Value aVal, Value bVal, boolean setFlags) {
  57         return emitAddOrSub(aVal, bVal, setFlags, true);
  58     }
  59 
  60     @Override
  61     public final Variable emitSub(Value aVal, Value bVal, boolean setFlags) {
  62         return emitAddOrSub(aVal, bVal, setFlags, false);
  63     }
  64 
  65     private Variable emitAddOrSub(Value aVal, Value bVal, boolean setFlags, boolean isAdd) {
  66         LIRKind resultKind;
  67         Value a = aVal;
  68         Value b = bVal;
  69 
  70         if (isNumericInteger(a.getPlatformKind())) {
  71             LIRKind aKind = a.getValueKind(LIRKind.class);
  72             LIRKind bKind = b.getValueKind(LIRKind.class);
  73             assert a.getPlatformKind() == b.getPlatformKind() : a.getPlatformKind() + " vs. " + b.getPlatformKind();
  74 
  75             if (aKind.isUnknownReference()) {
  76                 resultKind = aKind;
  77             } else if (bKind.isUnknownReference()) {
  78                 resultKind = bKind;
  79             } else if (aKind.isValue() && bKind.isValue()) {
  80                 resultKind = aKind;
  81             } else if (aKind.isValue()) {
  82                 if (bKind.isDerivedReference()) {
  83                     resultKind = bKind;
  84                 } else {
  85                     AllocatableValue allocatable = getLIRGen().asAllocatable(b);
  86                     resultKind = bKind.makeDerivedReference(allocatable);
  87                     b = allocatable;
  88                 }
  89             } else if (bKind.isValue()) {
  90                 if (aKind.isDerivedReference()) {
  91                     resultKind = aKind;
  92                 } else {
  93                     AllocatableValue allocatable = getLIRGen().asAllocatable(a);
  94                     resultKind = aKind.makeDerivedReference(allocatable);
  95                     a = allocatable;
  96                 }
  97             } else {
  98                 resultKind = aKind.makeUnknownReference();
  99             }
 100         } else {
 101             resultKind = LIRKind.combine(a, b);
 102         }
 103 
 104         return isAdd ? emitAdd(resultKind, a, b, setFlags) : emitSub(resultKind, a, b, setFlags);
 105     }
 106 
 107 }