1 /*
   2  * Copyright (c) 2015, 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 
  26 package org.graalvm.compiler.core.sparc;
  27 
  28 import org.graalvm.compiler.asm.sparc.SPARCAssembler;
  29 import org.graalvm.compiler.nodes.ValueNode;
  30 import org.graalvm.compiler.nodes.calc.AddNode;
  31 import org.graalvm.compiler.nodes.memory.address.AddressNode;
  32 import org.graalvm.compiler.phases.common.AddressLoweringPhase.AddressLowering;
  33 
  34 import jdk.vm.ci.meta.JavaConstant;
  35 
  36 public class SPARCAddressLowering extends AddressLowering {
  37 
  38     @Override
  39     public AddressNode lower(ValueNode base, ValueNode offset) {
  40         JavaConstant immBase = asImmediate(base);
  41         if (immBase != null && SPARCAssembler.isSimm13(immBase)) {
  42             return lower(offset, immBase.asLong());
  43         }
  44 
  45         JavaConstant immOffset = asImmediate(offset);
  46         if (immOffset != null && SPARCAssembler.isSimm13(immOffset)) {
  47             return lower(base, immOffset.asLong());
  48         }
  49         return base.graph().unique(new SPARCIndexedAddressNode(base, offset));
  50     }
  51 
  52     private AddressNode lower(ValueNode base, long displacement) {
  53         if (base instanceof AddNode) {
  54             AddNode add = (AddNode) base;
  55 
  56             JavaConstant immX = asImmediate(add.getX());
  57             if (immX != null && SPARCAssembler.isSimm13(displacement + immX.asLong())) {
  58                 return lower(add.getY(), displacement + immX.asLong());
  59             }
  60 
  61             JavaConstant immY = asImmediate(add.getY());
  62             if (immY != null && SPARCAssembler.isSimm13(displacement + immY.asLong())) {
  63                 return lower(add.getX(), displacement + immY.asLong());
  64             }
  65 
  66             if (displacement == 0) {
  67                 return lower(add.getX(), add.getY());
  68             }
  69         }
  70 
  71         assert SPARCAssembler.isSimm13(displacement);
  72         return base.graph().unique(new SPARCImmediateAddressNode(base, (int) displacement));
  73     }
  74 
  75     private static JavaConstant asImmediate(ValueNode value) {
  76         JavaConstant c = value.asJavaConstant();
  77         if (c != null && c.getJavaKind().isNumericInteger()) {
  78             return c;
  79         } else {
  80             return null;
  81         }
  82     }
  83 }