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.amd64;
  25 
  26 import jdk.vm.ci.meta.JavaConstant;
  27 
  28 import org.graalvm.compiler.core.common.NumUtil;
  29 import org.graalvm.compiler.asm.amd64.AMD64Address.Scale;
  30 import org.graalvm.compiler.core.common.type.IntegerStamp;
  31 import org.graalvm.compiler.debug.DebugContext;
  32 import org.graalvm.compiler.nodes.ValueNode;
  33 import org.graalvm.compiler.nodes.calc.AddNode;
  34 import org.graalvm.compiler.nodes.calc.LeftShiftNode;
  35 import org.graalvm.compiler.nodes.calc.ZeroExtendNode;
  36 import org.graalvm.compiler.nodes.memory.address.AddressNode;
  37 import org.graalvm.compiler.phases.common.AddressLoweringPhase.AddressLowering;
  38 
  39 public class AMD64AddressLowering extends AddressLowering {
  40 
  41     @Override
  42     public AddressNode lower(ValueNode address) {
  43         return lower(address, null);
  44     }
  45 
  46     @Override
  47     public AddressNode lower(ValueNode base, ValueNode offset) {
  48         AMD64AddressNode ret = new AMD64AddressNode(base, offset);
  49         boolean changed;
  50         do {
  51             changed = improve(base.getDebug(), ret);
  52         } while (changed);
  53         return base.graph().unique(ret);
  54     }
  55 
  56     /**
  57      * @param debug
  58      */
  59     protected boolean improve(DebugContext debug, AMD64AddressNode ret) {
  60         ValueNode newBase = improveInput(ret, ret.getBase(), 0);
  61         if (newBase != ret.getBase()) {
  62             ret.setBase(newBase);
  63             return true;
  64         }
  65 
  66         ValueNode newIdx = improveInput(ret, ret.getIndex(), ret.getScale().log2);
  67         if (newIdx != ret.getIndex()) {
  68             ret.setIndex(newIdx);
  69             return true;
  70         }
  71 
  72         if (ret.getIndex() instanceof LeftShiftNode) {
  73             LeftShiftNode shift = (LeftShiftNode) ret.getIndex();
  74             if (shift.getY().isConstant()) {
  75                 int amount = ret.getScale().log2 + shift.getY().asJavaConstant().asInt();
  76                 Scale scale = Scale.fromShift(amount);
  77                 if (scale != null) {
  78                     ret.setIndex(shift.getX());
  79                     ret.setScale(scale);
  80                     return true;
  81                 }
  82             }
  83         }
  84 
  85         if (ret.getScale() == Scale.Times1) {
  86             if (ret.getBase() == null || ret.getIndex() == null) {
  87                 if (ret.getBase() instanceof AddNode) {
  88                     AddNode add = (AddNode) ret.getBase();
  89                     ret.setBase(add.getX());
  90                     ret.setIndex(add.getY());
  91                     return true;
  92                 } else if (ret.getIndex() instanceof AddNode) {
  93                     AddNode add = (AddNode) ret.getIndex();
  94                     ret.setBase(add.getX());
  95                     ret.setIndex(add.getY());
  96                     return true;
  97                 }
  98             }
  99 
 100             if (ret.getBase() instanceof LeftShiftNode && !(ret.getIndex() instanceof LeftShiftNode)) {
 101                 ValueNode tmp = ret.getBase();
 102                 ret.setBase(ret.getIndex());
 103                 ret.setIndex(tmp);
 104                 return true;
 105             }
 106         }
 107 
 108         return false;
 109     }
 110 
 111     private static ValueNode improveInput(AMD64AddressNode address, ValueNode node, int shift) {
 112         if (node == null) {
 113             return null;
 114         }
 115 
 116         JavaConstant c = node.asJavaConstant();
 117         if (c != null) {
 118             return improveConstDisp(address, node, c, null, shift);
 119         } else {
 120             if (node.stamp() instanceof IntegerStamp && ((IntegerStamp) node.stamp()).getBits() == 64) {
 121                 if (node instanceof ZeroExtendNode) {
 122                     if (((ZeroExtendNode) node).getInputBits() == 32) {
 123                         /*
 124                          * We can just swallow a zero-extend from 32 bit to 64 bit because the upper
 125                          * half of the register will always be zero.
 126                          */
 127                         return ((ZeroExtendNode) node).getValue();
 128                     }
 129                 } else if (node instanceof AddNode) {
 130                     AddNode add = (AddNode) node;
 131                     if (add.getX().isConstant()) {
 132                         return improveConstDisp(address, node, add.getX().asJavaConstant(), add.getY(), shift);
 133                     } else if (add.getY().isConstant()) {
 134                         return improveConstDisp(address, node, add.getY().asJavaConstant(), add.getX(), shift);
 135                     }
 136                 }
 137             }
 138         }
 139 
 140         return node;
 141     }
 142 
 143     private static ValueNode improveConstDisp(AMD64AddressNode address, ValueNode original, JavaConstant c, ValueNode other, int shift) {
 144         if (c.getJavaKind().isNumericInteger()) {
 145             long disp = address.getDisplacement();
 146             disp += c.asLong() << shift;
 147             if (NumUtil.isInt(disp)) {
 148                 address.setDisplacement((int) disp);
 149                 return other;
 150             }
 151         }
 152         return original;
 153     }
 154 }