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