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